2 * Functions which deal with the fetching and displaying of messages.
4 * Copyright (c) 1996-2012 by the citadel.org team
6 * This program is open source software. You can redistribute it and/or
7 * modify it under the terms of the GNU General Public License, version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include "webserver.h"
20 HashList *MsgHeaderHandler = NULL;
21 HashList *MsgEvaluators = NULL;
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 */
30 void jsonMessageListHdr(void);
32 void display_enter(void);
34 typedef void (*MsgPartEvaluatorFunc)(message_summary *Sum, StrBuf *Buf);
36 typedef struct _MsgPartEvaluatorStruct {
37 MsgPartEvaluatorFunc f;
38 } MsgPartEvaluatorStruct;
42 /* workaround for json listview; its not useable directly */
43 if (WC->CurRoom.view == VIEW_JSON_LIST) {
44 StrBuf *View = NewStrBuf();
45 StrBufPrintf(View, "%d", VIEW_MAILBOX);
46 putbstr("view", View);;
49 int load_message(message_summary *Msg,
62 if (Msg->PartNum != NULL) {
63 serv_printf("MSG4 %ld|%s", Msg->msgnum, ChrPtr(Msg->PartNum));
66 serv_printf("MSG4 %ld", Msg->msgnum);
69 StrBuf_ServGetln(Buf);
70 if (GetServerStatus(Buf, NULL) != 1) {
72 StrBufAppendPrintf(*Error, "<strong>");
73 StrBufAppendPrintf(*Error, _("ERROR:"));
74 StrBufAppendPrintf(*Error, "</strong> %s<br>\n", &buf[4]);
79 /* begin everythingamundo table */
80 HdrToken = NewStrBuf();
81 while (!Done && StrBuf_ServGetln(Buf)>=0) {
82 if ( (StrLength(Buf)==3) &&
83 !strcmp(ChrPtr(Buf), "000"))
87 if (Msg->MsgBody->Data == NULL)
88 Msg->MsgBody->Data = NewStrBuf();
89 Msg->MsgBody->ContentType = NewStrBufPlain(HKEY("text/html"));
90 StrBufAppendPrintf(Msg->MsgBody->Data, "<div><i>");
91 StrBufAppendPrintf(Msg->MsgBody->Data, _("Empty message"));
92 StrBufAppendPrintf(Msg->MsgBody->Data, "</i><br><br>\n");
93 StrBufAppendPrintf(Msg->MsgBody->Data, "</div>\n");
98 case 0:/* Citadel Message Headers */
99 if (StrLength(Buf) == 0) {
103 StrBufExtract_token(HdrToken, Buf, 0, '=');
104 StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
106 /* look up one of the examine_* functions to parse the content */
107 if (GetHash(MsgHeaderHandler, SKEY(HdrToken), &vHdr) &&
109 Hdr = (headereval*)vHdr;
110 Hdr->evaluator(Msg, Buf, FoundCharset);
111 if (Hdr->Type == 1) {
115 else LogError(Target,
117 "don't know how to handle message header[%s]\n",
121 case 1:/* Message Mime Header */
122 if (StrLength(Buf) == 0) {
124 if (Msg->MsgBody->ContentType == NULL)
125 /* end of header or no header? */
126 Msg->MsgBody->ContentType = NewStrBufPlain(HKEY("text/plain"));
127 /* usual end of mime header */
131 StrBufExtract_token(HdrToken, Buf, 0, ':');
132 if (StrLength(HdrToken) > 0) {
133 StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
134 /* the examine*'s know how to do with mime headers too... */
135 if (GetHash(MsgHeaderHandler, SKEY(HdrToken), &vHdr) &&
137 Hdr = (headereval*)vHdr;
138 Hdr->evaluator(Msg, Buf, FoundCharset);
143 case 2: /* Message Body */
145 if (Msg->MsgBody->size_known > 0) {
146 StrBuf_ServGetBLOBBuffered(Msg->MsgBody->Data, Msg->MsgBody->length);
148 /*/ todo: check next line, if not 000, append following lines */
151 if (StrLength(Msg->MsgBody->Data) > 0)
152 StrBufAppendBufPlain(Msg->MsgBody->Data, "\n", 1, 0);
153 StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
157 StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
162 if (Msg->AllAttach == NULL)
163 Msg->AllAttach = NewHash(1,NULL);
164 /* now we put the body mimepart we read above into the mimelist */
165 Put(Msg->AllAttach, SKEY(Msg->MsgBody->PartNum), Msg->MsgBody, DestroyMime);
168 FreeStrBuf(&HdrToken);
175 * I wanna SEE that message!
177 * msgnum Message number to display
178 * printable_view Nonzero to display a printable view
179 * section Optional for encapsulated message/rfc822 submessage
181 int read_message(StrBuf *Target, const char *tmpl, long tmpllen, long msgnum, const StrBuf *PartNum, const StrBuf **OutMime)
184 StrBuf *FoundCharset;
187 message_summary *Msg = NULL;
191 WCTemplputParams SubTP;
192 StrBuf *Error = NULL;
195 FoundCharset = NewStrBuf();
196 Msg = (message_summary *)malloc(sizeof(message_summary));
197 memset(Msg, 0, sizeof(message_summary));
198 Msg->msgnum = msgnum;
199 Msg->PartNum = PartNum;
200 Msg->MsgBody = (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
201 memset(Msg->MsgBody, 0, sizeof(wc_mime_attachment));
202 Msg->MsgBody->msgnum = msgnum;
204 if (!load_message(Msg, FoundCharset, &Error)) {
205 StrBufAppendBuf(Target, Error, 0);
209 /* Extract just the content-type (omit attributes such as "charset") */
210 StrBufExtract_token(Buf, Msg->MsgBody->ContentType, 0, ';');
212 StrBufLowerCase(Buf);
214 /* Locate a renderer capable of converting this MIME part into HTML */
215 if (GetHash(MimeRenderHandler, SKEY(Buf), &vHdr) &&
217 RenderMimeFuncStruct *Render;
218 Render = (RenderMimeFuncStruct*)vHdr;
219 Render->f(Msg->MsgBody, NULL, FoundCharset);
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 wc_mime_attachment *Mime = (wc_mime_attachment*) vMime;
238 evaluate_mime_part(Msg, Mime);
241 StackContext(NULL, &SubTP, Msg, CTX_MAILSUM, 0, NULL);
243 *OutMime = DoTemplate(tmpl, tmpllen, Target, &SubTP);
245 UnStackContext(&SubTP);
247 DestroyMessageSummary(Msg);
248 FreeStrBuf(&FoundCharset);
255 HttpStatus(long CitadelStatus)
257 long httpstatus = 502;
259 switch (MAJORCODE(CitadelStatus))
261 case LISTING_FOLLOWS:
266 switch (MINORCODE(CitadelStatus))
274 case HIGHER_ACCESS_REQUIRED:
275 case MAX_SESSIONS_EXCEEDED:
277 case RESOURCE_NOT_OPEN:
279 case INVALID_FLOOR_OPERATION:
285 case MESSAGE_NOT_FOUND:
295 case CMD_NOT_SUPPORTED:
296 case PASSWORD_REQUIRED:
297 case ALREADY_LOGGED_IN:
298 case USERNAME_REQUIRED:
300 case SERVER_SHUTTING_DOWN:
311 case START_CHAT_MODE:
315 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)
328 long CitStatus = ERROR + NOT_HERE;
330 const StrBuf *Destination;
336 StrBuf *CmdBuf = NULL;
340 pMsg = strchr(ChrPtr(WCC->Hdr->HR.ReqLine), '/');
342 HttpStatus(CitStatus);
346 msgnum = atol(pMsg + 1);
347 StrBufCutAt(WCC->Hdr->HR.ReqLine, 0, pMsg);
348 gotoroom(WCC->Hdr->HR.ReqLine);
349 switch (WCC->Hdr->HR.eReqType)
353 Tmpl = sbstr("template");
354 if (StrLength(Tmpl) > 0)
355 read_message(WCC->WBuf, SKEY(Tmpl), msgnum, NULL, &Mime);
357 read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
358 http_transmit_thing(ChrPtr(Mime), 0);
361 CmdBuf = NewStrBuf ();
362 if ((WCC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
363 serv_printf("DELE %ld", msgnum);
365 else { /* Otherwise move it to Trash */
366 serv_printf("MOVE %ld|_TRASH_|0", msgnum);
368 StrBuf_ServGetln(CmdBuf);
369 GetServerStatusMsg(CmdBuf, &CitStatus, 1, 0);
370 HttpStatus(CitStatus);
375 if (GetHash(WCC->Hdr->HTTPHeaders, HKEY("DESTINATION"), &vLine) &&
377 Destination = (StrBuf*) vLine;
378 serv_printf("MOVE %ld|%s|%d", msgnum, ChrPtr(Destination), CopyMessage);
379 StrBuf_ServGetln(CmdBuf);
380 GetServerStatusMsg(CmdBuf, NULL, 1, 0);
381 GetServerStatus(CmdBuf, &CitStatus);
382 HttpStatus(CitStatus);
395 * Unadorned HTML output of an individual message, suitable
396 * for placing in a hidden iframe, for printing, or whatever
398 void embed_message(void) {
403 StrBuf *CmdBuf = NULL;
405 msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
406 if (msgnum <= 0) return;
408 switch (WCC->Hdr->HR.eReqType)
412 Tmpl = sbstr("template");
413 if (StrLength(Tmpl) > 0)
414 read_message(WCC->WBuf, SKEY(Tmpl), msgnum, NULL, &Mime);
416 read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
417 http_transmit_thing(ChrPtr(Mime), 0);
420 CmdBuf = NewStrBuf ();
421 if ((WCC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
422 serv_printf("DELE %ld", msgnum);
424 else { /* Otherwise move it to Trash */
425 serv_printf("MOVE %ld|_TRASH_|0", msgnum);
427 StrBuf_ServGetln(CmdBuf);
428 GetServerStatusMsg(CmdBuf, NULL, 1, 0);
438 * Printable view of a message
440 void print_message(void) {
444 msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
445 output_headers(0, 0, 0, 0, 0, 0);
447 hprintf("Content-type: text/html\r\n"
448 "Server: " PACKAGE_STRING "\r\n"
449 "Connection: close\r\n");
453 read_message(WC->WBuf, HKEY("view_message_print"), msgnum, NULL, &Mime);
459 * Display a message's headers
461 void display_headers(void) {
465 msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
466 output_headers(0, 0, 0, 0, 0, 0);
468 hprintf("Content-type: text/plain\r\n"
470 "Connection: close\r\n",
474 serv_printf("MSG2 %ld|3", msgnum);
475 serv_getln(buf, sizeof buf);
477 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
478 wc_printf("%s\n", buf);
486 message_summary *ReadOneMessageSummary(StrBuf *RawMessage, const char *DefaultSubject, long MsgNum)
489 MsgPartEvaluatorStruct *Eval;
490 message_summary *Msg;
499 serv_printf("MSG0 %ld|1", MsgNum); /* ask for headers only */
501 StrBuf_ServGetln(Buf);
502 if (GetServerStatus(Buf, NULL) == 1) {
507 Msg = (message_summary*)malloc(sizeof(message_summary));
508 memset(Msg, 0, sizeof(message_summary));
509 while (len = StrBuf_ServGetln(Buf),
512 strcmp(ChrPtr(Buf), "000")))
515 ebuf = strchr(ChrPtr(Buf), '=');
517 if (GetHash(MsgEvaluators, buf, nBuf, &vEval) && vEval != NULL) {
518 Eval = (MsgPartEvaluatorStruct*) vEval;
519 StrBufCutLeft(Buf, nBuf + 1);
522 else syslog(1, "Don't know how to handle Message Headerline [%s]", ChrPtr(Buf));
532 * load message pointers from the server for a "read messages" operation
534 * servcmd: the citadel command to send to the citserver
536 int load_msg_ptrs(const char *servcmd,
538 SharedMessageStatus *Stat,
539 load_msg_ptrs_detailheaders LH)
542 message_summary *Msg;
547 const char *Ptr = NULL;
550 Stat->lowest_found = LONG_MAX;
551 Stat->highest_found = LONG_MIN;
553 if (WCC->summ != NULL) {
554 DeleteHash(&WCC->summ);
556 WCC->summ = NewHash(1, Flathash);
560 StrBuf_ServGetln(Buf);
561 StatMajor = GetServerStatus(Buf, NULL);
566 if (filter != NULL) {
571 /* fall back to empty filter in case of we were fooled... */
577 return (Stat->nummsgs);
580 while (len = StrBuf_ServGetln(Buf),
583 strcmp(ChrPtr(Buf), "000")!= 0)))
585 if (Stat->nummsgs < Stat->maxload) {
588 Msg = (message_summary*)malloc(sizeof(message_summary));
589 memset(Msg, 0, sizeof(message_summary));
591 Msg->msgnum = StrBufExtractNext_long(Buf, &Ptr, '|');
592 Msg->date = StrBufExtractNext_long(Buf, &Ptr, '|');
594 if (Stat->nummsgs == 0) {
595 if (Msg->msgnum < Stat->lowest_found) {
596 Stat->lowest_found = Msg->msgnum;
598 if (Msg->msgnum > Stat->highest_found) {
599 Stat->highest_found = Msg->msgnum;
603 if ((Msg->msgnum == 0) && (StrLength(Buf) < 32)) {
609 * as citserver probably gives us messages in forward date sorting
610 * nummsgs should be the same order as the message date.
612 if (Msg->date == 0) {
613 Msg->date = Stat->nummsgs;
614 if (StrLength(Buf) < 32)
617 if ((!skipit) && (LH != NULL)) {
618 if (!LH(Buf, &Ptr, Msg, Buf2)){
624 Put(WCC->summ, (const char *)&n, sizeof(n), Msg, DestroyMessageSummary);
630 return (Stat->nummsgs);
636 * \brief sets FlagToSet for each of ScanMe that appears in MatchMSet
637 * \param ScanMe List of BasicMsgStruct to be searched it MatchSet
638 * \param MatchMSet MSet we want to flag
639 * \param FlagToSet Flag to set on each BasicMsgStruct->Flags if in MSet
641 void SetFlagsFromMSet(HashList *ScanMe, MSet *MatchMSet, int FlagToSet, int Reverse)
647 message_summary *Msg;
649 at = GetNewHashPos(ScanMe, 0);
650 while (GetNextHashPos(ScanMe, at, &HKLen, &HashKey, &vMsg)) {
651 /* Are you a new message, or an old message? */
652 Msg = (message_summary*) vMsg;
653 if (Reverse && IsInMSetList(MatchMSet, Msg->msgnum)) {
654 Msg->Flags = Msg->Flags | FlagToSet;
656 else if (!Reverse && !IsInMSetList(MatchMSet, Msg->msgnum)) {
657 Msg->Flags = Msg->Flags | FlagToSet;
664 void load_seen_flags(void)
670 OldMsg = NewStrBuf();
672 StrBuf_ServGetln(OldMsg);
673 if (GetServerStatus(OldMsg, NULL) == 2) {
674 StrBufCutLeft(OldMsg, 4);
681 if (ParseMSet(&MatchMSet, OldMsg))
683 SetFlagsFromMSet(WCC->summ, MatchMSet, MSGFLAG_READ, 0);
685 DeleteMSet(&MatchMSet);
689 extern readloop_struct rlid[];
691 typedef struct _RoomRenderer{
694 GetParamsGetServerCall_func GetParamsGetServerCall;
696 PrintViewHeader_func PrintPageHeader;
697 PrintViewHeader_func PrintViewHeader;
698 LoadMsgFromServer_func LoadMsgFromServer;
699 RenderView_or_Tail_func RenderView_or_Tail;
700 View_Cleanup_func ViewCleanup;
701 load_msg_ptrs_detailheaders LHParse;
706 * command loop for reading messages
708 * Set oper to "readnew" or "readold" or "readfwd" or "headers" or "readgt" or "readlt" or "do_search"
710 void readloop(long oper, eCustomRoomRenderer ForceRenderer)
712 RoomRenderer *ViewMsg;
715 message_summary *Msg;
717 char filter[256] = "";
723 WCTemplputParams SubTP;
724 SharedMessageStatus Stat;
727 if (havebstr("is_summary") && (1 == (ibstr("is_summary")))) {
728 WCC->CurRoom.view = VIEW_MAILBOX;
731 if (havebstr("view")) {
732 WCC->CurRoom.view = ibstr("view");
735 memset(&Stat, 0, sizeof(SharedMessageStatus));
736 Stat.maxload = 10000;
737 Stat.lowest_found = (-1);
738 Stat.highest_found = (-1);
739 if (ForceRenderer == eUseDefault)
740 GetHash(ReadLoopHandler, IKEY(WCC->CurRoom.view), &vViewMsg);
742 GetHash(ReadLoopHandler, IKEY(ForceRenderer), &vViewMsg);
743 if (vViewMsg == NULL) {
744 WCC->CurRoom.view = VIEW_BBS;
745 GetHash(ReadLoopHandler, IKEY(WCC->CurRoom.view), &vViewMsg);
747 if (vViewMsg == NULL) {
748 return; /* TODO: print message */
751 ViewMsg = (RoomRenderer*) vViewMsg;
752 if (ViewMsg->PrintPageHeader == NULL)
753 output_headers(1, 1, 1, 0, 0, 0);
755 ViewMsg->PrintPageHeader(&Stat, ViewSpecific);
757 if (ViewMsg->GetParamsGetServerCall != NULL) {
758 r = ViewMsg->GetParamsGetServerCall(
763 filter, sizeof(filter)
775 case 300: /* the callback hook should do the work for us here, since he knows what to do. */
781 if (!IsEmptyStr(cmd)) {
782 const char *p = NULL;
783 if (!IsEmptyStr(filter))
785 Stat.nummsgs = load_msg_ptrs(cmd, p, &Stat, ViewMsg->LHParse);
790 StackContext(NULL, &SubTP, NULL, CTX_MAILSUM, 0, NULL);
792 SortIt = RetrieveSort(&SubTP,
795 Stat.defaultsortorder);
797 UnStackContext(&SubTP);
799 SortByPayload(WCC->summ, SortIt);
801 if (Stat.startmsg < 0) {
805 if (Stat.load_seen) load_seen_flags();
808 * Print any inforation above the message list...
810 if (ViewMsg->PrintViewHeader != NULL)
811 ViewMsg->PrintViewHeader(&Stat, &ViewSpecific);
813 WCC->startmsg = Stat.startmsg;
814 WCC->maxmsgs = Stat.maxmsgs;
815 WCC->num_displayed = 0;
817 /* Put some helpful data in vars for mailsummary_json */
822 StrBufPrintf(Foo, "%ld", Stat.nummsgs);
823 PutBstr(HKEY("__READLOOP:TOTALMSGS"), NewStrBufDup(Foo));
824 StrBufPrintf(Foo, "%ld", Stat.startmsg);
825 PutBstr(HKEY("__READLOOP:STARTMSG"), Foo);
829 * iterate over each message. if we need to load an attachment, do it here.
832 if ((ViewMsg->LoadMsgFromServer != NULL) &&
835 at = GetNewHashPos(WCC->summ, 0);
836 Stat.num_displayed = i = 0;
837 while ( GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
838 Msg = (message_summary*) vMsg;
839 if ((Msg->msgnum >= Stat.startmsg) && (Stat.num_displayed <= Stat.maxmsgs)) {
840 ViewMsg->LoadMsgFromServer(&Stat,
843 (Msg->Flags & MSGFLAG_READ) != 0,
852 * Done iterating the message list. now tasks we want to do after.
854 if (ViewMsg->RenderView_or_Tail != NULL)
855 ViewMsg->RenderView_or_Tail(&Stat, &ViewSpecific, oper);
857 if (ViewMsg->ViewCleanup != NULL)
858 ViewMsg->ViewCleanup(&ViewSpecific);
862 if (WCC->summ != NULL) {
863 DeleteHash(&WCC->summ);
869 * Back end for post_message()
870 * ... this is where the actual message gets transmitted to the server.
872 void post_mime_to_server(void) {
874 char top_boundary[SIZ];
875 char alt_boundary[SIZ];
876 int is_multipart = 0;
878 wc_mime_attachment *att;
880 size_t encoded_length;
881 size_t encoded_strlen;
882 char *txtmail = NULL;
883 int include_text_alt = 0; /* Set to nonzero to include multipart/alternative text/plain */
885 sprintf(top_boundary, "Citadel--Multipart--%s--%04x--%04x",
886 ChrPtr(WCC->serv_info->serv_fqdn),
890 sprintf(alt_boundary, "Citadel--Multipart--%s--%04x--%04x",
891 ChrPtr(WCC->serv_info->serv_fqdn),
896 /* RFC2045 requires this, and some clients look for it... */
897 serv_puts("MIME-Version: 1.0");
898 serv_puts("X-Mailer: " PACKAGE_STRING);
900 /* If there are attachments, we have to do multipart/mixed */
901 if (GetCount(WCC->attachments) > 0) {
905 /* Only do multipart/alternative for mailboxes. BBS and Wiki rooms don't need it. */
906 if ((WCC->CurRoom.view == VIEW_MAILBOX) ||
907 (WCC->CurRoom.view == VIEW_JSON_LIST))
909 include_text_alt = 1;
913 /* Remember, serv_printf() appends an extra newline */
914 serv_printf("Content-type: multipart/mixed; boundary=\"%s\"\n", top_boundary);
915 serv_printf("This is a multipart message in MIME format.\n");
916 serv_printf("--%s", top_boundary);
919 /* Remember, serv_printf() appends an extra newline */
920 if (include_text_alt) {
921 serv_printf("Content-type: multipart/alternative; "
922 "boundary=\"%s\"\n", alt_boundary);
923 serv_printf("This is a multipart message in MIME format.\n");
924 serv_printf("--%s", alt_boundary);
926 serv_puts("Content-type: text/plain; charset=utf-8");
927 serv_puts("Content-Transfer-Encoding: quoted-printable");
929 txtmail = html_to_ascii(bstr("msgtext"), 0, 80, 0);
930 text_to_server_qp(txtmail); /* Transmit message in quoted-printable encoding */
933 serv_printf("\n--%s", alt_boundary);
936 serv_puts("Content-type: text/html; charset=utf-8");
937 serv_puts("Content-Transfer-Encoding: quoted-printable");
939 serv_puts("<html><body>\r\n");
940 text_to_server_qp(bstr("msgtext")); /* Transmit message in quoted-printable encoding */
941 serv_puts("</body></html>\r\n");
943 if (include_text_alt) {
944 serv_printf("--%s--", alt_boundary);
953 /* Add in the attachments */
954 it = GetNewHashPos(WCC->attachments, 0);
955 while (GetNextHashPos(WCC->attachments, it, &len, &Key, &vAtt)) {
956 att = (wc_mime_attachment *)vAtt;
957 if (att->length == 0)
959 encoded_length = ((att->length * 150) / 100);
960 encoded = malloc(encoded_length);
961 if (encoded == NULL) break;
962 encoded_strlen = CtdlEncodeBase64(encoded, ChrPtr(att->Data), StrLength(att->Data), 1);
964 serv_printf("--%s", top_boundary);
965 serv_printf("Content-type: %s", ChrPtr(att->ContentType));
966 serv_printf("Content-disposition: attachment; filename=\"%s\"", ChrPtr(att->FileName));
967 serv_puts("Content-transfer-encoding: base64");
969 serv_write(encoded, encoded_strlen);
974 serv_printf("--%s--", top_boundary);
983 * Post message (or don't post message)
985 * Note regarding the "dont_post" variable:
986 * A random value (actually, it's just a timestamp) is inserted as a hidden
987 * field called "postseq" when the display_enter page is generated. This
988 * value is checked when posting, using the static variable dont_post. If a
989 * user attempts to post twice using the same dont_post value, the message is
990 * discarded. This prevents the accidental double-saving of the same message
991 * if the user happens to click the browser "back" button.
993 void post_message(void)
996 StrBuf *EmailAddress;
999 StrBuf *encoded_subject = NULL;
1000 static long dont_post = (-1L);
1001 int is_anonymous = 0;
1002 const StrBuf *display_name = NULL;
1003 wcsession *WCC = WC;
1006 if (havebstr("force_room")) {
1007 gotoroom(sbstr("force_room"));
1010 if (havebstr("display_name")) {
1011 display_name = sbstr("display_name");
1012 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1013 display_name = NULL;
1018 if (!strcasecmp(bstr("submit_action"), "cancel")) {
1019 AppendImportantMessage(_("Cancelled. Message was not posted."), -1);
1020 } else if (lbstr("postseq") == dont_post) {
1021 AppendImportantMessage(
1022 _("Automatically cancelled because you have already "
1023 "saved this message."), -1);
1025 const char CMD[] = "ENT0 1|%s|%d|4|%s|%s||%s|%s|%s|%s|%s";
1026 StrBuf *Recp = NULL;
1029 char *wikipage = NULL;
1030 const StrBuf *my_email_addr = NULL;
1031 StrBuf *CmdBuf = NULL;
1032 StrBuf *references = NULL;
1033 int saving_to_drafts = 0;
1036 saving_to_drafts = !strcasecmp(bstr("submit_action"), "draft");
1039 if (saving_to_drafts) {
1040 /* temporarily change to the drafts room */
1041 serv_puts("GOTO _DRAFTS_");
1042 StrBuf_ServGetln(Buf);
1043 if (GetServerStatusMsg(Buf, NULL, 1, 2) != 2) {
1044 /* You probably don't even have a dumb Drafts folder */
1045 syslog(9, "%s:%d: server save to drafts error: %s\n", __FILE__, __LINE__, ChrPtr(Buf) + 4);
1046 AppendImportantMessage(_("Saved to Drafts failed: "), -1);
1053 if (havebstr("references"))
1055 const StrBuf *ref = sbstr("references");
1056 references = NewStrBufDup(ref);
1057 if (*ChrPtr(references) == '|') { /* remove leading '|' if present */
1058 StrBufCutLeft(references, 1);
1060 StrBufReplaceChars(references, '|', '!');
1062 if (havebstr("subject")) {
1065 * make enough room for the encoded string;
1066 * plus the QP header
1068 Subj = sbstr("subject");
1070 StrBufRFC2047encode(&encoded_subject, Subj);
1072 UserName = NewStrBuf();
1073 EmailAddress = NewStrBuf();
1074 EncBuf = NewStrBuf();
1076 Recp = StrBufSanitizeEmailRecipientVector(sbstr("recp"), UserName, EmailAddress, EncBuf);
1077 Cc = StrBufSanitizeEmailRecipientVector(sbstr("cc"), UserName, EmailAddress, EncBuf);
1078 Bcc = StrBufSanitizeEmailRecipientVector(sbstr("bcc"), UserName, EmailAddress, EncBuf);
1080 FreeStrBuf(&UserName);
1081 FreeStrBuf(&EmailAddress);
1082 FreeStrBuf(&EncBuf);
1084 wikipage = strdup(bstr("page"));
1085 str_wiki_index(wikipage);
1086 my_email_addr = sbstr("my_email_addr");
1088 HeaderLen = StrLength(Recp) +
1089 StrLength(encoded_subject) +
1093 StrLength(my_email_addr) +
1094 StrLength(references);
1095 CmdBuf = NewStrBufPlain(NULL, sizeof (CMD) + HeaderLen);
1096 StrBufPrintf(CmdBuf,
1098 saving_to_drafts?"":ChrPtr(Recp),
1100 ChrPtr(encoded_subject),
1101 ChrPtr(display_name),
1102 saving_to_drafts?"":ChrPtr(Cc),
1103 saving_to_drafts?"":ChrPtr(Bcc),
1105 ChrPtr(my_email_addr),
1106 ChrPtr(references));
1107 FreeStrBuf(&references);
1108 FreeStrBuf(&encoded_subject);
1111 if ((HeaderLen + StrLength(sbstr("msgtext")) < 10) &&
1112 (GetCount(WCC->attachments) == 0)){
1113 AppendImportantMessage(_("Refusing to post empty message.\n"), -1);
1114 FreeStrBuf(&CmdBuf);
1119 syslog(9, "%s\n", ChrPtr(CmdBuf));
1120 serv_puts(ChrPtr(CmdBuf));
1121 FreeStrBuf(&CmdBuf);
1123 StrBuf_ServGetln(Buf);
1124 if (GetServerStatus(Buf, NULL) == 4) {
1125 if (saving_to_drafts) {
1126 if ( (havebstr("recp"))
1127 || (havebstr("cc" ))
1128 || (havebstr("bcc" )) ) {
1129 /* save recipient headers or room to post to */
1130 serv_printf("To: %s", ChrPtr(Recp));
1131 serv_printf("Cc: %s", ChrPtr(Cc));
1132 serv_printf("Bcc: %s", ChrPtr(Bcc));
1134 serv_printf("X-Citadel-Room: %s", ChrPtr(WCC->CurRoom.name));
1137 post_mime_to_server();
1138 if (saving_to_drafts) {
1139 AppendImportantMessage(_("Message has been saved to Drafts.\n"), -1);
1140 gotoroom(WCC->CurRoom.name);
1142 readloop(readnew, eUseDefault);
1145 } else if ( (havebstr("recp"))
1146 || (havebstr("cc" ))
1147 || (havebstr("bcc" ))
1149 AppendImportantMessage(_("Message has been sent.\n"), -1);
1152 AppendImportantMessage(_("Message has been posted.\n"), -1);
1154 dont_post = lbstr("postseq");
1156 syslog(9, "%s:%d: server post error: %s", __FILE__, __LINE__, ChrPtr(Buf) + 4);
1157 AppendImportantMessage(ChrPtr(Buf) + 4, StrLength(Buf) - 4);
1159 if (saving_to_drafts) gotoroom(WCC->CurRoom.name);
1173 DeleteHash(&WCC->attachments);
1176 * We may have been supplied with instructions regarding the location
1177 * to which we must return after posting. If found, go there.
1179 if (havebstr("return_to")) {
1180 http_redirect(bstr("return_to"));
1183 * If we were editing a page in a wiki room, go to that page now.
1185 else if (havebstr("page")) {
1186 snprintf(buf, sizeof buf, "wiki?page=%s", bstr("page"));
1190 * Otherwise, just go to the "read messages" loop.
1194 readloop(readnew, eUseDefault);
1200 * Client is uploading an attachment
1202 void upload_attachment(void) {
1203 wcsession *WCC = WC;
1209 wc_mime_attachment *att;
1211 syslog(9, "upload_attachment()\n");
1212 wc_printf("upload_attachment()<br>\n");
1214 if (WCC->upload_length <= 0) {
1215 syslog(9, "ERROR no attachment was uploaded\n");
1216 wc_printf("ERROR no attachment was uploaded<br>\n");
1220 syslog(9, "Client is uploading %d bytes\n", WCC->upload_length);
1221 wc_printf("Client is uploading %d bytes<br>\n", WCC->upload_length);
1222 att = malloc(sizeof(wc_mime_attachment));
1223 memset(att, 0, sizeof(wc_mime_attachment ));
1224 att->length = WCC->upload_length;
1225 att->ContentType = NewStrBufPlain(WCC->upload_content_type, -1);
1226 att->FileName = NewStrBufDup(WCC->upload_filename);
1228 if (WCC->attachments == NULL) {
1229 WCC->attachments = NewHash(1, Flathash);
1232 /* And add it to the list. */
1234 if ((GetCount(WCC->attachments) > 0) &&
1235 GetHashAt(WCC->attachments,
1236 GetCount(WCC->attachments) -1,
1237 &newnlen, &newn, &v))
1238 n = *((int*) newn) + 1;
1239 Put(WCC->attachments, IKEY(n), att, DestroyMime);
1242 * Mozilla sends a simple filename, which is what we want,
1243 * but Satan's Browser sends an entire pathname. Reduce
1244 * the path to just a filename if we need to.
1246 pch = strrchr(ChrPtr(att->FileName), '/');
1248 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1250 pch = strrchr(ChrPtr(att->FileName), '\\');
1252 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1256 * Transfer control of this memory from the upload struct
1257 * to the attachment struct.
1259 att->Data = WCC->upload;
1261 WCC->upload_length = 0;
1266 * Remove an attachment from the message currently being composed.
1268 * Currently we identify the attachment to be removed by its filename.
1269 * There is probably a better way to do this.
1271 void remove_attachment(void) {
1272 wcsession *WCC = WC;
1273 wc_mime_attachment *att;
1275 StrBuf *WhichAttachment;
1280 WhichAttachment = NewStrBufDup(sbstr("which_attachment"));
1281 StrBufUnescape(WhichAttachment, 0);
1282 at = GetNewHashPos(WCC->attachments, 0);
1284 GetHashPos(WCC->attachments, at, &len, &key, &vAtt);
1286 att = (wc_mime_attachment*) vAtt;
1287 if ((att != NULL) &&
1288 (strcmp(ChrPtr(WhichAttachment),
1289 ChrPtr(att->FileName) ) == 0))
1291 DeleteEntryFromHash(WCC->attachments, at);
1295 while (NextHashPos(WCC->attachments, at));
1296 FreeStrBuf(&WhichAttachment);
1297 wc_printf("remove_attachment() completed\n");
1301 long FourHash(const char *key, long length)
1305 const unsigned char *ptr = (const unsigned char*)key;
1307 for (i = 0; i < 4; i++, ptr ++)
1327 const char *ReplyToModeStrings [3] = {
1332 typedef enum _eReplyToNodes {
1339 * display the message entry screen
1341 void display_enter(void)
1343 const char *ReplyingModeStr;
1344 eReplyToNodes ReplyMode = eReply;
1348 const StrBuf *display_name = NULL;
1349 int recipient_required = 0;
1350 int subject_required = 0;
1351 int is_anonymous = 0;
1352 wcsession *WCC = WC;
1356 if (havebstr("force_room")) {
1357 gotoroom(sbstr("force_room"));
1360 display_name = sbstr("display_name");
1361 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1362 display_name = NULL;
1367 * First, do we have permission to enter messages in this room at all?
1370 serv_puts("ENT0 0");
1371 StrBuf_ServGetln(Line);
1372 rc = GetServerStatusMsg(Line, &Result, 0, 2);
1374 if (Result == 570) { /* 570 means that we need a recipient here */
1375 recipient_required = 1;
1377 else if (rc != 2) { /* Any other error means that we cannot continue */
1378 rc = GetServerStatusMsg(Line, &Result, 0, 2);
1380 readloop(readnew, eUseDefault);
1385 /* Is the server strongly recommending that the user enter a message subject? */
1386 if (StrLength(Line) > 4) {
1387 subject_required = extract_int(ChrPtr(Line) + 4, 1);
1391 * Are we perhaps in an address book view? If so, then an "enter
1392 * message" command really means "add new entry."
1394 if (WCC->CurRoom.defview == VIEW_ADDRESSBOOK) {
1395 do_edit_vcard(-1, "", NULL, NULL, "", ChrPtr(WCC->CurRoom.name));
1401 * Are we perhaps in a calendar room? If so, then an "enter
1402 * message" command really means "add new calendar item."
1404 if (WCC->CurRoom.defview == VIEW_CALENDAR) {
1405 display_edit_event();
1411 * Are we perhaps in a tasks view? If so, then an "enter
1412 * message" command really means "add new task."
1414 if (WCC->CurRoom.defview == VIEW_TASKS) {
1415 display_edit_task();
1421 ReplyingModeStr = bstr("replying_mode");
1422 if (ReplyingModeStr != NULL) for (i = 0; i < 3; i++) {
1423 if (strcmp(ReplyingModeStr, ReplyToModeStrings[i]) == 0) {
1424 ReplyMode = (eReplyToNodes) i;
1431 * If the "replying_to" variable is set, it refers to a message
1432 * number from which we must extract some header fields...
1434 replying_to = lbstr("replying_to");
1435 if (replying_to > 0) {
1437 StrBuf *wefw = NULL;
1438 StrBuf *msgn = NULL;
1439 StrBuf *from = NULL;
1440 StrBuf *node = NULL;
1441 StrBuf *rfca = NULL;
1442 StrBuf *rcpt = NULL;
1443 StrBuf *cccc = NULL;
1444 StrBuf *replyto = NULL;
1445 serv_printf("MSG0 %ld|1", replying_to);
1447 StrBuf_ServGetln(Line);
1448 if (GetServerStatusMsg(Line, NULL, 0, 0) == 1)
1449 while (len = StrBuf_ServGetln(Line),
1452 strcmp(ChrPtr(Line), "000")))
1455 if ((StrLength(Line) > 4) &&
1456 (ChrPtr(Line)[4] == '='))
1457 which = FourHash(ChrPtr(Line), 4);
1459 if (which == l_subj)
1461 StrBuf *subj = NewStrBuf();
1462 StrBuf *FlatSubject;
1464 if (ReplyMode == eForward) {
1465 if (strncasecmp(ChrPtr(Line) + 5, "Fw:", 3)) {
1466 StrBufAppendBufPlain(subj, HKEY("Fw: "), 0);
1470 if (strncasecmp(ChrPtr(Line) + 5, "Re:", 3)) {
1471 StrBufAppendBufPlain(subj, HKEY("Re: "), 0);
1474 StrBufAppendBufPlain(subj,
1476 StrLength(Line) - 5, 0);
1477 FlatSubject = NewStrBufPlain(NULL, StrLength(subj));
1478 StrBuf_RFC822_to_Utf8(FlatSubject, subj, NULL, NULL);
1480 PutBstr(HKEY("subject"), FlatSubject);
1483 else if (which == l_wefw)
1488 wefw = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1490 /* Trim down excessively long lists of thread references. We eliminate the
1491 * second one in the list so that the thread root remains intact.
1493 rrtok = num_tokens(ChrPtr(wefw), '|');
1494 rrlen = StrLength(wefw);
1495 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
1496 StrBufRemove_token(wefw, 1, '|');
1500 else if (which == l_msgn) {
1501 msgn = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1504 else if (which == l_from) {
1506 from = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1507 FlatFrom = NewStrBufPlain(NULL, StrLength(from));
1508 StrBuf_RFC822_to_Utf8(FlatFrom, from, NULL, NULL);
1511 for (i=0; i<StrLength(from); ++i) {
1512 if (ChrPtr(from)[i] == ',')
1513 StrBufPeek(from, NULL, i, ' ');
1517 else if (which == l_rcpt) {
1518 rcpt = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1521 else if (which == l_cccc) {
1522 cccc = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1525 else if (which == l_node) {
1526 node = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1528 else if (which == l_replyto) {
1529 replyto = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1531 else if (which == l_rfca) {
1533 rfca = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1534 FlatRFCA = NewStrBufPlain(NULL, StrLength(rfca));
1535 StrBuf_RFC822_to_Utf8(FlatRFCA, rfca, NULL, NULL);
1542 if (StrLength(wefw) + StrLength(msgn) > 0) {
1543 StrBuf *refs = NewStrBuf();
1544 if (StrLength(wefw) > 0) {
1545 StrBufAppendBuf(refs, wefw, 0);
1547 if ( (StrLength(wefw) > 0) &&
1548 (StrLength(msgn) > 0) )
1550 StrBufAppendBufPlain(refs, HKEY("|"), 0);
1552 if (StrLength(msgn) > 0) {
1553 StrBufAppendBuf(refs, msgn, 0);
1555 PutBstr(HKEY("references"), refs);
1559 * If this is a Reply or a ReplyAll, copy the sender's email into the To: field
1561 if ((ReplyMode == eReply) || (ReplyMode == eReplyAll))
1564 if ((StrLength(replyto) > 0) && (ReplyMode == eReplyAll)) {
1565 to_rcpt = NewStrBuf();
1566 StrBufAppendBuf(to_rcpt, replyto, 0);
1568 else if (StrLength(rfca) > 0) {
1569 to_rcpt = NewStrBuf();
1570 StrBufAppendBuf(to_rcpt, from, 0);
1571 StrBufAppendBufPlain(to_rcpt, HKEY(" <"), 0);
1572 StrBufAppendBuf(to_rcpt, rfca, 0);
1573 StrBufAppendBufPlain(to_rcpt, HKEY(">"), 0);
1578 if ( (StrLength(node) > 0)
1579 && (strcasecmp(ChrPtr(node), ChrPtr(WCC->serv_info->serv_nodename)))
1581 StrBufAppendBufPlain(to_rcpt, HKEY(" @ "), 0);
1582 StrBufAppendBuf(to_rcpt, node, 0);
1585 PutBstr(HKEY("recp"), to_rcpt);
1589 * Only if this is a ReplyAll, copy all recipients into the Cc: field
1591 if (ReplyMode == eReplyAll)
1593 StrBuf *cc_rcpt = rcpt;
1595 if ((StrLength(cccc) > 0) && (StrLength(replyto) == 0))
1597 if (cc_rcpt != NULL) {
1598 StrBufAppendPrintf(cc_rcpt, ", ");
1599 StrBufAppendBuf(cc_rcpt, cccc, 0);
1605 if (cc_rcpt != NULL)
1606 PutBstr(HKEY("cc"), cc_rcpt);
1618 * Otherwise proceed normally.
1619 * Do a custom room banner with no navbar...
1622 if (recipient_required) {
1623 const StrBuf *Recp = NULL;
1624 const StrBuf *Cc = NULL;
1625 const StrBuf *Bcc = NULL;
1626 char *wikipage = NULL;
1627 StrBuf *CmdBuf = NULL;
1628 const char CMD[] = "ENT0 0|%s|%d|0||%s||%s|%s|%s";
1630 Recp = sbstr("recp");
1633 wikipage = strdup(bstr("page"));
1634 str_wiki_index(wikipage);
1636 CmdBuf = NewStrBufPlain(NULL,
1639 StrLength(display_name) +
1644 StrBufPrintf(CmdBuf,
1648 ChrPtr(display_name),
1653 serv_puts(ChrPtr(CmdBuf));
1654 StrBuf_ServGetln(CmdBuf);
1657 rc = GetServerStatusMsg(CmdBuf, &Result, 0, 0);
1659 if ( (Result == 570) /* invalid or missing recipient(s) */
1660 || (Result == 550) /* higher access required to send Internet mail */
1662 /* These errors will have been displayed and are excusable */
1664 else if (rc != 2) { /* Any other error means that we cannot continue */
1665 AppendImportantMessage(ChrPtr(CmdBuf) + 4, StrLength(CmdBuf) - 4);
1666 FreeStrBuf(&CmdBuf);
1668 readloop(readnew, eUseDefault);
1671 FreeStrBuf(&CmdBuf);
1673 if (recipient_required)
1674 PutBstr(HKEY("__RCPTREQUIRED"), NewStrBufPlain(HKEY("1")));
1675 if (recipient_required || subject_required)
1676 PutBstr(HKEY("__SUBJREQUIRED"), NewStrBufPlain(HKEY("1")));
1679 output_headers(1, 0, 0, 0, 1, 0);
1680 DoTemplate(HKEY("edit_message"), NULL, &NoCtx);
1689 void delete_msg(void)
1694 msgid = lbstr("msgid");
1696 if ((WC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
1697 serv_printf("DELE %ld", msgid);
1699 else { /* Otherwise move it to Trash */
1700 serv_printf("MOVE %ld|_TRASH_|0", msgid);
1703 StrBuf_ServGetln(Line);
1704 GetServerStatusMsg(Line, NULL, 1, 0);
1708 readloop(readnew, eUseDefault);
1713 * move a message to another room
1719 msgid = lbstr("msgid");
1721 if (havebstr("move_button")) {
1723 serv_printf("MOVE %ld|%s", msgid, bstr("target_room"));
1725 StrBuf_ServGetln(Line);
1726 GetServerStatusMsg(Line, NULL, 1, 0);
1729 AppendImportantMessage(_("The message was not moved."), -1);
1733 readloop(readnew, eUseDefault);
1739 * Generic function to output an arbitrary MIME attachment from
1740 * message being composed
1742 * partnum The MIME part to be output
1743 * filename Fake filename to give
1744 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1746 void postpart(StrBuf *partnum, StrBuf *filename, int force_download)
1749 StrBuf *content_type;
1750 wc_mime_attachment *part;
1753 i = StrToi(partnum);
1754 if (GetHash(WC->attachments, IKEY(i), &vPart) &&
1756 part = (wc_mime_attachment*) vPart;
1757 if (force_download) {
1758 content_type = NewStrBufPlain(HKEY("application/octet-stream"));
1761 content_type = NewStrBufDup(part->ContentType);
1763 StrBufAppendBuf(WC->WBuf, part->Data, 0);
1764 http_transmit_thing(ChrPtr(content_type), 0);
1766 hprintf("HTTP/1.1 404 %s\n", ChrPtr(partnum));
1767 output_headers(0, 0, 0, 0, 0, 0);
1768 hprintf("Content-Type: text/plain\r\n");
1770 wc_printf(_("An error occurred while retrieving this part: %s/%s\n"),
1771 ChrPtr(partnum), ChrPtr(filename));
1774 FreeStrBuf(&content_type);
1779 * Generic function to output an arbitrary MIME part from an arbitrary
1780 * message number on the server.
1782 * msgnum Number of the item on the citadel server
1783 * partnum The MIME part to be output
1784 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1786 void mimepart(int force_download)
1791 wcsession *WCC = WC;
1794 StrBuf *ContentType = NewStrBufPlain(HKEY("application/octet-stream"));
1797 att = Buf = NewStrBuf();
1798 msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
1799 StrBufExtract_token(att, WCC->Hdr->HR.ReqLine, 1, '/');
1801 serv_printf("OPNA %ld|%s", msgnum, ChrPtr(att));
1802 StrBuf_ServGetln(Buf);
1803 if (GetServerStatus(Buf, &ErrorDetail) == 2) {
1804 StrBufCutLeft(Buf, 4);
1805 bytes = StrBufExtract_long(Buf, 0, '|');
1806 if (!force_download) {
1807 StrBufExtract_token(ContentType, Buf, 3, '|');
1810 serv_read_binary(WCC->WBuf, bytes, Buf);
1812 StrBuf_ServGetln(Buf);
1813 CT = ChrPtr(ContentType);
1815 if (!force_download) {
1816 if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
1817 StrBufExtract_token(Buf, WCC->Hdr->HR.ReqLine, 2, '/');
1818 CT = GuessMimeByFilename(SKEY(Buf));
1820 if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
1821 CT = GuessMimeType(SKEY(WCC->WBuf));
1824 http_transmit_thing(CT, 0);
1826 StrBufCutLeft(Buf, 4);
1827 switch (ErrorDetail) {
1829 case ERROR + MESSAGE_NOT_FOUND:
1830 hprintf("HTTP/1.1 404 %s\n", ChrPtr(Buf));
1832 case ERROR + NOT_LOGGED_IN:
1833 hprintf("HTTP/1.1 401 %s\n", ChrPtr(Buf));
1836 case ERROR + HIGHER_ACCESS_REQUIRED:
1837 hprintf("HTTP/1.1 403 %s\n", ChrPtr(Buf));
1839 case ERROR + INTERNAL_ERROR:
1840 case ERROR + TOO_BIG:
1841 hprintf("HTTP/1.1 500 %s\n", ChrPtr(Buf));
1844 output_headers(0, 0, 0, 0, 0, 0);
1845 hprintf("Content-Type: text/plain\r\n");
1847 wc_printf(_("An error occurred while retrieving this part: %s\n"),
1851 FreeStrBuf(&ContentType);
1857 * Read any MIME part of a message, from the server, into memory.
1859 StrBuf *load_mimepart(long msgnum, char *partnum)
1865 serv_printf("DLAT %ld|%s", msgnum, partnum);
1866 StrBuf_ServGetln(Buf);
1867 if (GetServerStatus(Buf, NULL) == 6) {
1868 StrBufCutLeft(Buf, 4);
1869 bytes = StrBufExtract_long(Buf, 0, '|');
1872 StrBuf_ServGetBLOBBuffered(Buf, bytes);
1882 * Read any MIME part of a message, from the server, into memory.
1884 void MimeLoadData(wc_mime_attachment *Mime)
1889 /* TODO: is there a chance the content type is different from the one we know? */
1891 serv_printf("DLAT %ld|%s", Mime->msgnum, ChrPtr(Mime->PartNum));
1893 StrBuf_ServGetln(Buf);
1894 if (GetServerStatus(Buf, NULL) == 6) {
1895 Ptr = &(ChrPtr(Buf)[4]);
1896 bytes = StrBufExtractNext_long(Buf, &Ptr, '|');
1897 StrBufSkip_NTokenS(Buf, &Ptr, '|', 3); /* filename, cbtype, mimetype */
1898 if (Mime->Charset == NULL) Mime->Charset = NewStrBuf();
1899 StrBufExtract_NextToken(Mime->Charset, Buf, &Ptr, '|');
1901 if (Mime->Data == NULL)
1902 Mime->Data = NewStrBufPlain(NULL, bytes);
1903 StrBuf_ServGetBLOBBuffered(Mime->Data, bytes);
1906 FlushStrBuf(Mime->Data);
1907 /* TODO XImportant message */
1913 void view_mimepart(void) {
1917 void download_mimepart(void) {
1921 void view_postpart(void) {
1922 StrBuf *filename = NewStrBuf();
1923 StrBuf *partnum = NewStrBuf();
1925 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1926 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1928 postpart(partnum, filename, 0);
1930 FreeStrBuf(&filename);
1931 FreeStrBuf(&partnum);
1934 void download_postpart(void) {
1935 StrBuf *filename = NewStrBuf();
1936 StrBuf *partnum = NewStrBuf();
1938 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1939 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1941 postpart(partnum, filename, 1);
1943 FreeStrBuf(&filename);
1944 FreeStrBuf(&partnum);
1949 void show_num_attachments(void) {
1950 wc_printf("%d", GetCount(WC->attachments));
1954 void h_readnew(void) { readloop(readnew, eUseDefault);}
1955 void h_readold(void) { readloop(readold, eUseDefault);}
1956 void h_readfwd(void) { readloop(readfwd, eUseDefault);}
1957 void h_headers(void) { readloop(headers, eUseDefault);}
1958 void h_do_search(void) { readloop(do_search, eUseDefault);}
1959 void h_readgt(void) { readloop(readgt, eUseDefault);}
1960 void h_readlt(void) { readloop(readlt, eUseDefault);}
1964 /* Output message list in JSON format */
1965 void jsonMessageList(void) {
1966 StrBuf *View = NewStrBuf();
1967 const StrBuf *room = sbstr("room");
1968 long oper = (havebstr("query")) ? do_search : readnew;
1969 StrBufPrintf(View, "%d", VIEW_JSON_LIST);
1970 putbstr("view", View);;
1972 readloop(oper, eUseDefault);
1975 void RegisterReadLoopHandlerset(
1977 GetParamsGetServerCall_func GetParamsGetServerCall,
1978 PrintViewHeader_func PrintPageHeader,
1979 PrintViewHeader_func PrintViewHeader,
1980 load_msg_ptrs_detailheaders LH,
1981 LoadMsgFromServer_func LoadMsgFromServer,
1982 RenderView_or_Tail_func RenderView_or_Tail,
1983 View_Cleanup_func ViewCleanup
1986 RoomRenderer *Handler;
1988 Handler = (RoomRenderer*) malloc(sizeof(RoomRenderer));
1990 Handler->RoomType = RoomType;
1991 Handler->GetParamsGetServerCall = GetParamsGetServerCall;
1992 Handler->PrintPageHeader = PrintPageHeader;
1993 Handler->PrintViewHeader = PrintViewHeader;
1994 Handler->LoadMsgFromServer = LoadMsgFromServer;
1995 Handler->RenderView_or_Tail = RenderView_or_Tail;
1996 Handler->ViewCleanup = ViewCleanup;
1997 Handler->LHParse = LH;
1999 Put(ReadLoopHandler, IKEY(RoomType), Handler, NULL);
2006 RegisterPreference("use_sig",
2007 _("Attach signature to email messages?"),
2010 RegisterPreference("signature", _("Use this signature:"), PRF_QP_STRING, NULL);
2011 RegisterPreference("default_header_charset",
2012 _("Default character set for email headers:"),
2015 RegisterPreference("defaultfrom", _("Preferred email address"), PRF_STRING, NULL);
2016 RegisterPreference("defaultname",
2017 _("Preferred display name for email messages"),
2020 RegisterPreference("defaulthandle",
2021 _("Preferred display name for bulletin board posts"),
2024 RegisterPreference("mailbox",_("Mailbox view mode"), PRF_STRING, NULL);
2026 WebcitAddUrlHandler(HKEY("readnew"), "", 0, h_readnew, ANONYMOUS|NEED_URL);
2027 WebcitAddUrlHandler(HKEY("readold"), "", 0, h_readold, ANONYMOUS|NEED_URL);
2028 WebcitAddUrlHandler(HKEY("readfwd"), "", 0, h_readfwd, ANONYMOUS|NEED_URL);
2029 WebcitAddUrlHandler(HKEY("headers"), "", 0, h_headers, NEED_URL);
2030 WebcitAddUrlHandler(HKEY("readgt"), "", 0, h_readgt, ANONYMOUS|NEED_URL);
2031 WebcitAddUrlHandler(HKEY("readlt"), "", 0, h_readlt, ANONYMOUS|NEED_URL);
2032 WebcitAddUrlHandler(HKEY("do_search"), "", 0, h_do_search, 0);
2033 WebcitAddUrlHandler(HKEY("display_enter"), "", 0, display_enter, 0);
2034 WebcitAddUrlHandler(HKEY("post"), "", 0, post_message, PROHIBIT_STARTPAGE);
2035 WebcitAddUrlHandler(HKEY("move_msg"), "", 0, move_msg, PROHIBIT_STARTPAGE);
2036 WebcitAddUrlHandler(HKEY("delete_msg"), "", 0, delete_msg, PROHIBIT_STARTPAGE);
2037 WebcitAddUrlHandler(HKEY("msg"), "", 0, embed_message, NEED_URL);
2038 WebcitAddUrlHandler(HKEY("message"), "", 0, handle_one_message, NEED_URL|XHTTP_COMMANDS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
2039 WebcitAddUrlHandler(HKEY("printmsg"), "", 0, print_message, NEED_URL);
2040 WebcitAddUrlHandler(HKEY("msgheaders"), "", 0, display_headers, NEED_URL);
2042 WebcitAddUrlHandler(HKEY("mimepart"), "", 0, view_mimepart, NEED_URL);
2043 WebcitAddUrlHandler(HKEY("mimepart_download"), "", 0, download_mimepart, NEED_URL);
2044 WebcitAddUrlHandler(HKEY("postpart"), "", 0, view_postpart, NEED_URL|PROHIBIT_STARTPAGE);
2045 WebcitAddUrlHandler(HKEY("postpart_download"), "", 0, download_postpart, NEED_URL|PROHIBIT_STARTPAGE);
2046 WebcitAddUrlHandler(HKEY("upload_attachment"), "", 0, upload_attachment, AJAX);
2047 WebcitAddUrlHandler(HKEY("remove_attachment"), "", 0, remove_attachment, AJAX);
2048 WebcitAddUrlHandler(HKEY("show_num_attachments"), "", 0, show_num_attachments, AJAX);
2051 WebcitAddUrlHandler(HKEY("roommsgs"), "", 0, jsonMessageList,0);
2053 l_subj = FourHash("subj", 4);
2054 l_wefw = FourHash("wefw", 4);
2055 l_msgn = FourHash("msgn", 4);
2056 l_from = FourHash("from", 4);
2057 l_rcpt = FourHash("rcpt", 4);
2058 l_cccc = FourHash("cccc", 4);
2059 l_replyto = FourHash("rep2", 4);
2060 l_node = FourHash("node", 4);
2061 l_rfca = FourHash("rfca", 4);
2067 SessionDetachModule_MSG
2070 DeleteHash(&sess->summ);