2 * Functions which deal with the fetching and displaying of messages.
4 * Copyright (c) 1996-2011 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 as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 FlushStrBuf(WCC->ImportantMsg);
366 StrBufAppendBuf(WCC->ImportantMsg, CmdBuf, 4);
367 GetServerStatus(CmdBuf, &CitStatus);
368 HttpStatus(CitStatus);
373 if (GetHash(WCC->Hdr->HTTPHeaders, HKEY("DESTINATION"), &vLine) &&
375 Destination = (StrBuf*) vLine;
376 serv_printf("MOVE %ld|%s|%d", msgnum, ChrPtr(Destination), CopyMessage);
377 StrBuf_ServGetln(CmdBuf);
378 FlushStrBuf(WCC->ImportantMsg);
379 StrBufAppendBuf(WCC->ImportantMsg, CmdBuf, 4);
380 GetServerStatus(CmdBuf, &CitStatus);
381 HttpStatus(CitStatus);
394 * Unadorned HTML output of an individual message, suitable
395 * for placing in a hidden iframe, for printing, or whatever
397 void embed_message(void) {
402 StrBuf *CmdBuf = NULL;
404 msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
405 switch (WCC->Hdr->HR.eReqType)
409 Tmpl = sbstr("template");
410 if (StrLength(Tmpl) > 0)
411 read_message(WCC->WBuf, SKEY(Tmpl), msgnum, NULL, &Mime);
413 read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
414 http_transmit_thing(ChrPtr(Mime), 0);
417 CmdBuf = NewStrBuf ();
418 if ((WCC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
419 serv_printf("DELE %ld", msgnum);
421 else { /* Otherwise move it to Trash */
422 serv_printf("MOVE %ld|_TRASH_|0", msgnum);
424 StrBuf_ServGetln(CmdBuf);
425 FlushStrBuf(WCC->ImportantMsg);
426 StrBufAppendBuf(WCC->ImportantMsg, CmdBuf, 4);
436 * Printable view of a message
438 void print_message(void) {
442 msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
443 output_headers(0, 0, 0, 0, 0, 0);
445 hprintf("Content-type: text/html\r\n"
446 "Server: " PACKAGE_STRING "\r\n"
447 "Connection: close\r\n");
451 read_message(WC->WBuf, HKEY("view_message_print"), msgnum, NULL, &Mime);
457 * Display a message's headers
459 void display_headers(void) {
463 msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
464 output_headers(0, 0, 0, 0, 0, 0);
466 hprintf("Content-type: text/plain\r\n"
468 "Connection: close\r\n",
472 serv_printf("MSG2 %ld|3", msgnum);
473 serv_getln(buf, sizeof buf);
475 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
476 wc_printf("%s\n", buf);
484 message_summary *ReadOneMessageSummary(StrBuf *RawMessage, const char *DefaultSubject, long MsgNum)
487 MsgPartEvaluatorStruct *Eval;
488 message_summary *Msg;
497 serv_printf("MSG0 %ld|1", MsgNum); /* ask for headers only */
499 StrBuf_ServGetln(Buf);
500 if (GetServerStatus(Buf, NULL) == 1) {
505 Msg = (message_summary*)malloc(sizeof(message_summary));
506 memset(Msg, 0, sizeof(message_summary));
507 while (len = StrBuf_ServGetln(Buf),
510 strcmp(ChrPtr(Buf), "000")))
513 ebuf = strchr(ChrPtr(Buf), '=');
515 if (GetHash(MsgEvaluators, buf, nBuf, &vEval) && vEval != NULL) {
516 Eval = (MsgPartEvaluatorStruct*) vEval;
517 StrBufCutLeft(Buf, nBuf + 1);
520 else syslog(1, "Don't know how to handle Message Headerline [%s]", ChrPtr(Buf));
530 * load message pointers from the server for a "read messages" operation
532 * servcmd: the citadel command to send to the citserver
534 int load_msg_ptrs(const char *servcmd,
535 SharedMessageStatus *Stat,
536 load_msg_ptrs_detailheaders LH)
539 message_summary *Msg;
544 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 if (GetServerStatus(Buf, NULL) != 1) {
559 return (Stat->nummsgs);
562 while (len = StrBuf_ServGetln(Buf),
565 strcmp(ChrPtr(Buf), "000")!= 0)))
567 if (Stat->nummsgs < Stat->maxload) {
570 Msg = (message_summary*)malloc(sizeof(message_summary));
571 memset(Msg, 0, sizeof(message_summary));
573 Msg->msgnum = StrBufExtractNext_long(Buf, &Ptr, '|');
574 Msg->date = StrBufExtractNext_long(Buf, &Ptr, '|');
576 if (Stat->nummsgs == 0) {
577 if (Msg->msgnum < Stat->lowest_found) {
578 Stat->lowest_found = Msg->msgnum;
580 if (Msg->msgnum > Stat->highest_found) {
581 Stat->highest_found = Msg->msgnum;
585 if ((Msg->msgnum == 0) && (StrLength(Buf) < 32)) {
591 * as citserver probably gives us messages in forward date sorting
592 * nummsgs should be the same order as the message date.
594 if (Msg->date == 0) {
595 Msg->date = Stat->nummsgs;
596 if (StrLength(Buf) < 32)
599 if ((!skipit) && (LH != NULL)) {
600 if (!LH(Buf, &Ptr, Msg, Buf2)){
606 Put(WCC->summ, (const char *)&n, sizeof(n), Msg, DestroyMessageSummary);
612 return (Stat->nummsgs);
618 * \brief sets FlagToSet for each of ScanMe that appears in MatchMSet
619 * \param ScanMe List of BasicMsgStruct to be searched it MatchSet
620 * \param MatchMSet MSet we want to flag
621 * \param FlagToSet Flag to set on each BasicMsgStruct->Flags if in MSet
623 void SetFlagsFromMSet(HashList *ScanMe, MSet *MatchMSet, int FlagToSet, int Reverse)
629 message_summary *Msg;
631 at = GetNewHashPos(ScanMe, 0);
632 while (GetNextHashPos(ScanMe, at, &HKLen, &HashKey, &vMsg)) {
633 /* Are you a new message, or an old message? */
634 Msg = (message_summary*) vMsg;
635 if (Reverse && IsInMSetList(MatchMSet, Msg->msgnum)) {
636 Msg->Flags = Msg->Flags | FlagToSet;
638 else if (!Reverse && !IsInMSetList(MatchMSet, Msg->msgnum)) {
639 Msg->Flags = Msg->Flags | FlagToSet;
646 void load_seen_flags(void)
652 OldMsg = NewStrBuf();
654 StrBuf_ServGetln(OldMsg);
655 if (GetServerStatus(OldMsg, NULL) == 2) {
656 StrBufCutLeft(OldMsg, 4);
663 if (ParseMSet(&MatchMSet, OldMsg))
665 SetFlagsFromMSet(WCC->summ, MatchMSet, MSGFLAG_READ, 0);
667 DeleteMSet(&MatchMSet);
671 extern readloop_struct rlid[];
673 typedef struct _RoomRenderer{
676 GetParamsGetServerCall_func GetParamsGetServerCall;
677 PrintViewHeader_func PrintViewHeader;
678 LoadMsgFromServer_func LoadMsgFromServer;
679 RenderView_or_Tail_func RenderView_or_Tail;
680 View_Cleanup_func ViewCleanup;
681 load_msg_ptrs_detailheaders LHParse;
686 * command loop for reading messages
688 * Set oper to "readnew" or "readold" or "readfwd" or "headers" or "readgt" or "readlt" or "do_search"
690 void readloop(long oper, eCustomRoomRenderer ForceRenderer)
692 RoomRenderer *ViewMsg;
695 message_summary *Msg;
702 WCTemplputParams SubTP;
703 SharedMessageStatus Stat;
706 if (havebstr("is_summary") && (1 == (ibstr("is_summary")))) {
707 WCC->CurRoom.view = VIEW_MAILBOX;
710 if (havebstr("is_ajax") && (1 == (ibstr("is_ajax")))) {
714 if ((oper == do_search) && (WCC->CurRoom.view == VIEW_WIKI)) {
715 display_wiki_pagelist();
719 if (WCC->CurRoom.view == VIEW_WIKI) {
720 http_redirect("wiki?page=home");
724 memset(&Stat, 0, sizeof(SharedMessageStatus));
725 Stat.maxload = 10000;
726 Stat.lowest_found = (-1);
727 Stat.highest_found = (-1);
728 if (ForceRenderer == eUseDefault)
729 GetHash(ReadLoopHandler, IKEY(WCC->CurRoom.view), &vViewMsg);
731 GetHash(ReadLoopHandler, IKEY(ForceRenderer), &vViewMsg);
732 if (vViewMsg == NULL) {
733 WCC->CurRoom.view = VIEW_BBS;
734 GetHash(ReadLoopHandler, IKEY(WCC->CurRoom.view), &vViewMsg);
736 if (vViewMsg == NULL) {
737 return; /* TODO: print message */
740 ViewMsg = (RoomRenderer*) vViewMsg;
742 output_headers(1, 1, 1, 0, 0, 0);
743 } else if (WCC->CurRoom.view == VIEW_MAILBOX) {
744 jsonMessageListHdr();
747 if (ViewMsg->GetParamsGetServerCall != NULL) {
748 r = ViewMsg->GetParamsGetServerCall(
763 case 300: /* the callback hook should do the work for us here, since he knows what to do. */
769 if (!IsEmptyStr(cmd))
770 Stat.nummsgs = load_msg_ptrs(cmd, &Stat, ViewMsg->LHParse);
774 memset(&SubTP, 0, sizeof(WCTemplputParams));
775 SubTP.Filter.ContextType = CTX_MAILSUM;
776 SubTP.Context = NULL;
777 SortIt = RetrieveSort(&SubTP, NULL, 0,
778 HKEY("date"), Stat.defaultsortorder);
780 SortByPayload(WCC->summ, SortIt);
782 if (Stat.startmsg < 0) {
786 if (Stat.load_seen) load_seen_flags();
789 * Print any inforation above the message list...
791 if (ViewMsg->PrintViewHeader != NULL)
792 ViewMsg->PrintViewHeader(&Stat, &ViewSpecific);
794 WCC->startmsg = Stat.startmsg;
795 WCC->maxmsgs = Stat.maxmsgs;
796 WCC->num_displayed = 0;
798 /* Put some helpful data in vars for mailsummary_json */
803 StrBufPrintf(Foo, "%ld", Stat.nummsgs);
804 PutBstr(HKEY("__READLOOP:TOTALMSGS"), NewStrBufDup(Foo));
805 StrBufPrintf(Foo, "%ld", Stat.startmsg);
806 PutBstr(HKEY("__READLOOP:STARTMSG"), Foo);
810 * iterate over each message. if we need to load an attachment, do it here.
813 if ((ViewMsg->LoadMsgFromServer != NULL) &&
816 at = GetNewHashPos(WCC->summ, 0);
817 Stat.num_displayed = i = 0;
818 while ( GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
819 Msg = (message_summary*) vMsg;
820 if ((Msg->msgnum >= Stat.startmsg) && (Stat.num_displayed <= Stat.maxmsgs)) {
821 ViewMsg->LoadMsgFromServer(&Stat,
824 (Msg->Flags & MSGFLAG_READ) != 0,
833 * Done iterating the message list. now tasks we want to do after.
835 if (ViewMsg->RenderView_or_Tail != NULL)
836 ViewMsg->RenderView_or_Tail(&Stat, &ViewSpecific, oper);
838 if (ViewMsg->ViewCleanup != NULL)
839 ViewMsg->ViewCleanup(&ViewSpecific);
843 if (WCC->summ != NULL) {
844 DeleteHash(&WCC->summ);
850 * Back end for post_message()
851 * ... this is where the actual message gets transmitted to the server.
853 void post_mime_to_server(void) {
855 char top_boundary[SIZ];
856 char alt_boundary[SIZ];
857 int is_multipart = 0;
859 wc_mime_attachment *att;
861 size_t encoded_length;
862 size_t encoded_strlen;
863 char *txtmail = NULL;
864 int include_text_alt = 0; /* Set to nonzero to include multipart/alternative text/plain */
866 sprintf(top_boundary, "Citadel--Multipart--%s--%04x--%04x",
867 ChrPtr(WCC->serv_info->serv_fqdn),
871 sprintf(alt_boundary, "Citadel--Multipart--%s--%04x--%04x",
872 ChrPtr(WCC->serv_info->serv_fqdn),
877 /* RFC2045 requires this, and some clients look for it... */
878 serv_puts("MIME-Version: 1.0");
879 serv_puts("X-Mailer: " PACKAGE_STRING);
881 /* If there are attachments, we have to do multipart/mixed */
882 if (GetCount(WCC->attachments) > 0) {
886 /* Only do multipart/alternative for mailboxes. BBS and Wiki rooms don't need it. */
887 if (WC->CurRoom.view == VIEW_MAILBOX) {
888 include_text_alt = 1;
892 /* Remember, serv_printf() appends an extra newline */
893 serv_printf("Content-type: multipart/mixed; boundary=\"%s\"\n", top_boundary);
894 serv_printf("This is a multipart message in MIME format.\n");
895 serv_printf("--%s", top_boundary);
898 /* Remember, serv_printf() appends an extra newline */
899 if (include_text_alt) {
900 serv_printf("Content-type: multipart/alternative; "
901 "boundary=\"%s\"\n", alt_boundary);
902 serv_printf("This is a multipart message in MIME format.\n");
903 serv_printf("--%s", alt_boundary);
905 serv_puts("Content-type: text/plain; charset=utf-8");
906 serv_puts("Content-Transfer-Encoding: quoted-printable");
908 txtmail = html_to_ascii(bstr("msgtext"), 0, 80, 0);
909 text_to_server_qp(txtmail); /* Transmit message in quoted-printable encoding */
912 serv_printf("\n--%s", alt_boundary);
915 serv_puts("Content-type: text/html; charset=utf-8");
916 serv_puts("Content-Transfer-Encoding: quoted-printable");
918 serv_puts("<html><body>\r\n");
919 text_to_server_qp(bstr("msgtext")); /* Transmit message in quoted-printable encoding */
920 serv_puts("</body></html>\r\n");
922 if (include_text_alt) {
923 serv_printf("--%s--", alt_boundary);
932 /* Add in the attachments */
933 it = GetNewHashPos(WCC->attachments, 0);
934 while (GetNextHashPos(WCC->attachments, it, &len, &Key, &vAtt)) {
935 att = (wc_mime_attachment *)vAtt;
936 if (att->length == 0)
938 encoded_length = ((att->length * 150) / 100);
939 encoded = malloc(encoded_length);
940 if (encoded == NULL) break;
941 encoded_strlen = CtdlEncodeBase64(encoded, ChrPtr(att->Data), StrLength(att->Data), 1);
943 serv_printf("--%s", top_boundary);
944 serv_printf("Content-type: %s", ChrPtr(att->ContentType));
945 serv_printf("Content-disposition: attachment; filename=\"%s\"", ChrPtr(att->FileName));
946 serv_puts("Content-transfer-encoding: base64");
948 serv_write(encoded, encoded_strlen);
953 serv_printf("--%s--", top_boundary);
962 * Post message (or don't post message)
964 * Note regarding the "dont_post" variable:
965 * A random value (actually, it's just a timestamp) is inserted as a hidden
966 * field called "postseq" when the display_enter page is generated. This
967 * value is checked when posting, using the static variable dont_post. If a
968 * user attempts to post twice using the same dont_post value, the message is
969 * discarded. This prevents the accidental double-saving of the same message
970 * if the user happens to click the browser "back" button.
972 void post_message(void)
975 StrBuf *EmailAddress;
978 StrBuf *encoded_subject = NULL;
979 static long dont_post = (-1L);
980 int is_anonymous = 0;
981 const StrBuf *display_name = NULL;
985 if (havebstr("force_room")) {
986 gotoroom(sbstr("force_room"));
989 if (havebstr("display_name")) {
990 display_name = sbstr("display_name");
991 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
997 if (!strcasecmp(bstr("submit_action"), "cancel")) {
998 sprintf(WCC->ImportantMessage,
999 _("Cancelled. Message was not posted."));
1000 } else if (lbstr("postseq") == dont_post) {
1001 sprintf(WCC->ImportantMessage,
1002 _("Automatically cancelled because you have already "
1003 "saved this message."));
1005 const char CMD[] = "ENT0 1|%s|%d|4|%s|%s||%s|%s|%s|%s|%s";
1006 StrBuf *Recp = NULL;
1009 const StrBuf *Wikipage = NULL;
1010 const StrBuf *my_email_addr = NULL;
1011 StrBuf *CmdBuf = NULL;
1012 StrBuf *references = NULL;
1016 save_to_drafts = !strcasecmp(bstr("submit_action"), "drafts");
1019 if (save_to_drafts) {
1020 /* temporarily change to the drafts room */
1021 serv_puts("GOTO _DRAFTS_");
1022 StrBuf_ServGetln(Buf);
1023 if (GetServerStatus(Buf, NULL) != 2) {
1024 /* You probably don't even have a dumb Drafts folder */
1025 StrBufCutLeft(Buf, 4);
1026 syslog(9, "%s:%d: server save to drafts error: %s\n", __FILE__, __LINE__, ChrPtr(Buf));
1027 StrBufAppendBufPlain(WCC->ImportantMsg, _("Saved to Drafts failed: "), -1, 0);
1028 StrBufAppendBuf(WCC->ImportantMsg, Buf, 0);
1035 if (havebstr("references"))
1037 const StrBuf *ref = sbstr("references");
1038 references = NewStrBufDup(ref);
1039 if (*ChrPtr(references) == '|') { /* remove leading '|' if present */
1040 StrBufCutLeft(references, 1);
1042 StrBufReplaceChars(references, '|', '!');
1044 if (havebstr("subject")) {
1047 * make enough room for the encoded string;
1048 * plus the QP header
1050 Subj = sbstr("subject");
1052 StrBufRFC2047encode(&encoded_subject, Subj);
1054 UserName = NewStrBuf();
1055 EmailAddress = NewStrBuf();
1056 EncBuf = NewStrBuf();
1058 Recp = StrBufSanitizeEmailRecipientVector(sbstr("recp"), UserName, EmailAddress, EncBuf);
1059 Cc = StrBufSanitizeEmailRecipientVector(sbstr("cc"), UserName, EmailAddress, EncBuf);
1060 Bcc = StrBufSanitizeEmailRecipientVector(sbstr("bcc"), UserName, EmailAddress, EncBuf);
1062 FreeStrBuf(&UserName);
1063 FreeStrBuf(&EmailAddress);
1064 FreeStrBuf(&EncBuf);
1066 Wikipage = sbstr("page");
1067 my_email_addr = sbstr("my_email_addr");
1069 HeaderLen = StrLength(Recp) +
1070 StrLength(encoded_subject) +
1073 StrLength(Wikipage) +
1074 StrLength(my_email_addr) +
1075 StrLength(references);
1076 CmdBuf = NewStrBufPlain(NULL, sizeof (CMD) + HeaderLen);
1077 StrBufPrintf(CmdBuf,
1079 save_to_drafts?"":ChrPtr(Recp),
1081 ChrPtr(encoded_subject),
1082 ChrPtr(display_name),
1083 save_to_drafts?"":ChrPtr(Cc),
1084 save_to_drafts?"":ChrPtr(Bcc),
1086 ChrPtr(my_email_addr),
1087 ChrPtr(references));
1088 FreeStrBuf(&references);
1089 FreeStrBuf(&encoded_subject);
1091 if ((HeaderLen + StrLength(sbstr("msgtext")) < 10) &&
1092 (GetCount(WCC->attachments) == 0)){
1093 StrBufAppendBufPlain(WCC->ImportantMsg, _("Refusing to post empty message.\n"), -1, 0);
1094 FreeStrBuf(&CmdBuf);
1099 syslog(9, "%s\n", ChrPtr(CmdBuf));
1100 serv_puts(ChrPtr(CmdBuf));
1101 FreeStrBuf(&CmdBuf);
1103 StrBuf_ServGetln(Buf);
1104 if (GetServerStatus(Buf, NULL) == 4) {
1105 if (save_to_drafts) {
1106 if ( (havebstr("recp"))
1107 || (havebstr("cc" ))
1108 || (havebstr("bcc" )) ) {
1109 /* save recipient headers or room to post to */
1110 serv_printf("To: %s", ChrPtr(Recp));
1111 serv_printf("Cc: %s", ChrPtr(Cc));
1112 serv_printf("Bcc: %s", ChrPtr(Bcc));
1114 serv_printf("X-Citadel-Room: %s", ChrPtr(WC->CurRoom.name));
1117 post_mime_to_server();
1118 if (save_to_drafts) {
1119 StrBufAppendBufPlain(WCC->ImportantMsg, _("Message has been saved to Drafts.\n"), -1, 0);
1120 gotoroom(WCC->CurRoom.name);
1124 } else if ( (havebstr("recp"))
1125 || (havebstr("cc" ))
1126 || (havebstr("bcc" ))
1128 StrBufAppendBufPlain(WCC->ImportantMsg, _("Message has been sent.\n"), -1, 0);
1131 StrBufAppendBufPlain(WCC->ImportantMsg, _("Message has been posted.\n"), -1, 0);
1133 dont_post = lbstr("postseq");
1135 StrBufCutLeft(Buf, 4);
1137 syslog(9, "%s:%d: server post error: %s\n", __FILE__, __LINE__, ChrPtr(Buf));
1138 StrBufAppendBuf(WCC->ImportantMsg, Buf, 0);
1139 if (save_to_drafts) gotoroom(WCC->CurRoom.name);
1153 DeleteHash(&WCC->attachments);
1156 * We may have been supplied with instructions regarding the location
1157 * to which we must return after posting. If found, go there.
1159 if (havebstr("return_to")) {
1160 http_redirect(bstr("return_to"));
1163 * If we were editing a page in a wiki room, go to that page now.
1165 else if (havebstr("page")) {
1166 snprintf(buf, sizeof buf, "wiki?page=%s", bstr("page"));
1170 * Otherwise, just go to the "read messages" loop.
1173 readloop(readnew, eUseDefault);
1179 * Client is uploading an attachment
1181 void upload_attachment(void) {
1182 wcsession *WCC = WC;
1188 wc_mime_attachment *att;
1190 syslog(9, "upload_attachment()\n");
1191 wc_printf("upload_attachment()<br>\n");
1193 if (WCC->upload_length <= 0) {
1194 syslog(9, "ERROR no attachment was uploaded\n");
1195 wc_printf("ERROR no attachment was uploaded<br>\n");
1199 syslog(9, "Client is uploading %d bytes\n", WCC->upload_length);
1200 wc_printf("Client is uploading %d bytes<br>\n", WCC->upload_length);
1201 att = malloc(sizeof(wc_mime_attachment));
1202 memset(att, 0, sizeof(wc_mime_attachment ));
1203 att->length = WCC->upload_length;
1204 att->ContentType = NewStrBufPlain(WCC->upload_content_type, -1);
1205 att->FileName = NewStrBufDup(WCC->upload_filename);
1207 if (WCC->attachments == NULL) {
1208 WCC->attachments = NewHash(1, Flathash);
1211 /* And add it to the list. */
1213 if ((GetCount(WCC->attachments) > 0) &&
1214 GetHashAt(WCC->attachments,
1215 GetCount(WCC->attachments) -1,
1216 &newnlen, &newn, &v))
1217 n = *((int*) newn) + 1;
1218 Put(WCC->attachments, IKEY(n), att, DestroyMime);
1221 * Mozilla sends a simple filename, which is what we want,
1222 * but Satan's Browser sends an entire pathname. Reduce
1223 * the path to just a filename if we need to.
1225 pch = strrchr(ChrPtr(att->FileName), '/');
1227 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1229 pch = strrchr(ChrPtr(att->FileName), '\\');
1231 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1235 * Transfer control of this memory from the upload struct
1236 * to the attachment struct.
1238 att->Data = WCC->upload;
1240 WCC->upload_length = 0;
1245 * Remove an attachment from the message currently being composed.
1247 * Currently we identify the attachment to be removed by its filename.
1248 * There is probably a better way to do this.
1250 void remove_attachment(void) {
1251 wcsession *WCC = WC;
1252 wc_mime_attachment *att;
1254 StrBuf *WhichAttachment;
1259 WhichAttachment = NewStrBufDup(sbstr("which_attachment"));
1260 StrBufUnescape(WhichAttachment, 0);
1261 at = GetNewHashPos(WCC->attachments, 0);
1263 GetHashPos(WCC->attachments, at, &len, &key, &vAtt);
1265 att = (wc_mime_attachment*) vAtt;
1266 if ((att != NULL) &&
1267 (strcmp(ChrPtr(WhichAttachment),
1268 ChrPtr(att->FileName) ) == 0))
1270 DeleteEntryFromHash(WCC->attachments, at);
1274 while (NextHashPos(WCC->attachments, at));
1275 FreeStrBuf(&WhichAttachment);
1276 wc_printf("remove_attachment() completed\n");
1281 * display the message entry screen
1283 void display_enter(void)
1286 const StrBuf *display_name = NULL;
1287 int recipient_required = 0;
1288 int subject_required = 0;
1289 int recipient_bad = 0;
1290 int is_anonymous = 0;
1291 wcsession *WCC = WC;
1295 if (havebstr("force_room")) {
1296 gotoroom(sbstr("force_room"));
1299 display_name = sbstr("display_name");
1300 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1301 display_name = NULL;
1305 /* First test to see whether this is a room that requires recipients to be entered */
1306 serv_puts("ENT0 0");
1307 serv_getln(buf, sizeof buf);
1309 if (!strncmp(buf, "570", 3)) { /* 570 means that we need a recipient here */
1310 recipient_required = 1;
1312 else if (buf[0] != '2') { /* Any other error means that we cannot continue */
1313 sprintf(WCC->ImportantMessage, "%s", &buf[4]);
1314 readloop(readnew, eUseDefault);
1318 /* Is the server strongly recommending that the user enter a message subject? */
1319 if ((buf[3] != '\0') && (buf[4] != '\0')) {
1320 subject_required = extract_int(&buf[4], 1);
1324 * Are we perhaps in an address book view? If so, then an "enter
1325 * message" command really means "add new entry."
1327 if (WCC->CurRoom.defview == VIEW_ADDRESSBOOK) {
1328 do_edit_vcard(-1, "", NULL, NULL, "", ChrPtr(WCC->CurRoom.name));
1333 * Are we perhaps in a calendar room? If so, then an "enter
1334 * message" command really means "add new calendar item."
1336 if (WCC->CurRoom.defview == VIEW_CALENDAR) {
1337 display_edit_event();
1342 * Are we perhaps in a tasks view? If so, then an "enter
1343 * message" command really means "add new task."
1345 if (WCC->CurRoom.defview == VIEW_TASKS) {
1346 display_edit_task();
1352 * If the "replying_to" variable is set, it refers to a message
1353 * number from which we must extract some header fields...
1355 replying_to = lbstr("replying_to");
1356 if (replying_to > 0) {
1357 char wefw[1024] = "";
1358 char msgn[256] = "";
1359 char from[256] = "";
1360 char node[256] = "";
1361 char rfca[256] = "";
1362 char rcpt[SIZ] = "";
1363 char cccc[SIZ] = "";
1364 serv_printf("MSG0 %ld|1", replying_to);
1365 serv_getln(buf, sizeof buf);
1366 if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1368 if ( (!strncasecmp(buf, "subj=", 5)) && (strlen(buf) > 5) ) {
1369 StrBuf *subj = NewStrBuf();
1370 if (!strcasecmp(bstr("replying_mode"), "forward")) {
1371 if (strncasecmp(&buf[5], "Fw:", 3)) {
1372 StrBufAppendBufPlain(subj, HKEY("Fw: "), 0);
1376 if (strncasecmp(&buf[5], "Re:", 3)) {
1377 StrBufAppendBufPlain(subj, HKEY("Re: "), 0);
1380 StrBufAppendBufPlain(subj, &buf[5], -1, 0);
1381 PutBstr(HKEY("subject"), subj);
1384 else if (!strncasecmp(buf, "wefw=", 5)) {
1387 safestrncpy(wefw, &buf[5], sizeof wefw);
1389 /* Trim down excessively long lists of thread references. We eliminate the
1390 * second one in the list so that the thread root remains intact.
1392 rrtok = num_tokens(wefw, '|');
1393 rrlen = strlen(wefw);
1394 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
1395 remove_token(wefw, 1, '|');
1399 else if (!strncasecmp(buf, "msgn=", 5)) {
1400 safestrncpy(msgn, &buf[5], sizeof msgn);
1403 else if (!strncasecmp(buf, "from=", 5)) {
1404 safestrncpy(from, &buf[5], sizeof from);
1405 for (i=0; i<strlen(from); ++i) {
1406 if (from[i] == ',') from[i] = ' ';
1410 else if (!strncasecmp(buf, "rcpt=", 5)) {
1411 safestrncpy(rcpt, &buf[5], sizeof rcpt);
1414 else if (!strncasecmp(buf, "cccc=", 5)) {
1415 safestrncpy(cccc, &buf[5], sizeof cccc);
1418 else if (!strncasecmp(buf, "node=", 5)) {
1419 safestrncpy(node, &buf[5], sizeof node);
1422 else if (!strncasecmp(buf, "rfca=", 5)) {
1423 safestrncpy(rfca, &buf[5], sizeof rfca);
1428 if (strlen(wefw) + strlen(msgn) > 0) {
1429 StrBuf *refs = NewStrBuf();
1430 if (!IsEmptyStr(wefw)) {
1431 StrBufAppendBufPlain(refs, wefw, -1, 0);
1433 if ( (!IsEmptyStr(wefw)) && (!IsEmptyStr(msgn)) ) {
1434 StrBufAppendBufPlain(refs, HKEY("|"), 0);
1436 if (!IsEmptyStr(msgn)) {
1437 StrBufAppendBufPlain(refs, msgn, -1, 0);
1439 PutBstr(HKEY("references"), refs);
1443 * If this is a Reply or a ReplyAll, copy the sender's email into the To: field
1445 if ( (!strcasecmp(bstr("replying_mode"), "reply"))
1446 || (!strcasecmp(bstr("replying_mode"), "replyall"))
1448 StrBuf *to_rcpt = NewStrBuf();
1449 if (!IsEmptyStr(rfca)) {
1450 StrBufAppendPrintf(to_rcpt, "%s <%s>", from, rfca);
1453 StrBufAppendPrintf(to_rcpt, "%s", from);
1454 if ( (!IsEmptyStr(node))
1455 && (strcasecmp(node, ChrPtr(WC->serv_info->serv_nodename)))
1457 StrBufAppendPrintf(to_rcpt, " @ %s", node);
1460 PutBstr(HKEY("recp"), to_rcpt);
1464 * Only if this is a ReplyAll, copy all recipients into the Cc: field
1466 if ( (!strcasecmp(bstr("replying_mode"), "replyall"))
1468 StrBuf *cc_rcpt = NewStrBuf();
1469 if (!IsEmptyStr(rcpt)) {
1470 StrBufAppendPrintf(cc_rcpt, "%s", rcpt);
1472 if ( (!IsEmptyStr(rcpt)) && (!IsEmptyStr(cccc)) ) {
1473 StrBufAppendPrintf(cc_rcpt, ", ");
1475 if (!IsEmptyStr(cccc)) {
1476 StrBufAppendPrintf(cc_rcpt, "%s", cccc);
1478 PutBstr(HKEY("cc"), cc_rcpt);
1484 * Otherwise proceed normally.
1485 * Do a custom room banner with no navbar...
1488 if (recipient_required) {
1489 const StrBuf *Recp = NULL;
1490 const StrBuf *Cc = NULL;
1491 const StrBuf *Bcc = NULL;
1492 const StrBuf *Wikipage = NULL;
1493 StrBuf *CmdBuf = NULL;
1494 const char CMD[] = "ENT0 0|%s|%d|0||%s||%s|%s|%s";
1496 Recp = sbstr("recp");
1499 Wikipage = sbstr("page");
1501 CmdBuf = NewStrBufPlain(NULL,
1504 StrLength(display_name) +
1507 StrLength(Wikipage));
1509 StrBufPrintf(CmdBuf,
1513 ChrPtr(display_name),
1517 serv_puts(ChrPtr(CmdBuf));
1518 serv_getln(buf, sizeof buf);
1519 FreeStrBuf(&CmdBuf);
1521 if (!strncmp(buf, "570", 3)) { /* 570 means we have an invalid recipient listed */
1522 if (havebstr("recp") &&
1525 recipient_bad = 1; /* TODO: and now????? */
1528 else if (buf[0] != '2') { /* Any other error means that we cannot continue */
1529 wc_printf("<em>%s</em><br>\n", &buf[4]); /* TODO -> important message */
1533 if (recipient_required)
1534 PutBstr(HKEY("__RCPTREQUIRED"), NewStrBufPlain(HKEY("1")));
1535 if (recipient_required || subject_required)
1536 PutBstr(HKEY("__SUBJREQUIRED"), NewStrBufPlain(HKEY("1")));
1539 output_headers(1, 0, 0, 0, 1, 0);
1540 DoTemplate(HKEY("edit_message"), NULL, &NoCtx);
1549 void delete_msg(void)
1554 msgid = lbstr("msgid");
1556 if ((WC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
1557 serv_printf("DELE %ld", msgid);
1559 else { /* Otherwise move it to Trash */
1560 serv_printf("MOVE %ld|_TRASH_|0", msgid);
1563 serv_getln(buf, sizeof buf);
1564 sprintf(WC->ImportantMessage, "%s", &buf[4]);
1565 readloop(readnew, eUseDefault);
1570 * move a message to another room
1577 msgid = lbstr("msgid");
1579 if (havebstr("move_button")) {
1580 sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
1582 serv_getln(buf, sizeof buf);
1583 sprintf(WC->ImportantMessage, "%s", &buf[4]);
1585 sprintf(WC->ImportantMessage, (_("The message was not moved.")));
1588 readloop(readnew, eUseDefault);
1593 * Confirm move of a message
1595 void confirm_move_msg(void)
1600 output_headers(1, 1, 2, 0, 0, 0);
1601 wc_printf("<div id=\"banner\">\n");
1603 wc_printf(_("Confirm move of message"));
1605 wc_printf("</div>\n");
1607 wc_printf("<div id=\"content\" class=\"service\">\n");
1609 wc_printf("<CENTER>");
1611 wc_printf(_("Move this message to:"));
1612 wc_printf("<br>\n");
1614 wc_printf("<form METHOD=\"POST\" action=\"move_msg\">\n");
1615 wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
1616 wc_printf("<INPUT TYPE=\"hidden\" NAME=\"msgid\" VALUE=\"%s\">\n", bstr("msgid"));
1618 wc_printf("<SELECT NAME=\"target_room\" SIZE=5>\n");
1620 serv_getln(buf, sizeof buf);
1621 if (buf[0] == '1') {
1622 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1623 extract_token(targ, buf, 0, '|', sizeof targ);
1624 wc_printf("<OPTION>");
1629 wc_printf("</SELECT>\n");
1630 wc_printf("<br>\n");
1632 wc_printf("<INPUT TYPE=\"submit\" NAME=\"move_button\" VALUE=\"%s\">", _("Move"));
1633 wc_printf(" ");
1634 wc_printf("<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">", _("Cancel"));
1635 wc_printf("</form></CENTER>\n");
1637 wc_printf("</CENTER>\n");
1643 * Generic function to output an arbitrary MIME attachment from
1644 * message being composed
1646 * partnum The MIME part to be output
1647 * filename Fake filename to give
1648 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1650 void postpart(StrBuf *partnum, StrBuf *filename, int force_download)
1653 StrBuf *content_type;
1654 wc_mime_attachment *part;
1657 i = StrToi(partnum);
1658 if (GetHash(WC->attachments, IKEY(i), &vPart) &&
1660 part = (wc_mime_attachment*) vPart;
1661 if (force_download) {
1662 content_type = NewStrBufPlain(HKEY("application/octet-stream"));
1665 content_type = NewStrBufDup(part->ContentType);
1667 StrBufAppendBuf(WC->WBuf, part->Data, 0);
1668 http_transmit_thing(ChrPtr(content_type), 0);
1670 hprintf("HTTP/1.1 404 %s\n", ChrPtr(partnum));
1671 output_headers(0, 0, 0, 0, 0, 0);
1672 hprintf("Content-Type: text/plain\r\n");
1674 wc_printf(_("An error occurred while retrieving this part: %s/%s\n"),
1675 ChrPtr(partnum), ChrPtr(filename));
1678 FreeStrBuf(&content_type);
1683 * Generic function to output an arbitrary MIME part from an arbitrary
1684 * message number on the server.
1686 * msgnum Number of the item on the citadel server
1687 * partnum The MIME part to be output
1688 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1690 void mimepart(int force_download)
1694 wcsession *WCC = WC;
1697 StrBuf *ContentType = NewStrBufPlain(HKEY("application/octet-stream"));
1700 att = Buf = NewStrBuf();
1701 msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
1702 StrBufExtract_token(att, WCC->Hdr->HR.ReqLine, 1, '/');
1704 serv_printf("OPNA %ld|%s", msgnum, ChrPtr(att));
1705 StrBuf_ServGetln(Buf);
1706 if (GetServerStatus(Buf, NULL) == 2) {
1707 StrBufCutLeft(Buf, 4);
1708 bytes = StrBufExtract_long(Buf, 0, '|');
1709 if (!force_download) {
1710 StrBufExtract_token(ContentType, Buf, 3, '|');
1713 serv_read_binary(WCC->WBuf, bytes, Buf);
1715 StrBuf_ServGetln(Buf);
1716 CT = ChrPtr(ContentType);
1718 if (!force_download) {
1719 if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
1720 StrBufExtract_token(Buf, WCC->Hdr->HR.ReqLine, 2, '/');
1721 CT = GuessMimeByFilename(SKEY(Buf));
1723 if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
1724 CT = GuessMimeType(SKEY(WCC->WBuf));
1727 http_transmit_thing(CT, 0);
1729 StrBufCutLeft(Buf, 4);
1730 hprintf("HTTP/1.1 404 %s\n", ChrPtr(Buf));
1731 output_headers(0, 0, 0, 0, 0, 0);
1732 hprintf("Content-Type: text/plain\r\n");
1734 wc_printf(_("An error occurred while retrieving this part: %s\n"),
1738 FreeStrBuf(&ContentType);
1744 * Read any MIME part of a message, from the server, into memory.
1746 StrBuf *load_mimepart(long msgnum, char *partnum)
1752 serv_printf("DLAT %ld|%s", msgnum, partnum);
1753 StrBuf_ServGetln(Buf);
1754 if (GetServerStatus(Buf, NULL) == 6) {
1755 StrBufCutLeft(Buf, 4);
1756 bytes = StrBufExtract_long(Buf, 0, '|');
1759 StrBuf_ServGetBLOBBuffered(Buf, bytes);
1769 * Read any MIME part of a message, from the server, into memory.
1771 void MimeLoadData(wc_mime_attachment *Mime)
1776 /* TODO: is there a chance the content type is different from the one we know? */
1778 serv_printf("DLAT %ld|%s", Mime->msgnum, ChrPtr(Mime->PartNum));
1780 StrBuf_ServGetln(Buf);
1781 if (GetServerStatus(Buf, NULL) == 6) {
1782 Ptr = &(ChrPtr(Buf)[4]);
1783 bytes = StrBufExtractNext_long(Buf, &Ptr, '|');
1784 StrBufSkip_NTokenS(Buf, &Ptr, '|', 3); /* filename, cbtype, mimetype */
1785 if (Mime->Charset == NULL) Mime->Charset = NewStrBuf();
1786 StrBufExtract_NextToken(Mime->Charset, Buf, &Ptr, '|');
1788 if (Mime->Data == NULL)
1789 Mime->Data = NewStrBufPlain(NULL, bytes);
1790 StrBuf_ServGetBLOBBuffered(Mime->Data, bytes);
1793 FlushStrBuf(Mime->Data);
1794 /* TODO XImportant message */
1800 void view_mimepart(void) {
1804 void download_mimepart(void) {
1808 void view_postpart(void) {
1809 StrBuf *filename = NewStrBuf();
1810 StrBuf *partnum = NewStrBuf();
1812 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1813 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1815 postpart(partnum, filename, 0);
1817 FreeStrBuf(&filename);
1818 FreeStrBuf(&partnum);
1821 void download_postpart(void) {
1822 StrBuf *filename = NewStrBuf();
1823 StrBuf *partnum = NewStrBuf();
1825 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1826 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1828 postpart(partnum, filename, 1);
1830 FreeStrBuf(&filename);
1831 FreeStrBuf(&partnum);
1836 void show_num_attachments(void) {
1837 wc_printf("%d", GetCount(WC->attachments));
1841 void h_readnew(void) { readloop(readnew, eUseDefault);}
1842 void h_readold(void) { readloop(readold, eUseDefault);}
1843 void h_readfwd(void) { readloop(readfwd, eUseDefault);}
1844 void h_headers(void) { readloop(headers, eUseDefault);}
1845 void h_do_search(void) { readloop(do_search, eUseDefault);}
1846 void h_readgt(void) { readloop(readgt, eUseDefault);}
1847 void h_readlt(void) { readloop(readlt, eUseDefault);}
1849 void jsonMessageListHdr(void)
1851 /* TODO: make a generic function */
1852 hprintf("HTTP/1.1 200 OK\r\n");
1853 hprintf("Content-type: application/json; charset=utf-8\r\n");
1854 hprintf("Server: %s / %s\r\n", PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software));
1855 hprintf("Connection: close\r\n");
1856 hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
1861 /* Output message list in JSON format */
1862 void jsonMessageList(void) {
1863 const StrBuf *room = sbstr("room");
1864 long oper = (havebstr("query")) ? do_search : readnew;
1867 readloop(oper, eUseDefault);
1871 void RegisterReadLoopHandlerset(
1873 GetParamsGetServerCall_func GetParamsGetServerCall,
1874 PrintViewHeader_func PrintViewHeader,
1875 load_msg_ptrs_detailheaders LH,
1876 LoadMsgFromServer_func LoadMsgFromServer,
1877 RenderView_or_Tail_func RenderView_or_Tail,
1878 View_Cleanup_func ViewCleanup
1881 RoomRenderer *Handler;
1883 Handler = (RoomRenderer*) malloc(sizeof(RoomRenderer));
1885 Handler->RoomType = RoomType;
1886 Handler->GetParamsGetServerCall = GetParamsGetServerCall;
1887 Handler->PrintViewHeader = PrintViewHeader;
1888 Handler->LoadMsgFromServer = LoadMsgFromServer;
1889 Handler->RenderView_or_Tail = RenderView_or_Tail;
1890 Handler->ViewCleanup = ViewCleanup;
1891 Handler->LHParse = LH;
1893 Put(ReadLoopHandler, IKEY(RoomType), Handler, NULL);
1900 RegisterPreference("use_sig",
1901 _("Attach signature to email messages?"),
1904 RegisterPreference("signature", _("Use this signature:"), PRF_QP_STRING, NULL);
1905 RegisterPreference("default_header_charset",
1906 _("Default character set for email headers:"),
1909 RegisterPreference("defaultfrom", _("Preferred email address"), PRF_STRING, NULL);
1910 RegisterPreference("defaultname",
1911 _("Preferred display name for email messages"),
1914 RegisterPreference("defaulthandle",
1915 _("Preferred display name for bulletin board posts"),
1918 RegisterPreference("mailbox",_("Mailbox view mode"), PRF_STRING, NULL);
1920 WebcitAddUrlHandler(HKEY("readnew"), "", 0, h_readnew, ANONYMOUS|NEED_URL);
1921 WebcitAddUrlHandler(HKEY("readold"), "", 0, h_readold, ANONYMOUS|NEED_URL);
1922 WebcitAddUrlHandler(HKEY("readfwd"), "", 0, h_readfwd, ANONYMOUS|NEED_URL);
1923 WebcitAddUrlHandler(HKEY("headers"), "", 0, h_headers, NEED_URL);
1924 WebcitAddUrlHandler(HKEY("readgt"), "", 0, h_readgt, ANONYMOUS|NEED_URL);
1925 WebcitAddUrlHandler(HKEY("readlt"), "", 0, h_readlt, ANONYMOUS|NEED_URL);
1926 WebcitAddUrlHandler(HKEY("do_search"), "", 0, h_do_search, 0);
1927 WebcitAddUrlHandler(HKEY("display_enter"), "", 0, display_enter, 0);
1928 WebcitAddUrlHandler(HKEY("post"), "", 0, post_message, PROHIBIT_STARTPAGE);
1929 WebcitAddUrlHandler(HKEY("move_msg"), "", 0, move_msg, PROHIBIT_STARTPAGE);
1930 WebcitAddUrlHandler(HKEY("delete_msg"), "", 0, delete_msg, PROHIBIT_STARTPAGE);
1931 WebcitAddUrlHandler(HKEY("confirm_move_msg"), "", 0, confirm_move_msg, PROHIBIT_STARTPAGE);
1932 WebcitAddUrlHandler(HKEY("msg"), "", 0, embed_message, NEED_URL);
1933 WebcitAddUrlHandler(HKEY("message"), "", 0, handle_one_message, NEED_URL|XHTTP_COMMANDS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
1934 WebcitAddUrlHandler(HKEY("printmsg"), "", 0, print_message, NEED_URL);
1935 WebcitAddUrlHandler(HKEY("msgheaders"), "", 0, display_headers, NEED_URL);
1937 WebcitAddUrlHandler(HKEY("mimepart"), "", 0, view_mimepart, NEED_URL);
1938 WebcitAddUrlHandler(HKEY("mimepart_download"), "", 0, download_mimepart, NEED_URL);
1939 WebcitAddUrlHandler(HKEY("postpart"), "", 0, view_postpart, NEED_URL|PROHIBIT_STARTPAGE);
1940 WebcitAddUrlHandler(HKEY("postpart_download"), "", 0, download_postpart, NEED_URL|PROHIBIT_STARTPAGE);
1941 WebcitAddUrlHandler(HKEY("upload_attachment"), "", 0, upload_attachment, AJAX);
1942 WebcitAddUrlHandler(HKEY("remove_attachment"), "", 0, remove_attachment, AJAX);
1943 WebcitAddUrlHandler(HKEY("show_num_attachments"), "", 0, show_num_attachments, AJAX);
1946 WebcitAddUrlHandler(HKEY("roommsgs"), "", 0, jsonMessageList,0);
1951 SessionDetachModule_MSG
1954 DeleteHash(&sess->summ);