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 * display the message entry screen
1327 void display_enter(void)
1332 const StrBuf *display_name = NULL;
1333 int recipient_required = 0;
1334 int subject_required = 0;
1335 int is_anonymous = 0;
1336 wcsession *WCC = WC;
1340 if (havebstr("force_room")) {
1341 gotoroom(sbstr("force_room"));
1344 display_name = sbstr("display_name");
1345 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1346 display_name = NULL;
1351 * First, do we have permission to enter messages in this room at all?
1354 serv_puts("ENT0 0");
1355 StrBuf_ServGetln(Line);
1356 rc = GetServerStatusMsg(Line, &Result, 0, 2);
1358 if (Result == 570) { /* 570 means that we need a recipient here */
1359 recipient_required = 1;
1361 else if (rc != 2) { /* Any other error means that we cannot continue */
1362 rc = GetServerStatusMsg(Line, &Result, 0, 2);
1364 readloop(readnew, eUseDefault);
1369 /* Is the server strongly recommending that the user enter a message subject? */
1370 if (StrLength(Line) > 4) {
1371 subject_required = extract_int(ChrPtr(Line) + 4, 1);
1375 * Are we perhaps in an address book view? If so, then an "enter
1376 * message" command really means "add new entry."
1378 if (WCC->CurRoom.defview == VIEW_ADDRESSBOOK) {
1379 do_edit_vcard(-1, "", NULL, NULL, "", ChrPtr(WCC->CurRoom.name));
1385 * Are we perhaps in a calendar room? If so, then an "enter
1386 * message" command really means "add new calendar item."
1388 if (WCC->CurRoom.defview == VIEW_CALENDAR) {
1389 display_edit_event();
1395 * Are we perhaps in a tasks view? If so, then an "enter
1396 * message" command really means "add new task."
1398 if (WCC->CurRoom.defview == VIEW_TASKS) {
1399 display_edit_task();
1406 * If the "replying_to" variable is set, it refers to a message
1407 * number from which we must extract some header fields...
1409 replying_to = lbstr("replying_to");
1410 if (replying_to > 0) {
1412 StrBuf *wefw = NULL;
1413 StrBuf *msgn = NULL;
1414 StrBuf *from = NULL;
1415 StrBuf *node = NULL;
1416 StrBuf *rfca = NULL;
1417 StrBuf *rcpt = NULL;
1418 StrBuf *cccc = NULL;
1419 serv_printf("MSG0 %ld|1", replying_to);
1421 StrBuf_ServGetln(Line);
1422 if (GetServerStatusMsg(Line, NULL, 0, 0) == 1)
1423 while (len = StrBuf_ServGetln(Line),
1426 strcmp(ChrPtr(Line), "000")))
1429 if ((StrLength(Line) > 4) &&
1430 (ChrPtr(Line)[4] == '='))
1431 which = FourHash(ChrPtr(Line), 4);
1433 if (which == l_subj)
1435 StrBuf *subj = NewStrBuf();
1436 StrBuf *FlatSubject;
1438 if (!strcasecmp(bstr("replying_mode"), "forward")) {
1439 if (strncasecmp(ChrPtr(Line) + 5, "Fw:", 3)) {
1440 StrBufAppendBufPlain(subj, HKEY("Fw: "), 0);
1444 if (strncasecmp(ChrPtr(Line) + 5, "Re:", 3)) {
1445 StrBufAppendBufPlain(subj, HKEY("Re: "), 0);
1448 StrBufAppendBufPlain(subj,
1450 StrLength(Line) - 5, 0);
1451 FlatSubject = NewStrBufPlain(NULL, StrLength(subj));
1452 StrBuf_RFC822_to_Utf8(FlatSubject, subj, NULL, NULL);
1454 PutBstr(HKEY("subject"), FlatSubject);
1457 else if (which == l_wefw)
1462 wefw = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1464 /* Trim down excessively long lists of thread references. We eliminate the
1465 * second one in the list so that the thread root remains intact.
1467 rrtok = num_tokens(ChrPtr(wefw), '|');
1468 rrlen = StrLength(wefw);
1469 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
1470 StrBufRemove_token(wefw, 1, '|');
1474 else if (which == l_msgn) {
1475 msgn = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1478 else if (which == l_from) {
1480 from = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1481 FlatFrom = NewStrBufPlain(NULL, StrLength(from));
1482 StrBuf_RFC822_to_Utf8(FlatFrom, from, NULL, NULL);
1485 for (i=0; i<StrLength(from); ++i) {
1486 if (ChrPtr(from)[i] == ',')
1487 StrBufPeek(from, NULL, i, ' ');
1491 else if (which == l_rcpt) {
1492 rcpt = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1495 else if (which == l_cccc) {
1496 cccc = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1499 else if (which == l_node) {
1500 node = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1503 else if (which == l_rfca) {
1505 rfca = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1506 FlatRFCA = NewStrBufPlain(NULL, StrLength(rfca));
1507 StrBuf_RFC822_to_Utf8(FlatRFCA, rfca, NULL, NULL);
1514 if (StrLength(wefw) + StrLength(msgn) > 0) {
1515 StrBuf *refs = NewStrBuf();
1516 if (StrLength(wefw) > 0) {
1517 StrBufAppendBuf(refs, wefw, 0);
1519 if ( (StrLength(wefw) > 0) &&
1520 (StrLength(msgn) > 0) )
1522 StrBufAppendBufPlain(refs, HKEY("|"), 0);
1524 if (StrLength(msgn) > 0) {
1525 StrBufAppendBuf(refs, msgn, 0);
1527 PutBstr(HKEY("references"), refs);
1531 * If this is a Reply or a ReplyAll, copy the sender's email into the To: field
1533 if ( (!strcasecmp(bstr("replying_mode"), "reply"))
1534 || (!strcasecmp(bstr("replying_mode"), "replyall"))
1537 if (StrLength(rfca) > 0) {
1538 to_rcpt = NewStrBuf();
1539 StrBufAppendBuf(to_rcpt, from, 0);
1540 StrBufAppendBufPlain(to_rcpt, HKEY(" <"), 0);
1541 StrBufAppendBuf(to_rcpt, rfca, 0);
1542 StrBufAppendBufPlain(to_rcpt, HKEY(">"), 0);
1547 if ( (StrLength(node) > 0)
1548 && (strcasecmp(ChrPtr(node), ChrPtr(WC->serv_info->serv_nodename)))
1550 StrBufAppendBufPlain(to_rcpt, HKEY(" @ "), 0);
1551 StrBufAppendBuf(to_rcpt, node, 0);
1554 PutBstr(HKEY("recp"), to_rcpt);
1558 * Only if this is a ReplyAll, copy all recipients into the Cc: field
1560 if ( (!strcasecmp(bstr("replying_mode"), "replyall"))
1562 StrBuf *cc_rcpt = rcpt;
1564 if (StrLength(cccc) > 0) {
1565 if (cc_rcpt != NULL) {
1566 StrBufAppendPrintf(cc_rcpt, ", ");
1567 StrBufAppendBuf(cc_rcpt, cccc, 0);
1573 if (cc_rcpt != NULL)
1574 PutBstr(HKEY("cc"), cc_rcpt);
1586 * Otherwise proceed normally.
1587 * Do a custom room banner with no navbar...
1590 if (recipient_required) {
1591 const StrBuf *Recp = NULL;
1592 const StrBuf *Cc = NULL;
1593 const StrBuf *Bcc = NULL;
1594 char *wikipage = NULL;
1595 StrBuf *CmdBuf = NULL;
1596 const char CMD[] = "ENT0 0|%s|%d|0||%s||%s|%s|%s";
1598 Recp = sbstr("recp");
1601 wikipage = strdup(bstr("page"));
1602 str_wiki_index(wikipage);
1604 CmdBuf = NewStrBufPlain(NULL,
1607 StrLength(display_name) +
1612 StrBufPrintf(CmdBuf,
1616 ChrPtr(display_name),
1621 serv_puts(ChrPtr(CmdBuf));
1622 StrBuf_ServGetln(CmdBuf);
1625 rc = GetServerStatusMsg(CmdBuf, &Result, 0, 0);
1627 if ( (Result == 570) /* invalid or missing recipient(s) */
1628 || (Result == 550) /* higher access required to send Internet mail */
1630 /* These errors will have been displayed and are excusable */
1632 else if (rc != 2) { /* Any other error means that we cannot continue */
1633 AppendImportantMessage(ChrPtr(CmdBuf) + 4, StrLength(CmdBuf) - 4);
1634 FreeStrBuf(&CmdBuf);
1636 readloop(readnew, eUseDefault);
1639 FreeStrBuf(&CmdBuf);
1641 if (recipient_required)
1642 PutBstr(HKEY("__RCPTREQUIRED"), NewStrBufPlain(HKEY("1")));
1643 if (recipient_required || subject_required)
1644 PutBstr(HKEY("__SUBJREQUIRED"), NewStrBufPlain(HKEY("1")));
1647 output_headers(1, 0, 0, 0, 1, 0);
1648 DoTemplate(HKEY("edit_message"), NULL, &NoCtx);
1657 void delete_msg(void)
1662 msgid = lbstr("msgid");
1664 if ((WC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
1665 serv_printf("DELE %ld", msgid);
1667 else { /* Otherwise move it to Trash */
1668 serv_printf("MOVE %ld|_TRASH_|0", msgid);
1671 StrBuf_ServGetln(Line);
1672 GetServerStatusMsg(Line, NULL, 1, 0);
1676 readloop(readnew, eUseDefault);
1681 * move a message to another room
1687 msgid = lbstr("msgid");
1689 if (havebstr("move_button")) {
1691 serv_printf("MOVE %ld|%s", msgid, bstr("target_room"));
1693 StrBuf_ServGetln(Line);
1694 GetServerStatusMsg(Line, NULL, 1, 0);
1697 AppendImportantMessage(_("The message was not moved."), -1);
1701 readloop(readnew, eUseDefault);
1707 * Generic function to output an arbitrary MIME attachment from
1708 * message being composed
1710 * partnum The MIME part to be output
1711 * filename Fake filename to give
1712 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1714 void postpart(StrBuf *partnum, StrBuf *filename, int force_download)
1717 StrBuf *content_type;
1718 wc_mime_attachment *part;
1721 i = StrToi(partnum);
1722 if (GetHash(WC->attachments, IKEY(i), &vPart) &&
1724 part = (wc_mime_attachment*) vPart;
1725 if (force_download) {
1726 content_type = NewStrBufPlain(HKEY("application/octet-stream"));
1729 content_type = NewStrBufDup(part->ContentType);
1731 StrBufAppendBuf(WC->WBuf, part->Data, 0);
1732 http_transmit_thing(ChrPtr(content_type), 0);
1734 hprintf("HTTP/1.1 404 %s\n", ChrPtr(partnum));
1735 output_headers(0, 0, 0, 0, 0, 0);
1736 hprintf("Content-Type: text/plain\r\n");
1738 wc_printf(_("An error occurred while retrieving this part: %s/%s\n"),
1739 ChrPtr(partnum), ChrPtr(filename));
1742 FreeStrBuf(&content_type);
1747 * Generic function to output an arbitrary MIME part from an arbitrary
1748 * message number on the server.
1750 * msgnum Number of the item on the citadel server
1751 * partnum The MIME part to be output
1752 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1754 void mimepart(int force_download)
1759 wcsession *WCC = WC;
1762 StrBuf *ContentType = NewStrBufPlain(HKEY("application/octet-stream"));
1765 att = Buf = NewStrBuf();
1766 msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
1767 StrBufExtract_token(att, WCC->Hdr->HR.ReqLine, 1, '/');
1769 serv_printf("OPNA %ld|%s", msgnum, ChrPtr(att));
1770 StrBuf_ServGetln(Buf);
1771 if (GetServerStatus(Buf, &ErrorDetail) == 2) {
1772 StrBufCutLeft(Buf, 4);
1773 bytes = StrBufExtract_long(Buf, 0, '|');
1774 if (!force_download) {
1775 StrBufExtract_token(ContentType, Buf, 3, '|');
1778 serv_read_binary(WCC->WBuf, bytes, Buf);
1780 StrBuf_ServGetln(Buf);
1781 CT = ChrPtr(ContentType);
1783 if (!force_download) {
1784 if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
1785 StrBufExtract_token(Buf, WCC->Hdr->HR.ReqLine, 2, '/');
1786 CT = GuessMimeByFilename(SKEY(Buf));
1788 if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
1789 CT = GuessMimeType(SKEY(WCC->WBuf));
1792 http_transmit_thing(CT, 0);
1794 StrBufCutLeft(Buf, 4);
1795 switch (ErrorDetail) {
1797 case ERROR + MESSAGE_NOT_FOUND:
1798 hprintf("HTTP/1.1 404 %s\n", ChrPtr(Buf));
1800 case ERROR + NOT_LOGGED_IN:
1801 hprintf("HTTP/1.1 401 %s\n", ChrPtr(Buf));
1804 case ERROR + HIGHER_ACCESS_REQUIRED:
1805 hprintf("HTTP/1.1 403 %s\n", ChrPtr(Buf));
1807 case ERROR + INTERNAL_ERROR:
1808 case ERROR + TOO_BIG:
1809 hprintf("HTTP/1.1 500 %s\n", ChrPtr(Buf));
1812 output_headers(0, 0, 0, 0, 0, 0);
1813 hprintf("Content-Type: text/plain\r\n");
1815 wc_printf(_("An error occurred while retrieving this part: %s\n"),
1819 FreeStrBuf(&ContentType);
1825 * Read any MIME part of a message, from the server, into memory.
1827 StrBuf *load_mimepart(long msgnum, char *partnum)
1833 serv_printf("DLAT %ld|%s", msgnum, partnum);
1834 StrBuf_ServGetln(Buf);
1835 if (GetServerStatus(Buf, NULL) == 6) {
1836 StrBufCutLeft(Buf, 4);
1837 bytes = StrBufExtract_long(Buf, 0, '|');
1840 StrBuf_ServGetBLOBBuffered(Buf, bytes);
1850 * Read any MIME part of a message, from the server, into memory.
1852 void MimeLoadData(wc_mime_attachment *Mime)
1857 /* TODO: is there a chance the content type is different from the one we know? */
1859 serv_printf("DLAT %ld|%s", Mime->msgnum, ChrPtr(Mime->PartNum));
1861 StrBuf_ServGetln(Buf);
1862 if (GetServerStatus(Buf, NULL) == 6) {
1863 Ptr = &(ChrPtr(Buf)[4]);
1864 bytes = StrBufExtractNext_long(Buf, &Ptr, '|');
1865 StrBufSkip_NTokenS(Buf, &Ptr, '|', 3); /* filename, cbtype, mimetype */
1866 if (Mime->Charset == NULL) Mime->Charset = NewStrBuf();
1867 StrBufExtract_NextToken(Mime->Charset, Buf, &Ptr, '|');
1869 if (Mime->Data == NULL)
1870 Mime->Data = NewStrBufPlain(NULL, bytes);
1871 StrBuf_ServGetBLOBBuffered(Mime->Data, bytes);
1874 FlushStrBuf(Mime->Data);
1875 /* TODO XImportant message */
1881 void view_mimepart(void) {
1885 void download_mimepart(void) {
1889 void view_postpart(void) {
1890 StrBuf *filename = NewStrBuf();
1891 StrBuf *partnum = NewStrBuf();
1893 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1894 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1896 postpart(partnum, filename, 0);
1898 FreeStrBuf(&filename);
1899 FreeStrBuf(&partnum);
1902 void download_postpart(void) {
1903 StrBuf *filename = NewStrBuf();
1904 StrBuf *partnum = NewStrBuf();
1906 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1907 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1909 postpart(partnum, filename, 1);
1911 FreeStrBuf(&filename);
1912 FreeStrBuf(&partnum);
1917 void show_num_attachments(void) {
1918 wc_printf("%d", GetCount(WC->attachments));
1922 void h_readnew(void) { readloop(readnew, eUseDefault);}
1923 void h_readold(void) { readloop(readold, eUseDefault);}
1924 void h_readfwd(void) { readloop(readfwd, eUseDefault);}
1925 void h_headers(void) { readloop(headers, eUseDefault);}
1926 void h_do_search(void) { readloop(do_search, eUseDefault);}
1927 void h_readgt(void) { readloop(readgt, eUseDefault);}
1928 void h_readlt(void) { readloop(readlt, eUseDefault);}
1932 /* Output message list in JSON format */
1933 void jsonMessageList(void) {
1934 StrBuf *View = NewStrBuf();
1935 const StrBuf *room = sbstr("room");
1936 long oper = (havebstr("query")) ? do_search : readnew;
1937 StrBufPrintf(View, "%d", VIEW_JSON_LIST);
1938 putbstr("view", View);;
1940 readloop(oper, eUseDefault);
1943 void RegisterReadLoopHandlerset(
1945 GetParamsGetServerCall_func GetParamsGetServerCall,
1946 PrintViewHeader_func PrintPageHeader,
1947 PrintViewHeader_func PrintViewHeader,
1948 load_msg_ptrs_detailheaders LH,
1949 LoadMsgFromServer_func LoadMsgFromServer,
1950 RenderView_or_Tail_func RenderView_or_Tail,
1951 View_Cleanup_func ViewCleanup
1954 RoomRenderer *Handler;
1956 Handler = (RoomRenderer*) malloc(sizeof(RoomRenderer));
1958 Handler->RoomType = RoomType;
1959 Handler->GetParamsGetServerCall = GetParamsGetServerCall;
1960 Handler->PrintPageHeader = PrintPageHeader;
1961 Handler->PrintViewHeader = PrintViewHeader;
1962 Handler->LoadMsgFromServer = LoadMsgFromServer;
1963 Handler->RenderView_or_Tail = RenderView_or_Tail;
1964 Handler->ViewCleanup = ViewCleanup;
1965 Handler->LHParse = LH;
1967 Put(ReadLoopHandler, IKEY(RoomType), Handler, NULL);
1974 RegisterPreference("use_sig",
1975 _("Attach signature to email messages?"),
1978 RegisterPreference("signature", _("Use this signature:"), PRF_QP_STRING, NULL);
1979 RegisterPreference("default_header_charset",
1980 _("Default character set for email headers:"),
1983 RegisterPreference("defaultfrom", _("Preferred email address"), PRF_STRING, NULL);
1984 RegisterPreference("defaultname",
1985 _("Preferred display name for email messages"),
1988 RegisterPreference("defaulthandle",
1989 _("Preferred display name for bulletin board posts"),
1992 RegisterPreference("mailbox",_("Mailbox view mode"), PRF_STRING, NULL);
1994 WebcitAddUrlHandler(HKEY("readnew"), "", 0, h_readnew, ANONYMOUS|NEED_URL);
1995 WebcitAddUrlHandler(HKEY("readold"), "", 0, h_readold, ANONYMOUS|NEED_URL);
1996 WebcitAddUrlHandler(HKEY("readfwd"), "", 0, h_readfwd, ANONYMOUS|NEED_URL);
1997 WebcitAddUrlHandler(HKEY("headers"), "", 0, h_headers, NEED_URL);
1998 WebcitAddUrlHandler(HKEY("readgt"), "", 0, h_readgt, ANONYMOUS|NEED_URL);
1999 WebcitAddUrlHandler(HKEY("readlt"), "", 0, h_readlt, ANONYMOUS|NEED_URL);
2000 WebcitAddUrlHandler(HKEY("do_search"), "", 0, h_do_search, 0);
2001 WebcitAddUrlHandler(HKEY("display_enter"), "", 0, display_enter, 0);
2002 WebcitAddUrlHandler(HKEY("post"), "", 0, post_message, PROHIBIT_STARTPAGE);
2003 WebcitAddUrlHandler(HKEY("move_msg"), "", 0, move_msg, PROHIBIT_STARTPAGE);
2004 WebcitAddUrlHandler(HKEY("delete_msg"), "", 0, delete_msg, PROHIBIT_STARTPAGE);
2005 WebcitAddUrlHandler(HKEY("msg"), "", 0, embed_message, NEED_URL);
2006 WebcitAddUrlHandler(HKEY("message"), "", 0, handle_one_message, NEED_URL|XHTTP_COMMANDS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
2007 WebcitAddUrlHandler(HKEY("printmsg"), "", 0, print_message, NEED_URL);
2008 WebcitAddUrlHandler(HKEY("msgheaders"), "", 0, display_headers, NEED_URL);
2010 WebcitAddUrlHandler(HKEY("mimepart"), "", 0, view_mimepart, NEED_URL);
2011 WebcitAddUrlHandler(HKEY("mimepart_download"), "", 0, download_mimepart, NEED_URL);
2012 WebcitAddUrlHandler(HKEY("postpart"), "", 0, view_postpart, NEED_URL|PROHIBIT_STARTPAGE);
2013 WebcitAddUrlHandler(HKEY("postpart_download"), "", 0, download_postpart, NEED_URL|PROHIBIT_STARTPAGE);
2014 WebcitAddUrlHandler(HKEY("upload_attachment"), "", 0, upload_attachment, AJAX);
2015 WebcitAddUrlHandler(HKEY("remove_attachment"), "", 0, remove_attachment, AJAX);
2016 WebcitAddUrlHandler(HKEY("show_num_attachments"), "", 0, show_num_attachments, AJAX);
2019 WebcitAddUrlHandler(HKEY("roommsgs"), "", 0, jsonMessageList,0);
2021 l_subj = FourHash("subj", 4);
2022 l_wefw = FourHash("wefw", 4);
2023 l_msgn = FourHash("msgn", 4);
2024 l_from = FourHash("from", 4);
2025 l_rcpt = FourHash("rcpt", 4);
2026 l_cccc = FourHash("cccc", 4);
2027 l_node = FourHash("node", 4);
2028 l_rfca = FourHash("rfca", 4);
2034 SessionDetachModule_MSG
2037 DeleteHash(&sess->summ);