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