2 * Functions which deal with the fetching and displaying of messages.
4 * Copyright (c) 1996-2018 by the citadel.org team
6 * This program is open source software. You can redistribute it and/or
7 * modify it under the terms of the GNU General Public License, version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include "webserver.h"
20 HashList *MimeRenderHandler = NULL;
21 HashList *ReadLoopHandler = NULL;
22 int dbg_analyze_msg = 0;
24 #define SUBJ_COL_WIDTH_PCT 50 /* Mailbox view column width */
25 #define SENDER_COL_WIDTH_PCT 30 /* Mailbox view column width */
26 #define DATE_PLUS_BUTTONS_WIDTH_PCT 20 /* Mailbox view column width */
28 void jsonMessageListHdr(void);
30 void display_enter(void);
34 /* workaround for json listview; its not useable directly */
35 if (WC->CurRoom.view == VIEW_JSON_LIST) {
36 StrBuf *View = NewStrBuf();
37 StrBufPrintf(View, "%d", VIEW_MAILBOX);
38 putbstr("view", View);;
41 int load_message(message_summary *Msg,
53 if (Msg->PartNum != NULL) {
54 serv_printf("MSG4 %ld|%s", Msg->msgnum, ChrPtr(Msg->PartNum));
57 serv_printf("MSG4 %ld", Msg->msgnum);
60 StrBuf_ServGetln(Buf);
61 if (GetServerStatus(Buf, NULL) != 1) {
63 StrBufAppendPrintf(*Error, "<strong>");
64 StrBufAppendPrintf(*Error, _("ERROR:"));
65 StrBufAppendPrintf(*Error, "</strong> %s<br>\n", &buf[4]);
70 /* begin everythingamundo table */
71 HdrToken = NewStrBuf();
72 while (!Done && StrBuf_ServGetln(Buf)>=0) {
73 if ( (StrLength(Buf)==3) &&
74 !strcmp(ChrPtr(Buf), "000"))
78 if (Msg->MsgBody->Data == NULL)
79 Msg->MsgBody->Data = NewStrBuf();
80 Msg->MsgBody->ContentType = NewStrBufPlain(HKEY("text/html"));
81 StrBufAppendPrintf(Msg->MsgBody->Data, "<div><i>");
82 StrBufAppendPrintf(Msg->MsgBody->Data, _("Empty message"));
83 StrBufAppendPrintf(Msg->MsgBody->Data, "</i><br><br>\n");
84 StrBufAppendPrintf(Msg->MsgBody->Data, "</div>\n");
89 case 0:/* Citadel Message Headers */
90 if (StrLength(Buf) == 0) {
94 StrBufExtract_token(HdrToken, Buf, 0, '=');
95 StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
97 /* look up one of the examine_* functions to parse the content */
98 rc = EvaluateMsgHdr(SKEY(HdrToken), Msg, Buf, FoundCharset);
103 else LogError(Target,
105 "don't know how to handle message header[%s]\n",
109 case 1:/* Message Mime Header */
110 if (StrLength(Buf) == 0) {
112 if (Msg->MsgBody->ContentType == NULL)
113 /* end of header or no header? */
114 Msg->MsgBody->ContentType = NewStrBufPlain(HKEY("text/plain"));
115 /* usual end of mime header */
119 StrBufExtract_token(HdrToken, Buf, 0, ':');
120 if (StrLength(HdrToken) > 0) {
121 StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
122 /* the examine*'s know how to do with mime headers too... */
123 EvaluateMsgHdr(SKEY(HdrToken), Msg, Buf, FoundCharset);
128 case 2: /* Message Body */
130 if (Msg->MsgBody->size_known > 0) {
131 StrBuf_ServGetBLOBBuffered(Msg->MsgBody->Data, Msg->MsgBody->length);
133 /*/ todo: check next line, if not 000, append following lines */
136 if (StrLength(Msg->MsgBody->Data) > 0)
137 StrBufAppendBufPlain(Msg->MsgBody->Data, "\n", 1, 0);
138 StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
142 StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
147 if (Msg->AllAttach == NULL)
148 Msg->AllAttach = NewHash(1,NULL);
149 /* now we put the body mimepart we read above into the mimelist */
150 Put(Msg->AllAttach, SKEY(Msg->MsgBody->PartNum), Msg->MsgBody, DestroyMime);
153 FreeStrBuf(&HdrToken);
160 * I wanna SEE that message!
162 * msgnum Message number to display
163 * printable_view Nonzero to display a printable view
164 * section Optional for encapsulated message/rfc822 submessage
166 int read_message(StrBuf *Target, const char *tmpl, long tmpllen, long msgnum, const StrBuf *PartNum, const StrBuf **OutMime, WCTemplputParams *TP)
169 StrBuf *FoundCharset;
172 message_summary *Msg = NULL;
176 WCTemplputParams SuperTP;
177 WCTemplputParams SubTP;
178 StrBuf *Error = NULL;
180 memset(&SuperTP, 0, sizeof(WCTemplputParams));
181 memset(&SubTP, 0, sizeof(WCTemplputParams));
184 FoundCharset = NewStrBuf();
185 Msg = (message_summary *)malloc(sizeof(message_summary));
186 memset(Msg, 0, sizeof(message_summary));
187 Msg->msgnum = msgnum;
188 Msg->PartNum = PartNum;
189 Msg->MsgBody = (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
190 memset(Msg->MsgBody, 0, sizeof(wc_mime_attachment));
191 Msg->MsgBody->msgnum = msgnum;
193 if (!load_message(Msg, FoundCharset, &Error)) {
194 StrBufAppendBuf(Target, Error, 0);
198 /* Extract just the content-type (omit attributes such as "charset") */
199 StrBufExtract_token(Buf, Msg->MsgBody->ContentType, 0, ';');
201 StrBufLowerCase(Buf);
203 StackContext(TP, &SuperTP, Msg, CTX_MAILSUM, 0, NULL);
205 /* Locate a renderer capable of converting this MIME part into HTML */
206 if (GetHash(MimeRenderHandler, SKEY(Buf), &vHdr) &&
208 RenderMimeFuncStruct *Render;
210 StackContext(&SuperTP, &SubTP, Msg->MsgBody, CTX_MIME_ATACH, 0, NULL);
212 Render = (RenderMimeFuncStruct*)vHdr;
213 Render->f(Target, &SubTP, FoundCharset);
215 UnStackContext(&SubTP);
218 if (StrLength(Msg->reply_references)> 0) {
219 /* Trim down excessively long lists of thread references. We eliminate the
220 * second one in the list so that the thread root remains intact.
222 int rrtok = num_tokens(ChrPtr(Msg->reply_references), '|');
223 int rrlen = StrLength(Msg->reply_references);
224 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
225 StrBufRemove_token(Msg->reply_references, 1, '|');
229 /* now check if we need to translate some mimeparts, and remove the duplicate */
230 it = GetNewHashPos(Msg->AllAttach, 0);
231 while (GetNextHashPos(Msg->AllAttach, it, &len, &Key, &vMime) &&
233 StackContext(&SuperTP, &SubTP, vMime, CTX_MIME_ATACH, 0, NULL);
235 evaluate_mime_part(Target, &SubTP);
237 UnStackContext(&SubTP);
240 *OutMime = DoTemplate(tmpl, tmpllen, Target, &SuperTP);
242 UnStackContext(&SuperTP);
244 DestroyMessageSummary(Msg);
245 FreeStrBuf(&FoundCharset);
252 HttpStatus(long CitadelStatus)
254 long httpstatus = 502;
256 switch (MAJORCODE(CitadelStatus))
258 case LISTING_FOLLOWS:
263 switch (MINORCODE(CitadelStatus))
271 case HIGHER_ACCESS_REQUIRED:
272 case MAX_SESSIONS_EXCEEDED:
274 case RESOURCE_NOT_OPEN:
276 case INVALID_FLOOR_OPERATION:
282 case MESSAGE_NOT_FOUND:
292 case CMD_NOT_SUPPORTED:
293 case PASSWORD_REQUIRED:
294 case ALREADY_LOGGED_IN:
295 case USERNAME_REQUIRED:
297 case SERVER_SHUTTING_DOWN:
308 case START_CHAT_MODE:
312 httpstatus = 502; /* aeh... whut? */
320 * Unadorned HTML output of an individual message, suitable
321 * for placing in a hidden iframe, for printing, or whatever
323 void handle_one_message(void)
325 long CitStatus = ERROR + NOT_HERE;
327 const StrBuf *Destination;
333 StrBuf *CmdBuf = NULL;
337 pMsg = strchr(ChrPtr(WCC->Hdr->HR.ReqLine), '/');
339 HttpStatus(CitStatus);
343 msgnum = atol(pMsg + 1);
344 StrBufCutAt(WCC->Hdr->HR.ReqLine, 0, pMsg);
345 gotoroom(WCC->Hdr->HR.ReqLine);
346 switch (WCC->Hdr->HR.eReqType)
350 Tmpl = sbstr("template");
351 if (StrLength(Tmpl) > 0)
352 read_message(WCC->WBuf, SKEY(Tmpl), msgnum, NULL, &Mime, NULL);
354 read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime, NULL);
355 http_transmit_thing(ChrPtr(Mime), 0);
358 CmdBuf = NewStrBuf ();
359 if ((WCC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
360 serv_printf("DELE %ld", msgnum);
362 else { /* Otherwise move it to Trash */
363 serv_printf("MOVE %ld|_TRASH_|0", msgnum);
365 StrBuf_ServGetln(CmdBuf);
366 GetServerStatusMsg(CmdBuf, &CitStatus, 1, 0);
367 HttpStatus(CitStatus);
372 if (GetHash(WCC->Hdr->HTTPHeaders, HKEY("DESTINATION"), &vLine) &&
374 Destination = (StrBuf*) vLine;
375 serv_printf("MOVE %ld|%s|%d", msgnum, ChrPtr(Destination), CopyMessage);
376 StrBuf_ServGetln(CmdBuf);
377 GetServerStatusMsg(CmdBuf, NULL, 1, 0);
378 GetServerStatus(CmdBuf, &CitStatus);
379 HttpStatus(CitStatus);
392 * Unadorned HTML output of an individual message, suitable
393 * for placing in a hidden iframe, for printing, or whatever
395 void embed_message(void) {
400 StrBuf *CmdBuf = NULL;
402 msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
403 if (msgnum <= 0) return;
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, NULL);
413 read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime, NULL);
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 GetServerStatusMsg(CmdBuf, NULL, 1, 0);
435 * Printable view of a message
437 void print_message(void) {
441 msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
442 output_headers(0, 0, 0, 0, 0, 0);
444 hprintf("Content-type: text/html\r\n"
445 "Server: " PACKAGE_STRING "\r\n"
446 "Connection: close\r\n");
450 read_message(WC->WBuf, HKEY("view_message_print"), msgnum, NULL, &Mime, NULL);
456 * Display a message's headers
458 void display_headers(void) {
462 msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
463 output_headers(0, 0, 0, 0, 0, 0);
465 hprintf("Content-type: text/plain\r\n"
467 "Connection: close\r\n",
471 serv_printf("MSG2 %ld|1", msgnum);
472 serv_getln(buf, sizeof buf);
474 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
475 wc_printf("%s\n", buf);
485 * load message pointers from the server for a "read messages" operation
487 * servcmd: the citadel command to send to the citserver
489 int load_msg_ptrs(const char *servcmd,
491 StrBuf *FoundCharset,
492 SharedMessageStatus *Stat,
494 load_msg_ptrs_detailheaders LH,
495 StrBuf *FetchMessageList,
496 eMessageField *MessageFieldList,
500 message_summary *Msg;
505 const char *Ptr = NULL;
508 Stat->lowest_found = LONG_MAX;
509 Stat->highest_found = LONG_MIN;
511 if (WCC->summ != NULL) {
512 DeleteHash(&WCC->summ);
514 WCC->summ = NewHash(1, Flathash);
518 StrBuf_ServGetln(Buf);
519 StatMajor = GetServerStatus(Buf, NULL);
524 if (filter != NULL) {
529 else if (FetchMessageList != NULL) {
530 serv_putbuf(FetchMessageList);
533 /* fall back to empty filter in case of we were fooled... */
539 return (Stat->nummsgs);
542 while (len = StrBuf_ServGetln(Buf),
545 strcmp(ChrPtr(Buf), "000")!= 0)))
547 if (Stat->nummsgs < Stat->maxload) {
550 Msg = (message_summary*)malloc(sizeof(message_summary));
551 memset(Msg, 0, sizeof(message_summary));
553 Msg->msgnum = StrBufExtractNext_long(Buf, &Ptr, '|');
554 Msg->date = StrBufExtractNext_long(Buf, &Ptr, '|');
555 if (MessageFieldList != NULL) {
557 for (i = 0; i < HeaderCount; i++) {
558 StrBufExtract_NextToken(Buf2, Buf, &Ptr, '|');
559 if (StrLength(Buf2) > 0) {
560 EvaluateMsgHdrEnum(MessageFieldList[i], Msg, Buf2, FoundCharset);
565 if (Stat->nummsgs == 0) {
566 if (Msg->msgnum < Stat->lowest_found) {
567 Stat->lowest_found = Msg->msgnum;
569 if (Msg->msgnum > Stat->highest_found) {
570 Stat->highest_found = Msg->msgnum;
574 if ((Msg->msgnum == 0) && (StrLength(Buf) < 32)) {
580 * as citserver probably gives us messages in forward date sorting
581 * nummsgs should be the same order as the message date.
583 if (Msg->date == 0) {
584 Msg->date = Stat->nummsgs;
585 if (StrLength(Buf) < 32)
588 if ((!skipit) && (LH != NULL)) {
589 if (!LH(Buf, &Ptr, Msg, Buf2, ViewSpecific)){
595 Put(WCC->summ, (const char *)&n, sizeof(n), Msg, DestroyMessageSummary);
601 return (Stat->nummsgs);
607 * \brief sets FlagToSet for each of ScanMe that appears in MatchMSet
608 * \param ScanMe List of BasicMsgStruct to be searched it MatchSet
609 * \param MatchMSet MSet we want to flag
610 * \param FlagToSet Flag to set on each BasicMsgStruct->Flags if in MSet
612 long SetFlagsFromMSet(HashList *ScanMe, MSet *MatchMSet, int FlagToSet, int Reverse)
619 message_summary *Msg;
621 at = GetNewHashPos(ScanMe, 0);
622 while (GetNextHashPos(ScanMe, at, &HKLen, &HashKey, &vMsg)) {
623 /* Are you a new message, or an old message? */
624 Msg = (message_summary*) vMsg;
625 if (Reverse && IsInMSetList(MatchMSet, Msg->msgnum)) {
626 Msg->Flags = Msg->Flags | FlagToSet;
629 else if (!Reverse && !IsInMSetList(MatchMSet, Msg->msgnum)) {
630 Msg->Flags = Msg->Flags | FlagToSet;
639 long load_seen_flags(void)
646 OldMsg = NewStrBuf();
648 StrBuf_ServGetln(OldMsg);
649 if (GetServerStatus(OldMsg, NULL) == 2) {
650 StrBufCutLeft(OldMsg, 4);
657 if (ParseMSet(&MatchMSet, OldMsg))
659 count = SetFlagsFromMSet(WCC->summ, MatchMSet, MSGFLAG_READ, 0);
661 DeleteMSet(&MatchMSet);
666 extern readloop_struct rlid[];
668 typedef struct _RoomRenderer{
671 GetParamsGetServerCall_func GetParamsGetServerCall;
673 PrintViewHeader_func PrintPageHeader;
674 PrintViewHeader_func PrintViewHeader;
675 LoadMsgFromServer_func LoadMsgFromServer;
676 RenderView_or_Tail_func RenderView_or_Tail;
677 View_Cleanup_func ViewCleanup;
678 load_msg_ptrs_detailheaders LHParse;
680 StrBuf *FetchMessageList;
681 eMessageField *MessageFieldList;
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;
697 char filter[256] = "";
703 WCTemplputParams SubTP;
704 SharedMessageStatus Stat;
705 void *ViewSpecific = NULL;
707 if (havebstr("is_summary") && (1 == (ibstr("is_summary")))) {
708 WCC->CurRoom.view = VIEW_MAILBOX;
711 if (havebstr("view")) {
712 WCC->CurRoom.view = ibstr("view");
715 memset(&Stat, 0, sizeof(SharedMessageStatus));
716 Stat.maxload = 10000;
717 Stat.lowest_found = (-1);
718 Stat.highest_found = (-1);
719 if (ForceRenderer == eUseDefault)
720 GetHash(ReadLoopHandler, IKEY(WCC->CurRoom.view), &vViewMsg);
722 GetHash(ReadLoopHandler, IKEY(ForceRenderer), &vViewMsg);
723 if (vViewMsg == NULL) {
724 WCC->CurRoom.view = VIEW_BBS;
725 GetHash(ReadLoopHandler, IKEY(WCC->CurRoom.view), &vViewMsg);
727 if (vViewMsg == NULL) {
728 return; /* TODO: print message */
731 ViewMsg = (RoomRenderer*) vViewMsg;
732 if (ViewMsg->PrintPageHeader == NULL)
733 output_headers(1, 1, 1, 0, 0, 0);
735 ViewMsg->PrintPageHeader(&Stat, ViewSpecific);
737 if (ViewMsg->GetParamsGetServerCall != NULL) {
738 r = ViewMsg->GetParamsGetServerCall(
743 filter, sizeof(filter)
755 case 300: /* the callback hook should do the work for us here, since he knows what to do. */
761 if (!IsEmptyStr(cmd)) {
762 const char *p = NULL;
763 StrBuf *FoundCharset = NULL;
764 if (!IsEmptyStr(filter))
766 if (ViewMsg->HeaderCount > 0) {
767 FoundCharset = NewStrBuf();
769 Stat.nummsgs = load_msg_ptrs(cmd, p,
774 ViewMsg->FetchMessageList,
775 ViewMsg->MessageFieldList,
776 ViewMsg->HeaderCount);
777 FreeStrBuf(&FoundCharset);
782 StackContext(NULL, &SubTP, NULL, CTX_MAILSUM, 0, NULL);
784 SortIt = RetrieveSort(&SubTP,
787 Stat.defaultsortorder);
789 UnStackContext(&SubTP);
791 SortByPayload(WCC->summ, SortIt);
793 if (Stat.startmsg < 0) {
797 if (Stat.load_seen) Stat.numNewmsgs = load_seen_flags();
800 * Print any inforation above the message list...
802 if (ViewMsg->PrintViewHeader != NULL)
803 ViewMsg->PrintViewHeader(&Stat, &ViewSpecific);
805 WCC->startmsg = Stat.startmsg;
806 WCC->maxmsgs = Stat.maxmsgs;
807 WCC->num_displayed = 0;
809 /* Put some helpful data in vars for mailsummary_json */
814 StrBufPrintf(Foo, "%ld", Stat.nummsgs);
815 PutBstr(HKEY("__READLOOP:TOTALMSGS"), NewStrBufDup(Foo)); /* keep Foo! */
817 StrBufPrintf(Foo, "%ld", Stat.numNewmsgs);
818 PutBstr(HKEY("__READLOOP:NEWMSGS"), NewStrBufDup(Foo)); /* keep Foo! */
820 StrBufPrintf(Foo, "%ld", Stat.startmsg);
821 PutBstr(HKEY("__READLOOP:STARTMSG"), Foo); /* store Foo elsewhere, descope it here. */
825 * iterate over each message. if we need to load an attachment, do it here.
828 if ((ViewMsg->LoadMsgFromServer != NULL) &&
831 at = GetNewHashPos(WCC->summ, 0);
832 Stat.num_displayed = i = 0;
833 while ( GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
834 Msg = (message_summary*) vMsg;
835 if ((Msg->msgnum >= Stat.startmsg) && (Stat.num_displayed <= Stat.maxmsgs)) {
836 ViewMsg->LoadMsgFromServer(&Stat,
839 (Msg->Flags & MSGFLAG_READ) != 0,
848 * Done iterating the message list. now tasks we want to do after.
850 if (ViewMsg->RenderView_or_Tail != NULL)
851 ViewMsg->RenderView_or_Tail(&Stat, &ViewSpecific, oper);
853 if (ViewMsg->ViewCleanup != NULL)
854 ViewMsg->ViewCleanup(&ViewSpecific);
858 if (WCC->summ != NULL) {
859 DeleteHash(&WCC->summ);
865 * Back end for post_message()
866 * ... this is where the actual message gets transmitted to the server.
868 void post_mime_to_server(void) {
870 char top_boundary[SIZ];
871 char alt_boundary[SIZ];
872 int is_multipart = 0;
874 wc_mime_attachment *att;
876 size_t encoded_length;
877 size_t encoded_strlen;
878 char *txtmail = NULL;
879 int include_text_alt = 0; /* Set to nonzero to include multipart/alternative text/plain */
881 sprintf(top_boundary, "Citadel--Multipart--%s--%04x--%04x",
882 ChrPtr(WCC->serv_info->serv_fqdn),
886 sprintf(alt_boundary, "Citadel--Multipart--%s--%04x--%04x",
887 ChrPtr(WCC->serv_info->serv_fqdn),
892 /* RFC2045 requires this, and some clients look for it... */
893 serv_puts("MIME-Version: 1.0");
894 serv_puts("X-Mailer: " PACKAGE_STRING);
896 /* If there are attachments, we have to do multipart/mixed */
897 if (GetCount(WCC->attachments) > 0) {
901 /* Only do multipart/alternative for mailboxes. BBS and Wiki rooms don't need it. */
902 if ((WCC->CurRoom.view == VIEW_MAILBOX) ||
903 (WCC->CurRoom.view == VIEW_JSON_LIST))
905 include_text_alt = 1;
909 /* Remember, serv_printf() appends an extra newline */
910 serv_printf("Content-type: multipart/mixed; boundary=\"%s\"\n", top_boundary);
911 serv_printf("This is a multipart message in MIME format.\n");
912 serv_printf("--%s", top_boundary);
915 /* Remember, serv_printf() appends an extra newline */
916 if (include_text_alt) {
918 serv_printf("Content-type: multipart/alternative; "
919 "boundary=\"%s\"\n", alt_boundary);
920 serv_printf("This is a multipart message in MIME format.\n");
921 serv_printf("--%s", alt_boundary);
923 serv_puts("Content-type: text/plain; charset=utf-8");
924 serv_puts("Content-Transfer-Encoding: quoted-printable");
926 txtmail = html_to_ascii(bstr("msgtext"), 0, 80);
927 Buf = NewStrBufPlain(txtmail, -1);
930 text_to_server_qp(Buf); /* Transmit message in quoted-printable encoding */
932 serv_printf("\n--%s", alt_boundary);
935 serv_puts("Content-type: text/html; charset=utf-8");
936 serv_puts("Content-Transfer-Encoding: quoted-printable");
938 serv_puts("<html><body>\r\n");
939 text_to_server_qp(sbstr("msgtext")); /* Transmit message in quoted-printable encoding */
940 serv_puts("</body></html>\r\n");
942 if (include_text_alt) {
943 serv_printf("--%s--", alt_boundary);
952 /* Add in the attachments */
953 it = GetNewHashPos(WCC->attachments, 0);
954 while (GetNextHashPos(WCC->attachments, it, &len, &Key, &vAtt)) {
955 att = (wc_mime_attachment *)vAtt;
956 if (att->length == 0)
958 encoded_length = ((att->length * 150) / 100);
959 encoded = malloc(encoded_length);
960 if (encoded == NULL) break;
961 encoded_strlen = CtdlEncodeBase64(encoded, ChrPtr(att->Data), StrLength(att->Data), 1);
963 serv_printf("--%s", top_boundary);
964 serv_printf("Content-type: %s", ChrPtr(att->ContentType));
965 serv_printf("Content-disposition: attachment; filename=\"%s\"", ChrPtr(att->FileName));
966 serv_puts("Content-transfer-encoding: base64");
968 serv_write(encoded, encoded_strlen);
973 serv_printf("--%s--", top_boundary);
982 * Post message (or don't post message)
984 * Note regarding the "dont_post" variable:
985 * A random value (actually, it's just a timestamp) is inserted as a hidden
986 * field called "postseq" when the display_enter page is generated. This
987 * value is checked when posting, using the static variable dont_post. If a
988 * user attempts to post twice using the same dont_post value, the message is
989 * discarded. This prevents the accidental double-saving of the same message
990 * if the user happens to click the browser "back" button.
992 void post_message(void)
995 StrBuf *EmailAddress;
998 StrBuf *encoded_subject = NULL;
999 static long dont_post = (-1L);
1000 int is_anonymous = 0;
1001 const StrBuf *display_name = NULL;
1002 wcsession *WCC = WC;
1005 if (havebstr("force_room")) {
1006 gotoroom(sbstr("force_room"));
1009 if (havebstr("display_name")) {
1010 display_name = sbstr("display_name");
1011 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1012 display_name = NULL;
1017 if (!strcasecmp(bstr("submit_action"), "cancel")) {
1018 AppendImportantMessage(_("Cancelled. Message was not posted."), -1);
1019 } else if (lbstr("postseq") == dont_post) {
1020 AppendImportantMessage(
1021 _("Automatically cancelled because you have already "
1022 "saved this message."), -1);
1024 const char CMD[] = "ENT0 1|%s|%d|4|%s|%s||%s|%s|%s|%s|%s";
1025 StrBuf *Recp = NULL;
1028 StrBuf *wikipage = NULL;
1029 const StrBuf *my_email_addr = NULL;
1030 StrBuf *CmdBuf = NULL;
1031 StrBuf *references = NULL;
1032 int saving_to_drafts = 0;
1035 saving_to_drafts = !strcasecmp(bstr("submit_action"), "draft");
1038 if (saving_to_drafts) {
1039 /* temporarily change to the drafts room */
1040 serv_puts("GOTO _DRAFTS_");
1041 StrBuf_ServGetln(Buf);
1042 if (GetServerStatusMsg(Buf, NULL, 1, 2) != 2) {
1043 /* You probably don't even have a dumb Drafts folder */
1044 syslog(LOG_DEBUG, "%s:%d: server save to drafts error: %s\n", __FILE__, __LINE__, ChrPtr(Buf) + 4);
1045 AppendImportantMessage(_("Saved to Drafts failed: "), -1);
1052 if (havebstr("references"))
1054 const StrBuf *ref = sbstr("references");
1055 references = NewStrBufDup(ref);
1056 if (*ChrPtr(references) == '|') { /* remove leading '|' if present */
1057 StrBufCutLeft(references, 1);
1059 StrBufReplaceChars(references, '|', '!');
1061 if (havebstr("subject")) {
1064 * make enough room for the encoded string;
1065 * plus the QP header
1067 Subj = sbstr("subject");
1069 StrBufRFC2047encode(&encoded_subject, Subj);
1071 UserName = NewStrBuf();
1072 EmailAddress = NewStrBuf();
1073 EncBuf = NewStrBuf();
1075 Recp = StrBufSanitizeEmailRecipientVector(sbstr("recp"), UserName, EmailAddress, EncBuf);
1076 Cc = StrBufSanitizeEmailRecipientVector(sbstr("cc"), UserName, EmailAddress, EncBuf);
1077 Bcc = StrBufSanitizeEmailRecipientVector(sbstr("bcc"), UserName, EmailAddress, EncBuf);
1079 FreeStrBuf(&UserName);
1080 FreeStrBuf(&EmailAddress);
1081 FreeStrBuf(&EncBuf);
1083 wikipage = NewStrBufDup(sbstr("page"));
1084 str_wiki_index(wikipage);
1085 my_email_addr = sbstr("my_email_addr");
1087 HeaderLen = StrLength(Recp) +
1088 StrLength(encoded_subject) +
1091 StrLength(wikipage) +
1092 StrLength(my_email_addr) +
1093 StrLength(references);
1094 CmdBuf = NewStrBufPlain(NULL, sizeof (CMD) + HeaderLen);
1095 StrBufPrintf(CmdBuf,
1097 saving_to_drafts?"":ChrPtr(Recp),
1099 ChrPtr(encoded_subject),
1100 ChrPtr(display_name),
1101 saving_to_drafts?"":ChrPtr(Cc),
1102 saving_to_drafts?"":ChrPtr(Bcc),
1104 ChrPtr(my_email_addr),
1105 ChrPtr(references));
1106 FreeStrBuf(&references);
1107 FreeStrBuf(&encoded_subject);
1110 if ((HeaderLen + StrLength(sbstr("msgtext")) < 10) &&
1111 (GetCount(WCC->attachments) == 0)){
1112 AppendImportantMessage(_("Refusing to post empty message.\n"), -1);
1113 FreeStrBuf(&CmdBuf);
1118 syslog(LOG_DEBUG, "%s\n", ChrPtr(CmdBuf));
1119 serv_puts(ChrPtr(CmdBuf));
1120 FreeStrBuf(&CmdBuf);
1122 StrBuf_ServGetln(Buf);
1123 if (GetServerStatus(Buf, NULL) == 4) {
1124 if (saving_to_drafts) {
1125 if ( (havebstr("recp"))
1126 || (havebstr("cc" ))
1127 || (havebstr("bcc" )) ) {
1128 /* save recipient headers or room to post to */
1129 serv_printf("To: %s", ChrPtr(Recp));
1130 serv_printf("Cc: %s", ChrPtr(Cc));
1131 serv_printf("Bcc: %s", ChrPtr(Bcc));
1133 serv_printf("X-Citadel-Room: %s", ChrPtr(WCC->CurRoom.name));
1136 post_mime_to_server();
1137 if (saving_to_drafts) {
1138 AppendImportantMessage(_("Message has been saved to Drafts.\n"), -1);
1139 gotoroom(WCC->CurRoom.name);
1141 readloop(readnew, eUseDefault);
1144 } else if ( (havebstr("recp"))
1145 || (havebstr("cc" ))
1146 || (havebstr("bcc" ))
1148 AppendImportantMessage(_("Message has been sent.\n"), -1);
1151 AppendImportantMessage(_("Message has been posted.\n"), -1);
1153 dont_post = lbstr("postseq");
1155 syslog(LOG_DEBUG, "%s:%d: server post error: %s", __FILE__, __LINE__, ChrPtr(Buf) + 4);
1156 AppendImportantMessage(ChrPtr(Buf) + 4, StrLength(Buf) - 4);
1158 if (saving_to_drafts) gotoroom(WCC->CurRoom.name);
1172 DeleteHash(&WCC->attachments);
1175 * We may have been supplied with instructions regarding the location
1176 * to which we must return after posting. If found, go there.
1178 if (havebstr("return_to")) {
1179 http_redirect(bstr("return_to"));
1182 * If we were editing a page in a wiki room, go to that page now.
1184 else if (havebstr("page")) {
1185 snprintf(buf, sizeof buf, "wiki?page=%s", bstr("page"));
1189 * Otherwise, just go to the "read messages" loop.
1193 readloop(readnew, eUseDefault);
1199 * Client is uploading an attachment
1201 void upload_attachment(void) {
1202 wcsession *WCC = WC;
1208 wc_mime_attachment *att;
1209 const StrBuf *Tmpl = sbstr("template");
1210 const StrBuf *MimeType = NULL;
1214 syslog(LOG_DEBUG, "upload_attachment()\n");
1215 if (!Tmpl) wc_printf("upload_attachment()<br>\n");
1217 if (WCC->upload_length <= 0) {
1218 syslog(LOG_DEBUG, "ERROR no attachment was uploaded\n");
1221 putlbstr("UPLOAD_ERROR", 1);
1222 MimeType = DoTemplate(SKEY(Tmpl), NULL, &NoCtx);
1224 else wc_printf("ERROR no attachment was uploaded<br>\n");
1225 http_transmit_thing(ChrPtr(MimeType), 0);
1230 syslog(LOG_DEBUG, "Client is uploading %d bytes\n", WCC->upload_length);
1231 if (Tmpl) putlbstr("UPLOAD_LENGTH", WCC->upload_length);
1232 else wc_printf("Client is uploading %d bytes<br>\n", WCC->upload_length);
1234 att = (wc_mime_attachment*)malloc(sizeof(wc_mime_attachment));
1235 memset(att, 0, sizeof(wc_mime_attachment ));
1236 att->length = WCC->upload_length;
1237 att->ContentType = NewStrBufPlain(WCC->upload_content_type, -1);
1238 att->FileName = NewStrBufDup(WCC->upload_filename);
1239 UID = sbstr("qquuid");
1241 att->PartNum = NewStrBufDup(UID);
1243 if (WCC->attachments == NULL) {
1244 WCC->attachments = NewHash(1, Flathash);
1247 /* And add it to the list. */
1249 if ((GetCount(WCC->attachments) > 0) &&
1250 GetHashAt(WCC->attachments,
1251 GetCount(WCC->attachments) -1,
1252 &newnlen, &newn, &v))
1253 n = *((int*) newn) + 1;
1254 Put(WCC->attachments, IKEY(n), att, DestroyMime);
1257 * Mozilla sends a simple filename, which is what we want,
1258 * but Satan's Browser sends an entire pathname. Reduce
1259 * the path to just a filename if we need to.
1261 pch = strrchr(ChrPtr(att->FileName), '/');
1263 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1265 pch = strrchr(ChrPtr(att->FileName), '\\');
1267 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1271 * Transfer control of this memory from the upload struct
1272 * to the attachment struct.
1274 att->Data = WCC->upload;
1276 WCC->upload_length = 0;
1278 if (Tmpl) MimeType = DoTemplate(SKEY(Tmpl), NULL, &NoCtx);
1279 http_transmit_thing(ChrPtr(MimeType), 0);
1284 * Remove an attachment from the message currently being composed.
1286 * Currently we identify the attachment to be removed by its filename.
1287 * There is probably a better way to do this.
1289 void remove_attachment(void) {
1290 wcsession *WCC = WC;
1291 wc_mime_attachment *att;
1293 StrBuf *WhichAttachment;
1299 WhichAttachment = NewStrBufDup(sbstr("which_attachment"));
1300 if (ChrPtr(WhichAttachment)[0] == '/')
1301 StrBufCutLeft(WhichAttachment, 1);
1302 StrBufUnescape(WhichAttachment, 0);
1303 at = GetNewHashPos(WCC->attachments, 0);
1306 GetHashPos(WCC->attachments, at, &len, &key, &vAtt);
1308 att = (wc_mime_attachment*) vAtt;
1309 if ((att != NULL) &&
1311 !strcmp(ChrPtr(WhichAttachment), ChrPtr(att->FileName)) ||
1312 ((att->PartNum != NULL) &&
1313 !strcmp(ChrPtr(WhichAttachment), ChrPtr(att->PartNum)))
1316 DeleteEntryFromHash(WCC->attachments, at);
1321 while (NextHashPos(WCC->attachments, at));
1323 FreeStrBuf(&WhichAttachment);
1324 wc_printf("remove_attachment(%d) completed\n", found);
1328 const char *ReplyToModeStrings [3] = {
1333 typedef enum _eReplyToNodes {
1340 * display the message entry screen
1342 void display_enter(void)
1344 const char *ReplyingModeStr;
1345 eReplyToNodes ReplyMode = eReply;
1349 const StrBuf *display_name = NULL;
1350 int recipient_required = 0;
1351 int subject_required = 0;
1352 int is_anonymous = 0;
1353 wcsession *WCC = WC;
1357 if (havebstr("force_room")) {
1358 gotoroom(sbstr("force_room"));
1361 display_name = sbstr("display_name");
1362 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1363 display_name = NULL;
1368 * First, do we have permission to enter messages in this room at all?
1371 serv_puts("ENT0 0");
1372 StrBuf_ServGetln(Line);
1373 rc = GetServerStatusMsg(Line, &Result, 0, 2);
1375 if (Result == 570) { /* 570 means that we need a recipient here */
1376 recipient_required = 1;
1378 else if (rc != 2) { /* Any other error means that we cannot continue */
1379 rc = GetServerStatusMsg(Line, &Result, 0, 2);
1381 readloop(readnew, eUseDefault);
1386 /* Is the server strongly recommending that the user enter a message subject? */
1387 if (StrLength(Line) > 4) {
1388 subject_required = extract_int(ChrPtr(Line) + 4, 1);
1392 * Are we perhaps in an address book view? If so, then an "enter
1393 * message" command really means "add new entry."
1395 if (WCC->CurRoom.defview == VIEW_ADDRESSBOOK) {
1396 do_edit_vcard(-1, "", NULL, NULL, "", ChrPtr(WCC->CurRoom.name));
1402 * Are we perhaps in a calendar room? If so, then an "enter
1403 * message" command really means "add new calendar item."
1405 if (WCC->CurRoom.defview == VIEW_CALENDAR) {
1406 display_edit_event();
1412 * Are we perhaps in a tasks view? If so, then an "enter
1413 * message" command really means "add new task."
1415 if (WCC->CurRoom.defview == VIEW_TASKS) {
1416 display_edit_task();
1422 ReplyingModeStr = bstr("replying_mode");
1423 if (ReplyingModeStr != NULL) for (i = 0; i < 3; i++) {
1424 if (strcmp(ReplyingModeStr, ReplyToModeStrings[i]) == 0) {
1425 ReplyMode = (eReplyToNodes) i;
1432 * If the "replying_to" variable is set, it refers to a message
1433 * number from which we must extract some header fields...
1435 replying_to = lbstr("replying_to");
1436 if (replying_to > 0) {
1438 StrBuf *wefw = NULL;
1439 StrBuf *msgn = NULL;
1440 StrBuf *from = NULL;
1441 StrBuf *node = NULL;
1442 StrBuf *rfca = NULL;
1443 StrBuf *rcpt = NULL;
1444 StrBuf *cccc = NULL;
1445 StrBuf *replyto = NULL;
1446 StrBuf *nvto = NULL;
1447 serv_printf("MSG0 %ld|1", replying_to);
1449 StrBuf_ServGetln(Line);
1450 if (GetServerStatusMsg(Line, NULL, 0, 0) == 1)
1451 while (len = StrBuf_ServGetln(Line),
1454 strcmp(ChrPtr(Line), "000")))
1456 eMessageField which;
1457 if ((StrLength(Line) > 4) &&
1458 (ChrPtr(Line)[4] == '=') &&
1459 GetFieldFromMnemonic(&which, ChrPtr(Line)))
1462 StrBuf *subj = NewStrBuf();
1463 StrBuf *FlatSubject;
1465 if (ReplyMode == eForward) {
1466 if (strncasecmp(ChrPtr(Line) + 5, "Fw:", 3)) {
1467 StrBufAppendBufPlain(subj, HKEY("Fw: "), 0);
1471 if (strncasecmp(ChrPtr(Line) + 5, "Re:", 3)) {
1472 StrBufAppendBufPlain(subj, HKEY("Re: "), 0);
1475 StrBufAppendBufPlain(subj,
1477 StrLength(Line) - 5, 0);
1478 FlatSubject = NewStrBufPlain(NULL, StrLength(subj));
1479 StrBuf_RFC822_to_Utf8(FlatSubject, subj, NULL, NULL);
1481 PutBstr(HKEY("subject"), FlatSubject);
1490 wefw = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1492 /* Trim down excessively long lists of thread references. We eliminate the
1493 * second one in the list so that the thread root remains intact.
1495 rrtok = num_tokens(ChrPtr(wefw), '|');
1496 rrlen = StrLength(wefw);
1497 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
1498 StrBufRemove_token(wefw, 1, '|');
1504 msgn = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1509 from = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1510 FlatFrom = NewStrBufPlain(NULL, StrLength(from));
1511 StrBuf_RFC822_to_Utf8(FlatFrom, from, NULL, NULL);
1514 for (i=0; i<StrLength(from); ++i) {
1515 if (ChrPtr(from)[i] == ',')
1516 StrBufPeek(from, NULL, i, ' ');
1522 rcpt = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1527 cccc = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1532 node = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1535 replyto = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1539 rfca = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1540 FlatRFCA = NewStrBufPlain(NULL, StrLength(rfca));
1541 StrBuf_RFC822_to_Utf8(FlatRFCA, rfca, NULL, NULL);
1547 nvto = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1548 putbstr("nvto", nvto);
1571 if (StrLength(wefw) + StrLength(msgn) > 0) {
1572 StrBuf *refs = NewStrBuf();
1573 if (StrLength(wefw) > 0) {
1574 StrBufAppendBuf(refs, wefw, 0);
1576 if ( (StrLength(wefw) > 0) &&
1577 (StrLength(msgn) > 0) )
1579 StrBufAppendBufPlain(refs, HKEY("|"), 0);
1581 if (StrLength(msgn) > 0) {
1582 StrBufAppendBuf(refs, msgn, 0);
1584 PutBstr(HKEY("references"), refs);
1588 * If this is a Reply or a ReplyAll, copy the sender's email into the To: field
1590 if ((ReplyMode == eReply) || (ReplyMode == eReplyAll))
1593 if ((StrLength(replyto) > 0) && (ReplyMode == eReplyAll)) {
1594 to_rcpt = NewStrBuf();
1595 StrBufAppendBuf(to_rcpt, replyto, 0);
1597 else if (StrLength(rfca) > 0) {
1598 to_rcpt = NewStrBuf();
1599 StrBufAppendBuf(to_rcpt, from, 0);
1600 StrBufAppendBufPlain(to_rcpt, HKEY(" <"), 0);
1601 StrBufAppendBuf(to_rcpt, rfca, 0);
1602 StrBufAppendBufPlain(to_rcpt, HKEY(">"), 0);
1607 if ( (StrLength(node) > 0)
1608 && (strcasecmp(ChrPtr(node), ChrPtr(WCC->serv_info->serv_nodename)))
1610 StrBufAppendBufPlain(to_rcpt, HKEY(" @ "), 0);
1611 StrBufAppendBuf(to_rcpt, node, 0);
1614 PutBstr(HKEY("recp"), to_rcpt);
1618 * Only if this is a ReplyAll, copy all recipients into the Cc: field
1620 if (ReplyMode == eReplyAll)
1622 StrBuf *cc_rcpt = rcpt;
1624 if ((StrLength(cccc) > 0) && (StrLength(replyto) == 0))
1626 if (cc_rcpt != NULL) {
1627 StrBufAppendPrintf(cc_rcpt, ", ");
1628 StrBufAppendBuf(cc_rcpt, cccc, 0);
1634 if (cc_rcpt != NULL)
1635 PutBstr(HKEY("cc"), cc_rcpt);
1647 * Otherwise proceed normally.
1648 * Do a custom room banner with no navbar...
1651 if (recipient_required) {
1652 const StrBuf *Recp = NULL;
1653 const StrBuf *Cc = NULL;
1654 const StrBuf *Bcc = NULL;
1655 StrBuf *wikipage = NULL;
1656 StrBuf *CmdBuf = NULL;
1657 const char CMD[] = "ENT0 0|%s|%d|0||%s||%s|%s|%s";
1659 Recp = sbstr("recp");
1662 wikipage = NewStrBufDup(sbstr("page"));
1663 str_wiki_index(wikipage);
1665 CmdBuf = NewStrBufPlain(NULL,
1668 StrLength(display_name) +
1671 StrLength(wikipage));
1673 StrBufPrintf(CmdBuf,
1677 ChrPtr(display_name),
1682 serv_puts(ChrPtr(CmdBuf));
1683 StrBuf_ServGetln(CmdBuf);
1686 rc = GetServerStatusMsg(CmdBuf, &Result, 0, 0);
1688 if ( (Result == 570) /* invalid or missing recipient(s) */
1689 || (Result == 550) /* higher access required to send Internet mail */
1691 /* These errors will have been displayed and are excusable */
1693 else if (rc != 2) { /* Any other error means that we cannot continue */
1694 AppendImportantMessage(ChrPtr(CmdBuf) + 4, StrLength(CmdBuf) - 4);
1695 FreeStrBuf(&CmdBuf);
1697 readloop(readnew, eUseDefault);
1700 FreeStrBuf(&CmdBuf);
1702 if (recipient_required)
1703 PutBstr(HKEY("__RCPTREQUIRED"), NewStrBufPlain(HKEY("1")));
1704 if (recipient_required || subject_required)
1705 PutBstr(HKEY("__SUBJREQUIRED"), NewStrBufPlain(HKEY("1")));
1708 output_headers(1, 0, 0, 0, 1, 0);
1709 DoTemplate(HKEY("edit_message"), NULL, &NoCtx);
1718 void delete_msg(void)
1723 msgid = lbstr("msgid");
1725 if ((WC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
1726 serv_printf("DELE %ld", msgid);
1728 else { /* Otherwise move it to Trash */
1729 serv_printf("MOVE %ld|_TRASH_|0", msgid);
1732 StrBuf_ServGetln(Line);
1733 GetServerStatusMsg(Line, NULL, 1, 0);
1737 readloop(readnew, eUseDefault);
1742 * move a message to another room
1748 msgid = lbstr("msgid");
1750 if (havebstr("move_button")) {
1752 serv_printf("MOVE %ld|%s", msgid, bstr("target_room"));
1754 StrBuf_ServGetln(Line);
1755 GetServerStatusMsg(Line, NULL, 1, 0);
1758 AppendImportantMessage(_("The message was not moved."), -1);
1762 readloop(readnew, eUseDefault);
1768 * Generic function to output an arbitrary MIME attachment from
1769 * message being composed
1771 * partnum The MIME part to be output
1772 * filename Fake filename to give
1773 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1775 void postpart(StrBuf *partnum, StrBuf *filename, int force_download)
1778 StrBuf *content_type;
1779 wc_mime_attachment *part;
1782 i = StrToi(partnum);
1783 if (GetHash(WC->attachments, IKEY(i), &vPart) &&
1785 part = (wc_mime_attachment*) vPart;
1786 if (force_download) {
1787 content_type = NewStrBufPlain(HKEY("application/octet-stream"));
1790 content_type = NewStrBufDup(part->ContentType);
1792 StrBufAppendBuf(WC->WBuf, part->Data, 0);
1793 http_transmit_thing(ChrPtr(content_type), 0);
1795 hprintf("HTTP/1.1 404 %s\n", ChrPtr(partnum));
1796 output_headers(0, 0, 0, 0, 0, 0);
1797 hprintf("Content-Type: text/plain\r\n");
1799 wc_printf(_("An error occurred while retrieving this part: %s/%s\n"),
1800 ChrPtr(partnum), ChrPtr(filename));
1803 FreeStrBuf(&content_type);
1808 * Generic function to output an arbitrary MIME part from an arbitrary
1809 * message number on the server.
1811 * msgnum message number on the citadel server
1812 * partnum MIME part number to be output
1813 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1815 void view_or_download_mimepart(int force_download)
1820 StrBuf *ContentType;
1823 PartNum = NewStrBuf();
1825 msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
1826 StrBufExtract_token(PartNum, WC->Hdr->HR.ReqLine, 1, '/');
1829 serv_printf("DLAT %ld|%s", msgnum, ChrPtr(PartNum)); // DLAT will return: 6XX length|-1|filename|cbtype|cbcharset
1830 StrBuf_ServGetln(Buf);
1831 FreeStrBuf(&PartNum);
1833 if (GetServerStatus(Buf, NULL) != 6) {
1835 hprintf("HTTP/1.1 500 error\r\n"
1836 "Pragma: no-cache\r\n"
1837 "Cache-Control: no-store\r\n"
1839 "Content-Type: text/plain\r\n"
1842 wc_printf(_("An error occurred while retrieving this part: %s\n"), "--");
1846 StrBufCutLeft(Buf, 4);
1847 bytes = StrBufExtract_long(Buf, 0, '|');
1849 if (force_download) {
1850 ContentType = NewStrBufPlain(HKEY("application/octet-stream"));
1853 ContentType = NewStrBuf();
1854 StrBufExtract_token(ContentType, Buf, 3, '|');
1859 StrBuf_ServGetBLOBBuffered(Buf, bytes);
1863 http_transmit_thing(ChrPtr(ContentType), 0);
1864 FreeStrBuf(&ContentType);
1869 * Read any MIME part of a message, from the server, into memory.
1871 StrBuf *load_mimepart(long msgnum, char *partnum)
1877 serv_printf("DLAT %ld|%s", msgnum, partnum);
1878 StrBuf_ServGetln(Buf);
1879 if (GetServerStatus(Buf, NULL) == 6) {
1880 StrBufCutLeft(Buf, 4);
1881 bytes = StrBufExtract_long(Buf, 0, '|');
1884 StrBuf_ServGetBLOBBuffered(Buf, bytes);
1894 * Read any MIME part of a message, from the server, into memory.
1896 void MimeLoadData(wc_mime_attachment *Mime)
1902 serv_printf("DLAT %ld|%s", Mime->msgnum, ChrPtr(Mime->PartNum));
1904 StrBuf_ServGetln(Buf);
1905 if (GetServerStatus(Buf, NULL) == 6) {
1906 Ptr = &(ChrPtr(Buf)[4]);
1907 bytes = StrBufExtractNext_long(Buf, &Ptr, '|');
1908 StrBufSkip_NTokenS(Buf, &Ptr, '|', 3); /* filename, cbtype, mimetype */
1909 if (Mime->Charset == NULL) Mime->Charset = NewStrBuf();
1910 StrBufExtract_NextToken(Mime->Charset, Buf, &Ptr, '|');
1912 if (Mime->Data == NULL) {
1913 Mime->Data = NewStrBufPlain(NULL, bytes);
1915 StrBuf_ServGetBLOBBuffered(Mime->Data, bytes);
1918 FlushStrBuf(Mime->Data);
1924 void view_mimepart(void) {
1925 view_or_download_mimepart(0);
1928 void download_mimepart(void) {
1929 view_or_download_mimepart(1);
1932 void view_postpart(void) {
1933 StrBuf *filename = NewStrBuf();
1934 StrBuf *partnum = NewStrBuf();
1936 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1937 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1939 postpart(partnum, filename, 0);
1941 FreeStrBuf(&filename);
1942 FreeStrBuf(&partnum);
1945 void download_postpart(void) {
1946 StrBuf *filename = NewStrBuf();
1947 StrBuf *partnum = NewStrBuf();
1949 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1950 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1952 postpart(partnum, filename, 1);
1954 FreeStrBuf(&filename);
1955 FreeStrBuf(&partnum);
1960 void show_num_attachments(void) {
1961 wc_printf("%d", GetCount(WC->attachments));
1965 void h_readnew(void) { readloop(readnew, eUseDefault);}
1966 void h_readold(void) { readloop(readold, eUseDefault);}
1967 void h_readfwd(void) { readloop(readfwd, eUseDefault);}
1968 void h_headers(void) { readloop(headers, eUseDefault);}
1969 void h_do_search(void) { readloop(do_search, eUseDefault);}
1970 void h_readgt(void) { readloop(readgt, eUseDefault);}
1971 void h_readlt(void) { readloop(readlt, eUseDefault);}
1975 /* Output message list in JSON format */
1976 void jsonMessageList(void) {
1977 StrBuf *View = NewStrBuf();
1978 const StrBuf *room = sbstr("room");
1979 long oper = (havebstr("query")) ? do_search : readnew;
1980 StrBufPrintf(View, "%d", VIEW_JSON_LIST);
1981 putbstr("view", View);;
1983 readloop(oper, eUseDefault);
1986 void FreeReadLoopHandlerSet(void *v) {
1987 RoomRenderer *Handler = (RoomRenderer *) v;
1988 FreeStrBuf(&Handler->FetchMessageList);
1989 if (Handler->MessageFieldList != NULL) {
1990 free(Handler->MessageFieldList);
1995 void RegisterReadLoopHandlerset(
1997 GetParamsGetServerCall_func GetParamsGetServerCall,
1998 PrintViewHeader_func PrintPageHeader,
1999 PrintViewHeader_func PrintViewHeader,
2000 load_msg_ptrs_detailheaders LH,
2001 LoadMsgFromServer_func LoadMsgFromServer,
2002 RenderView_or_Tail_func RenderView_or_Tail,
2003 View_Cleanup_func ViewCleanup,
2004 const char **browseListFields
2009 RoomRenderer *Handler;
2011 Handler = (RoomRenderer*) malloc(sizeof(RoomRenderer));
2013 Handler->RoomType = RoomType;
2014 Handler->GetParamsGetServerCall = GetParamsGetServerCall;
2015 Handler->PrintPageHeader = PrintPageHeader;
2016 Handler->PrintViewHeader = PrintViewHeader;
2017 Handler->LoadMsgFromServer = LoadMsgFromServer;
2018 Handler->RenderView_or_Tail = RenderView_or_Tail;
2019 Handler->ViewCleanup = ViewCleanup;
2020 Handler->LHParse = LH;
2022 if (browseListFields != NULL) {
2023 while (browseListFields[count] != NULL) {
2026 Handler->HeaderCount = count;
2027 Handler->MessageFieldList = (eMessageField*) malloc(sizeof(eMessageField) * count);
2028 Handler->FetchMessageList = NewStrBufPlain(NULL, 5 * count + 4 + 5);
2029 StrBufPlain(Handler->FetchMessageList, HKEY("time\n"));
2030 for (i = 0; i < count; i++) {
2031 if (!GetFieldFromMnemonic(&Handler->MessageFieldList[i], browseListFields[i])) {
2032 fprintf(stderr, "Unknown message header: %s\n", browseListFields[i]);
2035 StrBufAppendBufPlain(Handler->FetchMessageList, browseListFields[i], 4, 0);
2036 StrBufAppendBufPlain(Handler->FetchMessageList, HKEY("\n"), 0);
2038 StrBufAppendBufPlain(Handler->FetchMessageList, HKEY("000"), 0);
2041 Handler->FetchMessageList = NULL;
2042 Handler->MessageFieldList = NULL;
2045 Put(ReadLoopHandler, IKEY(RoomType), Handler, FreeReadLoopHandlerSet);
2052 RegisterPreference("use_sig",
2053 _("Attach signature to email messages?"),
2056 RegisterPreference("signature", _("Use this signature:"), PRF_QP_STRING, NULL);
2057 RegisterPreference("default_header_charset",
2058 _("Default character set for email headers:"),
2061 RegisterPreference("defaultfrom", _("Preferred email address"), PRF_STRING, NULL);
2062 RegisterPreference("defaultname",
2063 _("Preferred display name for email messages"),
2066 RegisterPreference("defaulthandle",
2067 _("Preferred display name for bulletin board posts"),
2070 RegisterPreference("mailbox",_("Mailbox view mode"), PRF_STRING, NULL);
2072 WebcitAddUrlHandler(HKEY("readnew"), "", 0, h_readnew, ANONYMOUS|NEED_URL);
2073 WebcitAddUrlHandler(HKEY("readold"), "", 0, h_readold, ANONYMOUS|NEED_URL);
2074 WebcitAddUrlHandler(HKEY("readfwd"), "", 0, h_readfwd, ANONYMOUS|NEED_URL);
2075 WebcitAddUrlHandler(HKEY("headers"), "", 0, h_headers, NEED_URL);
2076 WebcitAddUrlHandler(HKEY("readgt"), "", 0, h_readgt, ANONYMOUS|NEED_URL);
2077 WebcitAddUrlHandler(HKEY("readlt"), "", 0, h_readlt, ANONYMOUS|NEED_URL);
2078 WebcitAddUrlHandler(HKEY("do_search"), "", 0, h_do_search, 0);
2079 WebcitAddUrlHandler(HKEY("display_enter"), "", 0, display_enter, 0);
2080 WebcitAddUrlHandler(HKEY("post"), "", 0, post_message, PROHIBIT_STARTPAGE);
2081 WebcitAddUrlHandler(HKEY("move_msg"), "", 0, move_msg, PROHIBIT_STARTPAGE);
2082 WebcitAddUrlHandler(HKEY("delete_msg"), "", 0, delete_msg, PROHIBIT_STARTPAGE);
2083 WebcitAddUrlHandler(HKEY("msg"), "", 0, embed_message, NEED_URL);
2084 WebcitAddUrlHandler(HKEY("message"), "", 0, handle_one_message, NEED_URL|XHTTP_COMMANDS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
2085 WebcitAddUrlHandler(HKEY("printmsg"), "", 0, print_message, NEED_URL);
2086 WebcitAddUrlHandler(HKEY("msgheaders"), "", 0, display_headers, NEED_URL);
2088 WebcitAddUrlHandler(HKEY("mimepart"), "", 0, view_mimepart, NEED_URL);
2089 WebcitAddUrlHandler(HKEY("mimepart_download"), "", 0, download_mimepart, NEED_URL);
2090 WebcitAddUrlHandler(HKEY("postpart"), "", 0, view_postpart, NEED_URL|PROHIBIT_STARTPAGE);
2091 WebcitAddUrlHandler(HKEY("postpart_download"), "", 0, download_postpart, NEED_URL|PROHIBIT_STARTPAGE);
2092 WebcitAddUrlHandler(HKEY("upload_attachment"), "", 0, upload_attachment, AJAX);
2093 WebcitAddUrlHandler(HKEY("remove_attachment"), "", 0, remove_attachment, AJAX);
2094 WebcitAddUrlHandler(HKEY("show_num_attachments"), "", 0, show_num_attachments, AJAX);
2097 WebcitAddUrlHandler(HKEY("roommsgs"), "", 0, jsonMessageList,0);
2101 SessionDetachModule_MSG
2104 DeleteHash(&sess->summ);