2 * Implements the message store.
4 * Copyright (c) 1987-2020 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * 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.
21 #include <libcitadel.h>
22 #include "ctdl_module.h"
23 #include "citserver.h"
26 #include "clientsocket.h"
30 #include "internet_addressing.h"
31 #include "euidindex.h"
33 #include "journaling.h"
35 struct addresses_to_be_filed *atbf = NULL;
38 * These are the four-character field headers we use when outputting
39 * messages in Citadel format (as opposed to RFC822 format).
42 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
43 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
44 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
45 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
46 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
47 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
48 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
49 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
51 "from", // A -> eAuthor
52 NULL, // B -> eBig_message
53 NULL, // C (formerly used as eRemoteRoom)
54 NULL, // D (formerly used as eDestination)
55 "exti", // E -> eXclusivID
56 "rfca", // F -> erFc822Addr
58 "hnod", // H (formerly used as eHumanNode)
59 "msgn", // I -> emessageId
60 "jrnl", // J -> eJournal
61 "rep2", // K -> eReplyTo
62 "list", // L -> eListID
63 "text", // M -> eMesageText
64 "locl", // N -> eOrigLocal
65 "room", // O -> eOriginalRoom
66 "path", // P -> eMessagePath
68 "rcpt", // R -> eRecipient
69 NULL, // S (formerly used as eSpecialField)
70 "time", // T -> eTimestamp
71 "subj", // U -> eMsgSubject
72 "nvto", // V -> eenVelopeTo
73 "wefw", // W -> eWeferences
75 "cccc", // Y -> eCarbonCopY
80 HashList *msgKeyLookup = NULL;
82 int GetFieldFromMnemonic(eMsgField *f, const char* c)
85 if (GetHash(msgKeyLookup, c, 4, &v)) {
92 void FillMsgKeyLookupTable(void)
96 msgKeyLookup = NewHash (1, FourHash);
98 for (i=0; i < 91; i++) {
99 if (msgkeys[i] != NULL) {
100 Put(msgKeyLookup, msgkeys[i], 4, (void*)i, reference_free_handler);
106 eMsgField FieldOrder[] = {
107 /* Important fields */
115 /* Semi-important fields */
121 /* G is not used yet */
124 /* Q is not used yet */
126 /* X is not used yet */
127 /* Z is not used yet */
134 /* Message text (MUST be last) */
136 /* Not saved to disk:
141 static const long NDiskFields = sizeof(FieldOrder) / sizeof(eMsgField);
144 int CM_IsEmpty(struct CtdlMessage *Msg, eMsgField which)
146 return !((Msg->cm_fields[which] != NULL) && (Msg->cm_fields[which][0] != '\0'));
150 void CM_SetField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
152 if (Msg->cm_fields[which] != NULL) {
153 free (Msg->cm_fields[which]);
155 if (length < 0) { // You can set the length to -1 to have CM_SetField measure it for you
156 length = strlen(buf);
158 Msg->cm_fields[which] = malloc(length + 1);
159 memcpy(Msg->cm_fields[which], buf, length);
160 Msg->cm_fields[which][length] = '\0';
161 Msg->cm_lengths[which] = length;
165 void CM_SetFieldLONG(struct CtdlMessage *Msg, eMsgField which, long lvalue)
169 len = snprintf(buf, sizeof(buf), "%ld", lvalue);
170 CM_SetField(Msg, which, buf, len);
174 void CM_CutFieldAt(struct CtdlMessage *Msg, eMsgField WhichToCut, long maxlen)
176 if (Msg->cm_fields[WhichToCut] == NULL)
179 if (Msg->cm_lengths[WhichToCut] > maxlen)
181 Msg->cm_fields[WhichToCut][maxlen] = '\0';
182 Msg->cm_lengths[WhichToCut] = maxlen;
187 void CM_FlushField(struct CtdlMessage *Msg, eMsgField which)
189 if (Msg->cm_fields[which] != NULL)
190 free (Msg->cm_fields[which]);
191 Msg->cm_fields[which] = NULL;
192 Msg->cm_lengths[which] = 0;
196 void CM_Flush(struct CtdlMessage *Msg)
200 if (CM_IsValidMsg(Msg) == 0) {
204 for (i = 0; i < 256; ++i) {
205 CM_FlushField(Msg, i);
210 void CM_CopyField(struct CtdlMessage *Msg, eMsgField WhichToPutTo, eMsgField WhichtToCopy)
213 if (Msg->cm_fields[WhichToPutTo] != NULL) {
214 free (Msg->cm_fields[WhichToPutTo]);
217 if (Msg->cm_fields[WhichtToCopy] != NULL) {
218 len = Msg->cm_lengths[WhichtToCopy];
219 Msg->cm_fields[WhichToPutTo] = malloc(len + 1);
220 memcpy(Msg->cm_fields[WhichToPutTo], Msg->cm_fields[WhichtToCopy], len);
221 Msg->cm_fields[WhichToPutTo][len] = '\0';
222 Msg->cm_lengths[WhichToPutTo] = len;
225 Msg->cm_fields[WhichToPutTo] = NULL;
226 Msg->cm_lengths[WhichToPutTo] = 0;
231 void CM_PrependToField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
233 if (Msg->cm_fields[which] != NULL) {
238 oldmsgsize = Msg->cm_lengths[which] + 1;
239 newmsgsize = length + oldmsgsize;
241 new = malloc(newmsgsize);
242 memcpy(new, buf, length);
243 memcpy(new + length, Msg->cm_fields[which], oldmsgsize);
244 free(Msg->cm_fields[which]);
245 Msg->cm_fields[which] = new;
246 Msg->cm_lengths[which] = newmsgsize - 1;
249 Msg->cm_fields[which] = malloc(length + 1);
250 memcpy(Msg->cm_fields[which], buf, length);
251 Msg->cm_fields[which][length] = '\0';
252 Msg->cm_lengths[which] = length;
257 void CM_SetAsField(struct CtdlMessage *Msg, eMsgField which, char **buf, long length)
259 if (Msg->cm_fields[which] != NULL) {
260 free (Msg->cm_fields[which]);
263 Msg->cm_fields[which] = *buf;
265 if (length < 0) { // You can set the length to -1 to have CM_SetField measure it for you
266 Msg->cm_lengths[which] = strlen(Msg->cm_fields[which]);
269 Msg->cm_lengths[which] = length;
274 void CM_SetAsFieldSB(struct CtdlMessage *Msg, eMsgField which, StrBuf **buf)
276 if (Msg->cm_fields[which] != NULL) {
277 free (Msg->cm_fields[which]);
280 Msg->cm_lengths[which] = StrLength(*buf);
281 Msg->cm_fields[which] = SmashStrBuf(buf);
285 void CM_GetAsField(struct CtdlMessage *Msg, eMsgField which, char **ret, long *retlen)
287 if (Msg->cm_fields[which] != NULL) {
288 *retlen = Msg->cm_lengths[which];
289 *ret = Msg->cm_fields[which];
290 Msg->cm_fields[which] = NULL;
291 Msg->cm_lengths[which] = 0;
301 * Returns 1 if the supplied pointer points to a valid Citadel message.
302 * If the pointer is NULL or the magic number check fails, returns 0.
304 int CM_IsValidMsg(struct CtdlMessage *msg) {
308 if ((msg->cm_magic) != CTDLMESSAGE_MAGIC) {
309 syslog(LOG_WARNING, "msgbase: CM_IsValidMsg() self-check failed");
316 void CM_FreeContents(struct CtdlMessage *msg)
320 for (i = 0; i < 256; ++i)
321 if (msg->cm_fields[i] != NULL) {
322 free(msg->cm_fields[i]);
323 msg->cm_lengths[i] = 0;
326 msg->cm_magic = 0; /* just in case */
331 * 'Destructor' for struct CtdlMessage
333 void CM_Free(struct CtdlMessage *msg)
335 if (CM_IsValidMsg(msg) == 0) {
336 if (msg != NULL) free (msg);
339 CM_FreeContents(msg);
344 int CM_DupField(eMsgField i, struct CtdlMessage *OrgMsg, struct CtdlMessage *NewMsg)
347 len = OrgMsg->cm_lengths[i];
348 NewMsg->cm_fields[i] = malloc(len + 1);
349 if (NewMsg->cm_fields[i] == NULL) {
352 memcpy(NewMsg->cm_fields[i], OrgMsg->cm_fields[i], len);
353 NewMsg->cm_fields[i][len] = '\0';
354 NewMsg->cm_lengths[i] = len;
359 struct CtdlMessage * CM_Duplicate(struct CtdlMessage *OrgMsg)
362 struct CtdlMessage *NewMsg;
364 if (CM_IsValidMsg(OrgMsg) == 0) {
367 NewMsg = (struct CtdlMessage *)malloc(sizeof(struct CtdlMessage));
368 if (NewMsg == NULL) {
372 memcpy(NewMsg, OrgMsg, sizeof(struct CtdlMessage));
374 memset(&NewMsg->cm_fields, 0, sizeof(char*) * 256);
376 for (i = 0; i < 256; ++i) {
377 if (OrgMsg->cm_fields[i] != NULL) {
378 if (!CM_DupField(i, OrgMsg, NewMsg)) {
389 /* Determine if a given message matches the fields in a message template.
390 * Return 0 for a successful match.
392 int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template) {
395 /* If there aren't any fields in the template, all messages will
398 if (template == NULL) return(0);
400 /* Null messages are bogus. */
401 if (msg == NULL) return(1);
403 for (i='A'; i<='Z'; ++i) {
404 if (template->cm_fields[i] != NULL) {
405 if (msg->cm_fields[i] == NULL) {
406 /* Considered equal if temmplate is empty string */
407 if (IsEmptyStr(template->cm_fields[i])) continue;
410 if ((template->cm_lengths[i] != msg->cm_lengths[i]) ||
411 (strcasecmp(msg->cm_fields[i], template->cm_fields[i])))
416 /* All compares succeeded: we have a match! */
422 * Retrieve the "seen" message list for the current room.
424 void CtdlGetSeen(char *buf, int which_set) {
427 /* Learn about the user and room in question */
428 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
430 if (which_set == ctdlsetseen_seen) {
431 safestrncpy(buf, vbuf.v_seen, SIZ);
433 if (which_set == ctdlsetseen_answered) {
434 safestrncpy(buf, vbuf.v_answered, SIZ);
440 * Manipulate the "seen msgs" string (or other message set strings)
442 void CtdlSetSeen(long *target_msgnums, int num_target_msgnums,
443 int target_setting, int which_set,
444 struct ctdluser *which_user, struct ctdlroom *which_room) {
445 struct cdbdata *cdbfr;
459 char *is_set; /* actually an array of booleans */
461 /* Don't bother doing *anything* if we were passed a list of zero messages */
462 if (num_target_msgnums < 1) {
466 /* If no room was specified, we go with the current room. */
468 which_room = &CC->room;
471 /* If no user was specified, we go with the current user. */
473 which_user = &CC->user;
476 syslog(LOG_DEBUG, "msgbase: CtdlSetSeen(%d msgs starting with %ld, %s, %d) in <%s>",
477 num_target_msgnums, target_msgnums[0],
478 (target_setting ? "SET" : "CLEAR"),
482 /* Learn about the user and room in question */
483 CtdlGetRelationship(&vbuf, which_user, which_room);
485 /* Load the message list */
486 cdbfr = cdb_fetch(CDB_MSGLISTS, &which_room->QRnumber, sizeof(long));
488 msglist = (long *) cdbfr->ptr;
489 cdbfr->ptr = NULL; /* CtdlSetSeen() now owns this memory */
490 num_msgs = cdbfr->len / sizeof(long);
493 return; /* No messages at all? No further action. */
496 is_set = malloc(num_msgs * sizeof(char));
497 memset(is_set, 0, (num_msgs * sizeof(char)) );
499 /* Decide which message set we're manipulating */
501 case ctdlsetseen_seen:
502 vset = NewStrBufPlain(vbuf.v_seen, -1);
504 case ctdlsetseen_answered:
505 vset = NewStrBufPlain(vbuf.v_answered, -1);
512 #if 0 /* This is a special diagnostic section. Do not allow it to run during normal operation. */
513 syslog(LOG_DEBUG, "There are %d messages in the room.\n", num_msgs);
514 for (i=0; i<num_msgs; ++i) {
515 if ((i > 0) && (msglist[i] <= msglist[i-1])) abort();
517 syslog(LOG_DEBUG, "We are twiddling %d of them.\n", num_target_msgnums);
518 for (k=0; k<num_target_msgnums; ++k) {
519 if ((k > 0) && (target_msgnums[k] <= target_msgnums[k-1])) abort();
523 /* Translate the existing sequence set into an array of booleans */
524 setstr = NewStrBuf();
528 while (StrBufExtract_NextToken(setstr, vset, &pvset, ',') >= 0) {
530 StrBufExtract_token(lostr, setstr, 0, ':');
531 if (StrBufNum_tokens(setstr, ':') >= 2) {
532 StrBufExtract_token(histr, setstr, 1, ':');
536 StrBufAppendBuf(histr, lostr, 0);
539 if (!strcmp(ChrPtr(histr), "*")) {
546 for (i = 0; i < num_msgs; ++i) {
547 if ((msglist[i] >= lo) && (msglist[i] <= hi)) {
556 /* Now translate the array of booleans back into a sequence set */
562 for (i=0; i<num_msgs; ++i) {
566 for (k=0; k<num_target_msgnums; ++k) {
567 if (msglist[i] == target_msgnums[k]) {
568 is_seen = target_setting;
572 if ((was_seen == 0) && (is_seen == 1)) {
575 else if ((was_seen == 1) && (is_seen == 0)) {
578 if (StrLength(vset) > 0) {
579 StrBufAppendBufPlain(vset, HKEY(","), 0);
582 StrBufAppendPrintf(vset, "%ld", hi);
585 StrBufAppendPrintf(vset, "%ld:%ld", lo, hi);
589 if ((is_seen) && (i == num_msgs - 1)) {
590 if (StrLength(vset) > 0) {
591 StrBufAppendBufPlain(vset, HKEY(","), 0);
593 if ((i==0) || (was_seen == 0)) {
594 StrBufAppendPrintf(vset, "%ld", msglist[i]);
597 StrBufAppendPrintf(vset, "%ld:%ld", lo, msglist[i]);
605 * We will have to stuff this string back into a 4096 byte buffer, so if it's
606 * larger than that now, truncate it by removing tokens from the beginning.
607 * The limit of 100 iterations is there to prevent an infinite loop in case
608 * something unexpected happens.
610 int number_of_truncations = 0;
611 while ( (StrLength(vset) > SIZ) && (number_of_truncations < 100) ) {
612 StrBufRemove_token(vset, 0, ',');
613 ++number_of_truncations;
617 * If we're truncating the sequence set of messages marked with the 'seen' flag,
618 * we want the earliest messages (the truncated ones) to be marked, not unmarked.
619 * Otherwise messages at the beginning will suddenly appear to be 'unseen'.
621 if ( (which_set == ctdlsetseen_seen) && (number_of_truncations > 0) ) {
623 first_tok = NewStrBuf();
624 StrBufExtract_token(first_tok, vset, 0, ',');
625 StrBufRemove_token(vset, 0, ',');
627 if (StrBufNum_tokens(first_tok, ':') > 1) {
628 StrBufRemove_token(first_tok, 0, ':');
632 new_set = NewStrBuf();
633 StrBufAppendBufPlain(new_set, HKEY("1:"), 0);
634 StrBufAppendBuf(new_set, first_tok, 0);
635 StrBufAppendBufPlain(new_set, HKEY(":"), 0);
636 StrBufAppendBuf(new_set, vset, 0);
639 FreeStrBuf(&first_tok);
643 /* Decide which message set we're manipulating */
645 case ctdlsetseen_seen:
646 safestrncpy(vbuf.v_seen, ChrPtr(vset), sizeof vbuf.v_seen);
648 case ctdlsetseen_answered:
649 safestrncpy(vbuf.v_answered, ChrPtr(vset), sizeof vbuf.v_answered);
655 CtdlSetRelationship(&vbuf, which_user, which_room);
661 * API function to perform an operation for each qualifying message in the
662 * current room. (Returns the number of messages processed.)
664 int CtdlForEachMessage(int mode, long ref, char *search_string,
666 struct CtdlMessage *compare,
667 ForEachMsgCallback CallBack,
672 struct cdbdata *cdbfr;
673 long *msglist = NULL;
675 int num_processed = 0;
678 struct CtdlMessage *msg = NULL;
681 int printed_lastold = 0;
682 int num_search_msgs = 0;
683 long *search_msgs = NULL;
685 int need_to_free_re = 0;
688 if ((content_type) && (!IsEmptyStr(content_type))) {
689 regcomp(&re, content_type, 0);
693 /* Learn about the user and room in question */
694 if (server_shutting_down) {
695 if (need_to_free_re) regfree(&re);
698 CtdlGetUser(&CC->user, CC->curr_user);
700 if (server_shutting_down) {
701 if (need_to_free_re) regfree(&re);
704 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
706 if (server_shutting_down) {
707 if (need_to_free_re) regfree(&re);
711 /* Load the message list */
712 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
714 if (need_to_free_re) regfree(&re);
715 return 0; /* No messages at all? No further action. */
718 msglist = (long *) cdbfr->ptr;
719 num_msgs = cdbfr->len / sizeof(long);
721 cdbfr->ptr = NULL; /* clear this so that cdb_free() doesn't free it */
722 cdb_free(cdbfr); /* we own this memory now */
725 * Now begin the traversal.
727 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
729 /* If the caller is looking for a specific MIME type, filter
730 * out all messages which are not of the type requested.
732 if ((content_type != NULL) && (!IsEmptyStr(content_type))) {
734 /* This call to GetMetaData() sits inside this loop
735 * so that we only do the extra database read per msg
736 * if we need to. Doing the extra read all the time
737 * really kills the server. If we ever need to use
738 * metadata for another search criterion, we need to
739 * move the read somewhere else -- but still be smart
740 * enough to only do the read if the caller has
741 * specified something that will need it.
743 if (server_shutting_down) {
744 if (need_to_free_re) regfree(&re);
748 GetMetaData(&smi, msglist[a]);
750 /* if (strcasecmp(smi.meta_content_type, content_type)) { old non-regex way */
751 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) != 0) {
757 num_msgs = sort_msglist(msglist, num_msgs);
759 /* If a template was supplied, filter out the messages which
760 * don't match. (This could induce some delays!)
763 if (compare != NULL) {
764 for (a = 0; a < num_msgs; ++a) {
765 if (server_shutting_down) {
766 if (need_to_free_re) regfree(&re);
770 msg = CtdlFetchMessage(msglist[a], 1);
772 if (CtdlMsgCmp(msg, compare)) {
781 /* If a search string was specified, get a message list from
782 * the full text index and remove messages which aren't on both
786 * Since the lists are sorted and strictly ascending, and the
787 * output list is guaranteed to be shorter than or equal to the
788 * input list, we overwrite the bottom of the input list. This
789 * eliminates the need to memmove big chunks of the list over and
792 if ( (num_msgs > 0) && (mode == MSGS_SEARCH) && (search_string) ) {
794 /* Call search module via hook mechanism.
795 * NULL means use any search function available.
796 * otherwise replace with a char * to name of search routine
798 CtdlModuleDoSearch(&num_search_msgs, &search_msgs, search_string, "fulltext");
800 if (num_search_msgs > 0) {
804 orig_num_msgs = num_msgs;
806 for (i=0; i<orig_num_msgs; ++i) {
807 for (j=0; j<num_search_msgs; ++j) {
808 if (msglist[i] == search_msgs[j]) {
809 msglist[num_msgs++] = msglist[i];
815 num_msgs = 0; /* No messages qualify */
817 if (search_msgs != NULL) free(search_msgs);
819 /* Now that we've purged messages which don't contain the search
820 * string, treat a MSGS_SEARCH just like a MSGS_ALL from this
827 * Now iterate through the message list, according to the
828 * criteria supplied by the caller.
831 for (a = 0; a < num_msgs; ++a) {
832 if (server_shutting_down) {
833 if (need_to_free_re) regfree(&re);
835 return num_processed;
837 thismsg = msglist[a];
838 if (mode == MSGS_ALL) {
842 is_seen = is_msg_in_sequence_set(
843 vbuf.v_seen, thismsg);
844 if (is_seen) lastold = thismsg;
850 || ((mode == MSGS_OLD) && (is_seen))
851 || ((mode == MSGS_NEW) && (!is_seen))
852 || ((mode == MSGS_LAST) && (a >= (num_msgs - ref)))
853 || ((mode == MSGS_FIRST) && (a < ref))
854 || ((mode == MSGS_GT) && (thismsg > ref))
855 || ((mode == MSGS_LT) && (thismsg < ref))
856 || ((mode == MSGS_EQ) && (thismsg == ref))
859 if ((mode == MSGS_NEW) && (CC->user.flags & US_LASTOLD) && (lastold > 0L) && (printed_lastold == 0) && (!is_seen)) {
861 CallBack(lastold, userdata);
867 CallBack(thismsg, userdata);
872 if (need_to_free_re) regfree(&re);
875 * We cache the most recent msglist in order to do security checks later
877 if (CC->client_socket > 0) {
878 if (CC->cached_msglist != NULL) {
879 free(CC->cached_msglist);
881 CC->cached_msglist = msglist;
882 CC->cached_num_msgs = num_msgs;
888 return num_processed;
893 * memfmout() - Citadel text formatter and paginator.
894 * Although the original purpose of this routine was to format
895 * text to the reader's screen width, all we're really using it
896 * for here is to format text out to 80 columns before sending it
897 * to the client. The client software may reformat it again.
900 char *mptr, /* where are we going to get our text from? */
901 const char *nl /* string to terminate lines with */
904 unsigned char ch = 0;
911 while (ch=*(mptr++), ch != 0) {
914 if (client_write(outbuf, len) == -1) {
915 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
919 if (client_write(nl, nllen) == -1) {
920 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
925 else if (ch == '\r') {
926 /* Ignore carriage returns. Newlines are always LF or CRLF but never CR. */
928 else if (isspace(ch)) {
929 if (column > 72) { /* Beyond 72 columns, break on the next space */
930 if (client_write(outbuf, len) == -1) {
931 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
935 if (client_write(nl, nllen) == -1) {
936 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
949 if (column > 1000) { /* Beyond 1000 columns, break anywhere */
950 if (client_write(outbuf, len) == -1) {
951 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
955 if (client_write(nl, nllen) == -1) {
956 syslog(LOG_ERR, "msgbase: memfmout(): aborting due to write failure");
964 if (client_write(outbuf, len) == -1) {
965 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
968 client_write(nl, nllen);
975 * Callback function for mime parser that simply lists the part
977 void list_this_part(char *name, char *filename, char *partnum, char *disp,
978 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
979 char *cbid, void *cbuserdata)
983 ma = (struct ma_info *)cbuserdata;
984 if (ma->is_ma == 0) {
985 cprintf("part=%s|%s|%s|%s|%s|%ld|%s|%s\n",
999 * Callback function for multipart prefix
1001 void list_this_pref(char *name, char *filename, char *partnum, char *disp,
1002 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1003 char *cbid, void *cbuserdata)
1007 ma = (struct ma_info *)cbuserdata;
1008 if (!strcasecmp(cbtype, "multipart/alternative")) {
1012 if (ma->is_ma == 0) {
1013 cprintf("pref=%s|%s\n", partnum, cbtype);
1019 * Callback function for multipart sufffix
1021 void list_this_suff(char *name, char *filename, char *partnum, char *disp,
1022 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1023 char *cbid, void *cbuserdata)
1027 ma = (struct ma_info *)cbuserdata;
1028 if (ma->is_ma == 0) {
1029 cprintf("suff=%s|%s\n", partnum, cbtype);
1031 if (!strcasecmp(cbtype, "multipart/alternative")) {
1038 * Callback function for mime parser that opens a section for downloading
1039 * we use serv_files function here:
1041 extern void OpenCmdResult(char *filename, const char *mime_type);
1042 void mime_download(char *name, char *filename, char *partnum, char *disp,
1043 void *content, char *cbtype, char *cbcharset, size_t length,
1044 char *encoding, char *cbid, void *cbuserdata)
1048 /* Silently go away if there's already a download open. */
1049 if (CC->download_fp != NULL)
1053 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
1054 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
1056 CC->download_fp = tmpfile();
1057 if (CC->download_fp == NULL) {
1058 syslog(LOG_EMERG, "msgbase: mime_download() couldn't write: %m");
1059 cprintf("%d cannot open temporary file: %s\n", ERROR + INTERNAL_ERROR, strerror(errno));
1063 rv = fwrite(content, length, 1, CC->download_fp);
1065 syslog(LOG_EMERG, "msgbase: mime_download() Couldn't write: %m");
1066 cprintf("%d unable to write tempfile.\n", ERROR + TOO_BIG);
1067 fclose(CC->download_fp);
1068 CC->download_fp = NULL;
1071 fflush(CC->download_fp);
1072 rewind(CC->download_fp);
1074 OpenCmdResult(filename, cbtype);
1080 * Callback function for mime parser that outputs a section all at once.
1081 * We can specify the desired section by part number *or* content-id.
1083 void mime_spew_section(char *name, char *filename, char *partnum, char *disp,
1084 void *content, char *cbtype, char *cbcharset, size_t length,
1085 char *encoding, char *cbid, void *cbuserdata)
1087 int *found_it = (int *)cbuserdata;
1090 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
1091 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
1094 cprintf("%d %d|-1|%s|%s|%s\n",
1101 client_write(content, length);
1106 struct CtdlMessage *CtdlDeserializeMessage(long msgnum, int with_body, const char *Buffer, long Length)
1108 struct CtdlMessage *ret = NULL;
1110 const char *upper_bound;
1112 cit_uint8_t field_header;
1116 upper_bound = Buffer + Length;
1121 /* Parse the three bytes that begin EVERY message on disk.
1122 * The first is always 0xFF, the on-disk magic number.
1123 * The second is the anonymous/public type byte.
1124 * The third is the format type byte (vari, fixed, or MIME).
1128 syslog(LOG_ERR, "msgbase: message %ld appears to be corrupted", msgnum);
1131 ret = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1132 memset(ret, 0, sizeof(struct CtdlMessage));
1134 ret->cm_magic = CTDLMESSAGE_MAGIC;
1135 ret->cm_anon_type = *mptr++; /* Anon type byte */
1136 ret->cm_format_type = *mptr++; /* Format type byte */
1139 * The rest is zero or more arbitrary fields. Load them in.
1140 * We're done when we encounter either a zero-length field or
1141 * have just processed the 'M' (message text) field.
1144 field_header = '\0';
1147 /* work around possibly buggy messages: */
1148 while (field_header == '\0') {
1149 if (mptr >= upper_bound) {
1152 field_header = *mptr++;
1154 if (mptr >= upper_bound) {
1157 which = field_header;
1160 CM_SetField(ret, which, mptr, len);
1162 mptr += len + 1; /* advance to next field */
1164 } while ((mptr < upper_bound) && (field_header != 'M'));
1171 * Load a message from disk into memory.
1172 * This is used by CtdlOutputMsg() and other fetch functions.
1174 * NOTE: Caller is responsible for freeing the returned CtdlMessage struct
1175 * using the CM_Free(); function.
1177 struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body)
1179 struct cdbdata *dmsgtext;
1180 struct CtdlMessage *ret = NULL;
1182 syslog(LOG_DEBUG, "msgbase: CtdlFetchMessage(%ld, %d)", msgnum, with_body);
1183 dmsgtext = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
1184 if (dmsgtext == NULL) {
1185 syslog(LOG_ERR, "msgbase: CtdlFetchMessage(%ld, %d) Failed!", msgnum, with_body);
1189 if (dmsgtext->ptr[dmsgtext->len - 1] != '\0') {
1190 syslog(LOG_ERR, "msgbase: CtdlFetchMessage(%ld, %d) Forcefully terminating message!!", msgnum, with_body);
1191 dmsgtext->ptr[dmsgtext->len - 1] = '\0';
1194 ret = CtdlDeserializeMessage(msgnum, with_body, dmsgtext->ptr, dmsgtext->len);
1202 /* Always make sure there's something in the msg text field. If
1203 * it's NULL, the message text is most likely stored separately,
1204 * so go ahead and fetch that. Failing that, just set a dummy
1205 * body so other code doesn't barf.
1207 if ( (CM_IsEmpty(ret, eMesageText)) && (with_body) ) {
1208 dmsgtext = cdb_fetch(CDB_BIGMSGS, &msgnum, sizeof(long));
1209 if (dmsgtext != NULL) {
1210 CM_SetAsField(ret, eMesageText, &dmsgtext->ptr, dmsgtext->len - 1);
1214 if (CM_IsEmpty(ret, eMesageText)) {
1215 CM_SetField(ret, eMesageText, HKEY("\r\n\r\n (no text)\r\n"));
1223 * Pre callback function for multipart/alternative
1225 * NOTE: this differs from the standard behavior for a reason. Normally when
1226 * displaying multipart/alternative you want to show the _last_ usable
1227 * format in the message. Here we show the _first_ one, because it's
1228 * usually text/plain. Since this set of functions is designed for text
1229 * output to non-MIME-aware clients, this is the desired behavior.
1232 void fixed_output_pre(char *name, char *filename, char *partnum, char *disp,
1233 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1234 char *cbid, void *cbuserdata)
1238 ma = (struct ma_info *)cbuserdata;
1239 syslog(LOG_DEBUG, "msgbase: fixed_output_pre() type=<%s>", cbtype);
1240 if (!strcasecmp(cbtype, "multipart/alternative")) {
1244 if (!strcasecmp(cbtype, "message/rfc822")) {
1251 * Post callback function for multipart/alternative
1253 void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
1254 void *content, char *cbtype, char *cbcharset, size_t length,
1255 char *encoding, char *cbid, void *cbuserdata)
1259 ma = (struct ma_info *)cbuserdata;
1260 syslog(LOG_DEBUG, "msgbase: fixed_output_post() type=<%s>", cbtype);
1261 if (!strcasecmp(cbtype, "multipart/alternative")) {
1265 if (!strcasecmp(cbtype, "message/rfc822")) {
1272 * Inline callback function for mime parser that wants to display text
1274 void fixed_output(char *name, char *filename, char *partnum, char *disp,
1275 void *content, char *cbtype, char *cbcharset, size_t length,
1276 char *encoding, char *cbid, void *cbuserdata)
1283 ma = (struct ma_info *)cbuserdata;
1286 "msgbase: fixed_output() part %s: %s (%s) (%ld bytes)",
1287 partnum, filename, cbtype, (long)length
1291 * If we're in the middle of a multipart/alternative scope and
1292 * we've already printed another section, skip this one.
1294 if ( (ma->is_ma) && (ma->did_print) ) {
1295 syslog(LOG_DEBUG, "msgbase: skipping part %s (%s)", partnum, cbtype);
1300 if ( (!strcasecmp(cbtype, "text/plain"))
1301 || (IsEmptyStr(cbtype)) ) {
1304 client_write(wptr, length);
1305 if (wptr[length-1] != '\n') {
1312 if (!strcasecmp(cbtype, "text/html")) {
1313 ptr = html_to_ascii(content, length, 80);
1315 client_write(ptr, wlen);
1316 if ((wlen > 0) && (ptr[wlen-1] != '\n')) {
1323 if (ma->use_fo_hooks) {
1324 if (PerformFixedOutputHooks(cbtype, content, length)) {
1325 /* above function returns nonzero if it handled the part */
1330 if (strncasecmp(cbtype, "multipart/", 10)) {
1331 cprintf("Part %s: %s (%s) (%ld bytes)\r\n",
1332 partnum, filename, cbtype, (long)length);
1339 * The client is elegant and sophisticated and wants to be choosy about
1340 * MIME content types, so figure out which multipart/alternative part
1341 * we're going to send.
1343 * We use a system of weights. When we find a part that matches one of the
1344 * MIME types we've declared as preferential, we can store it in ma->chosen_part
1345 * and then set ma->chosen_pref to that MIME type's position in our preference
1346 * list. If we then hit another match, we only replace the first match if
1347 * the preference value is lower.
1349 void choose_preferred(char *name, char *filename, char *partnum, char *disp,
1350 void *content, char *cbtype, char *cbcharset, size_t length,
1351 char *encoding, char *cbid, void *cbuserdata)
1357 ma = (struct ma_info *)cbuserdata;
1359 for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
1360 extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
1361 if ( (!strcasecmp(buf, cbtype)) && (!ma->freeze) ) {
1362 if (i < ma->chosen_pref) {
1363 syslog(LOG_DEBUG, "msgbase: setting chosen part to <%s>", partnum);
1364 safestrncpy(ma->chosen_part, partnum, sizeof ma->chosen_part);
1365 ma->chosen_pref = i;
1373 * Now that we've chosen our preferred part, output it.
1375 void output_preferred(char *name,
1389 int add_newline = 0;
1392 char *decoded = NULL;
1393 size_t bytes_decoded;
1396 ma = (struct ma_info *)cbuserdata;
1398 /* This is not the MIME part you're looking for... */
1399 if (strcasecmp(partnum, ma->chosen_part)) return;
1401 /* If the content-type of this part is in our preferred formats
1402 * list, we can simply output it verbatim.
1404 for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
1405 extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
1406 if (!strcasecmp(buf, cbtype)) {
1407 /* Yeah! Go! W00t!! */
1408 if (ma->dont_decode == 0)
1409 rc = mime_decode_now (content,
1415 break; /* Give us the chance, maybe theres another one. */
1417 if (rc == 0) text_content = (char *)content;
1419 text_content = decoded;
1420 length = bytes_decoded;
1423 if (text_content[length-1] != '\n') {
1426 cprintf("Content-type: %s", cbtype);
1427 if (!IsEmptyStr(cbcharset)) {
1428 cprintf("; charset=%s", cbcharset);
1430 cprintf("\nContent-length: %d\n",
1431 (int)(length + add_newline) );
1432 if (!IsEmptyStr(encoding)) {
1433 cprintf("Content-transfer-encoding: %s\n", encoding);
1436 cprintf("Content-transfer-encoding: 7bit\n");
1438 cprintf("X-Citadel-MSG4-Partnum: %s\n", partnum);
1440 if (client_write(text_content, length) == -1)
1442 syslog(LOG_ERR, "msgbase: output_preferred() aborting due to write failure");
1445 if (add_newline) cprintf("\n");
1446 if (decoded != NULL) free(decoded);
1451 /* No translations required or possible: output as text/plain */
1452 cprintf("Content-type: text/plain\n\n");
1454 if (ma->dont_decode == 0)
1455 rc = mime_decode_now (content,
1461 return; /* Give us the chance, maybe theres another one. */
1463 if (rc == 0) text_content = (char *)content;
1465 text_content = decoded;
1466 length = bytes_decoded;
1469 fixed_output(name, filename, partnum, disp, text_content, cbtype, cbcharset,
1470 length, encoding, cbid, cbuserdata);
1471 if (decoded != NULL) free(decoded);
1476 char desired_section[64];
1483 * Callback function for
1485 void extract_encapsulated_message(char *name, char *filename, char *partnum, char *disp,
1486 void *content, char *cbtype, char *cbcharset, size_t length,
1487 char *encoding, char *cbid, void *cbuserdata)
1489 struct encapmsg *encap;
1491 encap = (struct encapmsg *)cbuserdata;
1493 /* Only proceed if this is the desired section... */
1494 if (!strcasecmp(encap->desired_section, partnum)) {
1495 encap->msglen = length;
1496 encap->msg = malloc(length + 2);
1497 memcpy(encap->msg, content, length);
1504 * Determine whether the specified message exists in the cached_msglist
1505 * (This is a security check)
1507 int check_cached_msglist(long msgnum) {
1509 /* cases in which we skip the check */
1510 if (!CC) return om_ok; /* not a session */
1511 if (CC->client_socket <= 0) return om_ok; /* not a client session */
1512 if (CC->cached_msglist == NULL) return om_access_denied; /* no msglist fetched */
1513 if (CC->cached_num_msgs == 0) return om_access_denied; /* nothing to check */
1516 /* Do a binary search within the cached_msglist for the requested msgnum */
1518 int max = (CC->cached_num_msgs - 1);
1520 while (max >= min) {
1521 int middle = min + (max-min) / 2 ;
1522 if (msgnum == CC->cached_msglist[middle]) {
1525 if (msgnum > CC->cached_msglist[middle]) {
1533 return om_access_denied;
1538 * Get a message off disk. (returns om_* values found in msgbase.h)
1541 int CtdlOutputMsg(long msg_num, /* message number (local) to fetch */
1542 int mode, /* how would you like that message? */
1543 int headers_only, /* eschew the message body? */
1544 int do_proto, /* do Citadel protocol responses? */
1545 int crlf, /* Use CRLF newlines instead of LF? */
1546 char *section, /* NULL or a message/rfc822 section */
1547 int flags, /* various flags; see msgbase.h */
1552 struct CtdlMessage *TheMessage = NULL;
1553 int retcode = CIT_OK;
1554 struct encapmsg encap;
1557 syslog(LOG_DEBUG, "msgbase: CtdlOutputMsg(msgnum=%ld, mode=%d, section=%s)",
1559 (section ? section : "<>")
1562 r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
1565 if (r == om_not_logged_in) {
1566 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1569 cprintf("%d An unknown error has occurred.\n", ERROR);
1576 * Check to make sure the message is actually IN this room
1578 r = check_cached_msglist(msg_num);
1579 if (r == om_access_denied) {
1580 /* Not in the cache? We get ONE shot to check it again. */
1581 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, NULL, NULL);
1582 r = check_cached_msglist(msg_num);
1585 syslog(LOG_DEBUG, "msgbase: security check fail; message %ld is not in %s",
1586 msg_num, CC->room.QRname
1589 if (r == om_access_denied) {
1590 cprintf("%d message %ld was not found in this room\n",
1591 ERROR + HIGHER_ACCESS_REQUIRED,
1600 * Fetch the message from disk. If we're in HEADERS_FAST mode,
1601 * request that we don't even bother loading the body into memory.
1603 if (headers_only == HEADERS_FAST) {
1604 TheMessage = CtdlFetchMessage(msg_num, 0);
1607 TheMessage = CtdlFetchMessage(msg_num, 1);
1610 if (TheMessage == NULL) {
1611 if (do_proto) cprintf("%d Can't locate msg %ld on disk\n",
1612 ERROR + MESSAGE_NOT_FOUND, msg_num);
1613 return(om_no_such_msg);
1616 /* Here is the weird form of this command, to process only an
1617 * encapsulated message/rfc822 section.
1619 if (section) if (!IsEmptyStr(section)) if (strcmp(section, "0")) {
1620 memset(&encap, 0, sizeof encap);
1621 safestrncpy(encap.desired_section, section, sizeof encap.desired_section);
1622 mime_parser(CM_RANGE(TheMessage, eMesageText),
1623 *extract_encapsulated_message,
1624 NULL, NULL, (void *)&encap, 0
1627 if ((Author != NULL) && (*Author == NULL))
1630 CM_GetAsField(TheMessage, eAuthor, Author, &len);
1632 if ((Address != NULL) && (*Address == NULL))
1635 CM_GetAsField(TheMessage, erFc822Addr, Address, &len);
1637 if ((MessageID != NULL) && (*MessageID == NULL))
1640 CM_GetAsField(TheMessage, emessageId, MessageID, &len);
1642 CM_Free(TheMessage);
1646 encap.msg[encap.msglen] = 0;
1647 TheMessage = convert_internet_message(encap.msg);
1648 encap.msg = NULL; /* no free() here, TheMessage owns it now */
1650 /* Now we let it fall through to the bottom of this
1651 * function, because TheMessage now contains the
1652 * encapsulated message instead of the top-level
1653 * message. Isn't that neat?
1658 cprintf("%d msg %ld has no part %s\n",
1659 ERROR + MESSAGE_NOT_FOUND,
1663 retcode = om_no_such_msg;
1668 /* Ok, output the message now */
1669 if (retcode == CIT_OK)
1670 retcode = CtdlOutputPreLoadedMsg(TheMessage, mode, headers_only, do_proto, crlf, flags);
1671 if ((Author != NULL) && (*Author == NULL))
1674 CM_GetAsField(TheMessage, eAuthor, Author, &len);
1676 if ((Address != NULL) && (*Address == NULL))
1679 CM_GetAsField(TheMessage, erFc822Addr, Address, &len);
1681 if ((MessageID != NULL) && (*MessageID == NULL))
1684 CM_GetAsField(TheMessage, emessageId, MessageID, &len);
1687 CM_Free(TheMessage);
1693 void OutputCtdlMsgHeaders(
1694 struct CtdlMessage *TheMessage,
1695 int do_proto) /* do Citadel protocol responses? */
1699 char display_name[256];
1701 /* begin header processing loop for Citadel message format */
1702 safestrncpy(display_name, "<unknown>", sizeof display_name);
1703 if (!CM_IsEmpty(TheMessage, eAuthor)) {
1704 strcpy(buf, TheMessage->cm_fields[eAuthor]);
1705 if (TheMessage->cm_anon_type == MES_ANONONLY) {
1706 safestrncpy(display_name, "****", sizeof display_name);
1708 else if (TheMessage->cm_anon_type == MES_ANONOPT) {
1709 safestrncpy(display_name, "anonymous", sizeof display_name);
1712 safestrncpy(display_name, buf, sizeof display_name);
1714 if ((is_room_aide())
1715 && ((TheMessage->cm_anon_type == MES_ANONONLY)
1716 || (TheMessage->cm_anon_type == MES_ANONOPT))) {
1717 size_t tmp = strlen(display_name);
1718 snprintf(&display_name[tmp],
1719 sizeof display_name - tmp,
1724 /* Now spew the header fields in the order we like them. */
1725 for (i=0; i< NDiskFields; ++i) {
1727 Field = FieldOrder[i];
1728 if (Field != eMesageText) {
1729 if ( (!CM_IsEmpty(TheMessage, Field))
1730 && (msgkeys[Field] != NULL) ) {
1731 if ((Field == eenVelopeTo) ||
1732 (Field == eRecipient) ||
1733 (Field == eCarbonCopY)) {
1734 sanitize_truncated_recipient(TheMessage->cm_fields[Field]);
1736 if (Field == eAuthor) {
1737 if (do_proto) cprintf("%s=%s\n",
1741 /* Masquerade display name if needed */
1744 cprintf("%s=%s\n", msgkeys[Field], TheMessage->cm_fields[Field]);
1753 void OutputRFC822MsgHeaders(
1754 struct CtdlMessage *TheMessage,
1755 int flags, /* should the message be exported clean */
1756 const char *nl, int nlen,
1757 char *mid, long sizeof_mid,
1758 char *suser, long sizeof_suser,
1759 char *luser, long sizeof_luser,
1760 char *fuser, long sizeof_fuser,
1761 char *snode, long sizeof_snode)
1763 char datestamp[100];
1764 int subject_found = 0;
1771 for (i = 0; i < NDiskFields; ++i) {
1772 if (TheMessage->cm_fields[FieldOrder[i]]) {
1773 mptr = mpptr = TheMessage->cm_fields[FieldOrder[i]];
1774 switch (FieldOrder[i]) {
1776 safestrncpy(luser, mptr, sizeof_luser);
1777 safestrncpy(suser, mptr, sizeof_suser);
1780 if ((flags & QP_EADDR) != 0) {
1781 mptr = qp_encode_email_addrs(mptr);
1783 sanitize_truncated_recipient(mptr);
1784 cprintf("CC: %s%s", mptr, nl);
1787 cprintf("Return-Path: %s%s", mptr, nl);
1790 cprintf("List-ID: %s%s", mptr, nl);
1793 if ((flags & QP_EADDR) != 0)
1794 mptr = qp_encode_email_addrs(mptr);
1796 while ((*hptr != '\0') && isspace(*hptr))
1798 if (!IsEmptyStr(hptr))
1799 cprintf("Envelope-To: %s%s", hptr, nl);
1802 cprintf("Subject: %s%s", mptr, nl);
1806 safestrncpy(mid, mptr, sizeof_mid);
1809 safestrncpy(fuser, mptr, sizeof_fuser);
1811 if (haschar(mptr, '@') == 0) {
1812 sanitize_truncated_recipient(mptr);
1813 cprintf("To: %s@%s", mptr, CtdlGetConfigStr("c_fqdn"));
1817 if ((flags & QP_EADDR) != 0) {
1818 mptr = qp_encode_email_addrs(mptr);
1820 sanitize_truncated_recipient(mptr);
1821 cprintf("To: %s", mptr);
1826 datestring(datestamp, sizeof datestamp, atol(mptr), DATESTRING_RFC822);
1827 cprintf("Date: %s%s", datestamp, nl);
1830 cprintf("References: ");
1831 k = num_tokens(mptr, '|');
1832 for (j=0; j<k; ++j) {
1833 extract_token(buf, mptr, j, '|', sizeof buf);
1834 cprintf("<%s>", buf);
1845 while ((*hptr != '\0') && isspace(*hptr))
1847 if (!IsEmptyStr(hptr))
1848 cprintf("Reply-To: %s%s", mptr, nl);
1861 /* these don't map to mime message headers. */
1864 if (mptr != mpptr) {
1869 if (subject_found == 0) {
1870 cprintf("Subject: (no subject)%s", nl);
1875 void Dump_RFC822HeadersBody(
1876 struct CtdlMessage *TheMessage,
1877 int headers_only, /* eschew the message body? */
1878 int flags, /* should the bessage be exported clean? */
1879 const char *nl, int nlen)
1881 cit_uint8_t prev_ch;
1883 const char *StartOfText = StrBufNOTNULL;
1886 int nllen = strlen(nl);
1890 mptr = TheMessage->cm_fields[eMesageText];
1893 while (*mptr != '\0') {
1894 if (*mptr == '\r') {
1901 eoh = (*(mptr+1) == '\r') && (*(mptr+2) == '\n');
1903 eoh = *(mptr+1) == '\n';
1907 StartOfText = strchr(StartOfText, '\n');
1908 StartOfText = strchr(StartOfText, '\n');
1911 if (((headers_only == HEADERS_NONE) && (mptr >= StartOfText)) ||
1912 ((headers_only == HEADERS_ONLY) && (mptr < StartOfText)) ||
1913 ((headers_only != HEADERS_NONE) &&
1914 (headers_only != HEADERS_ONLY))
1916 if (*mptr == '\n') {
1917 memcpy(&outbuf[outlen], nl, nllen);
1919 outbuf[outlen] = '\0';
1922 outbuf[outlen++] = *mptr;
1926 if (flags & ESC_DOT) {
1927 if ((prev_ch == '\n') && (*mptr == '.') && ((*(mptr+1) == '\r') || (*(mptr+1) == '\n'))) {
1928 outbuf[outlen++] = '.';
1933 if (outlen > 1000) {
1934 if (client_write(outbuf, outlen) == -1) {
1935 syslog(LOG_ERR, "msgbase: Dump_RFC822HeadersBody() aborting due to write failure");
1938 lfSent = (outbuf[outlen - 1] == '\n');
1943 client_write(outbuf, outlen);
1944 lfSent = (outbuf[outlen - 1] == '\n');
1947 client_write(nl, nlen);
1951 /* If the format type on disk is 1 (fixed-format), then we want
1952 * everything to be output completely literally ... regardless of
1953 * what message transfer format is in use.
1955 void DumpFormatFixed(
1956 struct CtdlMessage *TheMessage,
1957 int mode, /* how would you like that message? */
1958 const char *nl, int nllen)
1966 mptr = TheMessage->cm_fields[eMesageText];
1968 if (mode == MT_MIME) {
1969 cprintf("Content-type: text/plain\n\n");
1973 while (ch = *mptr++, ch > 0) {
1977 if ((buflen > 250) && (!xlline)){
1981 while ((buflen > 0) &&
1982 (!isspace(buf[buflen])))
1988 mptr -= tbuflen - buflen;
1994 /* if we reach the outer bounds of our buffer, abort without respect for what we purge. */
1995 if (xlline && ((isspace(ch)) || (buflen > SIZ - nllen - 2))) {
2000 memcpy (&buf[buflen], nl, nllen);
2004 if (client_write(buf, buflen) == -1) {
2005 syslog(LOG_ERR, "msgbase: DumpFormatFixed() aborting due to write failure");
2017 if (!IsEmptyStr(buf)) {
2018 cprintf("%s%s", buf, nl);
2024 * Get a message off disk. (returns om_* values found in msgbase.h)
2026 int CtdlOutputPreLoadedMsg(
2027 struct CtdlMessage *TheMessage,
2028 int mode, /* how would you like that message? */
2029 int headers_only, /* eschew the message body? */
2030 int do_proto, /* do Citadel protocol responses? */
2031 int crlf, /* Use CRLF newlines instead of LF? */
2032 int flags /* should the bessage be exported clean? */
2035 const char *nl; /* newline string */
2039 /* Buffers needed for RFC822 translation. These are all filled
2040 * using functions that are bounds-checked, and therefore we can
2041 * make them substantially smaller than SIZ.
2049 syslog(LOG_DEBUG, "msgbase: CtdlOutputPreLoadedMsg(TheMessage=%s, %d, %d, %d, %d",
2050 ((TheMessage == NULL) ? "NULL" : "not null"),
2051 mode, headers_only, do_proto, crlf
2054 strcpy(mid, "unknown");
2055 nl = (crlf ? "\r\n" : "\n");
2056 nlen = crlf ? 2 : 1;
2058 if (!CM_IsValidMsg(TheMessage)) {
2059 syslog(LOG_ERR, "msgbase: error; invalid preloaded message for output");
2060 return(om_no_such_msg);
2063 /* Suppress envelope recipients if required to avoid disclosing BCC addresses.
2064 * Pad it with spaces in order to avoid changing the RFC822 length of the message.
2066 if ( (flags & SUPPRESS_ENV_TO) && (!CM_IsEmpty(TheMessage, eenVelopeTo)) ) {
2067 memset(TheMessage->cm_fields[eenVelopeTo], ' ', TheMessage->cm_lengths[eenVelopeTo]);
2070 /* Are we downloading a MIME component? */
2071 if (mode == MT_DOWNLOAD) {
2072 if (TheMessage->cm_format_type != FMT_RFC822) {
2074 cprintf("%d This is not a MIME message.\n",
2075 ERROR + ILLEGAL_VALUE);
2076 } else if (CC->download_fp != NULL) {
2077 if (do_proto) cprintf(
2078 "%d You already have a download open.\n",
2079 ERROR + RESOURCE_BUSY);
2081 /* Parse the message text component */
2082 mime_parser(CM_RANGE(TheMessage, eMesageText),
2083 *mime_download, NULL, NULL, NULL, 0);
2084 /* If there's no file open by this time, the requested
2085 * section wasn't found, so print an error
2087 if (CC->download_fp == NULL) {
2088 if (do_proto) cprintf(
2089 "%d Section %s not found.\n",
2090 ERROR + FILE_NOT_FOUND,
2091 CC->download_desired_section);
2094 return((CC->download_fp != NULL) ? om_ok : om_mime_error);
2097 /* MT_SPEW_SECTION is like MT_DOWNLOAD except it outputs the whole MIME part
2098 * in a single server operation instead of opening a download file.
2100 if (mode == MT_SPEW_SECTION) {
2101 if (TheMessage->cm_format_type != FMT_RFC822) {
2103 cprintf("%d This is not a MIME message.\n",
2104 ERROR + ILLEGAL_VALUE);
2106 /* Parse the message text component */
2109 mime_parser(CM_RANGE(TheMessage, eMesageText),
2110 *mime_spew_section, NULL, NULL, (void *)&found_it, 0);
2111 /* If section wasn't found, print an error
2114 if (do_proto) cprintf(
2115 "%d Section %s not found.\n",
2116 ERROR + FILE_NOT_FOUND,
2117 CC->download_desired_section);
2120 return((CC->download_fp != NULL) ? om_ok : om_mime_error);
2123 /* now for the user-mode message reading loops */
2124 if (do_proto) cprintf("%d msg:\n", LISTING_FOLLOWS);
2126 /* Does the caller want to skip the headers? */
2127 if (headers_only == HEADERS_NONE) goto START_TEXT;
2129 /* Tell the client which format type we're using. */
2130 if ( (mode == MT_CITADEL) && (do_proto) ) {
2131 cprintf("type=%d\n", TheMessage->cm_format_type);
2134 /* nhdr=yes means that we're only displaying headers, no body */
2135 if ( (TheMessage->cm_anon_type == MES_ANONONLY)
2136 && ((mode == MT_CITADEL) || (mode == MT_MIME))
2139 cprintf("nhdr=yes\n");
2142 if ((mode == MT_CITADEL) || (mode == MT_MIME))
2143 OutputCtdlMsgHeaders(TheMessage, do_proto);
2146 /* begin header processing loop for RFC822 transfer format */
2151 if (mode == MT_RFC822)
2152 OutputRFC822MsgHeaders(
2157 suser, sizeof(suser),
2158 luser, sizeof(luser),
2159 fuser, sizeof(fuser),
2160 snode, sizeof(snode)
2164 for (i=0; !IsEmptyStr(&suser[i]); ++i) {
2165 suser[i] = tolower(suser[i]);
2166 if (!isalnum(suser[i])) suser[i]='_';
2169 if (mode == MT_RFC822) {
2170 /* Construct a fun message id */
2171 cprintf("Message-ID: <%s", mid);
2172 if (strchr(mid, '@')==NULL) {
2173 cprintf("@%s", snode);
2177 if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONONLY)) {
2178 cprintf("From: \"----\" <x@x.org>%s", nl);
2180 else if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONOPT)) {
2181 cprintf("From: \"anonymous\" <x@x.org>%s", nl);
2183 else if (!IsEmptyStr(fuser)) {
2184 cprintf("From: \"%s\" <%s>%s", luser, fuser, nl);
2187 cprintf("From: \"%s\" <%s@%s>%s", luser, suser, snode, nl);
2190 /* Blank line signifying RFC822 end-of-headers */
2191 if (TheMessage->cm_format_type != FMT_RFC822) {
2196 /* end header processing loop ... at this point, we're in the text */
2198 if (headers_only == HEADERS_FAST) goto DONE;
2200 /* Tell the client about the MIME parts in this message */
2201 if (TheMessage->cm_format_type == FMT_RFC822) {
2202 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2203 memset(&ma, 0, sizeof(struct ma_info));
2204 mime_parser(CM_RANGE(TheMessage, eMesageText),
2205 (do_proto ? *list_this_part : NULL),
2206 (do_proto ? *list_this_pref : NULL),
2207 (do_proto ? *list_this_suff : NULL),
2210 else if (mode == MT_RFC822) { /* unparsed RFC822 dump */
2211 Dump_RFC822HeadersBody(
2220 if (headers_only == HEADERS_ONLY) {
2224 /* signify start of msg text */
2225 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2226 if (do_proto) cprintf("text\n");
2229 if (TheMessage->cm_format_type == FMT_FIXED)
2232 mode, /* how would you like that message? */
2235 /* If the message on disk is format 0 (Citadel vari-format), we
2236 * output using the formatter at 80 columns. This is the final output
2237 * form if the transfer format is RFC822, but if the transfer format
2238 * is Citadel proprietary, it'll still work, because the indentation
2239 * for new paragraphs is correct and the client will reformat the
2240 * message to the reader's screen width.
2242 if (TheMessage->cm_format_type == FMT_CITADEL) {
2243 if (mode == MT_MIME) {
2244 cprintf("Content-type: text/x-citadel-variformat\n\n");
2246 memfmout(TheMessage->cm_fields[eMesageText], nl);
2249 /* If the message on disk is format 4 (MIME), we've gotta hand it
2250 * off to the MIME parser. The client has already been told that
2251 * this message is format 1 (fixed format), so the callback function
2252 * we use will display those parts as-is.
2254 if (TheMessage->cm_format_type == FMT_RFC822) {
2255 memset(&ma, 0, sizeof(struct ma_info));
2257 if (mode == MT_MIME) {
2258 ma.use_fo_hooks = 0;
2259 strcpy(ma.chosen_part, "1");
2260 ma.chosen_pref = 9999;
2261 ma.dont_decode = CC->msg4_dont_decode;
2262 mime_parser(CM_RANGE(TheMessage, eMesageText),
2263 *choose_preferred, *fixed_output_pre,
2264 *fixed_output_post, (void *)&ma, 1);
2265 mime_parser(CM_RANGE(TheMessage, eMesageText),
2266 *output_preferred, NULL, NULL, (void *)&ma, 1);
2269 ma.use_fo_hooks = 1;
2270 mime_parser(CM_RANGE(TheMessage, eMesageText),
2271 *fixed_output, *fixed_output_pre,
2272 *fixed_output_post, (void *)&ma, 0);
2277 DONE: /* now we're done */
2278 if (do_proto) cprintf("000\n");
2283 * Save one or more message pointers into a specified room
2284 * (Returns 0 for success, nonzero for failure)
2285 * roomname may be NULL to use the current room
2287 * Note that the 'supplied_msg' field may be set to NULL, in which case
2288 * the message will be fetched from disk, by number, if we need to perform
2289 * replication checks. This adds an additional database read, so if the
2290 * caller already has the message in memory then it should be supplied. (Obviously
2291 * this mode of operation only works if we're saving a single message.)
2293 int CtdlSaveMsgPointersInRoom(char *roomname, long newmsgidlist[], int num_newmsgs,
2294 int do_repl_check, struct CtdlMessage *supplied_msg, int suppress_refcount_adj
2297 char hold_rm[ROOMNAMELEN];
2298 struct cdbdata *cdbfr;
2301 long highest_msg = 0L;
2304 struct CtdlMessage *msg = NULL;
2306 long *msgs_to_be_merged = NULL;
2307 int num_msgs_to_be_merged = 0;
2310 "msgbase: CtdlSaveMsgPointersInRoom(room=%s, num_msgs=%d, repl=%d, suppress_rca=%d)",
2311 roomname, num_newmsgs, do_repl_check, suppress_refcount_adj
2314 strcpy(hold_rm, CC->room.QRname);
2317 if (newmsgidlist == NULL) return(ERROR + INTERNAL_ERROR);
2318 if (num_newmsgs < 1) return(ERROR + INTERNAL_ERROR);
2319 if (num_newmsgs > 1) supplied_msg = NULL;
2321 /* Now the regular stuff */
2322 if (CtdlGetRoomLock(&CC->room,
2323 ((roomname != NULL) ? roomname : CC->room.QRname) )
2325 syslog(LOG_ERR, "msgbase: no such room <%s>", roomname);
2326 return(ERROR + ROOM_NOT_FOUND);
2330 msgs_to_be_merged = malloc(sizeof(long) * num_newmsgs);
2331 num_msgs_to_be_merged = 0;
2334 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
2335 if (cdbfr == NULL) {
2339 msglist = (long *) cdbfr->ptr;
2340 cdbfr->ptr = NULL; /* CtdlSaveMsgPointerInRoom() now owns this memory */
2341 num_msgs = cdbfr->len / sizeof(long);
2346 /* Create a list of msgid's which were supplied by the caller, but do
2347 * not already exist in the target room. It is absolutely taboo to
2348 * have more than one reference to the same message in a room.
2350 for (i=0; i<num_newmsgs; ++i) {
2352 if (num_msgs > 0) for (j=0; j<num_msgs; ++j) {
2353 if (msglist[j] == newmsgidlist[i]) {
2358 msgs_to_be_merged[num_msgs_to_be_merged++] = newmsgidlist[i];
2362 syslog(LOG_DEBUG, "msgbase: %d unique messages to be merged", num_msgs_to_be_merged);
2365 * Now merge the new messages
2367 msglist = realloc(msglist, (sizeof(long) * (num_msgs + num_msgs_to_be_merged)) );
2368 if (msglist == NULL) {
2369 syslog(LOG_ALERT, "msgbase: ERROR; can't realloc message list!");
2370 free(msgs_to_be_merged);
2371 return (ERROR + INTERNAL_ERROR);
2373 memcpy(&msglist[num_msgs], msgs_to_be_merged, (sizeof(long) * num_msgs_to_be_merged) );
2374 num_msgs += num_msgs_to_be_merged;
2376 /* Sort the message list, so all the msgid's are in order */
2377 num_msgs = sort_msglist(msglist, num_msgs);
2379 /* Determine the highest message number */
2380 highest_msg = msglist[num_msgs - 1];
2382 /* Write it back to disk. */
2383 cdb_store(CDB_MSGLISTS, &CC->room.QRnumber, (int)sizeof(long),
2384 msglist, (int)(num_msgs * sizeof(long)));
2386 /* Free up the memory we used. */
2389 /* Update the highest-message pointer and unlock the room. */
2390 CC->room.QRhighest = highest_msg;
2391 CtdlPutRoomLock(&CC->room);
2393 /* Perform replication checks if necessary */
2394 if ( (DoesThisRoomNeedEuidIndexing(&CC->room)) && (do_repl_check) ) {
2395 syslog(LOG_DEBUG, "msgbase: CtdlSaveMsgPointerInRoom() doing repl checks");
2397 for (i=0; i<num_msgs_to_be_merged; ++i) {
2398 msgid = msgs_to_be_merged[i];
2400 if (supplied_msg != NULL) {
2404 msg = CtdlFetchMessage(msgid, 0);
2408 ReplicationChecks(msg);
2410 /* If the message has an Exclusive ID, index that... */
2411 if (!CM_IsEmpty(msg, eExclusiveID)) {
2412 index_message_by_euid(msg->cm_fields[eExclusiveID], &CC->room, msgid);
2415 /* Free up the memory we may have allocated */
2416 if (msg != supplied_msg) {
2425 syslog(LOG_DEBUG, "msgbase: CtdlSaveMsgPointerInRoom() skips repl checks");
2428 /* Submit this room for processing by hooks */
2429 int total_roomhook_errors = PerformRoomHooks(&CC->room);
2430 if (total_roomhook_errors) {
2431 syslog(LOG_WARNING, "msgbase: room hooks returned %d errors", total_roomhook_errors);
2434 /* Go back to the room we were in before we wandered here... */
2435 CtdlGetRoom(&CC->room, hold_rm);
2437 /* Bump the reference count for all messages which were merged */
2438 if (!suppress_refcount_adj) {
2439 AdjRefCountList(msgs_to_be_merged, num_msgs_to_be_merged, +1);
2442 /* Free up memory... */
2443 if (msgs_to_be_merged != NULL) {
2444 free(msgs_to_be_merged);
2447 /* Return success. */
2453 * This is the same as CtdlSaveMsgPointersInRoom() but it only accepts
2456 int CtdlSaveMsgPointerInRoom(char *roomname, long msgid,
2457 int do_repl_check, struct CtdlMessage *supplied_msg)
2459 return CtdlSaveMsgPointersInRoom(roomname, &msgid, 1, do_repl_check, supplied_msg, 0);
2464 * Message base operation to save a new message to the message store
2465 * (returns new message number)
2467 * This is the back end for CtdlSubmitMsg() and should not be directly
2468 * called by server-side modules.
2471 long CtdlSaveThisMessage(struct CtdlMessage *msg, long msgid, int Reply) {
2479 * If the message is big, set its body aside for storage elsewhere
2480 * and we hide the message body from the serializer
2482 if (!CM_IsEmpty(msg, eMesageText) && msg->cm_lengths[eMesageText] > BIGMSG) {
2484 holdM = msg->cm_fields[eMesageText];
2485 msg->cm_fields[eMesageText] = NULL;
2486 holdMLen = msg->cm_lengths[eMesageText];
2487 msg->cm_lengths[eMesageText] = 0;
2490 /* Serialize our data structure for storage in the database */
2491 CtdlSerializeMessage(&smr, msg);
2494 /* put the message body back into the message */
2495 msg->cm_fields[eMesageText] = holdM;
2496 msg->cm_lengths[eMesageText] = holdMLen;
2501 cprintf("%d Unable to serialize message\n",
2502 ERROR + INTERNAL_ERROR);
2505 syslog(LOG_ERR, "msgbase: CtdlSaveMessage() unable to serialize message");
2511 /* Write our little bundle of joy into the message base */
2512 retval = cdb_store(CDB_MSGMAIN, &msgid, (int)sizeof(long), smr.ser, smr.len);
2514 syslog(LOG_ERR, "msgbase: can't store message %ld: %ld", msgid, retval);
2518 retval = cdb_store(CDB_BIGMSGS,
2525 syslog(LOG_ERR, "msgbase: failed to store message body for msgid %ld: %ld", msgid, retval);
2530 /* Free the memory we used for the serialized message */
2537 long send_message(struct CtdlMessage *msg) {
2543 /* Get a new message number */
2544 newmsgid = get_new_message_number();
2546 /* Generate an ID if we don't have one already */
2547 if (CM_IsEmpty(msg, emessageId)) {
2548 msgidbuflen = snprintf(msgidbuf, sizeof msgidbuf, "%08lX-%08lX@%s",
2549 (long unsigned int) time(NULL),
2550 (long unsigned int) newmsgid,
2551 CtdlGetConfigStr("c_fqdn")
2554 CM_SetField(msg, emessageId, msgidbuf, msgidbuflen);
2557 retval = CtdlSaveThisMessage(msg, newmsgid, 1);
2563 /* Return the *local* message ID to the caller
2564 * (even if we're storing an incoming network message)
2571 * Serialize a struct CtdlMessage into the format used on disk and network.
2573 * This function loads up a "struct ser_ret" (defined in server.h) which
2574 * contains the length of the serialized message and a pointer to the
2575 * serialized message in memory. THE LATTER MUST BE FREED BY THE CALLER.
2577 void CtdlSerializeMessage(struct ser_ret *ret, /* return values */
2578 struct CtdlMessage *msg) /* unserialized msg */
2584 * Check for valid message format
2586 if (CM_IsValidMsg(msg) == 0) {
2587 syslog(LOG_ERR, "msgbase: CtdlSerializeMessage() aborting due to invalid message");
2594 for (i=0; i < NDiskFields; ++i)
2595 if (msg->cm_fields[FieldOrder[i]] != NULL)
2596 ret->len += msg->cm_lengths[FieldOrder[i]] + 2;
2598 ret->ser = malloc(ret->len);
2599 if (ret->ser == NULL) {
2600 syslog(LOG_ERR, "msgbase: CtdlSerializeMessage() malloc(%ld) failed: %m", (long)ret->len);
2607 ret->ser[1] = msg->cm_anon_type;
2608 ret->ser[2] = msg->cm_format_type;
2611 for (i=0; i < NDiskFields; ++i) {
2612 if (msg->cm_fields[FieldOrder[i]] != NULL) {
2613 ret->ser[wlen++] = (char)FieldOrder[i];
2615 memcpy(&ret->ser[wlen],
2616 msg->cm_fields[FieldOrder[i]],
2617 msg->cm_lengths[FieldOrder[i]] + 1);
2619 wlen = wlen + msg->cm_lengths[FieldOrder[i]] + 1;
2623 if (ret->len != wlen) {
2624 syslog(LOG_ERR, "msgbase: ERROR; len=%ld wlen=%ld", (long)ret->len, (long)wlen);
2632 * Check to see if any messages already exist in the current room which
2633 * carry the same Exclusive ID as this one. If any are found, delete them.
2635 void ReplicationChecks(struct CtdlMessage *msg) {
2636 long old_msgnum = (-1L);
2638 if (DoesThisRoomNeedEuidIndexing(&CC->room) == 0) return;
2640 syslog(LOG_DEBUG, "msgbase: performing replication checks in <%s>", CC->room.QRname);
2642 /* No exclusive id? Don't do anything. */
2643 if (msg == NULL) return;
2644 if (CM_IsEmpty(msg, eExclusiveID)) return;
2646 /*syslog(LOG_DEBUG, "msgbase: exclusive ID: <%s> for room <%s>",
2647 msg->cm_fields[eExclusiveID], CC->room.QRname);*/
2649 old_msgnum = CtdlLocateMessageByEuid(msg->cm_fields[eExclusiveID], &CC->room);
2650 if (old_msgnum > 0L) {
2651 syslog(LOG_DEBUG, "msgbase: ReplicationChecks() replacing message %ld", old_msgnum);
2652 CtdlDeleteMessages(CC->room.QRname, &old_msgnum, 1, "");
2658 * Save a message to disk and submit it into the delivery system.
2660 long CtdlSubmitMsg(struct CtdlMessage *msg, /* message to save */
2661 recptypes *recps, /* recipients (if mail) */
2662 const char *force /* force a particular room? */
2664 char hold_rm[ROOMNAMELEN];
2665 char actual_rm[ROOMNAMELEN];
2666 char force_room[ROOMNAMELEN];
2667 char content_type[SIZ]; /* We have to learn this */
2668 char recipient[SIZ];
2669 char bounce_to[1024];
2672 const char *mptr = NULL;
2673 struct ctdluser userbuf;
2675 struct MetaData smi;
2676 char *collected_addresses = NULL;
2677 struct addresses_to_be_filed *aptr = NULL;
2678 StrBuf *saved_rfc822_version = NULL;
2679 int qualified_for_journaling = 0;
2681 syslog(LOG_DEBUG, "msgbase: CtdlSubmitMsg() called");
2682 if (CM_IsValidMsg(msg) == 0) return(-1); /* self check */
2684 /* If this message has no timestamp, we take the liberty of
2685 * giving it one, right now.
2687 if (CM_IsEmpty(msg, eTimestamp)) {
2688 CM_SetFieldLONG(msg, eTimestamp, time(NULL));
2691 /* If this message has no path, we generate one.
2693 if (CM_IsEmpty(msg, eMessagePath)) {
2694 if (!CM_IsEmpty(msg, eAuthor)) {
2695 CM_CopyField(msg, eMessagePath, eAuthor);
2696 for (a=0; !IsEmptyStr(&msg->cm_fields[eMessagePath][a]); ++a) {
2697 if (isspace(msg->cm_fields[eMessagePath][a])) {
2698 msg->cm_fields[eMessagePath][a] = ' ';
2703 CM_SetField(msg, eMessagePath, HKEY("unknown"));
2707 if (force == NULL) {
2708 force_room[0] = '\0';
2711 strcpy(force_room, force);
2714 /* Learn about what's inside, because it's what's inside that counts */
2715 if (CM_IsEmpty(msg, eMesageText)) {
2716 syslog(LOG_ERR, "msgbase: ERROR; attempt to save message with NULL body");
2720 switch (msg->cm_format_type) {
2722 strcpy(content_type, "text/x-citadel-variformat");
2725 strcpy(content_type, "text/plain");
2728 strcpy(content_type, "text/plain");
2729 mptr = bmstrcasestr(msg->cm_fields[eMesageText], "Content-type:");
2732 safestrncpy(content_type, &mptr[13], sizeof content_type);
2733 striplt(content_type);
2734 aptr = content_type;
2735 while (!IsEmptyStr(aptr)) {
2747 /* Goto the correct room */
2748 room = (recps) ? CC->room.QRname : SENTITEMS;
2749 syslog(LOG_DEBUG, "msgbase: selected room %s", room);
2750 strcpy(hold_rm, CC->room.QRname);
2751 strcpy(actual_rm, CC->room.QRname);
2752 if (recps != NULL) {
2753 strcpy(actual_rm, SENTITEMS);
2756 /* If the user is a twit, move to the twit room for posting */
2758 if (CC->user.axlevel == AxProbU) {
2759 strcpy(hold_rm, actual_rm);
2760 strcpy(actual_rm, CtdlGetConfigStr("c_twitroom"));
2761 syslog(LOG_DEBUG, "msgbase: diverting to twit room");
2765 /* ...or if this message is destined for Aide> then go there. */
2766 if (!IsEmptyStr(force_room)) {
2767 strcpy(actual_rm, force_room);
2770 syslog(LOG_DEBUG, "msgbase: final selection: %s (%s)", actual_rm, room);
2771 if (strcasecmp(actual_rm, CC->room.QRname)) {
2772 /* CtdlGetRoom(&CC->room, actual_rm); */
2773 CtdlUserGoto(actual_rm, 0, 1, NULL, NULL, NULL, NULL);
2777 * If this message has no O (room) field, generate one.
2779 if (CM_IsEmpty(msg, eOriginalRoom) && !IsEmptyStr(CC->room.QRname)) {
2780 CM_SetField(msg, eOriginalRoom, CC->room.QRname, -1);
2783 /* Perform "before save" hooks (aborting if any return nonzero) */
2784 syslog(LOG_DEBUG, "msgbase: performing before-save hooks");
2785 if (PerformMessageHooks(msg, recps, EVT_BEFORESAVE) > 0) return(-3);
2788 * If this message has an Exclusive ID, and the room is replication
2789 * checking enabled, then do replication checks.
2791 if (DoesThisRoomNeedEuidIndexing(&CC->room)) {
2792 ReplicationChecks(msg);
2795 /* Save it to disk */
2796 syslog(LOG_DEBUG, "msgbase: saving to disk");
2797 newmsgid = send_message(msg);
2798 if (newmsgid <= 0L) return(-5);
2800 /* Write a supplemental message info record. This doesn't have to
2801 * be a critical section because nobody else knows about this message
2804 syslog(LOG_DEBUG, "msgbase: creating metadata record");
2805 memset(&smi, 0, sizeof(struct MetaData));
2806 smi.meta_msgnum = newmsgid;
2807 smi.meta_refcount = 0;
2808 safestrncpy(smi.meta_content_type, content_type, sizeof smi.meta_content_type);
2811 * Measure how big this message will be when rendered as RFC822.
2812 * We do this for two reasons:
2813 * 1. We need the RFC822 length for the new metadata record, so the
2814 * POP and IMAP services don't have to calculate message lengths
2815 * while the user is waiting (multiplied by potentially hundreds
2816 * or thousands of messages).
2817 * 2. If journaling is enabled, we will need an RFC822 version of the
2818 * message to attach to the journalized copy.
2820 if (CC->redirect_buffer != NULL) {
2821 syslog(LOG_ALERT, "msgbase: CC->redirect_buffer is not NULL during message submission!");
2824 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
2825 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, QP_EADDR);
2826 smi.meta_rfc822_length = StrLength(CC->redirect_buffer);
2827 saved_rfc822_version = CC->redirect_buffer;
2828 CC->redirect_buffer = NULL;
2832 /* Now figure out where to store the pointers */
2833 syslog(LOG_DEBUG, "msgbase: storing pointers");
2835 /* If this is being done by the networker delivering a private
2836 * message, we want to BYPASS saving the sender's copy (because there
2837 * is no local sender; it would otherwise go to the Trashcan).
2839 if ((!CC->internal_pgm) || (recps == NULL)) {
2840 if (CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 1, msg) != 0) {
2841 syslog(LOG_ERR, "msgbase: ERROR saving message pointer!");
2842 CtdlSaveMsgPointerInRoom(CtdlGetConfigStr("c_aideroom"), newmsgid, 0, msg);
2846 /* For internet mail, drop a copy in the outbound queue room */
2847 if ((recps != NULL) && (recps->num_internet > 0)) {
2848 CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, newmsgid, 0, msg);
2851 /* If other rooms are specified, drop them there too. */
2852 if ((recps != NULL) && (recps->num_room > 0)) {
2853 for (i=0; i<num_tokens(recps->recp_room, '|'); ++i) {
2854 extract_token(recipient, recps->recp_room, i, '|', sizeof recipient);
2855 syslog(LOG_DEBUG, "msgbase: delivering to room <%s>", recipient);
2856 CtdlSaveMsgPointerInRoom(recipient, newmsgid, 0, msg);
2860 /* Bump this user's messages posted counter. */
2861 syslog(LOG_DEBUG, "msgbase: updating user");
2862 CtdlLockGetCurrentUser();
2863 CC->user.posted = CC->user.posted + 1;
2864 CtdlPutCurrentUserLock();
2866 /* Decide where bounces need to be delivered */
2867 if ((recps != NULL) && (recps->bounce_to == NULL)) {
2868 if (CC->logged_in) {
2869 strcpy(bounce_to, CC->user.fullname);
2871 else if (!IsEmptyStr(msg->cm_fields[eAuthor])){
2872 strcpy(bounce_to, msg->cm_fields[eAuthor]);
2874 recps->bounce_to = bounce_to;
2877 CM_SetFieldLONG(msg, eVltMsgNum, newmsgid);
2879 /* If this is private, local mail, make a copy in the
2880 * recipient's mailbox and bump the reference count.
2882 if ((recps != NULL) && (recps->num_local > 0)) {
2886 pch = recps->recp_local;
2887 recps->recp_local = recipient;
2888 ntokens = num_tokens(pch, '|');
2889 for (i=0; i<ntokens; ++i) {
2890 extract_token(recipient, pch, i, '|', sizeof recipient);
2891 syslog(LOG_DEBUG, "msgbase: delivering private local mail to <%s>", recipient);
2892 if (CtdlGetUser(&userbuf, recipient) == 0) {
2893 CtdlMailboxName(actual_rm, sizeof actual_rm, &userbuf, MAILROOM);
2894 CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0, msg);
2895 CtdlBumpNewMailCounter(userbuf.usernum);
2896 PerformMessageHooks(msg, recps, EVT_AFTERUSRMBOXSAVE);
2899 syslog(LOG_DEBUG, "msgbase: no user <%s>", recipient);
2900 CtdlSaveMsgPointerInRoom(CtdlGetConfigStr("c_aideroom"), newmsgid, 0, msg);
2903 recps->recp_local = pch;
2906 /* Perform "after save" hooks */
2907 syslog(LOG_DEBUG, "msgbase: performing after-save hooks");
2909 PerformMessageHooks(msg, recps, EVT_AFTERSAVE);
2910 CM_FlushField(msg, eVltMsgNum);
2912 /* Go back to the room we started from */
2913 syslog(LOG_DEBUG, "msgbase: returning to original room %s", hold_rm);
2914 if (strcasecmp(hold_rm, CC->room.QRname)) {
2915 CtdlUserGoto(hold_rm, 0, 1, NULL, NULL, NULL, NULL);
2919 * Any addresses to harvest for someone's address book?
2921 if ( (CC->logged_in) && (recps != NULL) ) {
2922 collected_addresses = harvest_collected_addresses(msg);
2925 if (collected_addresses != NULL) {
2926 aptr = (struct addresses_to_be_filed *) malloc(sizeof(struct addresses_to_be_filed));
2927 CtdlMailboxName(actual_rm, sizeof actual_rm, &CC->user, USERCONTACTSROOM);
2928 aptr->roomname = strdup(actual_rm);
2929 aptr->collected_addresses = collected_addresses;
2930 begin_critical_section(S_ATBF);
2933 end_critical_section(S_ATBF);
2937 * Determine whether this message qualifies for journaling.
2939 if (!CM_IsEmpty(msg, eJournal)) {
2940 qualified_for_journaling = 0;
2943 if (recps == NULL) {
2944 qualified_for_journaling = CtdlGetConfigInt("c_journal_pubmsgs");
2946 else if (recps->num_local + recps->num_ignet + recps->num_internet > 0) {
2947 qualified_for_journaling = CtdlGetConfigInt("c_journal_email");
2950 qualified_for_journaling = CtdlGetConfigInt("c_journal_pubmsgs");
2955 * Do we have to perform journaling? If so, hand off the saved
2956 * RFC822 version will be handed off to the journaler for background
2957 * submit. Otherwise, we have to free the memory ourselves.
2959 if (saved_rfc822_version != NULL) {
2960 if (qualified_for_journaling) {
2961 JournalBackgroundSubmit(msg, saved_rfc822_version, recps);
2964 FreeStrBuf(&saved_rfc822_version);
2968 if ((recps != NULL) && (recps->bounce_to == bounce_to))
2969 recps->bounce_to = NULL;
2977 * Convenience function for generating small administrative messages.
2979 long quickie_message(const char *from,
2980 const char *fromaddr,
2985 const char *subject)
2987 struct CtdlMessage *msg;
2988 recptypes *recp = NULL;
2990 msg = malloc(sizeof(struct CtdlMessage));
2991 memset(msg, 0, sizeof(struct CtdlMessage));
2992 msg->cm_magic = CTDLMESSAGE_MAGIC;
2993 msg->cm_anon_type = MES_NORMAL;
2994 msg->cm_format_type = format_type;
2996 if (!IsEmptyStr(from)) {
2997 CM_SetField(msg, eAuthor, from, -1);
2999 else if (!IsEmptyStr(fromaddr)) {
3001 CM_SetField(msg, eAuthor, fromaddr, -1);
3002 pAt = strchr(msg->cm_fields[eAuthor], '@');
3004 CM_CutFieldAt(msg, eAuthor, pAt - msg->cm_fields[eAuthor]);
3008 msg->cm_fields[eAuthor] = strdup("Citadel");
3011 if (!IsEmptyStr(fromaddr)) CM_SetField(msg, erFc822Addr, fromaddr, -1);
3012 if (!IsEmptyStr(room)) CM_SetField(msg, eOriginalRoom, room, -1);
3013 if (!IsEmptyStr(to)) {
3014 CM_SetField(msg, eRecipient, to, -1);
3015 recp = validate_recipients(to, NULL, 0);
3017 if (!IsEmptyStr(subject)) {
3018 CM_SetField(msg, eMsgSubject, subject, -1);
3020 if (!IsEmptyStr(text)) {
3021 CM_SetField(msg, eMesageText, text, -1);
3024 long msgnum = CtdlSubmitMsg(msg, recp, room);
3026 if (recp != NULL) free_recipients(recp);
3032 * Back end function used by CtdlMakeMessage() and similar functions
3034 StrBuf *CtdlReadMessageBodyBuf(char *terminator, /* token signalling EOT */
3036 size_t maxlen, /* maximum message length */
3037 StrBuf *exist, /* if non-null, append to it;
3038 exist is ALWAYS freed */
3039 int crlf /* CRLF newlines instead of LF */
3047 LineBuf = NewStrBufPlain(NULL, SIZ);
3048 if (exist == NULL) {
3049 Message = NewStrBufPlain(NULL, 4 * SIZ);
3052 Message = NewStrBufDup(exist);
3055 /* Do we need to change leading ".." to "." for SMTP escaping? */
3056 if ((tlen == 1) && (*terminator == '.')) {
3060 /* read in the lines of message text one by one */
3062 if (CtdlClientGetLine(LineBuf) < 0) {
3065 if ((StrLength(LineBuf) == tlen) && (!strcmp(ChrPtr(LineBuf), terminator))) {
3068 if ( (!flushing) && (!finished) ) {
3070 StrBufAppendBufPlain(LineBuf, HKEY("\r\n"), 0);
3073 StrBufAppendBufPlain(LineBuf, HKEY("\n"), 0);
3076 /* Unescape SMTP-style input of two dots at the beginning of the line */
3077 if ((dotdot) && (StrLength(LineBuf) > 1) && (ChrPtr(LineBuf)[0] == '.')) {
3078 StrBufCutLeft(LineBuf, 1);
3080 StrBufAppendBuf(Message, LineBuf, 0);
3083 /* if we've hit the max msg length, flush the rest */
3084 if (StrLength(Message) >= maxlen) flushing = 1;
3086 } while (!finished);
3087 FreeStrBuf(&LineBuf);
3093 * Back end function used by CtdlMakeMessage() and similar functions
3095 char *CtdlReadMessageBody(char *terminator, /* token signalling EOT */
3097 size_t maxlen, /* maximum message length */
3098 StrBuf *exist, /* if non-null, append to it;
3099 exist is ALWAYS freed */
3100 int crlf /* CRLF newlines instead of LF */
3105 Message = CtdlReadMessageBodyBuf(terminator,
3111 if (Message == NULL)
3114 return SmashStrBuf(&Message);
3118 struct CtdlMessage *CtdlMakeMessage(
3119 struct ctdluser *author, /* author's user structure */
3120 char *recipient, /* NULL if it's not mail */
3121 char *recp_cc, /* NULL if it's not mail */
3122 char *room, /* room where it's going */
3123 int type, /* see MES_ types in header file */
3124 int format_type, /* variformat, plain text, MIME... */
3125 char *fake_name, /* who we're masquerading as */
3126 char *my_email, /* which of my email addresses to use (empty is ok) */
3127 char *subject, /* Subject (optional) */
3128 char *supplied_euid, /* ...or NULL if this is irrelevant */
3129 char *preformatted_text, /* ...or NULL to read text from client */
3130 char *references /* Thread references */
3132 return CtdlMakeMessageLen(
3133 author, /* author's user structure */
3134 recipient, /* NULL if it's not mail */
3135 (recipient)?strlen(recipient) : 0,
3136 recp_cc, /* NULL if it's not mail */
3137 (recp_cc)?strlen(recp_cc): 0,
3138 room, /* room where it's going */
3139 (room)?strlen(room): 0,
3140 type, /* see MES_ types in header file */
3141 format_type, /* variformat, plain text, MIME... */
3142 fake_name, /* who we're masquerading as */
3143 (fake_name)?strlen(fake_name): 0,
3144 my_email, /* which of my email addresses to use (empty is ok) */
3145 (my_email)?strlen(my_email): 0,
3146 subject, /* Subject (optional) */
3147 (subject)?strlen(subject): 0,
3148 supplied_euid, /* ...or NULL if this is irrelevant */
3149 (supplied_euid)?strlen(supplied_euid):0,
3150 preformatted_text, /* ...or NULL to read text from client */
3151 (preformatted_text)?strlen(preformatted_text) : 0,
3152 references, /* Thread references */
3153 (references)?strlen(references):0);
3159 * Build a binary message to be saved on disk.
3160 * (NOTE: if you supply 'preformatted_text', the buffer you give it
3161 * will become part of the message. This means you are no longer
3162 * responsible for managing that memory -- it will be freed along with
3163 * the rest of the fields when CM_Free() is called.)
3165 struct CtdlMessage *CtdlMakeMessageLen(
3166 struct ctdluser *author, /* author's user structure */
3167 char *recipient, /* NULL if it's not mail */
3169 char *recp_cc, /* NULL if it's not mail */
3171 char *room, /* room where it's going */
3173 int type, /* see MES_ types in header file */
3174 int format_type, /* variformat, plain text, MIME... */
3175 char *fake_name, /* who we're masquerading as */
3177 char *my_email, /* which of my email addresses to use (empty is ok) */
3179 char *subject, /* Subject (optional) */
3181 char *supplied_euid, /* ...or NULL if this is irrelevant */
3183 char *preformatted_text, /* ...or NULL to read text from client */
3185 char *references, /* Thread references */
3190 struct CtdlMessage *msg;
3192 StrBuf *FakeEncAuthor = NULL;
3194 msg = malloc(sizeof(struct CtdlMessage));
3195 memset(msg, 0, sizeof(struct CtdlMessage));
3196 msg->cm_magic = CTDLMESSAGE_MAGIC;
3197 msg->cm_anon_type = type;
3198 msg->cm_format_type = format_type;
3200 if (recipient != NULL) rcplen = striplt(recipient);
3201 if (recp_cc != NULL) cclen = striplt(recp_cc);
3203 /* Path or Return-Path */
3205 CM_SetField(msg, eMessagePath, my_email, myelen);
3207 else if (!IsEmptyStr(author->fullname)) {
3208 CM_SetField(msg, eMessagePath, author->fullname, -1);
3210 convert_spaces_to_underscores(msg->cm_fields[eMessagePath]);
3212 blen = snprintf(buf, sizeof buf, "%ld", (long)time(NULL));
3213 CM_SetField(msg, eTimestamp, buf, blen);
3216 FakeAuthor = NewStrBufPlain (fake_name, fnlen);
3219 FakeAuthor = NewStrBufPlain (author->fullname, -1);
3221 StrBufRFC2047encode(&FakeEncAuthor, FakeAuthor);
3222 CM_SetAsFieldSB(msg, eAuthor, &FakeEncAuthor);
3223 FreeStrBuf(&FakeAuthor);
3225 if (!!IsEmptyStr(CC->room.QRname)) {
3226 if (CC->room.QRflags & QR_MAILBOX) { /* room */
3227 CM_SetField(msg, eOriginalRoom, &CC->room.QRname[11], -1);
3230 CM_SetField(msg, eOriginalRoom, CC->room.QRname, -1);
3235 CM_SetField(msg, eRecipient, recipient, rcplen);
3238 CM_SetField(msg, eCarbonCopY, recp_cc, cclen);
3242 CM_SetField(msg, erFc822Addr, my_email, myelen);
3244 else if ( (author == &CC->user) && (!IsEmptyStr(CC->cs_inet_email)) ) {
3245 CM_SetField(msg, erFc822Addr, CC->cs_inet_email, -1);
3248 if (subject != NULL) {
3250 length = striplt(subject);
3256 while ((subject[i] != '\0') &&
3257 (IsAscii = isascii(subject[i]) != 0 ))
3260 CM_SetField(msg, eMsgSubject, subject, subjlen);
3261 else /* ok, we've got utf8 in the string. */
3264 rfc2047Subj = rfc2047encode(subject, length);
3265 CM_SetAsField(msg, eMsgSubject, &rfc2047Subj, strlen(rfc2047Subj));
3272 CM_SetField(msg, eExclusiveID, supplied_euid, euidlen);
3276 CM_SetField(msg, eWeferences, references, reflen);
3279 if (preformatted_text != NULL) {
3280 CM_SetField(msg, eMesageText, preformatted_text, textlen);
3284 MsgBody = CtdlReadMessageBodyBuf(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
3285 if (MsgBody != NULL) {
3286 CM_SetAsFieldSB(msg, eMesageText, &MsgBody);
3295 * API function to delete messages which match a set of criteria
3296 * (returns the actual number of messages deleted)
3298 int CtdlDeleteMessages(const char *room_name, // which room
3299 long *dmsgnums, // array of msg numbers to be deleted
3300 int num_dmsgnums, // number of msgs to be deleted, or 0 for "any"
3301 char *content_type // or "" for any. regular expressions expected.
3303 struct ctdlroom qrbuf;
3304 struct cdbdata *cdbfr;
3305 long *msglist = NULL;
3306 long *dellist = NULL;
3309 int num_deleted = 0;
3311 struct MetaData smi;
3314 int need_to_free_re = 0;
3316 if (content_type) if (!IsEmptyStr(content_type)) {
3317 regcomp(&re, content_type, 0);
3318 need_to_free_re = 1;
3320 syslog(LOG_DEBUG, "msgbase: CtdlDeleteMessages(%s, %d msgs, %s)", room_name, num_dmsgnums, content_type);
3322 /* get room record, obtaining a lock... */
3323 if (CtdlGetRoomLock(&qrbuf, room_name) != 0) {
3324 syslog(LOG_ERR, "msgbase: CtdlDeleteMessages(): Room <%s> not found", room_name);
3325 if (need_to_free_re) regfree(&re);
3326 return(0); /* room not found */
3328 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
3330 if (cdbfr != NULL) {
3331 dellist = malloc(cdbfr->len);
3332 msglist = (long *) cdbfr->ptr;
3333 cdbfr->ptr = NULL; /* CtdlDeleteMessages() now owns this memory */
3334 num_msgs = cdbfr->len / sizeof(long);
3338 int have_contenttype = (content_type != NULL) && !IsEmptyStr(content_type);
3339 int have_delmsgs = (num_dmsgnums == 0) || (dmsgnums == NULL);
3340 int have_more_del = 1;
3342 num_msgs = sort_msglist(msglist, num_msgs);
3343 if (num_dmsgnums > 1)
3344 num_dmsgnums = sort_msglist(dmsgnums, num_dmsgnums);
3347 StrBuf *dbg = NewStrBuf();
3348 for (i = 0; i < num_dmsgnums; i++)
3349 StrBufAppendPrintf(dbg, ", %ld", dmsgnums[i]);
3350 syslog(LOG_DEBUG, "msgbase: Deleting before: %s", ChrPtr(dbg));
3355 while ((i < num_msgs) && (have_more_del)) {
3358 /* Set/clear a bit for each criterion */
3360 /* 0 messages in the list or a null list means that we are
3361 * interested in deleting any messages which meet the other criteria.
3364 delete_this |= 0x01;
3367 while ((i < num_msgs) && (msglist[i] < dmsgnums[j])) i++;
3372 if (msglist[i] == dmsgnums[j]) {
3373 delete_this |= 0x01;
3376 have_more_del = (j < num_dmsgnums);
3379 if (have_contenttype) {
3380 GetMetaData(&smi, msglist[i]);
3381 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) == 0) {
3382 delete_this |= 0x02;
3385 delete_this |= 0x02;
3388 /* Delete message only if all bits are set */
3389 if (delete_this == 0x03) {
3390 dellist[num_deleted++] = msglist[i];
3397 StrBuf *dbg = NewStrBuf();
3398 for (i = 0; i < num_deleted; i++)
3399 StrBufAppendPrintf(dbg, ", %ld", dellist[i]);
3400 syslog(LOG_DEBUG, "msgbase: Deleting: %s", ChrPtr(dbg));
3404 num_msgs = sort_msglist(msglist, num_msgs);
3405 cdb_store(CDB_MSGLISTS, &qrbuf.QRnumber, (int)sizeof(long),
3406 msglist, (int)(num_msgs * sizeof(long)));
3409 qrbuf.QRhighest = msglist[num_msgs - 1];
3411 qrbuf.QRhighest = 0;
3413 CtdlPutRoomLock(&qrbuf);
3415 /* Go through the messages we pulled out of the index, and decrement
3416 * their reference counts by 1. If this is the only room the message
3417 * was in, the reference count will reach zero and the message will
3418 * automatically be deleted from the database. We do this in a
3419 * separate pass because there might be plug-in hooks getting called,
3420 * and we don't want that happening during an S_ROOMS critical
3424 for (i=0; i<num_deleted; ++i) {
3425 PerformDeleteHooks(qrbuf.QRname, dellist[i]);
3427 AdjRefCountList(dellist, num_deleted, -1);
3429 /* Now free the memory we used, and go away. */
3430 if (msglist != NULL) free(msglist);
3431 if (dellist != NULL) free(dellist);
3432 syslog(LOG_DEBUG, "msgbase: %d message(s) deleted", num_deleted);
3433 if (need_to_free_re) regfree(&re);
3434 return (num_deleted);
3439 * GetMetaData() - Get the supplementary record for a message
3441 void GetMetaData(struct MetaData *smibuf, long msgnum)
3443 struct cdbdata *cdbsmi;
3446 memset(smibuf, 0, sizeof(struct MetaData));
3447 smibuf->meta_msgnum = msgnum;
3448 smibuf->meta_refcount = 1; /* Default reference count is 1 */
3450 /* Use the negative of the message number for its supp record index */
3451 TheIndex = (0L - msgnum);
3453 cdbsmi = cdb_fetch(CDB_MSGMAIN, &TheIndex, sizeof(long));
3454 if (cdbsmi == NULL) {
3455 return; /* record not found; leave it alone */
3457 memcpy(smibuf, cdbsmi->ptr,
3458 ((cdbsmi->len > sizeof(struct MetaData)) ?
3459 sizeof(struct MetaData) : cdbsmi->len)
3467 * PutMetaData() - (re)write supplementary record for a message
3469 void PutMetaData(struct MetaData *smibuf)
3473 /* Use the negative of the message number for the metadata db index */
3474 TheIndex = (0L - smibuf->meta_msgnum);
3476 cdb_store(CDB_MSGMAIN,
3477 &TheIndex, (int)sizeof(long),
3478 smibuf, (int)sizeof(struct MetaData)
3484 * Convenience function to process a big block of AdjRefCount() operations
3486 void AdjRefCountList(long *msgnum, long nmsg, int incr)
3490 for (i = 0; i < nmsg; i++) {
3491 AdjRefCount(msgnum[i], incr);
3497 * AdjRefCount - adjust the reference count for a message. We need to delete from disk any message whose reference count reaches zero.
3499 void AdjRefCount(long msgnum, int incr)
3501 struct MetaData smi;
3504 /* This is a *tight* critical section; please keep it that way, as
3505 * it may get called while nested in other critical sections.
3506 * Complicating this any further will surely cause deadlock!
3508 begin_critical_section(S_SUPPMSGMAIN);
3509 GetMetaData(&smi, msgnum);
3510 smi.meta_refcount += incr;
3512 end_critical_section(S_SUPPMSGMAIN);
3513 syslog(LOG_DEBUG, "msgbase: AdjRefCount() msg %ld ref count delta %+d, is now %d", msgnum, incr, smi.meta_refcount);
3515 /* If the reference count is now zero, delete both the message and its metadata record.
3517 if (smi.meta_refcount == 0) {
3518 syslog(LOG_DEBUG, "msgbase: deleting message <%ld>", msgnum);
3520 /* Call delete hooks with NULL room to show it has gone altogether */
3521 PerformDeleteHooks(NULL, msgnum);
3523 /* Remove from message base */
3525 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3526 cdb_delete(CDB_BIGMSGS, &delnum, (int)sizeof(long));
3528 /* Remove metadata record */
3529 delnum = (0L - msgnum);
3530 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3536 * Write a generic object to this room
3538 * Note: this could be much more efficient. Right now we use two temporary
3539 * files, and still pull the message into memory as with all others.
3541 void CtdlWriteObject(char *req_room, /* Room to stuff it in */
3542 char *content_type, /* MIME type of this object */
3543 char *raw_message, /* Data to be written */
3544 off_t raw_length, /* Size of raw_message */
3545 struct ctdluser *is_mailbox, /* Mailbox room? */
3546 int is_binary, /* Is encoding necessary? */
3547 int is_unique, /* Del others of this type? */
3548 unsigned int flags /* Internal save flags */
3550 struct ctdlroom qrbuf;
3551 char roomname[ROOMNAMELEN];
3552 struct CtdlMessage *msg;
3553 StrBuf *encoded_message = NULL;
3555 if (is_mailbox != NULL) {
3556 CtdlMailboxName(roomname, sizeof roomname, is_mailbox, req_room);
3559 safestrncpy(roomname, req_room, sizeof(roomname));
3562 syslog(LOG_DEBUG, "msfbase: raw length is %ld", (long)raw_length);
3565 encoded_message = NewStrBufPlain(NULL, (size_t) (((raw_length * 134) / 100) + 4096 ) );
3568 encoded_message = NewStrBufPlain(NULL, (size_t)(raw_length + 4096));
3571 StrBufAppendBufPlain(encoded_message, HKEY("Content-type: "), 0);
3572 StrBufAppendBufPlain(encoded_message, content_type, -1, 0);
3573 StrBufAppendBufPlain(encoded_message, HKEY("\n"), 0);
3576 StrBufAppendBufPlain(encoded_message, HKEY("Content-transfer-encoding: base64\n\n"), 0);
3579 StrBufAppendBufPlain(encoded_message, HKEY("Content-transfer-encoding: 7bit\n\n"), 0);
3583 StrBufBase64Append(encoded_message, NULL, raw_message, raw_length, 0);
3586 StrBufAppendBufPlain(encoded_message, raw_message, raw_length, 0);
3589 syslog(LOG_DEBUG, "msgbase: allocating");
3590 msg = malloc(sizeof(struct CtdlMessage));
3591 memset(msg, 0, sizeof(struct CtdlMessage));
3592 msg->cm_magic = CTDLMESSAGE_MAGIC;
3593 msg->cm_anon_type = MES_NORMAL;
3594 msg->cm_format_type = 4;
3595 CM_SetField(msg, eAuthor, CC->user.fullname, -1);
3596 CM_SetField(msg, eOriginalRoom, req_room, -1);
3597 msg->cm_flags = flags;
3599 CM_SetAsFieldSB(msg, eMesageText, &encoded_message);
3601 /* Create the requested room if we have to. */
3602 if (CtdlGetRoom(&qrbuf, roomname) != 0) {
3603 CtdlCreateRoom(roomname, ( (is_mailbox != NULL) ? 5 : 3 ), "", 0, 1, 0, VIEW_BBS);
3605 /* If the caller specified this object as unique, delete all
3606 * other objects of this type that are currently in the room.
3609 syslog(LOG_DEBUG, "msgbase: deleted %d other msgs of this type",
3610 CtdlDeleteMessages(roomname, NULL, 0, content_type)
3613 /* Now write the data */
3614 CtdlSubmitMsg(msg, NULL, roomname);
3619 /************************************************************************/
3620 /* MODULE INITIALIZATION */
3621 /************************************************************************/
3623 CTDL_MODULE_INIT(msgbase)
3626 FillMsgKeyLookupTable();
3629 /* return our module id for the log */