d68911edcec5adc16cc0cb6e675f47734f6f7566
[citadel] / webcit / msg_renderers.c
1 #include "webcit.h"
2 #include "webserver.h"
3 #include "dav.h"
4
5 CtxType CTX_MAILSUM = CTX_NONE;
6 CtxType CTX_MIME_ATACH = CTX_NONE;
7
8 static inline void CheckConvertBufs(struct wcsession *WCC)
9 {
10         if (WCC->ConvertBuf1 == NULL)
11                 WCC->ConvertBuf1 = NewStrBuf();
12         if (WCC->ConvertBuf2 == NULL)
13                 WCC->ConvertBuf2 = NewStrBuf();
14 }
15
16 /*
17  * message index functions
18  */
19
20
21 void DestroyMimeParts(wc_mime_attachment *Mime)
22 {
23         FreeStrBuf(&Mime->Name);
24         FreeStrBuf(&Mime->FileName);
25         FreeStrBuf(&Mime->PartNum);
26         FreeStrBuf(&Mime->Disposition);
27         FreeStrBuf(&Mime->ContentType);
28         FreeStrBuf(&Mime->Charset);
29         FreeStrBuf(&Mime->Data);
30 }
31
32 void DestroyMime(void *vMime)
33 {
34         wc_mime_attachment *Mime = (wc_mime_attachment*)vMime;
35         DestroyMimeParts(Mime);
36         free(Mime);
37 }
38
39 void DestroyMessageSummary(void *vMsg)
40 {
41         message_summary *Msg = (message_summary*) vMsg;
42
43         FreeStrBuf(&Msg->from);
44         FreeStrBuf(&Msg->to);
45         FreeStrBuf(&Msg->subj);
46         FreeStrBuf(&Msg->reply_inreplyto);
47         FreeStrBuf(&Msg->reply_references);
48         FreeStrBuf(&Msg->cccc);
49         FreeStrBuf(&Msg->ReplyTo);
50         FreeStrBuf(&Msg->hnod);
51         FreeStrBuf(&Msg->AllRcpt);
52         FreeStrBuf(&Msg->Room);
53         FreeStrBuf(&Msg->Rfca);
54         FreeStrBuf(&Msg->EnvTo);
55         FreeStrBuf(&Msg->OtherNode);
56
57         DeleteHash(&Msg->Attachments);  /* list of Attachments */
58         DeleteHash(&Msg->Submessages);
59         DeleteHash(&Msg->AttachLinks);
60         DeleteHash(&Msg->AllAttach);
61         free(Msg);
62 }
63
64
65
66 void RegisterMsgHdr(const char *HeaderName, long HdrNLen, ExamineMsgHeaderFunc evaluator, int type)
67 {
68         headereval *ev;
69         ev = (headereval*) malloc(sizeof(headereval));
70         ev->evaluator = evaluator;
71         ev->Type = type;
72         Put(MsgHeaderHandler, HeaderName, HdrNLen, ev, NULL);
73 }
74
75 void RegisterMimeRenderer(const char *HeaderName, long HdrNLen, 
76                           RenderMimeFunc MimeRenderer,
77                           int InlineRenderable,
78                           int Priority)
79 {
80         RenderMimeFuncStruct *f;
81
82         f = (RenderMimeFuncStruct*) malloc(sizeof(RenderMimeFuncStruct));
83         f->f = MimeRenderer;
84         Put(MimeRenderHandler, HeaderName, HdrNLen, f, NULL);
85         if (InlineRenderable)
86                 RegisterEmbeddableMimeType(HeaderName, HdrNLen, 10000 - Priority);
87 }
88
89 /*----------------------------------------------------------------------------*/
90
91 /*
92  *  comparator for two longs in descending order.
93  */
94 int longcmp_r(const void *s1, const void *s2) {
95         long l1;
96         long l2;
97
98         l1 = *(long *)GetSearchPayload(s1);
99         l2 = *(long *)GetSearchPayload(s2);
100
101         if (l1 > l2) return(-1);
102         if (l1 < l2) return(+1);
103         return(0);
104 }
105
106 /*
107  *  comparator for longs; descending order.
108  */
109 int qlongcmp_r(const void *s1, const void *s2) {
110         long l1 = (long) s1;
111         long l2 = (long) s2;
112
113         if (l1 > l2) return(-1);
114         if (l1 < l2) return(+1);
115         return(0);
116 }
117
118  
119 /*
120  * comparator for message summary structs by ascending subject.
121  */
122 int summcmp_subj(const void *s1, const void *s2) {
123         message_summary *summ1;
124         message_summary *summ2;
125         
126         summ1 = (message_summary *)GetSearchPayload(s1);
127         summ2 = (message_summary *)GetSearchPayload(s2);
128         return strcasecmp(ChrPtr(summ1->subj), ChrPtr(summ2->subj));
129 }
130
131 /*
132  * comparator for message summary structs by descending subject.
133  */
134 int summcmp_rsubj(const void *s1, const void *s2) {
135         message_summary *summ1;
136         message_summary *summ2;
137         
138         summ1 = (message_summary *)GetSearchPayload(s1);
139         summ2 = (message_summary *)GetSearchPayload(s2);
140         return strcasecmp(ChrPtr(summ2->subj), ChrPtr(summ1->subj));
141 }
142 /*
143  * comparator for message summary structs by descending subject.
144  */
145 int groupchange_subj(const void *s1, const void *s2) {
146         message_summary *summ1;
147         message_summary *summ2;
148         
149         summ1 = (message_summary *)s1;
150         summ2 = (message_summary *)s2;
151         return ChrPtr(summ2->subj)[0] != ChrPtr(summ1->subj)[0];
152 }
153
154 /*
155  * comparator for message summary structs by ascending sender.
156  */
157 int summcmp_sender(const void *s1, const void *s2) {
158         message_summary *summ1;
159         message_summary *summ2;
160         
161         summ1 = (message_summary *)GetSearchPayload(s1);
162         summ2 = (message_summary *)GetSearchPayload(s2);
163         return strcasecmp(ChrPtr(summ1->from), ChrPtr(summ2->from));
164 }
165
166 /*
167  * comparator for message summary structs by descending sender.
168  */
169 int summcmp_rsender(const void *s1, const void *s2) {
170         message_summary *summ1;
171         message_summary *summ2;
172         
173         summ1 = (message_summary *)GetSearchPayload(s1);
174         summ2 = (message_summary *)GetSearchPayload(s2);
175         return strcasecmp(ChrPtr(summ2->from), ChrPtr(summ1->from));
176 }
177 /*
178  * comparator for message summary structs by descending sender.
179  */
180 int groupchange_sender(const void *s1, const void *s2) {
181         message_summary *summ1;
182         message_summary *summ2;
183         
184         summ1 = (message_summary *)s1;
185         summ2 = (message_summary *)s2;
186         return strcasecmp(ChrPtr(summ2->from), ChrPtr(summ1->from)) != 0;
187
188 }
189
190 /*
191  * comparator for message summary structs by ascending date.
192  */
193 int summcmp_date(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 int summcmp_rdate(const void *s1, const void *s2) {
209         message_summary *summ1;
210         message_summary *summ2;
211         
212         summ1 = (message_summary *)GetSearchPayload(s1);
213         summ2 = (message_summary *)GetSearchPayload(s2);
214
215         if (summ1->date < summ2->date) return +1;
216         else if (summ1->date > summ2->date) return -1;
217         else return 0;
218 }
219
220 /*
221  * comparator for message summary structs by descending date.
222  */
223 const long DAYSECONDS = 24 * 60 * 60;
224 int groupchange_date(const void *s1, const void *s2) {
225         message_summary *summ1;
226         message_summary *summ2;
227         
228         summ1 = (message_summary *)s1;
229         summ2 = (message_summary *)s2;
230
231         return (summ1->date % DAYSECONDS) != (summ2->date %DAYSECONDS);
232 }
233
234
235 /*----------------------------------------------------------------------------*/
236 /* Don't wanna know... or? */
237 void examine_pref(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset) {return;}
238 void examine_suff(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset) {return;}
239 void examine_path(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset) {return;}
240 void examine_content_encoding(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
241 {
242 /* TODO: do we care? */
243 }
244
245 void examine_nhdr(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
246 {
247         Msg->nhdr = 0;
248         if (!strncasecmp(ChrPtr(HdrLine), "yes", 8))
249                 Msg->nhdr = 1;
250 }
251 int Conditional_ANONYMOUS_MESSAGE(StrBuf *Target, WCTemplputParams *TP)
252 {
253         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
254         return Msg->nhdr != 0;
255 }
256
257 void examine_type(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
258 {
259         Msg->format_type = StrToi(HdrLine);
260                         
261 }
262
263 void examine_from(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
264 {
265         wcsession *WCC = WC;
266
267         CheckConvertBufs(WCC);
268         FreeStrBuf(&Msg->from);
269         Msg->from = NewStrBufPlain(NULL, StrLength(HdrLine));
270         StrBuf_RFC822_2_Utf8(Msg->from, 
271                              HdrLine, 
272                              WCC->DefaultCharset, 
273                              FoundCharset,
274                              WCC->ConvertBuf1,
275                              WCC->ConvertBuf2);
276 }
277 void tmplput_MAIL_SUMM_FROM(StrBuf *Target, WCTemplputParams *TP)
278 {
279         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
280         StrBufAppendTemplate(Target, TP, Msg->from, 0);
281 }
282
283 void examine_subj(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
284 {
285         wcsession *WCC = WC;
286
287         CheckConvertBufs(WCC);
288         FreeStrBuf(&Msg->subj);
289         Msg->subj = NewStrBufPlain(NULL, StrLength(HdrLine));
290         StrBuf_RFC822_2_Utf8(Msg->subj, 
291                              HdrLine, 
292                              WCC->DefaultCharset, 
293                              FoundCharset,
294                              WCC->ConvertBuf1,
295                              WCC->ConvertBuf2);
296 }
297 void tmplput_MAIL_SUMM_SUBJECT(StrBuf *Target, WCTemplputParams *TP)
298 {
299         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
300
301         if (TP->Tokens->nParameters == 4)
302         {
303                 const char *pch;
304                 long len;
305                 
306                 GetTemplateTokenString(Target, TP, 3, &pch, &len);
307                 if ((len > 0)&&
308                     (strstr(ChrPtr(Msg->subj), pch) == NULL))
309                 {
310                         GetTemplateTokenString(Target, TP, 2, &pch, &len);
311                         StrBufAppendBufPlain(Target, pch, len, 0);
312                 }
313         }
314         StrBufAppendTemplate(Target, TP, Msg->subj, 0);
315 }
316 int Conditional_MAIL_SUMM_SUBJECT(StrBuf *Target, WCTemplputParams *TP)
317 {
318         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
319
320
321         return StrLength(Msg->subj) > 0;
322 }
323
324
325 void examine_msgn(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
326 {
327         wcsession *WCC = WC;
328
329         CheckConvertBufs(WCC);
330         FreeStrBuf(&Msg->reply_inreplyto);
331         Msg->reply_inreplyto = NewStrBufPlain(NULL, StrLength(HdrLine));
332         Msg->reply_inreplyto_hash = ThreadIdHash(HdrLine);
333         StrBuf_RFC822_2_Utf8(Msg->reply_inreplyto, 
334                              HdrLine, 
335                              WCC->DefaultCharset,
336                              FoundCharset,
337                              WCC->ConvertBuf1,
338                              WCC->ConvertBuf2);
339 }
340 void tmplput_MAIL_SUMM_INREPLYTO(StrBuf *Target, WCTemplputParams *TP)
341 {
342         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
343         StrBufAppendTemplate(Target, TP, Msg->reply_inreplyto, 0);
344 }
345
346 int Conditional_MAIL_SUMM_UNREAD(StrBuf *Target, WCTemplputParams *TP)
347 {
348         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
349         return (Msg->Flags & MSGFLAG_READ) != 0;
350 }
351
352 void examine_wefw(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
353 {
354         wcsession *WCC = WC;
355
356         CheckConvertBufs(WCC);
357         FreeStrBuf(&Msg->reply_references);
358         Msg->reply_references = NewStrBufPlain(NULL, StrLength(HdrLine));
359         Msg->reply_references_hash = ThreadIdHash(HdrLine);
360         StrBuf_RFC822_2_Utf8(Msg->reply_references, 
361                              HdrLine, 
362                              WCC->DefaultCharset, 
363                              FoundCharset,
364                              WCC->ConvertBuf1,
365                              WCC->ConvertBuf2);
366 }
367 void tmplput_MAIL_SUMM_REFIDS(StrBuf *Target, WCTemplputParams *TP)
368 {
369         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
370         StrBufAppendTemplate(Target, TP, Msg->reply_references, 0);
371 }
372
373 void examine_replyto(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
374 {
375         wcsession *WCC = WC;
376
377         CheckConvertBufs(WCC);
378         FreeStrBuf(&Msg->ReplyTo);
379         Msg->ReplyTo = NewStrBufPlain(NULL, StrLength(HdrLine));
380         StrBuf_RFC822_2_Utf8(Msg->ReplyTo, 
381                              HdrLine, 
382                              WCC->DefaultCharset, 
383                              FoundCharset,
384                              WCC->ConvertBuf1,
385                              WCC->ConvertBuf2);
386         if (Msg->AllRcpt == NULL)
387                 Msg->AllRcpt = NewStrBufPlain(NULL, StrLength(HdrLine));
388         if (StrLength(Msg->AllRcpt) > 0) {
389                 StrBufAppendBufPlain(Msg->AllRcpt, HKEY(", "), 0);
390         }
391         StrBufAppendBuf(Msg->AllRcpt, Msg->ReplyTo, 0);
392 }
393 void tmplput_MAIL_SUMM_REPLYTO(StrBuf *Target, WCTemplputParams *TP)
394 {
395         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
396         StrBufAppendTemplate(Target, TP, Msg->ReplyTo, 0);
397 }
398
399 void examine_cccc(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
400 {
401         wcsession *WCC = WC;
402
403         CheckConvertBufs(WCC);
404         FreeStrBuf(&Msg->cccc);
405         Msg->cccc = NewStrBufPlain(NULL, StrLength(HdrLine));
406         StrBuf_RFC822_2_Utf8(Msg->cccc, 
407                              HdrLine, 
408                              WCC->DefaultCharset, 
409                              FoundCharset,
410                              WCC->ConvertBuf1,
411                              WCC->ConvertBuf2);
412         if (Msg->AllRcpt == NULL)
413                 Msg->AllRcpt = NewStrBufPlain(NULL, StrLength(HdrLine));
414         if (StrLength(Msg->AllRcpt) > 0) {
415                 StrBufAppendBufPlain(Msg->AllRcpt, HKEY(", "), 0);
416         }
417         StrBufAppendBuf(Msg->AllRcpt, Msg->cccc, 0);
418 }
419 void tmplput_MAIL_SUMM_CCCC(StrBuf *Target, WCTemplputParams *TP)
420 {
421         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
422         StrBufAppendTemplate(Target, TP, Msg->cccc, 0);
423 }
424
425
426 void examine_room(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
427 {
428         if ((StrLength(HdrLine) > 0) &&
429             (strcasecmp(ChrPtr(HdrLine), ChrPtr(WC->CurRoom.name)))) {
430                 FreeStrBuf(&Msg->Room);
431                 Msg->Room = NewStrBufDup(HdrLine);              
432         }
433 }
434 void tmplput_MAIL_SUMM_ORGROOM(StrBuf *Target, WCTemplputParams *TP)
435 {
436         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
437         StrBufAppendTemplate(Target, TP, Msg->Room, 0);
438 }
439
440
441 void examine_rfca(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
442 {
443         FreeStrBuf(&Msg->Rfca);
444         Msg->Rfca = NewStrBufDup(HdrLine);
445 }
446 void tmplput_MAIL_SUMM_RFCA(StrBuf *Target, WCTemplputParams *TP)
447 {
448         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
449         StrBufAppendTemplate(Target, TP, Msg->Rfca, 0);
450 }
451 int Conditional_MAIL_SUMM_RFCA(StrBuf *Target, WCTemplputParams *TP)
452 {
453         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
454         return StrLength(Msg->Rfca) > 0;
455 }
456 int Conditional_MAIL_SUMM_CCCC(StrBuf *Target, WCTemplputParams *TP)
457 {
458         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
459         return StrLength(Msg->cccc) > 0;
460 }
461 int Conditional_MAIL_SUMM_REPLYTO(StrBuf *Target, WCTemplputParams *TP)
462 {
463         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
464         return StrLength(Msg->ReplyTo) > 0;
465 }
466
467 void examine_node(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
468 {
469         wcsession *WCC = WC;
470
471         if ( (StrLength(HdrLine) > 0) &&
472              ((WC->CurRoom.QRFlags & QR_NETWORK)
473               || ((strcasecmp(ChrPtr(HdrLine), ChrPtr(WCC->serv_info->serv_nodename))
474                    && (strcasecmp(ChrPtr(HdrLine), ChrPtr(WCC->serv_info->serv_fqdn))))))) {
475                 FreeStrBuf(&Msg->OtherNode);
476                 Msg->OtherNode = NewStrBufDup(HdrLine);
477         }
478 }
479 void tmplput_MAIL_SUMM_OTHERNODE(StrBuf *Target, WCTemplputParams *TP)
480 {
481         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
482         StrBufAppendTemplate(Target, TP, Msg->OtherNode, 0);
483 }
484 int Conditional_MAIL_SUMM_OTHERNODE(StrBuf *Target, WCTemplputParams *TP)
485 {
486         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
487         return StrLength(Msg->OtherNode) > 0;
488 }
489
490 void examine_nvto(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
491 {
492         wcsession *WCC = WC;
493
494         CheckConvertBufs(WCC);
495         FreeStrBuf(&Msg->EnvTo);
496         Msg->EnvTo = NewStrBufPlain(NULL, StrLength(HdrLine));
497         StrBuf_RFC822_2_Utf8(Msg->EnvTo, 
498                              HdrLine, 
499                              WCC->DefaultCharset, 
500                              FoundCharset,
501                              WCC->ConvertBuf1,
502                              WCC->ConvertBuf2);
503 }
504
505
506 void examine_rcpt(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
507 {
508         wcsession *WCC = WC;
509
510         CheckConvertBufs(WCC);
511         FreeStrBuf(&Msg->to);
512         Msg->to = NewStrBufPlain(NULL, StrLength(HdrLine));
513         StrBuf_RFC822_2_Utf8(Msg->to, 
514                              HdrLine, 
515                              WCC->DefaultCharset, 
516                              FoundCharset,
517                              WCC->ConvertBuf1,
518                              WCC->ConvertBuf2);
519         if (Msg->AllRcpt == NULL)
520                 Msg->AllRcpt = NewStrBufPlain(NULL, StrLength(HdrLine));
521         if (StrLength(Msg->AllRcpt) > 0) {
522                 StrBufAppendBufPlain(Msg->AllRcpt, HKEY(", "), 0);
523         }
524         StrBufAppendBuf(Msg->AllRcpt, Msg->to, 0);
525 }
526 void tmplput_MAIL_SUMM_TO(StrBuf *Target, WCTemplputParams *TP)
527 {
528         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
529         StrBufAppendTemplate(Target, TP, Msg->to, 0);
530 }
531 int Conditional_MAIL_SUMM_TO(StrBuf *Target, WCTemplputParams *TP) 
532 {
533         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
534         return StrLength(Msg->to) != 0;
535 }
536 int Conditional_MAIL_SUMM_SUBJ(StrBuf *Target, WCTemplputParams *TP) 
537 {
538         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
539         return StrLength(Msg->subj) != 0;
540 }
541 void tmplput_MAIL_SUMM_ALLRCPT(StrBuf *Target, WCTemplputParams *TP)
542 {
543         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
544         StrBufAppendTemplate(Target, TP, Msg->AllRcpt, 0);
545 }
546
547
548
549 void tmplput_SUMM_COUNT(StrBuf *Target, WCTemplputParams *TP)
550 {
551         StrBufAppendPrintf(Target, "%d", GetCount( WC->summ));
552 }
553
554 HashList *iterate_get_mailsumm_All(StrBuf *Target, WCTemplputParams *TP)
555 {
556         return WC->summ;
557 }
558 void examine_time(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
559 {
560         Msg->date = StrTol(HdrLine);
561 }
562
563 void tmplput_MAIL_SUMM_DATE_BRIEF(StrBuf *Target, WCTemplputParams *TP)
564 {
565         char datebuf[64];
566         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
567         webcit_fmt_date(datebuf, 64, Msg->date, DATEFMT_BRIEF);
568         StrBufAppendBufPlain(Target, datebuf, -1, 0);
569 }
570
571 void tmplput_MAIL_SUMM_EUID(StrBuf *Target, WCTemplputParams *TP)
572 {
573         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
574         StrBufAppendTemplate(Target, TP, Msg->euid, 0);
575 }
576
577 void tmplput_MAIL_SUMM_DATE_FULL(StrBuf *Target, WCTemplputParams *TP)
578 {
579         char datebuf[64];
580         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
581         webcit_fmt_date(datebuf, 64, Msg->date, DATEFMT_FULL);
582         StrBufAppendBufPlain(Target, datebuf, -1, 0);
583 }
584 void tmplput_MAIL_SUMM_DATE_NO(StrBuf *Target, WCTemplputParams *TP)
585 {
586         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
587         StrBufAppendPrintf(Target, "%ld", Msg->date, 0);
588 }
589
590
591
592 void render_MAIL(StrBuf *Target, WCTemplputParams *TP, StrBuf *FoundCharset)
593 {
594         wc_mime_attachment *Mime = (wc_mime_attachment *) CTX(CTX_MIME_ATACH);
595         const StrBuf *TemplateMime;
596
597         if (Mime->Data == NULL) 
598                 Mime->Data = NewStrBufPlain(NULL, Mime->length);
599         else 
600                 FlushStrBuf(Mime->Data);
601         read_message(Mime->Data, HKEY("view_submessage"), Mime->msgnum, Mime->PartNum, &TemplateMime);
602 /*
603         if ( (!IsEmptyStr(mime_submessages)) && (!section[0]) ) {
604                 for (i=0; i<num_tokens(mime_submessages, '|'); ++i) {
605                         extract_token(buf, mime_submessages, i, '|', sizeof buf);
606                         / ** use printable_view to suppress buttons * /
607                         wc_printf("<blockquote>");
608                         read_message(Mime->msgnum, 1, ChrPtr(Mime->Section));
609                         wc_printf("</blockquote>");
610                 }
611         }
612 */
613 }
614
615 void render_MIME_ICS(StrBuf *Target, WCTemplputParams *TP, StrBuf *FoundCharset)
616 {
617         wc_mime_attachment *Mime = (wc_mime_attachment *) CTX(CTX_MIME_ATACH);
618         if (StrLength(Mime->Data) == 0) {
619                 MimeLoadData(Mime);
620         }
621         if (StrLength(Mime->Data) > 0) {
622                 cal_process_attachment(Mime);
623         }
624 }
625
626
627
628 void examine_mime_part(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
629 {
630         const char *Ptr = NULL;
631         wc_mime_attachment *Mime;
632         StrBuf *Buf;
633         wcsession *WCC = WC;
634
635         CheckConvertBufs(WCC);  
636         Mime = (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
637         memset(Mime, 0, sizeof(wc_mime_attachment));
638         Mime->msgnum = Msg->msgnum;
639         Buf = NewStrBuf();
640
641         Mime->Name = NewStrBuf();
642         StrBufExtract_NextToken(Buf, HdrLine, &Ptr, '|');
643         StrBuf_RFC822_2_Utf8(Mime->Name, 
644                              Buf, 
645                              WCC->DefaultCharset, 
646                              FoundCharset,
647                              WCC->ConvertBuf1,
648                              WCC->ConvertBuf2);
649         StrBufTrim(Mime->Name);
650
651         StrBufExtract_NextToken(Buf, HdrLine, &Ptr, '|');
652         Mime->FileName = NewStrBuf();
653         StrBuf_RFC822_2_Utf8(Mime->FileName, 
654                              Buf, 
655                              WCC->DefaultCharset, 
656                              FoundCharset,
657                              WCC->ConvertBuf1,
658                              WCC->ConvertBuf2);
659         StrBufTrim(Mime->FileName);
660
661         Mime->PartNum = NewStrBuf();
662         StrBufExtract_NextToken(Mime->PartNum, HdrLine, &Ptr, '|');
663         StrBufTrim(Mime->PartNum);
664         if (strchr(ChrPtr(Mime->PartNum), '.') != NULL) 
665                 Mime->level = 2;
666         else
667                 Mime->level = 1;
668
669         Mime->Disposition = NewStrBuf();
670         StrBufExtract_NextToken(Mime->Disposition, HdrLine, &Ptr, '|');
671
672         Mime->ContentType = NewStrBuf();
673         StrBufExtract_NextToken(Mime->ContentType, HdrLine, &Ptr, '|');
674         StrBufTrim(Mime->ContentType);
675         StrBufLowerCase(Mime->ContentType);
676         if (!strcmp(ChrPtr(Mime->ContentType), "application/octet-stream")) {
677                 StrBufPlain(Mime->ContentType, 
678                             GuessMimeByFilename(SKEY(Mime->FileName)), -1);
679         }
680
681         Mime->length = StrBufExtractNext_int(HdrLine, &Ptr, '|');
682
683         StrBufSkip_NTokenS(HdrLine, &Ptr, '|', 1);  /* cbid?? */
684
685         Mime->Charset = NewStrBuf();
686         StrBufExtract_NextToken(Mime->Charset, HdrLine, &Ptr, '|');
687
688
689         if ( (StrLength(Mime->FileName) == 0) && (StrLength(Mime->Name) > 0) ) {
690                 StrBufAppendBuf(Mime->FileName, Mime->Name, 0);
691         }
692
693         if (StrLength(Msg->PartNum) > 0) {
694                 StrBuf *tmp;
695                 StrBufPrintf(Buf, "%s.%s", ChrPtr(Msg->PartNum), ChrPtr(Mime->PartNum));
696                 tmp = Mime->PartNum;
697                 Mime->PartNum = Buf;
698                 Buf = tmp;
699         }
700
701         if (Msg->AllAttach == NULL)
702                 Msg->AllAttach = NewHash(1,NULL);
703         Put(Msg->AllAttach, SKEY(Mime->PartNum), Mime, DestroyMime);
704         FreeStrBuf(&Buf);
705 }
706
707
708 void evaluate_mime_part(StrBuf *Target, WCTemplputParams *TP)
709 {
710         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
711         wc_mime_attachment *Mime = (wc_mime_attachment *) CTX(CTX_MIME_ATACH);
712         void *vMimeRenderer;
713
714         /* just print the root-node */
715         if ((Mime->level >= 1) &&
716             GetHash(MimeRenderHandler, SKEY(Mime->ContentType), &vMimeRenderer) &&
717             vMimeRenderer != NULL)
718         {
719                 Mime->Renderer = (RenderMimeFuncStruct*) vMimeRenderer;
720                 if (Msg->Submessages == NULL)
721                         Msg->Submessages = NewHash(1,NULL);
722                 Put(Msg->Submessages, SKEY(Mime->PartNum), Mime, reference_free_handler);
723         }
724         else if ((Mime->level >= 1) &&
725                  (!strcasecmp(ChrPtr(Mime->Disposition), "inline"))
726                  && (!strncasecmp(ChrPtr(Mime->ContentType), "image/", 6)) ){
727                 if (Msg->AttachLinks == NULL)
728                         Msg->AttachLinks = NewHash(1,NULL);
729                 Put(Msg->AttachLinks, SKEY(Mime->PartNum), Mime, reference_free_handler);
730         }
731         else if ((Mime->level >= 1) &&
732                  (StrLength(Mime->ContentType) > 0) &&
733                   ( (!strcasecmp(ChrPtr(Mime->Disposition), "attachment")) 
734                     || (!strcasecmp(ChrPtr(Mime->Disposition), "inline"))
735                     || (!strcasecmp(ChrPtr(Mime->Disposition), ""))))
736         {               
737                 if (Msg->AttachLinks == NULL)
738                         Msg->AttachLinks = NewHash(1,NULL);
739                 Put(Msg->AttachLinks, SKEY(Mime->PartNum), Mime, reference_free_handler);
740                 if ((strcasecmp(ChrPtr(Mime->ContentType), "application/octet-stream") == 0) && 
741                     (StrLength(Mime->FileName) > 0)) {
742                         FlushStrBuf(Mime->ContentType);
743                         StrBufAppendBufPlain(Mime->ContentType,
744                                              GuessMimeByFilename(SKEY(Mime->FileName)),
745                                              -1, 0);
746                 }
747         }
748 }
749
750 void tmplput_MAIL_SUMM_NATTACH(StrBuf *Target, WCTemplputParams *TP)
751 {
752         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
753         StrBufAppendPrintf(Target, "%ld", GetCount(Msg->Attachments));
754 }
755
756
757 void examine_hnod(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
758 {
759         wcsession *WCC = WC;
760
761         CheckConvertBufs(WCC);
762         FreeStrBuf(&Msg->hnod);
763         Msg->hnod = NewStrBufPlain(NULL, StrLength(HdrLine));
764         StrBuf_RFC822_2_Utf8(Msg->hnod, 
765                              HdrLine, 
766                              WCC->DefaultCharset, 
767                              FoundCharset,
768                              WCC->ConvertBuf1,
769                              WCC->ConvertBuf2);
770 }
771 void tmplput_MAIL_SUMM_H_NODE(StrBuf *Target, WCTemplputParams *TP)
772 {
773         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
774         StrBufAppendTemplate(Target, TP, Msg->hnod, 0);
775 }
776 int Conditional_MAIL_SUMM_H_NODE(StrBuf *Target, WCTemplputParams *TP)
777 {
778         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
779         return StrLength(Msg->hnod) > 0;
780 }
781
782
783
784 void examine_text(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
785 {
786         if (Msg->MsgBody->Data == NULL)
787                 Msg->MsgBody->Data = NewStrBufPlain(NULL, SIZ);
788         else
789                 FlushStrBuf(Msg->MsgBody->Data);
790 }
791
792 void examine_msg4_partnum(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
793 {
794         Msg->MsgBody->PartNum = NewStrBufDup(HdrLine);
795         StrBufTrim(Msg->MsgBody->PartNum);
796 }
797
798 void examine_content_lengh(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
799 {
800         Msg->MsgBody->length = StrTol(HdrLine);
801         Msg->MsgBody->size_known = 1;
802 }
803
804 void examine_content_type(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
805 {
806         void *vHdr;
807         headereval *Hdr;
808         StrBuf *Token;
809         StrBuf *Value;
810         const char* sem;
811         const char *eq;
812         int len;
813         StrBufTrim(HdrLine);
814         Msg->MsgBody->ContentType = NewStrBufDup(HdrLine);
815         sem = strchr(ChrPtr(HdrLine), ';');
816
817         if (sem != NULL) {
818                 Token = NewStrBufPlain(NULL, StrLength(HdrLine));
819                 Value = NewStrBufPlain(NULL, StrLength(HdrLine));
820                 len = sem - ChrPtr(HdrLine);
821                 StrBufCutAt(Msg->MsgBody->ContentType, len, NULL);
822                 while (sem != NULL) {
823                         while (isspace(*(sem + 1)))
824                                 sem ++;
825                         StrBufCutLeft(HdrLine, sem - ChrPtr(HdrLine));
826                         sem = strchr(ChrPtr(HdrLine), ';');
827                         if (sem != NULL)
828                                 len = sem - ChrPtr(HdrLine);
829                         else
830                                 len = StrLength(HdrLine);
831                         FlushStrBuf(Token);
832                         FlushStrBuf(Value);
833                         StrBufAppendBufPlain(Token, ChrPtr(HdrLine), len, 0);
834                         eq = strchr(ChrPtr(Token), '=');
835                         if (eq != NULL) {
836                                 len = eq - ChrPtr(Token);
837                                 StrBufAppendBufPlain(Value, eq + 1, StrLength(Token) - len - 1, 0); 
838                                 StrBufCutAt(Token, len, NULL);
839                                 StrBufTrim(Value);
840                         }
841                         StrBufTrim(Token);
842
843                         if (GetHash(MsgHeaderHandler, SKEY(Token), &vHdr) &&
844                             (vHdr != NULL)) {
845                                 Hdr = (headereval*)vHdr;
846                                 Hdr->evaluator(Msg, Value, FoundCharset);
847                         }
848                         else syslog(LOG_WARNING, "don't know how to handle content type sub-header[%s]\n", ChrPtr(Token));
849                 }
850                 FreeStrBuf(&Token);
851                 FreeStrBuf(&Value);
852         }
853 }
854
855
856 int ReadOneMessageSummary(message_summary *Msg, StrBuf *FoundCharset, StrBuf *Buf)
857 {
858         void *vHdr;
859         headereval *Hdr;
860         const char *buf;
861         const char *ebuf;
862         int nBuf;
863         long len;
864         
865         serv_printf("MSG0 %ld|1", Msg->msgnum); /* ask for headers only */
866         
867         StrBuf_ServGetln(Buf);
868         if (GetServerStatus(Buf, NULL) != 1) {
869                 return 0;
870         }
871
872         while (len = StrBuf_ServGetln(Buf),
873                (len >= 0) && 
874                ((len != 3)  ||
875                 strcmp(ChrPtr(Buf), "000")))
876         {
877                 buf = ChrPtr(Buf);
878                 ebuf = strchr(ChrPtr(Buf), '=');
879                 nBuf = ebuf - buf;
880                 
881                 if (GetHash(MsgHeaderHandler, buf, nBuf, &vHdr) &&
882                     (vHdr != NULL)) {
883                         Hdr = (headereval*)vHdr;
884                         StrBufCutLeft(Buf, nBuf + 1);
885                         Hdr->evaluator(Msg, Buf, FoundCharset);
886                 }
887                 else syslog(LOG_INFO, "Don't know how to handle Message Headerline [%s]", ChrPtr(Buf));
888         }
889         return 1;
890 }
891
892 void tmplput_MAIL_SUMM_N(StrBuf *Target, WCTemplputParams *TP)
893 {
894         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
895         StrBufAppendPrintf(Target, "%ld", Msg->msgnum);
896 }
897
898
899 void tmplput_MAIL_SUMM_PERMALINK(StrBuf *Target, WCTemplputParams *TP)
900 {
901         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
902         StrBuf *perma_link;
903         const StrBuf *View;
904
905         perma_link = NewStrBufPlain(HKEY("/readfwd?go="));
906         StrBufUrlescAppend(perma_link, WC->CurRoom.name, NULL);
907         View = sbstr("view");
908         if (View != NULL) {
909                 StrBufAppendBufPlain(perma_link, HKEY("?view="), 0);
910                 StrBufAppendBuf(perma_link, View, 0);
911         }
912         StrBufAppendBufPlain(perma_link, HKEY("?start_reading_at="), 0);
913         StrBufAppendPrintf(perma_link, "%ld#%ld", Msg->msgnum, Msg->msgnum);
914         StrBufAppendBuf(Target, perma_link, 0);
915         FreeStrBuf(&perma_link);
916 }
917
918
919 int Conditional_MAIL_MIME_ALL(StrBuf *Target, WCTemplputParams *TP)
920 {
921         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
922         return GetCount(Msg->Attachments) > 0;
923 }
924
925 int Conditional_MAIL_MIME_SUBMESSAGES(StrBuf *Target, WCTemplputParams *TP)
926 {
927         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
928         return GetCount(Msg->Submessages) > 0;
929 }
930
931 int Conditional_MAIL_MIME_ATTACHLINKS(StrBuf *Target, WCTemplputParams *TP)
932 {
933         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
934         return GetCount(Msg->AttachLinks) > 0;
935 }
936
937 int Conditional_MAIL_MIME_ATTACH(StrBuf *Target, WCTemplputParams *TP)
938 {
939         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
940         return GetCount(Msg->AllAttach) > 0;
941 }
942
943 void tmplput_QUOTED_MAIL_BODY(StrBuf *Target, WCTemplputParams *TP)
944 {
945         const StrBuf *Mime;
946         long MsgNum;
947         StrBuf *Buf;
948
949         MsgNum = LBstr(TKEY(0));
950         Buf = NewStrBuf();
951         read_message(Buf, HKEY("view_message_replyquote"), MsgNum, NULL, &Mime);
952         StrBufAppendTemplate(Target, TP, Buf, 1);
953         FreeStrBuf(&Buf);
954 }
955
956 void tmplput_EDIT_MAIL_BODY(StrBuf *Target, WCTemplputParams *TP)
957 {
958         const StrBuf *Mime;
959         long MsgNum;
960         StrBuf *Buf;
961
962         MsgNum = LBstr(TKEY(0));
963         Buf = NewStrBuf();
964         read_message(Buf, HKEY("view_message_edit"), MsgNum, NULL, &Mime);
965         StrBufAppendTemplate(Target, TP, Buf, 1);
966         FreeStrBuf(&Buf);
967 }
968
969 void tmplput_EDIT_WIKI_BODY(StrBuf *Target, WCTemplputParams *TP)
970 {
971         const StrBuf *Mime;
972         long msgnum;
973         StrBuf *Buf;
974
975         /* Insert the existing content of the wiki page into the editor.  But we only want
976          * to do this the first time -- if the user is uploading an attachment we don't want
977          * to do it again.
978          */
979         if (!havebstr("attach_button")) {
980                 char *wikipage = strdup(bstr("page"));
981                 putbstr("format", NewStrBufPlain(HKEY("plain")));
982                 str_wiki_index(wikipage);
983                 msgnum = locate_message_by_uid(wikipage);
984                 free(wikipage);
985                 if (msgnum >= 0L) {
986                         Buf = NewStrBuf();
987                         read_message(Buf, HKEY("view_message_wikiedit"), msgnum, NULL, &Mime);
988                         StrBufAppendTemplate(Target, TP, Buf, 1);
989                         FreeStrBuf(&Buf);
990                 }
991         }
992 }
993
994 void tmplput_MAIL_BODY(StrBuf *Target, WCTemplputParams *TP)
995 {
996         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
997         StrBufAppendTemplate(Target, TP, Msg->MsgBody->Data, 0);
998 }
999
1000
1001 void render_MAIL_variformat(StrBuf *Target, WCTemplputParams *TP, StrBuf *FoundCharset)
1002 {
1003         /* Messages in legacy Citadel variformat get handled thusly... */
1004         wc_mime_attachment *Mime = (wc_mime_attachment *) CTX(CTX_MIME_ATACH);
1005         StrBuf *TTarget = NewStrBufPlain(NULL, StrLength(Mime->Data));
1006         FmOut(TTarget, "JUSTIFY", Mime->Data);
1007         FreeStrBuf(&Mime->Data);
1008         Mime->Data = TTarget;
1009 }
1010
1011 void render_MAIL_text_plain(StrBuf *Target, WCTemplputParams *TP, StrBuf *FoundCharset)
1012 {
1013         wc_mime_attachment *Mime = (wc_mime_attachment *) CTX(CTX_MIME_ATACH);
1014         const char *ptr, *pte;
1015         const char *BufPtr = NULL;
1016         StrBuf *Line;
1017         StrBuf *Line1;
1018         StrBuf *Line2;
1019         StrBuf *TTarget;
1020         long Linecount;
1021         long nEmptyLines;
1022         int bn = 0;
1023         int bq = 0;
1024         int i;
1025         long len;
1026 #ifdef HAVE_ICONV
1027         StrBuf *cs = NULL;
1028         int ConvertIt = 1;
1029         iconv_t ic = (iconv_t)(-1) ;
1030 #endif
1031
1032         if ((StrLength(Mime->Data) == 0) && (Mime->length > 0)) {
1033                 FreeStrBuf(&Mime->Data);
1034                 MimeLoadData(Mime);
1035         }
1036
1037 #ifdef HAVE_ICONV
1038         if (ConvertIt) {
1039                 if (StrLength(Mime->Charset) != 0)
1040                         cs = Mime->Charset;
1041                 else if (StrLength(FoundCharset) > 0)
1042                         cs = FoundCharset;
1043                 else if (StrLength(WC->DefaultCharset) > 0)
1044                         cs = WC->DefaultCharset;
1045                 if (cs == NULL) {
1046                         ConvertIt = 0;
1047                 }
1048                 else if (!strcasecmp(ChrPtr(cs), "utf-8")) {
1049                         ConvertIt = 0;
1050                 }
1051                 else if (!strcasecmp(ChrPtr(cs), "us-ascii")) {
1052                         ConvertIt = 0;
1053                 }
1054                 else {
1055                         ctdl_iconv_open("UTF-8", ChrPtr(cs), &ic);
1056                         if (ic == (iconv_t)(-1) ) {
1057                                 syslog(LOG_WARNING, "%s:%d iconv_open(UTF-8, %s) failed: %s\n",
1058                                         __FILE__, __LINE__, ChrPtr(Mime->Charset), strerror(errno));
1059                         }
1060                 }
1061         }
1062 #endif
1063         Line = NewStrBufPlain(NULL, SIZ);
1064         Line1 = NewStrBufPlain(NULL, SIZ);
1065         Line2 = NewStrBufPlain(NULL, SIZ);
1066         TTarget = NewStrBufPlain(NULL, StrLength(Mime->Data));
1067         Linecount = 0;
1068         nEmptyLines = 0;
1069         if (StrLength(Mime->Data) > 0) 
1070                 do 
1071                 {
1072                         StrBufSipLine(Line, Mime->Data, &BufPtr);
1073                         bq = 0;
1074                         i = 0;
1075                         ptr = ChrPtr(Line);
1076                         len = StrLength(Line);
1077                         pte = ptr + len;
1078                 
1079                         while ((ptr < pte) &&
1080                                ((*ptr == '>') ||
1081                                 isspace(*ptr)))
1082                         {
1083                                 if (*ptr == '>')
1084                                         bq++;
1085                                 ptr ++;
1086                                 i++;
1087                         }
1088                         if (i > 0) StrBufCutLeft(Line, i);
1089                 
1090                         if (StrLength(Line) == 0) {
1091                                 if (Linecount == 0)
1092                                         continue;
1093                                 StrBufAppendBufPlain(TTarget, HKEY("<tt></tt><br>\n"), 0);
1094
1095                                 nEmptyLines ++;
1096                                 continue;
1097                         }
1098                         nEmptyLines = 0;
1099                         for (i = bn; i < bq; i++)                               
1100                                 StrBufAppendBufPlain(TTarget, HKEY("<blockquote>"), 0);
1101                         for (i = bq; i < bn; i++)                               
1102                                 StrBufAppendBufPlain(TTarget, HKEY("</blockquote>"), 0);
1103 #ifdef HAVE_ICONV
1104                         if (ConvertIt) {
1105                                 StrBufConvert(Line, Line1, &ic);
1106                         }
1107 #endif
1108                         StrBufAppendBufPlain(TTarget, HKEY("<tt>"), 0);
1109                         UrlizeText(Line1, Line, Line2);
1110
1111                         StrEscAppend(TTarget, Line1, NULL, 0, 0);
1112                         StrBufAppendBufPlain(TTarget, HKEY("</tt><br>\n"), 0);
1113                         bn = bq;
1114                         Linecount ++;
1115                 }
1116         while ((BufPtr != StrBufNOTNULL) &&
1117                (BufPtr != NULL));
1118
1119         if (nEmptyLines > 0)
1120                 StrBufCutRight(TTarget, nEmptyLines * (sizeof ("<tt></tt><br>\n") - 1));
1121         for (i = 0; i < bn; i++)                                
1122                 StrBufAppendBufPlain(TTarget, HKEY("</blockquote>"), 0);
1123
1124         StrBufAppendBufPlain(TTarget, HKEY("</i><br>"), 0);
1125 #ifdef HAVE_ICONV
1126         if (ic != (iconv_t)(-1) ) {
1127                 iconv_close(ic);
1128         }
1129 #endif
1130
1131         FreeStrBuf(&Mime->Data);
1132         Mime->Data = TTarget;
1133         FlushStrBuf(Mime->ContentType);
1134         StrBufAppendBufPlain(Mime->ContentType, HKEY("text/html"), 0);
1135         FlushStrBuf(Mime->Charset);
1136         StrBufAppendBufPlain(Mime->Charset, HKEY("UTF-8"), 0);
1137         FreeStrBuf(&Line);
1138         FreeStrBuf(&Line1);
1139         FreeStrBuf(&Line2);
1140 }
1141
1142 void render_MAIL_html(StrBuf *Target, WCTemplputParams *TP, StrBuf *FoundCharset)
1143 {
1144         wc_mime_attachment *Mime = (wc_mime_attachment *) CTX(CTX_MIME_ATACH);
1145         StrBuf *Buf;
1146
1147         if (StrLength(Mime->Data) == 0)
1148                 return;
1149
1150         Buf = NewStrBufPlain(NULL, StrLength(Mime->Data));
1151
1152         /* HTML is fun, but we've got to strip it first */
1153         output_html(ChrPtr(Mime->Charset), 
1154                     (WC->CurRoom.view == VIEW_WIKI ? 1 : 0), 
1155                     Mime->msgnum,
1156                     Mime->Data, Buf);
1157         FreeStrBuf(&Mime->Data);
1158         Mime->Data = Buf;
1159 }
1160
1161 #ifdef HAVE_MARKDOWN
1162 /*
1163 char * MarkdownHandleURL(const char* SourceURL, const int len, void* something)
1164 {
1165
1166 }
1167 */
1168 void render_MAIL_markdown(StrBuf *Target, WCTemplputParams *TP, StrBuf *FoundCharset)
1169 {
1170 #include <mkdio.h>
1171         wc_mime_attachment *Mime = (wc_mime_attachment *) CTX(CTX_MIME_ATACH);
1172         MMIOT *doc;
1173         char *md_as_html = NULL;
1174         const char *format;
1175
1176         if (StrLength(Mime->Data) == 0)
1177                 return;
1178
1179         format = bstr("format");
1180
1181         if ((format == NULL) || 
1182             strcmp(format, "plain"))
1183         {
1184                 doc = mkd_string(ChrPtr(Mime->Data), StrLength(Mime->Data), 0);
1185                 mkd_basename(doc, "/wiki?page=");
1186                 mkd_compile(doc, 0);
1187                 if (mkd_document(doc, &md_as_html) != EOF) {
1188                         FreeStrBuf(&Mime->Data);
1189                         Mime->Data = NewStrBufPlain(md_as_html, -1);
1190                 }
1191                 mkd_cleanup(doc);
1192         }
1193 }
1194 #endif
1195
1196 void render_MAIL_UNKNOWN(StrBuf *Target, WCTemplputParams *TP, StrBuf *FoundCharset)
1197 {
1198         wc_mime_attachment *Mime = (wc_mime_attachment *) CTX(CTX_MIME_ATACH);
1199         /* Unknown weirdness */
1200         FlushStrBuf(Mime->Data);
1201         StrBufAppendBufPlain(Mime->Data, _("I don't know how to display "), -1, 0);
1202         StrBufAppendBuf(Mime->Data, Mime->ContentType, 0);
1203         StrBufAppendBufPlain(Mime->Data, HKEY("<br>\n"), 0);
1204 }
1205
1206
1207 HashList *iterate_get_mime_All(StrBuf *Target, WCTemplputParams *TP)
1208 {
1209         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
1210         return Msg->Attachments;
1211 }
1212 HashList *iterate_get_mime_Submessages(StrBuf *Target, WCTemplputParams *TP)
1213 {
1214         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
1215         return Msg->Submessages;
1216 }
1217 HashList *iterate_get_mime_AttachLinks(StrBuf *Target, WCTemplputParams *TP)
1218 {
1219         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
1220         return Msg->AttachLinks;
1221 }
1222 HashList *iterate_get_mime_Attachments(StrBuf *Target, WCTemplputParams *TP)
1223 {
1224         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
1225         return Msg->AllAttach;
1226 }
1227
1228 void tmplput_MIME_Name(StrBuf *Target, WCTemplputParams *TP)
1229 {
1230         wc_mime_attachment *mime = (wc_mime_attachment*) CTX(CTX_MIME_ATACH);
1231         StrBufAppendTemplate(Target, TP, mime->Name, 0);
1232 }
1233
1234 void tmplput_MIME_FileName(StrBuf *Target, WCTemplputParams *TP)
1235 {
1236         wc_mime_attachment *mime = (wc_mime_attachment*) CTX(CTX_MIME_ATACH);
1237         StrBufAppendTemplate(Target, TP, mime->FileName, 0);
1238 }
1239
1240 void tmplput_MIME_PartNum(StrBuf *Target, WCTemplputParams *TP)
1241 {
1242         wc_mime_attachment *mime = (wc_mime_attachment*) CTX(CTX_MIME_ATACH);
1243         StrBufAppendTemplate(Target, TP, mime->PartNum, 0);
1244 }
1245
1246 void tmplput_MIME_MsgNum(StrBuf *Target, WCTemplputParams *TP)
1247 {
1248         wc_mime_attachment *mime = (wc_mime_attachment*) CTX(CTX_MIME_ATACH);
1249         StrBufAppendPrintf(Target, "%ld", mime->msgnum);
1250 }
1251
1252 void tmplput_MIME_Disposition(StrBuf *Target, WCTemplputParams *TP)
1253 {
1254         wc_mime_attachment *mime = (wc_mime_attachment*) CTX(CTX_MIME_ATACH);
1255         StrBufAppendTemplate(Target, TP, mime->Disposition, 0);
1256 }
1257
1258 void tmplput_MIME_ContentType(StrBuf *Target, WCTemplputParams *TP)
1259 {
1260         wc_mime_attachment *mime = (wc_mime_attachment*) CTX(CTX_MIME_ATACH);
1261         StrBufAppendTemplate(Target, TP, mime->ContentType, 0);
1262 }
1263
1264 void examine_charset(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
1265 {
1266         Msg->MsgBody->Charset = NewStrBufDup(HdrLine);
1267 }
1268
1269 void tmplput_MIME_Charset(StrBuf *Target, WCTemplputParams *TP)
1270 {
1271         wc_mime_attachment *mime = (wc_mime_attachment*) CTX(CTX_MIME_ATACH);
1272         StrBufAppendTemplate(Target, TP, mime->Charset, 0);
1273 }
1274
1275 void tmplput_MIME_Data(StrBuf *Target, WCTemplputParams *TP)
1276 {
1277         wc_mime_attachment *mime = (wc_mime_attachment*) CTX(CTX_MIME_ATACH);
1278         if (mime->Renderer != NULL)
1279                 mime->Renderer->f(Target, TP, NULL);
1280         StrBufAppendTemplate(Target, TP, mime->Data, 0);
1281         /* TODO: check whether we need to load it now? */
1282 }
1283
1284 void tmplput_MIME_LoadData(StrBuf *Target, WCTemplputParams *TP)
1285 {
1286         wcsession *WCC = WC;    
1287         wc_mime_attachment *mime = (wc_mime_attachment*) CTX(CTX_MIME_ATACH);
1288         wc_mime_attachment *att;
1289         
1290         if (( (!strcasecmp(ChrPtr(mime->Disposition), "inline"))||
1291               (!strcasecmp(ChrPtr(mime->Disposition), "attachment"))) && 
1292             (strcasecmp(ChrPtr(mime->ContentType), "application/ms-tnef")!=0))
1293         {
1294                 
1295                 int n;
1296                 char N[64];
1297                 /* steal this mime part... */
1298                 att = malloc(sizeof(wc_mime_attachment));
1299                 memcpy(att, mime, sizeof(wc_mime_attachment));
1300                 memset(mime, 0, sizeof(wc_mime_attachment));
1301
1302                 if (att->Data == NULL) 
1303                         MimeLoadData(att);
1304
1305                 if (WCC->attachments == NULL)
1306                         WCC->attachments = NewHash(1, NULL);
1307                 /* And add it to the list. */
1308                 n = snprintf(N, sizeof N, "%d", GetCount(WCC->attachments) + 1);
1309                 Put(WCC->attachments, N, n, att, DestroyMime);
1310         }
1311 }
1312
1313 void tmplput_MIME_Length(StrBuf *Target, WCTemplputParams *TP)
1314 {
1315         wc_mime_attachment *mime = (wc_mime_attachment*) CTX(CTX_MIME_ATACH);
1316         StrBufAppendPrintf(Target, "%ld", mime->length);
1317 }
1318
1319 HashList *iterate_get_registered_Attachments(StrBuf *Target, WCTemplputParams *TP)
1320 {
1321         return WC->attachments;
1322 }
1323
1324 void get_registered_Attachments_Count(StrBuf *Target, WCTemplputParams *TP)
1325 {
1326         StrBufAppendPrintf(Target, "%ld", GetCount (WC->attachments));
1327 }
1328
1329 void servcmd_do_search(char *buf, long bufsize)
1330 {
1331         snprintf(buf, bufsize, "MSGS SEARCH|%s", bstr("query"));
1332 }
1333
1334 void servcmd_headers(char *buf, long bufsize)
1335 {
1336         snprintf(buf, bufsize, "MSGS ALL");
1337 }
1338
1339 void servcmd_readfwd(char *buf, long bufsize)
1340 {
1341         snprintf(buf, bufsize, "MSGS ALL");
1342 }
1343
1344 void servcmd_readgt(char *buf, long bufsize)
1345 {
1346         snprintf(buf, bufsize, "MSGS GT|%s", bstr("gt"));
1347 }
1348
1349 void servcmd_readlt(char *buf, long bufsize)
1350 {
1351         snprintf(buf, bufsize, "MSGS LT|%s", bstr("lt"));
1352 }
1353
1354 void servcmd_readnew(char *buf, long bufsize)
1355 {
1356         snprintf(buf, bufsize, "MSGS NEW");
1357 }
1358
1359 void servcmd_readold(char *buf, long bufsize)
1360 {
1361         snprintf(buf, bufsize, "MSGS OLD");
1362 }
1363
1364
1365 /* DO NOT REORDER OR REMOVE ANY OF THESE */
1366 readloop_struct rlid[] = {
1367         { {HKEY("do_search")},  servcmd_do_search       },
1368         { {HKEY("headers")},    servcmd_headers         },
1369         { {HKEY("readfwd")},    servcmd_readfwd         },
1370         { {HKEY("readnew")},    servcmd_readnew         },
1371         { {HKEY("readold")},    servcmd_readold         },
1372         { {HKEY("readgt")},     servcmd_readgt          },
1373         { {HKEY("readlt")},     servcmd_readlt          }
1374 };
1375
1376
1377 int ParseMessageListHeaders_Detail(StrBuf *Line, 
1378                                    const char **pos, 
1379                                    message_summary *Msg, 
1380                                    StrBuf *ConversionBuffer)
1381 {
1382         wcsession *WCC = WC;
1383         long len;
1384         long totallen;
1385
1386         CheckConvertBufs(WCC);
1387
1388         totallen = StrLength(Line);
1389         Msg->from = NewStrBufPlain(NULL, totallen);
1390         len = StrBufExtract_NextToken(ConversionBuffer, Line, pos, '|');
1391         if (len > 0) {
1392                 /* Handle senders with RFC2047 encoding */
1393                 StrBuf_RFC822_2_Utf8(Msg->from, 
1394                                      ConversionBuffer, 
1395                                      WCC->DefaultCharset, 
1396                                      NULL, 
1397                                      WCC->ConvertBuf1,
1398                                      WCC->ConvertBuf2);
1399         }
1400                         
1401         /* node name */
1402         len = StrBufExtract_NextToken(ConversionBuffer, Line, pos, '|');
1403         if ((len > 0 ) &&
1404             ( ((WCC->CurRoom.QRFlags & QR_NETWORK)
1405                || ((strcasecmp(ChrPtr(ConversionBuffer), ChrPtr(WCC->serv_info->serv_nodename))
1406                     && (strcasecmp(ChrPtr(ConversionBuffer), ChrPtr(WCC->serv_info->serv_fqdn))))))))
1407         {
1408                 StrBufAppendBufPlain(Msg->from, HKEY(" @ "), 0);
1409                 StrBufAppendBuf(Msg->from, ConversionBuffer, 0);
1410         }
1411
1412         /* Internet address (not used)
1413          *      StrBufExtract_token(Msg->inetaddr, Line, 4, '|');
1414          */
1415         StrBufSkip_NTokenS(Line, pos, '|', 1);
1416         Msg->subj = NewStrBufPlain(NULL, totallen);
1417
1418         FlushStrBuf(ConversionBuffer);
1419         /* we assume the subject is the last parameter inside of the list; 
1420          * thus we don't use the tokenizer to fetch it, since it will hick up 
1421          * on tokenizer chars inside of the subjects
1422         StrBufExtract_NextToken(ConversionBuffer,  Line, pos, '|');
1423         */
1424         len = 0;
1425         if (*pos != StrBufNOTNULL) {
1426                 len = totallen - (*pos - ChrPtr(Line));
1427                 StrBufPlain(ConversionBuffer, *pos, len);
1428                 *pos = StrBufNOTNULL;
1429                 if ((len > 0) &&
1430                     (*(ChrPtr(ConversionBuffer) + len - 1) == '|'))
1431                         StrBufCutRight(ConversionBuffer, 1);
1432         }
1433
1434         if (len == 0)
1435                 StrBufAppendBufPlain(Msg->subj, _("(no subject)"), -1,0);
1436         else {
1437                 StrBuf_RFC822_2_Utf8(Msg->subj, 
1438                                      ConversionBuffer, 
1439                                      WCC->DefaultCharset, 
1440                                      NULL,
1441                                      WCC->ConvertBuf1,
1442                                      WCC->ConvertBuf2);
1443         }
1444
1445         return 1;
1446 }
1447
1448
1449 int mailview_GetParamsGetServerCall(SharedMessageStatus *Stat, 
1450                                     void **ViewSpecific, 
1451                                     long oper, 
1452                                     char *cmd, 
1453                                     long len,
1454                                     char *filter,
1455                                     long flen)
1456 {
1457         DoTemplate(HKEY("msg_listview"),NULL,&NoCtx);
1458
1459         return 200;
1460 }
1461
1462 int mailview_Cleanup(void **ViewSpecific)
1463 {
1464         /* Note: wDumpContent() will output one additional </div> tag. */
1465         /* We ought to move this out into template */
1466         wDumpContent(1);
1467
1468         return 0;
1469 }
1470
1471
1472 int json_GetParamsGetServerCall(SharedMessageStatus *Stat, 
1473                                 void **ViewSpecific, 
1474                                 long oper, 
1475                                 char *cmd, 
1476                                 long len,
1477                                 char *filter,
1478                                 long flen)
1479 {
1480         Stat->defaultsortorder = 2;
1481         Stat->sortit = 1;
1482         Stat->load_seen = 1;
1483         /* Generally using maxmsgs|startmsg is not required
1484            in mailbox view, but we have a 'safemode' for clients
1485            (*cough* Exploder) that simply can't handle too many */
1486         if (havebstr("maxmsgs"))  Stat->maxmsgs  = ibstr("maxmsgs");
1487         else                      Stat->maxmsgs  = 9999999;
1488         if (havebstr("startmsg")) Stat->startmsg = lbstr("startmsg");
1489         snprintf(cmd, len, "MSGS %s|%s||1",
1490                  (oper == do_search) ? "SEARCH" : "ALL",
1491                  (oper == do_search) ? bstr("query") : ""
1492                 );
1493
1494         return 200;
1495 }
1496 int json_MessageListHdr(SharedMessageStatus *Stat, void **ViewSpecific) 
1497 {
1498         /* TODO: make a generic function */
1499         hprintf("HTTP/1.1 200 OK\r\n");
1500         hprintf("Content-type: application/json; charset=utf-8\r\n");
1501         hprintf("Server: %s / %s\r\n", PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software));
1502         hprintf("Connection: close\r\n");
1503         hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
1504         begin_burst();
1505         return 0;
1506 }
1507
1508 int json_RenderView_or_Tail(SharedMessageStatus *Stat, 
1509                             void **ViewSpecific, 
1510                             long oper)
1511 {
1512         DoTemplate(HKEY("mailsummary_json"),NULL, NULL);
1513         
1514         return 0;
1515 }
1516
1517 int json_Cleanup(void **ViewSpecific)
1518 {
1519         /* Note: wDumpContent() will output one additional </div> tag. */
1520         /* We ought to move this out into template */
1521         end_burst();
1522
1523         return 0;
1524 }
1525
1526
1527
1528 void 
1529 InitModule_MSGRENDERERS
1530 (void)
1531 {
1532         RegisterCTX(CTX_MAILSUM);
1533         RegisterCTX(CTX_MIME_ATACH);
1534         RegisterReadLoopHandlerset(
1535                 VIEW_MAILBOX,
1536                 mailview_GetParamsGetServerCall,
1537                 NULL, /* TODO: is this right? */
1538                 NULL,
1539                 ParseMessageListHeaders_Detail,
1540                 NULL,
1541                 NULL,
1542                 mailview_Cleanup);
1543
1544         RegisterReadLoopHandlerset(
1545                 VIEW_JSON_LIST,
1546                 json_GetParamsGetServerCall,
1547                 json_MessageListHdr,
1548                 NULL, /* TODO: is this right? */
1549                 ParseMessageListHeaders_Detail,
1550                 NULL,
1551                 json_RenderView_or_Tail,
1552                 json_Cleanup);
1553
1554         RegisterSortFunc(HKEY("date"), 
1555                          NULL, 0,
1556                          summcmp_date,
1557                          summcmp_rdate,
1558                          groupchange_date,
1559                          CTX_MAILSUM);
1560         RegisterSortFunc(HKEY("subject"), 
1561                          NULL, 0,
1562                          summcmp_subj,
1563                          summcmp_rsubj,
1564                          groupchange_subj,
1565                          CTX_MAILSUM);
1566         RegisterSortFunc(HKEY("sender"),
1567                          NULL, 0,
1568                          summcmp_sender,
1569                          summcmp_rsender,
1570                          groupchange_sender,
1571                          CTX_MAILSUM);
1572
1573         RegisterNamespace("SUMM:COUNT", 0, 0, tmplput_SUMM_COUNT, NULL, CTX_NONE);
1574         /* iterate over all known mails in WC->summ */
1575         RegisterIterator("MAIL:SUMM:MSGS", 0, NULL, iterate_get_mailsumm_All,
1576                          NULL,NULL, CTX_MAILSUM, CTX_NONE, IT_NOFLAG);
1577
1578         RegisterNamespace("MAIL:SUMM:EUID", 0, 1, tmplput_MAIL_SUMM_EUID, NULL, CTX_MAILSUM);
1579         RegisterNamespace("MAIL:SUMM:DATEBRIEF", 0, 0, tmplput_MAIL_SUMM_DATE_BRIEF, NULL, CTX_MAILSUM);
1580         RegisterNamespace("MAIL:SUMM:DATEFULL", 0, 0, tmplput_MAIL_SUMM_DATE_FULL, NULL, CTX_MAILSUM);
1581         RegisterNamespace("MAIL:SUMM:DATENO",  0, 0, tmplput_MAIL_SUMM_DATE_NO,  NULL, CTX_MAILSUM);
1582         RegisterNamespace("MAIL:SUMM:N",       0, 0, tmplput_MAIL_SUMM_N,        NULL, CTX_MAILSUM);
1583         RegisterNamespace("MAIL:SUMM:PERMALINK", 0, 0, tmplput_MAIL_SUMM_PERMALINK, NULL, CTX_MAILSUM);
1584         RegisterNamespace("MAIL:SUMM:FROM",    0, 2, tmplput_MAIL_SUMM_FROM,     NULL, CTX_MAILSUM);
1585         RegisterNamespace("MAIL:SUMM:TO",      0, 2, tmplput_MAIL_SUMM_TO,       NULL, CTX_MAILSUM);
1586         RegisterNamespace("MAIL:SUMM:SUBJECT", 0, 4, tmplput_MAIL_SUMM_SUBJECT,  NULL, CTX_MAILSUM);
1587         RegisterNamespace("MAIL:SUMM:NTATACH", 0, 0, tmplput_MAIL_SUMM_NATTACH,  NULL, CTX_MAILSUM);
1588         RegisterNamespace("MAIL:SUMM:CCCC", 0, 2, tmplput_MAIL_SUMM_CCCC, NULL, CTX_MAILSUM);
1589         RegisterNamespace("MAIL:SUMM:REPLYTO", 0, 2, tmplput_MAIL_SUMM_REPLYTO, NULL, CTX_MAILSUM);
1590         RegisterNamespace("MAIL:SUMM:H_NODE", 0, 2, tmplput_MAIL_SUMM_H_NODE,  NULL, CTX_MAILSUM);
1591         RegisterNamespace("MAIL:SUMM:ALLRCPT", 0, 2, tmplput_MAIL_SUMM_ALLRCPT,  NULL, CTX_MAILSUM);
1592         RegisterNamespace("MAIL:SUMM:ORGROOM", 0, 2, tmplput_MAIL_SUMM_ORGROOM,  NULL, CTX_MAILSUM);
1593         RegisterNamespace("MAIL:SUMM:RFCA", 0, 2, tmplput_MAIL_SUMM_RFCA, NULL, CTX_MAILSUM);
1594         RegisterNamespace("MAIL:SUMM:OTHERNODE", 2, 0, tmplput_MAIL_SUMM_OTHERNODE,  NULL, CTX_MAILSUM);
1595         RegisterNamespace("MAIL:SUMM:REFIDS", 0, 1, tmplput_MAIL_SUMM_REFIDS,  NULL, CTX_MAILSUM);
1596         RegisterNamespace("MAIL:SUMM:INREPLYTO", 0, 2, tmplput_MAIL_SUMM_INREPLYTO,  NULL, CTX_MAILSUM);
1597         RegisterNamespace("MAIL:BODY", 0, 2, tmplput_MAIL_BODY,  NULL, CTX_MAILSUM);
1598         RegisterNamespace("MAIL:QUOTETEXT", 1, 2, tmplput_QUOTED_MAIL_BODY,  NULL, CTX_NONE);
1599         RegisterNamespace("MAIL:EDITTEXT", 1, 2, tmplput_EDIT_MAIL_BODY,  NULL, CTX_NONE);
1600         RegisterNamespace("MAIL:EDITWIKI", 1, 2, tmplput_EDIT_WIKI_BODY,  NULL, CTX_NONE);
1601         RegisterConditional("COND:MAIL:SUMM:RFCA", 0, Conditional_MAIL_SUMM_RFCA,  CTX_MAILSUM);
1602         RegisterConditional("COND:MAIL:SUMM:CCCC", 0, Conditional_MAIL_SUMM_CCCC,  CTX_MAILSUM);
1603         RegisterConditional("COND:MAIL:SUMM:REPLYTO", 0, Conditional_MAIL_SUMM_REPLYTO,  CTX_MAILSUM);
1604         RegisterConditional("COND:MAIL:SUMM:UNREAD", 0, Conditional_MAIL_SUMM_UNREAD, CTX_MAILSUM);
1605         RegisterConditional("COND:MAIL:SUMM:H_NODE", 0, Conditional_MAIL_SUMM_H_NODE, CTX_MAILSUM);
1606         RegisterConditional("COND:MAIL:SUMM:OTHERNODE", 0, Conditional_MAIL_SUMM_OTHERNODE, CTX_MAILSUM);
1607         RegisterConditional("COND:MAIL:SUMM:SUBJECT", 0, Conditional_MAIL_SUMM_SUBJECT, CTX_MAILSUM);
1608         RegisterConditional("COND:MAIL:ANON", 0, Conditional_ANONYMOUS_MESSAGE, CTX_MAILSUM);
1609         RegisterConditional("COND:MAIL:TO", 0, Conditional_MAIL_SUMM_TO, CTX_MAILSUM);  
1610         RegisterConditional("COND:MAIL:SUBJ", 0, Conditional_MAIL_SUMM_SUBJ, CTX_MAILSUM);      
1611
1612         /* do we have mimetypes to iterate over? */
1613         RegisterConditional("COND:MAIL:MIME:ATTACH", 0, Conditional_MAIL_MIME_ALL, CTX_MAILSUM);
1614         RegisterConditional("COND:MAIL:MIME:ATTACH:SUBMESSAGES", 0, Conditional_MAIL_MIME_SUBMESSAGES, CTX_MAILSUM);
1615         RegisterConditional("COND:MAIL:MIME:ATTACH:LINKS", 0, Conditional_MAIL_MIME_ATTACHLINKS, CTX_MAILSUM);
1616         RegisterConditional("COND:MAIL:MIME:ATTACH:ATT", 0, Conditional_MAIL_MIME_ATTACH, CTX_MAILSUM);
1617         RegisterIterator("MAIL:MIME:ATTACH", 0, NULL, iterate_get_mime_All, 
1618                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM, IT_NOFLAG);
1619         RegisterIterator("MAIL:MIME:ATTACH:SUBMESSAGES", 0, NULL, iterate_get_mime_Submessages, 
1620                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM, IT_NOFLAG);
1621         RegisterIterator("MAIL:MIME:ATTACH:LINKS", 0, NULL, iterate_get_mime_AttachLinks, 
1622                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM, IT_NOFLAG);
1623         RegisterIterator("MAIL:MIME:ATTACH:ATT", 0, NULL, iterate_get_mime_Attachments, 
1624                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM, IT_NOFLAG);
1625
1626         /* Parts of a mime attachent */
1627         RegisterNamespace("MAIL:MIME:NAME", 0, 2, tmplput_MIME_Name, NULL, CTX_MIME_ATACH);
1628         RegisterNamespace("MAIL:MIME:FILENAME", 0, 2, tmplput_MIME_FileName, NULL, CTX_MIME_ATACH);
1629         RegisterNamespace("MAIL:MIME:PARTNUM", 0, 2, tmplput_MIME_PartNum, NULL, CTX_MIME_ATACH);
1630         RegisterNamespace("MAIL:MIME:MSGNUM", 0, 2, tmplput_MIME_MsgNum, NULL, CTX_MIME_ATACH);
1631         RegisterNamespace("MAIL:MIME:DISPOSITION", 0, 2, tmplput_MIME_Disposition, NULL, CTX_MIME_ATACH);
1632         RegisterNamespace("MAIL:MIME:CONTENTTYPE", 0, 2, tmplput_MIME_ContentType, NULL, CTX_MIME_ATACH);
1633         RegisterNamespace("MAIL:MIME:CHARSET", 0, 2, tmplput_MIME_Charset, NULL, CTX_MIME_ATACH);
1634         RegisterNamespace("MAIL:MIME:LENGTH", 0, 2, tmplput_MIME_Length, NULL, CTX_MIME_ATACH);
1635         RegisterNamespace("MAIL:MIME:DATA", 0, 2, tmplput_MIME_Data, NULL, CTX_MIME_ATACH);
1636         /* load the actual attachment into WC->attachments; no output!!! */
1637         RegisterNamespace("MAIL:MIME:LOADDATA", 0, 0, tmplput_MIME_LoadData, NULL, CTX_MIME_ATACH);
1638
1639         /* iterate the WC->attachments; use the above tokens for their contents */
1640         RegisterIterator("MSG:ATTACHNAMES", 0, NULL, iterate_get_registered_Attachments, 
1641                          NULL, NULL, CTX_MIME_ATACH, CTX_NONE, IT_NOFLAG);
1642
1643         RegisterNamespace("MSG:NATTACH", 0, 0, get_registered_Attachments_Count,  NULL, CTX_NONE);
1644
1645         /* mime renderers translate an attachment into webcit viewable html text */
1646         RegisterMimeRenderer(HKEY("message/rfc822"), render_MAIL, 0, 150);
1647 //*
1648         RegisterMimeRenderer(HKEY("text/calendar"), render_MIME_ICS, 1, 501);
1649         RegisterMimeRenderer(HKEY("application/ics"), render_MIME_ICS, 1, 500);
1650 //*/
1651         RegisterMimeRenderer(HKEY("text/x-citadel-variformat"), render_MAIL_variformat, 1, 2);
1652         RegisterMimeRenderer(HKEY("text/plain"), render_MAIL_text_plain, 1, 3);
1653         RegisterMimeRenderer(HKEY("text"), render_MAIL_text_plain, 1, 1);
1654         RegisterMimeRenderer(HKEY("text/html"), render_MAIL_html, 1, 100);
1655 #ifdef HAVE_MARKDOWN
1656         RegisterMimeRenderer(HKEY("text/x-markdown"), render_MAIL_markdown, 1, 30);
1657 #endif
1658         RegisterMimeRenderer(HKEY(""), render_MAIL_UNKNOWN, 0, 0);
1659
1660         /* these headers are citserver replies to MSG4 and friends. one evaluator for each */
1661         RegisterMsgHdr(HKEY("nhdr"), examine_nhdr, 0);
1662         RegisterMsgHdr(HKEY("type"), examine_type, 0);
1663         RegisterMsgHdr(HKEY("from"), examine_from, 0);
1664         RegisterMsgHdr(HKEY("subj"), examine_subj, 0);
1665         RegisterMsgHdr(HKEY("msgn"), examine_msgn, 0);
1666         RegisterMsgHdr(HKEY("wefw"), examine_wefw, 0);
1667         RegisterMsgHdr(HKEY("cccc"), examine_cccc, 0);
1668         RegisterMsgHdr(HKEY("rep2"), examine_replyto, 0);
1669         RegisterMsgHdr(HKEY("hnod"), examine_hnod, 0);
1670         RegisterMsgHdr(HKEY("room"), examine_room, 0);
1671         RegisterMsgHdr(HKEY("rfca"), examine_rfca, 0);
1672         RegisterMsgHdr(HKEY("node"), examine_node, 0);
1673         RegisterMsgHdr(HKEY("rcpt"), examine_rcpt, 0);
1674         RegisterMsgHdr(HKEY("nvto"), examine_nvto, 0);
1675         RegisterMsgHdr(HKEY("time"), examine_time, 0);
1676         RegisterMsgHdr(HKEY("part"), examine_mime_part, 0);
1677         RegisterMsgHdr(HKEY("text"), examine_text, 1);
1678         /* these are the content-type headers we get infront of a message; put it into the same hash since it doesn't clash. */
1679         RegisterMsgHdr(HKEY("X-Citadel-MSG4-Partnum"), examine_msg4_partnum, 0);
1680         RegisterMsgHdr(HKEY("Content-type"), examine_content_type, 0);
1681         RegisterMsgHdr(HKEY("Content-length"), examine_content_lengh, 0);
1682         RegisterMsgHdr(HKEY("Content-transfer-encoding"), examine_content_encoding, 0); /* do we care? */
1683         RegisterMsgHdr(HKEY("charset"), examine_charset, 0);
1684
1685         /* Don't care about these... */
1686         RegisterMsgHdr(HKEY("pref"), examine_pref, 0);
1687         RegisterMsgHdr(HKEY("suff"), examine_suff, 0);
1688         RegisterMsgHdr(HKEY("path"), examine_path, 0);
1689 }
1690
1691 void 
1692 InitModule2_MSGRENDERERS
1693 (void)
1694 {
1695         /* and finalize the anouncement to the server... */
1696         CreateMimeStr();
1697 }
1698 void 
1699 ServerStartModule_MSGRENDERERS
1700 (void)
1701 {
1702         MsgHeaderHandler = NewHash(1, NULL);
1703         MimeRenderHandler = NewHash(1, NULL);
1704         ReadLoopHandler = NewHash(1, NULL);
1705 }
1706
1707 void 
1708 ServerShutdownModule_MSGRENDERERS
1709 (void)
1710 {
1711         DeleteHash(&MsgHeaderHandler);
1712         DeleteHash(&MimeRenderHandler);
1713         DeleteHash(&ReadLoopHandler);
1714 }
1715
1716
1717
1718 void 
1719 SessionDestroyModule_MSGRENDERERS
1720 (wcsession *sess)
1721 {
1722         DeleteHash(&sess->attachments);
1723         FreeStrBuf(&sess->ConvertBuf1);
1724         FreeStrBuf(&sess->ConvertBuf2);
1725 }