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 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 switch (WCC->Hdr->HR.eReqType)
406 Tmpl = sbstr("template");
407 if (StrLength(Tmpl) > 0)
408 read_message(WCC->WBuf, SKEY(Tmpl), msgnum, NULL, &Mime);
410 read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
411 http_transmit_thing(ChrPtr(Mime), 0);
414 CmdBuf = NewStrBuf ();
415 if ((WCC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
416 serv_printf("DELE %ld", msgnum);
418 else { /* Otherwise move it to Trash */
419 serv_printf("MOVE %ld|_TRASH_|0", msgnum);
421 StrBuf_ServGetln(CmdBuf);
422 GetServerStatusMsg(CmdBuf, NULL, 1, 0);
432 * Printable view of a message
434 void print_message(void) {
438 msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
439 output_headers(0, 0, 0, 0, 0, 0);
441 hprintf("Content-type: text/html\r\n"
442 "Server: " PACKAGE_STRING "\r\n"
443 "Connection: close\r\n");
447 read_message(WC->WBuf, HKEY("view_message_print"), msgnum, NULL, &Mime);
453 * Display a message's headers
455 void display_headers(void) {
459 msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
460 output_headers(0, 0, 0, 0, 0, 0);
462 hprintf("Content-type: text/plain\r\n"
464 "Connection: close\r\n",
468 serv_printf("MSG2 %ld|3", msgnum);
469 serv_getln(buf, sizeof buf);
471 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
472 wc_printf("%s\n", buf);
480 message_summary *ReadOneMessageSummary(StrBuf *RawMessage, const char *DefaultSubject, long MsgNum)
483 MsgPartEvaluatorStruct *Eval;
484 message_summary *Msg;
493 serv_printf("MSG0 %ld|1", MsgNum); /* ask for headers only */
495 StrBuf_ServGetln(Buf);
496 if (GetServerStatus(Buf, NULL) == 1) {
501 Msg = (message_summary*)malloc(sizeof(message_summary));
502 memset(Msg, 0, sizeof(message_summary));
503 while (len = StrBuf_ServGetln(Buf),
506 strcmp(ChrPtr(Buf), "000")))
509 ebuf = strchr(ChrPtr(Buf), '=');
511 if (GetHash(MsgEvaluators, buf, nBuf, &vEval) && vEval != NULL) {
512 Eval = (MsgPartEvaluatorStruct*) vEval;
513 StrBufCutLeft(Buf, nBuf + 1);
516 else syslog(1, "Don't know how to handle Message Headerline [%s]", ChrPtr(Buf));
526 * load message pointers from the server for a "read messages" operation
528 * servcmd: the citadel command to send to the citserver
530 int load_msg_ptrs(const char *servcmd,
531 SharedMessageStatus *Stat,
532 load_msg_ptrs_detailheaders LH)
535 message_summary *Msg;
540 const char *Ptr = NULL;
542 Stat->lowest_found = LONG_MAX;
543 Stat->highest_found = LONG_MIN;
545 if (WCC->summ != NULL) {
546 DeleteHash(&WCC->summ);
548 WCC->summ = NewHash(1, Flathash);
552 StrBuf_ServGetln(Buf);
553 if (GetServerStatus(Buf, NULL) != 1) {
555 return (Stat->nummsgs);
558 while (len = StrBuf_ServGetln(Buf),
561 strcmp(ChrPtr(Buf), "000")!= 0)))
563 if (Stat->nummsgs < Stat->maxload) {
566 Msg = (message_summary*)malloc(sizeof(message_summary));
567 memset(Msg, 0, sizeof(message_summary));
569 Msg->msgnum = StrBufExtractNext_long(Buf, &Ptr, '|');
570 Msg->date = StrBufExtractNext_long(Buf, &Ptr, '|');
572 if (Stat->nummsgs == 0) {
573 if (Msg->msgnum < Stat->lowest_found) {
574 Stat->lowest_found = Msg->msgnum;
576 if (Msg->msgnum > Stat->highest_found) {
577 Stat->highest_found = Msg->msgnum;
581 if ((Msg->msgnum == 0) && (StrLength(Buf) < 32)) {
587 * as citserver probably gives us messages in forward date sorting
588 * nummsgs should be the same order as the message date.
590 if (Msg->date == 0) {
591 Msg->date = Stat->nummsgs;
592 if (StrLength(Buf) < 32)
595 if ((!skipit) && (LH != NULL)) {
596 if (!LH(Buf, &Ptr, Msg, Buf2)){
602 Put(WCC->summ, (const char *)&n, sizeof(n), Msg, DestroyMessageSummary);
608 return (Stat->nummsgs);
614 * \brief sets FlagToSet for each of ScanMe that appears in MatchMSet
615 * \param ScanMe List of BasicMsgStruct to be searched it MatchSet
616 * \param MatchMSet MSet we want to flag
617 * \param FlagToSet Flag to set on each BasicMsgStruct->Flags if in MSet
619 void SetFlagsFromMSet(HashList *ScanMe, MSet *MatchMSet, int FlagToSet, int Reverse)
625 message_summary *Msg;
627 at = GetNewHashPos(ScanMe, 0);
628 while (GetNextHashPos(ScanMe, at, &HKLen, &HashKey, &vMsg)) {
629 /* Are you a new message, or an old message? */
630 Msg = (message_summary*) vMsg;
631 if (Reverse && IsInMSetList(MatchMSet, Msg->msgnum)) {
632 Msg->Flags = Msg->Flags | FlagToSet;
634 else if (!Reverse && !IsInMSetList(MatchMSet, Msg->msgnum)) {
635 Msg->Flags = Msg->Flags | FlagToSet;
642 void load_seen_flags(void)
648 OldMsg = NewStrBuf();
650 StrBuf_ServGetln(OldMsg);
651 if (GetServerStatus(OldMsg, NULL) == 2) {
652 StrBufCutLeft(OldMsg, 4);
659 if (ParseMSet(&MatchMSet, OldMsg))
661 SetFlagsFromMSet(WCC->summ, MatchMSet, MSGFLAG_READ, 0);
663 DeleteMSet(&MatchMSet);
667 extern readloop_struct rlid[];
669 typedef struct _RoomRenderer{
672 GetParamsGetServerCall_func GetParamsGetServerCall;
673 PrintViewHeader_func PrintViewHeader;
674 LoadMsgFromServer_func LoadMsgFromServer;
675 RenderView_or_Tail_func RenderView_or_Tail;
676 View_Cleanup_func ViewCleanup;
677 load_msg_ptrs_detailheaders LHParse;
682 * command loop for reading messages
684 * Set oper to "readnew" or "readold" or "readfwd" or "headers" or "readgt" or "readlt" or "do_search"
686 void readloop(long oper, eCustomRoomRenderer ForceRenderer)
688 RoomRenderer *ViewMsg;
691 message_summary *Msg;
698 WCTemplputParams SubTP;
699 SharedMessageStatus Stat;
702 if (havebstr("is_summary") && (1 == (ibstr("is_summary")))) {
703 WCC->CurRoom.view = VIEW_MAILBOX;
706 if (havebstr("is_ajax") && (1 == (ibstr("is_ajax")))) {
710 if ((oper == do_search) && (WCC->CurRoom.view == VIEW_WIKI)) {
711 display_wiki_pagelist();
715 if (WCC->CurRoom.view == VIEW_WIKI) {
716 http_redirect("wiki?page=home");
720 memset(&Stat, 0, sizeof(SharedMessageStatus));
721 Stat.maxload = 10000;
722 Stat.lowest_found = (-1);
723 Stat.highest_found = (-1);
724 if (ForceRenderer == eUseDefault)
725 GetHash(ReadLoopHandler, IKEY(WCC->CurRoom.view), &vViewMsg);
727 GetHash(ReadLoopHandler, IKEY(ForceRenderer), &vViewMsg);
728 if (vViewMsg == NULL) {
729 WCC->CurRoom.view = VIEW_BBS;
730 GetHash(ReadLoopHandler, IKEY(WCC->CurRoom.view), &vViewMsg);
732 if (vViewMsg == NULL) {
733 return; /* TODO: print message */
736 ViewMsg = (RoomRenderer*) vViewMsg;
738 output_headers(1, 1, 1, 0, 0, 0);
739 } else if (WCC->CurRoom.view == VIEW_MAILBOX) {
740 jsonMessageListHdr();
743 if (ViewMsg->GetParamsGetServerCall != NULL) {
744 r = ViewMsg->GetParamsGetServerCall(
759 case 300: /* the callback hook should do the work for us here, since he knows what to do. */
765 if (!IsEmptyStr(cmd))
766 Stat.nummsgs = load_msg_ptrs(cmd, &Stat, ViewMsg->LHParse);
770 memset(&SubTP, 0, sizeof(WCTemplputParams));
771 SubTP.Filter.ContextType = CTX_MAILSUM;
772 SubTP.Context = NULL;
773 SortIt = RetrieveSort(&SubTP, NULL, 0,
774 HKEY("date"), Stat.defaultsortorder);
776 SortByPayload(WCC->summ, SortIt);
778 if (Stat.startmsg < 0) {
782 if (Stat.load_seen) load_seen_flags();
785 * Print any inforation above the message list...
787 if (ViewMsg->PrintViewHeader != NULL)
788 ViewMsg->PrintViewHeader(&Stat, &ViewSpecific);
790 WCC->startmsg = Stat.startmsg;
791 WCC->maxmsgs = Stat.maxmsgs;
792 WCC->num_displayed = 0;
794 /* Put some helpful data in vars for mailsummary_json */
799 StrBufPrintf(Foo, "%ld", Stat.nummsgs);
800 PutBstr(HKEY("__READLOOP:TOTALMSGS"), NewStrBufDup(Foo));
801 StrBufPrintf(Foo, "%ld", Stat.startmsg);
802 PutBstr(HKEY("__READLOOP:STARTMSG"), Foo);
806 * iterate over each message. if we need to load an attachment, do it here.
809 if ((ViewMsg->LoadMsgFromServer != NULL) &&
812 at = GetNewHashPos(WCC->summ, 0);
813 Stat.num_displayed = i = 0;
814 while ( GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
815 Msg = (message_summary*) vMsg;
816 if ((Msg->msgnum >= Stat.startmsg) && (Stat.num_displayed <= Stat.maxmsgs)) {
817 ViewMsg->LoadMsgFromServer(&Stat,
820 (Msg->Flags & MSGFLAG_READ) != 0,
829 * Done iterating the message list. now tasks we want to do after.
831 if (ViewMsg->RenderView_or_Tail != NULL)
832 ViewMsg->RenderView_or_Tail(&Stat, &ViewSpecific, oper);
834 if (ViewMsg->ViewCleanup != NULL)
835 ViewMsg->ViewCleanup(&ViewSpecific);
839 if (WCC->summ != NULL) {
840 DeleteHash(&WCC->summ);
846 * Back end for post_message()
847 * ... this is where the actual message gets transmitted to the server.
849 void post_mime_to_server(void) {
851 char top_boundary[SIZ];
852 char alt_boundary[SIZ];
853 int is_multipart = 0;
855 wc_mime_attachment *att;
857 size_t encoded_length;
858 size_t encoded_strlen;
859 char *txtmail = NULL;
860 int include_text_alt = 0; /* Set to nonzero to include multipart/alternative text/plain */
862 sprintf(top_boundary, "Citadel--Multipart--%s--%04x--%04x",
863 ChrPtr(WCC->serv_info->serv_fqdn),
867 sprintf(alt_boundary, "Citadel--Multipart--%s--%04x--%04x",
868 ChrPtr(WCC->serv_info->serv_fqdn),
873 /* RFC2045 requires this, and some clients look for it... */
874 serv_puts("MIME-Version: 1.0");
875 serv_puts("X-Mailer: " PACKAGE_STRING);
877 /* If there are attachments, we have to do multipart/mixed */
878 if (GetCount(WCC->attachments) > 0) {
882 /* Only do multipart/alternative for mailboxes. BBS and Wiki rooms don't need it. */
883 if (WC->CurRoom.view == VIEW_MAILBOX) {
884 include_text_alt = 1;
888 /* Remember, serv_printf() appends an extra newline */
889 serv_printf("Content-type: multipart/mixed; boundary=\"%s\"\n", top_boundary);
890 serv_printf("This is a multipart message in MIME format.\n");
891 serv_printf("--%s", top_boundary);
894 /* Remember, serv_printf() appends an extra newline */
895 if (include_text_alt) {
896 serv_printf("Content-type: multipart/alternative; "
897 "boundary=\"%s\"\n", alt_boundary);
898 serv_printf("This is a multipart message in MIME format.\n");
899 serv_printf("--%s", alt_boundary);
901 serv_puts("Content-type: text/plain; charset=utf-8");
902 serv_puts("Content-Transfer-Encoding: quoted-printable");
904 txtmail = html_to_ascii(bstr("msgtext"), 0, 80, 0);
905 text_to_server_qp(txtmail); /* Transmit message in quoted-printable encoding */
908 serv_printf("\n--%s", alt_boundary);
911 serv_puts("Content-type: text/html; charset=utf-8");
912 serv_puts("Content-Transfer-Encoding: quoted-printable");
914 serv_puts("<html><body>\r\n");
915 text_to_server_qp(bstr("msgtext")); /* Transmit message in quoted-printable encoding */
916 serv_puts("</body></html>\r\n");
918 if (include_text_alt) {
919 serv_printf("--%s--", alt_boundary);
928 /* Add in the attachments */
929 it = GetNewHashPos(WCC->attachments, 0);
930 while (GetNextHashPos(WCC->attachments, it, &len, &Key, &vAtt)) {
931 att = (wc_mime_attachment *)vAtt;
932 if (att->length == 0)
934 encoded_length = ((att->length * 150) / 100);
935 encoded = malloc(encoded_length);
936 if (encoded == NULL) break;
937 encoded_strlen = CtdlEncodeBase64(encoded, ChrPtr(att->Data), StrLength(att->Data), 1);
939 serv_printf("--%s", top_boundary);
940 serv_printf("Content-type: %s", ChrPtr(att->ContentType));
941 serv_printf("Content-disposition: attachment; filename=\"%s\"", ChrPtr(att->FileName));
942 serv_puts("Content-transfer-encoding: base64");
944 serv_write(encoded, encoded_strlen);
949 serv_printf("--%s--", top_boundary);
958 * Post message (or don't post message)
960 * Note regarding the "dont_post" variable:
961 * A random value (actually, it's just a timestamp) is inserted as a hidden
962 * field called "postseq" when the display_enter page is generated. This
963 * value is checked when posting, using the static variable dont_post. If a
964 * user attempts to post twice using the same dont_post value, the message is
965 * discarded. This prevents the accidental double-saving of the same message
966 * if the user happens to click the browser "back" button.
968 void post_message(void)
971 StrBuf *EmailAddress;
974 StrBuf *encoded_subject = NULL;
975 static long dont_post = (-1L);
976 int is_anonymous = 0;
977 const StrBuf *display_name = NULL;
981 if (havebstr("force_room")) {
982 gotoroom(sbstr("force_room"));
985 if (havebstr("display_name")) {
986 display_name = sbstr("display_name");
987 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
993 if (!strcasecmp(bstr("submit_action"), "cancel")) {
994 AppendImportantMessage(_("Cancelled. Message was not posted."), -1);
995 } else if (lbstr("postseq") == dont_post) {
996 AppendImportantMessage(
997 _("Automatically cancelled because you have already "
998 "saved this message."), -1);
1000 const char CMD[] = "ENT0 1|%s|%d|4|%s|%s||%s|%s|%s|%s|%s";
1001 StrBuf *Recp = NULL;
1004 const StrBuf *Wikipage = NULL;
1005 const StrBuf *my_email_addr = NULL;
1006 StrBuf *CmdBuf = NULL;
1007 StrBuf *references = NULL;
1011 save_to_drafts = !strcasecmp(bstr("submit_action"), "drafts");
1014 if (save_to_drafts) {
1015 /* temporarily change to the drafts room */
1016 serv_puts("GOTO _DRAFTS_");
1017 StrBuf_ServGetln(Buf);
1018 if (GetServerStatusMsg(Buf, NULL, 1, 2) != 2) {
1019 /* You probably don't even have a dumb Drafts folder */
1020 syslog(9, "%s:%d: server save to drafts error: %s\n", __FILE__, __LINE__, ChrPtr(Buf) + 4);
1021 AppendImportantMessage(_("Saved to Drafts failed: "), -1);
1028 if (havebstr("references"))
1030 const StrBuf *ref = sbstr("references");
1031 references = NewStrBufDup(ref);
1032 if (*ChrPtr(references) == '|') { /* remove leading '|' if present */
1033 StrBufCutLeft(references, 1);
1035 StrBufReplaceChars(references, '|', '!');
1037 if (havebstr("subject")) {
1040 * make enough room for the encoded string;
1041 * plus the QP header
1043 Subj = sbstr("subject");
1045 StrBufRFC2047encode(&encoded_subject, Subj);
1047 UserName = NewStrBuf();
1048 EmailAddress = NewStrBuf();
1049 EncBuf = NewStrBuf();
1051 Recp = StrBufSanitizeEmailRecipientVector(sbstr("recp"), UserName, EmailAddress, EncBuf);
1052 Cc = StrBufSanitizeEmailRecipientVector(sbstr("cc"), UserName, EmailAddress, EncBuf);
1053 Bcc = StrBufSanitizeEmailRecipientVector(sbstr("bcc"), UserName, EmailAddress, EncBuf);
1055 FreeStrBuf(&UserName);
1056 FreeStrBuf(&EmailAddress);
1057 FreeStrBuf(&EncBuf);
1059 Wikipage = sbstr("page");
1060 my_email_addr = sbstr("my_email_addr");
1062 HeaderLen = StrLength(Recp) +
1063 StrLength(encoded_subject) +
1066 StrLength(Wikipage) +
1067 StrLength(my_email_addr) +
1068 StrLength(references);
1069 CmdBuf = NewStrBufPlain(NULL, sizeof (CMD) + HeaderLen);
1070 StrBufPrintf(CmdBuf,
1072 save_to_drafts?"":ChrPtr(Recp),
1074 ChrPtr(encoded_subject),
1075 ChrPtr(display_name),
1076 save_to_drafts?"":ChrPtr(Cc),
1077 save_to_drafts?"":ChrPtr(Bcc),
1079 ChrPtr(my_email_addr),
1080 ChrPtr(references));
1081 FreeStrBuf(&references);
1082 FreeStrBuf(&encoded_subject);
1084 if ((HeaderLen + StrLength(sbstr("msgtext")) < 10) &&
1085 (GetCount(WCC->attachments) == 0)){
1086 AppendImportantMessage(_("Refusing to post empty message.\n"), -1);
1087 FreeStrBuf(&CmdBuf);
1092 syslog(9, "%s\n", ChrPtr(CmdBuf));
1093 serv_puts(ChrPtr(CmdBuf));
1094 FreeStrBuf(&CmdBuf);
1096 StrBuf_ServGetln(Buf);
1097 if (GetServerStatus(Buf, NULL) == 4) {
1098 if (save_to_drafts) {
1099 if ( (havebstr("recp"))
1100 || (havebstr("cc" ))
1101 || (havebstr("bcc" )) ) {
1102 /* save recipient headers or room to post to */
1103 serv_printf("To: %s", ChrPtr(Recp));
1104 serv_printf("Cc: %s", ChrPtr(Cc));
1105 serv_printf("Bcc: %s", ChrPtr(Bcc));
1107 serv_printf("X-Citadel-Room: %s", ChrPtr(WC->CurRoom.name));
1110 post_mime_to_server();
1111 if (save_to_drafts) {
1112 AppendImportantMessage(_("Message has been saved to Drafts.\n"), -1);
1113 gotoroom(WCC->CurRoom.name);
1117 } else if ( (havebstr("recp"))
1118 || (havebstr("cc" ))
1119 || (havebstr("bcc" ))
1121 AppendImportantMessage(_("Message has been sent.\n"), -1);
1124 AppendImportantMessage(_("Message has been posted.\n"), -1);
1126 dont_post = lbstr("postseq");
1128 syslog(9, "%s:%d: server post error: %s\n", __FILE__, __LINE__, ChrPtr(Buf) + 4);
1129 AppendImportantMessage(ChrPtr(Buf) + 4, StrLength(Buf) - 4);
1130 if (save_to_drafts) gotoroom(WCC->CurRoom.name);
1144 DeleteHash(&WCC->attachments);
1147 * We may have been supplied with instructions regarding the location
1148 * to which we must return after posting. If found, go there.
1150 if (havebstr("return_to")) {
1151 http_redirect(bstr("return_to"));
1154 * If we were editing a page in a wiki room, go to that page now.
1156 else if (havebstr("page")) {
1157 snprintf(buf, sizeof buf, "wiki?page=%s", bstr("page"));
1161 * Otherwise, just go to the "read messages" loop.
1164 readloop(readnew, eUseDefault);
1170 * Client is uploading an attachment
1172 void upload_attachment(void) {
1173 wcsession *WCC = WC;
1179 wc_mime_attachment *att;
1181 syslog(9, "upload_attachment()\n");
1182 wc_printf("upload_attachment()<br>\n");
1184 if (WCC->upload_length <= 0) {
1185 syslog(9, "ERROR no attachment was uploaded\n");
1186 wc_printf("ERROR no attachment was uploaded<br>\n");
1190 syslog(9, "Client is uploading %d bytes\n", WCC->upload_length);
1191 wc_printf("Client is uploading %d bytes<br>\n", WCC->upload_length);
1192 att = malloc(sizeof(wc_mime_attachment));
1193 memset(att, 0, sizeof(wc_mime_attachment ));
1194 att->length = WCC->upload_length;
1195 att->ContentType = NewStrBufPlain(WCC->upload_content_type, -1);
1196 att->FileName = NewStrBufDup(WCC->upload_filename);
1198 if (WCC->attachments == NULL) {
1199 WCC->attachments = NewHash(1, Flathash);
1202 /* And add it to the list. */
1204 if ((GetCount(WCC->attachments) > 0) &&
1205 GetHashAt(WCC->attachments,
1206 GetCount(WCC->attachments) -1,
1207 &newnlen, &newn, &v))
1208 n = *((int*) newn) + 1;
1209 Put(WCC->attachments, IKEY(n), att, DestroyMime);
1212 * Mozilla sends a simple filename, which is what we want,
1213 * but Satan's Browser sends an entire pathname. Reduce
1214 * the path to just a filename if we need to.
1216 pch = strrchr(ChrPtr(att->FileName), '/');
1218 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1220 pch = strrchr(ChrPtr(att->FileName), '\\');
1222 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1226 * Transfer control of this memory from the upload struct
1227 * to the attachment struct.
1229 att->Data = WCC->upload;
1231 WCC->upload_length = 0;
1236 * Remove an attachment from the message currently being composed.
1238 * Currently we identify the attachment to be removed by its filename.
1239 * There is probably a better way to do this.
1241 void remove_attachment(void) {
1242 wcsession *WCC = WC;
1243 wc_mime_attachment *att;
1245 StrBuf *WhichAttachment;
1250 WhichAttachment = NewStrBufDup(sbstr("which_attachment"));
1251 StrBufUnescape(WhichAttachment, 0);
1252 at = GetNewHashPos(WCC->attachments, 0);
1254 GetHashPos(WCC->attachments, at, &len, &key, &vAtt);
1256 att = (wc_mime_attachment*) vAtt;
1257 if ((att != NULL) &&
1258 (strcmp(ChrPtr(WhichAttachment),
1259 ChrPtr(att->FileName) ) == 0))
1261 DeleteEntryFromHash(WCC->attachments, at);
1265 while (NextHashPos(WCC->attachments, at));
1266 FreeStrBuf(&WhichAttachment);
1267 wc_printf("remove_attachment() completed\n");
1271 long FourHash(const char *key, long length)
1275 const unsigned char *ptr = (const unsigned char*)key;
1277 for (i = 0; i < 4; i++, ptr ++)
1297 * display the message entry screen
1299 void display_enter(void)
1304 const StrBuf *display_name = NULL;
1305 int recipient_required = 0;
1306 int subject_required = 0;
1307 int recipient_bad = 0;
1308 int is_anonymous = 0;
1309 wcsession *WCC = WC;
1313 if (havebstr("force_room")) {
1314 gotoroom(sbstr("force_room"));
1317 display_name = sbstr("display_name");
1318 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1319 display_name = NULL;
1323 /* First test to see whether this is a room that requires recipients to be entered */
1325 serv_puts("ENT0 0");
1326 StrBuf_ServGetln(Line);
1327 rc = GetServerStatusMsg(Line, &Result, 0, 2);
1329 if (Result == 570) { /* 570 means that we need a recipient here */
1330 recipient_required = 1;
1332 else if (rc != 2) { /* Any other error means that we cannot continue */
1333 rc = GetServerStatusMsg(Line, &Result, 0, 2);
1334 readloop(readnew, eUseDefault);
1339 /* Is the server strongly recommending that the user enter a message subject? */
1340 if (StrLength(Line) > 4) {
1341 subject_required = extract_int(ChrPtr(Line) + 4, 1);
1345 * Are we perhaps in an address book view? If so, then an "enter
1346 * message" command really means "add new entry."
1348 if (WCC->CurRoom.defview == VIEW_ADDRESSBOOK) {
1349 do_edit_vcard(-1, "", NULL, NULL, "", ChrPtr(WCC->CurRoom.name));
1355 * Are we perhaps in a calendar room? If so, then an "enter
1356 * message" command really means "add new calendar item."
1358 if (WCC->CurRoom.defview == VIEW_CALENDAR) {
1359 display_edit_event();
1365 * Are we perhaps in a tasks view? If so, then an "enter
1366 * message" command really means "add new task."
1368 if (WCC->CurRoom.defview == VIEW_TASKS) {
1369 display_edit_task();
1376 * If the "replying_to" variable is set, it refers to a message
1377 * number from which we must extract some header fields...
1379 replying_to = lbstr("replying_to");
1380 if (replying_to > 0) {
1382 StrBuf *wefw = NULL;
1383 StrBuf *msgn = NULL;
1384 StrBuf *from = NULL;
1385 StrBuf *node = NULL;
1386 StrBuf *rfca = NULL;
1387 StrBuf *rcpt = NULL;
1388 StrBuf *cccc = NULL;
1389 serv_printf("MSG0 %ld|1", replying_to);
1391 StrBuf_ServGetln(Line);
1392 if (GetServerStatusMsg(Line, NULL, 0, 0) == 1)
1393 while (len = StrBuf_ServGetln(Line),
1396 strcmp(ChrPtr(Line), "000")))
1399 if ((StrLength(Line) > 4) &&
1400 (ChrPtr(Line)[4] == '='))
1401 which = FourHash(ChrPtr(Line), 4);
1403 if (which == l_subj)
1405 StrBuf *subj = NewStrBuf();
1406 StrBuf *FlatSubject;
1408 if (!strcasecmp(bstr("replying_mode"), "forward")) {
1409 if (strncasecmp(ChrPtr(Line) + 5, "Fw:", 3)) {
1410 StrBufAppendBufPlain(subj, HKEY("Fw: "), 0);
1414 if (strncasecmp(ChrPtr(Line) + 5, "Re:", 3)) {
1415 StrBufAppendBufPlain(subj, HKEY("Re: "), 0);
1418 StrBufAppendBufPlain(subj,
1420 StrLength(Line) - 5, 0);
1421 FlatSubject = NewStrBufPlain(NULL, StrLength(subj));
1422 StrBuf_RFC822_to_Utf8(FlatSubject, subj, NULL, NULL);
1424 PutBstr(HKEY("subject"), FlatSubject);
1427 else if (which == l_wefw)
1432 wefw = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1434 /* Trim down excessively long lists of thread references. We eliminate the
1435 * second one in the list so that the thread root remains intact.
1437 rrtok = num_tokens(ChrPtr(wefw), '|');
1438 rrlen = StrLength(wefw);
1439 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
1440 StrBufRemove_token(wefw, 1, '|');
1444 else if (which == l_msgn) {
1445 msgn = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1448 else if (which == l_from) {
1450 from = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1451 FlatFrom = NewStrBufPlain(NULL, StrLength(from));
1452 StrBuf_RFC822_to_Utf8(FlatFrom, from, NULL, NULL);
1455 for (i=0; i<StrLength(from); ++i) {
1456 if (ChrPtr(from)[i] == ',')
1457 StrBufPeek(from, NULL, i, ' ');
1461 else if (which == l_rcpt) {
1462 rcpt = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1465 else if (which == l_cccc) {
1466 cccc = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1469 else if (which == l_node) {
1470 node = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1473 else if (which == l_rfca) {
1475 rfca = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1476 FlatRFCA = NewStrBufPlain(NULL, StrLength(rfca));
1477 StrBuf_RFC822_to_Utf8(FlatRFCA, rfca, NULL, NULL);
1484 if (StrLength(wefw) + StrLength(msgn) > 0) {
1485 StrBuf *refs = NewStrBuf();
1486 if (StrLength(wefw) > 0) {
1487 StrBufAppendBuf(refs, wefw, 0);
1489 if ( (StrLength(wefw) > 0) &&
1490 (StrLength(msgn) > 0) )
1492 StrBufAppendBufPlain(refs, HKEY("|"), 0);
1494 if (StrLength(msgn) > 0) {
1495 StrBufAppendBuf(refs, msgn, 0);
1497 PutBstr(HKEY("references"), refs);
1501 * If this is a Reply or a ReplyAll, copy the sender's email into the To: field
1503 if ( (!strcasecmp(bstr("replying_mode"), "reply"))
1504 || (!strcasecmp(bstr("replying_mode"), "replyall"))
1507 if (StrLength(rfca) > 0) {
1508 to_rcpt = NewStrBuf();
1509 StrBufAppendBuf(to_rcpt, from, 0);
1510 StrBufAppendBufPlain(to_rcpt, HKEY(" <"), 0);
1511 StrBufAppendBuf(to_rcpt, rfca, 0);
1512 StrBufAppendBufPlain(to_rcpt, HKEY(">"), 0);
1517 if ( (StrLength(node) > 0)
1518 && (strcasecmp(ChrPtr(node), ChrPtr(WC->serv_info->serv_nodename)))
1520 StrBufAppendBufPlain(to_rcpt, HKEY(" @ "), 0);
1521 StrBufAppendBuf(to_rcpt, node, 0);
1524 PutBstr(HKEY("recp"), to_rcpt);
1528 * Only if this is a ReplyAll, copy all recipients into the Cc: field
1530 if ( (!strcasecmp(bstr("replying_mode"), "replyall"))
1532 StrBuf *cc_rcpt = rcpt;
1534 if (StrLength(cccc) > 0) {
1535 if (cc_rcpt != NULL) {
1536 StrBufAppendPrintf(cc_rcpt, ", ");
1537 StrBufAppendBuf(cc_rcpt, cccc, 0);
1543 if (cc_rcpt != NULL)
1544 PutBstr(HKEY("cc"), cc_rcpt);
1556 * Otherwise proceed normally.
1557 * Do a custom room banner with no navbar...
1560 if (recipient_required) {
1561 const StrBuf *Recp = NULL;
1562 const StrBuf *Cc = NULL;
1563 const StrBuf *Bcc = NULL;
1564 const StrBuf *Wikipage = NULL;
1565 StrBuf *CmdBuf = NULL;
1566 const char CMD[] = "ENT0 0|%s|%d|0||%s||%s|%s|%s";
1568 Recp = sbstr("recp");
1571 Wikipage = sbstr("page");
1573 CmdBuf = NewStrBufPlain(NULL,
1576 StrLength(display_name) +
1579 StrLength(Wikipage));
1581 StrBufPrintf(CmdBuf,
1585 ChrPtr(display_name),
1589 serv_puts(ChrPtr(CmdBuf));
1590 StrBuf_ServGetln(CmdBuf);
1592 rc = GetServerStatusMsg(CmdBuf, &Result, 0, 0);
1594 if (Result == 570) { /* 570 means we have an invalid recipient listed */
1595 if (havebstr("recp") &&
1598 recipient_bad = 1; /* TODO: and now????? */
1601 else if (rc != 2) { /* Any other error means that we cannot continue */
1602 wc_printf("<em>%s</em><br>\n", ChrPtr(CmdBuf) +4); /* TODO -> important message */
1603 FreeStrBuf(&CmdBuf);
1606 FreeStrBuf(&CmdBuf);
1608 if (recipient_required)
1609 PutBstr(HKEY("__RCPTREQUIRED"), NewStrBufPlain(HKEY("1")));
1610 if (recipient_required || subject_required)
1611 PutBstr(HKEY("__SUBJREQUIRED"), NewStrBufPlain(HKEY("1")));
1614 output_headers(1, 0, 0, 0, 1, 0);
1615 DoTemplate(HKEY("edit_message"), NULL, &NoCtx);
1624 void delete_msg(void)
1629 msgid = lbstr("msgid");
1631 if ((WC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
1632 serv_printf("DELE %ld", msgid);
1634 else { /* Otherwise move it to Trash */
1635 serv_printf("MOVE %ld|_TRASH_|0", msgid);
1638 StrBuf_ServGetln(Line);
1639 GetServerStatusMsg(Line, NULL, 1, 0);
1641 readloop(readnew, eUseDefault);
1646 * move a message to another room
1652 msgid = lbstr("msgid");
1654 if (havebstr("move_button")) {
1656 serv_printf("MOVE %ld|%s", msgid, bstr("target_room"));
1658 StrBuf_ServGetln(Line);
1659 GetServerStatusMsg(Line, NULL, 1, 0);
1662 AppendImportantMessage(_("The message was not moved."), -1);
1665 readloop(readnew, eUseDefault);
1671 * Generic function to output an arbitrary MIME attachment from
1672 * message being composed
1674 * partnum The MIME part to be output
1675 * filename Fake filename to give
1676 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1678 void postpart(StrBuf *partnum, StrBuf *filename, int force_download)
1681 StrBuf *content_type;
1682 wc_mime_attachment *part;
1685 i = StrToi(partnum);
1686 if (GetHash(WC->attachments, IKEY(i), &vPart) &&
1688 part = (wc_mime_attachment*) vPart;
1689 if (force_download) {
1690 content_type = NewStrBufPlain(HKEY("application/octet-stream"));
1693 content_type = NewStrBufDup(part->ContentType);
1695 StrBufAppendBuf(WC->WBuf, part->Data, 0);
1696 http_transmit_thing(ChrPtr(content_type), 0);
1698 hprintf("HTTP/1.1 404 %s\n", ChrPtr(partnum));
1699 output_headers(0, 0, 0, 0, 0, 0);
1700 hprintf("Content-Type: text/plain\r\n");
1702 wc_printf(_("An error occurred while retrieving this part: %s/%s\n"),
1703 ChrPtr(partnum), ChrPtr(filename));
1706 FreeStrBuf(&content_type);
1711 * Generic function to output an arbitrary MIME part from an arbitrary
1712 * message number on the server.
1714 * msgnum Number of the item on the citadel server
1715 * partnum The MIME part to be output
1716 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1718 void mimepart(int force_download)
1722 wcsession *WCC = WC;
1725 StrBuf *ContentType = NewStrBufPlain(HKEY("application/octet-stream"));
1728 att = Buf = NewStrBuf();
1729 msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
1730 StrBufExtract_token(att, WCC->Hdr->HR.ReqLine, 1, '/');
1732 serv_printf("OPNA %ld|%s", msgnum, ChrPtr(att));
1733 StrBuf_ServGetln(Buf);
1734 if (GetServerStatus(Buf, NULL) == 2) {
1735 StrBufCutLeft(Buf, 4);
1736 bytes = StrBufExtract_long(Buf, 0, '|');
1737 if (!force_download) {
1738 StrBufExtract_token(ContentType, Buf, 3, '|');
1741 serv_read_binary(WCC->WBuf, bytes, Buf);
1743 StrBuf_ServGetln(Buf);
1744 CT = ChrPtr(ContentType);
1746 if (!force_download) {
1747 if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
1748 StrBufExtract_token(Buf, WCC->Hdr->HR.ReqLine, 2, '/');
1749 CT = GuessMimeByFilename(SKEY(Buf));
1751 if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
1752 CT = GuessMimeType(SKEY(WCC->WBuf));
1755 http_transmit_thing(CT, 0);
1757 StrBufCutLeft(Buf, 4);
1758 hprintf("HTTP/1.1 404 %s\n", ChrPtr(Buf));
1759 output_headers(0, 0, 0, 0, 0, 0);
1760 hprintf("Content-Type: text/plain\r\n");
1762 wc_printf(_("An error occurred while retrieving this part: %s\n"),
1766 FreeStrBuf(&ContentType);
1772 * Read any MIME part of a message, from the server, into memory.
1774 StrBuf *load_mimepart(long msgnum, char *partnum)
1780 serv_printf("DLAT %ld|%s", msgnum, partnum);
1781 StrBuf_ServGetln(Buf);
1782 if (GetServerStatus(Buf, NULL) == 6) {
1783 StrBufCutLeft(Buf, 4);
1784 bytes = StrBufExtract_long(Buf, 0, '|');
1787 StrBuf_ServGetBLOBBuffered(Buf, bytes);
1797 * Read any MIME part of a message, from the server, into memory.
1799 void MimeLoadData(wc_mime_attachment *Mime)
1804 /* TODO: is there a chance the content type is different from the one we know? */
1806 serv_printf("DLAT %ld|%s", Mime->msgnum, ChrPtr(Mime->PartNum));
1808 StrBuf_ServGetln(Buf);
1809 if (GetServerStatus(Buf, NULL) == 6) {
1810 Ptr = &(ChrPtr(Buf)[4]);
1811 bytes = StrBufExtractNext_long(Buf, &Ptr, '|');
1812 StrBufSkip_NTokenS(Buf, &Ptr, '|', 3); /* filename, cbtype, mimetype */
1813 if (Mime->Charset == NULL) Mime->Charset = NewStrBuf();
1814 StrBufExtract_NextToken(Mime->Charset, Buf, &Ptr, '|');
1816 if (Mime->Data == NULL)
1817 Mime->Data = NewStrBufPlain(NULL, bytes);
1818 StrBuf_ServGetBLOBBuffered(Mime->Data, bytes);
1821 FlushStrBuf(Mime->Data);
1822 /* TODO XImportant message */
1828 void view_mimepart(void) {
1832 void download_mimepart(void) {
1836 void view_postpart(void) {
1837 StrBuf *filename = NewStrBuf();
1838 StrBuf *partnum = NewStrBuf();
1840 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1841 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1843 postpart(partnum, filename, 0);
1845 FreeStrBuf(&filename);
1846 FreeStrBuf(&partnum);
1849 void download_postpart(void) {
1850 StrBuf *filename = NewStrBuf();
1851 StrBuf *partnum = NewStrBuf();
1853 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1854 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1856 postpart(partnum, filename, 1);
1858 FreeStrBuf(&filename);
1859 FreeStrBuf(&partnum);
1864 void show_num_attachments(void) {
1865 wc_printf("%d", GetCount(WC->attachments));
1869 void h_readnew(void) { readloop(readnew, eUseDefault);}
1870 void h_readold(void) { readloop(readold, eUseDefault);}
1871 void h_readfwd(void) { readloop(readfwd, eUseDefault);}
1872 void h_headers(void) { readloop(headers, eUseDefault);}
1873 void h_do_search(void) { readloop(do_search, eUseDefault);}
1874 void h_readgt(void) { readloop(readgt, eUseDefault);}
1875 void h_readlt(void) { readloop(readlt, eUseDefault);}
1877 void jsonMessageListHdr(void)
1879 /* TODO: make a generic function */
1880 hprintf("HTTP/1.1 200 OK\r\n");
1881 hprintf("Content-type: application/json; charset=utf-8\r\n");
1882 hprintf("Server: %s / %s\r\n", PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software));
1883 hprintf("Connection: close\r\n");
1884 hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
1889 /* Output message list in JSON format */
1890 void jsonMessageList(void) {
1891 const StrBuf *room = sbstr("room");
1892 long oper = (havebstr("query")) ? do_search : readnew;
1895 readloop(oper, eUseDefault);
1899 void RegisterReadLoopHandlerset(
1901 GetParamsGetServerCall_func GetParamsGetServerCall,
1902 PrintViewHeader_func PrintViewHeader,
1903 load_msg_ptrs_detailheaders LH,
1904 LoadMsgFromServer_func LoadMsgFromServer,
1905 RenderView_or_Tail_func RenderView_or_Tail,
1906 View_Cleanup_func ViewCleanup
1909 RoomRenderer *Handler;
1911 Handler = (RoomRenderer*) malloc(sizeof(RoomRenderer));
1913 Handler->RoomType = RoomType;
1914 Handler->GetParamsGetServerCall = GetParamsGetServerCall;
1915 Handler->PrintViewHeader = PrintViewHeader;
1916 Handler->LoadMsgFromServer = LoadMsgFromServer;
1917 Handler->RenderView_or_Tail = RenderView_or_Tail;
1918 Handler->ViewCleanup = ViewCleanup;
1919 Handler->LHParse = LH;
1921 Put(ReadLoopHandler, IKEY(RoomType), Handler, NULL);
1928 RegisterPreference("use_sig",
1929 _("Attach signature to email messages?"),
1932 RegisterPreference("signature", _("Use this signature:"), PRF_QP_STRING, NULL);
1933 RegisterPreference("default_header_charset",
1934 _("Default character set for email headers:"),
1937 RegisterPreference("defaultfrom", _("Preferred email address"), PRF_STRING, NULL);
1938 RegisterPreference("defaultname",
1939 _("Preferred display name for email messages"),
1942 RegisterPreference("defaulthandle",
1943 _("Preferred display name for bulletin board posts"),
1946 RegisterPreference("mailbox",_("Mailbox view mode"), PRF_STRING, NULL);
1948 WebcitAddUrlHandler(HKEY("readnew"), "", 0, h_readnew, ANONYMOUS|NEED_URL);
1949 WebcitAddUrlHandler(HKEY("readold"), "", 0, h_readold, ANONYMOUS|NEED_URL);
1950 WebcitAddUrlHandler(HKEY("readfwd"), "", 0, h_readfwd, ANONYMOUS|NEED_URL);
1951 WebcitAddUrlHandler(HKEY("headers"), "", 0, h_headers, NEED_URL);
1952 WebcitAddUrlHandler(HKEY("readgt"), "", 0, h_readgt, ANONYMOUS|NEED_URL);
1953 WebcitAddUrlHandler(HKEY("readlt"), "", 0, h_readlt, ANONYMOUS|NEED_URL);
1954 WebcitAddUrlHandler(HKEY("do_search"), "", 0, h_do_search, 0);
1955 WebcitAddUrlHandler(HKEY("display_enter"), "", 0, display_enter, 0);
1956 WebcitAddUrlHandler(HKEY("post"), "", 0, post_message, PROHIBIT_STARTPAGE);
1957 WebcitAddUrlHandler(HKEY("move_msg"), "", 0, move_msg, PROHIBIT_STARTPAGE);
1958 WebcitAddUrlHandler(HKEY("delete_msg"), "", 0, delete_msg, PROHIBIT_STARTPAGE);
1959 WebcitAddUrlHandler(HKEY("msg"), "", 0, embed_message, NEED_URL);
1960 WebcitAddUrlHandler(HKEY("message"), "", 0, handle_one_message, NEED_URL|XHTTP_COMMANDS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
1961 WebcitAddUrlHandler(HKEY("printmsg"), "", 0, print_message, NEED_URL);
1962 WebcitAddUrlHandler(HKEY("msgheaders"), "", 0, display_headers, NEED_URL);
1964 WebcitAddUrlHandler(HKEY("mimepart"), "", 0, view_mimepart, NEED_URL);
1965 WebcitAddUrlHandler(HKEY("mimepart_download"), "", 0, download_mimepart, NEED_URL);
1966 WebcitAddUrlHandler(HKEY("postpart"), "", 0, view_postpart, NEED_URL|PROHIBIT_STARTPAGE);
1967 WebcitAddUrlHandler(HKEY("postpart_download"), "", 0, download_postpart, NEED_URL|PROHIBIT_STARTPAGE);
1968 WebcitAddUrlHandler(HKEY("upload_attachment"), "", 0, upload_attachment, AJAX);
1969 WebcitAddUrlHandler(HKEY("remove_attachment"), "", 0, remove_attachment, AJAX);
1970 WebcitAddUrlHandler(HKEY("show_num_attachments"), "", 0, show_num_attachments, AJAX);
1973 WebcitAddUrlHandler(HKEY("roommsgs"), "", 0, jsonMessageList,0);
1975 l_subj = FourHash("subj", 4);
1976 l_wefw = FourHash("wefw", 4);
1977 l_msgn = FourHash("msgn", 4);
1978 l_from = FourHash("from", 4);
1979 l_rcpt = FourHash("rcpt", 4);
1980 l_cccc = FourHash("cccc", 4);
1981 l_node = FourHash("node", 4);
1982 l_rfca = FourHash("rfca", 4);
1988 SessionDetachModule_MSG
1991 DeleteHash(&sess->summ);