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