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