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