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