2 * Functions which deal with the fetching and displaying of messages.
4 * Copyright (c) 1996-2012 by the citadel.org team
6 * This program is open source software. You can redistribute it and/or
7 * modify it under the terms of the GNU General Public License, version 3.
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 *MsgHeaderHandler = NULL;
21 HashList *MsgEvaluators = NULL;
22 HashList *MimeRenderHandler = NULL;
23 HashList *ReadLoopHandler = NULL;
24 int dbg_analyze_msg = 0;
26 #define SUBJ_COL_WIDTH_PCT 50 /* Mailbox view column width */
27 #define SENDER_COL_WIDTH_PCT 30 /* Mailbox view column width */
28 #define DATE_PLUS_BUTTONS_WIDTH_PCT 20 /* Mailbox view column width */
30 void jsonMessageListHdr(void);
32 void display_enter(void);
34 typedef void (*MsgPartEvaluatorFunc)(message_summary *Sum, StrBuf *Buf);
36 typedef struct _MsgPartEvaluatorStruct {
37 MsgPartEvaluatorFunc f;
38 } MsgPartEvaluatorStruct;
42 /* workaround for json listview; its not useable directly */
43 if (WC->CurRoom.view == VIEW_JSON_LIST) {
44 StrBuf *View = NewStrBuf();
45 StrBufPrintf(View, "%d", VIEW_MAILBOX);
46 putbstr("view", View);;
49 int load_message(message_summary *Msg,
62 if (Msg->PartNum != NULL) {
63 serv_printf("MSG4 %ld|%s", Msg->msgnum, ChrPtr(Msg->PartNum));
66 serv_printf("MSG4 %ld", Msg->msgnum);
69 StrBuf_ServGetln(Buf);
70 if (GetServerStatus(Buf, NULL) != 1) {
72 StrBufAppendPrintf(*Error, "<strong>");
73 StrBufAppendPrintf(*Error, _("ERROR:"));
74 StrBufAppendPrintf(*Error, "</strong> %s<br>\n", &buf[4]);
79 /* begin everythingamundo table */
80 HdrToken = NewStrBuf();
81 while (!Done && StrBuf_ServGetln(Buf)>=0) {
82 if ( (StrLength(Buf)==3) &&
83 !strcmp(ChrPtr(Buf), "000"))
87 if (Msg->MsgBody->Data == NULL)
88 Msg->MsgBody->Data = NewStrBuf();
89 Msg->MsgBody->ContentType = NewStrBufPlain(HKEY("text/html"));
90 StrBufAppendPrintf(Msg->MsgBody->Data, "<div><i>");
91 StrBufAppendPrintf(Msg->MsgBody->Data, _("Empty message"));
92 StrBufAppendPrintf(Msg->MsgBody->Data, "</i><br><br>\n");
93 StrBufAppendPrintf(Msg->MsgBody->Data, "</div>\n");
98 case 0:/* Citadel Message Headers */
99 if (StrLength(Buf) == 0) {
103 StrBufExtract_token(HdrToken, Buf, 0, '=');
104 StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
106 /* look up one of the examine_* functions to parse the content */
107 if (GetHash(MsgHeaderHandler, SKEY(HdrToken), &vHdr) &&
109 Hdr = (headereval*)vHdr;
110 Hdr->evaluator(Msg, Buf, FoundCharset);
111 if (Hdr->Type == 1) {
115 else LogError(Target,
117 "don't know how to handle message header[%s]\n",
121 case 1:/* Message Mime Header */
122 if (StrLength(Buf) == 0) {
124 if (Msg->MsgBody->ContentType == NULL)
125 /* end of header or no header? */
126 Msg->MsgBody->ContentType = NewStrBufPlain(HKEY("text/plain"));
127 /* usual end of mime header */
131 StrBufExtract_token(HdrToken, Buf, 0, ':');
132 if (StrLength(HdrToken) > 0) {
133 StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
134 /* the examine*'s know how to do with mime headers too... */
135 if (GetHash(MsgHeaderHandler, SKEY(HdrToken), &vHdr) &&
137 Hdr = (headereval*)vHdr;
138 Hdr->evaluator(Msg, Buf, FoundCharset);
143 case 2: /* Message Body */
145 if (Msg->MsgBody->size_known > 0) {
146 StrBuf_ServGetBLOBBuffered(Msg->MsgBody->Data, Msg->MsgBody->length);
148 /*/ todo: check next line, if not 000, append following lines */
151 if (StrLength(Msg->MsgBody->Data) > 0)
152 StrBufAppendBufPlain(Msg->MsgBody->Data, "\n", 1, 0);
153 StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
157 StrBufAppendBuf(Msg->MsgBody->Data, Buf, 0);
162 if (Msg->AllAttach == NULL)
163 Msg->AllAttach = NewHash(1,NULL);
164 /* now we put the body mimepart we read above into the mimelist */
165 Put(Msg->AllAttach, SKEY(Msg->MsgBody->PartNum), Msg->MsgBody, DestroyMime);
168 FreeStrBuf(&HdrToken);
175 * I wanna SEE that message!
177 * msgnum Message number to display
178 * printable_view Nonzero to display a printable view
179 * section Optional for encapsulated message/rfc822 submessage
181 int read_message(StrBuf *Target, const char *tmpl, long tmpllen, long msgnum, const StrBuf *PartNum, const StrBuf **OutMime)
184 StrBuf *FoundCharset;
187 message_summary *Msg = NULL;
191 WCTemplputParams SuperTP;
192 WCTemplputParams SubTP;
193 StrBuf *Error = NULL;
195 memset(&SuperTP, 0, sizeof(WCTemplputParams));
196 memset(&SubTP, 0, sizeof(WCTemplputParams));
199 FoundCharset = NewStrBuf();
200 Msg = (message_summary *)malloc(sizeof(message_summary));
201 memset(Msg, 0, sizeof(message_summary));
202 Msg->msgnum = msgnum;
203 Msg->PartNum = PartNum;
204 Msg->MsgBody = (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
205 memset(Msg->MsgBody, 0, sizeof(wc_mime_attachment));
206 Msg->MsgBody->msgnum = msgnum;
208 if (!load_message(Msg, FoundCharset, &Error)) {
209 StrBufAppendBuf(Target, Error, 0);
213 /* Extract just the content-type (omit attributes such as "charset") */
214 StrBufExtract_token(Buf, Msg->MsgBody->ContentType, 0, ';');
216 StrBufLowerCase(Buf);
218 StackContext(NULL, &SuperTP, Msg, CTX_MAILSUM, 0, NULL);
220 /* Locate a renderer capable of converting this MIME part into HTML */
221 if (GetHash(MimeRenderHandler, SKEY(Buf), &vHdr) &&
223 RenderMimeFuncStruct *Render;
225 StackContext(&SuperTP, &SubTP, Msg->MsgBody, CTX_MIME_ATACH, 0, NULL);
227 Render = (RenderMimeFuncStruct*)vHdr;
228 Render->f(Target, &SubTP, FoundCharset);
230 UnStackContext(&SubTP);
233 if (StrLength(Msg->reply_references)> 0) {
234 /* Trim down excessively long lists of thread references. We eliminate the
235 * second one in the list so that the thread root remains intact.
237 int rrtok = num_tokens(ChrPtr(Msg->reply_references), '|');
238 int rrlen = StrLength(Msg->reply_references);
239 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
240 StrBufRemove_token(Msg->reply_references, 1, '|');
244 /* now check if we need to translate some mimeparts, and remove the duplicate */
245 it = GetNewHashPos(Msg->AllAttach, 0);
246 while (GetNextHashPos(Msg->AllAttach, it, &len, &Key, &vMime) &&
248 StackContext(&SuperTP, &SubTP, vMime, CTX_MIME_ATACH, 0, NULL);
250 evaluate_mime_part(Target, &SubTP);
252 UnStackContext(&SubTP);
255 *OutMime = DoTemplate(tmpl, tmpllen, Target, &SuperTP);
257 UnStackContext(&SuperTP);
259 DestroyMessageSummary(Msg);
260 FreeStrBuf(&FoundCharset);
267 HttpStatus(long CitadelStatus)
269 long httpstatus = 502;
271 switch (MAJORCODE(CitadelStatus))
273 case LISTING_FOLLOWS:
278 switch (MINORCODE(CitadelStatus))
286 case HIGHER_ACCESS_REQUIRED:
287 case MAX_SESSIONS_EXCEEDED:
289 case RESOURCE_NOT_OPEN:
291 case INVALID_FLOOR_OPERATION:
297 case MESSAGE_NOT_FOUND:
307 case CMD_NOT_SUPPORTED:
308 case PASSWORD_REQUIRED:
309 case ALREADY_LOGGED_IN:
310 case USERNAME_REQUIRED:
312 case SERVER_SHUTTING_DOWN:
323 case START_CHAT_MODE:
327 httpstatus = 502; /* aeh... whut? */
335 * Unadorned HTML output of an individual message, suitable
336 * for placing in a hidden iframe, for printing, or whatever
338 void handle_one_message(void)
340 long CitStatus = ERROR + NOT_HERE;
342 const StrBuf *Destination;
348 StrBuf *CmdBuf = NULL;
352 pMsg = strchr(ChrPtr(WCC->Hdr->HR.ReqLine), '/');
354 HttpStatus(CitStatus);
358 msgnum = atol(pMsg + 1);
359 StrBufCutAt(WCC->Hdr->HR.ReqLine, 0, pMsg);
360 gotoroom(WCC->Hdr->HR.ReqLine);
361 switch (WCC->Hdr->HR.eReqType)
365 Tmpl = sbstr("template");
366 if (StrLength(Tmpl) > 0)
367 read_message(WCC->WBuf, SKEY(Tmpl), msgnum, NULL, &Mime);
369 read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
370 http_transmit_thing(ChrPtr(Mime), 0);
373 CmdBuf = NewStrBuf ();
374 if ((WCC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
375 serv_printf("DELE %ld", msgnum);
377 else { /* Otherwise move it to Trash */
378 serv_printf("MOVE %ld|_TRASH_|0", msgnum);
380 StrBuf_ServGetln(CmdBuf);
381 GetServerStatusMsg(CmdBuf, &CitStatus, 1, 0);
382 HttpStatus(CitStatus);
387 if (GetHash(WCC->Hdr->HTTPHeaders, HKEY("DESTINATION"), &vLine) &&
389 Destination = (StrBuf*) vLine;
390 serv_printf("MOVE %ld|%s|%d", msgnum, ChrPtr(Destination), CopyMessage);
391 StrBuf_ServGetln(CmdBuf);
392 GetServerStatusMsg(CmdBuf, NULL, 1, 0);
393 GetServerStatus(CmdBuf, &CitStatus);
394 HttpStatus(CitStatus);
407 * Unadorned HTML output of an individual message, suitable
408 * for placing in a hidden iframe, for printing, or whatever
410 void embed_message(void) {
415 StrBuf *CmdBuf = NULL;
417 msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
418 if (msgnum <= 0) return;
420 switch (WCC->Hdr->HR.eReqType)
424 Tmpl = sbstr("template");
425 if (StrLength(Tmpl) > 0)
426 read_message(WCC->WBuf, SKEY(Tmpl), msgnum, NULL, &Mime);
428 read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
429 http_transmit_thing(ChrPtr(Mime), 0);
432 CmdBuf = NewStrBuf ();
433 if ((WCC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
434 serv_printf("DELE %ld", msgnum);
436 else { /* Otherwise move it to Trash */
437 serv_printf("MOVE %ld|_TRASH_|0", msgnum);
439 StrBuf_ServGetln(CmdBuf);
440 GetServerStatusMsg(CmdBuf, NULL, 1, 0);
450 * Printable view of a message
452 void print_message(void) {
456 msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
457 output_headers(0, 0, 0, 0, 0, 0);
459 hprintf("Content-type: text/html\r\n"
460 "Server: " PACKAGE_STRING "\r\n"
461 "Connection: close\r\n");
465 read_message(WC->WBuf, HKEY("view_message_print"), msgnum, NULL, &Mime);
471 * Display a message's headers
473 void display_headers(void) {
477 msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
478 output_headers(0, 0, 0, 0, 0, 0);
480 hprintf("Content-type: text/plain\r\n"
482 "Connection: close\r\n",
486 serv_printf("MSG2 %ld|3", msgnum);
487 serv_getln(buf, sizeof buf);
489 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
490 wc_printf("%s\n", buf);
498 message_summary *ReadOneMessageSummary(StrBuf *RawMessage, const char *DefaultSubject, long MsgNum)
501 MsgPartEvaluatorStruct *Eval;
502 message_summary *Msg;
511 serv_printf("MSG0 %ld|1", MsgNum); /* ask for headers only */
513 StrBuf_ServGetln(Buf);
514 if (GetServerStatus(Buf, NULL) == 1) {
519 Msg = (message_summary*)malloc(sizeof(message_summary));
520 memset(Msg, 0, sizeof(message_summary));
521 while (len = StrBuf_ServGetln(Buf),
524 strcmp(ChrPtr(Buf), "000")))
527 ebuf = strchr(ChrPtr(Buf), '=');
529 if (GetHash(MsgEvaluators, buf, nBuf, &vEval) && vEval != NULL) {
530 Eval = (MsgPartEvaluatorStruct*) vEval;
531 StrBufCutLeft(Buf, nBuf + 1);
534 else syslog(LOG_INFO, "Don't know how to handle Message Headerline [%s]", ChrPtr(Buf));
544 * load message pointers from the server for a "read messages" operation
546 * servcmd: the citadel command to send to the citserver
548 int load_msg_ptrs(const char *servcmd,
550 SharedMessageStatus *Stat,
551 load_msg_ptrs_detailheaders LH)
554 message_summary *Msg;
559 const char *Ptr = NULL;
562 Stat->lowest_found = LONG_MAX;
563 Stat->highest_found = LONG_MIN;
565 if (WCC->summ != NULL) {
566 DeleteHash(&WCC->summ);
568 WCC->summ = NewHash(1, Flathash);
572 StrBuf_ServGetln(Buf);
573 StatMajor = GetServerStatus(Buf, NULL);
578 if (filter != NULL) {
583 /* fall back to empty filter in case of we were fooled... */
589 return (Stat->nummsgs);
592 while (len = StrBuf_ServGetln(Buf),
595 strcmp(ChrPtr(Buf), "000")!= 0)))
597 if (Stat->nummsgs < Stat->maxload) {
600 Msg = (message_summary*)malloc(sizeof(message_summary));
601 memset(Msg, 0, sizeof(message_summary));
603 Msg->msgnum = StrBufExtractNext_long(Buf, &Ptr, '|');
604 Msg->date = StrBufExtractNext_long(Buf, &Ptr, '|');
606 if (Stat->nummsgs == 0) {
607 if (Msg->msgnum < Stat->lowest_found) {
608 Stat->lowest_found = Msg->msgnum;
610 if (Msg->msgnum > Stat->highest_found) {
611 Stat->highest_found = Msg->msgnum;
615 if ((Msg->msgnum == 0) && (StrLength(Buf) < 32)) {
621 * as citserver probably gives us messages in forward date sorting
622 * nummsgs should be the same order as the message date.
624 if (Msg->date == 0) {
625 Msg->date = Stat->nummsgs;
626 if (StrLength(Buf) < 32)
629 if ((!skipit) && (LH != NULL)) {
630 if (!LH(Buf, &Ptr, Msg, Buf2)){
636 Put(WCC->summ, (const char *)&n, sizeof(n), Msg, DestroyMessageSummary);
642 return (Stat->nummsgs);
648 * \brief sets FlagToSet for each of ScanMe that appears in MatchMSet
649 * \param ScanMe List of BasicMsgStruct to be searched it MatchSet
650 * \param MatchMSet MSet we want to flag
651 * \param FlagToSet Flag to set on each BasicMsgStruct->Flags if in MSet
653 void SetFlagsFromMSet(HashList *ScanMe, MSet *MatchMSet, int FlagToSet, int Reverse)
659 message_summary *Msg;
661 at = GetNewHashPos(ScanMe, 0);
662 while (GetNextHashPos(ScanMe, at, &HKLen, &HashKey, &vMsg)) {
663 /* Are you a new message, or an old message? */
664 Msg = (message_summary*) vMsg;
665 if (Reverse && IsInMSetList(MatchMSet, Msg->msgnum)) {
666 Msg->Flags = Msg->Flags | FlagToSet;
668 else if (!Reverse && !IsInMSetList(MatchMSet, Msg->msgnum)) {
669 Msg->Flags = Msg->Flags | FlagToSet;
676 void load_seen_flags(void)
682 OldMsg = NewStrBuf();
684 StrBuf_ServGetln(OldMsg);
685 if (GetServerStatus(OldMsg, NULL) == 2) {
686 StrBufCutLeft(OldMsg, 4);
693 if (ParseMSet(&MatchMSet, OldMsg))
695 SetFlagsFromMSet(WCC->summ, MatchMSet, MSGFLAG_READ, 0);
697 DeleteMSet(&MatchMSet);
701 extern readloop_struct rlid[];
703 typedef struct _RoomRenderer{
706 GetParamsGetServerCall_func GetParamsGetServerCall;
708 PrintViewHeader_func PrintPageHeader;
709 PrintViewHeader_func PrintViewHeader;
710 LoadMsgFromServer_func LoadMsgFromServer;
711 RenderView_or_Tail_func RenderView_or_Tail;
712 View_Cleanup_func ViewCleanup;
713 load_msg_ptrs_detailheaders LHParse;
718 * command loop for reading messages
720 * Set oper to "readnew" or "readold" or "readfwd" or "headers" or "readgt" or "readlt" or "do_search"
722 void readloop(long oper, eCustomRoomRenderer ForceRenderer)
724 RoomRenderer *ViewMsg;
727 message_summary *Msg;
729 char filter[256] = "";
735 WCTemplputParams SubTP;
736 SharedMessageStatus Stat;
739 if (havebstr("is_summary") && (1 == (ibstr("is_summary")))) {
740 WCC->CurRoom.view = VIEW_MAILBOX;
743 if (havebstr("view")) {
744 WCC->CurRoom.view = ibstr("view");
747 memset(&Stat, 0, sizeof(SharedMessageStatus));
748 Stat.maxload = 10000;
749 Stat.lowest_found = (-1);
750 Stat.highest_found = (-1);
751 if (ForceRenderer == eUseDefault)
752 GetHash(ReadLoopHandler, IKEY(WCC->CurRoom.view), &vViewMsg);
754 GetHash(ReadLoopHandler, IKEY(ForceRenderer), &vViewMsg);
755 if (vViewMsg == NULL) {
756 WCC->CurRoom.view = VIEW_BBS;
757 GetHash(ReadLoopHandler, IKEY(WCC->CurRoom.view), &vViewMsg);
759 if (vViewMsg == NULL) {
760 return; /* TODO: print message */
763 ViewMsg = (RoomRenderer*) vViewMsg;
764 if (ViewMsg->PrintPageHeader == NULL)
765 output_headers(1, 1, 1, 0, 0, 0);
767 ViewMsg->PrintPageHeader(&Stat, ViewSpecific);
769 if (ViewMsg->GetParamsGetServerCall != NULL) {
770 r = ViewMsg->GetParamsGetServerCall(
775 filter, sizeof(filter)
787 case 300: /* the callback hook should do the work for us here, since he knows what to do. */
793 if (!IsEmptyStr(cmd)) {
794 const char *p = NULL;
795 if (!IsEmptyStr(filter))
797 Stat.nummsgs = load_msg_ptrs(cmd, p, &Stat, ViewMsg->LHParse);
802 StackContext(NULL, &SubTP, NULL, CTX_MAILSUM, 0, NULL);
804 SortIt = RetrieveSort(&SubTP,
807 Stat.defaultsortorder);
809 UnStackContext(&SubTP);
811 SortByPayload(WCC->summ, SortIt);
813 if (Stat.startmsg < 0) {
817 if (Stat.load_seen) load_seen_flags();
820 * Print any inforation above the message list...
822 if (ViewMsg->PrintViewHeader != NULL)
823 ViewMsg->PrintViewHeader(&Stat, &ViewSpecific);
825 WCC->startmsg = Stat.startmsg;
826 WCC->maxmsgs = Stat.maxmsgs;
827 WCC->num_displayed = 0;
829 /* Put some helpful data in vars for mailsummary_json */
834 StrBufPrintf(Foo, "%ld", Stat.nummsgs);
835 PutBstr(HKEY("__READLOOP:TOTALMSGS"), NewStrBufDup(Foo));
836 StrBufPrintf(Foo, "%ld", Stat.startmsg);
837 PutBstr(HKEY("__READLOOP:STARTMSG"), Foo);
841 * iterate over each message. if we need to load an attachment, do it here.
844 if ((ViewMsg->LoadMsgFromServer != NULL) &&
847 at = GetNewHashPos(WCC->summ, 0);
848 Stat.num_displayed = i = 0;
849 while ( GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
850 Msg = (message_summary*) vMsg;
851 if ((Msg->msgnum >= Stat.startmsg) && (Stat.num_displayed <= Stat.maxmsgs)) {
852 ViewMsg->LoadMsgFromServer(&Stat,
855 (Msg->Flags & MSGFLAG_READ) != 0,
864 * Done iterating the message list. now tasks we want to do after.
866 if (ViewMsg->RenderView_or_Tail != NULL)
867 ViewMsg->RenderView_or_Tail(&Stat, &ViewSpecific, oper);
869 if (ViewMsg->ViewCleanup != NULL)
870 ViewMsg->ViewCleanup(&ViewSpecific);
874 if (WCC->summ != NULL) {
875 DeleteHash(&WCC->summ);
881 * Back end for post_message()
882 * ... this is where the actual message gets transmitted to the server.
884 void post_mime_to_server(void) {
886 char top_boundary[SIZ];
887 char alt_boundary[SIZ];
888 int is_multipart = 0;
890 wc_mime_attachment *att;
892 size_t encoded_length;
893 size_t encoded_strlen;
894 char *txtmail = NULL;
895 int include_text_alt = 0; /* Set to nonzero to include multipart/alternative text/plain */
897 sprintf(top_boundary, "Citadel--Multipart--%s--%04x--%04x",
898 ChrPtr(WCC->serv_info->serv_fqdn),
902 sprintf(alt_boundary, "Citadel--Multipart--%s--%04x--%04x",
903 ChrPtr(WCC->serv_info->serv_fqdn),
908 /* RFC2045 requires this, and some clients look for it... */
909 serv_puts("MIME-Version: 1.0");
910 serv_puts("X-Mailer: " PACKAGE_STRING);
912 /* If there are attachments, we have to do multipart/mixed */
913 if (GetCount(WCC->attachments) > 0) {
917 /* Only do multipart/alternative for mailboxes. BBS and Wiki rooms don't need it. */
918 if ((WCC->CurRoom.view == VIEW_MAILBOX) ||
919 (WCC->CurRoom.view == VIEW_JSON_LIST))
921 include_text_alt = 1;
925 /* Remember, serv_printf() appends an extra newline */
926 serv_printf("Content-type: multipart/mixed; boundary=\"%s\"\n", top_boundary);
927 serv_printf("This is a multipart message in MIME format.\n");
928 serv_printf("--%s", top_boundary);
931 /* Remember, serv_printf() appends an extra newline */
932 if (include_text_alt) {
933 serv_printf("Content-type: multipart/alternative; "
934 "boundary=\"%s\"\n", alt_boundary);
935 serv_printf("This is a multipart message in MIME format.\n");
936 serv_printf("--%s", alt_boundary);
938 serv_puts("Content-type: text/plain; charset=utf-8");
939 serv_puts("Content-Transfer-Encoding: quoted-printable");
941 txtmail = html_to_ascii(bstr("msgtext"), 0, 80, 0);
942 text_to_server_qp(txtmail); /* Transmit message in quoted-printable encoding */
945 serv_printf("\n--%s", alt_boundary);
948 serv_puts("Content-type: text/html; charset=utf-8");
949 serv_puts("Content-Transfer-Encoding: quoted-printable");
951 serv_puts("<html><body>\r\n");
952 text_to_server_qp(bstr("msgtext")); /* Transmit message in quoted-printable encoding */
953 serv_puts("</body></html>\r\n");
955 if (include_text_alt) {
956 serv_printf("--%s--", alt_boundary);
965 /* Add in the attachments */
966 it = GetNewHashPos(WCC->attachments, 0);
967 while (GetNextHashPos(WCC->attachments, it, &len, &Key, &vAtt)) {
968 att = (wc_mime_attachment *)vAtt;
969 if (att->length == 0)
971 encoded_length = ((att->length * 150) / 100);
972 encoded = malloc(encoded_length);
973 if (encoded == NULL) break;
974 encoded_strlen = CtdlEncodeBase64(encoded, ChrPtr(att->Data), StrLength(att->Data), 1);
976 serv_printf("--%s", top_boundary);
977 serv_printf("Content-type: %s", ChrPtr(att->ContentType));
978 serv_printf("Content-disposition: attachment; filename=\"%s\"", ChrPtr(att->FileName));
979 serv_puts("Content-transfer-encoding: base64");
981 serv_write(encoded, encoded_strlen);
986 serv_printf("--%s--", top_boundary);
995 * Post message (or don't post message)
997 * Note regarding the "dont_post" variable:
998 * A random value (actually, it's just a timestamp) is inserted as a hidden
999 * field called "postseq" when the display_enter page is generated. This
1000 * value is checked when posting, using the static variable dont_post. If a
1001 * user attempts to post twice using the same dont_post value, the message is
1002 * discarded. This prevents the accidental double-saving of the same message
1003 * if the user happens to click the browser "back" button.
1005 void post_message(void)
1008 StrBuf *EmailAddress;
1011 StrBuf *encoded_subject = NULL;
1012 static long dont_post = (-1L);
1013 int is_anonymous = 0;
1014 const StrBuf *display_name = NULL;
1015 wcsession *WCC = WC;
1018 if (havebstr("force_room")) {
1019 gotoroom(sbstr("force_room"));
1022 if (havebstr("display_name")) {
1023 display_name = sbstr("display_name");
1024 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1025 display_name = NULL;
1030 if (!strcasecmp(bstr("submit_action"), "cancel")) {
1031 AppendImportantMessage(_("Cancelled. Message was not posted."), -1);
1032 } else if (lbstr("postseq") == dont_post) {
1033 AppendImportantMessage(
1034 _("Automatically cancelled because you have already "
1035 "saved this message."), -1);
1037 const char CMD[] = "ENT0 1|%s|%d|4|%s|%s||%s|%s|%s|%s|%s";
1038 StrBuf *Recp = NULL;
1041 char *wikipage = NULL;
1042 const StrBuf *my_email_addr = NULL;
1043 StrBuf *CmdBuf = NULL;
1044 StrBuf *references = NULL;
1045 int saving_to_drafts = 0;
1048 saving_to_drafts = !strcasecmp(bstr("submit_action"), "draft");
1051 if (saving_to_drafts) {
1052 /* temporarily change to the drafts room */
1053 serv_puts("GOTO _DRAFTS_");
1054 StrBuf_ServGetln(Buf);
1055 if (GetServerStatusMsg(Buf, NULL, 1, 2) != 2) {
1056 /* You probably don't even have a dumb Drafts folder */
1057 syslog(LOG_DEBUG, "%s:%d: server save to drafts error: %s\n", __FILE__, __LINE__, ChrPtr(Buf) + 4);
1058 AppendImportantMessage(_("Saved to Drafts failed: "), -1);
1065 if (havebstr("references"))
1067 const StrBuf *ref = sbstr("references");
1068 references = NewStrBufDup(ref);
1069 if (*ChrPtr(references) == '|') { /* remove leading '|' if present */
1070 StrBufCutLeft(references, 1);
1072 StrBufReplaceChars(references, '|', '!');
1074 if (havebstr("subject")) {
1077 * make enough room for the encoded string;
1078 * plus the QP header
1080 Subj = sbstr("subject");
1082 StrBufRFC2047encode(&encoded_subject, Subj);
1084 UserName = NewStrBuf();
1085 EmailAddress = NewStrBuf();
1086 EncBuf = NewStrBuf();
1088 Recp = StrBufSanitizeEmailRecipientVector(sbstr("recp"), UserName, EmailAddress, EncBuf);
1089 Cc = StrBufSanitizeEmailRecipientVector(sbstr("cc"), UserName, EmailAddress, EncBuf);
1090 Bcc = StrBufSanitizeEmailRecipientVector(sbstr("bcc"), UserName, EmailAddress, EncBuf);
1092 FreeStrBuf(&UserName);
1093 FreeStrBuf(&EmailAddress);
1094 FreeStrBuf(&EncBuf);
1096 wikipage = strdup(bstr("page"));
1097 str_wiki_index(wikipage);
1098 my_email_addr = sbstr("my_email_addr");
1100 HeaderLen = StrLength(Recp) +
1101 StrLength(encoded_subject) +
1105 StrLength(my_email_addr) +
1106 StrLength(references);
1107 CmdBuf = NewStrBufPlain(NULL, sizeof (CMD) + HeaderLen);
1108 StrBufPrintf(CmdBuf,
1110 saving_to_drafts?"":ChrPtr(Recp),
1112 ChrPtr(encoded_subject),
1113 ChrPtr(display_name),
1114 saving_to_drafts?"":ChrPtr(Cc),
1115 saving_to_drafts?"":ChrPtr(Bcc),
1117 ChrPtr(my_email_addr),
1118 ChrPtr(references));
1119 FreeStrBuf(&references);
1120 FreeStrBuf(&encoded_subject);
1123 if ((HeaderLen + StrLength(sbstr("msgtext")) < 10) &&
1124 (GetCount(WCC->attachments) == 0)){
1125 AppendImportantMessage(_("Refusing to post empty message.\n"), -1);
1126 FreeStrBuf(&CmdBuf);
1131 syslog(LOG_DEBUG, "%s\n", ChrPtr(CmdBuf));
1132 serv_puts(ChrPtr(CmdBuf));
1133 FreeStrBuf(&CmdBuf);
1135 StrBuf_ServGetln(Buf);
1136 if (GetServerStatus(Buf, NULL) == 4) {
1137 if (saving_to_drafts) {
1138 if ( (havebstr("recp"))
1139 || (havebstr("cc" ))
1140 || (havebstr("bcc" )) ) {
1141 /* save recipient headers or room to post to */
1142 serv_printf("To: %s", ChrPtr(Recp));
1143 serv_printf("Cc: %s", ChrPtr(Cc));
1144 serv_printf("Bcc: %s", ChrPtr(Bcc));
1146 serv_printf("X-Citadel-Room: %s", ChrPtr(WCC->CurRoom.name));
1149 post_mime_to_server();
1150 if (saving_to_drafts) {
1151 AppendImportantMessage(_("Message has been saved to Drafts.\n"), -1);
1152 gotoroom(WCC->CurRoom.name);
1154 readloop(readnew, eUseDefault);
1157 } else if ( (havebstr("recp"))
1158 || (havebstr("cc" ))
1159 || (havebstr("bcc" ))
1161 AppendImportantMessage(_("Message has been sent.\n"), -1);
1164 AppendImportantMessage(_("Message has been posted.\n"), -1);
1166 dont_post = lbstr("postseq");
1168 syslog(LOG_DEBUG, "%s:%d: server post error: %s", __FILE__, __LINE__, ChrPtr(Buf) + 4);
1169 AppendImportantMessage(ChrPtr(Buf) + 4, StrLength(Buf) - 4);
1171 if (saving_to_drafts) gotoroom(WCC->CurRoom.name);
1185 DeleteHash(&WCC->attachments);
1188 * We may have been supplied with instructions regarding the location
1189 * to which we must return after posting. If found, go there.
1191 if (havebstr("return_to")) {
1192 http_redirect(bstr("return_to"));
1195 * If we were editing a page in a wiki room, go to that page now.
1197 else if (havebstr("page")) {
1198 snprintf(buf, sizeof buf, "wiki?page=%s", bstr("page"));
1202 * Otherwise, just go to the "read messages" loop.
1206 readloop(readnew, eUseDefault);
1212 * Client is uploading an attachment
1214 void upload_attachment(void) {
1215 wcsession *WCC = WC;
1221 wc_mime_attachment *att;
1222 const StrBuf *Tmpl = sbstr("template");
1223 const StrBuf *MimeType = NULL;
1227 syslog(LOG_DEBUG, "upload_attachment()\n");
1228 if (!Tmpl) wc_printf("upload_attachment()<br>\n");
1230 if (WCC->upload_length <= 0) {
1231 syslog(LOG_DEBUG, "ERROR no attachment was uploaded\n");
1234 putlbstr("UPLOAD_ERROR", 1);
1235 MimeType = DoTemplate(SKEY(Tmpl), NULL, &NoCtx);
1237 else wc_printf("ERROR no attachment was uploaded<br>\n");
1238 http_transmit_thing(ChrPtr(MimeType), 0);
1243 syslog(LOG_DEBUG, "Client is uploading %d bytes\n", WCC->upload_length);
1244 if (Tmpl) putlbstr("UPLOAD_LENGTH", WCC->upload_length);
1245 else wc_printf("Client is uploading %d bytes<br>\n", WCC->upload_length);
1247 att = (wc_mime_attachment*)malloc(sizeof(wc_mime_attachment));
1248 memset(att, 0, sizeof(wc_mime_attachment ));
1249 att->length = WCC->upload_length;
1250 att->ContentType = NewStrBufPlain(WCC->upload_content_type, -1);
1251 att->FileName = NewStrBufDup(WCC->upload_filename);
1252 UID = SBSTR("qquuid");
1254 att->PartNum = NewStrBufDup(UID);
1256 if (WCC->attachments == NULL) {
1257 WCC->attachments = NewHash(1, Flathash);
1260 /* And add it to the list. */
1262 if ((GetCount(WCC->attachments) > 0) &&
1263 GetHashAt(WCC->attachments,
1264 GetCount(WCC->attachments) -1,
1265 &newnlen, &newn, &v))
1266 n = *((int*) newn) + 1;
1267 Put(WCC->attachments, IKEY(n), att, DestroyMime);
1270 * Mozilla sends a simple filename, which is what we want,
1271 * but Satan's Browser sends an entire pathname. Reduce
1272 * the path to just a filename if we need to.
1274 pch = strrchr(ChrPtr(att->FileName), '/');
1276 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1278 pch = strrchr(ChrPtr(att->FileName), '\\');
1280 StrBufCutLeft(att->FileName, pch - ChrPtr(att->FileName) + 1);
1284 * Transfer control of this memory from the upload struct
1285 * to the attachment struct.
1287 att->Data = WCC->upload;
1289 WCC->upload_length = 0;
1291 if (Tmpl) MimeType = DoTemplate(SKEY(Tmpl), NULL, &NoCtx);
1292 http_transmit_thing(ChrPtr(MimeType), 0);
1297 * Remove an attachment from the message currently being composed.
1299 * Currently we identify the attachment to be removed by its filename.
1300 * There is probably a better way to do this.
1302 void remove_attachment(void) {
1303 wcsession *WCC = WC;
1304 wc_mime_attachment *att;
1306 StrBuf *WhichAttachment;
1312 WhichAttachment = NewStrBufDup(sbstr("which_attachment"));
1313 if (ChrPtr(WhichAttachment)[0] == '/')
1314 StrBufCutLeft(WhichAttachment, 1);
1315 StrBufUnescape(WhichAttachment, 0);
1316 at = GetNewHashPos(WCC->attachments, 0);
1319 GetHashPos(WCC->attachments, at, &len, &key, &vAtt);
1321 att = (wc_mime_attachment*) vAtt;
1322 if ((att != NULL) &&
1324 !strcmp(ChrPtr(WhichAttachment), ChrPtr(att->FileName)) ||
1325 ((att->PartNum != NULL) &&
1326 !strcmp(ChrPtr(WhichAttachment), ChrPtr(att->PartNum)))
1329 DeleteEntryFromHash(WCC->attachments, at);
1334 while (NextHashPos(WCC->attachments, at));
1336 FreeStrBuf(&WhichAttachment);
1337 wc_printf("remove_attachment(%d) completed\n", found);
1341 long FourHash(const char *key, long length)
1345 const unsigned char *ptr = (const unsigned char*)key;
1347 for (i = 0; i < 4; i++, ptr ++)
1368 const char *ReplyToModeStrings [3] = {
1373 typedef enum _eReplyToNodes {
1380 * display the message entry screen
1382 void display_enter(void)
1384 const char *ReplyingModeStr;
1385 eReplyToNodes ReplyMode = eReply;
1389 const StrBuf *display_name = NULL;
1390 int recipient_required = 0;
1391 int subject_required = 0;
1392 int is_anonymous = 0;
1393 wcsession *WCC = WC;
1397 if (havebstr("force_room")) {
1398 gotoroom(sbstr("force_room"));
1401 display_name = sbstr("display_name");
1402 if (!strcmp(ChrPtr(display_name), "__ANONYMOUS__")) {
1403 display_name = NULL;
1408 * First, do we have permission to enter messages in this room at all?
1411 serv_puts("ENT0 0");
1412 StrBuf_ServGetln(Line);
1413 rc = GetServerStatusMsg(Line, &Result, 0, 2);
1415 if (Result == 570) { /* 570 means that we need a recipient here */
1416 recipient_required = 1;
1418 else if (rc != 2) { /* Any other error means that we cannot continue */
1419 rc = GetServerStatusMsg(Line, &Result, 0, 2);
1421 readloop(readnew, eUseDefault);
1426 /* Is the server strongly recommending that the user enter a message subject? */
1427 if (StrLength(Line) > 4) {
1428 subject_required = extract_int(ChrPtr(Line) + 4, 1);
1432 * Are we perhaps in an address book view? If so, then an "enter
1433 * message" command really means "add new entry."
1435 if (WCC->CurRoom.defview == VIEW_ADDRESSBOOK) {
1436 do_edit_vcard(-1, "", NULL, NULL, "", ChrPtr(WCC->CurRoom.name));
1442 * Are we perhaps in a calendar room? If so, then an "enter
1443 * message" command really means "add new calendar item."
1445 if (WCC->CurRoom.defview == VIEW_CALENDAR) {
1446 display_edit_event();
1452 * Are we perhaps in a tasks view? If so, then an "enter
1453 * message" command really means "add new task."
1455 if (WCC->CurRoom.defview == VIEW_TASKS) {
1456 display_edit_task();
1462 ReplyingModeStr = bstr("replying_mode");
1463 if (ReplyingModeStr != NULL) for (i = 0; i < 3; i++) {
1464 if (strcmp(ReplyingModeStr, ReplyToModeStrings[i]) == 0) {
1465 ReplyMode = (eReplyToNodes) i;
1472 * If the "replying_to" variable is set, it refers to a message
1473 * number from which we must extract some header fields...
1475 replying_to = lbstr("replying_to");
1476 if (replying_to > 0) {
1478 StrBuf *wefw = NULL;
1479 StrBuf *msgn = NULL;
1480 StrBuf *from = NULL;
1481 StrBuf *node = NULL;
1482 StrBuf *rfca = NULL;
1483 StrBuf *rcpt = NULL;
1484 StrBuf *cccc = NULL;
1485 StrBuf *replyto = NULL;
1486 StrBuf *nvto = NULL;
1487 serv_printf("MSG0 %ld|1", replying_to);
1489 StrBuf_ServGetln(Line);
1490 if (GetServerStatusMsg(Line, NULL, 0, 0) == 1)
1491 while (len = StrBuf_ServGetln(Line),
1494 strcmp(ChrPtr(Line), "000")))
1497 if ((StrLength(Line) > 4) &&
1498 (ChrPtr(Line)[4] == '='))
1499 which = FourHash(ChrPtr(Line), 4);
1501 if (which == l_subj)
1503 StrBuf *subj = NewStrBuf();
1504 StrBuf *FlatSubject;
1506 if (ReplyMode == eForward) {
1507 if (strncasecmp(ChrPtr(Line) + 5, "Fw:", 3)) {
1508 StrBufAppendBufPlain(subj, HKEY("Fw: "), 0);
1512 if (strncasecmp(ChrPtr(Line) + 5, "Re:", 3)) {
1513 StrBufAppendBufPlain(subj, HKEY("Re: "), 0);
1516 StrBufAppendBufPlain(subj,
1518 StrLength(Line) - 5, 0);
1519 FlatSubject = NewStrBufPlain(NULL, StrLength(subj));
1520 StrBuf_RFC822_to_Utf8(FlatSubject, subj, NULL, NULL);
1522 PutBstr(HKEY("subject"), FlatSubject);
1525 else if (which == l_wefw)
1530 wefw = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1532 /* Trim down excessively long lists of thread references. We eliminate the
1533 * second one in the list so that the thread root remains intact.
1535 rrtok = num_tokens(ChrPtr(wefw), '|');
1536 rrlen = StrLength(wefw);
1537 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
1538 StrBufRemove_token(wefw, 1, '|');
1542 else if (which == l_msgn) {
1543 msgn = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1546 else if (which == l_from) {
1548 from = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1549 FlatFrom = NewStrBufPlain(NULL, StrLength(from));
1550 StrBuf_RFC822_to_Utf8(FlatFrom, from, NULL, NULL);
1553 for (i=0; i<StrLength(from); ++i) {
1554 if (ChrPtr(from)[i] == ',')
1555 StrBufPeek(from, NULL, i, ' ');
1559 else if (which == l_rcpt) {
1560 rcpt = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1563 else if (which == l_cccc) {
1564 cccc = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1567 else if (which == l_node) {
1568 node = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1570 else if (which == l_replyto) {
1571 replyto = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1573 else if (which == l_rfca) {
1575 rfca = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1576 FlatRFCA = NewStrBufPlain(NULL, StrLength(rfca));
1577 StrBuf_RFC822_to_Utf8(FlatRFCA, rfca, NULL, NULL);
1581 else if (which == l_nvto) {
1582 nvto = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
1583 putbstr("nvto", nvto);
1588 if (StrLength(wefw) + StrLength(msgn) > 0) {
1589 StrBuf *refs = NewStrBuf();
1590 if (StrLength(wefw) > 0) {
1591 StrBufAppendBuf(refs, wefw, 0);
1593 if ( (StrLength(wefw) > 0) &&
1594 (StrLength(msgn) > 0) )
1596 StrBufAppendBufPlain(refs, HKEY("|"), 0);
1598 if (StrLength(msgn) > 0) {
1599 StrBufAppendBuf(refs, msgn, 0);
1601 PutBstr(HKEY("references"), refs);
1605 * If this is a Reply or a ReplyAll, copy the sender's email into the To: field
1607 if ((ReplyMode == eReply) || (ReplyMode == eReplyAll))
1610 if ((StrLength(replyto) > 0) && (ReplyMode == eReplyAll)) {
1611 to_rcpt = NewStrBuf();
1612 StrBufAppendBuf(to_rcpt, replyto, 0);
1614 else if (StrLength(rfca) > 0) {
1615 to_rcpt = NewStrBuf();
1616 StrBufAppendBuf(to_rcpt, from, 0);
1617 StrBufAppendBufPlain(to_rcpt, HKEY(" <"), 0);
1618 StrBufAppendBuf(to_rcpt, rfca, 0);
1619 StrBufAppendBufPlain(to_rcpt, HKEY(">"), 0);
1624 if ( (StrLength(node) > 0)
1625 && (strcasecmp(ChrPtr(node), ChrPtr(WCC->serv_info->serv_nodename)))
1627 StrBufAppendBufPlain(to_rcpt, HKEY(" @ "), 0);
1628 StrBufAppendBuf(to_rcpt, node, 0);
1631 PutBstr(HKEY("recp"), to_rcpt);
1635 * Only if this is a ReplyAll, copy all recipients into the Cc: field
1637 if (ReplyMode == eReplyAll)
1639 StrBuf *cc_rcpt = rcpt;
1641 if ((StrLength(cccc) > 0) && (StrLength(replyto) == 0))
1643 if (cc_rcpt != NULL) {
1644 StrBufAppendPrintf(cc_rcpt, ", ");
1645 StrBufAppendBuf(cc_rcpt, cccc, 0);
1651 if (cc_rcpt != NULL)
1652 PutBstr(HKEY("cc"), cc_rcpt);
1664 * Otherwise proceed normally.
1665 * Do a custom room banner with no navbar...
1668 if (recipient_required) {
1669 const StrBuf *Recp = NULL;
1670 const StrBuf *Cc = NULL;
1671 const StrBuf *Bcc = NULL;
1672 char *wikipage = NULL;
1673 StrBuf *CmdBuf = NULL;
1674 const char CMD[] = "ENT0 0|%s|%d|0||%s||%s|%s|%s";
1676 Recp = sbstr("recp");
1679 wikipage = strdup(bstr("page"));
1680 str_wiki_index(wikipage);
1682 CmdBuf = NewStrBufPlain(NULL,
1685 StrLength(display_name) +
1690 StrBufPrintf(CmdBuf,
1694 ChrPtr(display_name),
1699 serv_puts(ChrPtr(CmdBuf));
1700 StrBuf_ServGetln(CmdBuf);
1703 rc = GetServerStatusMsg(CmdBuf, &Result, 0, 0);
1705 if ( (Result == 570) /* invalid or missing recipient(s) */
1706 || (Result == 550) /* higher access required to send Internet mail */
1708 /* These errors will have been displayed and are excusable */
1710 else if (rc != 2) { /* Any other error means that we cannot continue */
1711 AppendImportantMessage(ChrPtr(CmdBuf) + 4, StrLength(CmdBuf) - 4);
1712 FreeStrBuf(&CmdBuf);
1714 readloop(readnew, eUseDefault);
1717 FreeStrBuf(&CmdBuf);
1719 if (recipient_required)
1720 PutBstr(HKEY("__RCPTREQUIRED"), NewStrBufPlain(HKEY("1")));
1721 if (recipient_required || subject_required)
1722 PutBstr(HKEY("__SUBJREQUIRED"), NewStrBufPlain(HKEY("1")));
1725 output_headers(1, 0, 0, 0, 1, 0);
1726 DoTemplate(HKEY("edit_message"), NULL, &NoCtx);
1735 void delete_msg(void)
1740 msgid = lbstr("msgid");
1742 if ((WC->CurRoom.RAFlags & UA_ISTRASH) != 0) { /* Delete from Trash is a real delete */
1743 serv_printf("DELE %ld", msgid);
1745 else { /* Otherwise move it to Trash */
1746 serv_printf("MOVE %ld|_TRASH_|0", msgid);
1749 StrBuf_ServGetln(Line);
1750 GetServerStatusMsg(Line, NULL, 1, 0);
1754 readloop(readnew, eUseDefault);
1759 * move a message to another room
1765 msgid = lbstr("msgid");
1767 if (havebstr("move_button")) {
1769 serv_printf("MOVE %ld|%s", msgid, bstr("target_room"));
1771 StrBuf_ServGetln(Line);
1772 GetServerStatusMsg(Line, NULL, 1, 0);
1775 AppendImportantMessage(_("The message was not moved."), -1);
1779 readloop(readnew, eUseDefault);
1785 * Generic function to output an arbitrary MIME attachment from
1786 * message being composed
1788 * partnum The MIME part to be output
1789 * filename Fake filename to give
1790 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1792 void postpart(StrBuf *partnum, StrBuf *filename, int force_download)
1795 StrBuf *content_type;
1796 wc_mime_attachment *part;
1799 i = StrToi(partnum);
1800 if (GetHash(WC->attachments, IKEY(i), &vPart) &&
1802 part = (wc_mime_attachment*) vPart;
1803 if (force_download) {
1804 content_type = NewStrBufPlain(HKEY("application/octet-stream"));
1807 content_type = NewStrBufDup(part->ContentType);
1809 StrBufAppendBuf(WC->WBuf, part->Data, 0);
1810 http_transmit_thing(ChrPtr(content_type), 0);
1812 hprintf("HTTP/1.1 404 %s\n", ChrPtr(partnum));
1813 output_headers(0, 0, 0, 0, 0, 0);
1814 hprintf("Content-Type: text/plain\r\n");
1816 wc_printf(_("An error occurred while retrieving this part: %s/%s\n"),
1817 ChrPtr(partnum), ChrPtr(filename));
1820 FreeStrBuf(&content_type);
1825 * Generic function to output an arbitrary MIME part from an arbitrary
1826 * message number on the server.
1828 * msgnum Number of the item on the citadel server
1829 * partnum The MIME part to be output
1830 * force_download Nonzero to force set the Content-Type: header to "application/octet-stream"
1832 void mimepart(int force_download)
1837 wcsession *WCC = WC;
1840 StrBuf *ContentType = NewStrBufPlain(HKEY("application/octet-stream"));
1843 att = Buf = NewStrBuf();
1844 msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
1845 StrBufExtract_token(att, WCC->Hdr->HR.ReqLine, 1, '/');
1847 serv_printf("OPNA %ld|%s", msgnum, ChrPtr(att));
1848 StrBuf_ServGetln(Buf);
1849 if (GetServerStatus(Buf, &ErrorDetail) == 2) {
1850 StrBufCutLeft(Buf, 4);
1851 bytes = StrBufExtract_long(Buf, 0, '|');
1852 if (!force_download) {
1853 StrBufExtract_token(ContentType, Buf, 3, '|');
1856 serv_read_binary(WCC->WBuf, bytes, Buf);
1858 StrBuf_ServGetln(Buf);
1859 CT = ChrPtr(ContentType);
1861 if (!force_download) {
1862 if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
1863 StrBufExtract_token(Buf, WCC->Hdr->HR.ReqLine, 2, '/');
1864 CT = GuessMimeByFilename(SKEY(Buf));
1866 if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
1867 CT = GuessMimeType(SKEY(WCC->WBuf));
1870 http_transmit_thing(CT, 0);
1872 StrBufCutLeft(Buf, 4);
1873 switch (ErrorDetail) {
1875 case ERROR + MESSAGE_NOT_FOUND:
1876 hprintf("HTTP/1.1 404 %s\n", ChrPtr(Buf));
1878 case ERROR + NOT_LOGGED_IN:
1879 hprintf("HTTP/1.1 401 %s\n", ChrPtr(Buf));
1882 case ERROR + HIGHER_ACCESS_REQUIRED:
1883 hprintf("HTTP/1.1 403 %s\n", ChrPtr(Buf));
1885 case ERROR + INTERNAL_ERROR:
1886 case ERROR + TOO_BIG:
1887 hprintf("HTTP/1.1 500 %s\n", ChrPtr(Buf));
1890 output_headers(0, 0, 0, 0, 0, 0);
1891 hprintf("Content-Type: text/plain\r\n");
1893 wc_printf(_("An error occurred while retrieving this part: %s\n"),
1897 FreeStrBuf(&ContentType);
1903 * Read any MIME part of a message, from the server, into memory.
1905 StrBuf *load_mimepart(long msgnum, char *partnum)
1911 serv_printf("DLAT %ld|%s", msgnum, partnum);
1912 StrBuf_ServGetln(Buf);
1913 if (GetServerStatus(Buf, NULL) == 6) {
1914 StrBufCutLeft(Buf, 4);
1915 bytes = StrBufExtract_long(Buf, 0, '|');
1918 StrBuf_ServGetBLOBBuffered(Buf, bytes);
1928 * Read any MIME part of a message, from the server, into memory.
1930 void MimeLoadData(wc_mime_attachment *Mime)
1935 /* TODO: is there a chance the content type is different from the one we know? */
1937 serv_printf("DLAT %ld|%s", Mime->msgnum, ChrPtr(Mime->PartNum));
1939 StrBuf_ServGetln(Buf);
1940 if (GetServerStatus(Buf, NULL) == 6) {
1941 Ptr = &(ChrPtr(Buf)[4]);
1942 bytes = StrBufExtractNext_long(Buf, &Ptr, '|');
1943 StrBufSkip_NTokenS(Buf, &Ptr, '|', 3); /* filename, cbtype, mimetype */
1944 if (Mime->Charset == NULL) Mime->Charset = NewStrBuf();
1945 StrBufExtract_NextToken(Mime->Charset, Buf, &Ptr, '|');
1947 if (Mime->Data == NULL)
1948 Mime->Data = NewStrBufPlain(NULL, bytes);
1949 StrBuf_ServGetBLOBBuffered(Mime->Data, bytes);
1952 FlushStrBuf(Mime->Data);
1953 /* TODO XImportant message */
1959 void view_mimepart(void) {
1963 void download_mimepart(void) {
1967 void view_postpart(void) {
1968 StrBuf *filename = NewStrBuf();
1969 StrBuf *partnum = NewStrBuf();
1971 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1972 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1974 postpart(partnum, filename, 0);
1976 FreeStrBuf(&filename);
1977 FreeStrBuf(&partnum);
1980 void download_postpart(void) {
1981 StrBuf *filename = NewStrBuf();
1982 StrBuf *partnum = NewStrBuf();
1984 StrBufExtract_token(partnum, WC->Hdr->HR.ReqLine, 0, '/');
1985 StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
1987 postpart(partnum, filename, 1);
1989 FreeStrBuf(&filename);
1990 FreeStrBuf(&partnum);
1995 void show_num_attachments(void) {
1996 wc_printf("%d", GetCount(WC->attachments));
2000 void h_readnew(void) { readloop(readnew, eUseDefault);}
2001 void h_readold(void) { readloop(readold, eUseDefault);}
2002 void h_readfwd(void) { readloop(readfwd, eUseDefault);}
2003 void h_headers(void) { readloop(headers, eUseDefault);}
2004 void h_do_search(void) { readloop(do_search, eUseDefault);}
2005 void h_readgt(void) { readloop(readgt, eUseDefault);}
2006 void h_readlt(void) { readloop(readlt, eUseDefault);}
2010 /* Output message list in JSON format */
2011 void jsonMessageList(void) {
2012 StrBuf *View = NewStrBuf();
2013 const StrBuf *room = sbstr("room");
2014 long oper = (havebstr("query")) ? do_search : readnew;
2015 StrBufPrintf(View, "%d", VIEW_JSON_LIST);
2016 putbstr("view", View);;
2018 readloop(oper, eUseDefault);
2021 void RegisterReadLoopHandlerset(
2023 GetParamsGetServerCall_func GetParamsGetServerCall,
2024 PrintViewHeader_func PrintPageHeader,
2025 PrintViewHeader_func PrintViewHeader,
2026 load_msg_ptrs_detailheaders LH,
2027 LoadMsgFromServer_func LoadMsgFromServer,
2028 RenderView_or_Tail_func RenderView_or_Tail,
2029 View_Cleanup_func ViewCleanup
2032 RoomRenderer *Handler;
2034 Handler = (RoomRenderer*) malloc(sizeof(RoomRenderer));
2036 Handler->RoomType = RoomType;
2037 Handler->GetParamsGetServerCall = GetParamsGetServerCall;
2038 Handler->PrintPageHeader = PrintPageHeader;
2039 Handler->PrintViewHeader = PrintViewHeader;
2040 Handler->LoadMsgFromServer = LoadMsgFromServer;
2041 Handler->RenderView_or_Tail = RenderView_or_Tail;
2042 Handler->ViewCleanup = ViewCleanup;
2043 Handler->LHParse = LH;
2045 Put(ReadLoopHandler, IKEY(RoomType), Handler, NULL);
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);
2099 l_subj = FourHash("subj", 4);
2100 l_wefw = FourHash("wefw", 4);
2101 l_msgn = FourHash("msgn", 4);
2102 l_from = FourHash("from", 4);
2103 l_rcpt = FourHash("rcpt", 4);
2104 l_cccc = FourHash("cccc", 4);
2105 l_replyto = FourHash("rep2", 4);
2106 l_node = FourHash("node", 4);
2107 l_rfca = FourHash("rfca", 4);
2108 l_nvto = FourHash("nvto", 4);
2114 SessionDetachModule_MSG
2117 DeleteHash(&sess->summ);