4 * Functions which deal with the fetching and displaying of messages.
12 HashList *MsgHeaderHandler = NULL;
13 HashList *MsgEvaluators = NULL;
14 HashList *MimeRenderHandler = NULL;
15 int dbg_analyze_msg = 0;
17 #define SUBJ_COL_WIDTH_PCT 50 /**< Mailbox view column width */
18 #define SENDER_COL_WIDTH_PCT 30 /**< Mailbox view column width */
19 #define DATE_PLUS_BUTTONS_WIDTH_PCT 20 /**< Mailbox view column width */
21 void jsonMessageListHdr(void);
23 void display_enter(void);
25 /*----------------------------------------------------------------------------*/
28 typedef void (*MsgPartEvaluatorFunc)(message_summary *Sum, StrBuf *Buf);
30 typedef struct _MsgPartEvaluatorStruct {
31 MsgPartEvaluatorFunc f;
32 }MsgPartEvaluatorStruct;
35 /*----------------------------------------------------------------------------*/
40 * I wanna SEE that message!
42 * msgnum Message number to display
43 * printable_view Nonzero to display a printable view
44 * section Optional for encapsulated message/rfc822 submessage
46 int read_message(StrBuf *Target, const char *tmpl, long tmpllen, long msgnum, int printable_view, const StrBuf *PartNum)
54 message_summary *Msg = NULL;
62 WCTemplputParams SubTP;
65 lprintf(1, "----------%s---------MSG4 %ld|%s--------------\n", tmpl, msgnum, ChrPtr(PartNum));
66 serv_printf("MSG4 %ld|%s", msgnum, ChrPtr(PartNum));
67 StrBuf_ServGetln(Buf);
68 if (GetServerStatus(Buf, NULL) != 1) {
69 StrBufAppendPrintf(Target, "<strong>");
70 StrBufAppendPrintf(Target, _("ERROR:"));
71 StrBufAppendPrintf(Target, "</strong> %s<br />\n", &buf[4]);
76 /** begin everythingamundo table */
79 HdrToken = NewStrBuf();
80 Msg = (message_summary *)malloc(sizeof(message_summary));
81 memset(Msg, 0, sizeof(message_summary));
83 Msg->PartNum = PartNum;
84 Msg->MsgBody = (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
85 memset(Msg->MsgBody, 0, sizeof(wc_mime_attachment));
86 Msg->MsgBody->msgnum = msgnum;
87 FoundCharset = NewStrBuf();
88 while ((StrBuf_ServGetln(Buf)>=0) && !Done) {
89 if ( (StrLength(Buf)==3) &&
90 !strcmp(ChrPtr(Buf), "000"))
94 lprintf(1, _("unexpected end of message"));
96 Msg->MsgBody->ContentType = NewStrBufPlain(HKEY("text/html"));
97 StrBufAppendPrintf(Msg->MsgBody->Data, "<div><i>");
98 StrBufAppendPrintf(Msg->MsgBody->Data, _("unexpected end of message"));
99 StrBufAppendPrintf(Msg->MsgBody->Data, " (1)</i><br /><br />\n");
100 StrBufAppendPrintf(Msg->MsgBody->Data, "</div>\n");
105 case 0:/* Citadel Message Headers */
106 if (StrLength(Buf) == 0) {
110 StrBufExtract_token(HdrToken, Buf, 0, '=');
111 StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
114 if (dbg_analyze_msg) lprintf(1, ":: [%s] = [%s]\n", ChrPtr(HdrToken), ChrPtr(Buf));
116 /* look up one of the examine_* functions to parse the content */
117 if (GetHash(MsgHeaderHandler, SKEY(HdrToken), &vHdr) &&
119 Hdr = (headereval*)vHdr;
120 Hdr->evaluator(Msg, Buf, FoundCharset);
121 if (Hdr->Type == 1) {
125 else LogError(Target,
127 "don't know how to handle message header[%s]\n",
130 case 1:/* Message Mime Header */
131 if (StrLength(Buf) == 0) {
133 if (Msg->MsgBody->ContentType == NULL)
134 /* end of header or no header? */
135 Msg->MsgBody->ContentType = NewStrBufPlain(HKEY("text/plain"));
136 /* usual end of mime header */
140 StrBufExtract_token(HdrToken, Buf, 0, ':');
141 if (StrLength(HdrToken) > 0) {
142 StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
144 if (dbg_analyze_msg) lprintf(1, ":: [%s] = [%s]\n", ChrPtr(HdrToken), ChrPtr(Buf));
146 /* the examine*'s know how to do with mime headers too... */
147 if (GetHash(MsgHeaderHandler, SKEY(HdrToken), &vHdr) &&
149 Hdr = (headereval*)vHdr;
150 Hdr->evaluator(Msg, Buf, FoundCharset);
155 case 2: /* Message Body */
157 if (Msg->MsgBody->size_known > 0) {
158 StrBuf_ServGetBLOB(Msg->MsgBody->Data, Msg->MsgBody->length);
160 /*/ todo: check next line, if not 000, append following lines */
163 if (StrLength(Msg->MsgBody->Data) > 0)
164 StrBufAppendBufPlain(Msg->MsgBody->Data, "\n", 1, 0);
165 StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
169 StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
174 if (Msg->AllAttach == NULL)
175 Msg->AllAttach = NewHash(1,NULL);
176 /* now we put the body mimepart we read above into the mimelist */
177 Put(Msg->AllAttach, SKEY(Msg->MsgBody->PartNum), Msg->MsgBody, DestroyMime);
179 /* strip the bare contenttype, so we ommit charset etc. */
180 StrBufExtract_token(Buf, Msg->MsgBody->ContentType, 0, ';');
182 /* look up the renderer, that will convert this mimeitem into the htmlized form */
183 if (GetHash(MimeRenderHandler, SKEY(Buf), &vHdr) &&
185 RenderMimeFuncStruct *Render;
186 Render = (RenderMimeFuncStruct*)vHdr;
187 Render->f(Msg->MsgBody, NULL, FoundCharset);
190 if (StrLength(Msg->reply_references)> 0) {
191 /* Trim down excessively long lists of thread references. We eliminate the
192 * second one in the list so that the thread root remains intact.
194 int rrtok = num_tokens(ChrPtr(Msg->reply_references), '|');
195 int rrlen = StrLength(Msg->reply_references);
196 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
197 StrBufRemove_token(Msg->reply_references, 1, '|');
201 /* Generate a reply-to address */
202 if (StrLength(Msg->Rfca) > 0) {
203 if (Msg->reply_to == NULL)
204 Msg->reply_to = NewStrBuf();
205 if (StrLength(Msg->from) > 0) {
206 StrBufPrintf(Msg->reply_to, "%s <%s>", ChrPtr(Msg->from), ChrPtr(Msg->Rfca));
209 FlushStrBuf(Msg->reply_to);
210 StrBufAppendBuf(Msg->reply_to, Msg->Rfca, 0);
215 if ((StrLength(Msg->OtherNode)>0) &&
216 (strcasecmp(ChrPtr(Msg->OtherNode), ChrPtr(WCC->serv_info->serv_nodename))) &&
217 (strcasecmp(ChrPtr(Msg->OtherNode), ChrPtr(WCC->serv_info->serv_humannode)) ))
219 if (Msg->reply_to == NULL)
220 Msg->reply_to = NewStrBuf();
221 StrBufPrintf(Msg->reply_to,
224 ChrPtr(Msg->OtherNode));
227 if (Msg->reply_to == NULL)
228 Msg->reply_to = NewStrBuf();
229 FlushStrBuf(Msg->reply_to);
230 StrBufAppendBuf(Msg->reply_to, Msg->from, 0);
234 /* now check if we need to translate some mimeparts, and remove the duplicate */
235 it = GetNewHashPos(Msg->AllAttach, 0);
236 while (GetNextHashPos(Msg->AllAttach, it, &len, &Key, &vMime) &&
238 wc_mime_attachment *Mime = (wc_mime_attachment*) vMime;
239 evaluate_mime_part(Msg, Mime);
242 memset(&SubTP, 0, sizeof(WCTemplputParams));
243 SubTP.Filter.ContextType = CTX_MAILSUM;
245 DoTemplate(tmpl, tmpllen, Target, &SubTP);
247 DestroyMessageSummary(Msg);
248 FreeStrBuf(&FoundCharset);
249 FreeStrBuf(&HdrToken);
257 * Unadorned HTML output of an individual message, suitable
258 * for placing in a hidden iframe, for printing, or whatever
260 * msgnum_as_string == Message number, as a string instead of as a long int
262 void embed_message(void) {
265 const StrBuf *Tmpl = sbstr("template");
267 msgnum = StrTol(WCC->UrlFragment2);
268 if (StrLength(Tmpl) > 0)
269 read_message(WCC->WBuf, SKEY(Tmpl), msgnum, 0, NULL);
271 read_message(WCC->WBuf, HKEY("view_message"), msgnum, 0, NULL);
276 * Printable view of a message
278 * msgnum_as_string == Message number, as a string instead of as a long int
280 void print_message(void) {
283 msgnum = StrTol(WC->UrlFragment2);
284 output_headers(0, 0, 0, 0, 0, 0);
286 hprintf("Content-type: text/html\r\n"
287 "Server: " PACKAGE_STRING "\r\n"
288 "Connection: close\r\n");
292 read_message(WC->WBuf, HKEY("view_message_print"), msgnum, 1, NULL);
298 * Mobile browser view of message
300 * @param msg_num_as_string Message number as a string instead of as a long int
302 void mobile_message_view(void) {
304 msgnum = StrTol(WC->UrlFragment2);
305 output_headers(1, 0, 0, 0, 0, 1);
307 do_template("msgcontrols", NULL);
308 read_message(WC->WBuf, HKEY("view_message"), msgnum,1, NULL);
313 * \brief Display a message's headers
315 * \param msgnum_as_string Message number, as a string instead of as a long int
317 void display_headers(void) {
321 msgnum = StrTol(WC->UrlFragment2);
322 output_headers(0, 0, 0, 0, 0, 0);
324 hprintf("Content-type: text/plain\r\n"
326 "Connection: close\r\n",
330 serv_printf("MSG2 %ld|3", msgnum);
331 serv_getln(buf, sizeof buf);
333 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
334 wprintf("%s\n", buf);
342 message_summary *ReadOneMessageSummary(StrBuf *RawMessage, const char *DefaultSubject, long MsgNum)
345 MsgPartEvaluatorStruct *Eval;
346 message_summary *Msg;
355 serv_printf("MSG0 %ld|1", MsgNum); /* ask for headers only */
357 StrBuf_ServGetln(Buf);
358 if (GetServerStatus(Buf, NULL) == 1) {
363 Msg = (message_summary*)malloc(sizeof(message_summary));
364 memset(Msg, 0, sizeof(message_summary));
365 while (len = StrBuf_ServGetln(Buf),
367 strcmp(ChrPtr(Buf), "000")== 0)){
369 ebuf = strchr(ChrPtr(Buf), '=');
371 if (GetHash(MsgEvaluators, buf, nBuf, &vEval) && vEval != NULL) {
372 Eval = (MsgPartEvaluatorStruct*) vEval;
373 StrBufCutLeft(Buf, nBuf + 1);
376 else lprintf(1, "Don't know how to handle Message Headerline [%s]", ChrPtr(Buf));
386 * load message pointers from the server for a "read messages" operation
388 * servcmd: the citadel command to send to the citserver
389 * with_headers: also include some of the headers with the message numbers (more expensive)
391 int load_msg_ptrs(char *servcmd, int with_headers)
393 StrBuf* FoundCharset = NULL;
395 message_summary *Msg;
403 if (WCC->summ != NULL) {
404 DeleteHash(&WCC->summ);
406 WCC->summ = NewHash(1, Flathash);
411 StrBuf_ServGetln(Buf);
412 if (GetServerStatus(Buf, NULL) != 1) {
417 while (len = StrBuf_ServGetln(Buf),
419 strcmp(ChrPtr(Buf), "000")!= 0))
421 if (nummsgs < maxload) {
423 Msg = (message_summary*)malloc(sizeof(message_summary));
424 memset(Msg, 0, sizeof(message_summary));
426 Msg->msgnum = StrBufExtract_long(Buf, 0, '|');
427 Msg->date = StrBufExtract_long(Buf, 1, '|');
429 * as citserver probably gives us messages in forward date sorting
430 * nummsgs should be the same order as the message date.
432 if (Msg->date == 0) {
434 if (StrLength(Buf) < 32)
438 Msg->from = NewStrBufPlain(NULL, StrLength(Buf));
439 StrBufExtract_token(Buf2, Buf, 2, '|');
440 if (StrLength(Buf2) != 0) {
441 /** Handle senders with RFC2047 encoding */
442 StrBuf_RFC822_to_Utf8(Msg->from, Buf2, WCC->DefaultCharset, FoundCharset);
446 StrBufExtract_token(Buf2, Buf, 3, '|');
447 if ((StrLength(Buf2) !=0 ) &&
448 ( ((WCC->room_flags & QR_NETWORK)
449 || ((strcasecmp(ChrPtr(Buf2), ChrPtr(WCC->serv_info->serv_nodename))
450 && (strcasecmp(ChrPtr(Buf2), ChrPtr(WCC->serv_info->serv_fqdn))))))))
452 StrBufAppendBufPlain(Msg->from, HKEY(" @ "), 0);
453 StrBufAppendBuf(Msg->from, Buf2, 0);
457 StrBufExtract_token(Msg->inetaddr, Buf, 4, '|');
460 Msg->subj = NewStrBufPlain(NULL, StrLength(Buf));
461 StrBufExtract_token(Buf2, Buf, 5, '|');
462 if (StrLength(Buf2) == 0)
463 StrBufAppendBufPlain(Msg->subj, _("(no subject)"), -1,0);
465 StrBuf_RFC822_to_Utf8(Msg->subj, Buf2, WCC->DefaultCharset, FoundCharset);
466 if ((StrLength(Msg->subj) > 75) &&
467 (StrBuf_Utf8StrLen(Msg->subj) > 75)) {
468 StrBuf_Utf8StrCut(Msg->subj, 72);
469 StrBufAppendBufPlain(Msg->subj, HKEY("..."), 0);
474 if ((StrLength(Msg->from) > 25) &&
475 (StrBuf_Utf8StrLen(Msg->from) > 25)) {
476 StrBuf_Utf8StrCut(Msg->from, 23);
477 StrBufAppendBufPlain(Msg->from, HKEY("..."), 0);
481 Put(WCC->summ, (const char *)&n, sizeof(n), Msg, DestroyMessageSummary);
491 inline message_summary* GetMessagePtrAt(int n, HashList *Summ)
499 GetHashAt(Summ, n, &HKLen, &Key, &vMsg);
500 return (message_summary*) vMsg;
504 long DrawMessageDropdown(StrBuf *Selector, long maxmsgs, long startmsg, int nMessages)
517 WCTemplputParams SubTP;
519 memset(&SubTP, 0, sizeof(WCTemplputParams));
520 SubTP.Filter.ContextType = CTX_LONGVECTOR;
521 SubTP.Context = &vector;
522 TmpBuf = NewStrBuf();
523 At = GetNewHashPos(WCC->summ, nMessages);
524 nItems = GetCount(WCC->summ);
528 vector[1] = startmsg;
532 vector[3] = abs(nMessages);
533 lo = GetHashPosCounter(At);
535 if (lo + nMessages >= nItems) {
537 vector[3] = nItems - lo;
542 hi = lo + nMessages - 1;
545 if (lo + nMessages < -1) {
549 if ((lo % abs(nMessages)) != 0) {
550 int offset = (lo % abs(nMessages) *
551 (nMessages / abs(nMessages)));
553 vector[3] = abs(offset);
561 done = !GetNextHashPos(WCC->summ, At, &hklen, &key, &vMsg);
564 * Bump these because although we're thinking in zero base, the user
565 * is a drooling idiot and is thinking in one base.
571 dbg_print_longvector(vector);
572 DoTemplate(HKEY("select_messageindex"), TmpBuf, &SubTP);
573 StrBufAppendBuf(Selector, TmpBuf, 0);
577 if (maxmsgs == 9999999) {
584 dbg_print_longvector(vector);
585 DoTemplate(HKEY("select_messageindex_all"), TmpBuf, &SubTP);
586 StrBufAppendBuf(Selector, TmpBuf, 0);
592 void load_seen_flags(void)
594 message_summary *Msg;
602 OldMsg = NewStrBuf();
604 StrBuf_ServGetln(OldMsg);
605 if (GetServerStatus(OldMsg, NULL) == 2) {
606 StrBufCutLeft(OldMsg, 4);
612 at = GetNewHashPos(WCC->summ, 0);
613 while (GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
614 /** Are you a new message, or an old message? */
615 Msg = (message_summary*) vMsg;
616 if (is_msg_in_mset(ChrPtr(OldMsg), Msg->msgnum)) {
627 extern readloop_struct rlid[];
630 * command loop for reading messages
632 * Set oper to "readnew" or "readold" or "readfwd" or "headers"
634 void readloop(long oper)
636 StrBuf *MessageDropdown = NULL;
637 StrBuf *BBViewToolBar = NULL;
639 message_summary *Msg;
643 int with_headers = 0;
647 long *displayed_msgs = NULL;
648 int num_displayed = 0;
649 int is_singlecard = 0;
652 int lowest_displayed = (-1);
653 int highest_displayed = 0;
654 addrbookent *addrbook = NULL;
661 int care_for_empty_list = 0;
664 int defaultsortorder = 0;
665 WCTemplputParams SubTP;
667 if (havebstr("is_summary") && (1 == (ibstr("is_summary"))))
668 WCC->wc_view = VIEW_MAILBOX;
671 output_headers(1, 1, 1, 0, 0, 0);
672 } else if (WCC->wc_view == VIEW_MAILBOX) {
673 jsonMessageListHdr();
676 switch (WCC->wc_view) {
678 sprintf(buf, "wiki?room=%s&page=home", ChrPtr(WCC->wc_roomname));
684 strcpy(cmd, "MSGS ALL|||1");
686 parse_calendar_view_request(&calv);
689 strcpy(cmd, "MSGS ALL");
693 strcpy(cmd, "MSGS ALL");
695 wprintf("<div id=\"new_notes_here\"></div>\n");
697 case VIEW_ADDRESSBOOK:
698 is_singlecard = ibstr("is_singlecard");
699 if (is_singlecard != 1) {
700 if (oper == do_search) {
701 snprintf(cmd, sizeof(cmd), "MSGS SEARCH|%s", bstr("query"));
704 strcpy(cmd, "MSGS ALL");
715 defaultsortorder = 2;
718 care_for_empty_list = 0;
720 /* Generally using maxmsgs|startmsg is not required
721 in mailbox view, but we have a 'safemode' for clients
722 (*cough* Exploder) that simply can't handle too many */
723 if (havebstr("maxmsgs")) maxmsgs = ibstr("maxmsgs");
724 else maxmsgs = 9999999;
725 if (havebstr("startmsg")) startmsg = lbstr("startmsg");
726 snprintf(cmd, sizeof(cmd), "MSGS %s|%s||1",
727 (oper == do_search) ? "SEARCH" : "ALL",
728 (oper == do_search) ? bstr("query") : ""
734 defaultsortorder = 1;
737 care_for_empty_list = 1;
739 rlid[oper].cmd(cmd, sizeof(cmd));
740 SetAccessCommand(oper);
742 if (havebstr("maxmsgs"))
743 maxmsgs = ibstr("maxmsgs");
744 if (maxmsgs == 0) maxmsgs = DEFAULT_MAXMSGS;
746 if (havebstr("startmsg")) {
747 startmsg = lbstr("startmsg");
752 nummsgs = load_msg_ptrs(cmd, with_headers);
754 if (care_for_empty_list) {
755 wprintf("<div class=\"nomsgs\"><br><em>");
758 wprintf(_("No new messages."));
761 wprintf(_("No old messages."));
764 wprintf(_("No messages here."));
766 wprintf("</em><br></div>\n");
774 memset(&SubTP, 0, sizeof(WCTemplputParams));
775 SubTP.Filter.ContextType = CTX_NONE;
776 SubTP.Context = NULL;
777 SortIt = RetrieveSort(&SubTP, NULL, 0,
778 HKEY("date"), defaultsortorder);
780 SortByPayload(WCC->summ, SortIt);
781 if (WCC->wc_view == VIEW_BBS) {
782 if (lbstr("SortOrder") == 2) {
784 num_displayed = -DEFAULT_MAXMSGS;
788 num_displayed = DEFAULT_MAXMSGS;
792 if (startmsg < 0) startmsg = (bbs_reverse) ? nummsgs - 1 : 0;
794 if (load_seen) load_seen_flags();
797 * If we're to print s.th. above the message list...
799 switch (WCC->wc_view) {
801 BBViewToolBar = NewStrBuf();
802 MessageDropdown = NewStrBuf();
804 maxmsgs = DrawMessageDropdown(MessageDropdown, maxmsgs, startmsg, num_displayed);
805 if (num_displayed < 0) {
807 if (num_displayed != maxmsgs)
808 maxmsgs = abs(maxmsgs) + 1;
810 maxmsgs = abs(maxmsgs);
813 memset(&SubTP, 0, sizeof(WCTemplputParams));
814 SubTP.Filter.ContextType = CTX_STRBUF;
815 SubTP.Context = MessageDropdown;
816 DoTemplate(HKEY("msg_listselector_top"), BBViewToolBar, &SubTP);
817 StrBufAppendBuf(WCC->WBuf, BBViewToolBar, 0);
818 FlushStrBuf(BBViewToolBar);
821 WCC->startmsg = startmsg;
822 WCC->maxmsgs = maxmsgs;
823 WCC->num_displayed = 0;
825 /* Put some helpful data in vars for mailsummary_json */
826 svputlong("READLOOP:TOTALMSGS", nummsgs);
827 svputlong("READLOOP:STARTMSG", startmsg);
828 svputlong("WCVIEW", WCC->wc_view);
830 * iterate over each message. if we need to load an attachment, do it here.
832 if (WCC->wc_view == VIEW_MAILBOX) goto NO_MSG_LOOP;
834 * iterate over each message. if we need to load an attachment, do it here.
836 at = GetNewHashPos(WCC->summ, 0);
837 num_displayed = i = 0;
838 while (GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
839 Msg = (message_summary*) vMsg;
840 if ((Msg->msgnum >= startmsg) && (num_displayed <= maxmsgs)) {
841 switch (WCC->wc_view) {
844 case VIEW_CALBRIEF: /* load the mime attachments for special tasks... */
846 load_calendar_item(Msg, Msg->is_new, &calv);
849 display_task(Msg, Msg->is_new);
852 display_note(Msg, Msg->is_new);
854 case VIEW_ADDRESSBOOK:
855 fetch_ab_name(Msg, buf);
857 addrbook = realloc(addrbook,
858 (sizeof(addrbookent) * num_ab) );
859 safestrncpy(addrbook[num_ab-1].ab_name, buf,
860 sizeof(addrbook[num_ab-1].ab_name));
861 addrbook[num_ab-1].ab_msgnum = Msg->msgnum;
863 case VIEW_BBS: /* Tag the mails we want to show in bbview... */
865 if (displayed_msgs == NULL) {
866 displayed_msgs = malloc(sizeof(long) *
867 (maxmsgs<nummsgs ? maxmsgs + 1 : nummsgs + 1));
869 if ((i >= startmsg) && (i < startmsg + maxmsgs)) {
870 displayed_msgs[num_displayed] = Msg->msgnum;
871 if (lowest_displayed < 0) lowest_displayed = a;
872 highest_displayed = a;
884 * Done iterating the message list. now tasks we want to do after.
886 switch (WCC->wc_view) {
888 DoTemplate(HKEY("mailsummary_json"),NULL, &SubTP);
891 if (displayed_msgs != NULL) {
892 /** if we do a split bbview in the future, begin messages div here */
894 for (a=0; a<num_displayed; ++a) {
895 read_message(WCC->WBuf, HKEY("view_message"), displayed_msgs[a], 0, NULL);
898 /** if we do a split bbview in the future, end messages div here */
900 free(displayed_msgs);
901 displayed_msgs = NULL;
903 memset(&SubTP, 0, sizeof(WCTemplputParams));
904 SubTP.Filter.ContextType = CTX_STRBUF;
905 SubTP.Context = MessageDropdown;
906 DoTemplate(HKEY("msg_listselector_bottom"), BBViewToolBar, &SubTP);
907 StrBufAppendBuf(WCC->WBuf, BBViewToolBar, 0);
909 FreeStrBuf(&BBViewToolBar);
910 FreeStrBuf(&MessageDropdown);
915 switch (WCC->wc_view) {
920 render_calendar_view(&calv);
923 do_tasks_view(); /** Render the task list */
927 case VIEW_ADDRESSBOOK:
929 read_message(WC->WBuf, HKEY("view_message"), lbstr("startmsg"), 0, NULL);
931 do_addrbook_view(addrbook, num_ab); /** Render the address book */
938 /** Note: wDumpContent() will output one additional </div> tag. */
939 if (WCC->wc_view != VIEW_MAILBOX) {
940 /* We ought to move this out into template */
947 if (WCC->summ != NULL) {
948 DeleteHash(&WCC->summ);
950 if (addrbook != NULL) free(addrbook);
951 FreeStrBuf(&BBViewToolBar);
956 * Back end for post_message()
957 * ... this is where the actual message gets transmitted to the server.
959 void post_mime_to_server(void) {
961 char top_boundary[SIZ];
962 char alt_boundary[SIZ];
963 int is_multipart = 0;
965 wc_mime_attachment *att;
967 size_t encoded_length;
968 size_t encoded_strlen;
969 char *txtmail = NULL;
971 sprintf(top_boundary, "Citadel--Multipart--%s--%04x--%04x",
972 ChrPtr(WCC->serv_info->serv_fqdn),
976 sprintf(alt_boundary, "Citadel--Multipart--%s--%04x--%04x",
977 ChrPtr(WCC->serv_info->serv_fqdn),
982 /* RFC2045 requires this, and some clients look for it... */
983 serv_puts("MIME-Version: 1.0");
984 serv_puts("X-Mailer: " PACKAGE_STRING);
986 /* If there are attachments, we have to do multipart/mixed */
987 if (GetCount(WCC->attachments) > 0) {
992 /* Remember, serv_printf() appends an extra newline */
993 serv_printf("Content-type: multipart/mixed; boundary=\"%s\"\n", top_boundary);
994 serv_printf("This is a multipart message in MIME format.\n");
995 serv_printf("--%s", top_boundary);
998 /* Remember, serv_printf() appends an extra newline */
999 serv_printf("Content-type: multipart/alternative; "
1000 "boundary=\"%s\"\n", alt_boundary);
1001 serv_printf("This is a multipart message in MIME format.\n");
1002 serv_printf("--%s", alt_boundary);
1004 serv_puts("Content-type: text/plain; charset=utf-8");
1005 serv_puts("Content-Transfer-Encoding: quoted-printable");
1007 txtmail = html_to_ascii(bstr("msgtext"), 0, 80, 0);
1008 text_to_server_qp(txtmail); /* Transmit message in quoted-printable encoding */
1011 serv_printf("--%s", alt_boundary);
1013 serv_puts("Content-type: text/html; charset=utf-8");
1014 serv_puts("Content-Transfer-Encoding: quoted-printable");
1016 serv_puts("<html><body>\r\n");
1017 text_to_server_qp(bstr("msgtext")); /* Transmit message in quoted-printable encoding */
1018 serv_puts("</body></html>\r\n");
1020 serv_printf("--%s--", alt_boundary);
1028 /* Add in the attachments */
1029 it = GetNewHashPos(WCC->attachments, 0);
1030 while (GetNextHashPos(WCC->attachments, it, &len, &Key, &vAtt)) {
1031 att = (wc_mime_attachment *)vAtt;
1032 encoded_length = ((att->length * 150) / 100);
1033 encoded = malloc(encoded_length);
1034 if (encoded == NULL) break;
1035 encoded_strlen = CtdlEncodeBase64(encoded, ChrPtr(att->Data), StrLength(att->Data), 1);
1037 serv_printf("--%s", top_boundary);
1038 serv_printf("Content-type: %s", ChrPtr(att->ContentType));
1039 serv_printf("Content-disposition: attachment; filename=\"%s\"", ChrPtr(att->FileName));
1040 serv_puts("Content-transfer-encoding: base64");
1042 serv_write(encoded, encoded_strlen);
1047 serv_printf("--%s--", top_boundary);
1056 * Post message (or don't post message)
1058 * Note regarding the "dont_post" variable:
1059 * A random value (actually, it's just a timestamp) is inserted as a hidden
1060 * field called "postseq" when the display_enter page is generated. This
1061 * value is checked when posting, using the static variable dont_post. If a
1062 * user attempts to post twice using the same dont_post value, the message is
1063 * discarded. This prevents the accidental double-saving of the same message
1064 * if the user happens to click the browser "back" button.
1066 void post_message(void)
1069 StrBuf *encoded_subject = NULL;
1070 static long dont_post = (-1L);
1071 wc_mime_attachment *att;
1072 int is_anonymous = 0;
1073 const StrBuf *display_name = NULL;
1074 wcsession *WCC = WC;
1076 if (havebstr("force_room")) {
1077 gotoroom(sbstr("force_room"));
1080 if (havebstr("display_name")) {
1081 display_name = sbstr("display_name");
1082 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1083 display_name = NULL;
1088 if (WCC->upload_length > 0) {
1093 lprintf(9, "%s:%d: we are uploading %d bytes\n", __FILE__, __LINE__, WCC->upload_length);
1094 /** There's an attachment. Save it to this struct... */
1095 att = malloc(sizeof(wc_mime_attachment));
1096 memset(att, 0, sizeof(wc_mime_attachment ));
1097 att->length = WCC->upload_length;
1098 att->ContentType = NewStrBufPlain(WCC->upload_content_type, -1);
1099 att->FileName = NewStrBufPlain(WCC->upload_filename, -1);
1102 if (WCC->attachments == NULL)
1103 WCC->attachments = NewHash(1, NULL);
1104 /* And add it to the list. */
1105 n = snprintf(N, sizeof N, "%d", GetCount(WCC->attachments) + 1);
1106 Put(WCC->attachments, N, n, att, DestroyMime);
1109 * Mozilla sends a simple filename, which is what we want,
1110 * but Satan's Browser sends an entire pathname. Reduce
1111 * the path to just a filename if we need to.
1113 pch = strrchr(ChrPtr(att->FileName), '/');
1115 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1117 pch = strrchr(ChrPtr(att->FileName), '\\');
1119 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1123 * Transfer control of this memory from the upload struct
1124 * to the attachment struct.
1126 att->Data = NewStrBufPlain(WCC->upload, WCC->upload_length);
1128 WCC->upload_length = 0;
1134 if (havebstr("cancel_button")) {
1135 sprintf(WCC->ImportantMessage,
1136 _("Cancelled. Message was not posted."));
1137 } else if (havebstr("attach_button")) {
1140 } else if (lbstr("postseq") == dont_post) {
1141 sprintf(WCC->ImportantMessage,
1142 _("Automatically cancelled because you have already "
1143 "saved this message."));
1145 const char CMD[] = "ENT0 1|%s|%d|4|%s|%s||%s|%s|%s|%s|%s";
1146 const StrBuf *Recp = NULL;
1147 const StrBuf *Cc = NULL;
1148 const StrBuf *Bcc = NULL;
1149 const StrBuf *Wikipage = NULL;
1150 const StrBuf *my_email_addr = NULL;
1151 StrBuf *CmdBuf = NULL;
1152 StrBuf *references = NULL;
1154 if (havebstr("references"))
1156 const StrBuf *ref = sbstr("references");
1157 references = NewStrBufPlain(ChrPtr(ref), StrLength(ref));
1158 lprintf(9, "Converting: %s\n", ChrPtr(references));
1159 StrBufReplaceChars(references, '|', '!');
1160 lprintf(9, "Converted: %s\n", ChrPtr(references));
1162 if (havebstr("subject")) {
1165 * make enough room for the encoded string;
1166 * plus the QP header
1168 Subj = sbstr("subject");
1170 StrBufRFC2047encode(&encoded_subject, Subj);
1172 Recp = sbstr("recp");
1175 Wikipage = sbstr("wikipage");
1176 my_email_addr = sbstr("my_email_addr");
1178 CmdBuf = NewStrBufPlain(NULL,
1181 StrLength(encoded_subject) +
1184 StrLength(Wikipage) +
1185 StrLength(my_email_addr) +
1186 StrLength(references));
1188 StrBufPrintf(CmdBuf,
1192 ChrPtr(encoded_subject),
1193 ChrPtr(display_name),
1197 ChrPtr(my_email_addr),
1198 ChrPtr(references));
1199 FreeStrBuf(&references);
1201 lprintf(9, "%s\n", ChrPtr(CmdBuf));
1202 serv_puts(ChrPtr(CmdBuf));
1203 serv_getln(buf, sizeof buf);
1204 FreeStrBuf(&CmdBuf);
1205 FreeStrBuf(&encoded_subject);
1206 if (buf[0] == '4') {
1207 post_mime_to_server();
1208 if ( (havebstr("recp"))
1209 || (havebstr("cc" ))
1210 || (havebstr("bcc" ))
1212 sprintf(WCC->ImportantMessage, _("Message has been sent.\n"));
1215 sprintf(WC->ImportantMessage, _("Message has been posted.\n"));
1217 dont_post = lbstr("postseq");
1219 lprintf(9, "%s:%d: server post error: %s\n", __FILE__, __LINE__, buf);
1220 sprintf(WC->ImportantMessage, "%s", &buf[4]);
1226 DeleteHash(&WCC->attachments);
1229 * We may have been supplied with instructions regarding the location
1230 * to which we must return after posting. If found, go there.
1232 if (havebstr("return_to")) {
1233 http_redirect(bstr("return_to"));
1236 * If we were editing a page in a wiki room, go to that page now.
1238 else if (havebstr("wikipage")) {
1239 snprintf(buf, sizeof buf, "wiki?page=%s", bstr("wikipage"));
1243 * Otherwise, just go to the "read messages" loop.
1254 * \brief display the message entry screen
1256 void display_enter(void)
1260 const StrBuf *display_name = NULL;
1261 int recipient_required = 0;
1262 int subject_required = 0;
1263 int recipient_bad = 0;
1264 int is_anonymous = 0;
1265 wcsession *WCC = WC;
1269 if (havebstr("force_room")) {
1270 gotoroom(sbstr("force_room"));
1273 display_name = sbstr("display_name");
1274 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1275 display_name = NULL;
1279 /** First test to see whether this is a room that requires recipients to be entered */
1280 serv_puts("ENT0 0");
1281 serv_getln(buf, sizeof buf);
1283 if (!strncmp(buf, "570", 3)) { /** 570 means that we need a recipient here */
1284 recipient_required = 1;
1286 else if (buf[0] != '2') { /** Any other error means that we cannot continue */
1287 sprintf(WCC->ImportantMessage, "%s", &buf[4]);
1292 /* Is the server strongly recommending that the user enter a message subject? */
1293 if ((buf[3] != '\0') && (buf[4] != '\0')) {
1294 subject_required = extract_int(&buf[4], 1);
1298 * Are we perhaps in an address book view? If so, then an "enter
1299 * message" command really means "add new entry."
1301 if (WCC->wc_default_view == VIEW_ADDRESSBOOK) {
1302 do_edit_vcard(-1, "", "", ChrPtr(WCC->wc_roomname));
1307 * Are we perhaps in a calendar room? If so, then an "enter
1308 * message" command really means "add new calendar item."
1310 if (WCC->wc_default_view == VIEW_CALENDAR) {
1311 display_edit_event();
1316 * Are we perhaps in a tasks view? If so, then an "enter
1317 * message" command really means "add new task."
1319 if (WCC->wc_default_view == VIEW_TASKS) {
1320 display_edit_task();
1325 * Otherwise proceed normally.
1326 * Do a custom room banner with no navbar...
1329 if (recipient_required) {
1330 const StrBuf *Recp = NULL;
1331 const StrBuf *Cc = NULL;
1332 const StrBuf *Bcc = NULL;
1333 const StrBuf *Wikipage = NULL;
1334 StrBuf *CmdBuf = NULL;
1335 const char CMD[] = "ENT0 0|%s|%d|0||%s||%s|%s|%s";
1337 Recp = sbstr("recp");
1340 Wikipage = sbstr("wikipage");
1342 CmdBuf = NewStrBufPlain(NULL,
1345 StrLength(display_name) +
1348 StrLength(Wikipage));
1350 StrBufPrintf(CmdBuf,
1354 ChrPtr(display_name),
1358 serv_puts(ChrPtr(CmdBuf));
1359 serv_getln(buf, sizeof buf);
1360 FreeStrBuf(&CmdBuf);
1362 if (!strncmp(buf, "570", 3)) { /** 570 means we have an invalid recipient listed */
1363 if (havebstr("recp") &&
1369 else if (buf[0] != '2') { /** Any other error means that we cannot continue */
1370 wprintf("<em>%s</em><br />\n", &buf[4]);/*TODO -> important message */
1374 svputlong("RCPTREQUIRED", recipient_required);
1375 svputlong("SUBJREQUIRED", recipient_required || subject_required);
1378 output_headers(1, 0, 0, 0, 1, 0);
1379 DoTemplate(HKEY("edit_message"), NULL, &NoCtx);
1386 * \brief delete a message
1388 void delete_msg(void)
1393 msgid = lbstr("msgid");
1395 if (WC->wc_is_trash) { /** Delete from Trash is a real delete */
1396 serv_printf("DELE %ld", msgid);
1398 else { /** Otherwise move it to Trash */
1399 serv_printf("MOVE %ld|_TRASH_|0", msgid);
1402 serv_getln(buf, sizeof buf);
1403 sprintf(WC->ImportantMessage, "%s", &buf[4]);
1410 * \brief move a message to another folder
1417 msgid = lbstr("msgid");
1419 if (havebstr("move_button")) {
1420 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
1422 serv_getln(buf, sizeof buf);
1423 sprintf(WC->ImportantMessage, "%s", &buf[4]);
1425 sprintf(WC->ImportantMessage, (_("The message was not moved.")));
1436 * Confirm move of a message
1438 void confirm_move_msg(void)
1444 msgid = lbstr("msgid");
1447 output_headers(1, 1, 2, 0, 0, 0);
1448 wprintf("<div id=\"banner\">\n");
1450 wprintf(_("Confirm move of message"));
1452 wprintf("</div>\n");
1454 wprintf("<div id=\"content\" class=\"service\">\n");
1456 wprintf("<CENTER>");
1458 wprintf(_("Move this message to:"));
1459 wprintf("<br />\n");
1461 wprintf("<form METHOD=\"POST\" action=\"move_msg\">\n");
1462 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
1463 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n", bstr("msgid"));
1465 wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
1467 serv_getln(buf, sizeof buf);
1468 if (buf[0] == '1') {
1469 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1470 extract_token(targ, buf, 0, '|', sizeof targ);
1471 wprintf("<OPTION>");
1476 wprintf("</SELECT>\n");
1477 wprintf("<br />\n");
1479 wprintf("<INPUT TYPE=\"submit\" NAME=\"move_button\" VALUE=\"%s\">", _("Move"));
1481 wprintf("<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">", _("Cancel"));
1482 wprintf("</form></CENTER>\n");
1484 wprintf("</CENTER>\n");
1490 * Generic function to output an arbitrary MIME attachment from
1491 * message being composed
1493 * partnum The MIME part to be output
1494 * filename Fake filename to give
1495 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1497 void postpart(StrBuf *partnum, StrBuf *filename, int force_download)
1500 StrBuf *content_type;
1501 wc_mime_attachment *part;
1503 if (GetHash(WC->attachments, SKEY(partnum), &vPart) &&
1505 part = (wc_mime_attachment*) vPart;
1506 if (force_download) {
1507 content_type = NewStrBufPlain(HKEY("application/octet-stream"));
1510 content_type = NewStrBufDup(part->ContentType);
1512 output_headers(0, 0, 0, 0, 0, 0);
1513 StrBufAppendBuf(WC->WBuf, part->Data, 0);
1514 http_transmit_thing(ChrPtr(content_type), 0);
1516 hprintf("HTTP/1.1 404 %s\n", ChrPtr(partnum));
1517 output_headers(0, 0, 0, 0, 0, 0);
1518 hprintf("Content-Type: text/plain\r\n");
1519 wprintf(_("An error occurred while retrieving this part: %s/%s\n"),
1520 ChrPtr(partnum), ChrPtr(filename));
1523 FreeStrBuf(&content_type);
1528 * Generic function to output an arbitrary MIME part from an arbitrary
1529 * message number on the server.
1531 * msgnum Number of the item on the citadel server
1532 * partnum The MIME part to be output
1533 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1535 void mimepart(int force_download)
1537 wcsession *WCC = WC;
1541 char content_type[256];
1542 const char *ContentType = &content_type[0];
1544 serv_printf("OPNA %s|%s", ChrPtr(WCC->UrlFragment2), ChrPtr(WCC->UrlFragment3));
1545 serv_getln(buf, sizeof buf);
1546 if (buf[0] == '2') {
1547 bytes = extract_long(&buf[4], 0);
1548 if (force_download) {
1549 strcpy(content_type, "application/octet-stream");
1552 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
1555 read_server_binary(WCC->WBuf, bytes);
1557 serv_getln(buf, sizeof buf);
1559 if (!force_download) {
1560 if (!strcasecmp(ContentType, "application/octet-stream")) {
1561 ContentType = GuessMimeByFilename(SKEY(WCC->UrlFragment4));
1563 if (!strcasecmp(ContentType, "application/octet-stream")) {
1564 ContentType = GuessMimeType(SKEY(WCC->WBuf));
1567 output_headers(0, 0, 0, 0, 0, 0);
1568 http_transmit_thing(ContentType, 0);
1570 hprintf("HTTP/1.1 404 %s\n", &buf[4]);
1571 output_headers(0, 0, 0, 0, 0, 0);
1572 hprintf("Content-Type: text/plain\r\n");
1573 wprintf(_("An error occurred while retrieving this part: %s\n"), &buf[4]);
1580 * Read any MIME part of a message, from the server, into memory.
1582 char *load_mimepart(long msgnum, char *partnum)
1586 char content_type[SIZ];
1589 serv_printf("DLAT %ld|%s", msgnum, partnum);
1590 serv_getln(buf, sizeof buf);
1591 if (buf[0] == '6') {
1592 bytes = extract_long(&buf[4], 0);
1593 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
1595 content = malloc(bytes + 2);
1596 serv_read(content, bytes);
1598 content[bytes] = 0; /* null terminate for good measure */
1607 * Read any MIME part of a message, from the server, into memory.
1609 void MimeLoadData(wc_mime_attachment *Mime)
1613 /* TODO: is there a chance the contenttype is different to the one we know? */
1614 serv_printf("DLAT %ld|%s", Mime->msgnum, ChrPtr(Mime->PartNum));
1615 serv_getln(buf, sizeof buf);
1616 if (buf[0] == '6') {
1617 bytes = extract_long(&buf[4], 0);
1619 if (Mime->Data == NULL)
1620 Mime->Data = NewStrBufPlain(NULL, bytes);
1621 StrBuf_ServGetBLOB(Mime->Data, bytes);
1625 FlushStrBuf(Mime->Data);
1626 /* TODO XImportant message */
1633 void view_mimepart(void) {
1637 void download_mimepart(void) {
1641 void view_postpart(void) {
1642 postpart(WC->UrlFragment2,
1647 void download_postpart(void) {
1648 postpart(WC->UrlFragment2,
1653 void h_readnew(void) { readloop(readnew);}
1654 void h_readold(void) { readloop(readold);}
1655 void h_readfwd(void) { readloop(readfwd);}
1656 void h_headers(void) { readloop(headers);}
1657 void h_do_search(void) { readloop(do_search);}
1659 void jsonMessageListHdr(void)
1661 /* TODO: make a generic function */
1662 hprintf("HTTP/1.1 200 OK\r\n");
1663 hprintf("Content-type: application/json; charset=utf-8\r\n");
1664 hprintf("Server: %s / %s\r\n", PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software));
1665 hprintf("Connection: close\r\n");
1666 hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
1669 /* Spit out the new summary view. This is basically a static page, so clients can cache the layout, all the dirty work is javascript :) */
1670 void new_summary_view(void) {
1672 DoTemplate(HKEY("msg_listview"),NULL,&NoCtx);
1673 DoTemplate(HKEY("trailing"),NULL,&NoCtx);
1676 /** Output message list in JSON-format */
1677 void jsonMessageList(void) {
1678 const StrBuf *room = sbstr("room");
1679 long oper = (havebstr("query")) ? do_search : readnew;
1690 RegisterPreference("use_sig",
1691 _("Attach signature to email messages?"),
1694 RegisterPreference("signature", _("Use this signature:"), PRF_QP_STRING, NULL);
1695 RegisterPreference("default_header_charset",
1696 _("Default character set for email headers:"),
1699 RegisterPreference("defaultfrom", _("Preferred email address"), PRF_STRING, NULL);
1700 RegisterPreference("defaultname",
1701 _("Preferred display name for email messages"),
1704 RegisterPreference("defaulthandle",
1705 _("Preferred display name for bulletin board posts"),
1708 RegisterPreference("mailbox",_("Mailbox view mode"), PRF_STRING, NULL);
1710 WebcitAddUrlHandler(HKEY("readnew"), h_readnew, NEED_URL);
1711 WebcitAddUrlHandler(HKEY("readold"), h_readold, NEED_URL);
1712 WebcitAddUrlHandler(HKEY("readfwd"), h_readfwd, NEED_URL);
1713 WebcitAddUrlHandler(HKEY("headers"), h_headers, NEED_URL);
1714 WebcitAddUrlHandler(HKEY("do_search"), h_do_search, 0);
1715 WebcitAddUrlHandler(HKEY("display_enter"), display_enter, 0);
1716 WebcitAddUrlHandler(HKEY("post"), post_message, 0);
1717 WebcitAddUrlHandler(HKEY("move_msg"), move_msg, 0);
1718 WebcitAddUrlHandler(HKEY("delete_msg"), delete_msg, 0);
1719 WebcitAddUrlHandler(HKEY("confirm_move_msg"), confirm_move_msg, 0);
1720 WebcitAddUrlHandler(HKEY("msg"), embed_message, NEED_URL|AJAX);
1721 WebcitAddUrlHandler(HKEY("printmsg"), print_message, NEED_URL);
1722 WebcitAddUrlHandler(HKEY("mobilemsg"), mobile_message_view, NEED_URL);
1723 WebcitAddUrlHandler(HKEY("msgheaders"), display_headers, NEED_URL);
1725 WebcitAddUrlHandler(HKEY("mimepart"), view_mimepart, NEED_URL);
1726 WebcitAddUrlHandler(HKEY("mimepart_download"), download_mimepart, NEED_URL);
1727 WebcitAddUrlHandler(HKEY("postpart"), view_postpart, NEED_URL);
1728 WebcitAddUrlHandler(HKEY("postpart_download"), download_postpart, NEED_URL);
1731 WebcitAddUrlHandler(HKEY("roommsgs"), jsonMessageList,0);