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 memset(&SubTP, 0, sizeof(WCTemplputParams));
242 SubTP.Filter.ContextType = CTX_MAILSUM;
244 *OutMime = DoTemplate(tmpl, tmpllen, Target, &SubTP);
246 DestroyMessageSummary(Msg);
247 FreeStrBuf(&FoundCharset);
254 HttpStatus(long CitadelStatus)
256 long httpstatus = 502;
258 switch (MAJORCODE(CitadelStatus))
260 case LISTING_FOLLOWS:
265 switch (MINORCODE(CitadelStatus))
273 case HIGHER_ACCESS_REQUIRED:
274 case MAX_SESSIONS_EXCEEDED:
276 case RESOURCE_NOT_OPEN:
278 case INVALID_FLOOR_OPERATION:
284 case MESSAGE_NOT_FOUND:
294 case CMD_NOT_SUPPORTED:
295 case PASSWORD_REQUIRED:
296 case ALREADY_LOGGED_IN:
297 case USERNAME_REQUIRED:
299 case SERVER_SHUTTING_DOWN:
310 case START_CHAT_MODE:
314 httpstatus = 502; /* aeh... whut? */
322 * Unadorned HTML output of an individual message, suitable
323 * for placing in a hidden iframe, for printing, or whatever
325 void handle_one_message(void)
329 const StrBuf *Destination;
335 StrBuf *CmdBuf = NULL;
339 pMsg = strchr(ChrPtr(WCC->Hdr->HR.ReqLine), '/');
341 HttpStatus(CitStatus);
345 msgnum = atol(pMsg + 1);
346 StrBufCutAt(WCC->Hdr->HR.ReqLine, 0, pMsg);
347 gotoroom(WCC->Hdr->HR.ReqLine);
348 switch (WCC->Hdr->HR.eReqType)
352 Tmpl = sbstr("template");
353 if (StrLength(Tmpl) > 0)
354 read_message(WCC->WBuf, SKEY(Tmpl), msgnum, NULL, &Mime);
356 read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
357 http_transmit_thing(ChrPtr(Mime), 0);
360 CmdBuf = NewStrBuf ();
361 if ((WCC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
362 serv_printf("DELE %ld", msgnum);
364 else { /* Otherwise move it to Trash */
365 serv_printf("MOVE %ld|_TRASH_|0", msgnum);
367 StrBuf_ServGetln(CmdBuf);
368 GetServerStatusMsg(CmdBuf, &CitStatus, 1, 0);
369 HttpStatus(CitStatus);
374 if (GetHash(WCC->Hdr->HTTPHeaders, HKEY("DESTINATION"), &vLine) &&
376 Destination = (StrBuf*) vLine;
377 serv_printf("MOVE %ld|%s|%d", msgnum, ChrPtr(Destination), CopyMessage);
378 StrBuf_ServGetln(CmdBuf);
379 GetServerStatusMsg(CmdBuf, NULL, 1, 0);
380 GetServerStatus(CmdBuf, &CitStatus);
381 HttpStatus(CitStatus);
394 * Unadorned HTML output of an individual message, suitable
395 * for placing in a hidden iframe, for printing, or whatever
397 void embed_message(void) {
402 StrBuf *CmdBuf = NULL;
404 msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
405 if (msgnum <= 0) return;
407 switch (WCC->Hdr->HR.eReqType)
411 Tmpl = sbstr("template");
412 if (StrLength(Tmpl) > 0)
413 read_message(WCC->WBuf, SKEY(Tmpl), msgnum, NULL, &Mime);
415 read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
416 http_transmit_thing(ChrPtr(Mime), 0);
419 CmdBuf = NewStrBuf ();
420 if ((WCC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
421 serv_printf("DELE %ld", msgnum);
423 else { /* Otherwise move it to Trash */
424 serv_printf("MOVE %ld|_TRASH_|0", msgnum);
426 StrBuf_ServGetln(CmdBuf);
427 GetServerStatusMsg(CmdBuf, NULL, 1, 0);
437 * Printable view of a message
439 void print_message(void) {
443 msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
444 output_headers(0, 0, 0, 0, 0, 0);
446 hprintf("Content-type: text/html\r\n"
447 "Server: " PACKAGE_STRING "\r\n"
448 "Connection: close\r\n");
452 read_message(WC->WBuf, HKEY("view_message_print"), msgnum, NULL, &Mime);
458 * Display a message's headers
460 void display_headers(void) {
464 msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
465 output_headers(0, 0, 0, 0, 0, 0);
467 hprintf("Content-type: text/plain\r\n"
469 "Connection: close\r\n",
473 serv_printf("MSG2 %ld|3", msgnum);
474 serv_getln(buf, sizeof buf);
476 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
477 wc_printf("%s\n", buf);
485 message_summary *ReadOneMessageSummary(StrBuf *RawMessage, const char *DefaultSubject, long MsgNum)
488 MsgPartEvaluatorStruct *Eval;
489 message_summary *Msg;
498 serv_printf("MSG0 %ld|1", MsgNum); /* ask for headers only */
500 StrBuf_ServGetln(Buf);
501 if (GetServerStatus(Buf, NULL) == 1) {
506 Msg = (message_summary*)malloc(sizeof(message_summary));
507 memset(Msg, 0, sizeof(message_summary));
508 while (len = StrBuf_ServGetln(Buf),
511 strcmp(ChrPtr(Buf), "000")))
514 ebuf = strchr(ChrPtr(Buf), '=');
516 if (GetHash(MsgEvaluators, buf, nBuf, &vEval) && vEval != NULL) {
517 Eval = (MsgPartEvaluatorStruct*) vEval;
518 StrBufCutLeft(Buf, nBuf + 1);
521 else syslog(1, "Don't know how to handle Message Headerline [%s]", ChrPtr(Buf));
531 * load message pointers from the server for a "read messages" operation
533 * servcmd: the citadel command to send to the citserver
535 int load_msg_ptrs(const char *servcmd,
537 SharedMessageStatus *Stat,
538 load_msg_ptrs_detailheaders LH)
541 message_summary *Msg;
546 const char *Ptr = NULL;
549 Stat->lowest_found = LONG_MAX;
550 Stat->highest_found = LONG_MIN;
552 if (WCC->summ != NULL) {
553 DeleteHash(&WCC->summ);
555 WCC->summ = NewHash(1, Flathash);
559 StrBuf_ServGetln(Buf);
560 StatMajor = GetServerStatus(Buf, NULL);
565 if (filter != NULL) {
570 /* fall back to empty filter in case of we were fooled... */
576 return (Stat->nummsgs);
579 while (len = StrBuf_ServGetln(Buf),
582 strcmp(ChrPtr(Buf), "000")!= 0)))
584 if (Stat->nummsgs < Stat->maxload) {
587 Msg = (message_summary*)malloc(sizeof(message_summary));
588 memset(Msg, 0, sizeof(message_summary));
590 Msg->msgnum = StrBufExtractNext_long(Buf, &Ptr, '|');
591 Msg->date = StrBufExtractNext_long(Buf, &Ptr, '|');
593 if (Stat->nummsgs == 0) {
594 if (Msg->msgnum < Stat->lowest_found) {
595 Stat->lowest_found = Msg->msgnum;
597 if (Msg->msgnum > Stat->highest_found) {
598 Stat->highest_found = Msg->msgnum;
602 if ((Msg->msgnum == 0) && (StrLength(Buf) < 32)) {
608 * as citserver probably gives us messages in forward date sorting
609 * nummsgs should be the same order as the message date.
611 if (Msg->date == 0) {
612 Msg->date = Stat->nummsgs;
613 if (StrLength(Buf) < 32)
616 if ((!skipit) && (LH != NULL)) {
617 if (!LH(Buf, &Ptr, Msg, Buf2)){
623 Put(WCC->summ, (const char *)&n, sizeof(n), Msg, DestroyMessageSummary);
629 return (Stat->nummsgs);
635 * \brief sets FlagToSet for each of ScanMe that appears in MatchMSet
636 * \param ScanMe List of BasicMsgStruct to be searched it MatchSet
637 * \param MatchMSet MSet we want to flag
638 * \param FlagToSet Flag to set on each BasicMsgStruct->Flags if in MSet
640 void SetFlagsFromMSet(HashList *ScanMe, MSet *MatchMSet, int FlagToSet, int Reverse)
646 message_summary *Msg;
648 at = GetNewHashPos(ScanMe, 0);
649 while (GetNextHashPos(ScanMe, at, &HKLen, &HashKey, &vMsg)) {
650 /* Are you a new message, or an old message? */
651 Msg = (message_summary*) vMsg;
652 if (Reverse && IsInMSetList(MatchMSet, Msg->msgnum)) {
653 Msg->Flags = Msg->Flags | FlagToSet;
655 else if (!Reverse && !IsInMSetList(MatchMSet, Msg->msgnum)) {
656 Msg->Flags = Msg->Flags | FlagToSet;
663 void load_seen_flags(void)
669 OldMsg = NewStrBuf();
671 StrBuf_ServGetln(OldMsg);
672 if (GetServerStatus(OldMsg, NULL) == 2) {
673 StrBufCutLeft(OldMsg, 4);
680 if (ParseMSet(&MatchMSet, OldMsg))
682 SetFlagsFromMSet(WCC->summ, MatchMSet, MSGFLAG_READ, 0);
684 DeleteMSet(&MatchMSet);
688 extern readloop_struct rlid[];
690 typedef struct _RoomRenderer{
693 GetParamsGetServerCall_func GetParamsGetServerCall;
695 PrintViewHeader_func PrintPageHeader;
696 PrintViewHeader_func PrintViewHeader;
697 LoadMsgFromServer_func LoadMsgFromServer;
698 RenderView_or_Tail_func RenderView_or_Tail;
699 View_Cleanup_func ViewCleanup;
700 load_msg_ptrs_detailheaders LHParse;
705 * command loop for reading messages
707 * Set oper to "readnew" or "readold" or "readfwd" or "headers" or "readgt" or "readlt" or "do_search"
709 void readloop(long oper, eCustomRoomRenderer ForceRenderer)
711 RoomRenderer *ViewMsg;
714 message_summary *Msg;
716 char filter[256] = "";
722 WCTemplputParams SubTP;
723 SharedMessageStatus Stat;
726 if (havebstr("is_summary") && (1 == (ibstr("is_summary")))) {
727 WCC->CurRoom.view = VIEW_MAILBOX;
730 if (havebstr("view")) {
731 WCC->CurRoom.view = ibstr("view");
734 memset(&Stat, 0, sizeof(SharedMessageStatus));
735 Stat.maxload = 10000;
736 Stat.lowest_found = (-1);
737 Stat.highest_found = (-1);
738 if (ForceRenderer == eUseDefault)
739 GetHash(ReadLoopHandler, IKEY(WCC->CurRoom.view), &vViewMsg);
741 GetHash(ReadLoopHandler, IKEY(ForceRenderer), &vViewMsg);
742 if (vViewMsg == NULL) {
743 WCC->CurRoom.view = VIEW_BBS;
744 GetHash(ReadLoopHandler, IKEY(WCC->CurRoom.view), &vViewMsg);
746 if (vViewMsg == NULL) {
747 return; /* TODO: print message */
750 ViewMsg = (RoomRenderer*) vViewMsg;
751 if (ViewMsg->PrintPageHeader == NULL)
752 output_headers(1, 1, 1, 0, 0, 0);
754 ViewMsg->PrintPageHeader(&Stat, ViewSpecific);
756 if (ViewMsg->GetParamsGetServerCall != NULL) {
757 r = ViewMsg->GetParamsGetServerCall(
762 filter, sizeof(filter)
774 case 300: /* the callback hook should do the work for us here, since he knows what to do. */
780 if (!IsEmptyStr(cmd)) {
781 const char *p = NULL;
782 if (!IsEmptyStr(filter))
784 Stat.nummsgs = load_msg_ptrs(cmd, p, &Stat, ViewMsg->LHParse);
789 memset(&SubTP, 0, sizeof(WCTemplputParams));
790 SubTP.Filter.ContextType = CTX_MAILSUM;
791 SubTP.Context = NULL;
792 SortIt = RetrieveSort(&SubTP, NULL, 0,
793 HKEY("date"), Stat.defaultsortorder);
795 SortByPayload(WCC->summ, SortIt);
797 if (Stat.startmsg < 0) {
801 if (Stat.load_seen) load_seen_flags();
804 * Print any inforation above the message list...
806 if (ViewMsg->PrintViewHeader != NULL)
807 ViewMsg->PrintViewHeader(&Stat, &ViewSpecific);
809 WCC->startmsg = Stat.startmsg;
810 WCC->maxmsgs = Stat.maxmsgs;
811 WCC->num_displayed = 0;
813 /* Put some helpful data in vars for mailsummary_json */
818 StrBufPrintf(Foo, "%ld", Stat.nummsgs);
819 PutBstr(HKEY("__READLOOP:TOTALMSGS"), NewStrBufDup(Foo));
820 StrBufPrintf(Foo, "%ld", Stat.startmsg);
821 PutBstr(HKEY("__READLOOP:STARTMSG"), Foo);
825 * iterate over each message. if we need to load an attachment, do it here.
828 if ((ViewMsg->LoadMsgFromServer != NULL) &&
831 at = GetNewHashPos(WCC->summ, 0);
832 Stat.num_displayed = i = 0;
833 while ( GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
834 Msg = (message_summary*) vMsg;
835 if ((Msg->msgnum >= Stat.startmsg) && (Stat.num_displayed <= Stat.maxmsgs)) {
836 ViewMsg->LoadMsgFromServer(&Stat,
839 (Msg->Flags & MSGFLAG_READ) != 0,
848 * Done iterating the message list. now tasks we want to do after.
850 if (ViewMsg->RenderView_or_Tail != NULL)
851 ViewMsg->RenderView_or_Tail(&Stat, &ViewSpecific, oper);
853 if (ViewMsg->ViewCleanup != NULL)
854 ViewMsg->ViewCleanup(&ViewSpecific);
858 if (WCC->summ != NULL) {
859 DeleteHash(&WCC->summ);
865 * Back end for post_message()
866 * ... this is where the actual message gets transmitted to the server.
868 void post_mime_to_server(void) {
870 char top_boundary[SIZ];
871 char alt_boundary[SIZ];
872 int is_multipart = 0;
874 wc_mime_attachment *att;
876 size_t encoded_length;
877 size_t encoded_strlen;
878 char *txtmail = NULL;
879 int include_text_alt = 0; /* Set to nonzero to include multipart/alternative text/plain */
881 sprintf(top_boundary, "Citadel--Multipart--%s--%04x--%04x",
882 ChrPtr(WCC->serv_info->serv_fqdn),
886 sprintf(alt_boundary, "Citadel--Multipart--%s--%04x--%04x",
887 ChrPtr(WCC->serv_info->serv_fqdn),
892 /* RFC2045 requires this, and some clients look for it... */
893 serv_puts("MIME-Version: 1.0");
894 serv_puts("X-Mailer: " PACKAGE_STRING);
896 /* If there are attachments, we have to do multipart/mixed */
897 if (GetCount(WCC->attachments) > 0) {
901 /* Only do multipart/alternative for mailboxes. BBS and Wiki rooms don't need it. */
902 if (WC->CurRoom.view == VIEW_MAILBOX) {
903 include_text_alt = 1;
907 /* Remember, serv_printf() appends an extra newline */
908 serv_printf("Content-type: multipart/mixed; boundary=\"%s\"\n", top_boundary);
909 serv_printf("This is a multipart message in MIME format.\n");
910 serv_printf("--%s", top_boundary);
913 /* Remember, serv_printf() appends an extra newline */
914 if (include_text_alt) {
915 serv_printf("Content-type: multipart/alternative; "
916 "boundary=\"%s\"\n", alt_boundary);
917 serv_printf("This is a multipart message in MIME format.\n");
918 serv_printf("--%s", alt_boundary);
920 serv_puts("Content-type: text/plain; charset=utf-8");
921 serv_puts("Content-Transfer-Encoding: quoted-printable");
923 txtmail = html_to_ascii(bstr("msgtext"), 0, 80, 0);
924 text_to_server_qp(txtmail); /* Transmit message in quoted-printable encoding */
927 serv_printf("\n--%s", alt_boundary);
930 serv_puts("Content-type: text/html; charset=utf-8");
931 serv_puts("Content-Transfer-Encoding: quoted-printable");
933 serv_puts("<html><body>\r\n");
934 text_to_server_qp(bstr("msgtext")); /* Transmit message in quoted-printable encoding */
935 serv_puts("</body></html>\r\n");
937 if (include_text_alt) {
938 serv_printf("--%s--", alt_boundary);
947 /* Add in the attachments */
948 it = GetNewHashPos(WCC->attachments, 0);
949 while (GetNextHashPos(WCC->attachments, it, &len, &Key, &vAtt)) {
950 att = (wc_mime_attachment *)vAtt;
951 if (att->length == 0)
953 encoded_length = ((att->length * 150) / 100);
954 encoded = malloc(encoded_length);
955 if (encoded == NULL) break;
956 encoded_strlen = CtdlEncodeBase64(encoded, ChrPtr(att->Data), StrLength(att->Data), 1);
958 serv_printf("--%s", top_boundary);
959 serv_printf("Content-type: %s", ChrPtr(att->ContentType));
960 serv_printf("Content-disposition: attachment; filename=\"%s\"", ChrPtr(att->FileName));
961 serv_puts("Content-transfer-encoding: base64");
963 serv_write(encoded, encoded_strlen);
968 serv_printf("--%s--", top_boundary);
977 * Post message (or don't post message)
979 * Note regarding the "dont_post" variable:
980 * A random value (actually, it's just a timestamp) is inserted as a hidden
981 * field called "postseq" when the display_enter page is generated. This
982 * value is checked when posting, using the static variable dont_post. If a
983 * user attempts to post twice using the same dont_post value, the message is
984 * discarded. This prevents the accidental double-saving of the same message
985 * if the user happens to click the browser "back" button.
987 void post_message(void)
990 StrBuf *EmailAddress;
993 StrBuf *encoded_subject = NULL;
994 static long dont_post = (-1L);
995 int is_anonymous = 0;
996 const StrBuf *display_name = NULL;
1000 if (havebstr("force_room")) {
1001 gotoroom(sbstr("force_room"));
1004 if (havebstr("display_name")) {
1005 display_name = sbstr("display_name");
1006 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1007 display_name = NULL;
1012 if (!strcasecmp(bstr("submit_action"), "cancel")) {
1013 AppendImportantMessage(_("Cancelled. Message was not posted."), -1);
1014 } else if (lbstr("postseq") == dont_post) {
1015 AppendImportantMessage(
1016 _("Automatically cancelled because you have already "
1017 "saved this message."), -1);
1019 const char CMD[] = "ENT0 1|%s|%d|4|%s|%s||%s|%s|%s|%s|%s";
1020 StrBuf *Recp = NULL;
1023 char *wikipage = NULL;
1024 const StrBuf *my_email_addr = NULL;
1025 StrBuf *CmdBuf = NULL;
1026 StrBuf *references = NULL;
1027 int saving_to_drafts = 0;
1030 saving_to_drafts = !strcasecmp(bstr("submit_action"), "draft");
1033 if (saving_to_drafts) {
1034 /* temporarily change to the drafts room */
1035 serv_puts("GOTO _DRAFTS_");
1036 StrBuf_ServGetln(Buf);
1037 if (GetServerStatusMsg(Buf, NULL, 1, 2) != 2) {
1038 /* You probably don't even have a dumb Drafts folder */
1039 syslog(9, "%s:%d: server save to drafts error: %s\n", __FILE__, __LINE__, ChrPtr(Buf) + 4);
1040 AppendImportantMessage(_("Saved to Drafts failed: "), -1);
1047 if (havebstr("references"))
1049 const StrBuf *ref = sbstr("references");
1050 references = NewStrBufDup(ref);
1051 if (*ChrPtr(references) == '|') { /* remove leading '|' if present */
1052 StrBufCutLeft(references, 1);
1054 StrBufReplaceChars(references, '|', '!');
1056 if (havebstr("subject")) {
1059 * make enough room for the encoded string;
1060 * plus the QP header
1062 Subj = sbstr("subject");
1064 StrBufRFC2047encode(&encoded_subject, Subj);
1066 UserName = NewStrBuf();
1067 EmailAddress = NewStrBuf();
1068 EncBuf = NewStrBuf();
1070 Recp = StrBufSanitizeEmailRecipientVector(sbstr("recp"), UserName, EmailAddress, EncBuf);
1071 Cc = StrBufSanitizeEmailRecipientVector(sbstr("cc"), UserName, EmailAddress, EncBuf);
1072 Bcc = StrBufSanitizeEmailRecipientVector(sbstr("bcc"), UserName, EmailAddress, EncBuf);
1074 FreeStrBuf(&UserName);
1075 FreeStrBuf(&EmailAddress);
1076 FreeStrBuf(&EncBuf);
1078 wikipage = strdup(bstr("page"));
1079 str_wiki_index(wikipage);
1080 my_email_addr = sbstr("my_email_addr");
1082 HeaderLen = StrLength(Recp) +
1083 StrLength(encoded_subject) +
1087 StrLength(my_email_addr) +
1088 StrLength(references);
1089 CmdBuf = NewStrBufPlain(NULL, sizeof (CMD) + HeaderLen);
1090 StrBufPrintf(CmdBuf,
1092 saving_to_drafts?"":ChrPtr(Recp),
1094 ChrPtr(encoded_subject),
1095 ChrPtr(display_name),
1096 saving_to_drafts?"":ChrPtr(Cc),
1097 saving_to_drafts?"":ChrPtr(Bcc),
1099 ChrPtr(my_email_addr),
1100 ChrPtr(references));
1101 FreeStrBuf(&references);
1102 FreeStrBuf(&encoded_subject);
1105 if ((HeaderLen + StrLength(sbstr("msgtext")) < 10) &&
1106 (GetCount(WCC->attachments) == 0)){
1107 AppendImportantMessage(_("Refusing to post empty message.\n"), -1);
1108 FreeStrBuf(&CmdBuf);
1113 syslog(9, "%s\n", ChrPtr(CmdBuf));
1114 serv_puts(ChrPtr(CmdBuf));
1115 FreeStrBuf(&CmdBuf);
1117 StrBuf_ServGetln(Buf);
1118 if (GetServerStatus(Buf, NULL) == 4) {
1119 if (saving_to_drafts) {
1120 if ( (havebstr("recp"))
1121 || (havebstr("cc" ))
1122 || (havebstr("bcc" )) ) {
1123 /* save recipient headers or room to post to */
1124 serv_printf("To: %s", ChrPtr(Recp));
1125 serv_printf("Cc: %s", ChrPtr(Cc));
1126 serv_printf("Bcc: %s", ChrPtr(Bcc));
1128 serv_printf("X-Citadel-Room: %s", ChrPtr(WC->CurRoom.name));
1131 post_mime_to_server();
1132 if (saving_to_drafts) {
1133 AppendImportantMessage(_("Message has been saved to Drafts.\n"), -1);
1134 gotoroom(WCC->CurRoom.name);
1136 readloop(readnew, eUseDefault);
1139 } else if ( (havebstr("recp"))
1140 || (havebstr("cc" ))
1141 || (havebstr("bcc" ))
1143 AppendImportantMessage(_("Message has been sent.\n"), -1);
1146 AppendImportantMessage(_("Message has been posted.\n"), -1);
1148 dont_post = lbstr("postseq");
1150 syslog(9, "%s:%d: server post error: %s", __FILE__, __LINE__, ChrPtr(Buf) + 4);
1151 AppendImportantMessage(ChrPtr(Buf) + 4, StrLength(Buf) - 4);
1153 if (saving_to_drafts) gotoroom(WCC->CurRoom.name);
1167 DeleteHash(&WCC->attachments);
1170 * We may have been supplied with instructions regarding the location
1171 * to which we must return after posting. If found, go there.
1173 if (havebstr("return_to")) {
1174 http_redirect(bstr("return_to"));
1177 * If we were editing a page in a wiki room, go to that page now.
1179 else if (havebstr("page")) {
1180 snprintf(buf, sizeof buf, "wiki?page=%s", bstr("page"));
1184 * Otherwise, just go to the "read messages" loop.
1188 readloop(readnew, eUseDefault);
1194 * Client is uploading an attachment
1196 void upload_attachment(void) {
1197 wcsession *WCC = WC;
1203 wc_mime_attachment *att;
1205 syslog(9, "upload_attachment()\n");
1206 wc_printf("upload_attachment()<br>\n");
1208 if (WCC->upload_length <= 0) {
1209 syslog(9, "ERROR no attachment was uploaded\n");
1210 wc_printf("ERROR no attachment was uploaded<br>\n");
1214 syslog(9, "Client is uploading %d bytes\n", WCC->upload_length);
1215 wc_printf("Client is uploading %d bytes<br>\n", WCC->upload_length);
1216 att = malloc(sizeof(wc_mime_attachment));
1217 memset(att, 0, sizeof(wc_mime_attachment ));
1218 att->length = WCC->upload_length;
1219 att->ContentType = NewStrBufPlain(WCC->upload_content_type, -1);
1220 att->FileName = NewStrBufDup(WCC->upload_filename);
1222 if (WCC->attachments == NULL) {
1223 WCC->attachments = NewHash(1, Flathash);
1226 /* And add it to the list. */
1228 if ((GetCount(WCC->attachments) > 0) &&
1229 GetHashAt(WCC->attachments,
1230 GetCount(WCC->attachments) -1,
1231 &newnlen, &newn, &v))
1232 n = *((int*) newn) + 1;
1233 Put(WCC->attachments, IKEY(n), att, DestroyMime);
1236 * Mozilla sends a simple filename, which is what we want,
1237 * but Satan's Browser sends an entire pathname. Reduce
1238 * the path to just a filename if we need to.
1240 pch = strrchr(ChrPtr(att->FileName), '/');
1242 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1244 pch = strrchr(ChrPtr(att->FileName), '\\');
1246 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1250 * Transfer control of this memory from the upload struct
1251 * to the attachment struct.
1253 att->Data = WCC->upload;
1255 WCC->upload_length = 0;
1260 * Remove an attachment from the message currently being composed.
1262 * Currently we identify the attachment to be removed by its filename.
1263 * There is probably a better way to do this.
1265 void remove_attachment(void) {
1266 wcsession *WCC = WC;
1267 wc_mime_attachment *att;
1269 StrBuf *WhichAttachment;
1274 WhichAttachment = NewStrBufDup(sbstr("which_attachment"));
1275 StrBufUnescape(WhichAttachment, 0);
1276 at = GetNewHashPos(WCC->attachments, 0);
1278 GetHashPos(WCC->attachments, at, &len, &key, &vAtt);
1280 att = (wc_mime_attachment*) vAtt;
1281 if ((att != NULL) &&
1282 (strcmp(ChrPtr(WhichAttachment),
1283 ChrPtr(att->FileName) ) == 0))
1285 DeleteEntryFromHash(WCC->attachments, at);
1289 while (NextHashPos(WCC->attachments, at));
1290 FreeStrBuf(&WhichAttachment);
1291 wc_printf("remove_attachment() completed\n");
1295 long FourHash(const char *key, long length)
1299 const unsigned char *ptr = (const unsigned char*)key;
1301 for (i = 0; i < 4; i++, ptr ++)
1321 const char *ReplyToModeStrings [3] = {
1326 typedef enum _eReplyToNodes {
1333 * display the message entry screen
1335 void display_enter(void)
1337 const char *ReplyingModeStr;
1338 eReplyToNodes ReplyMode = eReply;
1342 const StrBuf *display_name = NULL;
1343 int recipient_required = 0;
1344 int subject_required = 0;
1345 int is_anonymous = 0;
1346 wcsession *WCC = WC;
1350 if (havebstr("force_room")) {
1351 gotoroom(sbstr("force_room"));
1354 display_name = sbstr("display_name");
1355 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1356 display_name = NULL;
1361 * First, do we have permission to enter messages in this room at all?
1364 serv_puts("ENT0 0");
1365 StrBuf_ServGetln(Line);
1366 rc = GetServerStatusMsg(Line, &Result, 0, 2);
1368 if (Result == 570) { /* 570 means that we need a recipient here */
1369 recipient_required = 1;
1371 else if (rc != 2) { /* Any other error means that we cannot continue */
1372 rc = GetServerStatusMsg(Line, &Result, 0, 2);
1374 readloop(readnew, eUseDefault);
1379 /* Is the server strongly recommending that the user enter a message subject? */
1380 if (StrLength(Line) > 4) {
1381 subject_required = extract_int(ChrPtr(Line) + 4, 1);
1385 * Are we perhaps in an address book view? If so, then an "enter
1386 * message" command really means "add new entry."
1388 if (WCC->CurRoom.defview == VIEW_ADDRESSBOOK) {
1389 do_edit_vcard(-1, "", NULL, NULL, "", ChrPtr(WCC->CurRoom.name));
1395 * Are we perhaps in a calendar room? If so, then an "enter
1396 * message" command really means "add new calendar item."
1398 if (WCC->CurRoom.defview == VIEW_CALENDAR) {
1399 display_edit_event();
1405 * Are we perhaps in a tasks view? If so, then an "enter
1406 * message" command really means "add new task."
1408 if (WCC->CurRoom.defview == VIEW_TASKS) {
1409 display_edit_task();
1415 ReplyingModeStr = bstr("replying_mode");
1416 if (ReplyingModeStr != NULL) for (i = 0; i < 3; i++) {
1417 if (strcmp(ReplyingModeStr, ReplyToModeStrings[i]) == 0) {
1418 ReplyMode = (eReplyToNodes) i;
1425 * If the "replying_to" variable is set, it refers to a message
1426 * number from which we must extract some header fields...
1428 replying_to = lbstr("replying_to");
1429 if (replying_to > 0) {
1431 StrBuf *wefw = NULL;
1432 StrBuf *msgn = NULL;
1433 StrBuf *from = NULL;
1434 StrBuf *node = NULL;
1435 StrBuf *rfca = NULL;
1436 StrBuf *rcpt = NULL;
1437 StrBuf *cccc = NULL;
1438 StrBuf *replyto = NULL;
1439 serv_printf("MSG0 %ld|1", replying_to);
1441 StrBuf_ServGetln(Line);
1442 if (GetServerStatusMsg(Line, NULL, 0, 0) == 1)
1443 while (len = StrBuf_ServGetln(Line),
1446 strcmp(ChrPtr(Line), "000")))
1449 if ((StrLength(Line) > 4) &&
1450 (ChrPtr(Line)[4] == '='))
1451 which = FourHash(ChrPtr(Line), 4);
1453 if (which == l_subj)
1455 StrBuf *subj = NewStrBuf();
1456 StrBuf *FlatSubject;
1458 if (ReplyMode == eForward) {
1459 if (strncasecmp(ChrPtr(Line) + 5, "Fw:", 3)) {
1460 StrBufAppendBufPlain(subj, HKEY("Fw: "), 0);
1464 if (strncasecmp(ChrPtr(Line) + 5, "Re:", 3)) {
1465 StrBufAppendBufPlain(subj, HKEY("Re: "), 0);
1468 StrBufAppendBufPlain(subj,
1470 StrLength(Line) - 5, 0);
1471 FlatSubject = NewStrBufPlain(NULL, StrLength(subj));
1472 StrBuf_RFC822_to_Utf8(FlatSubject, subj, NULL, NULL);
1474 PutBstr(HKEY("subject"), FlatSubject);
1477 else if (which == l_wefw)
1482 wefw = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1484 /* Trim down excessively long lists of thread references. We eliminate the
1485 * second one in the list so that the thread root remains intact.
1487 rrtok = num_tokens(ChrPtr(wefw), '|');
1488 rrlen = StrLength(wefw);
1489 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
1490 StrBufRemove_token(wefw, 1, '|');
1494 else if (which == l_msgn) {
1495 msgn = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1498 else if (which == l_from) {
1500 from = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1501 FlatFrom = NewStrBufPlain(NULL, StrLength(from));
1502 StrBuf_RFC822_to_Utf8(FlatFrom, from, NULL, NULL);
1505 for (i=0; i<StrLength(from); ++i) {
1506 if (ChrPtr(from)[i] == ',')
1507 StrBufPeek(from, NULL, i, ' ');
1511 else if (which == l_rcpt) {
1512 rcpt = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1515 else if (which == l_cccc) {
1516 cccc = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1519 else if (which == l_node) {
1520 node = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1522 else if (which == l_replyto) {
1523 replyto = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1525 else if (which == l_rfca) {
1527 rfca = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1528 FlatRFCA = NewStrBufPlain(NULL, StrLength(rfca));
1529 StrBuf_RFC822_to_Utf8(FlatRFCA, rfca, NULL, NULL);
1536 if (StrLength(wefw) + StrLength(msgn) > 0) {
1537 StrBuf *refs = NewStrBuf();
1538 if (StrLength(wefw) > 0) {
1539 StrBufAppendBuf(refs, wefw, 0);
1541 if ( (StrLength(wefw) > 0) &&
1542 (StrLength(msgn) > 0) )
1544 StrBufAppendBufPlain(refs, HKEY("|"), 0);
1546 if (StrLength(msgn) > 0) {
1547 StrBufAppendBuf(refs, msgn, 0);
1549 PutBstr(HKEY("references"), refs);
1553 * If this is a Reply or a ReplyAll, copy the sender's email into the To: field
1555 if ((ReplyMode == eReply) || (ReplyMode == eReplyAll))
1558 if ((StrLength(replyto) > 0) && (ReplyMode == eReplyAll)) {
1559 to_rcpt = NewStrBuf();
1560 StrBufAppendBuf(to_rcpt, replyto, 0);
1562 else if (StrLength(rfca) > 0) {
1563 to_rcpt = NewStrBuf();
1564 StrBufAppendBuf(to_rcpt, from, 0);
1565 StrBufAppendBufPlain(to_rcpt, HKEY(" <"), 0);
1566 StrBufAppendBuf(to_rcpt, rfca, 0);
1567 StrBufAppendBufPlain(to_rcpt, HKEY(">"), 0);
1572 if ( (StrLength(node) > 0)
1573 && (strcasecmp(ChrPtr(node), ChrPtr(WC->serv_info->serv_nodename)))
1575 StrBufAppendBufPlain(to_rcpt, HKEY(" @ "), 0);
1576 StrBufAppendBuf(to_rcpt, node, 0);
1579 PutBstr(HKEY("recp"), to_rcpt);
1583 * Only if this is a ReplyAll, copy all recipients into the Cc: field
1585 if (ReplyMode == eReplyAll)
1587 StrBuf *cc_rcpt = rcpt;
1589 if ((StrLength(cccc) > 0) && (StrLength(replyto) == 0))
1591 if (cc_rcpt != NULL) {
1592 StrBufAppendPrintf(cc_rcpt, ", ");
1593 StrBufAppendBuf(cc_rcpt, cccc, 0);
1599 if (cc_rcpt != NULL)
1600 PutBstr(HKEY("cc"), cc_rcpt);
1612 * Otherwise proceed normally.
1613 * Do a custom room banner with no navbar...
1616 if (recipient_required) {
1617 const StrBuf *Recp = NULL;
1618 const StrBuf *Cc = NULL;
1619 const StrBuf *Bcc = NULL;
1620 char *wikipage = NULL;
1621 StrBuf *CmdBuf = NULL;
1622 const char CMD[] = "ENT0 0|%s|%d|0||%s||%s|%s|%s";
1624 Recp = sbstr("recp");
1627 wikipage = strdup(bstr("page"));
1628 str_wiki_index(wikipage);
1630 CmdBuf = NewStrBufPlain(NULL,
1633 StrLength(display_name) +
1638 StrBufPrintf(CmdBuf,
1642 ChrPtr(display_name),
1647 serv_puts(ChrPtr(CmdBuf));
1648 StrBuf_ServGetln(CmdBuf);
1651 rc = GetServerStatusMsg(CmdBuf, &Result, 0, 0);
1653 if ( (Result == 570) /* invalid or missing recipient(s) */
1654 || (Result == 550) /* higher access required to send Internet mail */
1656 /* These errors will have been displayed and are excusable */
1658 else if (rc != 2) { /* Any other error means that we cannot continue */
1659 AppendImportantMessage(ChrPtr(CmdBuf) + 4, StrLength(CmdBuf) - 4);
1660 FreeStrBuf(&CmdBuf);
1662 readloop(readnew, eUseDefault);
1665 FreeStrBuf(&CmdBuf);
1667 if (recipient_required)
1668 PutBstr(HKEY("__RCPTREQUIRED"), NewStrBufPlain(HKEY("1")));
1669 if (recipient_required || subject_required)
1670 PutBstr(HKEY("__SUBJREQUIRED"), NewStrBufPlain(HKEY("1")));
1673 output_headers(1, 0, 0, 0, 1, 0);
1674 DoTemplate(HKEY("edit_message"), NULL, &NoCtx);
1683 void delete_msg(void)
1688 msgid = lbstr("msgid");
1690 if ((WC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
1691 serv_printf("DELE %ld", msgid);
1693 else { /* Otherwise move it to Trash */
1694 serv_printf("MOVE %ld|_TRASH_|0", msgid);
1697 StrBuf_ServGetln(Line);
1698 GetServerStatusMsg(Line, NULL, 1, 0);
1702 readloop(readnew, eUseDefault);
1707 * move a message to another room
1713 msgid = lbstr("msgid");
1715 if (havebstr("move_button")) {
1717 serv_printf("MOVE %ld|%s", msgid, bstr("target_room"));
1719 StrBuf_ServGetln(Line);
1720 GetServerStatusMsg(Line, NULL, 1, 0);
1723 AppendImportantMessage(_("The message was not moved."), -1);
1727 readloop(readnew, eUseDefault);
1733 * Generic function to output an arbitrary MIME attachment from
1734 * message being composed
1736 * partnum The MIME part to be output
1737 * filename Fake filename to give
1738 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1740 void postpart(StrBuf *partnum, StrBuf *filename, int force_download)
1743 StrBuf *content_type;
1744 wc_mime_attachment *part;
1747 i = StrToi(partnum);
1748 if (GetHash(WC->attachments, IKEY(i), &vPart) &&
1750 part = (wc_mime_attachment*) vPart;
1751 if (force_download) {
1752 content_type = NewStrBufPlain(HKEY("application/octet-stream"));
1755 content_type = NewStrBufDup(part->ContentType);
1757 StrBufAppendBuf(WC->WBuf, part->Data, 0);
1758 http_transmit_thing(ChrPtr(content_type), 0);
1760 hprintf("HTTP/1.1 404 %s\n", ChrPtr(partnum));
1761 output_headers(0, 0, 0, 0, 0, 0);
1762 hprintf("Content-Type: text/plain\r\n");
1764 wc_printf(_("An error occurred while retrieving this part: %s/%s\n"),
1765 ChrPtr(partnum), ChrPtr(filename));
1768 FreeStrBuf(&content_type);
1773 * Generic function to output an arbitrary MIME part from an arbitrary
1774 * message number on the server.
1776 * msgnum Number of the item on the citadel server
1777 * partnum The MIME part to be output
1778 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1780 void mimepart(int force_download)
1785 wcsession *WCC = WC;
1788 StrBuf *ContentType = NewStrBufPlain(HKEY("application/octet-stream"));
1791 att = Buf = NewStrBuf();
1792 msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
1793 StrBufExtract_token(att, WCC->Hdr->HR.ReqLine, 1, '/');
1795 serv_printf("OPNA %ld|%s", msgnum, ChrPtr(att));
1796 StrBuf_ServGetln(Buf);
1797 if (GetServerStatus(Buf, &ErrorDetail) == 2) {
1798 StrBufCutLeft(Buf, 4);
1799 bytes = StrBufExtract_long(Buf, 0, '|');
1800 if (!force_download) {
1801 StrBufExtract_token(ContentType, Buf, 3, '|');
1804 serv_read_binary(WCC->WBuf, bytes, Buf);
1806 StrBuf_ServGetln(Buf);
1807 CT = ChrPtr(ContentType);
1809 if (!force_download) {
1810 if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
1811 StrBufExtract_token(Buf, WCC->Hdr->HR.ReqLine, 2, '/');
1812 CT = GuessMimeByFilename(SKEY(Buf));
1814 if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
1815 CT = GuessMimeType(SKEY(WCC->WBuf));
1818 http_transmit_thing(CT, 0);
1820 StrBufCutLeft(Buf, 4);
1821 switch (ErrorDetail) {
1823 case ERROR + MESSAGE_NOT_FOUND:
1824 hprintf("HTTP/1.1 404 %s\n", ChrPtr(Buf));
1826 case ERROR + NOT_LOGGED_IN:
1827 hprintf("HTTP/1.1 401 %s\n", ChrPtr(Buf));
1830 case ERROR + HIGHER_ACCESS_REQUIRED:
1831 hprintf("HTTP/1.1 403 %s\n", ChrPtr(Buf));
1833 case ERROR + INTERNAL_ERROR:
1834 case ERROR + TOO_BIG:
1835 hprintf("HTTP/1.1 500 %s\n", ChrPtr(Buf));
1838 output_headers(0, 0, 0, 0, 0, 0);
1839 hprintf("Content-Type: text/plain\r\n");
1841 wc_printf(_("An error occurred while retrieving this part: %s\n"),
1845 FreeStrBuf(&ContentType);
1851 * Read any MIME part of a message, from the server, into memory.
1853 StrBuf *load_mimepart(long msgnum, char *partnum)
1859 serv_printf("DLAT %ld|%s", msgnum, partnum);
1860 StrBuf_ServGetln(Buf);
1861 if (GetServerStatus(Buf, NULL) == 6) {
1862 StrBufCutLeft(Buf, 4);
1863 bytes = StrBufExtract_long(Buf, 0, '|');
1866 StrBuf_ServGetBLOBBuffered(Buf, bytes);
1876 * Read any MIME part of a message, from the server, into memory.
1878 void MimeLoadData(wc_mime_attachment *Mime)
1883 /* TODO: is there a chance the content type is different from the one we know? */
1885 serv_printf("DLAT %ld|%s", Mime->msgnum, ChrPtr(Mime->PartNum));
1887 StrBuf_ServGetln(Buf);
1888 if (GetServerStatus(Buf, NULL) == 6) {
1889 Ptr = &(ChrPtr(Buf)[4]);
1890 bytes = StrBufExtractNext_long(Buf, &Ptr, '|');
1891 StrBufSkip_NTokenS(Buf, &Ptr, '|', 3); /* filename, cbtype, mimetype */
1892 if (Mime->Charset == NULL) Mime->Charset = NewStrBuf();
1893 StrBufExtract_NextToken(Mime->Charset, Buf, &Ptr, '|');
1895 if (Mime->Data == NULL)
1896 Mime->Data = NewStrBufPlain(NULL, bytes);
1897 StrBuf_ServGetBLOBBuffered(Mime->Data, bytes);
1900 FlushStrBuf(Mime->Data);
1901 /* TODO XImportant message */
1907 void view_mimepart(void) {
1911 void download_mimepart(void) {
1915 void view_postpart(void) {
1916 StrBuf *filename = NewStrBuf();
1917 StrBuf *partnum = NewStrBuf();
1919 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1920 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1922 postpart(partnum, filename, 0);
1924 FreeStrBuf(&filename);
1925 FreeStrBuf(&partnum);
1928 void download_postpart(void) {
1929 StrBuf *filename = NewStrBuf();
1930 StrBuf *partnum = NewStrBuf();
1932 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1933 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1935 postpart(partnum, filename, 1);
1937 FreeStrBuf(&filename);
1938 FreeStrBuf(&partnum);
1943 void show_num_attachments(void) {
1944 wc_printf("%d", GetCount(WC->attachments));
1948 void h_readnew(void) { readloop(readnew, eUseDefault);}
1949 void h_readold(void) { readloop(readold, eUseDefault);}
1950 void h_readfwd(void) { readloop(readfwd, eUseDefault);}
1951 void h_headers(void) { readloop(headers, eUseDefault);}
1952 void h_do_search(void) { readloop(do_search, eUseDefault);}
1953 void h_readgt(void) { readloop(readgt, eUseDefault);}
1954 void h_readlt(void) { readloop(readlt, eUseDefault);}
1958 /* Output message list in JSON format */
1959 void jsonMessageList(void) {
1960 StrBuf *View = NewStrBuf();
1961 const StrBuf *room = sbstr("room");
1962 long oper = (havebstr("query")) ? do_search : readnew;
1963 StrBufPrintf(View, "%d", VIEW_JSON_LIST);
1964 putbstr("view", View);;
1966 readloop(oper, eUseDefault);
1969 void RegisterReadLoopHandlerset(
1971 GetParamsGetServerCall_func GetParamsGetServerCall,
1972 PrintViewHeader_func PrintPageHeader,
1973 PrintViewHeader_func PrintViewHeader,
1974 load_msg_ptrs_detailheaders LH,
1975 LoadMsgFromServer_func LoadMsgFromServer,
1976 RenderView_or_Tail_func RenderView_or_Tail,
1977 View_Cleanup_func ViewCleanup
1980 RoomRenderer *Handler;
1982 Handler = (RoomRenderer*) malloc(sizeof(RoomRenderer));
1984 Handler->RoomType = RoomType;
1985 Handler->GetParamsGetServerCall = GetParamsGetServerCall;
1986 Handler->PrintPageHeader = PrintPageHeader;
1987 Handler->PrintViewHeader = PrintViewHeader;
1988 Handler->LoadMsgFromServer = LoadMsgFromServer;
1989 Handler->RenderView_or_Tail = RenderView_or_Tail;
1990 Handler->ViewCleanup = ViewCleanup;
1991 Handler->LHParse = LH;
1993 Put(ReadLoopHandler, IKEY(RoomType), Handler, NULL);
2000 RegisterPreference("use_sig",
2001 _("Attach signature to email messages?"),
2004 RegisterPreference("signature", _("Use this signature:"), PRF_QP_STRING, NULL);
2005 RegisterPreference("default_header_charset",
2006 _("Default character set for email headers:"),
2009 RegisterPreference("defaultfrom", _("Preferred email address"), PRF_STRING, NULL);
2010 RegisterPreference("defaultname",
2011 _("Preferred display name for email messages"),
2014 RegisterPreference("defaulthandle",
2015 _("Preferred display name for bulletin board posts"),
2018 RegisterPreference("mailbox",_("Mailbox view mode"), PRF_STRING, NULL);
2020 WebcitAddUrlHandler(HKEY("readnew"), "", 0, h_readnew, ANONYMOUS|NEED_URL);
2021 WebcitAddUrlHandler(HKEY("readold"), "", 0, h_readold, ANONYMOUS|NEED_URL);
2022 WebcitAddUrlHandler(HKEY("readfwd"), "", 0, h_readfwd, ANONYMOUS|NEED_URL);
2023 WebcitAddUrlHandler(HKEY("headers"), "", 0, h_headers, NEED_URL);
2024 WebcitAddUrlHandler(HKEY("readgt"), "", 0, h_readgt, ANONYMOUS|NEED_URL);
2025 WebcitAddUrlHandler(HKEY("readlt"), "", 0, h_readlt, ANONYMOUS|NEED_URL);
2026 WebcitAddUrlHandler(HKEY("do_search"), "", 0, h_do_search, 0);
2027 WebcitAddUrlHandler(HKEY("display_enter"), "", 0, display_enter, 0);
2028 WebcitAddUrlHandler(HKEY("post"), "", 0, post_message, PROHIBIT_STARTPAGE);
2029 WebcitAddUrlHandler(HKEY("move_msg"), "", 0, move_msg, PROHIBIT_STARTPAGE);
2030 WebcitAddUrlHandler(HKEY("delete_msg"), "", 0, delete_msg, PROHIBIT_STARTPAGE);
2031 WebcitAddUrlHandler(HKEY("msg"), "", 0, embed_message, NEED_URL);
2032 WebcitAddUrlHandler(HKEY("message"), "", 0, handle_one_message, NEED_URL|XHTTP_COMMANDS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
2033 WebcitAddUrlHandler(HKEY("printmsg"), "", 0, print_message, NEED_URL);
2034 WebcitAddUrlHandler(HKEY("msgheaders"), "", 0, display_headers, NEED_URL);
2036 WebcitAddUrlHandler(HKEY("mimepart"), "", 0, view_mimepart, NEED_URL);
2037 WebcitAddUrlHandler(HKEY("mimepart_download"), "", 0, download_mimepart, NEED_URL);
2038 WebcitAddUrlHandler(HKEY("postpart"), "", 0, view_postpart, NEED_URL|PROHIBIT_STARTPAGE);
2039 WebcitAddUrlHandler(HKEY("postpart_download"), "", 0, download_postpart, NEED_URL|PROHIBIT_STARTPAGE);
2040 WebcitAddUrlHandler(HKEY("upload_attachment"), "", 0, upload_attachment, AJAX);
2041 WebcitAddUrlHandler(HKEY("remove_attachment"), "", 0, remove_attachment, AJAX);
2042 WebcitAddUrlHandler(HKEY("show_num_attachments"), "", 0, show_num_attachments, AJAX);
2045 WebcitAddUrlHandler(HKEY("roommsgs"), "", 0, jsonMessageList,0);
2047 l_subj = FourHash("subj", 4);
2048 l_wefw = FourHash("wefw", 4);
2049 l_msgn = FourHash("msgn", 4);
2050 l_from = FourHash("from", 4);
2051 l_rcpt = FourHash("rcpt", 4);
2052 l_cccc = FourHash("cccc", 4);
2053 l_replyto = FourHash("rep2", 4);
2054 l_node = FourHash("node", 4);
2055 l_rfca = FourHash("rfca", 4);
2061 SessionDetachModule_MSG
2064 DeleteHash(&sess->summ);