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