6b81166f9f12937bf96cf937a10414a13f8b8241
[citadel.git] / webcit / msg_renderers.c
1 /*----------------------------------------------------------------------------*/
2 #include "webcit.h"
3 #include "webserver.h"
4
5 /**
6  * message index functions
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         Put(MimeRenderHandler, HeaderName, HdrNLen, MimeRenderer, reference_free_handler);
66         
67 }
68
69 /*----------------------------------------------------------------------------*/
70
71
72 void examine_nhdr(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
73 {
74         Msg->nhdr = 0;
75         if (!strncasecmp(ChrPtr(HdrLine), "yes", 8))
76                 Msg->nhdr = 1;
77 }
78 int Conditional_ANONYMOUS_MESSAGE(WCTemplateToken *Tokens, void *Context, int ContextType)
79 {
80         message_summary *Msg = (message_summary*) Context;
81         return Msg->nhdr != 0;
82 }
83
84
85 void examine_type(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
86 {
87         Msg->format_type = StrToi(HdrLine);
88                         
89 }
90
91 void examine_from(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
92 {
93         FreeStrBuf(&Msg->from);
94         Msg->from = NewStrBufPlain(NULL, StrLength(HdrLine));
95         StrBuf_RFC822_to_Utf8(Msg->from, HdrLine, WC->DefaultCharset, FoundCharset);
96 }
97 void tmplput_MAIL_SUMM_FROM(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
98 {
99         message_summary *Msg = (message_summary*) Context;
100         StrBufAppendBuf(Target, Msg->from, 0);
101 }
102
103
104
105 void examine_subj(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
106 {
107         FreeStrBuf(&Msg->subj);
108         Msg->subj = NewStrBufPlain(NULL, StrLength(HdrLine));
109         StrBuf_RFC822_to_Utf8(Msg->subj, HdrLine, WC->DefaultCharset, FoundCharset);
110 }
111 void tmplput_MAIL_SUMM_SUBJECT(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
112 {/////TODO: Fwd: and RE: filter!!
113         message_summary *Msg = (message_summary*) Context;
114         StrBufAppendBuf(Target, Msg->subj, 0);
115 }
116
117
118 void examine_msgn(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
119 {
120         FreeStrBuf(&Msg->reply_inreplyto);
121         Msg->reply_inreplyto = NewStrBufPlain(NULL, StrLength(HdrLine));
122         StrBuf_RFC822_to_Utf8(Msg->reply_inreplyto, HdrLine, WC->DefaultCharset, FoundCharset);
123 }
124 void tmplput_MAIL_SUMM_INREPLYTO(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
125 {
126         message_summary *Msg = (message_summary*) Context;
127         StrBufAppendBuf(Target, Msg->reply_inreplyto, 0);
128 }
129
130 int Conditional_MAIL_SUMM_UNREAD(WCTemplateToken *Tokens, void *Context, int ContextType)
131 {
132         message_summary *Msg = (message_summary*) Context;
133         return Msg->is_new != 0;
134 }
135
136 void examine_wefw(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
137 {
138         FreeStrBuf(&Msg->reply_references);
139         Msg->reply_references = NewStrBufPlain(NULL, StrLength(HdrLine));
140         StrBuf_RFC822_to_Utf8(Msg->reply_references, HdrLine, WC->DefaultCharset, FoundCharset);
141 }
142 void tmplput_MAIL_SUMM_REFIDS(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
143 {
144         message_summary *Msg = (message_summary*) Context;
145         StrBufAppendBuf(Target, Msg->reply_references, 0);
146 }
147
148
149 void examine_cccc(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
150 {
151         FreeStrBuf(&Msg->cccc);
152         Msg->cccc = NewStrBufPlain(NULL, StrLength(HdrLine));
153         StrBuf_RFC822_to_Utf8(Msg->cccc, HdrLine, WC->DefaultCharset, FoundCharset);
154         if (Msg->AllRcpt == NULL)
155                 Msg->AllRcpt = NewStrBufPlain(NULL, StrLength(HdrLine));
156         if (StrLength(Msg->AllRcpt) > 0) {
157                 StrBufAppendBufPlain(Msg->AllRcpt, HKEY(", "), 0);
158         }
159         StrBufAppendBuf(Msg->AllRcpt, Msg->cccc, 0);
160 }
161 void tmplput_MAIL_SUMM_CCCC(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
162 {
163         message_summary *Msg = (message_summary*) Context;
164         StrBufAppendBuf(Target, Msg->cccc, 0);
165 }
166
167
168
169
170 void examine_room(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
171 {
172         if ((StrLength(HdrLine) > 0) &&
173             (strcasecmp(ChrPtr(HdrLine), WC->wc_roomname))) {
174                 FreeStrBuf(&Msg->Room);
175                 Msg->Room = NewStrBufDup(HdrLine);              
176         }
177 }
178 void tmplput_MAIL_SUMM_ORGROOM(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
179 {
180         message_summary *Msg = (message_summary*) Context;
181         StrBufAppendBuf(Target, Msg->Room, 0);
182 }
183
184
185 void examine_rfca(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
186 {
187         FreeStrBuf(&Msg->Rfca);
188         Msg->Rfca = NewStrBufDup(HdrLine);
189 }
190 void tmplput_MAIL_SUMM_RFCA(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
191 {
192         message_summary *Msg = (message_summary*) Context;
193         StrBufAppendBuf(Target, Msg->Rfca, 0);
194 }
195
196
197 void examine_node(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
198 {
199         if ( (StrLength(HdrLine) > 0) &&
200              ((WC->room_flags & QR_NETWORK)
201               || ((strcasecmp(ChrPtr(HdrLine), serv_info.serv_nodename)
202                    && (strcasecmp(ChrPtr(HdrLine), serv_info.serv_fqdn)))))) {
203                 FreeStrBuf(&Msg->OtherNode);
204                 Msg->OtherNode = NewStrBufDup(HdrLine);
205         }
206 }
207 void tmplput_MAIL_SUMM_OTHERNODE(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
208 {
209         message_summary *Msg = (message_summary*) Context;
210         StrBufAppendBuf(Target, Msg->OtherNode, 0);
211 }
212 int Conditional_MAIL_SUMM_OTHERNODE(WCTemplateToken *Tokens, void *Context, int ContextType)
213 {
214         message_summary *Msg = (message_summary*) Context;
215         return StrLength(Msg->OtherNode) > 0;
216 }
217
218
219 void examine_rcpt(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
220 {
221         FreeStrBuf(&Msg->to);
222         Msg->to = NewStrBufPlain(NULL, StrLength(HdrLine));
223         StrBuf_RFC822_to_Utf8(Msg->to, HdrLine, WC->DefaultCharset, FoundCharset);
224         if (Msg->AllRcpt == NULL)
225                 Msg->AllRcpt = NewStrBufPlain(NULL, StrLength(HdrLine));
226         if (StrLength(Msg->AllRcpt) > 0) {
227                 StrBufAppendBufPlain(Msg->AllRcpt, HKEY(", "), 0);
228         }
229         StrBufAppendBuf(Msg->AllRcpt, Msg->to, 0);
230 }
231 void tmplput_MAIL_SUMM_TO(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
232 {
233         message_summary *Msg = (message_summary*) Context;
234         StrBufAppendBuf(Target, Msg->to, 0);
235 }
236 void tmplput_MAIL_SUMM_ALLRCPT(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
237 {
238         message_summary *Msg = (message_summary*) Context;
239         StrBufAppendBuf(Target, Msg->AllRcpt, 0);
240 }
241
242
243
244 void examine_time(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
245 {
246         Msg->date = StrTol(HdrLine);
247 }
248 void tmplput_MAIL_SUMM_DATE_STR(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
249 {
250         char datebuf[64];
251         message_summary *Msg = (message_summary*) Context;
252         webcit_fmt_date(datebuf, Msg->date, 1);
253         StrBufAppendBufPlain(Target, datebuf, -1, 0);
254 }
255 void tmplput_MAIL_SUMM_DATE_NO(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
256 {
257         message_summary *Msg = (message_summary*) Context;
258         StrBufAppendPrintf(Target, "%ld", Msg->date, 0);
259 }
260
261
262
263 void render_MAIL(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
264 {
265         Mime->Data = NewStrBufPlain(NULL, Mime->length);
266         read_message(Mime->Data, HKEY("view_submessage"), Mime->msgnum, 0, Mime->PartNum);
267 /*
268         if ( (!IsEmptyStr(mime_submessages)) && (!section[0]) ) {
269                 for (i=0; i<num_tokens(mime_submessages, '|'); ++i) {
270                         extract_token(buf, mime_submessages, i, '|', sizeof buf);
271                         /** use printable_view to suppress buttons * /
272                         wprintf("<blockquote>");
273                         read_message(Mime->msgnum, 1, ChrPtr(Mime->Section));
274                         wprintf("</blockquote>");
275                 }
276         }
277 */
278 }
279
280 void render_MIME_VCard(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
281 {
282         MimeLoadData(Mime);
283         if (StrLength(Mime->Data) > 0) {
284                 StrBuf *Buf;
285                 Buf = NewStrBuf();
286                 /** If it's my vCard I can edit it */
287                 if (    (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
288                         || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
289                         || (WC->wc_view == VIEW_ADDRESSBOOK)
290                         ) {
291                         StrBufAppendPrintf(Buf, "<a href=\"edit_vcard?msgnum=%ld&partnum=%s\">",
292                                 Mime->msgnum, ChrPtr(Mime->PartNum));
293                         StrBufAppendPrintf(Buf, "[%s]</a>", _("edit"));
294                 }
295
296                 /* In all cases, display the full card */
297                 display_vcard(Buf, ChrPtr(Mime->Data), 0, 1, NULL, Mime->msgnum);
298                 FreeStrBuf(&Mime->Data);
299                 Mime->Data = Buf;
300         }
301
302 }
303 void render_MIME_ICS(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
304 {
305         MimeLoadData(Mime);
306         if (StrLength(Mime->Data) > 0) {
307                 cal_process_attachment(Mime);
308         }
309 }
310
311
312
313 void examine_mime_part(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
314 {
315         wc_mime_attachment *Mime;
316         StrBuf *Buf;
317         
318         Mime = (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
319         memset(Mime, 0, sizeof(wc_mime_attachment));
320         Mime->msgnum = Msg->msgnum;
321         Buf = NewStrBuf();
322
323         Mime->Name = NewStrBuf();
324         StrBufExtract_token(Mime->Name, HdrLine, 0, '|');
325         StrBufTrim(Mime->Name);
326
327         StrBufExtract_token(Buf, HdrLine, 1, '|');
328         Mime->FileName = NewStrBuf();
329         StrBuf_RFC822_to_Utf8(Mime->FileName, Buf, WC->DefaultCharset, FoundCharset);
330         StrBufTrim(Mime->FileName);
331
332         Mime->PartNum = NewStrBuf();
333         StrBufExtract_token(Mime->PartNum, HdrLine, 2, '|');
334         StrBufTrim(Mime->PartNum);
335         if (strchr(ChrPtr(Mime->PartNum), '.') != NULL)
336                 Mime->level = 2;
337         else
338                 Mime->level = 1;
339
340         Mime->Disposition = NewStrBuf();
341         StrBufExtract_token(Mime->Disposition, HdrLine, 3, '|');
342
343         Mime->ContentType = NewStrBuf();
344         StrBufExtract_token(Mime->ContentType, HdrLine, 4, '|');
345         StrBufTrim(Mime->ContentType);
346         StrBufLowerCase(Mime->ContentType);
347
348         Mime->length = StrBufExtract_int(HdrLine, 5, '|');
349
350         if ( (StrLength(Mime->FileName) == 0) && (StrLength(Mime->Name) > 0) ) {
351                 StrBufAppendBuf(Mime->FileName, Mime->Name, 0);
352         }
353
354         if (StrLength(Msg->PartNum) > 0) {
355                 StrBuf *tmp;
356                 StrBufPrintf(Buf, "%s.%s", ChrPtr(Msg->PartNum), ChrPtr(Mime->PartNum));
357                 tmp = Mime->PartNum;
358                 Mime->PartNum = Buf;
359                 Buf = tmp;
360         }
361
362         if (Msg->AllAttach == NULL)
363                 Msg->AllAttach = NewHash(1,NULL);
364         Put(Msg->AllAttach, SKEY(Mime->PartNum), Mime, DestroyMime);
365         FreeStrBuf(&Buf);
366 }
367
368
369 void evaluate_mime_part(message_summary *Msg, wc_mime_attachment *Mime)
370 {
371         void *vMimeRenderer;
372
373         /* just print the root-node */
374         if ((Mime->level == 1) &&
375             GetHash(MimeRenderHandler, SKEY(Mime->ContentType), &vMimeRenderer) &&
376             vMimeRenderer != NULL)
377         {
378                 Mime->Renderer = (RenderMimeFunc) vMimeRenderer;
379                 if (Msg->Submessages == NULL)
380                         Msg->Submessages = NewHash(1,NULL);
381                 Put(Msg->Submessages, SKEY(Mime->PartNum), Mime, reference_free_handler);
382         }
383         else if ((Mime->level == 1) &&
384                  (!strcasecmp(ChrPtr(Mime->Disposition), "inline"))
385                  && (!strncasecmp(ChrPtr(Mime->ContentType), "image/", 6)) ){
386                 if (Msg->AttachLinks == NULL)
387                         Msg->AttachLinks = NewHash(1,NULL);
388                 Put(Msg->AttachLinks, SKEY(Mime->PartNum), Mime, reference_free_handler);
389         }
390         else if ((Mime->level == 1) &&
391                  (StrLength(Mime->ContentType) > 0) &&
392                   ( (!strcasecmp(ChrPtr(Mime->Disposition), "attachment")) 
393                     || (!strcasecmp(ChrPtr(Mime->Disposition), "inline"))
394                     || (!strcasecmp(ChrPtr(Mime->Disposition), ""))))
395         {               
396                 if (Msg->AttachLinks == NULL)
397                         Msg->AttachLinks = NewHash(1,NULL);
398                 Put(Msg->AttachLinks, SKEY(Mime->PartNum), Mime, reference_free_handler);
399                 if (strcasecmp(ChrPtr(Mime->ContentType), "application/octet-stream") == 0) {
400                         FlushStrBuf(Mime->ContentType);
401                         StrBufAppendBufPlain(Mime->ContentType,
402                                              GuessMimeByFilename(SKEY(Mime->FileName)),
403                                              -1, 0);
404                 }
405         }
406 }
407
408 void tmplput_MAIL_SUMM_NATTACH(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
409 {
410         message_summary *Msg = (message_summary*) Context;
411         StrBufAppendPrintf(Target, "%ld", GetCount(Msg->Attachments));
412 }
413
414
415
416
417
418
419
420 void examine_hnod(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
421 {
422         FreeStrBuf(&Msg->hnod);
423         Msg->hnod = NewStrBufPlain(NULL, StrLength(HdrLine));
424         StrBuf_RFC822_to_Utf8(Msg->hnod, HdrLine, WC->DefaultCharset, FoundCharset);
425 }
426 void tmplput_MAIL_SUMM_H_NODE(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
427 {
428         message_summary *Msg = (message_summary*) Context;
429         StrBufAppendBuf(Target, Msg->hnod, 0);
430 }
431 int Conditional_MAIL_SUMM_H_NODE(WCTemplateToken *Tokens, void *Context, int ContextType)
432 {
433         message_summary *Msg = (message_summary*) Context;
434         return StrLength(Msg->hnod) > 0;
435 }
436
437
438
439 void examine_text(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
440 {
441         Msg->MsgBody->Data = NewStrBuf();
442 }
443
444 void examine_msg4_partnum(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
445 {
446         Msg->MsgBody->PartNum = NewStrBufDup(HdrLine);
447         StrBufTrim(Msg->MsgBody->PartNum);/////TODO: striplt == trim?
448 }
449
450 void examine_content_encoding(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
451 {
452 ////TODO: do we care?
453 }
454
455 void examine_content_lengh(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
456 {
457         Msg->MsgBody->length = StrTol(HdrLine);
458         Msg->MsgBody->size_known = 1;
459 }
460
461 void examine_content_type(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
462 {////TODO
463         int len, i;
464         Msg->MsgBody->ContentType = NewStrBufDup(HdrLine);
465         StrBufTrim(Msg->MsgBody->ContentType);/////todo==striplt?
466         len = StrLength(Msg->MsgBody->ContentType);
467         for (i=0; i<len; ++i) {
468                 if (!strncasecmp(ChrPtr(Msg->MsgBody->ContentType) + i, "charset=", 8)) {/// TODO: WHUT?
469 //                      safestrncpy(mime_charset, &mime_content_type[i+8],
470                         ///                         sizeof mime_charset);
471                 }
472         }/****
473         for (i=0; i<len; ++i) {
474                 if (mime_content_type[i] == ';') {
475                         mime_content_type[i] = 0;
476                         len = i - 1;
477                 }
478         }
479         len = strlen(mime_charset);
480         for (i=0; i<len; ++i) {
481                 if (mime_charset[i] == ';') {
482                         mime_charset[i] = 0;
483                         len = i - 1;
484                 }
485         }
486          */
487 }
488
489 void tmplput_MAIL_SUMM_N(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
490 {
491         message_summary *Msg = (message_summary*) Context;
492         StrBufAppendPrintf(Target, "%ld", Msg->msgnum);
493 }
494
495
496
497 int Conditional_MAIL_MIME_ALL(WCTemplateToken *Tokens, void *Context, int ContextType)
498 {
499         message_summary *Msg = (message_summary*) Context;
500         return GetCount(Msg->Attachments) > 0;
501 }
502
503 int Conditional_MAIL_MIME_SUBMESSAGES(WCTemplateToken *Tokens, void *Context, int ContextType)
504 {
505         message_summary *Msg = (message_summary*) Context;
506         return GetCount(Msg->Submessages) > 0;
507 }
508
509 int Conditional_MAIL_MIME_ATTACHLINKS(WCTemplateToken *Tokens, void *Context, int ContextType)
510 {
511         message_summary *Msg = (message_summary*) Context;
512         return GetCount(Msg->AttachLinks) > 0;
513 }
514
515 int Conditional_MAIL_MIME_ATTACH(WCTemplateToken *Tokens, void *Context, int ContextType)
516 {
517         message_summary *Msg = (message_summary*) Context;
518         return GetCount(Msg->AllAttach) > 0;
519 }
520
521
522
523 /*----------------------------------------------------------------------------*/
524 void tmplput_QUOTED_MAIL_BODY(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
525 {
526         long MsgNum;
527         MsgNum = LBstr(Token->Params[0]->Start, Token->Params[0]->len);
528         read_message(Target, HKEY("view_message_replyquote"), MsgNum, 0, NULL);
529 }
530
531 void tmplput_MAIL_BODY(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
532 {
533         message_summary *Msg = (message_summary*) Context;
534         StrBufAppendBuf(Target, Msg->MsgBody->Data, 0);
535 }
536
537
538 void render_MAIL_variformat(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
539 {
540         /* Messages in legacy Citadel variformat get handled thusly... */
541         StrBuf *Target = NewStrBufPlain(NULL, StrLength(Mime->Data));
542         FmOut(Target, "JUSTIFY", Mime->Data);
543         FreeStrBuf(&Mime->Data);
544         Mime->Data = Target;
545 }
546
547 void render_MAIL_text_plain(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
548 {
549         StrBuf *cs = NULL;
550         const char *ptr, *pte;
551         const char *BufPtr = NULL;
552         StrBuf *Line = NewStrBuf();
553         StrBuf *Line1 = NewStrBuf();
554         StrBuf *Line2 = NewStrBuf();
555         StrBuf *Target = NewStrBufPlain(NULL, StrLength(Mime->Data));
556         int ConvertIt = 1;
557         int bn = 0;
558         int bq = 0;
559         int i, n, done = 0;
560         long len;
561 #ifdef HAVE_ICONV
562         iconv_t ic = (iconv_t)(-1) ;
563 #endif
564
565         if ((StrLength(Mime->Data) == 0) && (Mime->length > 0)) {
566                 FreeStrBuf(&Mime->Data);
567                 Mime->Data = NewStrBufPlain(NULL, Mime->length);
568                 if (!read_message(Mime->Data, HKEY("view_submessage"), Mime->msgnum, 0, Mime->PartNum))
569                         return;
570         }
571
572         /* Boring old 80-column fixed format text gets handled this way... */
573         if ((strcasecmp(ChrPtr(Mime->Charset), "us-ascii") == 0) &&
574             (strcasecmp(ChrPtr(Mime->Charset), "UTF-8") == 0))
575                 ConvertIt = 0;
576
577 #ifdef HAVE_ICONV
578         if (ConvertIt) {
579                 if (StrLength(Mime->Charset) != 0)
580                         cs = Mime->Charset;
581                 else if (StrLength(FoundCharset) > 0)
582                         cs = FoundCharset;
583                 else if (StrLength(WC->DefaultCharset) > 0)
584                         cs = WC->DefaultCharset;
585                 if (cs == 0) {
586                         ConvertIt = 0;
587                 }
588                 else {
589                         ctdl_iconv_open("UTF-8", ChrPtr(cs), &ic);
590                         if (ic == (iconv_t)(-1) ) {
591                                 lprintf(5, "%s:%d iconv_open(UTF-8, %s) failed: %s\n",
592                                         __FILE__, __LINE__, ChrPtr(Mime->Charset), strerror(errno));
593                         }
594                 }
595         }
596 #endif
597
598         while ((n = StrBufSipLine(Line, Mime->Data, &BufPtr), n >= 0) && !done)
599         {
600                 done = n == 0;
601                 bq = 0;
602                 i = 0;
603                 ptr = ChrPtr(Line);
604                 len = StrLength(Line);
605                 pte = ptr + len;
606                 
607                 while ((ptr < pte) &&
608                        ((*ptr == '>') ||
609                         isspace(*ptr)))
610                 {
611                         if (*ptr == '>')
612                                 bq++;
613                         ptr ++;
614                         i++;
615                 }
616                 if (i > 0) StrBufCutLeft(Line, i);
617                 
618                 if (StrLength(Line) == 0)
619                         continue;
620
621                 for (i = bn; i < bq; i++)                               
622                         StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
623                 for (i = bq; i < bn; i++)                               
624                         StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
625
626                 if (ConvertIt == 1) {
627                         StrBufConvert(Line, Line1, &ic);
628                 }
629
630                 StrBufAppendBufPlain(Target, HKEY("<tt>"), 0);
631                 UrlizeText(Line1, Line, Line2);
632
633                 StrEscAppend(Target, Line1, NULL, 0, 0);
634                 StrBufAppendBufPlain(Target, HKEY("</tt><br />\n"), 0);
635                 bn = bq;
636         }
637
638         for (i = 0; i < bn; i++)                                
639                 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
640
641         StrBufAppendBufPlain(Target, HKEY("</i><br />"), 0);
642 #ifdef HAVE_ICONV
643         if (ic != (iconv_t)(-1) ) {
644                 iconv_close(ic);
645         }
646 #endif
647         FreeStrBuf(&Mime->Data);
648         Mime->Data = Target;
649         FreeStrBuf(&Line);
650         FreeStrBuf(&Line1);
651         FreeStrBuf(&Line2);
652 }
653
654 void render_MAIL_html(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
655 {
656         StrBuf *Buf;
657         /* HTML is fun, but we've got to strip it first */
658         if (StrLength(Mime->Data) == 0)
659                 return;
660
661         Buf = NewStrBufPlain(NULL, StrLength(Mime->Data));
662
663         output_html(ChrPtr(Mime->Charset), 
664                     (WC->wc_view == VIEW_WIKI ? 1 : 0), 
665                     StrToi(Mime->PartNum), 
666                     Mime->Data, Buf);
667         FreeStrBuf(&Mime->Data);
668         Mime->Data = Buf;
669 }
670
671 void render_MAIL_UNKNOWN(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
672 {
673         /* Unknown weirdness */
674         FlushStrBuf(Mime->Data);
675         StrBufAppendBufPlain(Mime->Data, _("I don't know how to display "), -1, 0);
676         StrBufAppendBuf(Mime->Data, Mime->ContentType, 0);
677         StrBufAppendBufPlain(Mime->Data, HKEY("<br />\n"), 0);
678 }
679
680
681
682
683
684
685 HashList *iterate_get_mime_All(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
686 {
687         message_summary *Msg = (message_summary*) Context;
688         return Msg->Attachments;
689 }
690 HashList *iterate_get_mime_Submessages(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
691 {
692         message_summary *Msg = (message_summary*) Context;
693         return Msg->Submessages;
694 }
695 HashList *iterate_get_mime_AttachLinks(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
696 {
697         message_summary *Msg = (message_summary*) Context;
698         return Msg->AttachLinks;
699 }
700 HashList *iterate_get_mime_Attachments(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
701 {
702         message_summary *Msg = (message_summary*) Context;
703         return Msg->AllAttach;
704 }
705
706 void tmplput_MIME_Name(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
707 {
708         wc_mime_attachment *mime = (wc_mime_attachment*) Context;
709         StrBufAppendBuf(Target, mime->Name, 0);
710 }
711
712 void tmplput_MIME_FileName(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
713 {
714         wc_mime_attachment *mime = (wc_mime_attachment*) Context;
715         StrBufAppendBuf(Target, mime->FileName, 0);
716 }
717
718 void tmplput_MIME_PartNum(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
719 {
720         wc_mime_attachment *mime = (wc_mime_attachment*) Context;
721         StrBufAppendBuf(Target, mime->PartNum, 0);
722 }
723
724 void tmplput_MIME_MsgNum(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
725 {
726         wc_mime_attachment *mime = (wc_mime_attachment*) Context;
727         StrBufAppendPrintf(Target, "%ld", mime->msgnum);
728 }
729
730 void tmplput_MIME_Disposition(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
731 {
732         wc_mime_attachment *mime = (wc_mime_attachment*) Context;
733         StrBufAppendBuf(Target, mime->Disposition, 0);
734 }
735
736 void tmplput_MIME_ContentType(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
737 {
738         wc_mime_attachment *mime = (wc_mime_attachment*) Context;
739         StrBufAppendBuf(Target, mime->ContentType, 0);
740 }
741
742 void tmplput_MIME_Charset(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
743 {
744         wc_mime_attachment *mime = (wc_mime_attachment*) Context;
745         StrBufAppendBuf(Target, mime->Charset, 0);
746 }
747
748 void tmplput_MIME_Data(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
749 {
750         wc_mime_attachment *mime = (wc_mime_attachment*) Context;
751         if (mime->Renderer != NULL)
752                 mime->Renderer(mime, NULL, NULL);
753         StrBufAppendBuf(Target, mime->Data, 0); /// TODO: check whether we need to load it now?
754 }
755
756 void tmplput_MIME_Length(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
757 {
758         wc_mime_attachment *mime = (wc_mime_attachment*) Context;
759         StrBufAppendPrintf(Target, "%ld", mime->length);
760 }
761
762 HashList *iterate_get_registered_Attachments(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
763 {
764         return WC->attachments;
765 }
766
767 void tmplput_ATT_Length(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
768 {
769         wc_attachment *att = (wc_attachment*) Context;
770         StrBufAppendPrintf(Target, "%ld", att->length);
771 }
772
773 void tmplput_ATT_Contenttype(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
774 {
775         wc_attachment *att = (wc_attachment*) Context;
776         StrBufAppendBuf(Target, att->content_type, 0);
777 }
778
779 void tmplput_ATT_FileName(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
780 {
781         wc_attachment *att = (wc_attachment*) Context;
782         StrBufAppendBuf(Target, att->filename, 0);
783 }
784
785
786
787
788
789
790 void 
791 InitModule_MSGRENDERERS
792 (void)
793 {
794         RegisterNamespace("MAIL:SUMM:DATESTR", 0, 0, tmplput_MAIL_SUMM_DATE_STR, CTX_MAILSUM);
795         RegisterNamespace("MAIL:SUMM:DATENO",  0, 0, tmplput_MAIL_SUMM_DATE_NO,  CTX_MAILSUM);
796         RegisterNamespace("MAIL:SUMM:N",       0, 0, tmplput_MAIL_SUMM_N,        CTX_MAILSUM);
797         RegisterNamespace("MAIL:SUMM:FROM",    0, 2, tmplput_MAIL_SUMM_FROM,     CTX_MAILSUM);
798         RegisterNamespace("MAIL:SUMM:TO",      0, 2, tmplput_MAIL_SUMM_TO,       CTX_MAILSUM);
799         RegisterNamespace("MAIL:SUMM:SUBJECT", 0, 4, tmplput_MAIL_SUMM_SUBJECT,  CTX_MAILSUM);
800         RegisterNamespace("MAIL:SUMM:NTATACH", 0, 0, tmplput_MAIL_SUMM_NATTACH,  CTX_MAILSUM);
801         RegisterNamespace("MAIL:SUMM:CCCC", 0, 2, tmplput_MAIL_SUMM_CCCC,  CTX_MAILSUM);
802         RegisterNamespace("MAIL:SUMM:H_NODE", 0, 2, tmplput_MAIL_SUMM_H_NODE,  CTX_MAILSUM);
803         RegisterNamespace("MAIL:SUMM:ALLRCPT", 0, 2, tmplput_MAIL_SUMM_ALLRCPT,  CTX_MAILSUM);
804         RegisterNamespace("MAIL:SUMM:ORGROOM", 0, 2, tmplput_MAIL_SUMM_ORGROOM,  CTX_MAILSUM);
805         RegisterNamespace("MAIL:SUMM:RFCA", 0, 2, tmplput_MAIL_SUMM_RFCA,  CTX_MAILSUM);
806         RegisterNamespace("MAIL:SUMM:OTHERNODE", 2, 0, tmplput_MAIL_SUMM_OTHERNODE,  CTX_MAILSUM);
807         RegisterNamespace("MAIL:SUMM:REFIDS", 0, 0, tmplput_MAIL_SUMM_REFIDS,  CTX_MAILSUM);
808         RegisterNamespace("MAIL:SUMM:INREPLYTO", 0, 2, tmplput_MAIL_SUMM_INREPLYTO,  CTX_MAILSUM);
809         RegisterNamespace("MAIL:BODY", 0, 2, tmplput_MAIL_BODY,  CTX_MAILSUM);
810         RegisterNamespace("MAIL:QUOTETEXT", 1, 2, tmplput_QUOTED_MAIL_BODY,  CTX_NONE);
811         RegisterNamespace("ATT:SIZE", 0, 1, tmplput_ATT_Length, CTX_ATT);
812         RegisterNamespace("ATT:TYPE", 0, 1, tmplput_ATT_Contenttype, CTX_ATT);
813         RegisterNamespace("ATT:FILENAME", 0, 1, tmplput_ATT_FileName, CTX_ATT);
814
815         RegisterConditional(HKEY("COND:MAIL:SUMM:UNREAD"), 0, Conditional_MAIL_SUMM_UNREAD, CTX_MAILSUM);
816         RegisterConditional(HKEY("COND:MAIL:SUMM:H_NODE"), 0, Conditional_MAIL_SUMM_H_NODE, CTX_MAILSUM);
817         RegisterConditional(HKEY("COND:MAIL:SUMM:OTHERNODE"), 0, Conditional_MAIL_SUMM_OTHERNODE, CTX_MAILSUM);
818         RegisterConditional(HKEY("COND:MAIL:ANON"), 0, Conditional_ANONYMOUS_MESSAGE, CTX_MAILSUM);
819
820         RegisterConditional(HKEY("COND:MAIL:MIME:ATTACH"), 0, Conditional_MAIL_MIME_ALL, CTX_MAILSUM);
821         RegisterConditional(HKEY("COND:MAIL:MIME:ATTACH:SUBMESSAGES"), 0, Conditional_MAIL_MIME_SUBMESSAGES, CTX_MAILSUM);
822         RegisterConditional(HKEY("COND:MAIL:MIME:ATTACH:LINKS"), 0, Conditional_MAIL_MIME_ATTACHLINKS, CTX_MAILSUM);
823         RegisterConditional(HKEY("COND:MAIL:MIME:ATTACH:ATT"), 0, Conditional_MAIL_MIME_ATTACH, CTX_MAILSUM);
824
825         RegisterIterator("MAIL:MIME:ATTACH", 0, NULL, iterate_get_mime_All, 
826                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM);
827         RegisterIterator("MAIL:MIME:ATTACH:SUBMESSAGES", 0, NULL, iterate_get_mime_Submessages, 
828                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM);
829         RegisterIterator("MAIL:MIME:ATTACH:LINKS", 0, NULL, iterate_get_mime_AttachLinks, 
830                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM);
831         RegisterIterator("MAIL:MIME:ATTACH:ATT", 0, NULL, iterate_get_mime_Attachments, 
832                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM);
833
834         RegisterNamespace("MAIL:MIME:NAME", 0, 2, tmplput_MIME_Name, CTX_MIME_ATACH);
835         RegisterNamespace("MAIL:MIME:FILENAME", 0, 2, tmplput_MIME_FileName, CTX_MIME_ATACH);
836         RegisterNamespace("MAIL:MIME:PARTNUM", 0, 2, tmplput_MIME_PartNum, CTX_MIME_ATACH);
837         RegisterNamespace("MAIL:MIME:MSGNUM", 0, 2, tmplput_MIME_MsgNum, CTX_MIME_ATACH);
838         RegisterNamespace("MAIL:MIME:DISPOSITION", 0, 2, tmplput_MIME_Disposition, CTX_MIME_ATACH);
839         RegisterNamespace("MAIL:MIME:CONTENTTYPE", 0, 2, tmplput_MIME_ContentType, CTX_MIME_ATACH);
840         RegisterNamespace("MAIL:MIME:CHARSET", 0, 2, tmplput_MIME_Charset, CTX_MIME_ATACH);
841         RegisterNamespace("MAIL:MIME:LENGTH", 0, 2, tmplput_MIME_Length, CTX_MIME_ATACH);
842         RegisterNamespace("MAIL:MIME:DATA", 0, 2, tmplput_MIME_Data, CTX_MIME_ATACH);
843
844         RegisterIterator("MSG:ATTACHNAMES", 0, NULL, iterate_get_registered_Attachments, 
845                          NULL, NULL, CTX_ATT, CTX_NONE);
846
847         RegisterMimeRenderer(HKEY("message/rfc822"), render_MAIL);
848         RegisterMimeRenderer(HKEY("text/x-vcard"), render_MIME_VCard);
849         RegisterMimeRenderer(HKEY("text/vcard"), render_MIME_VCard);
850         RegisterMimeRenderer(HKEY("text/calendar"), render_MIME_ICS);
851         RegisterMimeRenderer(HKEY("application/ics"), render_MIME_ICS);
852
853         RegisterMimeRenderer(HKEY("text/x-citadel-variformat"), render_MAIL_variformat);
854         RegisterMimeRenderer(HKEY("text/plain"), render_MAIL_text_plain);
855         RegisterMimeRenderer(HKEY("text"), render_MAIL_text_plain);
856         RegisterMimeRenderer(HKEY("text/html"), render_MAIL_html);
857         RegisterMimeRenderer(HKEY(""), render_MAIL_UNKNOWN);
858
859         RegisterMsgHdr(HKEY("nhdr"), examine_nhdr, 0);
860         RegisterMsgHdr(HKEY("type"), examine_type, 0);
861         RegisterMsgHdr(HKEY("from"), examine_from, 0);
862         RegisterMsgHdr(HKEY("subj"), examine_subj, 0);
863         RegisterMsgHdr(HKEY("msgn"), examine_msgn, 0);
864         RegisterMsgHdr(HKEY("wefw"), examine_wefw, 0);
865         RegisterMsgHdr(HKEY("cccc"), examine_cccc, 0);
866         RegisterMsgHdr(HKEY("hnod"), examine_hnod, 0);
867         RegisterMsgHdr(HKEY("room"), examine_room, 0);
868         RegisterMsgHdr(HKEY("rfca"), examine_rfca, 0);
869         RegisterMsgHdr(HKEY("node"), examine_node, 0);
870         RegisterMsgHdr(HKEY("rcpt"), examine_rcpt, 0);
871         RegisterMsgHdr(HKEY("time"), examine_time, 0);
872         RegisterMsgHdr(HKEY("part"), examine_mime_part, 0);
873         RegisterMsgHdr(HKEY("text"), examine_text, 1);
874         RegisterMsgHdr(HKEY("X-Citadel-MSG4-Partnum"), examine_msg4_partnum, 0);
875         RegisterMsgHdr(HKEY("Content-type"), examine_content_type, 0);
876         RegisterMsgHdr(HKEY("Content-length"), examine_content_lengh, 0);
877         RegisterMsgHdr(HKEY("Content-transfer-encoding"), examine_content_encoding, 0);
878 }