f294c5e3537ab894babe9b943775687e508eb587
[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 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 *Token, void *Context, int ContextType)
212 {
213         message_summary *Msg = (message_summary*) Context;
214         StrBufAppendBuf(Target, 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 *Token, void *Context, int ContextType)
236 {
237         message_summary *Msg = (message_summary*) Context;
238         StrBufAppendBuf(Target, Msg->to, 0);
239 }
240 void tmplput_MAIL_SUMM_ALLRCPT(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
241 {
242         message_summary *Msg = (message_summary*) Context;
243         StrBufAppendBuf(Target, 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 *Token, 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 *Token, 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(Mime->Name, HdrLine, 0, '|');
329         StrBufTrim(Mime->Name);
330
331         StrBufExtract_token(Buf, HdrLine, 1, '|');
332         Mime->FileName = NewStrBuf();
333         StrBuf_RFC822_to_Utf8(Mime->FileName, Buf, WC->DefaultCharset, FoundCharset);
334         StrBufTrim(Mime->FileName);
335
336         Mime->PartNum = NewStrBuf();
337         StrBufExtract_token(Mime->PartNum, HdrLine, 2, '|');
338         StrBufTrim(Mime->PartNum);
339         if (strchr(ChrPtr(Mime->PartNum), '.') != NULL)
340                 Mime->level = 2;
341         else
342                 Mime->level = 1;
343
344         Mime->Disposition = NewStrBuf();
345         StrBufExtract_token(Mime->Disposition, HdrLine, 3, '|');
346
347         Mime->ContentType = NewStrBuf();
348         StrBufExtract_token(Mime->ContentType, HdrLine, 4, '|');
349         StrBufTrim(Mime->ContentType);
350         StrBufLowerCase(Mime->ContentType);
351
352         Mime->length = StrBufExtract_int(HdrLine, 5, '|');
353
354         if ( (StrLength(Mime->FileName) == 0) && (StrLength(Mime->Name) > 0) ) {
355                 StrBufAppendBuf(Mime->FileName, Mime->Name, 0);
356         }
357
358         if (StrLength(Msg->PartNum) > 0) {
359                 StrBuf *tmp;
360                 StrBufPrintf(Buf, "%s.%s", ChrPtr(Msg->PartNum), ChrPtr(Mime->PartNum));
361                 tmp = Mime->PartNum;
362                 Mime->PartNum = Buf;
363                 Buf = tmp;
364         }
365
366         if (Msg->AllAttach == NULL)
367                 Msg->AllAttach = NewHash(1,NULL);
368         Put(Msg->AllAttach, SKEY(Mime->PartNum), Mime, DestroyMime);
369         FreeStrBuf(&Buf);
370 }
371
372
373 void evaluate_mime_part(message_summary *Msg, wc_mime_attachment *Mime)
374 {
375         void *vMimeRenderer;
376
377         /* just print the root-node */
378         if ((Mime->level == 1) &&
379             GetHash(MimeRenderHandler, SKEY(Mime->ContentType), &vMimeRenderer) &&
380             vMimeRenderer != NULL)
381         {
382                 Mime->Renderer = (RenderMimeFunc) vMimeRenderer;
383                 if (Msg->Submessages == NULL)
384                         Msg->Submessages = NewHash(1,NULL);
385                 Put(Msg->Submessages, SKEY(Mime->PartNum), Mime, reference_free_handler);
386         }
387         else if ((Mime->level == 1) &&
388                  (!strcasecmp(ChrPtr(Mime->Disposition), "inline"))
389                  && (!strncasecmp(ChrPtr(Mime->ContentType), "image/", 6)) ){
390                 if (Msg->AttachLinks == NULL)
391                         Msg->AttachLinks = NewHash(1,NULL);
392                 Put(Msg->AttachLinks, SKEY(Mime->PartNum), Mime, reference_free_handler);
393         }
394         else if ((Mime->level == 1) &&
395                  (StrLength(Mime->ContentType) > 0) &&
396                   ( (!strcasecmp(ChrPtr(Mime->Disposition), "attachment")) 
397                     || (!strcasecmp(ChrPtr(Mime->Disposition), "inline"))
398                     || (!strcasecmp(ChrPtr(Mime->Disposition), ""))))
399         {               
400                 if (Msg->AttachLinks == NULL)
401                         Msg->AttachLinks = NewHash(1,NULL);
402                 Put(Msg->AttachLinks, SKEY(Mime->PartNum), Mime, reference_free_handler);
403                 if (strcasecmp(ChrPtr(Mime->ContentType), "application/octet-stream") == 0) {
404                         FlushStrBuf(Mime->ContentType);
405                         StrBufAppendBufPlain(Mime->ContentType,
406                                              GuessMimeByFilename(SKEY(Mime->FileName)),
407                                              -1, 0);
408                 }
409         }
410 }
411
412 void tmplput_MAIL_SUMM_NATTACH(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
413 {
414         message_summary *Msg = (message_summary*) Context;
415         StrBufAppendPrintf(Target, "%ld", GetCount(Msg->Attachments));
416 }
417
418
419
420
421
422
423
424 void examine_hnod(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
425 {
426         FreeStrBuf(&Msg->hnod);
427         Msg->hnod = NewStrBufPlain(NULL, StrLength(HdrLine));
428         StrBuf_RFC822_to_Utf8(Msg->hnod, HdrLine, WC->DefaultCharset, FoundCharset);
429 }
430 void tmplput_MAIL_SUMM_H_NODE(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
431 {
432         message_summary *Msg = (message_summary*) Context;
433         StrBufAppendBuf(Target, Msg->hnod, 0);
434 }
435 int Conditional_MAIL_SUMM_H_NODE(WCTemplateToken *Tokens, void *Context, int ContextType)
436 {
437         message_summary *Msg = (message_summary*) Context;
438         return StrLength(Msg->hnod) > 0;
439 }
440
441
442
443 void examine_text(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
444 {
445         Msg->MsgBody->Data = NewStrBuf();
446 }
447
448 void examine_msg4_partnum(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
449 {
450         Msg->MsgBody->PartNum = NewStrBufDup(HdrLine);
451         StrBufTrim(Msg->MsgBody->PartNum);/////TODO: striplt == trim?
452 }
453
454 void examine_content_encoding(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
455 {
456 ////TODO: do we care?
457 }
458
459 void examine_content_lengh(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
460 {
461         Msg->MsgBody->length = StrTol(HdrLine);
462         Msg->MsgBody->size_known = 1;
463 }
464
465 void examine_content_type(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
466 {////TODO
467         int len, i;
468         Msg->MsgBody->ContentType = NewStrBufDup(HdrLine);
469         StrBufTrim(Msg->MsgBody->ContentType);/////todo==striplt?
470         len = StrLength(Msg->MsgBody->ContentType);
471         for (i=0; i<len; ++i) {
472                 if (!strncasecmp(ChrPtr(Msg->MsgBody->ContentType) + i, "charset=", 8)) {/// TODO: WHUT?
473 //                      safestrncpy(mime_charset, &mime_content_type[i+8],
474                         ///                         sizeof mime_charset);
475                 }
476         }/****
477         for (i=0; i<len; ++i) {
478                 if (mime_content_type[i] == ';') {
479                         mime_content_type[i] = 0;
480                         len = i - 1;
481                 }
482         }
483         len = strlen(mime_charset);
484         for (i=0; i<len; ++i) {
485                 if (mime_charset[i] == ';') {
486                         mime_charset[i] = 0;
487                         len = i - 1;
488                 }
489         }
490          */
491 }
492
493 void tmplput_MAIL_SUMM_N(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
494 {
495         message_summary *Msg = (message_summary*) Context;
496         StrBufAppendPrintf(Target, "%ld", Msg->msgnum);
497 }
498
499
500
501 int Conditional_MAIL_MIME_ALL(WCTemplateToken *Tokens, void *Context, int ContextType)
502 {
503         message_summary *Msg = (message_summary*) Context;
504         return GetCount(Msg->Attachments) > 0;
505 }
506
507 int Conditional_MAIL_MIME_SUBMESSAGES(WCTemplateToken *Tokens, void *Context, int ContextType)
508 {
509         message_summary *Msg = (message_summary*) Context;
510         return GetCount(Msg->Submessages) > 0;
511 }
512
513 int Conditional_MAIL_MIME_ATTACHLINKS(WCTemplateToken *Tokens, void *Context, int ContextType)
514 {
515         message_summary *Msg = (message_summary*) Context;
516         return GetCount(Msg->AttachLinks) > 0;
517 }
518
519 int Conditional_MAIL_MIME_ATTACH(WCTemplateToken *Tokens, void *Context, int ContextType)
520 {
521         message_summary *Msg = (message_summary*) Context;
522         return GetCount(Msg->AllAttach) > 0;
523 }
524
525
526
527 /*----------------------------------------------------------------------------*/
528 void tmplput_QUOTED_MAIL_BODY(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
529 {
530         long MsgNum;
531         MsgNum = LBstr(Token->Params[0]->Start, Token->Params[0]->len);
532         read_message(Target, HKEY("view_message_replyquote"), MsgNum, 0, NULL);
533 }
534
535 void tmplput_MAIL_BODY(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
536 {
537         message_summary *Msg = (message_summary*) Context;
538         StrBufAppendBuf(Target, Msg->MsgBody->Data, 0);
539 }
540
541
542 void render_MAIL_variformat(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
543 {
544         /* Messages in legacy Citadel variformat get handled thusly... */
545         StrBuf *Target = NewStrBufPlain(NULL, StrLength(Mime->Data));
546         FmOut(Target, "JUSTIFY", Mime->Data);
547         FreeStrBuf(&Mime->Data);
548         Mime->Data = Target;
549 }
550
551 void render_MAIL_text_plain(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
552 {
553         StrBuf *cs = NULL;
554         const char *ptr, *pte;
555         const char *BufPtr = NULL;
556         StrBuf *Line = NewStrBuf();
557         StrBuf *Line1 = NewStrBuf();
558         StrBuf *Line2 = NewStrBuf();
559         StrBuf *Target = NewStrBufPlain(NULL, StrLength(Mime->Data));
560         int ConvertIt = 1;
561         int bn = 0;
562         int bq = 0;
563         int i, n, done = 0;
564         long len;
565 #ifdef HAVE_ICONV
566         iconv_t ic = (iconv_t)(-1) ;
567 #endif
568
569         if ((StrLength(Mime->Data) == 0) && (Mime->length > 0)) {
570                 FreeStrBuf(&Mime->Data);
571                 Mime->Data = NewStrBufPlain(NULL, Mime->length);
572                 if (!read_message(Mime->Data, HKEY("view_submessage"), Mime->msgnum, 0, Mime->PartNum))
573                         return;
574         }
575
576         /* Boring old 80-column fixed format text gets handled this way... */
577         if ((strcasecmp(ChrPtr(Mime->Charset), "us-ascii") == 0) &&
578             (strcasecmp(ChrPtr(Mime->Charset), "UTF-8") == 0))
579                 ConvertIt = 0;
580
581 #ifdef HAVE_ICONV
582         if (ConvertIt) {
583                 if (StrLength(Mime->Charset) != 0)
584                         cs = Mime->Charset;
585                 else if (StrLength(FoundCharset) > 0)
586                         cs = FoundCharset;
587                 else if (StrLength(WC->DefaultCharset) > 0)
588                         cs = WC->DefaultCharset;
589                 if (cs == 0) {
590                         ConvertIt = 0;
591                 }
592                 else {
593                         ctdl_iconv_open("UTF-8", ChrPtr(cs), &ic);
594                         if (ic == (iconv_t)(-1) ) {
595                                 lprintf(5, "%s:%d iconv_open(UTF-8, %s) failed: %s\n",
596                                         __FILE__, __LINE__, ChrPtr(Mime->Charset), strerror(errno));
597                         }
598                 }
599         }
600 #endif
601
602         while ((n = StrBufSipLine(Line, Mime->Data, &BufPtr), n >= 0) && !done)
603         {
604                 done = n == 0;
605                 bq = 0;
606                 i = 0;
607                 ptr = ChrPtr(Line);
608                 len = StrLength(Line);
609                 pte = ptr + len;
610                 
611                 while ((ptr < pte) &&
612                        ((*ptr == '>') ||
613                         isspace(*ptr)))
614                 {
615                         if (*ptr == '>')
616                                 bq++;
617                         ptr ++;
618                         i++;
619                 }
620                 if (i > 0) StrBufCutLeft(Line, i);
621                 
622                 if (StrLength(Line) == 0)
623                         continue;
624
625                 for (i = bn; i < bq; i++)                               
626                         StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
627                 for (i = bq; i < bn; i++)                               
628                         StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
629
630                 if (ConvertIt == 1) {
631                         StrBufConvert(Line, Line1, &ic);
632                 }
633
634                 StrBufAppendBufPlain(Target, HKEY("<tt>"), 0);
635                 UrlizeText(Line1, Line, Line2);
636
637                 StrEscAppend(Target, Line1, NULL, 0, 0);
638                 StrBufAppendBufPlain(Target, HKEY("</tt><br />\n"), 0);
639                 bn = bq;
640         }
641
642         for (i = 0; i < bn; i++)                                
643                 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
644
645         StrBufAppendBufPlain(Target, HKEY("</i><br />"), 0);
646 #ifdef HAVE_ICONV
647         if (ic != (iconv_t)(-1) ) {
648                 iconv_close(ic);
649         }
650 #endif
651         FreeStrBuf(&Mime->Data);
652         Mime->Data = Target;
653         FreeStrBuf(&Line);
654         FreeStrBuf(&Line1);
655         FreeStrBuf(&Line2);
656 }
657
658 void render_MAIL_html(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
659 {
660         StrBuf *Buf;
661         /* HTML is fun, but we've got to strip it first */
662         if (StrLength(Mime->Data) == 0)
663                 return;
664
665         Buf = NewStrBufPlain(NULL, StrLength(Mime->Data));
666
667         output_html(ChrPtr(Mime->Charset), 
668                     (WC->wc_view == VIEW_WIKI ? 1 : 0), 
669                     StrToi(Mime->PartNum), 
670                     Mime->Data, Buf);
671         FreeStrBuf(&Mime->Data);
672         Mime->Data = Buf;
673 }
674
675 void render_MAIL_UNKNOWN(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
676 {
677         /* Unknown weirdness */
678         FlushStrBuf(Mime->Data);
679         StrBufAppendBufPlain(Mime->Data, _("I don't know how to display "), -1, 0);
680         StrBufAppendBuf(Mime->Data, Mime->ContentType, 0);
681         StrBufAppendBufPlain(Mime->Data, HKEY("<br />\n"), 0);
682 }
683
684
685
686
687
688
689 HashList *iterate_get_mime_All(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
690 {
691         message_summary *Msg = (message_summary*) Context;
692         return Msg->Attachments;
693 }
694 HashList *iterate_get_mime_Submessages(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
695 {
696         message_summary *Msg = (message_summary*) Context;
697         return Msg->Submessages;
698 }
699 HashList *iterate_get_mime_AttachLinks(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
700 {
701         message_summary *Msg = (message_summary*) Context;
702         return Msg->AttachLinks;
703 }
704 HashList *iterate_get_mime_Attachments(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
705 {
706         message_summary *Msg = (message_summary*) Context;
707         return Msg->AllAttach;
708 }
709
710 void tmplput_MIME_Name(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
711 {
712         wc_mime_attachment *mime = (wc_mime_attachment*) Context;
713         StrBufAppendBuf(Target, mime->Name, 0);
714 }
715
716 void tmplput_MIME_FileName(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
717 {
718         wc_mime_attachment *mime = (wc_mime_attachment*) Context;
719         StrBufAppendBuf(Target, mime->FileName, 0);
720 }
721
722 void tmplput_MIME_PartNum(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
723 {
724         wc_mime_attachment *mime = (wc_mime_attachment*) Context;
725         StrBufAppendBuf(Target, mime->PartNum, 0);
726 }
727
728 void tmplput_MIME_MsgNum(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
729 {
730         wc_mime_attachment *mime = (wc_mime_attachment*) Context;
731         StrBufAppendPrintf(Target, "%ld", mime->msgnum);
732 }
733
734 void tmplput_MIME_Disposition(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
735 {
736         wc_mime_attachment *mime = (wc_mime_attachment*) Context;
737         StrBufAppendBuf(Target, mime->Disposition, 0);
738 }
739
740 void tmplput_MIME_ContentType(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
741 {
742         wc_mime_attachment *mime = (wc_mime_attachment*) Context;
743         StrBufAppendBuf(Target, mime->ContentType, 0);
744 }
745
746 void tmplput_MIME_Charset(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
747 {
748         wc_mime_attachment *mime = (wc_mime_attachment*) Context;
749         StrBufAppendBuf(Target, mime->Charset, 0);
750 }
751
752 void tmplput_MIME_Data(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
753 {
754         wc_mime_attachment *mime = (wc_mime_attachment*) Context;
755         if (mime->Renderer != NULL)
756                 mime->Renderer(mime, NULL, NULL);
757         StrBufAppendBuf(Target, mime->Data, 0); /// TODO: check whether we need to load it now?
758 }
759
760 void tmplput_MIME_Length(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
761 {
762         wc_mime_attachment *mime = (wc_mime_attachment*) Context;
763         StrBufAppendPrintf(Target, "%ld", mime->length);
764 }
765
766 HashList *iterate_get_registered_Attachments(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
767 {
768         return WC->attachments;
769 }
770
771 void tmplput_ATT_Length(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
772 {
773         wc_attachment *att = (wc_attachment*) Context;
774         StrBufAppendPrintf(Target, "%ld", att->length);
775 }
776
777 void tmplput_ATT_Contenttype(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
778 {
779         wc_attachment *att = (wc_attachment*) Context;
780         StrBufAppendBuf(Target, att->content_type, 0);
781 }
782
783 void tmplput_ATT_FileName(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
784 {
785         wc_attachment *att = (wc_attachment*) Context;
786         StrBufAppendBuf(Target, att->filename, 0);
787 }
788
789
790
791
792
793
794 void 
795 InitModule_MSGRENDERERS
796 (void)
797 {
798         RegisterNamespace("MAIL:SUMM:DATESTR", 0, 0, tmplput_MAIL_SUMM_DATE_STR, CTX_MAILSUM);
799         RegisterNamespace("MAIL:SUMM:DATENO",  0, 0, tmplput_MAIL_SUMM_DATE_NO,  CTX_MAILSUM);
800         RegisterNamespace("MAIL:SUMM:N",       0, 0, tmplput_MAIL_SUMM_N,        CTX_MAILSUM);
801         RegisterNamespace("MAIL:SUMM:FROM",    0, 2, tmplput_MAIL_SUMM_FROM,     CTX_MAILSUM);
802         RegisterNamespace("MAIL:SUMM:TO",      0, 2, tmplput_MAIL_SUMM_TO,       CTX_MAILSUM);
803         RegisterNamespace("MAIL:SUMM:SUBJECT", 0, 4, tmplput_MAIL_SUMM_SUBJECT,  CTX_MAILSUM);
804         RegisterNamespace("MAIL:SUMM:NTATACH", 0, 0, tmplput_MAIL_SUMM_NATTACH,  CTX_MAILSUM);
805         RegisterNamespace("MAIL:SUMM:CCCC", 0, 2, tmplput_MAIL_SUMM_CCCC,  CTX_MAILSUM);
806         RegisterNamespace("MAIL:SUMM:H_NODE", 0, 2, tmplput_MAIL_SUMM_H_NODE,  CTX_MAILSUM);
807         RegisterNamespace("MAIL:SUMM:ALLRCPT", 0, 2, tmplput_MAIL_SUMM_ALLRCPT,  CTX_MAILSUM);
808         RegisterNamespace("MAIL:SUMM:ORGROOM", 0, 2, tmplput_MAIL_SUMM_ORGROOM,  CTX_MAILSUM);
809         RegisterNamespace("MAIL:SUMM:RFCA", 0, 2, tmplput_MAIL_SUMM_RFCA,  CTX_MAILSUM);
810         RegisterNamespace("MAIL:SUMM:OTHERNODE", 2, 0, tmplput_MAIL_SUMM_OTHERNODE,  CTX_MAILSUM);
811         RegisterNamespace("MAIL:SUMM:REFIDS", 0, 0, tmplput_MAIL_SUMM_REFIDS,  CTX_MAILSUM);
812         RegisterNamespace("MAIL:SUMM:INREPLYTO", 0, 2, tmplput_MAIL_SUMM_INREPLYTO,  CTX_MAILSUM);
813         RegisterNamespace("MAIL:BODY", 0, 2, tmplput_MAIL_BODY,  CTX_MAILSUM);
814         RegisterNamespace("MAIL:QUOTETEXT", 1, 2, tmplput_QUOTED_MAIL_BODY,  CTX_NONE);
815         RegisterNamespace("ATT:SIZE", 0, 1, tmplput_ATT_Length, CTX_ATT);
816         RegisterNamespace("ATT:TYPE", 0, 1, tmplput_ATT_Contenttype, CTX_ATT);
817         RegisterNamespace("ATT:FILENAME", 0, 1, tmplput_ATT_FileName, CTX_ATT);
818
819         RegisterConditional(HKEY("MAIL:SUMM:RFCA"), 0, Conditional_MAIL_SUMM_RFCA,  CTX_MAILSUM);
820         RegisterConditional(HKEY("COND:MAIL:SUMM:UNREAD"), 0, Conditional_MAIL_SUMM_UNREAD, CTX_MAILSUM);
821         RegisterConditional(HKEY("COND:MAIL:SUMM:H_NODE"), 0, Conditional_MAIL_SUMM_H_NODE, CTX_MAILSUM);
822         RegisterConditional(HKEY("COND:MAIL:SUMM:OTHERNODE"), 0, Conditional_MAIL_SUMM_OTHERNODE, CTX_MAILSUM);
823         RegisterConditional(HKEY("COND:MAIL:ANON"), 0, Conditional_ANONYMOUS_MESSAGE, CTX_MAILSUM);
824
825         RegisterConditional(HKEY("COND:MAIL:MIME:ATTACH"), 0, Conditional_MAIL_MIME_ALL, CTX_MAILSUM);
826         RegisterConditional(HKEY("COND:MAIL:MIME:ATTACH:SUBMESSAGES"), 0, Conditional_MAIL_MIME_SUBMESSAGES, CTX_MAILSUM);
827         RegisterConditional(HKEY("COND:MAIL:MIME:ATTACH:LINKS"), 0, Conditional_MAIL_MIME_ATTACHLINKS, CTX_MAILSUM);
828         RegisterConditional(HKEY("COND:MAIL:MIME:ATTACH:ATT"), 0, Conditional_MAIL_MIME_ATTACH, CTX_MAILSUM);
829
830         RegisterIterator("MAIL:MIME:ATTACH", 0, NULL, iterate_get_mime_All, 
831                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM);
832         RegisterIterator("MAIL:MIME:ATTACH:SUBMESSAGES", 0, NULL, iterate_get_mime_Submessages, 
833                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM);
834         RegisterIterator("MAIL:MIME:ATTACH:LINKS", 0, NULL, iterate_get_mime_AttachLinks, 
835                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM);
836         RegisterIterator("MAIL:MIME:ATTACH:ATT", 0, NULL, iterate_get_mime_Attachments, 
837                          NULL, NULL, CTX_MIME_ATACH, CTX_MAILSUM);
838
839         RegisterNamespace("MAIL:MIME:NAME", 0, 2, tmplput_MIME_Name, CTX_MIME_ATACH);
840         RegisterNamespace("MAIL:MIME:FILENAME", 0, 2, tmplput_MIME_FileName, CTX_MIME_ATACH);
841         RegisterNamespace("MAIL:MIME:PARTNUM", 0, 2, tmplput_MIME_PartNum, CTX_MIME_ATACH);
842         RegisterNamespace("MAIL:MIME:MSGNUM", 0, 2, tmplput_MIME_MsgNum, CTX_MIME_ATACH);
843         RegisterNamespace("MAIL:MIME:DISPOSITION", 0, 2, tmplput_MIME_Disposition, CTX_MIME_ATACH);
844         RegisterNamespace("MAIL:MIME:CONTENTTYPE", 0, 2, tmplput_MIME_ContentType, CTX_MIME_ATACH);
845         RegisterNamespace("MAIL:MIME:CHARSET", 0, 2, tmplput_MIME_Charset, CTX_MIME_ATACH);
846         RegisterNamespace("MAIL:MIME:LENGTH", 0, 2, tmplput_MIME_Length, CTX_MIME_ATACH);
847         RegisterNamespace("MAIL:MIME:DATA", 0, 2, tmplput_MIME_Data, CTX_MIME_ATACH);
848
849         RegisterIterator("MSG:ATTACHNAMES", 0, NULL, iterate_get_registered_Attachments, 
850                          NULL, NULL, CTX_ATT, CTX_NONE);
851
852         RegisterMimeRenderer(HKEY("message/rfc822"), render_MAIL);
853         RegisterMimeRenderer(HKEY("text/x-vcard"), render_MIME_VCard);
854         RegisterMimeRenderer(HKEY("text/vcard"), render_MIME_VCard);
855         RegisterMimeRenderer(HKEY("text/calendar"), render_MIME_ICS);
856         RegisterMimeRenderer(HKEY("application/ics"), render_MIME_ICS);
857
858         RegisterMimeRenderer(HKEY("text/x-citadel-variformat"), render_MAIL_variformat);
859         RegisterMimeRenderer(HKEY("text/plain"), render_MAIL_text_plain);
860         RegisterMimeRenderer(HKEY("text"), render_MAIL_text_plain);
861         RegisterMimeRenderer(HKEY("text/html"), render_MAIL_html);
862         RegisterMimeRenderer(HKEY(""), render_MAIL_UNKNOWN);
863
864         RegisterMsgHdr(HKEY("nhdr"), examine_nhdr, 0);
865         RegisterMsgHdr(HKEY("type"), examine_type, 0);
866         RegisterMsgHdr(HKEY("from"), examine_from, 0);
867         RegisterMsgHdr(HKEY("subj"), examine_subj, 0);
868         RegisterMsgHdr(HKEY("msgn"), examine_msgn, 0);
869         RegisterMsgHdr(HKEY("wefw"), examine_wefw, 0);
870         RegisterMsgHdr(HKEY("cccc"), examine_cccc, 0);
871         RegisterMsgHdr(HKEY("hnod"), examine_hnod, 0);
872         RegisterMsgHdr(HKEY("room"), examine_room, 0);
873         RegisterMsgHdr(HKEY("rfca"), examine_rfca, 0);
874         RegisterMsgHdr(HKEY("node"), examine_node, 0);
875         RegisterMsgHdr(HKEY("rcpt"), examine_rcpt, 0);
876         RegisterMsgHdr(HKEY("time"), examine_time, 0);
877         RegisterMsgHdr(HKEY("part"), examine_mime_part, 0);
878         RegisterMsgHdr(HKEY("text"), examine_text, 1);
879         RegisterMsgHdr(HKEY("X-Citadel-MSG4-Partnum"), examine_msg4_partnum, 0);
880         RegisterMsgHdr(HKEY("Content-type"), examine_content_type, 0);
881         RegisterMsgHdr(HKEY("Content-length"), examine_content_lengh, 0);
882         RegisterMsgHdr(HKEY("Content-transfer-encoding"), examine_content_encoding, 0);
883 }