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