1 // Implements the message store.
3 // Copyright (c) 1987-2023 by the citadel.org team
5 // This program is open source software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License version 3.
14 #include <libcitadel.h>
15 #include "ctdl_module.h"
16 #include "citserver.h"
19 #include "clientsocket.h"
23 #include "internet_addressing.h"
24 #include "euidindex.h"
26 #include "journaling.h"
28 struct addresses_to_be_filed *atbf = NULL;
30 // These are the four-character field headers we use when outputting
31 // messages in Citadel format (as opposed to RFC822 format).
33 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
34 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
35 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
36 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
37 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
38 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
39 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
40 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
42 "from", // A -> eAuthor
43 NULL, // B -> eBig_message
44 NULL, // C (formerly used as eRemoteRoom)
45 NULL, // D (formerly used as eDestination)
46 "exti", // E -> eXclusivID
47 "rfca", // F -> erFc822Addr
49 "hnod", // H (formerly used as eHumanNode)
50 "msgn", // I -> emessageId
51 "jrnl", // J -> eJournal
52 "rep2", // K -> eReplyTo
53 "list", // L -> eListID
54 "text", // M -> eMesageText
55 NULL, // N (formerly used as eNodename)
56 "room", // O -> eOriginalRoom
57 "path", // P -> eMessagePath
59 "rcpt", // R -> eRecipient
60 NULL, // S (formerly used as eSpecialField)
61 "time", // T -> eTimestamp
62 "subj", // U -> eMsgSubject
63 "nvto", // V -> eenVelopeTo
64 "wefw", // W -> eWeferences
66 "cccc", // Y -> eCarbonCopY
71 HashList *msgKeyLookup = NULL;
73 int GetFieldFromMnemonic(eMsgField *f, const char* c) {
75 if (GetHash(msgKeyLookup, c, 4, &v)) {
82 void FillMsgKeyLookupTable(void) {
85 msgKeyLookup = NewHash (1, FourHash);
87 for (i=0; i < 91; i++) {
88 if (msgkeys[i] != NULL) {
89 Put(msgKeyLookup, msgkeys[i], 4, (void*)i, reference_free_handler);
95 eMsgField FieldOrder[] = {
96 /* Important fields */
104 /* Semi-important fields */
109 /* G is not used yet */
112 /* Q is not used yet */
114 /* X is not used yet */
115 /* Z is not used yet */
122 /* Message text (MUST be last) */
124 /* Not saved to disk:
129 static const long NDiskFields = sizeof(FieldOrder) / sizeof(eMsgField);
132 int CM_IsEmpty(struct CtdlMessage *Msg, eMsgField which) {
133 return !((Msg->cm_fields[which] != NULL) && (Msg->cm_fields[which][0] != '\0'));
137 void CM_SetField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length) {
138 if (Msg->cm_fields[which] != NULL) {
139 free (Msg->cm_fields[which]);
141 if (length < 0) { // You can set the length to -1 to have CM_SetField measure it for you
142 length = strlen(buf);
144 Msg->cm_fields[which] = malloc(length + 1);
145 memcpy(Msg->cm_fields[which], buf, length);
146 Msg->cm_fields[which][length] = '\0';
147 Msg->cm_lengths[which] = length;
151 void CM_SetFieldLONG(struct CtdlMessage *Msg, eMsgField which, long lvalue) {
154 len = snprintf(buf, sizeof(buf), "%ld", lvalue);
155 CM_SetField(Msg, which, buf, len);
159 void CM_CutFieldAt(struct CtdlMessage *Msg, eMsgField WhichToCut, long maxlen) {
160 if (Msg->cm_fields[WhichToCut] == NULL)
163 if (Msg->cm_lengths[WhichToCut] > maxlen)
165 Msg->cm_fields[WhichToCut][maxlen] = '\0';
166 Msg->cm_lengths[WhichToCut] = maxlen;
171 void CM_FlushField(struct CtdlMessage *Msg, eMsgField which) {
172 if (Msg->cm_fields[which] != NULL)
173 free (Msg->cm_fields[which]);
174 Msg->cm_fields[which] = NULL;
175 Msg->cm_lengths[which] = 0;
179 void CM_Flush(struct CtdlMessage *Msg) {
182 if (CM_IsValidMsg(Msg) == 0) {
186 for (i = 0; i < 256; ++i) {
187 CM_FlushField(Msg, i);
192 void CM_CopyField(struct CtdlMessage *Msg, eMsgField WhichToPutTo, eMsgField WhichtToCopy) {
194 if (Msg->cm_fields[WhichToPutTo] != NULL) {
195 free (Msg->cm_fields[WhichToPutTo]);
198 if (Msg->cm_fields[WhichtToCopy] != NULL) {
199 len = Msg->cm_lengths[WhichtToCopy];
200 Msg->cm_fields[WhichToPutTo] = malloc(len + 1);
201 memcpy(Msg->cm_fields[WhichToPutTo], Msg->cm_fields[WhichtToCopy], len);
202 Msg->cm_fields[WhichToPutTo][len] = '\0';
203 Msg->cm_lengths[WhichToPutTo] = len;
206 Msg->cm_fields[WhichToPutTo] = NULL;
207 Msg->cm_lengths[WhichToPutTo] = 0;
212 void CM_PrependToField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length) {
213 if (Msg->cm_fields[which] != NULL) {
218 oldmsgsize = Msg->cm_lengths[which] + 1;
219 newmsgsize = length + oldmsgsize;
221 new = malloc(newmsgsize);
222 memcpy(new, buf, length);
223 memcpy(new + length, Msg->cm_fields[which], oldmsgsize);
224 free(Msg->cm_fields[which]);
225 Msg->cm_fields[which] = new;
226 Msg->cm_lengths[which] = newmsgsize - 1;
229 Msg->cm_fields[which] = malloc(length + 1);
230 memcpy(Msg->cm_fields[which], buf, length);
231 Msg->cm_fields[which][length] = '\0';
232 Msg->cm_lengths[which] = length;
237 void CM_SetAsField(struct CtdlMessage *Msg, eMsgField which, char **buf, long length) {
238 if (Msg->cm_fields[which] != NULL) {
239 free (Msg->cm_fields[which]);
242 Msg->cm_fields[which] = *buf;
244 if (length < 0) { // You can set the length to -1 to have CM_SetField measure it for you
245 Msg->cm_lengths[which] = strlen(Msg->cm_fields[which]);
248 Msg->cm_lengths[which] = length;
253 void CM_SetAsFieldSB(struct CtdlMessage *Msg, eMsgField which, StrBuf **buf) {
254 if (Msg->cm_fields[which] != NULL) {
255 free (Msg->cm_fields[which]);
258 Msg->cm_lengths[which] = StrLength(*buf);
259 Msg->cm_fields[which] = SmashStrBuf(buf);
263 void CM_GetAsField(struct CtdlMessage *Msg, eMsgField which, char **ret, long *retlen) {
264 if (Msg->cm_fields[which] != NULL) {
265 *retlen = Msg->cm_lengths[which];
266 *ret = Msg->cm_fields[which];
267 Msg->cm_fields[which] = NULL;
268 Msg->cm_lengths[which] = 0;
277 // Returns 1 if the supplied pointer points to a valid Citadel message.
278 // If the pointer is NULL or the magic number check fails, returns 0.
279 int CM_IsValidMsg(struct CtdlMessage *msg) {
283 if ((msg->cm_magic) != CTDLMESSAGE_MAGIC) {
284 syslog(LOG_WARNING, "msgbase: CM_IsValidMsg() self-check failed");
291 void CM_FreeContents(struct CtdlMessage *msg) {
294 for (i = 0; i < 256; ++i)
295 if (msg->cm_fields[i] != NULL) {
296 free(msg->cm_fields[i]);
297 msg->cm_lengths[i] = 0;
300 msg->cm_magic = 0; // just in case
304 // 'Destructor' for struct CtdlMessage
305 void CM_Free(struct CtdlMessage *msg) {
306 if (CM_IsValidMsg(msg) == 0) {
307 if (msg != NULL) free (msg);
310 CM_FreeContents(msg);
315 int CM_DupField(eMsgField i, struct CtdlMessage *OrgMsg, struct CtdlMessage *NewMsg) {
317 len = OrgMsg->cm_lengths[i];
318 NewMsg->cm_fields[i] = malloc(len + 1);
319 if (NewMsg->cm_fields[i] == NULL) {
322 memcpy(NewMsg->cm_fields[i], OrgMsg->cm_fields[i], len);
323 NewMsg->cm_fields[i][len] = '\0';
324 NewMsg->cm_lengths[i] = len;
329 struct CtdlMessage *CM_Duplicate(struct CtdlMessage *OrgMsg) {
331 struct CtdlMessage *NewMsg;
333 if (CM_IsValidMsg(OrgMsg) == 0) {
336 NewMsg = (struct CtdlMessage *)malloc(sizeof(struct CtdlMessage));
337 if (NewMsg == NULL) {
341 memcpy(NewMsg, OrgMsg, sizeof(struct CtdlMessage));
343 memset(&NewMsg->cm_fields, 0, sizeof(char*) * 256);
345 for (i = 0; i < 256; ++i) {
346 if (OrgMsg->cm_fields[i] != NULL) {
347 if (!CM_DupField(i, OrgMsg, NewMsg)) {
358 // Determine if a given message matches the fields in a message template.
359 // Return 0 for a successful match.
360 int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template) {
363 // If there aren't any fields in the template, all messages will match.
364 if (template == NULL) return(0);
366 // Null messages are bogus.
367 if (msg == NULL) return(1);
369 for (i='A'; i<='Z'; ++i) {
370 if (template->cm_fields[i] != NULL) {
371 if (msg->cm_fields[i] == NULL) {
372 // Considered equal if temmplate is empty string
373 if (IsEmptyStr(template->cm_fields[i])) continue;
376 if ((template->cm_lengths[i] != msg->cm_lengths[i]) ||
377 (strcasecmp(msg->cm_fields[i], template->cm_fields[i])))
382 // All compares succeeded: we have a match!
387 // Retrieve the "seen" message list for the current room.
388 void CtdlGetSeen(char *buf, int which_set) {
391 // Learn about the user and room in question
392 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
394 if (which_set == ctdlsetseen_seen) {
395 safestrncpy(buf, vbuf.v_seen, SIZ);
397 if (which_set == ctdlsetseen_answered) {
398 safestrncpy(buf, vbuf.v_answered, SIZ);
403 // Manipulate the "seen msgs" string (or other message set strings)
404 void CtdlSetSeen(long *target_msgnums, int num_target_msgnums,
405 int target_setting, int which_set,
406 struct ctdluser *which_user, struct ctdlroom *which_room) {
420 char *is_set; // actually an array of booleans
422 // Don't bother doing *anything* if we were passed a list of zero messages
423 if (num_target_msgnums < 1) {
427 // If no room was specified, we go with the current room.
429 which_room = &CC->room;
432 // If no user was specified, we go with the current user.
434 which_user = &CC->user;
437 syslog(LOG_DEBUG, "msgbase: CtdlSetSeen(%d msgs starting with %ld, %s, %d) in <%s>",
438 num_target_msgnums, target_msgnums[0],
439 (target_setting ? "SET" : "CLEAR"),
443 // Learn about the user and room in question
444 CtdlGetRelationship(&vbuf, which_user, which_room);
446 // Load the message list
447 num_msgs = CtdlFetchMsgList(which_room->QRnumber, &msglist);
453 is_set = malloc(num_msgs * sizeof(char));
454 memset(is_set, 0, (num_msgs * sizeof(char)) );
456 // Decide which message set we're manipulating
458 case ctdlsetseen_seen:
459 vset = NewStrBufPlain(vbuf.v_seen, -1);
461 case ctdlsetseen_answered:
462 vset = NewStrBufPlain(vbuf.v_answered, -1);
468 // Translate the existing sequence set into an array of booleans
469 setstr = NewStrBuf();
473 while (StrBufExtract_NextToken(setstr, vset, &pvset, ',') >= 0) {
475 StrBufExtract_token(lostr, setstr, 0, ':');
476 if (StrBufNum_tokens(setstr, ':') >= 2) {
477 StrBufExtract_token(histr, setstr, 1, ':');
481 StrBufAppendBuf(histr, lostr, 0);
484 if (!strcmp(ChrPtr(histr), "*")) {
491 for (i = 0; i < num_msgs; ++i) {
492 if ((msglist[i] >= lo) && (msglist[i] <= hi)) {
501 // Now translate the array of booleans back into a sequence set
507 for (i=0; i<num_msgs; ++i) {
511 for (k=0; k<num_target_msgnums; ++k) {
512 if (msglist[i] == target_msgnums[k]) {
513 is_seen = target_setting;
517 if ((was_seen == 0) && (is_seen == 1)) {
520 else if ((was_seen == 1) && (is_seen == 0)) {
523 if (StrLength(vset) > 0) {
524 StrBufAppendBufPlain(vset, HKEY(","), 0);
527 StrBufAppendPrintf(vset, "%ld", hi);
530 StrBufAppendPrintf(vset, "%ld:%ld", lo, hi);
534 if ((is_seen) && (i == num_msgs - 1)) {
535 if (StrLength(vset) > 0) {
536 StrBufAppendBufPlain(vset, HKEY(","), 0);
538 if ((i==0) || (was_seen == 0)) {
539 StrBufAppendPrintf(vset, "%ld", msglist[i]);
542 StrBufAppendPrintf(vset, "%ld:%ld", lo, msglist[i]);
549 // We will have to stuff this string back into a 4096 byte buffer, so if it's
550 // larger than that now, truncate it by removing tokens from the beginning.
551 // The limit of 100 iterations is there to prevent an infinite loop in case
552 // something unexpected happens.
553 int number_of_truncations = 0;
554 while ( (StrLength(vset) > SIZ) && (number_of_truncations < 100) ) {
555 StrBufRemove_token(vset, 0, ',');
556 ++number_of_truncations;
559 // If we're truncating the sequence set of messages marked with the 'seen' flag,
560 // we want the earliest messages (the truncated ones) to be marked, not unmarked.
561 // Otherwise messages at the beginning will suddenly appear to be 'unseen'.
562 if ( (which_set == ctdlsetseen_seen) && (number_of_truncations > 0) ) {
564 first_tok = NewStrBuf();
565 StrBufExtract_token(first_tok, vset, 0, ',');
566 StrBufRemove_token(vset, 0, ',');
568 if (StrBufNum_tokens(first_tok, ':') > 1) {
569 StrBufRemove_token(first_tok, 0, ':');
573 new_set = NewStrBuf();
574 StrBufAppendBufPlain(new_set, HKEY("1:"), 0);
575 StrBufAppendBuf(new_set, first_tok, 0);
576 StrBufAppendBufPlain(new_set, HKEY(":"), 0);
577 StrBufAppendBuf(new_set, vset, 0);
580 FreeStrBuf(&first_tok);
584 // Decide which message set we're manipulating. Zero the buffers so they compress well.
586 case ctdlsetseen_seen:
587 memset(vbuf.v_seen, 0, sizeof vbuf.v_seen);
588 safestrncpy(vbuf.v_seen, ChrPtr(vset), sizeof vbuf.v_seen);
590 case ctdlsetseen_answered:
591 memset(vbuf.v_answered, 0, sizeof vbuf.v_seen);
592 safestrncpy(vbuf.v_answered, ChrPtr(vset), sizeof vbuf.v_answered);
598 CtdlSetRelationship(&vbuf, which_user, which_room);
603 // API function to perform an operation for each qualifying message in the
604 // current room. (Returns the number of messages processed.)
605 int CtdlForEachMessage(int mode, long ref, char *search_string,
607 struct CtdlMessage *compare,
608 ForEachMsgCallback CallBack,
613 long *msglist = NULL;
615 int num_processed = 0;
618 struct CtdlMessage *msg = NULL;
621 int printed_lastold = 0;
622 int num_search_msgs = 0;
623 long *search_msgs = NULL;
625 int need_to_free_re = 0;
628 if ((content_type) && (!IsEmptyStr(content_type))) {
629 regcomp(&re, content_type, 0);
633 // Learn about the user and room in question
634 if (server_shutting_down) {
635 if (need_to_free_re) regfree(&re);
638 CtdlGetUser(&CC->user, CC->curr_user);
640 if (server_shutting_down) {
641 if (need_to_free_re) regfree(&re);
644 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
646 if (server_shutting_down) {
647 if (need_to_free_re) regfree(&re);
651 // Load the message list
652 num_msgs = CtdlFetchMsgList(CC->room.QRnumber, &msglist);
655 if (need_to_free_re) regfree(&re);
656 return 0; // No messages at all? No further action.
660 * Now begin the traversal.
662 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
664 /* If the caller is looking for a specific MIME type, filter
665 * out all messages which are not of the type requested.
667 if ((content_type != NULL) && (!IsEmptyStr(content_type))) {
669 /* This call to GetMetaData() sits inside this loop
670 * so that we only do the extra database read per msg
671 * if we need to. Doing the extra read all the time
672 * really kills the server. If we ever need to use
673 * metadata for another search criterion, we need to
674 * move the read somewhere else -- but still be smart
675 * enough to only do the read if the caller has
676 * specified something that will need it.
678 if (server_shutting_down) {
679 if (need_to_free_re) regfree(&re);
683 GetMetaData(&smi, msglist[a]);
685 /* if (strcasecmp(smi.meta_content_type, content_type)) { old non-regex way */
686 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) != 0) {
692 num_msgs = sort_msglist(msglist, num_msgs);
694 /* If a template was supplied, filter out the messages which
695 * don't match. (This could induce some delays!)
698 if (compare != NULL) {
699 for (a = 0; a < num_msgs; ++a) {
700 if (server_shutting_down) {
701 if (need_to_free_re) regfree(&re);
705 msg = CtdlFetchMessage(msglist[a], 1);
707 if (CtdlMsgCmp(msg, compare)) {
716 /* If a search string was specified, get a message list from
717 * the full text index and remove messages which aren't on both
721 * Since the lists are sorted and strictly ascending, and the
722 * output list is guaranteed to be shorter than or equal to the
723 * input list, we overwrite the bottom of the input list. This
724 * eliminates the need to memmove big chunks of the list over and
727 if ( (num_msgs > 0) && (mode == MSGS_SEARCH) && (search_string) ) {
729 /* Call search module via hook mechanism.
730 * NULL means use any search function available.
731 * otherwise replace with a char * to name of search routine
733 CtdlModuleDoSearch(&num_search_msgs, &search_msgs, search_string, "fulltext");
735 if (num_search_msgs > 0) {
739 orig_num_msgs = num_msgs;
741 for (i=0; i<orig_num_msgs; ++i) {
742 for (j=0; j<num_search_msgs; ++j) {
743 if (msglist[i] == search_msgs[j]) {
744 msglist[num_msgs++] = msglist[i];
750 num_msgs = 0; /* No messages qualify */
752 if (search_msgs != NULL) free(search_msgs);
754 /* Now that we've purged messages which don't contain the search
755 * string, treat a MSGS_SEARCH just like a MSGS_ALL from this
762 * Now iterate through the message list, according to the
763 * criteria supplied by the caller.
766 for (a = 0; a < num_msgs; ++a) {
767 if (server_shutting_down) {
768 if (need_to_free_re) regfree(&re);
770 return num_processed;
772 thismsg = msglist[a];
773 if (mode == MSGS_ALL) {
777 is_seen = is_msg_in_sequence_set(vbuf.v_seen, thismsg);
778 if (is_seen) lastold = thismsg;
784 || ((mode == MSGS_OLD) && (is_seen))
785 || ((mode == MSGS_NEW) && (!is_seen))
786 || ((mode == MSGS_LAST) && (a >= (num_msgs - ref)))
787 || ((mode == MSGS_FIRST) && (a < ref))
788 || ((mode == MSGS_GT) && (thismsg > ref))
789 || ((mode == MSGS_LT) && (thismsg < ref))
790 || ((mode == MSGS_EQ) && (thismsg == ref))
793 if ((mode == MSGS_NEW) && (CC->user.flags & US_LASTOLD) && (lastold > 0L) && (printed_lastold == 0) && (!is_seen)) {
795 CallBack(lastold, userdata);
801 CallBack(thismsg, userdata);
806 if (need_to_free_re) regfree(&re);
809 * We cache the most recent msglist in order to do security checks later
811 if (CC->client_socket > 0) {
812 if (CC->cached_msglist != NULL) {
813 free(CC->cached_msglist);
815 CC->cached_msglist = msglist;
816 CC->cached_num_msgs = num_msgs;
822 return num_processed;
827 * memfmout() - Citadel text formatter and paginator.
828 * Although the original purpose of this routine was to format
829 * text to the reader's screen width, all we're really using it
830 * for here is to format text out to 80 columns before sending it
831 * to the client. The client software may reformat it again.
834 char *mptr, /* where are we going to get our text from? */
835 const char *nl /* string to terminate lines with */
838 unsigned char ch = 0;
845 while (ch=*(mptr++), ch != 0) {
848 if (client_write(outbuf, len) == -1) {
849 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
853 if (client_write(nl, nllen) == -1) {
854 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
859 else if (ch == '\r') {
860 /* Ignore carriage returns. Newlines are always LF or CRLF but never CR. */
862 else if (isspace(ch)) {
863 if (column > 72) { /* Beyond 72 columns, break on the next space */
864 if (client_write(outbuf, len) == -1) {
865 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
869 if (client_write(nl, nllen) == -1) {
870 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
883 if (column > 1000) { /* Beyond 1000 columns, break anywhere */
884 if (client_write(outbuf, len) == -1) {
885 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
889 if (client_write(nl, nllen) == -1) {
890 syslog(LOG_ERR, "msgbase: memfmout(): aborting due to write failure");
898 if (client_write(outbuf, len) == -1) {
899 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
902 client_write(nl, nllen);
909 * Callback function for mime parser that simply lists the part
911 void list_this_part(char *name, char *filename, char *partnum, char *disp,
912 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
913 char *cbid, void *cbuserdata)
917 ma = (struct ma_info *)cbuserdata;
918 if (ma->is_ma == 0) {
919 cprintf("part=%s|%s|%s|%s|%s|%ld|%s|%s\n",
933 * Callback function for multipart prefix
935 void list_this_pref(char *name, char *filename, char *partnum, char *disp,
936 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
937 char *cbid, void *cbuserdata)
941 ma = (struct ma_info *)cbuserdata;
942 if (!strcasecmp(cbtype, "multipart/alternative")) {
946 if (ma->is_ma == 0) {
947 cprintf("pref=%s|%s\n", partnum, cbtype);
953 * Callback function for multipart sufffix
955 void list_this_suff(char *name, char *filename, char *partnum, char *disp,
956 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
957 char *cbid, void *cbuserdata)
961 ma = (struct ma_info *)cbuserdata;
962 if (ma->is_ma == 0) {
963 cprintf("suff=%s|%s\n", partnum, cbtype);
965 if (!strcasecmp(cbtype, "multipart/alternative")) {
972 * Callback function for mime parser that opens a section for downloading
973 * we use serv_files function here:
975 extern void OpenCmdResult(char *filename, const char *mime_type);
976 void mime_download(char *name, char *filename, char *partnum, char *disp,
977 void *content, char *cbtype, char *cbcharset, size_t length,
978 char *encoding, char *cbid, void *cbuserdata)
982 /* Silently go away if there's already a download open. */
983 if (CC->download_fp != NULL)
987 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
988 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
990 CC->download_fp = tmpfile();
991 if (CC->download_fp == NULL) {
992 syslog(LOG_ERR, "msgbase: mime_download() couldn't write: %m");
993 cprintf("%d cannot open temporary file: %s\n", ERROR + INTERNAL_ERROR, strerror(errno));
997 rv = fwrite(content, length, 1, CC->download_fp);
999 syslog(LOG_ERR, "msgbase: mime_download() Couldn't write: %m");
1000 cprintf("%d unable to write tempfile.\n", ERROR + TOO_BIG);
1001 fclose(CC->download_fp);
1002 CC->download_fp = NULL;
1005 fflush(CC->download_fp);
1006 rewind(CC->download_fp);
1008 OpenCmdResult(filename, cbtype);
1014 * Callback function for mime parser that outputs a section all at once.
1015 * We can specify the desired section by part number *or* content-id.
1017 void mime_spew_section(char *name, char *filename, char *partnum, char *disp,
1018 void *content, char *cbtype, char *cbcharset, size_t length,
1019 char *encoding, char *cbid, void *cbuserdata)
1021 int *found_it = (int *)cbuserdata;
1024 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
1025 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
1028 cprintf("%d %d|-1|%s|%s|%s\n",
1035 client_write(content, length);
1040 struct CtdlMessage *CtdlDeserializeMessage(long msgnum, int with_body, const char *Buffer, long Length) {
1041 struct CtdlMessage *ret = NULL;
1043 const char *upper_bound;
1045 cit_uint8_t field_header;
1049 upper_bound = Buffer + Length;
1054 // Parse the three bytes that begin EVERY message on disk.
1055 // The first is always 0xFF, the on-disk magic number.
1056 // The second is the anonymous/public type byte.
1057 // The third is the format type byte (vari, fixed, or MIME).
1061 syslog(LOG_ERR, "msgbase: message %ld appears to be corrupted", msgnum);
1064 ret = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1065 memset(ret, 0, sizeof(struct CtdlMessage));
1067 ret->cm_magic = CTDLMESSAGE_MAGIC;
1068 ret->cm_anon_type = *mptr++; // Anon type byte
1069 ret->cm_format_type = *mptr++; // Format type byte
1071 // The rest is zero or more arbitrary fields. Load them in.
1072 // We're done when we encounter either a zero-length field or
1073 // have just processed the 'M' (message text) field.
1076 field_header = '\0';
1079 while (field_header == '\0') { // work around possibly buggy messages
1080 if (mptr >= upper_bound) {
1083 field_header = *mptr++;
1085 if (mptr >= upper_bound) {
1088 which = field_header;
1091 CM_SetField(ret, which, mptr, len);
1093 mptr += len + 1; // advance to next field
1095 } while ((mptr < upper_bound) && (field_header != 'M'));
1100 // Load a message from disk into memory.
1101 // This is used by CtdlOutputMsg() and other fetch functions.
1103 // NOTE: Caller is responsible for freeing the returned CtdlMessage struct
1104 // using the CM_Free(); function.
1106 struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body) {
1107 struct cdbdata dmsgtext;
1108 struct CtdlMessage *ret = NULL;
1110 syslog(LOG_DEBUG, "msgbase: CtdlFetchMessage(%ld, %d)", msgnum, with_body);
1111 dmsgtext = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
1112 if (dmsgtext.len == 0) {
1113 syslog(LOG_ERR, "msgbase: message #%ld was not found", msgnum);
1117 if (dmsgtext.ptr[dmsgtext.len - 1] != '\0') {
1118 // FIXME LMDB cannot write to immutable memory
1119 syslog(LOG_ERR, "msgbase: CtdlFetchMessage(%ld, %d) Forcefully terminating message!!", msgnum, with_body);
1120 dmsgtext.ptr[dmsgtext.len - 1] = '\0';
1123 ret = CtdlDeserializeMessage(msgnum, with_body, dmsgtext.ptr, dmsgtext.len);
1128 // Always make sure there's something in the msg text field. If
1129 // it's NULL, the message text is most likely stored separately,
1130 // so go ahead and fetch that. Failing that, just set a dummy
1131 // body so other code doesn't barf.
1133 if ( (CM_IsEmpty(ret, eMesageText)) && (with_body) ) {
1134 dmsgtext = cdb_fetch(CDB_BIGMSGS, &msgnum, sizeof(long));
1135 if (dmsgtext.len > 0) {
1136 CM_SetAsField(ret, eMesageText, &dmsgtext.ptr, dmsgtext.len - 1);
1139 if (CM_IsEmpty(ret, eMesageText)) {
1140 CM_SetField(ret, eMesageText, HKEY("\r\n\r\n (no text)\r\n"));
1147 // Pre callback function for multipart/alternative
1149 // NOTE: this differs from the standard behavior for a reason. Normally when
1150 // displaying multipart/alternative you want to show the _last_ usable
1151 // format in the message. Here we show the _first_ one, because it's
1152 // usually text/plain. Since this set of functions is designed for text
1153 // output to non-MIME-aware clients, this is the desired behavior.
1155 void fixed_output_pre(char *name, char *filename, char *partnum, char *disp,
1156 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1157 char *cbid, void *cbuserdata)
1161 ma = (struct ma_info *)cbuserdata;
1162 syslog(LOG_DEBUG, "msgbase: fixed_output_pre() type=<%s>", cbtype);
1163 if (!strcasecmp(cbtype, "multipart/alternative")) {
1167 if (!strcasecmp(cbtype, "message/rfc822")) {
1174 // Post callback function for multipart/alternative
1176 void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
1177 void *content, char *cbtype, char *cbcharset, size_t length,
1178 char *encoding, char *cbid, void *cbuserdata)
1182 ma = (struct ma_info *)cbuserdata;
1183 syslog(LOG_DEBUG, "msgbase: fixed_output_post() type=<%s>", cbtype);
1184 if (!strcasecmp(cbtype, "multipart/alternative")) {
1188 if (!strcasecmp(cbtype, "message/rfc822")) {
1194 // Inline callback function for mime parser that wants to display text
1195 void fixed_output(char *name, char *filename, char *partnum, char *disp,
1196 void *content, char *cbtype, char *cbcharset, size_t length,
1197 char *encoding, char *cbid, void *cbuserdata)
1204 ma = (struct ma_info *)cbuserdata;
1207 "msgbase: fixed_output() part %s: %s (%s) (%ld bytes)",
1208 partnum, filename, cbtype, (long)length
1211 // If we're in the middle of a multipart/alternative scope and
1212 // we've already printed another section, skip this one.
1213 if ( (ma->is_ma) && (ma->did_print) ) {
1214 syslog(LOG_DEBUG, "msgbase: skipping part %s (%s)", partnum, cbtype);
1219 if ( (!strcasecmp(cbtype, "text/plain"))
1220 || (IsEmptyStr(cbtype)) ) {
1223 client_write(wptr, length);
1224 if (wptr[length-1] != '\n') {
1231 if (!strcasecmp(cbtype, "text/html")) {
1232 ptr = html_to_ascii(content, length, 80, 0);
1234 client_write(ptr, wlen);
1235 if ((wlen > 0) && (ptr[wlen-1] != '\n')) {
1242 if (ma->use_fo_hooks) {
1243 if (PerformFixedOutputHooks(cbtype, content, length)) { // returns nonzero if it handled the part
1248 if (strncasecmp(cbtype, "multipart/", 10)) {
1249 cprintf("Part %s: %s (%s) (%ld bytes)\r\n",
1250 partnum, filename, cbtype, (long)length);
1256 // The client is elegant and sophisticated and wants to be choosy about
1257 // MIME content types, so figure out which multipart/alternative part
1258 // we're going to send.
1260 // We use a system of weights. When we find a part that matches one of the
1261 // MIME types we've declared as preferential, we can store it in ma->chosen_part
1262 // and then set ma->chosen_pref to that MIME type's position in our preference
1263 // list. If we then hit another match, we only replace the first match if
1264 // the preference value is lower.
1265 void choose_preferred(char *name, char *filename, char *partnum, char *disp,
1266 void *content, char *cbtype, char *cbcharset, size_t length,
1267 char *encoding, char *cbid, void *cbuserdata)
1273 ma = (struct ma_info *)cbuserdata;
1275 for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
1276 extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
1277 if ( (!strcasecmp(buf, cbtype)) && (!ma->freeze) ) {
1278 if (i < ma->chosen_pref) {
1279 syslog(LOG_DEBUG, "msgbase: setting chosen part to <%s>", partnum);
1280 safestrncpy(ma->chosen_part, partnum, sizeof ma->chosen_part);
1281 ma->chosen_pref = i;
1288 // Now that we've chosen our preferred part, output it.
1289 void output_preferred(char *name,
1303 int add_newline = 0;
1306 char *decoded = NULL;
1307 size_t bytes_decoded;
1310 ma = (struct ma_info *)cbuserdata;
1312 // This is not the MIME part you're looking for...
1313 if (strcasecmp(partnum, ma->chosen_part)) return;
1315 // If the content-type of this part is in our preferred formats
1316 // list, we can simply output it verbatim.
1317 for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
1318 extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
1319 if (!strcasecmp(buf, cbtype)) {
1320 /* Yeah! Go! W00t!! */
1321 if (ma->dont_decode == 0)
1322 rc = mime_decode_now (content,
1328 break; // Give us the chance, maybe theres another one.
1330 if (rc == 0) text_content = (char *)content;
1332 text_content = decoded;
1333 length = bytes_decoded;
1336 if (text_content[length-1] != '\n') {
1339 cprintf("Content-type: %s", cbtype);
1340 if (!IsEmptyStr(cbcharset)) {
1341 cprintf("; charset=%s", cbcharset);
1343 cprintf("\nContent-length: %d\n",
1344 (int)(length + add_newline) );
1345 if (!IsEmptyStr(encoding)) {
1346 cprintf("Content-transfer-encoding: %s\n", encoding);
1349 cprintf("Content-transfer-encoding: 7bit\n");
1351 cprintf("X-Citadel-MSG4-Partnum: %s\n", partnum);
1353 if (client_write(text_content, length) == -1)
1355 syslog(LOG_ERR, "msgbase: output_preferred() aborting due to write failure");
1358 if (add_newline) cprintf("\n");
1359 if (decoded != NULL) free(decoded);
1364 // No translations required or possible: output as text/plain
1365 cprintf("Content-type: text/plain\n\n");
1367 if (ma->dont_decode == 0)
1368 rc = mime_decode_now (content,
1374 return; // Give us the chance, maybe theres another one.
1376 if (rc == 0) text_content = (char *)content;
1378 text_content = decoded;
1379 length = bytes_decoded;
1382 fixed_output(name, filename, partnum, disp, text_content, cbtype, cbcharset, length, encoding, cbid, cbuserdata);
1383 if (decoded != NULL) free(decoded);
1388 char desired_section[64];
1394 // Callback function
1395 void extract_encapsulated_message(char *name, char *filename, char *partnum, char *disp,
1396 void *content, char *cbtype, char *cbcharset, size_t length,
1397 char *encoding, char *cbid, void *cbuserdata)
1399 struct encapmsg *encap;
1401 encap = (struct encapmsg *)cbuserdata;
1403 // Only proceed if this is the desired section...
1404 if (!strcasecmp(encap->desired_section, partnum)) {
1405 encap->msglen = length;
1406 encap->msg = malloc(length + 2);
1407 memcpy(encap->msg, content, length);
1413 // Determine whether the specified message exists in the cached_msglist
1414 // (This is a security check)
1415 int check_cached_msglist(long msgnum) {
1417 // cases in which we skip the check
1418 if (!CC) return om_ok; // not a session
1419 if (CC->client_socket <= 0) return om_ok; // not a client session
1420 if (CC->cached_msglist == NULL) return om_access_denied; // no msglist fetched
1421 if (CC->cached_num_msgs == 0) return om_access_denied; // nothing to check
1423 // Do a binary search within the cached_msglist for the requested msgnum
1425 int max = (CC->cached_num_msgs - 1);
1427 while (max >= min) {
1428 int middle = min + (max-min) / 2 ;
1429 if (msgnum == CC->cached_msglist[middle]) {
1432 if (msgnum > CC->cached_msglist[middle]) {
1440 return om_access_denied;
1444 // Get a message off disk. (returns om_* values found in msgbase.h)
1445 int CtdlOutputMsg(long msg_num, // message number (local) to fetch
1446 int mode, // how would you like that message?
1447 int headers_only, // eschew the message body?
1448 int do_proto, // do Citadel protocol responses?
1449 int crlf, // Use CRLF newlines instead of LF?
1450 char *section, // NULL or a message/rfc822 section
1451 int flags, // various flags; see msgbase.h
1456 struct CtdlMessage *TheMessage = NULL;
1457 int retcode = CIT_OK;
1458 struct encapmsg encap;
1461 syslog(LOG_DEBUG, "msgbase: CtdlOutputMsg(msgnum=%ld, mode=%d, section=%s)",
1463 (section ? section : "<>")
1466 r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
1469 if (r == om_not_logged_in) {
1470 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1473 cprintf("%d An unknown error has occurred.\n", ERROR);
1480 * Check to make sure the message is actually IN this room
1482 r = check_cached_msglist(msg_num);
1483 if (r == om_access_denied) {
1484 /* Not in the cache? We get ONE shot to check it again. */
1485 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, NULL, NULL);
1486 r = check_cached_msglist(msg_num);
1489 syslog(LOG_DEBUG, "msgbase: security check fail; message %ld is not in %s",
1490 msg_num, CC->room.QRname
1493 if (r == om_access_denied) {
1494 cprintf("%d message %ld was not found in this room\n",
1495 ERROR + HIGHER_ACCESS_REQUIRED,
1504 * Fetch the message from disk. If we're in HEADERS_FAST mode,
1505 * request that we don't even bother loading the body into memory.
1507 if (headers_only == HEADERS_FAST) {
1508 TheMessage = CtdlFetchMessage(msg_num, 0);
1511 TheMessage = CtdlFetchMessage(msg_num, 1);
1514 if (TheMessage == NULL) {
1515 if (do_proto) cprintf("%d Can't locate msg %ld on disk\n",
1516 ERROR + MESSAGE_NOT_FOUND, msg_num);
1517 return(om_no_such_msg);
1520 /* Here is the weird form of this command, to process only an
1521 * encapsulated message/rfc822 section.
1523 if (section) if (!IsEmptyStr(section)) if (strcmp(section, "0")) {
1524 memset(&encap, 0, sizeof encap);
1525 safestrncpy(encap.desired_section, section, sizeof encap.desired_section);
1526 mime_parser(CM_RANGE(TheMessage, eMesageText),
1527 *extract_encapsulated_message,
1528 NULL, NULL, (void *)&encap, 0
1531 if ((Author != NULL) && (*Author == NULL))
1534 CM_GetAsField(TheMessage, eAuthor, Author, &len);
1536 if ((Address != NULL) && (*Address == NULL))
1539 CM_GetAsField(TheMessage, erFc822Addr, Address, &len);
1541 if ((MessageID != NULL) && (*MessageID == NULL))
1544 CM_GetAsField(TheMessage, emessageId, MessageID, &len);
1546 CM_Free(TheMessage);
1550 encap.msg[encap.msglen] = 0;
1551 TheMessage = convert_internet_message(encap.msg);
1552 encap.msg = NULL; /* no free() here, TheMessage owns it now */
1554 /* Now we let it fall through to the bottom of this
1555 * function, because TheMessage now contains the
1556 * encapsulated message instead of the top-level
1557 * message. Isn't that neat?
1562 cprintf("%d msg %ld has no part %s\n",
1563 ERROR + MESSAGE_NOT_FOUND,
1567 retcode = om_no_such_msg;
1572 /* Ok, output the message now */
1573 if (retcode == CIT_OK)
1574 retcode = CtdlOutputPreLoadedMsg(TheMessage, mode, headers_only, do_proto, crlf, flags);
1575 if ((Author != NULL) && (*Author == NULL))
1578 CM_GetAsField(TheMessage, eAuthor, Author, &len);
1580 if ((Address != NULL) && (*Address == NULL))
1583 CM_GetAsField(TheMessage, erFc822Addr, Address, &len);
1585 if ((MessageID != NULL) && (*MessageID == NULL))
1588 CM_GetAsField(TheMessage, emessageId, MessageID, &len);
1591 CM_Free(TheMessage);
1597 void OutputCtdlMsgHeaders(struct CtdlMessage *TheMessage, int do_proto) {
1600 char display_name[256];
1602 /* begin header processing loop for Citadel message format */
1603 safestrncpy(display_name, "<unknown>", sizeof display_name);
1604 if (!CM_IsEmpty(TheMessage, eAuthor)) {
1605 strcpy(buf, TheMessage->cm_fields[eAuthor]);
1606 if (TheMessage->cm_anon_type == MES_ANONONLY) {
1607 safestrncpy(display_name, "****", sizeof display_name);
1609 else if (TheMessage->cm_anon_type == MES_ANONOPT) {
1610 safestrncpy(display_name, "anonymous", sizeof display_name);
1613 safestrncpy(display_name, buf, sizeof display_name);
1615 if ((is_room_aide())
1616 && ((TheMessage->cm_anon_type == MES_ANONONLY)
1617 || (TheMessage->cm_anon_type == MES_ANONOPT))) {
1618 size_t tmp = strlen(display_name);
1619 snprintf(&display_name[tmp],
1620 sizeof display_name - tmp,
1625 /* Now spew the header fields in the order we like them. */
1626 for (i=0; i< NDiskFields; ++i) {
1628 Field = FieldOrder[i];
1629 if (Field != eMesageText) {
1630 if ( (!CM_IsEmpty(TheMessage, Field)) && (msgkeys[Field] != NULL) ) {
1631 if ((Field == eenVelopeTo) || (Field == eRecipient) || (Field == eCarbonCopY)) {
1632 sanitize_truncated_recipient(TheMessage->cm_fields[Field]);
1634 if (Field == eAuthor) {
1636 cprintf("%s=%s\n", msgkeys[Field], display_name);
1639 /* Masquerade display name if needed */
1642 cprintf("%s=%s\n", msgkeys[Field], TheMessage->cm_fields[Field]);
1645 /* Give the client a hint about whether the message originated locally */
1646 if (Field == erFc822Addr) {
1647 if (IsDirectory(TheMessage->cm_fields[Field] ,0)) {
1648 cprintf("locl=yes\n"); // message originated locally.
1660 void OutputRFC822MsgHeaders(
1661 struct CtdlMessage *TheMessage,
1662 int flags, /* should the message be exported clean */
1663 const char *nl, int nlen,
1664 char *mid, long sizeof_mid,
1665 char *suser, long sizeof_suser,
1666 char *luser, long sizeof_luser,
1667 char *fuser, long sizeof_fuser,
1668 char *snode, long sizeof_snode)
1670 char datestamp[100];
1671 int subject_found = 0;
1678 for (i = 0; i < NDiskFields; ++i) {
1679 if (TheMessage->cm_fields[FieldOrder[i]]) {
1680 mptr = mpptr = TheMessage->cm_fields[FieldOrder[i]];
1681 switch (FieldOrder[i]) {
1683 safestrncpy(luser, mptr, sizeof_luser);
1684 safestrncpy(suser, mptr, sizeof_suser);
1687 if ((flags & QP_EADDR) != 0) {
1688 mptr = qp_encode_email_addrs(mptr);
1690 sanitize_truncated_recipient(mptr);
1691 cprintf("CC: %s%s", mptr, nl);
1694 cprintf("Return-Path: %s%s", mptr, nl);
1697 cprintf("List-ID: %s%s", mptr, nl);
1700 if ((flags & QP_EADDR) != 0)
1701 mptr = qp_encode_email_addrs(mptr);
1703 while ((*hptr != '\0') && isspace(*hptr))
1705 if (!IsEmptyStr(hptr))
1706 cprintf("Envelope-To: %s%s", hptr, nl);
1709 cprintf("Subject: %s%s", mptr, nl);
1713 safestrncpy(mid, mptr, sizeof_mid);
1716 safestrncpy(fuser, mptr, sizeof_fuser);
1719 if (haschar(mptr, '@') == 0) {
1720 sanitize_truncated_recipient(mptr);
1721 cprintf("To: %s@%s", mptr, CtdlGetConfigStr("c_fqdn"));
1725 if ((flags & QP_EADDR) != 0) {
1726 mptr = qp_encode_email_addrs(mptr);
1728 sanitize_truncated_recipient(mptr);
1729 cprintf("To: %s", mptr);
1734 datestring(datestamp, sizeof datestamp, atol(mptr), DATESTRING_RFC822);
1735 cprintf("Date: %s%s", datestamp, nl);
1738 cprintf("References: ");
1739 k = num_tokens(mptr, '|');
1740 for (j=0; j<k; ++j) {
1741 extract_token(buf, mptr, j, '|', sizeof buf);
1742 cprintf("<%s>", buf);
1753 while ((*hptr != '\0') && isspace(*hptr))
1755 if (!IsEmptyStr(hptr))
1756 cprintf("Reply-To: %s%s", mptr, nl);
1768 /* these don't map to mime message headers. */
1771 if (mptr != mpptr) {
1776 if (subject_found == 0) {
1777 cprintf("Subject: (no subject)%s", nl);
1782 void Dump_RFC822HeadersBody(
1783 struct CtdlMessage *TheMessage,
1784 int headers_only, /* eschew the message body? */
1785 int flags, /* should the bessage be exported clean? */
1786 const char *nl, int nlen)
1788 cit_uint8_t prev_ch;
1790 const char *StartOfText = StrBufNOTNULL;
1793 int nllen = strlen(nl);
1797 mptr = TheMessage->cm_fields[eMesageText];
1800 while (*mptr != '\0') {
1801 if (*mptr == '\r') {
1808 eoh = (*(mptr+1) == '\r') && (*(mptr+2) == '\n');
1810 eoh = *(mptr+1) == '\n';
1814 StartOfText = strchr(StartOfText, '\n');
1815 StartOfText = strchr(StartOfText, '\n');
1818 if (((headers_only == HEADERS_NONE) && (mptr >= StartOfText)) ||
1819 ((headers_only == HEADERS_ONLY) && (mptr < StartOfText)) ||
1820 ((headers_only != HEADERS_NONE) &&
1821 (headers_only != HEADERS_ONLY))
1823 if (*mptr == '\n') {
1824 memcpy(&outbuf[outlen], nl, nllen);
1826 outbuf[outlen] = '\0';
1829 outbuf[outlen++] = *mptr;
1833 if (flags & ESC_DOT) {
1834 if ((prev_ch == '\n') && (*mptr == '.') && ((*(mptr+1) == '\r') || (*(mptr+1) == '\n'))) {
1835 outbuf[outlen++] = '.';
1840 if (outlen > 1000) {
1841 if (client_write(outbuf, outlen) == -1) {
1842 syslog(LOG_ERR, "msgbase: Dump_RFC822HeadersBody() aborting due to write failure");
1845 lfSent = (outbuf[outlen - 1] == '\n');
1850 client_write(outbuf, outlen);
1851 lfSent = (outbuf[outlen - 1] == '\n');
1854 client_write(nl, nlen);
1858 /* If the format type on disk is 1 (fixed-format), then we want
1859 * everything to be output completely literally ... regardless of
1860 * what message transfer format is in use.
1862 void DumpFormatFixed(
1863 struct CtdlMessage *TheMessage,
1864 int mode, /* how would you like that message? */
1865 const char *nl, int nllen)
1873 mptr = TheMessage->cm_fields[eMesageText];
1875 if (mode == MT_MIME) {
1876 cprintf("Content-type: text/plain\n\n");
1880 while (ch = *mptr++, ch > 0) {
1884 if ((buflen > 250) && (!xlline)){
1888 while ((buflen > 0) &&
1889 (!isspace(buf[buflen])))
1895 mptr -= tbuflen - buflen;
1901 /* if we reach the outer bounds of our buffer, abort without respect for what we purge. */
1902 if (xlline && ((isspace(ch)) || (buflen > SIZ - nllen - 2))) {
1907 memcpy (&buf[buflen], nl, nllen);
1911 if (client_write(buf, buflen) == -1) {
1912 syslog(LOG_ERR, "msgbase: DumpFormatFixed() aborting due to write failure");
1924 if (!IsEmptyStr(buf)) {
1925 cprintf("%s%s", buf, nl);
1931 * Get a message off disk. (returns om_* values found in msgbase.h)
1933 int CtdlOutputPreLoadedMsg(
1934 struct CtdlMessage *TheMessage,
1935 int mode, /* how would you like that message? */
1936 int headers_only, /* eschew the message body? */
1937 int do_proto, /* do Citadel protocol responses? */
1938 int crlf, /* Use CRLF newlines instead of LF? */
1939 int flags /* should the bessage be exported clean? */
1942 const char *nl; /* newline string */
1946 /* Buffers needed for RFC822 translation. These are all filled
1947 * using functions that are bounds-checked, and therefore we can
1948 * make them substantially smaller than SIZ.
1956 syslog(LOG_DEBUG, "msgbase: CtdlOutputPreLoadedMsg(TheMessage=%s, %d, %d, %d, %d",
1957 ((TheMessage == NULL) ? "NULL" : "not null"),
1958 mode, headers_only, do_proto, crlf
1961 strcpy(mid, "unknown");
1962 nl = (crlf ? "\r\n" : "\n");
1963 nlen = crlf ? 2 : 1;
1965 if (!CM_IsValidMsg(TheMessage)) {
1966 syslog(LOG_ERR, "msgbase: error; invalid preloaded message for output");
1967 return(om_no_such_msg);
1970 /* Suppress envelope recipients if required to avoid disclosing BCC addresses.
1971 * Pad it with spaces in order to avoid changing the RFC822 length of the message.
1973 if ( (flags & SUPPRESS_ENV_TO) && (!CM_IsEmpty(TheMessage, eenVelopeTo)) ) {
1974 memset(TheMessage->cm_fields[eenVelopeTo], ' ', TheMessage->cm_lengths[eenVelopeTo]);
1977 /* Are we downloading a MIME component? */
1978 if (mode == MT_DOWNLOAD) {
1979 if (TheMessage->cm_format_type != FMT_RFC822) {
1981 cprintf("%d This is not a MIME message.\n",
1982 ERROR + ILLEGAL_VALUE);
1983 } else if (CC->download_fp != NULL) {
1984 if (do_proto) cprintf(
1985 "%d You already have a download open.\n",
1986 ERROR + RESOURCE_BUSY);
1988 /* Parse the message text component */
1989 mime_parser(CM_RANGE(TheMessage, eMesageText),
1990 *mime_download, NULL, NULL, NULL, 0);
1991 /* If there's no file open by this time, the requested
1992 * section wasn't found, so print an error
1994 if (CC->download_fp == NULL) {
1995 if (do_proto) cprintf(
1996 "%d Section %s not found.\n",
1997 ERROR + FILE_NOT_FOUND,
1998 CC->download_desired_section);
2001 return((CC->download_fp != NULL) ? om_ok : om_mime_error);
2004 // MT_SPEW_SECTION is like MT_DOWNLOAD except it outputs the whole MIME part
2005 // in a single server operation instead of opening a download file.
2006 if (mode == MT_SPEW_SECTION) {
2007 if (TheMessage->cm_format_type != FMT_RFC822) {
2009 cprintf("%d This is not a MIME message.\n",
2010 ERROR + ILLEGAL_VALUE);
2013 // Locate and parse the component specified by the caller
2015 mime_parser(CM_RANGE(TheMessage, eMesageText), *mime_spew_section, NULL, NULL, (void *)&found_it, 0);
2017 // If section wasn't found, print an error
2020 cprintf( "%d Section %s not found.\n", ERROR + FILE_NOT_FOUND, CC->download_desired_section);
2024 return((CC->download_fp != NULL) ? om_ok : om_mime_error);
2027 // now for the user-mode message reading loops
2028 if (do_proto) cprintf("%d msg:\n", LISTING_FOLLOWS);
2030 // Does the caller want to skip the headers?
2031 if (headers_only == HEADERS_NONE) goto START_TEXT;
2033 // Tell the client which format type we're using.
2034 if ( (mode == MT_CITADEL) && (do_proto) ) {
2035 cprintf("type=%d\n", TheMessage->cm_format_type); // Tell the client which format type we're using.
2038 // nhdr=yes means that we're only displaying headers, no body
2039 if ( (TheMessage->cm_anon_type == MES_ANONONLY)
2040 && ((mode == MT_CITADEL) || (mode == MT_MIME))
2043 cprintf("nhdr=yes\n");
2046 if ((mode == MT_CITADEL) || (mode == MT_MIME)) {
2047 OutputCtdlMsgHeaders(TheMessage, do_proto);
2050 // begin header processing loop for RFC822 transfer format
2055 if (mode == MT_RFC822)
2056 OutputRFC822MsgHeaders(
2061 suser, sizeof(suser),
2062 luser, sizeof(luser),
2063 fuser, sizeof(fuser),
2064 snode, sizeof(snode)
2068 for (i=0; !IsEmptyStr(&suser[i]); ++i) {
2069 suser[i] = tolower(suser[i]);
2070 if (!isalnum(suser[i])) suser[i]='_';
2073 if (mode == MT_RFC822) {
2074 /* Construct a fun message id */
2075 cprintf("Message-ID: <%s", mid);
2076 if (strchr(mid, '@')==NULL) {
2077 cprintf("@%s", snode);
2081 if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONONLY)) {
2082 cprintf("From: \"----\" <x@x.org>%s", nl);
2084 else if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONOPT)) {
2085 cprintf("From: \"anonymous\" <x@x.org>%s", nl);
2087 else if (!IsEmptyStr(fuser)) {
2088 cprintf("From: \"%s\" <%s>%s", luser, fuser, nl);
2091 cprintf("From: \"%s\" <%s@%s>%s", luser, suser, snode, nl);
2094 /* Blank line signifying RFC822 end-of-headers */
2095 if (TheMessage->cm_format_type != FMT_RFC822) {
2100 // end header processing loop ... at this point, we're in the text
2102 if (headers_only == HEADERS_FAST) goto DONE;
2104 // Tell the client about the MIME parts in this message
2105 if (TheMessage->cm_format_type == FMT_RFC822) {
2106 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2107 memset(&ma, 0, sizeof(struct ma_info));
2108 mime_parser(CM_RANGE(TheMessage, eMesageText),
2109 (do_proto ? *list_this_part : NULL),
2110 (do_proto ? *list_this_pref : NULL),
2111 (do_proto ? *list_this_suff : NULL),
2114 else if (mode == MT_RFC822) { // unparsed RFC822 dump
2115 Dump_RFC822HeadersBody(
2124 if (headers_only == HEADERS_ONLY) {
2128 // signify start of msg text
2129 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2130 if (do_proto) cprintf("text\n");
2133 if (TheMessage->cm_format_type == FMT_FIXED)
2136 mode, // how would you like that message?
2139 // If the message on disk is format 0 (Citadel vari-format), we
2140 // output using the formatter at 80 columns. This is the final output
2141 // form if the transfer format is RFC822, but if the transfer format
2142 // is Citadel proprietary, it'll still work, because the indentation
2143 // for new paragraphs is correct and the client will reformat the
2144 // message to the reader's screen width.
2146 if (TheMessage->cm_format_type == FMT_CITADEL) {
2147 if (mode == MT_MIME) {
2148 cprintf("Content-type: text/x-citadel-variformat\n\n");
2150 memfmout(TheMessage->cm_fields[eMesageText], nl);
2153 // If the message on disk is format 4 (MIME), we've gotta hand it
2154 // off to the MIME parser. The client has already been told that
2155 // this message is format 1 (fixed format), so the callback function
2156 // we use will display those parts as-is.
2158 if (TheMessage->cm_format_type == FMT_RFC822) {
2159 memset(&ma, 0, sizeof(struct ma_info));
2161 if (mode == MT_MIME) {
2162 ma.use_fo_hooks = 0;
2163 strcpy(ma.chosen_part, "1");
2164 ma.chosen_pref = 9999;
2165 ma.dont_decode = CC->msg4_dont_decode;
2166 mime_parser(CM_RANGE(TheMessage, eMesageText),
2167 *choose_preferred, *fixed_output_pre,
2168 *fixed_output_post, (void *)&ma, 1);
2169 mime_parser(CM_RANGE(TheMessage, eMesageText),
2170 *output_preferred, NULL, NULL, (void *)&ma, 1);
2173 ma.use_fo_hooks = 1;
2174 mime_parser(CM_RANGE(TheMessage, eMesageText),
2175 *fixed_output, *fixed_output_pre,
2176 *fixed_output_post, (void *)&ma, 0);
2181 DONE: /* now we're done */
2182 if (do_proto) cprintf("000\n");
2186 // Save one or more message pointers into a specified room
2187 // (Returns 0 for success, nonzero for failure)
2188 // roomname may be NULL to use the current room
2190 // Note that the 'supplied_msg' field may be set to NULL, in which case
2191 // the message will be fetched from disk, by number, if we need to perform
2192 // replication checks. This adds an additional database read, so if the
2193 // caller already has the message in memory then it should be supplied. (Obviously
2194 // this mode of operation only works if we're saving a single message.)
2196 int CtdlSaveMsgPointersInRoom(char *roomname, long newmsgidlist[], int num_newmsgs,
2197 int do_repl_check, struct CtdlMessage *supplied_msg, int suppress_refcount_adj
2200 char hold_rm[ROOMNAMELEN];
2201 struct cdbdata *cdbfr;
2204 long highest_msg = 0L;
2207 struct CtdlMessage *msg = NULL;
2209 long *msgs_to_be_merged = NULL;
2210 int num_msgs_to_be_merged = 0;
2213 "msgbase: CtdlSaveMsgPointersInRoom(room=%s, num_msgs=%d, repl=%d, suppress_rca=%d)",
2214 roomname, num_newmsgs, do_repl_check, suppress_refcount_adj
2217 strcpy(hold_rm, CC->room.QRname);
2220 if (newmsgidlist == NULL) return(ERROR + INTERNAL_ERROR);
2221 if (num_newmsgs < 1) return(ERROR + INTERNAL_ERROR);
2222 if (num_newmsgs > 1) supplied_msg = NULL;
2224 /* Now the regular stuff */
2225 if (CtdlGetRoomLock(&CC->room,
2226 ((roomname != NULL) ? roomname : CC->room.QRname) )
2228 syslog(LOG_ERR, "msgbase: no such room <%s>", roomname);
2229 return(ERROR + ROOM_NOT_FOUND);
2232 msgs_to_be_merged = malloc(sizeof(long) * num_newmsgs);
2233 num_msgs_to_be_merged = 0;
2234 num_msgs = CtdlFetchMsgList(CC->room.QRnumber, &msglist);
2236 /* Create a list of msgid's which were supplied by the caller, but do
2237 * not already exist in the target room. It is absolutely taboo to
2238 * have more than one reference to the same message in a room.
2240 for (i=0; i<num_newmsgs; ++i) {
2242 if (num_msgs > 0) for (j=0; j<num_msgs; ++j) {
2243 if (msglist[j] == newmsgidlist[i]) {
2248 msgs_to_be_merged[num_msgs_to_be_merged++] = newmsgidlist[i];
2252 syslog(LOG_DEBUG, "msgbase: %d unique messages to be merged", num_msgs_to_be_merged);
2255 * Now merge the new messages
2257 msglist = realloc(msglist, (sizeof(long) * (num_msgs + num_msgs_to_be_merged)) );
2258 if (msglist == NULL) {
2259 syslog(LOG_ALERT, "msgbase: ERROR; can't realloc message list!");
2260 free(msgs_to_be_merged);
2261 return (ERROR + INTERNAL_ERROR);
2263 memcpy(&msglist[num_msgs], msgs_to_be_merged, (sizeof(long) * num_msgs_to_be_merged) );
2264 num_msgs += num_msgs_to_be_merged;
2266 /* Sort the message list, so all the msgid's are in order */
2267 num_msgs = sort_msglist(msglist, num_msgs);
2269 /* Determine the highest message number */
2270 highest_msg = msglist[num_msgs - 1];
2272 /* Write it back to disk. */
2273 cdb_store(CDB_MSGLISTS, &CC->room.QRnumber, (int)sizeof(long), msglist, (int)(num_msgs * sizeof(long)));
2275 /* Free up the memory we used. */
2278 /* Update the highest-message pointer and unlock the room. */
2279 CC->room.QRhighest = highest_msg;
2280 CtdlPutRoomLock(&CC->room);
2282 /* Perform replication checks if necessary */
2283 if ( (DoesThisRoomNeedEuidIndexing(&CC->room)) && (do_repl_check) ) {
2284 syslog(LOG_DEBUG, "msgbase: CtdlSaveMsgPointerInRoom() doing repl checks");
2286 for (i=0; i<num_msgs_to_be_merged; ++i) {
2287 msgid = msgs_to_be_merged[i];
2289 if (supplied_msg != NULL) {
2293 msg = CtdlFetchMessage(msgid, 0);
2297 ReplicationChecks(msg);
2299 /* If the message has an Exclusive ID, index that... */
2300 if (!CM_IsEmpty(msg, eExclusiveID)) {
2301 index_message_by_euid(msg->cm_fields[eExclusiveID], &CC->room, msgid);
2304 /* Free up the memory we may have allocated */
2305 if (msg != supplied_msg) {
2314 syslog(LOG_DEBUG, "msgbase: CtdlSaveMsgPointerInRoom() skips repl checks");
2317 /* Submit this room for processing by hooks */
2318 int total_roomhook_errors = PerformRoomHooks(&CC->room);
2319 if (total_roomhook_errors) {
2320 syslog(LOG_WARNING, "msgbase: room hooks returned %d errors", total_roomhook_errors);
2323 /* Go back to the room we were in before we wandered here... */
2324 CtdlGetRoom(&CC->room, hold_rm);
2326 /* Bump the reference count for all messages which were merged */
2327 if (!suppress_refcount_adj) {
2328 AdjRefCountList(msgs_to_be_merged, num_msgs_to_be_merged, +1);
2331 /* Free up memory... */
2332 if (msgs_to_be_merged != NULL) {
2333 free(msgs_to_be_merged);
2336 /* Return success. */
2342 * This is the same as CtdlSaveMsgPointersInRoom() but it only accepts
2345 int CtdlSaveMsgPointerInRoom(char *roomname, long msgid, int do_repl_check, struct CtdlMessage *supplied_msg) {
2346 return CtdlSaveMsgPointersInRoom(roomname, &msgid, 1, do_repl_check, supplied_msg, 0);
2351 * Message base operation to save a new message to the message store
2352 * (returns new message number)
2354 * This is the back end for CtdlSubmitMsg() and should not be directly
2355 * called by server-side modules.
2358 long CtdlSaveThisMessage(struct CtdlMessage *msg, long msgid, int Reply) {
2366 * If the message is big, set its body aside for storage elsewhere
2367 * and we hide the message body from the serializer
2369 if (!CM_IsEmpty(msg, eMesageText) && msg->cm_lengths[eMesageText] > BIGMSG) {
2371 holdM = msg->cm_fields[eMesageText];
2372 msg->cm_fields[eMesageText] = NULL;
2373 holdMLen = msg->cm_lengths[eMesageText];
2374 msg->cm_lengths[eMesageText] = 0;
2377 /* Serialize our data structure for storage in the database */
2378 CtdlSerializeMessage(&smr, msg);
2381 /* put the message body back into the message */
2382 msg->cm_fields[eMesageText] = holdM;
2383 msg->cm_lengths[eMesageText] = holdMLen;
2388 cprintf("%d Unable to serialize message\n",
2389 ERROR + INTERNAL_ERROR);
2392 syslog(LOG_ERR, "msgbase: CtdlSaveMessage() unable to serialize message");
2398 /* Write our little bundle of joy into the message base */
2399 retval = cdb_store(CDB_MSGMAIN, &msgid, (int)sizeof(long), smr.ser, smr.len);
2401 syslog(LOG_ERR, "msgbase: can't store message %ld: %ld", msgid, retval);
2405 retval = cdb_store(CDB_BIGMSGS, &msgid, (int)sizeof(long), holdM, (holdMLen + 1));
2407 syslog(LOG_ERR, "msgbase: failed to store message body for msgid %ld: %ld", msgid, retval);
2412 /* Free the memory we used for the serialized message */
2418 long send_message(struct CtdlMessage *msg) {
2424 /* Get a new message number */
2425 newmsgid = get_new_message_number();
2427 /* Generate an ID if we don't have one already */
2428 if (CM_IsEmpty(msg, emessageId)) {
2429 msgidbuflen = snprintf(msgidbuf, sizeof msgidbuf, "%08lX-%08lX@%s",
2430 (long unsigned int) time(NULL),
2431 (long unsigned int) newmsgid,
2432 CtdlGetConfigStr("c_fqdn")
2435 CM_SetField(msg, emessageId, msgidbuf, msgidbuflen);
2438 retval = CtdlSaveThisMessage(msg, newmsgid, 1);
2444 /* Return the *local* message ID to the caller
2445 * (even if we're storing an incoming network message)
2452 * Serialize a struct CtdlMessage into the format used on disk.
2454 * This function loads up a "struct ser_ret" (defined in server.h) which
2455 * contains the length of the serialized message and a pointer to the
2456 * serialized message in memory. THE LATTER MUST BE FREED BY THE CALLER.
2458 void CtdlSerializeMessage(struct ser_ret *ret, /* return values */
2459 struct CtdlMessage *msg) /* unserialized msg */
2465 * Check for valid message format
2467 if (CM_IsValidMsg(msg) == 0) {
2468 syslog(LOG_ERR, "msgbase: CtdlSerializeMessage() aborting due to invalid message");
2475 for (i=0; i < NDiskFields; ++i)
2476 if (msg->cm_fields[FieldOrder[i]] != NULL)
2477 ret->len += msg->cm_lengths[FieldOrder[i]] + 2;
2479 ret->ser = malloc(ret->len);
2480 if (ret->ser == NULL) {
2481 syslog(LOG_ERR, "msgbase: CtdlSerializeMessage() malloc(%ld) failed: %m", (long)ret->len);
2488 ret->ser[1] = msg->cm_anon_type;
2489 ret->ser[2] = msg->cm_format_type;
2492 for (i=0; i < NDiskFields; ++i) {
2493 if (msg->cm_fields[FieldOrder[i]] != NULL) {
2494 ret->ser[wlen++] = (char)FieldOrder[i];
2496 memcpy(&ret->ser[wlen],
2497 msg->cm_fields[FieldOrder[i]],
2498 msg->cm_lengths[FieldOrder[i]] + 1);
2500 wlen = wlen + msg->cm_lengths[FieldOrder[i]] + 1;
2504 if (ret->len != wlen) {
2505 syslog(LOG_ERR, "msgbase: ERROR; len=%ld wlen=%ld", (long)ret->len, (long)wlen);
2513 * Check to see if any messages already exist in the current room which
2514 * carry the same Exclusive ID as this one. If any are found, delete them.
2516 void ReplicationChecks(struct CtdlMessage *msg) {
2517 long old_msgnum = (-1L);
2519 if (DoesThisRoomNeedEuidIndexing(&CC->room) == 0) return;
2521 syslog(LOG_DEBUG, "msgbase: performing replication checks in <%s>", CC->room.QRname);
2523 /* No exclusive id? Don't do anything. */
2524 if (msg == NULL) return;
2525 if (CM_IsEmpty(msg, eExclusiveID)) return;
2527 /*syslog(LOG_DEBUG, "msgbase: exclusive ID: <%s> for room <%s>",
2528 msg->cm_fields[eExclusiveID], CC->room.QRname);*/
2530 old_msgnum = CtdlLocateMessageByEuid(msg->cm_fields[eExclusiveID], &CC->room);
2531 if (old_msgnum > 0L) {
2532 syslog(LOG_DEBUG, "msgbase: ReplicationChecks() replacing message %ld", old_msgnum);
2533 CtdlDeleteMessages(CC->room.QRname, &old_msgnum, 1, "");
2539 * Save a message to disk and submit it into the delivery system.
2541 long CtdlSubmitMsg(struct CtdlMessage *msg, /* message to save */
2542 struct recptypes *recps, /* recipients (if mail) */
2543 const char *force /* force a particular room? */
2545 char hold_rm[ROOMNAMELEN];
2546 char actual_rm[ROOMNAMELEN];
2547 char force_room[ROOMNAMELEN];
2548 char content_type[SIZ]; /* We have to learn this */
2549 char recipient[SIZ];
2550 char bounce_to[1024];
2553 const char *mptr = NULL;
2554 struct ctdluser userbuf;
2556 struct MetaData smi;
2557 char *collected_addresses = NULL;
2558 struct addresses_to_be_filed *aptr = NULL;
2559 StrBuf *saved_rfc822_version = NULL;
2560 int qualified_for_journaling = 0;
2562 syslog(LOG_DEBUG, "msgbase: CtdlSubmitMsg() called");
2563 if (CM_IsValidMsg(msg) == 0) return(-1); /* self check */
2565 /* If this message has no timestamp, we take the liberty of
2566 * giving it one, right now.
2568 if (CM_IsEmpty(msg, eTimestamp)) {
2569 CM_SetFieldLONG(msg, eTimestamp, time(NULL));
2572 /* If this message has no path, we generate one.
2574 if (CM_IsEmpty(msg, eMessagePath)) {
2575 if (!CM_IsEmpty(msg, eAuthor)) {
2576 CM_CopyField(msg, eMessagePath, eAuthor);
2577 for (a=0; !IsEmptyStr(&msg->cm_fields[eMessagePath][a]); ++a) {
2578 if (isspace(msg->cm_fields[eMessagePath][a])) {
2579 msg->cm_fields[eMessagePath][a] = ' ';
2584 CM_SetField(msg, eMessagePath, HKEY("unknown"));
2588 if (force == NULL) {
2589 force_room[0] = '\0';
2592 strcpy(force_room, force);
2595 /* Learn about what's inside, because it's what's inside that counts */
2596 if (CM_IsEmpty(msg, eMesageText)) {
2597 syslog(LOG_ERR, "msgbase: ERROR; attempt to save message with NULL body");
2601 switch (msg->cm_format_type) {
2603 strcpy(content_type, "text/x-citadel-variformat");
2606 strcpy(content_type, "text/plain");
2609 strcpy(content_type, "text/plain");
2610 mptr = bmstrcasestr(msg->cm_fields[eMesageText], "Content-type:");
2613 safestrncpy(content_type, &mptr[13], sizeof content_type);
2614 string_trim(content_type);
2615 aptr = content_type;
2616 while (!IsEmptyStr(aptr)) {
2628 /* Goto the correct room */
2629 room = (recps) ? CC->room.QRname : SENTITEMS;
2630 syslog(LOG_DEBUG, "msgbase: selected room %s", room);
2631 strcpy(hold_rm, CC->room.QRname);
2632 strcpy(actual_rm, CC->room.QRname);
2633 if (recps != NULL) {
2634 strcpy(actual_rm, SENTITEMS);
2637 /* If the user is a twit, move to the twit room for posting */
2639 if (CC->user.axlevel == AxProbU) {
2640 strcpy(hold_rm, actual_rm);
2641 strcpy(actual_rm, CtdlGetConfigStr("c_twitroom"));
2642 syslog(LOG_DEBUG, "msgbase: diverting to twit room");
2646 /* ...or if this message is destined for Aide> then go there. */
2647 if (!IsEmptyStr(force_room)) {
2648 strcpy(actual_rm, force_room);
2651 syslog(LOG_DEBUG, "msgbase: final selection: %s (%s)", actual_rm, room);
2652 if (strcasecmp(actual_rm, CC->room.QRname)) {
2653 CtdlUserGoto(actual_rm, 0, 1, NULL, NULL, NULL, NULL);
2657 * If this message has no O (room) field, generate one.
2659 if (CM_IsEmpty(msg, eOriginalRoom) && !IsEmptyStr(CC->room.QRname)) {
2660 CM_SetField(msg, eOriginalRoom, CC->room.QRname, -1);
2663 /* Perform "before save" hooks (aborting if any return nonzero) */
2664 syslog(LOG_DEBUG, "msgbase: performing before-save hooks");
2665 if (PerformMessageHooks(msg, recps, EVT_BEFORESAVE) > 0) return(-3);
2668 * If this message has an Exclusive ID, and the room is replication
2669 * checking enabled, then do replication checks.
2671 if (DoesThisRoomNeedEuidIndexing(&CC->room)) {
2672 ReplicationChecks(msg);
2675 /* Save it to disk */
2676 syslog(LOG_DEBUG, "msgbase: saving to disk");
2677 newmsgid = send_message(msg);
2678 if (newmsgid <= 0L) return(-5);
2680 /* Write a supplemental message info record. This doesn't have to
2681 * be a critical section because nobody else knows about this message
2684 syslog(LOG_DEBUG, "msgbase: creating metadata record");
2685 memset(&smi, 0, sizeof(struct MetaData));
2686 smi.meta_msgnum = newmsgid;
2687 smi.meta_refcount = 0;
2688 safestrncpy(smi.meta_content_type, content_type, sizeof smi.meta_content_type);
2691 * Measure how big this message will be when rendered as RFC822.
2692 * We do this for two reasons:
2693 * 1. We need the RFC822 length for the new metadata record, so the
2694 * POP and IMAP services don't have to calculate message lengths
2695 * while the user is waiting (multiplied by potentially hundreds
2696 * or thousands of messages).
2697 * 2. If journaling is enabled, we will need an RFC822 version of the
2698 * message to attach to the journalized copy.
2700 if (CC->redirect_buffer != NULL) {
2701 syslog(LOG_ALERT, "msgbase: CC->redirect_buffer is not NULL during message submission!");
2702 exit(CTDLEXIT_REDIRECT);
2704 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
2705 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, QP_EADDR);
2706 smi.meta_rfc822_length = StrLength(CC->redirect_buffer);
2707 saved_rfc822_version = CC->redirect_buffer;
2708 CC->redirect_buffer = NULL;
2712 /* Now figure out where to store the pointers */
2713 syslog(LOG_DEBUG, "msgbase: storing pointers");
2715 /* If this is being done by the networker delivering a private
2716 * message, we want to BYPASS saving the sender's copy (because there
2717 * is no local sender; it would otherwise go to the Trashcan).
2719 if ((!CC->internal_pgm) || (recps == NULL)) {
2720 if (CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 1, msg) != 0) {
2721 syslog(LOG_ERR, "msgbase: ERROR saving message pointer %ld in %s", newmsgid, actual_rm);
2722 CtdlSaveMsgPointerInRoom(CtdlGetConfigStr("c_aideroom"), newmsgid, 0, msg);
2726 /* For internet mail, drop a copy in the outbound queue room */
2727 if ((recps != NULL) && (recps->num_internet > 0)) {
2728 CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, newmsgid, 0, msg);
2731 /* If other rooms are specified, drop them there too. */
2732 if ((recps != NULL) && (recps->num_room > 0)) {
2733 for (i=0; i<num_tokens(recps->recp_room, '|'); ++i) {
2734 extract_token(recipient, recps->recp_room, i, '|', sizeof recipient);
2735 syslog(LOG_DEBUG, "msgbase: delivering to room <%s>", recipient);
2736 CtdlSaveMsgPointerInRoom(recipient, newmsgid, 0, msg);
2740 /* Decide where bounces need to be delivered */
2741 if ((recps != NULL) && (recps->bounce_to == NULL)) {
2742 if (CC->logged_in) {
2743 strcpy(bounce_to, CC->user.fullname);
2745 else if (!IsEmptyStr(msg->cm_fields[eAuthor])){
2746 strcpy(bounce_to, msg->cm_fields[eAuthor]);
2748 recps->bounce_to = bounce_to;
2751 CM_SetFieldLONG(msg, eVltMsgNum, newmsgid);
2753 // If this is private, local mail, make a copy in the recipient's mailbox and bump the reference count.
2754 if ((recps != NULL) && (recps->num_local > 0)) {
2758 pch = recps->recp_local;
2759 recps->recp_local = recipient;
2760 ntokens = num_tokens(pch, '|');
2761 for (i=0; i<ntokens; ++i) {
2762 extract_token(recipient, pch, i, '|', sizeof recipient);
2763 syslog(LOG_DEBUG, "msgbase: delivering private local mail to <%s>", recipient);
2764 if (CtdlGetUser(&userbuf, recipient) == 0) {
2765 CtdlMailboxName(actual_rm, sizeof actual_rm, &userbuf, MAILROOM);
2766 CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0, msg);
2767 CtdlBumpNewMailCounter(userbuf.usernum); // if this user is logged in, tell them they have new mail.
2768 PerformMessageHooks(msg, recps, EVT_AFTERUSRMBOXSAVE);
2771 syslog(LOG_DEBUG, "msgbase: no user <%s>", recipient);
2772 CtdlSaveMsgPointerInRoom(CtdlGetConfigStr("c_aideroom"), newmsgid, 0, msg);
2775 recps->recp_local = pch;
2778 /* Perform "after save" hooks */
2779 syslog(LOG_DEBUG, "msgbase: performing after-save hooks");
2781 PerformMessageHooks(msg, recps, EVT_AFTERSAVE);
2782 CM_FlushField(msg, eVltMsgNum);
2784 /* Go back to the room we started from */
2785 syslog(LOG_DEBUG, "msgbase: returning to original room %s", hold_rm);
2786 if (strcasecmp(hold_rm, CC->room.QRname)) {
2787 CtdlUserGoto(hold_rm, 0, 1, NULL, NULL, NULL, NULL);
2791 * Any addresses to harvest for someone's address book?
2793 if ( (CC->logged_in) && (recps != NULL) ) {
2794 collected_addresses = harvest_collected_addresses(msg);
2797 if (collected_addresses != NULL) {
2798 aptr = (struct addresses_to_be_filed *) malloc(sizeof(struct addresses_to_be_filed));
2799 CtdlMailboxName(actual_rm, sizeof actual_rm, &CC->user, USERCONTACTSROOM);
2800 aptr->roomname = strdup(actual_rm);
2801 aptr->collected_addresses = collected_addresses;
2802 begin_critical_section(S_ATBF);
2805 end_critical_section(S_ATBF);
2809 * Determine whether this message qualifies for journaling.
2811 if (!CM_IsEmpty(msg, eJournal)) {
2812 qualified_for_journaling = 0;
2815 if (recps == NULL) {
2816 qualified_for_journaling = CtdlGetConfigInt("c_journal_pubmsgs");
2818 else if (recps->num_local + recps->num_internet > 0) {
2819 qualified_for_journaling = CtdlGetConfigInt("c_journal_email");
2822 qualified_for_journaling = CtdlGetConfigInt("c_journal_pubmsgs");
2827 * Do we have to perform journaling? If so, hand off the saved
2828 * RFC822 version will be handed off to the journaler for background
2829 * submit. Otherwise, we have to free the memory ourselves.
2831 if (saved_rfc822_version != NULL) {
2832 if (qualified_for_journaling) {
2833 JournalBackgroundSubmit(msg, saved_rfc822_version, recps);
2836 FreeStrBuf(&saved_rfc822_version);
2840 if ((recps != NULL) && (recps->bounce_to == bounce_to))
2841 recps->bounce_to = NULL;
2849 * Convenience function for generating small administrative messages.
2851 long quickie_message(char *from,
2859 struct CtdlMessage *msg;
2860 struct recptypes *recp = NULL;
2862 msg = malloc(sizeof(struct CtdlMessage));
2863 memset(msg, 0, sizeof(struct CtdlMessage));
2864 msg->cm_magic = CTDLMESSAGE_MAGIC;
2865 msg->cm_anon_type = MES_NORMAL;
2866 msg->cm_format_type = format_type;
2868 if (!IsEmptyStr(from)) {
2869 CM_SetField(msg, eAuthor, from, -1);
2871 else if (!IsEmptyStr(fromaddr)) {
2873 CM_SetField(msg, eAuthor, fromaddr, -1);
2874 pAt = strchr(msg->cm_fields[eAuthor], '@');
2876 CM_CutFieldAt(msg, eAuthor, pAt - msg->cm_fields[eAuthor]);
2880 msg->cm_fields[eAuthor] = strdup("Citadel");
2883 if (!IsEmptyStr(fromaddr)) CM_SetField(msg, erFc822Addr, fromaddr, -1);
2884 if (!IsEmptyStr(room)) CM_SetField(msg, eOriginalRoom, room, -1);
2885 if (!IsEmptyStr(to)) {
2886 CM_SetField(msg, eRecipient, to, -1);
2887 recp = validate_recipients(to, NULL, 0);
2889 if (!IsEmptyStr(subject)) {
2890 CM_SetField(msg, eMsgSubject, subject, -1);
2892 if (!IsEmptyStr(text)) {
2893 CM_SetField(msg, eMesageText, text, -1);
2896 long msgnum = CtdlSubmitMsg(msg, recp, room);
2898 if (recp != NULL) free_recipients(recp);
2904 * Back end function used by CtdlMakeMessage() and similar functions
2906 StrBuf *CtdlReadMessageBodyBuf(char *terminator, // token signalling EOT
2908 size_t maxlen, // maximum message length
2909 StrBuf *exist, // if non-null, append to it; exist is ALWAYS freed
2910 int crlf // CRLF newlines instead of LF
2918 LineBuf = NewStrBufPlain(NULL, SIZ);
2919 if (exist == NULL) {
2920 Message = NewStrBufPlain(NULL, 4 * SIZ);
2923 Message = NewStrBufDup(exist);
2926 /* Do we need to change leading ".." to "." for SMTP escaping? */
2927 if ((tlen == 1) && (*terminator == '.')) {
2931 /* read in the lines of message text one by one */
2933 if (CtdlClientGetLine(LineBuf) < 0) {
2936 if ((StrLength(LineBuf) == tlen) && (!strcmp(ChrPtr(LineBuf), terminator))) {
2939 if ( (!flushing) && (!finished) ) {
2941 StrBufAppendBufPlain(LineBuf, HKEY("\r\n"), 0);
2944 StrBufAppendBufPlain(LineBuf, HKEY("\n"), 0);
2947 /* Unescape SMTP-style input of two dots at the beginning of the line */
2948 if ((dotdot) && (StrLength(LineBuf) > 1) && (ChrPtr(LineBuf)[0] == '.')) {
2949 StrBufCutLeft(LineBuf, 1);
2951 StrBufAppendBuf(Message, LineBuf, 0);
2954 /* if we've hit the max msg length, flush the rest */
2955 if (StrLength(Message) >= maxlen) {
2959 } while (!finished);
2960 FreeStrBuf(&LineBuf);
2963 syslog(LOG_ERR, "msgbase: exceeded maximum message length of %ld - message was truncated", maxlen);
2970 // Back end function used by CtdlMakeMessage() and similar functions
2971 char *CtdlReadMessageBody(char *terminator, // token signalling EOT
2973 size_t maxlen, // maximum message length
2974 StrBuf *exist, // if non-null, append to it; exist is ALWAYS freed
2975 int crlf // CRLF newlines instead of LF
2979 Message = CtdlReadMessageBodyBuf(terminator,
2985 if (Message == NULL) {
2989 return SmashStrBuf(&Message);
2994 struct CtdlMessage *CtdlMakeMessage(
2995 struct ctdluser *author, /* author's user structure */
2996 char *recipient, /* NULL if it's not mail */
2997 char *recp_cc, /* NULL if it's not mail */
2998 char *room, /* room where it's going */
2999 int type, /* see MES_ types in header file */
3000 int format_type, /* variformat, plain text, MIME... */
3001 char *fake_name, /* who we're masquerading as */
3002 char *my_email, /* which of my email addresses to use (empty is ok) */
3003 char *subject, /* Subject (optional) */
3004 char *supplied_euid, /* ...or NULL if this is irrelevant */
3005 char *preformatted_text, /* ...or NULL to read text from client */
3006 char *references /* Thread references */
3008 return CtdlMakeMessageLen(
3009 author, /* author's user structure */
3010 recipient, /* NULL if it's not mail */
3011 (recipient)?strlen(recipient) : 0,
3012 recp_cc, /* NULL if it's not mail */
3013 (recp_cc)?strlen(recp_cc): 0,
3014 room, /* room where it's going */
3015 (room)?strlen(room): 0,
3016 type, /* see MES_ types in header file */
3017 format_type, /* variformat, plain text, MIME... */
3018 fake_name, /* who we're masquerading as */
3019 (fake_name)?strlen(fake_name): 0,
3020 my_email, /* which of my email addresses to use (empty is ok) */
3021 (my_email)?strlen(my_email): 0,
3022 subject, /* Subject (optional) */
3023 (subject)?strlen(subject): 0,
3024 supplied_euid, /* ...or NULL if this is irrelevant */
3025 (supplied_euid)?strlen(supplied_euid):0,
3026 preformatted_text, /* ...or NULL to read text from client */
3027 (preformatted_text)?strlen(preformatted_text) : 0,
3028 references, /* Thread references */
3029 (references)?strlen(references):0);
3035 * Build a binary message to be saved on disk.
3036 * (NOTE: if you supply 'preformatted_text', the buffer you give it
3037 * will become part of the message. This means you are no longer
3038 * responsible for managing that memory -- it will be freed along with
3039 * the rest of the fields when CM_Free() is called.)
3041 struct CtdlMessage *CtdlMakeMessageLen(
3042 struct ctdluser *author, /* author's user structure */
3043 char *recipient, /* NULL if it's not mail */
3045 char *recp_cc, /* NULL if it's not mail */
3047 char *room, /* room where it's going */
3049 int type, /* see MES_ types in header file */
3050 int format_type, /* variformat, plain text, MIME... */
3051 char *fake_name, /* who we're masquerading as */
3053 char *my_email, /* which of my email addresses to use (empty is ok) */
3055 char *subject, /* Subject (optional) */
3057 char *supplied_euid, /* ...or NULL if this is irrelevant */
3059 char *preformatted_text, /* ...or NULL to read text from client */
3061 char *references, /* Thread references */
3066 struct CtdlMessage *msg;
3068 StrBuf *FakeEncAuthor = NULL;
3070 msg = malloc(sizeof(struct CtdlMessage));
3071 memset(msg, 0, sizeof(struct CtdlMessage));
3072 msg->cm_magic = CTDLMESSAGE_MAGIC;
3073 msg->cm_anon_type = type;
3074 msg->cm_format_type = format_type;
3076 if (recipient != NULL) rcplen = string_trim(recipient);
3077 if (recp_cc != NULL) cclen = string_trim(recp_cc);
3079 /* Path or Return-Path */
3081 CM_SetField(msg, eMessagePath, my_email, myelen);
3083 else if (!IsEmptyStr(author->fullname)) {
3084 CM_SetField(msg, eMessagePath, author->fullname, -1);
3086 convert_spaces_to_underscores(msg->cm_fields[eMessagePath]);
3088 blen = snprintf(buf, sizeof buf, "%ld", (long)time(NULL));
3089 CM_SetField(msg, eTimestamp, buf, blen);
3092 FakeAuthor = NewStrBufPlain (fake_name, fnlen);
3095 FakeAuthor = NewStrBufPlain (author->fullname, -1);
3097 StrBufRFC2047encode(&FakeEncAuthor, FakeAuthor);
3098 CM_SetAsFieldSB(msg, eAuthor, &FakeEncAuthor);
3099 FreeStrBuf(&FakeAuthor);
3101 if (!!IsEmptyStr(CC->room.QRname)) {
3102 if (CC->room.QRflags & QR_MAILBOX) { /* room */
3103 CM_SetField(msg, eOriginalRoom, &CC->room.QRname[11], -1);
3106 CM_SetField(msg, eOriginalRoom, CC->room.QRname, -1);
3111 CM_SetField(msg, eRecipient, recipient, rcplen);
3114 CM_SetField(msg, eCarbonCopY, recp_cc, cclen);
3118 CM_SetField(msg, erFc822Addr, my_email, myelen);
3120 else if ( (author == &CC->user) && (!IsEmptyStr(CC->cs_inet_email)) ) {
3121 CM_SetField(msg, erFc822Addr, CC->cs_inet_email, -1);
3124 if (subject != NULL) {
3126 length = string_trim(subject);
3132 while ((subject[i] != '\0') &&
3133 (IsAscii = isascii(subject[i]) != 0 ))
3136 CM_SetField(msg, eMsgSubject, subject, subjlen);
3137 else /* ok, we've got utf8 in the string. */
3140 rfc2047Subj = rfc2047encode(subject, length);
3141 CM_SetAsField(msg, eMsgSubject, &rfc2047Subj, strlen(rfc2047Subj));
3148 CM_SetField(msg, eExclusiveID, supplied_euid, euidlen);
3152 CM_SetField(msg, eWeferences, references, reflen);
3155 if (preformatted_text != NULL) {
3156 CM_SetField(msg, eMesageText, preformatted_text, textlen);
3160 MsgBody = CtdlReadMessageBodyBuf(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
3161 if (MsgBody != NULL) {
3162 CM_SetAsFieldSB(msg, eMesageText, &MsgBody);
3171 * API function to delete messages which match a set of criteria
3172 * (returns the actual number of messages deleted)
3174 int CtdlDeleteMessages(const char *room_name, // which room
3175 long *dmsgnums, // array of msg numbers to be deleted
3176 int num_dmsgnums, // number of msgs to be deleted, or 0 for "any"
3177 char *content_type // or "" for any. regular expressions expected.
3179 struct ctdlroom qrbuf;
3180 struct cdbdata *cdbfr;
3181 long *msglist = NULL;
3182 long *dellist = NULL;
3185 int num_deleted = 0;
3187 struct MetaData smi;
3190 int need_to_free_re = 0;
3192 if (content_type) if (!IsEmptyStr(content_type)) {
3193 regcomp(&re, content_type, 0);
3194 need_to_free_re = 1;
3196 syslog(LOG_DEBUG, "msgbase: CtdlDeleteMessages(%s, %d msgs, %s)", room_name, num_dmsgnums, content_type);
3198 /* get room record, obtaining a lock... */
3199 if (CtdlGetRoomLock(&qrbuf, room_name) != 0) {
3200 syslog(LOG_ERR, "msgbase: CtdlDeleteMessages(): Room <%s> not found", room_name);
3201 if (need_to_free_re) regfree(&re);
3202 return(0); /* room not found */
3205 num_msgs = CtdlFetchMsgList(qrbuf.QRnumber, &msglist);
3207 dellist = malloc(num_msgs * sizeof(long));
3208 int have_contenttype = (content_type != NULL) && !IsEmptyStr(content_type);
3209 int have_delmsgs = (num_dmsgnums == 0) || (dmsgnums == NULL);
3210 int have_more_del = 1;
3212 num_msgs = sort_msglist(msglist, num_msgs);
3213 if (num_dmsgnums > 1) {
3214 num_dmsgnums = sort_msglist(dmsgnums, num_dmsgnums);
3218 while ((i < num_msgs) && (have_more_del)) {
3221 /* Set/clear a bit for each criterion */
3223 /* 0 messages in the list or a null list means that we are
3224 * interested in deleting any messages which meet the other criteria.
3227 delete_this |= 0x01;
3230 while ((i < num_msgs) && (msglist[i] < dmsgnums[j])) i++;
3235 if (msglist[i] == dmsgnums[j]) {
3236 delete_this |= 0x01;
3239 have_more_del = (j < num_dmsgnums);
3242 if (have_contenttype) {
3243 GetMetaData(&smi, msglist[i]);
3244 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) == 0) {
3245 delete_this |= 0x02;
3248 delete_this |= 0x02;
3251 /* Delete message only if all bits are set */
3252 if (delete_this == 0x03) {
3253 dellist[num_deleted++] = msglist[i];
3259 num_msgs = sort_msglist(msglist, num_msgs);
3260 cdb_store(CDB_MSGLISTS, &qrbuf.QRnumber, (int)sizeof(long), msglist, (int)(num_msgs * sizeof(long)));
3263 qrbuf.QRhighest = msglist[num_msgs - 1];
3266 qrbuf.QRhighest = 0;
3269 CtdlPutRoomLock(&qrbuf);
3271 /* Go through the messages we pulled out of the index, and decrement
3272 * their reference counts by 1. If this is the only room the message
3273 * was in, the reference count will reach zero and the message will
3274 * automatically be deleted from the database. We do this in a
3275 * separate pass because there might be plug-in hooks getting called,
3276 * and we don't want that happening during an S_ROOMS critical
3280 for (i=0; i<num_deleted; ++i) {
3281 PerformDeleteHooks(qrbuf.QRname, dellist[i]);
3283 AdjRefCountList(dellist, num_deleted, -1);
3285 /* Now free the memory we used, and go away. */
3286 if (msglist != NULL) free(msglist);
3287 if (dellist != NULL) free(dellist);
3288 syslog(LOG_DEBUG, "msgbase: %d message(s) deleted", num_deleted);
3289 if (need_to_free_re) regfree(&re);
3290 return (num_deleted);
3295 * GetMetaData() - Get the supplementary record for a message
3297 void GetMetaData(struct MetaData *smibuf, long msgnum) {
3298 struct cdbdata cdbsmi;
3301 memset(smibuf, 0, sizeof(struct MetaData));
3302 smibuf->meta_msgnum = msgnum;
3303 smibuf->meta_refcount = 1; /* Default reference count is 1 */
3305 /* Use the negative of the message number for its supp record index */
3306 TheIndex = (0L - msgnum);
3308 cdbsmi = cdb_fetch(CDB_MSGMAIN, &TheIndex, sizeof(long));
3309 if (cdbsmi.len == 0) {
3310 return; /* record not found; leave it alone */
3312 memcpy(smibuf, cdbsmi.ptr, ((cdbsmi.len > sizeof(struct MetaData)) ? sizeof(struct MetaData) : cdbsmi.len));
3318 * PutMetaData() - (re)write supplementary record for a message
3320 void PutMetaData(struct MetaData *smibuf)
3324 /* Use the negative of the message number for the metadata db index */
3325 TheIndex = (0L - smibuf->meta_msgnum);
3327 cdb_store(CDB_MSGMAIN,
3328 &TheIndex, (int)sizeof(long),
3329 smibuf, (int)sizeof(struct MetaData)
3335 * Convenience function to process a big block of AdjRefCount() operations
3337 void AdjRefCountList(long *msgnum, long nmsg, int incr)
3341 for (i = 0; i < nmsg; i++) {
3342 AdjRefCount(msgnum[i], incr);
3348 * AdjRefCount - adjust the reference count for a message. We need to delete from disk any message whose reference count reaches zero.
3350 void AdjRefCount(long msgnum, int incr)
3352 struct MetaData smi;
3355 /* This is a *tight* critical section; please keep it that way, as
3356 * it may get called while nested in other critical sections.
3357 * Complicating this any further will surely cause deadlock!
3359 begin_critical_section(S_SUPPMSGMAIN);
3360 GetMetaData(&smi, msgnum);
3361 smi.meta_refcount += incr;
3363 end_critical_section(S_SUPPMSGMAIN);
3364 syslog(LOG_DEBUG, "msgbase: AdjRefCount() msg %ld ref count delta %+d, is now %d", msgnum, incr, smi.meta_refcount);
3366 /* If the reference count is now zero, delete both the message and its metadata record.
3368 if (smi.meta_refcount == 0) {
3369 syslog(LOG_DEBUG, "msgbase: deleting message <%ld>", msgnum);
3371 /* Call delete hooks with NULL room to show it has gone altogether */
3372 PerformDeleteHooks(NULL, msgnum);
3374 /* Remove from message base */
3376 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3377 cdb_delete(CDB_BIGMSGS, &delnum, (int)sizeof(long));
3379 /* Remove metadata record */
3380 delnum = (0L - msgnum);
3381 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3387 * Write a generic object to this room
3389 * Returns the message number of the written object, in case you need it.
3391 long CtdlWriteObject(char *req_room, /* Room to stuff it in */
3392 char *content_type, /* MIME type of this object */
3393 char *raw_message, /* Data to be written */
3394 off_t raw_length, /* Size of raw_message */
3395 struct ctdluser *is_mailbox, /* Mailbox room? */
3396 int is_binary, /* Is encoding necessary? */
3397 unsigned int flags /* Internal save flags */
3399 struct ctdlroom qrbuf;
3400 char roomname[ROOMNAMELEN];
3401 struct CtdlMessage *msg;
3402 StrBuf *encoded_message = NULL;
3404 if (is_mailbox != NULL) {
3405 CtdlMailboxName(roomname, sizeof roomname, is_mailbox, req_room);
3408 safestrncpy(roomname, req_room, sizeof(roomname));
3411 syslog(LOG_DEBUG, "msfbase: raw length is %ld", (long)raw_length);
3414 encoded_message = NewStrBufPlain(NULL, (size_t) (((raw_length * 134) / 100) + 4096 ) );
3417 encoded_message = NewStrBufPlain(NULL, (size_t)(raw_length + 4096));
3420 StrBufAppendBufPlain(encoded_message, HKEY("Content-type: "), 0);
3421 StrBufAppendBufPlain(encoded_message, content_type, -1, 0);
3422 StrBufAppendBufPlain(encoded_message, HKEY("\n"), 0);
3425 StrBufAppendBufPlain(encoded_message, HKEY("Content-transfer-encoding: base64\n\n"), 0);
3428 StrBufAppendBufPlain(encoded_message, HKEY("Content-transfer-encoding: 7bit\n\n"), 0);
3432 StrBufBase64Append(encoded_message, NULL, raw_message, raw_length, 0);
3435 StrBufAppendBufPlain(encoded_message, raw_message, raw_length, 0);
3438 syslog(LOG_DEBUG, "msgbase: allocating");
3439 msg = malloc(sizeof(struct CtdlMessage));
3440 memset(msg, 0, sizeof(struct CtdlMessage));
3441 msg->cm_magic = CTDLMESSAGE_MAGIC;
3442 msg->cm_anon_type = MES_NORMAL;
3443 msg->cm_format_type = 4;
3444 CM_SetField(msg, eAuthor, CC->user.fullname, -1);
3445 CM_SetField(msg, eOriginalRoom, req_room, -1);
3446 msg->cm_flags = flags;
3448 CM_SetAsFieldSB(msg, eMesageText, &encoded_message);
3450 /* Create the requested room if we have to. */
3451 if (CtdlGetRoom(&qrbuf, roomname) != 0) {
3452 CtdlCreateRoom(roomname, ( (is_mailbox != NULL) ? 5 : 3 ), "", 0, 1, 0, VIEW_BBS);
3455 /* Now write the data */
3456 long new_msgnum = CtdlSubmitMsg(msg, NULL, roomname);
3462 /************************************************************************/
3463 /* MODULE INITIALIZATION */
3464 /************************************************************************/
3466 char *ctdl_module_init_msgbase(void) {
3468 FillMsgKeyLookupTable();
3471 /* return our module id for the log */