* BIG rewrite of header handling and slimming of context_loop and session_loop; shuff...
[citadel.git] / webcit / msg_renderers.c
1 /*----------------------------------------------------------------------------*/
2 #include "webcit.h"
3 #include "webserver.h"
4
5 /**
6  * message index functions
7  */
8
9
10 void DestroyMimeParts(wc_mime_attachment *Mime)
11 {
12         FreeStrBuf(&Mime->Name);
13         FreeStrBuf(&Mime->FileName);
14         FreeStrBuf(&Mime->PartNum);
15         FreeStrBuf(&Mime->Disposition);
16         FreeStrBuf(&Mime->ContentType);
17         FreeStrBuf(&Mime->Charset);
18         FreeStrBuf(&Mime->Data);
19 }
20
21 void DestroyMime(void *vMime)
22 {
23         wc_mime_attachment *Mime = (wc_mime_attachment*)vMime;
24         DestroyMimeParts(Mime);
25         free(Mime);
26 }
27
28 void DestroyMessageSummary(void *vMsg)
29 {
30         message_summary *Msg = (message_summary*) vMsg;
31
32         FreeStrBuf(&Msg->from);
33         FreeStrBuf(&Msg->to);
34         FreeStrBuf(&Msg->subj);
35         FreeStrBuf(&Msg->reply_inreplyto);
36         FreeStrBuf(&Msg->reply_references);
37         FreeStrBuf(&Msg->cccc);
38         FreeStrBuf(&Msg->hnod);
39         FreeStrBuf(&Msg->AllRcpt);
40         FreeStrBuf(&Msg->Room);
41         FreeStrBuf(&Msg->Rfca);
42         FreeStrBuf(&Msg->OtherNode);
43
44         FreeStrBuf(&Msg->reply_to);
45
46         DeleteHash(&Msg->Attachments);  /**< list of Accachments */
47         DeleteHash(&Msg->Submessages);
48         DeleteHash(&Msg->AttachLinks);
49         DeleteHash(&Msg->AllAttach);
50         free(Msg);
51 }
52
53
54
55 void RegisterMsgHdr(const char *HeaderName, long HdrNLen, ExamineMsgHeaderFunc evaluator, int type)
56 {
57         headereval *ev;
58         ev = (headereval*) malloc(sizeof(headereval));
59         ev->evaluator = evaluator;
60         ev->Type = type;
61         Put(MsgHeaderHandler, HeaderName, HdrNLen, ev, NULL);
62 }
63
64 void RegisterMimeRenderer(const char *HeaderName, long HdrNLen, RenderMimeFunc MimeRenderer)
65 {
66         RenderMimeFuncStruct *f;
67
68         f = (RenderMimeFuncStruct*) malloc(sizeof(RenderMimeFuncStruct));
69         f->f = MimeRenderer;
70         Put(MimeRenderHandler, HeaderName, HdrNLen, f, NULL);
71         
72 }
73
74 /*----------------------------------------------------------------------------*/
75
76 /*
77  *  comparator for two longs in descending order.
78  */
79 int longcmp_r(const void *s1, const void *s2) {
80         long l1;
81         long l2;
82
83         l1 = *(long *)GetSearchPayload(s1);
84         l2 = *(long *)GetSearchPayload(s2);
85
86         if (l1 > l2) return(-1);
87         if (l1 < l2) return(+1);
88         return(0);
89 }
90
91 /*
92  *  comparator for longs; descending order.
93  */
94 int qlongcmp_r(const void *s1, const void *s2) {
95         long l1 = (long) s1;
96         long l2 = (long) s2;
97
98         if (l1 > l2) return(-1);
99         if (l1 < l2) return(+1);
100         return(0);
101 }
102
103  
104 /*
105  * comparator for message summary structs by ascending subject.
106  */
107 int summcmp_subj(const void *s1, const void *s2) {
108         message_summary *summ1;
109         message_summary *summ2;
110         
111         summ1 = (message_summary *)GetSearchPayload(s1);
112         summ2 = (message_summary *)GetSearchPayload(s2);
113         return strcasecmp(ChrPtr(summ1->subj), ChrPtr(summ2->subj));
114 }
115
116 /*
117  * comparator for message summary structs by descending subject.
118  */
119 int summcmp_rsubj(const void *s1, const void *s2) {
120         message_summary *summ1;
121         message_summary *summ2;
122         
123         summ1 = (message_summary *)GetSearchPayload(s1);
124         summ2 = (message_summary *)GetSearchPayload(s2);
125         return strcasecmp(ChrPtr(summ2->subj), ChrPtr(summ1->subj));
126 }
127 /*
128  * comparator for message summary structs by descending subject.
129  */
130 int groupchange_subj(const void *s1, const void *s2) {
131         message_summary *summ1;
132         message_summary *summ2;
133         
134         summ1 = (message_summary *)s1;
135         summ2 = (message_summary *)s2;
136         return ChrPtr(summ2->subj)[0] != ChrPtr(summ1->subj)[0];
137 }
138
139 /*
140  * comparator for message summary structs by ascending sender.
141  */
142 int summcmp_sender(const void *s1, const void *s2) {
143         message_summary *summ1;
144         message_summary *summ2;
145         
146         summ1 = (message_summary *)GetSearchPayload(s1);
147         summ2 = (message_summary *)GetSearchPayload(s2);
148         return strcasecmp(ChrPtr(summ1->from), ChrPtr(summ2->from));
149 }
150
151 /*
152  * comparator for message summary structs by descending sender.
153  */
154 int summcmp_rsender(const void *s1, const void *s2) {
155         message_summary *summ1;
156         message_summary *summ2;
157         
158         summ1 = (message_summary *)GetSearchPayload(s1);
159         summ2 = (message_summary *)GetSearchPayload(s2);
160         return strcasecmp(ChrPtr(summ2->from), ChrPtr(summ1->from));
161 }
162 /*
163  * comparator for message summary structs by descending sender.
164  */
165 int groupchange_sender(const void *s1, const void *s2) {
166         message_summary *summ1;
167         message_summary *summ2;
168         
169         summ1 = (message_summary *)s1;
170         summ2 = (message_summary *)s2;
171         return strcasecmp(ChrPtr(summ2->from), ChrPtr(summ1->from)) != 0;
172
173 }
174
175 /*
176  * comparator for message summary structs by ascending date.
177  */
178 int summcmp_date(const void *s1, const void *s2) {
179         message_summary *summ1;
180         message_summary *summ2;
181         
182         summ1 = (message_summary *)GetSearchPayload(s1);
183         summ2 = (message_summary *)GetSearchPayload(s2);
184
185         if (summ1->date < summ2->date) return -1;
186         else if (summ1->date > summ2->date) return +1;
187         else return 0;
188 }
189
190 /*
191  * comparator for message summary structs by descending date.
192  */
193 int summcmp_rdate(const void *s1, const void *s2) {
194         message_summary *summ1;
195         message_summary *summ2;
196         
197         summ1 = (message_summary *)GetSearchPayload(s1);
198         summ2 = (message_summary *)GetSearchPayload(s2);
199
200         if (summ1->date < summ2->date) return +1;
201         else if (summ1->date > summ2->date) return -1;
202         else return 0;
203 }
204
205 /*
206  * comparator for message summary structs by descending date.
207  */
208 const long DAYSECONDS = 24 * 60 * 60;
209 int groupchange_date(const void *s1, const void *s2) {
210         message_summary *summ1;
211         message_summary *summ2;
212         
213         summ1 = (message_summary *)s1;
214         summ2 = (message_summary *)s2;
215
216         return (summ1->date % DAYSECONDS) != (summ2->date %DAYSECONDS);
217 }
218
219
220 /*----------------------------------------------------------------------------*/
221 /* Don't wanna know... or? */
222 void examine_pref(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset) {return;}
223 void examine_suff(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset) {return;}
224 void examine_path(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset) {return;}
225 void examine_content_encoding(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
226 {
227 /* TODO: do we care? */
228 }
229
230 void examine_nhdr(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
231 {
232         Msg->nhdr = 0;
233         if (!strncasecmp(ChrPtr(HdrLine), "yes", 8))
234                 Msg->nhdr = 1;
235 }
236 int Conditional_ANONYMOUS_MESSAGE(StrBuf *Target, WCTemplputParams *TP)
237 {
238         message_summary *Msg = (message_summary*) CTX;
239         return Msg->nhdr != 0;
240 }
241
242 void examine_type(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
243 {
244         Msg->format_type = StrToi(HdrLine);
245                         
246 }
247
248 void examine_from(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
249 {
250         FreeStrBuf(&Msg->from);
251         Msg->from = NewStrBufPlain(NULL, StrLength(HdrLine));
252         StrBuf_RFC822_to_Utf8(Msg->from, HdrLine, WC->DefaultCharset, FoundCharset);
253 }
254 void tmplput_MAIL_SUMM_FROM(StrBuf *Target, WCTemplputParams *TP)
255 {
256         message_summary *Msg = (message_summary*) CTX;
257         StrBufAppendTemplate(Target, TP, Msg->from, 0);
258 }
259
260
261
262 void examine_subj(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
263 {
264         FreeStrBuf(&Msg->subj);
265         Msg->subj = NewStrBufPlain(NULL, StrLength(HdrLine));
266         StrBuf_RFC822_to_Utf8(Msg->subj, HdrLine, WC->DefaultCharset, FoundCharset);
267 }
268 void tmplput_MAIL_SUMM_SUBJECT(StrBuf *Target, WCTemplputParams *TP)
269 {/*////TODO: Fwd: and RE: filter!!*/
270
271         message_summary *Msg = (message_summary*) CTX;
272         StrBufAppendTemplate(Target, TP, Msg->subj, 0);
273 }
274 int Conditional_MAIL_SUMM_SUBJECT(StrBuf *Target, WCTemplputParams *TP)
275 {
276         message_summary *Msg = (message_summary*) CTX;
277         return StrLength(Msg->subj) > 0;
278 }
279
280
281 void examine_msgn(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
282 {
283         FreeStrBuf(&Msg->reply_inreplyto);
284         Msg->reply_inreplyto = NewStrBufPlain(NULL, StrLength(HdrLine));
285         StrBuf_RFC822_to_Utf8(Msg->reply_inreplyto, HdrLine, WC->DefaultCharset, FoundCharset);
286 }
287 void tmplput_MAIL_SUMM_INREPLYTO(StrBuf *Target, WCTemplputParams *TP)
288 {
289         message_summary *Msg = (message_summary*) CTX;
290         StrBufAppendTemplate(Target, TP, Msg->reply_inreplyto, 0);
291 }
292
293 int Conditional_MAIL_SUMM_UNREAD(StrBuf *Target, WCTemplputParams *TP)
294 {
295         message_summary *Msg = (message_summary*) CTX;
296         return Msg->is_new != 0;
297 }
298
299 void examine_wefw(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
300 {
301         FreeStrBuf(&Msg->reply_references);
302         Msg->reply_references = NewStrBufPlain(NULL, StrLength(HdrLine));
303         StrBuf_RFC822_to_Utf8(Msg->reply_references, HdrLine, WC->DefaultCharset, FoundCharset);
304 }
305 void tmplput_MAIL_SUMM_REFIDS(StrBuf *Target, WCTemplputParams *TP)
306 {
307         message_summary *Msg = (message_summary*) CTX;
308         StrBufAppendTemplate(Target, TP, Msg->reply_references, 0);
309 }
310
311
312 void examine_cccc(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
313 {
314         FreeStrBuf(&Msg->cccc);
315         Msg->cccc = NewStrBufPlain(NULL, StrLength(HdrLine));
316         StrBuf_RFC822_to_Utf8(Msg->cccc, HdrLine, WC->DefaultCharset, FoundCharset);
317         if (Msg->AllRcpt == NULL)
318                 Msg->AllRcpt = NewStrBufPlain(NULL, StrLength(HdrLine));
319         if (StrLength(Msg->AllRcpt) > 0) {
320                 StrBufAppendBufPlain(Msg->AllRcpt, HKEY(", "), 0);
321         }
322         StrBufAppendBuf(Msg->AllRcpt, Msg->cccc, 0);
323 }
324 void tmplput_MAIL_SUMM_CCCC(StrBuf *Target, WCTemplputParams *TP)
325 {
326         message_summary *Msg = (message_summary*) CTX;
327         StrBufAppendTemplate(Target, TP, Msg->cccc, 0);
328 }
329
330
331 void examine_room(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
332 {
333         if ((StrLength(HdrLine) > 0) &&
334             (strcasecmp(ChrPtr(HdrLine), ChrPtr(WC->wc_roomname)))) {
335                 FreeStrBuf(&Msg->Room);
336                 Msg->Room = NewStrBufDup(HdrLine);              
337         }
338 }
339 void tmplput_MAIL_SUMM_ORGROOM(StrBuf *Target, WCTemplputParams *TP)
340 {
341         message_summary *Msg = (message_summary*) CTX;
342         StrBufAppendTemplate(Target, TP, Msg->Room, 0);
343 }
344
345
346 void examine_rfca(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
347 {
348         FreeStrBuf(&Msg->Rfca);
349         Msg->Rfca = NewStrBufDup(HdrLine);
350 }
351 void tmplput_MAIL_SUMM_RFCA(StrBuf *Target, WCTemplputParams *TP)
352 {
353         message_summary *Msg = (message_summary*) CTX;
354         StrBufAppendTemplate(Target, TP, Msg->Rfca, 0);
355 }
356 int Conditional_MAIL_SUMM_RFCA(StrBuf *Target, WCTemplputParams *TP)
357 {
358         message_summary *Msg = (message_summary*) CTX;
359         return StrLength(Msg->Rfca) > 0;
360 }
361 int Conditional_MAIL_SUMM_CCCC(StrBuf *Target, WCTemplputParams *TP)
362 {
363         message_summary *Msg = (message_summary*) CTX;
364         return StrLength(Msg->cccc) > 0;
365 }
366
367 void examine_node(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
368 {
369         wcsession *WCC = WC;
370
371         if ( (StrLength(HdrLine) > 0) &&
372              ((WC->room_flags & QR_NETWORK)
373               || ((strcasecmp(ChrPtr(HdrLine), ChrPtr(WCC->serv_info->serv_nodename))
374                    && (strcasecmp(ChrPtr(HdrLine), ChrPtr(WCC->serv_info->serv_fqdn))))))) {
375                 FreeStrBuf(&Msg->OtherNode);
376                 Msg->OtherNode = NewStrBufDup(HdrLine);
377         }
378 }
379 void tmplput_MAIL_SUMM_OTHERNODE(StrBuf *Target, WCTemplputParams *TP)
380 {
381         message_summary *Msg = (message_summary*) CTX;
382         StrBufAppendTemplate(Target, TP, Msg->OtherNode, 0);
383 }
384 int Conditional_MAIL_SUMM_OTHERNODE(StrBuf *Target, WCTemplputParams *TP)
385 {
386         message_summary *Msg = (message_summary*) CTX;
387         return StrLength(Msg->OtherNode) > 0;
388 }
389
390
391 void examine_rcpt(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
392 {
393         FreeStrBuf(&Msg->to);
394         Msg->to = NewStrBufPlain(NULL, StrLength(HdrLine));
395         StrBuf_RFC822_to_Utf8(Msg->to, HdrLine, WC->DefaultCharset, FoundCharset);
396         if (Msg->AllRcpt == NULL)
397                 Msg->AllRcpt = NewStrBufPlain(NULL, StrLength(HdrLine));
398         if (StrLength(Msg->AllRcpt) > 0) {
399                 StrBufAppendBufPlain(Msg->AllRcpt, HKEY(", "), 0);
400         }
401         StrBufAppendBuf(Msg->AllRcpt, Msg->to, 0);
402 }
403 void tmplput_MAIL_SUMM_TO(StrBuf *Target, WCTemplputParams *TP)
404 {
405         message_summary *Msg = (message_summary*) CTX;
406         StrBufAppendTemplate(Target, TP, Msg->to, 0);
407 }
408 int Conditional_MAIL_SUMM_TO(StrBuf *Target, WCTemplputParams *TP) 
409 {
410         message_summary *Msg = (message_summary*) CTX;
411         return StrLength(Msg->to) != 0;
412 }
413 int Conditional_MAIL_SUMM_SUBJ(StrBuf *Target, WCTemplputParams *TP) 
414 {
415         message_summary *Msg = (message_summary*) CTX;
416         return StrLength(Msg->subj) != 0;
417 }
418 void tmplput_MAIL_SUMM_ALLRCPT(StrBuf *Target, WCTemplputParams *TP)
419 {
420         message_summary *Msg = (message_summary*) CTX;
421         StrBufAppendTemplate(Target, TP, Msg->AllRcpt, 0);
422 }
423
424
425
426 void tmplput_SUMM_COUNT(StrBuf *Target, WCTemplputParams *TP)
427 {
428         StrBufAppendPrintf(Target, "%d", GetCount( WC->summ));
429 }
430
431 HashList *iterate_get_mailsumm_All(StrBuf *Target, WCTemplputParams *TP)
432 {
433         return WC->summ;
434 }
435 void examine_time(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
436 {
437         Msg->date = StrTol(HdrLine);
438 }
439
440 void tmplput_MAIL_SUMM_DATE_BRIEF(StrBuf *Target, WCTemplputParams *TP)
441 {
442         char datebuf[64];
443         message_summary *Msg = (message_summary*) CTX;
444         webcit_fmt_date(datebuf, 64, Msg->date, DATEFMT_BRIEF);
445         StrBufAppendBufPlain(Target, datebuf, -1, 0);
446 }
447
448 void tmplput_MAIL_SUMM_DATE_FULL(StrBuf *Target, WCTemplputParams *TP)
449 {
450         char datebuf[64];
451         message_summary *Msg = (message_summary*) CTX;
452         webcit_fmt_date(datebuf, 64, Msg->date, DATEFMT_FULL);
453         StrBufAppendBufPlain(Target, datebuf, -1, 0);
454 }
455 void tmplput_MAIL_SUMM_DATE_NO(StrBuf *Target, WCTemplputParams *TP)
456 {
457         message_summary *Msg = (message_summary*) CTX;
458         StrBufAppendPrintf(Target, "%ld", Msg->date, 0);
459 }
460
461
462
463 void render_MAIL(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
464 {
465         const StrBuf *TemplateMime;
466
467         Mime->Data = NewStrBufPlain(NULL, Mime->length);
468         read_message(Mime->Data, HKEY("view_submessage"), Mime->msgnum, Mime->PartNum, &TemplateMime);
469 /*
470         if ( (!IsEmptyStr(mime_submessages)) && (!section[0]) ) {
471                 for (i=0; i<num_tokens(mime_submessages, '|'); ++i) {
472                         extract_token(buf, mime_submessages, i, '|', sizeof buf);
473                         / ** use printable_view to suppress buttons * /
474                         wprintf("<blockquote>");
475                         read_message(Mime->msgnum, 1, ChrPtr(Mime->Section));
476                         wprintf("</blockquote>");
477                 }
478         }
479 */
480 }
481
482 void render_MIME_VCard(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
483 {
484         wcsession *WCC = WC;
485         MimeLoadData(Mime);
486         if (StrLength(Mime->Data) > 0) {
487                 StrBuf *Buf;
488                 Buf = NewStrBuf();
489                 /** If it's my vCard I can edit it */
490                 if (    (!strcasecmp(ChrPtr(WCC->wc_roomname), USERCONFIGROOM))
491                         || (!strcasecmp(&(ChrPtr(WCC->wc_roomname)[11]), USERCONFIGROOM))
492                         || (WC->wc_view == VIEW_ADDRESSBOOK)
493                         ) {
494                         StrBufAppendPrintf(Buf, "<a href=\"edit_vcard?msgnum=%ld&partnum=%s\">",
495                                 Mime->msgnum, ChrPtr(Mime->PartNum));
496                         StrBufAppendPrintf(Buf, "[%s]</a>", _("edit"));
497                 }
498
499                 /* In all cases, display the full card */
500                 display_vcard(Buf, Mime->Data, 0, 1, NULL, Mime->msgnum);
501                 FreeStrBuf(&Mime->Data);
502                 Mime->Data = Buf;
503         }
504
505 }
506
507 void render_MIME_VNote(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
508 {
509         MimeLoadData(Mime);
510         if (StrLength(Mime->Data) > 0) {
511                 struct vnote *v;
512                 StrBuf *Buf;
513
514                 Buf = NewStrBuf();
515                 v = vnote_new_from_str(ChrPtr(Mime->Data));
516                 if (v) {
517                         WCTemplputParams TP;
518                         
519                         memset(&TP, 0, sizeof(WCTemplputParams));
520                         TP.Filter.ContextType = CTX_VNOTE;
521                         TP.Context = v;
522                         DoTemplate(HKEY("mail_vnoteitem"),
523                                    Buf, &TP);
524                         
525                         vnote_free(v);
526                         
527                         FreeStrBuf(&Mime->Data);
528                         Mime->Data = Buf;
529                 }
530                 else
531                         FlushStrBuf(Mime->Data);
532         }
533
534 }
535
536 void render_MIME_ICS(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
537 {
538         if (StrLength(Mime->Data) == 0) {
539                 MimeLoadData(Mime);
540         }
541         if (StrLength(Mime->Data) > 0) {
542                 cal_process_attachment(Mime);
543         }
544 }
545
546
547
548 void examine_mime_part(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
549 {
550         wc_mime_attachment *Mime;
551         StrBuf *Buf;
552         
553         Mime = (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
554         memset(Mime, 0, sizeof(wc_mime_attachment));
555         Mime->msgnum = Msg->msgnum;
556         Buf = NewStrBuf();
557
558         Mime->Name = NewStrBuf();
559         StrBufExtract_token(Buf, HdrLine, 0, '|');
560         StrBuf_RFC822_to_Utf8(Mime->Name, Buf, WC->DefaultCharset, FoundCharset);
561         StrBufTrim(Mime->Name);
562
563         StrBufExtract_token(Buf, HdrLine, 1, '|');
564         Mime->FileName = NewStrBuf();
565         StrBuf_RFC822_to_Utf8(Mime->FileName, Buf, WC->DefaultCharset, FoundCharset);
566         StrBufTrim(Mime->FileName);
567
568         Mime->PartNum = NewStrBuf();
569         StrBufExtract_token(Mime->PartNum, HdrLine, 2, '|');
570         StrBufTrim(Mime->PartNum);
571         if (strchr(ChrPtr(Mime->PartNum), '.') != NULL) 
572                 Mime->level = 2;
573         else
574                 Mime->level = 1;
575
576         Mime->Disposition = NewStrBuf();
577         StrBufExtract_token(Mime->Disposition, HdrLine, 3, '|');
578
579         Mime->ContentType = NewStrBuf();
580         StrBufExtract_token(Mime->ContentType, HdrLine, 4, '|');
581         StrBufTrim(Mime->ContentType);
582         StrBufLowerCase(Mime->ContentType);
583
584         if (!strcmp(ChrPtr(Mime->ContentType), "application/octet-stream")) {
585                 StrBufPlain(Mime->ContentType, 
586                             GuessMimeByFilename(SKEY(Mime->FileName)), -1);
587         }
588         Mime->length = StrBufExtract_int(HdrLine, 5, '|');
589
590         if ( (StrLength(Mime->FileName) == 0) && (StrLength(Mime->Name) > 0) ) {
591                 StrBufAppendBuf(Mime->FileName, Mime->Name, 0);
592         }
593
594         if (StrLength(Msg->PartNum) > 0) {
595                 StrBuf *tmp;
596                 StrBufPrintf(Buf, "%s.%s", ChrPtr(Msg->PartNum), ChrPtr(Mime->PartNum));
597                 tmp = Mime->PartNum;
598                 Mime->PartNum = Buf;
599                 Buf = tmp;
600         }
601
602         if (Msg->AllAttach == NULL)
603                 Msg->AllAttach = NewHash(1,NULL);
604         Put(Msg->AllAttach, SKEY(Mime->PartNum), Mime, DestroyMime);
605         FreeStrBuf(&Buf);
606 }
607
608
609 void evaluate_mime_part(message_summary *Msg, wc_mime_attachment *Mime)
610 {
611         void *vMimeRenderer;
612
613         /* just print the root-node */
614         if ((Mime->level == 1) &&
615             GetHash(MimeRenderHandler, SKEY(Mime->ContentType), &vMimeRenderer) &&
616             vMimeRenderer != NULL)
617         {
618                 Mime->Renderer = (RenderMimeFuncStruct*) vMimeRenderer;
619                 if (Msg->Submessages == NULL)
620                         Msg->Submessages = NewHash(1,NULL);
621                 Put(Msg->Submessages, SKEY(Mime->PartNum), Mime, reference_free_handler);
622         }
623         else if ((Mime->level == 1) &&
624                  (!strcasecmp(ChrPtr(Mime->Disposition), "inline"))
625                  && (!strncasecmp(ChrPtr(Mime->ContentType), "image/", 6)) ){
626                 if (Msg->AttachLinks == NULL)
627                         Msg->AttachLinks = NewHash(1,NULL);
628                 Put(Msg->AttachLinks, SKEY(Mime->PartNum), Mime, reference_free_handler);
629         }
630         else if ((Mime->level == 1) &&
631                  (StrLength(Mime->ContentType) > 0) &&
632                   ( (!strcasecmp(ChrPtr(Mime->Disposition), "attachment")) 
633                     || (!strcasecmp(ChrPtr(Mime->Disposition), "inline"))
634                     || (!strcasecmp(ChrPtr(Mime->Disposition), ""))))
635         {               
636                 if (Msg->AttachLinks == NULL)
637                         Msg->AttachLinks = NewHash(1,NULL);
638                 Put(Msg->AttachLinks, SKEY(Mime->PartNum), Mime, reference_free_handler);
639                 if ((strcasecmp(ChrPtr(Mime->ContentType), "application/octet-stream") == 0) && 
640                     (StrLength(Mime->FileName) > 0)) {
641                         FlushStrBuf(Mime->ContentType);
642                         StrBufAppendBufPlain(Mime->ContentType,
643                                              GuessMimeByFilename(SKEY(Mime->FileName)),
644                                              -1, 0);
645                 }
646         }
647 }
648
649 void tmplput_MAIL_SUMM_NATTACH(StrBuf *Target, WCTemplputParams *TP)
650 {
651         message_summary *Msg = (message_summary*) CTX;
652         StrBufAppendPrintf(Target, "%ld", GetCount(Msg->Attachments));
653 }
654
655
656 void examine_hnod(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
657 {
658         FreeStrBuf(&Msg->hnod);
659         Msg->hnod = NewStrBufPlain(NULL, StrLength(HdrLine));
660         StrBuf_RFC822_to_Utf8(Msg->hnod, HdrLine, WC->DefaultCharset, FoundCharset);
661 }
662 void tmplput_MAIL_SUMM_H_NODE(StrBuf *Target, WCTemplputParams *TP)
663 {
664         message_summary *Msg = (message_summary*) CTX;
665         StrBufAppendTemplate(Target, TP, Msg->hnod, 0);
666 }
667 int Conditional_MAIL_SUMM_H_NODE(StrBuf *Target, WCTemplputParams *TP)
668 {
669         message_summary *Msg = (message_summary*) CTX;
670         return StrLength(Msg->hnod) > 0;
671 }
672
673
674
675 void examine_text(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
676 {
677         Msg->MsgBody->Data = NewStrBuf();
678 }
679
680 void examine_msg4_partnum(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
681 {
682         Msg->MsgBody->PartNum = NewStrBufDup(HdrLine);
683         StrBufTrim(Msg->MsgBody->PartNum);
684 }
685
686 void examine_content_lengh(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
687 {
688         Msg->MsgBody->length = StrTol(HdrLine);
689         Msg->MsgBody->size_known = 1;
690 }
691
692 void examine_content_type(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
693 {
694         void *vHdr;
695         headereval *Hdr;
696         StrBuf *Token;
697         StrBuf *Value;
698         const char* sem;
699         const char *eq;
700         int len;
701         StrBufTrim(HdrLine);
702         Msg->MsgBody->ContentType = NewStrBufDup(HdrLine);
703         sem = strchr(ChrPtr(HdrLine), ';');
704
705         if (sem != NULL) {
706                 Token = NewStrBufPlain(NULL, StrLength(HdrLine));
707                 Value = NewStrBufPlain(NULL, StrLength(HdrLine));
708                 len = sem - ChrPtr(HdrLine);
709                 StrBufCutAt(Msg->MsgBody->ContentType, len, NULL);
710                 while (sem != NULL) {
711                         while (isspace(*(sem + 1)))
712                                 sem ++;
713                         StrBufCutLeft(HdrLine, sem - ChrPtr(HdrLine));
714                         sem = strchr(ChrPtr(HdrLine), ';');
715                         if (sem != NULL)
716                                 len = sem - ChrPtr(HdrLine);
717                         else
718                                 len = StrLength(HdrLine);
719                         FlushStrBuf(Token);
720                         FlushStrBuf(Value);
721                         StrBufAppendBufPlain(Token, ChrPtr(HdrLine), len, 0);
722                         eq = strchr(ChrPtr(Token), '=');
723                         if (eq != NULL) {
724                                 len = eq - ChrPtr(Token);
725                                 StrBufAppendBufPlain(Value, eq + 1, StrLength(Token) - len - 1, 0); 
726                                 StrBufCutAt(Token, len, NULL);
727                                 StrBufTrim(Value);
728                         }
729                         StrBufTrim(Token);
730
731                         if (GetHash(MsgHeaderHandler, SKEY(Token), &vHdr) &&
732                             (vHdr != NULL)) {
733                                 Hdr = (headereval*)vHdr;
734                                 Hdr->evaluator(Msg, Value, FoundCharset);
735                         }
736                         else lprintf(1, "don't know how to handle content type sub-header[%s]\n", ChrPtr(Token));
737                 }
738                 FreeStrBuf(&Token);
739                 FreeStrBuf(&Value);
740         }
741 }
742
743 void tmplput_MAIL_SUMM_N(StrBuf *Target, WCTemplputParams *TP)
744 {
745         message_summary *Msg = (message_summary*) CTX;
746         StrBufAppendPrintf(Target, "%ld", Msg->msgnum);
747 }
748
749
750
751 int Conditional_MAIL_MIME_ALL(StrBuf *Target, WCTemplputParams *TP)
752 {
753         message_summary *Msg = (message_summary*) CTX;
754         return GetCount(Msg->Attachments) > 0;
755 }
756
757 int Conditional_MAIL_MIME_SUBMESSAGES(StrBuf *Target, WCTemplputParams *TP)
758 {
759         message_summary *Msg = (message_summary*) CTX;
760         return GetCount(Msg->Submessages) > 0;
761 }
762
763 int Conditional_MAIL_MIME_ATTACHLINKS(StrBuf *Target, WCTemplputParams *TP)
764 {
765         message_summary *Msg = (message_summary*) CTX;
766         return GetCount(Msg->AttachLinks) > 0;
767 }
768
769 int Conditional_MAIL_MIME_ATTACH(StrBuf *Target, WCTemplputParams *TP)
770 {
771         message_summary *Msg = (message_summary*) CTX;
772         return GetCount(Msg->AllAttach) > 0;
773 }
774
775
776
777 /*----------------------------------------------------------------------------*/
778 void tmplput_QUOTED_MAIL_BODY(StrBuf *Target, WCTemplputParams *TP)
779 {
780         const StrBuf *Mime;
781         long MsgNum;
782         StrBuf *Buf;
783
784         MsgNum = LBstr(TKEY(0));
785         Buf = NewStrBuf();
786         read_message(Buf, HKEY("view_message_replyquote"), MsgNum, NULL, &Mime);
787         StrBufAppendTemplate(Target, TP, Buf, 1);
788         FreeStrBuf(&Buf);
789 }
790
791 void tmplput_MAIL_BODY(StrBuf *Target, WCTemplputParams *TP)
792 {
793         message_summary *Msg = (message_summary*) CTX;
794         StrBufAppendTemplate(Target, TP, Msg->MsgBody->Data, 0);
795 }
796
797
798 void render_MAIL_variformat(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
799 {
800         /* Messages in legacy Citadel variformat get handled thusly... */
801         StrBuf *Target = NewStrBufPlain(NULL, StrLength(Mime->Data));
802         FmOut(Target, "JUSTIFY", Mime->Data);
803         FreeStrBuf(&Mime->Data);
804         Mime->Data = Target;
805 }
806
807 void render_MAIL_text_plain(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
808 {
809         StrBuf *cs = NULL;
810         const char *ptr, *pte;
811         const char *BufPtr = NULL;
812         StrBuf *Line;
813         StrBuf *Line1;
814         StrBuf *Line2;
815         StrBuf *Target;
816
817         int ConvertIt = 1;
818         int bn = 0;
819         int bq = 0;
820         int i, n, done = 0;
821         long len;
822 #ifdef HAVE_ICONV
823         iconv_t ic = (iconv_t)(-1) ;
824 #endif
825
826         if ((StrLength(Mime->Data) == 0) && (Mime->length > 0)) {
827                 FreeStrBuf(&Mime->Data);
828                 MimeLoadData(Mime);
829         }
830
831         /* Boring old 80-column fixed format text gets handled this way... */
832         if ((strcasecmp(ChrPtr(Mime->Charset), "us-ascii") == 0) &&
833             (strcasecmp(ChrPtr(Mime->Charset), "UTF-8") == 0))
834                 ConvertIt = 0;
835
836 #ifdef HAVE_ICONV
837         if (ConvertIt) {
838                 if (StrLength(Mime->Charset) != 0)
839                         cs = Mime->Charset;
840                 else if (StrLength(FoundCharset) > 0)
841                         cs = FoundCharset;
842                 else if (StrLength(WC->DefaultCharset) > 0)
843                         cs = WC->DefaultCharset;
844                 if (cs == NULL) {
845                         ConvertIt = 0;
846                 }
847                 else {
848                         ctdl_iconv_open("UTF-8", ChrPtr(cs), &ic);
849                         if (ic == (iconv_t)(-1) ) {
850                                 lprintf(5, "%s:%d iconv_open(UTF-8, %s) failed: %s\n",
851                                         __FILE__, __LINE__, ChrPtr(Mime->Charset), strerror(errno));
852                         }
853                 }
854         }
855 #endif
856         Line = NewStrBuf();
857         Line1 = NewStrBuf();
858         Line2 = NewStrBuf();
859         Target = NewStrBufPlain(NULL, StrLength(Mime->Data));
860
861         while ((n = StrBufSipLine(Line, Mime->Data, &BufPtr), n >= 0) && !done)
862         {
863                 done = n == 0;
864                 bq = 0;
865                 i = 0;
866                 ptr = ChrPtr(Line);
867                 len = StrLength(Line);
868                 pte = ptr + len;
869                 
870                 while ((ptr < pte) &&
871                        ((*ptr == '>') ||
872                         isspace(*ptr)))
873                 {
874                         if (*ptr == '>')
875                                 bq++;
876                         ptr ++;
877                         i++;
878                 }
879                 if (i > 0) StrBufCutLeft(Line, i);
880                 
881                 if (StrLength(Line) == 0) {
882                         StrBufAppendBufPlain(Target, HKEY("<tt></tt><br />\n"), 0);
883                         continue;
884                 }
885
886                 for (i = bn; i < bq; i++)                               
887                         StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
888                 for (i = bq; i < bn; i++)                               
889                         StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
890
891                 if (ConvertIt == 1) {
892                         StrBufConvert(Line, Line1, &ic);
893                 }
894
895                 StrBufAppendBufPlain(Target, HKEY("<tt>"), 0);
896                 UrlizeText(Line1, Line, Line2);
897
898                 StrEscAppend(Target, Line1, NULL, 0, 0);
899                 StrBufAppendBufPlain(Target, HKEY("</tt><br />\n"), 0);
900                 bn = bq;
901         }
902
903         for (i = 0; i < bn; i++)                                
904                 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
905
906         StrBufAppendBufPlain(Target, HKEY("</i><br />"), 0);
907 #ifdef HAVE_ICONV
908         if (ic != (iconv_t)(-1) ) {
909                 iconv_close(ic);
910         }
911 #endif
912
913         FreeStrBuf(&Mime->Data);
914         Mime->Data = Target;
915         FlushStrBuf(Mime->ContentType);
916         StrBufAppendBufPlain(Mime->ContentType, HKEY("text/html"), 0);
917         FlushStrBuf(Mime->Charset);
918         StrBufAppendBufPlain(Mime->Charset, HKEY("UTF-8"), 0);
919         FreeStrBuf(&Line);
920         FreeStrBuf(&Line1);
921         FreeStrBuf(&Line2);
922 }
923
924 void render_MAIL_html(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
925 {
926         StrBuf *Buf;
927
928         if (StrLength(Mime->Data) == 0)
929                 return;
930
931         Buf = NewStrBufPlain(NULL, StrLength(Mime->Data));
932
933         /* HTML is fun, but we've got to strip it first */
934         output_html(ChrPtr(Mime->Charset), 
935                     (WC->wc_view == VIEW_WIKI ? 1 : 0), 
936                     Mime->msgnum,
937                     Mime->Data, Buf);
938         FreeStrBuf(&Mime->Data);
939         Mime->Data = Buf;
940 }
941
942 void render_MAIL_UNKNOWN(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
943 {
944         /* Unknown weirdness */
945         FlushStrBuf(Mime->Data);
946         StrBufAppendBufPlain(Mime->Data, _("I don't know how to display "), -1, 0);
947         StrBufAppendBuf(Mime->Data, Mime->ContentType, 0);
948         StrBufAppendBufPlain(Mime->Data, HKEY("<br />\n"), 0);
949 }
950
951
952
953
954
955
956 HashList *iterate_get_mime_All(StrBuf *Target, WCTemplputParams *TP)
957 {
958         message_summary *Msg = (message_summary*) CTX;
959         return Msg->Attachments;
960 }
961 HashList *iterate_get_mime_Submessages(StrBuf *Target, WCTemplputParams *TP)
962 {
963         message_summary *Msg = (message_summary*) CTX;
964         return Msg->Submessages;
965 }
966 HashList *iterate_get_mime_AttachLinks(StrBuf *Target, WCTemplputParams *TP)
967 {
968         message_summary *Msg = (message_summary*) CTX;
969         return Msg->AttachLinks;
970 }
971 HashList *iterate_get_mime_Attachments(StrBuf *Target, WCTemplputParams *TP)
972 {
973         message_summary *Msg = (message_summary*) CTX;
974         return Msg->AllAttach;
975 }
976
977 void tmplput_MIME_Name(StrBuf *Target, WCTemplputParams *TP)
978 {
979         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
980         StrBufAppendTemplate(Target, TP, mime->Name, 0);
981 }
982
983 void tmplput_MIME_FileName(StrBuf *Target, WCTemplputParams *TP)
984 {
985         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
986         StrBufAppendTemplate(Target, TP, mime->FileName, 0);
987 }
988
989 void tmplput_MIME_PartNum(StrBuf *Target, WCTemplputParams *TP)
990 {
991         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
992         StrBufAppendTemplate(Target, TP, mime->PartNum, 0);
993 }
994
995 void tmplput_MIME_MsgNum(StrBuf *Target, WCTemplputParams *TP)
996 {
997         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
998         StrBufAppendPrintf(Target, "%ld", mime->msgnum);
999 }
1000
1001 void tmplput_MIME_Disposition(StrBuf *Target, WCTemplputParams *TP)
1002 {
1003         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
1004         StrBufAppendTemplate(Target, TP, mime->Disposition, 0);
1005 }
1006
1007 void tmplput_MIME_ContentType(StrBuf *Target, WCTemplputParams *TP)
1008 {
1009         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
1010         StrBufAppendTemplate(Target, TP, mime->ContentType, 0);
1011 }
1012
1013 void examine_charset(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
1014 {
1015         Msg->MsgBody->Charset = NewStrBufDup(HdrLine);
1016 }
1017
1018 void tmplput_MIME_Charset(StrBuf *Target, WCTemplputParams *TP)
1019 {
1020         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
1021         StrBufAppendTemplate(Target, TP, mime->Charset, 0);
1022 }
1023
1024 void tmplput_MIME_Data(StrBuf *Target, WCTemplputParams *TP)
1025 {
1026         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
1027         if (mime->Renderer != NULL)
1028                 mime->Renderer->f(mime, NULL, NULL);
1029         StrBufAppendTemplate(Target, TP, mime->Data, 0);
1030         /* TODO: check whether we need to load it now? */
1031 }
1032
1033 void tmplput_MIME_LoadData(StrBuf *Target, WCTemplputParams *TP)
1034 {
1035         wcsession *WCC = WC;    
1036         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
1037         wc_mime_attachment *att;
1038         
1039         if ( (!strcasecmp(ChrPtr(mime->Disposition), "inline"))||
1040              (!strcasecmp(ChrPtr(mime->Disposition), "attachment")) ) 
1041         {
1042                 
1043                 int n;
1044                 char N[64];
1045                 /* steal this mime part... */
1046                 att = malloc(sizeof(wc_mime_attachment));
1047                 memcpy(att, mime, sizeof(wc_mime_attachment));
1048                 memset(mime, 0, sizeof(wc_mime_attachment));
1049
1050                 if (att->Data == NULL) 
1051                         MimeLoadData(att);
1052
1053                 if (WCC->attachments == NULL)
1054                         WCC->attachments = NewHash(1, NULL);
1055                 /* And add it to the list. */
1056                 n = snprintf(N, sizeof N, "%d", GetCount(WCC->attachments) + 1);
1057                 Put(WCC->attachments, N, n, att, DestroyMime);
1058         }
1059 }
1060
1061 void tmplput_MIME_Length(StrBuf *Target, WCTemplputParams *TP)
1062 {
1063         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
1064         StrBufAppendPrintf(Target, "%ld", mime->length);
1065 }
1066
1067 HashList *iterate_get_registered_Attachments(StrBuf *Target, WCTemplputParams *TP)
1068 {
1069         return WC->attachments;
1070 }
1071
1072 void servcmd_do_search(char *buf, long bufsize)
1073 {
1074         snprintf(buf, bufsize, "MSGS SEARCH|%s", bstr("query"));
1075 }
1076
1077 void servcmd_headers(char *buf, long bufsize)
1078 {
1079         snprintf(buf, bufsize, "MSGS ALL");
1080 }
1081
1082 void servcmd_readfwd(char *buf, long bufsize)
1083 {
1084         snprintf(buf, bufsize, "MSGS ALL");
1085 }
1086
1087 void servcmd_readnew(char *buf, long bufsize)
1088 {
1089         snprintf(buf, bufsize, "MSGS NEW");
1090 }
1091
1092 void servcmd_readold(char *buf, long bufsize)
1093 {
1094         snprintf(buf, bufsize, "MSGS OLD");
1095 }
1096
1097
1098 readloop_struct rlid[] = {
1099         { {HKEY("do_search")}, servcmd_do_search},
1100         { {HKEY("headers")},   servcmd_headers},
1101         { {HKEY("readfwd")},   servcmd_readfwd},
1102         { {HKEY("readnew")},   servcmd_readnew},
1103         { {HKEY("readold")},   servcmd_readold}
1104 };
1105
1106
1107
1108 void SetAccessCommand(long Oper)
1109 {
1110 /* TODO: whats achieved by this?
1111         wcsession *WCC = WC;    
1112
1113         if (WCC->UrlFragment1 != NULL ) {
1114                 FlushStrBuf(WCC->UrlFragment1);
1115                 StrBufAppendBufPlain(WCC->UrlFragment1, 
1116                                      rlid[Oper].name.Key, rlid[Oper].name.len, 0);
1117         }
1118         else 
1119                 WCC->UrlFragment1 = NewStrBufPlain(rlid[Oper].name.Key, rlid[Oper].name.len);
1120 */
1121 }
1122                 
1123
1124
1125
1126
1127 void 
1128 InitModule_MSGRENDERERS
1129 (void)
1130 {
1131         RegisterSortFunc(HKEY("date"), 
1132                          NULL, 0,
1133                          summcmp_date,
1134                          summcmp_rdate,
1135                          groupchange_date,
1136                          CTX_MAILSUM);
1137         RegisterSortFunc(HKEY("subject"), 
1138                          NULL, 0,
1139                          summcmp_subj,
1140                          summcmp_rsubj,
1141                          groupchange_subj,
1142                          CTX_MAILSUM);
1143         RegisterSortFunc(HKEY("sender"),
1144                          NULL, 0,
1145                          summcmp_sender,
1146                          summcmp_rsender,
1147                          groupchange_sender,
1148                          CTX_MAILSUM);
1149
1150         RegisterNamespace("SUMM:COUNT", 0, 0, tmplput_SUMM_COUNT, CTX_NONE);
1151         /* iterate over all known mails in WC->summ */
1152         RegisterIterator("MAIL:SUMM:MSGS", 0, NULL, iterate_get_mailsumm_All,
1153                          NULL,NULL, CTX_MAILSUM, CTX_NONE, IT_NOFLAG);
1154
1155         RegisterNamespace("MAIL:SUMM:DATEBRIEF", 0, 0, tmplput_MAIL_SUMM_DATE_BRIEF, CTX_MAILSUM);
1156         RegisterNamespace("MAIL:SUMM:DATEFULL", 0, 0, tmplput_MAIL_SUMM_DATE_FULL, CTX_MAILSUM);
1157         RegisterNamespace("MAIL:SUMM:DATENO",  0, 0, tmplput_MAIL_SUMM_DATE_NO,  CTX_MAILSUM);
1158         RegisterNamespace("MAIL:SUMM:N",       0, 0, tmplput_MAIL_SUMM_N,        CTX_MAILSUM);
1159         RegisterNamespace("MAIL:SUMM:FROM",    0, 2, tmplput_MAIL_SUMM_FROM,     CTX_MAILSUM);
1160         RegisterNamespace("MAIL:SUMM:TO",      0, 2, tmplput_MAIL_SUMM_TO,       CTX_MAILSUM);
1161         RegisterNamespace("MAIL:SUMM:SUBJECT", 0, 4, tmplput_MAIL_SUMM_SUBJECT,  CTX_MAILSUM);
1162         RegisterNamespace("MAIL:SUMM:NTATACH", 0, 0, tmplput_MAIL_SUMM_NATTACH,  CTX_MAILSUM);
1163         RegisterNamespace("MAIL:SUMM:CCCC", 0, 2, tmplput_MAIL_SUMM_CCCC,  CTX_MAILSUM);
1164         RegisterNamespace("MAIL:SUMM:H_NODE", 0, 2, tmplput_MAIL_SUMM_H_NODE,  CTX_MAILSUM);
1165         RegisterNamespace("MAIL:SUMM:ALLRCPT", 0, 2, tmplput_MAIL_SUMM_ALLRCPT,  CTX_MAILSUM);
1166         RegisterNamespace("MAIL:SUMM:ORGROOM", 0, 2, tmplput_MAIL_SUMM_ORGROOM,  CTX_MAILSUM);
1167         RegisterNamespace("MAIL:SUMM:RFCA", 0, 2, tmplput_MAIL_SUMM_RFCA,  CTX_MAILSUM);
1168         RegisterNamespace("MAIL:SUMM:OTHERNODE", 2, 0, tmplput_MAIL_SUMM_OTHERNODE,  CTX_MAILSUM);
1169         RegisterNamespace("MAIL:SUMM:REFIDS", 0, 1, tmplput_MAIL_SUMM_REFIDS,  CTX_MAILSUM);
1170         RegisterNamespace("MAIL:SUMM:INREPLYTO", 0, 2, tmplput_MAIL_SUMM_INREPLYTO,  CTX_MAILSUM);
1171         RegisterNamespace("MAIL:BODY", 0, 2, tmplput_MAIL_BODY,  CTX_MAILSUM);
1172         RegisterNamespace("MAIL:QUOTETEXT", 1, 2, tmplput_QUOTED_MAIL_BODY,  CTX_NONE);
1173         RegisterConditional(HKEY("COND:MAIL:SUMM:RFCA"), 0, Conditional_MAIL_SUMM_RFCA,  CTX_MAILSUM);
1174         RegisterConditional(HKEY("COND:MAIL:SUMM:CCCC"), 0, Conditional_MAIL_SUMM_CCCC,  CTX_MAILSUM);
1175         RegisterConditional(HKEY("COND:MAIL:SUMM:UNREAD"), 0, Conditional_MAIL_SUMM_UNREAD, CTX_MAILSUM);
1176         RegisterConditional(HKEY("COND:MAIL:SUMM:H_NODE"), 0, Conditional_MAIL_SUMM_H_NODE, CTX_MAILSUM);
1177         RegisterConditional(HKEY("COND:MAIL:SUMM:OTHERNODE"), 0, Conditional_MAIL_SUMM_OTHERNODE, CTX_MAILSUM);
1178         RegisterConditional(HKEY("COND:MAIL:SUMM:SUBJECT"), 0, Conditional_MAIL_SUMM_SUBJECT, CTX_MAILSUM);
1179         RegisterConditional(HKEY("COND:MAIL:ANON"), 0, Conditional_ANONYMOUS_MESSAGE, CTX_MAILSUM);
1180         RegisterConditional(HKEY("COND:MAIL:TO"), 0, Conditional_MAIL_SUMM_TO, CTX_MAILSUM);    
1181         RegisterConditional(HKEY("COND:MAIL:SUBJ"), 0, Conditional_MAIL_SUMM_SUBJ, CTX_MAILSUM);        
1182
1183         /* do we have mimetypes to iterate over? */
1184         RegisterConditional(HKEY("COND:MAIL:MIME:ATTACH"), 0, Conditional_MAIL_MIME_ALL, CTX_MAILSUM);
1185         RegisterConditional(HKEY("COND:MAIL:MIME:ATTACH:SUBMESSAGES"), 0, Conditional_MAIL_MIME_SUBMESSAGES, CTX_MAILSUM);
1186         RegisterConditional(HKEY("COND:MAIL:MIME:ATTACH:LINKS"), 0, Conditional_MAIL_MIME_ATTACHLINKS, CTX_MAILSUM);
1187         RegisterConditional(HKEY("COND:MAIL:MIME:ATTACH:ATT"), 0, Conditional_MAIL_MIME_ATTACH, CTX_MAILSUM);
1188         RegisterIterator("MAIL:MIME:ATTACH", 0, NULL, iterate_get_mime_All, 
1189                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM, IT_NOFLAG);
1190         RegisterIterator("MAIL:MIME:ATTACH:SUBMESSAGES", 0, NULL, iterate_get_mime_Submessages, 
1191                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM, IT_NOFLAG);
1192         RegisterIterator("MAIL:MIME:ATTACH:LINKS", 0, NULL, iterate_get_mime_AttachLinks, 
1193                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM, IT_NOFLAG);
1194         RegisterIterator("MAIL:MIME:ATTACH:ATT", 0, NULL, iterate_get_mime_Attachments, 
1195                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM, IT_NOFLAG);
1196
1197         /* Parts of a mime attachent */
1198         RegisterNamespace("MAIL:MIME:NAME", 0, 2, tmplput_MIME_Name, CTX_MIME_ATACH);
1199         RegisterNamespace("MAIL:MIME:FILENAME", 0, 2, tmplput_MIME_FileName, CTX_MIME_ATACH);
1200         RegisterNamespace("MAIL:MIME:PARTNUM", 0, 2, tmplput_MIME_PartNum, CTX_MIME_ATACH);
1201         RegisterNamespace("MAIL:MIME:MSGNUM", 0, 2, tmplput_MIME_MsgNum, CTX_MIME_ATACH);
1202         RegisterNamespace("MAIL:MIME:DISPOSITION", 0, 2, tmplput_MIME_Disposition, CTX_MIME_ATACH);
1203         RegisterNamespace("MAIL:MIME:CONTENTTYPE", 0, 2, tmplput_MIME_ContentType, CTX_MIME_ATACH);
1204         RegisterNamespace("MAIL:MIME:CHARSET", 0, 2, tmplput_MIME_Charset, CTX_MIME_ATACH);
1205         RegisterNamespace("MAIL:MIME:LENGTH", 0, 2, tmplput_MIME_Length, CTX_MIME_ATACH);
1206         RegisterNamespace("MAIL:MIME:DATA", 0, 2, tmplput_MIME_Data, CTX_MIME_ATACH);
1207         /* load the actual attachment into WC->attachments; no output!!! */
1208         RegisterNamespace("MAIL:MIME:LOADDATA", 0, 0, tmplput_MIME_LoadData, CTX_MIME_ATACH);
1209
1210         /* iterate the WC->attachments; use the above tokens for their contents */
1211         RegisterIterator("MSG:ATTACHNAMES", 0, NULL, iterate_get_registered_Attachments, 
1212                          NULL, NULL, CTX_MIME_ATACH, CTX_NONE, IT_NOFLAG);
1213
1214         /* mime renderers translate an attachment into webcit viewable html text */
1215         RegisterMimeRenderer(HKEY("message/rfc822"), render_MAIL);
1216         RegisterMimeRenderer(HKEY("text/vnote"), render_MIME_VNote);
1217         RegisterMimeRenderer(HKEY("text/x-vcard"), render_MIME_VCard);
1218         RegisterMimeRenderer(HKEY("text/vcard"), render_MIME_VCard);
1219         RegisterMimeRenderer(HKEY("text/calendar"), render_MIME_ICS);
1220         RegisterMimeRenderer(HKEY("application/ics"), render_MIME_ICS);
1221         RegisterMimeRenderer(HKEY("text/x-citadel-variformat"), render_MAIL_variformat);
1222         RegisterMimeRenderer(HKEY("text/plain"), render_MAIL_text_plain);
1223         RegisterMimeRenderer(HKEY("text"), render_MAIL_text_plain);
1224         RegisterMimeRenderer(HKEY("text/html"), render_MAIL_html);
1225         RegisterMimeRenderer(HKEY(""), render_MAIL_UNKNOWN);
1226
1227         /* these headers are citserver replies to MSG4 and friends. one evaluator for each */
1228         RegisterMsgHdr(HKEY("nhdr"), examine_nhdr, 0);
1229         RegisterMsgHdr(HKEY("type"), examine_type, 0);
1230         RegisterMsgHdr(HKEY("from"), examine_from, 0);
1231         RegisterMsgHdr(HKEY("subj"), examine_subj, 0);
1232         RegisterMsgHdr(HKEY("msgn"), examine_msgn, 0);
1233         RegisterMsgHdr(HKEY("wefw"), examine_wefw, 0);
1234         RegisterMsgHdr(HKEY("cccc"), examine_cccc, 0);
1235         RegisterMsgHdr(HKEY("hnod"), examine_hnod, 0);
1236         RegisterMsgHdr(HKEY("room"), examine_room, 0);
1237         RegisterMsgHdr(HKEY("rfca"), examine_rfca, 0);
1238         RegisterMsgHdr(HKEY("node"), examine_node, 0);
1239         RegisterMsgHdr(HKEY("rcpt"), examine_rcpt, 0);
1240         RegisterMsgHdr(HKEY("time"), examine_time, 0);
1241         RegisterMsgHdr(HKEY("part"), examine_mime_part, 0);
1242         RegisterMsgHdr(HKEY("text"), examine_text, 1);
1243         /* these are the content-type headers we get infront of a message; put it into the same hash since it doesn't clash. */
1244         RegisterMsgHdr(HKEY("X-Citadel-MSG4-Partnum"), examine_msg4_partnum, 0);
1245         RegisterMsgHdr(HKEY("Content-type"), examine_content_type, 0);
1246         RegisterMsgHdr(HKEY("Content-length"), examine_content_lengh, 0);
1247         RegisterMsgHdr(HKEY("Content-transfer-encoding"), examine_content_encoding, 0); /* do we care? */
1248         RegisterMsgHdr(HKEY("charset"), examine_charset, 0);
1249
1250         /* Don't care about these... */
1251         RegisterMsgHdr(HKEY("pref"), examine_pref, 0);
1252         RegisterMsgHdr(HKEY("suff"), examine_suff, 0);
1253         RegisterMsgHdr(HKEY("path"), examine_path, 0);
1254 }
1255
1256 void 
1257 ServerStartModule_MSGRENDERERS
1258 (void)
1259 {
1260         MsgHeaderHandler = NewHash(1, NULL);
1261         MimeRenderHandler = NewHash(1, NULL);
1262 }
1263
1264 void 
1265 ServerShutdownModule_MSGRENDERERS
1266 (void)
1267 {
1268         DeleteHash(&MsgHeaderHandler);
1269         DeleteHash(&MimeRenderHandler);
1270 }
1271
1272
1273
1274 void 
1275 SessionDestroyModule_MSGRENDERERS
1276 (wcsession *sess)
1277 {
1278         DeleteHash(&sess->attachments);
1279 }