]> code.citadel.org Git - citadel.git/blob - webcit/messages.c
e41b83b6f2fb17dce4ac4ce4840aa3c26425854d
[citadel.git] / webcit / messages.c
1 /*
2  * $Id$
3  *
4  * Functions which deal with the fetching and displaying of messages.
5  *
6  */
7
8 #include "webcit.h"
9 #include "webserver.h"
10 #include "groupdav.h"
11
12 HashList *MsgHeaderHandler = NULL;
13 HashList *MsgEvaluators = NULL;
14 HashList *MimeRenderHandler = NULL;
15
16 #define SUBJ_COL_WIDTH_PCT              50      /**< Mailbox view column width */
17 #define SENDER_COL_WIDTH_PCT            30      /**< Mailbox view column width */
18 #define DATE_PLUS_BUTTONS_WIDTH_PCT     20      /**< Mailbox view column width */
19
20 void display_enter(void);
21 int longcmp_r(const void *s1, const void *s2);
22 int summcmp_subj(const void *s1, const void *s2);
23 int summcmp_rsubj(const void *s1, const void *s2);
24 int summcmp_sender(const void *s1, const void *s2);
25 int summcmp_rsender(const void *s1, const void *s2);
26 int summcmp_date(const void *s1, const void *s2);
27 int summcmp_rdate(const void *s1, const void *s2);
28
29 /*----------------------------------------------------------------------------*/
30
31
32 typedef void (*MsgPartEvaluatorFunc)(message_summary *Sum, StrBuf *Buf);
33
34
35 struct attach_link {
36         char partnum[32];
37         char html[1024];
38 };
39
40
41
42 typedef struct _headereval {
43         ExamineMsgHeaderFunc evaluator;
44         int Type;
45 } headereval;
46
47
48
49 enum {
50         eUp,
51         eDown,
52         eNone
53 };
54
55 const char* SortIcons[3] = {
56         "static/up_pointer.gif",
57         "static/down_pointer.gif",
58         "static/sort_none.gif"
59 };
60
61 enum  {/// SortByEnum
62         eDate,
63         eRDate,
64         eSubject,
65         eRSubject,
66         eSender,
67         eRSender,
68         eReverse,
69         eUnSet
70 }; 
71
72 /* SortEnum to plain string representation */
73 static const char* SortByStrings[] = {
74         "date",
75         "rdate",
76         "subject", 
77         "rsubject", 
78         "sender",
79         "rsender",
80         "reverse",
81         "unset"
82 };
83
84 /* SortEnum to sort-Function Table */
85 const CompareFunc SortFuncs[eUnSet] = {
86         summcmp_date,
87         summcmp_rdate,
88         summcmp_subj,
89         summcmp_rsubj,
90         summcmp_sender,
91         summcmp_rsender,
92         summcmp_rdate
93 };
94
95 /* given a SortEnum, which icon should we choose? */
96 const int SortDateToIcon[eUnSet] = { eUp, eDown, eNone, eNone, eNone, eNone, eNone};
97 const int SortSubjectToIcon[eUnSet] = { eNone, eNone, eUp, eDown, eNone, eNone, eNone};
98 const int SortSenderToIcon[eUnSet] = { eNone, eNone, eNone, eNone, eUp, eDown, eNone};
99
100 /* given a SortEnum, which would be the "opposite" search option? */
101 const int DateInvertSortString[eUnSet] =  { eRDate, eDate, eDate, eDate, eDate, eDate, eDate};
102 const int SubjectInvertSortString[eUnSet] =  { eSubject, eSubject, eRSubject, eUnSet, eSubject, eSubject, eSubject};
103 const int SenderInvertSortString[eUnSet] =  { eSender, eSender, eSender, eSender, eRSender, eUnSet, eSender};
104
105
106
107
108 /*
109  * Address book entry (keep it short and sweet, it's just a quickie lookup
110  * which we can use to get to the real meat and bones later)
111  */
112 struct addrbookent {
113         char ab_name[64]; /**< name string */
114         long ab_msgnum;   /**< message number of address book entry */
115 };
116
117 /*----------------------------------------------------------------------------*/
118
119 /*
120  * Translates sortoption String to its SortEnum representation 
121  * returns the enum matching the string; defaults to RDate
122  */
123 //SortByEnum 
124 int StrToESort (const StrBuf *sortby)
125 {////todoo: hash
126         int result = eDate;
127
128         if (!IsEmptyStr(ChrPtr(sortby))) while (result < eUnSet){
129                         if (!strcasecmp(ChrPtr(sortby), 
130                                         SortByStrings[result])) 
131                                 return result;
132                         result ++;
133                 }
134         return eRDate;
135 }
136
137
138 typedef int (*QSortFunction) (const void*, const void*);
139
140 /*
141  * qsort() compatible function to compare two longs in descending order.
142  */
143 int longcmp_r(const void *s1, const void *s2) {
144         long l1;
145         long l2;
146
147         l1 = *(long *)GetSearchPayload(s1);
148         l2 = *(long *)GetSearchPayload(s2);
149
150         if (l1 > l2) return(-1);
151         if (l1 < l2) return(+1);
152         return(0);
153 }
154
155  
156 /*
157  * qsort() compatible function to compare two message summary structs by ascending subject.
158  */
159 int summcmp_subj(const void *s1, const void *s2) {
160         message_summary *summ1;
161         message_summary *summ2;
162         
163         summ1 = (message_summary *)GetSearchPayload(s1);
164         summ2 = (message_summary *)GetSearchPayload(s2);
165         return strcasecmp(ChrPtr(summ1->subj), ChrPtr(summ2->subj));
166 }
167
168 /*
169  * qsort() compatible function to compare two message summary structs by descending subject.
170  */
171 int summcmp_rsubj(const void *s1, const void *s2) {
172         message_summary *summ1;
173         message_summary *summ2;
174         
175         summ1 = (message_summary *)GetSearchPayload(s1);
176         summ2 = (message_summary *)GetSearchPayload(s2);
177         return strcasecmp(ChrPtr(summ2->subj), ChrPtr(summ1->subj));
178 }
179
180 /*
181  * qsort() compatible function to compare two message summary structs by ascending sender.
182  */
183 int summcmp_sender(const void *s1, const void *s2) {
184         message_summary *summ1;
185         message_summary *summ2;
186         
187         summ1 = (message_summary *)GetSearchPayload(s1);
188         summ2 = (message_summary *)GetSearchPayload(s2);
189         return strcasecmp(ChrPtr(summ1->from), ChrPtr(summ2->from));
190 }
191
192 /*
193  * qsort() compatible function to compare two message summary structs by descending sender.
194  */
195 int summcmp_rsender(const void *s1, const void *s2) {
196         message_summary *summ1;
197         message_summary *summ2;
198         
199         summ1 = (message_summary *)GetSearchPayload(s1);
200         summ2 = (message_summary *)GetSearchPayload(s2);
201         return strcasecmp(ChrPtr(summ2->from), ChrPtr(summ1->from));
202 }
203
204 /*
205  * qsort() compatible function to compare two message summary structs by ascending date.
206  */
207 int summcmp_date(const void *s1, const void *s2) {
208         message_summary *summ1;
209         message_summary *summ2;
210         
211         summ1 = (message_summary *)GetSearchPayload(s1);
212         summ2 = (message_summary *)GetSearchPayload(s2);
213
214         if (summ1->date < summ2->date) return -1;
215         else if (summ1->date > summ2->date) return +1;
216         else return 0;
217 }
218
219 /*
220  * qsort() compatible function to compare two message summary structs by descending date.
221  */
222 int summcmp_rdate(const void *s1, const void *s2) {
223         message_summary *summ1;
224         message_summary *summ2;
225         
226         summ1 = (message_summary *)GetSearchPayload(s1);
227         summ2 = (message_summary *)GetSearchPayload(s2);
228
229         if (summ1->date < summ2->date) return +1;
230         else if (summ1->date > summ2->date) return -1;
231         else return 0;
232 }
233
234
235
236
237
238 /*----------------------------------------------------------------------------*/
239
240 /**
241  * message index functions
242  */
243
244
245
246
247 void DestroyMessageSummary(void *vMsg)
248 {
249         message_summary *Msg = (message_summary*) vMsg;
250
251         FreeStrBuf(&Msg->from);
252         FreeStrBuf(&Msg->to);
253         FreeStrBuf(&Msg->subj);
254         FreeStrBuf(&Msg->reply_inreplyto);
255         FreeStrBuf(&Msg->reply_references);
256         FreeStrBuf(&Msg->cccc);
257         FreeStrBuf(&Msg->hnod);
258         FreeStrBuf(&Msg->AllRcpt);
259         FreeStrBuf(&Msg->Room);
260         FreeStrBuf(&Msg->Rfca);
261         FreeStrBuf(&Msg->OtherNode);
262
263         free(Msg);
264 }
265
266
267
268 void RegisterMsgHdr(const char *HeaderName, long HdrNLen, ExamineMsgHeaderFunc evaluator, int type)
269 {
270         headereval *ev;
271         ev = (headereval*) malloc(sizeof(headereval));
272         ev->evaluator = evaluator;
273         ev->Type = type;
274         Put(MsgHeaderHandler, HeaderName, HdrNLen, ev, NULL);
275 }
276
277 void RegisterMimeRenderer(const char *HeaderName, long HdrNLen, RenderMimeFunc MimeRenderer)
278 {
279         Put(MimeRenderHandler, HeaderName, HdrNLen, MimeRenderer, reference_free_handler);
280         
281 }
282
283 /*----------------------------------------------------------------------------*/
284
285
286 void examine_nhdr(message_summary *Msg, StrBuf *HdrLine)
287 {
288         Msg->nhdr = 0;
289         if (!strncasecmp(ChrPtr(HdrLine), "yes", 8))
290                 Msg->nhdr = 1;
291 }
292
293 void examine_type(message_summary *Msg, StrBuf *HdrLine)
294 {
295         Msg->format_type = StrToi(HdrLine);
296                         
297 }
298
299 void examine_from(message_summary *Msg, StrBuf *HdrLine)
300 {
301         FreeStrBuf(&Msg->from);
302         Msg->from = NewStrBufPlain(NULL, StrLength(HdrLine));
303         StrBuf_RFC822_to_Utf8(Msg->from, HdrLine, WC->DefaultCharset);
304 }
305 void tmplput_MAIL_SUMM_FROM(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
306 {
307         message_summary *Msg = (message_summary*) Context;
308         StrBufAppendBuf(Target, Msg->from, 0);
309         lprintf(1,"%s", ChrPtr(Msg->from));
310 }
311
312
313
314 void examine_subj(message_summary *Msg, StrBuf *HdrLine)
315 {
316         FreeStrBuf(&Msg->subj);
317         Msg->subj = NewStrBufPlain(NULL, StrLength(HdrLine));
318         StrBuf_RFC822_to_Utf8(Msg->subj, HdrLine, WC->DefaultCharset);
319         lprintf(1,"%s", ChrPtr(Msg->subj));
320 }
321 void tmplput_MAIL_SUMM_SUBJECT(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
322 {/////TODO: Fwd: and RE: filter!!
323         message_summary *Msg = (message_summary*) Context;
324         StrBufAppendBuf(Target, Msg->subj, 0);
325 }
326
327
328 void examine_msgn(message_summary *Msg, StrBuf *HdrLine)
329 {
330         FreeStrBuf(&Msg->reply_inreplyto);
331         Msg->reply_inreplyto = NewStrBufPlain(NULL, StrLength(HdrLine));
332         StrBuf_RFC822_to_Utf8(Msg->reply_inreplyto, HdrLine, WC->DefaultCharset);
333 }
334 void tmplput_MAIL_SUMM_INREPLYTO(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
335 {
336         message_summary *Msg = (message_summary*) Context;
337         StrBufAppendBuf(Target, Msg->reply_inreplyto, 0);
338 }
339
340
341 void examine_wefw(message_summary *Msg, StrBuf *HdrLine)
342 {
343         FreeStrBuf(&Msg->reply_references);
344         Msg->reply_references = NewStrBufPlain(NULL, StrLength(HdrLine));
345         StrBuf_RFC822_to_Utf8(Msg->reply_references, HdrLine, WC->DefaultCharset);
346 }
347 void tmplput_MAIL_SUMM_REFIDS(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
348 {
349         message_summary *Msg = (message_summary*) Context;
350         StrBufAppendBuf(Target, Msg->reply_references, 0);
351 }
352
353
354 void examine_cccc(message_summary *Msg, StrBuf *HdrLine)
355 {
356         FreeStrBuf(&Msg->cccc);
357         Msg->cccc = NewStrBufPlain(NULL, StrLength(HdrLine));
358         StrBuf_RFC822_to_Utf8(Msg->cccc, HdrLine, WC->DefaultCharset);
359         if (Msg->AllRcpt == NULL)
360                 Msg->AllRcpt = NewStrBufPlain(NULL, StrLength(HdrLine));
361         if (StrLength(Msg->AllRcpt) > 0) {
362                 StrBufAppendBufPlain(Msg->AllRcpt, HKEY(", "), 0);
363         }
364         StrBufAppendBuf(Msg->AllRcpt, Msg->cccc, 0);
365 }
366 void tmplput_MAIL_SUMM_CCCC(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
367 {
368         message_summary *Msg = (message_summary*) Context;
369         StrBufAppendBuf(Target, Msg->cccc, 0);
370 }
371
372
373
374
375 void examine_room(message_summary *Msg, StrBuf *HdrLine)
376 {
377         if ((StrLength(HdrLine) > 0) &&
378             (strcasecmp(ChrPtr(HdrLine), WC->wc_roomname))) {
379                 FreeStrBuf(&Msg->Room);
380                 Msg->Room = NewStrBufDup(HdrLine);              
381         }
382 }
383 void tmplput_MAIL_SUMM_ORGROOM(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
384 {
385         message_summary *Msg = (message_summary*) Context;
386         StrBufAppendBuf(Target, Msg->Room, 0);
387 }
388
389
390 void examine_rfca(message_summary *Msg, StrBuf *HdrLine)
391 {
392         FreeStrBuf(&Msg->Rfca);
393         Msg->Rfca = NewStrBufDup(HdrLine);
394 }
395 void tmplput_MAIL_SUMM_RFCA(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
396 {
397         message_summary *Msg = (message_summary*) Context;
398         StrBufAppendBuf(Target, Msg->Rfca, 0);
399 }
400
401
402 void examine_node(message_summary *Msg, StrBuf *HdrLine)
403 {
404         if ( (StrLength(HdrLine) > 0) &&
405              ((WC->room_flags & QR_NETWORK)
406               || ((strcasecmp(ChrPtr(HdrLine), serv_info.serv_nodename)
407                    && (strcasecmp(ChrPtr(HdrLine), serv_info.serv_fqdn)))))) {
408                 FreeStrBuf(&Msg->OtherNode);
409                 Msg->OtherNode = NewStrBufDup(HdrLine);
410         }
411 }
412 void tmplput_MAIL_SUMM_OTHERNODE(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
413 {
414         message_summary *Msg = (message_summary*) Context;
415         StrBufAppendBuf(Target, Msg->OtherNode, 0);
416 }
417 int Conditional_MAIL_SUMM_OTHERNODE(WCTemplateToken *Tokens, void *Context, int ContextType)
418 {
419         message_summary *Msg = (message_summary*) Context;
420         return StrLength(Msg->OtherNode) > 0;
421 }
422
423
424 void examine_rcpt(message_summary *Msg, StrBuf *HdrLine)
425 {
426         FreeStrBuf(&Msg->to);
427         Msg->to = NewStrBufPlain(NULL, StrLength(HdrLine));
428         StrBuf_RFC822_to_Utf8(Msg->to, HdrLine, WC->DefaultCharset);
429         if (Msg->AllRcpt == NULL)
430                 Msg->AllRcpt = NewStrBufPlain(NULL, StrLength(HdrLine));
431         if (StrLength(Msg->AllRcpt) > 0) {
432                 StrBufAppendBufPlain(Msg->AllRcpt, HKEY(", "), 0);
433         }
434         StrBufAppendBuf(Msg->AllRcpt, Msg->to, 0);
435 }
436 void tmplput_MAIL_SUMM_TO(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
437 {
438         message_summary *Msg = (message_summary*) Context;
439         StrBufAppendBuf(Target, Msg->to, 0);
440 }
441 void tmplput_MAIL_SUMM_ALLRCPT(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
442 {
443         message_summary *Msg = (message_summary*) Context;
444         StrBufAppendBuf(Target, Msg->AllRcpt, 0);
445 }
446
447
448
449 void examine_time(message_summary *Msg, StrBuf *HdrLine)
450 {
451         Msg->date = StrTol(HdrLine);
452 }
453 void tmplput_MAIL_SUMM_DATE_STR(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
454 {
455         char datebuf[64];
456         message_summary *Msg = (message_summary*) Context;
457         webcit_fmt_date(datebuf, Msg->date, 1);
458         StrBufAppendBufPlain(Target, datebuf, -1, 0);
459 }
460 void tmplput_MAIL_SUMM_DATE_NO(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
461 {
462         message_summary *Msg = (message_summary*) Context;
463         StrBufAppendPrintf(Target, "%ld", Msg->date, 0);
464 }
465
466
467
468 void examine_mime_part(message_summary *Msg, StrBuf *HdrLine)
469 {
470         wc_mime_attachment *mime;
471         StrBuf *Buf;
472
473         mime = (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
474         memset(mime, 0, sizeof(wc_mime_attachment));
475         Buf=NewStrBuf();
476
477         mime->Name = NewStrBuf();
478         StrBufExtract_token(mime->Name, HdrLine, 0, '|');
479         StrBufExtract_token(Buf, HdrLine, 1, '|');
480         StrBuf_RFC822_to_Utf8(mime->FileName, Buf, WC->DefaultCharset);
481         mime->PartNum = NewStrBuf();
482         StrBufExtract_token(mime->PartNum, HdrLine, 2, '|');
483         mime->Disposition = NewStrBuf();
484         StrBufExtract_token(mime->Disposition, HdrLine, 3, '|');
485         mime->ContentType = NewStrBuf();
486         StrBufExtract_token(mime->ContentType, HdrLine, 4, '|');
487         mime->length = StrBufExtract_int(HdrLine, 5, '|');
488
489         StrBufTrim(mime->Name);
490         StrBufTrim(mime->FileName);
491         if ( (StrLength(mime->FileName) == 0) && (StrLength(mime->Name) > 0) ) {
492                 StrBufAppendBuf(mime->FileName, mime->Name, 0);
493         }
494
495         if (!strcasecmp(ChrPtr(mime->ContentType), "message/rfc822")) {
496                 if (Msg->Submessages == NULL)
497                         Msg->Submessages = NewHash(1,NULL);
498                 Put(Msg->Submessages, SKEY(mime->PartNum), mime->PartNum, reference_free_handler);
499         }
500         else if ((!strcasecmp(ChrPtr(mime->Disposition), "inline"))
501                  && (!strncasecmp(ChrPtr(mime->ContentType), "image/", 6)) ){
502                 if (Msg->AttachLinks == NULL)
503                         Msg->AttachLinks = NewHash(1,NULL);
504                 Put(Msg->AttachLinks, SKEY(mime->PartNum), mime->PartNum, reference_free_handler);
505         }
506         else if ((StrLength(mime->ContentType) > 0) &&
507                   ( (!strcasecmp(ChrPtr(mime->Disposition), "attachment")) 
508                     || (!strcasecmp(ChrPtr(mime->Disposition), "inline"))
509                     || (!strcasecmp(ChrPtr(mime->Disposition), ""))))
510         {
511                 
512                 if (Msg->AttachLinks == NULL)
513                         Msg->AttachLinks = NewHash(1,NULL);
514                 Put(Msg->AttachLinks, SKEY(mime->PartNum), mime->PartNum, reference_free_handler);
515                 if (strcasecmp(ChrPtr(mime->ContentType), "application/octet-stream") == 0) {
516                         FlushStrBuf(mime->ContentType);
517                         StrBufAppendBufPlain(mime->ContentType,
518                                              GuessMimeByFilename(SKEY(mime->FileName)),
519                                              -1, 0);
520                 }
521         }
522
523         /** begin handler prep ** */
524         if (  (!strcasecmp(ChrPtr(mime->ContentType), "text/x-vcard"))
525               || (!strcasecmp(ChrPtr(mime->ContentType), "text/vcard")) ) {
526                 Msg->vcard_partnum_ref = mime;
527         }
528         else if (  (!strcasecmp(ChrPtr(mime->ContentType), "text/calendar"))
529               || (!strcasecmp(ChrPtr(mime->ContentType), "application/ics")) ) {
530                 Msg->cal_partnum_ref = mime;
531         }
532         /** end handler prep ***/
533 }
534 void tmplput_MAIL_SUMM_NATTACH(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
535 {
536         message_summary *Msg = (message_summary*) Context;
537         StrBufAppendPrintf(Target, "%ld", GetCount(Msg->Attachments));
538 }
539
540
541
542
543
544
545
546 void examine_hnod(message_summary *Msg, StrBuf *HdrLine)
547 {
548         FreeStrBuf(&Msg->hnod);
549         Msg->hnod = NewStrBufPlain(NULL, StrLength(HdrLine));
550         StrBuf_RFC822_to_Utf8(Msg->hnod, HdrLine, WC->DefaultCharset);
551 }
552 void tmplput_MAIL_SUMM_H_NODE(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
553 {
554         message_summary *Msg = (message_summary*) Context;
555         StrBufAppendBuf(Target, Msg->hnod, 0);
556 }
557 int Conditional_MAIL_SUMM_H_NODE(WCTemplateToken *Tokens, void *Context, int ContextType)
558 {
559         message_summary *Msg = (message_summary*) Context;
560         return StrLength(Msg->hnod) > 0;
561 }
562
563
564
565 void examine_text(message_summary *Msg, StrBuf *HdrLine)
566 {////TODO: read messages here
567         Msg->MsgBody.Data = NewStrBuf();
568 }
569
570 void examine_msg4_partnum(message_summary *Msg, StrBuf *HdrLine)
571 {
572         Msg->MsgBody.PartNum = NewStrBufDup(HdrLine);
573         StrBufTrim(Msg->MsgBody.PartNum);/////TODO: striplt == trim?
574 }
575
576 void examine_content_encoding(message_summary *Msg, StrBuf *HdrLine)
577 {
578 ////TODO: do we care?
579 }
580
581 void examine_content_lengh(message_summary *Msg, StrBuf *HdrLine)
582 {
583         Msg->MsgBody.length = StrTol(HdrLine);
584 }
585
586 void examine_content_type(message_summary *Msg, StrBuf *HdrLine)
587 {////TODO
588         int len, i;
589         Msg->MsgBody.ContentType = NewStrBufDup(HdrLine);
590         StrBufTrim(Msg->MsgBody.ContentType);/////todo==striplt?
591         len = StrLength(Msg->MsgBody.ContentType);
592         for (i=0; i<len; ++i) {
593                 if (!strncasecmp(ChrPtr(Msg->MsgBody.ContentType) + i, "charset=", 8)) {/// TODO: WHUT?
594 //                      safestrncpy(mime_charset, &mime_content_type[i+8],
595                         ///                         sizeof mime_charset);
596                 }
597         }/****
598         for (i=0; i<len; ++i) {
599                 if (mime_content_type[i] == ';') {
600                         mime_content_type[i] = 0;
601                         len = i - 1;
602                 }
603         }
604         len = strlen(mime_charset);
605         for (i=0; i<len; ++i) {
606                 if (mime_charset[i] == ';') {
607                         mime_charset[i] = 0;
608                         len = i - 1;
609                 }
610         }
611          */
612 }
613
614 void tmplput_MAIL_SUMM_N(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
615 {
616         message_summary *Msg = (message_summary*) Context;
617         StrBufAppendPrintf(Target, "%ld", Msg->msgnum);
618 }
619
620
621 int Conditional_MAIL_SUMM_UNREAD(WCTemplateToken *Tokens, void *Context, int ContextType)
622 {
623         message_summary *Msg = (message_summary*) Context;
624         return Msg->is_new != 0;
625 }
626
627
628
629 /*----------------------------------------------------------------------------*/
630
631
632 void tmplput_MAIL_BODY(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
633 {
634         message_summary *Msg = (message_summary*) Context;
635         StrBufAppendBuf(Target, Msg->MsgBody.Data, 0);
636 }
637
638
639 void render_MAIL_variformat(wc_mime_attachment *Mime, StrBuf *RawData)
640 {
641         /* Messages in legacy Citadel variformat get handled thusly... */
642         fmout("JUSTIFY");///todo: this won't work that way.
643         
644 }
645
646 void render_MAIL_text_plain(wc_mime_attachment *Mime, StrBuf *RawData)
647 {
648         const char *ptr, *pte;
649         const char *BufPtr = NULL;
650         StrBuf *Line = NewStrBuf();
651         StrBuf *Line1 = NewStrBuf();
652         StrBuf *Line2 = NewStrBuf();
653         StrBuf *Target = NewStrBufPlain(NULL, StrLength(Mime->Data));
654         int ConvertIt = 1;
655         int bn = 0;
656         int bq = 0;
657         int i;
658         long len;
659 #ifdef HAVE_ICONV
660         iconv_t ic = (iconv_t)(-1) ;
661 #endif
662
663         /* Boring old 80-column fixed format text gets handled this way... */
664         if ((strcasecmp(ChrPtr(Mime->Charset), "us-ascii") == 0) &&
665             (strcasecmp(ChrPtr(Mime->Charset), "UTF-8") == 0))
666                 ConvertIt = 0;
667
668 #ifdef HAVE_ICONV
669         if (ConvertIt) {
670                 ctdl_iconv_open("UTF-8", ChrPtr(Mime->Charset), &ic);
671                 if (ic == (iconv_t)(-1) ) {
672                         lprintf(5, "%s:%d iconv_open(UTF-8, %s) failed: %s\n",
673                                 __FILE__, __LINE__, ChrPtr(Mime->Charset), strerror(errno));
674                 }
675         }
676 #endif
677
678         while (StrBufSipLine(Line, Mime->Data, &BufPtr) > 0)
679         {
680                 bq = 0;
681                 i = 0;
682
683                 ptr = ChrPtr(Line);
684                 len = StrLength(Line);
685                 pte = ptr + len;
686                 
687                 while ((ptr < pte) &&
688                        ((*ptr == '>') ||
689                         isspace(*ptr)))
690                 {
691                         if (*ptr == '>')
692                                 bq++;
693                         ptr ++;
694                         i++;
695                 }
696                 if (i > 0) StrBufCutLeft(Line, i);
697                 
698                 if (StrLength(Line) == 0)
699                         continue;
700
701                 for (i = bn; i < bq; i++)                               
702                         StrBufAppendBufPlain(Target, HKEY("<blockquote>"), 0);
703                 for (i = bq; i < bn; i++)                               
704                         StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
705                 
706                 StrBufAppendBufPlain(Target, HKEY("<tt>"), 0);
707                 UrlizeText(Line1, Line, Line2);
708
709                 StrEscAppend(Target, Line1, NULL, 0, 0);
710                 StrBufAppendBufPlain(Target, HKEY("</tt><br />\n"), 0);
711                 bn = bq;
712         }
713
714         for (i = bq; i < bn; i++)                               
715                 StrBufAppendBufPlain(Target, HKEY("</blockquote>"), 0);
716
717         StrBufAppendBufPlain(Target, HKEY("</i><br />"), 0);
718 #ifdef HAVE_ICONV
719         if (ic != (iconv_t)(-1) ) {
720                 iconv_close(ic);
721         }
722 #endif
723         FreeStrBuf(&Mime->Data);
724         Mime->Data = Target;
725
726 }
727
728 void render_MAIL_html(wc_mime_attachment *Mime, StrBuf *RawData)
729 {
730         StrBuf *Buf;
731         /* HTML is fun, but we've got to strip it first */
732         Buf = NewStrBufPlain(NULL, StrLength(Mime->Data));
733         output_html(ChrPtr(Mime->Charset), 
734                     (WC->wc_view == VIEW_WIKI ? 1 : 0), 
735                     StrToi(Mime->PartNum), 
736                     Mime->Data, Buf);
737         FreeStrBuf(&Mime->Data);
738         Mime->Data = Buf;
739 }
740
741 void render_MAIL_UNKNOWN(wc_mime_attachment *Mime, StrBuf *RawData)
742 {
743         /* Unknown weirdness */
744 ////    wprintf(_("I don't know how to display %s"), Msg->MsgBody->ContentType);
745         wprintf("<br />\n");
746 }
747
748
749
750
751
752
753
754 /*
755  * Look for URL's embedded in a buffer and make them linkable.  We use a
756  * target window in order to keep the Citadel session in its own window.
757  */
758 void UrlizeText(StrBuf* Target, StrBuf *Source, StrBuf *WrkBuf)
759 {
760         int len, UrlLen, Offset, TrailerLen;
761         const char *start, *end, *pos;
762         
763         FlushStrBuf(Target);
764
765         start = NULL;
766         len = StrLength(Source);
767         end = ChrPtr(Source) + len;
768         for (pos = ChrPtr(Source); (pos < end) && (start == NULL); ++pos) {
769                 if (!strncasecmp(pos, "http://", 7))
770                         start = pos;
771                 if (!strncasecmp(pos, "ftp://", 6))
772                         start = pos;
773         }
774
775         if (start == NULL) {
776                 StrBufAppendBuf(Target, Source, 0);
777                 return;
778         }
779         FlushStrBuf(WrkBuf);
780
781         for (pos = ChrPtr(Source) + len; pos > start; --pos) {
782                 if (  (!isprint(*pos))
783                    || (isspace(*pos))
784                    || (*pos == '{')
785                    || (*pos == '}')
786                    || (*pos == '|')
787                    || (*pos == '\\')
788                    || (*pos == '^')
789                    || (*pos == '[')
790                    || (*pos == ']')
791                    || (*pos == '`')
792                    || (*pos == '<')
793                    || (*pos == '>')
794                    || (*pos == '(')
795                    || (*pos == ')')
796                 ) {
797                         end = pos;
798                 }
799         }
800         
801         UrlLen = end - start;
802         StrBufAppendBufPlain(WrkBuf, start, UrlLen, 0);
803
804         Offset = start - ChrPtr(Source);
805         if (Offset != 0)
806                 StrBufAppendBufPlain(Target, ChrPtr(Source), Offset, 0);
807         StrBufAppendPrintf(Target, "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
808                            LB, QU, ChrPtr(WrkBuf), QU, QU, TARGET, 
809                            QU, RB, ChrPtr(WrkBuf), LB, RB);
810
811         TrailerLen = len - (end - start);
812         if (TrailerLen > 0)
813                 StrBufAppendBufPlain(Target, end, TrailerLen, 0);
814 }
815 void url(char *buf, size_t bufsize)
816 {
817         int len, UrlLen, Offset, TrailerLen, outpos;
818         char *start, *end, *pos;
819         char urlbuf[SIZ];
820         char outbuf[SIZ];
821
822         start = NULL;
823         len = strlen(buf);
824         if (len > bufsize) {
825                 lprintf(1, "URL: content longer than buffer!");
826                 return;
827         }
828         end = buf + len;
829         for (pos = buf; (pos < end) && (start == NULL); ++pos) {
830                 if (!strncasecmp(pos, "http://", 7))
831                         start = pos;
832                 if (!strncasecmp(pos, "ftp://", 6))
833                         start = pos;
834         }
835
836         if (start == NULL)
837                 return;
838
839         for (pos = buf+len; pos > start; --pos) {
840                 if (  (!isprint(*pos))
841                    || (isspace(*pos))
842                    || (*pos == '{')
843                    || (*pos == '}')
844                    || (*pos == '|')
845                    || (*pos == '\\')
846                    || (*pos == '^')
847                    || (*pos == '[')
848                    || (*pos == ']')
849                    || (*pos == '`')
850                    || (*pos == '<')
851                    || (*pos == '>')
852                    || (*pos == '(')
853                    || (*pos == ')')
854                 ) {
855                         end = pos;
856                 }
857         }
858         
859         UrlLen = end - start;
860         if (UrlLen > sizeof(urlbuf)){
861                 lprintf(1, "URL: content longer than buffer!");
862                 return;
863         }
864         memcpy(urlbuf, start, UrlLen);
865         urlbuf[UrlLen] = '\0';
866
867         Offset = start - buf;
868         if ((Offset != 0) && (Offset < sizeof(outbuf)))
869                 memcpy(outbuf, buf, Offset);
870         outpos = snprintf(&outbuf[Offset], sizeof(outbuf) - Offset,  
871                           "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
872                           LB, QU, urlbuf, QU, QU, TARGET, QU, RB, urlbuf, LB, RB);
873         if (outpos >= sizeof(outbuf) - Offset) {
874                 lprintf(1, "URL: content longer than buffer!");
875                 return;
876         }
877
878         TrailerLen = len - (end - start);
879         if (TrailerLen > 0)
880                 memcpy(outbuf + Offset + outpos, end, TrailerLen);
881         if (Offset + outpos + TrailerLen > bufsize) {
882                 lprintf(1, "URL: content longer than buffer!");
883                 return;
884         }
885         memcpy (buf, outbuf, Offset + outpos + TrailerLen);
886         *(buf + Offset + outpos + TrailerLen) = '\0';
887 }
888
889
890 /**
891  * \brief Turn a vCard "n" (name) field into something displayable.
892  * \param name the name field to convert
893  */
894 void vcard_n_prettyize(char *name)
895 {
896         char *original_name;
897         int i, j, len;
898
899         original_name = strdup(name);
900         len = strlen(original_name);
901         for (i=0; i<5; ++i) {
902                 if (len > 0) {
903                         if (original_name[len-1] == ' ') {
904                                 original_name[--len] = 0;
905                         }
906                         if (original_name[len-1] == ';') {
907                                 original_name[--len] = 0;
908                         }
909                 }
910         }
911         strcpy(name, "");
912         j=0;
913         for (i=0; i<len; ++i) {
914                 if (original_name[i] == ';') {
915                         name[j++] = ',';
916                         name[j++] = ' ';                        
917                 }
918                 else {
919                         name[j++] = original_name[i];
920                 }
921         }
922         name[j] = '\0';
923         free(original_name);
924 }
925
926
927
928
929 /**
930  * \brief preparse a vcard name
931  * display_vcard() calls this after parsing the textual vCard into
932  * our 'struct vCard' data object.
933  * This gets called instead of display_parsed_vcard() if we are only looking
934  * to extract the person's name instead of displaying the card.
935  * \param v the vcard to retrieve the name from
936  * \param storename where to put the name at
937  */
938 void fetchname_parsed_vcard(struct vCard *v, char *storename) {
939         char *name;
940
941         strcpy(storename, "");
942
943         name = vcard_get_prop(v, "n", 1, 0, 0);
944         if (name != NULL) {
945                 strcpy(storename, name);
946                 /* vcard_n_prettyize(storename); */
947         }
948
949 }
950
951
952
953 /**
954  * \brief html print a vcard
955  * display_vcard() calls this after parsing the textual vCard into
956  * our 'struct vCard' data object.
957  *
958  * Set 'full' to nonzero to display the full card, otherwise it will only
959  * show a summary line.
960  *
961  * This code is a bit ugly, so perhaps an explanation is due: we do this
962  * in two passes through the vCard fields.  On the first pass, we process
963  * fields we understand, and then render them in a pretty fashion at the
964  * end.  Then we make a second pass, outputting all the fields we don't
965  * understand in a simple two-column name/value format.
966  * \param v the vCard to display
967  * \param full display all items of the vcard?
968  * \param msgnum Citadel message pointer
969  */
970 void display_parsed_vcard(struct vCard *v, int full, long msgnum) {
971         int i, j;
972         char buf[SIZ];
973         char *name;
974         int is_qp = 0;
975         int is_b64 = 0;
976         char *thisname, *thisvalue;
977         char firsttoken[SIZ];
978         int pass;
979
980         char fullname[SIZ];
981         char title[SIZ];
982         char org[SIZ];
983         char phone[SIZ];
984         char mailto[SIZ];
985
986         strcpy(fullname, "");
987         strcpy(phone, "");
988         strcpy(mailto, "");
989         strcpy(title, "");
990         strcpy(org, "");
991
992         if (!full) {
993                 wprintf("<TD>");
994                 name = vcard_get_prop(v, "fn", 1, 0, 0);
995                 if (name != NULL) {
996                         escputs(name);
997                 }
998                 else if (name = vcard_get_prop(v, "n", 1, 0, 0), name != NULL) {
999                         strcpy(fullname, name);
1000                         vcard_n_prettyize(fullname);
1001                         escputs(fullname);
1002                 }
1003                 else {
1004                         wprintf("&nbsp;");
1005                 }
1006                 wprintf("</TD>");
1007                 return;
1008         }
1009
1010         wprintf("<div align=center>"
1011                 "<table bgcolor=#aaaaaa width=50%%>");
1012         for (pass=1; pass<=2; ++pass) {
1013
1014                 if (v->numprops) for (i=0; i<(v->numprops); ++i) {
1015                         int len;
1016                         thisname = strdup(v->prop[i].name);
1017                         extract_token(firsttoken, thisname, 0, ';', sizeof firsttoken);
1018         
1019                         for (j=0; j<num_tokens(thisname, ';'); ++j) {
1020                                 extract_token(buf, thisname, j, ';', sizeof buf);
1021                                 if (!strcasecmp(buf, "encoding=quoted-printable")) {
1022                                         is_qp = 1;
1023                                         remove_token(thisname, j, ';');
1024                                 }
1025                                 if (!strcasecmp(buf, "encoding=base64")) {
1026                                         is_b64 = 1;
1027                                         remove_token(thisname, j, ';');
1028                                 }
1029                         }
1030                         
1031                         len = strlen(v->prop[i].value);
1032                         /* if we have some untagged QP, detect it here. */
1033                         if (!is_qp && (strstr(v->prop[i].value, "=?")!=NULL))
1034                                 utf8ify_rfc822_string(v->prop[i].value);
1035
1036                         if (is_qp) {
1037                                 // %ff can become 6 bytes in utf8 
1038                                 thisvalue = malloc(len * 2 + 3); 
1039                                 j = CtdlDecodeQuotedPrintable(
1040                                         thisvalue, v->prop[i].value,
1041                                         len);
1042                                 thisvalue[j] = 0;
1043                         }
1044                         else if (is_b64) {
1045                                 // ff will become one byte..
1046                                 thisvalue = malloc(len + 50);
1047                                 CtdlDecodeBase64(
1048                                         thisvalue, v->prop[i].value,
1049                                         strlen(v->prop[i].value) );
1050                         }
1051                         else {
1052                                 thisvalue = strdup(v->prop[i].value);
1053                         }
1054         
1055                         /** Various fields we may encounter ***/
1056         
1057                         /** N is name, but only if there's no FN already there */
1058                         if (!strcasecmp(firsttoken, "n")) {
1059                                 if (IsEmptyStr(fullname)) {
1060                                         strcpy(fullname, thisvalue);
1061                                         vcard_n_prettyize(fullname);
1062                                 }
1063                         }
1064         
1065                         /** FN (full name) is a true 'display name' field */
1066                         else if (!strcasecmp(firsttoken, "fn")) {
1067                                 strcpy(fullname, thisvalue);
1068                         }
1069
1070                         /** title */
1071                         else if (!strcasecmp(firsttoken, "title")) {
1072                                 strcpy(title, thisvalue);
1073                         }
1074         
1075                         /** organization */
1076                         else if (!strcasecmp(firsttoken, "org")) {
1077                                 strcpy(org, thisvalue);
1078                         }
1079         
1080                         else if (!strcasecmp(firsttoken, "email")) {
1081                                 size_t len;
1082                                 if (!IsEmptyStr(mailto)) strcat(mailto, "<br />");
1083                                 strcat(mailto,
1084                                         "<a href=\"display_enter"
1085                                         "?force_room=_MAIL_?recp=");
1086
1087                                 len = strlen(mailto);
1088                                 urlesc(&mailto[len], SIZ - len, "\"");
1089                                 len = strlen(mailto);
1090                                 urlesc(&mailto[len], SIZ - len,  fullname);
1091                                 len = strlen(mailto);
1092                                 urlesc(&mailto[len], SIZ - len, "\" <");
1093                                 len = strlen(mailto);
1094                                 urlesc(&mailto[len], SIZ - len, thisvalue);
1095                                 len = strlen(mailto);
1096                                 urlesc(&mailto[len], SIZ - len, ">");
1097
1098                                 strcat(mailto, "\">");
1099                                 len = strlen(mailto);
1100                                 stresc(mailto+len, SIZ - len, thisvalue, 1, 1);
1101                                 strcat(mailto, "</A>");
1102                         }
1103                         else if (!strcasecmp(firsttoken, "tel")) {
1104                                 if (!IsEmptyStr(phone)) strcat(phone, "<br />");
1105                                 strcat(phone, thisvalue);
1106                                 for (j=0; j<num_tokens(thisname, ';'); ++j) {
1107                                         extract_token(buf, thisname, j, ';', sizeof buf);
1108                                         if (!strcasecmp(buf, "tel"))
1109                                                 strcat(phone, "");
1110                                         else if (!strcasecmp(buf, "work"))
1111                                                 strcat(phone, _(" (work)"));
1112                                         else if (!strcasecmp(buf, "home"))
1113                                                 strcat(phone, _(" (home)"));
1114                                         else if (!strcasecmp(buf, "cell"))
1115                                                 strcat(phone, _(" (cell)"));
1116                                         else {
1117                                                 strcat(phone, " (");
1118                                                 strcat(phone, buf);
1119                                                 strcat(phone, ")");
1120                                         }
1121                                 }
1122                         }
1123                         else if (!strcasecmp(firsttoken, "adr")) {
1124                                 if (pass == 2) {
1125                                         wprintf("<TR><TD>");
1126                                         wprintf(_("Address:"));
1127                                         wprintf("</TD><TD>");
1128                                         for (j=0; j<num_tokens(thisvalue, ';'); ++j) {
1129                                                 extract_token(buf, thisvalue, j, ';', sizeof buf);
1130                                                 if (!IsEmptyStr(buf)) {
1131                                                         escputs(buf);
1132                                                         if (j<3) wprintf("<br />");
1133                                                         else wprintf(" ");
1134                                                 }
1135                                         }
1136                                         wprintf("</TD></TR>\n");
1137                                 }
1138                         }
1139                         /* else if (!strcasecmp(firsttoken, "photo") && full && pass == 2) { 
1140                                 // Only output on second pass
1141                                 wprintf("<tr><td>");
1142                                 wprintf(_("Photo:"));
1143                                 wprintf("</td><td>");
1144                                 wprintf("<img src=\"/vcardphoto/%ld/\" alt=\"Contact photo\"/>",msgnum);
1145                                 wprintf("</td></tr>\n");
1146                         } */
1147                         else if (!strcasecmp(firsttoken, "version")) {
1148                                 /* ignore */
1149                         }
1150                         else if (!strcasecmp(firsttoken, "rev")) {
1151                                 /* ignore */
1152                         }
1153                         else if (!strcasecmp(firsttoken, "label")) {
1154                                 /* ignore */
1155                         }
1156                         else {
1157
1158                                 /*** Don't show extra fields.  They're ugly.
1159                                 if (pass == 2) {
1160                                         wprintf("<TR><TD>");
1161                                         escputs(thisname);
1162                                         wprintf("</TD><TD>");
1163                                         escputs(thisvalue);
1164                                         wprintf("</TD></TR>\n");
1165                                 }
1166                                 ***/
1167                         }
1168         
1169                         free(thisname);
1170                         free(thisvalue);
1171                 }
1172         
1173                 if (pass == 1) {
1174                         wprintf("<TR BGCOLOR=\"#AAAAAA\">"
1175                         "<TD COLSPAN=2 BGCOLOR=\"#FFFFFF\">"
1176                         "<IMG ALIGN=CENTER src=\"static/viewcontacts_48x.gif\">"
1177                         "<FONT SIZE=+1><B>");
1178                         escputs(fullname);
1179                         wprintf("</B></FONT>");
1180                         if (!IsEmptyStr(title)) {
1181                                 wprintf("<div align=right>");
1182                                 escputs(title);
1183                                 wprintf("</div>");
1184                         }
1185                         if (!IsEmptyStr(org)) {
1186                                 wprintf("<div align=right>");
1187                                 escputs(org);
1188                                 wprintf("</div>");
1189                         }
1190                         wprintf("</TD></TR>\n");
1191                 
1192                         if (!IsEmptyStr(phone)) {
1193                                 wprintf("<tr><td>");
1194                                 wprintf(_("Telephone:"));
1195                                 wprintf("</td><td>%s</td></tr>\n", phone);
1196                         }
1197                         if (!IsEmptyStr(mailto)) {
1198                                 wprintf("<tr><td>");
1199                                 wprintf(_("E-mail:"));
1200                                 wprintf("</td><td>%s</td></tr>\n", mailto);
1201                         }
1202                 }
1203
1204         }
1205
1206         wprintf("</table></div>\n");
1207 }
1208
1209
1210
1211 /**
1212  * \brief  Display a textual vCard
1213  * (Converts to a vCard object and then calls the actual display function)
1214  * Set 'full' to nonzero to display the whole card instead of a one-liner.
1215  * Or, if "storename" is non-NULL, just store the person's name in that
1216  * buffer instead of displaying the card at all.
1217  * \param vcard_source the buffer containing the vcard text
1218  * \param alpha what???
1219  * \param full should we usse all lines?
1220  * \param storename where to store???
1221  * \param msgnum Citadel message pointer
1222  */
1223 void display_vcard(char *vcard_source, char alpha, int full, char *storename, 
1224         long msgnum) {
1225         struct vCard *v;
1226         char *name;
1227         char buf[SIZ];
1228         char this_alpha = 0;
1229
1230         v = vcard_load(vcard_source);
1231         if (v == NULL) return;
1232
1233         name = vcard_get_prop(v, "n", 1, 0, 0);
1234         if (name != NULL) {
1235                 utf8ify_rfc822_string(name);
1236                 strcpy(buf, name);
1237                 this_alpha = buf[0];
1238         }
1239
1240         if (storename != NULL) {
1241                 fetchname_parsed_vcard(v, storename);
1242         }
1243         else if (       (alpha == 0)
1244                         || ((isalpha(alpha)) && (tolower(alpha) == tolower(this_alpha)) )
1245                         || ((!isalpha(alpha)) && (!isalpha(this_alpha)))
1246                 ) {
1247                 display_parsed_vcard(v, full,msgnum);
1248         }
1249
1250         vcard_free(v);
1251 }
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264 /*
1265  * I wanna SEE that message!
1266  *
1267  * msgnum               Message number to display
1268  * printable_view       Nonzero to display a printable view
1269  * section              Optional for encapsulated message/rfc822 submessage
1270  */
1271 void read_message(long msgnum, int printable_view, char *section) {
1272         struct wcsession *WCC = WC;
1273         StrBuf *Buf;
1274         StrBuf *Token;
1275         message_summary *Msg;
1276         headereval *Hdr;
1277         void *vHdr;
1278         char buf[SIZ];
1279         struct attach_link *attach_links = NULL;
1280         int num_attach_links = 0;
1281 //      char mime_submessages[256] = "";
1282         char reply_references[1024] = "";
1283         int nhdr = 0;
1284         int i = 0;
1285         int Done = 0;
1286         int state=0;
1287         char vcard_partnum[256] = "";
1288         char cal_partnum[256] = "";
1289         char *part_source = NULL;
1290         char msg4_partnum[32] = "";
1291
1292 ////    strcpy(mime_submessages, "");
1293
1294         Buf = NewStrBuf();
1295         serv_printf("MSG4 %ld|%s", msgnum, section);
1296         StrBuf_ServGetln(Buf);
1297         if (GetServerStatus(Buf, NULL) != 1) {
1298                 wprintf("<strong>");
1299                 wprintf(_("ERROR:"));
1300                 wprintf("</strong> %s<br />\n", &buf[4]);
1301                 FreeStrBuf(&Buf);
1302                 return;
1303         }
1304         svputlong("MsgPrintable", printable_view);
1305         /** begin everythingamundo table */
1306
1307
1308         Token = NewStrBuf();
1309         Msg = (message_summary *)malloc(sizeof(message_summary));
1310         memset(Msg, 0, sizeof(message_summary));
1311         Msg->MsgBody.length=-1;
1312         while ((StrBuf_ServGetln(Buf)>=0) && !Done) {
1313                 if ( (StrLength(Buf)==3) && 
1314                     !strcmp(ChrPtr(Buf), "000")) 
1315                 {
1316                         Done = 1;
1317                         if (state < 2) {
1318                                 wprintf("<i>");
1319                                 wprintf(_("unexpected end of message"));
1320                                 wprintf(" (1)</i><br /><br />\n");
1321                                 wprintf("</div>\n");
1322                                 FreeStrBuf(&Buf);
1323                                 FreeStrBuf(&Token);
1324                                 DestroyMessageSummary(Msg);
1325                                 return;
1326                         }
1327                         else {
1328                                 break;
1329                         }
1330                 }
1331                 switch (state) {
1332                 case 0:/* Citadel Message Headers */
1333                         if (StrLength(Buf) == 0) {
1334                                 state ++;
1335                                 break;
1336                         }
1337                         StrBufExtract_token(Token, Buf, 0, '=');
1338                         StrBufCutLeft(Buf, StrLength(Token) + 1);
1339                         
1340                         lprintf(1, ":: [%s] = [%s]\n", ChrPtr(Token), ChrPtr(Buf));
1341                         if (GetHash(MsgHeaderHandler, SKEY(Token), &vHdr) &&
1342                             (vHdr != NULL)) {
1343                                 Hdr = (headereval*)vHdr;
1344                                 Hdr->evaluator(Msg, Buf);
1345                                 if (Hdr->Type == 1) {
1346                                         state++;
1347                                 }
1348                         }
1349                         else lprintf(1, "don't know how to handle message header[%s]\n", ChrPtr(Token));
1350                         break;
1351                 case 1:/* Message Mime Header */
1352                         if (StrLength(Buf) == 0) {
1353                                 state++;
1354                                 if (Msg->MsgBody.ContentType == NULL)
1355                                         /* end of header or no header? */
1356                                         Msg->MsgBody.ContentType = NewStrBufPlain(HKEY("text/plain"));
1357                                  /* usual end of mime header */
1358                         }
1359                         else
1360                         {
1361                                 StrBufExtract_token(Token, Buf, 0, ':');
1362                                 if (StrLength(Token) > 0) {
1363                                         StrBufCutLeft(Buf, StrLength(Token) + 1);
1364                                         lprintf(1, ":: [%s] = [%s]\n", ChrPtr(Token), ChrPtr(Buf));
1365                                         if (GetHash(MsgHeaderHandler, SKEY(Token), &vHdr) &&
1366                                             (vHdr != NULL)) {
1367                                                 Hdr = (headereval*)vHdr;
1368                                                 Hdr->evaluator(Msg, Buf);
1369                                         }
1370                                         break;
1371                                 }
1372                         }
1373                 case 2: /* Message Body */
1374                         
1375                         if (Msg->MsgBody.length > 0) {
1376                                 StrBuf_ServGetBLOB(Msg->MsgBody.Data, Msg->MsgBody.length);
1377                                 state ++;
1378                                         /// todo: check next line, if not 000, append following lines
1379                         }
1380                         else if (1){
1381                                 if (StrLength(Msg->MsgBody.Data) > 0)
1382                                         StrBufAppendBufPlain(Msg->MsgBody.Data, "\n", 1, 0);
1383                                 StrBufAppendBuf(Msg->MsgBody.Data, Buf, 0);
1384                         }
1385                         break;
1386                 case 3:
1387                         StrBufAppendBuf(Msg->MsgBody.Data, Buf, 0);
1388                         break;
1389                 }
1390         }
1391
1392
1393         if (GetHash(MimeRenderHandler, SKEY(Msg->MsgBody.ContentType), &vHdr) &&
1394             (vHdr != NULL)) {
1395                 RenderMimeFunc Render;
1396                 Render = (RenderMimeFunc)vHdr;
1397                 Render(&Msg->MsgBody, NULL);
1398         }
1399
1400
1401         if (StrLength(Msg->reply_references)> 0) {
1402                 /* Trim down excessively long lists of thread references.  We eliminate the
1403                  * second one in the list so that the thread root remains intact.
1404                  */
1405                 int rrtok = num_tokens(ChrPtr(Msg->reply_references), '|');
1406                 int rrlen = StrLength(Msg->reply_references);
1407                 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
1408                         remove_token(reply_references, 1, '|');////todo
1409                 }
1410         }
1411
1412         /* Generate a reply-to address */
1413         if (StrLength(Msg->Rfca) > 0) {
1414                 if (Msg->reply_to == NULL)
1415                         Msg->reply_to = NewStrBuf();
1416                 if (StrLength(Msg->from) > 0) {
1417                         StrBufPrintf(Msg->reply_to, "%s <%s>", ChrPtr(Msg->from), ChrPtr(Msg->Rfca));
1418                 }
1419                 else {
1420                         FlushStrBuf(Msg->reply_to);
1421                         StrBufAppendBuf(Msg->reply_to, Msg->Rfca, 0);
1422                 }
1423         }
1424         else 
1425         {
1426                 if ((StrLength(Msg->OtherNode)>0) && 
1427                     (strcasecmp(ChrPtr(Msg->OtherNode), serv_info.serv_nodename)) &&
1428                     (strcasecmp(ChrPtr(Msg->OtherNode), serv_info.serv_humannode)) ) 
1429                 {
1430                         if (Msg->reply_to == NULL)
1431                                 Msg->reply_to = NewStrBuf();
1432                         StrBufPrintf(Msg->reply_to, 
1433                                      "%s @ %s",
1434                                      ChrPtr(Msg->from), 
1435                                      ChrPtr(Msg->OtherNode));
1436                 }
1437                 else {
1438                         if (Msg->reply_to == NULL)
1439                                 Msg->reply_to = NewStrBuf();
1440                         FlushStrBuf(Msg->reply_to);
1441                         StrBufAppendBuf(Msg->reply_to, Msg->from, 0);
1442                 }
1443         }
1444 ///TODO: why this?
1445         if (nhdr == 1) {
1446                 wprintf("****");
1447         }
1448
1449         if (StrLength(Msg->cccc)> 0) {
1450                 StrBuf *tmp;
1451                 tmp = Msg->cccc;
1452                 Msg->cccc = Buf;
1453                 StrBuf_RFC822_to_Utf8(Msg->cccc, tmp, WCC->DefaultCharset);
1454                 Buf = tmp;
1455         }
1456         if (StrLength(Msg->subj)> 0) {
1457                 StrBuf *tmp;
1458                 tmp = Msg->subj;
1459                 Msg->subj = Buf;
1460                 StrBuf_RFC822_to_Utf8(Msg->subj, tmp, WCC->DefaultCharset);
1461                 Buf = tmp;
1462         }
1463
1464         DoTemplate(HKEY("view_message"), NULL, Msg, CTX_MAILSUM);
1465
1466
1467
1468 //// put message renderer lookup here.
1469 ///ENDBODY:     /* If there are attached submessages, display them now... */
1470 ///
1471 ///     if ( (!IsEmptyStr(mime_submessages)) && (!section[0]) ) {
1472 ///             for (i=0; i<num_tokens(mime_submessages, '|'); ++i) {
1473 ///                     extract_token(buf, mime_submessages, i, '|', sizeof buf);
1474 ///                     /** use printable_view to suppress buttons */
1475 ///                     wprintf("<blockquote>");
1476 ///                     read_message(msgnum, 1, buf);
1477 ///                     wprintf("</blockquote>");
1478 ///             }
1479 ///     }
1480
1481
1482         /* Afterwards, offer links to download attachments 'n' such */
1483         if ( (num_attach_links > 0) && (!section[0]) ) {
1484                 for (i=0; i<num_attach_links; ++i) {
1485                         if (strcasecmp(attach_links[i].partnum, msg4_partnum)) {
1486                                 wprintf("%s", attach_links[i].html);
1487                         }
1488                 }
1489         }
1490
1491         /* Handler for vCard parts */
1492         if (!IsEmptyStr(vcard_partnum)) {
1493                 part_source = load_mimepart(msgnum, vcard_partnum);
1494                 if (part_source != NULL) {
1495
1496                         /** If it's my vCard I can edit it */
1497                         if (    (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
1498                                 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
1499                                 || (WC->wc_view == VIEW_ADDRESSBOOK)
1500                         ) {
1501                                 wprintf("<a href=\"edit_vcard?msgnum=%ld&partnum=%s\">",
1502                                         msgnum, vcard_partnum);
1503                                 wprintf("[%s]</a>", _("edit"));
1504                         }
1505
1506                         /* In all cases, display the full card */
1507                         display_vcard(part_source, 0, 1, NULL,msgnum);
1508                 }
1509         }
1510
1511         /* Handler for calendar parts */
1512         if (!IsEmptyStr(cal_partnum)) {
1513                 part_source = load_mimepart(msgnum, cal_partnum);
1514                 if (part_source != NULL) {
1515                         cal_process_attachment(part_source, msgnum, cal_partnum);
1516                 }
1517         }
1518
1519         if (part_source) {
1520                 free(part_source);
1521                 part_source = NULL;
1522         }
1523
1524         wprintf("</div>\n");
1525
1526         /* end everythingamundo table */
1527         if (!printable_view) {
1528                 wprintf("</div>\n");
1529         }
1530
1531         if (num_attach_links > 0) {
1532                 free(attach_links);
1533         }
1534
1535 }
1536
1537
1538
1539 /*
1540  * Unadorned HTML output of an individual message, suitable
1541  * for placing in a hidden iframe, for printing, or whatever
1542  *
1543  * msgnum_as_string == Message number, as a string instead of as a long int
1544  */
1545 void embed_message(void) {
1546         long msgnum = 0L;
1547
1548         msgnum = StrTol(WC->UrlFragment1);
1549         read_message(msgnum, 0, "");
1550 }
1551
1552
1553 /*
1554  * Printable view of a message
1555  *
1556  * msgnum_as_string == Message number, as a string instead of as a long int
1557  */
1558 void print_message(void) {
1559         long msgnum = 0L;
1560
1561         msgnum = StrTol(WC->UrlFragment1);
1562         output_headers(0, 0, 0, 0, 0, 0);
1563
1564         hprintf("Content-type: text/html\r\n"
1565                 "Server: %s\r\n"
1566                 "Connection: close\r\n",
1567                 PACKAGE_STRING);
1568         begin_burst();
1569
1570         wprintf("\r\n<html>\n<head><title>");
1571         escputs(WC->wc_fullname);
1572         wprintf("</title></head>\n"
1573                 "<body onLoad=\" window.print(); window.close(); \">\n"
1574         );
1575         
1576         read_message(msgnum, 1, "");
1577
1578         wprintf("\n</body></html>\n\n");
1579         wDumpContent(0);
1580 }
1581
1582 /* 
1583  * Mobile browser view of message
1584  *
1585  * @param msg_num_as_string Message number as a string instead of as a long int 
1586  */
1587 void mobile_message_view(void) {
1588   long msgnum = 0L;
1589   msgnum = StrTol(WC->UrlFragment1);
1590   output_headers(1, 0, 0, 0, 0, 1);
1591   begin_burst();
1592   do_template("msgcontrols", NULL);
1593   read_message(msgnum,1, "");
1594   wDumpContent(0);
1595 }
1596
1597 /**
1598  * \brief Display a message's headers
1599  *
1600  * \param msgnum_as_string Message number, as a string instead of as a long int
1601  */
1602 void display_headers(void) {
1603         long msgnum = 0L;
1604         char buf[1024];
1605
1606         msgnum = StrTol(WC->UrlFragment1);
1607         output_headers(0, 0, 0, 0, 0, 0);
1608
1609         hprintf("Content-type: text/plain\r\n"
1610                 "Server: %s\r\n"
1611                 "Connection: close\r\n",
1612                 PACKAGE_STRING);
1613         begin_burst();
1614
1615         serv_printf("MSG2 %ld|3", msgnum);
1616         serv_getln(buf, sizeof buf);
1617         if (buf[0] == '1') {
1618                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1619                         wprintf("%s\n", buf);
1620                 }
1621         }
1622
1623         wDumpContent(0);
1624 }
1625
1626
1627
1628 /**
1629  * \brief Read message in simple, JavaScript-embeddable form for 'forward'
1630  *      or 'reply quoted' operations.
1631  *
1632  * NOTE: it is VITALLY IMPORTANT that we output no single-quotes or linebreaks
1633  *       in this function.  Doing so would throw a JavaScript error in the
1634  *       'supplied text' argument to the editor.
1635  *
1636  * \param msgnum Message number of the message we want to quote
1637  * \param forward_attachments Nonzero if we want attachments to be forwarded
1638  */
1639 void pullquote_message(long msgnum, int forward_attachments, int include_headers) {
1640         char buf[SIZ];
1641         char mime_partnum[256];
1642         char mime_filename[256];
1643         char mime_content_type[256];
1644         char mime_charset[256];
1645         char mime_disposition[256];
1646         int mime_length;
1647         char *attachments = NULL;
1648         char *ptr = NULL;
1649         int num_attachments = 0;
1650         struct wc_attachment *att, *aptr;
1651         char m_subject[1024];
1652         char from[256];
1653         char node[256];
1654         char rfca[256];
1655         char to[256];
1656         char reply_to[512];
1657         char now[256];
1658         int format_type = 0;
1659         int nhdr = 0;
1660         int bq = 0;
1661         int i = 0;
1662 #ifdef HAVE_ICONV
1663         iconv_t ic = (iconv_t)(-1) ;
1664         char *ibuf;                /**< Buffer of characters to be converted */
1665         char *obuf;                /**< Buffer for converted characters      */
1666         size_t ibuflen;    /**< Length of input buffer         */
1667         size_t obuflen;    /**< Length of output buffer       */
1668         char *osav;                /**< Saved pointer to output buffer       */
1669 #endif
1670
1671         strcpy(from, "");
1672         strcpy(node, "");
1673         strcpy(rfca, "");
1674         strcpy(reply_to, "");
1675         strcpy(mime_content_type, "text/plain");
1676         strcpy(mime_charset, "us-ascii");
1677
1678         serv_printf("MSG4 %ld", msgnum);
1679         serv_getln(buf, sizeof buf);
1680         if (buf[0] != '1') {
1681                 wprintf(_("ERROR:"));
1682                 wprintf("%s<br />", &buf[4]);
1683                 return;
1684         }
1685
1686         strcpy(m_subject, "");
1687
1688         while (serv_getln(buf, sizeof buf), strcasecmp(buf, "text")) {
1689                 if (!strcmp(buf, "000")) {
1690                         wprintf("%s (3)", _("unexpected end of message"));
1691                         return;
1692                 }
1693                 if (include_headers) {
1694                         if (!strncasecmp(buf, "nhdr=yes", 8))
1695                                 nhdr = 1;
1696                         if (nhdr == 1)
1697                                 buf[0] = '_';
1698                         if (!strncasecmp(buf, "type=", 5))
1699                                 format_type = atoi(&buf[5]);
1700                         if (!strncasecmp(buf, "from=", 5)) {
1701                                 strcpy(from, &buf[5]);
1702                                 wprintf(_("from "));
1703                                 utf8ify_rfc822_string(from);
1704                                 msgescputs(from);
1705                         }
1706                         if (!strncasecmp(buf, "subj=", 5)) {
1707                                 strcpy(m_subject, &buf[5]);
1708                         }
1709                         if ((!strncasecmp(buf, "hnod=", 5))
1710                             && (strcasecmp(&buf[5], serv_info.serv_humannode))) {
1711                                 wprintf("(%s) ", &buf[5]);
1712                         }
1713                         if ((!strncasecmp(buf, "room=", 5))
1714                             && (strcasecmp(&buf[5], WC->wc_roomname))
1715                             && (!IsEmptyStr(&buf[5])) ) {
1716                                 wprintf(_("in "));
1717                                 wprintf("%s&gt; ", &buf[5]);
1718                         }
1719                         if (!strncasecmp(buf, "rfca=", 5)) {
1720                                 strcpy(rfca, &buf[5]);
1721                                 wprintf("&lt;");
1722                                 msgescputs(rfca);
1723                                 wprintf("&gt; ");
1724                         }
1725                         if (!strncasecmp(buf, "node=", 5)) {
1726                                 strcpy(node, &buf[5]);
1727                                 if ( ((WC->room_flags & QR_NETWORK)
1728                                 || ((strcasecmp(&buf[5], serv_info.serv_nodename)
1729                                 && (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
1730                                 && (IsEmptyStr(rfca))
1731                                 ) {
1732                                         wprintf("@%s ", &buf[5]);
1733                                 }
1734                         }
1735                         if (!strncasecmp(buf, "rcpt=", 5)) {
1736                                 wprintf(_("to "));
1737                                 strcpy(to, &buf[5]);
1738                                 utf8ify_rfc822_string(to);
1739                                 wprintf("%s ", to);
1740                         }
1741                         if (!strncasecmp(buf, "time=", 5)) {
1742                                 webcit_fmt_date(now, atol(&buf[5]), 0);
1743                                 wprintf("%s ", now);
1744                         }
1745                 }
1746
1747                 /**
1748                  * Save attachment info for later.  We can't start downloading them
1749                  * yet because we're in the middle of a server transaction.
1750                  */
1751                 if (!strncasecmp(buf, "part=", 5)) {
1752                         ptr = malloc( (strlen(buf) + ((attachments != NULL) ? strlen(attachments) : 0)) ) ;
1753                         if (ptr != NULL) {
1754                                 ++num_attachments;
1755                                 sprintf(ptr, "%s%s\n",
1756                                         ((attachments != NULL) ? attachments : ""),
1757                                         &buf[5]
1758                                 );
1759                                 free(attachments);
1760                                 attachments = ptr;
1761                                 lprintf(9, "attachments=<%s>\n", attachments);
1762                         }
1763                 }
1764
1765         }
1766
1767         if (include_headers) {
1768                 wprintf("<br>");
1769
1770                 utf8ify_rfc822_string(m_subject);
1771                 if (!IsEmptyStr(m_subject)) {
1772                         wprintf(_("Subject:"));
1773                         wprintf(" ");
1774                         msgescputs(m_subject);
1775                         wprintf("<br />");
1776                 }
1777
1778                 /**
1779                  * Begin body
1780                  */
1781                 wprintf("<br />");
1782         }
1783
1784         /**
1785          * Learn the content type
1786          */
1787         strcpy(mime_content_type, "text/plain");
1788         while (serv_getln(buf, sizeof buf), (!IsEmptyStr(buf))) {
1789                 if (!strcmp(buf, "000")) {
1790                         wprintf("%s (4)", _("unexpected end of message"));
1791                         goto ENDBODY;
1792                 }
1793                 if (!strncasecmp(buf, "Content-type: ", 14)) {
1794                         int len;
1795                         safestrncpy(mime_content_type, &buf[14],
1796                                 sizeof(mime_content_type));
1797                         for (i=0; i<strlen(mime_content_type); ++i) {
1798                                 if (!strncasecmp(&mime_content_type[i], "charset=", 8)) {
1799                                         safestrncpy(mime_charset, &mime_content_type[i+8],
1800                                                 sizeof mime_charset);
1801                                 }
1802                         }
1803                         len = strlen(mime_content_type);
1804                         for (i=0; i<len; ++i) {
1805                                 if (mime_content_type[i] == ';') {
1806                                         mime_content_type[i] = 0;
1807                                         len = i - 1;
1808                                 }
1809                         }
1810                         len = strlen(mime_charset);
1811                         for (i=0; i<len; ++i) {
1812                                 if (mime_charset[i] == ';') {
1813                                         mime_charset[i] = 0;
1814                                         len = i - 1;
1815                                 }
1816                         }
1817                 }
1818         }
1819
1820         /** Set up a character set conversion if we need to (and if we can) */
1821 #ifdef HAVE_ICONV
1822         if ( (strcasecmp(mime_charset, "us-ascii"))
1823            && (strcasecmp(mime_charset, "UTF-8"))
1824            && (strcasecmp(mime_charset, ""))
1825         ) {
1826                 ctdl_iconv_open("UTF-8", mime_charset, &ic);
1827                 if (ic == (iconv_t)(-1) ) {
1828                         lprintf(5, "%s:%d iconv_open(%s, %s) failed: %s\n",
1829                                 __FILE__, __LINE__, "UTF-8", mime_charset, strerror(errno));
1830                 }
1831         }
1832 #endif
1833
1834         /** Messages in legacy Citadel variformat get handled thusly... */
1835         if (!strcasecmp(mime_content_type, "text/x-citadel-variformat")) {
1836                 pullquote_fmout();
1837         }
1838
1839         /* Boring old 80-column fixed format text gets handled this way... */
1840         else if (!strcasecmp(mime_content_type, "text/plain")) {
1841                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1842                         int len;
1843                         len = strlen(buf);
1844                         if ((len > 0) && (buf[len-1] == '\n')) buf[--len] = 0;
1845                         if ((len > 0) && (buf[len-1] == '\r')) buf[--len] = 0;
1846
1847 #ifdef HAVE_ICONV
1848                         if (ic != (iconv_t)(-1) ) {
1849                                 ibuf = buf;
1850                                 ibuflen = len;
1851                                 obuflen = SIZ;
1852                                 obuf = (char *) malloc(obuflen);
1853                                 osav = obuf;
1854                                 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
1855                                 osav[SIZ-obuflen] = 0;
1856                                 safestrncpy(buf, osav, sizeof buf);
1857                                 free(osav);
1858                         }
1859 #endif
1860
1861                         len = strlen(buf);
1862                         while ((!IsEmptyStr(buf)) && (isspace(buf[len - 1]))) 
1863                                 buf[--len] = 0;
1864                         if ((bq == 0) &&
1865                         ((!strncmp(buf, ">", 1)) || (!strncmp(buf, " >", 2)) )) {
1866                                 wprintf("<blockquote>");
1867                                 bq = 1;
1868                         } else if ((bq == 1) &&
1869                                 (strncmp(buf, ">", 1)) && (strncmp(buf, " >", 2)) ) {
1870                                 wprintf("</blockquote>");
1871                                 bq = 0;
1872                         }
1873                         wprintf("<tt>");
1874                         url(buf, sizeof(buf));
1875                         msgescputs1(buf);
1876                         wprintf("</tt><br />");
1877                 }
1878                 wprintf("</i><br />");
1879         }
1880
1881         /** HTML just gets escaped and stuffed back into the editor */
1882         else if (!strcasecmp(mime_content_type, "text/html")) {
1883                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1884                         strcat(buf, "\n");
1885                         msgescputs(buf);
1886                 }
1887         }//// TODO: charset? utf8?
1888
1889         /** Unknown weirdness ... don't know how to handle this content type */
1890         else {
1891                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) { }
1892         }
1893
1894 ENDBODY:
1895         /** end of body handler */
1896
1897         /*
1898          * If there were attachments, we have to download them and insert them
1899          * into the attachment chain for the forwarded message we are composing.
1900          */
1901         if ( (forward_attachments) && (num_attachments) ) {
1902                 for (i=0; i<num_attachments; ++i) {
1903                         extract_token(buf, attachments, i, '\n', sizeof buf);
1904                         extract_token(mime_filename, buf, 1, '|', sizeof mime_filename);
1905                         extract_token(mime_partnum, buf, 2, '|', sizeof mime_partnum);
1906                         extract_token(mime_disposition, buf, 3, '|', sizeof mime_disposition);
1907                         extract_token(mime_content_type, buf, 4, '|', sizeof mime_content_type);
1908                         mime_length = extract_int(buf, 5);
1909
1910                         /*
1911                          * tracing  ... uncomment if necessary
1912                          *
1913                          */
1914                         lprintf(9, "fwd filename: %s\n", mime_filename);
1915                         lprintf(9, "fwd partnum : %s\n", mime_partnum);
1916                         lprintf(9, "fwd conttype: %s\n", mime_content_type);
1917                         lprintf(9, "fwd dispose : %s\n", mime_disposition);
1918                         lprintf(9, "fwd length  : %d\n", mime_length);
1919
1920                         if ( (!strcasecmp(mime_disposition, "inline"))
1921                            || (!strcasecmp(mime_disposition, "attachment")) ) {
1922                 
1923                                 /* Create an attachment struct from this mime part... */
1924                                 att = malloc(sizeof(struct wc_attachment));
1925                                 memset(att, 0, sizeof(struct wc_attachment));
1926                                 att->length = mime_length;
1927                                 strcpy(att->content_type, mime_content_type);
1928                                 strcpy(att->filename, mime_filename);
1929                                 att->next = NULL;
1930                                 att->data = load_mimepart(msgnum, mime_partnum);
1931                 
1932                                 /* And add it to the list. */
1933                                 if (WC->first_attachment == NULL) {
1934                                         WC->first_attachment = att;
1935                                 }
1936                                 else {
1937                                         aptr = WC->first_attachment;
1938                                         while (aptr->next != NULL) aptr = aptr->next;
1939                                         aptr->next = att;
1940                                 }
1941                         }
1942
1943                 }
1944         }
1945
1946 #ifdef HAVE_ICONV
1947         if (ic != (iconv_t)(-1) ) {
1948                 iconv_close(ic);
1949         }
1950 #endif
1951
1952         if (attachments != NULL) {
1953                 free(attachments);
1954         }
1955 }
1956
1957
1958
1959
1960 void EvaluateMimePart(message_summary *Sum, StrBuf *Buf)
1961 {//// paert=; TODO
1962 /*
1963         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
1964         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
1965         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
1966         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
1967         mime_length = extract_int(&buf[5], 5);
1968         
1969         if (  (!strcasecmp(mime_content_type, "text/x-vcard"))
1970               || (!strcasecmp(mime_content_type, "text/vcard")) ) {
1971                 strcpy(vcard_partnum, mime_partnum);
1972         }
1973 */
1974 }
1975
1976 message_summary *ReadOneMessageSummary(StrBuf *RawMessage, const char *DefaultSubject, long MsgNum) 
1977 {
1978         void                 *vEval;
1979         MsgPartEvaluatorFunc  Eval;
1980         message_summary      *Msg;
1981         StrBuf *Buf;
1982         const char *buf;
1983         const char *ebuf;
1984         int nBuf;
1985         long len;
1986         
1987         Buf = NewStrBuf();
1988
1989         serv_printf("MSG0 %ld|1", MsgNum);      /* ask for headers only */
1990         
1991         StrBuf_ServGetln(Buf);
1992         if (GetServerStatus(Buf, NULL) == 1) {
1993                 FreeStrBuf(&Buf);
1994                 return NULL;
1995         }
1996
1997         Msg = (message_summary*)malloc(sizeof(message_summary));
1998         memset(Msg, 0, sizeof(message_summary));
1999         while (len = StrBuf_ServGetln(Buf),
2000                ((len != 3)  ||
2001                 strcmp(ChrPtr(Buf), "000")== 0)){
2002                 buf = ChrPtr(Buf);
2003                 ebuf = strchr(ChrPtr(Buf), '=');
2004                 nBuf = ebuf - buf;
2005                 if (GetHash(MsgEvaluators, buf, nBuf, &vEval) && vEval != NULL) {
2006                         Eval = (MsgPartEvaluatorFunc) vEval;
2007                         StrBufCutLeft(Buf, nBuf + 1);
2008                         Eval(Msg, Buf);
2009                 }
2010                 else lprintf(1, "Don't know how to handle Message Headerline [%s]", ChrPtr(Buf));
2011         }
2012         return Msg;
2013 }
2014
2015
2016 /**
2017  * \brief display the adressbook overview
2018  * \param msgnum the citadel message number
2019  * \param alpha what????
2020  */
2021 void display_addressbook(long msgnum, char alpha) {
2022         //char buf[SIZ];
2023         /* char mime_partnum[SIZ]; */
2024 /*      char mime_filename[SIZ]; */
2025 /*      char mime_content_type[SIZ]; */
2026         ///char mime_disposition[SIZ];
2027         //int mime_length;
2028         char vcard_partnum[SIZ];
2029         char *vcard_source = NULL;
2030         message_summary summ;////TODO: this will leak
2031
2032         memset(&summ, 0, sizeof(summ));
2033         ///safestrncpy(summ.subj, _("(no subject)"), sizeof summ.subj);
2034 ///Load Message headers
2035 //      Msg = 
2036         if (!IsEmptyStr(vcard_partnum)) {
2037                 vcard_source = load_mimepart(msgnum, vcard_partnum);
2038                 if (vcard_source != NULL) {
2039
2040                         /** Display the summary line */
2041                         display_vcard(vcard_source, alpha, 0, NULL,msgnum);
2042
2043                         /** If it's my vCard I can edit it */
2044                         if (    (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
2045                                 || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
2046                                 || (WC->wc_view == VIEW_ADDRESSBOOK)
2047                         ) {
2048                                 wprintf("<a href=\"edit_vcard?"
2049                                         "msgnum=%ld&partnum=%s\">",
2050                                         msgnum, vcard_partnum);
2051                                 wprintf("[%s]</a>", _("edit"));
2052                         }
2053
2054                         free(vcard_source);
2055                 }
2056         }
2057
2058 }
2059
2060
2061
2062 /**
2063  * \brief  If it's an old "Firstname Lastname" style record, try to convert it.
2064  * \param namebuf name to analyze, reverse if nescessary
2065  */
2066 void lastfirst_firstlast(char *namebuf) {
2067         char firstname[SIZ];
2068         char lastname[SIZ];
2069         int i;
2070
2071         if (namebuf == NULL) return;
2072         if (strchr(namebuf, ';') != NULL) return;
2073
2074         i = num_tokens(namebuf, ' ');
2075         if (i < 2) return;
2076
2077         extract_token(lastname, namebuf, i-1, ' ', sizeof lastname);
2078         remove_token(namebuf, i-1, ' ');
2079         strcpy(firstname, namebuf);
2080         sprintf(namebuf, "%s; %s", lastname, firstname);
2081 }
2082
2083 /**
2084  * \brief fetch what??? name
2085  * \param msgnum the citadel message number
2086  * \param namebuf where to put the name in???
2087  */
2088 void fetch_ab_name(message_summary *Msg, char *namebuf) {
2089         char buf[SIZ];
2090         char mime_partnum[SIZ];
2091         char mime_filename[SIZ];
2092         char mime_content_type[SIZ];
2093         char mime_disposition[SIZ];
2094         int mime_length;
2095         char vcard_partnum[SIZ];
2096         char *vcard_source = NULL;
2097         int i, len;
2098         message_summary summ;/// TODO this will lak
2099
2100         if (namebuf == NULL) return;
2101         strcpy(namebuf, "");
2102
2103         memset(&summ, 0, sizeof(summ));
2104         //////safestrncpy(summ.subj, "(no subject)", sizeof summ.subj);
2105
2106         sprintf(buf, "MSG0 %ld|0", Msg->msgnum);        /** unfortunately we need the mime info now */
2107         serv_puts(buf);
2108         serv_getln(buf, sizeof buf);
2109         if (buf[0] != '1') return;
2110
2111         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
2112                 if (!strncasecmp(buf, "part=", 5)) {
2113                         extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
2114                         extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
2115                         extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
2116                         extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
2117                         mime_length = extract_int(&buf[5], 5);
2118
2119                         if (  (!strcasecmp(mime_content_type, "text/x-vcard"))
2120                            || (!strcasecmp(mime_content_type, "text/vcard")) ) {
2121                                 strcpy(vcard_partnum, mime_partnum);
2122                         }
2123
2124                 }
2125         }
2126
2127         if (!IsEmptyStr(vcard_partnum)) {
2128                 vcard_source = load_mimepart(Msg->msgnum, vcard_partnum);
2129                 if (vcard_source != NULL) {
2130
2131                         /* Grab the name off the card */
2132                         display_vcard(vcard_source, 0, 0, namebuf, Msg->msgnum);
2133
2134                         free(vcard_source);
2135                 }
2136         }
2137
2138         lastfirst_firstlast(namebuf);
2139         striplt(namebuf);
2140         len = strlen(namebuf);
2141         for (i=0; i<len; ++i) {
2142                 if (namebuf[i] != ';') return;
2143         }
2144         strcpy(namebuf, _("(no name)"));
2145 }
2146
2147
2148
2149 /**
2150  * \brief Record compare function for sorting address book indices
2151  * \param ab1 adressbook one
2152  * \param ab2 adressbook two
2153  */
2154 int abcmp(const void *ab1, const void *ab2) {
2155         return(strcasecmp(
2156                 (((const struct addrbookent *)ab1)->ab_name),
2157                 (((const struct addrbookent *)ab2)->ab_name)
2158         ));
2159 }
2160
2161
2162 /**
2163  * \brief Helper function for do_addrbook_view()
2164  * Converts a name into a three-letter tab label
2165  * \param tabbuf the tabbuffer to add name to
2166  * \param name the name to add to the tabbuffer
2167  */
2168 void nametab(char *tabbuf, long len, char *name) {
2169         stresc(tabbuf, len, name, 0, 0);
2170         tabbuf[0] = toupper(tabbuf[0]);
2171         tabbuf[1] = tolower(tabbuf[1]);
2172         tabbuf[2] = tolower(tabbuf[2]);
2173         tabbuf[3] = 0;
2174 }
2175
2176
2177 /**
2178  * \brief Render the address book using info we gathered during the scan
2179  * \param addrbook the addressbook to render
2180  * \param num_ab the number of the addressbook
2181  */
2182 void do_addrbook_view(struct addrbookent *addrbook, int num_ab) {
2183         int i = 0;
2184         int displayed = 0;
2185         int bg = 0;
2186         static int NAMESPERPAGE = 60;
2187         int num_pages = 0;
2188         int tabfirst = 0;
2189         char tabfirst_label[64];
2190         int tablast = 0;
2191         char tablast_label[64];
2192         char this_tablabel[64];
2193         int page = 0;
2194         char **tablabels;
2195
2196         if (num_ab == 0) {
2197                 wprintf("<br /><br /><br /><div align=\"center\"><i>");
2198                 wprintf(_("This address book is empty."));
2199                 wprintf("</i></div>\n");
2200                 return;
2201         }
2202
2203         if (num_ab > 1) {
2204                 qsort(addrbook, num_ab, sizeof(struct addrbookent), abcmp);
2205         }
2206
2207         num_pages = (num_ab / NAMESPERPAGE) + 1;
2208
2209         tablabels = malloc(num_pages * sizeof (char *));
2210         if (tablabels == NULL) {
2211                 wprintf("<br /><br /><br /><div align=\"center\"><i>");
2212                 wprintf(_("An internal error has occurred."));
2213                 wprintf("</i></div>\n");
2214                 return;
2215         }
2216
2217         for (i=0; i<num_pages; ++i) {
2218                 tabfirst = i * NAMESPERPAGE;
2219                 tablast = tabfirst + NAMESPERPAGE - 1;
2220                 if (tablast > (num_ab - 1)) tablast = (num_ab - 1);
2221                 nametab(tabfirst_label, 64, addrbook[tabfirst].ab_name);
2222                 nametab(tablast_label, 64, addrbook[tablast].ab_name);
2223                 sprintf(this_tablabel, "%s&nbsp;-&nbsp;%s", tabfirst_label, tablast_label);
2224                 tablabels[i] = strdup(this_tablabel);
2225         }
2226
2227         tabbed_dialog(num_pages, tablabels);
2228         page = (-1);
2229
2230         for (i=0; i<num_ab; ++i) {
2231
2232                 if ((i / NAMESPERPAGE) != page) {       /* New tab */
2233                         page = (i / NAMESPERPAGE);
2234                         if (page > 0) {
2235                                 wprintf("</tr></table>\n");
2236                                 end_tab(page-1, num_pages);
2237                         }
2238                         begin_tab(page, num_pages);
2239                         wprintf("<table border=0 cellspacing=0 cellpadding=3 width=100%%>\n");
2240                         displayed = 0;
2241                 }
2242
2243                 if ((displayed % 4) == 0) {
2244                         if (displayed > 0) {
2245                                 wprintf("</tr>\n");
2246                         }
2247                         bg = 1 - bg;
2248                         wprintf("<tr bgcolor=\"#%s\">",
2249                                 (bg ? "DDDDDD" : "FFFFFF")
2250                         );
2251                 }
2252         
2253                 wprintf("<td>");
2254
2255                 wprintf("<a href=\"readfwd?startmsg=%ld?is_singlecard=1",
2256                         addrbook[i].ab_msgnum);
2257                 wprintf("?maxmsgs=1?is_summary=0?alpha=%s\">", bstr("alpha"));
2258                 vcard_n_prettyize(addrbook[i].ab_name);
2259                 escputs(addrbook[i].ab_name);
2260                 wprintf("</a></td>\n");
2261                 ++displayed;
2262         }
2263
2264         /* Placeholders for empty columns at end */
2265         if ((num_ab % 4) != 0) {
2266                 for (i=0; i<(4-(num_ab % 4)); ++i) {
2267                         wprintf("<td>&nbsp;</td>");
2268                 }
2269         }
2270
2271         wprintf("</tr></table>\n");
2272         end_tab((num_pages-1), num_pages);
2273
2274         begin_tab(num_pages, num_pages);
2275         /* FIXME there ought to be something here */
2276         end_tab(num_pages, num_pages);
2277
2278         for (i=0; i<num_pages; ++i) {
2279                 free(tablabels[i]);
2280         }
2281         free(tablabels);
2282 }
2283
2284
2285
2286 /*
2287  * load message pointers from the server for a "read messages" operation
2288  *
2289  * servcmd:             the citadel command to send to the citserver
2290  * with_headers:        also include some of the headers with the message numbers (more expensive)
2291  */
2292 int load_msg_ptrs(char *servcmd, int with_headers)
2293 {
2294         struct wcsession *WCC = WC;
2295         message_summary *Msg;
2296         StrBuf *Buf, *Buf2;
2297         ///char buf[1024];
2298         ///time_t datestamp;
2299         //char fullname[128];
2300         //char nodename[128];
2301         //char inetaddr[128];
2302         //char subject[1024];
2303         ///char *ptr;
2304         int nummsgs;
2305         ////int sbjlen;
2306         int maxload = 0;
2307         long len;
2308
2309         ////int num_summ_alloc = 0;
2310
2311         if (WCC->summ != NULL) {
2312                 if (WCC->summ != NULL)
2313                         DeleteHash(&WCC->summ);
2314         }
2315         WCC->summ = NewHash(1, Flathash);
2316         nummsgs = 0;
2317         maxload = 1000;/// TODO
2318         
2319         Buf = NewStrBuf();
2320         serv_puts(servcmd);
2321         StrBuf_ServGetln(Buf);
2322         if (GetServerStatus(Buf, NULL) != 1) {
2323                 FreeStrBuf(&Buf);
2324                 return (nummsgs);
2325         }
2326 // TODO                         if (with_headers) { //// TODO: Have Attachments?
2327         Buf2 = NewStrBuf();
2328         while (len = StrBuf_ServGetln(Buf),
2329                ((len != 3)  ||
2330                 strcmp(ChrPtr(Buf), "000")!= 0))
2331         {
2332                 if (nummsgs < maxload) {
2333                         Msg = (message_summary*)malloc(sizeof(message_summary));
2334                         memset(Msg, 0, sizeof(message_summary));
2335
2336                         Msg->msgnum = StrBufExtract_long(Buf, 0, '|');
2337                         Msg->date = StrBufExtract_long(Buf, 1, '|');
2338
2339                         Msg->from = NewStrBufPlain(NULL, StrLength(Buf));
2340                         StrBufExtract_token(Buf2, Buf, 2, '|');
2341                         if (StrLength(Buf2) != 0) {
2342                                 /** Handle senders with RFC2047 encoding */
2343                                 StrBuf_RFC822_to_Utf8(Msg->from, Buf2, WCC->DefaultCharset);
2344                         }
2345                         
2346                         /** Nodename */
2347                         StrBufExtract_token(Buf2, Buf, 3, '|');
2348                         if ((StrLength(Buf2) !=0 ) &&
2349                             ( ((WCC->room_flags & QR_NETWORK)
2350                                || ((strcasecmp(ChrPtr(Buf2), serv_info.serv_nodename)
2351                                     && (strcasecmp(ChrPtr(Buf2), serv_info.serv_fqdn)))))))
2352                         {
2353                                 StrBufAppendBufPlain(Msg->from, HKEY(" @ "), 0);
2354                                 StrBufAppendBuf(Msg->from, Buf2, 0);
2355                         }
2356
2357                         /** Not used:
2358                         StrBufExtract_token(Msg->inetaddr, Buf, 4, '|');
2359                         */
2360
2361                         Msg->subj = NewStrBufPlain(NULL, StrLength(Buf));
2362                         StrBufExtract_token(Buf2,  Buf, 5, '|');
2363                         if (StrLength(Buf2) == 0)
2364                                 StrBufAppendBufPlain(Msg->subj, _("(no subj)"), 0, -1);
2365                         else {
2366                                 StrBuf_RFC822_to_Utf8(Msg->subj, Buf2, WCC->DefaultCharset);
2367                                 if ((StrLength(Msg->subj) > 75) && 
2368                                     (StrBuf_Utf8StrLen(Msg->subj) > 75)) {
2369                                         StrBuf_Utf8StrCut(Msg->subj, 72);
2370                                         StrBufAppendBufPlain(Msg->subj, HKEY("..."), 0);
2371                                 }
2372                         }
2373
2374
2375                         if ((StrLength(Msg->from) > 25) && 
2376                             (StrBuf_Utf8StrLen(Msg->from) > 25)) {
2377                                 StrBuf_Utf8StrCut(Msg->from, 23);
2378                                 StrBufAppendBufPlain(Msg->from, HKEY("..."), 0);
2379                         }
2380                         Put(WCC->summ, (const char*)&Msg->msgnum, sizeof(Msg->msgnum), Msg, DestroyMessageSummary);
2381                 }
2382                 nummsgs++;
2383         }
2384         FreeStrBuf(&Buf2);
2385         FreeStrBuf(&Buf);
2386         return (nummsgs);
2387 }
2388
2389
2390
2391
2392
2393
2394 /*
2395  * command loop for reading messages
2396  *
2397  * Set oper to "readnew" or "readold" or "readfwd" or "headers"
2398  */
2399 void readloop(char *oper)
2400 {
2401         void *vMsg;
2402         message_summary *Msg;
2403         char cmd[256] = "";
2404         char buf[SIZ];
2405         char old_msgs[SIZ];
2406         int a, b;
2407         int nummsgs;
2408         long startmsg;
2409         int maxmsgs;
2410         long *displayed_msgs = NULL;
2411         int num_displayed = 0;
2412         int is_summary = 0;
2413         int is_addressbook = 0;
2414         int is_singlecard = 0;
2415         int is_calendar = 0;
2416         struct calview calv;
2417         int is_tasks = 0;
2418         int is_notes = 0;
2419         int is_bbview = 0;
2420         int lo, hi;
2421         int lowest_displayed = (-1);
2422         int highest_displayed = 0;
2423         struct addrbookent *addrbook = NULL;
2424         int num_ab = 0;
2425         const StrBuf *sortby = NULL;
2426         //SortByEnum 
2427         int SortBy = eRDate;
2428         const StrBuf *sortpref_value;
2429         int bbs_reverse = 0;
2430         struct wcsession *WCC = WC;     /* This is done to make it run faster; WC is a function */
2431         HashPos *at;
2432         const char *HashKey;
2433         long HKLen;
2434
2435         if (WCC->wc_view == VIEW_WIKI) {
2436                 sprintf(buf, "wiki?room=%s&page=home", WCC->wc_roomname);
2437                 http_redirect(buf);
2438                 return;
2439         }
2440
2441         startmsg = lbstr("startmsg");
2442         maxmsgs = ibstr("maxmsgs");
2443         is_summary = (ibstr("is_summary") && !WCC->is_mobile);
2444         if (maxmsgs == 0) maxmsgs = DEFAULT_MAXMSGS;
2445
2446         sortpref_value = get_room_pref("sort");
2447
2448         sortby = sbstr("sortby");
2449         if ( (!IsEmptyStr(ChrPtr(sortby))) && 
2450              (strcasecmp(ChrPtr(sortby), ChrPtr(sortpref_value)) != 0)) {
2451                 set_room_pref("sort", NewStrBufDup(sortby), 1);
2452                 sortpref_value = NULL;
2453                 sortpref_value = sortby;
2454         }
2455
2456         SortBy = StrToESort(sortpref_value);
2457         /* message board sort */
2458         if (SortBy == eReverse) {
2459                 bbs_reverse = 1;
2460         }
2461         else {
2462                 bbs_reverse = 0;
2463         }
2464
2465         output_headers(1, 1, 1, 0, 0, 0);
2466
2467         /*
2468          * When in summary mode, always show ALL messages instead of just
2469          * new or old.  Otherwise, show what the user asked for.
2470          */
2471         if (!strcmp(oper, "readnew")) {
2472                 strcpy(cmd, "MSGS NEW");
2473         }
2474         else if (!strcmp(oper, "readold")) {
2475                 strcpy(cmd, "MSGS OLD");
2476         }
2477         else if (!strcmp(oper, "do_search")) {
2478                 snprintf(cmd, sizeof(cmd), "MSGS SEARCH|%s", bstr("query"));
2479         }
2480         else {
2481                 strcpy(cmd, "MSGS ALL");
2482         }
2483
2484         if ((WCC->wc_view == VIEW_MAILBOX) && (maxmsgs > 1) && !WCC->is_mobile) {
2485                 is_summary = 1;
2486                 if (!strcmp(oper, "do_search")) {
2487                         snprintf(cmd, sizeof(cmd), "MSGS SEARCH|%s", bstr("query"));
2488                 }
2489                 else {
2490                         strcpy(cmd, "MSGS ALL");
2491                 }
2492         }
2493
2494         if ((WCC->wc_view == VIEW_ADDRESSBOOK) && (maxmsgs > 1)) {
2495                 is_addressbook = 1;
2496                 if (!strcmp(oper, "do_search")) {
2497                         snprintf(cmd, sizeof(cmd), "MSGS SEARCH|%s", bstr("query"));
2498                 }
2499                 else {
2500                         strcpy(cmd, "MSGS ALL");
2501                 }
2502                 maxmsgs = 9999999;
2503         }
2504
2505         if (is_summary) {                       /**< fetch header summary */
2506                 snprintf(cmd, sizeof(cmd), "MSGS %s|%s||1",
2507                         (!strcmp(oper, "do_search") ? "SEARCH" : "ALL"),
2508                         (!strcmp(oper, "do_search") ? bstr("query") : "")
2509                 );
2510                 startmsg = 1;
2511                 maxmsgs = 9999999;
2512         } 
2513         if (WCC->is_mobile) {
2514                 maxmsgs = 20;
2515                 snprintf(cmd, sizeof(cmd), "MSGS %s|%s||1",
2516                         (!strcmp(oper, "do_search") ? "SEARCH" : "ALL"),
2517                         (!strcmp(oper, "do_search") ? bstr("query") : "")
2518                 );
2519                 SortBy =  eRDate;
2520         }
2521
2522         /*
2523          * Are we doing a summary view?  If so, we need to know old messages
2524          * and new messages, so we can do that pretty boldface thing for the
2525          * new messages.
2526          */
2527         strcpy(old_msgs, "");
2528         if ((is_summary) || (WCC->wc_default_view == VIEW_CALENDAR) || WCC->is_mobile){
2529                 serv_puts("GTSN");
2530                 serv_getln(buf, sizeof buf);
2531                 if (buf[0] == '2') {
2532                         strcpy(old_msgs, &buf[4]);
2533                 }
2534         }
2535
2536         is_singlecard = ibstr("is_singlecard");
2537
2538         if (WCC->wc_default_view == VIEW_CALENDAR) {            /**< calendar */
2539                 is_calendar = 1;
2540                 strcpy(cmd, "MSGS ALL|||1");
2541                 maxmsgs = 32767;
2542                 parse_calendar_view_request(&calv);
2543         }
2544         if (WCC->wc_default_view == VIEW_TASKS) {               /**< tasks */
2545                 is_tasks = 1;
2546                 strcpy(cmd, "MSGS ALL");
2547                 maxmsgs = 32767;
2548         }
2549         if (WCC->wc_default_view == VIEW_NOTES) {               /**< notes */
2550                 is_notes = 1;
2551                 strcpy(cmd, "MSGS ALL");
2552                 maxmsgs = 32767;
2553         }
2554
2555         if (is_notes) {
2556                 wprintf("<div id=\"new_notes_here\"></div>\n");
2557         }
2558
2559         nummsgs = load_msg_ptrs(cmd, (is_summary || WCC->is_mobile));
2560         if (nummsgs == 0) {
2561
2562                 if ((!is_tasks) && (!is_calendar) && (!is_notes) && (!is_addressbook)) {
2563                         wprintf("<div align=\"center\"><br /><em>");
2564                         if (!strcmp(oper, "readnew")) {
2565                                 wprintf(_("No new messages."));
2566                         } else if (!strcmp(oper, "readold")) {
2567                                 wprintf(_("No old messages."));
2568                         } else {
2569                                 wprintf(_("No messages here."));
2570                         }
2571                         wprintf("</em><br /></div>\n");
2572                 }
2573
2574                 goto DONE;
2575         }
2576
2577         if ((is_summary) || (WCC->wc_default_view == VIEW_CALENDAR) || WCC->is_mobile){
2578                 void *vMsg;
2579                 message_summary *Msg;
2580
2581                 at = GetNewHashPos();
2582                 while (GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
2583                         /** Are you a new message, or an old message? */
2584                         Msg = (message_summary*) vMsg;
2585                         if (is_summary) {
2586                                 if (is_msg_in_mset(old_msgs, Msg->msgnum)) {
2587                                         Msg->is_new = 0;
2588                                 }
2589                                 else {
2590                                         Msg->is_new = 1;
2591                                 }
2592                         }
2593                 }
2594                 DeleteHashPos(&at);
2595         }
2596
2597         if (startmsg == 0L) {
2598                 if (bbs_reverse) {
2599                         startmsg = WCC->msgarr[(nummsgs >= maxmsgs) ? (nummsgs - maxmsgs) : 0];
2600                 }
2601                 else {
2602                         startmsg = WCC->msgarr[0];
2603                 }
2604         }
2605
2606         if (is_summary || WCC->is_mobile) {
2607                 SortByPayload(WCC->summ, SortFuncs[SortBy]);
2608         }
2609
2610         if (is_summary) {
2611
2612                 wprintf("<script language=\"javascript\" type=\"text/javascript\">"
2613                         " document.onkeydown = CtdlMsgListKeyPress;     "
2614                         " if (document.layers) {                        "
2615                         "       document.captureEvents(Event.KEYPRESS); "
2616                         " }                                             "
2617                         "</script>\n"
2618                 );
2619
2620                 /** note that Date and Delete are now in the same column */
2621                 wprintf("<div id=\"message_list_hdr\">"
2622                         "<div class=\"fix_scrollbar_bug\">"
2623                         "<table cellspacing=0 style=\"width:100%%\">"
2624                         "<tr>"
2625                 );
2626                 wprintf("<th width=%d%%>%s <a href=\"readfwd?startmsg=1?maxmsgs=9999999?is_summary=1?sortby=%s\"><img border=\"0\" src=\"%s\" /></a> </th>\n"
2627                         "<th width=%d%%>%s <a href=\"readfwd?startmsg=1?maxmsgs=9999999?is_summary=1?sortby=%s\"><img border=\"0\" src=\"%s\" /></a> </th>\n"
2628                         "<th width=%d%%>%s <a href=\"readfwd?startmsg=1?maxmsgs=9999999?is_summary=1?sortby=%s\"><img border=\"0\" src=\"%s\" /></a> \n"
2629                         "&nbsp;"
2630                         "<input type=\"submit\" name=\"delete_button\" id=\"delbutton\" "
2631                         " onClick=\"CtdlDeleteSelectedMessages(event)\" "
2632                         " value=\"%s\">"
2633                         "</th>"
2634                         "</tr>\n"
2635                         ,
2636                         SUBJ_COL_WIDTH_PCT,
2637                         _("Subject"),
2638                         SortByStrings[SubjectInvertSortString[SortBy]],
2639                         SortIcons[SortSubjectToIcon[SortBy]],
2640                         SENDER_COL_WIDTH_PCT,
2641                         _("Sender"),
2642                         SortByStrings[SenderInvertSortString[SortBy]],
2643                         SortIcons[SortSenderToIcon[SortBy]],
2644                         DATE_PLUS_BUTTONS_WIDTH_PCT,
2645                         _("Date"),
2646                         SortByStrings[DateInvertSortString[SortBy]],
2647                         SortIcons[SortDateToIcon[SortBy]],
2648                         _("Delete")
2649                 );
2650                 wprintf("</table></div></div>\n");
2651                 wprintf("<div id=\"message_list\">"
2652
2653                         "<div class=\"fix_scrollbar_bug\">\n"
2654                         "<table class=\"mailbox_summary\" id=\"summary_headers\" "
2655                         "cellspacing=0 style=\"width:100%%;-moz-user-select:none;\">"
2656                 );
2657         } else if (WCC->is_mobile) {
2658                 wprintf("<div id=\"message_list\">");
2659         }
2660
2661
2662         /**
2663          * Set the "is_bbview" variable if it appears that we are looking at
2664          * a classic bulletin board view.
2665          */
2666         if ((!is_tasks) && (!is_calendar) && (!is_addressbook)
2667               && (!is_notes) && (!is_singlecard) && (!is_summary)) {
2668                 is_bbview = 1;
2669         }
2670
2671         /**
2672          * If we're not currently looking at ALL requested
2673          * messages, then display the selector bar
2674          */
2675         if (is_bbview) {
2676                 /** begin bbview scroller */
2677                 wprintf("<form name=\"msgomatictop\" class=\"selector_top\" > \n <p>");
2678                 wprintf(_("Reading #"));//// TODO this isn't used, should it? : , lowest_displayed, highest_displayed);
2679
2680                 wprintf("<select name=\"whichones\" size=\"1\" "
2681                         "OnChange=\"location.href=msgomatictop.whichones.options"
2682                         "[selectedIndex].value\">\n");
2683
2684                 if (bbs_reverse) {
2685                         for (b=nummsgs-1; b>=0; b = b - maxmsgs) {
2686                                 hi = b + 1;
2687                                 lo = b - maxmsgs + 2;
2688                                 if (lo < 1) lo = 1;
2689                                 wprintf("<option %s value="
2690                                         "\"%s"
2691                                         "&startmsg=%ld"
2692                                         "&maxmsgs=%d"
2693                                         "&is_summary=%d\">"
2694                                         "%d-%d</option> \n",
2695                                         ((WCC->msgarr[lo-1] == startmsg) ? "selected" : ""),
2696                                         oper,
2697                                         WCC->msgarr[lo-1],
2698                                         maxmsgs,
2699                                         is_summary,
2700                                         hi, lo);
2701                         }
2702                 }
2703                 else {
2704                         for (b=0; b<nummsgs; b = b + maxmsgs) {
2705                                 lo = b + 1;
2706                                 hi = b + maxmsgs + 1;
2707                                 if (hi > nummsgs) hi = nummsgs;
2708                                 wprintf("<option %s value="
2709                                         "\"%s"
2710                                         "&startmsg=%ld"
2711                                         "&maxmsgs=%d"
2712                                         "&is_summary=%d\">"
2713                                         "%d-%d</option> \n",
2714                                         ((WCC->msgarr[b] == startmsg) ? "selected" : ""),
2715                                         oper,
2716                                         WCC->msgarr[lo-1],
2717                                         maxmsgs,
2718                                         is_summary,
2719                                         lo, hi);
2720                         }
2721                 }
2722
2723                 wprintf("<option value=\"%s?startmsg=%ld"
2724                         "&maxmsgs=9999999&is_summary=%d\">",
2725                         oper,
2726                         WCC->msgarr[0], is_summary);
2727                 wprintf(_("All"));
2728                 wprintf("</option>");
2729                 wprintf("</select> ");
2730                 wprintf(_("of %d messages."), nummsgs);
2731
2732                 /** forward/reverse */
2733                 wprintf("<input type=\"radio\" %s name=\"direction\" value=\"\""
2734                         "OnChange=\"location.href='%s?sortby=forward'\"",  
2735                         (bbs_reverse ? "" : "checked"),
2736                         oper
2737                 );
2738                 wprintf(">");
2739                 wprintf(_("oldest to newest"));
2740                 wprintf("&nbsp;&nbsp;&nbsp;&nbsp;");
2741
2742                 wprintf("<input type=\"radio\" %s name=\"direction\" value=\"\""
2743                         "OnChange=\"location.href='%s?sortby=reverse'\"", 
2744                         (bbs_reverse ? "checked" : ""),
2745                         oper
2746                 );
2747                 wprintf(">");
2748                 wprintf(_("newest to oldest"));
2749                 wprintf("\n");
2750         
2751                 wprintf("</p></form>\n");
2752                 /** end bbview scroller */
2753         }
2754                         
2755         at = GetNewHashPos();
2756         while (GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
2757                 Msg = (message_summary*) vMsg;          
2758                 if ((Msg->msgnum >= startmsg) && (num_displayed < maxmsgs)) {
2759                                 
2760                         /** Display the message */
2761                         if (is_summary) {
2762                                 DoTemplate(HKEY("section_mailsummary"), NULL, Msg, CTX_MAILSUM);
2763                         }
2764                         else if (is_addressbook) {
2765                                 fetch_ab_name(Msg, buf);
2766                                 ++num_ab;
2767                                 addrbook = realloc(addrbook,
2768                                                    (sizeof(struct addrbookent) * num_ab) );
2769                                 safestrncpy(addrbook[num_ab-1].ab_name, buf,
2770                                             sizeof(addrbook[num_ab-1].ab_name));
2771                                 addrbook[num_ab-1].ab_msgnum = Msg->msgnum;
2772                         }
2773                         else if (is_calendar) {
2774                                 load_calendar_item(Msg, Msg->is_new, &calv);
2775                         }
2776                         else if (is_tasks) {
2777                                 display_task(Msg, Msg->is_new);
2778                         }
2779                         else if (is_notes) {
2780                                 display_note(Msg, Msg->is_new);
2781                         } else if (WCC->is_mobile) {
2782                                 DoTemplate(HKEY("section_mailsummary"), NULL, Msg, CTX_MAILSUM);
2783                         }
2784                         else {
2785                                 if (displayed_msgs == NULL) {
2786                                         displayed_msgs = malloc(sizeof(long) *
2787                                                                 (maxmsgs<nummsgs ? maxmsgs : nummsgs));
2788                                 }
2789                                 displayed_msgs[num_displayed] = Msg->msgnum;
2790                         }
2791                         
2792                         if (lowest_displayed < 0) lowest_displayed = a;
2793                         highest_displayed = a;
2794                         
2795                         ++num_displayed;
2796                 }
2797         }
2798         DeleteHashPos(&at);
2799
2800         /** Output loop */
2801         if (displayed_msgs != NULL) {
2802                 if (bbs_reverse) {
2803                         qsort(displayed_msgs, num_displayed, sizeof(long), longcmp_r);
2804                 }
2805
2806                 /** if we do a split bbview in the future, begin messages div here */
2807
2808                 for (a=0; a<num_displayed; ++a) {
2809                         read_message(displayed_msgs[a], 0, "");
2810                 }
2811
2812                 /** if we do a split bbview in the future, end messages div here */
2813
2814                 free(displayed_msgs);
2815                 displayed_msgs = NULL;
2816         }
2817
2818         if (is_summary) {
2819                 wprintf("</table>"
2820                         "</div>\n");                    /**< end of 'fix_scrollbar_bug' div */
2821                 wprintf("</div>");                      /**< end of 'message_list' div */
2822                 
2823                 /** Here's the grab-it-to-resize-the-message-list widget */
2824                 wprintf("<div id=\"resize_msglist\" "
2825                         "onMouseDown=\"CtdlResizeMsgListMouseDown(event)\">"
2826                         "<div class=\"fix_scrollbar_bug\"> <hr>"
2827                         "</div></div>\n"
2828                 );
2829
2830                 wprintf("<div id=\"preview_pane\">");   /**< The preview pane will initially be empty */
2831         } else if (WCC->is_mobile) {
2832                 wprintf("</div>");
2833         }
2834
2835         /**
2836          * Bump these because although we're thinking in zero base, the user
2837          * is a drooling idiot and is thinking in one base.
2838          */
2839         ++lowest_displayed;
2840         ++highest_displayed;
2841
2842         /**
2843          * If we're not currently looking at ALL requested
2844          * messages, then display the selector bar
2845          */
2846         if (is_bbview) {
2847                 /** begin bbview scroller */
2848                 wprintf("<form name=\"msgomatic\" class=\"selector_bottom\" > \n <p>");
2849                 wprintf(_("Reading #")); /// TODO: this isn't used: , lowest_displayed, highest_displayed);
2850
2851                 wprintf("<select name=\"whichones\" size=\"1\" "
2852                         "OnChange=\"location.href=msgomatic.whichones.options"
2853                         "[selectedIndex].value\">\n");
2854
2855                 if (bbs_reverse) {
2856                         for (b=nummsgs-1; b>=0; b = b - maxmsgs) {
2857                                 hi = b + 1;
2858                                 lo = b - maxmsgs + 2;
2859                                 if (lo < 1) lo = 1;
2860                                 wprintf("<option %s value="
2861                                         "\"%s"
2862                                         "&startmsg=%ld"
2863                                         "&maxmsgs=%d"
2864                                         "&is_summary=%d\">"
2865                                         "%d-%d</option> \n",
2866                                         ((WCC->msgarr[lo-1] == startmsg) ? "selected" : ""),
2867                                         oper,
2868                                         WCC->msgarr[lo-1],
2869                                         maxmsgs,
2870                                         is_summary,
2871                                         hi, lo);
2872                         }
2873                 }
2874                 else {
2875                         for (b=0; b<nummsgs; b = b + maxmsgs) {
2876                                 lo = b + 1;
2877                                 hi = b + maxmsgs + 1;
2878                                 if (hi > nummsgs) hi = nummsgs;
2879                                 wprintf("<option %s value="
2880                                         "\"%s"
2881                                         "&startmsg=%ld"
2882                                         "&maxmsgs=%d"
2883                                         "&is_summary=%d\">"
2884                                         "%d-%d</option> \n",
2885                                         ((WCC->msgarr[b] == startmsg) ? "selected" : ""),
2886                                         oper,
2887                                         WCC->msgarr[lo-1],
2888                                         maxmsgs,
2889                                         is_summary,
2890                                         lo, hi);
2891                         }
2892                 }
2893
2894                 wprintf("<option value=\"%s&startmsg=%ld"
2895                         "&maxmsgs=9999999&is_summary=%d\">",
2896                         oper,
2897                         WCC->msgarr[0], is_summary);
2898                 wprintf(_("All"));
2899                 wprintf("</option>");
2900                 wprintf("</select> ");
2901                 wprintf(_("of %d messages."), nummsgs);
2902
2903                 /** forward/reverse */
2904                 wprintf("<input type=\"radio\" %s name=\"direction\" value=\"\""
2905                         "OnChange=\"location.href='%s&sortby=forward'\"",  
2906                         (bbs_reverse ? "" : "checked"),
2907                         oper
2908                 );
2909                 wprintf(">");
2910                 wprintf(_("oldest to newest"));
2911                 wprintf("&nbsp;&nbsp;&nbsp;&nbsp;");
2912                 wprintf("<input type=\"radio\" %s name=\"direction\" value=\"\""
2913                         "OnChange=\"location.href='%s&sortby=reverse'\"", 
2914                         (bbs_reverse ? "checked" : ""),
2915                         oper
2916                 );
2917                 wprintf(">");
2918                 wprintf(_("newest to oldest"));
2919                 wprintf("\n");
2920
2921                 wprintf("</p></form>\n");
2922                 /** end bbview scroller */
2923         }
2924         
2925 DONE:
2926         if (is_tasks) {
2927                 do_tasks_view();        /** Render the task list */
2928         }
2929
2930         if (is_calendar) {
2931                 render_calendar_view(&calv);
2932         }
2933
2934         if (is_addressbook) {
2935                 do_addrbook_view(addrbook, num_ab);     /** Render the address book */
2936         }
2937
2938         /** Note: wDumpContent() will output one additional </div> tag. */
2939         wprintf("</div>\n");            /** end of 'content' div */
2940         wDumpContent(1);
2941
2942         /** free the summary */
2943         if (WCC->summ != NULL) {
2944                 DeleteHash(&WCC->summ);
2945         }
2946         if (addrbook != NULL) free(addrbook);
2947 }
2948
2949
2950 /*
2951  * Back end for post_message()
2952  * ... this is where the actual message gets transmitted to the server.
2953  */
2954 void post_mime_to_server(void) {
2955         char top_boundary[SIZ];
2956         char alt_boundary[SIZ];
2957         int is_multipart = 0;
2958         static int seq = 0;
2959         struct wc_attachment *att;
2960         char *encoded;
2961         size_t encoded_length;
2962         size_t encoded_strlen;
2963         char *txtmail = NULL;
2964
2965         sprintf(top_boundary, "Citadel--Multipart--%s--%04x--%04x",
2966                 serv_info.serv_fqdn,
2967                 getpid(),
2968                 ++seq
2969         );
2970         sprintf(alt_boundary, "Citadel--Multipart--%s--%04x--%04x",
2971                 serv_info.serv_fqdn,
2972                 getpid(),
2973                 ++seq
2974         );
2975
2976         /* RFC2045 requires this, and some clients look for it... */
2977         serv_puts("MIME-Version: 1.0");
2978         serv_puts("X-Mailer: " PACKAGE_STRING);
2979
2980         /* If there are attachments, we have to do multipart/mixed */
2981         if (WC->first_attachment != NULL) {
2982                 is_multipart = 1;
2983         }
2984
2985         if (is_multipart) {
2986                 /* Remember, serv_printf() appends an extra newline */
2987                 serv_printf("Content-type: multipart/mixed; boundary=\"%s\"\n", top_boundary);
2988                 serv_printf("This is a multipart message in MIME format.\n");
2989                 serv_printf("--%s", top_boundary);
2990         }
2991
2992         /* Remember, serv_printf() appends an extra newline */
2993         serv_printf("Content-type: multipart/alternative; "
2994                 "boundary=\"%s\"\n", alt_boundary);
2995         serv_printf("This is a multipart message in MIME format.\n");
2996         serv_printf("--%s", alt_boundary);
2997
2998         serv_puts("Content-type: text/plain; charset=utf-8");
2999         serv_puts("Content-Transfer-Encoding: quoted-printable");
3000         serv_puts("");
3001         txtmail = html_to_ascii(bstr("msgtext"), 0, 80, 0);
3002         text_to_server_qp(txtmail);     /* Transmit message in quoted-printable encoding */
3003         free(txtmail);
3004
3005         serv_printf("--%s", alt_boundary);
3006
3007         serv_puts("Content-type: text/html; charset=utf-8");
3008         serv_puts("Content-Transfer-Encoding: quoted-printable");
3009         serv_puts("");
3010         serv_puts("<html><body>\r\n");
3011         text_to_server_qp(bstr("msgtext"));     /* Transmit message in quoted-printable encoding */
3012         serv_puts("</body></html>\r\n");
3013
3014         serv_printf("--%s--", alt_boundary);
3015         
3016         if (is_multipart) {
3017
3018                 /* Add in the attachments */
3019                 for (att = WC->first_attachment; att!=NULL; att=att->next) {
3020
3021                         encoded_length = ((att->length * 150) / 100);
3022                         encoded = malloc(encoded_length);
3023                         if (encoded == NULL) break;
3024                         encoded_strlen = CtdlEncodeBase64(encoded, att->data, att->length, 1);
3025
3026                         serv_printf("--%s", top_boundary);
3027                         serv_printf("Content-type: %s", att->content_type);
3028                         serv_printf("Content-disposition: attachment; filename=\"%s\"", att->filename);
3029                         serv_puts("Content-transfer-encoding: base64");
3030                         serv_puts("");
3031                         serv_write(encoded, encoded_strlen);
3032                         serv_puts("");
3033                         serv_puts("");
3034                         free(encoded);
3035                 }
3036                 serv_printf("--%s--", top_boundary);
3037         }
3038
3039         serv_puts("000");
3040 }
3041
3042
3043 /*
3044  * Post message (or don't post message)
3045  *
3046  * Note regarding the "dont_post" variable:
3047  * A random value (actually, it's just a timestamp) is inserted as a hidden
3048  * field called "postseq" when the display_enter page is generated.  This
3049  * value is checked when posting, using the static variable dont_post.  If a
3050  * user attempts to post twice using the same dont_post value, the message is
3051  * discarded.  This prevents the accidental double-saving of the same message
3052  * if the user happens to click the browser "back" button.
3053  */
3054 void post_message(void)
3055 {
3056         char buf[1024];
3057         StrBuf *encoded_subject = NULL;
3058         static long dont_post = (-1L);
3059         struct wc_attachment *att, *aptr;
3060         int is_anonymous = 0;
3061         const StrBuf *display_name = NULL;
3062         struct wcsession *WCC = WC;
3063         
3064         if (havebstr("force_room")) {
3065                 gotoroom(bstr("force_room"));
3066         }
3067
3068         if (havebstr("display_name")) {
3069                 display_name = sbstr("display_name");
3070                 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
3071                         display_name = NULL;
3072                         is_anonymous = 1;
3073                 }
3074         }
3075
3076         if (WCC->upload_length > 0) {
3077
3078                 lprintf(9, "%s:%d: we are uploading %d bytes\n", __FILE__, __LINE__, WCC->upload_length);
3079                 /** There's an attachment.  Save it to this struct... */
3080                 att = malloc(sizeof(struct wc_attachment));
3081                 memset(att, 0, sizeof(struct wc_attachment));
3082                 att->length = WCC->upload_length;
3083                 strcpy(att->content_type, WCC->upload_content_type);
3084                 strcpy(att->filename, WCC->upload_filename);
3085                 att->next = NULL;
3086
3087                 /** And add it to the list. */
3088                 if (WCC->first_attachment == NULL) {
3089                         WCC->first_attachment = att;
3090                 }
3091                 else {
3092                         aptr = WCC->first_attachment;
3093                         while (aptr->next != NULL) aptr = aptr->next;
3094                         aptr->next = att;
3095                 }
3096
3097                 /**
3098                  * Mozilla sends a simple filename, which is what we want,
3099                  * but Satan's Browser sends an entire pathname.  Reduce
3100                  * the path to just a filename if we need to.
3101                  */
3102                 while (num_tokens(att->filename, '/') > 1) {
3103                         remove_token(att->filename, 0, '/');
3104                 }
3105                 while (num_tokens(att->filename, '\\') > 1) {
3106                         remove_token(att->filename, 0, '\\');
3107                 }
3108
3109                 /**
3110                  * Transfer control of this memory from the upload struct
3111                  * to the attachment struct.
3112                  */
3113                 att->data = WCC->upload;
3114                 WCC->upload_length = 0;
3115                 WCC->upload = NULL;
3116                 display_enter();
3117                 return;
3118         }
3119
3120         if (havebstr("cancel_button")) {
3121                 sprintf(WCC->ImportantMessage, 
3122                         _("Cancelled.  Message was not posted."));
3123         } else if (havebstr("attach_button")) {
3124                 display_enter();
3125                 return;
3126         } else if (lbstr("postseq") == dont_post) {
3127                 sprintf(WCC->ImportantMessage, 
3128                         _("Automatically cancelled because you have already "
3129                         "saved this message."));
3130         } else {
3131                 const char CMD[] = "ENT0 1|%s|%d|4|%s|%s||%s|%s|%s|%s|%s";
3132                 const StrBuf *Recp = NULL; 
3133                 const StrBuf *Cc = NULL;
3134                 const StrBuf *Bcc = NULL;
3135                 const StrBuf *Wikipage = NULL;
3136                 const StrBuf *my_email_addr = NULL;
3137                 StrBuf *CmdBuf = NULL;;
3138                 StrBuf *references = NULL;
3139
3140                 if (havebstr("references"))
3141                 {
3142                         const StrBuf *ref = sbstr("references");
3143                         references = NewStrBufPlain(ChrPtr(ref), StrLength(ref));
3144                         lprintf(9, "Converting: %s\n", ChrPtr(references));
3145                         StrBufReplaceChars(references, '|', '!');
3146                         lprintf(9, "Converted: %s\n", ChrPtr(references));
3147                 }
3148                 if (havebstr("subject")) {
3149                         const StrBuf *Subj;
3150                         /*
3151                          * make enough room for the encoded string; 
3152                          * plus the QP header 
3153                          */
3154                         Subj = sbstr("subject");
3155                         
3156                         StrBufRFC2047encode(&encoded_subject, Subj);
3157                 }
3158                 Recp = sbstr("recp");
3159                 Cc = sbstr("cc");
3160                 Bcc = sbstr("bcc");
3161                 Wikipage = sbstr("wikipage");
3162                 my_email_addr = sbstr("my_email_addr");
3163                 
3164                 CmdBuf = NewStrBufPlain(NULL, 
3165                                         sizeof (CMD) + 
3166                                         StrLength(Recp) + 
3167                                         StrLength(encoded_subject) +
3168                                         StrLength(Cc) +
3169                                         StrLength(Bcc) + 
3170                                         StrLength(Wikipage) +
3171                                         StrLength(my_email_addr) + 
3172                                         StrLength(references));
3173
3174                 StrBufPrintf(CmdBuf, 
3175                              CMD,
3176                              ChrPtr(Recp),
3177                              is_anonymous,
3178                              ChrPtr(encoded_subject),
3179                              ChrPtr(display_name),
3180                              ChrPtr(Cc),
3181                              ChrPtr(Bcc),
3182                              ChrPtr(Wikipage),
3183                              ChrPtr(my_email_addr),
3184                              ChrPtr(references));
3185                 FreeStrBuf(&references);
3186
3187                 lprintf(9, "%s\n", CmdBuf);
3188                 serv_puts(ChrPtr(CmdBuf));
3189                 serv_getln(buf, sizeof buf);
3190                 FreeStrBuf(&CmdBuf);
3191                 FreeStrBuf(&encoded_subject);
3192                 if (buf[0] == '4') {
3193                         post_mime_to_server();
3194                         if (  (havebstr("recp"))
3195                            || (havebstr("cc"  ))
3196                            || (havebstr("bcc" ))
3197                         ) {
3198                                 sprintf(WCC->ImportantMessage, _("Message has been sent.\n"));
3199                         }
3200                         else {
3201                                 sprintf(WC->ImportantMessage, _("Message has been posted.\n"));
3202                         }
3203                         dont_post = lbstr("postseq");
3204                 } else {
3205                         lprintf(9, "%s:%d: server post error: %s\n", __FILE__, __LINE__, buf);
3206                         sprintf(WC->ImportantMessage, "%s", &buf[4]);
3207                         display_enter();
3208                         return;
3209                 }
3210         }
3211
3212         free_attachments(WCC);
3213
3214         /**
3215          *  We may have been supplied with instructions regarding the location
3216          *  to which we must return after posting.  If found, go there.
3217          */
3218         if (havebstr("return_to")) {
3219                 http_redirect(bstr("return_to"));
3220         }
3221         /**
3222          *  If we were editing a page in a wiki room, go to that page now.
3223          */
3224         else if (havebstr("wikipage")) {
3225                 snprintf(buf, sizeof buf, "wiki?page=%s", bstr("wikipage"));
3226                 http_redirect(buf);
3227         }
3228         /**
3229          *  Otherwise, just go to the "read messages" loop.
3230          */
3231         else {
3232                 readloop("readnew");
3233         }
3234 }
3235
3236
3237
3238
3239 /**
3240  * \brief display the message entry screen
3241  */
3242 void display_enter(void)
3243 {
3244         char buf[SIZ];
3245         StrBuf *ebuf;
3246         long now;
3247         const StrBuf *display_name = NULL;
3248         struct wc_attachment *att;
3249         int recipient_required = 0;
3250         int subject_required = 0;
3251         int recipient_bad = 0;
3252         int is_anonymous = 0;
3253         long existing_page = (-1L);
3254         struct wcsession *WCC = WC;
3255
3256         now = time(NULL);
3257
3258         if (havebstr("force_room")) {
3259                 gotoroom(bstr("force_room"));
3260         }
3261
3262         display_name = sbstr("display_name");
3263         if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
3264                 display_name = NULL;
3265                 is_anonymous = 1;
3266         }
3267
3268         /** First test to see whether this is a room that requires recipients to be entered */
3269         serv_puts("ENT0 0");
3270         serv_getln(buf, sizeof buf);
3271
3272         if (!strncmp(buf, "570", 3)) {          /** 570 means that we need a recipient here */
3273                 recipient_required = 1;
3274         }
3275         else if (buf[0] != '2') {               /** Any other error means that we cannot continue */
3276                 sprintf(WCC->ImportantMessage, "%s", &buf[4]);
3277                 readloop("readnew");
3278                 return;
3279         }
3280
3281         /* Is the server strongly recommending that the user enter a message subject? */
3282         if ((buf[3] != '\0') && (buf[4] != '\0')) {
3283                 subject_required = extract_int(&buf[4], 1);
3284         }
3285
3286         /**
3287          * Are we perhaps in an address book view?  If so, then an "enter
3288          * message" command really means "add new entry."
3289          */
3290         if (WCC->wc_default_view == VIEW_ADDRESSBOOK) {
3291                 do_edit_vcard(-1, "", "", WCC->wc_roomname);
3292                 return;
3293         }
3294
3295         /*
3296          * Are we perhaps in a calendar room?  If so, then an "enter
3297          * message" command really means "add new calendar item."
3298          */
3299         if (WCC->wc_default_view == VIEW_CALENDAR) {
3300                 display_edit_event();
3301                 return;
3302         }
3303
3304         /*
3305          * Are we perhaps in a tasks view?  If so, then an "enter
3306          * message" command really means "add new task."
3307          */
3308         if (WCC->wc_default_view == VIEW_TASKS) {
3309                 display_edit_task();
3310                 return;
3311         }
3312
3313         /*
3314          * Otherwise proceed normally.
3315          * Do a custom room banner with no navbar...
3316          */
3317         output_headers(1, 1, 2, 0, 0, 0);
3318         wprintf("<div id=\"banner\">\n");
3319         embed_room_banner(NULL, navbar_none);
3320         wprintf("</div>\n");
3321         wprintf("<div id=\"content\">\n"
3322                 "<div class=\"fix_scrollbar_bug message \">");
3323
3324         /* Now check our actual recipients if there are any */
3325         if (recipient_required) {
3326                 const StrBuf *Recp = NULL; 
3327                 const StrBuf *Cc = NULL;
3328                 const StrBuf *Bcc = NULL;
3329                 const StrBuf *Wikipage = NULL;
3330                 StrBuf *CmdBuf = NULL;;
3331                 const char CMD[] = "ENT0 0|%s|%d|0||%s||%s|%s|%s";
3332                 
3333                 Recp = sbstr("recp");
3334                 Cc = sbstr("cc");
3335                 Bcc = sbstr("bcc");
3336                 Wikipage = sbstr("wikipage");
3337                 
3338                 CmdBuf = NewStrBufPlain(NULL, 
3339                                         sizeof (CMD) + 
3340                                         StrLength(Recp) + 
3341                                         StrLength(display_name) +
3342                                         StrLength(Cc) +
3343                                         StrLength(Bcc) + 
3344                                         StrLength(Wikipage));
3345
3346                 StrBufPrintf(CmdBuf, 
3347                              CMD,
3348                              ChrPtr(Recp), 
3349                              is_anonymous,
3350                              ChrPtr(display_name),
3351                              ChrPtr(Cc), 
3352                              ChrPtr(Bcc), 
3353                              ChrPtr(Wikipage));
3354                 serv_puts(ChrPtr(CmdBuf));
3355                 serv_getln(buf, sizeof buf);
3356                 FreeStrBuf(&CmdBuf);
3357
3358                 if (!strncmp(buf, "570", 3)) {  /** 570 means we have an invalid recipient listed */
3359                         if (havebstr("recp") && 
3360                             havebstr("cc"  ) && 
3361                             havebstr("bcc" )) {
3362                                 recipient_bad = 1;
3363                         }
3364                 }
3365                 else if (buf[0] != '2') {       /** Any other error means that we cannot continue */
3366                         wprintf("<em>%s</em><br />\n", &buf[4]);
3367                         goto DONE;
3368                 }
3369         }
3370
3371         /** If we got this far, we can display the message entry screen. */
3372
3373         /* begin message entry screen */
3374         wprintf("<form "
3375                 "enctype=\"multipart/form-data\" "
3376                 "method=\"POST\" "
3377                 "accept-charset=\"UTF-8\" "
3378                 "action=\"post\" "
3379                 "name=\"enterform\""
3380                 ">\n");
3381         wprintf("<input type=\"hidden\" name=\"postseq\" value=\"%ld\">\n", now);
3382         if (WCC->wc_view == VIEW_WIKI) {
3383                 wprintf("<input type=\"hidden\" name=\"wikipage\" value=\"%s\">\n", bstr("wikipage"));
3384         }
3385         wprintf("<input type=\"hidden\" name=\"return_to\" value=\"%s\">\n", bstr("return_to"));
3386         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WCC->nonce);
3387         wprintf("<input type=\"hidden\" name=\"force_room\" value=\"");
3388         escputs(WCC->wc_roomname);
3389         wprintf("\">\n");
3390         wprintf("<input type=\"hidden\" name=\"references\" value=\"");
3391         escputs(bstr("references"));
3392         wprintf("\">\n");
3393
3394         /** submit or cancel buttons */
3395         wprintf("<p class=\"send_edit_msg\">");
3396         wprintf("<input type=\"submit\" name=\"send_button\" value=\"");
3397         if (recipient_required) {
3398                 wprintf(_("Send message"));
3399         } else {
3400                 wprintf(_("Post message"));
3401         }
3402         wprintf("\">&nbsp;"
3403                 "<input type=\"submit\" name=\"cancel_button\" value=\"%s\">\n", _("Cancel"));
3404         wprintf("</p>");
3405
3406         /** header bar */
3407
3408         wprintf("<img src=\"static/newmess3_24x.gif\" class=\"imgedit\">");
3409         wprintf("  ");  /** header bar */
3410         webcit_fmt_date(buf, now, 0);
3411         wprintf("%s", buf);
3412         wprintf("\n");  /** header bar */
3413
3414         wprintf("<table width=\"100%%\" class=\"edit_msg_table\">");
3415         wprintf("<tr>");
3416         wprintf("<th><label for=\"from_id\" > ");
3417         wprintf(_(" <I>from</I> "));
3418         wprintf("</label></th>");
3419
3420         wprintf("<td colspan=\"2\">");
3421
3422         /* Allow the user to select any of his valid screen names */
3423
3424         wprintf("<select name=\"display_name\" size=1 id=\"from_id\">\n");
3425
3426         serv_puts("GVSN");
3427         serv_getln(buf, sizeof buf);
3428         if (buf[0] == '1') {
3429                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
3430                         wprintf("<option %s value=\"",
3431                                 ((!strcasecmp(bstr("display_name"), buf)) ? "selected" : "")
3432                         );
3433                         escputs(buf);
3434                         wprintf("\">");
3435                         escputs(buf);
3436                         wprintf("</option>\n");
3437                 }
3438         }
3439
3440         if (WCC->room_flags & QR_ANONOPT) {
3441                 wprintf("<option %s value=\"__ANONYMOUS__\">%s</option>\n",
3442                         ((!strcasecmp(bstr("__ANONYMOUS__"), WCC->wc_fullname)) ? "selected" : ""),
3443                         _("Anonymous")
3444                 );
3445         }
3446
3447         wprintf("</select>\n");
3448
3449         /* If this is an email (not a post), allow the user to select any of his
3450          * valid email addresses.
3451          */
3452         if (recipient_required) {
3453                 serv_puts("GVEA");
3454                 serv_getln(buf, sizeof buf);
3455                 if (buf[0] == '1') {
3456                         wprintf("<select name=\"my_email_addr\" size=1>\n");
3457                         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
3458                                 wprintf("<option value=\"");
3459                                 escputs(buf);
3460                                 wprintf("\">&lt;");
3461                                 escputs(buf);
3462                                 wprintf("&gt;</option>\n");
3463                         }
3464                         wprintf("</select>\n");
3465                 }
3466         }
3467
3468         wprintf(_(" <I>in</I> "));
3469         escputs(WCC->wc_roomname);
3470
3471         wprintf("</td></tr>");
3472
3473         if (recipient_required) {
3474                 char *ccraw;
3475                 char *copy;
3476                 size_t len;
3477                 wprintf("<tr><th><label for=\"recp_id\"> ");
3478                 wprintf(_("To:"));
3479                 wprintf("</label></th>"
3480                         "<td><input autocomplete=\"off\" type=\"text\" name=\"recp\" id=\"recp_id\" value=\"");
3481                 ccraw = xbstr("recp", &len);
3482                 copy = (char*) malloc(len * 2 + 1);
3483                 memcpy(copy, ccraw, len + 1); 
3484                 utf8ify_rfc822_string(copy);
3485                 escputs(copy);
3486                 free(copy);
3487                 wprintf("\" size=45 maxlength=1000 />");
3488                 wprintf("<div class=\"auto_complete\" id=\"recp_name_choices\"></div>");
3489                 wprintf("</td><td rowspan=\"3\" align=\"left\" valign=\"top\">");
3490
3491                 /** Pop open an address book -- begin **/
3492                 wprintf(
3493                         "<a href=\"javascript:PopOpenAddressBook('recp_id|%s|cc_id|%s|bcc_id|%s');\" "
3494                         "title=\"%s\">"
3495                         "<img align=middle border=0 width=24 height=24 src=\"static/viewcontacts_24x.gif\">"
3496                         "&nbsp;%s</a>",
3497                         _("To:"), _("CC:"), _("BCC:"),
3498                         _("Contacts"), _("Contacts")
3499                 );
3500                 /** Pop open an address book -- end **/
3501
3502                 wprintf("</td></tr>");
3503
3504                 wprintf("<tr><th><label for=\"cc_id\"> ");
3505                 wprintf(_("CC:"));
3506                 wprintf("</label></th>"
3507                         "<td><input autocomplete=\"off\" type=\"text\" name=\"cc\" id=\"cc_id\" value=\"");
3508                 ccraw = xbstr("cc", &len);
3509                 copy = (char*) malloc(len * 2 + 1);
3510                 memcpy(copy, ccraw, len + 1); 
3511                 utf8ify_rfc822_string(copy);
3512                 escputs(copy);
3513                 free(copy);
3514                 wprintf("\" size=45 maxlength=1000 />");
3515                 wprintf("<div class=\"auto_complete\" id=\"cc_name_choices\"></div>");
3516                 wprintf("</td></tr>");
3517
3518                 wprintf("<tr><th><label for=\"bcc_id\"> ");
3519                 wprintf(_("BCC:"));
3520                 wprintf("</label></th>"
3521                         "<td><input autocomplete=\"off\" type=\"text\" name=\"bcc\" id=\"bcc_id\" value=\"");
3522                 ccraw = xbstr("bcc", &len);
3523                 copy = (char*) malloc(len * 2 + 1);
3524                 memcpy(copy, ccraw, len + 1); 
3525                 utf8ify_rfc822_string(copy);
3526                 escputs(copy);
3527                 free(copy);
3528                 wprintf("\" size=45 maxlength=1000 />");
3529                 wprintf("<div class=\"auto_complete\" id=\"bcc_name_choices\"></div>");
3530                 wprintf("</td></tr>");
3531
3532                 /** Initialize the autocomplete ajax helpers (found in wclib.js) */
3533                 wprintf("<script type=\"text/javascript\">      \n"
3534                         " activate_entmsg_autocompleters();     \n"
3535                         "</script>                              \n"
3536                 );
3537
3538         }
3539
3540         wprintf("<tr><th><label for=\"subject_id\" > ");
3541         if (recipient_required || subject_required) {
3542                 wprintf(_("Subject:"));
3543         }
3544         else {
3545                 wprintf(_("Subject (optional):"));
3546         }
3547         wprintf("</label></th>"
3548                 "<td colspan=\"2\">"
3549                 "<input type=\"text\" name=\"subject\" id=\"subject_id\" value=\"");
3550         escputs(bstr("subject"));
3551         wprintf("\" size=45 maxlength=70>\n");
3552         wprintf("</td></tr>");
3553
3554         wprintf("<tr><td colspan=\"3\">\n");
3555
3556         wprintf("<textarea name=\"msgtext\" cols=\"80\" rows=\"15\">");
3557
3558         /** If we're continuing from a previous edit, put our partially-composed message back... */
3559         msgescputs(bstr("msgtext"));
3560
3561         /* If we're forwarding a message, insert it here... */
3562         if (lbstr("fwdquote") > 0L) {
3563                 wprintf("<br><div align=center><i>");
3564                 wprintf(_("--- forwarded message ---"));
3565                 wprintf("</i></div><br>");
3566                 pullquote_message(lbstr("fwdquote"), 1, 1);
3567         }
3568
3569         /** If we're replying quoted, insert the quote here... */
3570         else if (lbstr("replyquote") > 0L) {
3571                 wprintf("<br>"
3572                         "<blockquote>");
3573                 pullquote_message(lbstr("replyquote"), 0, 1);
3574                 wprintf("</blockquote><br>");
3575         }
3576
3577         /** If we're editing a wiki page, insert the existing page here... */
3578         else if (WCC->wc_view == VIEW_WIKI) {
3579                 safestrncpy(buf, bstr("wikipage"), sizeof buf);
3580                 str_wiki_index(buf);
3581                 existing_page = locate_message_by_uid(buf);
3582                 if (existing_page >= 0L) {
3583                         pullquote_message(existing_page, 1, 0);
3584                 }
3585         }
3586
3587         /** Insert our signature if appropriate... */
3588         if ( (WCC->is_mailbox) && !yesbstr("sig_inserted") ) {
3589                 int UseSig;
3590                 get_pref_yesno("use_sig", &UseSig, 0);
3591                 if (UseSig) {
3592                         StrBuf *Sig;
3593                         const char *sig, *esig;
3594
3595                         get_preference("signature", &ebuf);
3596                         Sig = NewStrBuf();
3597                         StrBufEUid_unescapize(Sig, ebuf);
3598                         sig = ChrPtr(Sig);
3599                         esig = sig + StrLength(Sig);
3600                         wprintf("<br>--<br>");
3601                         while (sig <= esig) {
3602                                 if (*sig == '\n') {
3603                                         wprintf("<br>");
3604                                 }
3605                                 else if (*sig == '<') {
3606                                         wprintf("&lt;");
3607                                 }
3608                                 else if (*sig == '>') {
3609                                         wprintf("&gt;");
3610                                 }
3611                                 else if (*sig == '&') {
3612                                         wprintf("&amp;");
3613                                 }
3614                                 else if (*sig == '\"') {
3615                                         wprintf("&quot;");
3616                                 }
3617                                 else if (*sig == '\'') {
3618                                         wprintf("&#39;");
3619                                 }
3620                                 else /* since we're utf 8, is this a good idea? if (isprint(*sig))*/ {
3621                                         wprintf("%c", *sig);
3622                                 } 
3623                                 sig ++;
3624                         }
3625                         FreeStrBuf(&Sig);
3626                 }
3627         }
3628
3629         wprintf("</textarea>\n");
3630
3631         /** Make sure we only insert our signature once */
3632         /** We don't care if it was there or not before, it needs to be there now. */
3633         wprintf("<input type=\"hidden\" name=\"sig_inserted\" value=\"yes\">\n");
3634         
3635         /**
3636          * The following template embeds the TinyMCE richedit control, and automatically
3637          * transforms the textarea into a richedit textarea.
3638          */
3639         do_template("richedit", NULL);
3640
3641         /** Enumerate any attachments which are already in place... */
3642         wprintf("<div class=\"attachment buttons\"><img src=\"static/diskette_24x.gif\" class=\"imgedit\" > ");
3643         wprintf(_("Attachments:"));
3644         wprintf(" ");
3645         wprintf("<select name=\"which_attachment\" size=1>");
3646         for (att = WCC->first_attachment; att != NULL; att = att->next) {
3647                 wprintf("<option value=\"");
3648                 urlescputs(att->filename);
3649                 wprintf("\">");
3650                 escputs(att->filename);
3651                 /* wprintf(" (%s, %d bytes)",att->content_type,att->length); */
3652                 wprintf("</option>\n");
3653         }
3654         wprintf("</select>");
3655
3656         /** Now offer the ability to attach additional files... */
3657         wprintf("&nbsp;&nbsp;&nbsp;");
3658         wprintf(_("Attach file:"));
3659         wprintf(" <input name=\"attachfile\" class=\"attachfile\" "
3660                 "size=16 type=\"file\">\n&nbsp;&nbsp;"
3661                 "<input type=\"submit\" name=\"attach_button\" value=\"%s\">\n", _("Add"));
3662         wprintf("</div>");      /* End of "attachment buttons" div */
3663
3664
3665         wprintf("</td></tr></table>");
3666         
3667         wprintf("</form>\n");
3668         wprintf("</div>\n");    /* end of "fix_scrollbar_bug" div */
3669
3670         /* NOTE: address_book_popup() will close the "content" div.  Don't close it here. */
3671 DONE:   address_book_popup();
3672         wDumpContent(1);
3673 }
3674
3675
3676 /**
3677  * \brief delete a message
3678  */
3679 void delete_msg(void)
3680 {
3681         long msgid;
3682         char buf[SIZ];
3683
3684         msgid = lbstr("msgid");
3685
3686         if (WC->wc_is_trash) {  /** Delete from Trash is a real delete */
3687                 serv_printf("DELE %ld", msgid); 
3688         }
3689         else {                  /** Otherwise move it to Trash */
3690                 serv_printf("MOVE %ld|_TRASH_|0", msgid);
3691         }
3692
3693         serv_getln(buf, sizeof buf);
3694         sprintf(WC->ImportantMessage, "%s", &buf[4]);
3695
3696         readloop("readnew");
3697 }
3698
3699
3700 /**
3701  * \brief move a message to another folder
3702  */
3703 void move_msg(void)
3704 {
3705         long msgid;
3706         char buf[SIZ];
3707
3708         msgid = lbstr("msgid");
3709
3710         if (havebstr("move_button")) {
3711                 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
3712                 serv_puts(buf);
3713                 serv_getln(buf, sizeof buf);
3714                 sprintf(WC->ImportantMessage, "%s", &buf[4]);
3715         } else {
3716                 sprintf(WC->ImportantMessage, (_("The message was not moved.")));
3717         }
3718
3719         readloop("readnew");
3720 }
3721
3722
3723
3724
3725
3726 /*
3727  * Confirm move of a message
3728  */
3729 void confirm_move_msg(void)
3730 {
3731         long msgid;
3732         char buf[SIZ];
3733         char targ[SIZ];
3734
3735         msgid = lbstr("msgid");
3736
3737
3738         output_headers(1, 1, 2, 0, 0, 0);
3739         wprintf("<div id=\"banner\">\n");
3740         wprintf("<h1>");
3741         wprintf(_("Confirm move of message"));
3742         wprintf("</h1>");
3743         wprintf("</div>\n");
3744
3745         wprintf("<div id=\"content\" class=\"service\">\n");
3746
3747         wprintf("<CENTER>");
3748
3749         wprintf(_("Move this message to:"));
3750         wprintf("<br />\n");
3751
3752         wprintf("<form METHOD=\"POST\" action=\"move_msg\">\n");
3753         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
3754         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n", bstr("msgid"));
3755
3756         wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
3757         serv_puts("LKRA");
3758         serv_getln(buf, sizeof buf);
3759         if (buf[0] == '1') {
3760                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
3761                         extract_token(targ, buf, 0, '|', sizeof targ);
3762                         wprintf("<OPTION>");
3763                         escputs(targ);
3764                         wprintf("\n");
3765                 }
3766         }
3767         wprintf("</SELECT>\n");
3768         wprintf("<br />\n");
3769
3770         wprintf("<INPUT TYPE=\"submit\" NAME=\"move_button\" VALUE=\"%s\">", _("Move"));
3771         wprintf("&nbsp;");
3772         wprintf("<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">", _("Cancel"));
3773         wprintf("</form></CENTER>\n");
3774
3775         wprintf("</CENTER>\n");
3776         wDumpContent(1);
3777 }
3778
3779 void readnew(void) { readloop("readnew");}
3780 void readold(void) { readloop("readold");}
3781 void readfwd(void) { readloop("readfwd");}
3782 void headers(void) { readloop("headers");}
3783 void do_search(void) { readloop("do_search");}
3784
3785
3786
3787
3788
3789
3790 void 
3791 InitModule_MSG
3792 (void)
3793 {
3794         WebcitAddUrlHandler(HKEY("readnew"), readnew, 0);
3795         WebcitAddUrlHandler(HKEY("readold"), readold, 0);
3796         WebcitAddUrlHandler(HKEY("readfwd"), readfwd, 0);
3797         WebcitAddUrlHandler(HKEY("headers"), headers, 0);
3798         WebcitAddUrlHandler(HKEY("do_search"), do_search, 0);
3799         WebcitAddUrlHandler(HKEY("display_enter"), display_enter, 0);
3800         WebcitAddUrlHandler(HKEY("post"), post_message, 0);
3801         WebcitAddUrlHandler(HKEY("move_msg"), move_msg, 0);
3802         WebcitAddUrlHandler(HKEY("delete_msg"), delete_msg, 0);
3803         WebcitAddUrlHandler(HKEY("confirm_move_msg"), confirm_move_msg, 0);
3804         WebcitAddUrlHandler(HKEY("msg"), embed_message, NEED_URL|AJAX);
3805         WebcitAddUrlHandler(HKEY("printmsg"), print_message, NEED_URL);
3806         WebcitAddUrlHandler(HKEY("mobilemsg"), mobile_message_view, NEED_URL);
3807         WebcitAddUrlHandler(HKEY("msgheaders"), display_headers, NEED_URL);
3808
3809         RegisterNamespace("MAIL:SUMM:DATESTR", 0, 0, tmplput_MAIL_SUMM_DATE_STR, CTX_MAILSUM);
3810         RegisterNamespace("MAIL:SUMM:DATENO",  0, 0, tmplput_MAIL_SUMM_DATE_NO,  CTX_MAILSUM);
3811         RegisterNamespace("MAIL:SUMM:N",       0, 0, tmplput_MAIL_SUMM_N,        CTX_MAILSUM);
3812         RegisterNamespace("MAIL:SUMM:FROM",    0, 2, tmplput_MAIL_SUMM_FROM,     CTX_MAILSUM);
3813         RegisterNamespace("MAIL:SUMM:TO",      0, 2, tmplput_MAIL_SUMM_TO,       CTX_MAILSUM);
3814         RegisterNamespace("MAIL:SUMM:SUBJECT", 0, 4, tmplput_MAIL_SUMM_SUBJECT,  CTX_MAILSUM);
3815         RegisterNamespace("MAIL:SUMM:NATTACH", 0, 0, tmplput_MAIL_SUMM_NATTACH,  CTX_MAILSUM);
3816         RegisterNamespace("MAIL:SUMM:CCCC", 0, 2, tmplput_MAIL_SUMM_CCCC,  CTX_MAILSUM);
3817         RegisterNamespace("MAIL:SUMM:H_NODE", 0, 2, tmplput_MAIL_SUMM_H_NODE,  CTX_MAILSUM);
3818         RegisterNamespace("MAIL:SUMM:ALLRCPT", 0, 2, tmplput_MAIL_SUMM_ALLRCPT,  CTX_MAILSUM);
3819         RegisterNamespace("MAIL:SUMM:ORGROOM", 0, 2, tmplput_MAIL_SUMM_ORGROOM,  CTX_MAILSUM);
3820         RegisterNamespace("MAIL:SUMM:RFCA", 0, 2, tmplput_MAIL_SUMM_RFCA,  CTX_MAILSUM);
3821         RegisterNamespace("MAIL:SUMM:OTHERNODE", 2, 0, tmplput_MAIL_SUMM_OTHERNODE,  CTX_MAILSUM);
3822         RegisterNamespace("MAIL:SUMM:REFIDS", 0, 0, tmplput_MAIL_SUMM_REFIDS,  CTX_MAILSUM);
3823         RegisterNamespace("MAIL:SUMM:INREPLYTO", 0, 2, tmplput_MAIL_SUMM_INREPLYTO,  CTX_MAILSUM);
3824         RegisterNamespace("MAIL:BODY", 0, 2, tmplput_MAIL_BODY,  CTX_MAILSUM);
3825
3826
3827
3828         RegisterMimeRenderer(HKEY("text/x-citadel-variformat"), render_MAIL_variformat);
3829         RegisterMimeRenderer(HKEY("text/plain"), render_MAIL_text_plain);
3830         RegisterMimeRenderer(HKEY("text"), render_MAIL_text_plain);
3831         RegisterMimeRenderer(HKEY("text/html"), render_MAIL_html);
3832         RegisterMimeRenderer(HKEY(""), render_MAIL_UNKNOWN);
3833
3834
3835         RegisterConditional(HKEY("COND:MAIL:SUMM:UNREAD"), 0, Conditional_MAIL_SUMM_UNREAD, CTX_MAILSUM);
3836         RegisterConditional(HKEY("COND:MAIL:SUMM:H_NODE"), 0, Conditional_MAIL_SUMM_H_NODE, CTX_MAILSUM);
3837         RegisterConditional(HKEY("COND:MAIL:SUMM:OTHERNODE"), 0, Conditional_MAIL_SUMM_OTHERNODE, CTX_MAILSUM);
3838
3839         RegisterMsgHdr(HKEY("nhdr"), examine_nhdr, 0);
3840         RegisterMsgHdr(HKEY("type"), examine_type, 0);
3841         RegisterMsgHdr(HKEY("from"), examine_from, 0);
3842         RegisterMsgHdr(HKEY("subj"), examine_subj, 0);
3843         RegisterMsgHdr(HKEY("msgn"), examine_msgn, 0);
3844         RegisterMsgHdr(HKEY("wefw"), examine_wefw, 0);
3845         RegisterMsgHdr(HKEY("cccc"), examine_cccc, 0);
3846         RegisterMsgHdr(HKEY("hnod"), examine_hnod, 0);
3847         RegisterMsgHdr(HKEY("room"), examine_room, 0);
3848         RegisterMsgHdr(HKEY("rfca"), examine_rfca, 0);
3849         RegisterMsgHdr(HKEY("node"), examine_node, 0);
3850         RegisterMsgHdr(HKEY("rcpt"), examine_rcpt, 0);
3851         RegisterMsgHdr(HKEY("time"), examine_time, 0);
3852         RegisterMsgHdr(HKEY("part"), examine_mime_part, 0);
3853         RegisterMsgHdr(HKEY("text"), examine_text, 1);
3854         RegisterMsgHdr(HKEY("X-Citadel-MSG4-Partnum"), examine_msg4_partnum, 0);
3855         RegisterMsgHdr(HKEY("Content-type"), examine_content_type, 0);
3856         RegisterMsgHdr(HKEY("Content-length"), examine_content_lengh, 0);
3857         RegisterMsgHdr(HKEY("Content-transfer-encoding"), examine_content_encoding, 0);
3858         return ;
3859 }