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;
40 int load_message(message_summary *Msg,
53 if (Msg->PartNum != NULL) {
54 serv_printf("MSG4 %ld|%s", Msg->msgnum, ChrPtr(Msg->PartNum));
57 serv_printf("MSG4 %ld", Msg->msgnum);
60 StrBuf_ServGetln(Buf);
61 if (GetServerStatus(Buf, NULL) != 1) {
63 StrBufAppendPrintf(*Error, "<strong>");
64 StrBufAppendPrintf(*Error, _("ERROR:"));
65 StrBufAppendPrintf(*Error, "</strong> %s<br>\n", &buf[4]);
70 /* begin everythingamundo table */
71 HdrToken = NewStrBuf();
72 while (!Done && StrBuf_ServGetln(Buf)>=0) {
73 if ( (StrLength(Buf)==3) &&
74 !strcmp(ChrPtr(Buf), "000"))
78 if (Msg->MsgBody->Data == NULL)
79 Msg->MsgBody->Data = NewStrBuf();
80 Msg->MsgBody->ContentType = NewStrBufPlain(HKEY("text/html"));
81 StrBufAppendPrintf(Msg->MsgBody->Data, "<div><i>");
82 StrBufAppendPrintf(Msg->MsgBody->Data, _("Empty message"));
83 StrBufAppendPrintf(Msg->MsgBody->Data, "</i><br><br>\n");
84 StrBufAppendPrintf(Msg->MsgBody->Data, "</div>\n");
89 case 0:/* Citadel Message Headers */
90 if (StrLength(Buf) == 0) {
94 StrBufExtract_token(HdrToken, Buf, 0, '=');
95 StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
97 /* look up one of the examine_* functions to parse the content */
98 if (GetHash(MsgHeaderHandler, SKEY(HdrToken), &vHdr) &&
100 Hdr = (headereval*)vHdr;
101 Hdr->evaluator(Msg, Buf, FoundCharset);
102 if (Hdr->Type == 1) {
106 else LogError(Target,
108 "don't know how to handle message header[%s]\n",
112 case 1:/* Message Mime Header */
113 if (StrLength(Buf) == 0) {
115 if (Msg->MsgBody->ContentType == NULL)
116 /* end of header or no header? */
117 Msg->MsgBody->ContentType = NewStrBufPlain(HKEY("text/plain"));
118 /* usual end of mime header */
122 StrBufExtract_token(HdrToken, Buf, 0, ':');
123 if (StrLength(HdrToken) > 0) {
124 StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
125 /* the examine*'s know how to do with mime headers too... */
126 if (GetHash(MsgHeaderHandler, SKEY(HdrToken), &vHdr) &&
128 Hdr = (headereval*)vHdr;
129 Hdr->evaluator(Msg, Buf, FoundCharset);
134 case 2: /* Message Body */
136 if (Msg->MsgBody->size_known > 0) {
137 StrBuf_ServGetBLOBBuffered(Msg->MsgBody->Data, Msg->MsgBody->length);
139 /*/ todo: check next line, if not 000, append following lines */
142 if (StrLength(Msg->MsgBody->Data) > 0)
143 StrBufAppendBufPlain(Msg->MsgBody->Data, "\n", 1, 0);
144 StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
148 StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
153 if (Msg->AllAttach == NULL)
154 Msg->AllAttach = NewHash(1,NULL);
155 /* now we put the body mimepart we read above into the mimelist */
156 Put(Msg->AllAttach, SKEY(Msg->MsgBody->PartNum), Msg->MsgBody, DestroyMime);
159 FreeStrBuf(&HdrToken);
166 * I wanna SEE that message!
168 * msgnum Message number to display
169 * printable_view Nonzero to display a printable view
170 * section Optional for encapsulated message/rfc822 submessage
172 int read_message(StrBuf *Target, const char *tmpl, long tmpllen, long msgnum, const StrBuf *PartNum, const StrBuf **OutMime)
175 StrBuf *FoundCharset;
178 message_summary *Msg = NULL;
182 WCTemplputParams SubTP;
183 StrBuf *Error = NULL;
186 FoundCharset = NewStrBuf();
187 Msg = (message_summary *)malloc(sizeof(message_summary));
188 memset(Msg, 0, sizeof(message_summary));
189 Msg->msgnum = msgnum;
190 Msg->PartNum = PartNum;
191 Msg->MsgBody = (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
192 memset(Msg->MsgBody, 0, sizeof(wc_mime_attachment));
193 Msg->MsgBody->msgnum = msgnum;
195 if (!load_message(Msg, FoundCharset, &Error)) {
196 StrBufAppendBuf(Target, Error, 0);
200 /* Extract just the content-type (omit attributes such as "charset") */
201 StrBufExtract_token(Buf, Msg->MsgBody->ContentType, 0, ';');
203 StrBufLowerCase(Buf);
205 /* Locate a renderer capable of converting this MIME part into HTML */
206 if (GetHash(MimeRenderHandler, SKEY(Buf), &vHdr) &&
208 RenderMimeFuncStruct *Render;
209 Render = (RenderMimeFuncStruct*)vHdr;
210 Render->f(Msg->MsgBody, NULL, FoundCharset);
213 if (StrLength(Msg->reply_references)> 0) {
214 /* Trim down excessively long lists of thread references. We eliminate the
215 * second one in the list so that the thread root remains intact.
217 int rrtok = num_tokens(ChrPtr(Msg->reply_references), '|');
218 int rrlen = StrLength(Msg->reply_references);
219 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
220 StrBufRemove_token(Msg->reply_references, 1, '|');
224 /* now check if we need to translate some mimeparts, and remove the duplicate */
225 it = GetNewHashPos(Msg->AllAttach, 0);
226 while (GetNextHashPos(Msg->AllAttach, it, &len, &Key, &vMime) &&
228 wc_mime_attachment *Mime = (wc_mime_attachment*) vMime;
229 evaluate_mime_part(Msg, Mime);
232 memset(&SubTP, 0, sizeof(WCTemplputParams));
233 SubTP.Filter.ContextType = CTX_MAILSUM;
235 *OutMime = DoTemplate(tmpl, tmpllen, Target, &SubTP);
237 DestroyMessageSummary(Msg);
238 FreeStrBuf(&FoundCharset);
245 HttpStatus(long CitadelStatus)
247 long httpstatus = 502;
249 switch (MAJORCODE(CitadelStatus))
251 case LISTING_FOLLOWS:
256 switch (MINORCODE(CitadelStatus))
264 case HIGHER_ACCESS_REQUIRED:
265 case MAX_SESSIONS_EXCEEDED:
267 case RESOURCE_NOT_OPEN:
269 case INVALID_FLOOR_OPERATION:
275 case MESSAGE_NOT_FOUND:
285 case CMD_NOT_SUPPORTED:
286 case PASSWORD_REQUIRED:
287 case ALREADY_LOGGED_IN:
288 case USERNAME_REQUIRED:
290 case SERVER_SHUTTING_DOWN:
301 case START_CHAT_MODE:
305 httpstatus = 502; /* aeh... whut? */
313 * Unadorned HTML output of an individual message, suitable
314 * for placing in a hidden iframe, for printing, or whatever
316 void handle_one_message(void)
320 const StrBuf *Destination;
326 StrBuf *CmdBuf = NULL;
330 pMsg = strchr(ChrPtr(WCC->Hdr->HR.ReqLine), '/');
332 HttpStatus(CitStatus);
336 msgnum = atol(pMsg + 1);
337 StrBufCutAt(WCC->Hdr->HR.ReqLine, 0, pMsg);
338 gotoroom(WCC->Hdr->HR.ReqLine);
339 switch (WCC->Hdr->HR.eReqType)
343 Tmpl = sbstr("template");
344 if (StrLength(Tmpl) > 0)
345 read_message(WCC->WBuf, SKEY(Tmpl), msgnum, NULL, &Mime);
347 read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
348 http_transmit_thing(ChrPtr(Mime), 0);
351 CmdBuf = NewStrBuf ();
352 if ((WCC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
353 serv_printf("DELE %ld", msgnum);
355 else { /* Otherwise move it to Trash */
356 serv_printf("MOVE %ld|_TRASH_|0", msgnum);
358 StrBuf_ServGetln(CmdBuf);
359 GetServerStatusMsg(CmdBuf, &CitStatus, 1, 0);
360 HttpStatus(CitStatus);
365 if (GetHash(WCC->Hdr->HTTPHeaders, HKEY("DESTINATION"), &vLine) &&
367 Destination = (StrBuf*) vLine;
368 serv_printf("MOVE %ld|%s|%d", msgnum, ChrPtr(Destination), CopyMessage);
369 StrBuf_ServGetln(CmdBuf);
370 GetServerStatusMsg(CmdBuf, NULL, 1, 0);
371 GetServerStatus(CmdBuf, &CitStatus);
372 HttpStatus(CitStatus);
385 * Unadorned HTML output of an individual message, suitable
386 * for placing in a hidden iframe, for printing, or whatever
388 void embed_message(void) {
393 StrBuf *CmdBuf = NULL;
395 msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
396 if (msgnum <= 0) return;
398 switch (WCC->Hdr->HR.eReqType)
402 Tmpl = sbstr("template");
403 if (StrLength(Tmpl) > 0)
404 read_message(WCC->WBuf, SKEY(Tmpl), msgnum, NULL, &Mime);
406 read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
407 http_transmit_thing(ChrPtr(Mime), 0);
410 CmdBuf = NewStrBuf ();
411 if ((WCC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
412 serv_printf("DELE %ld", msgnum);
414 else { /* Otherwise move it to Trash */
415 serv_printf("MOVE %ld|_TRASH_|0", msgnum);
417 StrBuf_ServGetln(CmdBuf);
418 GetServerStatusMsg(CmdBuf, NULL, 1, 0);
428 * Printable view of a message
430 void print_message(void) {
434 msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
435 output_headers(0, 0, 0, 0, 0, 0);
437 hprintf("Content-type: text/html\r\n"
438 "Server: " PACKAGE_STRING "\r\n"
439 "Connection: close\r\n");
443 read_message(WC->WBuf, HKEY("view_message_print"), msgnum, NULL, &Mime);
449 * Display a message's headers
451 void display_headers(void) {
455 msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
456 output_headers(0, 0, 0, 0, 0, 0);
458 hprintf("Content-type: text/plain\r\n"
460 "Connection: close\r\n",
464 serv_printf("MSG2 %ld|3", msgnum);
465 serv_getln(buf, sizeof buf);
467 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
468 wc_printf("%s\n", buf);
476 message_summary *ReadOneMessageSummary(StrBuf *RawMessage, const char *DefaultSubject, long MsgNum)
479 MsgPartEvaluatorStruct *Eval;
480 message_summary *Msg;
489 serv_printf("MSG0 %ld|1", MsgNum); /* ask for headers only */
491 StrBuf_ServGetln(Buf);
492 if (GetServerStatus(Buf, NULL) == 1) {
497 Msg = (message_summary*)malloc(sizeof(message_summary));
498 memset(Msg, 0, sizeof(message_summary));
499 while (len = StrBuf_ServGetln(Buf),
502 strcmp(ChrPtr(Buf), "000")))
505 ebuf = strchr(ChrPtr(Buf), '=');
507 if (GetHash(MsgEvaluators, buf, nBuf, &vEval) && vEval != NULL) {
508 Eval = (MsgPartEvaluatorStruct*) vEval;
509 StrBufCutLeft(Buf, nBuf + 1);
512 else syslog(1, "Don't know how to handle Message Headerline [%s]", ChrPtr(Buf));
522 * load message pointers from the server for a "read messages" operation
524 * servcmd: the citadel command to send to the citserver
526 int load_msg_ptrs(const char *servcmd,
528 SharedMessageStatus *Stat,
529 load_msg_ptrs_detailheaders LH)
532 message_summary *Msg;
537 const char *Ptr = NULL;
540 Stat->lowest_found = LONG_MAX;
541 Stat->highest_found = LONG_MIN;
543 if (WCC->summ != NULL) {
544 DeleteHash(&WCC->summ);
546 WCC->summ = NewHash(1, Flathash);
550 StrBuf_ServGetln(Buf);
551 StatMajor = GetServerStatus(Buf, NULL);
556 if (filter != NULL) {
561 /* fall back to empty filter in case of we were fooled... */
567 return (Stat->nummsgs);
570 while (len = StrBuf_ServGetln(Buf),
573 strcmp(ChrPtr(Buf), "000")!= 0)))
575 if (Stat->nummsgs < Stat->maxload) {
578 Msg = (message_summary*)malloc(sizeof(message_summary));
579 memset(Msg, 0, sizeof(message_summary));
581 Msg->msgnum = StrBufExtractNext_long(Buf, &Ptr, '|');
582 Msg->date = StrBufExtractNext_long(Buf, &Ptr, '|');
584 if (Stat->nummsgs == 0) {
585 if (Msg->msgnum < Stat->lowest_found) {
586 Stat->lowest_found = Msg->msgnum;
588 if (Msg->msgnum > Stat->highest_found) {
589 Stat->highest_found = Msg->msgnum;
593 if ((Msg->msgnum == 0) && (StrLength(Buf) < 32)) {
599 * as citserver probably gives us messages in forward date sorting
600 * nummsgs should be the same order as the message date.
602 if (Msg->date == 0) {
603 Msg->date = Stat->nummsgs;
604 if (StrLength(Buf) < 32)
607 if ((!skipit) && (LH != NULL)) {
608 if (!LH(Buf, &Ptr, Msg, Buf2)){
614 Put(WCC->summ, (const char *)&n, sizeof(n), Msg, DestroyMessageSummary);
620 return (Stat->nummsgs);
626 * \brief sets FlagToSet for each of ScanMe that appears in MatchMSet
627 * \param ScanMe List of BasicMsgStruct to be searched it MatchSet
628 * \param MatchMSet MSet we want to flag
629 * \param FlagToSet Flag to set on each BasicMsgStruct->Flags if in MSet
631 void SetFlagsFromMSet(HashList *ScanMe, MSet *MatchMSet, int FlagToSet, int Reverse)
637 message_summary *Msg;
639 at = GetNewHashPos(ScanMe, 0);
640 while (GetNextHashPos(ScanMe, at, &HKLen, &HashKey, &vMsg)) {
641 /* Are you a new message, or an old message? */
642 Msg = (message_summary*) vMsg;
643 if (Reverse && IsInMSetList(MatchMSet, Msg->msgnum)) {
644 Msg->Flags = Msg->Flags | FlagToSet;
646 else if (!Reverse && !IsInMSetList(MatchMSet, Msg->msgnum)) {
647 Msg->Flags = Msg->Flags | FlagToSet;
654 void load_seen_flags(void)
660 OldMsg = NewStrBuf();
662 StrBuf_ServGetln(OldMsg);
663 if (GetServerStatus(OldMsg, NULL) == 2) {
664 StrBufCutLeft(OldMsg, 4);
671 if (ParseMSet(&MatchMSet, OldMsg))
673 SetFlagsFromMSet(WCC->summ, MatchMSet, MSGFLAG_READ, 0);
675 DeleteMSet(&MatchMSet);
679 extern readloop_struct rlid[];
681 typedef struct _RoomRenderer{
684 GetParamsGetServerCall_func GetParamsGetServerCall;
686 PrintViewHeader_func PrintPageHeader;
687 PrintViewHeader_func PrintViewHeader;
688 LoadMsgFromServer_func LoadMsgFromServer;
689 RenderView_or_Tail_func RenderView_or_Tail;
690 View_Cleanup_func ViewCleanup;
691 load_msg_ptrs_detailheaders LHParse;
696 * command loop for reading messages
698 * Set oper to "readnew" or "readold" or "readfwd" or "headers" or "readgt" or "readlt" or "do_search"
700 void readloop(long oper, eCustomRoomRenderer ForceRenderer)
702 RoomRenderer *ViewMsg;
705 message_summary *Msg;
707 char filter[256] = "";
713 WCTemplputParams SubTP;
714 SharedMessageStatus Stat;
717 if (havebstr("is_summary") && (1 == (ibstr("is_summary")))) {
718 WCC->CurRoom.view = VIEW_MAILBOX;
721 if (havebstr("view")) {
722 WCC->CurRoom.view = ibstr("view");
725 memset(&Stat, 0, sizeof(SharedMessageStatus));
726 Stat.maxload = 10000;
727 Stat.lowest_found = (-1);
728 Stat.highest_found = (-1);
729 if (ForceRenderer == eUseDefault)
730 GetHash(ReadLoopHandler, IKEY(WCC->CurRoom.view), &vViewMsg);
732 GetHash(ReadLoopHandler, IKEY(ForceRenderer), &vViewMsg);
733 if (vViewMsg == NULL) {
734 WCC->CurRoom.view = VIEW_BBS;
735 GetHash(ReadLoopHandler, IKEY(WCC->CurRoom.view), &vViewMsg);
737 if (vViewMsg == NULL) {
738 return; /* TODO: print message */
741 ViewMsg = (RoomRenderer*) vViewMsg;
742 if (ViewMsg->PrintPageHeader == NULL)
743 output_headers(1, 1, 1, 0, 0, 0);
745 ViewMsg->PrintPageHeader(&Stat, ViewSpecific);
747 if (ViewMsg->GetParamsGetServerCall != NULL) {
748 r = ViewMsg->GetParamsGetServerCall(
753 filter, sizeof(filter)
765 case 300: /* the callback hook should do the work for us here, since he knows what to do. */
771 if (!IsEmptyStr(cmd)) {
772 const char *p = NULL;
773 if (!IsEmptyStr(filter))
775 Stat.nummsgs = load_msg_ptrs(cmd, p, &Stat, ViewMsg->LHParse);
780 memset(&SubTP, 0, sizeof(WCTemplputParams));
781 SubTP.Filter.ContextType = CTX_MAILSUM;
782 SubTP.Context = NULL;
783 SortIt = RetrieveSort(&SubTP, NULL, 0,
784 HKEY("date"), Stat.defaultsortorder);
786 SortByPayload(WCC->summ, SortIt);
788 if (Stat.startmsg < 0) {
792 if (Stat.load_seen) load_seen_flags();
795 * Print any inforation above the message list...
797 if (ViewMsg->PrintViewHeader != NULL)
798 ViewMsg->PrintViewHeader(&Stat, &ViewSpecific);
800 WCC->startmsg = Stat.startmsg;
801 WCC->maxmsgs = Stat.maxmsgs;
802 WCC->num_displayed = 0;
804 /* Put some helpful data in vars for mailsummary_json */
809 StrBufPrintf(Foo, "%ld", Stat.nummsgs);
810 PutBstr(HKEY("__READLOOP:TOTALMSGS"), NewStrBufDup(Foo));
811 StrBufPrintf(Foo, "%ld", Stat.startmsg);
812 PutBstr(HKEY("__READLOOP:STARTMSG"), Foo);
816 * iterate over each message. if we need to load an attachment, do it here.
819 if ((ViewMsg->LoadMsgFromServer != NULL) &&
822 at = GetNewHashPos(WCC->summ, 0);
823 Stat.num_displayed = i = 0;
824 while ( GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
825 Msg = (message_summary*) vMsg;
826 if ((Msg->msgnum >= Stat.startmsg) && (Stat.num_displayed <= Stat.maxmsgs)) {
827 ViewMsg->LoadMsgFromServer(&Stat,
830 (Msg->Flags & MSGFLAG_READ) != 0,
839 * Done iterating the message list. now tasks we want to do after.
841 if (ViewMsg->RenderView_or_Tail != NULL)
842 ViewMsg->RenderView_or_Tail(&Stat, &ViewSpecific, oper);
844 if (ViewMsg->ViewCleanup != NULL)
845 ViewMsg->ViewCleanup(&ViewSpecific);
849 if (WCC->summ != NULL) {
850 DeleteHash(&WCC->summ);
856 * Back end for post_message()
857 * ... this is where the actual message gets transmitted to the server.
859 void post_mime_to_server(void) {
861 char top_boundary[SIZ];
862 char alt_boundary[SIZ];
863 int is_multipart = 0;
865 wc_mime_attachment *att;
867 size_t encoded_length;
868 size_t encoded_strlen;
869 char *txtmail = NULL;
870 int include_text_alt = 0; /* Set to nonzero to include multipart/alternative text/plain */
872 sprintf(top_boundary, "Citadel--Multipart--%s--%04x--%04x",
873 ChrPtr(WCC->serv_info->serv_fqdn),
877 sprintf(alt_boundary, "Citadel--Multipart--%s--%04x--%04x",
878 ChrPtr(WCC->serv_info->serv_fqdn),
883 /* RFC2045 requires this, and some clients look for it... */
884 serv_puts("MIME-Version: 1.0");
885 serv_puts("X-Mailer: " PACKAGE_STRING);
887 /* If there are attachments, we have to do multipart/mixed */
888 if (GetCount(WCC->attachments) > 0) {
892 /* Only do multipart/alternative for mailboxes. BBS and Wiki rooms don't need it. */
893 if (WC->CurRoom.view == VIEW_MAILBOX) {
894 include_text_alt = 1;
898 /* Remember, serv_printf() appends an extra newline */
899 serv_printf("Content-type: multipart/mixed; boundary=\"%s\"\n", top_boundary);
900 serv_printf("This is a multipart message in MIME format.\n");
901 serv_printf("--%s", top_boundary);
904 /* Remember, serv_printf() appends an extra newline */
905 if (include_text_alt) {
906 serv_printf("Content-type: multipart/alternative; "
907 "boundary=\"%s\"\n", alt_boundary);
908 serv_printf("This is a multipart message in MIME format.\n");
909 serv_printf("--%s", alt_boundary);
911 serv_puts("Content-type: text/plain; charset=utf-8");
912 serv_puts("Content-Transfer-Encoding: quoted-printable");
914 txtmail = html_to_ascii(bstr("msgtext"), 0, 80, 0);
915 text_to_server_qp(txtmail); /* Transmit message in quoted-printable encoding */
918 serv_printf("\n--%s", alt_boundary);
921 serv_puts("Content-type: text/html; charset=utf-8");
922 serv_puts("Content-Transfer-Encoding: quoted-printable");
924 serv_puts("<html><body>\r\n");
925 text_to_server_qp(bstr("msgtext")); /* Transmit message in quoted-printable encoding */
926 serv_puts("</body></html>\r\n");
928 if (include_text_alt) {
929 serv_printf("--%s--", alt_boundary);
938 /* Add in the attachments */
939 it = GetNewHashPos(WCC->attachments, 0);
940 while (GetNextHashPos(WCC->attachments, it, &len, &Key, &vAtt)) {
941 att = (wc_mime_attachment *)vAtt;
942 if (att->length == 0)
944 encoded_length = ((att->length * 150) / 100);
945 encoded = malloc(encoded_length);
946 if (encoded == NULL) break;
947 encoded_strlen = CtdlEncodeBase64(encoded, ChrPtr(att->Data), StrLength(att->Data), 1);
949 serv_printf("--%s", top_boundary);
950 serv_printf("Content-type: %s", ChrPtr(att->ContentType));
951 serv_printf("Content-disposition: attachment; filename=\"%s\"", ChrPtr(att->FileName));
952 serv_puts("Content-transfer-encoding: base64");
954 serv_write(encoded, encoded_strlen);
959 serv_printf("--%s--", top_boundary);
968 * Post message (or don't post message)
970 * Note regarding the "dont_post" variable:
971 * A random value (actually, it's just a timestamp) is inserted as a hidden
972 * field called "postseq" when the display_enter page is generated. This
973 * value is checked when posting, using the static variable dont_post. If a
974 * user attempts to post twice using the same dont_post value, the message is
975 * discarded. This prevents the accidental double-saving of the same message
976 * if the user happens to click the browser "back" button.
978 void post_message(void)
981 StrBuf *EmailAddress;
984 StrBuf *encoded_subject = NULL;
985 static long dont_post = (-1L);
986 int is_anonymous = 0;
987 const StrBuf *display_name = NULL;
991 if (havebstr("force_room")) {
992 gotoroom(sbstr("force_room"));
995 if (havebstr("display_name")) {
996 display_name = sbstr("display_name");
997 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1003 if (!strcasecmp(bstr("submit_action"), "cancel")) {
1004 AppendImportantMessage(_("Cancelled. Message was not posted."), -1);
1005 } else if (lbstr("postseq") == dont_post) {
1006 AppendImportantMessage(
1007 _("Automatically cancelled because you have already "
1008 "saved this message."), -1);
1010 const char CMD[] = "ENT0 1|%s|%d|4|%s|%s||%s|%s|%s|%s|%s";
1011 StrBuf *Recp = NULL;
1014 char *wikipage = NULL;
1015 const StrBuf *my_email_addr = NULL;
1016 StrBuf *CmdBuf = NULL;
1017 StrBuf *references = NULL;
1018 int saving_to_drafts = 0;
1021 saving_to_drafts = !strcasecmp(bstr("submit_action"), "draft");
1024 if (saving_to_drafts) {
1025 /* temporarily change to the drafts room */
1026 serv_puts("GOTO _DRAFTS_");
1027 StrBuf_ServGetln(Buf);
1028 if (GetServerStatusMsg(Buf, NULL, 1, 2) != 2) {
1029 /* You probably don't even have a dumb Drafts folder */
1030 syslog(9, "%s:%d: server save to drafts error: %s\n", __FILE__, __LINE__, ChrPtr(Buf) + 4);
1031 AppendImportantMessage(_("Saved to Drafts failed: "), -1);
1038 if (havebstr("references"))
1040 const StrBuf *ref = sbstr("references");
1041 references = NewStrBufDup(ref);
1042 if (*ChrPtr(references) == '|') { /* remove leading '|' if present */
1043 StrBufCutLeft(references, 1);
1045 StrBufReplaceChars(references, '|', '!');
1047 if (havebstr("subject")) {
1050 * make enough room for the encoded string;
1051 * plus the QP header
1053 Subj = sbstr("subject");
1055 StrBufRFC2047encode(&encoded_subject, Subj);
1057 UserName = NewStrBuf();
1058 EmailAddress = NewStrBuf();
1059 EncBuf = NewStrBuf();
1061 Recp = StrBufSanitizeEmailRecipientVector(sbstr("recp"), UserName, EmailAddress, EncBuf);
1062 Cc = StrBufSanitizeEmailRecipientVector(sbstr("cc"), UserName, EmailAddress, EncBuf);
1063 Bcc = StrBufSanitizeEmailRecipientVector(sbstr("bcc"), UserName, EmailAddress, EncBuf);
1065 FreeStrBuf(&UserName);
1066 FreeStrBuf(&EmailAddress);
1067 FreeStrBuf(&EncBuf);
1069 wikipage = strdup(bstr("page"));
1070 str_wiki_index(wikipage);
1071 my_email_addr = sbstr("my_email_addr");
1073 HeaderLen = StrLength(Recp) +
1074 StrLength(encoded_subject) +
1078 StrLength(my_email_addr) +
1079 StrLength(references);
1080 CmdBuf = NewStrBufPlain(NULL, sizeof (CMD) + HeaderLen);
1081 StrBufPrintf(CmdBuf,
1083 saving_to_drafts?"":ChrPtr(Recp),
1085 ChrPtr(encoded_subject),
1086 ChrPtr(display_name),
1087 saving_to_drafts?"":ChrPtr(Cc),
1088 saving_to_drafts?"":ChrPtr(Bcc),
1090 ChrPtr(my_email_addr),
1091 ChrPtr(references));
1092 FreeStrBuf(&references);
1093 FreeStrBuf(&encoded_subject);
1096 if ((HeaderLen + StrLength(sbstr("msgtext")) < 10) &&
1097 (GetCount(WCC->attachments) == 0)){
1098 AppendImportantMessage(_("Refusing to post empty message.\n"), -1);
1099 FreeStrBuf(&CmdBuf);
1104 syslog(9, "%s\n", ChrPtr(CmdBuf));
1105 serv_puts(ChrPtr(CmdBuf));
1106 FreeStrBuf(&CmdBuf);
1108 StrBuf_ServGetln(Buf);
1109 if (GetServerStatus(Buf, NULL) == 4) {
1110 if (saving_to_drafts) {
1111 if ( (havebstr("recp"))
1112 || (havebstr("cc" ))
1113 || (havebstr("bcc" )) ) {
1114 /* save recipient headers or room to post to */
1115 serv_printf("To: %s", ChrPtr(Recp));
1116 serv_printf("Cc: %s", ChrPtr(Cc));
1117 serv_printf("Bcc: %s", ChrPtr(Bcc));
1119 serv_printf("X-Citadel-Room: %s", ChrPtr(WC->CurRoom.name));
1122 post_mime_to_server();
1123 if (saving_to_drafts) {
1124 AppendImportantMessage(_("Message has been saved to Drafts.\n"), -1);
1125 gotoroom(WCC->CurRoom.name);
1126 readloop(readnew, eUseDefault);
1129 } else if ( (havebstr("recp"))
1130 || (havebstr("cc" ))
1131 || (havebstr("bcc" ))
1133 AppendImportantMessage(_("Message has been sent.\n"), -1);
1136 AppendImportantMessage(_("Message has been posted.\n"), -1);
1138 dont_post = lbstr("postseq");
1140 syslog(9, "%s:%d: server post error: %s", __FILE__, __LINE__, ChrPtr(Buf) + 4);
1141 AppendImportantMessage(ChrPtr(Buf) + 4, StrLength(Buf) - 4);
1143 if (saving_to_drafts) gotoroom(WCC->CurRoom.name);
1157 DeleteHash(&WCC->attachments);
1160 * We may have been supplied with instructions regarding the location
1161 * to which we must return after posting. If found, go there.
1163 if (havebstr("return_to")) {
1164 http_redirect(bstr("return_to"));
1167 * If we were editing a page in a wiki room, go to that page now.
1169 else if (havebstr("page")) {
1170 snprintf(buf, sizeof buf, "wiki?page=%s", bstr("page"));
1174 * Otherwise, just go to the "read messages" loop.
1177 readloop(readnew, eUseDefault);
1183 * Client is uploading an attachment
1185 void upload_attachment(void) {
1186 wcsession *WCC = WC;
1192 wc_mime_attachment *att;
1194 syslog(9, "upload_attachment()\n");
1195 wc_printf("upload_attachment()<br>\n");
1197 if (WCC->upload_length <= 0) {
1198 syslog(9, "ERROR no attachment was uploaded\n");
1199 wc_printf("ERROR no attachment was uploaded<br>\n");
1203 syslog(9, "Client is uploading %d bytes\n", WCC->upload_length);
1204 wc_printf("Client is uploading %d bytes<br>\n", WCC->upload_length);
1205 att = malloc(sizeof(wc_mime_attachment));
1206 memset(att, 0, sizeof(wc_mime_attachment ));
1207 att->length = WCC->upload_length;
1208 att->ContentType = NewStrBufPlain(WCC->upload_content_type, -1);
1209 att->FileName = NewStrBufDup(WCC->upload_filename);
1211 if (WCC->attachments == NULL) {
1212 WCC->attachments = NewHash(1, Flathash);
1215 /* And add it to the list. */
1217 if ((GetCount(WCC->attachments) > 0) &&
1218 GetHashAt(WCC->attachments,
1219 GetCount(WCC->attachments) -1,
1220 &newnlen, &newn, &v))
1221 n = *((int*) newn) + 1;
1222 Put(WCC->attachments, IKEY(n), att, DestroyMime);
1225 * Mozilla sends a simple filename, which is what we want,
1226 * but Satan's Browser sends an entire pathname. Reduce
1227 * the path to just a filename if we need to.
1229 pch = strrchr(ChrPtr(att->FileName), '/');
1231 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1233 pch = strrchr(ChrPtr(att->FileName), '\\');
1235 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1239 * Transfer control of this memory from the upload struct
1240 * to the attachment struct.
1242 att->Data = WCC->upload;
1244 WCC->upload_length = 0;
1249 * Remove an attachment from the message currently being composed.
1251 * Currently we identify the attachment to be removed by its filename.
1252 * There is probably a better way to do this.
1254 void remove_attachment(void) {
1255 wcsession *WCC = WC;
1256 wc_mime_attachment *att;
1258 StrBuf *WhichAttachment;
1263 WhichAttachment = NewStrBufDup(sbstr("which_attachment"));
1264 StrBufUnescape(WhichAttachment, 0);
1265 at = GetNewHashPos(WCC->attachments, 0);
1267 GetHashPos(WCC->attachments, at, &len, &key, &vAtt);
1269 att = (wc_mime_attachment*) vAtt;
1270 if ((att != NULL) &&
1271 (strcmp(ChrPtr(WhichAttachment),
1272 ChrPtr(att->FileName) ) == 0))
1274 DeleteEntryFromHash(WCC->attachments, at);
1278 while (NextHashPos(WCC->attachments, at));
1279 FreeStrBuf(&WhichAttachment);
1280 wc_printf("remove_attachment() completed\n");
1284 long FourHash(const char *key, long length)
1288 const unsigned char *ptr = (const unsigned char*)key;
1290 for (i = 0; i < 4; i++, ptr ++)
1310 * display the message entry screen
1312 void display_enter(void)
1317 const StrBuf *display_name = NULL;
1318 int recipient_required = 0;
1319 int subject_required = 0;
1320 int is_anonymous = 0;
1321 wcsession *WCC = WC;
1325 if (havebstr("force_room")) {
1326 gotoroom(sbstr("force_room"));
1329 display_name = sbstr("display_name");
1330 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1331 display_name = NULL;
1336 * First, do we have permission to enter messages in this room at all?
1339 serv_puts("ENT0 0");
1340 StrBuf_ServGetln(Line);
1341 rc = GetServerStatusMsg(Line, &Result, 0, 2);
1343 if (Result == 570) { /* 570 means that we need a recipient here */
1344 recipient_required = 1;
1346 else if (rc != 2) { /* Any other error means that we cannot continue */
1347 rc = GetServerStatusMsg(Line, &Result, 0, 2);
1348 readloop(readnew, eUseDefault);
1353 /* Is the server strongly recommending that the user enter a message subject? */
1354 if (StrLength(Line) > 4) {
1355 subject_required = extract_int(ChrPtr(Line) + 4, 1);
1359 * Are we perhaps in an address book view? If so, then an "enter
1360 * message" command really means "add new entry."
1362 if (WCC->CurRoom.defview == VIEW_ADDRESSBOOK) {
1363 do_edit_vcard(-1, "", NULL, NULL, "", ChrPtr(WCC->CurRoom.name));
1369 * Are we perhaps in a calendar room? If so, then an "enter
1370 * message" command really means "add new calendar item."
1372 if (WCC->CurRoom.defview == VIEW_CALENDAR) {
1373 display_edit_event();
1379 * Are we perhaps in a tasks view? If so, then an "enter
1380 * message" command really means "add new task."
1382 if (WCC->CurRoom.defview == VIEW_TASKS) {
1383 display_edit_task();
1390 * If the "replying_to" variable is set, it refers to a message
1391 * number from which we must extract some header fields...
1393 replying_to = lbstr("replying_to");
1394 if (replying_to > 0) {
1396 StrBuf *wefw = NULL;
1397 StrBuf *msgn = NULL;
1398 StrBuf *from = NULL;
1399 StrBuf *node = NULL;
1400 StrBuf *rfca = NULL;
1401 StrBuf *rcpt = NULL;
1402 StrBuf *cccc = NULL;
1403 serv_printf("MSG0 %ld|1", replying_to);
1405 StrBuf_ServGetln(Line);
1406 if (GetServerStatusMsg(Line, NULL, 0, 0) == 1)
1407 while (len = StrBuf_ServGetln(Line),
1410 strcmp(ChrPtr(Line), "000")))
1413 if ((StrLength(Line) > 4) &&
1414 (ChrPtr(Line)[4] == '='))
1415 which = FourHash(ChrPtr(Line), 4);
1417 if (which == l_subj)
1419 StrBuf *subj = NewStrBuf();
1420 StrBuf *FlatSubject;
1422 if (!strcasecmp(bstr("replying_mode"), "forward")) {
1423 if (strncasecmp(ChrPtr(Line) + 5, "Fw:", 3)) {
1424 StrBufAppendBufPlain(subj, HKEY("Fw: "), 0);
1428 if (strncasecmp(ChrPtr(Line) + 5, "Re:", 3)) {
1429 StrBufAppendBufPlain(subj, HKEY("Re: "), 0);
1432 StrBufAppendBufPlain(subj,
1434 StrLength(Line) - 5, 0);
1435 FlatSubject = NewStrBufPlain(NULL, StrLength(subj));
1436 StrBuf_RFC822_to_Utf8(FlatSubject, subj, NULL, NULL);
1438 PutBstr(HKEY("subject"), FlatSubject);
1441 else if (which == l_wefw)
1446 wefw = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1448 /* Trim down excessively long lists of thread references. We eliminate the
1449 * second one in the list so that the thread root remains intact.
1451 rrtok = num_tokens(ChrPtr(wefw), '|');
1452 rrlen = StrLength(wefw);
1453 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
1454 StrBufRemove_token(wefw, 1, '|');
1458 else if (which == l_msgn) {
1459 msgn = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1462 else if (which == l_from) {
1464 from = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1465 FlatFrom = NewStrBufPlain(NULL, StrLength(from));
1466 StrBuf_RFC822_to_Utf8(FlatFrom, from, NULL, NULL);
1469 for (i=0; i<StrLength(from); ++i) {
1470 if (ChrPtr(from)[i] == ',')
1471 StrBufPeek(from, NULL, i, ' ');
1475 else if (which == l_rcpt) {
1476 rcpt = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1479 else if (which == l_cccc) {
1480 cccc = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1483 else if (which == l_node) {
1484 node = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1487 else if (which == l_rfca) {
1489 rfca = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1490 FlatRFCA = NewStrBufPlain(NULL, StrLength(rfca));
1491 StrBuf_RFC822_to_Utf8(FlatRFCA, rfca, NULL, NULL);
1498 if (StrLength(wefw) + StrLength(msgn) > 0) {
1499 StrBuf *refs = NewStrBuf();
1500 if (StrLength(wefw) > 0) {
1501 StrBufAppendBuf(refs, wefw, 0);
1503 if ( (StrLength(wefw) > 0) &&
1504 (StrLength(msgn) > 0) )
1506 StrBufAppendBufPlain(refs, HKEY("|"), 0);
1508 if (StrLength(msgn) > 0) {
1509 StrBufAppendBuf(refs, msgn, 0);
1511 PutBstr(HKEY("references"), refs);
1515 * If this is a Reply or a ReplyAll, copy the sender's email into the To: field
1517 if ( (!strcasecmp(bstr("replying_mode"), "reply"))
1518 || (!strcasecmp(bstr("replying_mode"), "replyall"))
1521 if (StrLength(rfca) > 0) {
1522 to_rcpt = NewStrBuf();
1523 StrBufAppendBuf(to_rcpt, from, 0);
1524 StrBufAppendBufPlain(to_rcpt, HKEY(" <"), 0);
1525 StrBufAppendBuf(to_rcpt, rfca, 0);
1526 StrBufAppendBufPlain(to_rcpt, HKEY(">"), 0);
1531 if ( (StrLength(node) > 0)
1532 && (strcasecmp(ChrPtr(node), ChrPtr(WC->serv_info->serv_nodename)))
1534 StrBufAppendBufPlain(to_rcpt, HKEY(" @ "), 0);
1535 StrBufAppendBuf(to_rcpt, node, 0);
1538 PutBstr(HKEY("recp"), to_rcpt);
1542 * Only if this is a ReplyAll, copy all recipients into the Cc: field
1544 if ( (!strcasecmp(bstr("replying_mode"), "replyall"))
1546 StrBuf *cc_rcpt = rcpt;
1548 if (StrLength(cccc) > 0) {
1549 if (cc_rcpt != NULL) {
1550 StrBufAppendPrintf(cc_rcpt, ", ");
1551 StrBufAppendBuf(cc_rcpt, cccc, 0);
1557 if (cc_rcpt != NULL)
1558 PutBstr(HKEY("cc"), cc_rcpt);
1570 * Otherwise proceed normally.
1571 * Do a custom room banner with no navbar...
1574 if (recipient_required) {
1575 const StrBuf *Recp = NULL;
1576 const StrBuf *Cc = NULL;
1577 const StrBuf *Bcc = NULL;
1578 char *wikipage = NULL;
1579 StrBuf *CmdBuf = NULL;
1580 const char CMD[] = "ENT0 0|%s|%d|0||%s||%s|%s|%s";
1582 Recp = sbstr("recp");
1585 wikipage = strdup(bstr("page"));
1586 str_wiki_index(wikipage);
1588 CmdBuf = NewStrBufPlain(NULL,
1591 StrLength(display_name) +
1596 StrBufPrintf(CmdBuf,
1600 ChrPtr(display_name),
1605 serv_puts(ChrPtr(CmdBuf));
1606 StrBuf_ServGetln(CmdBuf);
1609 rc = GetServerStatusMsg(CmdBuf, &Result, 0, 0);
1611 if ( (Result == 570) /* invalid or missing recipient(s) */
1612 || (Result == 550) /* higher access required to send Internet mail */
1614 /* These errors will have been displayed and are excusable */
1616 else if (rc != 2) { /* Any other error means that we cannot continue */
1617 AppendImportantMessage(ChrPtr(CmdBuf) + 4, StrLength(CmdBuf) - 4);
1618 FreeStrBuf(&CmdBuf);
1619 readloop(readnew, eUseDefault);
1622 FreeStrBuf(&CmdBuf);
1624 if (recipient_required)
1625 PutBstr(HKEY("__RCPTREQUIRED"), NewStrBufPlain(HKEY("1")));
1626 if (recipient_required || subject_required)
1627 PutBstr(HKEY("__SUBJREQUIRED"), NewStrBufPlain(HKEY("1")));
1630 output_headers(1, 0, 0, 0, 1, 0);
1631 DoTemplate(HKEY("edit_message"), NULL, &NoCtx);
1640 void delete_msg(void)
1645 msgid = lbstr("msgid");
1647 if ((WC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
1648 serv_printf("DELE %ld", msgid);
1650 else { /* Otherwise move it to Trash */
1651 serv_printf("MOVE %ld|_TRASH_|0", msgid);
1654 StrBuf_ServGetln(Line);
1655 GetServerStatusMsg(Line, NULL, 1, 0);
1657 readloop(readnew, eUseDefault);
1662 * move a message to another room
1668 msgid = lbstr("msgid");
1670 if (havebstr("move_button")) {
1672 serv_printf("MOVE %ld|%s", msgid, bstr("target_room"));
1674 StrBuf_ServGetln(Line);
1675 GetServerStatusMsg(Line, NULL, 1, 0);
1678 AppendImportantMessage(_("The message was not moved."), -1);
1681 readloop(readnew, eUseDefault);
1687 * Generic function to output an arbitrary MIME attachment from
1688 * message being composed
1690 * partnum The MIME part to be output
1691 * filename Fake filename to give
1692 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1694 void postpart(StrBuf *partnum, StrBuf *filename, int force_download)
1697 StrBuf *content_type;
1698 wc_mime_attachment *part;
1701 i = StrToi(partnum);
1702 if (GetHash(WC->attachments, IKEY(i), &vPart) &&
1704 part = (wc_mime_attachment*) vPart;
1705 if (force_download) {
1706 content_type = NewStrBufPlain(HKEY("application/octet-stream"));
1709 content_type = NewStrBufDup(part->ContentType);
1711 StrBufAppendBuf(WC->WBuf, part->Data, 0);
1712 http_transmit_thing(ChrPtr(content_type), 0);
1714 hprintf("HTTP/1.1 404 %s\n", ChrPtr(partnum));
1715 output_headers(0, 0, 0, 0, 0, 0);
1716 hprintf("Content-Type: text/plain\r\n");
1718 wc_printf(_("An error occurred while retrieving this part: %s/%s\n"),
1719 ChrPtr(partnum), ChrPtr(filename));
1722 FreeStrBuf(&content_type);
1727 * Generic function to output an arbitrary MIME part from an arbitrary
1728 * message number on the server.
1730 * msgnum Number of the item on the citadel server
1731 * partnum The MIME part to be output
1732 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1734 void mimepart(int force_download)
1739 wcsession *WCC = WC;
1742 StrBuf *ContentType = NewStrBufPlain(HKEY("application/octet-stream"));
1745 att = Buf = NewStrBuf();
1746 msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
1747 StrBufExtract_token(att, WCC->Hdr->HR.ReqLine, 1, '/');
1749 serv_printf("OPNA %ld|%s", msgnum, ChrPtr(att));
1750 StrBuf_ServGetln(Buf);
1751 if (GetServerStatus(Buf, &ErrorDetail) == 2) {
1752 StrBufCutLeft(Buf, 4);
1753 bytes = StrBufExtract_long(Buf, 0, '|');
1754 if (!force_download) {
1755 StrBufExtract_token(ContentType, Buf, 3, '|');
1758 serv_read_binary(WCC->WBuf, bytes, Buf);
1760 StrBuf_ServGetln(Buf);
1761 CT = ChrPtr(ContentType);
1763 if (!force_download) {
1764 if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
1765 StrBufExtract_token(Buf, WCC->Hdr->HR.ReqLine, 2, '/');
1766 CT = GuessMimeByFilename(SKEY(Buf));
1768 if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
1769 CT = GuessMimeType(SKEY(WCC->WBuf));
1772 http_transmit_thing(CT, 0);
1774 StrBufCutLeft(Buf, 4);
1775 switch (ErrorDetail) {
1777 case ERROR + MESSAGE_NOT_FOUND:
1778 hprintf("HTTP/1.1 404 %s\n", ChrPtr(Buf));
1780 case ERROR + NOT_LOGGED_IN:
1781 hprintf("HTTP/1.1 401 %s\n", ChrPtr(Buf));
1784 case ERROR + HIGHER_ACCESS_REQUIRED:
1785 hprintf("HTTP/1.1 403 %s\n", ChrPtr(Buf));
1787 case ERROR + INTERNAL_ERROR:
1788 case ERROR + TOO_BIG:
1789 hprintf("HTTP/1.1 500 %s\n", ChrPtr(Buf));
1792 output_headers(0, 0, 0, 0, 0, 0);
1793 hprintf("Content-Type: text/plain\r\n");
1795 wc_printf(_("An error occurred while retrieving this part: %s\n"),
1799 FreeStrBuf(&ContentType);
1805 * Read any MIME part of a message, from the server, into memory.
1807 StrBuf *load_mimepart(long msgnum, char *partnum)
1813 serv_printf("DLAT %ld|%s", msgnum, partnum);
1814 StrBuf_ServGetln(Buf);
1815 if (GetServerStatus(Buf, NULL) == 6) {
1816 StrBufCutLeft(Buf, 4);
1817 bytes = StrBufExtract_long(Buf, 0, '|');
1820 StrBuf_ServGetBLOBBuffered(Buf, bytes);
1830 * Read any MIME part of a message, from the server, into memory.
1832 void MimeLoadData(wc_mime_attachment *Mime)
1837 /* TODO: is there a chance the content type is different from the one we know? */
1839 serv_printf("DLAT %ld|%s", Mime->msgnum, ChrPtr(Mime->PartNum));
1841 StrBuf_ServGetln(Buf);
1842 if (GetServerStatus(Buf, NULL) == 6) {
1843 Ptr = &(ChrPtr(Buf)[4]);
1844 bytes = StrBufExtractNext_long(Buf, &Ptr, '|');
1845 StrBufSkip_NTokenS(Buf, &Ptr, '|', 3); /* filename, cbtype, mimetype */
1846 if (Mime->Charset == NULL) Mime->Charset = NewStrBuf();
1847 StrBufExtract_NextToken(Mime->Charset, Buf, &Ptr, '|');
1849 if (Mime->Data == NULL)
1850 Mime->Data = NewStrBufPlain(NULL, bytes);
1851 StrBuf_ServGetBLOBBuffered(Mime->Data, bytes);
1854 FlushStrBuf(Mime->Data);
1855 /* TODO XImportant message */
1861 void view_mimepart(void) {
1865 void download_mimepart(void) {
1869 void view_postpart(void) {
1870 StrBuf *filename = NewStrBuf();
1871 StrBuf *partnum = NewStrBuf();
1873 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1874 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1876 postpart(partnum, filename, 0);
1878 FreeStrBuf(&filename);
1879 FreeStrBuf(&partnum);
1882 void download_postpart(void) {
1883 StrBuf *filename = NewStrBuf();
1884 StrBuf *partnum = NewStrBuf();
1886 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1887 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1889 postpart(partnum, filename, 1);
1891 FreeStrBuf(&filename);
1892 FreeStrBuf(&partnum);
1897 void show_num_attachments(void) {
1898 wc_printf("%d", GetCount(WC->attachments));
1902 void h_readnew(void) { readloop(readnew, eUseDefault);}
1903 void h_readold(void) { readloop(readold, eUseDefault);}
1904 void h_readfwd(void) { readloop(readfwd, eUseDefault);}
1905 void h_headers(void) { readloop(headers, eUseDefault);}
1906 void h_do_search(void) { readloop(do_search, eUseDefault);}
1907 void h_readgt(void) { readloop(readgt, eUseDefault);}
1908 void h_readlt(void) { readloop(readlt, eUseDefault);}
1912 /* Output message list in JSON format */
1913 void jsonMessageList(void) {
1914 StrBuf *View = NewStrBuf();
1915 const StrBuf *room = sbstr("room");
1916 long oper = (havebstr("query")) ? do_search : readnew;
1917 StrBufPrintf(View, "%d", VIEW_JSON_LIST);
1918 putbstr("view", View);;
1920 readloop(oper, eUseDefault);
1923 void RegisterReadLoopHandlerset(
1925 GetParamsGetServerCall_func GetParamsGetServerCall,
1926 PrintViewHeader_func PrintPageHeader,
1927 PrintViewHeader_func PrintViewHeader,
1928 load_msg_ptrs_detailheaders LH,
1929 LoadMsgFromServer_func LoadMsgFromServer,
1930 RenderView_or_Tail_func RenderView_or_Tail,
1931 View_Cleanup_func ViewCleanup
1934 RoomRenderer *Handler;
1936 Handler = (RoomRenderer*) malloc(sizeof(RoomRenderer));
1938 Handler->RoomType = RoomType;
1939 Handler->GetParamsGetServerCall = GetParamsGetServerCall;
1940 Handler->PrintPageHeader = PrintPageHeader;
1941 Handler->PrintViewHeader = PrintViewHeader;
1942 Handler->LoadMsgFromServer = LoadMsgFromServer;
1943 Handler->RenderView_or_Tail = RenderView_or_Tail;
1944 Handler->ViewCleanup = ViewCleanup;
1945 Handler->LHParse = LH;
1947 Put(ReadLoopHandler, IKEY(RoomType), Handler, NULL);
1954 RegisterPreference("use_sig",
1955 _("Attach signature to email messages?"),
1958 RegisterPreference("signature", _("Use this signature:"), PRF_QP_STRING, NULL);
1959 RegisterPreference("default_header_charset",
1960 _("Default character set for email headers:"),
1963 RegisterPreference("defaultfrom", _("Preferred email address"), PRF_STRING, NULL);
1964 RegisterPreference("defaultname",
1965 _("Preferred display name for email messages"),
1968 RegisterPreference("defaulthandle",
1969 _("Preferred display name for bulletin board posts"),
1972 RegisterPreference("mailbox",_("Mailbox view mode"), PRF_STRING, NULL);
1974 WebcitAddUrlHandler(HKEY("readnew"), "", 0, h_readnew, ANONYMOUS|NEED_URL);
1975 WebcitAddUrlHandler(HKEY("readold"), "", 0, h_readold, ANONYMOUS|NEED_URL);
1976 WebcitAddUrlHandler(HKEY("readfwd"), "", 0, h_readfwd, ANONYMOUS|NEED_URL);
1977 WebcitAddUrlHandler(HKEY("headers"), "", 0, h_headers, NEED_URL);
1978 WebcitAddUrlHandler(HKEY("readgt"), "", 0, h_readgt, ANONYMOUS|NEED_URL);
1979 WebcitAddUrlHandler(HKEY("readlt"), "", 0, h_readlt, ANONYMOUS|NEED_URL);
1980 WebcitAddUrlHandler(HKEY("do_search"), "", 0, h_do_search, 0);
1981 WebcitAddUrlHandler(HKEY("display_enter"), "", 0, display_enter, 0);
1982 WebcitAddUrlHandler(HKEY("post"), "", 0, post_message, PROHIBIT_STARTPAGE);
1983 WebcitAddUrlHandler(HKEY("move_msg"), "", 0, move_msg, PROHIBIT_STARTPAGE);
1984 WebcitAddUrlHandler(HKEY("delete_msg"), "", 0, delete_msg, PROHIBIT_STARTPAGE);
1985 WebcitAddUrlHandler(HKEY("msg"), "", 0, embed_message, NEED_URL);
1986 WebcitAddUrlHandler(HKEY("message"), "", 0, handle_one_message, NEED_URL|XHTTP_COMMANDS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
1987 WebcitAddUrlHandler(HKEY("printmsg"), "", 0, print_message, NEED_URL);
1988 WebcitAddUrlHandler(HKEY("msgheaders"), "", 0, display_headers, NEED_URL);
1990 WebcitAddUrlHandler(HKEY("mimepart"), "", 0, view_mimepart, NEED_URL);
1991 WebcitAddUrlHandler(HKEY("mimepart_download"), "", 0, download_mimepart, NEED_URL);
1992 WebcitAddUrlHandler(HKEY("postpart"), "", 0, view_postpart, NEED_URL|PROHIBIT_STARTPAGE);
1993 WebcitAddUrlHandler(HKEY("postpart_download"), "", 0, download_postpart, NEED_URL|PROHIBIT_STARTPAGE);
1994 WebcitAddUrlHandler(HKEY("upload_attachment"), "", 0, upload_attachment, AJAX);
1995 WebcitAddUrlHandler(HKEY("remove_attachment"), "", 0, remove_attachment, AJAX);
1996 WebcitAddUrlHandler(HKEY("show_num_attachments"), "", 0, show_num_attachments, AJAX);
1999 WebcitAddUrlHandler(HKEY("roommsgs"), "", 0, jsonMessageList,0);
2001 l_subj = FourHash("subj", 4);
2002 l_wefw = FourHash("wefw", 4);
2003 l_msgn = FourHash("msgn", 4);
2004 l_from = FourHash("from", 4);
2005 l_rcpt = FourHash("rcpt", 4);
2006 l_cccc = FourHash("cccc", 4);
2007 l_node = FourHash("node", 4);
2008 l_rfca = FourHash("rfca", 4);
2014 SessionDetachModule_MSG
2017 DeleteHash(&sess->summ);