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