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