4 * Functions which deal with the fetching and displaying of messages.
12 HashList *MsgHeaderHandler = NULL;
13 HashList *MsgEvaluators = NULL;
14 HashList *MimeRenderHandler = NULL;
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 */
22 void display_enter(void);
23 int longcmp_r(const void *s1, const void *s2);
24 int summcmp_subj(const void *s1, const void *s2);
25 int summcmp_rsubj(const void *s1, const void *s2);
26 int summcmp_sender(const void *s1, const void *s2);
27 int summcmp_rsender(const void *s1, const void *s2);
28 int summcmp_date(const void *s1, const void *s2);
29 int summcmp_rdate(const void *s1, const void *s2);
31 /*----------------------------------------------------------------------------*/
34 typedef void (*MsgPartEvaluatorFunc)(message_summary *Sum, StrBuf *Buf);
38 /*----------------------------------------------------------------------------*/
43 * I wanna SEE that message!
45 * msgnum Message number to display
46 * printable_view Nonzero to display a printable view
47 * section Optional for encapsulated message/rfc822 submessage
49 int read_message(StrBuf *Target, const char *tmpl, long tmpllen, long msgnum, int printable_view, const StrBuf *PartNum) {
55 message_summary *Msg = NULL;
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 FoundCharset = NewStrBuf();
87 while ((StrBuf_ServGetln(Buf)>=0) && !Done) {
88 if ( (StrLength(Buf)==3) &&
89 !strcmp(ChrPtr(Buf), "000"))
93 lprintf(1, _("unexpected end of message"));
95 Msg->MsgBody->ContentType = NewStrBufPlain(HKEY("text/html"));
96 StrBufAppendPrintf(Msg->MsgBody->Data, "<div><i>");
97 StrBufAppendPrintf(Msg->MsgBody->Data, _("unexpected end of message"));
98 StrBufAppendPrintf(Msg->MsgBody->Data, " (1)</i><br /><br />\n");
99 StrBufAppendPrintf(Msg->MsgBody->Data, "</div>\n");
104 case 0:/* Citadel Message Headers */
105 if (StrLength(Buf) == 0) {
109 StrBufExtract_token(HdrToken, Buf, 0, '=');
110 StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
112 lprintf(1, ":: [%s] = [%s]\n", ChrPtr(HdrToken), ChrPtr(Buf));
113 /* look up one of the examine_* functions to parse the content */
114 if (GetHash(MsgHeaderHandler, SKEY(HdrToken), &vHdr) &&
116 Hdr = (headereval*)vHdr;
117 Hdr->evaluator(Msg, Buf, FoundCharset);
118 if (Hdr->Type == 1) {
122 else lprintf(1, "don't know how to handle message header[%s]\n", ChrPtr(HdrToken));
124 case 1:/* Message Mime Header */
125 if (StrLength(Buf) == 0) {
127 if (Msg->MsgBody->ContentType == NULL)
128 /* end of header or no header? */
129 Msg->MsgBody->ContentType = NewStrBufPlain(HKEY("text/plain"));
130 /* usual end of mime header */
134 StrBufExtract_token(HdrToken, Buf, 0, ':');
135 if (StrLength(HdrToken) > 0) {
136 StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
137 lprintf(1, ":: [%s] = [%s]\n", ChrPtr(HdrToken), ChrPtr(Buf));
138 /* the examine*'s know how to do with mime headers too... */
139 if (GetHash(MsgHeaderHandler, SKEY(HdrToken), &vHdr) &&
141 Hdr = (headereval*)vHdr;
142 Hdr->evaluator(Msg, Buf, FoundCharset);
147 case 2: /* Message Body */
149 if (Msg->MsgBody->size_known > 0) {
150 StrBuf_ServGetBLOB(Msg->MsgBody->Data, Msg->MsgBody->length);
152 /// todo: check next line, if not 000, append following lines
155 if (StrLength(Msg->MsgBody->Data) > 0)
156 StrBufAppendBufPlain(Msg->MsgBody->Data, "\n", 1, 0);
157 StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
161 StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
166 if (Msg->AllAttach == NULL)
167 Msg->AllAttach = NewHash(1,NULL);
168 /* now we put the body mimepart we read above into the mimelist */
169 Put(Msg->AllAttach, SKEY(Msg->MsgBody->PartNum), Msg->MsgBody, DestroyMime);
171 /* strip the bare contenttype, so we ommit charset etc. */
172 StrBufExtract_token(Buf, Msg->MsgBody->ContentType, 0, ';');
174 /* look up the renderer, that will convert this mimeitem into the htmlized form */
175 if (GetHash(MimeRenderHandler, SKEY(Buf), &vHdr) &&
177 RenderMimeFunc Render;
178 Render = (RenderMimeFunc)vHdr;
179 Render(Msg->MsgBody, NULL, FoundCharset);
182 if (StrLength(Msg->reply_references)> 0) {
183 /* Trim down excessively long lists of thread references. We eliminate the
184 * second one in the list so that the thread root remains intact.
186 int rrtok = num_tokens(ChrPtr(Msg->reply_references), '|');
187 int rrlen = StrLength(Msg->reply_references);
188 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
189 StrBufRemove_token(Msg->reply_references, 1, '|');
193 /* Generate a reply-to address */
194 if (StrLength(Msg->Rfca) > 0) {
195 if (Msg->reply_to == NULL)
196 Msg->reply_to = NewStrBuf();
197 if (StrLength(Msg->from) > 0) {
198 StrBufPrintf(Msg->reply_to, "%s <%s>", ChrPtr(Msg->from), ChrPtr(Msg->Rfca));
201 FlushStrBuf(Msg->reply_to);
202 StrBufAppendBuf(Msg->reply_to, Msg->Rfca, 0);
207 if ((StrLength(Msg->OtherNode)>0) &&
208 (strcasecmp(ChrPtr(Msg->OtherNode), serv_info.serv_nodename)) &&
209 (strcasecmp(ChrPtr(Msg->OtherNode), serv_info.serv_humannode)) )
211 if (Msg->reply_to == NULL)
212 Msg->reply_to = NewStrBuf();
213 StrBufPrintf(Msg->reply_to,
216 ChrPtr(Msg->OtherNode));
219 if (Msg->reply_to == NULL)
220 Msg->reply_to = NewStrBuf();
221 FlushStrBuf(Msg->reply_to);
222 StrBufAppendBuf(Msg->reply_to, Msg->from, 0);
226 /* now check if we need to translate some mimeparts, and remove the duplicate */
227 it = GetNewHashPos(Msg->AllAttach, 0);
228 while (GetNextHashPos(Msg->AllAttach, it, &len, &Key, &vMime) &&
230 wc_mime_attachment *Mime = (wc_mime_attachment*) vMime;
231 evaluate_mime_part(Msg, Mime);
235 DoTemplate(tmpl, tmpllen, Target, Msg, CTX_MAILSUM);
237 DestroyMessageSummary(Msg);
238 FreeStrBuf(&FoundCharset);
239 FreeStrBuf(&HdrToken);
247 * Unadorned HTML output of an individual message, suitable
248 * for placing in a hidden iframe, for printing, or whatever
250 * msgnum_as_string == Message number, as a string instead of as a long int
252 void embed_message(void) {
254 const StrBuf *Tmpl = sbstr("template");
256 msgnum = StrTol(WC->UrlFragment2);
257 if (StrLength(Tmpl) > 0)
258 read_message(WC->WBuf, SKEY(Tmpl), msgnum, 0, NULL);
260 read_message(WC->WBuf, HKEY("view_message"), msgnum, 0, NULL);
265 * Printable view of a message
267 * msgnum_as_string == Message number, as a string instead of as a long int
269 void print_message(void) {
272 msgnum = StrTol(WC->UrlFragment2);
273 output_headers(0, 0, 0, 0, 0, 0);
275 hprintf("Content-type: text/html\r\n"
276 "Server: " PACKAGE_STRING "\r\n"
277 "Connection: close\r\n");
281 read_message(WC->WBuf, HKEY("view_message_print"), msgnum, 1, NULL);
287 * Mobile browser view of message
289 * @param msg_num_as_string Message number as a string instead of as a long int
291 void mobile_message_view(void) {
293 msgnum = StrTol(WC->UrlFragment2);
294 output_headers(1, 0, 0, 0, 0, 1);
296 do_template("msgcontrols", NULL);
297 read_message(WC->WBuf, HKEY("view_message"), msgnum,1, NULL);
302 * \brief Display a message's headers
304 * \param msgnum_as_string Message number, as a string instead of as a long int
306 void display_headers(void) {
310 msgnum = StrTol(WC->UrlFragment2);
311 output_headers(0, 0, 0, 0, 0, 0);
313 hprintf("Content-type: text/plain\r\n"
315 "Connection: close\r\n",
319 serv_printf("MSG2 %ld|3", msgnum);
320 serv_getln(buf, sizeof buf);
322 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
323 wprintf("%s\n", buf);
331 message_summary *ReadOneMessageSummary(StrBuf *RawMessage, const char *DefaultSubject, long MsgNum)
334 MsgPartEvaluatorFunc Eval;
335 message_summary *Msg;
344 serv_printf("MSG0 %ld|1", MsgNum); /* ask for headers only */
346 StrBuf_ServGetln(Buf);
347 if (GetServerStatus(Buf, NULL) == 1) {
352 Msg = (message_summary*)malloc(sizeof(message_summary));
353 memset(Msg, 0, sizeof(message_summary));
354 while (len = StrBuf_ServGetln(Buf),
356 strcmp(ChrPtr(Buf), "000")== 0)){
358 ebuf = strchr(ChrPtr(Buf), '=');
360 if (GetHash(MsgEvaluators, buf, nBuf, &vEval) && vEval != NULL) {
361 Eval = (MsgPartEvaluatorFunc) vEval;
362 StrBufCutLeft(Buf, nBuf + 1);
365 else lprintf(1, "Don't know how to handle Message Headerline [%s]", ChrPtr(Buf));
375 * load message pointers from the server for a "read messages" operation
377 * servcmd: the citadel command to send to the citserver
378 * with_headers: also include some of the headers with the message numbers (more expensive)
380 int load_msg_ptrs(char *servcmd, int with_headers)
382 StrBuf* FoundCharset = NULL;
384 message_summary *Msg;
391 if (WCC->summ != NULL) {
392 if (WCC->summ != NULL)
393 DeleteHash(&WCC->summ);
395 WCC->summ = NewHash(1, Flathash);
400 StrBuf_ServGetln(Buf);
401 if (GetServerStatus(Buf, NULL) != 1) {
405 // TODO if (with_headers) { //// TODO: Have Attachments?
407 while (len = StrBuf_ServGetln(Buf),
409 strcmp(ChrPtr(Buf), "000")!= 0))
411 if (nummsgs < maxload) {
412 Msg = (message_summary*)malloc(sizeof(message_summary));
413 memset(Msg, 0, sizeof(message_summary));
415 Msg->msgnum = StrBufExtract_long(Buf, 0, '|');
416 Msg->date = StrBufExtract_long(Buf, 1, '|');
418 Msg->from = NewStrBufPlain(NULL, StrLength(Buf));
419 StrBufExtract_token(Buf2, Buf, 2, '|');
420 if (StrLength(Buf2) != 0) {
421 /** Handle senders with RFC2047 encoding */
422 StrBuf_RFC822_to_Utf8(Msg->from, Buf2, WCC->DefaultCharset, FoundCharset);
426 StrBufExtract_token(Buf2, Buf, 3, '|');
427 if ((StrLength(Buf2) !=0 ) &&
428 ( ((WCC->room_flags & QR_NETWORK)
429 || ((strcasecmp(ChrPtr(Buf2), serv_info.serv_nodename)
430 && (strcasecmp(ChrPtr(Buf2), serv_info.serv_fqdn)))))))
432 StrBufAppendBufPlain(Msg->from, HKEY(" @ "), 0);
433 StrBufAppendBuf(Msg->from, Buf2, 0);
437 StrBufExtract_token(Msg->inetaddr, Buf, 4, '|');
440 Msg->subj = NewStrBufPlain(NULL, StrLength(Buf));
441 StrBufExtract_token(Buf2, Buf, 5, '|');
442 if (StrLength(Buf2) == 0)
443 StrBufAppendBufPlain(Msg->subj, _("(no subj)"), 0, -1);
445 StrBuf_RFC822_to_Utf8(Msg->subj, Buf2, WCC->DefaultCharset, FoundCharset);
446 if ((StrLength(Msg->subj) > 75) &&
447 (StrBuf_Utf8StrLen(Msg->subj) > 75)) {
448 StrBuf_Utf8StrCut(Msg->subj, 72);
449 StrBufAppendBufPlain(Msg->subj, HKEY("..."), 0);
454 if ((StrLength(Msg->from) > 25) &&
455 (StrBuf_Utf8StrLen(Msg->from) > 25)) {
456 StrBuf_Utf8StrCut(Msg->from, 23);
457 StrBufAppendBufPlain(Msg->from, HKEY("..."), 0);
460 Put(WCC->summ, (const char *)&n, sizeof(n), Msg, DestroyMessageSummary);
470 inline message_summary* GetMessagePtrAt(int n, HashList *Summ)
478 GetHashAt(Summ, n, &HKLen, &Key, &vMsg);
479 return (message_summary*) vMsg;
483 void DrawMessageDropdown(StrBuf *Selector, long maxmsgs, long startmsg)
487 message_summary* Msg;
498 int nMessages = DEFAULT_MAXMSGS;
500 TmpBuf = NewStrBuf();
501 At = GetNewHashPos(WCC->summ, (lbstr("SortOrder") == 1)? -nMessages : nMessages);
502 nItems = GetCount(WCC->summ);
505 vector[1] = startmsg;
511 lo = GetHashPosCounter(At);
512 if (lo + nMessages > nItems) {
518 done = !GetNextHashPos(WCC->summ, At, &hklen, &key, &vMsg);
519 Msg = (message_summary*) vMsg;
520 n = (Msg==NULL)? 0 : Msg->msgnum;
527 DoTemplate(HKEY("select_messageindex"), TmpBuf, &vector, CTX_LONGVECTOR);
528 StrBufAppendBuf(Selector, TmpBuf, 0);
531 vector[6] = StartMsg;
533 DoTemplate(HKEY("select_messageindex_all"), TmpBuf, &vector, CTX_LONGVECTOR);
534 StrBufAppendBuf(Selector, TmpBuf, 0);
540 extern readloop_struct rlid[];
543 * command loop for reading messages
545 * Set oper to "readnew" or "readold" or "readfwd" or "headers"
547 void readloop(long oper)
549 StrBuf *MessageDropdown = NULL;
550 StrBuf *BBViewToolBar = NULL;
552 message_summary *Msg;
560 long *displayed_msgs = NULL;
561 int num_displayed = 0;
563 int is_addressbook = 0;
564 int is_singlecard = 0;
570 int lowest_displayed = (-1);
571 int highest_displayed = 0;
572 addrbookent *addrbook = NULL;
579 int care_for_empty_list = 0;
583 switch (WCC->wc_view) {
585 sprintf(buf, "wiki?room=%s&page=home", WCC->wc_roomname);
591 strcpy(cmd, "MSGS ALL|||1");
593 parse_calendar_view_request(&calv);
597 strcpy(cmd, "MSGS ALL");
602 strcpy(cmd, "MSGS ALL");
604 wprintf("<div id=\"new_notes_here\"></div>\n");
606 case VIEW_ADDRESSBOOK:
607 is_singlecard = ibstr("is_singlecard");
610 if (oper == do_search) {
611 snprintf(cmd, sizeof(cmd), "MSGS SEARCH|%s", bstr("query"));
614 strcpy(cmd, "MSGS ALL");
621 care_for_empty_list = 1;
622 startmsg = lbstr("startmsg");
623 if (havebstr("maxmsgs"))
624 maxmsgs = ibstr("maxmsgs");
625 is_summary = (ibstr("is_summary") && !WCC->is_mobile);
626 if (maxmsgs == 0) maxmsgs = DEFAULT_MAXMSGS;
631 * When in summary mode, always show ALL messages instead of just
632 * new or old. Otherwise, show what the user asked for.
634 rlid[oper].cmd(cmd, sizeof(cmd));
636 if ((WCC->wc_view == VIEW_MAILBOX) && (maxmsgs > 1) && !WCC->is_mobile) {
638 if (oper != do_search) {
639 strcpy(cmd, "MSGS ALL");
643 is_bbview = !is_summary;
644 if (is_summary) { /**< fetch header summary */
646 snprintf(cmd, sizeof(cmd), "MSGS %s|%s||1",
647 (oper == do_search) ? "SEARCH" : "ALL",
648 (oper == do_search) ? bstr("query") : ""
654 bbs_reverse = is_bbview && (lbstr("SortOrder") == 2);
656 sortit = is_summary || WCC->is_mobile;
660 output_headers(1, 1, 1, 0, 0, 0);
662 /* TODO: how can we best sort this in?
663 if (WCC->is_mobile) {
665 snprintf(cmd, sizeof(cmd), "MSGS %s|%s||1",
666 (!strcmp(oper, "do_search") ? "SEARCH" : "ALL"),
667 (!strcmp(oper, "do_search") ? bstr("query") : "")
673 * Are we doing a summary view? If so, we need to know old messages
674 * and new messages, so we can do that pretty boldface thing for the
677 nummsgs = load_msg_ptrs(cmd, (is_summary || WCC->is_mobile));
679 if (care_for_empty_list) {
680 wprintf("<div align=\"center\"><br /><em>");
683 wprintf(_("No new messages."));
686 wprintf(_("No old messages."));
689 wprintf(_("No messages here."));
691 wprintf("</em><br /></div>\n");
696 if (is_bbview && (startmsg == 0L)) {
698 Msg = GetMessagePtrAt((nummsgs >= maxmsgs) ? (nummsgs - maxmsgs) : 0, WCC->summ);
699 startmsg = (Msg==NULL)? 0 : Msg->msgnum;
702 Msg = GetMessagePtrAt(0, WCC->summ);
703 startmsg = (Msg==NULL)? 0 : Msg->msgnum;
707 /* This is done to make it run faster; WC is a function */
711 strcpy(old_msgs, "");
713 serv_getln(buf, sizeof buf);
715 strcpy(old_msgs, &buf[4]);
717 at = GetNewHashPos(WCC->summ, 0);
718 while (GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
719 /** Are you a new message, or an old message? */
720 Msg = (message_summary*) vMsg;
721 if (is_msg_in_mset(old_msgs, Msg->msgnum)) {
733 SortIt = RetrieveSort(CTX_MAILSUM, NULL,
736 SortByPayload(WCC->summ, SortIt);
740 do_template("summary_header", NULL);
741 } else if (WCC->is_mobile) {
742 wprintf("<div id=\"message_list\">");
747 * If we're not currently looking at ALL requested
748 * messages, then display the selector bar
751 BBViewToolBar = NewStrBuf();
752 MessageDropdown = NewStrBuf();
754 DrawMessageDropdown(MessageDropdown, maxmsgs, startmsg);
756 DoTemplate(HKEY("msg_listselector_top"), BBViewToolBar, MessageDropdown, CTX_STRBUF);
757 StrBufAppendBuf(WCC->WBuf, BBViewToolBar, 0);
758 FlushStrBuf(BBViewToolBar);
761 at = GetNewHashPos(WCC->summ, 0);
762 while (GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
763 Msg = (message_summary*) vMsg;
764 if ((Msg->msgnum >= startmsg) && (num_displayed < maxmsgs)) {
766 /** Display the message */
768 DoTemplate(HKEY("section_mailsummary"), NULL, Msg, CTX_MAILSUM);
770 else if (is_addressbook) {
771 fetch_ab_name(Msg, buf);
773 addrbook = realloc(addrbook,
774 (sizeof(addrbookent) * num_ab) );
775 safestrncpy(addrbook[num_ab-1].ab_name, buf,
776 sizeof(addrbook[num_ab-1].ab_name));
777 addrbook[num_ab-1].ab_msgnum = Msg->msgnum;
779 else if (is_calendar) {
780 load_calendar_item(Msg, Msg->is_new, &calv);
783 display_task(Msg, Msg->is_new);
786 display_note(Msg, Msg->is_new);
787 } else if (WCC->is_mobile) {
788 DoTemplate(HKEY("section_mailsummary"), NULL, Msg, CTX_MAILSUM);
791 if (displayed_msgs == NULL) {
792 displayed_msgs = malloc(sizeof(long) *
793 (maxmsgs<nummsgs ? maxmsgs : nummsgs));
795 displayed_msgs[num_displayed] = Msg->msgnum;
798 if (lowest_displayed < 0) lowest_displayed = a;
799 highest_displayed = a;
807 if (displayed_msgs != NULL) {
809 ////TODOqsort(displayed_msgs, num_displayed, sizeof(long), qlongcmp_r);
812 /** if we do a split bbview in the future, begin messages div here */
814 for (a=0; a<num_displayed; ++a) {
815 read_message(WC->WBuf, HKEY("view_message"), displayed_msgs[a], 0, NULL);
818 /** if we do a split bbview in the future, end messages div here */
820 free(displayed_msgs);
821 displayed_msgs = NULL;
825 do_template("summary_trailer", NULL);
826 } else if (WCC->is_mobile) {
831 * Bump these because although we're thinking in zero base, the user
832 * is a drooling idiot and is thinking in one base.
838 * If we're not currently looking at ALL requested
839 * messages, then display the selector bar
842 /** begin bbview scroller */
844 DoTemplate(HKEY("msg_listselector_bottom"), BBViewToolBar, MessageDropdown, CTX_STRBUF);
845 StrBufAppendBuf(WCC->WBuf, BBViewToolBar, 0);
847 FreeStrBuf(&BBViewToolBar);
848 FreeStrBuf(&MessageDropdown);
853 do_tasks_view(); /** Render the task list */
857 render_calendar_view(&calv);
860 if (is_addressbook) {
861 do_addrbook_view(addrbook, num_ab); /** Render the address book */
864 /** Note: wDumpContent() will output one additional </div> tag. */
865 wprintf("</div>\n"); /** end of 'content' div */
868 /** free the summary */
869 if (WCC->summ != NULL) {
870 DeleteHash(&WCC->summ);
872 if (addrbook != NULL) free(addrbook);
873 FreeStrBuf(&BBViewToolBar);
878 * Back end for post_message()
879 * ... this is where the actual message gets transmitted to the server.
881 void post_mime_to_server(void) {
883 char top_boundary[SIZ];
884 char alt_boundary[SIZ];
885 int is_multipart = 0;
887 wc_mime_attachment *att;
889 size_t encoded_length;
890 size_t encoded_strlen;
891 char *txtmail = NULL;
893 sprintf(top_boundary, "Citadel--Multipart--%s--%04x--%04x",
898 sprintf(alt_boundary, "Citadel--Multipart--%s--%04x--%04x",
904 /* RFC2045 requires this, and some clients look for it... */
905 serv_puts("MIME-Version: 1.0");
906 serv_puts("X-Mailer: " PACKAGE_STRING);
908 /* If there are attachments, we have to do multipart/mixed */
909 if (GetCount(WCC->attachments) > 0) {
914 /* Remember, serv_printf() appends an extra newline */
915 serv_printf("Content-type: multipart/mixed; boundary=\"%s\"\n", top_boundary);
916 serv_printf("This is a multipart message in MIME format.\n");
917 serv_printf("--%s", top_boundary);
920 /* Remember, serv_printf() appends an extra newline */
921 serv_printf("Content-type: multipart/alternative; "
922 "boundary=\"%s\"\n", alt_boundary);
923 serv_printf("This is a multipart message in MIME format.\n");
924 serv_printf("--%s", alt_boundary);
926 serv_puts("Content-type: text/plain; charset=utf-8");
927 serv_puts("Content-Transfer-Encoding: quoted-printable");
929 txtmail = html_to_ascii(bstr("msgtext"), 0, 80, 0);
930 text_to_server_qp(txtmail); /* Transmit message in quoted-printable encoding */
933 serv_printf("--%s", alt_boundary);
935 serv_puts("Content-type: text/html; charset=utf-8");
936 serv_puts("Content-Transfer-Encoding: quoted-printable");
938 serv_puts("<html><body>\r\n");
939 text_to_server_qp(bstr("msgtext")); /* Transmit message in quoted-printable encoding */
940 serv_puts("</body></html>\r\n");
942 serv_printf("--%s--", alt_boundary);
950 /* Add in the attachments */
951 it = GetNewHashPos(WCC->attachments, 0);
952 while (GetNextHashPos(WCC->attachments, it, &len, &Key, &vAtt)) {
953 att = (wc_mime_attachment *)vAtt;
954 encoded_length = ((att->length * 150) / 100);
955 encoded = malloc(encoded_length);
956 if (encoded == NULL) break;
957 encoded_strlen = CtdlEncodeBase64(encoded, ChrPtr(att->Data), StrLength(att->Data), 1);
959 serv_printf("--%s", top_boundary);
960 serv_printf("Content-type: %s", ChrPtr(att->ContentType));
961 serv_printf("Content-disposition: attachment; filename=\"%s\"", ChrPtr(att->FileName));
962 serv_puts("Content-transfer-encoding: base64");
964 serv_write(encoded, encoded_strlen);
969 serv_printf("--%s--", top_boundary);
978 * Post message (or don't post message)
980 * Note regarding the "dont_post" variable:
981 * A random value (actually, it's just a timestamp) is inserted as a hidden
982 * field called "postseq" when the display_enter page is generated. This
983 * value is checked when posting, using the static variable dont_post. If a
984 * user attempts to post twice using the same dont_post value, the message is
985 * discarded. This prevents the accidental double-saving of the same message
986 * if the user happens to click the browser "back" button.
988 void post_message(void)
991 StrBuf *encoded_subject = NULL;
992 static long dont_post = (-1L);
993 wc_mime_attachment *att;
994 int is_anonymous = 0;
995 const StrBuf *display_name = NULL;
998 if (havebstr("force_room")) {
999 gotoroom(bstr("force_room"));
1002 if (havebstr("display_name")) {
1003 display_name = sbstr("display_name");
1004 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1005 display_name = NULL;
1010 if (WCC->upload_length > 0) {
1015 lprintf(9, "%s:%d: we are uploading %d bytes\n", __FILE__, __LINE__, WCC->upload_length);
1016 /** There's an attachment. Save it to this struct... */
1017 att = malloc(sizeof(wc_mime_attachment));
1018 memset(att, 0, sizeof(wc_mime_attachment ));
1019 att->length = WCC->upload_length;
1020 att->ContentType = NewStrBufPlain(WCC->upload_content_type, -1);
1021 att->FileName = NewStrBufPlain(WCC->upload_filename, -1);
1024 if (WCC->attachments == NULL)
1025 WCC->attachments = NewHash(1, NULL);
1026 /* And add it to the list. */
1027 n = snprintf(N, sizeof N, "%d", GetCount(WCC->attachments) + 1);
1028 Put(WCC->attachments, N, n, att, DestroyMime);
1031 * Mozilla sends a simple filename, which is what we want,
1032 * but Satan's Browser sends an entire pathname. Reduce
1033 * the path to just a filename if we need to.
1035 pch = strrchr(ChrPtr(att->FileName), '/');
1037 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName));
1039 pch = strrchr(ChrPtr(att->FileName), '\\');
1041 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName));
1045 * Transfer control of this memory from the upload struct
1046 * to the attachment struct.
1048 att->Data = NewStrBufPlain(WCC->upload, WCC->upload_length);
1050 WCC->upload_length = 0;
1056 if (havebstr("cancel_button")) {
1057 sprintf(WCC->ImportantMessage,
1058 _("Cancelled. Message was not posted."));
1059 } else if (havebstr("attach_button")) {
1062 } else if (lbstr("postseq") == dont_post) {
1063 sprintf(WCC->ImportantMessage,
1064 _("Automatically cancelled because you have already "
1065 "saved this message."));
1067 const char CMD[] = "ENT0 1|%s|%d|4|%s|%s||%s|%s|%s|%s|%s";
1068 const StrBuf *Recp = NULL;
1069 const StrBuf *Cc = NULL;
1070 const StrBuf *Bcc = NULL;
1071 const StrBuf *Wikipage = NULL;
1072 const StrBuf *my_email_addr = NULL;
1073 StrBuf *CmdBuf = NULL;;
1074 StrBuf *references = NULL;
1076 if (havebstr("references"))
1078 const StrBuf *ref = sbstr("references");
1079 references = NewStrBufPlain(ChrPtr(ref), StrLength(ref));
1080 lprintf(9, "Converting: %s\n", ChrPtr(references));
1081 StrBufReplaceChars(references, '|', '!');
1082 lprintf(9, "Converted: %s\n", ChrPtr(references));
1084 if (havebstr("subject")) {
1087 * make enough room for the encoded string;
1088 * plus the QP header
1090 Subj = sbstr("subject");
1092 StrBufRFC2047encode(&encoded_subject, Subj);
1094 Recp = sbstr("recp");
1097 Wikipage = sbstr("wikipage");
1098 my_email_addr = sbstr("my_email_addr");
1100 CmdBuf = NewStrBufPlain(NULL,
1103 StrLength(encoded_subject) +
1106 StrLength(Wikipage) +
1107 StrLength(my_email_addr) +
1108 StrLength(references));
1110 StrBufPrintf(CmdBuf,
1114 ChrPtr(encoded_subject),
1115 ChrPtr(display_name),
1119 ChrPtr(my_email_addr),
1120 ChrPtr(references));
1121 FreeStrBuf(&references);
1123 lprintf(9, "%s\n", ChrPtr(CmdBuf));
1124 serv_puts(ChrPtr(CmdBuf));
1125 serv_getln(buf, sizeof buf);
1126 FreeStrBuf(&CmdBuf);
1127 FreeStrBuf(&encoded_subject);
1128 if (buf[0] == '4') {
1129 post_mime_to_server();
1130 if ( (havebstr("recp"))
1131 || (havebstr("cc" ))
1132 || (havebstr("bcc" ))
1134 sprintf(WCC->ImportantMessage, _("Message has been sent.\n"));
1137 sprintf(WC->ImportantMessage, _("Message has been posted.\n"));
1139 dont_post = lbstr("postseq");
1141 lprintf(9, "%s:%d: server post error: %s\n", __FILE__, __LINE__, buf);
1142 sprintf(WC->ImportantMessage, "%s", &buf[4]);
1148 DeleteHash(&WCC->attachments);
1151 * We may have been supplied with instructions regarding the location
1152 * to which we must return after posting. If found, go there.
1154 if (havebstr("return_to")) {
1155 http_redirect(bstr("return_to"));
1158 * If we were editing a page in a wiki room, go to that page now.
1160 else if (havebstr("wikipage")) {
1161 snprintf(buf, sizeof buf, "wiki?page=%s", bstr("wikipage"));
1165 * Otherwise, just go to the "read messages" loop.
1176 * \brief display the message entry screen
1178 void display_enter(void)
1182 const StrBuf *display_name = NULL;
1183 int recipient_required = 0;
1184 int subject_required = 0;
1185 int recipient_bad = 0;
1186 int is_anonymous = 0;
1187 wcsession *WCC = WC;
1191 if (havebstr("force_room")) {
1192 gotoroom(bstr("force_room"));
1195 display_name = sbstr("display_name");
1196 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1197 display_name = NULL;
1201 /** First test to see whether this is a room that requires recipients to be entered */
1202 serv_puts("ENT0 0");
1203 serv_getln(buf, sizeof buf);
1205 if (!strncmp(buf, "570", 3)) { /** 570 means that we need a recipient here */
1206 recipient_required = 1;
1208 else if (buf[0] != '2') { /** Any other error means that we cannot continue */
1209 sprintf(WCC->ImportantMessage, "%s", &buf[4]);
1214 /* Is the server strongly recommending that the user enter a message subject? */
1215 if ((buf[3] != '\0') && (buf[4] != '\0')) {
1216 subject_required = extract_int(&buf[4], 1);
1220 * Are we perhaps in an address book view? If so, then an "enter
1221 * message" command really means "add new entry."
1223 if (WCC->wc_default_view == VIEW_ADDRESSBOOK) {
1224 do_edit_vcard(-1, "", "", WCC->wc_roomname);
1229 * Are we perhaps in a calendar room? If so, then an "enter
1230 * message" command really means "add new calendar item."
1232 if (WCC->wc_default_view == VIEW_CALENDAR) {
1233 display_edit_event();
1238 * Are we perhaps in a tasks view? If so, then an "enter
1239 * message" command really means "add new task."
1241 if (WCC->wc_default_view == VIEW_TASKS) {
1242 display_edit_task();
1247 * Otherwise proceed normally.
1248 * Do a custom room banner with no navbar...
1251 if (recipient_required) {
1252 const StrBuf *Recp = NULL;
1253 const StrBuf *Cc = NULL;
1254 const StrBuf *Bcc = NULL;
1255 const StrBuf *Wikipage = NULL;
1256 StrBuf *CmdBuf = NULL;;
1257 const char CMD[] = "ENT0 0|%s|%d|0||%s||%s|%s|%s";
1259 Recp = sbstr("recp");
1262 Wikipage = sbstr("wikipage");
1264 CmdBuf = NewStrBufPlain(NULL,
1267 StrLength(display_name) +
1270 StrLength(Wikipage));
1272 StrBufPrintf(CmdBuf,
1276 ChrPtr(display_name),
1280 serv_puts(ChrPtr(CmdBuf));
1281 serv_getln(buf, sizeof buf);
1282 FreeStrBuf(&CmdBuf);
1284 if (!strncmp(buf, "570", 3)) { /** 570 means we have an invalid recipient listed */
1285 if (havebstr("recp") &&
1291 else if (buf[0] != '2') { /** Any other error means that we cannot continue */
1292 wprintf("<em>%s</em><br />\n", &buf[4]);/// -> important message
1296 svputlong("RCPTREQUIRED", recipient_required);
1297 svputlong("SUBJREQUIRED", recipient_required || subject_required);
1300 output_headers(1, 0, 0, 0, 1, 0);
1301 DoTemplate(HKEY("edit_message"), NULL, NULL, CTX_NONE);
1308 * \brief delete a message
1310 void delete_msg(void)
1315 msgid = lbstr("msgid");
1317 if (WC->wc_is_trash) { /** Delete from Trash is a real delete */
1318 serv_printf("DELE %ld", msgid);
1320 else { /** Otherwise move it to Trash */
1321 serv_printf("MOVE %ld|_TRASH_|0", msgid);
1324 serv_getln(buf, sizeof buf);
1325 sprintf(WC->ImportantMessage, "%s", &buf[4]);
1332 * \brief move a message to another folder
1339 msgid = lbstr("msgid");
1341 if (havebstr("move_button")) {
1342 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
1344 serv_getln(buf, sizeof buf);
1345 sprintf(WC->ImportantMessage, "%s", &buf[4]);
1347 sprintf(WC->ImportantMessage, (_("The message was not moved.")));
1358 * Confirm move of a message
1360 void confirm_move_msg(void)
1366 msgid = lbstr("msgid");
1369 output_headers(1, 1, 2, 0, 0, 0);
1370 wprintf("<div id=\"banner\">\n");
1372 wprintf(_("Confirm move of message"));
1374 wprintf("</div>\n");
1376 wprintf("<div id=\"content\" class=\"service\">\n");
1378 wprintf("<CENTER>");
1380 wprintf(_("Move this message to:"));
1381 wprintf("<br />\n");
1383 wprintf("<form METHOD=\"POST\" action=\"move_msg\">\n");
1384 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
1385 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n", bstr("msgid"));
1387 wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
1389 serv_getln(buf, sizeof buf);
1390 if (buf[0] == '1') {
1391 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1392 extract_token(targ, buf, 0, '|', sizeof targ);
1393 wprintf("<OPTION>");
1398 wprintf("</SELECT>\n");
1399 wprintf("<br />\n");
1401 wprintf("<INPUT TYPE=\"submit\" NAME=\"move_button\" VALUE=\"%s\">", _("Move"));
1403 wprintf("<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">", _("Cancel"));
1404 wprintf("</form></CENTER>\n");
1406 wprintf("</CENTER>\n");
1412 * Generic function to output an arbitrary MIME attachment from
1413 * message being composed
1415 * partnum The MIME part to be output
1416 * filename Fake filename to give
1417 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1419 void postpart(StrBuf *partnum, StrBuf *filename, int force_download)
1422 StrBuf *content_type;
1423 wc_mime_attachment *part;
1425 if (GetHash(WC->attachments, SKEY(partnum), &vPart) &&
1427 part = (wc_mime_attachment*) vPart;
1428 if (force_download) {
1429 content_type = NewStrBufPlain(HKEY("application/octet-stream"));
1432 content_type = NewStrBufDup(part->ContentType);
1434 output_headers(0, 0, 0, 0, 0, 0);
1435 StrBufAppendBuf(WC->WBuf, part->Data, 0);
1436 http_transmit_thing(ChrPtr(content_type), 0);
1438 hprintf("HTTP/1.1 404 %s\n", ChrPtr(partnum));
1439 output_headers(0, 0, 0, 0, 0, 0);
1440 hprintf("Content-Type: text/plain\r\n");
1441 wprintf(_("An error occurred while retrieving this part: %s/%s\n"),
1442 ChrPtr(partnum), ChrPtr(filename));
1445 FreeStrBuf(&content_type);
1450 * Generic function to output an arbitrary MIME part from an arbitrary
1451 * message number on the server.
1453 * msgnum Number of the item on the citadel server
1454 * partnum The MIME part to be output
1455 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1457 void mimepart(const char *msgnum, const char *partnum, int force_download)
1461 char content_type[256];
1463 serv_printf("OPNA %s|%s", msgnum, partnum);
1464 serv_getln(buf, sizeof buf);
1465 if (buf[0] == '2') {
1466 bytes = extract_long(&buf[4], 0);
1467 if (force_download) {
1468 strcpy(content_type, "application/octet-stream");
1471 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
1473 output_headers(0, 0, 0, 0, 0, 0);
1475 read_server_binary(WC->WBuf, bytes);
1477 serv_getln(buf, sizeof buf);
1478 http_transmit_thing(content_type, 0);
1480 hprintf("HTTP/1.1 404 %s\n", &buf[4]);
1481 output_headers(0, 0, 0, 0, 0, 0);
1482 hprintf("Content-Type: text/plain\r\n");
1483 wprintf(_("An error occurred while retrieving this part: %s\n"), &buf[4]);
1490 * Read any MIME part of a message, from the server, into memory.
1492 char *load_mimepart(long msgnum, char *partnum)
1496 char content_type[SIZ];
1499 serv_printf("DLAT %ld|%s", msgnum, partnum);
1500 serv_getln(buf, sizeof buf);
1501 if (buf[0] == '6') {
1502 bytes = extract_long(&buf[4], 0);
1503 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
1505 content = malloc(bytes + 2);
1506 serv_read(content, bytes);
1508 content[bytes] = 0; /* null terminate for good measure */
1517 * Read any MIME part of a message, from the server, into memory.
1519 void MimeLoadData(wc_mime_attachment *Mime)
1523 //// TODO: is there a chance the contenttype is different to the one we know?
1524 serv_printf("DLAT %ld|%s", Mime->msgnum, ChrPtr(Mime->PartNum));
1525 serv_getln(buf, sizeof buf);
1526 if (buf[0] == '6') {
1527 bytes = extract_long(&buf[4], 0);
1529 if (Mime->Data == NULL)
1530 Mime->Data = NewStrBufPlain(NULL, bytes);
1531 StrBuf_ServGetBLOB(Mime->Data, bytes);
1535 FlushStrBuf(Mime->Data);
1536 /// TODO XImportant message
1543 void view_mimepart(void) {
1544 mimepart(ChrPtr(WC->UrlFragment2),
1545 ChrPtr(WC->UrlFragment3),
1549 void download_mimepart(void) {
1550 mimepart(ChrPtr(WC->UrlFragment2),
1551 ChrPtr(WC->UrlFragment3),
1555 void view_postpart(void) {
1556 postpart(WC->UrlFragment2,
1561 void download_postpart(void) {
1562 postpart(WC->UrlFragment2,
1569 void h_readnew(void) { readloop(readnew);}
1570 void h_readold(void) { readloop(readold);}
1571 void h_readfwd(void) { readloop(readfwd);}
1572 void h_headers(void) { readloop(headers);}
1573 void h_do_search(void) { readloop(do_search);}
1584 WebcitAddUrlHandler(HKEY("readnew"), h_readnew, NEED_URL);
1585 WebcitAddUrlHandler(HKEY("readold"), h_readold, NEED_URL);
1586 WebcitAddUrlHandler(HKEY("readfwd"), h_readfwd, NEED_URL);
1587 WebcitAddUrlHandler(HKEY("headers"), h_headers, NEED_URL);
1588 WebcitAddUrlHandler(HKEY("do_search"), h_do_search, 0);
1589 WebcitAddUrlHandler(HKEY("display_enter"), display_enter, 0);
1590 WebcitAddUrlHandler(HKEY("post"), post_message, 0);
1591 WebcitAddUrlHandler(HKEY("move_msg"), move_msg, 0);
1592 WebcitAddUrlHandler(HKEY("delete_msg"), delete_msg, 0);
1593 WebcitAddUrlHandler(HKEY("confirm_move_msg"), confirm_move_msg, 0);
1594 WebcitAddUrlHandler(HKEY("msg"), embed_message, NEED_URL|AJAX);
1595 WebcitAddUrlHandler(HKEY("printmsg"), print_message, NEED_URL);
1596 WebcitAddUrlHandler(HKEY("mobilemsg"), mobile_message_view, NEED_URL);
1597 WebcitAddUrlHandler(HKEY("msgheaders"), display_headers, NEED_URL);
1599 WebcitAddUrlHandler(HKEY("mimepart"), view_mimepart, NEED_URL);
1600 WebcitAddUrlHandler(HKEY("mimepart_download"), download_mimepart, NEED_URL);
1601 WebcitAddUrlHandler(HKEY("postpart"), view_postpart, NEED_URL);
1602 WebcitAddUrlHandler(HKEY("postpart_download"), download_postpart, NEED_URL);