* load inline texts with DLAT not MSG4
[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
275
276 void examine_msgn(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
277 {
278         FreeStrBuf(&Msg->reply_inreplyto);
279         Msg->reply_inreplyto = NewStrBufPlain(NULL, StrLength(HdrLine));
280         StrBuf_RFC822_to_Utf8(Msg->reply_inreplyto, HdrLine, WC->DefaultCharset, FoundCharset);
281 }
282 void tmplput_MAIL_SUMM_INREPLYTO(StrBuf *Target, WCTemplputParams *TP)
283 {
284         message_summary *Msg = (message_summary*) CTX;
285         StrBufAppendTemplate(Target, TP, Msg->reply_inreplyto, 0);
286 }
287
288 int Conditional_MAIL_SUMM_UNREAD(StrBuf *Target, WCTemplputParams *TP)
289 {
290         message_summary *Msg = (message_summary*) CTX;
291         return Msg->is_new != 0;
292 }
293
294 void examine_wefw(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
295 {
296         FreeStrBuf(&Msg->reply_references);
297         Msg->reply_references = NewStrBufPlain(NULL, StrLength(HdrLine));
298         StrBuf_RFC822_to_Utf8(Msg->reply_references, HdrLine, WC->DefaultCharset, FoundCharset);
299 }
300 void tmplput_MAIL_SUMM_REFIDS(StrBuf *Target, WCTemplputParams *TP)
301 {
302         message_summary *Msg = (message_summary*) CTX;
303         StrBufAppendTemplate(Target, TP, Msg->reply_references, 0);
304 }
305
306
307 void examine_cccc(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
308 {
309         FreeStrBuf(&Msg->cccc);
310         Msg->cccc = NewStrBufPlain(NULL, StrLength(HdrLine));
311         StrBuf_RFC822_to_Utf8(Msg->cccc, HdrLine, WC->DefaultCharset, FoundCharset);
312         if (Msg->AllRcpt == NULL)
313                 Msg->AllRcpt = NewStrBufPlain(NULL, StrLength(HdrLine));
314         if (StrLength(Msg->AllRcpt) > 0) {
315                 StrBufAppendBufPlain(Msg->AllRcpt, HKEY(", "), 0);
316         }
317         StrBufAppendBuf(Msg->AllRcpt, Msg->cccc, 0);
318 }
319 void tmplput_MAIL_SUMM_CCCC(StrBuf *Target, WCTemplputParams *TP)
320 {
321         message_summary *Msg = (message_summary*) CTX;
322         StrBufAppendTemplate(Target, TP, Msg->cccc, 0);
323 }
324
325
326 void examine_room(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
327 {
328         if ((StrLength(HdrLine) > 0) &&
329             (strcasecmp(ChrPtr(HdrLine), ChrPtr(WC->wc_roomname)))) {
330                 FreeStrBuf(&Msg->Room);
331                 Msg->Room = NewStrBufDup(HdrLine);              
332         }
333 }
334 void tmplput_MAIL_SUMM_ORGROOM(StrBuf *Target, WCTemplputParams *TP)
335 {
336         message_summary *Msg = (message_summary*) CTX;
337         StrBufAppendTemplate(Target, TP, Msg->Room, 0);
338 }
339
340
341 void examine_rfca(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
342 {
343         FreeStrBuf(&Msg->Rfca);
344         Msg->Rfca = NewStrBufDup(HdrLine);
345 }
346 void tmplput_MAIL_SUMM_RFCA(StrBuf *Target, WCTemplputParams *TP)
347 {
348         message_summary *Msg = (message_summary*) CTX;
349         StrBufAppendTemplate(Target, TP, Msg->Rfca, 0);
350 }
351 int Conditional_MAIL_SUMM_RFCA(StrBuf *Target, WCTemplputParams *TP)
352 {
353         message_summary *Msg = (message_summary*) CTX;
354         return StrLength(Msg->Rfca) > 0;
355 }
356
357 void examine_node(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
358 {
359         if ( (StrLength(HdrLine) > 0) &&
360              ((WC->room_flags & QR_NETWORK)
361               || ((strcasecmp(ChrPtr(HdrLine), ChrPtr(serv_info.serv_nodename))
362                    && (strcasecmp(ChrPtr(HdrLine), ChrPtr(serv_info.serv_fqdn))))))) {
363                 FreeStrBuf(&Msg->OtherNode);
364                 Msg->OtherNode = NewStrBufDup(HdrLine);
365         }
366 }
367 void tmplput_MAIL_SUMM_OTHERNODE(StrBuf *Target, WCTemplputParams *TP)
368 {
369         message_summary *Msg = (message_summary*) CTX;
370         StrBufAppendTemplate(Target, TP, Msg->OtherNode, 0);
371 }
372 int Conditional_MAIL_SUMM_OTHERNODE(StrBuf *Target, WCTemplputParams *TP)
373 {
374         message_summary *Msg = (message_summary*) CTX;
375         return StrLength(Msg->OtherNode) > 0;
376 }
377
378
379 void examine_rcpt(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
380 {
381         FreeStrBuf(&Msg->to);
382         Msg->to = NewStrBufPlain(NULL, StrLength(HdrLine));
383         StrBuf_RFC822_to_Utf8(Msg->to, HdrLine, WC->DefaultCharset, FoundCharset);
384         if (Msg->AllRcpt == NULL)
385                 Msg->AllRcpt = NewStrBufPlain(NULL, StrLength(HdrLine));
386         if (StrLength(Msg->AllRcpt) > 0) {
387                 StrBufAppendBufPlain(Msg->AllRcpt, HKEY(", "), 0);
388         }
389         StrBufAppendBuf(Msg->AllRcpt, Msg->to, 0);
390 }
391 void tmplput_MAIL_SUMM_TO(StrBuf *Target, WCTemplputParams *TP)
392 {
393         message_summary *Msg = (message_summary*) CTX;
394         StrBufAppendTemplate(Target, TP, Msg->to, 0);
395 }
396 void tmplput_MAIL_SUMM_ALLRCPT(StrBuf *Target, WCTemplputParams *TP)
397 {
398         message_summary *Msg = (message_summary*) CTX;
399         StrBufAppendTemplate(Target, TP, Msg->AllRcpt, 0);
400 }
401
402
403 HashList *iterate_get_mailsumm_All(StrBuf *Target, WCTemplputParams *TP)
404 {
405         return WC->summ;
406 }
407 int Conditional_ROOM_DISPLAY_MSG(StrBuf *Target, WCTemplputParams *TP) {
408         wcsession *WCC = WC;
409         IterateStruct *ITC = CCTX;
410         int num_inset = ITC->n;
411         if ((num_inset >= WC->startmsg) && (WCC->num_displayed <= WCC->maxmsgs)) {
412         WCC->num_displayed = WCC->num_displayed+1;
413         return 1; /* Pass GO, collect $200 */
414         } 
415         return 0;
416 }
417 int Conditional_MAIL_SUMM_LASTMSG(StrBuf *Target, WCTemplputParams *TP) {
418   IterateStruct *ITC = CCTX;
419         int is_last_n = ITC->LastN;
420 /*
421         //GetHash(WC->vars, HKEY("ITERATE:N"), (void *)&n_dsubst);
422         //num_inset = n_dsubst->lvalue;
423         */
424         /* Is the num_displayed higher than maxmsgs? OR last in iterator */
425         return ((WC->num_displayed > WC->maxmsgs) || (is_last_n == 1));
426 }
427 void examine_time(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
428 {
429         Msg->date = StrTol(HdrLine);
430 }
431
432 void tmplput_MAIL_SUMM_DATE_BRIEF(StrBuf *Target, WCTemplputParams *TP)
433 {
434         char datebuf[64];
435         message_summary *Msg = (message_summary*) CTX;
436         webcit_fmt_date(datebuf, Msg->date, 1);
437         StrBufAppendBufPlain(Target, datebuf, -1, 0);
438 }
439
440 void tmplput_MAIL_SUMM_DATE_FULL(StrBuf *Target, WCTemplputParams *TP)
441 {
442         char datebuf[64];
443         message_summary *Msg = (message_summary*) CTX;
444         webcit_fmt_date(datebuf, Msg->date, 0);
445         StrBufAppendBufPlain(Target, datebuf, -1, 0);
446 }
447 void tmplput_MAIL_SUMM_DATE_NO(StrBuf *Target, WCTemplputParams *TP)
448 {
449         message_summary *Msg = (message_summary*) CTX;
450         StrBufAppendPrintf(Target, "%ld", Msg->date, 0);
451 }
452
453
454
455 void render_MAIL(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
456 {
457         Mime->Data = NewStrBufPlain(NULL, Mime->length);
458         read_message(Mime->Data, HKEY("view_submessage"), Mime->msgnum, 0, Mime->PartNum);
459 /*
460         if ( (!IsEmptyStr(mime_submessages)) && (!section[0]) ) {
461                 for (i=0; i<num_tokens(mime_submessages, '|'); ++i) {
462                         extract_token(buf, mime_submessages, i, '|', sizeof buf);
463                         / ** use printable_view to suppress buttons * /
464                         wprintf("<blockquote>");
465                         read_message(Mime->msgnum, 1, ChrPtr(Mime->Section));
466                         wprintf("</blockquote>");
467                 }
468         }
469 */
470 }
471
472 void render_MIME_VCard(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
473 {
474         wcsession *WCC = WC;
475         MimeLoadData(Mime);
476         if (StrLength(Mime->Data) > 0) {
477                 StrBuf *Buf;
478                 Buf = NewStrBuf();
479                 /** If it's my vCard I can edit it */
480                 if (    (!strcasecmp(ChrPtr(WCC->wc_roomname), USERCONFIGROOM))
481                         || (!strcasecmp(&(ChrPtr(WCC->wc_roomname)[11]), USERCONFIGROOM))
482                         || (WC->wc_view == VIEW_ADDRESSBOOK)
483                         ) {
484                         StrBufAppendPrintf(Buf, "<a href=\"edit_vcard?msgnum=%ld&partnum=%s\">",
485                                 Mime->msgnum, ChrPtr(Mime->PartNum));
486                         StrBufAppendPrintf(Buf, "[%s]</a>", _("edit"));
487                 }
488
489                 /* In all cases, display the full card */
490                 display_vcard(Buf, ChrPtr(Mime->Data), 0, 1, NULL, Mime->msgnum);
491                 FreeStrBuf(&Mime->Data);
492                 Mime->Data = Buf;
493         }
494
495 }
496
497 void render_MIME_ICS(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
498 {
499         if (StrLength(Mime->Data) == 0) {
500                 MimeLoadData(Mime);
501         }
502         if (StrLength(Mime->Data) > 0) {
503                 cal_process_attachment(Mime);
504         }
505 }
506
507
508
509 void examine_mime_part(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
510 {
511         wc_mime_attachment *Mime;
512         StrBuf *Buf;
513         
514         Mime = (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
515         memset(Mime, 0, sizeof(wc_mime_attachment));
516         Mime->msgnum = Msg->msgnum;
517         Buf = NewStrBuf();
518
519         Mime->Name = NewStrBuf();
520         StrBufExtract_token(Buf, HdrLine, 0, '|');
521         StrBuf_RFC822_to_Utf8(Mime->Name, Buf, WC->DefaultCharset, FoundCharset);
522         StrBufTrim(Mime->Name);
523
524         StrBufExtract_token(Buf, HdrLine, 1, '|');
525         Mime->FileName = NewStrBuf();
526         StrBuf_RFC822_to_Utf8(Mime->FileName, Buf, WC->DefaultCharset, FoundCharset);
527         StrBufTrim(Mime->FileName);
528
529         Mime->PartNum = NewStrBuf();
530         StrBufExtract_token(Mime->PartNum, HdrLine, 2, '|');
531         StrBufTrim(Mime->PartNum);
532         if (strchr(ChrPtr(Mime->PartNum), '.') != NULL)
533                 Mime->level = 2;
534         else
535                 Mime->level = 1;
536
537         Mime->Disposition = NewStrBuf();
538         StrBufExtract_token(Mime->Disposition, HdrLine, 3, '|');
539
540         Mime->ContentType = NewStrBuf();
541         StrBufExtract_token(Mime->ContentType, HdrLine, 4, '|');
542         StrBufTrim(Mime->ContentType);
543         StrBufLowerCase(Mime->ContentType);
544
545         Mime->length = StrBufExtract_int(HdrLine, 5, '|');
546
547         if ( (StrLength(Mime->FileName) == 0) && (StrLength(Mime->Name) > 0) ) {
548                 StrBufAppendBuf(Mime->FileName, Mime->Name, 0);
549         }
550
551         if (StrLength(Msg->PartNum) > 0) {
552                 StrBuf *tmp;
553                 StrBufPrintf(Buf, "%s.%s", ChrPtr(Msg->PartNum), ChrPtr(Mime->PartNum));
554                 tmp = Mime->PartNum;
555                 Mime->PartNum = Buf;
556                 Buf = tmp;
557         }
558
559         if (Msg->AllAttach == NULL)
560                 Msg->AllAttach = NewHash(1,NULL);
561         Put(Msg->AllAttach, SKEY(Mime->PartNum), Mime, DestroyMime);
562         FreeStrBuf(&Buf);
563 }
564
565
566 void evaluate_mime_part(message_summary *Msg, wc_mime_attachment *Mime)
567 {
568         void *vMimeRenderer;
569
570         /* just print the root-node */
571         if ((Mime->level == 1) &&
572             GetHash(MimeRenderHandler, SKEY(Mime->ContentType), &vMimeRenderer) &&
573             vMimeRenderer != NULL)
574         {
575                 Mime->Renderer = (RenderMimeFuncStruct*) vMimeRenderer;
576                 if (Msg->Submessages == NULL)
577                         Msg->Submessages = NewHash(1,NULL);
578                 Put(Msg->Submessages, SKEY(Mime->PartNum), Mime, reference_free_handler);
579         }
580         else if ((Mime->level == 1) &&
581                  (!strcasecmp(ChrPtr(Mime->Disposition), "inline"))
582                  && (!strncasecmp(ChrPtr(Mime->ContentType), "image/", 6)) ){
583                 if (Msg->AttachLinks == NULL)
584                         Msg->AttachLinks = NewHash(1,NULL);
585                 Put(Msg->AttachLinks, SKEY(Mime->PartNum), Mime, reference_free_handler);
586         }
587         else if ((Mime->level == 1) &&
588                  (StrLength(Mime->ContentType) > 0) &&
589                   ( (!strcasecmp(ChrPtr(Mime->Disposition), "attachment")) 
590                     || (!strcasecmp(ChrPtr(Mime->Disposition), "inline"))
591                     || (!strcasecmp(ChrPtr(Mime->Disposition), ""))))
592         {               
593                 if (Msg->AttachLinks == NULL)
594                         Msg->AttachLinks = NewHash(1,NULL);
595                 Put(Msg->AttachLinks, SKEY(Mime->PartNum), Mime, reference_free_handler);
596                 if ((strcasecmp(ChrPtr(Mime->ContentType), "application/octet-stream") == 0) && 
597                     (StrLength(Mime->FileName) > 0)) {
598                         FlushStrBuf(Mime->ContentType);
599                         StrBufAppendBufPlain(Mime->ContentType,
600                                              GuessMimeByFilename(SKEY(Mime->FileName)),
601                                              -1, 0);
602                 }
603         }
604 }
605
606 void tmplput_MAIL_SUMM_NATTACH(StrBuf *Target, WCTemplputParams *TP)
607 {
608         message_summary *Msg = (message_summary*) CTX;
609         StrBufAppendPrintf(Target, "%ld", GetCount(Msg->Attachments));
610 }
611
612
613 void examine_hnod(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
614 {
615         FreeStrBuf(&Msg->hnod);
616         Msg->hnod = NewStrBufPlain(NULL, StrLength(HdrLine));
617         StrBuf_RFC822_to_Utf8(Msg->hnod, HdrLine, WC->DefaultCharset, FoundCharset);
618 }
619 void tmplput_MAIL_SUMM_H_NODE(StrBuf *Target, WCTemplputParams *TP)
620 {
621         message_summary *Msg = (message_summary*) CTX;
622         StrBufAppendTemplate(Target, TP, Msg->hnod, 0);
623 }
624 int Conditional_MAIL_SUMM_H_NODE(StrBuf *Target, WCTemplputParams *TP)
625 {
626         message_summary *Msg = (message_summary*) CTX;
627         return StrLength(Msg->hnod) > 0;
628 }
629
630
631
632 void examine_text(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
633 {
634         Msg->MsgBody->Data = NewStrBuf();
635 }
636
637 void examine_msg4_partnum(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
638 {
639         Msg->MsgBody->PartNum = NewStrBufDup(HdrLine);
640         StrBufTrim(Msg->MsgBody->PartNum);
641 }
642
643 void examine_content_lengh(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
644 {
645         Msg->MsgBody->length = StrTol(HdrLine);
646         Msg->MsgBody->size_known = 1;
647 }
648
649 void examine_content_type(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
650 {
651         void *vHdr;
652         headereval *Hdr;
653         StrBuf *Token;
654         StrBuf *Value;
655         const char* sem;
656         const char *eq;
657         int len;
658         StrBufTrim(HdrLine);
659         Msg->MsgBody->ContentType = NewStrBufDup(HdrLine);
660         sem = strchr(ChrPtr(HdrLine), ';');
661
662         if (sem != NULL) {
663                 Token = NewStrBufPlain(NULL, StrLength(HdrLine));
664                 Value = NewStrBufPlain(NULL, StrLength(HdrLine));
665                 len = sem - ChrPtr(HdrLine);
666                 StrBufCutAt(Msg->MsgBody->ContentType, len, NULL);
667                 while (sem != NULL) {
668                         while (isspace(*(sem + 1)))
669                                 sem ++;
670                         StrBufCutLeft(HdrLine, sem - ChrPtr(HdrLine));
671                         sem = strchr(ChrPtr(HdrLine), ';');
672                         if (sem != NULL)
673                                 len = sem - ChrPtr(HdrLine);
674                         else
675                                 len = StrLength(HdrLine);
676                         FlushStrBuf(Token);
677                         FlushStrBuf(Value);
678                         StrBufAppendBufPlain(Token, ChrPtr(HdrLine), len, 0);
679                         eq = strchr(ChrPtr(Token), '=');
680                         if (eq != NULL) {
681                                 len = eq - ChrPtr(Token);
682                                 StrBufAppendBufPlain(Value, eq + 1, StrLength(Token) - len - 1, 0); 
683                                 StrBufCutAt(Token, len, NULL);
684                                 StrBufTrim(Value);
685                         }
686                         StrBufTrim(Token);
687
688                         if (GetHash(MsgHeaderHandler, SKEY(Token), &vHdr) &&
689                             (vHdr != NULL)) {
690                                 Hdr = (headereval*)vHdr;
691                                 Hdr->evaluator(Msg, Value, FoundCharset);
692                         }
693                         else lprintf(1, "don't know how to handle content type sub-header[%s]\n", ChrPtr(Token));
694                 }
695                 FreeStrBuf(&Token);
696                 FreeStrBuf(&Value);
697         }
698 }
699
700 void tmplput_MAIL_SUMM_N(StrBuf *Target, WCTemplputParams *TP)
701 {
702         message_summary *Msg = (message_summary*) CTX;
703         StrBufAppendPrintf(Target, "%ld", Msg->msgnum);
704 }
705
706
707
708 int Conditional_MAIL_MIME_ALL(StrBuf *Target, WCTemplputParams *TP)
709 {
710         message_summary *Msg = (message_summary*) CTX;
711         return GetCount(Msg->Attachments) > 0;
712 }
713
714 int Conditional_MAIL_MIME_SUBMESSAGES(StrBuf *Target, WCTemplputParams *TP)
715 {
716         message_summary *Msg = (message_summary*) CTX;
717         return GetCount(Msg->Submessages) > 0;
718 }
719
720 int Conditional_MAIL_MIME_ATTACHLINKS(StrBuf *Target, WCTemplputParams *TP)
721 {
722         message_summary *Msg = (message_summary*) CTX;
723         return GetCount(Msg->AttachLinks) > 0;
724 }
725
726 int Conditional_MAIL_MIME_ATTACH(StrBuf *Target, WCTemplputParams *TP)
727 {
728         message_summary *Msg = (message_summary*) CTX;
729         return GetCount(Msg->AllAttach) > 0;
730 }
731
732
733
734 /*----------------------------------------------------------------------------*/
735 void tmplput_QUOTED_MAIL_BODY(StrBuf *Target, WCTemplputParams *TP)
736 {
737         long MsgNum;
738         StrBuf *Buf;
739
740         MsgNum = LBstr(TKEY(0));
741         Buf = NewStrBuf();
742         read_message(Buf, HKEY("view_message_replyquote"), MsgNum, 0, NULL);
743         StrBufAppendTemplate(Target, TP, Buf, 1);
744         FreeStrBuf(&Buf);
745 }
746
747 void tmplput_MAIL_BODY(StrBuf *Target, WCTemplputParams *TP)
748 {
749         message_summary *Msg = (message_summary*) CTX;
750         StrBufAppendTemplate(Target, TP, Msg->MsgBody->Data, 0);
751 }
752
753
754 void render_MAIL_variformat(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
755 {
756         /* Messages in legacy Citadel variformat get handled thusly... */
757         StrBuf *Target = NewStrBufPlain(NULL, StrLength(Mime->Data));
758         FmOut(Target, "JUSTIFY", Mime->Data);
759         FreeStrBuf(&Mime->Data);
760         Mime->Data = Target;
761 }
762
763 void render_MAIL_text_plain(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
764 {
765         StrBuf *cs = NULL;
766         const char *ptr, *pte;
767         const char *BufPtr = NULL;
768         StrBuf *Line;
769         StrBuf *Line1;
770         StrBuf *Line2;
771         StrBuf *Target;
772
773         int ConvertIt = 1;
774         int bn = 0;
775         int bq = 0;
776         int i, n, done = 0;
777         long len;
778 #ifdef HAVE_ICONV
779         iconv_t ic = (iconv_t)(-1) ;
780 #endif
781
782         if ((StrLength(Mime->Data) == 0) && (Mime->length > 0)) {
783                 FreeStrBuf(&Mime->Data);
784                 MimeLoadData(Mime);
785         }
786
787         /* Boring old 80-column fixed format text gets handled this way... */
788         if ((strcasecmp(ChrPtr(Mime->Charset), "us-ascii") == 0) &&
789             (strcasecmp(ChrPtr(Mime->Charset), "UTF-8") == 0))
790                 ConvertIt = 0;
791
792 #ifdef HAVE_ICONV
793         if (ConvertIt) {
794                 if (StrLength(Mime->Charset) != 0)
795                         cs = Mime->Charset;
796                 else if (StrLength(FoundCharset) > 0)
797                         cs = FoundCharset;
798                 else if (StrLength(WC->DefaultCharset) > 0)
799                         cs = WC->DefaultCharset;
800                 if (cs == NULL) {
801                         ConvertIt = 0;
802                 }
803                 else {
804                         ctdl_iconv_open("UTF-8", ChrPtr(cs), &ic);
805                         if (ic == (iconv_t)(-1) ) {
806                                 lprintf(5, "%s:%d iconv_open(UTF-8, %s) failed: %s\n",
807                                         __FILE__, __LINE__, ChrPtr(Mime->Charset), strerror(errno));
808                         }
809                 }
810         }
811 #endif
812         Line = NewStrBuf();
813         Line1 = NewStrBuf();
814         Line2 = NewStrBuf();
815         Target = NewStrBufPlain(NULL, StrLength(Mime->Data));
816
817         while ((n = StrBufSipLine(Line, Mime->Data, &BufPtr), n >= 0) && !done)
818         {
819                 done = n == 0;
820                 bq = 0;
821                 i = 0;
822                 ptr = ChrPtr(Line);
823                 len = StrLength(Line);
824                 pte = ptr + len;
825                 
826                 while ((ptr < pte) &&
827                        ((*ptr == '>') ||
828                         isspace(*ptr)))
829                 {
830                         if (*ptr == '>')
831                                 bq++;
832                         ptr ++;
833                         i++;
834                 }
835                 if (i > 0) StrBufCutLeft(Line, i);
836                 
837                 if (StrLength(Line) == 0)
838                         continue;
839
840                 for (i = bn; i < bq; i++)                               
841                         StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
842                 for (i = bq; i < bn; i++)                               
843                         StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
844
845                 if (ConvertIt == 1) {
846                         StrBufConvert(Line, Line1, &ic);
847                 }
848
849                 StrBufAppendBufPlain(Target, HKEY("<tt>"), 0);
850                 UrlizeText(Line1, Line, Line2);
851
852                 StrEscAppend(Target, Line1, NULL, 0, 0);
853                 StrBufAppendBufPlain(Target, HKEY("</tt><br />\n"), 0);
854                 bn = bq;
855         }
856
857         for (i = 0; i < bn; i++)                                
858                 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
859
860         StrBufAppendBufPlain(Target, HKEY("</i><br />"), 0);
861 #ifdef HAVE_ICONV
862         if (ic != (iconv_t)(-1) ) {
863                 iconv_close(ic);
864         }
865 #endif
866
867         FreeStrBuf(&Mime->Data);
868         Mime->Data = Target;
869         FlushStrBuf(Mime->ContentType);
870         StrBufAppendBufPlain(Mime->ContentType, HKEY("text/html"), 0);
871         FlushStrBuf(Mime->Charset);
872         StrBufAppendBufPlain(Mime->Charset, HKEY("UTF-8"), 0);
873         FreeStrBuf(&Line);
874         FreeStrBuf(&Line1);
875         FreeStrBuf(&Line2);
876 }
877
878 void render_MAIL_html(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
879 {
880         StrBuf *Buf;
881
882         if (StrLength(Mime->Data) == 0)
883                 return;
884
885         Buf = NewStrBufPlain(NULL, StrLength(Mime->Data));
886
887         /* HTML is fun, but we've got to strip it first */
888         output_html(ChrPtr(Mime->Charset), 
889                     (WC->wc_view == VIEW_WIKI ? 1 : 0), 
890                     Mime->msgnum,
891                     Mime->Data, Buf);
892         FreeStrBuf(&Mime->Data);
893         Mime->Data = Buf;
894 }
895
896 void render_MAIL_UNKNOWN(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
897 {
898         /* Unknown weirdness */
899         FlushStrBuf(Mime->Data);
900         StrBufAppendBufPlain(Mime->Data, _("I don't know how to display "), -1, 0);
901         StrBufAppendBuf(Mime->Data, Mime->ContentType, 0);
902         StrBufAppendBufPlain(Mime->Data, HKEY("<br />\n"), 0);
903 }
904
905
906
907
908
909
910 HashList *iterate_get_mime_All(StrBuf *Target, WCTemplputParams *TP)
911 {
912         message_summary *Msg = (message_summary*) CTX;
913         return Msg->Attachments;
914 }
915 HashList *iterate_get_mime_Submessages(StrBuf *Target, WCTemplputParams *TP)
916 {
917         message_summary *Msg = (message_summary*) CTX;
918         return Msg->Submessages;
919 }
920 HashList *iterate_get_mime_AttachLinks(StrBuf *Target, WCTemplputParams *TP)
921 {
922         message_summary *Msg = (message_summary*) CTX;
923         return Msg->AttachLinks;
924 }
925 HashList *iterate_get_mime_Attachments(StrBuf *Target, WCTemplputParams *TP)
926 {
927         message_summary *Msg = (message_summary*) CTX;
928         return Msg->AllAttach;
929 }
930
931 void tmplput_MIME_Name(StrBuf *Target, WCTemplputParams *TP)
932 {
933         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
934         StrBufAppendTemplate(Target, TP, mime->Name, 0);
935 }
936
937 void tmplput_MIME_FileName(StrBuf *Target, WCTemplputParams *TP)
938 {
939         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
940         StrBufAppendTemplate(Target, TP, mime->FileName, 0);
941 }
942
943 void tmplput_MIME_PartNum(StrBuf *Target, WCTemplputParams *TP)
944 {
945         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
946         StrBufAppendTemplate(Target, TP, mime->PartNum, 0);
947 }
948
949 void tmplput_MIME_MsgNum(StrBuf *Target, WCTemplputParams *TP)
950 {
951         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
952         StrBufAppendPrintf(Target, "%ld", mime->msgnum);
953 }
954
955 void tmplput_MIME_Disposition(StrBuf *Target, WCTemplputParams *TP)
956 {
957         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
958         StrBufAppendTemplate(Target, TP, mime->Disposition, 0);
959 }
960
961 void tmplput_MIME_ContentType(StrBuf *Target, WCTemplputParams *TP)
962 {
963         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
964         StrBufAppendTemplate(Target, TP, mime->ContentType, 0);
965 }
966
967 void examine_charset(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
968 {
969         Msg->MsgBody->Charset = NewStrBufDup(HdrLine);
970 }
971
972 void tmplput_MIME_Charset(StrBuf *Target, WCTemplputParams *TP)
973 {
974         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
975         StrBufAppendTemplate(Target, TP, mime->Charset, 0);
976 }
977
978 void tmplput_MIME_Data(StrBuf *Target, WCTemplputParams *TP)
979 {
980         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
981         if (mime->Renderer != NULL)
982                 mime->Renderer->f(mime, NULL, NULL);
983         StrBufAppendTemplate(Target, TP, mime->Data, 0);
984         /* TODO: check whether we need to load it now? */
985 }
986
987 void tmplput_MIME_LoadData(StrBuf *Target, WCTemplputParams *TP)
988 {
989         wcsession *WCC = WC;    
990         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
991         wc_mime_attachment *att;
992         
993         if ( (!strcasecmp(ChrPtr(mime->Disposition), "inline"))||
994              (!strcasecmp(ChrPtr(mime->Disposition), "attachment")) ) 
995         {
996                 
997                 int n;
998                 char N[64];
999                 /* steal this mime part... */
1000                 att = malloc(sizeof(wc_mime_attachment));
1001                 memcpy(att, mime, sizeof(wc_mime_attachment));
1002                 memset(mime, 0, sizeof(wc_mime_attachment));
1003
1004                 if (att->Data == NULL) 
1005                         MimeLoadData(att);
1006
1007                 if (WCC->attachments == NULL)
1008                         WCC->attachments = NewHash(1, NULL);
1009                 /* And add it to the list. */
1010                 n = snprintf(N, sizeof N, "%d", GetCount(WCC->attachments) + 1);
1011                 Put(WCC->attachments, N, n, att, DestroyMime);
1012         }
1013 }
1014
1015 void tmplput_MIME_Length(StrBuf *Target, WCTemplputParams *TP)
1016 {
1017         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
1018         StrBufAppendPrintf(Target, "%ld", mime->length);
1019 }
1020
1021 HashList *iterate_get_registered_Attachments(StrBuf *Target, WCTemplputParams *TP)
1022 {
1023         return WC->attachments;
1024 }
1025
1026 void servcmd_do_search(char *buf, long bufsize)
1027 {
1028         snprintf(buf, bufsize, "MSGS SEARCH|%s", bstr("query"));
1029 }
1030
1031 void servcmd_headers(char *buf, long bufsize)
1032 {
1033         snprintf(buf, bufsize, "MSGS ALL");
1034 }
1035
1036 void servcmd_readfwd(char *buf, long bufsize)
1037 {
1038         snprintf(buf, bufsize, "MSGS ALL");
1039 }
1040
1041 void servcmd_readnew(char *buf, long bufsize)
1042 {
1043         snprintf(buf, bufsize, "MSGS NEW");
1044 }
1045
1046 void servcmd_readold(char *buf, long bufsize)
1047 {
1048         snprintf(buf, bufsize, "MSGS OLD");
1049 }
1050
1051
1052 readloop_struct rlid[] = {
1053         { {HKEY("do_search")}, servcmd_do_search},
1054         { {HKEY("headers")},   servcmd_headers},
1055         { {HKEY("readfwd")},   servcmd_readfwd},
1056         { {HKEY("readnew")},   servcmd_readnew},
1057         { {HKEY("readold")},   servcmd_readold}
1058 };
1059
1060
1061
1062 void SetAccessCommand(long Oper)
1063 {
1064         wcsession *WCC = WC;    
1065
1066         if (WCC->UrlFragment1 != NULL ) {
1067                 FlushStrBuf(WCC->UrlFragment1);
1068                 StrBufAppendBufPlain(WCC->UrlFragment1, 
1069                                      rlid[Oper].name.Key, rlid[Oper].name.len, 0);
1070         }
1071         else 
1072                 WCC->UrlFragment1 = NewStrBufPlain(rlid[Oper].name.Key, rlid[Oper].name.len);
1073 }
1074                 
1075
1076
1077
1078
1079 void 
1080 InitModule_MSGRENDERERS
1081 (void)
1082 {
1083         RegisterSortFunc(HKEY("date"), 
1084                          NULL, 0,
1085                          summcmp_date,
1086                          summcmp_rdate,
1087                          groupchange_date,
1088                          CTX_MAILSUM);
1089         RegisterSortFunc(HKEY("subject"), 
1090                          NULL, 0,
1091                          summcmp_subj,
1092                          summcmp_rsubj,
1093                          groupchange_subj,
1094                          CTX_MAILSUM);
1095         RegisterSortFunc(HKEY("sender"),
1096                          NULL, 0,
1097                          summcmp_sender,
1098                          summcmp_rsender,
1099                          groupchange_sender,
1100                          CTX_MAILSUM);
1101
1102         /* iterate over all known mails in WC->summ */
1103         RegisterIterator("MAIL:SUMM:MSGS", 0, NULL, iterate_get_mailsumm_All,
1104                          NULL,NULL, CTX_MAILSUM, CTX_NONE, IT_NOFLAG);
1105
1106         RegisterNamespace("MAIL:SUMM:DATEBRIEF", 0, 0, tmplput_MAIL_SUMM_DATE_BRIEF, CTX_MAILSUM);
1107         RegisterNamespace("MAIL:SUMM:DATEFULL", 0, 0, tmplput_MAIL_SUMM_DATE_FULL, CTX_MAILSUM);
1108         RegisterNamespace("MAIL:SUMM:DATENO",  0, 0, tmplput_MAIL_SUMM_DATE_NO,  CTX_MAILSUM);
1109         RegisterNamespace("MAIL:SUMM:N",       0, 0, tmplput_MAIL_SUMM_N,        CTX_MAILSUM);
1110         RegisterNamespace("MAIL:SUMM:FROM",    0, 2, tmplput_MAIL_SUMM_FROM,     CTX_MAILSUM);
1111         RegisterNamespace("MAIL:SUMM:TO",      0, 2, tmplput_MAIL_SUMM_TO,       CTX_MAILSUM);
1112         RegisterNamespace("MAIL:SUMM:SUBJECT", 0, 4, tmplput_MAIL_SUMM_SUBJECT,  CTX_MAILSUM);
1113         RegisterNamespace("MAIL:SUMM:NTATACH", 0, 0, tmplput_MAIL_SUMM_NATTACH,  CTX_MAILSUM);
1114         RegisterNamespace("MAIL:SUMM:CCCC", 0, 2, tmplput_MAIL_SUMM_CCCC,  CTX_MAILSUM);
1115         RegisterNamespace("MAIL:SUMM:H_NODE", 0, 2, tmplput_MAIL_SUMM_H_NODE,  CTX_MAILSUM);
1116         RegisterNamespace("MAIL:SUMM:ALLRCPT", 0, 2, tmplput_MAIL_SUMM_ALLRCPT,  CTX_MAILSUM);
1117         RegisterNamespace("MAIL:SUMM:ORGROOM", 0, 2, tmplput_MAIL_SUMM_ORGROOM,  CTX_MAILSUM);
1118         RegisterNamespace("MAIL:SUMM:RFCA", 0, 2, tmplput_MAIL_SUMM_RFCA,  CTX_MAILSUM);
1119         RegisterNamespace("MAIL:SUMM:OTHERNODE", 2, 0, tmplput_MAIL_SUMM_OTHERNODE,  CTX_MAILSUM);
1120         RegisterNamespace("MAIL:SUMM:REFIDS", 0, 0, tmplput_MAIL_SUMM_REFIDS,  CTX_MAILSUM);
1121         RegisterNamespace("MAIL:SUMM:INREPLYTO", 0, 2, tmplput_MAIL_SUMM_INREPLYTO,  CTX_MAILSUM);
1122         RegisterNamespace("MAIL:BODY", 0, 2, tmplput_MAIL_BODY,  CTX_MAILSUM);
1123         RegisterNamespace("MAIL:QUOTETEXT", 1, 2, tmplput_QUOTED_MAIL_BODY,  CTX_NONE);
1124         RegisterConditional(HKEY("COND:ROOM:DISPLAYMSG"), 0, Conditional_ROOM_DISPLAY_MSG, CTX_MAILSUM);
1125         RegisterConditional(HKEY("COND:MAIL:SUMM:LASTMSG"), 0, Conditional_MAIL_SUMM_LASTMSG, CTX_MAILSUM);
1126         RegisterConditional(HKEY("COND:MAIL:SUMM:RFCA"), 0, Conditional_MAIL_SUMM_RFCA,  CTX_MAILSUM);
1127         RegisterConditional(HKEY("COND:MAIL:SUMM:UNREAD"), 0, Conditional_MAIL_SUMM_UNREAD, CTX_MAILSUM);
1128         RegisterConditional(HKEY("COND:MAIL:SUMM:H_NODE"), 0, Conditional_MAIL_SUMM_H_NODE, CTX_MAILSUM);
1129         RegisterConditional(HKEY("COND:MAIL:SUMM:OTHERNODE"), 0, Conditional_MAIL_SUMM_OTHERNODE, CTX_MAILSUM);
1130         RegisterConditional(HKEY("COND:MAIL:ANON"), 0, Conditional_ANONYMOUS_MESSAGE, CTX_MAILSUM);
1131         
1132
1133         /* do we have mimetypes to iterate over? */
1134         RegisterConditional(HKEY("COND:MAIL:MIME:ATTACH"), 0, Conditional_MAIL_MIME_ALL, CTX_MAILSUM);
1135         RegisterConditional(HKEY("COND:MAIL:MIME:ATTACH:SUBMESSAGES"), 0, Conditional_MAIL_MIME_SUBMESSAGES, CTX_MAILSUM);
1136         RegisterConditional(HKEY("COND:MAIL:MIME:ATTACH:LINKS"), 0, Conditional_MAIL_MIME_ATTACHLINKS, CTX_MAILSUM);
1137         RegisterConditional(HKEY("COND:MAIL:MIME:ATTACH:ATT"), 0, Conditional_MAIL_MIME_ATTACH, CTX_MAILSUM);
1138         RegisterIterator("MAIL:MIME:ATTACH", 0, NULL, iterate_get_mime_All, 
1139                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM, IT_NOFLAG);
1140         RegisterIterator("MAIL:MIME:ATTACH:SUBMESSAGES", 0, NULL, iterate_get_mime_Submessages, 
1141                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM, IT_NOFLAG);
1142         RegisterIterator("MAIL:MIME:ATTACH:LINKS", 0, NULL, iterate_get_mime_AttachLinks, 
1143                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM, IT_NOFLAG);
1144         RegisterIterator("MAIL:MIME:ATTACH:ATT", 0, NULL, iterate_get_mime_Attachments, 
1145                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM, IT_NOFLAG);
1146
1147         /* Parts of a mime attachent */
1148         RegisterNamespace("MAIL:MIME:NAME", 0, 2, tmplput_MIME_Name, CTX_MIME_ATACH);
1149         RegisterNamespace("MAIL:MIME:FILENAME", 0, 2, tmplput_MIME_FileName, CTX_MIME_ATACH);
1150         RegisterNamespace("MAIL:MIME:PARTNUM", 0, 2, tmplput_MIME_PartNum, CTX_MIME_ATACH);
1151         RegisterNamespace("MAIL:MIME:MSGNUM", 0, 2, tmplput_MIME_MsgNum, CTX_MIME_ATACH);
1152         RegisterNamespace("MAIL:MIME:DISPOSITION", 0, 2, tmplput_MIME_Disposition, CTX_MIME_ATACH);
1153         RegisterNamespace("MAIL:MIME:CONTENTTYPE", 0, 2, tmplput_MIME_ContentType, CTX_MIME_ATACH);
1154         RegisterNamespace("MAIL:MIME:CHARSET", 0, 2, tmplput_MIME_Charset, CTX_MIME_ATACH);
1155         RegisterNamespace("MAIL:MIME:LENGTH", 0, 2, tmplput_MIME_Length, CTX_MIME_ATACH);
1156         RegisterNamespace("MAIL:MIME:DATA", 0, 2, tmplput_MIME_Data, CTX_MIME_ATACH);
1157         /* load the actual attachment into WC->attachments; no output!!! */
1158         RegisterNamespace("MAIL:MIME:LOADDATA", 0, 0, tmplput_MIME_LoadData, CTX_MIME_ATACH);
1159
1160         /* iterate the WC->attachments; use the above tokens for their contents */
1161         RegisterIterator("MSG:ATTACHNAMES", 0, NULL, iterate_get_registered_Attachments, 
1162                          NULL, NULL, CTX_MIME_ATACH, CTX_NONE, IT_NOFLAG);
1163
1164         /* mime renderers translate an attachment into webcit viewable html text */
1165         RegisterMimeRenderer(HKEY("message/rfc822"), render_MAIL);
1166         RegisterMimeRenderer(HKEY("text/x-vcard"), render_MIME_VCard);
1167         RegisterMimeRenderer(HKEY("text/vcard"), render_MIME_VCard);
1168         RegisterMimeRenderer(HKEY("text/calendar"), render_MIME_ICS);
1169         RegisterMimeRenderer(HKEY("application/ics"), render_MIME_ICS);
1170         RegisterMimeRenderer(HKEY("text/x-citadel-variformat"), render_MAIL_variformat);
1171         RegisterMimeRenderer(HKEY("text/plain"), render_MAIL_text_plain);
1172         RegisterMimeRenderer(HKEY("text"), render_MAIL_text_plain);
1173         RegisterMimeRenderer(HKEY("text/html"), render_MAIL_html);
1174         RegisterMimeRenderer(HKEY(""), render_MAIL_UNKNOWN);
1175
1176         /* these headers are citserver replies to MSG4 and friends. one evaluator for each */
1177         RegisterMsgHdr(HKEY("nhdr"), examine_nhdr, 0);
1178         RegisterMsgHdr(HKEY("type"), examine_type, 0);
1179         RegisterMsgHdr(HKEY("from"), examine_from, 0);
1180         RegisterMsgHdr(HKEY("subj"), examine_subj, 0);
1181         RegisterMsgHdr(HKEY("msgn"), examine_msgn, 0);
1182         RegisterMsgHdr(HKEY("wefw"), examine_wefw, 0);
1183         RegisterMsgHdr(HKEY("cccc"), examine_cccc, 0);
1184         RegisterMsgHdr(HKEY("hnod"), examine_hnod, 0);
1185         RegisterMsgHdr(HKEY("room"), examine_room, 0);
1186         RegisterMsgHdr(HKEY("rfca"), examine_rfca, 0);
1187         RegisterMsgHdr(HKEY("node"), examine_node, 0);
1188         RegisterMsgHdr(HKEY("rcpt"), examine_rcpt, 0);
1189         RegisterMsgHdr(HKEY("time"), examine_time, 0);
1190         RegisterMsgHdr(HKEY("part"), examine_mime_part, 0);
1191         RegisterMsgHdr(HKEY("text"), examine_text, 1);
1192         /* these are the content-type headers we get infront of a message; put it into the same hash since it doesn't clash. */
1193         RegisterMsgHdr(HKEY("X-Citadel-MSG4-Partnum"), examine_msg4_partnum, 0);
1194         RegisterMsgHdr(HKEY("Content-type"), examine_content_type, 0);
1195         RegisterMsgHdr(HKEY("Content-length"), examine_content_lengh, 0);
1196         RegisterMsgHdr(HKEY("Content-transfer-encoding"), examine_content_encoding, 0); /* do we care? */
1197         RegisterMsgHdr(HKEY("charset"), examine_charset, 0);
1198
1199         /* Don't care about these... */
1200         RegisterMsgHdr(HKEY("pref"), examine_pref, 0);
1201         RegisterMsgHdr(HKEY("suff"), examine_suff, 0);
1202         RegisterMsgHdr(HKEY("path"), examine_path, 0);
1203 }