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