Finalize vcard editing:
[citadel.git] / 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_VCard(StrBuf *Target, WCTemplputParams *TP, StrBuf *FoundCharset)
616 {
617         wc_mime_attachment *Mime = (wc_mime_attachment *) CTX(CTX_MIME_ATACH);
618         wcsession *WCC = WC;
619         if (StrLength(Mime->Data) == 0)
620                 MimeLoadData(Mime);
621         if (StrLength(Mime->Data) > 0) {
622                 StrBuf *Buf;
623                 Buf = NewStrBuf();
624                 /** If it's my vCard I can edit it */
625                 if (    (!strcasecmp(ChrPtr(WCC->CurRoom.name), USERCONFIGROOM))
626                         || ((StrLength(WCC->CurRoom.name) > 11) &&
627                             (!strcasecmp(&(ChrPtr(WCC->CurRoom.name)[11]), USERCONFIGROOM)))
628                         || (WCC->CurRoom.view == VIEW_ADDRESSBOOK)
629                         ) {
630                         StrBufAppendPrintf(Buf, "<a href=\"edit_vcard?msgnum=%ld?partnum=%s\">",
631                                 Mime->msgnum, ChrPtr(Mime->PartNum));
632                         StrBufAppendPrintf(Buf, "[%s]</a>", _("edit"));
633                 }
634
635                 /* In all cases, display the full card */
636                 display_vcard(Buf, Mime, 0, 1, NULL, -1);
637                 FreeStrBuf(&Mime->Data);
638                 Mime->Data = Buf;
639         }
640
641 }
642
643 void render_MIME_ICS(StrBuf *Target, WCTemplputParams *TP, StrBuf *FoundCharset)
644 {
645         wc_mime_attachment *Mime = (wc_mime_attachment *) CTX(CTX_MIME_ATACH);
646         if (StrLength(Mime->Data) == 0) {
647                 MimeLoadData(Mime);
648         }
649         if (StrLength(Mime->Data) > 0) {
650                 cal_process_attachment(Mime);
651         }
652 }
653
654
655
656 void examine_mime_part(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
657 {
658         const char *Ptr = NULL;
659         wc_mime_attachment *Mime;
660         StrBuf *Buf;
661         wcsession *WCC = WC;
662
663         CheckConvertBufs(WCC);  
664         Mime = (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
665         memset(Mime, 0, sizeof(wc_mime_attachment));
666         Mime->msgnum = Msg->msgnum;
667         Buf = NewStrBuf();
668
669         Mime->Name = NewStrBuf();
670         StrBufExtract_NextToken(Buf, HdrLine, &Ptr, '|');
671         StrBuf_RFC822_2_Utf8(Mime->Name, 
672                              Buf, 
673                              WCC->DefaultCharset, 
674                              FoundCharset,
675                              WCC->ConvertBuf1,
676                              WCC->ConvertBuf2);
677         StrBufTrim(Mime->Name);
678
679         StrBufExtract_NextToken(Buf, HdrLine, &Ptr, '|');
680         Mime->FileName = NewStrBuf();
681         StrBuf_RFC822_2_Utf8(Mime->FileName, 
682                              Buf, 
683                              WCC->DefaultCharset, 
684                              FoundCharset,
685                              WCC->ConvertBuf1,
686                              WCC->ConvertBuf2);
687         StrBufTrim(Mime->FileName);
688
689         Mime->PartNum = NewStrBuf();
690         StrBufExtract_NextToken(Mime->PartNum, HdrLine, &Ptr, '|');
691         StrBufTrim(Mime->PartNum);
692         if (strchr(ChrPtr(Mime->PartNum), '.') != NULL) 
693                 Mime->level = 2;
694         else
695                 Mime->level = 1;
696
697         Mime->Disposition = NewStrBuf();
698         StrBufExtract_NextToken(Mime->Disposition, HdrLine, &Ptr, '|');
699
700         Mime->ContentType = NewStrBuf();
701         StrBufExtract_NextToken(Mime->ContentType, HdrLine, &Ptr, '|');
702         StrBufTrim(Mime->ContentType);
703         StrBufLowerCase(Mime->ContentType);
704         if (!strcmp(ChrPtr(Mime->ContentType), "application/octet-stream")) {
705                 StrBufPlain(Mime->ContentType, 
706                             GuessMimeByFilename(SKEY(Mime->FileName)), -1);
707         }
708
709         Mime->length = StrBufExtractNext_int(HdrLine, &Ptr, '|');
710
711         StrBufSkip_NTokenS(HdrLine, &Ptr, '|', 1);  /* cbid?? */
712
713         Mime->Charset = NewStrBuf();
714         StrBufExtract_NextToken(Mime->Charset, HdrLine, &Ptr, '|');
715
716
717         if ( (StrLength(Mime->FileName) == 0) && (StrLength(Mime->Name) > 0) ) {
718                 StrBufAppendBuf(Mime->FileName, Mime->Name, 0);
719         }
720
721         if (StrLength(Msg->PartNum) > 0) {
722                 StrBuf *tmp;
723                 StrBufPrintf(Buf, "%s.%s", ChrPtr(Msg->PartNum), ChrPtr(Mime->PartNum));
724                 tmp = Mime->PartNum;
725                 Mime->PartNum = Buf;
726                 Buf = tmp;
727         }
728
729         if (Msg->AllAttach == NULL)
730                 Msg->AllAttach = NewHash(1,NULL);
731         Put(Msg->AllAttach, SKEY(Mime->PartNum), Mime, DestroyMime);
732         FreeStrBuf(&Buf);
733 }
734
735
736 void evaluate_mime_part(StrBuf *Target, WCTemplputParams *TP)
737 {
738         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
739         wc_mime_attachment *Mime = (wc_mime_attachment *) CTX(CTX_MIME_ATACH);
740         void *vMimeRenderer;
741
742         /* just print the root-node */
743         if ((Mime->level >= 1) &&
744             GetHash(MimeRenderHandler, SKEY(Mime->ContentType), &vMimeRenderer) &&
745             vMimeRenderer != NULL)
746         {
747                 Mime->Renderer = (RenderMimeFuncStruct*) vMimeRenderer;
748                 if (Msg->Submessages == NULL)
749                         Msg->Submessages = NewHash(1,NULL);
750                 Put(Msg->Submessages, SKEY(Mime->PartNum), Mime, reference_free_handler);
751         }
752         else if ((Mime->level >= 1) &&
753                  (!strcasecmp(ChrPtr(Mime->Disposition), "inline"))
754                  && (!strncasecmp(ChrPtr(Mime->ContentType), "image/", 6)) ){
755                 if (Msg->AttachLinks == NULL)
756                         Msg->AttachLinks = NewHash(1,NULL);
757                 Put(Msg->AttachLinks, SKEY(Mime->PartNum), Mime, reference_free_handler);
758         }
759         else if ((Mime->level >= 1) &&
760                  (StrLength(Mime->ContentType) > 0) &&
761                   ( (!strcasecmp(ChrPtr(Mime->Disposition), "attachment")) 
762                     || (!strcasecmp(ChrPtr(Mime->Disposition), "inline"))
763                     || (!strcasecmp(ChrPtr(Mime->Disposition), ""))))
764         {               
765                 if (Msg->AttachLinks == NULL)
766                         Msg->AttachLinks = NewHash(1,NULL);
767                 Put(Msg->AttachLinks, SKEY(Mime->PartNum), Mime, reference_free_handler);
768                 if ((strcasecmp(ChrPtr(Mime->ContentType), "application/octet-stream") == 0) && 
769                     (StrLength(Mime->FileName) > 0)) {
770                         FlushStrBuf(Mime->ContentType);
771                         StrBufAppendBufPlain(Mime->ContentType,
772                                              GuessMimeByFilename(SKEY(Mime->FileName)),
773                                              -1, 0);
774                 }
775         }
776 }
777
778 void tmplput_MAIL_SUMM_NATTACH(StrBuf *Target, WCTemplputParams *TP)
779 {
780         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
781         StrBufAppendPrintf(Target, "%ld", GetCount(Msg->Attachments));
782 }
783
784
785 void examine_hnod(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
786 {
787         wcsession *WCC = WC;
788
789         CheckConvertBufs(WCC);
790         FreeStrBuf(&Msg->hnod);
791         Msg->hnod = NewStrBufPlain(NULL, StrLength(HdrLine));
792         StrBuf_RFC822_2_Utf8(Msg->hnod, 
793                              HdrLine, 
794                              WCC->DefaultCharset, 
795                              FoundCharset,
796                              WCC->ConvertBuf1,
797                              WCC->ConvertBuf2);
798 }
799 void tmplput_MAIL_SUMM_H_NODE(StrBuf *Target, WCTemplputParams *TP)
800 {
801         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
802         StrBufAppendTemplate(Target, TP, Msg->hnod, 0);
803 }
804 int Conditional_MAIL_SUMM_H_NODE(StrBuf *Target, WCTemplputParams *TP)
805 {
806         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
807         return StrLength(Msg->hnod) > 0;
808 }
809
810
811
812 void examine_text(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
813 {
814         if (Msg->MsgBody->Data == NULL)
815                 Msg->MsgBody->Data = NewStrBufPlain(NULL, SIZ);
816         else
817                 FlushStrBuf(Msg->MsgBody->Data);
818 }
819
820 void examine_msg4_partnum(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
821 {
822         Msg->MsgBody->PartNum = NewStrBufDup(HdrLine);
823         StrBufTrim(Msg->MsgBody->PartNum);
824 }
825
826 void examine_content_lengh(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
827 {
828         Msg->MsgBody->length = StrTol(HdrLine);
829         Msg->MsgBody->size_known = 1;
830 }
831
832 void examine_content_type(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
833 {
834         void *vHdr;
835         headereval *Hdr;
836         StrBuf *Token;
837         StrBuf *Value;
838         const char* sem;
839         const char *eq;
840         int len;
841         StrBufTrim(HdrLine);
842         Msg->MsgBody->ContentType = NewStrBufDup(HdrLine);
843         sem = strchr(ChrPtr(HdrLine), ';');
844
845         if (sem != NULL) {
846                 Token = NewStrBufPlain(NULL, StrLength(HdrLine));
847                 Value = NewStrBufPlain(NULL, StrLength(HdrLine));
848                 len = sem - ChrPtr(HdrLine);
849                 StrBufCutAt(Msg->MsgBody->ContentType, len, NULL);
850                 while (sem != NULL) {
851                         while (isspace(*(sem + 1)))
852                                 sem ++;
853                         StrBufCutLeft(HdrLine, sem - ChrPtr(HdrLine));
854                         sem = strchr(ChrPtr(HdrLine), ';');
855                         if (sem != NULL)
856                                 len = sem - ChrPtr(HdrLine);
857                         else
858                                 len = StrLength(HdrLine);
859                         FlushStrBuf(Token);
860                         FlushStrBuf(Value);
861                         StrBufAppendBufPlain(Token, ChrPtr(HdrLine), len, 0);
862                         eq = strchr(ChrPtr(Token), '=');
863                         if (eq != NULL) {
864                                 len = eq - ChrPtr(Token);
865                                 StrBufAppendBufPlain(Value, eq + 1, StrLength(Token) - len - 1, 0); 
866                                 StrBufCutAt(Token, len, NULL);
867                                 StrBufTrim(Value);
868                         }
869                         StrBufTrim(Token);
870
871                         if (GetHash(MsgHeaderHandler, SKEY(Token), &vHdr) &&
872                             (vHdr != NULL)) {
873                                 Hdr = (headereval*)vHdr;
874                                 Hdr->evaluator(Msg, Value, FoundCharset);
875                         }
876                         else syslog(LOG_WARNING, "don't know how to handle content type sub-header[%s]\n", ChrPtr(Token));
877                 }
878                 FreeStrBuf(&Token);
879                 FreeStrBuf(&Value);
880         }
881 }
882
883
884 int ReadOneMessageSummary(message_summary *Msg, StrBuf *FoundCharset, StrBuf *Buf)
885 {
886         void *vHdr;
887         headereval *Hdr;
888         const char *buf;
889         const char *ebuf;
890         int nBuf;
891         long len;
892         
893         serv_printf("MSG0 %ld|1", Msg->msgnum); /* ask for headers only */
894         
895         StrBuf_ServGetln(Buf);
896         if (GetServerStatus(Buf, NULL) != 1) {
897                 return 0;
898         }
899
900         while (len = StrBuf_ServGetln(Buf),
901                (len >= 0) && 
902                ((len != 3)  ||
903                 strcmp(ChrPtr(Buf), "000")))
904         {
905                 buf = ChrPtr(Buf);
906                 ebuf = strchr(ChrPtr(Buf), '=');
907                 nBuf = ebuf - buf;
908                 
909                 if (GetHash(MsgHeaderHandler, buf, nBuf, &vHdr) &&
910                     (vHdr != NULL)) {
911                         Hdr = (headereval*)vHdr;
912                         StrBufCutLeft(Buf, nBuf + 1);
913                         Hdr->evaluator(Msg, Buf, FoundCharset);
914                 }
915                 else syslog(LOG_INFO, "Don't know how to handle Message Headerline [%s]", ChrPtr(Buf));
916         }
917         return 1;
918 }
919
920 void tmplput_MAIL_SUMM_N(StrBuf *Target, WCTemplputParams *TP)
921 {
922         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
923         StrBufAppendPrintf(Target, "%ld", Msg->msgnum);
924 }
925
926
927 void tmplput_MAIL_SUMM_PERMALINK(StrBuf *Target, WCTemplputParams *TP)
928 {
929         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
930         StrBuf *perma_link;
931         const StrBuf *View;
932
933         perma_link = NewStrBufPlain(HKEY("/readfwd?go="));
934         StrBufUrlescAppend(perma_link, WC->CurRoom.name, NULL);
935         View = sbstr("view");
936         if (View != NULL) {
937                 StrBufAppendBufPlain(perma_link, HKEY("?view="), 0);
938                 StrBufAppendBuf(perma_link, View, 0);
939         }
940         StrBufAppendBufPlain(perma_link, HKEY("?start_reading_at="), 0);
941         StrBufAppendPrintf(perma_link, "%ld#%ld", Msg->msgnum, Msg->msgnum);
942         StrBufAppendBuf(Target, perma_link, 0);
943         FreeStrBuf(&perma_link);
944 }
945
946
947 int Conditional_MAIL_MIME_ALL(StrBuf *Target, WCTemplputParams *TP)
948 {
949         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
950         return GetCount(Msg->Attachments) > 0;
951 }
952
953 int Conditional_MAIL_MIME_SUBMESSAGES(StrBuf *Target, WCTemplputParams *TP)
954 {
955         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
956         return GetCount(Msg->Submessages) > 0;
957 }
958
959 int Conditional_MAIL_MIME_ATTACHLINKS(StrBuf *Target, WCTemplputParams *TP)
960 {
961         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
962         return GetCount(Msg->AttachLinks) > 0;
963 }
964
965 int Conditional_MAIL_MIME_ATTACH(StrBuf *Target, WCTemplputParams *TP)
966 {
967         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
968         return GetCount(Msg->AllAttach) > 0;
969 }
970
971 void tmplput_QUOTED_MAIL_BODY(StrBuf *Target, WCTemplputParams *TP)
972 {
973         const StrBuf *Mime;
974         long MsgNum;
975         StrBuf *Buf;
976
977         MsgNum = LBstr(TKEY(0));
978         Buf = NewStrBuf();
979         read_message(Buf, HKEY("view_message_replyquote"), MsgNum, NULL, &Mime);
980         StrBufAppendTemplate(Target, TP, Buf, 1);
981         FreeStrBuf(&Buf);
982 }
983
984 void tmplput_EDIT_MAIL_BODY(StrBuf *Target, WCTemplputParams *TP)
985 {
986         const StrBuf *Mime;
987         long MsgNum;
988         StrBuf *Buf;
989
990         MsgNum = LBstr(TKEY(0));
991         Buf = NewStrBuf();
992         read_message(Buf, HKEY("view_message_edit"), MsgNum, NULL, &Mime);
993         StrBufAppendTemplate(Target, TP, Buf, 1);
994         FreeStrBuf(&Buf);
995 }
996
997 void tmplput_EDIT_WIKI_BODY(StrBuf *Target, WCTemplputParams *TP)
998 {
999         const StrBuf *Mime;
1000         long msgnum;
1001         StrBuf *Buf;
1002
1003         /* Insert the existing content of the wiki page into the editor.  But we only want
1004          * to do this the first time -- if the user is uploading an attachment we don't want
1005          * to do it again.
1006          */
1007         if (!havebstr("attach_button")) {
1008                 char *wikipage = strdup(bstr("page"));
1009                 putbstr("format", NewStrBufPlain(HKEY("plain")));
1010                 str_wiki_index(wikipage);
1011                 msgnum = locate_message_by_uid(wikipage);
1012                 free(wikipage);
1013                 if (msgnum >= 0L) {
1014                         Buf = NewStrBuf();
1015                         read_message(Buf, HKEY("view_message_wikiedit"), msgnum, NULL, &Mime);
1016                         StrBufAppendTemplate(Target, TP, Buf, 1);
1017                         FreeStrBuf(&Buf);
1018                 }
1019         }
1020 }
1021
1022 void tmplput_MAIL_BODY(StrBuf *Target, WCTemplputParams *TP)
1023 {
1024         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
1025         StrBufAppendTemplate(Target, TP, Msg->MsgBody->Data, 0);
1026 }
1027
1028
1029 void render_MAIL_variformat(StrBuf *Target, WCTemplputParams *TP, StrBuf *FoundCharset)
1030 {
1031         /* Messages in legacy Citadel variformat get handled thusly... */
1032         wc_mime_attachment *Mime = (wc_mime_attachment *) CTX(CTX_MIME_ATACH);
1033         StrBuf *TTarget = NewStrBufPlain(NULL, StrLength(Mime->Data));
1034         FmOut(TTarget, "JUSTIFY", Mime->Data);
1035         FreeStrBuf(&Mime->Data);
1036         Mime->Data = TTarget;
1037 }
1038
1039 void render_MAIL_text_plain(StrBuf *Target, WCTemplputParams *TP, StrBuf *FoundCharset)
1040 {
1041         wc_mime_attachment *Mime = (wc_mime_attachment *) CTX(CTX_MIME_ATACH);
1042         const char *ptr, *pte;
1043         const char *BufPtr = NULL;
1044         StrBuf *Line;
1045         StrBuf *Line1;
1046         StrBuf *Line2;
1047         StrBuf *TTarget;
1048         long Linecount;
1049         long nEmptyLines;
1050         int bn = 0;
1051         int bq = 0;
1052         int i;
1053         long len;
1054 #ifdef HAVE_ICONV
1055         StrBuf *cs = NULL;
1056         int ConvertIt = 1;
1057         iconv_t ic = (iconv_t)(-1) ;
1058 #endif
1059
1060         if ((StrLength(Mime->Data) == 0) && (Mime->length > 0)) {
1061                 FreeStrBuf(&Mime->Data);
1062                 MimeLoadData(Mime);
1063         }
1064
1065 #ifdef HAVE_ICONV
1066         if (ConvertIt) {
1067                 if (StrLength(Mime->Charset) != 0)
1068                         cs = Mime->Charset;
1069                 else if (StrLength(FoundCharset) > 0)
1070                         cs = FoundCharset;
1071                 else if (StrLength(WC->DefaultCharset) > 0)
1072                         cs = WC->DefaultCharset;
1073                 if (cs == NULL) {
1074                         ConvertIt = 0;
1075                 }
1076                 else if (!strcasecmp(ChrPtr(cs), "utf-8")) {
1077                         ConvertIt = 0;
1078                 }
1079                 else if (!strcasecmp(ChrPtr(cs), "us-ascii")) {
1080                         ConvertIt = 0;
1081                 }
1082                 else {
1083                         ctdl_iconv_open("UTF-8", ChrPtr(cs), &ic);
1084                         if (ic == (iconv_t)(-1) ) {
1085                                 syslog(LOG_WARNING, "%s:%d iconv_open(UTF-8, %s) failed: %s\n",
1086                                         __FILE__, __LINE__, ChrPtr(Mime->Charset), strerror(errno));
1087                         }
1088                 }
1089         }
1090 #endif
1091         Line = NewStrBufPlain(NULL, SIZ);
1092         Line1 = NewStrBufPlain(NULL, SIZ);
1093         Line2 = NewStrBufPlain(NULL, SIZ);
1094         TTarget = NewStrBufPlain(NULL, StrLength(Mime->Data));
1095         Linecount = 0;
1096         nEmptyLines = 0;
1097         if (StrLength(Mime->Data) > 0) 
1098                 do 
1099                 {
1100                         StrBufSipLine(Line, Mime->Data, &BufPtr);
1101                         bq = 0;
1102                         i = 0;
1103                         ptr = ChrPtr(Line);
1104                         len = StrLength(Line);
1105                         pte = ptr + len;
1106                 
1107                         while ((ptr < pte) &&
1108                                ((*ptr == '>') ||
1109                                 isspace(*ptr)))
1110                         {
1111                                 if (*ptr == '>')
1112                                         bq++;
1113                                 ptr ++;
1114                                 i++;
1115                         }
1116                         if (i > 0) StrBufCutLeft(Line, i);
1117                 
1118                         if (StrLength(Line) == 0) {
1119                                 if (Linecount == 0)
1120                                         continue;
1121                                 StrBufAppendBufPlain(TTarget, HKEY("<tt></tt><br>\n"), 0);
1122
1123                                 nEmptyLines ++;
1124                                 continue;
1125                         }
1126                         nEmptyLines = 0;
1127                         for (i = bn; i < bq; i++)                               
1128                                 StrBufAppendBufPlain(TTarget, HKEY("<blockquote>"), 0);
1129                         for (i = bq; i < bn; i++)                               
1130                                 StrBufAppendBufPlain(TTarget, HKEY("</blockquote>"), 0);
1131 #ifdef HAVE_ICONV
1132                         if (ConvertIt) {
1133                                 StrBufConvert(Line, Line1, &ic);
1134                         }
1135 #endif
1136                         StrBufAppendBufPlain(TTarget, HKEY("<tt>"), 0);
1137                         UrlizeText(Line1, Line, Line2);
1138
1139                         StrEscAppend(TTarget, Line1, NULL, 0, 0);
1140                         StrBufAppendBufPlain(TTarget, HKEY("</tt><br>\n"), 0);
1141                         bn = bq;
1142                         Linecount ++;
1143                 }
1144         while ((BufPtr != StrBufNOTNULL) &&
1145                (BufPtr != NULL));
1146
1147         if (nEmptyLines > 0)
1148                 StrBufCutRight(TTarget, nEmptyLines * (sizeof ("<tt></tt><br>\n") - 1));
1149         for (i = 0; i < bn; i++)                                
1150                 StrBufAppendBufPlain(TTarget, HKEY("</blockquote>"), 0);
1151
1152         StrBufAppendBufPlain(TTarget, HKEY("</i><br>"), 0);
1153 #ifdef HAVE_ICONV
1154         if (ic != (iconv_t)(-1) ) {
1155                 iconv_close(ic);
1156         }
1157 #endif
1158
1159         FreeStrBuf(&Mime->Data);
1160         Mime->Data = TTarget;
1161         FlushStrBuf(Mime->ContentType);
1162         StrBufAppendBufPlain(Mime->ContentType, HKEY("text/html"), 0);
1163         FlushStrBuf(Mime->Charset);
1164         StrBufAppendBufPlain(Mime->Charset, HKEY("UTF-8"), 0);
1165         FreeStrBuf(&Line);
1166         FreeStrBuf(&Line1);
1167         FreeStrBuf(&Line2);
1168 }
1169
1170 void render_MAIL_html(StrBuf *Target, WCTemplputParams *TP, StrBuf *FoundCharset)
1171 {
1172         wc_mime_attachment *Mime = (wc_mime_attachment *) CTX(CTX_MIME_ATACH);
1173         StrBuf *Buf;
1174
1175         if (StrLength(Mime->Data) == 0)
1176                 return;
1177
1178         Buf = NewStrBufPlain(NULL, StrLength(Mime->Data));
1179
1180         /* HTML is fun, but we've got to strip it first */
1181         output_html(ChrPtr(Mime->Charset), 
1182                     (WC->CurRoom.view == VIEW_WIKI ? 1 : 0), 
1183                     Mime->msgnum,
1184                     Mime->Data, Buf);
1185         FreeStrBuf(&Mime->Data);
1186         Mime->Data = Buf;
1187 }
1188
1189 #ifdef HAVE_MARKDOWN
1190 /*
1191 char * MarkdownHandleURL(const char* SourceURL, const int len, void* something)
1192 {
1193
1194 }
1195 */
1196 void render_MAIL_markdown(StrBuf *Target, WCTemplputParams *TP, StrBuf *FoundCharset)
1197 {
1198 #include <mkdio.h>
1199         wc_mime_attachment *Mime = (wc_mime_attachment *) CTX(CTX_MIME_ATACH);
1200         MMIOT *doc;
1201         char *md_as_html = NULL;
1202         const char *format;
1203
1204         if (StrLength(Mime->Data) == 0)
1205                 return;
1206
1207         format = bstr("format");
1208
1209         if ((format == NULL) || 
1210             strcmp(format, "plain"))
1211         {
1212                 doc = mkd_string(ChrPtr(Mime->Data), StrLength(Mime->Data), 0);
1213                 mkd_basename(doc, "/wiki?page=");
1214                 mkd_compile(doc, 0);
1215                 if (mkd_document(doc, &md_as_html) != EOF) {
1216                         FreeStrBuf(&Mime->Data);
1217                         Mime->Data = NewStrBufPlain(md_as_html, -1);
1218                 }
1219                 mkd_cleanup(doc);
1220         }
1221 }
1222 #endif
1223
1224 void render_MAIL_UNKNOWN(StrBuf *Target, WCTemplputParams *TP, StrBuf *FoundCharset)
1225 {
1226         wc_mime_attachment *Mime = (wc_mime_attachment *) CTX(CTX_MIME_ATACH);
1227         /* Unknown weirdness */
1228         FlushStrBuf(Mime->Data);
1229         StrBufAppendBufPlain(Mime->Data, _("I don't know how to display "), -1, 0);
1230         StrBufAppendBuf(Mime->Data, Mime->ContentType, 0);
1231         StrBufAppendBufPlain(Mime->Data, HKEY("<br>\n"), 0);
1232 }
1233
1234
1235 HashList *iterate_get_mime_All(StrBuf *Target, WCTemplputParams *TP)
1236 {
1237         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
1238         return Msg->Attachments;
1239 }
1240 HashList *iterate_get_mime_Submessages(StrBuf *Target, WCTemplputParams *TP)
1241 {
1242         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
1243         return Msg->Submessages;
1244 }
1245 HashList *iterate_get_mime_AttachLinks(StrBuf *Target, WCTemplputParams *TP)
1246 {
1247         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
1248         return Msg->AttachLinks;
1249 }
1250 HashList *iterate_get_mime_Attachments(StrBuf *Target, WCTemplputParams *TP)
1251 {
1252         message_summary *Msg = (message_summary*) CTX(CTX_MAILSUM);
1253         return Msg->AllAttach;
1254 }
1255
1256 void tmplput_MIME_Name(StrBuf *Target, WCTemplputParams *TP)
1257 {
1258         wc_mime_attachment *mime = (wc_mime_attachment*) CTX(CTX_MIME_ATACH);
1259         StrBufAppendTemplate(Target, TP, mime->Name, 0);
1260 }
1261
1262 void tmplput_MIME_FileName(StrBuf *Target, WCTemplputParams *TP)
1263 {
1264         wc_mime_attachment *mime = (wc_mime_attachment*) CTX(CTX_MIME_ATACH);
1265         StrBufAppendTemplate(Target, TP, mime->FileName, 0);
1266 }
1267
1268 void tmplput_MIME_PartNum(StrBuf *Target, WCTemplputParams *TP)
1269 {
1270         wc_mime_attachment *mime = (wc_mime_attachment*) CTX(CTX_MIME_ATACH);
1271         StrBufAppendTemplate(Target, TP, mime->PartNum, 0);
1272 }
1273
1274 void tmplput_MIME_MsgNum(StrBuf *Target, WCTemplputParams *TP)
1275 {
1276         wc_mime_attachment *mime = (wc_mime_attachment*) CTX(CTX_MIME_ATACH);
1277         StrBufAppendPrintf(Target, "%ld", mime->msgnum);
1278 }
1279
1280 void tmplput_MIME_Disposition(StrBuf *Target, WCTemplputParams *TP)
1281 {
1282         wc_mime_attachment *mime = (wc_mime_attachment*) CTX(CTX_MIME_ATACH);
1283         StrBufAppendTemplate(Target, TP, mime->Disposition, 0);
1284 }
1285
1286 void tmplput_MIME_ContentType(StrBuf *Target, WCTemplputParams *TP)
1287 {
1288         wc_mime_attachment *mime = (wc_mime_attachment*) CTX(CTX_MIME_ATACH);
1289         StrBufAppendTemplate(Target, TP, mime->ContentType, 0);
1290 }
1291
1292 void examine_charset(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
1293 {
1294         Msg->MsgBody->Charset = NewStrBufDup(HdrLine);
1295 }
1296
1297 void tmplput_MIME_Charset(StrBuf *Target, WCTemplputParams *TP)
1298 {
1299         wc_mime_attachment *mime = (wc_mime_attachment*) CTX(CTX_MIME_ATACH);
1300         StrBufAppendTemplate(Target, TP, mime->Charset, 0);
1301 }
1302
1303 void tmplput_MIME_Data(StrBuf *Target, WCTemplputParams *TP)
1304 {
1305         wc_mime_attachment *mime = (wc_mime_attachment*) CTX(CTX_MIME_ATACH);
1306         if (mime->Renderer != NULL)
1307                 mime->Renderer->f(Target, TP, NULL);
1308         StrBufAppendTemplate(Target, TP, mime->Data, 0);
1309         /* TODO: check whether we need to load it now? */
1310 }
1311
1312 void tmplput_MIME_LoadData(StrBuf *Target, WCTemplputParams *TP)
1313 {
1314         wcsession *WCC = WC;    
1315         wc_mime_attachment *mime = (wc_mime_attachment*) CTX(CTX_MIME_ATACH);
1316         wc_mime_attachment *att;
1317         
1318         if (( (!strcasecmp(ChrPtr(mime->Disposition), "inline"))||
1319               (!strcasecmp(ChrPtr(mime->Disposition), "attachment"))) && 
1320             (strcasecmp(ChrPtr(mime->ContentType), "application/ms-tnef")!=0))
1321         {
1322                 
1323                 int n;
1324                 char N[64];
1325                 /* steal this mime part... */
1326                 att = malloc(sizeof(wc_mime_attachment));
1327                 memcpy(att, mime, sizeof(wc_mime_attachment));
1328                 memset(mime, 0, sizeof(wc_mime_attachment));
1329
1330                 if (att->Data == NULL) 
1331                         MimeLoadData(att);
1332
1333                 if (WCC->attachments == NULL)
1334                         WCC->attachments = NewHash(1, NULL);
1335                 /* And add it to the list. */
1336                 n = snprintf(N, sizeof N, "%d", GetCount(WCC->attachments) + 1);
1337                 Put(WCC->attachments, N, n, att, DestroyMime);
1338         }
1339 }
1340
1341 void tmplput_MIME_Length(StrBuf *Target, WCTemplputParams *TP)
1342 {
1343         wc_mime_attachment *mime = (wc_mime_attachment*) CTX(CTX_MIME_ATACH);
1344         StrBufAppendPrintf(Target, "%ld", mime->length);
1345 }
1346
1347 HashList *iterate_get_registered_Attachments(StrBuf *Target, WCTemplputParams *TP)
1348 {
1349         return WC->attachments;
1350 }
1351
1352 void get_registered_Attachments_Count(StrBuf *Target, WCTemplputParams *TP)
1353 {
1354         StrBufAppendPrintf(Target, "%ld", GetCount (WC->attachments));
1355 }
1356
1357 void servcmd_do_search(char *buf, long bufsize)
1358 {
1359         snprintf(buf, bufsize, "MSGS SEARCH|%s", bstr("query"));
1360 }
1361
1362 void servcmd_headers(char *buf, long bufsize)
1363 {
1364         snprintf(buf, bufsize, "MSGS ALL");
1365 }
1366
1367 void servcmd_readfwd(char *buf, long bufsize)
1368 {
1369         snprintf(buf, bufsize, "MSGS ALL");
1370 }
1371
1372 void servcmd_readgt(char *buf, long bufsize)
1373 {
1374         snprintf(buf, bufsize, "MSGS GT|%s", bstr("gt"));
1375 }
1376
1377 void servcmd_readlt(char *buf, long bufsize)
1378 {
1379         snprintf(buf, bufsize, "MSGS LT|%s", bstr("lt"));
1380 }
1381
1382 void servcmd_readnew(char *buf, long bufsize)
1383 {
1384         snprintf(buf, bufsize, "MSGS NEW");
1385 }
1386
1387 void servcmd_readold(char *buf, long bufsize)
1388 {
1389         snprintf(buf, bufsize, "MSGS OLD");
1390 }
1391
1392
1393 /* DO NOT REORDER OR REMOVE ANY OF THESE */
1394 readloop_struct rlid[] = {
1395         { {HKEY("do_search")},  servcmd_do_search       },
1396         { {HKEY("headers")},    servcmd_headers         },
1397         { {HKEY("readfwd")},    servcmd_readfwd         },
1398         { {HKEY("readnew")},    servcmd_readnew         },
1399         { {HKEY("readold")},    servcmd_readold         },
1400         { {HKEY("readgt")},     servcmd_readgt          },
1401         { {HKEY("readlt")},     servcmd_readlt          }
1402 };
1403
1404
1405 int ParseMessageListHeaders_Detail(StrBuf *Line, 
1406                                    const char **pos, 
1407                                    message_summary *Msg, 
1408                                    StrBuf *ConversionBuffer)
1409 {
1410         wcsession *WCC = WC;
1411         long len;
1412         long totallen;
1413
1414         CheckConvertBufs(WCC);
1415
1416         totallen = StrLength(Line);
1417         Msg->from = NewStrBufPlain(NULL, totallen);
1418         len = StrBufExtract_NextToken(ConversionBuffer, Line, pos, '|');
1419         if (len > 0) {
1420                 /* Handle senders with RFC2047 encoding */
1421                 StrBuf_RFC822_2_Utf8(Msg->from, 
1422                                      ConversionBuffer, 
1423                                      WCC->DefaultCharset, 
1424                                      NULL, 
1425                                      WCC->ConvertBuf1,
1426                                      WCC->ConvertBuf2);
1427         }
1428                         
1429         /* node name */
1430         len = StrBufExtract_NextToken(ConversionBuffer, Line, pos, '|');
1431         if ((len > 0 ) &&
1432             ( ((WCC->CurRoom.QRFlags & QR_NETWORK)
1433                || ((strcasecmp(ChrPtr(ConversionBuffer), ChrPtr(WCC->serv_info->serv_nodename))
1434                     && (strcasecmp(ChrPtr(ConversionBuffer), ChrPtr(WCC->serv_info->serv_fqdn))))))))
1435         {
1436                 StrBufAppendBufPlain(Msg->from, HKEY(" @ "), 0);
1437                 StrBufAppendBuf(Msg->from, ConversionBuffer, 0);
1438         }
1439
1440         /* Internet address (not used)
1441          *      StrBufExtract_token(Msg->inetaddr, Line, 4, '|');
1442          */
1443         StrBufSkip_NTokenS(Line, pos, '|', 1);
1444         Msg->subj = NewStrBufPlain(NULL, totallen);
1445
1446         FlushStrBuf(ConversionBuffer);
1447         /* we assume the subject is the last parameter inside of the list; 
1448          * thus we don't use the tokenizer to fetch it, since it will hick up 
1449          * on tokenizer chars inside of the subjects
1450         StrBufExtract_NextToken(ConversionBuffer,  Line, pos, '|');
1451         */
1452         len = 0;
1453         if (*pos != StrBufNOTNULL) {
1454                 len = totallen - (*pos - ChrPtr(Line));
1455                 StrBufPlain(ConversionBuffer, *pos, len);
1456                 *pos = StrBufNOTNULL;
1457                 if ((len > 0) &&
1458                     (*(ChrPtr(ConversionBuffer) + len - 1) == '|'))
1459                         StrBufCutRight(ConversionBuffer, 1);
1460         }
1461
1462         if (len == 0)
1463                 StrBufAppendBufPlain(Msg->subj, _("(no subject)"), -1,0);
1464         else {
1465                 StrBuf_RFC822_2_Utf8(Msg->subj, 
1466                                      ConversionBuffer, 
1467                                      WCC->DefaultCharset, 
1468                                      NULL,
1469                                      WCC->ConvertBuf1,
1470                                      WCC->ConvertBuf2);
1471         }
1472
1473         return 1;
1474 }
1475
1476
1477 int mailview_GetParamsGetServerCall(SharedMessageStatus *Stat, 
1478                                     void **ViewSpecific, 
1479                                     long oper, 
1480                                     char *cmd, 
1481                                     long len,
1482                                     char *filter,
1483                                     long flen)
1484 {
1485         DoTemplate(HKEY("msg_listview"),NULL,&NoCtx);
1486
1487         return 200;
1488 }
1489
1490 int mailview_Cleanup(void **ViewSpecific)
1491 {
1492         /* Note: wDumpContent() will output one additional </div> tag. */
1493         /* We ought to move this out into template */
1494         wDumpContent(1);
1495
1496         return 0;
1497 }
1498
1499
1500 int json_GetParamsGetServerCall(SharedMessageStatus *Stat, 
1501                                 void **ViewSpecific, 
1502                                 long oper, 
1503                                 char *cmd, 
1504                                 long len,
1505                                 char *filter,
1506                                 long flen)
1507 {
1508         Stat->defaultsortorder = 2;
1509         Stat->sortit = 1;
1510         Stat->load_seen = 1;
1511         /* Generally using maxmsgs|startmsg is not required
1512            in mailbox view, but we have a 'safemode' for clients
1513            (*cough* Exploder) that simply can't handle too many */
1514         if (havebstr("maxmsgs"))  Stat->maxmsgs  = ibstr("maxmsgs");
1515         else                      Stat->maxmsgs  = 9999999;
1516         if (havebstr("startmsg")) Stat->startmsg = lbstr("startmsg");
1517         snprintf(cmd, len, "MSGS %s|%s||1",
1518                  (oper == do_search) ? "SEARCH" : "ALL",
1519                  (oper == do_search) ? bstr("query") : ""
1520                 );
1521
1522         return 200;
1523 }
1524 int json_MessageListHdr(SharedMessageStatus *Stat, void **ViewSpecific) 
1525 {
1526         /* TODO: make a generic function */
1527         hprintf("HTTP/1.1 200 OK\r\n");
1528         hprintf("Content-type: application/json; charset=utf-8\r\n");
1529         hprintf("Server: %s / %s\r\n", PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software));
1530         hprintf("Connection: close\r\n");
1531         hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
1532         begin_burst();
1533         return 0;
1534 }
1535
1536 int json_RenderView_or_Tail(SharedMessageStatus *Stat, 
1537                             void **ViewSpecific, 
1538                             long oper)
1539 {
1540         DoTemplate(HKEY("mailsummary_json"),NULL, NULL);
1541         
1542         return 0;
1543 }
1544
1545 int json_Cleanup(void **ViewSpecific)
1546 {
1547         /* Note: wDumpContent() will output one additional </div> tag. */
1548         /* We ought to move this out into template */
1549         end_burst();
1550
1551         return 0;
1552 }
1553
1554
1555
1556 void 
1557 InitModule_MSGRENDERERS
1558 (void)
1559 {
1560         RegisterCTX(CTX_MAILSUM);
1561         RegisterCTX(CTX_MIME_ATACH);
1562         RegisterReadLoopHandlerset(
1563                 VIEW_MAILBOX,
1564                 mailview_GetParamsGetServerCall,
1565                 NULL, /* TODO: is this right? */
1566                 NULL,
1567                 ParseMessageListHeaders_Detail,
1568                 NULL,
1569                 NULL,
1570                 mailview_Cleanup);
1571
1572         RegisterReadLoopHandlerset(
1573                 VIEW_JSON_LIST,
1574                 json_GetParamsGetServerCall,
1575                 json_MessageListHdr,
1576                 NULL, /* TODO: is this right? */
1577                 ParseMessageListHeaders_Detail,
1578                 NULL,
1579                 json_RenderView_or_Tail,
1580                 json_Cleanup);
1581
1582         RegisterSortFunc(HKEY("date"), 
1583                          NULL, 0,
1584                          summcmp_date,
1585                          summcmp_rdate,
1586                          groupchange_date,
1587                          CTX_MAILSUM);
1588         RegisterSortFunc(HKEY("subject"), 
1589                          NULL, 0,
1590                          summcmp_subj,
1591                          summcmp_rsubj,
1592                          groupchange_subj,
1593                          CTX_MAILSUM);
1594         RegisterSortFunc(HKEY("sender"),
1595                          NULL, 0,
1596                          summcmp_sender,
1597                          summcmp_rsender,
1598                          groupchange_sender,
1599                          CTX_MAILSUM);
1600
1601         RegisterNamespace("SUMM:COUNT", 0, 0, tmplput_SUMM_COUNT, NULL, CTX_NONE);
1602         /* iterate over all known mails in WC->summ */
1603         RegisterIterator("MAIL:SUMM:MSGS", 0, NULL, iterate_get_mailsumm_All,
1604                          NULL,NULL, CTX_MAILSUM, CTX_NONE, IT_NOFLAG);
1605
1606         RegisterNamespace("MAIL:SUMM:EUID", 0, 1, tmplput_MAIL_SUMM_EUID, NULL, CTX_MAILSUM);
1607         RegisterNamespace("MAIL:SUMM:DATEBRIEF", 0, 0, tmplput_MAIL_SUMM_DATE_BRIEF, NULL, CTX_MAILSUM);
1608         RegisterNamespace("MAIL:SUMM:DATEFULL", 0, 0, tmplput_MAIL_SUMM_DATE_FULL, NULL, CTX_MAILSUM);
1609         RegisterNamespace("MAIL:SUMM:DATENO",  0, 0, tmplput_MAIL_SUMM_DATE_NO,  NULL, CTX_MAILSUM);
1610         RegisterNamespace("MAIL:SUMM:N",       0, 0, tmplput_MAIL_SUMM_N,        NULL, CTX_MAILSUM);
1611         RegisterNamespace("MAIL:SUMM:PERMALINK", 0, 0, tmplput_MAIL_SUMM_PERMALINK, NULL, CTX_MAILSUM);
1612         RegisterNamespace("MAIL:SUMM:FROM",    0, 2, tmplput_MAIL_SUMM_FROM,     NULL, CTX_MAILSUM);
1613         RegisterNamespace("MAIL:SUMM:TO",      0, 2, tmplput_MAIL_SUMM_TO,       NULL, CTX_MAILSUM);
1614         RegisterNamespace("MAIL:SUMM:SUBJECT", 0, 4, tmplput_MAIL_SUMM_SUBJECT,  NULL, CTX_MAILSUM);
1615         RegisterNamespace("MAIL:SUMM:NTATACH", 0, 0, tmplput_MAIL_SUMM_NATTACH,  NULL, CTX_MAILSUM);
1616         RegisterNamespace("MAIL:SUMM:CCCC", 0, 2, tmplput_MAIL_SUMM_CCCC, NULL, CTX_MAILSUM);
1617         RegisterNamespace("MAIL:SUMM:REPLYTO", 0, 2, tmplput_MAIL_SUMM_REPLYTO, NULL, CTX_MAILSUM);
1618         RegisterNamespace("MAIL:SUMM:H_NODE", 0, 2, tmplput_MAIL_SUMM_H_NODE,  NULL, CTX_MAILSUM);
1619         RegisterNamespace("MAIL:SUMM:ALLRCPT", 0, 2, tmplput_MAIL_SUMM_ALLRCPT,  NULL, CTX_MAILSUM);
1620         RegisterNamespace("MAIL:SUMM:ORGROOM", 0, 2, tmplput_MAIL_SUMM_ORGROOM,  NULL, CTX_MAILSUM);
1621         RegisterNamespace("MAIL:SUMM:RFCA", 0, 2, tmplput_MAIL_SUMM_RFCA, NULL, CTX_MAILSUM);
1622         RegisterNamespace("MAIL:SUMM:OTHERNODE", 2, 0, tmplput_MAIL_SUMM_OTHERNODE,  NULL, CTX_MAILSUM);
1623         RegisterNamespace("MAIL:SUMM:REFIDS", 0, 1, tmplput_MAIL_SUMM_REFIDS,  NULL, CTX_MAILSUM);
1624         RegisterNamespace("MAIL:SUMM:INREPLYTO", 0, 2, tmplput_MAIL_SUMM_INREPLYTO,  NULL, CTX_MAILSUM);
1625         RegisterNamespace("MAIL:BODY", 0, 2, tmplput_MAIL_BODY,  NULL, CTX_MAILSUM);
1626         RegisterNamespace("MAIL:QUOTETEXT", 1, 2, tmplput_QUOTED_MAIL_BODY,  NULL, CTX_NONE);
1627         RegisterNamespace("MAIL:EDITTEXT", 1, 2, tmplput_EDIT_MAIL_BODY,  NULL, CTX_NONE);
1628         RegisterNamespace("MAIL:EDITWIKI", 1, 2, tmplput_EDIT_WIKI_BODY,  NULL, CTX_NONE);
1629         RegisterConditional("COND:MAIL:SUMM:RFCA", 0, Conditional_MAIL_SUMM_RFCA,  CTX_MAILSUM);
1630         RegisterConditional("COND:MAIL:SUMM:CCCC", 0, Conditional_MAIL_SUMM_CCCC,  CTX_MAILSUM);
1631         RegisterConditional("COND:MAIL:SUMM:REPLYTO", 0, Conditional_MAIL_SUMM_REPLYTO,  CTX_MAILSUM);
1632         RegisterConditional("COND:MAIL:SUMM:UNREAD", 0, Conditional_MAIL_SUMM_UNREAD, CTX_MAILSUM);
1633         RegisterConditional("COND:MAIL:SUMM:H_NODE", 0, Conditional_MAIL_SUMM_H_NODE, CTX_MAILSUM);
1634         RegisterConditional("COND:MAIL:SUMM:OTHERNODE", 0, Conditional_MAIL_SUMM_OTHERNODE, CTX_MAILSUM);
1635         RegisterConditional("COND:MAIL:SUMM:SUBJECT", 0, Conditional_MAIL_SUMM_SUBJECT, CTX_MAILSUM);
1636         RegisterConditional("COND:MAIL:ANON", 0, Conditional_ANONYMOUS_MESSAGE, CTX_MAILSUM);
1637         RegisterConditional("COND:MAIL:TO", 0, Conditional_MAIL_SUMM_TO, CTX_MAILSUM);  
1638         RegisterConditional("COND:MAIL:SUBJ", 0, Conditional_MAIL_SUMM_SUBJ, CTX_MAILSUM);      
1639
1640         /* do we have mimetypes to iterate over? */
1641         RegisterConditional("COND:MAIL:MIME:ATTACH", 0, Conditional_MAIL_MIME_ALL, CTX_MAILSUM);
1642         RegisterConditional("COND:MAIL:MIME:ATTACH:SUBMESSAGES", 0, Conditional_MAIL_MIME_SUBMESSAGES, CTX_MAILSUM);
1643         RegisterConditional("COND:MAIL:MIME:ATTACH:LINKS", 0, Conditional_MAIL_MIME_ATTACHLINKS, CTX_MAILSUM);
1644         RegisterConditional("COND:MAIL:MIME:ATTACH:ATT", 0, Conditional_MAIL_MIME_ATTACH, CTX_MAILSUM);
1645         RegisterIterator("MAIL:MIME:ATTACH", 0, NULL, iterate_get_mime_All, 
1646                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM, IT_NOFLAG);
1647         RegisterIterator("MAIL:MIME:ATTACH:SUBMESSAGES", 0, NULL, iterate_get_mime_Submessages, 
1648                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM, IT_NOFLAG);
1649         RegisterIterator("MAIL:MIME:ATTACH:LINKS", 0, NULL, iterate_get_mime_AttachLinks, 
1650                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM, IT_NOFLAG);
1651         RegisterIterator("MAIL:MIME:ATTACH:ATT", 0, NULL, iterate_get_mime_Attachments, 
1652                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM, IT_NOFLAG);
1653
1654         /* Parts of a mime attachent */
1655         RegisterNamespace("MAIL:MIME:NAME", 0, 2, tmplput_MIME_Name, NULL, CTX_MIME_ATACH);
1656         RegisterNamespace("MAIL:MIME:FILENAME", 0, 2, tmplput_MIME_FileName, NULL, CTX_MIME_ATACH);
1657         RegisterNamespace("MAIL:MIME:PARTNUM", 0, 2, tmplput_MIME_PartNum, NULL, CTX_MIME_ATACH);
1658         RegisterNamespace("MAIL:MIME:MSGNUM", 0, 2, tmplput_MIME_MsgNum, NULL, CTX_MIME_ATACH);
1659         RegisterNamespace("MAIL:MIME:DISPOSITION", 0, 2, tmplput_MIME_Disposition, NULL, CTX_MIME_ATACH);
1660         RegisterNamespace("MAIL:MIME:CONTENTTYPE", 0, 2, tmplput_MIME_ContentType, NULL, CTX_MIME_ATACH);
1661         RegisterNamespace("MAIL:MIME:CHARSET", 0, 2, tmplput_MIME_Charset, NULL, CTX_MIME_ATACH);
1662         RegisterNamespace("MAIL:MIME:LENGTH", 0, 2, tmplput_MIME_Length, NULL, CTX_MIME_ATACH);
1663         RegisterNamespace("MAIL:MIME:DATA", 0, 2, tmplput_MIME_Data, NULL, CTX_MIME_ATACH);
1664         /* load the actual attachment into WC->attachments; no output!!! */
1665         RegisterNamespace("MAIL:MIME:LOADDATA", 0, 0, tmplput_MIME_LoadData, NULL, CTX_MIME_ATACH);
1666
1667         /* iterate the WC->attachments; use the above tokens for their contents */
1668         RegisterIterator("MSG:ATTACHNAMES", 0, NULL, iterate_get_registered_Attachments, 
1669                          NULL, NULL, CTX_MIME_ATACH, CTX_NONE, IT_NOFLAG);
1670
1671         RegisterNamespace("MSG:NATTACH", 0, 0, get_registered_Attachments_Count,  NULL, CTX_NONE);
1672
1673         /* mime renderers translate an attachment into webcit viewable html text */
1674         RegisterMimeRenderer(HKEY("message/rfc822"), render_MAIL, 0, 150);
1675         RegisterMimeRenderer(HKEY("text/x-vcard"), render_MIME_VCard, 1, 201);
1676         RegisterMimeRenderer(HKEY("text/vcard"), render_MIME_VCard, 1, 200);
1677 //*
1678         RegisterMimeRenderer(HKEY("text/calendar"), render_MIME_ICS, 1, 501);
1679         RegisterMimeRenderer(HKEY("application/ics"), render_MIME_ICS, 1, 500);
1680 //*/
1681         RegisterMimeRenderer(HKEY("text/x-citadel-variformat"), render_MAIL_variformat, 1, 2);
1682         RegisterMimeRenderer(HKEY("text/plain"), render_MAIL_text_plain, 1, 3);
1683         RegisterMimeRenderer(HKEY("text"), render_MAIL_text_plain, 1, 1);
1684         RegisterMimeRenderer(HKEY("text/html"), render_MAIL_html, 1, 100);
1685 #ifdef HAVE_MARKDOWN
1686         RegisterMimeRenderer(HKEY("text/x-markdown"), render_MAIL_markdown, 1, 30);
1687 #endif
1688         RegisterMimeRenderer(HKEY(""), render_MAIL_UNKNOWN, 0, 0);
1689
1690         /* these headers are citserver replies to MSG4 and friends. one evaluator for each */
1691         RegisterMsgHdr(HKEY("nhdr"), examine_nhdr, 0);
1692         RegisterMsgHdr(HKEY("type"), examine_type, 0);
1693         RegisterMsgHdr(HKEY("from"), examine_from, 0);
1694         RegisterMsgHdr(HKEY("subj"), examine_subj, 0);
1695         RegisterMsgHdr(HKEY("msgn"), examine_msgn, 0);
1696         RegisterMsgHdr(HKEY("wefw"), examine_wefw, 0);
1697         RegisterMsgHdr(HKEY("cccc"), examine_cccc, 0);
1698         RegisterMsgHdr(HKEY("rep2"), examine_replyto, 0);
1699         RegisterMsgHdr(HKEY("hnod"), examine_hnod, 0);
1700         RegisterMsgHdr(HKEY("room"), examine_room, 0);
1701         RegisterMsgHdr(HKEY("rfca"), examine_rfca, 0);
1702         RegisterMsgHdr(HKEY("node"), examine_node, 0);
1703         RegisterMsgHdr(HKEY("rcpt"), examine_rcpt, 0);
1704         RegisterMsgHdr(HKEY("nvto"), examine_nvto, 0);
1705         RegisterMsgHdr(HKEY("time"), examine_time, 0);
1706         RegisterMsgHdr(HKEY("part"), examine_mime_part, 0);
1707         RegisterMsgHdr(HKEY("text"), examine_text, 1);
1708         /* these are the content-type headers we get infront of a message; put it into the same hash since it doesn't clash. */
1709         RegisterMsgHdr(HKEY("X-Citadel-MSG4-Partnum"), examine_msg4_partnum, 0);
1710         RegisterMsgHdr(HKEY("Content-type"), examine_content_type, 0);
1711         RegisterMsgHdr(HKEY("Content-length"), examine_content_lengh, 0);
1712         RegisterMsgHdr(HKEY("Content-transfer-encoding"), examine_content_encoding, 0); /* do we care? */
1713         RegisterMsgHdr(HKEY("charset"), examine_charset, 0);
1714
1715         /* Don't care about these... */
1716         RegisterMsgHdr(HKEY("pref"), examine_pref, 0);
1717         RegisterMsgHdr(HKEY("suff"), examine_suff, 0);
1718         RegisterMsgHdr(HKEY("path"), examine_path, 0);
1719 }
1720
1721 void 
1722 InitModule2_MSGRENDERERS
1723 (void)
1724 {
1725         /* and finalize the anouncement to the server... */
1726         CreateMimeStr();
1727 }
1728 void 
1729 ServerStartModule_MSGRENDERERS
1730 (void)
1731 {
1732         MsgHeaderHandler = NewHash(1, NULL);
1733         MimeRenderHandler = NewHash(1, NULL);
1734         ReadLoopHandler = NewHash(1, NULL);
1735 }
1736
1737 void 
1738 ServerShutdownModule_MSGRENDERERS
1739 (void)
1740 {
1741         DeleteHash(&MsgHeaderHandler);
1742         DeleteHash(&MimeRenderHandler);
1743         DeleteHash(&ReadLoopHandler);
1744 }
1745
1746
1747
1748 void 
1749 SessionDestroyModule_MSGRENDERERS
1750 (wcsession *sess)
1751 {
1752         DeleteHash(&sess->attachments);
1753         FreeStrBuf(&sess->ConvertBuf1);
1754         FreeStrBuf(&sess->ConvertBuf2);
1755 }