* style cleanup
[citadel.git] / webcit / msg_renderers.c
1 #include "webcit.h"
2 #include "webserver.h"
3
4 /*
5  * message index functions
6  */
7
8
9 void DestroyMimeParts(wc_mime_attachment *Mime)
10 {
11         FreeStrBuf(&Mime->Name);
12         FreeStrBuf(&Mime->FileName);
13         FreeStrBuf(&Mime->PartNum);
14         FreeStrBuf(&Mime->Disposition);
15         FreeStrBuf(&Mime->ContentType);
16         FreeStrBuf(&Mime->Charset);
17         FreeStrBuf(&Mime->Data);
18 }
19
20 void DestroyMime(void *vMime)
21 {
22         wc_mime_attachment *Mime = (wc_mime_attachment*)vMime;
23         DestroyMimeParts(Mime);
24         free(Mime);
25 }
26
27 void DestroyMessageSummary(void *vMsg)
28 {
29         message_summary *Msg = (message_summary*) vMsg;
30
31         FreeStrBuf(&Msg->from);
32         FreeStrBuf(&Msg->to);
33         FreeStrBuf(&Msg->subj);
34         FreeStrBuf(&Msg->reply_inreplyto);
35         FreeStrBuf(&Msg->reply_references);
36         FreeStrBuf(&Msg->cccc);
37         FreeStrBuf(&Msg->hnod);
38         FreeStrBuf(&Msg->AllRcpt);
39         FreeStrBuf(&Msg->Room);
40         FreeStrBuf(&Msg->Rfca);
41         FreeStrBuf(&Msg->OtherNode);
42
43         FreeStrBuf(&Msg->reply_to);
44
45         DeleteHash(&Msg->Attachments);  /**< list of Accachments */
46         DeleteHash(&Msg->Submessages);
47         DeleteHash(&Msg->AttachLinks);
48         DeleteHash(&Msg->AllAttach);
49         free(Msg);
50 }
51
52
53
54 void RegisterMsgHdr(const char *HeaderName, long HdrNLen, ExamineMsgHeaderFunc evaluator, int type)
55 {
56         headereval *ev;
57         ev = (headereval*) malloc(sizeof(headereval));
58         ev->evaluator = evaluator;
59         ev->Type = type;
60         Put(MsgHeaderHandler, HeaderName, HdrNLen, ev, NULL);
61 }
62
63 void RegisterMimeRenderer(const char *HeaderName, long HdrNLen, RenderMimeFunc MimeRenderer)
64 {
65         RenderMimeFuncStruct *f;
66
67         f = (RenderMimeFuncStruct*) malloc(sizeof(RenderMimeFuncStruct));
68         f->f = MimeRenderer;
69         Put(MimeRenderHandler, HeaderName, HdrNLen, f, NULL);
70         
71 }
72
73 /*----------------------------------------------------------------------------*/
74
75 /*
76  *  comparator for two longs in descending order.
77  */
78 int longcmp_r(const void *s1, const void *s2) {
79         long l1;
80         long l2;
81
82         l1 = *(long *)GetSearchPayload(s1);
83         l2 = *(long *)GetSearchPayload(s2);
84
85         if (l1 > l2) return(-1);
86         if (l1 < l2) return(+1);
87         return(0);
88 }
89
90 /*
91  *  comparator for longs; descending order.
92  */
93 int qlongcmp_r(const void *s1, const void *s2) {
94         long l1 = (long) s1;
95         long l2 = (long) s2;
96
97         if (l1 > l2) return(-1);
98         if (l1 < l2) return(+1);
99         return(0);
100 }
101
102  
103 /*
104  * comparator for message summary structs by ascending subject.
105  */
106 int summcmp_subj(const void *s1, const void *s2) {
107         message_summary *summ1;
108         message_summary *summ2;
109         
110         summ1 = (message_summary *)GetSearchPayload(s1);
111         summ2 = (message_summary *)GetSearchPayload(s2);
112         return strcasecmp(ChrPtr(summ1->subj), ChrPtr(summ2->subj));
113 }
114
115 /*
116  * comparator for message summary structs by descending subject.
117  */
118 int summcmp_rsubj(const void *s1, const void *s2) {
119         message_summary *summ1;
120         message_summary *summ2;
121         
122         summ1 = (message_summary *)GetSearchPayload(s1);
123         summ2 = (message_summary *)GetSearchPayload(s2);
124         return strcasecmp(ChrPtr(summ2->subj), ChrPtr(summ1->subj));
125 }
126 /*
127  * comparator for message summary structs by descending subject.
128  */
129 int groupchange_subj(const void *s1, const void *s2) {
130         message_summary *summ1;
131         message_summary *summ2;
132         
133         summ1 = (message_summary *)s1;
134         summ2 = (message_summary *)s2;
135         return ChrPtr(summ2->subj)[0] != ChrPtr(summ1->subj)[0];
136 }
137
138 /*
139  * comparator for message summary structs by ascending sender.
140  */
141 int summcmp_sender(const void *s1, const void *s2) {
142         message_summary *summ1;
143         message_summary *summ2;
144         
145         summ1 = (message_summary *)GetSearchPayload(s1);
146         summ2 = (message_summary *)GetSearchPayload(s2);
147         return strcasecmp(ChrPtr(summ1->from), ChrPtr(summ2->from));
148 }
149
150 /*
151  * comparator for message summary structs by descending sender.
152  */
153 int summcmp_rsender(const void *s1, const void *s2) {
154         message_summary *summ1;
155         message_summary *summ2;
156         
157         summ1 = (message_summary *)GetSearchPayload(s1);
158         summ2 = (message_summary *)GetSearchPayload(s2);
159         return strcasecmp(ChrPtr(summ2->from), ChrPtr(summ1->from));
160 }
161 /*
162  * comparator for message summary structs by descending sender.
163  */
164 int groupchange_sender(const void *s1, const void *s2) {
165         message_summary *summ1;
166         message_summary *summ2;
167         
168         summ1 = (message_summary *)s1;
169         summ2 = (message_summary *)s2;
170         return strcasecmp(ChrPtr(summ2->from), ChrPtr(summ1->from)) != 0;
171
172 }
173
174 /*
175  * comparator for message summary structs by ascending date.
176  */
177 int summcmp_date(const void *s1, const void *s2) {
178         message_summary *summ1;
179         message_summary *summ2;
180         
181         summ1 = (message_summary *)GetSearchPayload(s1);
182         summ2 = (message_summary *)GetSearchPayload(s2);
183
184         if (summ1->date < summ2->date) return -1;
185         else if (summ1->date > summ2->date) return +1;
186         else return 0;
187 }
188
189 /*
190  * comparator for message summary structs by descending date.
191  */
192 int summcmp_rdate(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 const long DAYSECONDS = 24 * 60 * 60;
208 int groupchange_date(const void *s1, const void *s2) {
209         message_summary *summ1;
210         message_summary *summ2;
211         
212         summ1 = (message_summary *)s1;
213         summ2 = (message_summary *)s2;
214
215         return (summ1->date % DAYSECONDS) != (summ2->date %DAYSECONDS);
216 }
217
218
219 /*----------------------------------------------------------------------------*/
220 /* Don't wanna know... or? */
221 void examine_pref(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset) {return;}
222 void examine_suff(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset) {return;}
223 void examine_path(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset) {return;}
224 void examine_content_encoding(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
225 {
226 /* TODO: do we care? */
227 }
228
229 void examine_nhdr(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
230 {
231         Msg->nhdr = 0;
232         if (!strncasecmp(ChrPtr(HdrLine), "yes", 8))
233                 Msg->nhdr = 1;
234 }
235 int Conditional_ANONYMOUS_MESSAGE(StrBuf *Target, WCTemplputParams *TP)
236 {
237         message_summary *Msg = (message_summary*) CTX;
238         return Msg->nhdr != 0;
239 }
240
241 void examine_type(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
242 {
243         Msg->format_type = StrToi(HdrLine);
244                         
245 }
246
247 void examine_from(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
248 {
249         FreeStrBuf(&Msg->from);
250         Msg->from = NewStrBufPlain(NULL, StrLength(HdrLine));
251         StrBuf_RFC822_to_Utf8(Msg->from, HdrLine, WC->DefaultCharset, FoundCharset);
252 }
253 void tmplput_MAIL_SUMM_FROM(StrBuf *Target, WCTemplputParams *TP)
254 {
255         message_summary *Msg = (message_summary*) CTX;
256         StrBufAppendTemplate(Target, TP, Msg->from, 0);
257 }
258
259
260
261 void examine_subj(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
262 {
263         FreeStrBuf(&Msg->subj);
264         Msg->subj = NewStrBufPlain(NULL, StrLength(HdrLine));
265         StrBuf_RFC822_to_Utf8(Msg->subj, HdrLine, WC->DefaultCharset, FoundCharset);
266 }
267 void tmplput_MAIL_SUMM_SUBJECT(StrBuf *Target, WCTemplputParams *TP)
268 {/*////TODO: Fwd: and RE: filter!!*/
269
270         message_summary *Msg = (message_summary*) CTX;
271         StrBufAppendTemplate(Target, TP, Msg->subj, 0);
272 }
273 int Conditional_MAIL_SUMM_SUBJECT(StrBuf *Target, WCTemplputParams *TP)
274 {
275         message_summary *Msg = (message_summary*) CTX;
276         return StrLength(Msg->subj) > 0;
277 }
278
279
280 void examine_msgn(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
281 {
282         FreeStrBuf(&Msg->reply_inreplyto);
283         Msg->reply_inreplyto = NewStrBufPlain(NULL, StrLength(HdrLine));
284         StrBuf_RFC822_to_Utf8(Msg->reply_inreplyto, HdrLine, WC->DefaultCharset, FoundCharset);
285 }
286 void tmplput_MAIL_SUMM_INREPLYTO(StrBuf *Target, WCTemplputParams *TP)
287 {
288         message_summary *Msg = (message_summary*) CTX;
289         StrBufAppendTemplate(Target, TP, Msg->reply_inreplyto, 0);
290 }
291
292 int Conditional_MAIL_SUMM_UNREAD(StrBuf *Target, WCTemplputParams *TP)
293 {
294         message_summary *Msg = (message_summary*) CTX;
295         return Msg->is_new != 0;
296 }
297
298 void examine_wefw(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
299 {
300         FreeStrBuf(&Msg->reply_references);
301         Msg->reply_references = NewStrBufPlain(NULL, StrLength(HdrLine));
302         StrBuf_RFC822_to_Utf8(Msg->reply_references, HdrLine, WC->DefaultCharset, FoundCharset);
303 }
304 void tmplput_MAIL_SUMM_REFIDS(StrBuf *Target, WCTemplputParams *TP)
305 {
306         message_summary *Msg = (message_summary*) CTX;
307         StrBufAppendTemplate(Target, TP, Msg->reply_references, 0);
308 }
309
310
311 void examine_cccc(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
312 {
313         FreeStrBuf(&Msg->cccc);
314         Msg->cccc = NewStrBufPlain(NULL, StrLength(HdrLine));
315         StrBuf_RFC822_to_Utf8(Msg->cccc, HdrLine, WC->DefaultCharset, FoundCharset);
316         if (Msg->AllRcpt == NULL)
317                 Msg->AllRcpt = NewStrBufPlain(NULL, StrLength(HdrLine));
318         if (StrLength(Msg->AllRcpt) > 0) {
319                 StrBufAppendBufPlain(Msg->AllRcpt, HKEY(", "), 0);
320         }
321         StrBufAppendBuf(Msg->AllRcpt, Msg->cccc, 0);
322 }
323 void tmplput_MAIL_SUMM_CCCC(StrBuf *Target, WCTemplputParams *TP)
324 {
325         message_summary *Msg = (message_summary*) CTX;
326         StrBufAppendTemplate(Target, TP, Msg->cccc, 0);
327 }
328
329
330 void examine_room(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
331 {
332         if ((StrLength(HdrLine) > 0) &&
333             (strcasecmp(ChrPtr(HdrLine), ChrPtr(WC->wc_roomname)))) {
334                 FreeStrBuf(&Msg->Room);
335                 Msg->Room = NewStrBufDup(HdrLine);              
336         }
337 }
338 void tmplput_MAIL_SUMM_ORGROOM(StrBuf *Target, WCTemplputParams *TP)
339 {
340         message_summary *Msg = (message_summary*) CTX;
341         StrBufAppendTemplate(Target, TP, Msg->Room, 0);
342 }
343
344
345 void examine_rfca(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
346 {
347         FreeStrBuf(&Msg->Rfca);
348         Msg->Rfca = NewStrBufDup(HdrLine);
349 }
350 void tmplput_MAIL_SUMM_RFCA(StrBuf *Target, WCTemplputParams *TP)
351 {
352         message_summary *Msg = (message_summary*) CTX;
353         StrBufAppendTemplate(Target, TP, Msg->Rfca, 0);
354 }
355 int Conditional_MAIL_SUMM_RFCA(StrBuf *Target, WCTemplputParams *TP)
356 {
357         message_summary *Msg = (message_summary*) CTX;
358         return StrLength(Msg->Rfca) > 0;
359 }
360 int Conditional_MAIL_SUMM_CCCC(StrBuf *Target, WCTemplputParams *TP)
361 {
362         message_summary *Msg = (message_summary*) CTX;
363         return StrLength(Msg->cccc) > 0;
364 }
365
366 void examine_node(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
367 {
368         wcsession *WCC = WC;
369
370         if ( (StrLength(HdrLine) > 0) &&
371              ((WC->room_flags & QR_NETWORK)
372               || ((strcasecmp(ChrPtr(HdrLine), ChrPtr(WCC->serv_info->serv_nodename))
373                    && (strcasecmp(ChrPtr(HdrLine), ChrPtr(WCC->serv_info->serv_fqdn))))))) {
374                 FreeStrBuf(&Msg->OtherNode);
375                 Msg->OtherNode = NewStrBufDup(HdrLine);
376         }
377 }
378 void tmplput_MAIL_SUMM_OTHERNODE(StrBuf *Target, WCTemplputParams *TP)
379 {
380         message_summary *Msg = (message_summary*) CTX;
381         StrBufAppendTemplate(Target, TP, Msg->OtherNode, 0);
382 }
383 int Conditional_MAIL_SUMM_OTHERNODE(StrBuf *Target, WCTemplputParams *TP)
384 {
385         message_summary *Msg = (message_summary*) CTX;
386         return StrLength(Msg->OtherNode) > 0;
387 }
388
389
390 void examine_rcpt(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
391 {
392         FreeStrBuf(&Msg->to);
393         Msg->to = NewStrBufPlain(NULL, StrLength(HdrLine));
394         StrBuf_RFC822_to_Utf8(Msg->to, HdrLine, WC->DefaultCharset, FoundCharset);
395         if (Msg->AllRcpt == NULL)
396                 Msg->AllRcpt = NewStrBufPlain(NULL, StrLength(HdrLine));
397         if (StrLength(Msg->AllRcpt) > 0) {
398                 StrBufAppendBufPlain(Msg->AllRcpt, HKEY(", "), 0);
399         }
400         StrBufAppendBuf(Msg->AllRcpt, Msg->to, 0);
401 }
402 void tmplput_MAIL_SUMM_TO(StrBuf *Target, WCTemplputParams *TP)
403 {
404         message_summary *Msg = (message_summary*) CTX;
405         StrBufAppendTemplate(Target, TP, Msg->to, 0);
406 }
407 int Conditional_MAIL_SUMM_TO(StrBuf *Target, WCTemplputParams *TP) 
408 {
409         message_summary *Msg = (message_summary*) CTX;
410         return StrLength(Msg->to) != 0;
411 }
412 int Conditional_MAIL_SUMM_SUBJ(StrBuf *Target, WCTemplputParams *TP) 
413 {
414         message_summary *Msg = (message_summary*) CTX;
415         return StrLength(Msg->subj) != 0;
416 }
417 void tmplput_MAIL_SUMM_ALLRCPT(StrBuf *Target, WCTemplputParams *TP)
418 {
419         message_summary *Msg = (message_summary*) CTX;
420         StrBufAppendTemplate(Target, TP, Msg->AllRcpt, 0);
421 }
422
423
424
425 void tmplput_SUMM_COUNT(StrBuf *Target, WCTemplputParams *TP)
426 {
427         StrBufAppendPrintf(Target, "%d", GetCount( WC->summ));
428 }
429
430 HashList *iterate_get_mailsumm_All(StrBuf *Target, WCTemplputParams *TP)
431 {
432         return WC->summ;
433 }
434 void examine_time(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
435 {
436         Msg->date = StrTol(HdrLine);
437 }
438
439 void tmplput_MAIL_SUMM_DATE_BRIEF(StrBuf *Target, WCTemplputParams *TP)
440 {
441         char datebuf[64];
442         message_summary *Msg = (message_summary*) CTX;
443         webcit_fmt_date(datebuf, 64, Msg->date, DATEFMT_BRIEF);
444         StrBufAppendBufPlain(Target, datebuf, -1, 0);
445 }
446
447 void tmplput_MAIL_SUMM_DATE_FULL(StrBuf *Target, WCTemplputParams *TP)
448 {
449         char datebuf[64];
450         message_summary *Msg = (message_summary*) CTX;
451         webcit_fmt_date(datebuf, 64, Msg->date, DATEFMT_FULL);
452         StrBufAppendBufPlain(Target, datebuf, -1, 0);
453 }
454 void tmplput_MAIL_SUMM_DATE_NO(StrBuf *Target, WCTemplputParams *TP)
455 {
456         message_summary *Msg = (message_summary*) CTX;
457         StrBufAppendPrintf(Target, "%ld", Msg->date, 0);
458 }
459
460
461
462 void render_MAIL(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
463 {
464         const StrBuf *TemplateMime;
465
466         Mime->Data = NewStrBufPlain(NULL, Mime->length);
467         read_message(Mime->Data, HKEY("view_submessage"), Mime->msgnum, Mime->PartNum, &TemplateMime);
468 /*
469         if ( (!IsEmptyStr(mime_submessages)) && (!section[0]) ) {
470                 for (i=0; i<num_tokens(mime_submessages, '|'); ++i) {
471                         extract_token(buf, mime_submessages, i, '|', sizeof buf);
472                         / ** use printable_view to suppress buttons * /
473                         wprintf("<blockquote>");
474                         read_message(Mime->msgnum, 1, ChrPtr(Mime->Section));
475                         wprintf("</blockquote>");
476                 }
477         }
478 */
479 }
480
481 void render_MIME_VCard(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
482 {
483         wcsession *WCC = WC;
484         MimeLoadData(Mime);
485         if (StrLength(Mime->Data) > 0) {
486                 StrBuf *Buf;
487                 Buf = NewStrBuf();
488                 /** If it's my vCard I can edit it */
489                 if (    (!strcasecmp(ChrPtr(WCC->wc_roomname), USERCONFIGROOM))
490                         || (!strcasecmp(&(ChrPtr(WCC->wc_roomname)[11]), USERCONFIGROOM))
491                         || (WC->wc_view == VIEW_ADDRESSBOOK)
492                         ) {
493                         StrBufAppendPrintf(Buf, "<a href=\"edit_vcard?msgnum=%ld&partnum=%s\">",
494                                 Mime->msgnum, ChrPtr(Mime->PartNum));
495                         StrBufAppendPrintf(Buf, "[%s]</a>", _("edit"));
496                 }
497
498                 /* In all cases, display the full card */
499                 display_vcard(Buf, Mime->Data, 0, 1, NULL, Mime->msgnum);
500                 FreeStrBuf(&Mime->Data);
501                 Mime->Data = Buf;
502         }
503
504 }
505
506 void render_MIME_VNote(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
507 {
508         MimeLoadData(Mime);
509         if (StrLength(Mime->Data) > 0) {
510                 struct vnote *v;
511                 StrBuf *Buf;
512
513                 Buf = NewStrBuf();
514                 v = vnote_new_from_str(ChrPtr(Mime->Data));
515                 if (v) {
516                         WCTemplputParams TP;
517                         
518                         memset(&TP, 0, sizeof(WCTemplputParams));
519                         TP.Filter.ContextType = CTX_VNOTE;
520                         TP.Context = v;
521                         DoTemplate(HKEY("mail_vnoteitem"),
522                                    Buf, &TP);
523                         
524                         vnote_free(v);
525                         
526                         FreeStrBuf(&Mime->Data);
527                         Mime->Data = Buf;
528                 }
529                 else
530                         FlushStrBuf(Mime->Data);
531         }
532
533 }
534
535 void render_MIME_ICS(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
536 {
537         if (StrLength(Mime->Data) == 0) {
538                 MimeLoadData(Mime);
539         }
540         if (StrLength(Mime->Data) > 0) {
541                 cal_process_attachment(Mime);
542         }
543 }
544
545
546
547 void examine_mime_part(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
548 {
549         wc_mime_attachment *Mime;
550         StrBuf *Buf;
551         
552         Mime = (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
553         memset(Mime, 0, sizeof(wc_mime_attachment));
554         Mime->msgnum = Msg->msgnum;
555         Buf = NewStrBuf();
556
557         Mime->Name = NewStrBuf();
558         StrBufExtract_token(Buf, HdrLine, 0, '|');
559         StrBuf_RFC822_to_Utf8(Mime->Name, Buf, WC->DefaultCharset, FoundCharset);
560         StrBufTrim(Mime->Name);
561
562         StrBufExtract_token(Buf, HdrLine, 1, '|');
563         Mime->FileName = NewStrBuf();
564         StrBuf_RFC822_to_Utf8(Mime->FileName, Buf, WC->DefaultCharset, FoundCharset);
565         StrBufTrim(Mime->FileName);
566
567         Mime->PartNum = NewStrBuf();
568         StrBufExtract_token(Mime->PartNum, HdrLine, 2, '|');
569         StrBufTrim(Mime->PartNum);
570         if (strchr(ChrPtr(Mime->PartNum), '.') != NULL) 
571                 Mime->level = 2;
572         else
573                 Mime->level = 1;
574
575         Mime->Disposition = NewStrBuf();
576         StrBufExtract_token(Mime->Disposition, HdrLine, 3, '|');
577
578         Mime->ContentType = NewStrBuf();
579         StrBufExtract_token(Mime->ContentType, HdrLine, 4, '|');
580         StrBufTrim(Mime->ContentType);
581         StrBufLowerCase(Mime->ContentType);
582
583         if (!strcmp(ChrPtr(Mime->ContentType), "application/octet-stream")) {
584                 StrBufPlain(Mime->ContentType, 
585                             GuessMimeByFilename(SKEY(Mime->FileName)), -1);
586         }
587         Mime->length = StrBufExtract_int(HdrLine, 5, '|');
588
589         if ( (StrLength(Mime->FileName) == 0) && (StrLength(Mime->Name) > 0) ) {
590                 StrBufAppendBuf(Mime->FileName, Mime->Name, 0);
591         }
592
593         if (StrLength(Msg->PartNum) > 0) {
594                 StrBuf *tmp;
595                 StrBufPrintf(Buf, "%s.%s", ChrPtr(Msg->PartNum), ChrPtr(Mime->PartNum));
596                 tmp = Mime->PartNum;
597                 Mime->PartNum = Buf;
598                 Buf = tmp;
599         }
600
601         if (Msg->AllAttach == NULL)
602                 Msg->AllAttach = NewHash(1,NULL);
603         Put(Msg->AllAttach, SKEY(Mime->PartNum), Mime, DestroyMime);
604         FreeStrBuf(&Buf);
605 }
606
607
608 void evaluate_mime_part(message_summary *Msg, wc_mime_attachment *Mime)
609 {
610         void *vMimeRenderer;
611
612         /* just print the root-node */
613         if ((Mime->level == 1) &&
614             GetHash(MimeRenderHandler, SKEY(Mime->ContentType), &vMimeRenderer) &&
615             vMimeRenderer != NULL)
616         {
617                 Mime->Renderer = (RenderMimeFuncStruct*) vMimeRenderer;
618                 if (Msg->Submessages == NULL)
619                         Msg->Submessages = NewHash(1,NULL);
620                 Put(Msg->Submessages, SKEY(Mime->PartNum), Mime, reference_free_handler);
621         }
622         else if ((Mime->level == 1) &&
623                  (!strcasecmp(ChrPtr(Mime->Disposition), "inline"))
624                  && (!strncasecmp(ChrPtr(Mime->ContentType), "image/", 6)) ){
625                 if (Msg->AttachLinks == NULL)
626                         Msg->AttachLinks = NewHash(1,NULL);
627                 Put(Msg->AttachLinks, SKEY(Mime->PartNum), Mime, reference_free_handler);
628         }
629         else if ((Mime->level == 1) &&
630                  (StrLength(Mime->ContentType) > 0) &&
631                   ( (!strcasecmp(ChrPtr(Mime->Disposition), "attachment")) 
632                     || (!strcasecmp(ChrPtr(Mime->Disposition), "inline"))
633                     || (!strcasecmp(ChrPtr(Mime->Disposition), ""))))
634         {               
635                 if (Msg->AttachLinks == NULL)
636                         Msg->AttachLinks = NewHash(1,NULL);
637                 Put(Msg->AttachLinks, SKEY(Mime->PartNum), Mime, reference_free_handler);
638                 if ((strcasecmp(ChrPtr(Mime->ContentType), "application/octet-stream") == 0) && 
639                     (StrLength(Mime->FileName) > 0)) {
640                         FlushStrBuf(Mime->ContentType);
641                         StrBufAppendBufPlain(Mime->ContentType,
642                                              GuessMimeByFilename(SKEY(Mime->FileName)),
643                                              -1, 0);
644                 }
645         }
646 }
647
648 void tmplput_MAIL_SUMM_NATTACH(StrBuf *Target, WCTemplputParams *TP)
649 {
650         message_summary *Msg = (message_summary*) CTX;
651         StrBufAppendPrintf(Target, "%ld", GetCount(Msg->Attachments));
652 }
653
654
655 void examine_hnod(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
656 {
657         FreeStrBuf(&Msg->hnod);
658         Msg->hnod = NewStrBufPlain(NULL, StrLength(HdrLine));
659         StrBuf_RFC822_to_Utf8(Msg->hnod, HdrLine, WC->DefaultCharset, FoundCharset);
660 }
661 void tmplput_MAIL_SUMM_H_NODE(StrBuf *Target, WCTemplputParams *TP)
662 {
663         message_summary *Msg = (message_summary*) CTX;
664         StrBufAppendTemplate(Target, TP, Msg->hnod, 0);
665 }
666 int Conditional_MAIL_SUMM_H_NODE(StrBuf *Target, WCTemplputParams *TP)
667 {
668         message_summary *Msg = (message_summary*) CTX;
669         return StrLength(Msg->hnod) > 0;
670 }
671
672
673
674 void examine_text(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
675 {
676         Msg->MsgBody->Data = NewStrBufPlain(NULL, SIZ);
677 }
678
679 void examine_msg4_partnum(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
680 {
681         Msg->MsgBody->PartNum = NewStrBufDup(HdrLine);
682         StrBufTrim(Msg->MsgBody->PartNum);
683 }
684
685 void examine_content_lengh(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
686 {
687         Msg->MsgBody->length = StrTol(HdrLine);
688         Msg->MsgBody->size_known = 1;
689 }
690
691 void examine_content_type(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
692 {
693         void *vHdr;
694         headereval *Hdr;
695         StrBuf *Token;
696         StrBuf *Value;
697         const char* sem;
698         const char *eq;
699         int len;
700         StrBufTrim(HdrLine);
701         Msg->MsgBody->ContentType = NewStrBufDup(HdrLine);
702         sem = strchr(ChrPtr(HdrLine), ';');
703
704         if (sem != NULL) {
705                 Token = NewStrBufPlain(NULL, StrLength(HdrLine));
706                 Value = NewStrBufPlain(NULL, StrLength(HdrLine));
707                 len = sem - ChrPtr(HdrLine);
708                 StrBufCutAt(Msg->MsgBody->ContentType, len, NULL);
709                 while (sem != NULL) {
710                         while (isspace(*(sem + 1)))
711                                 sem ++;
712                         StrBufCutLeft(HdrLine, sem - ChrPtr(HdrLine));
713                         sem = strchr(ChrPtr(HdrLine), ';');
714                         if (sem != NULL)
715                                 len = sem - ChrPtr(HdrLine);
716                         else
717                                 len = StrLength(HdrLine);
718                         FlushStrBuf(Token);
719                         FlushStrBuf(Value);
720                         StrBufAppendBufPlain(Token, ChrPtr(HdrLine), len, 0);
721                         eq = strchr(ChrPtr(Token), '=');
722                         if (eq != NULL) {
723                                 len = eq - ChrPtr(Token);
724                                 StrBufAppendBufPlain(Value, eq + 1, StrLength(Token) - len - 1, 0); 
725                                 StrBufCutAt(Token, len, NULL);
726                                 StrBufTrim(Value);
727                         }
728                         StrBufTrim(Token);
729
730                         if (GetHash(MsgHeaderHandler, SKEY(Token), &vHdr) &&
731                             (vHdr != NULL)) {
732                                 Hdr = (headereval*)vHdr;
733                                 Hdr->evaluator(Msg, Value, FoundCharset);
734                         }
735                         else lprintf(1, "don't know how to handle content type sub-header[%s]\n", ChrPtr(Token));
736                 }
737                 FreeStrBuf(&Token);
738                 FreeStrBuf(&Value);
739         }
740 }
741
742 void tmplput_MAIL_SUMM_N(StrBuf *Target, WCTemplputParams *TP)
743 {
744         message_summary *Msg = (message_summary*) CTX;
745         StrBufAppendPrintf(Target, "%ld", Msg->msgnum);
746 }
747
748
749
750 int Conditional_MAIL_MIME_ALL(StrBuf *Target, WCTemplputParams *TP)
751 {
752         message_summary *Msg = (message_summary*) CTX;
753         return GetCount(Msg->Attachments) > 0;
754 }
755
756 int Conditional_MAIL_MIME_SUBMESSAGES(StrBuf *Target, WCTemplputParams *TP)
757 {
758         message_summary *Msg = (message_summary*) CTX;
759         return GetCount(Msg->Submessages) > 0;
760 }
761
762 int Conditional_MAIL_MIME_ATTACHLINKS(StrBuf *Target, WCTemplputParams *TP)
763 {
764         message_summary *Msg = (message_summary*) CTX;
765         return GetCount(Msg->AttachLinks) > 0;
766 }
767
768 int Conditional_MAIL_MIME_ATTACH(StrBuf *Target, WCTemplputParams *TP)
769 {
770         message_summary *Msg = (message_summary*) CTX;
771         return GetCount(Msg->AllAttach) > 0;
772 }
773
774 void tmplput_QUOTED_MAIL_BODY(StrBuf *Target, WCTemplputParams *TP)
775 {
776         const StrBuf *Mime;
777         long MsgNum;
778         StrBuf *Buf;
779
780         MsgNum = LBstr(TKEY(0));
781         Buf = NewStrBuf();
782         read_message(Buf, HKEY("view_message_replyquote"), MsgNum, NULL, &Mime);
783         StrBufAppendTemplate(Target, TP, Buf, 1);
784         FreeStrBuf(&Buf);
785 }
786
787 void tmplput_EDIT_MAIL_BODY(StrBuf *Target, WCTemplputParams *TP)
788 {
789         const StrBuf *Mime;
790         long MsgNum;
791         StrBuf *Buf;
792
793         MsgNum = LBstr(TKEY(0));
794         Buf = NewStrBuf();
795         read_message(Buf, HKEY("view_message_edit"), MsgNum, NULL, &Mime);
796         StrBufAppendTemplate(Target, TP, Buf, 1);
797         FreeStrBuf(&Buf);
798 }
799
800 void tmplput_MAIL_BODY(StrBuf *Target, WCTemplputParams *TP)
801 {
802         message_summary *Msg = (message_summary*) CTX;
803         StrBufAppendTemplate(Target, TP, Msg->MsgBody->Data, 0);
804 }
805
806
807 void render_MAIL_variformat(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
808 {
809         /* Messages in legacy Citadel variformat get handled thusly... */
810         StrBuf *Target = NewStrBufPlain(NULL, StrLength(Mime->Data));
811         FmOut(Target, "JUSTIFY", Mime->Data);
812         FreeStrBuf(&Mime->Data);
813         Mime->Data = Target;
814 }
815
816 void render_MAIL_text_plain(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
817 {
818         StrBuf *cs = NULL;
819         const char *ptr, *pte;
820         const char *BufPtr = NULL;
821         StrBuf *Line;
822         StrBuf *Line1;
823         StrBuf *Line2;
824         StrBuf *Target;
825
826         int ConvertIt = 1;
827         int bn = 0;
828         int bq = 0;
829         int i, n, done = 0;
830         long len;
831 #ifdef HAVE_ICONV
832         iconv_t ic = (iconv_t)(-1) ;
833 #endif
834
835         if ((StrLength(Mime->Data) == 0) && (Mime->length > 0)) {
836                 FreeStrBuf(&Mime->Data);
837                 MimeLoadData(Mime);
838         }
839
840         /* Boring old 80-column fixed format text gets handled this way... */
841         if ((strcasecmp(ChrPtr(Mime->Charset), "us-ascii") == 0) &&
842             (strcasecmp(ChrPtr(Mime->Charset), "UTF-8") == 0))
843                 ConvertIt = 0;
844
845 #ifdef HAVE_ICONV
846         if (ConvertIt) {
847                 if (StrLength(Mime->Charset) != 0)
848                         cs = Mime->Charset;
849                 else if (StrLength(FoundCharset) > 0)
850                         cs = FoundCharset;
851                 else if (StrLength(WC->DefaultCharset) > 0)
852                         cs = WC->DefaultCharset;
853                 if (cs == NULL) {
854                         ConvertIt = 0;
855                 }
856                 else {
857                         ctdl_iconv_open("UTF-8", ChrPtr(cs), &ic);
858                         if (ic == (iconv_t)(-1) ) {
859                                 lprintf(5, "%s:%d iconv_open(UTF-8, %s) failed: %s\n",
860                                         __FILE__, __LINE__, ChrPtr(Mime->Charset), strerror(errno));
861                         }
862                 }
863         }
864 #endif
865         Line = NewStrBufPlain(NULL, SIZ);
866         Line1 = NewStrBufPlain(NULL, SIZ);
867         Line2 = NewStrBufPlain(NULL, SIZ);
868         Target = NewStrBufPlain(NULL, StrLength(Mime->Data));
869
870         while ((n = StrBufSipLine(Line, Mime->Data, &BufPtr), n >= 0) && !done)
871         {
872                 done = n == 0;
873                 bq = 0;
874                 i = 0;
875                 ptr = ChrPtr(Line);
876                 len = StrLength(Line);
877                 pte = ptr + len;
878                 
879                 while ((ptr < pte) &&
880                        ((*ptr == '>') ||
881                         isspace(*ptr)))
882                 {
883                         if (*ptr == '>')
884                                 bq++;
885                         ptr ++;
886                         i++;
887                 }
888                 if (i > 0) StrBufCutLeft(Line, i);
889                 
890                 if (StrLength(Line) == 0) {
891                         StrBufAppendBufPlain(Target, HKEY("<tt></tt><br />\n"), 0);
892                         continue;
893                 }
894
895                 for (i = bn; i < bq; i++)                               
896                         StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
897                 for (i = bq; i < bn; i++)                               
898                         StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
899
900                 if (ConvertIt == 1) {
901                         StrBufConvert(Line, Line1, &ic);
902                 }
903
904                 StrBufAppendBufPlain(Target, HKEY("<tt>"), 0);
905                 UrlizeText(Line1, Line, Line2);
906
907                 StrEscAppend(Target, Line1, NULL, 0, 0);
908                 StrBufAppendBufPlain(Target, HKEY("</tt><br />\n"), 0);
909                 bn = bq;
910         }
911
912         for (i = 0; i < bn; i++)                                
913                 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
914
915         StrBufAppendBufPlain(Target, HKEY("</i><br />"), 0);
916 #ifdef HAVE_ICONV
917         if (ic != (iconv_t)(-1) ) {
918                 iconv_close(ic);
919         }
920 #endif
921
922         FreeStrBuf(&Mime->Data);
923         Mime->Data = Target;
924         FlushStrBuf(Mime->ContentType);
925         StrBufAppendBufPlain(Mime->ContentType, HKEY("text/html"), 0);
926         FlushStrBuf(Mime->Charset);
927         StrBufAppendBufPlain(Mime->Charset, HKEY("UTF-8"), 0);
928         FreeStrBuf(&Line);
929         FreeStrBuf(&Line1);
930         FreeStrBuf(&Line2);
931 }
932
933 void render_MAIL_html(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
934 {
935         StrBuf *Buf;
936
937         if (StrLength(Mime->Data) == 0)
938                 return;
939
940         Buf = NewStrBufPlain(NULL, StrLength(Mime->Data));
941
942         /* HTML is fun, but we've got to strip it first */
943         output_html(ChrPtr(Mime->Charset), 
944                     (WC->wc_view == VIEW_WIKI ? 1 : 0), 
945                     Mime->msgnum,
946                     Mime->Data, Buf);
947         FreeStrBuf(&Mime->Data);
948         Mime->Data = Buf;
949 }
950
951 void render_MAIL_UNKNOWN(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
952 {
953         /* Unknown weirdness */
954         FlushStrBuf(Mime->Data);
955         StrBufAppendBufPlain(Mime->Data, _("I don't know how to display "), -1, 0);
956         StrBufAppendBuf(Mime->Data, Mime->ContentType, 0);
957         StrBufAppendBufPlain(Mime->Data, HKEY("<br />\n"), 0);
958 }
959
960
961 HashList *iterate_get_mime_All(StrBuf *Target, WCTemplputParams *TP)
962 {
963         message_summary *Msg = (message_summary*) CTX;
964         return Msg->Attachments;
965 }
966 HashList *iterate_get_mime_Submessages(StrBuf *Target, WCTemplputParams *TP)
967 {
968         message_summary *Msg = (message_summary*) CTX;
969         return Msg->Submessages;
970 }
971 HashList *iterate_get_mime_AttachLinks(StrBuf *Target, WCTemplputParams *TP)
972 {
973         message_summary *Msg = (message_summary*) CTX;
974         return Msg->AttachLinks;
975 }
976 HashList *iterate_get_mime_Attachments(StrBuf *Target, WCTemplputParams *TP)
977 {
978         message_summary *Msg = (message_summary*) CTX;
979         return Msg->AllAttach;
980 }
981
982 void tmplput_MIME_Name(StrBuf *Target, WCTemplputParams *TP)
983 {
984         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
985         StrBufAppendTemplate(Target, TP, mime->Name, 0);
986 }
987
988 void tmplput_MIME_FileName(StrBuf *Target, WCTemplputParams *TP)
989 {
990         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
991         StrBufAppendTemplate(Target, TP, mime->FileName, 0);
992 }
993
994 void tmplput_MIME_PartNum(StrBuf *Target, WCTemplputParams *TP)
995 {
996         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
997         StrBufAppendTemplate(Target, TP, mime->PartNum, 0);
998 }
999
1000 void tmplput_MIME_MsgNum(StrBuf *Target, WCTemplputParams *TP)
1001 {
1002         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
1003         StrBufAppendPrintf(Target, "%ld", mime->msgnum);
1004 }
1005
1006 void tmplput_MIME_Disposition(StrBuf *Target, WCTemplputParams *TP)
1007 {
1008         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
1009         StrBufAppendTemplate(Target, TP, mime->Disposition, 0);
1010 }
1011
1012 void tmplput_MIME_ContentType(StrBuf *Target, WCTemplputParams *TP)
1013 {
1014         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
1015         StrBufAppendTemplate(Target, TP, mime->ContentType, 0);
1016 }
1017
1018 void examine_charset(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
1019 {
1020         Msg->MsgBody->Charset = NewStrBufDup(HdrLine);
1021 }
1022
1023 void tmplput_MIME_Charset(StrBuf *Target, WCTemplputParams *TP)
1024 {
1025         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
1026         StrBufAppendTemplate(Target, TP, mime->Charset, 0);
1027 }
1028
1029 void tmplput_MIME_Data(StrBuf *Target, WCTemplputParams *TP)
1030 {
1031         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
1032         if (mime->Renderer != NULL)
1033                 mime->Renderer->f(mime, NULL, NULL);
1034         StrBufAppendTemplate(Target, TP, mime->Data, 0);
1035         /* TODO: check whether we need to load it now? */
1036 }
1037
1038 void tmplput_MIME_LoadData(StrBuf *Target, WCTemplputParams *TP)
1039 {
1040         wcsession *WCC = WC;    
1041         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
1042         wc_mime_attachment *att;
1043         
1044         if ( (!strcasecmp(ChrPtr(mime->Disposition), "inline"))||
1045              (!strcasecmp(ChrPtr(mime->Disposition), "attachment")) ) 
1046         {
1047                 
1048                 int n;
1049                 char N[64];
1050                 /* steal this mime part... */
1051                 att = malloc(sizeof(wc_mime_attachment));
1052                 memcpy(att, mime, sizeof(wc_mime_attachment));
1053                 memset(mime, 0, sizeof(wc_mime_attachment));
1054
1055                 if (att->Data == NULL) 
1056                         MimeLoadData(att);
1057
1058                 if (WCC->attachments == NULL)
1059                         WCC->attachments = NewHash(1, NULL);
1060                 /* And add it to the list. */
1061                 n = snprintf(N, sizeof N, "%d", GetCount(WCC->attachments) + 1);
1062                 Put(WCC->attachments, N, n, att, DestroyMime);
1063         }
1064 }
1065
1066 void tmplput_MIME_Length(StrBuf *Target, WCTemplputParams *TP)
1067 {
1068         wc_mime_attachment *mime = (wc_mime_attachment*) CTX;
1069         StrBufAppendPrintf(Target, "%ld", mime->length);
1070 }
1071
1072 HashList *iterate_get_registered_Attachments(StrBuf *Target, WCTemplputParams *TP)
1073 {
1074         return WC->attachments;
1075 }
1076
1077 void servcmd_do_search(char *buf, long bufsize)
1078 {
1079         snprintf(buf, bufsize, "MSGS SEARCH|%s", bstr("query"));
1080 }
1081
1082 void servcmd_headers(char *buf, long bufsize)
1083 {
1084         snprintf(buf, bufsize, "MSGS ALL");
1085 }
1086
1087 void servcmd_readfwd(char *buf, long bufsize)
1088 {
1089         snprintf(buf, bufsize, "MSGS ALL");
1090 }
1091
1092 void servcmd_readnew(char *buf, long bufsize)
1093 {
1094         snprintf(buf, bufsize, "MSGS NEW");
1095 }
1096
1097 void servcmd_readold(char *buf, long bufsize)
1098 {
1099         snprintf(buf, bufsize, "MSGS OLD");
1100 }
1101
1102
1103 readloop_struct rlid[] = {
1104         { {HKEY("do_search")}, servcmd_do_search},
1105         { {HKEY("headers")},   servcmd_headers},
1106         { {HKEY("readfwd")},   servcmd_readfwd},
1107         { {HKEY("readnew")},   servcmd_readnew},
1108         { {HKEY("readold")},   servcmd_readold}
1109 };
1110
1111
1112
1113 void SetAccessCommand(long Oper)
1114 {
1115 /* TODO: whats achieved by this?
1116         wcsession *WCC = WC;    
1117
1118         if (WCC->UrlFragment1 != NULL ) {
1119                 FlushStrBuf(WCC->UrlFragment1);
1120                 StrBufAppendBufPlain(WCC->UrlFragment1, 
1121                                      rlid[Oper].name.Key, rlid[Oper].name.len, 0);
1122         }
1123         else 
1124                 WCC->UrlFragment1 = NewStrBufPlain(rlid[Oper].name.Key, rlid[Oper].name.len);
1125 */
1126 }
1127                 
1128
1129
1130
1131
1132 void 
1133 InitModule_MSGRENDERERS
1134 (void)
1135 {
1136         RegisterSortFunc(HKEY("date"), 
1137                          NULL, 0,
1138                          summcmp_date,
1139                          summcmp_rdate,
1140                          groupchange_date,
1141                          CTX_MAILSUM);
1142         RegisterSortFunc(HKEY("subject"), 
1143                          NULL, 0,
1144                          summcmp_subj,
1145                          summcmp_rsubj,
1146                          groupchange_subj,
1147                          CTX_MAILSUM);
1148         RegisterSortFunc(HKEY("sender"),
1149                          NULL, 0,
1150                          summcmp_sender,
1151                          summcmp_rsender,
1152                          groupchange_sender,
1153                          CTX_MAILSUM);
1154
1155         RegisterNamespace("SUMM:COUNT", 0, 0, tmplput_SUMM_COUNT, CTX_NONE);
1156         /* iterate over all known mails in WC->summ */
1157         RegisterIterator("MAIL:SUMM:MSGS", 0, NULL, iterate_get_mailsumm_All,
1158                          NULL,NULL, CTX_MAILSUM, CTX_NONE, IT_NOFLAG);
1159
1160         RegisterNamespace("MAIL:SUMM:DATEBRIEF", 0, 0, tmplput_MAIL_SUMM_DATE_BRIEF, CTX_MAILSUM);
1161         RegisterNamespace("MAIL:SUMM:DATEFULL", 0, 0, tmplput_MAIL_SUMM_DATE_FULL, CTX_MAILSUM);
1162         RegisterNamespace("MAIL:SUMM:DATENO",  0, 0, tmplput_MAIL_SUMM_DATE_NO,  CTX_MAILSUM);
1163         RegisterNamespace("MAIL:SUMM:N",       0, 0, tmplput_MAIL_SUMM_N,        CTX_MAILSUM);
1164         RegisterNamespace("MAIL:SUMM:FROM",    0, 2, tmplput_MAIL_SUMM_FROM,     CTX_MAILSUM);
1165         RegisterNamespace("MAIL:SUMM:TO",      0, 2, tmplput_MAIL_SUMM_TO,       CTX_MAILSUM);
1166         RegisterNamespace("MAIL:SUMM:SUBJECT", 0, 4, tmplput_MAIL_SUMM_SUBJECT,  CTX_MAILSUM);
1167         RegisterNamespace("MAIL:SUMM:NTATACH", 0, 0, tmplput_MAIL_SUMM_NATTACH,  CTX_MAILSUM);
1168         RegisterNamespace("MAIL:SUMM:CCCC", 0, 2, tmplput_MAIL_SUMM_CCCC,  CTX_MAILSUM);
1169         RegisterNamespace("MAIL:SUMM:H_NODE", 0, 2, tmplput_MAIL_SUMM_H_NODE,  CTX_MAILSUM);
1170         RegisterNamespace("MAIL:SUMM:ALLRCPT", 0, 2, tmplput_MAIL_SUMM_ALLRCPT,  CTX_MAILSUM);
1171         RegisterNamespace("MAIL:SUMM:ORGROOM", 0, 2, tmplput_MAIL_SUMM_ORGROOM,  CTX_MAILSUM);
1172         RegisterNamespace("MAIL:SUMM:RFCA", 0, 2, tmplput_MAIL_SUMM_RFCA,  CTX_MAILSUM);
1173         RegisterNamespace("MAIL:SUMM:OTHERNODE", 2, 0, tmplput_MAIL_SUMM_OTHERNODE,  CTX_MAILSUM);
1174         RegisterNamespace("MAIL:SUMM:REFIDS", 0, 1, tmplput_MAIL_SUMM_REFIDS,  CTX_MAILSUM);
1175         RegisterNamespace("MAIL:SUMM:INREPLYTO", 0, 2, tmplput_MAIL_SUMM_INREPLYTO,  CTX_MAILSUM);
1176         RegisterNamespace("MAIL:BODY", 0, 2, tmplput_MAIL_BODY,  CTX_MAILSUM);
1177         RegisterNamespace("MAIL:QUOTETEXT", 1, 2, tmplput_QUOTED_MAIL_BODY,  CTX_NONE);
1178         RegisterNamespace("MAIL:EDITTEXT", 1, 2, tmplput_EDIT_MAIL_BODY,  CTX_NONE);
1179         RegisterConditional(HKEY("COND:MAIL:SUMM:RFCA"), 0, Conditional_MAIL_SUMM_RFCA,  CTX_MAILSUM);
1180         RegisterConditional(HKEY("COND:MAIL:SUMM:CCCC"), 0, Conditional_MAIL_SUMM_CCCC,  CTX_MAILSUM);
1181         RegisterConditional(HKEY("COND:MAIL:SUMM:UNREAD"), 0, Conditional_MAIL_SUMM_UNREAD, CTX_MAILSUM);
1182         RegisterConditional(HKEY("COND:MAIL:SUMM:H_NODE"), 0, Conditional_MAIL_SUMM_H_NODE, CTX_MAILSUM);
1183         RegisterConditional(HKEY("COND:MAIL:SUMM:OTHERNODE"), 0, Conditional_MAIL_SUMM_OTHERNODE, CTX_MAILSUM);
1184         RegisterConditional(HKEY("COND:MAIL:SUMM:SUBJECT"), 0, Conditional_MAIL_SUMM_SUBJECT, CTX_MAILSUM);
1185         RegisterConditional(HKEY("COND:MAIL:ANON"), 0, Conditional_ANONYMOUS_MESSAGE, CTX_MAILSUM);
1186         RegisterConditional(HKEY("COND:MAIL:TO"), 0, Conditional_MAIL_SUMM_TO, CTX_MAILSUM);    
1187         RegisterConditional(HKEY("COND:MAIL:SUBJ"), 0, Conditional_MAIL_SUMM_SUBJ, CTX_MAILSUM);        
1188
1189         /* do we have mimetypes to iterate over? */
1190         RegisterConditional(HKEY("COND:MAIL:MIME:ATTACH"), 0, Conditional_MAIL_MIME_ALL, CTX_MAILSUM);
1191         RegisterConditional(HKEY("COND:MAIL:MIME:ATTACH:SUBMESSAGES"), 0, Conditional_MAIL_MIME_SUBMESSAGES, CTX_MAILSUM);
1192         RegisterConditional(HKEY("COND:MAIL:MIME:ATTACH:LINKS"), 0, Conditional_MAIL_MIME_ATTACHLINKS, CTX_MAILSUM);
1193         RegisterConditional(HKEY("COND:MAIL:MIME:ATTACH:ATT"), 0, Conditional_MAIL_MIME_ATTACH, CTX_MAILSUM);
1194         RegisterIterator("MAIL:MIME:ATTACH", 0, NULL, iterate_get_mime_All, 
1195                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM, IT_NOFLAG);
1196         RegisterIterator("MAIL:MIME:ATTACH:SUBMESSAGES", 0, NULL, iterate_get_mime_Submessages, 
1197                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM, IT_NOFLAG);
1198         RegisterIterator("MAIL:MIME:ATTACH:LINKS", 0, NULL, iterate_get_mime_AttachLinks, 
1199                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM, IT_NOFLAG);
1200         RegisterIterator("MAIL:MIME:ATTACH:ATT", 0, NULL, iterate_get_mime_Attachments, 
1201                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM, IT_NOFLAG);
1202
1203         /* Parts of a mime attachent */
1204         RegisterNamespace("MAIL:MIME:NAME", 0, 2, tmplput_MIME_Name, CTX_MIME_ATACH);
1205         RegisterNamespace("MAIL:MIME:FILENAME", 0, 2, tmplput_MIME_FileName, CTX_MIME_ATACH);
1206         RegisterNamespace("MAIL:MIME:PARTNUM", 0, 2, tmplput_MIME_PartNum, CTX_MIME_ATACH);
1207         RegisterNamespace("MAIL:MIME:MSGNUM", 0, 2, tmplput_MIME_MsgNum, CTX_MIME_ATACH);
1208         RegisterNamespace("MAIL:MIME:DISPOSITION", 0, 2, tmplput_MIME_Disposition, CTX_MIME_ATACH);
1209         RegisterNamespace("MAIL:MIME:CONTENTTYPE", 0, 2, tmplput_MIME_ContentType, CTX_MIME_ATACH);
1210         RegisterNamespace("MAIL:MIME:CHARSET", 0, 2, tmplput_MIME_Charset, CTX_MIME_ATACH);
1211         RegisterNamespace("MAIL:MIME:LENGTH", 0, 2, tmplput_MIME_Length, CTX_MIME_ATACH);
1212         RegisterNamespace("MAIL:MIME:DATA", 0, 2, tmplput_MIME_Data, CTX_MIME_ATACH);
1213         /* load the actual attachment into WC->attachments; no output!!! */
1214         RegisterNamespace("MAIL:MIME:LOADDATA", 0, 0, tmplput_MIME_LoadData, CTX_MIME_ATACH);
1215
1216         /* iterate the WC->attachments; use the above tokens for their contents */
1217         RegisterIterator("MSG:ATTACHNAMES", 0, NULL, iterate_get_registered_Attachments, 
1218                          NULL, NULL, CTX_MIME_ATACH, CTX_NONE, IT_NOFLAG);
1219
1220         /* mime renderers translate an attachment into webcit viewable html text */
1221         RegisterMimeRenderer(HKEY("message/rfc822"), render_MAIL);
1222         RegisterMimeRenderer(HKEY("text/vnote"), render_MIME_VNote);
1223         RegisterMimeRenderer(HKEY("text/x-vcard"), render_MIME_VCard);
1224         RegisterMimeRenderer(HKEY("text/vcard"), render_MIME_VCard);
1225         RegisterMimeRenderer(HKEY("text/calendar"), render_MIME_ICS);
1226         RegisterMimeRenderer(HKEY("application/ics"), render_MIME_ICS);
1227         RegisterMimeRenderer(HKEY("text/x-citadel-variformat"), render_MAIL_variformat);
1228         RegisterMimeRenderer(HKEY("text/plain"), render_MAIL_text_plain);
1229         RegisterMimeRenderer(HKEY("text"), render_MAIL_text_plain);
1230         RegisterMimeRenderer(HKEY("text/html"), render_MAIL_html);
1231         RegisterMimeRenderer(HKEY(""), render_MAIL_UNKNOWN);
1232
1233         /* these headers are citserver replies to MSG4 and friends. one evaluator for each */
1234         RegisterMsgHdr(HKEY("nhdr"), examine_nhdr, 0);
1235         RegisterMsgHdr(HKEY("type"), examine_type, 0);
1236         RegisterMsgHdr(HKEY("from"), examine_from, 0);
1237         RegisterMsgHdr(HKEY("subj"), examine_subj, 0);
1238         RegisterMsgHdr(HKEY("msgn"), examine_msgn, 0);
1239         RegisterMsgHdr(HKEY("wefw"), examine_wefw, 0);
1240         RegisterMsgHdr(HKEY("cccc"), examine_cccc, 0);
1241         RegisterMsgHdr(HKEY("hnod"), examine_hnod, 0);
1242         RegisterMsgHdr(HKEY("room"), examine_room, 0);
1243         RegisterMsgHdr(HKEY("rfca"), examine_rfca, 0);
1244         RegisterMsgHdr(HKEY("node"), examine_node, 0);
1245         RegisterMsgHdr(HKEY("rcpt"), examine_rcpt, 0);
1246         RegisterMsgHdr(HKEY("time"), examine_time, 0);
1247         RegisterMsgHdr(HKEY("part"), examine_mime_part, 0);
1248         RegisterMsgHdr(HKEY("text"), examine_text, 1);
1249         /* these are the content-type headers we get infront of a message; put it into the same hash since it doesn't clash. */
1250         RegisterMsgHdr(HKEY("X-Citadel-MSG4-Partnum"), examine_msg4_partnum, 0);
1251         RegisterMsgHdr(HKEY("Content-type"), examine_content_type, 0);
1252         RegisterMsgHdr(HKEY("Content-length"), examine_content_lengh, 0);
1253         RegisterMsgHdr(HKEY("Content-transfer-encoding"), examine_content_encoding, 0); /* do we care? */
1254         RegisterMsgHdr(HKEY("charset"), examine_charset, 0);
1255
1256         /* Don't care about these... */
1257         RegisterMsgHdr(HKEY("pref"), examine_pref, 0);
1258         RegisterMsgHdr(HKEY("suff"), examine_suff, 0);
1259         RegisterMsgHdr(HKEY("path"), examine_path, 0);
1260 }
1261
1262 void 
1263 ServerStartModule_MSGRENDERERS
1264 (void)
1265 {
1266         MsgHeaderHandler = NewHash(1, NULL);
1267         MimeRenderHandler = NewHash(1, NULL);
1268 }
1269
1270 void 
1271 ServerShutdownModule_MSGRENDERERS
1272 (void)
1273 {
1274         DeleteHash(&MsgHeaderHandler);
1275         DeleteHash(&MimeRenderHandler);
1276 }
1277
1278
1279
1280 void 
1281 SessionDestroyModule_MSGRENDERERS
1282 (wcsession *sess)
1283 {
1284         DeleteHash(&sess->attachments);
1285 }