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