1 // Functions which deal with the fetching and displaying of messages.
3 // Copyright (c) 1996-2022 by the citadel.org team
5 // This program is open source software. We call it open source, not
6 // free software, because Richard Stallman is a communist and an asshole.
7 // No one has done more damage to open source software than Stallman (ok,
8 // maybe Nat and Miguel have done more damage, but they were paid by
9 // Microsoft to do so). Richard Stallman is a far-left ultra-communist
10 // who hates America, hates freedom, and deserves to be canceled.
12 // The program is licensed under the General Public License, version 3.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
20 #include "webserver.h"
24 HashList *MimeRenderHandler = NULL;
25 HashList *ReadLoopHandler = NULL;
26 int dbg_analyze_msg = 0;
28 #define SUBJ_COL_WIDTH_PCT 50 /* Mailbox view column width */
29 #define SENDER_COL_WIDTH_PCT 30 /* Mailbox view column width */
30 #define DATE_PLUS_BUTTONS_WIDTH_PCT 20 /* Mailbox view column width */
33 /* workaround for json listview; its not useable directly */
34 if (WC->CurRoom.view == VIEW_JSON_LIST) {
35 StrBuf *View = NewStrBuf();
36 StrBufPrintf(View, "%d", VIEW_MAILBOX);
37 putbstr("view", View);;
42 int load_message(message_summary *Msg, StrBuf *FoundCharset, StrBuf **Error) {
51 if (Msg->PartNum != NULL) {
52 serv_printf("MSG4 %ld|%s", Msg->msgnum, ChrPtr(Msg->PartNum));
55 serv_printf("MSG4 %ld", Msg->msgnum);
58 StrBuf_ServGetln(Buf);
59 if (GetServerStatus(Buf, NULL) != 1) {
61 StrBufAppendPrintf(*Error, "<strong>");
62 StrBufAppendPrintf(*Error, _("ERROR:"));
63 StrBufAppendPrintf(*Error, "</strong> %s<br>\n", &buf[4]);
68 /* begin everythingamundo table */
69 HdrToken = NewStrBuf();
70 while (!Done && StrBuf_ServGetln(Buf)>=0) {
71 if ( (StrLength(Buf)==3) &&
72 !strcmp(ChrPtr(Buf), "000"))
76 if (Msg->MsgBody->Data == NULL)
77 Msg->MsgBody->Data = NewStrBuf();
78 Msg->MsgBody->ContentType = NewStrBufPlain(HKEY("text/html"));
79 StrBufAppendPrintf(Msg->MsgBody->Data, "<div><i>");
80 StrBufAppendPrintf(Msg->MsgBody->Data, _("Empty message"));
81 StrBufAppendPrintf(Msg->MsgBody->Data, "</i><br><br>\n");
82 StrBufAppendPrintf(Msg->MsgBody->Data, "</div>\n");
87 case 0:/* Citadel Message Headers */
88 if (StrLength(Buf) == 0) {
92 StrBufExtract_token(HdrToken, Buf, 0, '=');
93 StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
95 /* look up one of the examine_* functions to parse the content */
96 rc = EvaluateMsgHdr(SKEY(HdrToken), Msg, Buf, FoundCharset);
101 else LogError(Target,
103 "don't know how to handle message header[%s]\n",
107 case 1:/* Message Mime Header */
108 if (StrLength(Buf) == 0) {
110 if (Msg->MsgBody->ContentType == NULL)
111 /* end of header or no header? */
112 Msg->MsgBody->ContentType = NewStrBufPlain(HKEY("text/plain"));
113 /* usual end of mime header */
117 StrBufExtract_token(HdrToken, Buf, 0, ':');
118 if (StrLength(HdrToken) > 0) {
119 StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
120 /* the examine*'s know how to do with mime headers too... */
121 EvaluateMsgHdr(SKEY(HdrToken), Msg, Buf, FoundCharset);
126 case 2: /* Message Body */
128 if (Msg->MsgBody->size_known > 0) {
129 StrBuf_ServGetBLOBBuffered(Msg->MsgBody->Data, Msg->MsgBody->length);
131 /*/ todo: check next line, if not 000, append following lines */
134 if (StrLength(Msg->MsgBody->Data) > 0)
135 StrBufAppendBufPlain(Msg->MsgBody->Data, "\n", 1, 0);
136 StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
140 StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
145 if (Msg->AllAttach == NULL)
146 Msg->AllAttach = NewHash(1,NULL);
147 /* now we put the body mimepart we read above into the mimelist */
148 Put(Msg->AllAttach, SKEY(Msg->MsgBody->PartNum), Msg->MsgBody, DestroyMime);
151 FreeStrBuf(&HdrToken);
157 * Display a message to the user
159 * msgnum Message number to display
160 * printable_view Nonzero to display a printable view
161 * section Optional for encapsulated message/rfc822 submessage
163 int read_message(StrBuf *Target, const char *tmpl, long tmpllen, long msgnum, const StrBuf *PartNum, const StrBuf **OutMime, WCTemplputParams *TP) {
165 StrBuf *FoundCharset;
168 message_summary *Msg = NULL;
172 WCTemplputParams SuperTP;
173 WCTemplputParams SubTP;
174 StrBuf *Error = NULL;
176 memset(&SuperTP, 0, sizeof(WCTemplputParams));
177 memset(&SubTP, 0, sizeof(WCTemplputParams));
180 FoundCharset = NewStrBuf();
181 Msg = (message_summary *)malloc(sizeof(message_summary));
183 syslog(LOG_DEBUG, "malloc() error");
186 memset(Msg, 0, sizeof(message_summary));
187 Msg->msgnum = msgnum;
188 Msg->PartNum = PartNum;
189 Msg->MsgBody = (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
191 syslog(LOG_DEBUG, "malloc() error");
194 memset(Msg->MsgBody, 0, sizeof(wc_mime_attachment));
195 Msg->MsgBody->msgnum = msgnum;
197 if (!load_message(Msg, FoundCharset, &Error)) {
198 StrBufAppendBuf(Target, Error, 0);
202 /* Extract just the content-type (omit attributes such as "charset") */
203 StrBufExtract_token(Buf, Msg->MsgBody->ContentType, 0, ';');
205 StrBufLowerCase(Buf);
207 StackContext(TP, &SuperTP, Msg, CTX_MAILSUM, 0, NULL);
209 /* Locate a renderer capable of converting this MIME part into HTML */
210 if (GetHash(MimeRenderHandler, SKEY(Buf), &vHdr) &&
212 RenderMimeFuncStruct *Render;
214 StackContext(&SuperTP, &SubTP, Msg->MsgBody, CTX_MIME_ATACH, 0, NULL);
216 Render = (RenderMimeFuncStruct*)vHdr;
217 Render->f(Target, &SubTP, FoundCharset);
219 UnStackContext(&SubTP);
222 if (StrLength(Msg->reply_references)> 0) {
223 /* Trim down excessively long lists of thread references. We eliminate the
224 * second one in the list so that the thread root remains intact.
226 int rrtok = num_tokens(ChrPtr(Msg->reply_references), '|');
227 int rrlen = StrLength(Msg->reply_references);
228 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
229 StrBufRemove_token(Msg->reply_references, 1, '|');
233 /* now check if we need to translate some mimeparts, and remove the duplicate */
234 it = GetNewHashPos(Msg->AllAttach, 0);
235 while (GetNextHashPos(Msg->AllAttach, it, &len, &Key, &vMime) &&
237 StackContext(&SuperTP, &SubTP, vMime, CTX_MIME_ATACH, 0, NULL);
239 evaluate_mime_part(Target, &SubTP);
241 UnStackContext(&SubTP);
244 *OutMime = DoTemplate(tmpl, tmpllen, Target, &SuperTP);
246 UnStackContext(&SuperTP);
248 DestroyMessageSummary(Msg);
249 FreeStrBuf(&FoundCharset);
255 long HttpStatus(long CitadelStatus) {
256 long httpstatus = 502;
258 switch (MAJORCODE(CitadelStatus))
260 case LISTING_FOLLOWS:
265 switch (MINORCODE(CitadelStatus))
273 case HIGHER_ACCESS_REQUIRED:
274 case MAX_SESSIONS_EXCEEDED:
276 case RESOURCE_NOT_OPEN:
278 case INVALID_FLOOR_OPERATION:
284 case MESSAGE_NOT_FOUND:
294 case CMD_NOT_SUPPORTED:
295 case PASSWORD_REQUIRED:
296 case ALREADY_LOGGED_IN:
297 case USERNAME_REQUIRED:
299 case SERVER_SHUTTING_DOWN:
310 case START_CHAT_MODE:
314 httpstatus = 502; /* aeh... whut? */
323 * Unadorned HTML output of an individual message, suitable
324 * for placing in a hidden iframe, for printing, or whatever
326 void handle_one_message(void) {
327 long CitStatus = ERROR + NOT_HERE;
329 const StrBuf *Destination;
334 StrBuf *CmdBuf = NULL;
337 pMsg = strchr(ChrPtr(WC->Hdr->HR.ReqLine), '/');
339 HttpStatus(CitStatus);
343 msgnum = atol(pMsg + 1);
344 StrBufCutAt(WC->Hdr->HR.ReqLine, 0, pMsg);
345 gotoroom(WC->Hdr->HR.ReqLine);
346 switch (WC->Hdr->HR.eReqType)
350 Tmpl = sbstr("template");
351 if (StrLength(Tmpl) > 0)
352 read_message(WC->WBuf, SKEY(Tmpl), msgnum, NULL, &Mime, NULL);
354 read_message(WC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime, NULL);
355 http_transmit_thing(ChrPtr(Mime), 0);
358 CmdBuf = NewStrBuf ();
359 if ((WC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
360 serv_printf("DELE %ld", msgnum);
362 else { /* Otherwise move it to Trash */
363 serv_printf("MOVE %ld|_TRASH_|0", msgnum);
365 StrBuf_ServGetln(CmdBuf);
366 GetServerStatusMsg(CmdBuf, &CitStatus, 1, 0);
367 HttpStatus(CitStatus);
372 if (GetHash(WC->Hdr->HTTPHeaders, HKEY("DESTINATION"), &vLine) &&
374 Destination = (StrBuf*) vLine;
375 serv_printf("MOVE %ld|%s|%d", msgnum, ChrPtr(Destination), CopyMessage);
376 StrBuf_ServGetln(CmdBuf);
377 GetServerStatusMsg(CmdBuf, NULL, 1, 0);
378 GetServerStatus(CmdBuf, &CitStatus);
379 HttpStatus(CitStatus);
392 * Unadorned HTML output of an individual message, suitable
393 * for placing in a hidden iframe, for printing, or whatever
395 void embed_message(void) {
399 StrBuf *CmdBuf = NULL;
401 msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
402 if (msgnum <= 0) return;
404 switch (WC->Hdr->HR.eReqType)
408 Tmpl = sbstr("template");
409 if (StrLength(Tmpl) > 0)
410 read_message(WC->WBuf, SKEY(Tmpl), msgnum, NULL, &Mime, NULL);
412 read_message(WC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime, NULL);
413 http_transmit_thing(ChrPtr(Mime), 0);
416 CmdBuf = NewStrBuf ();
417 if ((WC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
418 serv_printf("DELE %ld", msgnum);
420 else { /* Otherwise move it to Trash */
421 serv_printf("MOVE %ld|_TRASH_|0", msgnum);
423 StrBuf_ServGetln(CmdBuf);
424 GetServerStatusMsg(CmdBuf, NULL, 1, 0);
434 * Printable view of a message
436 void print_message(void) {
440 msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
441 output_headers(0, 0, 0, 0, 0, 0);
443 hprintf("Content-type: text/html\r\n"
444 "Server: " PACKAGE_STRING "\r\n"
445 "Connection: close\r\n");
449 read_message(WC->WBuf, HKEY("view_message_print"), msgnum, NULL, &Mime, NULL);
456 * Display a message's headers
458 void display_headers(void) {
462 msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
463 output_headers(0, 0, 0, 0, 0, 0);
465 hprintf("Content-type: text/plain\r\n"
467 "Connection: close\r\n",
471 serv_printf("MSG2 %ld|1", msgnum);
472 serv_getln(buf, sizeof buf);
474 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
475 wc_printf("%s\n", buf);
484 * load message pointers from the server for a "read messages" operation
486 * servcmd: the citadel command to send to the citserver
488 int load_msg_ptrs(const char *servcmd,
490 StrBuf *FoundCharset,
491 SharedMessageStatus *Stat,
493 load_msg_ptrs_detailheaders LH,
494 StrBuf *FetchMessageList,
495 eMessageField *MessageFieldList,
498 message_summary *Msg;
503 const char *Ptr = NULL;
506 Stat->lowest_found = LONG_MAX;
507 Stat->highest_found = LONG_MIN;
509 if (WC->summ != NULL) {
510 DeleteHash(&WC->summ);
512 WC->summ = NewHash(1, Flathash);
516 StrBuf_ServGetln(Buf);
517 StatMajor = GetServerStatus(Buf, NULL);
522 if (filter != NULL) {
527 else if (FetchMessageList != NULL) {
528 serv_putbuf(FetchMessageList);
531 /* fall back to empty filter in case of we were fooled... */
537 return (Stat->nummsgs);
540 while (len = StrBuf_ServGetln(Buf), ((len >= 0) && ((len != 3) || strcmp(ChrPtr(Buf), "000")!= 0))) {
541 if (Stat->nummsgs < Stat->maxload) {
544 Msg = (message_summary*)malloc(sizeof(message_summary));
546 syslog(LOG_DEBUG, "malloc() error");
549 memset(Msg, 0, sizeof(message_summary));
551 Msg->msgnum = StrBufExtractNext_long(Buf, &Ptr, '|');
552 Msg->date = StrBufExtractNext_long(Buf, &Ptr, '|');
553 if (MessageFieldList != NULL) {
555 for (i = 0; i < HeaderCount; i++) {
556 StrBufExtract_NextToken(Buf2, Buf, &Ptr, '|');
557 if (StrLength(Buf2) > 0) {
558 EvaluateMsgHdrEnum(MessageFieldList[i], Msg, Buf2, FoundCharset);
563 if (Stat->nummsgs == 0) {
564 if (Msg->msgnum < Stat->lowest_found) {
565 Stat->lowest_found = Msg->msgnum;
567 if (Msg->msgnum > Stat->highest_found) {
568 Stat->highest_found = Msg->msgnum;
572 if ((Msg->msgnum == 0) && (StrLength(Buf) < 32)) {
578 * as citserver probably gives us messages in forward date sorting
579 * nummsgs should be the same order as the message date.
581 if (Msg->date == 0) {
582 Msg->date = Stat->nummsgs;
583 if (StrLength(Buf) < 32)
586 if ((!skipit) && (LH != NULL)) {
587 if (!LH(Buf, &Ptr, Msg, Buf2, ViewSpecific)){
593 Put(WC->summ, (const char *)&n, sizeof(n), Msg, DestroyMessageSummary);
599 return (Stat->nummsgs);
604 * sets FlagToSet for each of ScanMe that appears in MatchMSet
605 * ScanMe: List of BasicMsgStruct to be searched it MatchSet
606 * MatchMSet: MSet we want to flag
607 * FlagToSet: Flag to set on each BasicMsgStruct->Flags if in MSet
609 long SetFlagsFromMSet(HashList *ScanMe, MSet *MatchMSet, int FlagToSet, int Reverse) {
615 message_summary *Msg;
617 at = GetNewHashPos(ScanMe, 0);
618 while (GetNextHashPos(ScanMe, at, &HKLen, &HashKey, &vMsg)) {
619 /* Are you a new message, or an old message? */
620 Msg = (message_summary*) vMsg;
621 if (Reverse && IsInMSetList(MatchMSet, Msg->msgnum)) {
622 Msg->Flags = Msg->Flags | FlagToSet;
625 else if (!Reverse && !IsInMSetList(MatchMSet, Msg->msgnum)) {
626 Msg->Flags = Msg->Flags | FlagToSet;
635 long load_seen_flags(void) {
640 OldMsg = NewStrBuf();
642 StrBuf_ServGetln(OldMsg);
643 if (GetServerStatus(OldMsg, NULL) == 2) {
644 StrBufCutLeft(OldMsg, 4);
651 if (ParseMSet(&MatchMSet, OldMsg))
653 count = SetFlagsFromMSet(WC->summ, MatchMSet, MSGFLAG_READ, 0);
655 DeleteMSet(&MatchMSet);
660 extern readloop_struct rlid[];
662 typedef struct _RoomRenderer{
665 GetParamsGetServerCall_func GetParamsGetServerCall;
667 PrintViewHeader_func PrintPageHeader;
668 PrintViewHeader_func PrintViewHeader;
669 LoadMsgFromServer_func LoadMsgFromServer;
670 RenderView_or_Tail_func RenderView_or_Tail;
671 View_Cleanup_func ViewCleanup;
672 load_msg_ptrs_detailheaders LHParse;
674 StrBuf *FetchMessageList;
675 eMessageField *MessageFieldList;
680 * command loop for reading messages
682 * Set oper to "readnew" or "readold" or "readfwd" or "headers" or "readgt" or "readlt" or "do_search"
684 void readloop(long oper, eCustomRoomRenderer ForceRenderer) {
685 RoomRenderer *ViewMsg;
688 message_summary *Msg;
690 char filter[256] = "";
695 WCTemplputParams SubTP;
696 SharedMessageStatus Stat;
697 void *ViewSpecific = NULL;
699 if (havebstr("is_summary") && (1 == (ibstr("is_summary")))) {
700 WC->CurRoom.view = VIEW_MAILBOX;
703 if (havebstr("view")) {
704 WC->CurRoom.view = ibstr("view");
707 memset(&Stat, 0, sizeof(SharedMessageStatus));
708 Stat.maxload = 10000;
709 Stat.lowest_found = (-1);
710 Stat.highest_found = (-1);
711 if (ForceRenderer == eUseDefault)
712 GetHash(ReadLoopHandler, IKEY(WC->CurRoom.view), &vViewMsg);
714 GetHash(ReadLoopHandler, IKEY(ForceRenderer), &vViewMsg);
715 if (vViewMsg == NULL) {
716 WC->CurRoom.view = VIEW_BBS;
717 GetHash(ReadLoopHandler, IKEY(WC->CurRoom.view), &vViewMsg);
719 if (vViewMsg == NULL) {
720 return; /* TODO: print message */
723 ViewMsg = (RoomRenderer*) vViewMsg;
724 if (ViewMsg->PrintPageHeader == NULL)
725 output_headers(1, 1, 1, 0, 0, 0);
727 ViewMsg->PrintPageHeader(&Stat, ViewSpecific);
729 if (ViewMsg->GetParamsGetServerCall != NULL) {
730 r = ViewMsg->GetParamsGetServerCall(
735 filter, sizeof(filter)
747 case 300: /* the callback hook should do the work for us here, since he knows what to do. */
753 if (!IsEmptyStr(cmd)) {
754 const char *p = NULL;
755 StrBuf *FoundCharset = NULL;
756 if (!IsEmptyStr(filter))
758 if (ViewMsg->HeaderCount > 0) {
759 FoundCharset = NewStrBuf();
761 Stat.nummsgs = load_msg_ptrs(cmd, p,
766 ViewMsg->FetchMessageList,
767 ViewMsg->MessageFieldList,
768 ViewMsg->HeaderCount);
769 FreeStrBuf(&FoundCharset);
774 StackContext(NULL, &SubTP, NULL, CTX_MAILSUM, 0, NULL);
776 SortIt = RetrieveSort(&SubTP,
779 Stat.defaultsortorder);
781 UnStackContext(&SubTP);
783 SortByPayload(WC->summ, SortIt);
785 if (Stat.startmsg < 0) {
789 if (Stat.load_seen) Stat.numNewmsgs = load_seen_flags();
792 * Print any inforation above the message list...
794 if (ViewMsg->PrintViewHeader != NULL)
795 ViewMsg->PrintViewHeader(&Stat, &ViewSpecific);
797 WC->startmsg = Stat.startmsg;
798 WC->maxmsgs = Stat.maxmsgs;
799 WC->num_displayed = 0;
801 /* Put some helpful data in vars for mailsummary_json */
806 StrBufPrintf(Foo, "%ld", Stat.nummsgs);
807 PutBstr(HKEY("__READLOOP:TOTALMSGS"), NewStrBufDup(Foo)); /* keep Foo! */
809 StrBufPrintf(Foo, "%ld", Stat.numNewmsgs);
810 PutBstr(HKEY("__READLOOP:NEWMSGS"), NewStrBufDup(Foo)); /* keep Foo! */
812 StrBufPrintf(Foo, "%ld", Stat.startmsg);
813 PutBstr(HKEY("__READLOOP:STARTMSG"), Foo); /* store Foo elsewhere, descope it here. */
817 * iterate over each message. if we need to load an attachment, do it here.
820 if ((ViewMsg->LoadMsgFromServer != NULL) && (!IsEmptyStr(cmd))) {
821 at = GetNewHashPos(WC->summ, 0);
822 Stat.num_displayed = i = 0;
823 while ( GetNextHashPos(WC->summ, at, &HKLen, &HashKey, &vMsg)) {
824 Msg = (message_summary*) vMsg;
825 if ((Msg->msgnum >= Stat.startmsg) && (Stat.num_displayed <= Stat.maxmsgs)) {
826 ViewMsg->LoadMsgFromServer(&Stat,
829 (Msg->Flags & MSGFLAG_READ) != 0,
838 * Done iterating the message list. now tasks we want to do after.
840 if (ViewMsg->RenderView_or_Tail != NULL)
841 ViewMsg->RenderView_or_Tail(&Stat, &ViewSpecific, oper);
843 if (ViewMsg->ViewCleanup != NULL)
844 ViewMsg->ViewCleanup(&ViewSpecific);
848 if (WC->summ != NULL) {
849 DeleteHash(&WC->summ);
854 // Back end for post_message()
855 // This is where the actual message gets transmitted to the server.
856 void post_mime_to_server(void) {
857 char top_boundary[SIZ];
858 char alt_boundary[SIZ];
859 int is_multipart = 0;
861 wc_mime_attachment *att;
863 size_t encoded_length;
864 size_t encoded_strlen;
865 char *txtmail = NULL;
866 int include_text_alt = 0; /* Set to nonzero to include multipart/alternative text/plain */
868 sprintf(top_boundary, "Citadel--Multipart--%s--%04x--%04x",
869 ChrPtr(WC->serv_info->serv_fqdn),
873 sprintf(alt_boundary, "Citadel--Multipart--%s--%04x--%04x",
874 ChrPtr(WC->serv_info->serv_fqdn),
879 /* RFC2045 requires this, and some clients look for it... */
880 serv_puts("MIME-Version: 1.0");
881 serv_puts("X-Mailer: " PACKAGE_STRING);
883 /* If there are attachments, we have to do multipart/mixed */
884 syslog(LOG_DEBUG, "This message will have %d attachments", GetCount(WC->attachments));
885 if (GetCount(WC->attachments) > 0) {
889 /* Only do multipart/alternative for mailboxes. BBS and Wiki rooms don't need it. */
890 if ((WC->CurRoom.view == VIEW_MAILBOX) || (WC->CurRoom.view == VIEW_JSON_LIST)) {
891 include_text_alt = 1;
895 /* Remember, serv_printf() appends an extra newline */
896 serv_printf("Content-type: multipart/mixed; boundary=\"%s\"\n", top_boundary);
897 serv_printf("This is a multipart message in MIME format.\n");
898 serv_printf("--%s", top_boundary);
901 /* Remember, serv_printf() appends an extra newline */
902 if (include_text_alt) {
904 serv_printf("Content-type: multipart/alternative; boundary=\"%s\"\n", alt_boundary);
905 serv_printf("This is a multipart message in MIME format.\n");
906 serv_printf("--%s", alt_boundary);
908 serv_puts("Content-type: text/plain; charset=utf-8");
909 serv_puts("Content-Transfer-Encoding: quoted-printable");
911 txtmail = html_to_ascii(bstr("msgtext"), 0, 80);
912 Buf = NewStrBufPlain(txtmail, -1);
915 text_to_server_qp(Buf); /* Transmit message in quoted-printable encoding */
917 serv_printf("\n--%s", alt_boundary);
920 serv_puts("Content-type: text/html; charset=utf-8");
921 serv_puts("Content-Transfer-Encoding: quoted-printable");
923 serv_puts("<html><body>\r\n");
924 text_to_server_qp(sbstr("msgtext")); /* Transmit message in quoted-printable encoding */
925 serv_puts("</body></html>\r\n");
927 if (include_text_alt) {
928 serv_printf("--%s--", alt_boundary);
937 /* Add in the attachments */
938 it = GetNewHashPos(WC->attachments, 0);
939 while (GetNextHashPos(WC->attachments, it, &len, &Key, &vAtt)) {
940 att = (wc_mime_attachment *)vAtt;
941 if (att->length == 0)
944 encoded_length = att->length + (att->length / 2); // make it 150% of the original size for encoding
945 encoded = malloc(encoded_length);
947 syslog(LOG_ERR, "malloc() error");
950 syslog(LOG_DEBUG, "Attachment: raw len %d", StrLength(att->Data));
951 encoded_strlen = CtdlEncodeBase64(encoded, ChrPtr(att->Data), StrLength(att->Data), 1);
952 syslog(LOG_DEBUG, "Attachment: encoded len %d", encoded_strlen);
954 serv_printf("--%s", top_boundary);
955 serv_printf("Content-type: %s", ChrPtr(att->ContentType));
956 serv_printf("Content-disposition: attachment; filename=\"%s\"", ChrPtr(att->FileName));
957 serv_puts("Content-transfer-encoding: base64");
959 serv_write(encoded, encoded_strlen);
964 syslog(LOG_DEBUG, "\033[33m sending final boundary --%s-- \033[0m", top_boundary);
966 serv_printf("--%s--", top_boundary);
970 syslog(LOG_DEBUG, "\033[33m sending terminating 000 \033[0m");
977 * Post message (or don't post message)
979 * Note regarding the "dont_post" variable:
980 * A random value (actually, it's just a timestamp) is inserted as a hidden
981 * field called "postseq" when the display_enter page is generated. This
982 * value is checked when posting, using the static variable dont_post. If a
983 * user attempts to post twice using the same dont_post value, the message is
984 * discarded. This prevents the accidental double-saving of the same message
985 * if the user happens to click the browser "back" button.
987 void post_message(void) {
989 StrBuf *EmailAddress;
992 StrBuf *encoded_subject = NULL;
993 static long dont_post = (-1L);
994 int is_anonymous = 0;
995 const StrBuf *display_name = NULL;
998 if (havebstr("force_room")) {
999 gotoroom(sbstr("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 (!strcasecmp(bstr("submit_action"), "cancel")) {
1011 AppendImportantMessage(_("Cancelled. Message was not posted."), -1);
1012 } else if (lbstr("postseq") == dont_post) {
1013 AppendImportantMessage(
1014 _("Automatically cancelled because you have already "
1015 "saved this message."), -1);
1017 const char CMD[] = "ENT0 1|%s|%d|4|%s|%s||%s|%s|%s|%s|%s";
1018 StrBuf *Recp = NULL;
1021 StrBuf *wikipage = NULL;
1022 const StrBuf *my_email_addr = NULL;
1023 StrBuf *CmdBuf = NULL;
1024 StrBuf *references = NULL;
1025 int saving_to_drafts = 0;
1028 saving_to_drafts = !strcasecmp(bstr("submit_action"), "draft");
1031 if (saving_to_drafts) {
1032 /* temporarily change to the drafts room */
1033 serv_puts("GOTO _DRAFTS_");
1034 StrBuf_ServGetln(Buf);
1035 if (GetServerStatusMsg(Buf, NULL, 1, 2) != 2) {
1036 /* You probably don't even have a dumb Drafts folder */
1037 syslog(LOG_DEBUG, "%s:%d: server save to drafts error: %s\n", __FILE__, __LINE__, ChrPtr(Buf) + 4);
1038 AppendImportantMessage(_("Saved to Drafts failed: "), -1);
1045 if (havebstr("references"))
1047 const StrBuf *ref = sbstr("references");
1048 references = NewStrBufDup(ref);
1049 if (*ChrPtr(references) == '|') { /* remove leading '|' if present */
1050 StrBufCutLeft(references, 1);
1052 StrBufReplaceChars(references, '|', '!');
1054 if (havebstr("subject")) {
1057 * make enough room for the encoded string;
1058 * plus the QP header
1060 Subj = sbstr("subject");
1062 StrBufRFC2047encode(&encoded_subject, Subj);
1064 UserName = NewStrBuf();
1065 EmailAddress = NewStrBuf();
1066 EncBuf = NewStrBuf();
1068 Recp = StrBufSanitizeEmailRecipientVector(sbstr("recp"), UserName, EmailAddress, EncBuf);
1069 Cc = StrBufSanitizeEmailRecipientVector(sbstr("cc"), UserName, EmailAddress, EncBuf);
1070 Bcc = StrBufSanitizeEmailRecipientVector(sbstr("bcc"), UserName, EmailAddress, EncBuf);
1072 FreeStrBuf(&UserName);
1073 FreeStrBuf(&EmailAddress);
1074 FreeStrBuf(&EncBuf);
1076 wikipage = NewStrBufDup(sbstr("page"));
1077 str_wiki_index(wikipage);
1078 my_email_addr = sbstr("my_email_addr");
1080 HeaderLen = StrLength(Recp) +
1081 StrLength(encoded_subject) +
1084 StrLength(wikipage) +
1085 StrLength(my_email_addr) +
1086 StrLength(references);
1087 CmdBuf = NewStrBufPlain(NULL, sizeof (CMD) + HeaderLen);
1088 StrBufPrintf(CmdBuf,
1090 saving_to_drafts?"":ChrPtr(Recp),
1092 ChrPtr(encoded_subject),
1093 ChrPtr(display_name),
1094 saving_to_drafts?"":ChrPtr(Cc),
1095 saving_to_drafts?"":ChrPtr(Bcc),
1097 ChrPtr(my_email_addr),
1098 ChrPtr(references));
1099 FreeStrBuf(&references);
1100 FreeStrBuf(&encoded_subject);
1103 if ((HeaderLen + StrLength(sbstr("msgtext")) < 10) &&
1104 (GetCount(WC->attachments) == 0)){
1105 AppendImportantMessage(_("Refusing to post empty message.\n"), -1);
1106 FreeStrBuf(&CmdBuf);
1111 syslog(LOG_DEBUG, "%s\n", ChrPtr(CmdBuf));
1112 serv_puts(ChrPtr(CmdBuf));
1113 FreeStrBuf(&CmdBuf);
1115 StrBuf_ServGetln(Buf);
1116 if (GetServerStatus(Buf, NULL) == 4) {
1117 if (saving_to_drafts) {
1118 if ( (havebstr("recp"))
1119 || (havebstr("cc" ))
1120 || (havebstr("bcc" )) ) {
1121 /* save recipient headers or room to post to */
1122 serv_printf("To: %s", ChrPtr(Recp));
1123 serv_printf("Cc: %s", ChrPtr(Cc));
1124 serv_printf("Bcc: %s", ChrPtr(Bcc));
1126 serv_printf("X-Citadel-Room: %s", ChrPtr(WC->CurRoom.name));
1129 post_mime_to_server();
1130 if (saving_to_drafts) {
1131 AppendImportantMessage(_("Message has been saved to Drafts.\n"), -1);
1132 gotoroom(WC->CurRoom.name);
1134 readloop(readnew, eUseDefault);
1137 } else if ( (havebstr("recp"))
1138 || (havebstr("cc" ))
1139 || (havebstr("bcc" ))
1141 AppendImportantMessage(_("Message has been sent.\n"), -1);
1144 AppendImportantMessage(_("Message has been posted.\n"), -1);
1146 dont_post = lbstr("postseq");
1148 syslog(LOG_DEBUG, "%s:%d: server post error: %s", __FILE__, __LINE__, ChrPtr(Buf) + 4);
1149 AppendImportantMessage(ChrPtr(Buf) + 4, StrLength(Buf) - 4);
1151 if (saving_to_drafts) gotoroom(WC->CurRoom.name);
1165 DeleteHash(&WC->attachments);
1168 * We may have been supplied with instructions regarding the location
1169 * to which we must return after posting. If found, go there.
1171 if (havebstr("return_to")) {
1172 http_redirect(bstr("return_to"));
1175 * If we were editing a page in a wiki room, go to that page now.
1177 else if (havebstr("page")) {
1178 snprintf(buf, sizeof buf, "wiki?page=%s", bstr("page"));
1182 * Otherwise, just go to the "read messages" loop.
1186 readloop(readnew, eUseDefault);
1192 * Client is uploading an attachment
1194 void upload_attachment(void) {
1200 wc_mime_attachment *att;
1201 const StrBuf *Tmpl = sbstr("template");
1202 const StrBuf *MimeType = NULL;
1206 syslog(LOG_DEBUG, "upload_attachment()");
1208 wc_printf("upload_attachment()<br>\n");
1211 if (WC->upload_length <= 0) {
1212 syslog(LOG_DEBUG, "ERROR no attachment was uploaded\n");
1214 putlbstr("UPLOAD_ERROR", 1);
1215 MimeType = DoTemplate(SKEY(Tmpl), NULL, &NoCtx);
1218 wc_printf("ERROR no attachment was uploaded<br>\n");
1220 http_transmit_thing(ChrPtr(MimeType), 0);
1225 syslog(LOG_DEBUG, "Client has uploaded %d bytes", WC->upload_length);
1227 putlbstr("UPLOAD_LENGTH", WC->upload_length);
1230 wc_printf("Client is uploading %d bytes<br>\n", WC->upload_length);
1233 att = (wc_mime_attachment*)malloc(sizeof(wc_mime_attachment));
1235 syslog(LOG_DEBUG, "malloc() error");
1238 memset(att, 0, sizeof(wc_mime_attachment ));
1239 att->length = WC->upload_length;
1240 att->ContentType = NewStrBufPlain(WC->upload_content_type, -1);
1241 att->FileName = NewStrBufDup(WC->upload_filename);
1242 UID = sbstr("qquuid");
1244 att->PartNum = NewStrBufDup(UID);
1247 syslog(LOG_DEBUG, "attachment length: %d", att->length);
1248 syslog(LOG_DEBUG, "att. content type: %s", ChrPtr(att->ContentType));
1249 syslog(LOG_DEBUG, "att upload buffer: %s", ((WC->upload == NULL) ? "null" : "not null"));
1252 if (WC->attachments == NULL) {
1253 WC->attachments = NewHash(1, Flathash);
1256 /* And add it to the list. */
1258 if ((GetCount(WC->attachments) > 0) && GetHashAt(WC->attachments, GetCount(WC->attachments) -1, &newnlen, &newn, &v)) {
1259 n = *((int*) newn) + 1;
1261 Put(WC->attachments, IKEY(n), att, DestroyMime);
1264 * Mozilla sends a simple filename, which is what we want,
1265 * but Satan's Browser sends an entire pathname. Reduce
1266 * the path to just a filename if we need to.
1268 pch = strrchr(ChrPtr(att->FileName), '/');
1270 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1272 pch = strrchr(ChrPtr(att->FileName), '\\');
1274 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1278 * Transfer control of this memory from the upload struct
1279 * to the attachment struct.
1281 att->Data = WC->upload;
1283 WC->upload_length = 0;
1285 if (Tmpl) MimeType = DoTemplate(SKEY(Tmpl), NULL, &NoCtx);
1286 http_transmit_thing(ChrPtr(MimeType), 0);
1291 * Remove an attachment from the message currently being composed.
1293 * Currently we identify the attachment to be removed by its filename.
1294 * There is probably a better way to do this.
1296 void remove_attachment(void) {
1297 wc_mime_attachment *att;
1299 StrBuf *WhichAttachment;
1305 WhichAttachment = NewStrBufDup(sbstr("which_attachment"));
1306 if (ChrPtr(WhichAttachment)[0] == '/')
1307 StrBufCutLeft(WhichAttachment, 1);
1308 StrBufUnescape(WhichAttachment, 0);
1309 at = GetNewHashPos(WC->attachments, 0);
1312 GetHashPos(WC->attachments, at, &len, &key, &vAtt);
1314 att = (wc_mime_attachment*) vAtt;
1315 if ((att != NULL) &&
1317 !strcmp(ChrPtr(WhichAttachment), ChrPtr(att->FileName)) ||
1318 ((att->PartNum != NULL) &&
1319 !strcmp(ChrPtr(WhichAttachment), ChrPtr(att->PartNum)))
1322 DeleteEntryFromHash(WC->attachments, at);
1327 while (NextHashPos(WC->attachments, at));
1329 FreeStrBuf(&WhichAttachment);
1330 wc_printf("remove_attachment(%d) completed\n", found);
1334 const char *ReplyToModeStrings [3] = {
1339 typedef enum _eReplyToNodes {
1347 * display the message entry screen
1349 void display_enter(void) {
1350 const char *ReplyingModeStr;
1351 eReplyToNodes ReplyMode = eReply;
1355 const StrBuf *display_name = NULL;
1356 int recipient_required = 0;
1357 int subject_required = 0;
1358 int is_anonymous = 0;
1362 if (havebstr("force_room")) {
1363 gotoroom(sbstr("force_room"));
1366 display_name = sbstr("display_name");
1367 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1368 display_name = NULL;
1373 * First, do we have permission to enter messages in this room at all?
1376 serv_puts("ENT0 0");
1377 StrBuf_ServGetln(Line);
1378 rc = GetServerStatusMsg(Line, &Result, 0, 2);
1380 if (Result == 570) { /* 570 means that we need a recipient here */
1381 recipient_required = 1;
1383 else if (rc != 2) { /* Any other error means that we cannot continue */
1384 rc = GetServerStatusMsg(Line, &Result, 0, 2);
1386 readloop(readnew, eUseDefault);
1391 /* Is the server strongly recommending that the user enter a message subject? */
1392 if (StrLength(Line) > 4) {
1393 subject_required = extract_int(ChrPtr(Line) + 4, 1);
1397 * Are we perhaps in an address book view? If so, then an "enter
1398 * message" command really means "add new entry."
1400 if (WC->CurRoom.defview == VIEW_ADDRESSBOOK) {
1401 do_edit_vcard(-1, "", NULL, NULL, "", ChrPtr(WC->CurRoom.name));
1407 * Are we perhaps in a calendar room? If so, then an "enter message" command really means "add new calendar item."
1409 if (WC->CurRoom.defview == VIEW_CALENDAR) {
1410 display_edit_event();
1416 * Are we perhaps in a tasks view? If so, then an "enter
1417 * message" command really means "add new task."
1419 if (WC->CurRoom.defview == VIEW_TASKS) {
1420 display_edit_task();
1425 ReplyingModeStr = bstr("replying_mode");
1426 if (ReplyingModeStr != NULL) {
1427 for (i = 0; i < 3; i++) {
1428 if (strcmp(ReplyingModeStr, ReplyToModeStrings[i]) == 0) {
1429 ReplyMode = (eReplyToNodes) i;
1436 * If the "replying_to" variable is set, it refers to a message
1437 * number from which we must extract some header fields...
1439 replying_to = lbstr("replying_to");
1440 if (replying_to > 0) {
1442 StrBuf *wefw = NULL;
1443 StrBuf *msgn = NULL;
1444 StrBuf *from = NULL;
1445 StrBuf *rfca = NULL;
1446 StrBuf *rcpt = NULL;
1447 StrBuf *cccc = NULL;
1448 StrBuf *replyto = NULL;
1449 StrBuf *nvto = NULL;
1450 int message_originated_locally = 0;
1451 serv_printf("MSG0 %ld|1", replying_to);
1453 StrBuf_ServGetln(Line);
1454 if (GetServerStatusMsg(Line, NULL, 0, 0) == 1)
1455 while (len = StrBuf_ServGetln(Line), (len >= 0) && ((len != 3) || strcmp(ChrPtr(Line), "000"))) {
1456 eMessageField which;
1457 if ((StrLength(Line) > 4) && (ChrPtr(Line)[4] == '=') && GetFieldFromMnemonic(&which, ChrPtr(Line))) switch (which) {
1459 StrBuf *subj = NewStrBuf();
1460 StrBuf *FlatSubject;
1462 if (ReplyMode == eForward) {
1463 if (strncasecmp(ChrPtr(Line) + 5, "Fw:", 3)) {
1464 StrBufAppendBufPlain(subj, HKEY("Fw: "), 0);
1468 if (strncasecmp(ChrPtr(Line) + 5, "Re:", 3)) {
1469 StrBufAppendBufPlain(subj, HKEY("Re: "), 0);
1472 StrBufAppendBufPlain(subj,
1474 StrLength(Line) - 5, 0);
1475 FlatSubject = NewStrBufPlain(NULL, StrLength(subj));
1476 StrBuf_RFC822_to_Utf8(FlatSubject, subj, NULL, NULL);
1478 PutBstr(HKEY("subject"), FlatSubject);
1483 message_originated_locally = 1;
1492 wefw = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1494 /* Trim down excessively long lists of thread references. We eliminate the
1495 * second one in the list so that the thread root remains intact.
1497 rrtok = num_tokens(ChrPtr(wefw), '|');
1498 rrlen = StrLength(wefw);
1499 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
1500 StrBufRemove_token(wefw, 1, '|');
1506 msgn = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1511 from = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1512 FlatFrom = NewStrBufPlain(NULL, StrLength(from));
1513 StrBuf_RFC822_to_Utf8(FlatFrom, from, NULL, NULL);
1516 for (i=0; i<StrLength(from); ++i) {
1517 if (ChrPtr(from)[i] == ',')
1518 StrBufPeek(from, NULL, i, ' ');
1523 rcpt = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1526 cccc = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1529 replyto = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1533 rfca = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1534 FlatRFCA = NewStrBufPlain(NULL, StrLength(rfca));
1535 StrBuf_RFC822_to_Utf8(FlatRFCA, rfca, NULL, NULL);
1541 nvto = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1542 putbstr("nvto", nvto);
1562 if (StrLength(wefw) + StrLength(msgn) > 0) {
1563 StrBuf *refs = NewStrBuf();
1564 if (StrLength(wefw) > 0) {
1565 StrBufAppendBuf(refs, wefw, 0);
1567 if ( (StrLength(wefw) > 0) &&
1568 (StrLength(msgn) > 0) )
1570 StrBufAppendBufPlain(refs, HKEY("|"), 0);
1572 if (StrLength(msgn) > 0) {
1573 StrBufAppendBuf(refs, msgn, 0);
1575 PutBstr(HKEY("references"), refs);
1579 * If this is a Reply or a ReplyAll, copy the sender's email into the To: field
1581 if ((ReplyMode == eReply) || (ReplyMode == eReplyAll)) {
1584 if ((StrLength(replyto) > 0) && (ReplyMode == eReplyAll)) {
1585 to_rcpt = NewStrBuf();
1586 StrBufAppendBuf(to_rcpt, replyto, 0);
1589 else if ( (StrLength(rfca) > 0) && (message_originated_locally == 0) ) {
1590 to_rcpt = NewStrBuf();
1591 StrBufAppendBuf(to_rcpt, from, 0);
1592 StrBufAppendBufPlain(to_rcpt, HKEY(" <"), 0);
1593 StrBufAppendBuf(to_rcpt, rfca, 0);
1594 StrBufAppendBufPlain(to_rcpt, HKEY(">"), 0);
1601 PutBstr(HKEY("recp"), to_rcpt);
1605 * Only if this is a ReplyAll, copy all recipients into the Cc: field
1607 if (ReplyMode == eReplyAll) {
1608 StrBuf *cc_rcpt = rcpt;
1610 if ((StrLength(cccc) > 0) && (StrLength(replyto) == 0)) {
1611 if (cc_rcpt != NULL) {
1612 StrBufAppendPrintf(cc_rcpt, ", ");
1613 StrBufAppendBuf(cc_rcpt, cccc, 0);
1620 if (cc_rcpt != NULL) {
1621 PutBstr(HKEY("cc"), cc_rcpt);
1625 // We might not need these logs anymore.
1626 syslog(LOG_DEBUG, "wefw = %s", ChrPtr(wefw));
1627 syslog(LOG_DEBUG, "msgn = %s", ChrPtr(msgn));
1628 syslog(LOG_DEBUG, "from = %s", ChrPtr(from));
1629 syslog(LOG_DEBUG, "rfca = %s", ChrPtr(rfca));
1630 syslog(LOG_DEBUG, "rcpt = %s", ChrPtr(rcpt));
1631 syslog(LOG_DEBUG, "cccc = %s", ChrPtr(cccc));
1632 syslog(LOG_DEBUG, "replyto = %s", ChrPtr(replyto));
1633 syslog(LOG_DEBUG, "nvto = %s", ChrPtr(nvto));
1634 syslog(LOG_DEBUG, "local = %d" , message_originated_locally);
1646 * Otherwise proceed normally.
1647 * Do a custom room banner with no navbar...
1649 if (recipient_required) {
1650 const StrBuf *Recp = NULL;
1651 const StrBuf *Cc = NULL;
1652 const StrBuf *Bcc = NULL;
1653 StrBuf *wikipage = NULL;
1654 StrBuf *CmdBuf = NULL;
1655 const char CMD[] = "ENT0 0|%s|%d|0||%s||%s|%s|%s";
1657 Recp = sbstr("recp");
1660 wikipage = NewStrBufDup(sbstr("page"));
1661 str_wiki_index(wikipage);
1663 CmdBuf = NewStrBufPlain(NULL,
1666 StrLength(display_name) +
1669 StrLength(wikipage));
1671 StrBufPrintf(CmdBuf,
1675 ChrPtr(display_name),
1680 serv_puts(ChrPtr(CmdBuf));
1681 StrBuf_ServGetln(CmdBuf);
1684 rc = GetServerStatusMsg(CmdBuf, &Result, 0, 0);
1686 if ( (Result == 570) /* invalid or missing recipient(s) */
1687 || (Result == 550) /* higher access required to send Internet mail */
1689 /* These errors will have been displayed and are excusable */
1691 else if (rc != 2) { /* Any other error means that we cannot continue */
1692 AppendImportantMessage(ChrPtr(CmdBuf) + 4, StrLength(CmdBuf) - 4);
1693 FreeStrBuf(&CmdBuf);
1695 readloop(readnew, eUseDefault);
1698 FreeStrBuf(&CmdBuf);
1700 if (recipient_required)
1701 PutBstr(HKEY("__RCPTREQUIRED"), NewStrBufPlain(HKEY("1")));
1702 if (recipient_required || subject_required)
1703 PutBstr(HKEY("__SUBJREQUIRED"), NewStrBufPlain(HKEY("1")));
1706 output_headers(1, 0, 0, 0, 1, 0);
1707 DoTemplate(HKEY("edit_message"), NULL, &NoCtx);
1717 void delete_msg(void) {
1721 msgid = lbstr("msgid");
1723 if ((WC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
1724 serv_printf("DELE %ld", msgid);
1726 else { /* Otherwise move it to Trash */
1727 serv_printf("MOVE %ld|_TRASH_|0", msgid);
1730 StrBuf_ServGetln(Line);
1731 GetServerStatusMsg(Line, NULL, 1, 0);
1735 readloop(readnew, eUseDefault);
1740 * move a message to another room
1742 void move_msg(void) {
1745 msgid = lbstr("msgid");
1747 if (havebstr("move_button")) {
1749 serv_printf("MOVE %ld|%s", msgid, bstr("target_room"));
1751 StrBuf_ServGetln(Line);
1752 GetServerStatusMsg(Line, NULL, 1, 0);
1755 AppendImportantMessage(_("The message was not moved."), -1);
1759 readloop(readnew, eUseDefault);
1764 * Generic function to output an arbitrary MIME attachment from
1765 * message being composed
1767 * partnum The MIME part to be output
1768 * filename Fake filename to give
1769 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1771 void postpart(StrBuf *partnum, StrBuf *filename, int force_download) {
1773 StrBuf *content_type;
1774 wc_mime_attachment *part;
1777 i = StrToi(partnum);
1778 if (GetHash(WC->attachments, IKEY(i), &vPart) &&
1780 part = (wc_mime_attachment*) vPart;
1781 if (force_download) {
1782 content_type = NewStrBufPlain(HKEY("application/octet-stream"));
1785 content_type = NewStrBufDup(part->ContentType);
1787 StrBufAppendBuf(WC->WBuf, part->Data, 0);
1788 http_transmit_thing(ChrPtr(content_type), 0);
1790 hprintf("HTTP/1.1 404 %s\n", ChrPtr(partnum));
1791 output_headers(0, 0, 0, 0, 0, 0);
1792 hprintf("Content-Type: text/plain\r\n");
1794 wc_printf(_("An error occurred while retrieving this part: %s/%s\n"),
1795 ChrPtr(partnum), ChrPtr(filename));
1798 FreeStrBuf(&content_type);
1802 // Generic function to output an arbitrary MIME part from an arbitrary
1803 // message number on the server.
1805 // msgnum message number on the citadel server
1806 // partnum MIME part number to be output
1807 // force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1809 void view_or_download_mimepart(int force_download) {
1813 StrBuf *ContentType;
1816 PartNum = NewStrBuf();
1818 msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
1819 StrBufExtract_token(PartNum, WC->Hdr->HR.ReqLine, 1, '/');
1822 serv_printf("DLAT %ld|%s", msgnum, ChrPtr(PartNum)); // DLAT will return: 6XX length|-1|filename|cbtype|cbcharset
1823 StrBuf_ServGetln(Buf);
1824 FreeStrBuf(&PartNum);
1826 if (GetServerStatus(Buf, NULL) != 6) {
1828 hprintf("HTTP/1.1 500 error\r\n"
1829 "Pragma: no-cache\r\n"
1830 "Cache-Control: no-store\r\n"
1832 "Content-Type: text/plain\r\n"
1835 wc_printf(_("An error occurred while retrieving this part: %s\n"), "--");
1839 StrBufCutLeft(Buf, 4);
1840 bytes = StrBufExtract_long(Buf, 0, '|');
1842 syslog(LOG_DEBUG, "\033[31mview or download %ld bytes\033[0m", bytes);
1845 if (force_download) {
1846 ContentType = NewStrBufPlain(HKEY("application/octet-stream"));
1849 ContentType = NewStrBuf();
1850 StrBufExtract_token(ContentType, Buf, 3, '|');
1855 StrBuf_ServGetBLOBBuffered(Buf, bytes);
1859 http_transmit_thing(ChrPtr(ContentType), 0);
1860 FreeStrBuf(&ContentType);
1865 * Read any MIME part of a message, from the server, into memory.
1867 StrBuf *load_mimepart(long msgnum, char *partnum) {
1872 serv_printf("DLAT %ld|%s", msgnum, partnum);
1873 StrBuf_ServGetln(Buf);
1874 if (GetServerStatus(Buf, NULL) == 6) {
1875 StrBufCutLeft(Buf, 4);
1876 bytes = StrBufExtract_long(Buf, 0, '|');
1879 StrBuf_ServGetBLOBBuffered(Buf, bytes);
1890 * Read any MIME part of a message, from the server, into memory.
1892 void MimeLoadData(wc_mime_attachment *Mime) {
1897 serv_printf("DLAT %ld|%s", Mime->msgnum, ChrPtr(Mime->PartNum));
1899 StrBuf_ServGetln(Buf);
1900 if (GetServerStatus(Buf, NULL) == 6) {
1901 Ptr = &(ChrPtr(Buf)[4]);
1902 bytes = StrBufExtractNext_long(Buf, &Ptr, '|');
1903 StrBufSkip_NTokenS(Buf, &Ptr, '|', 3); /* filename, cbtype, mimetype */
1904 if (Mime->Charset == NULL) Mime->Charset = NewStrBuf();
1905 StrBufExtract_NextToken(Mime->Charset, Buf, &Ptr, '|');
1907 if (Mime->Data == NULL) {
1908 Mime->Data = NewStrBufPlain(NULL, bytes);
1910 StrBuf_ServGetBLOBBuffered(Mime->Data, bytes);
1913 FlushStrBuf(Mime->Data);
1919 void view_mimepart(void) {
1920 view_or_download_mimepart(0);
1924 void download_mimepart(void) {
1925 view_or_download_mimepart(1);
1929 void view_postpart(void) {
1930 StrBuf *filename = NewStrBuf();
1931 StrBuf *partnum = NewStrBuf();
1933 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1934 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1936 postpart(partnum, filename, 0);
1938 FreeStrBuf(&filename);
1939 FreeStrBuf(&partnum);
1943 void download_postpart(void) {
1944 StrBuf *filename = NewStrBuf();
1945 StrBuf *partnum = NewStrBuf();
1947 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1948 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1950 postpart(partnum, filename, 1);
1952 FreeStrBuf(&filename);
1953 FreeStrBuf(&partnum);
1957 void show_num_attachments(void) {
1958 wc_printf("%d", GetCount(WC->attachments));
1962 void h_readnew(void) { readloop(readnew, eUseDefault);}
1963 void h_readold(void) { readloop(readold, eUseDefault);}
1964 void h_readfwd(void) { readloop(readfwd, eUseDefault);}
1965 void h_headers(void) { readloop(headers, eUseDefault);}
1966 void h_do_search(void) { readloop(do_search, eUseDefault);}
1967 void h_readgt(void) { readloop(readgt, eUseDefault);}
1968 void h_readlt(void) { readloop(readlt, eUseDefault);}
1971 /* Output message list in JSON format */
1972 void jsonMessageList(void) {
1973 StrBuf *View = NewStrBuf();
1974 const StrBuf *room = sbstr("room");
1975 long oper = (havebstr("query")) ? do_search : readnew;
1976 StrBufPrintf(View, "%d", VIEW_JSON_LIST);
1977 putbstr("view", View);;
1979 readloop(oper, eUseDefault);
1983 void FreeReadLoopHandlerSet(void *v) {
1984 RoomRenderer *Handler = (RoomRenderer *) v;
1985 FreeStrBuf(&Handler->FetchMessageList);
1986 if (Handler->MessageFieldList != NULL) {
1987 free(Handler->MessageFieldList);
1993 void RegisterReadLoopHandlerset(
1995 GetParamsGetServerCall_func GetParamsGetServerCall,
1996 PrintViewHeader_func PrintPageHeader,
1997 PrintViewHeader_func PrintViewHeader,
1998 load_msg_ptrs_detailheaders LH,
1999 LoadMsgFromServer_func LoadMsgFromServer,
2000 RenderView_or_Tail_func RenderView_or_Tail,
2001 View_Cleanup_func ViewCleanup,
2002 const char **browseListFields
2007 RoomRenderer *Handler;
2009 Handler = (RoomRenderer*) malloc(sizeof(RoomRenderer));
2011 Handler->RoomType = RoomType;
2012 Handler->GetParamsGetServerCall = GetParamsGetServerCall;
2013 Handler->PrintPageHeader = PrintPageHeader;
2014 Handler->PrintViewHeader = PrintViewHeader;
2015 Handler->LoadMsgFromServer = LoadMsgFromServer;
2016 Handler->RenderView_or_Tail = RenderView_or_Tail;
2017 Handler->ViewCleanup = ViewCleanup;
2018 Handler->LHParse = LH;
2020 if (browseListFields != NULL) {
2021 while (browseListFields[count] != NULL) {
2024 Handler->HeaderCount = count;
2025 Handler->MessageFieldList = (eMessageField*) malloc(sizeof(eMessageField) * count);
2026 Handler->FetchMessageList = NewStrBufPlain(NULL, 5 * count + 4 + 5);
2027 StrBufPlain(Handler->FetchMessageList, HKEY("time\n"));
2028 for (i = 0; i < count; i++) {
2029 if (!GetFieldFromMnemonic(&Handler->MessageFieldList[i], browseListFields[i])) {
2030 fprintf(stderr, "Unknown message header: %s\n", browseListFields[i]);
2033 StrBufAppendBufPlain(Handler->FetchMessageList, browseListFields[i], 4, 0);
2034 StrBufAppendBufPlain(Handler->FetchMessageList, HKEY("\n"), 0);
2036 StrBufAppendBufPlain(Handler->FetchMessageList, HKEY("000"), 0);
2039 Handler->FetchMessageList = NULL;
2040 Handler->MessageFieldList = NULL;
2043 Put(ReadLoopHandler, IKEY(RoomType), Handler, FreeReadLoopHandlerSet);
2050 RegisterPreference("use_sig",
2051 _("Attach signature to email messages?"),
2054 RegisterPreference("signature", _("Use this signature:"), PRF_QP_STRING, NULL);
2055 RegisterPreference("default_header_charset",
2056 _("Default character set for email headers:"),
2059 RegisterPreference("defaultfrom", _("Preferred email address"), PRF_STRING, NULL);
2060 RegisterPreference("defaultname",
2061 _("Preferred display name for email messages"),
2064 RegisterPreference("defaulthandle",
2065 _("Preferred display name for bulletin board posts"),
2068 RegisterPreference("mailbox",_("Mailbox view mode"), PRF_STRING, NULL);
2070 WebcitAddUrlHandler(HKEY("readnew"), "", 0, h_readnew, ANONYMOUS|NEED_URL);
2071 WebcitAddUrlHandler(HKEY("readold"), "", 0, h_readold, ANONYMOUS|NEED_URL);
2072 WebcitAddUrlHandler(HKEY("readfwd"), "", 0, h_readfwd, ANONYMOUS|NEED_URL);
2073 WebcitAddUrlHandler(HKEY("headers"), "", 0, h_headers, NEED_URL);
2074 WebcitAddUrlHandler(HKEY("readgt"), "", 0, h_readgt, ANONYMOUS|NEED_URL);
2075 WebcitAddUrlHandler(HKEY("readlt"), "", 0, h_readlt, ANONYMOUS|NEED_URL);
2076 WebcitAddUrlHandler(HKEY("do_search"), "", 0, h_do_search, 0);
2077 WebcitAddUrlHandler(HKEY("display_enter"), "", 0, display_enter, 0);
2078 WebcitAddUrlHandler(HKEY("post"), "", 0, post_message, PROHIBIT_STARTPAGE);
2079 WebcitAddUrlHandler(HKEY("move_msg"), "", 0, move_msg, PROHIBIT_STARTPAGE);
2080 WebcitAddUrlHandler(HKEY("delete_msg"), "", 0, delete_msg, PROHIBIT_STARTPAGE);
2081 WebcitAddUrlHandler(HKEY("msg"), "", 0, embed_message, NEED_URL);
2082 WebcitAddUrlHandler(HKEY("message"), "", 0, handle_one_message, NEED_URL|XHTTP_COMMANDS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
2083 WebcitAddUrlHandler(HKEY("printmsg"), "", 0, print_message, NEED_URL);
2084 WebcitAddUrlHandler(HKEY("msgheaders"), "", 0, display_headers, NEED_URL);
2086 WebcitAddUrlHandler(HKEY("mimepart"), "", 0, view_mimepart, NEED_URL);
2087 WebcitAddUrlHandler(HKEY("mimepart_download"), "", 0, download_mimepart, NEED_URL);
2088 WebcitAddUrlHandler(HKEY("postpart"), "", 0, view_postpart, NEED_URL|PROHIBIT_STARTPAGE);
2089 WebcitAddUrlHandler(HKEY("postpart_download"), "", 0, download_postpart, NEED_URL|PROHIBIT_STARTPAGE);
2090 WebcitAddUrlHandler(HKEY("upload_attachment"), "", 0, upload_attachment, AJAX);
2091 WebcitAddUrlHandler(HKEY("remove_attachment"), "", 0, remove_attachment, AJAX);
2092 WebcitAddUrlHandler(HKEY("show_num_attachments"), "", 0, show_num_attachments, AJAX);
2095 WebcitAddUrlHandler(HKEY("roommsgs"), "", 0, jsonMessageList,0);
2099 SessionDetachModule_MSG
2102 DeleteHash(&sess->summ);