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