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)
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 (WC->CurRoom.view == VIEW_MAILBOX) {
907 include_text_alt = 1;
911 /* Remember, serv_printf() appends an extra newline */
912 serv_printf("Content-type: multipart/mixed; boundary=\"%s\"\n", top_boundary);
913 serv_printf("This is a multipart message in MIME format.\n");
914 serv_printf("--%s", top_boundary);
917 /* Remember, serv_printf() appends an extra newline */
918 if (include_text_alt) {
919 serv_printf("Content-type: multipart/alternative; "
920 "boundary=\"%s\"\n", alt_boundary);
921 serv_printf("This is a multipart message in MIME format.\n");
922 serv_printf("--%s", alt_boundary);
924 serv_puts("Content-type: text/plain; charset=utf-8");
925 serv_puts("Content-Transfer-Encoding: quoted-printable");
927 txtmail = html_to_ascii(bstr("msgtext"), 0, 80, 0);
928 text_to_server_qp(txtmail); /* Transmit message in quoted-printable encoding */
931 serv_printf("\n--%s", alt_boundary);
934 serv_puts("Content-type: text/html; charset=utf-8");
935 serv_puts("Content-Transfer-Encoding: quoted-printable");
937 serv_puts("<html><body>\r\n");
938 text_to_server_qp(bstr("msgtext")); /* Transmit message in quoted-printable encoding */
939 serv_puts("</body></html>\r\n");
941 if (include_text_alt) {
942 serv_printf("--%s--", alt_boundary);
951 /* Add in the attachments */
952 it = GetNewHashPos(WCC->attachments, 0);
953 while (GetNextHashPos(WCC->attachments, it, &len, &Key, &vAtt)) {
954 att = (wc_mime_attachment *)vAtt;
955 if (att->length == 0)
957 encoded_length = ((att->length * 150) / 100);
958 encoded = malloc(encoded_length);
959 if (encoded == NULL) break;
960 encoded_strlen = CtdlEncodeBase64(encoded, ChrPtr(att->Data), StrLength(att->Data), 1);
962 serv_printf("--%s", top_boundary);
963 serv_printf("Content-type: %s", ChrPtr(att->ContentType));
964 serv_printf("Content-disposition: attachment; filename=\"%s\"", ChrPtr(att->FileName));
965 serv_puts("Content-transfer-encoding: base64");
967 serv_write(encoded, encoded_strlen);
972 serv_printf("--%s--", top_boundary);
981 * Post message (or don't post message)
983 * Note regarding the "dont_post" variable:
984 * A random value (actually, it's just a timestamp) is inserted as a hidden
985 * field called "postseq" when the display_enter page is generated. This
986 * value is checked when posting, using the static variable dont_post. If a
987 * user attempts to post twice using the same dont_post value, the message is
988 * discarded. This prevents the accidental double-saving of the same message
989 * if the user happens to click the browser "back" button.
991 void post_message(void)
994 StrBuf *EmailAddress;
997 StrBuf *encoded_subject = NULL;
998 static long dont_post = (-1L);
999 int is_anonymous = 0;
1000 const StrBuf *display_name = NULL;
1001 wcsession *WCC = WC;
1004 if (havebstr("force_room")) {
1005 gotoroom(sbstr("force_room"));
1008 if (havebstr("display_name")) {
1009 display_name = sbstr("display_name");
1010 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1011 display_name = NULL;
1016 if (!strcasecmp(bstr("submit_action"), "cancel")) {
1017 AppendImportantMessage(_("Cancelled. Message was not posted."), -1);
1018 } else if (lbstr("postseq") == dont_post) {
1019 AppendImportantMessage(
1020 _("Automatically cancelled because you have already "
1021 "saved this message."), -1);
1023 const char CMD[] = "ENT0 1|%s|%d|4|%s|%s||%s|%s|%s|%s|%s";
1024 StrBuf *Recp = NULL;
1027 char *wikipage = NULL;
1028 const StrBuf *my_email_addr = NULL;
1029 StrBuf *CmdBuf = NULL;
1030 StrBuf *references = NULL;
1031 int saving_to_drafts = 0;
1034 saving_to_drafts = !strcasecmp(bstr("submit_action"), "draft");
1037 if (saving_to_drafts) {
1038 /* temporarily change to the drafts room */
1039 serv_puts("GOTO _DRAFTS_");
1040 StrBuf_ServGetln(Buf);
1041 if (GetServerStatusMsg(Buf, NULL, 1, 2) != 2) {
1042 /* You probably don't even have a dumb Drafts folder */
1043 syslog(9, "%s:%d: server save to drafts error: %s\n", __FILE__, __LINE__, ChrPtr(Buf) + 4);
1044 AppendImportantMessage(_("Saved to Drafts failed: "), -1);
1051 if (havebstr("references"))
1053 const StrBuf *ref = sbstr("references");
1054 references = NewStrBufDup(ref);
1055 if (*ChrPtr(references) == '|') { /* remove leading '|' if present */
1056 StrBufCutLeft(references, 1);
1058 StrBufReplaceChars(references, '|', '!');
1060 if (havebstr("subject")) {
1063 * make enough room for the encoded string;
1064 * plus the QP header
1066 Subj = sbstr("subject");
1068 StrBufRFC2047encode(&encoded_subject, Subj);
1070 UserName = NewStrBuf();
1071 EmailAddress = NewStrBuf();
1072 EncBuf = NewStrBuf();
1074 Recp = StrBufSanitizeEmailRecipientVector(sbstr("recp"), UserName, EmailAddress, EncBuf);
1075 Cc = StrBufSanitizeEmailRecipientVector(sbstr("cc"), UserName, EmailAddress, EncBuf);
1076 Bcc = StrBufSanitizeEmailRecipientVector(sbstr("bcc"), UserName, EmailAddress, EncBuf);
1078 FreeStrBuf(&UserName);
1079 FreeStrBuf(&EmailAddress);
1080 FreeStrBuf(&EncBuf);
1082 wikipage = strdup(bstr("page"));
1083 str_wiki_index(wikipage);
1084 my_email_addr = sbstr("my_email_addr");
1086 HeaderLen = StrLength(Recp) +
1087 StrLength(encoded_subject) +
1091 StrLength(my_email_addr) +
1092 StrLength(references);
1093 CmdBuf = NewStrBufPlain(NULL, sizeof (CMD) + HeaderLen);
1094 StrBufPrintf(CmdBuf,
1096 saving_to_drafts?"":ChrPtr(Recp),
1098 ChrPtr(encoded_subject),
1099 ChrPtr(display_name),
1100 saving_to_drafts?"":ChrPtr(Cc),
1101 saving_to_drafts?"":ChrPtr(Bcc),
1103 ChrPtr(my_email_addr),
1104 ChrPtr(references));
1105 FreeStrBuf(&references);
1106 FreeStrBuf(&encoded_subject);
1109 if ((HeaderLen + StrLength(sbstr("msgtext")) < 10) &&
1110 (GetCount(WCC->attachments) == 0)){
1111 AppendImportantMessage(_("Refusing to post empty message.\n"), -1);
1112 FreeStrBuf(&CmdBuf);
1117 syslog(9, "%s\n", ChrPtr(CmdBuf));
1118 serv_puts(ChrPtr(CmdBuf));
1119 FreeStrBuf(&CmdBuf);
1121 StrBuf_ServGetln(Buf);
1122 if (GetServerStatus(Buf, NULL) == 4) {
1123 if (saving_to_drafts) {
1124 if ( (havebstr("recp"))
1125 || (havebstr("cc" ))
1126 || (havebstr("bcc" )) ) {
1127 /* save recipient headers or room to post to */
1128 serv_printf("To: %s", ChrPtr(Recp));
1129 serv_printf("Cc: %s", ChrPtr(Cc));
1130 serv_printf("Bcc: %s", ChrPtr(Bcc));
1132 serv_printf("X-Citadel-Room: %s", ChrPtr(WC->CurRoom.name));
1135 post_mime_to_server();
1136 if (saving_to_drafts) {
1137 AppendImportantMessage(_("Message has been saved to Drafts.\n"), -1);
1138 gotoroom(WCC->CurRoom.name);
1140 readloop(readnew, eUseDefault);
1143 } else if ( (havebstr("recp"))
1144 || (havebstr("cc" ))
1145 || (havebstr("bcc" ))
1147 AppendImportantMessage(_("Message has been sent.\n"), -1);
1150 AppendImportantMessage(_("Message has been posted.\n"), -1);
1152 dont_post = lbstr("postseq");
1154 syslog(9, "%s:%d: server post error: %s", __FILE__, __LINE__, ChrPtr(Buf) + 4);
1155 AppendImportantMessage(ChrPtr(Buf) + 4, StrLength(Buf) - 4);
1157 if (saving_to_drafts) gotoroom(WCC->CurRoom.name);
1171 DeleteHash(&WCC->attachments);
1174 * We may have been supplied with instructions regarding the location
1175 * to which we must return after posting. If found, go there.
1177 if (havebstr("return_to")) {
1178 http_redirect(bstr("return_to"));
1181 * If we were editing a page in a wiki room, go to that page now.
1183 else if (havebstr("page")) {
1184 snprintf(buf, sizeof buf, "wiki?page=%s", bstr("page"));
1188 * Otherwise, just go to the "read messages" loop.
1192 readloop(readnew, eUseDefault);
1198 * Client is uploading an attachment
1200 void upload_attachment(void) {
1201 wcsession *WCC = WC;
1207 wc_mime_attachment *att;
1209 syslog(9, "upload_attachment()\n");
1210 wc_printf("upload_attachment()<br>\n");
1212 if (WCC->upload_length <= 0) {
1213 syslog(9, "ERROR no attachment was uploaded\n");
1214 wc_printf("ERROR no attachment was uploaded<br>\n");
1218 syslog(9, "Client is uploading %d bytes\n", WCC->upload_length);
1219 wc_printf("Client is uploading %d bytes<br>\n", WCC->upload_length);
1220 att = 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);
1226 if (WCC->attachments == NULL) {
1227 WCC->attachments = NewHash(1, Flathash);
1230 /* And add it to the list. */
1232 if ((GetCount(WCC->attachments) > 0) &&
1233 GetHashAt(WCC->attachments,
1234 GetCount(WCC->attachments) -1,
1235 &newnlen, &newn, &v))
1236 n = *((int*) newn) + 1;
1237 Put(WCC->attachments, IKEY(n), att, DestroyMime);
1240 * Mozilla sends a simple filename, which is what we want,
1241 * but Satan's Browser sends an entire pathname. Reduce
1242 * the path to just a filename if we need to.
1244 pch = strrchr(ChrPtr(att->FileName), '/');
1246 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1248 pch = strrchr(ChrPtr(att->FileName), '\\');
1250 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1254 * Transfer control of this memory from the upload struct
1255 * to the attachment struct.
1257 att->Data = WCC->upload;
1259 WCC->upload_length = 0;
1264 * Remove an attachment from the message currently being composed.
1266 * Currently we identify the attachment to be removed by its filename.
1267 * There is probably a better way to do this.
1269 void remove_attachment(void) {
1270 wcsession *WCC = WC;
1271 wc_mime_attachment *att;
1273 StrBuf *WhichAttachment;
1278 WhichAttachment = NewStrBufDup(sbstr("which_attachment"));
1279 StrBufUnescape(WhichAttachment, 0);
1280 at = GetNewHashPos(WCC->attachments, 0);
1282 GetHashPos(WCC->attachments, at, &len, &key, &vAtt);
1284 att = (wc_mime_attachment*) vAtt;
1285 if ((att != NULL) &&
1286 (strcmp(ChrPtr(WhichAttachment),
1287 ChrPtr(att->FileName) ) == 0))
1289 DeleteEntryFromHash(WCC->attachments, at);
1293 while (NextHashPos(WCC->attachments, at));
1294 FreeStrBuf(&WhichAttachment);
1295 wc_printf("remove_attachment() completed\n");
1299 long FourHash(const char *key, long length)
1303 const unsigned char *ptr = (const unsigned char*)key;
1305 for (i = 0; i < 4; i++, ptr ++)
1325 const char *ReplyToModeStrings [3] = {
1330 typedef enum _eReplyToNodes {
1337 * display the message entry screen
1339 void display_enter(void)
1341 const char *ReplyingModeStr;
1342 eReplyToNodes ReplyMode = eReply;
1346 const StrBuf *display_name = NULL;
1347 int recipient_required = 0;
1348 int subject_required = 0;
1349 int is_anonymous = 0;
1350 wcsession *WCC = WC;
1354 if (havebstr("force_room")) {
1355 gotoroom(sbstr("force_room"));
1358 display_name = sbstr("display_name");
1359 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1360 display_name = NULL;
1365 * First, do we have permission to enter messages in this room at all?
1368 serv_puts("ENT0 0");
1369 StrBuf_ServGetln(Line);
1370 rc = GetServerStatusMsg(Line, &Result, 0, 2);
1372 if (Result == 570) { /* 570 means that we need a recipient here */
1373 recipient_required = 1;
1375 else if (rc != 2) { /* Any other error means that we cannot continue */
1376 rc = GetServerStatusMsg(Line, &Result, 0, 2);
1378 readloop(readnew, eUseDefault);
1383 /* Is the server strongly recommending that the user enter a message subject? */
1384 if (StrLength(Line) > 4) {
1385 subject_required = extract_int(ChrPtr(Line) + 4, 1);
1389 * Are we perhaps in an address book view? If so, then an "enter
1390 * message" command really means "add new entry."
1392 if (WCC->CurRoom.defview == VIEW_ADDRESSBOOK) {
1393 do_edit_vcard(-1, "", NULL, NULL, "", ChrPtr(WCC->CurRoom.name));
1399 * Are we perhaps in a calendar room? If so, then an "enter
1400 * message" command really means "add new calendar item."
1402 if (WCC->CurRoom.defview == VIEW_CALENDAR) {
1403 display_edit_event();
1409 * Are we perhaps in a tasks view? If so, then an "enter
1410 * message" command really means "add new task."
1412 if (WCC->CurRoom.defview == VIEW_TASKS) {
1413 display_edit_task();
1419 ReplyingModeStr = bstr("replying_mode");
1420 if (ReplyingModeStr != NULL) for (i = 0; i < 3; i++) {
1421 if (strcmp(ReplyingModeStr, ReplyToModeStrings[i]) == 0) {
1422 ReplyMode = (eReplyToNodes) i;
1429 * If the "replying_to" variable is set, it refers to a message
1430 * number from which we must extract some header fields...
1432 replying_to = lbstr("replying_to");
1433 if (replying_to > 0) {
1435 StrBuf *wefw = NULL;
1436 StrBuf *msgn = NULL;
1437 StrBuf *from = NULL;
1438 StrBuf *node = NULL;
1439 StrBuf *rfca = NULL;
1440 StrBuf *rcpt = NULL;
1441 StrBuf *cccc = NULL;
1442 StrBuf *replyto = NULL;
1443 serv_printf("MSG0 %ld|1", replying_to);
1445 StrBuf_ServGetln(Line);
1446 if (GetServerStatusMsg(Line, NULL, 0, 0) == 1)
1447 while (len = StrBuf_ServGetln(Line),
1450 strcmp(ChrPtr(Line), "000")))
1453 if ((StrLength(Line) > 4) &&
1454 (ChrPtr(Line)[4] == '='))
1455 which = FourHash(ChrPtr(Line), 4);
1457 if (which == l_subj)
1459 StrBuf *subj = NewStrBuf();
1460 StrBuf *FlatSubject;
1462 if (ReplyMode == eForward) {
1463 if (strncasecmp(ChrPtr(Line) + 5, "Fw:", 3)) {
1464 StrBufAppendBufPlain(subj, HKEY("Fw: "), 0);
1468 if (strncasecmp(ChrPtr(Line) + 5, "Re:", 3)) {
1469 StrBufAppendBufPlain(subj, HKEY("Re: "), 0);
1472 StrBufAppendBufPlain(subj,
1474 StrLength(Line) - 5, 0);
1475 FlatSubject = NewStrBufPlain(NULL, StrLength(subj));
1476 StrBuf_RFC822_to_Utf8(FlatSubject, subj, NULL, NULL);
1478 PutBstr(HKEY("subject"), FlatSubject);
1481 else if (which == l_wefw)
1486 wefw = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1488 /* Trim down excessively long lists of thread references. We eliminate the
1489 * second one in the list so that the thread root remains intact.
1491 rrtok = num_tokens(ChrPtr(wefw), '|');
1492 rrlen = StrLength(wefw);
1493 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
1494 StrBufRemove_token(wefw, 1, '|');
1498 else if (which == l_msgn) {
1499 msgn = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1502 else if (which == l_from) {
1504 from = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1505 FlatFrom = NewStrBufPlain(NULL, StrLength(from));
1506 StrBuf_RFC822_to_Utf8(FlatFrom, from, NULL, NULL);
1509 for (i=0; i<StrLength(from); ++i) {
1510 if (ChrPtr(from)[i] == ',')
1511 StrBufPeek(from, NULL, i, ' ');
1515 else if (which == l_rcpt) {
1516 rcpt = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1519 else if (which == l_cccc) {
1520 cccc = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1523 else if (which == l_node) {
1524 node = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1526 else if (which == l_replyto) {
1527 replyto = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1529 else if (which == l_rfca) {
1531 rfca = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1532 FlatRFCA = NewStrBufPlain(NULL, StrLength(rfca));
1533 StrBuf_RFC822_to_Utf8(FlatRFCA, rfca, NULL, NULL);
1540 if (StrLength(wefw) + StrLength(msgn) > 0) {
1541 StrBuf *refs = NewStrBuf();
1542 if (StrLength(wefw) > 0) {
1543 StrBufAppendBuf(refs, wefw, 0);
1545 if ( (StrLength(wefw) > 0) &&
1546 (StrLength(msgn) > 0) )
1548 StrBufAppendBufPlain(refs, HKEY("|"), 0);
1550 if (StrLength(msgn) > 0) {
1551 StrBufAppendBuf(refs, msgn, 0);
1553 PutBstr(HKEY("references"), refs);
1557 * If this is a Reply or a ReplyAll, copy the sender's email into the To: field
1559 if ((ReplyMode == eReply) || (ReplyMode == eReplyAll))
1562 if ((StrLength(replyto) > 0) && (ReplyMode == eReplyAll)) {
1563 to_rcpt = NewStrBuf();
1564 StrBufAppendBuf(to_rcpt, replyto, 0);
1566 else if (StrLength(rfca) > 0) {
1567 to_rcpt = NewStrBuf();
1568 StrBufAppendBuf(to_rcpt, from, 0);
1569 StrBufAppendBufPlain(to_rcpt, HKEY(" <"), 0);
1570 StrBufAppendBuf(to_rcpt, rfca, 0);
1571 StrBufAppendBufPlain(to_rcpt, HKEY(">"), 0);
1576 if ( (StrLength(node) > 0)
1577 && (strcasecmp(ChrPtr(node), ChrPtr(WC->serv_info->serv_nodename)))
1579 StrBufAppendBufPlain(to_rcpt, HKEY(" @ "), 0);
1580 StrBufAppendBuf(to_rcpt, node, 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)
1591 StrBuf *cc_rcpt = rcpt;
1593 if ((StrLength(cccc) > 0) && (StrLength(replyto) == 0))
1595 if (cc_rcpt != NULL) {
1596 StrBufAppendPrintf(cc_rcpt, ", ");
1597 StrBufAppendBuf(cc_rcpt, cccc, 0);
1603 if (cc_rcpt != NULL)
1604 PutBstr(HKEY("cc"), cc_rcpt);
1616 * Otherwise proceed normally.
1617 * Do a custom room banner with no navbar...
1620 if (recipient_required) {
1621 const StrBuf *Recp = NULL;
1622 const StrBuf *Cc = NULL;
1623 const StrBuf *Bcc = NULL;
1624 char *wikipage = NULL;
1625 StrBuf *CmdBuf = NULL;
1626 const char CMD[] = "ENT0 0|%s|%d|0||%s||%s|%s|%s";
1628 Recp = sbstr("recp");
1631 wikipage = strdup(bstr("page"));
1632 str_wiki_index(wikipage);
1634 CmdBuf = NewStrBufPlain(NULL,
1637 StrLength(display_name) +
1642 StrBufPrintf(CmdBuf,
1646 ChrPtr(display_name),
1651 serv_puts(ChrPtr(CmdBuf));
1652 StrBuf_ServGetln(CmdBuf);
1655 rc = GetServerStatusMsg(CmdBuf, &Result, 0, 0);
1657 if ( (Result == 570) /* invalid or missing recipient(s) */
1658 || (Result == 550) /* higher access required to send Internet mail */
1660 /* These errors will have been displayed and are excusable */
1662 else if (rc != 2) { /* Any other error means that we cannot continue */
1663 AppendImportantMessage(ChrPtr(CmdBuf) + 4, StrLength(CmdBuf) - 4);
1664 FreeStrBuf(&CmdBuf);
1666 readloop(readnew, eUseDefault);
1669 FreeStrBuf(&CmdBuf);
1671 if (recipient_required)
1672 PutBstr(HKEY("__RCPTREQUIRED"), NewStrBufPlain(HKEY("1")));
1673 if (recipient_required || subject_required)
1674 PutBstr(HKEY("__SUBJREQUIRED"), NewStrBufPlain(HKEY("1")));
1677 output_headers(1, 0, 0, 0, 1, 0);
1678 DoTemplate(HKEY("edit_message"), NULL, &NoCtx);
1687 void delete_msg(void)
1692 msgid = lbstr("msgid");
1694 if ((WC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
1695 serv_printf("DELE %ld", msgid);
1697 else { /* Otherwise move it to Trash */
1698 serv_printf("MOVE %ld|_TRASH_|0", msgid);
1701 StrBuf_ServGetln(Line);
1702 GetServerStatusMsg(Line, NULL, 1, 0);
1706 readloop(readnew, eUseDefault);
1711 * move a message to another room
1717 msgid = lbstr("msgid");
1719 if (havebstr("move_button")) {
1721 serv_printf("MOVE %ld|%s", msgid, bstr("target_room"));
1723 StrBuf_ServGetln(Line);
1724 GetServerStatusMsg(Line, NULL, 1, 0);
1727 AppendImportantMessage(_("The message was not moved."), -1);
1731 readloop(readnew, eUseDefault);
1737 * Generic function to output an arbitrary MIME attachment from
1738 * message being composed
1740 * partnum The MIME part to be output
1741 * filename Fake filename to give
1742 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1744 void postpart(StrBuf *partnum, StrBuf *filename, int force_download)
1747 StrBuf *content_type;
1748 wc_mime_attachment *part;
1751 i = StrToi(partnum);
1752 if (GetHash(WC->attachments, IKEY(i), &vPart) &&
1754 part = (wc_mime_attachment*) vPart;
1755 if (force_download) {
1756 content_type = NewStrBufPlain(HKEY("application/octet-stream"));
1759 content_type = NewStrBufDup(part->ContentType);
1761 StrBufAppendBuf(WC->WBuf, part->Data, 0);
1762 http_transmit_thing(ChrPtr(content_type), 0);
1764 hprintf("HTTP/1.1 404 %s\n", ChrPtr(partnum));
1765 output_headers(0, 0, 0, 0, 0, 0);
1766 hprintf("Content-Type: text/plain\r\n");
1768 wc_printf(_("An error occurred while retrieving this part: %s/%s\n"),
1769 ChrPtr(partnum), ChrPtr(filename));
1772 FreeStrBuf(&content_type);
1777 * Generic function to output an arbitrary MIME part from an arbitrary
1778 * message number on the server.
1780 * msgnum Number of the item on the citadel server
1781 * partnum The MIME part to be output
1782 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1784 void mimepart(int force_download)
1789 wcsession *WCC = WC;
1792 StrBuf *ContentType = NewStrBufPlain(HKEY("application/octet-stream"));
1795 att = Buf = NewStrBuf();
1796 msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
1797 StrBufExtract_token(att, WCC->Hdr->HR.ReqLine, 1, '/');
1799 serv_printf("OPNA %ld|%s", msgnum, ChrPtr(att));
1800 StrBuf_ServGetln(Buf);
1801 if (GetServerStatus(Buf, &ErrorDetail) == 2) {
1802 StrBufCutLeft(Buf, 4);
1803 bytes = StrBufExtract_long(Buf, 0, '|');
1804 if (!force_download) {
1805 StrBufExtract_token(ContentType, Buf, 3, '|');
1808 serv_read_binary(WCC->WBuf, bytes, Buf);
1810 StrBuf_ServGetln(Buf);
1811 CT = ChrPtr(ContentType);
1813 if (!force_download) {
1814 if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
1815 StrBufExtract_token(Buf, WCC->Hdr->HR.ReqLine, 2, '/');
1816 CT = GuessMimeByFilename(SKEY(Buf));
1818 if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
1819 CT = GuessMimeType(SKEY(WCC->WBuf));
1822 http_transmit_thing(CT, 0);
1824 StrBufCutLeft(Buf, 4);
1825 switch (ErrorDetail) {
1827 case ERROR + MESSAGE_NOT_FOUND:
1828 hprintf("HTTP/1.1 404 %s\n", ChrPtr(Buf));
1830 case ERROR + NOT_LOGGED_IN:
1831 hprintf("HTTP/1.1 401 %s\n", ChrPtr(Buf));
1834 case ERROR + HIGHER_ACCESS_REQUIRED:
1835 hprintf("HTTP/1.1 403 %s\n", ChrPtr(Buf));
1837 case ERROR + INTERNAL_ERROR:
1838 case ERROR + TOO_BIG:
1839 hprintf("HTTP/1.1 500 %s\n", ChrPtr(Buf));
1842 output_headers(0, 0, 0, 0, 0, 0);
1843 hprintf("Content-Type: text/plain\r\n");
1845 wc_printf(_("An error occurred while retrieving this part: %s\n"),
1849 FreeStrBuf(&ContentType);
1855 * Read any MIME part of a message, from the server, into memory.
1857 StrBuf *load_mimepart(long msgnum, char *partnum)
1863 serv_printf("DLAT %ld|%s", msgnum, partnum);
1864 StrBuf_ServGetln(Buf);
1865 if (GetServerStatus(Buf, NULL) == 6) {
1866 StrBufCutLeft(Buf, 4);
1867 bytes = StrBufExtract_long(Buf, 0, '|');
1870 StrBuf_ServGetBLOBBuffered(Buf, bytes);
1880 * Read any MIME part of a message, from the server, into memory.
1882 void MimeLoadData(wc_mime_attachment *Mime)
1887 /* TODO: is there a chance the content type is different from the one we know? */
1889 serv_printf("DLAT %ld|%s", Mime->msgnum, ChrPtr(Mime->PartNum));
1891 StrBuf_ServGetln(Buf);
1892 if (GetServerStatus(Buf, NULL) == 6) {
1893 Ptr = &(ChrPtr(Buf)[4]);
1894 bytes = StrBufExtractNext_long(Buf, &Ptr, '|');
1895 StrBufSkip_NTokenS(Buf, &Ptr, '|', 3); /* filename, cbtype, mimetype */
1896 if (Mime->Charset == NULL) Mime->Charset = NewStrBuf();
1897 StrBufExtract_NextToken(Mime->Charset, Buf, &Ptr, '|');
1899 if (Mime->Data == NULL)
1900 Mime->Data = NewStrBufPlain(NULL, bytes);
1901 StrBuf_ServGetBLOBBuffered(Mime->Data, bytes);
1904 FlushStrBuf(Mime->Data);
1905 /* TODO XImportant message */
1911 void view_mimepart(void) {
1915 void download_mimepart(void) {
1919 void view_postpart(void) {
1920 StrBuf *filename = NewStrBuf();
1921 StrBuf *partnum = NewStrBuf();
1923 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1924 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1926 postpart(partnum, filename, 0);
1928 FreeStrBuf(&filename);
1929 FreeStrBuf(&partnum);
1932 void download_postpart(void) {
1933 StrBuf *filename = NewStrBuf();
1934 StrBuf *partnum = NewStrBuf();
1936 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1937 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1939 postpart(partnum, filename, 1);
1941 FreeStrBuf(&filename);
1942 FreeStrBuf(&partnum);
1947 void show_num_attachments(void) {
1948 wc_printf("%d", GetCount(WC->attachments));
1952 void h_readnew(void) { readloop(readnew, eUseDefault);}
1953 void h_readold(void) { readloop(readold, eUseDefault);}
1954 void h_readfwd(void) { readloop(readfwd, eUseDefault);}
1955 void h_headers(void) { readloop(headers, eUseDefault);}
1956 void h_do_search(void) { readloop(do_search, eUseDefault);}
1957 void h_readgt(void) { readloop(readgt, eUseDefault);}
1958 void h_readlt(void) { readloop(readlt, eUseDefault);}
1962 /* Output message list in JSON format */
1963 void jsonMessageList(void) {
1964 StrBuf *View = NewStrBuf();
1965 const StrBuf *room = sbstr("room");
1966 long oper = (havebstr("query")) ? do_search : readnew;
1967 StrBufPrintf(View, "%d", VIEW_JSON_LIST);
1968 putbstr("view", View);;
1970 readloop(oper, eUseDefault);
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
1984 RoomRenderer *Handler;
1986 Handler = (RoomRenderer*) malloc(sizeof(RoomRenderer));
1988 Handler->RoomType = RoomType;
1989 Handler->GetParamsGetServerCall = GetParamsGetServerCall;
1990 Handler->PrintPageHeader = PrintPageHeader;
1991 Handler->PrintViewHeader = PrintViewHeader;
1992 Handler->LoadMsgFromServer = LoadMsgFromServer;
1993 Handler->RenderView_or_Tail = RenderView_or_Tail;
1994 Handler->ViewCleanup = ViewCleanup;
1995 Handler->LHParse = LH;
1997 Put(ReadLoopHandler, IKEY(RoomType), Handler, NULL);
2004 RegisterPreference("use_sig",
2005 _("Attach signature to email messages?"),
2008 RegisterPreference("signature", _("Use this signature:"), PRF_QP_STRING, NULL);
2009 RegisterPreference("default_header_charset",
2010 _("Default character set for email headers:"),
2013 RegisterPreference("defaultfrom", _("Preferred email address"), PRF_STRING, NULL);
2014 RegisterPreference("defaultname",
2015 _("Preferred display name for email messages"),
2018 RegisterPreference("defaulthandle",
2019 _("Preferred display name for bulletin board posts"),
2022 RegisterPreference("mailbox",_("Mailbox view mode"), PRF_STRING, NULL);
2024 WebcitAddUrlHandler(HKEY("readnew"), "", 0, h_readnew, ANONYMOUS|NEED_URL);
2025 WebcitAddUrlHandler(HKEY("readold"), "", 0, h_readold, ANONYMOUS|NEED_URL);
2026 WebcitAddUrlHandler(HKEY("readfwd"), "", 0, h_readfwd, ANONYMOUS|NEED_URL);
2027 WebcitAddUrlHandler(HKEY("headers"), "", 0, h_headers, NEED_URL);
2028 WebcitAddUrlHandler(HKEY("readgt"), "", 0, h_readgt, ANONYMOUS|NEED_URL);
2029 WebcitAddUrlHandler(HKEY("readlt"), "", 0, h_readlt, ANONYMOUS|NEED_URL);
2030 WebcitAddUrlHandler(HKEY("do_search"), "", 0, h_do_search, 0);
2031 WebcitAddUrlHandler(HKEY("display_enter"), "", 0, display_enter, 0);
2032 WebcitAddUrlHandler(HKEY("post"), "", 0, post_message, PROHIBIT_STARTPAGE);
2033 WebcitAddUrlHandler(HKEY("move_msg"), "", 0, move_msg, PROHIBIT_STARTPAGE);
2034 WebcitAddUrlHandler(HKEY("delete_msg"), "", 0, delete_msg, PROHIBIT_STARTPAGE);
2035 WebcitAddUrlHandler(HKEY("msg"), "", 0, embed_message, NEED_URL);
2036 WebcitAddUrlHandler(HKEY("message"), "", 0, handle_one_message, NEED_URL|XHTTP_COMMANDS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
2037 WebcitAddUrlHandler(HKEY("printmsg"), "", 0, print_message, NEED_URL);
2038 WebcitAddUrlHandler(HKEY("msgheaders"), "", 0, display_headers, NEED_URL);
2040 WebcitAddUrlHandler(HKEY("mimepart"), "", 0, view_mimepart, NEED_URL);
2041 WebcitAddUrlHandler(HKEY("mimepart_download"), "", 0, download_mimepart, NEED_URL);
2042 WebcitAddUrlHandler(HKEY("postpart"), "", 0, view_postpart, NEED_URL|PROHIBIT_STARTPAGE);
2043 WebcitAddUrlHandler(HKEY("postpart_download"), "", 0, download_postpart, NEED_URL|PROHIBIT_STARTPAGE);
2044 WebcitAddUrlHandler(HKEY("upload_attachment"), "", 0, upload_attachment, AJAX);
2045 WebcitAddUrlHandler(HKEY("remove_attachment"), "", 0, remove_attachment, AJAX);
2046 WebcitAddUrlHandler(HKEY("show_num_attachments"), "", 0, show_num_attachments, AJAX);
2049 WebcitAddUrlHandler(HKEY("roommsgs"), "", 0, jsonMessageList,0);
2051 l_subj = FourHash("subj", 4);
2052 l_wefw = FourHash("wefw", 4);
2053 l_msgn = FourHash("msgn", 4);
2054 l_from = FourHash("from", 4);
2055 l_rcpt = FourHash("rcpt", 4);
2056 l_cccc = FourHash("cccc", 4);
2057 l_replyto = FourHash("rep2", 4);
2058 l_node = FourHash("node", 4);
2059 l_rfca = FourHash("rfca", 4);
2065 SessionDetachModule_MSG
2068 DeleteHash(&sess->summ);