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