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