2 * Functions which deal with the fetching and displaying of messages.
4 * Copyright (c) 1996-2020 by the citadel.org team
6 * This program is open source software. We call it open source, not
7 * free software, because Richard Stallman is a communist and an asshole.
9 * The program is licensed under the General Public License, version 3.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #include "webserver.h"
22 HashList *MimeRenderHandler = NULL;
23 HashList *ReadLoopHandler = NULL;
24 int dbg_analyze_msg = 0;
26 #define SUBJ_COL_WIDTH_PCT 50 /* Mailbox view column width */
27 #define SENDER_COL_WIDTH_PCT 30 /* Mailbox view column width */
28 #define DATE_PLUS_BUTTONS_WIDTH_PCT 20 /* Mailbox view column width */
31 /* workaround for json listview; its not useable directly */
32 if (WC->CurRoom.view == VIEW_JSON_LIST) {
33 StrBuf *View = NewStrBuf();
34 StrBufPrintf(View, "%d", VIEW_MAILBOX);
35 putbstr("view", View);;
40 int load_message(message_summary *Msg, StrBuf *FoundCharset, StrBuf **Error) {
49 if (Msg->PartNum != NULL) {
50 serv_printf("MSG4 %ld|%s", Msg->msgnum, ChrPtr(Msg->PartNum));
53 serv_printf("MSG4 %ld", Msg->msgnum);
56 StrBuf_ServGetln(Buf);
57 if (GetServerStatus(Buf, NULL) != 1) {
59 StrBufAppendPrintf(*Error, "<strong>");
60 StrBufAppendPrintf(*Error, _("ERROR:"));
61 StrBufAppendPrintf(*Error, "</strong> %s<br>\n", &buf[4]);
66 /* begin everythingamundo table */
67 HdrToken = NewStrBuf();
68 while (!Done && StrBuf_ServGetln(Buf)>=0) {
69 if ( (StrLength(Buf)==3) &&
70 !strcmp(ChrPtr(Buf), "000"))
74 if (Msg->MsgBody->Data == NULL)
75 Msg->MsgBody->Data = NewStrBuf();
76 Msg->MsgBody->ContentType = NewStrBufPlain(HKEY("text/html"));
77 StrBufAppendPrintf(Msg->MsgBody->Data, "<div><i>");
78 StrBufAppendPrintf(Msg->MsgBody->Data, _("Empty message"));
79 StrBufAppendPrintf(Msg->MsgBody->Data, "</i><br><br>\n");
80 StrBufAppendPrintf(Msg->MsgBody->Data, "</div>\n");
85 case 0:/* Citadel Message Headers */
86 if (StrLength(Buf) == 0) {
90 StrBufExtract_token(HdrToken, Buf, 0, '=');
91 StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
93 /* look up one of the examine_* functions to parse the content */
94 rc = EvaluateMsgHdr(SKEY(HdrToken), Msg, Buf, FoundCharset);
101 "don't know how to handle message header[%s]\n",
105 case 1:/* Message Mime Header */
106 if (StrLength(Buf) == 0) {
108 if (Msg->MsgBody->ContentType == NULL)
109 /* end of header or no header? */
110 Msg->MsgBody->ContentType = NewStrBufPlain(HKEY("text/plain"));
111 /* usual end of mime header */
115 StrBufExtract_token(HdrToken, Buf, 0, ':');
116 if (StrLength(HdrToken) > 0) {
117 StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
118 /* the examine*'s know how to do with mime headers too... */
119 EvaluateMsgHdr(SKEY(HdrToken), Msg, Buf, FoundCharset);
124 case 2: /* Message Body */
126 if (Msg->MsgBody->size_known > 0) {
127 StrBuf_ServGetBLOBBuffered(Msg->MsgBody->Data, Msg->MsgBody->length);
129 /*/ todo: check next line, if not 000, append following lines */
132 if (StrLength(Msg->MsgBody->Data) > 0)
133 StrBufAppendBufPlain(Msg->MsgBody->Data, "\n", 1, 0);
134 StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
138 StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
143 if (Msg->AllAttach == NULL)
144 Msg->AllAttach = NewHash(1,NULL);
145 /* now we put the body mimepart we read above into the mimelist */
146 Put(Msg->AllAttach, SKEY(Msg->MsgBody->PartNum), Msg->MsgBody, DestroyMime);
149 FreeStrBuf(&HdrToken);
155 * Display a message to the user
157 * msgnum Message number to display
158 * printable_view Nonzero to display a printable view
159 * section Optional for encapsulated message/rfc822 submessage
161 int read_message(StrBuf *Target, const char *tmpl, long tmpllen, long msgnum, const StrBuf *PartNum, const StrBuf **OutMime, WCTemplputParams *TP) {
163 StrBuf *FoundCharset;
166 message_summary *Msg = NULL;
170 WCTemplputParams SuperTP;
171 WCTemplputParams SubTP;
172 StrBuf *Error = NULL;
174 memset(&SuperTP, 0, sizeof(WCTemplputParams));
175 memset(&SubTP, 0, sizeof(WCTemplputParams));
178 FoundCharset = NewStrBuf();
179 Msg = (message_summary *)malloc(sizeof(message_summary));
180 memset(Msg, 0, sizeof(message_summary));
181 Msg->msgnum = msgnum;
182 Msg->PartNum = PartNum;
183 Msg->MsgBody = (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
184 memset(Msg->MsgBody, 0, sizeof(wc_mime_attachment));
185 Msg->MsgBody->msgnum = msgnum;
187 if (!load_message(Msg, FoundCharset, &Error)) {
188 StrBufAppendBuf(Target, Error, 0);
192 /* Extract just the content-type (omit attributes such as "charset") */
193 StrBufExtract_token(Buf, Msg->MsgBody->ContentType, 0, ';');
195 StrBufLowerCase(Buf);
197 StackContext(TP, &SuperTP, Msg, CTX_MAILSUM, 0, NULL);
199 /* Locate a renderer capable of converting this MIME part into HTML */
200 if (GetHash(MimeRenderHandler, SKEY(Buf), &vHdr) &&
202 RenderMimeFuncStruct *Render;
204 StackContext(&SuperTP, &SubTP, Msg->MsgBody, CTX_MIME_ATACH, 0, NULL);
206 Render = (RenderMimeFuncStruct*)vHdr;
207 Render->f(Target, &SubTP, FoundCharset);
209 UnStackContext(&SubTP);
212 if (StrLength(Msg->reply_references)> 0) {
213 /* Trim down excessively long lists of thread references. We eliminate the
214 * second one in the list so that the thread root remains intact.
216 int rrtok = num_tokens(ChrPtr(Msg->reply_references), '|');
217 int rrlen = StrLength(Msg->reply_references);
218 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
219 StrBufRemove_token(Msg->reply_references, 1, '|');
223 /* now check if we need to translate some mimeparts, and remove the duplicate */
224 it = GetNewHashPos(Msg->AllAttach, 0);
225 while (GetNextHashPos(Msg->AllAttach, it, &len, &Key, &vMime) &&
227 StackContext(&SuperTP, &SubTP, vMime, CTX_MIME_ATACH, 0, NULL);
229 evaluate_mime_part(Target, &SubTP);
231 UnStackContext(&SubTP);
234 *OutMime = DoTemplate(tmpl, tmpllen, Target, &SuperTP);
236 UnStackContext(&SuperTP);
238 DestroyMessageSummary(Msg);
239 FreeStrBuf(&FoundCharset);
245 long HttpStatus(long CitadelStatus) {
246 long httpstatus = 502;
248 switch (MAJORCODE(CitadelStatus))
250 case LISTING_FOLLOWS:
255 switch (MINORCODE(CitadelStatus))
263 case HIGHER_ACCESS_REQUIRED:
264 case MAX_SESSIONS_EXCEEDED:
266 case RESOURCE_NOT_OPEN:
268 case INVALID_FLOOR_OPERATION:
274 case MESSAGE_NOT_FOUND:
284 case CMD_NOT_SUPPORTED:
285 case PASSWORD_REQUIRED:
286 case ALREADY_LOGGED_IN:
287 case USERNAME_REQUIRED:
289 case SERVER_SHUTTING_DOWN:
300 case START_CHAT_MODE:
304 httpstatus = 502; /* aeh... whut? */
313 * Unadorned HTML output of an individual message, suitable
314 * for placing in a hidden iframe, for printing, or whatever
316 void handle_one_message(void) {
317 long CitStatus = ERROR + NOT_HERE;
319 const StrBuf *Destination;
325 StrBuf *CmdBuf = NULL;
328 pMsg = strchr(ChrPtr(WCC->Hdr->HR.ReqLine), '/');
330 HttpStatus(CitStatus);
334 msgnum = atol(pMsg + 1);
335 StrBufCutAt(WCC->Hdr->HR.ReqLine, 0, pMsg);
336 gotoroom(WCC->Hdr->HR.ReqLine);
337 switch (WCC->Hdr->HR.eReqType)
341 Tmpl = sbstr("template");
342 if (StrLength(Tmpl) > 0)
343 read_message(WCC->WBuf, SKEY(Tmpl), msgnum, NULL, &Mime, NULL);
345 read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime, NULL);
346 http_transmit_thing(ChrPtr(Mime), 0);
349 CmdBuf = NewStrBuf ();
350 if ((WCC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
351 serv_printf("DELE %ld", msgnum);
353 else { /* Otherwise move it to Trash */
354 serv_printf("MOVE %ld|_TRASH_|0", msgnum);
356 StrBuf_ServGetln(CmdBuf);
357 GetServerStatusMsg(CmdBuf, &CitStatus, 1, 0);
358 HttpStatus(CitStatus);
363 if (GetHash(WCC->Hdr->HTTPHeaders, HKEY("DESTINATION"), &vLine) &&
365 Destination = (StrBuf*) vLine;
366 serv_printf("MOVE %ld|%s|%d", msgnum, ChrPtr(Destination), CopyMessage);
367 StrBuf_ServGetln(CmdBuf);
368 GetServerStatusMsg(CmdBuf, NULL, 1, 0);
369 GetServerStatus(CmdBuf, &CitStatus);
370 HttpStatus(CitStatus);
383 * Unadorned HTML output of an individual message, suitable
384 * for placing in a hidden iframe, for printing, or whatever
386 void embed_message(void) {
391 StrBuf *CmdBuf = NULL;
393 msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
394 if (msgnum <= 0) return;
396 switch (WCC->Hdr->HR.eReqType)
400 Tmpl = sbstr("template");
401 if (StrLength(Tmpl) > 0)
402 read_message(WCC->WBuf, SKEY(Tmpl), msgnum, NULL, &Mime, NULL);
404 read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime, NULL);
405 http_transmit_thing(ChrPtr(Mime), 0);
408 CmdBuf = NewStrBuf ();
409 if ((WCC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
410 serv_printf("DELE %ld", msgnum);
412 else { /* Otherwise move it to Trash */
413 serv_printf("MOVE %ld|_TRASH_|0", msgnum);
415 StrBuf_ServGetln(CmdBuf);
416 GetServerStatusMsg(CmdBuf, NULL, 1, 0);
426 * Printable view of a message
428 void print_message(void) {
432 msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
433 output_headers(0, 0, 0, 0, 0, 0);
435 hprintf("Content-type: text/html\r\n"
436 "Server: " PACKAGE_STRING "\r\n"
437 "Connection: close\r\n");
441 read_message(WC->WBuf, HKEY("view_message_print"), msgnum, NULL, &Mime, NULL);
448 * Display a message's headers
450 void display_headers(void) {
454 msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
455 output_headers(0, 0, 0, 0, 0, 0);
457 hprintf("Content-type: text/plain\r\n"
459 "Connection: close\r\n",
463 serv_printf("MSG2 %ld|1", msgnum);
464 serv_getln(buf, sizeof buf);
466 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
467 wc_printf("%s\n", buf);
476 * load message pointers from the server for a "read messages" operation
478 * servcmd: the citadel command to send to the citserver
480 int load_msg_ptrs(const char *servcmd,
482 StrBuf *FoundCharset,
483 SharedMessageStatus *Stat,
485 load_msg_ptrs_detailheaders LH,
486 StrBuf *FetchMessageList,
487 eMessageField *MessageFieldList,
491 message_summary *Msg;
496 const char *Ptr = NULL;
499 Stat->lowest_found = LONG_MAX;
500 Stat->highest_found = LONG_MIN;
502 if (WCC->summ != NULL) {
503 DeleteHash(&WCC->summ);
505 WCC->summ = NewHash(1, Flathash);
509 StrBuf_ServGetln(Buf);
510 StatMajor = GetServerStatus(Buf, NULL);
515 if (filter != NULL) {
520 else if (FetchMessageList != NULL) {
521 serv_putbuf(FetchMessageList);
524 /* fall back to empty filter in case of we were fooled... */
530 return (Stat->nummsgs);
533 while (len = StrBuf_ServGetln(Buf),
536 strcmp(ChrPtr(Buf), "000")!= 0)))
538 if (Stat->nummsgs < Stat->maxload) {
541 Msg = (message_summary*)malloc(sizeof(message_summary));
542 memset(Msg, 0, sizeof(message_summary));
544 Msg->msgnum = StrBufExtractNext_long(Buf, &Ptr, '|');
545 Msg->date = StrBufExtractNext_long(Buf, &Ptr, '|');
546 if (MessageFieldList != NULL) {
548 for (i = 0; i < HeaderCount; i++) {
549 StrBufExtract_NextToken(Buf2, Buf, &Ptr, '|');
550 if (StrLength(Buf2) > 0) {
551 EvaluateMsgHdrEnum(MessageFieldList[i], Msg, Buf2, FoundCharset);
556 if (Stat->nummsgs == 0) {
557 if (Msg->msgnum < Stat->lowest_found) {
558 Stat->lowest_found = Msg->msgnum;
560 if (Msg->msgnum > Stat->highest_found) {
561 Stat->highest_found = Msg->msgnum;
565 if ((Msg->msgnum == 0) && (StrLength(Buf) < 32)) {
571 * as citserver probably gives us messages in forward date sorting
572 * nummsgs should be the same order as the message date.
574 if (Msg->date == 0) {
575 Msg->date = Stat->nummsgs;
576 if (StrLength(Buf) < 32)
579 if ((!skipit) && (LH != NULL)) {
580 if (!LH(Buf, &Ptr, Msg, Buf2, ViewSpecific)){
586 Put(WCC->summ, (const char *)&n, sizeof(n), Msg, DestroyMessageSummary);
592 return (Stat->nummsgs);
597 * sets FlagToSet for each of ScanMe that appears in MatchMSet
598 * ScanMe: List of BasicMsgStruct to be searched it MatchSet
599 * MatchMSet: MSet we want to flag
600 * FlagToSet: Flag to set on each BasicMsgStruct->Flags if in MSet
602 long SetFlagsFromMSet(HashList *ScanMe, MSet *MatchMSet, int FlagToSet, int Reverse) {
608 message_summary *Msg;
610 at = GetNewHashPos(ScanMe, 0);
611 while (GetNextHashPos(ScanMe, at, &HKLen, &HashKey, &vMsg)) {
612 /* Are you a new message, or an old message? */
613 Msg = (message_summary*) vMsg;
614 if (Reverse && IsInMSetList(MatchMSet, Msg->msgnum)) {
615 Msg->Flags = Msg->Flags | FlagToSet;
618 else if (!Reverse && !IsInMSetList(MatchMSet, Msg->msgnum)) {
619 Msg->Flags = Msg->Flags | FlagToSet;
628 long load_seen_flags(void) {
634 OldMsg = NewStrBuf();
636 StrBuf_ServGetln(OldMsg);
637 if (GetServerStatus(OldMsg, NULL) == 2) {
638 StrBufCutLeft(OldMsg, 4);
645 if (ParseMSet(&MatchMSet, OldMsg))
647 count = SetFlagsFromMSet(WCC->summ, MatchMSet, MSGFLAG_READ, 0);
649 DeleteMSet(&MatchMSet);
654 extern readloop_struct rlid[];
656 typedef struct _RoomRenderer{
659 GetParamsGetServerCall_func GetParamsGetServerCall;
661 PrintViewHeader_func PrintPageHeader;
662 PrintViewHeader_func PrintViewHeader;
663 LoadMsgFromServer_func LoadMsgFromServer;
664 RenderView_or_Tail_func RenderView_or_Tail;
665 View_Cleanup_func ViewCleanup;
666 load_msg_ptrs_detailheaders LHParse;
668 StrBuf *FetchMessageList;
669 eMessageField *MessageFieldList;
674 * command loop for reading messages
676 * Set oper to "readnew" or "readold" or "readfwd" or "headers" or "readgt" or "readlt" or "do_search"
678 void readloop(long oper, eCustomRoomRenderer ForceRenderer) {
679 RoomRenderer *ViewMsg;
682 message_summary *Msg;
684 char filter[256] = "";
690 WCTemplputParams SubTP;
691 SharedMessageStatus Stat;
692 void *ViewSpecific = NULL;
694 if (havebstr("is_summary") && (1 == (ibstr("is_summary")))) {
695 WCC->CurRoom.view = VIEW_MAILBOX;
698 if (havebstr("view")) {
699 WCC->CurRoom.view = ibstr("view");
702 memset(&Stat, 0, sizeof(SharedMessageStatus));
703 Stat.maxload = 10000;
704 Stat.lowest_found = (-1);
705 Stat.highest_found = (-1);
706 if (ForceRenderer == eUseDefault)
707 GetHash(ReadLoopHandler, IKEY(WCC->CurRoom.view), &vViewMsg);
709 GetHash(ReadLoopHandler, IKEY(ForceRenderer), &vViewMsg);
710 if (vViewMsg == NULL) {
711 WCC->CurRoom.view = VIEW_BBS;
712 GetHash(ReadLoopHandler, IKEY(WCC->CurRoom.view), &vViewMsg);
714 if (vViewMsg == NULL) {
715 return; /* TODO: print message */
718 ViewMsg = (RoomRenderer*) vViewMsg;
719 if (ViewMsg->PrintPageHeader == NULL)
720 output_headers(1, 1, 1, 0, 0, 0);
722 ViewMsg->PrintPageHeader(&Stat, ViewSpecific);
724 if (ViewMsg->GetParamsGetServerCall != NULL) {
725 r = ViewMsg->GetParamsGetServerCall(
730 filter, sizeof(filter)
742 case 300: /* the callback hook should do the work for us here, since he knows what to do. */
748 if (!IsEmptyStr(cmd)) {
749 const char *p = NULL;
750 StrBuf *FoundCharset = NULL;
751 if (!IsEmptyStr(filter))
753 if (ViewMsg->HeaderCount > 0) {
754 FoundCharset = NewStrBuf();
756 Stat.nummsgs = load_msg_ptrs(cmd, p,
761 ViewMsg->FetchMessageList,
762 ViewMsg->MessageFieldList,
763 ViewMsg->HeaderCount);
764 FreeStrBuf(&FoundCharset);
769 StackContext(NULL, &SubTP, NULL, CTX_MAILSUM, 0, NULL);
771 SortIt = RetrieveSort(&SubTP,
774 Stat.defaultsortorder);
776 UnStackContext(&SubTP);
778 SortByPayload(WCC->summ, SortIt);
780 if (Stat.startmsg < 0) {
784 if (Stat.load_seen) Stat.numNewmsgs = load_seen_flags();
787 * Print any inforation above the message list...
789 if (ViewMsg->PrintViewHeader != NULL)
790 ViewMsg->PrintViewHeader(&Stat, &ViewSpecific);
792 WCC->startmsg = Stat.startmsg;
793 WCC->maxmsgs = Stat.maxmsgs;
794 WCC->num_displayed = 0;
796 /* Put some helpful data in vars for mailsummary_json */
801 StrBufPrintf(Foo, "%ld", Stat.nummsgs);
802 PutBstr(HKEY("__READLOOP:TOTALMSGS"), NewStrBufDup(Foo)); /* keep Foo! */
804 StrBufPrintf(Foo, "%ld", Stat.numNewmsgs);
805 PutBstr(HKEY("__READLOOP:NEWMSGS"), NewStrBufDup(Foo)); /* keep Foo! */
807 StrBufPrintf(Foo, "%ld", Stat.startmsg);
808 PutBstr(HKEY("__READLOOP:STARTMSG"), Foo); /* store Foo elsewhere, descope it here. */
812 * iterate over each message. if we need to load an attachment, do it here.
815 if ((ViewMsg->LoadMsgFromServer != NULL) &&
818 at = GetNewHashPos(WCC->summ, 0);
819 Stat.num_displayed = i = 0;
820 while ( GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
821 Msg = (message_summary*) vMsg;
822 if ((Msg->msgnum >= Stat.startmsg) && (Stat.num_displayed <= Stat.maxmsgs)) {
823 ViewMsg->LoadMsgFromServer(&Stat,
826 (Msg->Flags & MSGFLAG_READ) != 0,
835 * Done iterating the message list. now tasks we want to do after.
837 if (ViewMsg->RenderView_or_Tail != NULL)
838 ViewMsg->RenderView_or_Tail(&Stat, &ViewSpecific, oper);
840 if (ViewMsg->ViewCleanup != NULL)
841 ViewMsg->ViewCleanup(&ViewSpecific);
845 if (WCC->summ != NULL) {
846 DeleteHash(&WCC->summ);
852 * Back end for post_message()
853 * ... this is where the actual message gets transmitted to the server.
855 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(WCC->serv_info->serv_fqdn),
873 sprintf(alt_boundary, "Citadel--Multipart--%s--%04x--%04x",
874 ChrPtr(WCC->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 if (GetCount(WCC->attachments) > 0) {
888 /* Only do multipart/alternative for mailboxes. BBS and Wiki rooms don't need it. */
889 if ((WCC->CurRoom.view == VIEW_MAILBOX) ||
890 (WCC->CurRoom.view == VIEW_JSON_LIST))
892 include_text_alt = 1;
896 /* Remember, serv_printf() appends an extra newline */
897 serv_printf("Content-type: multipart/mixed; boundary=\"%s\"\n", top_boundary);
898 serv_printf("This is a multipart message in MIME format.\n");
899 serv_printf("--%s", top_boundary);
902 /* Remember, serv_printf() appends an extra newline */
903 if (include_text_alt) {
905 serv_printf("Content-type: multipart/alternative; "
906 "boundary=\"%s\"\n", alt_boundary);
907 serv_printf("This is a multipart message in MIME format.\n");
908 serv_printf("--%s", alt_boundary);
910 serv_puts("Content-type: text/plain; charset=utf-8");
911 serv_puts("Content-Transfer-Encoding: quoted-printable");
913 txtmail = html_to_ascii(bstr("msgtext"), 0, 80);
914 Buf = NewStrBufPlain(txtmail, -1);
917 text_to_server_qp(Buf); /* Transmit message in quoted-printable encoding */
919 serv_printf("\n--%s", alt_boundary);
922 serv_puts("Content-type: text/html; charset=utf-8");
923 serv_puts("Content-Transfer-Encoding: quoted-printable");
925 serv_puts("<html><body>\r\n");
926 text_to_server_qp(sbstr("msgtext")); /* Transmit message in quoted-printable encoding */
927 serv_puts("</body></html>\r\n");
929 if (include_text_alt) {
930 serv_printf("--%s--", alt_boundary);
939 /* Add in the attachments */
940 it = GetNewHashPos(WCC->attachments, 0);
941 while (GetNextHashPos(WCC->attachments, it, &len, &Key, &vAtt)) {
942 att = (wc_mime_attachment *)vAtt;
943 if (att->length == 0)
945 encoded_length = ((att->length * 150) / 100);
946 encoded = malloc(encoded_length);
947 if (encoded == NULL) break;
948 encoded_strlen = CtdlEncodeBase64(encoded, ChrPtr(att->Data), StrLength(att->Data), 1);
950 serv_printf("--%s", top_boundary);
951 serv_printf("Content-type: %s", ChrPtr(att->ContentType));
952 serv_printf("Content-disposition: attachment; filename=\"%s\"", ChrPtr(att->FileName));
953 serv_puts("Content-transfer-encoding: base64");
955 serv_write(encoded, encoded_strlen);
960 serv_printf("--%s--", top_boundary);
969 * Post message (or don't post message)
971 * Note regarding the "dont_post" variable:
972 * A random value (actually, it's just a timestamp) is inserted as a hidden
973 * field called "postseq" when the display_enter page is generated. This
974 * value is checked when posting, using the static variable dont_post. If a
975 * user attempts to post twice using the same dont_post value, the message is
976 * discarded. This prevents the accidental double-saving of the same message
977 * if the user happens to click the browser "back" button.
979 void post_message(void) {
981 StrBuf *EmailAddress;
984 StrBuf *encoded_subject = NULL;
985 static long dont_post = (-1L);
986 int is_anonymous = 0;
987 const StrBuf *display_name = NULL;
991 if (havebstr("force_room")) {
992 gotoroom(sbstr("force_room"));
995 if (havebstr("display_name")) {
996 display_name = sbstr("display_name");
997 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1003 if (!strcasecmp(bstr("submit_action"), "cancel")) {
1004 AppendImportantMessage(_("Cancelled. Message was not posted."), -1);
1005 } else if (lbstr("postseq") == dont_post) {
1006 AppendImportantMessage(
1007 _("Automatically cancelled because you have already "
1008 "saved this message."), -1);
1010 const char CMD[] = "ENT0 1|%s|%d|4|%s|%s||%s|%s|%s|%s|%s";
1011 StrBuf *Recp = NULL;
1014 StrBuf *wikipage = NULL;
1015 const StrBuf *my_email_addr = NULL;
1016 StrBuf *CmdBuf = NULL;
1017 StrBuf *references = NULL;
1018 int saving_to_drafts = 0;
1021 saving_to_drafts = !strcasecmp(bstr("submit_action"), "draft");
1024 if (saving_to_drafts) {
1025 /* temporarily change to the drafts room */
1026 serv_puts("GOTO _DRAFTS_");
1027 StrBuf_ServGetln(Buf);
1028 if (GetServerStatusMsg(Buf, NULL, 1, 2) != 2) {
1029 /* You probably don't even have a dumb Drafts folder */
1030 syslog(LOG_DEBUG, "%s:%d: server save to drafts error: %s\n", __FILE__, __LINE__, ChrPtr(Buf) + 4);
1031 AppendImportantMessage(_("Saved to Drafts failed: "), -1);
1038 if (havebstr("references"))
1040 const StrBuf *ref = sbstr("references");
1041 references = NewStrBufDup(ref);
1042 if (*ChrPtr(references) == '|') { /* remove leading '|' if present */
1043 StrBufCutLeft(references, 1);
1045 StrBufReplaceChars(references, '|', '!');
1047 if (havebstr("subject")) {
1050 * make enough room for the encoded string;
1051 * plus the QP header
1053 Subj = sbstr("subject");
1055 StrBufRFC2047encode(&encoded_subject, Subj);
1057 UserName = NewStrBuf();
1058 EmailAddress = NewStrBuf();
1059 EncBuf = NewStrBuf();
1061 Recp = StrBufSanitizeEmailRecipientVector(sbstr("recp"), UserName, EmailAddress, EncBuf);
1062 Cc = StrBufSanitizeEmailRecipientVector(sbstr("cc"), UserName, EmailAddress, EncBuf);
1063 Bcc = StrBufSanitizeEmailRecipientVector(sbstr("bcc"), UserName, EmailAddress, EncBuf);
1065 FreeStrBuf(&UserName);
1066 FreeStrBuf(&EmailAddress);
1067 FreeStrBuf(&EncBuf);
1069 wikipage = NewStrBufDup(sbstr("page"));
1070 str_wiki_index(wikipage);
1071 my_email_addr = sbstr("my_email_addr");
1073 HeaderLen = StrLength(Recp) +
1074 StrLength(encoded_subject) +
1077 StrLength(wikipage) +
1078 StrLength(my_email_addr) +
1079 StrLength(references);
1080 CmdBuf = NewStrBufPlain(NULL, sizeof (CMD) + HeaderLen);
1081 StrBufPrintf(CmdBuf,
1083 saving_to_drafts?"":ChrPtr(Recp),
1085 ChrPtr(encoded_subject),
1086 ChrPtr(display_name),
1087 saving_to_drafts?"":ChrPtr(Cc),
1088 saving_to_drafts?"":ChrPtr(Bcc),
1090 ChrPtr(my_email_addr),
1091 ChrPtr(references));
1092 FreeStrBuf(&references);
1093 FreeStrBuf(&encoded_subject);
1096 if ((HeaderLen + StrLength(sbstr("msgtext")) < 10) &&
1097 (GetCount(WCC->attachments) == 0)){
1098 AppendImportantMessage(_("Refusing to post empty message.\n"), -1);
1099 FreeStrBuf(&CmdBuf);
1104 syslog(LOG_DEBUG, "%s\n", ChrPtr(CmdBuf));
1105 serv_puts(ChrPtr(CmdBuf));
1106 FreeStrBuf(&CmdBuf);
1108 StrBuf_ServGetln(Buf);
1109 if (GetServerStatus(Buf, NULL) == 4) {
1110 if (saving_to_drafts) {
1111 if ( (havebstr("recp"))
1112 || (havebstr("cc" ))
1113 || (havebstr("bcc" )) ) {
1114 /* save recipient headers or room to post to */
1115 serv_printf("To: %s", ChrPtr(Recp));
1116 serv_printf("Cc: %s", ChrPtr(Cc));
1117 serv_printf("Bcc: %s", ChrPtr(Bcc));
1119 serv_printf("X-Citadel-Room: %s", ChrPtr(WCC->CurRoom.name));
1122 post_mime_to_server();
1123 if (saving_to_drafts) {
1124 AppendImportantMessage(_("Message has been saved to Drafts.\n"), -1);
1125 gotoroom(WCC->CurRoom.name);
1127 readloop(readnew, eUseDefault);
1130 } else if ( (havebstr("recp"))
1131 || (havebstr("cc" ))
1132 || (havebstr("bcc" ))
1134 AppendImportantMessage(_("Message has been sent.\n"), -1);
1137 AppendImportantMessage(_("Message has been posted.\n"), -1);
1139 dont_post = lbstr("postseq");
1141 syslog(LOG_DEBUG, "%s:%d: server post error: %s", __FILE__, __LINE__, ChrPtr(Buf) + 4);
1142 AppendImportantMessage(ChrPtr(Buf) + 4, StrLength(Buf) - 4);
1144 if (saving_to_drafts) gotoroom(WCC->CurRoom.name);
1158 DeleteHash(&WCC->attachments);
1161 * We may have been supplied with instructions regarding the location
1162 * to which we must return after posting. If found, go there.
1164 if (havebstr("return_to")) {
1165 http_redirect(bstr("return_to"));
1168 * If we were editing a page in a wiki room, go to that page now.
1170 else if (havebstr("page")) {
1171 snprintf(buf, sizeof buf, "wiki?page=%s", bstr("page"));
1175 * Otherwise, just go to the "read messages" loop.
1179 readloop(readnew, eUseDefault);
1185 * Client is uploading an attachment
1187 void upload_attachment(void) {
1188 wcsession *WCC = WC;
1194 wc_mime_attachment *att;
1195 const StrBuf *Tmpl = sbstr("template");
1196 const StrBuf *MimeType = NULL;
1200 syslog(LOG_DEBUG, "upload_attachment()\n");
1201 if (!Tmpl) wc_printf("upload_attachment()<br>\n");
1203 if (WCC->upload_length <= 0) {
1204 syslog(LOG_DEBUG, "ERROR no attachment was uploaded\n");
1207 putlbstr("UPLOAD_ERROR", 1);
1208 MimeType = DoTemplate(SKEY(Tmpl), NULL, &NoCtx);
1210 else wc_printf("ERROR no attachment was uploaded<br>\n");
1211 http_transmit_thing(ChrPtr(MimeType), 0);
1216 syslog(LOG_DEBUG, "Client is uploading %d bytes\n", WCC->upload_length);
1217 if (Tmpl) putlbstr("UPLOAD_LENGTH", WCC->upload_length);
1218 else wc_printf("Client is uploading %d bytes<br>\n", WCC->upload_length);
1220 att = (wc_mime_attachment*)malloc(sizeof(wc_mime_attachment));
1221 memset(att, 0, sizeof(wc_mime_attachment ));
1222 att->length = WCC->upload_length;
1223 att->ContentType = NewStrBufPlain(WCC->upload_content_type, -1);
1224 att->FileName = NewStrBufDup(WCC->upload_filename);
1225 UID = sbstr("qquuid");
1227 att->PartNum = NewStrBufDup(UID);
1229 if (WCC->attachments == NULL) {
1230 WCC->attachments = NewHash(1, Flathash);
1233 /* And add it to the list. */
1235 if ((GetCount(WCC->attachments) > 0) &&
1236 GetHashAt(WCC->attachments,
1237 GetCount(WCC->attachments) -1,
1238 &newnlen, &newn, &v))
1239 n = *((int*) newn) + 1;
1240 Put(WCC->attachments, IKEY(n), att, DestroyMime);
1243 * Mozilla sends a simple filename, which is what we want,
1244 * but Satan's Browser sends an entire pathname. Reduce
1245 * the path to just a filename if we need to.
1247 pch = strrchr(ChrPtr(att->FileName), '/');
1249 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1251 pch = strrchr(ChrPtr(att->FileName), '\\');
1253 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1257 * Transfer control of this memory from the upload struct
1258 * to the attachment struct.
1260 att->Data = WCC->upload;
1262 WCC->upload_length = 0;
1264 if (Tmpl) MimeType = DoTemplate(SKEY(Tmpl), NULL, &NoCtx);
1265 http_transmit_thing(ChrPtr(MimeType), 0);
1270 * Remove an attachment from the message currently being composed.
1272 * Currently we identify the attachment to be removed by its filename.
1273 * There is probably a better way to do this.
1275 void remove_attachment(void) {
1276 wcsession *WCC = WC;
1277 wc_mime_attachment *att;
1279 StrBuf *WhichAttachment;
1285 WhichAttachment = NewStrBufDup(sbstr("which_attachment"));
1286 if (ChrPtr(WhichAttachment)[0] == '/')
1287 StrBufCutLeft(WhichAttachment, 1);
1288 StrBufUnescape(WhichAttachment, 0);
1289 at = GetNewHashPos(WCC->attachments, 0);
1292 GetHashPos(WCC->attachments, at, &len, &key, &vAtt);
1294 att = (wc_mime_attachment*) vAtt;
1295 if ((att != NULL) &&
1297 !strcmp(ChrPtr(WhichAttachment), ChrPtr(att->FileName)) ||
1298 ((att->PartNum != NULL) &&
1299 !strcmp(ChrPtr(WhichAttachment), ChrPtr(att->PartNum)))
1302 DeleteEntryFromHash(WCC->attachments, at);
1307 while (NextHashPos(WCC->attachments, at));
1309 FreeStrBuf(&WhichAttachment);
1310 wc_printf("remove_attachment(%d) completed\n", found);
1314 const char *ReplyToModeStrings [3] = {
1319 typedef enum _eReplyToNodes {
1327 * display the message entry screen
1329 void display_enter(void) {
1330 const char *ReplyingModeStr;
1331 eReplyToNodes ReplyMode = eReply;
1335 const StrBuf *display_name = NULL;
1336 int recipient_required = 0;
1337 int subject_required = 0;
1338 int is_anonymous = 0;
1339 wcsession *WCC = WC;
1343 if (havebstr("force_room")) {
1344 gotoroom(sbstr("force_room"));
1347 display_name = sbstr("display_name");
1348 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1349 display_name = NULL;
1354 * First, do we have permission to enter messages in this room at all?
1357 serv_puts("ENT0 0");
1358 StrBuf_ServGetln(Line);
1359 rc = GetServerStatusMsg(Line, &Result, 0, 2);
1361 if (Result == 570) { /* 570 means that we need a recipient here */
1362 recipient_required = 1;
1364 else if (rc != 2) { /* Any other error means that we cannot continue */
1365 rc = GetServerStatusMsg(Line, &Result, 0, 2);
1367 readloop(readnew, eUseDefault);
1372 /* Is the server strongly recommending that the user enter a message subject? */
1373 if (StrLength(Line) > 4) {
1374 subject_required = extract_int(ChrPtr(Line) + 4, 1);
1378 * Are we perhaps in an address book view? If so, then an "enter
1379 * message" command really means "add new entry."
1381 if (WCC->CurRoom.defview == VIEW_ADDRESSBOOK) {
1382 do_edit_vcard(-1, "", NULL, NULL, "", ChrPtr(WCC->CurRoom.name));
1388 * Are we perhaps in a calendar room? If so, then an "enter
1389 * message" command really means "add new calendar item."
1391 if (WCC->CurRoom.defview == VIEW_CALENDAR) {
1392 display_edit_event();
1398 * Are we perhaps in a tasks view? If so, then an "enter
1399 * message" command really means "add new task."
1401 if (WCC->CurRoom.defview == VIEW_TASKS) {
1402 display_edit_task();
1407 ReplyingModeStr = bstr("replying_mode");
1408 if (ReplyingModeStr != NULL) {
1409 for (i = 0; i < 3; i++) {
1410 if (strcmp(ReplyingModeStr, ReplyToModeStrings[i]) == 0) {
1411 ReplyMode = (eReplyToNodes) i;
1418 * If the "replying_to" variable is set, it refers to a message
1419 * number from which we must extract some header fields...
1421 replying_to = lbstr("replying_to");
1422 if (replying_to > 0) {
1424 StrBuf *wefw = NULL;
1425 StrBuf *msgn = NULL;
1426 StrBuf *from = NULL;
1427 StrBuf *rfca = NULL;
1428 StrBuf *rcpt = NULL;
1429 StrBuf *cccc = NULL;
1430 StrBuf *replyto = NULL;
1431 StrBuf *nvto = NULL;
1432 int message_originated_locally = 0;
1433 serv_printf("MSG0 %ld|1", replying_to);
1435 StrBuf_ServGetln(Line);
1436 if (GetServerStatusMsg(Line, NULL, 0, 0) == 1)
1437 while (len = StrBuf_ServGetln(Line), (len >= 0) && ((len != 3) || strcmp(ChrPtr(Line), "000"))) {
1438 eMessageField which;
1439 if ((StrLength(Line) > 4) && (ChrPtr(Line)[4] == '=') && GetFieldFromMnemonic(&which, ChrPtr(Line))) switch (which) {
1441 StrBuf *subj = NewStrBuf();
1442 StrBuf *FlatSubject;
1444 if (ReplyMode == eForward) {
1445 if (strncasecmp(ChrPtr(Line) + 5, "Fw:", 3)) {
1446 StrBufAppendBufPlain(subj, HKEY("Fw: "), 0);
1450 if (strncasecmp(ChrPtr(Line) + 5, "Re:", 3)) {
1451 StrBufAppendBufPlain(subj, HKEY("Re: "), 0);
1454 StrBufAppendBufPlain(subj,
1456 StrLength(Line) - 5, 0);
1457 FlatSubject = NewStrBufPlain(NULL, StrLength(subj));
1458 StrBuf_RFC822_to_Utf8(FlatSubject, subj, NULL, NULL);
1460 PutBstr(HKEY("subject"), FlatSubject);
1465 message_originated_locally = 1;
1474 wefw = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1476 /* Trim down excessively long lists of thread references. We eliminate the
1477 * second one in the list so that the thread root remains intact.
1479 rrtok = num_tokens(ChrPtr(wefw), '|');
1480 rrlen = StrLength(wefw);
1481 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
1482 StrBufRemove_token(wefw, 1, '|');
1488 msgn = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1493 from = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1494 FlatFrom = NewStrBufPlain(NULL, StrLength(from));
1495 StrBuf_RFC822_to_Utf8(FlatFrom, from, NULL, NULL);
1498 for (i=0; i<StrLength(from); ++i) {
1499 if (ChrPtr(from)[i] == ',')
1500 StrBufPeek(from, NULL, i, ' ');
1505 rcpt = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1508 cccc = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1511 replyto = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1515 rfca = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1516 FlatRFCA = NewStrBufPlain(NULL, StrLength(rfca));
1517 StrBuf_RFC822_to_Utf8(FlatRFCA, rfca, NULL, NULL);
1523 nvto = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1524 putbstr("nvto", nvto);
1544 if (StrLength(wefw) + StrLength(msgn) > 0) {
1545 StrBuf *refs = NewStrBuf();
1546 if (StrLength(wefw) > 0) {
1547 StrBufAppendBuf(refs, wefw, 0);
1549 if ( (StrLength(wefw) > 0) &&
1550 (StrLength(msgn) > 0) )
1552 StrBufAppendBufPlain(refs, HKEY("|"), 0);
1554 if (StrLength(msgn) > 0) {
1555 StrBufAppendBuf(refs, msgn, 0);
1557 PutBstr(HKEY("references"), refs);
1561 * If this is a Reply or a ReplyAll, copy the sender's email into the To: field
1563 if ((ReplyMode == eReply) || (ReplyMode == eReplyAll)) {
1566 if ((StrLength(replyto) > 0) && (ReplyMode == eReplyAll)) {
1567 to_rcpt = NewStrBuf();
1568 StrBufAppendBuf(to_rcpt, replyto, 0);
1571 else if ( (StrLength(rfca) > 0) && (message_originated_locally == 0) ) {
1572 to_rcpt = NewStrBuf();
1573 StrBufAppendBuf(to_rcpt, from, 0);
1574 StrBufAppendBufPlain(to_rcpt, HKEY(" <"), 0);
1575 StrBufAppendBuf(to_rcpt, rfca, 0);
1576 StrBufAppendBufPlain(to_rcpt, HKEY(">"), 0);
1583 PutBstr(HKEY("recp"), to_rcpt);
1587 * Only if this is a ReplyAll, copy all recipients into the Cc: field
1589 if (ReplyMode == eReplyAll) {
1590 StrBuf *cc_rcpt = rcpt;
1592 if ((StrLength(cccc) > 0) && (StrLength(replyto) == 0)) {
1593 if (cc_rcpt != NULL) {
1594 StrBufAppendPrintf(cc_rcpt, ", ");
1595 StrBufAppendBuf(cc_rcpt, cccc, 0);
1602 if (cc_rcpt != NULL) {
1603 PutBstr(HKEY("cc"), cc_rcpt);
1608 syslog(LOG_DEBUG, "wefw = %s", ChrPtr(wefw));
1609 syslog(LOG_DEBUG, "msgn = %s", ChrPtr(msgn));
1610 syslog(LOG_DEBUG, "from = %s", ChrPtr(from));
1611 syslog(LOG_DEBUG, "rfca = %s", ChrPtr(rfca));
1612 syslog(LOG_DEBUG, "rcpt = %s", ChrPtr(rcpt));
1613 syslog(LOG_DEBUG, "cccc = %s", ChrPtr(cccc));
1614 syslog(LOG_DEBUG, "replyto = %s", ChrPtr(replyto));
1615 syslog(LOG_DEBUG, "nvto = %s", ChrPtr(nvto));
1616 syslog(LOG_DEBUG, "local = %d" , message_originated_locally);
1628 * Otherwise proceed normally.
1629 * Do a custom room banner with no navbar...
1631 if (recipient_required) {
1632 const StrBuf *Recp = NULL;
1633 const StrBuf *Cc = NULL;
1634 const StrBuf *Bcc = NULL;
1635 StrBuf *wikipage = NULL;
1636 StrBuf *CmdBuf = NULL;
1637 const char CMD[] = "ENT0 0|%s|%d|0||%s||%s|%s|%s";
1639 Recp = sbstr("recp");
1642 wikipage = NewStrBufDup(sbstr("page"));
1643 str_wiki_index(wikipage);
1645 CmdBuf = NewStrBufPlain(NULL,
1648 StrLength(display_name) +
1651 StrLength(wikipage));
1653 StrBufPrintf(CmdBuf,
1657 ChrPtr(display_name),
1662 serv_puts(ChrPtr(CmdBuf));
1663 StrBuf_ServGetln(CmdBuf);
1666 rc = GetServerStatusMsg(CmdBuf, &Result, 0, 0);
1668 if ( (Result == 570) /* invalid or missing recipient(s) */
1669 || (Result == 550) /* higher access required to send Internet mail */
1671 /* These errors will have been displayed and are excusable */
1673 else if (rc != 2) { /* Any other error means that we cannot continue */
1674 AppendImportantMessage(ChrPtr(CmdBuf) + 4, StrLength(CmdBuf) - 4);
1675 FreeStrBuf(&CmdBuf);
1677 readloop(readnew, eUseDefault);
1680 FreeStrBuf(&CmdBuf);
1682 if (recipient_required)
1683 PutBstr(HKEY("__RCPTREQUIRED"), NewStrBufPlain(HKEY("1")));
1684 if (recipient_required || subject_required)
1685 PutBstr(HKEY("__SUBJREQUIRED"), NewStrBufPlain(HKEY("1")));
1688 output_headers(1, 0, 0, 0, 1, 0);
1689 DoTemplate(HKEY("edit_message"), NULL, &NoCtx);
1699 void delete_msg(void) {
1703 msgid = lbstr("msgid");
1705 if ((WC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
1706 serv_printf("DELE %ld", msgid);
1708 else { /* Otherwise move it to Trash */
1709 serv_printf("MOVE %ld|_TRASH_|0", msgid);
1712 StrBuf_ServGetln(Line);
1713 GetServerStatusMsg(Line, NULL, 1, 0);
1717 readloop(readnew, eUseDefault);
1722 * move a message to another room
1724 void move_msg(void) {
1727 msgid = lbstr("msgid");
1729 if (havebstr("move_button")) {
1731 serv_printf("MOVE %ld|%s", msgid, bstr("target_room"));
1733 StrBuf_ServGetln(Line);
1734 GetServerStatusMsg(Line, NULL, 1, 0);
1737 AppendImportantMessage(_("The message was not moved."), -1);
1741 readloop(readnew, eUseDefault);
1746 * Generic function to output an arbitrary MIME attachment from
1747 * message being composed
1749 * partnum The MIME part to be output
1750 * filename Fake filename to give
1751 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1753 void postpart(StrBuf *partnum, StrBuf *filename, int force_download) {
1755 StrBuf *content_type;
1756 wc_mime_attachment *part;
1759 i = StrToi(partnum);
1760 if (GetHash(WC->attachments, IKEY(i), &vPart) &&
1762 part = (wc_mime_attachment*) vPart;
1763 if (force_download) {
1764 content_type = NewStrBufPlain(HKEY("application/octet-stream"));
1767 content_type = NewStrBufDup(part->ContentType);
1769 StrBufAppendBuf(WC->WBuf, part->Data, 0);
1770 http_transmit_thing(ChrPtr(content_type), 0);
1772 hprintf("HTTP/1.1 404 %s\n", ChrPtr(partnum));
1773 output_headers(0, 0, 0, 0, 0, 0);
1774 hprintf("Content-Type: text/plain\r\n");
1776 wc_printf(_("An error occurred while retrieving this part: %s/%s\n"),
1777 ChrPtr(partnum), ChrPtr(filename));
1780 FreeStrBuf(&content_type);
1785 * Generic function to output an arbitrary MIME part from an arbitrary
1786 * message number on the server.
1788 * msgnum message number on the citadel server
1789 * partnum MIME part number to be output
1790 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1792 void view_or_download_mimepart(int force_download) {
1796 StrBuf *ContentType;
1799 PartNum = NewStrBuf();
1801 msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
1802 StrBufExtract_token(PartNum, WC->Hdr->HR.ReqLine, 1, '/');
1805 serv_printf("DLAT %ld|%s", msgnum, ChrPtr(PartNum)); // DLAT will return: 6XX length|-1|filename|cbtype|cbcharset
1806 StrBuf_ServGetln(Buf);
1807 FreeStrBuf(&PartNum);
1809 if (GetServerStatus(Buf, NULL) != 6) {
1811 hprintf("HTTP/1.1 500 error\r\n"
1812 "Pragma: no-cache\r\n"
1813 "Cache-Control: no-store\r\n"
1815 "Content-Type: text/plain\r\n"
1818 wc_printf(_("An error occurred while retrieving this part: %s\n"), "--");
1822 StrBufCutLeft(Buf, 4);
1823 bytes = StrBufExtract_long(Buf, 0, '|');
1825 if (force_download) {
1826 ContentType = NewStrBufPlain(HKEY("application/octet-stream"));
1829 ContentType = NewStrBuf();
1830 StrBufExtract_token(ContentType, Buf, 3, '|');
1835 StrBuf_ServGetBLOBBuffered(Buf, bytes);
1839 http_transmit_thing(ChrPtr(ContentType), 0);
1840 FreeStrBuf(&ContentType);
1845 * Read any MIME part of a message, from the server, into memory.
1847 StrBuf *load_mimepart(long msgnum, char *partnum) {
1852 serv_printf("DLAT %ld|%s", msgnum, partnum);
1853 StrBuf_ServGetln(Buf);
1854 if (GetServerStatus(Buf, NULL) == 6) {
1855 StrBufCutLeft(Buf, 4);
1856 bytes = StrBufExtract_long(Buf, 0, '|');
1859 StrBuf_ServGetBLOBBuffered(Buf, bytes);
1870 * Read any MIME part of a message, from the server, into memory.
1872 void MimeLoadData(wc_mime_attachment *Mime) {
1877 serv_printf("DLAT %ld|%s", Mime->msgnum, ChrPtr(Mime->PartNum));
1879 StrBuf_ServGetln(Buf);
1880 if (GetServerStatus(Buf, NULL) == 6) {
1881 Ptr = &(ChrPtr(Buf)[4]);
1882 bytes = StrBufExtractNext_long(Buf, &Ptr, '|');
1883 StrBufSkip_NTokenS(Buf, &Ptr, '|', 3); /* filename, cbtype, mimetype */
1884 if (Mime->Charset == NULL) Mime->Charset = NewStrBuf();
1885 StrBufExtract_NextToken(Mime->Charset, Buf, &Ptr, '|');
1887 if (Mime->Data == NULL) {
1888 Mime->Data = NewStrBufPlain(NULL, bytes);
1890 StrBuf_ServGetBLOBBuffered(Mime->Data, bytes);
1893 FlushStrBuf(Mime->Data);
1899 void view_mimepart(void) {
1900 view_or_download_mimepart(0);
1904 void download_mimepart(void) {
1905 view_or_download_mimepart(1);
1909 void view_postpart(void) {
1910 StrBuf *filename = NewStrBuf();
1911 StrBuf *partnum = NewStrBuf();
1913 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1914 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1916 postpart(partnum, filename, 0);
1918 FreeStrBuf(&filename);
1919 FreeStrBuf(&partnum);
1923 void download_postpart(void) {
1924 StrBuf *filename = NewStrBuf();
1925 StrBuf *partnum = NewStrBuf();
1927 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1928 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1930 postpart(partnum, filename, 1);
1932 FreeStrBuf(&filename);
1933 FreeStrBuf(&partnum);
1937 void show_num_attachments(void) {
1938 wc_printf("%d", GetCount(WC->attachments));
1942 void h_readnew(void) { readloop(readnew, eUseDefault);}
1943 void h_readold(void) { readloop(readold, eUseDefault);}
1944 void h_readfwd(void) { readloop(readfwd, eUseDefault);}
1945 void h_headers(void) { readloop(headers, eUseDefault);}
1946 void h_do_search(void) { readloop(do_search, eUseDefault);}
1947 void h_readgt(void) { readloop(readgt, eUseDefault);}
1948 void h_readlt(void) { readloop(readlt, eUseDefault);}
1951 /* Output message list in JSON format */
1952 void jsonMessageList(void) {
1953 StrBuf *View = NewStrBuf();
1954 const StrBuf *room = sbstr("room");
1955 long oper = (havebstr("query")) ? do_search : readnew;
1956 StrBufPrintf(View, "%d", VIEW_JSON_LIST);
1957 putbstr("view", View);;
1959 readloop(oper, eUseDefault);
1963 void FreeReadLoopHandlerSet(void *v) {
1964 RoomRenderer *Handler = (RoomRenderer *) v;
1965 FreeStrBuf(&Handler->FetchMessageList);
1966 if (Handler->MessageFieldList != NULL) {
1967 free(Handler->MessageFieldList);
1973 void RegisterReadLoopHandlerset(
1975 GetParamsGetServerCall_func GetParamsGetServerCall,
1976 PrintViewHeader_func PrintPageHeader,
1977 PrintViewHeader_func PrintViewHeader,
1978 load_msg_ptrs_detailheaders LH,
1979 LoadMsgFromServer_func LoadMsgFromServer,
1980 RenderView_or_Tail_func RenderView_or_Tail,
1981 View_Cleanup_func ViewCleanup,
1982 const char **browseListFields
1987 RoomRenderer *Handler;
1989 Handler = (RoomRenderer*) malloc(sizeof(RoomRenderer));
1991 Handler->RoomType = RoomType;
1992 Handler->GetParamsGetServerCall = GetParamsGetServerCall;
1993 Handler->PrintPageHeader = PrintPageHeader;
1994 Handler->PrintViewHeader = PrintViewHeader;
1995 Handler->LoadMsgFromServer = LoadMsgFromServer;
1996 Handler->RenderView_or_Tail = RenderView_or_Tail;
1997 Handler->ViewCleanup = ViewCleanup;
1998 Handler->LHParse = LH;
2000 if (browseListFields != NULL) {
2001 while (browseListFields[count] != NULL) {
2004 Handler->HeaderCount = count;
2005 Handler->MessageFieldList = (eMessageField*) malloc(sizeof(eMessageField) * count);
2006 Handler->FetchMessageList = NewStrBufPlain(NULL, 5 * count + 4 + 5);
2007 StrBufPlain(Handler->FetchMessageList, HKEY("time\n"));
2008 for (i = 0; i < count; i++) {
2009 if (!GetFieldFromMnemonic(&Handler->MessageFieldList[i], browseListFields[i])) {
2010 fprintf(stderr, "Unknown message header: %s\n", browseListFields[i]);
2013 StrBufAppendBufPlain(Handler->FetchMessageList, browseListFields[i], 4, 0);
2014 StrBufAppendBufPlain(Handler->FetchMessageList, HKEY("\n"), 0);
2016 StrBufAppendBufPlain(Handler->FetchMessageList, HKEY("000"), 0);
2019 Handler->FetchMessageList = NULL;
2020 Handler->MessageFieldList = NULL;
2023 Put(ReadLoopHandler, IKEY(RoomType), Handler, FreeReadLoopHandlerSet);
2030 RegisterPreference("use_sig",
2031 _("Attach signature to email messages?"),
2034 RegisterPreference("signature", _("Use this signature:"), PRF_QP_STRING, NULL);
2035 RegisterPreference("default_header_charset",
2036 _("Default character set for email headers:"),
2039 RegisterPreference("defaultfrom", _("Preferred email address"), PRF_STRING, NULL);
2040 RegisterPreference("defaultname",
2041 _("Preferred display name for email messages"),
2044 RegisterPreference("defaulthandle",
2045 _("Preferred display name for bulletin board posts"),
2048 RegisterPreference("mailbox",_("Mailbox view mode"), PRF_STRING, NULL);
2050 WebcitAddUrlHandler(HKEY("readnew"), "", 0, h_readnew, ANONYMOUS|NEED_URL);
2051 WebcitAddUrlHandler(HKEY("readold"), "", 0, h_readold, ANONYMOUS|NEED_URL);
2052 WebcitAddUrlHandler(HKEY("readfwd"), "", 0, h_readfwd, ANONYMOUS|NEED_URL);
2053 WebcitAddUrlHandler(HKEY("headers"), "", 0, h_headers, NEED_URL);
2054 WebcitAddUrlHandler(HKEY("readgt"), "", 0, h_readgt, ANONYMOUS|NEED_URL);
2055 WebcitAddUrlHandler(HKEY("readlt"), "", 0, h_readlt, ANONYMOUS|NEED_URL);
2056 WebcitAddUrlHandler(HKEY("do_search"), "", 0, h_do_search, 0);
2057 WebcitAddUrlHandler(HKEY("display_enter"), "", 0, display_enter, 0);
2058 WebcitAddUrlHandler(HKEY("post"), "", 0, post_message, PROHIBIT_STARTPAGE);
2059 WebcitAddUrlHandler(HKEY("move_msg"), "", 0, move_msg, PROHIBIT_STARTPAGE);
2060 WebcitAddUrlHandler(HKEY("delete_msg"), "", 0, delete_msg, PROHIBIT_STARTPAGE);
2061 WebcitAddUrlHandler(HKEY("msg"), "", 0, embed_message, NEED_URL);
2062 WebcitAddUrlHandler(HKEY("message"), "", 0, handle_one_message, NEED_URL|XHTTP_COMMANDS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
2063 WebcitAddUrlHandler(HKEY("printmsg"), "", 0, print_message, NEED_URL);
2064 WebcitAddUrlHandler(HKEY("msgheaders"), "", 0, display_headers, NEED_URL);
2066 WebcitAddUrlHandler(HKEY("mimepart"), "", 0, view_mimepart, NEED_URL);
2067 WebcitAddUrlHandler(HKEY("mimepart_download"), "", 0, download_mimepart, NEED_URL);
2068 WebcitAddUrlHandler(HKEY("postpart"), "", 0, view_postpart, NEED_URL|PROHIBIT_STARTPAGE);
2069 WebcitAddUrlHandler(HKEY("postpart_download"), "", 0, download_postpart, NEED_URL|PROHIBIT_STARTPAGE);
2070 WebcitAddUrlHandler(HKEY("upload_attachment"), "", 0, upload_attachment, AJAX);
2071 WebcitAddUrlHandler(HKEY("remove_attachment"), "", 0, remove_attachment, AJAX);
2072 WebcitAddUrlHandler(HKEY("show_num_attachments"), "", 0, show_num_attachments, AJAX);
2075 WebcitAddUrlHandler(HKEY("roommsgs"), "", 0, jsonMessageList,0);
2079 SessionDetachModule_MSG
2082 DeleteHash(&sess->summ);