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 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,
533 SharedMessageStatus *Stat,
534 load_msg_ptrs_detailheaders LH)
537 message_summary *Msg;
542 const char *Ptr = NULL;
544 Stat->lowest_found = LONG_MAX;
545 Stat->highest_found = LONG_MIN;
547 if (WCC->summ != NULL) {
548 DeleteHash(&WCC->summ);
550 WCC->summ = NewHash(1, Flathash);
554 StrBuf_ServGetln(Buf);
555 if (GetServerStatus(Buf, NULL) != 1) {
557 return (Stat->nummsgs);
560 while (len = StrBuf_ServGetln(Buf),
563 strcmp(ChrPtr(Buf), "000")!= 0)))
565 if (Stat->nummsgs < Stat->maxload) {
568 Msg = (message_summary*)malloc(sizeof(message_summary));
569 memset(Msg, 0, sizeof(message_summary));
571 Msg->msgnum = StrBufExtractNext_long(Buf, &Ptr, '|');
572 Msg->date = StrBufExtractNext_long(Buf, &Ptr, '|');
574 if (Stat->nummsgs == 0) {
575 if (Msg->msgnum < Stat->lowest_found) {
576 Stat->lowest_found = Msg->msgnum;
578 if (Msg->msgnum > Stat->highest_found) {
579 Stat->highest_found = Msg->msgnum;
583 if ((Msg->msgnum == 0) && (StrLength(Buf) < 32)) {
589 * as citserver probably gives us messages in forward date sorting
590 * nummsgs should be the same order as the message date.
592 if (Msg->date == 0) {
593 Msg->date = Stat->nummsgs;
594 if (StrLength(Buf) < 32)
597 if ((!skipit) && (LH != NULL)) {
598 if (!LH(Buf, &Ptr, Msg, Buf2)){
604 Put(WCC->summ, (const char *)&n, sizeof(n), Msg, DestroyMessageSummary);
610 return (Stat->nummsgs);
616 * \brief sets FlagToSet for each of ScanMe that appears in MatchMSet
617 * \param ScanMe List of BasicMsgStruct to be searched it MatchSet
618 * \param MatchMSet MSet we want to flag
619 * \param FlagToSet Flag to set on each BasicMsgStruct->Flags if in MSet
621 void SetFlagsFromMSet(HashList *ScanMe, MSet *MatchMSet, int FlagToSet, int Reverse)
627 message_summary *Msg;
629 at = GetNewHashPos(ScanMe, 0);
630 while (GetNextHashPos(ScanMe, at, &HKLen, &HashKey, &vMsg)) {
631 /* Are you a new message, or an old message? */
632 Msg = (message_summary*) vMsg;
633 if (Reverse && IsInMSetList(MatchMSet, Msg->msgnum)) {
634 Msg->Flags = Msg->Flags | FlagToSet;
636 else if (!Reverse && !IsInMSetList(MatchMSet, Msg->msgnum)) {
637 Msg->Flags = Msg->Flags | FlagToSet;
644 void load_seen_flags(void)
650 OldMsg = NewStrBuf();
652 StrBuf_ServGetln(OldMsg);
653 if (GetServerStatus(OldMsg, NULL) == 2) {
654 StrBufCutLeft(OldMsg, 4);
661 if (ParseMSet(&MatchMSet, OldMsg))
663 SetFlagsFromMSet(WCC->summ, MatchMSet, MSGFLAG_READ, 0);
665 DeleteMSet(&MatchMSet);
669 extern readloop_struct rlid[];
671 typedef struct _RoomRenderer{
674 GetParamsGetServerCall_func GetParamsGetServerCall;
675 PrintViewHeader_func PrintViewHeader;
676 LoadMsgFromServer_func LoadMsgFromServer;
677 RenderView_or_Tail_func RenderView_or_Tail;
678 View_Cleanup_func ViewCleanup;
679 load_msg_ptrs_detailheaders LHParse;
684 * command loop for reading messages
686 * Set oper to "readnew" or "readold" or "readfwd" or "headers" or "readgt" or "readlt" or "do_search"
688 void readloop(long oper, eCustomRoomRenderer ForceRenderer)
690 RoomRenderer *ViewMsg;
693 message_summary *Msg;
700 WCTemplputParams SubTP;
701 SharedMessageStatus Stat;
704 if (havebstr("is_summary") && (1 == (ibstr("is_summary")))) {
705 WCC->CurRoom.view = VIEW_MAILBOX;
708 if (havebstr("is_ajax") && (1 == (ibstr("is_ajax")))) {
712 if ((oper == do_search) && (WCC->CurRoom.view == VIEW_WIKI)) {
713 display_wiki_pagelist();
717 if (WCC->CurRoom.view == VIEW_WIKI) {
718 http_redirect("wiki?page=home");
722 memset(&Stat, 0, sizeof(SharedMessageStatus));
723 Stat.maxload = 10000;
724 Stat.lowest_found = (-1);
725 Stat.highest_found = (-1);
726 if (ForceRenderer == eUseDefault)
727 GetHash(ReadLoopHandler, IKEY(WCC->CurRoom.view), &vViewMsg);
729 GetHash(ReadLoopHandler, IKEY(ForceRenderer), &vViewMsg);
730 if (vViewMsg == NULL) {
731 WCC->CurRoom.view = VIEW_BBS;
732 GetHash(ReadLoopHandler, IKEY(WCC->CurRoom.view), &vViewMsg);
734 if (vViewMsg == NULL) {
735 return; /* TODO: print message */
738 ViewMsg = (RoomRenderer*) vViewMsg;
740 output_headers(1, 1, 1, 0, 0, 0);
741 } else if (WCC->CurRoom.view == VIEW_MAILBOX) {
742 jsonMessageListHdr();
745 if (ViewMsg->GetParamsGetServerCall != NULL) {
746 r = ViewMsg->GetParamsGetServerCall(
761 case 300: /* the callback hook should do the work for us here, since he knows what to do. */
767 if (!IsEmptyStr(cmd))
768 Stat.nummsgs = load_msg_ptrs(cmd, &Stat, ViewMsg->LHParse);
772 memset(&SubTP, 0, sizeof(WCTemplputParams));
773 SubTP.Filter.ContextType = CTX_MAILSUM;
774 SubTP.Context = NULL;
775 SortIt = RetrieveSort(&SubTP, NULL, 0,
776 HKEY("date"), Stat.defaultsortorder);
778 SortByPayload(WCC->summ, SortIt);
780 if (Stat.startmsg < 0) {
784 if (Stat.load_seen) load_seen_flags();
787 * Print any inforation above the message list...
789 if (ViewMsg->PrintViewHeader != NULL)
790 ViewMsg->PrintViewHeader(&Stat, &ViewSpecific);
792 WCC->startmsg = Stat.startmsg;
793 WCC->maxmsgs = Stat.maxmsgs;
794 WCC->num_displayed = 0;
796 /* Put some helpful data in vars for mailsummary_json */
801 StrBufPrintf(Foo, "%ld", Stat.nummsgs);
802 PutBstr(HKEY("__READLOOP:TOTALMSGS"), NewStrBufDup(Foo));
803 StrBufPrintf(Foo, "%ld", Stat.startmsg);
804 PutBstr(HKEY("__READLOOP:STARTMSG"), Foo);
808 * iterate over each message. if we need to load an attachment, do it here.
811 if ((ViewMsg->LoadMsgFromServer != NULL) &&
814 at = GetNewHashPos(WCC->summ, 0);
815 Stat.num_displayed = i = 0;
816 while ( GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
817 Msg = (message_summary*) vMsg;
818 if ((Msg->msgnum >= Stat.startmsg) && (Stat.num_displayed <= Stat.maxmsgs)) {
819 ViewMsg->LoadMsgFromServer(&Stat,
822 (Msg->Flags & MSGFLAG_READ) != 0,
831 * Done iterating the message list. now tasks we want to do after.
833 if (ViewMsg->RenderView_or_Tail != NULL)
834 ViewMsg->RenderView_or_Tail(&Stat, &ViewSpecific, oper);
836 if (ViewMsg->ViewCleanup != NULL)
837 ViewMsg->ViewCleanup(&ViewSpecific);
841 if (WCC->summ != NULL) {
842 DeleteHash(&WCC->summ);
848 * Back end for post_message()
849 * ... this is where the actual message gets transmitted to the server.
851 void post_mime_to_server(void) {
853 char top_boundary[SIZ];
854 char alt_boundary[SIZ];
855 int is_multipart = 0;
857 wc_mime_attachment *att;
859 size_t encoded_length;
860 size_t encoded_strlen;
861 char *txtmail = NULL;
862 int include_text_alt = 0; /* Set to nonzero to include multipart/alternative text/plain */
864 sprintf(top_boundary, "Citadel--Multipart--%s--%04x--%04x",
865 ChrPtr(WCC->serv_info->serv_fqdn),
869 sprintf(alt_boundary, "Citadel--Multipart--%s--%04x--%04x",
870 ChrPtr(WCC->serv_info->serv_fqdn),
875 /* RFC2045 requires this, and some clients look for it... */
876 serv_puts("MIME-Version: 1.0");
877 serv_puts("X-Mailer: " PACKAGE_STRING);
879 /* If there are attachments, we have to do multipart/mixed */
880 if (GetCount(WCC->attachments) > 0) {
884 /* Only do multipart/alternative for mailboxes. BBS and Wiki rooms don't need it. */
885 if (WC->CurRoom.view == VIEW_MAILBOX) {
886 include_text_alt = 1;
890 /* Remember, serv_printf() appends an extra newline */
891 serv_printf("Content-type: multipart/mixed; boundary=\"%s\"\n", top_boundary);
892 serv_printf("This is a multipart message in MIME format.\n");
893 serv_printf("--%s", top_boundary);
896 /* Remember, serv_printf() appends an extra newline */
897 if (include_text_alt) {
898 serv_printf("Content-type: multipart/alternative; "
899 "boundary=\"%s\"\n", alt_boundary);
900 serv_printf("This is a multipart message in MIME format.\n");
901 serv_printf("--%s", alt_boundary);
903 serv_puts("Content-type: text/plain; charset=utf-8");
904 serv_puts("Content-Transfer-Encoding: quoted-printable");
906 txtmail = html_to_ascii(bstr("msgtext"), 0, 80, 0);
907 text_to_server_qp(txtmail); /* Transmit message in quoted-printable encoding */
910 serv_printf("\n--%s", alt_boundary);
913 serv_puts("Content-type: text/html; charset=utf-8");
914 serv_puts("Content-Transfer-Encoding: quoted-printable");
916 serv_puts("<html><body>\r\n");
917 text_to_server_qp(bstr("msgtext")); /* Transmit message in quoted-printable encoding */
918 serv_puts("</body></html>\r\n");
920 if (include_text_alt) {
921 serv_printf("--%s--", alt_boundary);
930 /* Add in the attachments */
931 it = GetNewHashPos(WCC->attachments, 0);
932 while (GetNextHashPos(WCC->attachments, it, &len, &Key, &vAtt)) {
933 att = (wc_mime_attachment *)vAtt;
934 if (att->length == 0)
936 encoded_length = ((att->length * 150) / 100);
937 encoded = malloc(encoded_length);
938 if (encoded == NULL) break;
939 encoded_strlen = CtdlEncodeBase64(encoded, ChrPtr(att->Data), StrLength(att->Data), 1);
941 serv_printf("--%s", top_boundary);
942 serv_printf("Content-type: %s", ChrPtr(att->ContentType));
943 serv_printf("Content-disposition: attachment; filename=\"%s\"", ChrPtr(att->FileName));
944 serv_puts("Content-transfer-encoding: base64");
946 serv_write(encoded, encoded_strlen);
951 serv_printf("--%s--", top_boundary);
960 * Post message (or don't post message)
962 * Note regarding the "dont_post" variable:
963 * A random value (actually, it's just a timestamp) is inserted as a hidden
964 * field called "postseq" when the display_enter page is generated. This
965 * value is checked when posting, using the static variable dont_post. If a
966 * user attempts to post twice using the same dont_post value, the message is
967 * discarded. This prevents the accidental double-saving of the same message
968 * if the user happens to click the browser "back" button.
970 void post_message(void)
973 StrBuf *EmailAddress;
976 StrBuf *encoded_subject = NULL;
977 static long dont_post = (-1L);
978 int is_anonymous = 0;
979 const StrBuf *display_name = NULL;
983 if (havebstr("force_room")) {
984 gotoroom(sbstr("force_room"));
987 if (havebstr("display_name")) {
988 display_name = sbstr("display_name");
989 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
995 if (!strcasecmp(bstr("submit_action"), "cancel")) {
996 AppendImportantMessage(_("Cancelled. Message was not posted."), -1);
997 } else if (lbstr("postseq") == dont_post) {
998 AppendImportantMessage(
999 _("Automatically cancelled because you have already "
1000 "saved this message."), -1);
1002 const char CMD[] = "ENT0 1|%s|%d|4|%s|%s||%s|%s|%s|%s|%s";
1003 StrBuf *Recp = NULL;
1006 const StrBuf *Wikipage = NULL;
1007 const StrBuf *my_email_addr = NULL;
1008 StrBuf *CmdBuf = NULL;
1009 StrBuf *references = NULL;
1010 int saving_to_drafts = 0;
1013 saving_to_drafts = !strcasecmp(bstr("submit_action"), "drafts");
1016 if (saving_to_drafts) {
1017 /* temporarily change to the drafts room */
1018 serv_puts("GOTO _DRAFTS_");
1019 StrBuf_ServGetln(Buf);
1020 if (GetServerStatusMsg(Buf, NULL, 1, 2) != 2) {
1021 /* You probably don't even have a dumb Drafts folder */
1022 syslog(9, "%s:%d: server save to drafts error: %s\n", __FILE__, __LINE__, ChrPtr(Buf) + 4);
1023 AppendImportantMessage(_("Saved to Drafts failed: "), -1);
1030 if (havebstr("references"))
1032 const StrBuf *ref = sbstr("references");
1033 references = NewStrBufDup(ref);
1034 if (*ChrPtr(references) == '|') { /* remove leading '|' if present */
1035 StrBufCutLeft(references, 1);
1037 StrBufReplaceChars(references, '|', '!');
1039 if (havebstr("subject")) {
1042 * make enough room for the encoded string;
1043 * plus the QP header
1045 Subj = sbstr("subject");
1047 StrBufRFC2047encode(&encoded_subject, Subj);
1049 UserName = NewStrBuf();
1050 EmailAddress = NewStrBuf();
1051 EncBuf = NewStrBuf();
1053 Recp = StrBufSanitizeEmailRecipientVector(sbstr("recp"), UserName, EmailAddress, EncBuf);
1054 Cc = StrBufSanitizeEmailRecipientVector(sbstr("cc"), UserName, EmailAddress, EncBuf);
1055 Bcc = StrBufSanitizeEmailRecipientVector(sbstr("bcc"), UserName, EmailAddress, EncBuf);
1057 FreeStrBuf(&UserName);
1058 FreeStrBuf(&EmailAddress);
1059 FreeStrBuf(&EncBuf);
1061 Wikipage = sbstr("page");
1062 my_email_addr = sbstr("my_email_addr");
1064 HeaderLen = StrLength(Recp) +
1065 StrLength(encoded_subject) +
1068 StrLength(Wikipage) +
1069 StrLength(my_email_addr) +
1070 StrLength(references);
1071 CmdBuf = NewStrBufPlain(NULL, sizeof (CMD) + HeaderLen);
1072 StrBufPrintf(CmdBuf,
1074 saving_to_drafts?"":ChrPtr(Recp),
1076 ChrPtr(encoded_subject),
1077 ChrPtr(display_name),
1078 saving_to_drafts?"":ChrPtr(Cc),
1079 saving_to_drafts?"":ChrPtr(Bcc),
1081 ChrPtr(my_email_addr),
1082 ChrPtr(references));
1083 FreeStrBuf(&references);
1084 FreeStrBuf(&encoded_subject);
1086 if ((HeaderLen + StrLength(sbstr("msgtext")) < 10) &&
1087 (GetCount(WCC->attachments) == 0)){
1088 AppendImportantMessage(_("Refusing to post empty message.\n"), -1);
1089 FreeStrBuf(&CmdBuf);
1094 syslog(9, "%s\n", ChrPtr(CmdBuf));
1095 serv_puts(ChrPtr(CmdBuf));
1096 FreeStrBuf(&CmdBuf);
1098 StrBuf_ServGetln(Buf);
1099 if (GetServerStatus(Buf, NULL) == 4) {
1100 if (saving_to_drafts) {
1101 if ( (havebstr("recp"))
1102 || (havebstr("cc" ))
1103 || (havebstr("bcc" )) ) {
1104 /* save recipient headers or room to post to */
1105 serv_printf("To: %s", ChrPtr(Recp));
1106 serv_printf("Cc: %s", ChrPtr(Cc));
1107 serv_printf("Bcc: %s", ChrPtr(Bcc));
1109 serv_printf("X-Citadel-Room: %s", ChrPtr(WC->CurRoom.name));
1112 post_mime_to_server();
1113 if (saving_to_drafts) {
1114 AppendImportantMessage(_("Message has been saved to Drafts.\n"), -1);
1115 gotoroom(WCC->CurRoom.name);
1119 } else if ( (havebstr("recp"))
1120 || (havebstr("cc" ))
1121 || (havebstr("bcc" ))
1123 AppendImportantMessage(_("Message has been sent.\n"), -1);
1126 AppendImportantMessage(_("Message has been posted.\n"), -1);
1128 dont_post = lbstr("postseq");
1130 syslog(9, "%s:%d: server post error: %s", __FILE__, __LINE__, ChrPtr(Buf) + 4);
1131 AppendImportantMessage(ChrPtr(Buf) + 4, StrLength(Buf) - 4);
1133 if (saving_to_drafts) gotoroom(WCC->CurRoom.name);
1147 DeleteHash(&WCC->attachments);
1150 * We may have been supplied with instructions regarding the location
1151 * to which we must return after posting. If found, go there.
1153 if (havebstr("return_to")) {
1154 http_redirect(bstr("return_to"));
1157 * If we were editing a page in a wiki room, go to that page now.
1159 else if (havebstr("page")) {
1160 snprintf(buf, sizeof buf, "wiki?page=%s", bstr("page"));
1164 * Otherwise, just go to the "read messages" loop.
1167 readloop(readnew, eUseDefault);
1173 * Client is uploading an attachment
1175 void upload_attachment(void) {
1176 wcsession *WCC = WC;
1182 wc_mime_attachment *att;
1184 syslog(9, "upload_attachment()\n");
1185 wc_printf("upload_attachment()<br>\n");
1187 if (WCC->upload_length <= 0) {
1188 syslog(9, "ERROR no attachment was uploaded\n");
1189 wc_printf("ERROR no attachment was uploaded<br>\n");
1193 syslog(9, "Client is uploading %d bytes\n", WCC->upload_length);
1194 wc_printf("Client is uploading %d bytes<br>\n", WCC->upload_length);
1195 att = malloc(sizeof(wc_mime_attachment));
1196 memset(att, 0, sizeof(wc_mime_attachment ));
1197 att->length = WCC->upload_length;
1198 att->ContentType = NewStrBufPlain(WCC->upload_content_type, -1);
1199 att->FileName = NewStrBufDup(WCC->upload_filename);
1201 if (WCC->attachments == NULL) {
1202 WCC->attachments = NewHash(1, Flathash);
1205 /* And add it to the list. */
1207 if ((GetCount(WCC->attachments) > 0) &&
1208 GetHashAt(WCC->attachments,
1209 GetCount(WCC->attachments) -1,
1210 &newnlen, &newn, &v))
1211 n = *((int*) newn) + 1;
1212 Put(WCC->attachments, IKEY(n), att, DestroyMime);
1215 * Mozilla sends a simple filename, which is what we want,
1216 * but Satan's Browser sends an entire pathname. Reduce
1217 * the path to just a filename if we need to.
1219 pch = strrchr(ChrPtr(att->FileName), '/');
1221 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1223 pch = strrchr(ChrPtr(att->FileName), '\\');
1225 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1229 * Transfer control of this memory from the upload struct
1230 * to the attachment struct.
1232 att->Data = WCC->upload;
1234 WCC->upload_length = 0;
1239 * Remove an attachment from the message currently being composed.
1241 * Currently we identify the attachment to be removed by its filename.
1242 * There is probably a better way to do this.
1244 void remove_attachment(void) {
1245 wcsession *WCC = WC;
1246 wc_mime_attachment *att;
1248 StrBuf *WhichAttachment;
1253 WhichAttachment = NewStrBufDup(sbstr("which_attachment"));
1254 StrBufUnescape(WhichAttachment, 0);
1255 at = GetNewHashPos(WCC->attachments, 0);
1257 GetHashPos(WCC->attachments, at, &len, &key, &vAtt);
1259 att = (wc_mime_attachment*) vAtt;
1260 if ((att != NULL) &&
1261 (strcmp(ChrPtr(WhichAttachment),
1262 ChrPtr(att->FileName) ) == 0))
1264 DeleteEntryFromHash(WCC->attachments, at);
1268 while (NextHashPos(WCC->attachments, at));
1269 FreeStrBuf(&WhichAttachment);
1270 wc_printf("remove_attachment() completed\n");
1274 long FourHash(const char *key, long length)
1278 const unsigned char *ptr = (const unsigned char*)key;
1280 for (i = 0; i < 4; i++, ptr ++)
1300 * display the message entry screen
1302 void display_enter(void)
1307 const StrBuf *display_name = NULL;
1308 int recipient_required = 0;
1309 int subject_required = 0;
1310 int is_anonymous = 0;
1311 wcsession *WCC = WC;
1315 if (havebstr("force_room")) {
1316 gotoroom(sbstr("force_room"));
1319 display_name = sbstr("display_name");
1320 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1321 display_name = NULL;
1326 * First, do we have permission to enter messages in this room at all?
1329 serv_puts("ENT0 0");
1330 StrBuf_ServGetln(Line);
1331 rc = GetServerStatusMsg(Line, &Result, 0, 2);
1333 if (Result == 570) { /* 570 means that we need a recipient here */
1334 recipient_required = 1;
1336 else if (rc != 2) { /* Any other error means that we cannot continue */
1337 rc = GetServerStatusMsg(Line, &Result, 0, 2);
1338 readloop(readnew, eUseDefault);
1343 /* Is the server strongly recommending that the user enter a message subject? */
1344 if (StrLength(Line) > 4) {
1345 subject_required = extract_int(ChrPtr(Line) + 4, 1);
1349 * Are we perhaps in an address book view? If so, then an "enter
1350 * message" command really means "add new entry."
1352 if (WCC->CurRoom.defview == VIEW_ADDRESSBOOK) {
1353 do_edit_vcard(-1, "", NULL, NULL, "", ChrPtr(WCC->CurRoom.name));
1359 * Are we perhaps in a calendar room? If so, then an "enter
1360 * message" command really means "add new calendar item."
1362 if (WCC->CurRoom.defview == VIEW_CALENDAR) {
1363 display_edit_event();
1369 * Are we perhaps in a tasks view? If so, then an "enter
1370 * message" command really means "add new task."
1372 if (WCC->CurRoom.defview == VIEW_TASKS) {
1373 display_edit_task();
1380 * If the "replying_to" variable is set, it refers to a message
1381 * number from which we must extract some header fields...
1383 replying_to = lbstr("replying_to");
1384 if (replying_to > 0) {
1386 StrBuf *wefw = NULL;
1387 StrBuf *msgn = NULL;
1388 StrBuf *from = NULL;
1389 StrBuf *node = NULL;
1390 StrBuf *rfca = NULL;
1391 StrBuf *rcpt = NULL;
1392 StrBuf *cccc = NULL;
1393 serv_printf("MSG0 %ld|1", replying_to);
1395 StrBuf_ServGetln(Line);
1396 if (GetServerStatusMsg(Line, NULL, 0, 0) == 1)
1397 while (len = StrBuf_ServGetln(Line),
1400 strcmp(ChrPtr(Line), "000")))
1403 if ((StrLength(Line) > 4) &&
1404 (ChrPtr(Line)[4] == '='))
1405 which = FourHash(ChrPtr(Line), 4);
1407 if (which == l_subj)
1409 StrBuf *subj = NewStrBuf();
1410 StrBuf *FlatSubject;
1412 if (!strcasecmp(bstr("replying_mode"), "forward")) {
1413 if (strncasecmp(ChrPtr(Line) + 5, "Fw:", 3)) {
1414 StrBufAppendBufPlain(subj, HKEY("Fw: "), 0);
1418 if (strncasecmp(ChrPtr(Line) + 5, "Re:", 3)) {
1419 StrBufAppendBufPlain(subj, HKEY("Re: "), 0);
1422 StrBufAppendBufPlain(subj,
1424 StrLength(Line) - 5, 0);
1425 FlatSubject = NewStrBufPlain(NULL, StrLength(subj));
1426 StrBuf_RFC822_to_Utf8(FlatSubject, subj, NULL, NULL);
1428 PutBstr(HKEY("subject"), FlatSubject);
1431 else if (which == l_wefw)
1436 wefw = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1438 /* Trim down excessively long lists of thread references. We eliminate the
1439 * second one in the list so that the thread root remains intact.
1441 rrtok = num_tokens(ChrPtr(wefw), '|');
1442 rrlen = StrLength(wefw);
1443 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
1444 StrBufRemove_token(wefw, 1, '|');
1448 else if (which == l_msgn) {
1449 msgn = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1452 else if (which == l_from) {
1454 from = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1455 FlatFrom = NewStrBufPlain(NULL, StrLength(from));
1456 StrBuf_RFC822_to_Utf8(FlatFrom, from, NULL, NULL);
1459 for (i=0; i<StrLength(from); ++i) {
1460 if (ChrPtr(from)[i] == ',')
1461 StrBufPeek(from, NULL, i, ' ');
1465 else if (which == l_rcpt) {
1466 rcpt = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1469 else if (which == l_cccc) {
1470 cccc = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1473 else if (which == l_node) {
1474 node = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1477 else if (which == l_rfca) {
1479 rfca = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1480 FlatRFCA = NewStrBufPlain(NULL, StrLength(rfca));
1481 StrBuf_RFC822_to_Utf8(FlatRFCA, rfca, NULL, NULL);
1488 if (StrLength(wefw) + StrLength(msgn) > 0) {
1489 StrBuf *refs = NewStrBuf();
1490 if (StrLength(wefw) > 0) {
1491 StrBufAppendBuf(refs, wefw, 0);
1493 if ( (StrLength(wefw) > 0) &&
1494 (StrLength(msgn) > 0) )
1496 StrBufAppendBufPlain(refs, HKEY("|"), 0);
1498 if (StrLength(msgn) > 0) {
1499 StrBufAppendBuf(refs, msgn, 0);
1501 PutBstr(HKEY("references"), refs);
1505 * If this is a Reply or a ReplyAll, copy the sender's email into the To: field
1507 if ( (!strcasecmp(bstr("replying_mode"), "reply"))
1508 || (!strcasecmp(bstr("replying_mode"), "replyall"))
1511 if (StrLength(rfca) > 0) {
1512 to_rcpt = NewStrBuf();
1513 StrBufAppendBuf(to_rcpt, from, 0);
1514 StrBufAppendBufPlain(to_rcpt, HKEY(" <"), 0);
1515 StrBufAppendBuf(to_rcpt, rfca, 0);
1516 StrBufAppendBufPlain(to_rcpt, HKEY(">"), 0);
1521 if ( (StrLength(node) > 0)
1522 && (strcasecmp(ChrPtr(node), ChrPtr(WC->serv_info->serv_nodename)))
1524 StrBufAppendBufPlain(to_rcpt, HKEY(" @ "), 0);
1525 StrBufAppendBuf(to_rcpt, node, 0);
1528 PutBstr(HKEY("recp"), to_rcpt);
1532 * Only if this is a ReplyAll, copy all recipients into the Cc: field
1534 if ( (!strcasecmp(bstr("replying_mode"), "replyall"))
1536 StrBuf *cc_rcpt = rcpt;
1538 if (StrLength(cccc) > 0) {
1539 if (cc_rcpt != NULL) {
1540 StrBufAppendPrintf(cc_rcpt, ", ");
1541 StrBufAppendBuf(cc_rcpt, cccc, 0);
1547 if (cc_rcpt != NULL)
1548 PutBstr(HKEY("cc"), cc_rcpt);
1560 * Otherwise proceed normally.
1561 * Do a custom room banner with no navbar...
1564 if (recipient_required) {
1565 const StrBuf *Recp = NULL;
1566 const StrBuf *Cc = NULL;
1567 const StrBuf *Bcc = NULL;
1568 const StrBuf *Wikipage = NULL;
1569 StrBuf *CmdBuf = NULL;
1570 const char CMD[] = "ENT0 0|%s|%d|0||%s||%s|%s|%s";
1572 Recp = sbstr("recp");
1575 Wikipage = sbstr("page");
1577 CmdBuf = NewStrBufPlain(NULL,
1580 StrLength(display_name) +
1583 StrLength(Wikipage));
1585 StrBufPrintf(CmdBuf,
1589 ChrPtr(display_name),
1593 serv_puts(ChrPtr(CmdBuf));
1594 StrBuf_ServGetln(CmdBuf);
1596 rc = GetServerStatusMsg(CmdBuf, &Result, 0, 0);
1598 if ( (Result == 570) /* invalid or missing recipient(s) */
1599 || (Result == 550) /* higher access required to send Internet mail */
1601 /* These errors will have been displayed and are excusable */
1603 else if (rc != 2) { /* Any other error means that we cannot continue */
1604 AppendImportantMessage(ChrPtr(CmdBuf) + 4, StrLength(CmdBuf) - 4);
1605 FreeStrBuf(&CmdBuf);
1606 readloop(readnew, eUseDefault);
1609 FreeStrBuf(&CmdBuf);
1611 if (recipient_required)
1612 PutBstr(HKEY("__RCPTREQUIRED"), NewStrBufPlain(HKEY("1")));
1613 if (recipient_required || subject_required)
1614 PutBstr(HKEY("__SUBJREQUIRED"), NewStrBufPlain(HKEY("1")));
1617 output_headers(1, 0, 0, 0, 1, 0);
1618 DoTemplate(HKEY("edit_message"), NULL, &NoCtx);
1627 void delete_msg(void)
1632 msgid = lbstr("msgid");
1634 if ((WC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
1635 serv_printf("DELE %ld", msgid);
1637 else { /* Otherwise move it to Trash */
1638 serv_printf("MOVE %ld|_TRASH_|0", msgid);
1641 StrBuf_ServGetln(Line);
1642 GetServerStatusMsg(Line, NULL, 1, 0);
1644 readloop(readnew, eUseDefault);
1649 * move a message to another room
1655 msgid = lbstr("msgid");
1657 if (havebstr("move_button")) {
1659 serv_printf("MOVE %ld|%s", msgid, bstr("target_room"));
1661 StrBuf_ServGetln(Line);
1662 GetServerStatusMsg(Line, NULL, 1, 0);
1665 AppendImportantMessage(_("The message was not moved."), -1);
1668 readloop(readnew, eUseDefault);
1674 * Generic function to output an arbitrary MIME attachment from
1675 * message being composed
1677 * partnum The MIME part to be output
1678 * filename Fake filename to give
1679 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1681 void postpart(StrBuf *partnum, StrBuf *filename, int force_download)
1684 StrBuf *content_type;
1685 wc_mime_attachment *part;
1688 i = StrToi(partnum);
1689 if (GetHash(WC->attachments, IKEY(i), &vPart) &&
1691 part = (wc_mime_attachment*) vPart;
1692 if (force_download) {
1693 content_type = NewStrBufPlain(HKEY("application/octet-stream"));
1696 content_type = NewStrBufDup(part->ContentType);
1698 StrBufAppendBuf(WC->WBuf, part->Data, 0);
1699 http_transmit_thing(ChrPtr(content_type), 0);
1701 hprintf("HTTP/1.1 404 %s\n", ChrPtr(partnum));
1702 output_headers(0, 0, 0, 0, 0, 0);
1703 hprintf("Content-Type: text/plain\r\n");
1705 wc_printf(_("An error occurred while retrieving this part: %s/%s\n"),
1706 ChrPtr(partnum), ChrPtr(filename));
1709 FreeStrBuf(&content_type);
1714 * Generic function to output an arbitrary MIME part from an arbitrary
1715 * message number on the server.
1717 * msgnum Number of the item on the citadel server
1718 * partnum The MIME part to be output
1719 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1721 void mimepart(int force_download)
1725 wcsession *WCC = WC;
1728 StrBuf *ContentType = NewStrBufPlain(HKEY("application/octet-stream"));
1731 att = Buf = NewStrBuf();
1732 msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
1733 StrBufExtract_token(att, WCC->Hdr->HR.ReqLine, 1, '/');
1735 serv_printf("OPNA %ld|%s", msgnum, ChrPtr(att));
1736 StrBuf_ServGetln(Buf);
1737 if (GetServerStatus(Buf, NULL) == 2) {
1738 StrBufCutLeft(Buf, 4);
1739 bytes = StrBufExtract_long(Buf, 0, '|');
1740 if (!force_download) {
1741 StrBufExtract_token(ContentType, Buf, 3, '|');
1744 serv_read_binary(WCC->WBuf, bytes, Buf);
1746 StrBuf_ServGetln(Buf);
1747 CT = ChrPtr(ContentType);
1749 if (!force_download) {
1750 if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
1751 StrBufExtract_token(Buf, WCC->Hdr->HR.ReqLine, 2, '/');
1752 CT = GuessMimeByFilename(SKEY(Buf));
1754 if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
1755 CT = GuessMimeType(SKEY(WCC->WBuf));
1758 http_transmit_thing(CT, 0);
1760 StrBufCutLeft(Buf, 4);
1761 hprintf("HTTP/1.1 404 %s\n", ChrPtr(Buf));
1762 output_headers(0, 0, 0, 0, 0, 0);
1763 hprintf("Content-Type: text/plain\r\n");
1765 wc_printf(_("An error occurred while retrieving this part: %s\n"),
1769 FreeStrBuf(&ContentType);
1775 * Read any MIME part of a message, from the server, into memory.
1777 StrBuf *load_mimepart(long msgnum, char *partnum)
1783 serv_printf("DLAT %ld|%s", msgnum, partnum);
1784 StrBuf_ServGetln(Buf);
1785 if (GetServerStatus(Buf, NULL) == 6) {
1786 StrBufCutLeft(Buf, 4);
1787 bytes = StrBufExtract_long(Buf, 0, '|');
1790 StrBuf_ServGetBLOBBuffered(Buf, bytes);
1800 * Read any MIME part of a message, from the server, into memory.
1802 void MimeLoadData(wc_mime_attachment *Mime)
1807 /* TODO: is there a chance the content type is different from the one we know? */
1809 serv_printf("DLAT %ld|%s", Mime->msgnum, ChrPtr(Mime->PartNum));
1811 StrBuf_ServGetln(Buf);
1812 if (GetServerStatus(Buf, NULL) == 6) {
1813 Ptr = &(ChrPtr(Buf)[4]);
1814 bytes = StrBufExtractNext_long(Buf, &Ptr, '|');
1815 StrBufSkip_NTokenS(Buf, &Ptr, '|', 3); /* filename, cbtype, mimetype */
1816 if (Mime->Charset == NULL) Mime->Charset = NewStrBuf();
1817 StrBufExtract_NextToken(Mime->Charset, Buf, &Ptr, '|');
1819 if (Mime->Data == NULL)
1820 Mime->Data = NewStrBufPlain(NULL, bytes);
1821 StrBuf_ServGetBLOBBuffered(Mime->Data, bytes);
1824 FlushStrBuf(Mime->Data);
1825 /* TODO XImportant message */
1831 void view_mimepart(void) {
1835 void download_mimepart(void) {
1839 void view_postpart(void) {
1840 StrBuf *filename = NewStrBuf();
1841 StrBuf *partnum = NewStrBuf();
1843 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1844 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1846 postpart(partnum, filename, 0);
1848 FreeStrBuf(&filename);
1849 FreeStrBuf(&partnum);
1852 void download_postpart(void) {
1853 StrBuf *filename = NewStrBuf();
1854 StrBuf *partnum = NewStrBuf();
1856 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1857 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1859 postpart(partnum, filename, 1);
1861 FreeStrBuf(&filename);
1862 FreeStrBuf(&partnum);
1867 void show_num_attachments(void) {
1868 wc_printf("%d", GetCount(WC->attachments));
1872 void h_readnew(void) { readloop(readnew, eUseDefault);}
1873 void h_readold(void) { readloop(readold, eUseDefault);}
1874 void h_readfwd(void) { readloop(readfwd, eUseDefault);}
1875 void h_headers(void) { readloop(headers, eUseDefault);}
1876 void h_do_search(void) { readloop(do_search, eUseDefault);}
1877 void h_readgt(void) { readloop(readgt, eUseDefault);}
1878 void h_readlt(void) { readloop(readlt, eUseDefault);}
1880 void jsonMessageListHdr(void)
1882 /* TODO: make a generic function */
1883 hprintf("HTTP/1.1 200 OK\r\n");
1884 hprintf("Content-type: application/json; charset=utf-8\r\n");
1885 hprintf("Server: %s / %s\r\n", PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software));
1886 hprintf("Connection: close\r\n");
1887 hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
1892 /* Output message list in JSON format */
1893 void jsonMessageList(void) {
1894 const StrBuf *room = sbstr("room");
1895 long oper = (havebstr("query")) ? do_search : readnew;
1898 readloop(oper, eUseDefault);
1902 void RegisterReadLoopHandlerset(
1904 GetParamsGetServerCall_func GetParamsGetServerCall,
1905 PrintViewHeader_func PrintViewHeader,
1906 load_msg_ptrs_detailheaders LH,
1907 LoadMsgFromServer_func LoadMsgFromServer,
1908 RenderView_or_Tail_func RenderView_or_Tail,
1909 View_Cleanup_func ViewCleanup
1912 RoomRenderer *Handler;
1914 Handler = (RoomRenderer*) malloc(sizeof(RoomRenderer));
1916 Handler->RoomType = RoomType;
1917 Handler->GetParamsGetServerCall = GetParamsGetServerCall;
1918 Handler->PrintViewHeader = PrintViewHeader;
1919 Handler->LoadMsgFromServer = LoadMsgFromServer;
1920 Handler->RenderView_or_Tail = RenderView_or_Tail;
1921 Handler->ViewCleanup = ViewCleanup;
1922 Handler->LHParse = LH;
1924 Put(ReadLoopHandler, IKEY(RoomType), Handler, NULL);
1931 RegisterPreference("use_sig",
1932 _("Attach signature to email messages?"),
1935 RegisterPreference("signature", _("Use this signature:"), PRF_QP_STRING, NULL);
1936 RegisterPreference("default_header_charset",
1937 _("Default character set for email headers:"),
1940 RegisterPreference("defaultfrom", _("Preferred email address"), PRF_STRING, NULL);
1941 RegisterPreference("defaultname",
1942 _("Preferred display name for email messages"),
1945 RegisterPreference("defaulthandle",
1946 _("Preferred display name for bulletin board posts"),
1949 RegisterPreference("mailbox",_("Mailbox view mode"), PRF_STRING, NULL);
1951 WebcitAddUrlHandler(HKEY("readnew"), "", 0, h_readnew, ANONYMOUS|NEED_URL);
1952 WebcitAddUrlHandler(HKEY("readold"), "", 0, h_readold, ANONYMOUS|NEED_URL);
1953 WebcitAddUrlHandler(HKEY("readfwd"), "", 0, h_readfwd, ANONYMOUS|NEED_URL);
1954 WebcitAddUrlHandler(HKEY("headers"), "", 0, h_headers, NEED_URL);
1955 WebcitAddUrlHandler(HKEY("readgt"), "", 0, h_readgt, ANONYMOUS|NEED_URL);
1956 WebcitAddUrlHandler(HKEY("readlt"), "", 0, h_readlt, ANONYMOUS|NEED_URL);
1957 WebcitAddUrlHandler(HKEY("do_search"), "", 0, h_do_search, 0);
1958 WebcitAddUrlHandler(HKEY("display_enter"), "", 0, display_enter, 0);
1959 WebcitAddUrlHandler(HKEY("post"), "", 0, post_message, PROHIBIT_STARTPAGE);
1960 WebcitAddUrlHandler(HKEY("move_msg"), "", 0, move_msg, PROHIBIT_STARTPAGE);
1961 WebcitAddUrlHandler(HKEY("delete_msg"), "", 0, delete_msg, PROHIBIT_STARTPAGE);
1962 WebcitAddUrlHandler(HKEY("msg"), "", 0, embed_message, NEED_URL);
1963 WebcitAddUrlHandler(HKEY("message"), "", 0, handle_one_message, NEED_URL|XHTTP_COMMANDS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
1964 WebcitAddUrlHandler(HKEY("printmsg"), "", 0, print_message, NEED_URL);
1965 WebcitAddUrlHandler(HKEY("msgheaders"), "", 0, display_headers, NEED_URL);
1967 WebcitAddUrlHandler(HKEY("mimepart"), "", 0, view_mimepart, NEED_URL);
1968 WebcitAddUrlHandler(HKEY("mimepart_download"), "", 0, download_mimepart, NEED_URL);
1969 WebcitAddUrlHandler(HKEY("postpart"), "", 0, view_postpart, NEED_URL|PROHIBIT_STARTPAGE);
1970 WebcitAddUrlHandler(HKEY("postpart_download"), "", 0, download_postpart, NEED_URL|PROHIBIT_STARTPAGE);
1971 WebcitAddUrlHandler(HKEY("upload_attachment"), "", 0, upload_attachment, AJAX);
1972 WebcitAddUrlHandler(HKEY("remove_attachment"), "", 0, remove_attachment, AJAX);
1973 WebcitAddUrlHandler(HKEY("show_num_attachments"), "", 0, show_num_attachments, AJAX);
1976 WebcitAddUrlHandler(HKEY("roommsgs"), "", 0, jsonMessageList,0);
1978 l_subj = FourHash("subj", 4);
1979 l_wefw = FourHash("wefw", 4);
1980 l_msgn = FourHash("msgn", 4);
1981 l_from = FourHash("from", 4);
1982 l_rcpt = FourHash("rcpt", 4);
1983 l_cccc = FourHash("cccc", 4);
1984 l_node = FourHash("node", 4);
1985 l_rfca = FourHash("rfca", 4);
1991 SessionDetachModule_MSG
1994 DeleteHash(&sess->summ);