4 * Functions which deal with the fetching and displaying of messages.
12 HashList *MsgHeaderHandler = NULL;
13 HashList *MsgEvaluators = NULL;
14 HashList *MimeRenderHandler = NULL;
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 */
23 void display_enter(void);
24 int longcmp_r(const void *s1, const void *s2);
25 int summcmp_subj(const void *s1, const void *s2);
26 int summcmp_rsubj(const void *s1, const void *s2);
27 int summcmp_sender(const void *s1, const void *s2);
28 int summcmp_rsender(const void *s1, const void *s2);
29 int summcmp_date(const void *s1, const void *s2);
30 int summcmp_rdate(const void *s1, const void *s2);
32 /*----------------------------------------------------------------------------*/
35 typedef void (*MsgPartEvaluatorFunc)(message_summary *Sum, StrBuf *Buf);
39 /*----------------------------------------------------------------------------*/
44 * I wanna SEE that message!
46 * msgnum Message number to display
47 * printable_view Nonzero to display a printable view
48 * section Optional for encapsulated message/rfc822 submessage
50 int read_message(StrBuf *Target, const char *tmpl, long tmpllen, long msgnum, int printable_view, const StrBuf *PartNum) {
56 message_summary *Msg = NULL;
66 lprintf(1, "----------%s---------MSG4 %ld|%s--------------\n", tmpl, msgnum, ChrPtr(PartNum));
67 serv_printf("MSG4 %ld|%s", msgnum, ChrPtr(PartNum));
68 StrBuf_ServGetln(Buf);
69 if (GetServerStatus(Buf, NULL) != 1) {
70 StrBufAppendPrintf(Target, "<strong>");
71 StrBufAppendPrintf(Target, _("ERROR:"));
72 StrBufAppendPrintf(Target, "</strong> %s<br />\n", &buf[4]);
77 /** begin everythingamundo table */
80 HdrToken = NewStrBuf();
81 Msg = (message_summary *)malloc(sizeof(message_summary));
82 memset(Msg, 0, sizeof(message_summary));
84 Msg->PartNum = PartNum;
85 Msg->MsgBody = (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
86 memset(Msg->MsgBody, 0, sizeof(wc_mime_attachment));
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 (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 lprintf(1, "don't know how to handle message header[%s]\n", ChrPtr(HdrToken));
127 case 1:/* Message Mime Header */
128 if (StrLength(Buf) == 0) {
130 if (Msg->MsgBody->ContentType == NULL)
131 /* end of header or no header? */
132 Msg->MsgBody->ContentType = NewStrBufPlain(HKEY("text/plain"));
133 /* usual end of mime header */
137 StrBufExtract_token(HdrToken, Buf, 0, ':');
138 if (StrLength(HdrToken) > 0) {
139 StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
141 if (analyze_msg) lprintf(1, ":: [%s] = [%s]\n", ChrPtr(HdrToken), ChrPtr(Buf));
143 /* the examine*'s know how to do with mime headers too... */
144 if (GetHash(MsgHeaderHandler, SKEY(HdrToken), &vHdr) &&
146 Hdr = (headereval*)vHdr;
147 Hdr->evaluator(Msg, Buf, FoundCharset);
152 case 2: /* Message Body */
154 if (Msg->MsgBody->size_known > 0) {
155 StrBuf_ServGetBLOB(Msg->MsgBody->Data, Msg->MsgBody->length);
157 /// todo: check next line, if not 000, append following lines
160 if (StrLength(Msg->MsgBody->Data) > 0)
161 StrBufAppendBufPlain(Msg->MsgBody->Data, "\n", 1, 0);
162 StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
166 StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
171 if (Msg->AllAttach == NULL)
172 Msg->AllAttach = NewHash(1,NULL);
173 /* now we put the body mimepart we read above into the mimelist */
174 Put(Msg->AllAttach, SKEY(Msg->MsgBody->PartNum), Msg->MsgBody, DestroyMime);
176 /* strip the bare contenttype, so we ommit charset etc. */
177 StrBufExtract_token(Buf, Msg->MsgBody->ContentType, 0, ';');
179 /* look up the renderer, that will convert this mimeitem into the htmlized form */
180 if (GetHash(MimeRenderHandler, SKEY(Buf), &vHdr) &&
182 RenderMimeFunc Render;
183 Render = (RenderMimeFunc)vHdr;
184 Render(Msg->MsgBody, NULL, FoundCharset);
187 if (StrLength(Msg->reply_references)> 0) {
188 /* Trim down excessively long lists of thread references. We eliminate the
189 * second one in the list so that the thread root remains intact.
191 int rrtok = num_tokens(ChrPtr(Msg->reply_references), '|');
192 int rrlen = StrLength(Msg->reply_references);
193 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
194 StrBufRemove_token(Msg->reply_references, 1, '|');
198 /* Generate a reply-to address */
199 if (StrLength(Msg->Rfca) > 0) {
200 if (Msg->reply_to == NULL)
201 Msg->reply_to = NewStrBuf();
202 if (StrLength(Msg->from) > 0) {
203 StrBufPrintf(Msg->reply_to, "%s <%s>", ChrPtr(Msg->from), ChrPtr(Msg->Rfca));
206 FlushStrBuf(Msg->reply_to);
207 StrBufAppendBuf(Msg->reply_to, Msg->Rfca, 0);
212 if ((StrLength(Msg->OtherNode)>0) &&
213 (strcasecmp(ChrPtr(Msg->OtherNode), serv_info.serv_nodename)) &&
214 (strcasecmp(ChrPtr(Msg->OtherNode), serv_info.serv_humannode)) )
216 if (Msg->reply_to == NULL)
217 Msg->reply_to = NewStrBuf();
218 StrBufPrintf(Msg->reply_to,
221 ChrPtr(Msg->OtherNode));
224 if (Msg->reply_to == NULL)
225 Msg->reply_to = NewStrBuf();
226 FlushStrBuf(Msg->reply_to);
227 StrBufAppendBuf(Msg->reply_to, Msg->from, 0);
231 /* now check if we need to translate some mimeparts, and remove the duplicate */
232 it = GetNewHashPos(Msg->AllAttach, 0);
233 while (GetNextHashPos(Msg->AllAttach, it, &len, &Key, &vMime) &&
235 wc_mime_attachment *Mime = (wc_mime_attachment*) vMime;
236 evaluate_mime_part(Msg, Mime);
240 DoTemplate(tmpl, tmpllen, Target, Msg, CTX_MAILSUM);
242 DestroyMessageSummary(Msg);
243 FreeStrBuf(&FoundCharset);
244 FreeStrBuf(&HdrToken);
252 * Unadorned HTML output of an individual message, suitable
253 * for placing in a hidden iframe, for printing, or whatever
255 * msgnum_as_string == Message number, as a string instead of as a long int
257 void embed_message(void) {
259 const StrBuf *Tmpl = sbstr("template");
261 msgnum = StrTol(WC->UrlFragment2);
262 if (StrLength(Tmpl) > 0)
263 read_message(WC->WBuf, SKEY(Tmpl), msgnum, 0, NULL);
265 read_message(WC->WBuf, HKEY("view_message"), msgnum, 0, NULL);
270 * Printable view of a message
272 * msgnum_as_string == Message number, as a string instead of as a long int
274 void print_message(void) {
277 msgnum = StrTol(WC->UrlFragment2);
278 output_headers(0, 0, 0, 0, 0, 0);
280 hprintf("Content-type: text/html\r\n"
281 "Server: " PACKAGE_STRING "\r\n"
282 "Connection: close\r\n");
286 read_message(WC->WBuf, HKEY("view_message_print"), msgnum, 1, NULL);
292 * Mobile browser view of message
294 * @param msg_num_as_string Message number as a string instead of as a long int
296 void mobile_message_view(void) {
298 msgnum = StrTol(WC->UrlFragment2);
299 output_headers(1, 0, 0, 0, 0, 1);
301 do_template("msgcontrols", NULL);
302 read_message(WC->WBuf, HKEY("view_message"), msgnum,1, NULL);
307 * \brief Display a message's headers
309 * \param msgnum_as_string Message number, as a string instead of as a long int
311 void display_headers(void) {
315 msgnum = StrTol(WC->UrlFragment2);
316 output_headers(0, 0, 0, 0, 0, 0);
318 hprintf("Content-type: text/plain\r\n"
320 "Connection: close\r\n",
324 serv_printf("MSG2 %ld|3", msgnum);
325 serv_getln(buf, sizeof buf);
327 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
328 wprintf("%s\n", buf);
336 message_summary *ReadOneMessageSummary(StrBuf *RawMessage, const char *DefaultSubject, long MsgNum)
339 MsgPartEvaluatorFunc Eval;
340 message_summary *Msg;
349 serv_printf("MSG0 %ld|1", MsgNum); /* ask for headers only */
351 StrBuf_ServGetln(Buf);
352 if (GetServerStatus(Buf, NULL) == 1) {
357 Msg = (message_summary*)malloc(sizeof(message_summary));
358 memset(Msg, 0, sizeof(message_summary));
359 while (len = StrBuf_ServGetln(Buf),
361 strcmp(ChrPtr(Buf), "000")== 0)){
363 ebuf = strchr(ChrPtr(Buf), '=');
365 if (GetHash(MsgEvaluators, buf, nBuf, &vEval) && vEval != NULL) {
366 Eval = (MsgPartEvaluatorFunc) vEval;
367 StrBufCutLeft(Buf, nBuf + 1);
370 else lprintf(1, "Don't know how to handle Message Headerline [%s]", ChrPtr(Buf));
380 * load message pointers from the server for a "read messages" operation
382 * servcmd: the citadel command to send to the citserver
383 * with_headers: also include some of the headers with the message numbers (more expensive)
385 int load_msg_ptrs(char *servcmd, int with_headers)
387 StrBuf* FoundCharset = NULL;
389 message_summary *Msg;
397 if (WCC->summ != NULL) {
398 if (WCC->summ != NULL)
399 DeleteHash(&WCC->summ);
401 WCC->summ = NewHash(1, Flathash);
406 StrBuf_ServGetln(Buf);
407 if (GetServerStatus(Buf, NULL) != 1) {
411 // TODO if (with_headers) { //// TODO: Have Attachments?
413 while (len = StrBuf_ServGetln(Buf),
415 strcmp(ChrPtr(Buf), "000")!= 0))
417 if (nummsgs < maxload) {
419 Msg = (message_summary*)malloc(sizeof(message_summary));
420 memset(Msg, 0, sizeof(message_summary));
422 Msg->msgnum = StrBufExtract_long(Buf, 0, '|');
423 Msg->date = StrBufExtract_long(Buf, 1, '|');
425 * as citserver probably gives us messages in forward date sorting
426 * nummsgs should be the same order as the message date.
428 if (Msg->date == 0) {
430 if (StrLength(Buf) < 32)
434 Msg->from = NewStrBufPlain(NULL, StrLength(Buf));
435 StrBufExtract_token(Buf2, Buf, 2, '|');
436 if (StrLength(Buf2) != 0) {
437 /** Handle senders with RFC2047 encoding */
438 StrBuf_RFC822_to_Utf8(Msg->from, Buf2, WCC->DefaultCharset, FoundCharset);
442 StrBufExtract_token(Buf2, Buf, 3, '|');
443 if ((StrLength(Buf2) !=0 ) &&
444 ( ((WCC->room_flags & QR_NETWORK)
445 || ((strcasecmp(ChrPtr(Buf2), serv_info.serv_nodename)
446 && (strcasecmp(ChrPtr(Buf2), serv_info.serv_fqdn)))))))
448 StrBufAppendBufPlain(Msg->from, HKEY(" @ "), 0);
449 StrBufAppendBuf(Msg->from, Buf2, 0);
453 StrBufExtract_token(Msg->inetaddr, Buf, 4, '|');
456 Msg->subj = NewStrBufPlain(NULL, StrLength(Buf));
457 StrBufExtract_token(Buf2, Buf, 5, '|');
458 if (StrLength(Buf2) == 0)
459 StrBufAppendBufPlain(Msg->subj, _("(no subj)"), -1,0);
461 StrBuf_RFC822_to_Utf8(Msg->subj, Buf2, WCC->DefaultCharset, FoundCharset);
462 if ((StrLength(Msg->subj) > 75) &&
463 (StrBuf_Utf8StrLen(Msg->subj) > 75)) {
464 StrBuf_Utf8StrCut(Msg->subj, 72);
465 StrBufAppendBufPlain(Msg->subj, HKEY("..."), 0);
470 if ((StrLength(Msg->from) > 25) &&
471 (StrBuf_Utf8StrLen(Msg->from) > 25)) {
472 StrBuf_Utf8StrCut(Msg->from, 23);
473 StrBufAppendBufPlain(Msg->from, HKEY("..."), 0);
477 Put(WCC->summ, (const char *)&n, sizeof(n), Msg, DestroyMessageSummary);
487 inline message_summary* GetMessagePtrAt(int n, HashList *Summ)
495 GetHashAt(Summ, n, &HKLen, &Key, &vMsg);
496 return (message_summary*) vMsg;
500 long DrawMessageDropdown(StrBuf *Selector, long maxmsgs, long startmsg, int nMessages)
514 TmpBuf = NewStrBuf();
515 At = GetNewHashPos(WCC->summ, nMessages);
516 nItems = GetCount(WCC->summ);
520 vector[1] = startmsg;
524 vector[3] = abs(nMessages);
525 lo = GetHashPosCounter(At);
527 if (lo + nMessages >= nItems) {
529 vector[3] = nItems - lo;
534 hi = lo + nMessages - 1;
537 if (lo + nMessages < -1) {
541 if ((lo % abs(nMessages)) != 0) {
542 int offset = (lo % abs(nMessages) *
543 (nMessages / abs(nMessages)));
545 vector[3] = abs(offset);
553 done = !GetNextHashPos(WCC->summ, At, &hklen, &key, &vMsg);
556 * Bump these because although we're thinking in zero base, the user
557 * is a drooling idiot and is thinking in one base.
563 dbg_print_longvector(vector);
564 DoTemplate(HKEY("select_messageindex"), TmpBuf, &vector, CTX_LONGVECTOR);
565 StrBufAppendBuf(Selector, TmpBuf, 0);
569 vector[1] = lbstr("maxmsgs") == 9999999;
571 dbg_print_longvector(vector);
572 DoTemplate(HKEY("select_messageindex_all"), TmpBuf, &vector, CTX_LONGVECTOR);
573 StrBufAppendBuf(Selector, TmpBuf, 0);
579 void load_seen_flags(void)
581 message_summary *Msg;
584 StrBuf *OldMsg = NewStrBuf();;
590 StrBuf_ServGetln(OldMsg);
591 if (ChrPtr(OldMsg)[0] == '2') {
592 StrBufCutLeft(OldMsg, 4);
598 at = GetNewHashPos(WCC->summ, 0);
599 while (GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
600 /** Are you a new message, or an old message? */
601 Msg = (message_summary*) vMsg;
602 if (is_msg_in_mset(ChrPtr(OldMsg), Msg->msgnum)) {
613 extern readloop_struct rlid[];
616 * command loop for reading messages
618 * Set oper to "readnew" or "readold" or "readfwd" or "headers"
620 void readloop(long oper)
622 StrBuf *MessageDropdown = NULL;
623 StrBuf *BBViewToolBar = NULL;
625 message_summary *Msg;
629 int with_headers = 0;
633 long *displayed_msgs = NULL;
634 int num_displayed = 0;
635 int is_singlecard = 0;
638 int lowest_displayed = (-1);
639 int highest_displayed = 0;
640 addrbookent *addrbook = NULL;
647 int care_for_empty_list = 0;
650 int defaultsortorder = 0;
652 if (havebstr("is_summary") && (1 == (ibstr("is_summary"))))
653 WCC->wc_view = VIEW_MAILBOX;
655 switch (WCC->wc_view) {
657 sprintf(buf, "wiki?room=%s&page=home", WCC->wc_roomname);
663 strcpy(cmd, "MSGS ALL|||1");
665 parse_calendar_view_request(&calv);
668 strcpy(cmd, "MSGS ALL");
672 strcpy(cmd, "MSGS ALL");
674 wprintf("<div id=\"new_notes_here\"></div>\n");
676 case VIEW_ADDRESSBOOK:
677 is_singlecard = ibstr("is_singlecard");
678 if (is_singlecard != 1) {
679 if (oper == do_search) {
680 snprintf(cmd, sizeof(cmd), "MSGS SEARCH|%s", bstr("query"));
683 strcpy(cmd, "MSGS ALL");
690 defaultsortorder = 2;
693 care_for_empty_list = 1;
695 if (WCC->is_mobile) maxmsgs = 20;
696 else maxmsgs = 9999999;
697 snprintf(cmd, sizeof(cmd), "MSGS %s|%s||1",
698 (oper == do_search) ? "SEARCH" : "ALL",
699 (oper == do_search) ? bstr("query") : ""
704 defaultsortorder = 1;
707 care_for_empty_list = 1;
709 rlid[oper].cmd(cmd, sizeof(cmd));
710 SetAccessCommand(oper);
712 if (havebstr("maxmsgs"))
713 maxmsgs = ibstr("maxmsgs");
714 if (maxmsgs == 0) maxmsgs = DEFAULT_MAXMSGS;
716 if (havebstr("startmsg")) {
717 startmsg = lbstr("startmsg");
721 output_headers(1, 1, 1, 0, 0, 0);
723 nummsgs = load_msg_ptrs(cmd, with_headers);
725 if (care_for_empty_list) {
726 wprintf("<div align=\"center\"><br /><em>");
729 wprintf(_("No new messages."));
732 wprintf(_("No old messages."));
735 wprintf(_("No messages here."));
737 wprintf("</em><br /></div>\n");
745 SortIt = RetrieveSort(CTX_MAILSUM, NULL,
746 HKEY("date"), defaultsortorder);
748 SortByPayload(WCC->summ, SortIt);
749 if (WCC->wc_view == VIEW_BBS) {
750 if (lbstr("SortOrder") == 2) {
752 num_displayed = -DEFAULT_MAXMSGS;
756 num_displayed = DEFAULT_MAXMSGS;
760 if (startmsg < 0) startmsg = (bbs_reverse) ? nummsgs - 1 : 0;
762 if (load_seen) load_seen_flags();
765 * If we're to print s.th. above the message list...
767 switch (WCC->wc_view) {
769 do_template("summary_header", NULL);
772 BBViewToolBar = NewStrBuf();
773 MessageDropdown = NewStrBuf();
775 maxmsgs = DrawMessageDropdown(MessageDropdown, maxmsgs, startmsg, num_displayed);
776 if (num_displayed < 0) {
778 if (num_displayed != maxmsgs)
779 maxmsgs = abs(maxmsgs) + 1;
781 maxmsgs = abs(maxmsgs);
784 DoTemplate(HKEY("msg_listselector_top"), BBViewToolBar, MessageDropdown, CTX_STRBUF);
785 StrBufAppendBuf(WCC->WBuf, BBViewToolBar, 0);
786 FlushStrBuf(BBViewToolBar);
791 * iterate over each message. if we need to load an attachment, do it here.
793 at = GetNewHashPos(WCC->summ, 0);
794 num_displayed = i = 0;
795 while (GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
796 Msg = (message_summary*) vMsg;
797 if ((Msg->msgnum >= startmsg) && (num_displayed <= maxmsgs)) {
798 switch (WCC->wc_view) {
801 case VIEW_CALBRIEF: /* load the mime attachments for special tasks... */
803 load_calendar_item(Msg, Msg->is_new, &calv);
806 display_task(Msg, Msg->is_new);
809 display_note(Msg, Msg->is_new);
811 case VIEW_ADDRESSBOOK:
812 fetch_ab_name(Msg, buf);
814 addrbook = realloc(addrbook,
815 (sizeof(addrbookent) * num_ab) );
816 safestrncpy(addrbook[num_ab-1].ab_name, buf,
817 sizeof(addrbook[num_ab-1].ab_name));
818 addrbook[num_ab-1].ab_msgnum = Msg->msgnum;
820 case VIEW_MAILBOX: /* here we just need the abstract, so render it now. */
821 DoTemplate(HKEY("section_mailsummary"), NULL, Msg, CTX_MAILSUM);
825 case VIEW_BBS: /* Tag the mails we want to show in bbview... */
827 if (displayed_msgs == NULL) {
828 displayed_msgs = malloc(sizeof(long) *
829 (maxmsgs<nummsgs ? maxmsgs + 1 : nummsgs + 1));
831 if ((i >= startmsg) && (i < startmsg + maxmsgs)) {
832 displayed_msgs[num_displayed] = Msg->msgnum;
833 if (lowest_displayed < 0) lowest_displayed = a;
834 highest_displayed = a;
846 * Done iterating the message list. now tasks we want to do after.
848 switch (WCC->wc_view) {
850 do_template("summary_trailer", NULL);
853 if (displayed_msgs != NULL) {
854 /** if we do a split bbview in the future, begin messages div here */
856 for (a=0; a<num_displayed; ++a) {
857 read_message(WCC->WBuf, HKEY("view_message"), displayed_msgs[a], 0, NULL);
860 /** if we do a split bbview in the future, end messages div here */
862 free(displayed_msgs);
863 displayed_msgs = NULL;
865 DoTemplate(HKEY("msg_listselector_bottom"), BBViewToolBar, MessageDropdown, CTX_STRBUF);
866 StrBufAppendBuf(WCC->WBuf, BBViewToolBar, 0);
868 FreeStrBuf(&BBViewToolBar);
869 FreeStrBuf(&MessageDropdown);
874 switch (WCC->wc_view) {
879 render_calendar_view(&calv);
882 do_tasks_view(); /** Render the task list */
886 case VIEW_ADDRESSBOOK:
887 do_addrbook_view(addrbook, num_ab); /** Render the address book */
894 /** Note: wDumpContent() will output one additional </div> tag. */
895 wprintf("</div>\n"); /** end of 'content' div */
898 /** free the summary */
899 if (WCC->summ != NULL) {
900 DeleteHash(&WCC->summ);
902 if (addrbook != NULL) free(addrbook);
903 FreeStrBuf(&BBViewToolBar);
908 * Back end for post_message()
909 * ... this is where the actual message gets transmitted to the server.
911 void post_mime_to_server(void) {
913 char top_boundary[SIZ];
914 char alt_boundary[SIZ];
915 int is_multipart = 0;
917 wc_mime_attachment *att;
919 size_t encoded_length;
920 size_t encoded_strlen;
921 char *txtmail = NULL;
923 sprintf(top_boundary, "Citadel--Multipart--%s--%04x--%04x",
928 sprintf(alt_boundary, "Citadel--Multipart--%s--%04x--%04x",
934 /* RFC2045 requires this, and some clients look for it... */
935 serv_puts("MIME-Version: 1.0");
936 serv_puts("X-Mailer: " PACKAGE_STRING);
938 /* If there are attachments, we have to do multipart/mixed */
939 if (GetCount(WCC->attachments) > 0) {
944 /* Remember, serv_printf() appends an extra newline */
945 serv_printf("Content-type: multipart/mixed; boundary=\"%s\"\n", top_boundary);
946 serv_printf("This is a multipart message in MIME format.\n");
947 serv_printf("--%s", top_boundary);
950 /* Remember, serv_printf() appends an extra newline */
951 serv_printf("Content-type: multipart/alternative; "
952 "boundary=\"%s\"\n", alt_boundary);
953 serv_printf("This is a multipart message in MIME format.\n");
954 serv_printf("--%s", alt_boundary);
956 serv_puts("Content-type: text/plain; charset=utf-8");
957 serv_puts("Content-Transfer-Encoding: quoted-printable");
959 txtmail = html_to_ascii(bstr("msgtext"), 0, 80, 0);
960 text_to_server_qp(txtmail); /* Transmit message in quoted-printable encoding */
963 serv_printf("--%s", alt_boundary);
965 serv_puts("Content-type: text/html; charset=utf-8");
966 serv_puts("Content-Transfer-Encoding: quoted-printable");
968 serv_puts("<html><body>\r\n");
969 text_to_server_qp(bstr("msgtext")); /* Transmit message in quoted-printable encoding */
970 serv_puts("</body></html>\r\n");
972 serv_printf("--%s--", alt_boundary);
980 /* Add in the attachments */
981 it = GetNewHashPos(WCC->attachments, 0);
982 while (GetNextHashPos(WCC->attachments, it, &len, &Key, &vAtt)) {
983 att = (wc_mime_attachment *)vAtt;
984 encoded_length = ((att->length * 150) / 100);
985 encoded = malloc(encoded_length);
986 if (encoded == NULL) break;
987 encoded_strlen = CtdlEncodeBase64(encoded, ChrPtr(att->Data), StrLength(att->Data), 1);
989 serv_printf("--%s", top_boundary);
990 serv_printf("Content-type: %s", ChrPtr(att->ContentType));
991 serv_printf("Content-disposition: attachment; filename=\"%s\"", ChrPtr(att->FileName));
992 serv_puts("Content-transfer-encoding: base64");
994 serv_write(encoded, encoded_strlen);
999 serv_printf("--%s--", top_boundary);
1008 * Post message (or don't post message)
1010 * Note regarding the "dont_post" variable:
1011 * A random value (actually, it's just a timestamp) is inserted as a hidden
1012 * field called "postseq" when the display_enter page is generated. This
1013 * value is checked when posting, using the static variable dont_post. If a
1014 * user attempts to post twice using the same dont_post value, the message is
1015 * discarded. This prevents the accidental double-saving of the same message
1016 * if the user happens to click the browser "back" button.
1018 void post_message(void)
1021 StrBuf *encoded_subject = NULL;
1022 static long dont_post = (-1L);
1023 wc_mime_attachment *att;
1024 int is_anonymous = 0;
1025 const StrBuf *display_name = NULL;
1026 wcsession *WCC = WC;
1028 if (havebstr("force_room")) {
1029 gotoroom(bstr("force_room"));
1032 if (havebstr("display_name")) {
1033 display_name = sbstr("display_name");
1034 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1035 display_name = NULL;
1040 if (WCC->upload_length > 0) {
1045 lprintf(9, "%s:%d: we are uploading %d bytes\n", __FILE__, __LINE__, WCC->upload_length);
1046 /** There's an attachment. Save it to this struct... */
1047 att = malloc(sizeof(wc_mime_attachment));
1048 memset(att, 0, sizeof(wc_mime_attachment ));
1049 att->length = WCC->upload_length;
1050 att->ContentType = NewStrBufPlain(WCC->upload_content_type, -1);
1051 att->FileName = NewStrBufPlain(WCC->upload_filename, -1);
1054 if (WCC->attachments == NULL)
1055 WCC->attachments = NewHash(1, NULL);
1056 /* And add it to the list. */
1057 n = snprintf(N, sizeof N, "%d", GetCount(WCC->attachments) + 1);
1058 Put(WCC->attachments, N, n, att, DestroyMime);
1061 * Mozilla sends a simple filename, which is what we want,
1062 * but Satan's Browser sends an entire pathname. Reduce
1063 * the path to just a filename if we need to.
1065 pch = strrchr(ChrPtr(att->FileName), '/');
1067 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName));
1069 pch = strrchr(ChrPtr(att->FileName), '\\');
1071 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName));
1075 * Transfer control of this memory from the upload struct
1076 * to the attachment struct.
1078 att->Data = NewStrBufPlain(WCC->upload, WCC->upload_length);
1080 WCC->upload_length = 0;
1086 if (havebstr("cancel_button")) {
1087 sprintf(WCC->ImportantMessage,
1088 _("Cancelled. Message was not posted."));
1089 } else if (havebstr("attach_button")) {
1092 } else if (lbstr("postseq") == dont_post) {
1093 sprintf(WCC->ImportantMessage,
1094 _("Automatically cancelled because you have already "
1095 "saved this message."));
1097 const char CMD[] = "ENT0 1|%s|%d|4|%s|%s||%s|%s|%s|%s|%s";
1098 const StrBuf *Recp = NULL;
1099 const StrBuf *Cc = NULL;
1100 const StrBuf *Bcc = NULL;
1101 const StrBuf *Wikipage = NULL;
1102 const StrBuf *my_email_addr = NULL;
1103 StrBuf *CmdBuf = NULL;;
1104 StrBuf *references = NULL;
1106 if (havebstr("references"))
1108 const StrBuf *ref = sbstr("references");
1109 references = NewStrBufPlain(ChrPtr(ref), StrLength(ref));
1110 lprintf(9, "Converting: %s\n", ChrPtr(references));
1111 StrBufReplaceChars(references, '|', '!');
1112 lprintf(9, "Converted: %s\n", ChrPtr(references));
1114 if (havebstr("subject")) {
1117 * make enough room for the encoded string;
1118 * plus the QP header
1120 Subj = sbstr("subject");
1122 StrBufRFC2047encode(&encoded_subject, Subj);
1124 Recp = sbstr("recp");
1127 Wikipage = sbstr("wikipage");
1128 my_email_addr = sbstr("my_email_addr");
1130 CmdBuf = NewStrBufPlain(NULL,
1133 StrLength(encoded_subject) +
1136 StrLength(Wikipage) +
1137 StrLength(my_email_addr) +
1138 StrLength(references));
1140 StrBufPrintf(CmdBuf,
1144 ChrPtr(encoded_subject),
1145 ChrPtr(display_name),
1149 ChrPtr(my_email_addr),
1150 ChrPtr(references));
1151 FreeStrBuf(&references);
1153 lprintf(9, "%s\n", ChrPtr(CmdBuf));
1154 serv_puts(ChrPtr(CmdBuf));
1155 serv_getln(buf, sizeof buf);
1156 FreeStrBuf(&CmdBuf);
1157 FreeStrBuf(&encoded_subject);
1158 if (buf[0] == '4') {
1159 post_mime_to_server();
1160 if ( (havebstr("recp"))
1161 || (havebstr("cc" ))
1162 || (havebstr("bcc" ))
1164 sprintf(WCC->ImportantMessage, _("Message has been sent.\n"));
1167 sprintf(WC->ImportantMessage, _("Message has been posted.\n"));
1169 dont_post = lbstr("postseq");
1171 lprintf(9, "%s:%d: server post error: %s\n", __FILE__, __LINE__, buf);
1172 sprintf(WC->ImportantMessage, "%s", &buf[4]);
1178 DeleteHash(&WCC->attachments);
1181 * We may have been supplied with instructions regarding the location
1182 * to which we must return after posting. If found, go there.
1184 if (havebstr("return_to")) {
1185 http_redirect(bstr("return_to"));
1188 * If we were editing a page in a wiki room, go to that page now.
1190 else if (havebstr("wikipage")) {
1191 snprintf(buf, sizeof buf, "wiki?page=%s", bstr("wikipage"));
1195 * Otherwise, just go to the "read messages" loop.
1206 * \brief display the message entry screen
1208 void display_enter(void)
1212 const StrBuf *display_name = NULL;
1213 int recipient_required = 0;
1214 int subject_required = 0;
1215 int recipient_bad = 0;
1216 int is_anonymous = 0;
1217 wcsession *WCC = WC;
1221 if (havebstr("force_room")) {
1222 gotoroom(bstr("force_room"));
1225 display_name = sbstr("display_name");
1226 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1227 display_name = NULL;
1231 /** First test to see whether this is a room that requires recipients to be entered */
1232 serv_puts("ENT0 0");
1233 serv_getln(buf, sizeof buf);
1235 if (!strncmp(buf, "570", 3)) { /** 570 means that we need a recipient here */
1236 recipient_required = 1;
1238 else if (buf[0] != '2') { /** Any other error means that we cannot continue */
1239 sprintf(WCC->ImportantMessage, "%s", &buf[4]);
1244 /* Is the server strongly recommending that the user enter a message subject? */
1245 if ((buf[3] != '\0') && (buf[4] != '\0')) {
1246 subject_required = extract_int(&buf[4], 1);
1250 * Are we perhaps in an address book view? If so, then an "enter
1251 * message" command really means "add new entry."
1253 if (WCC->wc_default_view == VIEW_ADDRESSBOOK) {
1254 do_edit_vcard(-1, "", "", WCC->wc_roomname);
1259 * Are we perhaps in a calendar room? If so, then an "enter
1260 * message" command really means "add new calendar item."
1262 if (WCC->wc_default_view == VIEW_CALENDAR) {
1263 display_edit_event();
1268 * Are we perhaps in a tasks view? If so, then an "enter
1269 * message" command really means "add new task."
1271 if (WCC->wc_default_view == VIEW_TASKS) {
1272 display_edit_task();
1277 * Otherwise proceed normally.
1278 * Do a custom room banner with no navbar...
1281 if (recipient_required) {
1282 const StrBuf *Recp = NULL;
1283 const StrBuf *Cc = NULL;
1284 const StrBuf *Bcc = NULL;
1285 const StrBuf *Wikipage = NULL;
1286 StrBuf *CmdBuf = NULL;;
1287 const char CMD[] = "ENT0 0|%s|%d|0||%s||%s|%s|%s";
1289 Recp = sbstr("recp");
1292 Wikipage = sbstr("wikipage");
1294 CmdBuf = NewStrBufPlain(NULL,
1297 StrLength(display_name) +
1300 StrLength(Wikipage));
1302 StrBufPrintf(CmdBuf,
1306 ChrPtr(display_name),
1310 serv_puts(ChrPtr(CmdBuf));
1311 serv_getln(buf, sizeof buf);
1312 FreeStrBuf(&CmdBuf);
1314 if (!strncmp(buf, "570", 3)) { /** 570 means we have an invalid recipient listed */
1315 if (havebstr("recp") &&
1321 else if (buf[0] != '2') { /** Any other error means that we cannot continue */
1322 wprintf("<em>%s</em><br />\n", &buf[4]);/// -> important message
1326 svputlong("RCPTREQUIRED", recipient_required);
1327 svputlong("SUBJREQUIRED", recipient_required || subject_required);
1330 output_headers(1, 0, 0, 0, 1, 0);
1331 DoTemplate(HKEY("edit_message"), NULL, NULL, CTX_NONE);
1338 * \brief delete a message
1340 void delete_msg(void)
1345 msgid = lbstr("msgid");
1347 if (WC->wc_is_trash) { /** Delete from Trash is a real delete */
1348 serv_printf("DELE %ld", msgid);
1350 else { /** Otherwise move it to Trash */
1351 serv_printf("MOVE %ld|_TRASH_|0", msgid);
1354 serv_getln(buf, sizeof buf);
1355 sprintf(WC->ImportantMessage, "%s", &buf[4]);
1362 * \brief move a message to another folder
1369 msgid = lbstr("msgid");
1371 if (havebstr("move_button")) {
1372 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
1374 serv_getln(buf, sizeof buf);
1375 sprintf(WC->ImportantMessage, "%s", &buf[4]);
1377 sprintf(WC->ImportantMessage, (_("The message was not moved.")));
1388 * Confirm move of a message
1390 void confirm_move_msg(void)
1396 msgid = lbstr("msgid");
1399 output_headers(1, 1, 2, 0, 0, 0);
1400 wprintf("<div id=\"banner\">\n");
1402 wprintf(_("Confirm move of message"));
1404 wprintf("</div>\n");
1406 wprintf("<div id=\"content\" class=\"service\">\n");
1408 wprintf("<CENTER>");
1410 wprintf(_("Move this message to:"));
1411 wprintf("<br />\n");
1413 wprintf("<form METHOD=\"POST\" action=\"move_msg\">\n");
1414 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
1415 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n", bstr("msgid"));
1417 wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
1419 serv_getln(buf, sizeof buf);
1420 if (buf[0] == '1') {
1421 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1422 extract_token(targ, buf, 0, '|', sizeof targ);
1423 wprintf("<OPTION>");
1428 wprintf("</SELECT>\n");
1429 wprintf("<br />\n");
1431 wprintf("<INPUT TYPE=\"submit\" NAME=\"move_button\" VALUE=\"%s\">", _("Move"));
1433 wprintf("<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">", _("Cancel"));
1434 wprintf("</form></CENTER>\n");
1436 wprintf("</CENTER>\n");
1442 * Generic function to output an arbitrary MIME attachment from
1443 * message being composed
1445 * partnum The MIME part to be output
1446 * filename Fake filename to give
1447 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1449 void postpart(StrBuf *partnum, StrBuf *filename, int force_download)
1452 StrBuf *content_type;
1453 wc_mime_attachment *part;
1455 if (GetHash(WC->attachments, SKEY(partnum), &vPart) &&
1457 part = (wc_mime_attachment*) vPart;
1458 if (force_download) {
1459 content_type = NewStrBufPlain(HKEY("application/octet-stream"));
1462 content_type = NewStrBufDup(part->ContentType);
1464 output_headers(0, 0, 0, 0, 0, 0);
1465 StrBufAppendBuf(WC->WBuf, part->Data, 0);
1466 http_transmit_thing(ChrPtr(content_type), 0);
1468 hprintf("HTTP/1.1 404 %s\n", ChrPtr(partnum));
1469 output_headers(0, 0, 0, 0, 0, 0);
1470 hprintf("Content-Type: text/plain\r\n");
1471 wprintf(_("An error occurred while retrieving this part: %s/%s\n"),
1472 ChrPtr(partnum), ChrPtr(filename));
1475 FreeStrBuf(&content_type);
1480 * Generic function to output an arbitrary MIME part from an arbitrary
1481 * message number on the server.
1483 * msgnum Number of the item on the citadel server
1484 * partnum The MIME part to be output
1485 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1487 void mimepart(const char *msgnum, const char *partnum, int force_download)
1491 char content_type[256];
1493 serv_printf("OPNA %s|%s", msgnum, partnum);
1494 serv_getln(buf, sizeof buf);
1495 if (buf[0] == '2') {
1496 bytes = extract_long(&buf[4], 0);
1497 if (force_download) {
1498 strcpy(content_type, "application/octet-stream");
1501 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
1503 output_headers(0, 0, 0, 0, 0, 0);
1505 read_server_binary(WC->WBuf, bytes);
1507 serv_getln(buf, sizeof buf);
1508 http_transmit_thing(content_type, 0);
1510 hprintf("HTTP/1.1 404 %s\n", &buf[4]);
1511 output_headers(0, 0, 0, 0, 0, 0);
1512 hprintf("Content-Type: text/plain\r\n");
1513 wprintf(_("An error occurred while retrieving this part: %s\n"), &buf[4]);
1520 * Read any MIME part of a message, from the server, into memory.
1522 char *load_mimepart(long msgnum, char *partnum)
1526 char content_type[SIZ];
1529 serv_printf("DLAT %ld|%s", msgnum, partnum);
1530 serv_getln(buf, sizeof buf);
1531 if (buf[0] == '6') {
1532 bytes = extract_long(&buf[4], 0);
1533 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
1535 content = malloc(bytes + 2);
1536 serv_read(content, bytes);
1538 content[bytes] = 0; /* null terminate for good measure */
1547 * Read any MIME part of a message, from the server, into memory.
1549 void MimeLoadData(wc_mime_attachment *Mime)
1553 //// TODO: is there a chance the contenttype is different to the one we know?
1554 serv_printf("DLAT %ld|%s", Mime->msgnum, ChrPtr(Mime->PartNum));
1555 serv_getln(buf, sizeof buf);
1556 if (buf[0] == '6') {
1557 bytes = extract_long(&buf[4], 0);
1559 if (Mime->Data == NULL)
1560 Mime->Data = NewStrBufPlain(NULL, bytes);
1561 StrBuf_ServGetBLOB(Mime->Data, bytes);
1565 FlushStrBuf(Mime->Data);
1566 /// TODO XImportant message
1573 void view_mimepart(void) {
1574 mimepart(ChrPtr(WC->UrlFragment2),
1575 ChrPtr(WC->UrlFragment3),
1579 void download_mimepart(void) {
1580 mimepart(ChrPtr(WC->UrlFragment2),
1581 ChrPtr(WC->UrlFragment3),
1585 void view_postpart(void) {
1586 postpart(WC->UrlFragment2,
1591 void download_postpart(void) {
1592 postpart(WC->UrlFragment2,
1597 void h_readnew(void) { readloop(readnew);}
1598 void h_readold(void) { readloop(readold);}
1599 void h_readfwd(void) { readloop(readfwd);}
1600 void h_headers(void) { readloop(headers);}
1601 void h_do_search(void) { readloop(do_search);}
1607 WebcitAddUrlHandler(HKEY("readnew"), h_readnew, NEED_URL);
1608 WebcitAddUrlHandler(HKEY("readold"), h_readold, NEED_URL);
1609 WebcitAddUrlHandler(HKEY("readfwd"), h_readfwd, NEED_URL);
1610 WebcitAddUrlHandler(HKEY("headers"), h_headers, NEED_URL);
1611 WebcitAddUrlHandler(HKEY("do_search"), h_do_search, 0);
1612 WebcitAddUrlHandler(HKEY("display_enter"), display_enter, 0);
1613 WebcitAddUrlHandler(HKEY("post"), post_message, 0);
1614 WebcitAddUrlHandler(HKEY("move_msg"), move_msg, 0);
1615 WebcitAddUrlHandler(HKEY("delete_msg"), delete_msg, 0);
1616 WebcitAddUrlHandler(HKEY("confirm_move_msg"), confirm_move_msg, 0);
1617 WebcitAddUrlHandler(HKEY("msg"), embed_message, NEED_URL|AJAX);
1618 WebcitAddUrlHandler(HKEY("printmsg"), print_message, NEED_URL);
1619 WebcitAddUrlHandler(HKEY("mobilemsg"), mobile_message_view, NEED_URL);
1620 WebcitAddUrlHandler(HKEY("msgheaders"), display_headers, NEED_URL);
1622 WebcitAddUrlHandler(HKEY("mimepart"), view_mimepart, NEED_URL);
1623 WebcitAddUrlHandler(HKEY("mimepart_download"), download_mimepart, NEED_URL);
1624 WebcitAddUrlHandler(HKEY("postpart"), view_postpart, NEED_URL);
1625 WebcitAddUrlHandler(HKEY("postpart_download"), download_postpart, NEED_URL);