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.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
22 #include "webserver.h"
26 HashList *MsgHeaderHandler = NULL;
27 HashList *MsgEvaluators = NULL;
28 HashList *MimeRenderHandler = NULL;
29 HashList *ReadLoopHandler = NULL;
30 int dbg_analyze_msg = 0;
32 #define SUBJ_COL_WIDTH_PCT 50 /* Mailbox view column width */
33 #define SENDER_COL_WIDTH_PCT 30 /* Mailbox view column width */
34 #define DATE_PLUS_BUTTONS_WIDTH_PCT 20 /* Mailbox view column width */
36 void jsonMessageListHdr(void);
38 void display_enter(void);
40 typedef void (*MsgPartEvaluatorFunc)(message_summary *Sum, StrBuf *Buf);
42 typedef struct _MsgPartEvaluatorStruct {
43 MsgPartEvaluatorFunc f;
44 } MsgPartEvaluatorStruct;
46 int load_message(message_summary *Msg,
59 if (Msg->PartNum != NULL) {
60 serv_printf("MSG4 %ld|%s", Msg->msgnum, ChrPtr(Msg->PartNum));
63 serv_printf("MSG4 %ld", Msg->msgnum);
66 StrBuf_ServGetln(Buf);
67 if (GetServerStatus(Buf, NULL) != 1) {
69 StrBufAppendPrintf(*Error, "<strong>");
70 StrBufAppendPrintf(*Error, _("ERROR:"));
71 StrBufAppendPrintf(*Error, "</strong> %s<br>\n", &buf[4]);
76 /* begin everythingamundo table */
77 HdrToken = NewStrBuf();
78 while (!Done && StrBuf_ServGetln(Buf)>=0) {
79 if ( (StrLength(Buf)==3) &&
80 !strcmp(ChrPtr(Buf), "000"))
84 if (Msg->MsgBody->Data == NULL)
85 Msg->MsgBody->Data = NewStrBuf();
86 Msg->MsgBody->ContentType = NewStrBufPlain(HKEY("text/html"));
87 StrBufAppendPrintf(Msg->MsgBody->Data, "<div><i>");
88 StrBufAppendPrintf(Msg->MsgBody->Data, _("Empty message"));
89 StrBufAppendPrintf(Msg->MsgBody->Data, "</i><br><br>\n");
90 StrBufAppendPrintf(Msg->MsgBody->Data, "</div>\n");
95 case 0:/* Citadel Message Headers */
96 if (StrLength(Buf) == 0) {
100 StrBufExtract_token(HdrToken, Buf, 0, '=');
101 StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
103 /* look up one of the examine_* functions to parse the content */
104 if (GetHash(MsgHeaderHandler, SKEY(HdrToken), &vHdr) &&
106 Hdr = (headereval*)vHdr;
107 Hdr->evaluator(Msg, Buf, FoundCharset);
108 if (Hdr->Type == 1) {
112 else LogError(Target,
114 "don't know how to handle message header[%s]\n",
118 case 1:/* Message Mime Header */
119 if (StrLength(Buf) == 0) {
121 if (Msg->MsgBody->ContentType == NULL)
122 /* end of header or no header? */
123 Msg->MsgBody->ContentType = NewStrBufPlain(HKEY("text/plain"));
124 /* usual end of mime header */
128 StrBufExtract_token(HdrToken, Buf, 0, ':');
129 if (StrLength(HdrToken) > 0) {
130 StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
131 /* the examine*'s know how to do with mime headers too... */
132 if (GetHash(MsgHeaderHandler, SKEY(HdrToken), &vHdr) &&
134 Hdr = (headereval*)vHdr;
135 Hdr->evaluator(Msg, Buf, FoundCharset);
140 case 2: /* Message Body */
142 if (Msg->MsgBody->size_known > 0) {
143 StrBuf_ServGetBLOBBuffered(Msg->MsgBody->Data, Msg->MsgBody->length);
145 /*/ todo: check next line, if not 000, append following lines */
148 if (StrLength(Msg->MsgBody->Data) > 0)
149 StrBufAppendBufPlain(Msg->MsgBody->Data, "\n", 1, 0);
150 StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
154 StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
159 if (Msg->AllAttach == NULL)
160 Msg->AllAttach = NewHash(1,NULL);
161 /* now we put the body mimepart we read above into the mimelist */
162 Put(Msg->AllAttach, SKEY(Msg->MsgBody->PartNum), Msg->MsgBody, DestroyMime);
165 FreeStrBuf(&HdrToken);
172 * I wanna SEE that message!
174 * msgnum Message number to display
175 * printable_view Nonzero to display a printable view
176 * section Optional for encapsulated message/rfc822 submessage
178 int read_message(StrBuf *Target, const char *tmpl, long tmpllen, long msgnum, const StrBuf *PartNum, const StrBuf **OutMime)
181 StrBuf *FoundCharset;
184 message_summary *Msg = NULL;
188 WCTemplputParams SubTP;
189 StrBuf *Error = NULL;
192 FoundCharset = NewStrBuf();
193 Msg = (message_summary *)malloc(sizeof(message_summary));
194 memset(Msg, 0, sizeof(message_summary));
195 Msg->msgnum = msgnum;
196 Msg->PartNum = PartNum;
197 Msg->MsgBody = (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
198 memset(Msg->MsgBody, 0, sizeof(wc_mime_attachment));
199 Msg->MsgBody->msgnum = msgnum;
201 if (!load_message(Msg, FoundCharset, &Error)) {
202 StrBufAppendBuf(Target, Error, 0);
206 /* Extract just the content-type (omit attributes such as "charset") */
207 StrBufExtract_token(Buf, Msg->MsgBody->ContentType, 0, ';');
209 StrBufLowerCase(Buf);
211 /* Locate a renderer capable of converting this MIME part into HTML */
212 if (GetHash(MimeRenderHandler, SKEY(Buf), &vHdr) &&
214 RenderMimeFuncStruct *Render;
215 Render = (RenderMimeFuncStruct*)vHdr;
216 Render->f(Msg->MsgBody, NULL, FoundCharset);
219 if (StrLength(Msg->reply_references)> 0) {
220 /* Trim down excessively long lists of thread references. We eliminate the
221 * second one in the list so that the thread root remains intact.
223 int rrtok = num_tokens(ChrPtr(Msg->reply_references), '|');
224 int rrlen = StrLength(Msg->reply_references);
225 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
226 StrBufRemove_token(Msg->reply_references, 1, '|');
230 /* now check if we need to translate some mimeparts, and remove the duplicate */
231 it = GetNewHashPos(Msg->AllAttach, 0);
232 while (GetNextHashPos(Msg->AllAttach, it, &len, &Key, &vMime) &&
234 wc_mime_attachment *Mime = (wc_mime_attachment*) vMime;
235 evaluate_mime_part(Msg, Mime);
238 memset(&SubTP, 0, sizeof(WCTemplputParams));
239 SubTP.Filter.ContextType = CTX_MAILSUM;
241 *OutMime = DoTemplate(tmpl, tmpllen, Target, &SubTP);
243 DestroyMessageSummary(Msg);
244 FreeStrBuf(&FoundCharset);
251 HttpStatus(long CitadelStatus)
253 long httpstatus = 502;
255 switch (MAJORCODE(CitadelStatus))
257 case LISTING_FOLLOWS:
262 switch (MINORCODE(CitadelStatus))
270 case HIGHER_ACCESS_REQUIRED:
271 case MAX_SESSIONS_EXCEEDED:
273 case RESOURCE_NOT_OPEN:
275 case INVALID_FLOOR_OPERATION:
281 case MESSAGE_NOT_FOUND:
291 case CMD_NOT_SUPPORTED:
292 case PASSWORD_REQUIRED:
293 case ALREADY_LOGGED_IN:
294 case USERNAME_REQUIRED:
296 case SERVER_SHUTTING_DOWN:
307 case START_CHAT_MODE:
311 httpstatus = 502; /* aeh... whut? */
319 * Unadorned HTML output of an individual message, suitable
320 * for placing in a hidden iframe, for printing, or whatever
322 void handle_one_message(void)
326 const StrBuf *Destination;
332 StrBuf *CmdBuf = NULL;
336 pMsg = strchr(ChrPtr(WCC->Hdr->HR.ReqLine), '/');
338 HttpStatus(CitStatus);
342 msgnum = atol(pMsg + 1);
343 StrBufCutAt(WCC->Hdr->HR.ReqLine, 0, pMsg);
344 gotoroom(WCC->Hdr->HR.ReqLine);
345 switch (WCC->Hdr->HR.eReqType)
349 Tmpl = sbstr("template");
350 if (StrLength(Tmpl) > 0)
351 read_message(WCC->WBuf, SKEY(Tmpl), msgnum, NULL, &Mime);
353 read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
354 http_transmit_thing(ChrPtr(Mime), 0);
357 CmdBuf = NewStrBuf ();
358 if ((WCC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
359 serv_printf("DELE %ld", msgnum);
361 else { /* Otherwise move it to Trash */
362 serv_printf("MOVE %ld|_TRASH_|0", msgnum);
364 StrBuf_ServGetln(CmdBuf);
365 GetServerStatusMsg(CmdBuf, &CitStatus, 1, 0);
366 HttpStatus(CitStatus);
371 if (GetHash(WCC->Hdr->HTTPHeaders, HKEY("DESTINATION"), &vLine) &&
373 Destination = (StrBuf*) vLine;
374 serv_printf("MOVE %ld|%s|%d", msgnum, ChrPtr(Destination), CopyMessage);
375 StrBuf_ServGetln(CmdBuf);
376 GetServerStatusMsg(CmdBuf, NULL, 1, 0);
377 GetServerStatus(CmdBuf, &CitStatus);
378 HttpStatus(CitStatus);
391 * Unadorned HTML output of an individual message, suitable
392 * for placing in a hidden iframe, for printing, or whatever
394 void embed_message(void) {
399 StrBuf *CmdBuf = NULL;
401 msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
402 if (msgnum <= 0) return;
404 switch (WCC->Hdr->HR.eReqType)
408 Tmpl = sbstr("template");
409 if (StrLength(Tmpl) > 0)
410 read_message(WCC->WBuf, SKEY(Tmpl), msgnum, NULL, &Mime);
412 read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
413 http_transmit_thing(ChrPtr(Mime), 0);
416 CmdBuf = NewStrBuf ();
417 if ((WCC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
418 serv_printf("DELE %ld", msgnum);
420 else { /* Otherwise move it to Trash */
421 serv_printf("MOVE %ld|_TRASH_|0", msgnum);
423 StrBuf_ServGetln(CmdBuf);
424 GetServerStatusMsg(CmdBuf, NULL, 1, 0);
434 * Printable view of a message
436 void print_message(void) {
440 msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
441 output_headers(0, 0, 0, 0, 0, 0);
443 hprintf("Content-type: text/html\r\n"
444 "Server: " PACKAGE_STRING "\r\n"
445 "Connection: close\r\n");
449 read_message(WC->WBuf, HKEY("view_message_print"), msgnum, NULL, &Mime);
455 * Display a message's headers
457 void display_headers(void) {
461 msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
462 output_headers(0, 0, 0, 0, 0, 0);
464 hprintf("Content-type: text/plain\r\n"
466 "Connection: close\r\n",
470 serv_printf("MSG2 %ld|3", msgnum);
471 serv_getln(buf, sizeof buf);
473 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
474 wc_printf("%s\n", buf);
482 message_summary *ReadOneMessageSummary(StrBuf *RawMessage, const char *DefaultSubject, long MsgNum)
485 MsgPartEvaluatorStruct *Eval;
486 message_summary *Msg;
495 serv_printf("MSG0 %ld|1", MsgNum); /* ask for headers only */
497 StrBuf_ServGetln(Buf);
498 if (GetServerStatus(Buf, NULL) == 1) {
503 Msg = (message_summary*)malloc(sizeof(message_summary));
504 memset(Msg, 0, sizeof(message_summary));
505 while (len = StrBuf_ServGetln(Buf),
508 strcmp(ChrPtr(Buf), "000")))
511 ebuf = strchr(ChrPtr(Buf), '=');
513 if (GetHash(MsgEvaluators, buf, nBuf, &vEval) && vEval != NULL) {
514 Eval = (MsgPartEvaluatorStruct*) vEval;
515 StrBufCutLeft(Buf, nBuf + 1);
518 else syslog(1, "Don't know how to handle Message Headerline [%s]", ChrPtr(Buf));
528 * load message pointers from the server for a "read messages" operation
530 * servcmd: the citadel command to send to the citserver
532 int load_msg_ptrs(const char *servcmd,
534 SharedMessageStatus *Stat,
535 load_msg_ptrs_detailheaders LH)
538 message_summary *Msg;
543 const char *Ptr = NULL;
546 Stat->lowest_found = LONG_MAX;
547 Stat->highest_found = LONG_MIN;
549 if (WCC->summ != NULL) {
550 DeleteHash(&WCC->summ);
552 WCC->summ = NewHash(1, Flathash);
556 StrBuf_ServGetln(Buf);
557 StatMajor = GetServerStatus(Buf, NULL);
562 if (filter != NULL) {
567 /* fall back to empty filter in case of we were fooled... */
573 return (Stat->nummsgs);
576 while (len = StrBuf_ServGetln(Buf),
579 strcmp(ChrPtr(Buf), "000")!= 0)))
581 if (Stat->nummsgs < Stat->maxload) {
584 Msg = (message_summary*)malloc(sizeof(message_summary));
585 memset(Msg, 0, sizeof(message_summary));
587 Msg->msgnum = StrBufExtractNext_long(Buf, &Ptr, '|');
588 Msg->date = StrBufExtractNext_long(Buf, &Ptr, '|');
590 if (Stat->nummsgs == 0) {
591 if (Msg->msgnum < Stat->lowest_found) {
592 Stat->lowest_found = Msg->msgnum;
594 if (Msg->msgnum > Stat->highest_found) {
595 Stat->highest_found = Msg->msgnum;
599 if ((Msg->msgnum == 0) && (StrLength(Buf) < 32)) {
605 * as citserver probably gives us messages in forward date sorting
606 * nummsgs should be the same order as the message date.
608 if (Msg->date == 0) {
609 Msg->date = Stat->nummsgs;
610 if (StrLength(Buf) < 32)
613 if ((!skipit) && (LH != NULL)) {
614 if (!LH(Buf, &Ptr, Msg, Buf2)){
620 Put(WCC->summ, (const char *)&n, sizeof(n), Msg, DestroyMessageSummary);
626 return (Stat->nummsgs);
632 * \brief sets FlagToSet for each of ScanMe that appears in MatchMSet
633 * \param ScanMe List of BasicMsgStruct to be searched it MatchSet
634 * \param MatchMSet MSet we want to flag
635 * \param FlagToSet Flag to set on each BasicMsgStruct->Flags if in MSet
637 void SetFlagsFromMSet(HashList *ScanMe, MSet *MatchMSet, int FlagToSet, int Reverse)
643 message_summary *Msg;
645 at = GetNewHashPos(ScanMe, 0);
646 while (GetNextHashPos(ScanMe, at, &HKLen, &HashKey, &vMsg)) {
647 /* Are you a new message, or an old message? */
648 Msg = (message_summary*) vMsg;
649 if (Reverse && IsInMSetList(MatchMSet, Msg->msgnum)) {
650 Msg->Flags = Msg->Flags | FlagToSet;
652 else if (!Reverse && !IsInMSetList(MatchMSet, Msg->msgnum)) {
653 Msg->Flags = Msg->Flags | FlagToSet;
660 void load_seen_flags(void)
666 OldMsg = NewStrBuf();
668 StrBuf_ServGetln(OldMsg);
669 if (GetServerStatus(OldMsg, NULL) == 2) {
670 StrBufCutLeft(OldMsg, 4);
677 if (ParseMSet(&MatchMSet, OldMsg))
679 SetFlagsFromMSet(WCC->summ, MatchMSet, MSGFLAG_READ, 0);
681 DeleteMSet(&MatchMSet);
685 extern readloop_struct rlid[];
687 typedef struct _RoomRenderer{
690 GetParamsGetServerCall_func GetParamsGetServerCall;
692 PrintViewHeader_func PrintPageHeader;
693 PrintViewHeader_func PrintViewHeader;
694 LoadMsgFromServer_func LoadMsgFromServer;
695 RenderView_or_Tail_func RenderView_or_Tail;
696 View_Cleanup_func ViewCleanup;
697 load_msg_ptrs_detailheaders LHParse;
702 * command loop for reading messages
704 * Set oper to "readnew" or "readold" or "readfwd" or "headers" or "readgt" or "readlt" or "do_search"
706 void readloop(long oper, eCustomRoomRenderer ForceRenderer)
708 RoomRenderer *ViewMsg;
711 message_summary *Msg;
713 char filter[256] = "";
719 WCTemplputParams SubTP;
720 SharedMessageStatus Stat;
723 if (havebstr("is_summary") && (1 == (ibstr("is_summary")))) {
724 WCC->CurRoom.view = VIEW_MAILBOX;
727 if (havebstr("view")) {
728 WCC->CurRoom.view = ibstr("view");
731 memset(&Stat, 0, sizeof(SharedMessageStatus));
732 Stat.maxload = 10000;
733 Stat.lowest_found = (-1);
734 Stat.highest_found = (-1);
735 if (ForceRenderer == eUseDefault)
736 GetHash(ReadLoopHandler, IKEY(WCC->CurRoom.view), &vViewMsg);
738 GetHash(ReadLoopHandler, IKEY(ForceRenderer), &vViewMsg);
739 if (vViewMsg == NULL) {
740 WCC->CurRoom.view = VIEW_BBS;
741 GetHash(ReadLoopHandler, IKEY(WCC->CurRoom.view), &vViewMsg);
743 if (vViewMsg == NULL) {
744 return; /* TODO: print message */
747 ViewMsg = (RoomRenderer*) vViewMsg;
748 if (ViewMsg->PrintPageHeader == NULL)
749 output_headers(1, 1, 1, 0, 0, 0);
751 ViewMsg->PrintPageHeader(&Stat, ViewSpecific);
753 if (ViewMsg->GetParamsGetServerCall != NULL) {
754 r = ViewMsg->GetParamsGetServerCall(
759 filter, sizeof(filter)
771 case 300: /* the callback hook should do the work for us here, since he knows what to do. */
777 if (!IsEmptyStr(cmd)) {
778 const char *p = NULL;
779 if (!IsEmptyStr(filter))
781 Stat.nummsgs = load_msg_ptrs(cmd, p, &Stat, ViewMsg->LHParse);
786 memset(&SubTP, 0, sizeof(WCTemplputParams));
787 SubTP.Filter.ContextType = CTX_MAILSUM;
788 SubTP.Context = NULL;
789 SortIt = RetrieveSort(&SubTP, NULL, 0,
790 HKEY("date"), Stat.defaultsortorder);
792 SortByPayload(WCC->summ, SortIt);
794 if (Stat.startmsg < 0) {
798 if (Stat.load_seen) load_seen_flags();
801 * Print any inforation above the message list...
803 if (ViewMsg->PrintViewHeader != NULL)
804 ViewMsg->PrintViewHeader(&Stat, &ViewSpecific);
806 WCC->startmsg = Stat.startmsg;
807 WCC->maxmsgs = Stat.maxmsgs;
808 WCC->num_displayed = 0;
810 /* Put some helpful data in vars for mailsummary_json */
815 StrBufPrintf(Foo, "%ld", Stat.nummsgs);
816 PutBstr(HKEY("__READLOOP:TOTALMSGS"), NewStrBufDup(Foo));
817 StrBufPrintf(Foo, "%ld", Stat.startmsg);
818 PutBstr(HKEY("__READLOOP:STARTMSG"), Foo);
822 * iterate over each message. if we need to load an attachment, do it here.
825 if ((ViewMsg->LoadMsgFromServer != NULL) &&
828 at = GetNewHashPos(WCC->summ, 0);
829 Stat.num_displayed = i = 0;
830 while ( GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
831 Msg = (message_summary*) vMsg;
832 if ((Msg->msgnum >= Stat.startmsg) && (Stat.num_displayed <= Stat.maxmsgs)) {
833 ViewMsg->LoadMsgFromServer(&Stat,
836 (Msg->Flags & MSGFLAG_READ) != 0,
845 * Done iterating the message list. now tasks we want to do after.
847 if (ViewMsg->RenderView_or_Tail != NULL)
848 ViewMsg->RenderView_or_Tail(&Stat, &ViewSpecific, oper);
850 if (ViewMsg->ViewCleanup != NULL)
851 ViewMsg->ViewCleanup(&ViewSpecific);
855 if (WCC->summ != NULL) {
856 DeleteHash(&WCC->summ);
862 * Back end for post_message()
863 * ... this is where the actual message gets transmitted to the server.
865 void post_mime_to_server(void) {
867 char top_boundary[SIZ];
868 char alt_boundary[SIZ];
869 int is_multipart = 0;
871 wc_mime_attachment *att;
873 size_t encoded_length;
874 size_t encoded_strlen;
875 char *txtmail = NULL;
876 int include_text_alt = 0; /* Set to nonzero to include multipart/alternative text/plain */
878 sprintf(top_boundary, "Citadel--Multipart--%s--%04x--%04x",
879 ChrPtr(WCC->serv_info->serv_fqdn),
883 sprintf(alt_boundary, "Citadel--Multipart--%s--%04x--%04x",
884 ChrPtr(WCC->serv_info->serv_fqdn),
889 /* RFC2045 requires this, and some clients look for it... */
890 serv_puts("MIME-Version: 1.0");
891 serv_puts("X-Mailer: " PACKAGE_STRING);
893 /* If there are attachments, we have to do multipart/mixed */
894 if (GetCount(WCC->attachments) > 0) {
898 /* Only do multipart/alternative for mailboxes. BBS and Wiki rooms don't need it. */
899 if (WC->CurRoom.view == VIEW_MAILBOX) {
900 include_text_alt = 1;
904 /* Remember, serv_printf() appends an extra newline */
905 serv_printf("Content-type: multipart/mixed; boundary=\"%s\"\n", top_boundary);
906 serv_printf("This is a multipart message in MIME format.\n");
907 serv_printf("--%s", top_boundary);
910 /* Remember, serv_printf() appends an extra newline */
911 if (include_text_alt) {
912 serv_printf("Content-type: multipart/alternative; "
913 "boundary=\"%s\"\n", alt_boundary);
914 serv_printf("This is a multipart message in MIME format.\n");
915 serv_printf("--%s", alt_boundary);
917 serv_puts("Content-type: text/plain; charset=utf-8");
918 serv_puts("Content-Transfer-Encoding: quoted-printable");
920 txtmail = html_to_ascii(bstr("msgtext"), 0, 80, 0);
921 text_to_server_qp(txtmail); /* Transmit message in quoted-printable encoding */
924 serv_printf("\n--%s", alt_boundary);
927 serv_puts("Content-type: text/html; charset=utf-8");
928 serv_puts("Content-Transfer-Encoding: quoted-printable");
930 serv_puts("<html><body>\r\n");
931 text_to_server_qp(bstr("msgtext")); /* Transmit message in quoted-printable encoding */
932 serv_puts("</body></html>\r\n");
934 if (include_text_alt) {
935 serv_printf("--%s--", alt_boundary);
944 /* Add in the attachments */
945 it = GetNewHashPos(WCC->attachments, 0);
946 while (GetNextHashPos(WCC->attachments, it, &len, &Key, &vAtt)) {
947 att = (wc_mime_attachment *)vAtt;
948 if (att->length == 0)
950 encoded_length = ((att->length * 150) / 100);
951 encoded = malloc(encoded_length);
952 if (encoded == NULL) break;
953 encoded_strlen = CtdlEncodeBase64(encoded, ChrPtr(att->Data), StrLength(att->Data), 1);
955 serv_printf("--%s", top_boundary);
956 serv_printf("Content-type: %s", ChrPtr(att->ContentType));
957 serv_printf("Content-disposition: attachment; filename=\"%s\"", ChrPtr(att->FileName));
958 serv_puts("Content-transfer-encoding: base64");
960 serv_write(encoded, encoded_strlen);
965 serv_printf("--%s--", top_boundary);
974 * Post message (or don't post message)
976 * Note regarding the "dont_post" variable:
977 * A random value (actually, it's just a timestamp) is inserted as a hidden
978 * field called "postseq" when the display_enter page is generated. This
979 * value is checked when posting, using the static variable dont_post. If a
980 * user attempts to post twice using the same dont_post value, the message is
981 * discarded. This prevents the accidental double-saving of the same message
982 * if the user happens to click the browser "back" button.
984 void post_message(void)
987 StrBuf *EmailAddress;
990 StrBuf *encoded_subject = NULL;
991 static long dont_post = (-1L);
992 int is_anonymous = 0;
993 const StrBuf *display_name = NULL;
997 if (havebstr("force_room")) {
998 gotoroom(sbstr("force_room"));
1001 if (havebstr("display_name")) {
1002 display_name = sbstr("display_name");
1003 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1004 display_name = NULL;
1009 if (!strcasecmp(bstr("submit_action"), "cancel")) {
1010 AppendImportantMessage(_("Cancelled. Message was not posted."), -1);
1011 } else if (lbstr("postseq") == dont_post) {
1012 AppendImportantMessage(
1013 _("Automatically cancelled because you have already "
1014 "saved this message."), -1);
1016 const char CMD[] = "ENT0 1|%s|%d|4|%s|%s||%s|%s|%s|%s|%s";
1017 StrBuf *Recp = NULL;
1020 const StrBuf *Wikipage = NULL;
1021 const StrBuf *my_email_addr = NULL;
1022 StrBuf *CmdBuf = NULL;
1023 StrBuf *references = NULL;
1024 int saving_to_drafts = 0;
1027 saving_to_drafts = !strcasecmp(bstr("submit_action"), "draft");
1030 if (saving_to_drafts) {
1031 /* temporarily change to the drafts room */
1032 serv_puts("GOTO _DRAFTS_");
1033 StrBuf_ServGetln(Buf);
1034 if (GetServerStatusMsg(Buf, NULL, 1, 2) != 2) {
1035 /* You probably don't even have a dumb Drafts folder */
1036 syslog(9, "%s:%d: server save to drafts error: %s\n", __FILE__, __LINE__, ChrPtr(Buf) + 4);
1037 AppendImportantMessage(_("Saved to Drafts failed: "), -1);
1044 if (havebstr("references"))
1046 const StrBuf *ref = sbstr("references");
1047 references = NewStrBufDup(ref);
1048 if (*ChrPtr(references) == '|') { /* remove leading '|' if present */
1049 StrBufCutLeft(references, 1);
1051 StrBufReplaceChars(references, '|', '!');
1053 if (havebstr("subject")) {
1056 * make enough room for the encoded string;
1057 * plus the QP header
1059 Subj = sbstr("subject");
1061 StrBufRFC2047encode(&encoded_subject, Subj);
1063 UserName = NewStrBuf();
1064 EmailAddress = NewStrBuf();
1065 EncBuf = NewStrBuf();
1067 Recp = StrBufSanitizeEmailRecipientVector(sbstr("recp"), UserName, EmailAddress, EncBuf);
1068 Cc = StrBufSanitizeEmailRecipientVector(sbstr("cc"), UserName, EmailAddress, EncBuf);
1069 Bcc = StrBufSanitizeEmailRecipientVector(sbstr("bcc"), UserName, EmailAddress, EncBuf);
1071 FreeStrBuf(&UserName);
1072 FreeStrBuf(&EmailAddress);
1073 FreeStrBuf(&EncBuf);
1075 Wikipage = sbstr("page");
1076 my_email_addr = sbstr("my_email_addr");
1078 HeaderLen = StrLength(Recp) +
1079 StrLength(encoded_subject) +
1082 StrLength(Wikipage) +
1083 StrLength(my_email_addr) +
1084 StrLength(references);
1085 CmdBuf = NewStrBufPlain(NULL, sizeof (CMD) + HeaderLen);
1086 StrBufPrintf(CmdBuf,
1088 saving_to_drafts?"":ChrPtr(Recp),
1090 ChrPtr(encoded_subject),
1091 ChrPtr(display_name),
1092 saving_to_drafts?"":ChrPtr(Cc),
1093 saving_to_drafts?"":ChrPtr(Bcc),
1095 ChrPtr(my_email_addr),
1096 ChrPtr(references));
1097 FreeStrBuf(&references);
1098 FreeStrBuf(&encoded_subject);
1100 if ((HeaderLen + StrLength(sbstr("msgtext")) < 10) &&
1101 (GetCount(WCC->attachments) == 0)){
1102 AppendImportantMessage(_("Refusing to post empty message.\n"), -1);
1103 FreeStrBuf(&CmdBuf);
1108 syslog(9, "%s\n", ChrPtr(CmdBuf));
1109 serv_puts(ChrPtr(CmdBuf));
1110 FreeStrBuf(&CmdBuf);
1112 StrBuf_ServGetln(Buf);
1113 if (GetServerStatus(Buf, NULL) == 4) {
1114 if (saving_to_drafts) {
1115 if ( (havebstr("recp"))
1116 || (havebstr("cc" ))
1117 || (havebstr("bcc" )) ) {
1118 /* save recipient headers or room to post to */
1119 serv_printf("To: %s", ChrPtr(Recp));
1120 serv_printf("Cc: %s", ChrPtr(Cc));
1121 serv_printf("Bcc: %s", ChrPtr(Bcc));
1123 serv_printf("X-Citadel-Room: %s", ChrPtr(WC->CurRoom.name));
1126 post_mime_to_server();
1127 if (saving_to_drafts) {
1128 AppendImportantMessage(_("Message has been saved to Drafts.\n"), -1);
1129 gotoroom(WCC->CurRoom.name);
1130 readloop(readnew, eUseDefault);
1133 } else if ( (havebstr("recp"))
1134 || (havebstr("cc" ))
1135 || (havebstr("bcc" ))
1137 AppendImportantMessage(_("Message has been sent.\n"), -1);
1140 AppendImportantMessage(_("Message has been posted.\n"), -1);
1142 dont_post = lbstr("postseq");
1144 syslog(9, "%s:%d: server post error: %s", __FILE__, __LINE__, ChrPtr(Buf) + 4);
1145 AppendImportantMessage(ChrPtr(Buf) + 4, StrLength(Buf) - 4);
1147 if (saving_to_drafts) gotoroom(WCC->CurRoom.name);
1161 DeleteHash(&WCC->attachments);
1164 * We may have been supplied with instructions regarding the location
1165 * to which we must return after posting. If found, go there.
1167 if (havebstr("return_to")) {
1168 http_redirect(bstr("return_to"));
1171 * If we were editing a page in a wiki room, go to that page now.
1173 else if (havebstr("page")) {
1174 snprintf(buf, sizeof buf, "wiki?page=%s", bstr("page"));
1178 * Otherwise, just go to the "read messages" loop.
1181 readloop(readnew, eUseDefault);
1187 * Client is uploading an attachment
1189 void upload_attachment(void) {
1190 wcsession *WCC = WC;
1196 wc_mime_attachment *att;
1198 syslog(9, "upload_attachment()\n");
1199 wc_printf("upload_attachment()<br>\n");
1201 if (WCC->upload_length <= 0) {
1202 syslog(9, "ERROR no attachment was uploaded\n");
1203 wc_printf("ERROR no attachment was uploaded<br>\n");
1207 syslog(9, "Client is uploading %d bytes\n", WCC->upload_length);
1208 wc_printf("Client is uploading %d bytes<br>\n", WCC->upload_length);
1209 att = malloc(sizeof(wc_mime_attachment));
1210 memset(att, 0, sizeof(wc_mime_attachment ));
1211 att->length = WCC->upload_length;
1212 att->ContentType = NewStrBufPlain(WCC->upload_content_type, -1);
1213 att->FileName = NewStrBufDup(WCC->upload_filename);
1215 if (WCC->attachments == NULL) {
1216 WCC->attachments = NewHash(1, Flathash);
1219 /* And add it to the list. */
1221 if ((GetCount(WCC->attachments) > 0) &&
1222 GetHashAt(WCC->attachments,
1223 GetCount(WCC->attachments) -1,
1224 &newnlen, &newn, &v))
1225 n = *((int*) newn) + 1;
1226 Put(WCC->attachments, IKEY(n), att, DestroyMime);
1229 * Mozilla sends a simple filename, which is what we want,
1230 * but Satan's Browser sends an entire pathname. Reduce
1231 * the path to just a filename if we need to.
1233 pch = strrchr(ChrPtr(att->FileName), '/');
1235 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1237 pch = strrchr(ChrPtr(att->FileName), '\\');
1239 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1243 * Transfer control of this memory from the upload struct
1244 * to the attachment struct.
1246 att->Data = WCC->upload;
1248 WCC->upload_length = 0;
1253 * Remove an attachment from the message currently being composed.
1255 * Currently we identify the attachment to be removed by its filename.
1256 * There is probably a better way to do this.
1258 void remove_attachment(void) {
1259 wcsession *WCC = WC;
1260 wc_mime_attachment *att;
1262 StrBuf *WhichAttachment;
1267 WhichAttachment = NewStrBufDup(sbstr("which_attachment"));
1268 StrBufUnescape(WhichAttachment, 0);
1269 at = GetNewHashPos(WCC->attachments, 0);
1271 GetHashPos(WCC->attachments, at, &len, &key, &vAtt);
1273 att = (wc_mime_attachment*) vAtt;
1274 if ((att != NULL) &&
1275 (strcmp(ChrPtr(WhichAttachment),
1276 ChrPtr(att->FileName) ) == 0))
1278 DeleteEntryFromHash(WCC->attachments, at);
1282 while (NextHashPos(WCC->attachments, at));
1283 FreeStrBuf(&WhichAttachment);
1284 wc_printf("remove_attachment() completed\n");
1288 long FourHash(const char *key, long length)
1292 const unsigned char *ptr = (const unsigned char*)key;
1294 for (i = 0; i < 4; i++, ptr ++)
1314 * display the message entry screen
1316 void display_enter(void)
1321 const StrBuf *display_name = NULL;
1322 int recipient_required = 0;
1323 int subject_required = 0;
1324 int is_anonymous = 0;
1325 wcsession *WCC = WC;
1329 if (havebstr("force_room")) {
1330 gotoroom(sbstr("force_room"));
1333 display_name = sbstr("display_name");
1334 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1335 display_name = NULL;
1340 * First, do we have permission to enter messages in this room at all?
1343 serv_puts("ENT0 0");
1344 StrBuf_ServGetln(Line);
1345 rc = GetServerStatusMsg(Line, &Result, 0, 2);
1347 if (Result == 570) { /* 570 means that we need a recipient here */
1348 recipient_required = 1;
1350 else if (rc != 2) { /* Any other error means that we cannot continue */
1351 rc = GetServerStatusMsg(Line, &Result, 0, 2);
1352 readloop(readnew, eUseDefault);
1357 /* Is the server strongly recommending that the user enter a message subject? */
1358 if (StrLength(Line) > 4) {
1359 subject_required = extract_int(ChrPtr(Line) + 4, 1);
1363 * Are we perhaps in an address book view? If so, then an "enter
1364 * message" command really means "add new entry."
1366 if (WCC->CurRoom.defview == VIEW_ADDRESSBOOK) {
1367 do_edit_vcard(-1, "", NULL, NULL, "", ChrPtr(WCC->CurRoom.name));
1373 * Are we perhaps in a calendar room? If so, then an "enter
1374 * message" command really means "add new calendar item."
1376 if (WCC->CurRoom.defview == VIEW_CALENDAR) {
1377 display_edit_event();
1383 * Are we perhaps in a tasks view? If so, then an "enter
1384 * message" command really means "add new task."
1386 if (WCC->CurRoom.defview == VIEW_TASKS) {
1387 display_edit_task();
1394 * If the "replying_to" variable is set, it refers to a message
1395 * number from which we must extract some header fields...
1397 replying_to = lbstr("replying_to");
1398 if (replying_to > 0) {
1400 StrBuf *wefw = NULL;
1401 StrBuf *msgn = NULL;
1402 StrBuf *from = NULL;
1403 StrBuf *node = NULL;
1404 StrBuf *rfca = NULL;
1405 StrBuf *rcpt = NULL;
1406 StrBuf *cccc = NULL;
1407 serv_printf("MSG0 %ld|1", replying_to);
1409 StrBuf_ServGetln(Line);
1410 if (GetServerStatusMsg(Line, NULL, 0, 0) == 1)
1411 while (len = StrBuf_ServGetln(Line),
1414 strcmp(ChrPtr(Line), "000")))
1417 if ((StrLength(Line) > 4) &&
1418 (ChrPtr(Line)[4] == '='))
1419 which = FourHash(ChrPtr(Line), 4);
1421 if (which == l_subj)
1423 StrBuf *subj = NewStrBuf();
1424 StrBuf *FlatSubject;
1426 if (!strcasecmp(bstr("replying_mode"), "forward")) {
1427 if (strncasecmp(ChrPtr(Line) + 5, "Fw:", 3)) {
1428 StrBufAppendBufPlain(subj, HKEY("Fw: "), 0);
1432 if (strncasecmp(ChrPtr(Line) + 5, "Re:", 3)) {
1433 StrBufAppendBufPlain(subj, HKEY("Re: "), 0);
1436 StrBufAppendBufPlain(subj,
1438 StrLength(Line) - 5, 0);
1439 FlatSubject = NewStrBufPlain(NULL, StrLength(subj));
1440 StrBuf_RFC822_to_Utf8(FlatSubject, subj, NULL, NULL);
1442 PutBstr(HKEY("subject"), FlatSubject);
1445 else if (which == l_wefw)
1450 wefw = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1452 /* Trim down excessively long lists of thread references. We eliminate the
1453 * second one in the list so that the thread root remains intact.
1455 rrtok = num_tokens(ChrPtr(wefw), '|');
1456 rrlen = StrLength(wefw);
1457 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
1458 StrBufRemove_token(wefw, 1, '|');
1462 else if (which == l_msgn) {
1463 msgn = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1466 else if (which == l_from) {
1468 from = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1469 FlatFrom = NewStrBufPlain(NULL, StrLength(from));
1470 StrBuf_RFC822_to_Utf8(FlatFrom, from, NULL, NULL);
1473 for (i=0; i<StrLength(from); ++i) {
1474 if (ChrPtr(from)[i] == ',')
1475 StrBufPeek(from, NULL, i, ' ');
1479 else if (which == l_rcpt) {
1480 rcpt = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1483 else if (which == l_cccc) {
1484 cccc = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1487 else if (which == l_node) {
1488 node = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1491 else if (which == l_rfca) {
1493 rfca = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1494 FlatRFCA = NewStrBufPlain(NULL, StrLength(rfca));
1495 StrBuf_RFC822_to_Utf8(FlatRFCA, rfca, NULL, NULL);
1502 if (StrLength(wefw) + StrLength(msgn) > 0) {
1503 StrBuf *refs = NewStrBuf();
1504 if (StrLength(wefw) > 0) {
1505 StrBufAppendBuf(refs, wefw, 0);
1507 if ( (StrLength(wefw) > 0) &&
1508 (StrLength(msgn) > 0) )
1510 StrBufAppendBufPlain(refs, HKEY("|"), 0);
1512 if (StrLength(msgn) > 0) {
1513 StrBufAppendBuf(refs, msgn, 0);
1515 PutBstr(HKEY("references"), refs);
1519 * If this is a Reply or a ReplyAll, copy the sender's email into the To: field
1521 if ( (!strcasecmp(bstr("replying_mode"), "reply"))
1522 || (!strcasecmp(bstr("replying_mode"), "replyall"))
1525 if (StrLength(rfca) > 0) {
1526 to_rcpt = NewStrBuf();
1527 StrBufAppendBuf(to_rcpt, from, 0);
1528 StrBufAppendBufPlain(to_rcpt, HKEY(" <"), 0);
1529 StrBufAppendBuf(to_rcpt, rfca, 0);
1530 StrBufAppendBufPlain(to_rcpt, HKEY(">"), 0);
1535 if ( (StrLength(node) > 0)
1536 && (strcasecmp(ChrPtr(node), ChrPtr(WC->serv_info->serv_nodename)))
1538 StrBufAppendBufPlain(to_rcpt, HKEY(" @ "), 0);
1539 StrBufAppendBuf(to_rcpt, node, 0);
1542 PutBstr(HKEY("recp"), to_rcpt);
1546 * Only if this is a ReplyAll, copy all recipients into the Cc: field
1548 if ( (!strcasecmp(bstr("replying_mode"), "replyall"))
1550 StrBuf *cc_rcpt = rcpt;
1552 if (StrLength(cccc) > 0) {
1553 if (cc_rcpt != NULL) {
1554 StrBufAppendPrintf(cc_rcpt, ", ");
1555 StrBufAppendBuf(cc_rcpt, cccc, 0);
1561 if (cc_rcpt != NULL)
1562 PutBstr(HKEY("cc"), cc_rcpt);
1574 * Otherwise proceed normally.
1575 * Do a custom room banner with no navbar...
1578 if (recipient_required) {
1579 const StrBuf *Recp = NULL;
1580 const StrBuf *Cc = NULL;
1581 const StrBuf *Bcc = NULL;
1582 const StrBuf *Wikipage = NULL;
1583 StrBuf *CmdBuf = NULL;
1584 const char CMD[] = "ENT0 0|%s|%d|0||%s||%s|%s|%s";
1586 Recp = sbstr("recp");
1589 Wikipage = sbstr("page");
1591 CmdBuf = NewStrBufPlain(NULL,
1594 StrLength(display_name) +
1597 StrLength(Wikipage));
1599 StrBufPrintf(CmdBuf,
1603 ChrPtr(display_name),
1607 serv_puts(ChrPtr(CmdBuf));
1608 StrBuf_ServGetln(CmdBuf);
1610 rc = GetServerStatusMsg(CmdBuf, &Result, 0, 0);
1612 if ( (Result == 570) /* invalid or missing recipient(s) */
1613 || (Result == 550) /* higher access required to send Internet mail */
1615 /* These errors will have been displayed and are excusable */
1617 else if (rc != 2) { /* Any other error means that we cannot continue */
1618 AppendImportantMessage(ChrPtr(CmdBuf) + 4, StrLength(CmdBuf) - 4);
1619 FreeStrBuf(&CmdBuf);
1620 readloop(readnew, eUseDefault);
1623 FreeStrBuf(&CmdBuf);
1625 if (recipient_required)
1626 PutBstr(HKEY("__RCPTREQUIRED"), NewStrBufPlain(HKEY("1")));
1627 if (recipient_required || subject_required)
1628 PutBstr(HKEY("__SUBJREQUIRED"), NewStrBufPlain(HKEY("1")));
1631 output_headers(1, 0, 0, 0, 1, 0);
1632 DoTemplate(HKEY("edit_message"), NULL, &NoCtx);
1641 void delete_msg(void)
1646 msgid = lbstr("msgid");
1648 if ((WC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
1649 serv_printf("DELE %ld", msgid);
1651 else { /* Otherwise move it to Trash */
1652 serv_printf("MOVE %ld|_TRASH_|0", msgid);
1655 StrBuf_ServGetln(Line);
1656 GetServerStatusMsg(Line, NULL, 1, 0);
1658 readloop(readnew, eUseDefault);
1663 * move a message to another room
1669 msgid = lbstr("msgid");
1671 if (havebstr("move_button")) {
1673 serv_printf("MOVE %ld|%s", msgid, bstr("target_room"));
1675 StrBuf_ServGetln(Line);
1676 GetServerStatusMsg(Line, NULL, 1, 0);
1679 AppendImportantMessage(_("The message was not moved."), -1);
1682 readloop(readnew, eUseDefault);
1688 * Generic function to output an arbitrary MIME attachment from
1689 * message being composed
1691 * partnum The MIME part to be output
1692 * filename Fake filename to give
1693 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1695 void postpart(StrBuf *partnum, StrBuf *filename, int force_download)
1698 StrBuf *content_type;
1699 wc_mime_attachment *part;
1702 i = StrToi(partnum);
1703 if (GetHash(WC->attachments, IKEY(i), &vPart) &&
1705 part = (wc_mime_attachment*) vPart;
1706 if (force_download) {
1707 content_type = NewStrBufPlain(HKEY("application/octet-stream"));
1710 content_type = NewStrBufDup(part->ContentType);
1712 StrBufAppendBuf(WC->WBuf, part->Data, 0);
1713 http_transmit_thing(ChrPtr(content_type), 0);
1715 hprintf("HTTP/1.1 404 %s\n", ChrPtr(partnum));
1716 output_headers(0, 0, 0, 0, 0, 0);
1717 hprintf("Content-Type: text/plain\r\n");
1719 wc_printf(_("An error occurred while retrieving this part: %s/%s\n"),
1720 ChrPtr(partnum), ChrPtr(filename));
1723 FreeStrBuf(&content_type);
1728 * Generic function to output an arbitrary MIME part from an arbitrary
1729 * message number on the server.
1731 * msgnum Number of the item on the citadel server
1732 * partnum The MIME part to be output
1733 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1735 void mimepart(int force_download)
1740 wcsession *WCC = WC;
1743 StrBuf *ContentType = NewStrBufPlain(HKEY("application/octet-stream"));
1746 att = Buf = NewStrBuf();
1747 msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
1748 StrBufExtract_token(att, WCC->Hdr->HR.ReqLine, 1, '/');
1750 serv_printf("OPNA %ld|%s", msgnum, ChrPtr(att));
1751 StrBuf_ServGetln(Buf);
1752 if (GetServerStatus(Buf, &ErrorDetail) == 2) {
1753 StrBufCutLeft(Buf, 4);
1754 bytes = StrBufExtract_long(Buf, 0, '|');
1755 if (!force_download) {
1756 StrBufExtract_token(ContentType, Buf, 3, '|');
1759 serv_read_binary(WCC->WBuf, bytes, Buf);
1761 StrBuf_ServGetln(Buf);
1762 CT = ChrPtr(ContentType);
1764 if (!force_download) {
1765 if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
1766 StrBufExtract_token(Buf, WCC->Hdr->HR.ReqLine, 2, '/');
1767 CT = GuessMimeByFilename(SKEY(Buf));
1769 if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
1770 CT = GuessMimeType(SKEY(WCC->WBuf));
1773 http_transmit_thing(CT, 0);
1775 StrBufCutLeft(Buf, 4);
1776 switch (ErrorDetail) {
1778 case ERROR + MESSAGE_NOT_FOUND:
1779 hprintf("HTTP/1.1 404 %s\n", ChrPtr(Buf));
1781 case ERROR + NOT_LOGGED_IN:
1782 hprintf("HTTP/1.1 401 %s\n", ChrPtr(Buf));
1785 case ERROR + HIGHER_ACCESS_REQUIRED:
1786 hprintf("HTTP/1.1 403 %s\n", ChrPtr(Buf));
1788 case ERROR + INTERNAL_ERROR:
1789 case ERROR + TOO_BIG:
1790 hprintf("HTTP/1.1 500 %s\n", ChrPtr(Buf));
1793 output_headers(0, 0, 0, 0, 0, 0);
1794 hprintf("Content-Type: text/plain\r\n");
1796 wc_printf(_("An error occurred while retrieving this part: %s\n"),
1800 FreeStrBuf(&ContentType);
1806 * Read any MIME part of a message, from the server, into memory.
1808 StrBuf *load_mimepart(long msgnum, char *partnum)
1814 serv_printf("DLAT %ld|%s", msgnum, partnum);
1815 StrBuf_ServGetln(Buf);
1816 if (GetServerStatus(Buf, NULL) == 6) {
1817 StrBufCutLeft(Buf, 4);
1818 bytes = StrBufExtract_long(Buf, 0, '|');
1821 StrBuf_ServGetBLOBBuffered(Buf, bytes);
1831 * Read any MIME part of a message, from the server, into memory.
1833 void MimeLoadData(wc_mime_attachment *Mime)
1838 /* TODO: is there a chance the content type is different from the one we know? */
1840 serv_printf("DLAT %ld|%s", Mime->msgnum, ChrPtr(Mime->PartNum));
1842 StrBuf_ServGetln(Buf);
1843 if (GetServerStatus(Buf, NULL) == 6) {
1844 Ptr = &(ChrPtr(Buf)[4]);
1845 bytes = StrBufExtractNext_long(Buf, &Ptr, '|');
1846 StrBufSkip_NTokenS(Buf, &Ptr, '|', 3); /* filename, cbtype, mimetype */
1847 if (Mime->Charset == NULL) Mime->Charset = NewStrBuf();
1848 StrBufExtract_NextToken(Mime->Charset, Buf, &Ptr, '|');
1850 if (Mime->Data == NULL)
1851 Mime->Data = NewStrBufPlain(NULL, bytes);
1852 StrBuf_ServGetBLOBBuffered(Mime->Data, bytes);
1855 FlushStrBuf(Mime->Data);
1856 /* TODO XImportant message */
1862 void view_mimepart(void) {
1866 void download_mimepart(void) {
1870 void view_postpart(void) {
1871 StrBuf *filename = NewStrBuf();
1872 StrBuf *partnum = NewStrBuf();
1874 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1875 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1877 postpart(partnum, filename, 0);
1879 FreeStrBuf(&filename);
1880 FreeStrBuf(&partnum);
1883 void download_postpart(void) {
1884 StrBuf *filename = NewStrBuf();
1885 StrBuf *partnum = NewStrBuf();
1887 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1888 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1890 postpart(partnum, filename, 1);
1892 FreeStrBuf(&filename);
1893 FreeStrBuf(&partnum);
1898 void show_num_attachments(void) {
1899 wc_printf("%d", GetCount(WC->attachments));
1903 void h_readnew(void) { readloop(readnew, eUseDefault);}
1904 void h_readold(void) { readloop(readold, eUseDefault);}
1905 void h_readfwd(void) { readloop(readfwd, eUseDefault);}
1906 void h_headers(void) { readloop(headers, eUseDefault);}
1907 void h_do_search(void) { readloop(do_search, eUseDefault);}
1908 void h_readgt(void) { readloop(readgt, eUseDefault);}
1909 void h_readlt(void) { readloop(readlt, eUseDefault);}
1913 /* Output message list in JSON format */
1914 void jsonMessageList(void) {
1915 StrBuf *View = NewStrBuf();
1916 const StrBuf *room = sbstr("room");
1917 long oper = (havebstr("query")) ? do_search : readnew;
1918 StrBufPrintf(View, "%d", VIEW_JSON_LIST);
1919 putbstr("view", View);;
1921 readloop(oper, eUseDefault);
1924 void RegisterReadLoopHandlerset(
1926 GetParamsGetServerCall_func GetParamsGetServerCall,
1927 PrintViewHeader_func PrintPageHeader,
1928 PrintViewHeader_func PrintViewHeader,
1929 load_msg_ptrs_detailheaders LH,
1930 LoadMsgFromServer_func LoadMsgFromServer,
1931 RenderView_or_Tail_func RenderView_or_Tail,
1932 View_Cleanup_func ViewCleanup
1935 RoomRenderer *Handler;
1937 Handler = (RoomRenderer*) malloc(sizeof(RoomRenderer));
1939 Handler->RoomType = RoomType;
1940 Handler->GetParamsGetServerCall = GetParamsGetServerCall;
1941 Handler->PrintPageHeader = PrintPageHeader;
1942 Handler->PrintViewHeader = PrintViewHeader;
1943 Handler->LoadMsgFromServer = LoadMsgFromServer;
1944 Handler->RenderView_or_Tail = RenderView_or_Tail;
1945 Handler->ViewCleanup = ViewCleanup;
1946 Handler->LHParse = LH;
1948 Put(ReadLoopHandler, IKEY(RoomType), Handler, NULL);
1955 RegisterPreference("use_sig",
1956 _("Attach signature to email messages?"),
1959 RegisterPreference("signature", _("Use this signature:"), PRF_QP_STRING, NULL);
1960 RegisterPreference("default_header_charset",
1961 _("Default character set for email headers:"),
1964 RegisterPreference("defaultfrom", _("Preferred email address"), PRF_STRING, NULL);
1965 RegisterPreference("defaultname",
1966 _("Preferred display name for email messages"),
1969 RegisterPreference("defaulthandle",
1970 _("Preferred display name for bulletin board posts"),
1973 RegisterPreference("mailbox",_("Mailbox view mode"), PRF_STRING, NULL);
1975 WebcitAddUrlHandler(HKEY("readnew"), "", 0, h_readnew, ANONYMOUS|NEED_URL);
1976 WebcitAddUrlHandler(HKEY("readold"), "", 0, h_readold, ANONYMOUS|NEED_URL);
1977 WebcitAddUrlHandler(HKEY("readfwd"), "", 0, h_readfwd, ANONYMOUS|NEED_URL);
1978 WebcitAddUrlHandler(HKEY("headers"), "", 0, h_headers, NEED_URL);
1979 WebcitAddUrlHandler(HKEY("readgt"), "", 0, h_readgt, ANONYMOUS|NEED_URL);
1980 WebcitAddUrlHandler(HKEY("readlt"), "", 0, h_readlt, ANONYMOUS|NEED_URL);
1981 WebcitAddUrlHandler(HKEY("do_search"), "", 0, h_do_search, 0);
1982 WebcitAddUrlHandler(HKEY("display_enter"), "", 0, display_enter, 0);
1983 WebcitAddUrlHandler(HKEY("post"), "", 0, post_message, PROHIBIT_STARTPAGE);
1984 WebcitAddUrlHandler(HKEY("move_msg"), "", 0, move_msg, PROHIBIT_STARTPAGE);
1985 WebcitAddUrlHandler(HKEY("delete_msg"), "", 0, delete_msg, PROHIBIT_STARTPAGE);
1986 WebcitAddUrlHandler(HKEY("msg"), "", 0, embed_message, NEED_URL);
1987 WebcitAddUrlHandler(HKEY("message"), "", 0, handle_one_message, NEED_URL|XHTTP_COMMANDS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
1988 WebcitAddUrlHandler(HKEY("printmsg"), "", 0, print_message, NEED_URL);
1989 WebcitAddUrlHandler(HKEY("msgheaders"), "", 0, display_headers, NEED_URL);
1991 WebcitAddUrlHandler(HKEY("mimepart"), "", 0, view_mimepart, NEED_URL);
1992 WebcitAddUrlHandler(HKEY("mimepart_download"), "", 0, download_mimepart, NEED_URL);
1993 WebcitAddUrlHandler(HKEY("postpart"), "", 0, view_postpart, NEED_URL|PROHIBIT_STARTPAGE);
1994 WebcitAddUrlHandler(HKEY("postpart_download"), "", 0, download_postpart, NEED_URL|PROHIBIT_STARTPAGE);
1995 WebcitAddUrlHandler(HKEY("upload_attachment"), "", 0, upload_attachment, AJAX);
1996 WebcitAddUrlHandler(HKEY("remove_attachment"), "", 0, remove_attachment, AJAX);
1997 WebcitAddUrlHandler(HKEY("show_num_attachments"), "", 0, show_num_attachments, AJAX);
2000 WebcitAddUrlHandler(HKEY("roommsgs"), "", 0, jsonMessageList,0);
2002 l_subj = FourHash("subj", 4);
2003 l_wefw = FourHash("wefw", 4);
2004 l_msgn = FourHash("msgn", 4);
2005 l_from = FourHash("from", 4);
2006 l_rcpt = FourHash("rcpt", 4);
2007 l_cccc = FourHash("cccc", 4);
2008 l_node = FourHash("node", 4);
2009 l_rfca = FourHash("rfca", 4);
2015 SessionDetachModule_MSG
2018 DeleteHash(&sess->summ);