2 * Implements the message store.
4 * Copyright (c) 1987-2018 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>
23 #include "ctdl_module.h"
24 #include "citserver.h"
27 #include "clientsocket.h"
31 #include "internet_addressing.h"
32 #include "euidindex.h"
34 #include "journaling.h"
36 struct addresses_to_be_filed *atbf = NULL;
39 * These are the four-character field headers we use when outputting
40 * messages in Citadel format (as opposed to RFC822 format).
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,
50 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
52 "from", /* A -> eAuthor */
53 NULL, /* B -> eBig_message */
54 NULL, /* C -> eRemoteRoom FIXME no more ignet */
55 NULL, /* D -> eDestination FIXME no more ignet */
56 "exti", /* E -> eXclusivID */
57 "rfca", /* F -> erFc822Addr */
59 "hnod", /* H -> eHumanNode FIXME no more ignet */
60 "msgn", /* I -> emessageId */
61 "jrnl", /* J -> eJournal */
62 "rep2", /* K -> eReplyTo */
63 "list", /* L -> eListID */
64 "text", /* M -> eMesageText */
65 "node", /* N -> eNodeName FIXME no more ignet */
66 "room", /* O -> eOriginalRoom */
67 "path", /* P -> eMessagePath */
69 "rcpt", /* R -> eRecipient */
70 "spec", /* S -> eSpecialField FIXME we might not be using this anymore */
71 "time", /* T -> eTimestamp */
72 "subj", /* U -> eMsgSubject */
73 "nvto", /* V -> eenVelopeTo */
74 "wefw", /* W -> eWeferences */
76 "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);
105 eMsgField FieldOrder[] = {
106 /* Important fields */
117 /* Semi-important fields */
123 /* G is not used yet, may become virus signature*/
126 /* Q is not used yet */
129 /* X is not used yet */
130 /* Z is not used yet */
137 /* Message text (MUST be last) */
139 /* Not saved to disk:
144 static const long NDiskFields = sizeof(FieldOrder) / sizeof(eMsgField);
147 int CM_IsEmpty(struct CtdlMessage *Msg, eMsgField which)
149 return !((Msg->cm_fields[which] != NULL) && (Msg->cm_fields[which][0] != '\0'));
153 void CM_SetField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
155 if (Msg->cm_fields[which] != NULL) {
156 free (Msg->cm_fields[which]);
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 Msg->cm_lengths[which] = length;
269 void CM_SetAsFieldSB(struct CtdlMessage *Msg, eMsgField which, StrBuf **buf)
271 if (Msg->cm_fields[which] != NULL) {
272 free (Msg->cm_fields[which]);
275 Msg->cm_lengths[which] = StrLength(*buf);
276 Msg->cm_fields[which] = SmashStrBuf(buf);
280 void CM_GetAsField(struct CtdlMessage *Msg, eMsgField which, char **ret, long *retlen)
282 if (Msg->cm_fields[which] != NULL) {
283 *retlen = Msg->cm_lengths[which];
284 *ret = Msg->cm_fields[which];
285 Msg->cm_fields[which] = NULL;
286 Msg->cm_lengths[which] = 0;
296 * Returns 1 if the supplied pointer points to a valid Citadel message.
297 * If the pointer is NULL or the magic number check fails, returns 0.
299 int CM_IsValidMsg(struct CtdlMessage *msg) {
303 if ((msg->cm_magic) != CTDLMESSAGE_MAGIC) {
304 syslog(LOG_WARNING, "msgbase: CM_IsValidMsg() self-check failed");
311 void CM_FreeContents(struct CtdlMessage *msg)
315 for (i = 0; i < 256; ++i)
316 if (msg->cm_fields[i] != NULL) {
317 free(msg->cm_fields[i]);
318 msg->cm_lengths[i] = 0;
321 msg->cm_magic = 0; /* just in case */
326 * 'Destructor' for struct CtdlMessage
328 void CM_Free(struct CtdlMessage *msg)
330 if (CM_IsValidMsg(msg) == 0) {
331 if (msg != NULL) free (msg);
334 CM_FreeContents(msg);
339 int CM_DupField(eMsgField i, struct CtdlMessage *OrgMsg, struct CtdlMessage *NewMsg)
342 len = OrgMsg->cm_lengths[i];
343 NewMsg->cm_fields[i] = malloc(len + 1);
344 if (NewMsg->cm_fields[i] == NULL) {
347 memcpy(NewMsg->cm_fields[i], OrgMsg->cm_fields[i], len);
348 NewMsg->cm_fields[i][len] = '\0';
349 NewMsg->cm_lengths[i] = len;
354 struct CtdlMessage * CM_Duplicate(struct CtdlMessage *OrgMsg)
357 struct CtdlMessage *NewMsg;
359 if (CM_IsValidMsg(OrgMsg) == 0) {
362 NewMsg = (struct CtdlMessage *)malloc(sizeof(struct CtdlMessage));
363 if (NewMsg == NULL) {
367 memcpy(NewMsg, OrgMsg, sizeof(struct CtdlMessage));
369 memset(&NewMsg->cm_fields, 0, sizeof(char*) * 256);
371 for (i = 0; i < 256; ++i) {
372 if (OrgMsg->cm_fields[i] != NULL) {
373 if (!CM_DupField(i, OrgMsg, NewMsg)) {
384 /* Determine if a given message matches the fields in a message template.
385 * Return 0 for a successful match.
387 int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template) {
390 /* If there aren't any fields in the template, all messages will
393 if (template == NULL) return(0);
395 /* Null messages are bogus. */
396 if (msg == NULL) return(1);
398 for (i='A'; i<='Z'; ++i) {
399 if (template->cm_fields[i] != NULL) {
400 if (msg->cm_fields[i] == NULL) {
401 /* Considered equal if temmplate is empty string */
402 if (IsEmptyStr(template->cm_fields[i])) continue;
405 if ((template->cm_lengths[i] != msg->cm_lengths[i]) ||
406 (strcasecmp(msg->cm_fields[i], template->cm_fields[i])))
411 /* All compares succeeded: we have a match! */
417 * Retrieve the "seen" message list for the current room.
419 void CtdlGetSeen(char *buf, int which_set) {
422 /* Learn about the user and room in question */
423 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
425 if (which_set == ctdlsetseen_seen) {
426 safestrncpy(buf, vbuf.v_seen, SIZ);
428 if (which_set == ctdlsetseen_answered) {
429 safestrncpy(buf, vbuf.v_answered, SIZ);
435 * Manipulate the "seen msgs" string (or other message set strings)
437 void CtdlSetSeen(long *target_msgnums, int num_target_msgnums,
438 int target_setting, int which_set,
439 struct ctdluser *which_user, struct ctdlroom *which_room) {
440 struct cdbdata *cdbfr;
454 char *is_set; /* actually an array of booleans */
456 /* Don't bother doing *anything* if we were passed a list of zero messages */
457 if (num_target_msgnums < 1) {
461 /* If no room was specified, we go with the current room. */
463 which_room = &CC->room;
466 /* If no user was specified, we go with the current user. */
468 which_user = &CC->user;
471 syslog(LOG_DEBUG, "msgbase: CtdlSetSeen(%d msgs starting with %ld, %s, %d) in <%s>",
472 num_target_msgnums, target_msgnums[0],
473 (target_setting ? "SET" : "CLEAR"),
477 /* Learn about the user and room in question */
478 CtdlGetRelationship(&vbuf, which_user, which_room);
480 /* Load the message list */
481 cdbfr = cdb_fetch(CDB_MSGLISTS, &which_room->QRnumber, sizeof(long));
483 msglist = (long *) cdbfr->ptr;
484 cdbfr->ptr = NULL; /* CtdlSetSeen() now owns this memory */
485 num_msgs = cdbfr->len / sizeof(long);
488 return; /* No messages at all? No further action. */
491 is_set = malloc(num_msgs * sizeof(char));
492 memset(is_set, 0, (num_msgs * sizeof(char)) );
494 /* Decide which message set we're manipulating */
496 case ctdlsetseen_seen:
497 vset = NewStrBufPlain(vbuf.v_seen, -1);
499 case ctdlsetseen_answered:
500 vset = NewStrBufPlain(vbuf.v_answered, -1);
507 #if 0 /* This is a special diagnostic section. Do not allow it to run during normal operation. */
508 syslog(LOG_DEBUG, "There are %d messages in the room.\n", num_msgs);
509 for (i=0; i<num_msgs; ++i) {
510 if ((i > 0) && (msglist[i] <= msglist[i-1])) abort();
512 syslog(LOG_DEBUG, "We are twiddling %d of them.\n", num_target_msgnums);
513 for (k=0; k<num_target_msgnums; ++k) {
514 if ((k > 0) && (target_msgnums[k] <= target_msgnums[k-1])) abort();
518 /* Translate the existing sequence set into an array of booleans */
519 setstr = NewStrBuf();
523 while (StrBufExtract_NextToken(setstr, vset, &pvset, ',') >= 0) {
525 StrBufExtract_token(lostr, setstr, 0, ':');
526 if (StrBufNum_tokens(setstr, ':') >= 2) {
527 StrBufExtract_token(histr, setstr, 1, ':');
531 StrBufAppendBuf(histr, lostr, 0);
534 if (!strcmp(ChrPtr(histr), "*")) {
541 for (i = 0; i < num_msgs; ++i) {
542 if ((msglist[i] >= lo) && (msglist[i] <= hi)) {
551 /* Now translate the array of booleans back into a sequence set */
557 for (i=0; i<num_msgs; ++i) {
561 for (k=0; k<num_target_msgnums; ++k) {
562 if (msglist[i] == target_msgnums[k]) {
563 is_seen = target_setting;
567 if ((was_seen == 0) && (is_seen == 1)) {
570 else if ((was_seen == 1) && (is_seen == 0)) {
573 if (StrLength(vset) > 0) {
574 StrBufAppendBufPlain(vset, HKEY(","), 0);
577 StrBufAppendPrintf(vset, "%ld", hi);
580 StrBufAppendPrintf(vset, "%ld:%ld", lo, hi);
584 if ((is_seen) && (i == num_msgs - 1)) {
585 if (StrLength(vset) > 0) {
586 StrBufAppendBufPlain(vset, HKEY(","), 0);
588 if ((i==0) || (was_seen == 0)) {
589 StrBufAppendPrintf(vset, "%ld", msglist[i]);
592 StrBufAppendPrintf(vset, "%ld:%ld", lo, msglist[i]);
600 * We will have to stuff this string back into a 4096 byte buffer, so if it's
601 * larger than that now, truncate it by removing tokens from the beginning.
602 * The limit of 100 iterations is there to prevent an infinite loop in case
603 * something unexpected happens.
605 int number_of_truncations = 0;
606 while ( (StrLength(vset) > SIZ) && (number_of_truncations < 100) ) {
607 StrBufRemove_token(vset, 0, ',');
608 ++number_of_truncations;
612 * If we're truncating the sequence set of messages marked with the 'seen' flag,
613 * we want the earliest messages (the truncated ones) to be marked, not unmarked.
614 * Otherwise messages at the beginning will suddenly appear to be 'unseen'.
616 if ( (which_set == ctdlsetseen_seen) && (number_of_truncations > 0) ) {
618 first_tok = NewStrBuf();
619 StrBufExtract_token(first_tok, vset, 0, ',');
620 StrBufRemove_token(vset, 0, ',');
622 if (StrBufNum_tokens(first_tok, ':') > 1) {
623 StrBufRemove_token(first_tok, 0, ':');
627 new_set = NewStrBuf();
628 StrBufAppendBufPlain(new_set, HKEY("1:"), 0);
629 StrBufAppendBuf(new_set, first_tok, 0);
630 StrBufAppendBufPlain(new_set, HKEY(":"), 0);
631 StrBufAppendBuf(new_set, vset, 0);
634 FreeStrBuf(&first_tok);
638 /* Decide which message set we're manipulating */
640 case ctdlsetseen_seen:
641 safestrncpy(vbuf.v_seen, ChrPtr(vset), sizeof vbuf.v_seen);
643 case ctdlsetseen_answered:
644 safestrncpy(vbuf.v_answered, ChrPtr(vset), sizeof vbuf.v_answered);
650 CtdlSetRelationship(&vbuf, which_user, which_room);
656 * API function to perform an operation for each qualifying message in the
657 * current room. (Returns the number of messages processed.)
659 int CtdlForEachMessage(int mode, long ref, char *search_string,
661 struct CtdlMessage *compare,
662 ForEachMsgCallback CallBack,
667 struct cdbdata *cdbfr;
668 long *msglist = NULL;
670 int num_processed = 0;
673 struct CtdlMessage *msg = NULL;
676 int printed_lastold = 0;
677 int num_search_msgs = 0;
678 long *search_msgs = NULL;
680 int need_to_free_re = 0;
683 if ((content_type) && (!IsEmptyStr(content_type))) {
684 regcomp(&re, content_type, 0);
688 /* Learn about the user and room in question */
689 if (server_shutting_down) {
690 if (need_to_free_re) regfree(&re);
693 CtdlGetUser(&CC->user, CC->curr_user);
695 if (server_shutting_down) {
696 if (need_to_free_re) regfree(&re);
699 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
701 if (server_shutting_down) {
702 if (need_to_free_re) regfree(&re);
706 /* Load the message list */
707 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
709 if (need_to_free_re) regfree(&re);
710 return 0; /* No messages at all? No further action. */
713 msglist = (long *) cdbfr->ptr;
714 num_msgs = cdbfr->len / sizeof(long);
716 cdbfr->ptr = NULL; /* clear this so that cdb_free() doesn't free it */
717 cdb_free(cdbfr); /* we own this memory now */
720 * Now begin the traversal.
722 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
724 /* If the caller is looking for a specific MIME type, filter
725 * out all messages which are not of the type requested.
727 if ((content_type != NULL) && (!IsEmptyStr(content_type))) {
729 /* This call to GetMetaData() sits inside this loop
730 * so that we only do the extra database read per msg
731 * if we need to. Doing the extra read all the time
732 * really kills the server. If we ever need to use
733 * metadata for another search criterion, we need to
734 * move the read somewhere else -- but still be smart
735 * enough to only do the read if the caller has
736 * specified something that will need it.
738 if (server_shutting_down) {
739 if (need_to_free_re) regfree(&re);
743 GetMetaData(&smi, msglist[a]);
745 /* if (strcasecmp(smi.meta_content_type, content_type)) { old non-regex way */
746 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) != 0) {
752 num_msgs = sort_msglist(msglist, num_msgs);
754 /* If a template was supplied, filter out the messages which
755 * don't match. (This could induce some delays!)
758 if (compare != NULL) {
759 for (a = 0; a < num_msgs; ++a) {
760 if (server_shutting_down) {
761 if (need_to_free_re) regfree(&re);
765 msg = CtdlFetchMessage(msglist[a], 1, 1);
767 if (CtdlMsgCmp(msg, compare)) {
776 /* If a search string was specified, get a message list from
777 * the full text index and remove messages which aren't on both
781 * Since the lists are sorted and strictly ascending, and the
782 * output list is guaranteed to be shorter than or equal to the
783 * input list, we overwrite the bottom of the input list. This
784 * eliminates the need to memmove big chunks of the list over and
787 if ( (num_msgs > 0) && (mode == MSGS_SEARCH) && (search_string) ) {
789 /* Call search module via hook mechanism.
790 * NULL means use any search function available.
791 * otherwise replace with a char * to name of search routine
793 CtdlModuleDoSearch(&num_search_msgs, &search_msgs, search_string, "fulltext");
795 if (num_search_msgs > 0) {
799 orig_num_msgs = num_msgs;
801 for (i=0; i<orig_num_msgs; ++i) {
802 for (j=0; j<num_search_msgs; ++j) {
803 if (msglist[i] == search_msgs[j]) {
804 msglist[num_msgs++] = msglist[i];
810 num_msgs = 0; /* No messages qualify */
812 if (search_msgs != NULL) free(search_msgs);
814 /* Now that we've purged messages which don't contain the search
815 * string, treat a MSGS_SEARCH just like a MSGS_ALL from this
822 * Now iterate through the message list, according to the
823 * criteria supplied by the caller.
826 for (a = 0; a < num_msgs; ++a) {
827 if (server_shutting_down) {
828 if (need_to_free_re) regfree(&re);
830 return num_processed;
832 thismsg = msglist[a];
833 if (mode == MSGS_ALL) {
837 is_seen = is_msg_in_sequence_set(
838 vbuf.v_seen, thismsg);
839 if (is_seen) lastold = thismsg;
845 || ((mode == MSGS_OLD) && (is_seen))
846 || ((mode == MSGS_NEW) && (!is_seen))
847 || ((mode == MSGS_LAST) && (a >= (num_msgs - ref)))
848 || ((mode == MSGS_FIRST) && (a < ref))
849 || ((mode == MSGS_GT) && (thismsg > ref))
850 || ((mode == MSGS_LT) && (thismsg < ref))
851 || ((mode == MSGS_EQ) && (thismsg == ref))
854 if ((mode == MSGS_NEW) && (CC->user.flags & US_LASTOLD) && (lastold > 0L) && (printed_lastold == 0) && (!is_seen)) {
856 CallBack(lastold, userdata);
862 CallBack(thismsg, userdata);
867 if (need_to_free_re) regfree(&re);
870 * We cache the most recent msglist in order to do security checks later
872 if (CC->client_socket > 0) {
873 if (CC->cached_msglist != NULL) {
874 free(CC->cached_msglist);
876 CC->cached_msglist = msglist;
877 CC->cached_num_msgs = num_msgs;
883 return num_processed;
888 * memfmout() - Citadel text formatter and paginator.
889 * Although the original purpose of this routine was to format
890 * text to the reader's screen width, all we're really using it
891 * for here is to format text out to 80 columns before sending it
892 * to the client. The client software may reformat it again.
895 char *mptr, /* where are we going to get our text from? */
896 const char *nl /* string to terminate lines with */
899 unsigned char ch = 0;
906 while (ch=*(mptr++), ch != 0) {
909 if (client_write(outbuf, len) == -1) {
910 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
914 if (client_write(nl, nllen) == -1) {
915 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
920 else if (ch == '\r') {
921 /* Ignore carriage returns. Newlines are always LF or CRLF but never CR. */
923 else if (isspace(ch)) {
924 if (column > 72) { /* Beyond 72 columns, break on the next space */
925 if (client_write(outbuf, len) == -1) {
926 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
930 if (client_write(nl, nllen) == -1) {
931 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
944 if (column > 1000) { /* Beyond 1000 columns, break anywhere */
945 if (client_write(outbuf, len) == -1) {
946 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
950 if (client_write(nl, nllen) == -1) {
951 syslog(LOG_ERR, "msgbase: memfmout(): aborting due to write failure");
959 if (client_write(outbuf, len) == -1) {
960 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
963 client_write(nl, nllen);
970 * Callback function for mime parser that simply lists the part
972 void list_this_part(char *name, char *filename, char *partnum, char *disp,
973 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
974 char *cbid, void *cbuserdata)
978 ma = (struct ma_info *)cbuserdata;
979 if (ma->is_ma == 0) {
980 cprintf("part=%s|%s|%s|%s|%s|%ld|%s|%s\n",
994 * Callback function for multipart prefix
996 void list_this_pref(char *name, char *filename, char *partnum, char *disp,
997 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
998 char *cbid, void *cbuserdata)
1002 ma = (struct ma_info *)cbuserdata;
1003 if (!strcasecmp(cbtype, "multipart/alternative")) {
1007 if (ma->is_ma == 0) {
1008 cprintf("pref=%s|%s\n", partnum, cbtype);
1014 * Callback function for multipart sufffix
1016 void list_this_suff(char *name, char *filename, char *partnum, char *disp,
1017 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1018 char *cbid, void *cbuserdata)
1022 ma = (struct ma_info *)cbuserdata;
1023 if (ma->is_ma == 0) {
1024 cprintf("suff=%s|%s\n", partnum, cbtype);
1026 if (!strcasecmp(cbtype, "multipart/alternative")) {
1033 * Callback function for mime parser that opens a section for downloading
1034 * we use serv_files function here:
1036 extern void OpenCmdResult(char *filename, const char *mime_type);
1037 void mime_download(char *name, char *filename, char *partnum, char *disp,
1038 void *content, char *cbtype, char *cbcharset, size_t length,
1039 char *encoding, char *cbid, void *cbuserdata)
1043 /* Silently go away if there's already a download open. */
1044 if (CC->download_fp != NULL)
1048 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
1049 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
1051 CC->download_fp = tmpfile();
1052 if (CC->download_fp == NULL) {
1053 syslog(LOG_EMERG, "msgbase: mime_download() couldn't write: %m");
1054 cprintf("%d cannot open temporary file: %s\n", ERROR + INTERNAL_ERROR, strerror(errno));
1058 rv = fwrite(content, length, 1, CC->download_fp);
1060 syslog(LOG_EMERG, "msgbase: mime_download() Couldn't write: %m");
1061 cprintf("%d unable to write tempfile.\n", ERROR + TOO_BIG);
1062 fclose(CC->download_fp);
1063 CC->download_fp = NULL;
1066 fflush(CC->download_fp);
1067 rewind(CC->download_fp);
1069 OpenCmdResult(filename, cbtype);
1075 * Callback function for mime parser that outputs a section all at once.
1076 * We can specify the desired section by part number *or* content-id.
1078 void mime_spew_section(char *name, char *filename, char *partnum, char *disp,
1079 void *content, char *cbtype, char *cbcharset, size_t length,
1080 char *encoding, char *cbid, void *cbuserdata)
1082 int *found_it = (int *)cbuserdata;
1085 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
1086 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
1089 cprintf("%d %d|-1|%s|%s|%s\n",
1096 client_write(content, length);
1101 struct CtdlMessage *CtdlDeserializeMessage(long msgnum, int with_body, const char *Buffer, long Length)
1103 struct CtdlMessage *ret = NULL;
1105 const char *upper_bound;
1107 cit_uint8_t field_header;
1111 upper_bound = Buffer + Length;
1113 /* Parse the three bytes that begin EVERY message on disk.
1114 * The first is always 0xFF, the on-disk magic number.
1115 * The second is the anonymous/public type byte.
1116 * The third is the format type byte (vari, fixed, or MIME).
1120 syslog(LOG_ERR, "msgbase: message %ld appears to be corrupted", msgnum);
1123 ret = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1124 memset(ret, 0, sizeof(struct CtdlMessage));
1126 ret->cm_magic = CTDLMESSAGE_MAGIC;
1127 ret->cm_anon_type = *mptr++; /* Anon type byte */
1128 ret->cm_format_type = *mptr++; /* Format type byte */
1131 * The rest is zero or more arbitrary fields. Load them in.
1132 * We're done when we encounter either a zero-length field or
1133 * have just processed the 'M' (message text) field.
1136 field_header = '\0';
1139 /* work around possibly buggy messages: */
1140 while (field_header == '\0') {
1141 if (mptr >= upper_bound) {
1144 field_header = *mptr++;
1146 if (mptr >= upper_bound) {
1149 which = field_header;
1152 CM_SetField(ret, which, mptr, len);
1154 mptr += len + 1; /* advance to next field */
1156 } while ((mptr < upper_bound) && (field_header != 'M'));
1163 * Load a message from disk into memory.
1164 * This is used by CtdlOutputMsg() and other fetch functions.
1166 * NOTE: Caller is responsible for freeing the returned CtdlMessage struct
1167 * using the CM_Free(); function.
1169 struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body, int run_msg_hooks)
1171 struct cdbdata *dmsgtext;
1172 struct CtdlMessage *ret = NULL;
1174 syslog(LOG_DEBUG, "msgbase: CtdlFetchMessage(%ld, %d)", msgnum, with_body);
1175 dmsgtext = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
1176 if (dmsgtext == NULL) {
1177 syslog(LOG_ERR, "msgbase: CtdlFetchMessage(%ld, %d) Failed!", msgnum, with_body);
1181 if (dmsgtext->ptr[dmsgtext->len - 1] != '\0') {
1182 syslog(LOG_ERR, "msgbase: CtdlFetchMessage(%ld, %d) Forcefully terminating message!!", msgnum, with_body);
1183 dmsgtext->ptr[dmsgtext->len - 1] = '\0';
1186 ret = CtdlDeserializeMessage(msgnum, with_body, dmsgtext->ptr, dmsgtext->len);
1194 /* Always make sure there's something in the msg text field. If
1195 * it's NULL, the message text is most likely stored separately,
1196 * so go ahead and fetch that. Failing that, just set a dummy
1197 * body so other code doesn't barf.
1199 if ( (CM_IsEmpty(ret, eMesageText)) && (with_body) ) {
1200 dmsgtext = cdb_fetch(CDB_BIGMSGS, &msgnum, sizeof(long));
1201 if (dmsgtext != NULL) {
1202 CM_SetAsField(ret, eMesageText, &dmsgtext->ptr, dmsgtext->len - 1);
1206 if (CM_IsEmpty(ret, eMesageText)) {
1207 CM_SetField(ret, eMesageText, HKEY("\r\n\r\n (no text)\r\n"));
1210 /* Perform "before read" hooks (aborting if any return nonzero) */
1211 if (run_msg_hooks && (PerformMessageHooks(ret, NULL, EVT_BEFOREREAD) > 0)) {
1221 * Pre callback function for multipart/alternative
1223 * NOTE: this differs from the standard behavior for a reason. Normally when
1224 * displaying multipart/alternative you want to show the _last_ usable
1225 * format in the message. Here we show the _first_ one, because it's
1226 * usually text/plain. Since this set of functions is designed for text
1227 * output to non-MIME-aware clients, this is the desired behavior.
1230 void fixed_output_pre(char *name, char *filename, char *partnum, char *disp,
1231 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1232 char *cbid, void *cbuserdata)
1236 ma = (struct ma_info *)cbuserdata;
1237 syslog(LOG_DEBUG, "msgbase: fixed_output_pre() type=<%s>", cbtype);
1238 if (!strcasecmp(cbtype, "multipart/alternative")) {
1242 if (!strcasecmp(cbtype, "message/rfc822")) {
1249 * Post callback function for multipart/alternative
1251 void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
1252 void *content, char *cbtype, char *cbcharset, size_t length,
1253 char *encoding, char *cbid, void *cbuserdata)
1257 ma = (struct ma_info *)cbuserdata;
1258 syslog(LOG_DEBUG, "msgbase: fixed_output_post() type=<%s>", cbtype);
1259 if (!strcasecmp(cbtype, "multipart/alternative")) {
1263 if (!strcasecmp(cbtype, "message/rfc822")) {
1270 * Inline callback function for mime parser that wants to display text
1272 void fixed_output(char *name, char *filename, char *partnum, char *disp,
1273 void *content, char *cbtype, char *cbcharset, size_t length,
1274 char *encoding, char *cbid, void *cbuserdata)
1281 ma = (struct ma_info *)cbuserdata;
1284 "msgbase: fixed_output() part %s: %s (%s) (%ld bytes)",
1285 partnum, filename, cbtype, (long)length
1289 * If we're in the middle of a multipart/alternative scope and
1290 * we've already printed another section, skip this one.
1292 if ( (ma->is_ma) && (ma->did_print) ) {
1293 syslog(LOG_DEBUG, "msgbase: skipping part %s (%s)", partnum, cbtype);
1298 if ( (!strcasecmp(cbtype, "text/plain"))
1299 || (IsEmptyStr(cbtype)) ) {
1302 client_write(wptr, length);
1303 if (wptr[length-1] != '\n') {
1310 if (!strcasecmp(cbtype, "text/html")) {
1311 ptr = html_to_ascii(content, length, 80, 0);
1313 client_write(ptr, wlen);
1314 if ((wlen > 0) && (ptr[wlen-1] != '\n')) {
1321 if (ma->use_fo_hooks) {
1322 if (PerformFixedOutputHooks(cbtype, content, length)) {
1323 /* above function returns nonzero if it handled the part */
1328 if (strncasecmp(cbtype, "multipart/", 10)) {
1329 cprintf("Part %s: %s (%s) (%ld bytes)\r\n",
1330 partnum, filename, cbtype, (long)length);
1337 * The client is elegant and sophisticated and wants to be choosy about
1338 * MIME content types, so figure out which multipart/alternative part
1339 * we're going to send.
1341 * We use a system of weights. When we find a part that matches one of the
1342 * MIME types we've declared as preferential, we can store it in ma->chosen_part
1343 * and then set ma->chosen_pref to that MIME type's position in our preference
1344 * list. If we then hit another match, we only replace the first match if
1345 * the preference value is lower.
1347 void choose_preferred(char *name, char *filename, char *partnum, char *disp,
1348 void *content, char *cbtype, char *cbcharset, size_t length,
1349 char *encoding, char *cbid, void *cbuserdata)
1355 ma = (struct ma_info *)cbuserdata;
1357 for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
1358 extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
1359 if ( (!strcasecmp(buf, cbtype)) && (!ma->freeze) ) {
1360 if (i < ma->chosen_pref) {
1361 syslog(LOG_DEBUG, "msgbase: setting chosen part to <%s>", partnum);
1362 safestrncpy(ma->chosen_part, partnum, sizeof ma->chosen_part);
1363 ma->chosen_pref = i;
1371 * Now that we've chosen our preferred part, output it.
1373 void output_preferred(char *name,
1387 int add_newline = 0;
1390 char *decoded = NULL;
1391 size_t bytes_decoded;
1394 ma = (struct ma_info *)cbuserdata;
1396 /* This is not the MIME part you're looking for... */
1397 if (strcasecmp(partnum, ma->chosen_part)) return;
1399 /* If the content-type of this part is in our preferred formats
1400 * list, we can simply output it verbatim.
1402 for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
1403 extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
1404 if (!strcasecmp(buf, cbtype)) {
1405 /* Yeah! Go! W00t!! */
1406 if (ma->dont_decode == 0)
1407 rc = mime_decode_now (content,
1413 break; /* Give us the chance, maybe theres another one. */
1415 if (rc == 0) text_content = (char *)content;
1417 text_content = decoded;
1418 length = bytes_decoded;
1421 if (text_content[length-1] != '\n') {
1424 cprintf("Content-type: %s", cbtype);
1425 if (!IsEmptyStr(cbcharset)) {
1426 cprintf("; charset=%s", cbcharset);
1428 cprintf("\nContent-length: %d\n",
1429 (int)(length + add_newline) );
1430 if (!IsEmptyStr(encoding)) {
1431 cprintf("Content-transfer-encoding: %s\n", encoding);
1434 cprintf("Content-transfer-encoding: 7bit\n");
1436 cprintf("X-Citadel-MSG4-Partnum: %s\n", partnum);
1438 if (client_write(text_content, length) == -1)
1440 syslog(LOG_ERR, "msgbase: output_preferred() aborting due to write failure");
1443 if (add_newline) cprintf("\n");
1444 if (decoded != NULL) free(decoded);
1449 /* No translations required or possible: output as text/plain */
1450 cprintf("Content-type: text/plain\n\n");
1452 if (ma->dont_decode == 0)
1453 rc = mime_decode_now (content,
1459 return; /* Give us the chance, maybe theres another one. */
1461 if (rc == 0) text_content = (char *)content;
1463 text_content = decoded;
1464 length = bytes_decoded;
1467 fixed_output(name, filename, partnum, disp, text_content, cbtype, cbcharset,
1468 length, encoding, cbid, cbuserdata);
1469 if (decoded != NULL) free(decoded);
1474 char desired_section[64];
1481 * Callback function for
1483 void extract_encapsulated_message(char *name, char *filename, char *partnum, char *disp,
1484 void *content, char *cbtype, char *cbcharset, size_t length,
1485 char *encoding, char *cbid, void *cbuserdata)
1487 struct encapmsg *encap;
1489 encap = (struct encapmsg *)cbuserdata;
1491 /* Only proceed if this is the desired section... */
1492 if (!strcasecmp(encap->desired_section, partnum)) {
1493 encap->msglen = length;
1494 encap->msg = malloc(length + 2);
1495 memcpy(encap->msg, content, length);
1502 * Determine whether the specified message exists in the cached_msglist
1503 * (This is a security check)
1505 int check_cached_msglist(long msgnum) {
1507 /* cases in which we skip the check */
1508 if (!CC) return om_ok; /* not a session */
1509 if (CC->client_socket <= 0) return om_ok; /* not a client session */
1510 if (CC->cached_msglist == NULL) return om_access_denied; /* no msglist fetched */
1511 if (CC->cached_num_msgs == 0) return om_access_denied; /* nothing to check */
1514 /* Do a binary search within the cached_msglist for the requested msgnum */
1516 int max = (CC->cached_num_msgs - 1);
1518 while (max >= min) {
1519 int middle = min + (max-min) / 2 ;
1520 if (msgnum == CC->cached_msglist[middle]) {
1523 if (msgnum > CC->cached_msglist[middle]) {
1531 return om_access_denied;
1536 * Get a message off disk. (returns om_* values found in msgbase.h)
1539 int CtdlOutputMsg(long msg_num, /* message number (local) to fetch */
1540 int mode, /* how would you like that message? */
1541 int headers_only, /* eschew the message body? */
1542 int do_proto, /* do Citadel protocol responses? */
1543 int crlf, /* Use CRLF newlines instead of LF? */
1544 char *section, /* NULL or a message/rfc822 section */
1545 int flags, /* various flags; see msgbase.h */
1550 struct CtdlMessage *TheMessage = NULL;
1551 int retcode = CIT_OK;
1552 struct encapmsg encap;
1555 syslog(LOG_DEBUG, "msgbase: CtdlOutputMsg(msgnum=%ld, mode=%d, section=%s)",
1557 (section ? section : "<>")
1560 r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
1563 if (r == om_not_logged_in) {
1564 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1567 cprintf("%d An unknown error has occurred.\n", ERROR);
1574 * Check to make sure the message is actually IN this room
1576 r = check_cached_msglist(msg_num);
1577 if (r == om_access_denied) {
1578 /* Not in the cache? We get ONE shot to check it again. */
1579 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, NULL, NULL);
1580 r = check_cached_msglist(msg_num);
1583 syslog(LOG_DEBUG, "msgbase: security check fail; message %ld is not in %s",
1584 msg_num, CC->room.QRname
1587 if (r == om_access_denied) {
1588 cprintf("%d message %ld was not found in this room\n",
1589 ERROR + HIGHER_ACCESS_REQUIRED,
1598 * Fetch the message from disk. If we're in HEADERS_FAST mode,
1599 * request that we don't even bother loading the body into memory.
1601 if (headers_only == HEADERS_FAST) {
1602 TheMessage = CtdlFetchMessage(msg_num, 0, 1);
1605 TheMessage = CtdlFetchMessage(msg_num, 1, 1);
1608 if (TheMessage == NULL) {
1609 if (do_proto) cprintf("%d Can't locate msg %ld on disk\n",
1610 ERROR + MESSAGE_NOT_FOUND, msg_num);
1611 return(om_no_such_msg);
1614 /* Here is the weird form of this command, to process only an
1615 * encapsulated message/rfc822 section.
1617 if (section) if (!IsEmptyStr(section)) if (strcmp(section, "0")) {
1618 memset(&encap, 0, sizeof encap);
1619 safestrncpy(encap.desired_section, section, sizeof encap.desired_section);
1620 mime_parser(CM_RANGE(TheMessage, eMesageText),
1621 *extract_encapsulated_message,
1622 NULL, NULL, (void *)&encap, 0
1625 if ((Author != NULL) && (*Author == NULL))
1628 CM_GetAsField(TheMessage, eAuthor, Author, &len);
1630 if ((Address != NULL) && (*Address == NULL))
1633 CM_GetAsField(TheMessage, erFc822Addr, Address, &len);
1635 if ((MessageID != NULL) && (*MessageID == NULL))
1638 CM_GetAsField(TheMessage, emessageId, MessageID, &len);
1640 CM_Free(TheMessage);
1644 encap.msg[encap.msglen] = 0;
1645 TheMessage = convert_internet_message(encap.msg);
1646 encap.msg = NULL; /* no free() here, TheMessage owns it now */
1648 /* Now we let it fall through to the bottom of this
1649 * function, because TheMessage now contains the
1650 * encapsulated message instead of the top-level
1651 * message. Isn't that neat?
1656 cprintf("%d msg %ld has no part %s\n",
1657 ERROR + MESSAGE_NOT_FOUND,
1661 retcode = om_no_such_msg;
1666 /* Ok, output the message now */
1667 if (retcode == CIT_OK)
1668 retcode = CtdlOutputPreLoadedMsg(TheMessage, mode, headers_only, do_proto, crlf, flags);
1669 if ((Author != NULL) && (*Author == NULL))
1672 CM_GetAsField(TheMessage, eAuthor, Author, &len);
1674 if ((Address != NULL) && (*Address == NULL))
1677 CM_GetAsField(TheMessage, erFc822Addr, Address, &len);
1679 if ((MessageID != NULL) && (*MessageID == NULL))
1682 CM_GetAsField(TheMessage, emessageId, MessageID, &len);
1685 CM_Free(TheMessage);
1691 void OutputCtdlMsgHeaders(
1692 struct CtdlMessage *TheMessage,
1693 int do_proto) /* do Citadel protocol responses? */
1698 char display_name[256];
1700 /* begin header processing loop for Citadel message format */
1701 safestrncpy(display_name, "<unknown>", sizeof display_name);
1702 if (!CM_IsEmpty(TheMessage, eAuthor)) {
1703 strcpy(buf, TheMessage->cm_fields[eAuthor]);
1704 if (TheMessage->cm_anon_type == MES_ANONONLY) {
1705 safestrncpy(display_name, "****", sizeof display_name);
1707 else if (TheMessage->cm_anon_type == MES_ANONOPT) {
1708 safestrncpy(display_name, "anonymous", sizeof display_name);
1711 safestrncpy(display_name, buf, sizeof display_name);
1713 if ((is_room_aide())
1714 && ((TheMessage->cm_anon_type == MES_ANONONLY)
1715 || (TheMessage->cm_anon_type == MES_ANONOPT))) {
1716 size_t tmp = strlen(display_name);
1717 snprintf(&display_name[tmp],
1718 sizeof display_name - tmp,
1723 /* Don't show Internet address for users on the
1724 * local Citadel network.
1727 if (!CM_IsEmpty(TheMessage, eNodeName) && (haschar(TheMessage->cm_fields[eNodeName], '.') == 0)) {
1731 /* Now spew the header fields in the order we like them. */
1732 for (i=0; i< NDiskFields; ++i) {
1734 Field = FieldOrder[i];
1735 if (Field != eMesageText) {
1736 if ( (!CM_IsEmpty(TheMessage, Field))
1737 && (msgkeys[Field] != NULL) ) {
1738 if ((Field == eenVelopeTo) ||
1739 (Field == eRecipient) ||
1740 (Field == eCarbonCopY)) {
1741 sanitize_truncated_recipient(TheMessage->cm_fields[Field]);
1743 if (Field == eAuthor) {
1744 if (do_proto) cprintf("%s=%s\n",
1748 else if ((Field == erFc822Addr) && (suppress_f)) {
1751 /* Masquerade display name if needed */
1754 cprintf("%s=%s\n", msgkeys[Field], TheMessage->cm_fields[Field]);
1763 void OutputRFC822MsgHeaders(
1764 struct CtdlMessage *TheMessage,
1765 int flags, /* should the bessage be exported clean */
1766 const char *nl, int nlen,
1767 char *mid, long sizeof_mid,
1768 char *suser, long sizeof_suser,
1769 char *luser, long sizeof_luser,
1770 char *fuser, long sizeof_fuser,
1771 char *snode, long sizeof_snode)
1773 char datestamp[100];
1774 int subject_found = 0;
1781 for (i = 0; i < NDiskFields; ++i) {
1782 if (TheMessage->cm_fields[FieldOrder[i]]) {
1783 mptr = mpptr = TheMessage->cm_fields[FieldOrder[i]];
1784 switch (FieldOrder[i]) {
1786 safestrncpy(luser, mptr, sizeof_luser);
1787 safestrncpy(suser, mptr, sizeof_suser);
1790 if ((flags & QP_EADDR) != 0) {
1791 mptr = qp_encode_email_addrs(mptr);
1793 sanitize_truncated_recipient(mptr);
1794 cprintf("CC: %s%s", mptr, nl);
1797 cprintf("Return-Path: %s%s", mptr, nl);
1800 cprintf("List-ID: %s%s", mptr, nl);
1803 if ((flags & QP_EADDR) != 0)
1804 mptr = qp_encode_email_addrs(mptr);
1806 while ((*hptr != '\0') && isspace(*hptr))
1808 if (!IsEmptyStr(hptr))
1809 cprintf("Envelope-To: %s%s", hptr, nl);
1812 cprintf("Subject: %s%s", mptr, nl);
1816 safestrncpy(mid, mptr, sizeof_mid);
1819 safestrncpy(fuser, mptr, sizeof_fuser);
1821 safestrncpy(snode, mptr, sizeof_snode);
1824 if (haschar(mptr, '@') == 0) {
1825 sanitize_truncated_recipient(mptr);
1826 cprintf("To: %s@%s", mptr, CtdlGetConfigStr("c_fqdn"));
1830 if ((flags & QP_EADDR) != 0) {
1831 mptr = qp_encode_email_addrs(mptr);
1833 sanitize_truncated_recipient(mptr);
1834 cprintf("To: %s", mptr);
1839 datestring(datestamp, sizeof datestamp, atol(mptr), DATESTRING_RFC822);
1840 cprintf("Date: %s%s", datestamp, nl);
1843 cprintf("References: ");
1844 k = num_tokens(mptr, '|');
1845 for (j=0; j<k; ++j) {
1846 extract_token(buf, mptr, j, '|', sizeof buf);
1847 cprintf("<%s>", buf);
1858 while ((*hptr != '\0') && isspace(*hptr))
1860 if (!IsEmptyStr(hptr))
1861 cprintf("Reply-To: %s%s", mptr, nl);
1877 /* these don't map to mime message headers. */
1880 if (mptr != mpptr) {
1885 if (subject_found == 0) {
1886 cprintf("Subject: (no subject)%s", nl);
1891 void Dump_RFC822HeadersBody(
1892 struct CtdlMessage *TheMessage,
1893 int headers_only, /* eschew the message body? */
1894 int flags, /* should the bessage be exported clean? */
1895 const char *nl, int nlen)
1897 cit_uint8_t prev_ch;
1899 const char *StartOfText = StrBufNOTNULL;
1902 int nllen = strlen(nl);
1906 mptr = TheMessage->cm_fields[eMesageText];
1909 while (*mptr != '\0') {
1910 if (*mptr == '\r') {
1917 eoh = (*(mptr+1) == '\r') && (*(mptr+2) == '\n');
1919 eoh = *(mptr+1) == '\n';
1923 StartOfText = strchr(StartOfText, '\n');
1924 StartOfText = strchr(StartOfText, '\n');
1927 if (((headers_only == HEADERS_NONE) && (mptr >= StartOfText)) ||
1928 ((headers_only == HEADERS_ONLY) && (mptr < StartOfText)) ||
1929 ((headers_only != HEADERS_NONE) &&
1930 (headers_only != HEADERS_ONLY))
1932 if (*mptr == '\n') {
1933 memcpy(&outbuf[outlen], nl, nllen);
1935 outbuf[outlen] = '\0';
1938 outbuf[outlen++] = *mptr;
1942 if (flags & ESC_DOT) {
1943 if ((prev_ch == '\n') && (*mptr == '.') && ((*(mptr+1) == '\r') || (*(mptr+1) == '\n'))) {
1944 outbuf[outlen++] = '.';
1949 if (outlen > 1000) {
1950 if (client_write(outbuf, outlen) == -1) {
1951 syslog(LOG_ERR, "msgbase: Dump_RFC822HeadersBody() aborting due to write failure");
1954 lfSent = (outbuf[outlen - 1] == '\n');
1959 client_write(outbuf, outlen);
1960 lfSent = (outbuf[outlen - 1] == '\n');
1963 client_write(nl, nlen);
1967 /* If the format type on disk is 1 (fixed-format), then we want
1968 * everything to be output completely literally ... regardless of
1969 * what message transfer format is in use.
1971 void DumpFormatFixed(
1972 struct CtdlMessage *TheMessage,
1973 int mode, /* how would you like that message? */
1974 const char *nl, int nllen)
1982 mptr = TheMessage->cm_fields[eMesageText];
1984 if (mode == MT_MIME) {
1985 cprintf("Content-type: text/plain\n\n");
1989 while (ch = *mptr++, ch > 0) {
1993 if ((buflen > 250) && (!xlline)){
1997 while ((buflen > 0) &&
1998 (!isspace(buf[buflen])))
2004 mptr -= tbuflen - buflen;
2010 /* if we reach the outer bounds of our buffer, abort without respect for what we purge. */
2011 if (xlline && ((isspace(ch)) || (buflen > SIZ - nllen - 2))) {
2016 memcpy (&buf[buflen], nl, nllen);
2020 if (client_write(buf, buflen) == -1) {
2021 syslog(LOG_ERR, "msgbase: DumpFormatFixed() aborting due to write failure");
2033 if (!IsEmptyStr(buf)) {
2034 cprintf("%s%s", buf, nl);
2040 * Get a message off disk. (returns om_* values found in msgbase.h)
2042 int CtdlOutputPreLoadedMsg(
2043 struct CtdlMessage *TheMessage,
2044 int mode, /* how would you like that message? */
2045 int headers_only, /* eschew the message body? */
2046 int do_proto, /* do Citadel protocol responses? */
2047 int crlf, /* Use CRLF newlines instead of LF? */
2048 int flags /* should the bessage be exported clean? */
2051 const char *nl; /* newline string */
2055 /* Buffers needed for RFC822 translation. These are all filled
2056 * using functions that are bounds-checked, and therefore we can
2057 * make them substantially smaller than SIZ.
2065 syslog(LOG_DEBUG, "msgbase: CtdlOutputPreLoadedMsg(TheMessage=%s, %d, %d, %d, %d",
2066 ((TheMessage == NULL) ? "NULL" : "not null"),
2067 mode, headers_only, do_proto, crlf
2070 strcpy(mid, "unknown");
2071 nl = (crlf ? "\r\n" : "\n");
2072 nlen = crlf ? 2 : 1;
2074 if (!CM_IsValidMsg(TheMessage)) {
2075 syslog(LOG_ERR, "msgbase: error; invalid preloaded message for output");
2076 return(om_no_such_msg);
2079 /* Suppress envelope recipients if required to avoid disclosing BCC addresses.
2080 * Pad it with spaces in order to avoid changing the RFC822 length of the message.
2082 if ( (flags & SUPPRESS_ENV_TO) && (!CM_IsEmpty(TheMessage, eenVelopeTo)) ) {
2083 memset(TheMessage->cm_fields[eenVelopeTo], ' ', TheMessage->cm_lengths[eenVelopeTo]);
2086 /* Are we downloading a MIME component? */
2087 if (mode == MT_DOWNLOAD) {
2088 if (TheMessage->cm_format_type != FMT_RFC822) {
2090 cprintf("%d This is not a MIME message.\n",
2091 ERROR + ILLEGAL_VALUE);
2092 } else if (CC->download_fp != NULL) {
2093 if (do_proto) cprintf(
2094 "%d You already have a download open.\n",
2095 ERROR + RESOURCE_BUSY);
2097 /* Parse the message text component */
2098 mime_parser(CM_RANGE(TheMessage, eMesageText),
2099 *mime_download, NULL, NULL, NULL, 0);
2100 /* If there's no file open by this time, the requested
2101 * section wasn't found, so print an error
2103 if (CC->download_fp == NULL) {
2104 if (do_proto) cprintf(
2105 "%d Section %s not found.\n",
2106 ERROR + FILE_NOT_FOUND,
2107 CC->download_desired_section);
2110 return((CC->download_fp != NULL) ? om_ok : om_mime_error);
2113 /* MT_SPEW_SECTION is like MT_DOWNLOAD except it outputs the whole MIME part
2114 * in a single server operation instead of opening a download file.
2116 if (mode == MT_SPEW_SECTION) {
2117 if (TheMessage->cm_format_type != FMT_RFC822) {
2119 cprintf("%d This is not a MIME message.\n",
2120 ERROR + ILLEGAL_VALUE);
2122 /* Parse the message text component */
2125 mime_parser(CM_RANGE(TheMessage, eMesageText),
2126 *mime_spew_section, NULL, NULL, (void *)&found_it, 0);
2127 /* If section wasn't found, print an error
2130 if (do_proto) cprintf(
2131 "%d Section %s not found.\n",
2132 ERROR + FILE_NOT_FOUND,
2133 CC->download_desired_section);
2136 return((CC->download_fp != NULL) ? om_ok : om_mime_error);
2139 /* now for the user-mode message reading loops */
2140 if (do_proto) cprintf("%d msg:\n", LISTING_FOLLOWS);
2142 /* Does the caller want to skip the headers? */
2143 if (headers_only == HEADERS_NONE) goto START_TEXT;
2145 /* Tell the client which format type we're using. */
2146 if ( (mode == MT_CITADEL) && (do_proto) ) {
2147 cprintf("type=%d\n", TheMessage->cm_format_type);
2150 /* nhdr=yes means that we're only displaying headers, no body */
2151 if ( (TheMessage->cm_anon_type == MES_ANONONLY)
2152 && ((mode == MT_CITADEL) || (mode == MT_MIME))
2155 cprintf("nhdr=yes\n");
2158 if ((mode == MT_CITADEL) || (mode == MT_MIME))
2159 OutputCtdlMsgHeaders(TheMessage, do_proto);
2162 /* begin header processing loop for RFC822 transfer format */
2166 memcpy(snode, CtdlGetConfigStr("c_nodename"), strlen(CtdlGetConfigStr("c_nodename")) + 1);
2167 if (mode == MT_RFC822)
2168 OutputRFC822MsgHeaders(
2173 suser, sizeof(suser),
2174 luser, sizeof(luser),
2175 fuser, sizeof(fuser),
2176 snode, sizeof(snode)
2180 for (i=0; !IsEmptyStr(&suser[i]); ++i) {
2181 suser[i] = tolower(suser[i]);
2182 if (!isalnum(suser[i])) suser[i]='_';
2185 if (mode == MT_RFC822) {
2186 if (!strcasecmp(snode, NODENAME)) {
2187 safestrncpy(snode, FQDN, sizeof snode);
2190 /* Construct a fun message id */
2191 cprintf("Message-ID: <%s", mid);/// todo: this possibly breaks threadding mails.
2192 if (strchr(mid, '@')==NULL) {
2193 cprintf("@%s", snode);
2197 if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONONLY)) {
2198 cprintf("From: \"----\" <x@x.org>%s", nl);
2200 else if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONOPT)) {
2201 cprintf("From: \"anonymous\" <x@x.org>%s", nl);
2203 else if (!IsEmptyStr(fuser)) {
2204 cprintf("From: \"%s\" <%s>%s", luser, fuser, nl);
2207 cprintf("From: \"%s\" <%s@%s>%s", luser, suser, snode, nl);
2210 /* Blank line signifying RFC822 end-of-headers */
2211 if (TheMessage->cm_format_type != FMT_RFC822) {
2216 /* end header processing loop ... at this point, we're in the text */
2218 if (headers_only == HEADERS_FAST) goto DONE;
2220 /* Tell the client about the MIME parts in this message */
2221 if (TheMessage->cm_format_type == FMT_RFC822) {
2222 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2223 memset(&ma, 0, sizeof(struct ma_info));
2224 mime_parser(CM_RANGE(TheMessage, eMesageText),
2225 (do_proto ? *list_this_part : NULL),
2226 (do_proto ? *list_this_pref : NULL),
2227 (do_proto ? *list_this_suff : NULL),
2230 else if (mode == MT_RFC822) { /* unparsed RFC822 dump */
2231 Dump_RFC822HeadersBody(
2240 if (headers_only == HEADERS_ONLY) {
2244 /* signify start of msg text */
2245 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2246 if (do_proto) cprintf("text\n");
2249 if (TheMessage->cm_format_type == FMT_FIXED)
2252 mode, /* how would you like that message? */
2255 /* If the message on disk is format 0 (Citadel vari-format), we
2256 * output using the formatter at 80 columns. This is the final output
2257 * form if the transfer format is RFC822, but if the transfer format
2258 * is Citadel proprietary, it'll still work, because the indentation
2259 * for new paragraphs is correct and the client will reformat the
2260 * message to the reader's screen width.
2262 if (TheMessage->cm_format_type == FMT_CITADEL) {
2263 if (mode == MT_MIME) {
2264 cprintf("Content-type: text/x-citadel-variformat\n\n");
2266 memfmout(TheMessage->cm_fields[eMesageText], nl);
2269 /* If the message on disk is format 4 (MIME), we've gotta hand it
2270 * off to the MIME parser. The client has already been told that
2271 * this message is format 1 (fixed format), so the callback function
2272 * we use will display those parts as-is.
2274 if (TheMessage->cm_format_type == FMT_RFC822) {
2275 memset(&ma, 0, sizeof(struct ma_info));
2277 if (mode == MT_MIME) {
2278 ma.use_fo_hooks = 0;
2279 strcpy(ma.chosen_part, "1");
2280 ma.chosen_pref = 9999;
2281 ma.dont_decode = CC->msg4_dont_decode;
2282 mime_parser(CM_RANGE(TheMessage, eMesageText),
2283 *choose_preferred, *fixed_output_pre,
2284 *fixed_output_post, (void *)&ma, 1);
2285 mime_parser(CM_RANGE(TheMessage, eMesageText),
2286 *output_preferred, NULL, NULL, (void *)&ma, 1);
2289 ma.use_fo_hooks = 1;
2290 mime_parser(CM_RANGE(TheMessage, eMesageText),
2291 *fixed_output, *fixed_output_pre,
2292 *fixed_output_post, (void *)&ma, 0);
2297 DONE: /* now we're done */
2298 if (do_proto) cprintf("000\n");
2303 * Save one or more message pointers into a specified room
2304 * (Returns 0 for success, nonzero for failure)
2305 * roomname may be NULL to use the current room
2307 * Note that the 'supplied_msg' field may be set to NULL, in which case
2308 * the message will be fetched from disk, by number, if we need to perform
2309 * replication checks. This adds an additional database read, so if the
2310 * caller already has the message in memory then it should be supplied. (Obviously
2311 * this mode of operation only works if we're saving a single message.)
2313 int CtdlSaveMsgPointersInRoom(char *roomname, long newmsgidlist[], int num_newmsgs,
2314 int do_repl_check, struct CtdlMessage *supplied_msg, int suppress_refcount_adj
2317 char hold_rm[ROOMNAMELEN];
2318 struct cdbdata *cdbfr;
2321 long highest_msg = 0L;
2324 struct CtdlMessage *msg = NULL;
2326 long *msgs_to_be_merged = NULL;
2327 int num_msgs_to_be_merged = 0;
2330 "msgbase: CtdlSaveMsgPointersInRoom(room=%s, num_msgs=%d, repl=%d, suppress_rca=%d)",
2331 roomname, num_newmsgs, do_repl_check, suppress_refcount_adj
2334 strcpy(hold_rm, CC->room.QRname);
2337 if (newmsgidlist == NULL) return(ERROR + INTERNAL_ERROR);
2338 if (num_newmsgs < 1) return(ERROR + INTERNAL_ERROR);
2339 if (num_newmsgs > 1) supplied_msg = NULL;
2341 /* Now the regular stuff */
2342 if (CtdlGetRoomLock(&CC->room,
2343 ((roomname != NULL) ? roomname : CC->room.QRname) )
2345 syslog(LOG_ERR, "msgbase: no such room <%s>", roomname);
2346 return(ERROR + ROOM_NOT_FOUND);
2350 msgs_to_be_merged = malloc(sizeof(long) * num_newmsgs);
2351 num_msgs_to_be_merged = 0;
2354 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
2355 if (cdbfr == NULL) {
2359 msglist = (long *) cdbfr->ptr;
2360 cdbfr->ptr = NULL; /* CtdlSaveMsgPointerInRoom() now owns this memory */
2361 num_msgs = cdbfr->len / sizeof(long);
2366 /* Create a list of msgid's which were supplied by the caller, but do
2367 * not already exist in the target room. It is absolutely taboo to
2368 * have more than one reference to the same message in a room.
2370 for (i=0; i<num_newmsgs; ++i) {
2372 if (num_msgs > 0) for (j=0; j<num_msgs; ++j) {
2373 if (msglist[j] == newmsgidlist[i]) {
2378 msgs_to_be_merged[num_msgs_to_be_merged++] = newmsgidlist[i];
2382 syslog(LOG_DEBUG, "msgbase: %d unique messages to be merged", num_msgs_to_be_merged);
2385 * Now merge the new messages
2387 msglist = realloc(msglist, (sizeof(long) * (num_msgs + num_msgs_to_be_merged)) );
2388 if (msglist == NULL) {
2389 syslog(LOG_ALERT, "msgbase: ERROR; can't realloc message list!");
2390 free(msgs_to_be_merged);
2391 return (ERROR + INTERNAL_ERROR);
2393 memcpy(&msglist[num_msgs], msgs_to_be_merged, (sizeof(long) * num_msgs_to_be_merged) );
2394 num_msgs += num_msgs_to_be_merged;
2396 /* Sort the message list, so all the msgid's are in order */
2397 num_msgs = sort_msglist(msglist, num_msgs);
2399 /* Determine the highest message number */
2400 highest_msg = msglist[num_msgs - 1];
2402 /* Write it back to disk. */
2403 cdb_store(CDB_MSGLISTS, &CC->room.QRnumber, (int)sizeof(long),
2404 msglist, (int)(num_msgs * sizeof(long)));
2406 /* Free up the memory we used. */
2409 /* Update the highest-message pointer and unlock the room. */
2410 CC->room.QRhighest = highest_msg;
2411 CtdlPutRoomLock(&CC->room);
2413 /* Perform replication checks if necessary */
2414 if ( (DoesThisRoomNeedEuidIndexing(&CC->room)) && (do_repl_check) ) {
2415 syslog(LOG_DEBUG, "msgbase: CtdlSaveMsgPointerInRoom() doing repl checks");
2417 for (i=0; i<num_msgs_to_be_merged; ++i) {
2418 msgid = msgs_to_be_merged[i];
2420 if (supplied_msg != NULL) {
2424 msg = CtdlFetchMessage(msgid, 0, 1);
2428 ReplicationChecks(msg);
2430 /* If the message has an Exclusive ID, index that... */
2431 if (!CM_IsEmpty(msg, eExclusiveID)) {
2432 index_message_by_euid(msg->cm_fields[eExclusiveID], &CC->room, msgid);
2435 /* Free up the memory we may have allocated */
2436 if (msg != supplied_msg) {
2445 syslog(LOG_DEBUG, "msgbase: CtdlSaveMsgPointerInRoom() skips repl checks");
2448 /* Submit this room for processing by hooks */
2449 PerformRoomHooks(&CC->room);
2451 /* Go back to the room we were in before we wandered here... */
2452 CtdlGetRoom(&CC->room, hold_rm);
2454 /* Bump the reference count for all messages which were merged */
2455 if (!suppress_refcount_adj) {
2456 AdjRefCountList(msgs_to_be_merged, num_msgs_to_be_merged, +1);
2459 /* Free up memory... */
2460 if (msgs_to_be_merged != NULL) {
2461 free(msgs_to_be_merged);
2464 /* Return success. */
2470 * This is the same as CtdlSaveMsgPointersInRoom() but it only accepts
2473 int CtdlSaveMsgPointerInRoom(char *roomname, long msgid,
2474 int do_repl_check, struct CtdlMessage *supplied_msg)
2476 return CtdlSaveMsgPointersInRoom(roomname, &msgid, 1, do_repl_check, supplied_msg, 0);
2483 * Message base operation to save a new message to the message store
2484 * (returns new message number)
2486 * This is the back end for CtdlSubmitMsg() and should not be directly
2487 * called by server-side modules.
2490 long CtdlSaveThisMessage(struct CtdlMessage *msg, long msgid, int Reply) {
2498 * If the message is big, set its body aside for storage elsewhere
2499 * and we hide the message body from the serializer
2501 if (!CM_IsEmpty(msg, eMesageText) && msg->cm_lengths[eMesageText] > BIGMSG)
2504 holdM = msg->cm_fields[eMesageText];
2505 msg->cm_fields[eMesageText] = NULL;
2506 holdMLen = msg->cm_lengths[eMesageText];
2507 msg->cm_lengths[eMesageText] = 0;
2510 /* Serialize our data structure for storage in the database */
2511 CtdlSerializeMessage(&smr, msg);
2514 /* put the message body back into the message */
2515 msg->cm_fields[eMesageText] = holdM;
2516 msg->cm_lengths[eMesageText] = holdMLen;
2521 cprintf("%d Unable to serialize message\n",
2522 ERROR + INTERNAL_ERROR);
2525 syslog(LOG_ERR, "msgbase: CtdlSaveMessage() unable to serialize message");
2531 /* Write our little bundle of joy into the message base */
2532 retval = cdb_store(CDB_MSGMAIN, &msgid, (int)sizeof(long),
2535 syslog(LOG_ERR, "msgbase: can't store message %ld: %ld", msgid, retval);
2539 retval = cdb_store(CDB_BIGMSGS,
2546 syslog(LOG_ERR, "msgbase: failed to store message body for msgid %ld: %ld", msgid, retval);
2551 /* Free the memory we used for the serialized message */
2557 long send_message(struct CtdlMessage *msg) {
2563 /* Get a new message number */
2564 newmsgid = get_new_message_number();
2566 /* Generate an ID if we don't have one already */
2567 if (CM_IsEmpty(msg, emessageId)) {
2568 msgidbuflen = snprintf(msgidbuf, sizeof msgidbuf, "%08lX-%08lX@%s",
2569 (long unsigned int) time(NULL),
2570 (long unsigned int) newmsgid,
2571 CtdlGetConfigStr("c_fqdn")
2574 CM_SetField(msg, emessageId, msgidbuf, msgidbuflen);
2577 retval = CtdlSaveThisMessage(msg, newmsgid, 1);
2583 /* Return the *local* message ID to the caller
2584 * (even if we're storing an incoming network message)
2592 * Serialize a struct CtdlMessage into the format used on disk and network.
2594 * This function loads up a "struct ser_ret" (defined in server.h) which
2595 * contains the length of the serialized message and a pointer to the
2596 * serialized message in memory. THE LATTER MUST BE FREED BY THE CALLER.
2598 void CtdlSerializeMessage(struct ser_ret *ret, /* return values */
2599 struct CtdlMessage *msg) /* unserialized msg */
2605 * Check for valid message format
2607 if (CM_IsValidMsg(msg) == 0) {
2608 syslog(LOG_ERR, "msgbase: CtdlSerializeMessage() aborting due to invalid message");
2615 for (i=0; i < NDiskFields; ++i)
2616 if (msg->cm_fields[FieldOrder[i]] != NULL)
2617 ret->len += msg->cm_lengths[FieldOrder[i]] + 2;
2619 ret->ser = malloc(ret->len);
2620 if (ret->ser == NULL) {
2621 syslog(LOG_ERR, "msgbase: CtdlSerializeMessage() malloc(%ld) failed: %m", (long)ret->len);
2628 ret->ser[1] = msg->cm_anon_type;
2629 ret->ser[2] = msg->cm_format_type;
2632 for (i=0; i < NDiskFields; ++i)
2633 if (msg->cm_fields[FieldOrder[i]] != NULL)
2635 ret->ser[wlen++] = (char)FieldOrder[i];
2637 memcpy(&ret->ser[wlen],
2638 msg->cm_fields[FieldOrder[i]],
2639 msg->cm_lengths[FieldOrder[i]] + 1);
2641 wlen = wlen + msg->cm_lengths[FieldOrder[i]] + 1;
2644 if (ret->len != wlen) {
2645 syslog(LOG_ERR, "msgbase: ERROR; len=%ld wlen=%ld", (long)ret->len, (long)wlen);
2653 * Check to see if any messages already exist in the current room which
2654 * carry the same Exclusive ID as this one. If any are found, delete them.
2656 void ReplicationChecks(struct CtdlMessage *msg) {
2657 long old_msgnum = (-1L);
2659 if (DoesThisRoomNeedEuidIndexing(&CC->room) == 0) return;
2661 syslog(LOG_DEBUG, "msgbase: performing replication checks in <%s>", CC->room.QRname);
2663 /* No exclusive id? Don't do anything. */
2664 if (msg == NULL) return;
2665 if (CM_IsEmpty(msg, eExclusiveID)) return;
2667 /*syslog(LOG_DEBUG, "msgbase: exclusive ID: <%s> for room <%s>",
2668 msg->cm_fields[eExclusiveID], CC->room.QRname);*/
2670 old_msgnum = CtdlLocateMessageByEuid(msg->cm_fields[eExclusiveID], &CC->room);
2671 if (old_msgnum > 0L) {
2672 syslog(LOG_DEBUG, "msgbase: ReplicationChecks() replacing message %ld", old_msgnum);
2673 CtdlDeleteMessages(CC->room.QRname, &old_msgnum, 1, "");
2680 * Save a message to disk and submit it into the delivery system.
2682 long CtdlSubmitMsg(struct CtdlMessage *msg, /* message to save */
2683 recptypes *recps, /* recipients (if mail) */
2684 const char *force, /* force a particular room? */
2685 int flags /* should the message be exported clean? */
2688 char hold_rm[ROOMNAMELEN];
2689 char actual_rm[ROOMNAMELEN];
2690 char force_room[ROOMNAMELEN];
2691 char content_type[SIZ]; /* We have to learn this */
2692 char recipient[SIZ];
2693 char bounce_to[1024];
2696 const char *mptr = NULL;
2697 struct ctdluser userbuf;
2699 struct MetaData smi;
2700 char *collected_addresses = NULL;
2701 struct addresses_to_be_filed *aptr = NULL;
2702 StrBuf *saved_rfc822_version = NULL;
2703 int qualified_for_journaling = 0;
2705 syslog(LOG_DEBUG, "msgbase: CtdlSubmitMsg() called");
2706 if (CM_IsValidMsg(msg) == 0) return(-1); /* self check */
2708 /* If this message has no timestamp, we take the liberty of
2709 * giving it one, right now.
2711 if (CM_IsEmpty(msg, eTimestamp)) {
2712 CM_SetFieldLONG(msg, eTimestamp, time(NULL));
2715 /* If this message has no path, we generate one.
2717 if (CM_IsEmpty(msg, eMessagePath)) {
2718 if (!CM_IsEmpty(msg, eAuthor)) {
2719 CM_CopyField(msg, eMessagePath, eAuthor);
2720 for (a=0; !IsEmptyStr(&msg->cm_fields[eMessagePath][a]); ++a) {
2721 if (isspace(msg->cm_fields[eMessagePath][a])) {
2722 msg->cm_fields[eMessagePath][a] = ' ';
2727 CM_SetField(msg, eMessagePath, HKEY("unknown"));
2731 if (force == NULL) {
2732 force_room[0] = '\0';
2735 strcpy(force_room, force);
2738 /* Learn about what's inside, because it's what's inside that counts */
2739 if (CM_IsEmpty(msg, eMesageText)) {
2740 syslog(LOG_ERR, "msgbase: ERROR; attempt to save message with NULL body");
2744 switch (msg->cm_format_type) {
2746 strcpy(content_type, "text/x-citadel-variformat");
2749 strcpy(content_type, "text/plain");
2752 strcpy(content_type, "text/plain");
2753 mptr = bmstrcasestr(msg->cm_fields[eMesageText], "Content-type:");
2756 safestrncpy(content_type, &mptr[13], sizeof content_type);
2757 striplt(content_type);
2758 aptr = content_type;
2759 while (!IsEmptyStr(aptr)) {
2771 /* Goto the correct room */
2772 room = (recps) ? CC->room.QRname : SENTITEMS;
2773 syslog(LOG_DEBUG, "msgbase: selected room %s", room);
2774 strcpy(hold_rm, CC->room.QRname);
2775 strcpy(actual_rm, CC->room.QRname);
2776 if (recps != NULL) {
2777 strcpy(actual_rm, SENTITEMS);
2780 /* If the user is a twit, move to the twit room for posting */
2782 if (CC->user.axlevel == AxProbU) {
2783 strcpy(hold_rm, actual_rm);
2784 strcpy(actual_rm, CtdlGetConfigStr("c_twitroom"));
2785 syslog(LOG_DEBUG, "msgbase: diverting to twit room");
2789 /* ...or if this message is destined for Aide> then go there. */
2790 if (!IsEmptyStr(force_room)) {
2791 strcpy(actual_rm, force_room);
2794 syslog(LOG_DEBUG, "msgbase: final selection: %s (%s)", actual_rm, room);
2795 if (strcasecmp(actual_rm, CC->room.QRname)) {
2796 /* CtdlGetRoom(&CC->room, actual_rm); */
2797 CtdlUserGoto(actual_rm, 0, 1, NULL, NULL, NULL, NULL);
2801 * If this message has no O (room) field, generate one.
2803 if (CM_IsEmpty(msg, eOriginalRoom) && !IsEmptyStr(CC->room.QRname)) {
2804 CM_SetField(msg, eOriginalRoom, CC->room.QRname, strlen(CC->room.QRname));
2807 /* Perform "before save" hooks (aborting if any return nonzero) */
2808 syslog(LOG_DEBUG, "msgbase: performing before-save hooks");
2809 if (PerformMessageHooks(msg, recps, EVT_BEFORESAVE) > 0) return(-3);
2812 * If this message has an Exclusive ID, and the room is replication
2813 * checking enabled, then do replication checks.
2815 if (DoesThisRoomNeedEuidIndexing(&CC->room)) {
2816 ReplicationChecks(msg);
2819 /* Save it to disk */
2820 syslog(LOG_DEBUG, "msgbase: saving to disk");
2821 newmsgid = send_message(msg);
2822 if (newmsgid <= 0L) return(-5);
2824 /* Write a supplemental message info record. This doesn't have to
2825 * be a critical section because nobody else knows about this message
2828 syslog(LOG_DEBUG, "msgbase: creating metadata record");
2829 memset(&smi, 0, sizeof(struct MetaData));
2830 smi.meta_msgnum = newmsgid;
2831 smi.meta_refcount = 0;
2832 safestrncpy(smi.meta_content_type, content_type,
2833 sizeof smi.meta_content_type);
2836 * Measure how big this message will be when rendered as RFC822.
2837 * We do this for two reasons:
2838 * 1. We need the RFC822 length for the new metadata record, so the
2839 * POP and IMAP services don't have to calculate message lengths
2840 * while the user is waiting (multiplied by potentially hundreds
2841 * or thousands of messages).
2842 * 2. If journaling is enabled, we will need an RFC822 version of the
2843 * message to attach to the journalized copy.
2845 if (CC->redirect_buffer != NULL) {
2846 syslog(LOG_ALERT, "msgbase: CC->redirect_buffer is not NULL during message submission!");
2849 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
2850 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, QP_EADDR);
2851 smi.meta_rfc822_length = StrLength(CC->redirect_buffer);
2852 saved_rfc822_version = CC->redirect_buffer;
2853 CC->redirect_buffer = NULL;
2857 /* Now figure out where to store the pointers */
2858 syslog(LOG_DEBUG, "msgbase: storing pointers");
2860 /* If this is being done by the networker delivering a private
2861 * message, we want to BYPASS saving the sender's copy (because there
2862 * is no local sender; it would otherwise go to the Trashcan).
2864 if ((!CC->internal_pgm) || (recps == NULL)) {
2865 if (CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 1, msg) != 0) {
2866 syslog(LOG_ERR, "msgbase: ERROR saving message pointer!");
2867 CtdlSaveMsgPointerInRoom(CtdlGetConfigStr("c_aideroom"), newmsgid, 0, msg);
2871 /* For internet mail, drop a copy in the outbound queue room */
2872 if ((recps != NULL) && (recps->num_internet > 0)) {
2873 CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, newmsgid, 0, msg);
2876 /* If other rooms are specified, drop them there too. */
2877 if ((recps != NULL) && (recps->num_room > 0))
2878 for (i=0; i<num_tokens(recps->recp_room, '|'); ++i) {
2879 extract_token(recipient, recps->recp_room, i,
2880 '|', sizeof recipient);
2881 syslog(LOG_DEBUG, "msgbase: delivering to room <%s>", recipient);
2882 CtdlSaveMsgPointerInRoom(recipient, newmsgid, 0, msg);
2885 /* Bump this user's messages posted counter. */
2886 syslog(LOG_DEBUG, "msgbase: updating user");
2887 CtdlLockGetCurrentUser();
2888 CC->user.posted = CC->user.posted + 1;
2889 CtdlPutCurrentUserLock();
2891 /* Decide where bounces need to be delivered */
2892 if ((recps != NULL) && (recps->bounce_to == NULL))
2894 if (CC->logged_in) {
2895 snprintf(bounce_to, sizeof bounce_to, "%s@%s", CC->user.fullname, CtdlGetConfigStr("c_nodename"));
2898 snprintf(bounce_to, sizeof bounce_to, "%s@%s", msg->cm_fields[eAuthor], msg->cm_fields[eNodeName]);
2900 recps->bounce_to = bounce_to;
2903 CM_SetFieldLONG(msg, eVltMsgNum, newmsgid);
2906 /* If this is private, local mail, make a copy in the
2907 * recipient's mailbox and bump the reference count.
2909 if ((recps != NULL) && (recps->num_local > 0))
2914 pch = recps->recp_local;
2915 recps->recp_local = recipient;
2916 ntokens = num_tokens(pch, '|');
2917 for (i=0; i<ntokens; ++i)
2919 extract_token(recipient, pch, i, '|', sizeof recipient);
2920 syslog(LOG_DEBUG, "msgbase: delivering private local mail to <%s>", recipient);
2921 if (CtdlGetUser(&userbuf, recipient) == 0) {
2922 CtdlMailboxName(actual_rm, sizeof actual_rm, &userbuf, MAILROOM);
2923 CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0, msg);
2924 CtdlBumpNewMailCounter(userbuf.usernum);
2925 PerformMessageHooks(msg, recps, EVT_AFTERUSRMBOXSAVE);
2928 syslog(LOG_DEBUG, "msgbase: no user <%s>", recipient);
2929 CtdlSaveMsgPointerInRoom(CtdlGetConfigStr("c_aideroom"), newmsgid, 0, msg);
2932 recps->recp_local = pch;
2935 /* Perform "after save" hooks */
2936 syslog(LOG_DEBUG, "msgbase: performing after-save hooks");
2938 PerformMessageHooks(msg, recps, EVT_AFTERSAVE);
2939 CM_FlushField(msg, eVltMsgNum);
2941 /* Go back to the room we started from */
2942 syslog(LOG_DEBUG, "msgbase: returning to original room %s", hold_rm);
2943 if (strcasecmp(hold_rm, CC->room.QRname))
2944 CtdlUserGoto(hold_rm, 0, 1, NULL, NULL, NULL, NULL);
2947 * Any addresses to harvest for someone's address book?
2949 if ( (CC->logged_in) && (recps != NULL) ) {
2950 collected_addresses = harvest_collected_addresses(msg);
2953 if (collected_addresses != NULL) {
2954 aptr = (struct addresses_to_be_filed *)
2955 malloc(sizeof(struct addresses_to_be_filed));
2956 CtdlMailboxName(actual_rm, sizeof actual_rm,
2957 &CC->user, USERCONTACTSROOM);
2958 aptr->roomname = strdup(actual_rm);
2959 aptr->collected_addresses = collected_addresses;
2960 begin_critical_section(S_ATBF);
2963 end_critical_section(S_ATBF);
2967 * Determine whether this message qualifies for journaling.
2969 if (!CM_IsEmpty(msg, eJournal)) {
2970 qualified_for_journaling = 0;
2973 if (recps == NULL) {
2974 qualified_for_journaling = CtdlGetConfigInt("c_journal_pubmsgs");
2976 else if (recps->num_local + recps->num_ignet + recps->num_internet > 0) {
2977 qualified_for_journaling = CtdlGetConfigInt("c_journal_email");
2980 qualified_for_journaling = CtdlGetConfigInt("c_journal_pubmsgs");
2985 * Do we have to perform journaling? If so, hand off the saved
2986 * RFC822 version will be handed off to the journaler for background
2987 * submit. Otherwise, we have to free the memory ourselves.
2989 if (saved_rfc822_version != NULL) {
2990 if (qualified_for_journaling) {
2991 JournalBackgroundSubmit(msg, saved_rfc822_version, recps);
2994 FreeStrBuf(&saved_rfc822_version);
2998 if ((recps != NULL) && (recps->bounce_to == bounce_to))
2999 recps->bounce_to = NULL;
3007 * Convenience function for generating small administrative messages.
3009 long quickie_message(const char *from,
3010 const char *fromaddr,
3015 const char *subject)
3017 struct CtdlMessage *msg;
3018 recptypes *recp = NULL;
3020 msg = malloc(sizeof(struct CtdlMessage));
3021 memset(msg, 0, sizeof(struct CtdlMessage));
3022 msg->cm_magic = CTDLMESSAGE_MAGIC;
3023 msg->cm_anon_type = MES_NORMAL;
3024 msg->cm_format_type = format_type;
3026 if (!IsEmptyStr(from)) {
3027 CM_SetField(msg, eAuthor, from, strlen(from));
3029 else if (!IsEmptyStr(fromaddr)) {
3031 CM_SetField(msg, eAuthor, fromaddr, strlen(fromaddr));
3032 pAt = strchr(msg->cm_fields[eAuthor], '@');
3034 CM_CutFieldAt(msg, eAuthor, pAt - msg->cm_fields[eAuthor]);
3038 msg->cm_fields[eAuthor] = strdup("Citadel");
3041 if (!IsEmptyStr(fromaddr)) CM_SetField(msg, erFc822Addr, fromaddr, strlen(fromaddr));
3042 if (!IsEmptyStr(room)) CM_SetField(msg, eOriginalRoom, room, strlen(room));
3043 CM_SetField(msg, eNodeName, CtdlGetConfigStr("c_nodename"), strlen(CtdlGetConfigStr("c_nodename")));
3044 if (!IsEmptyStr(to)) {
3045 CM_SetField(msg, eRecipient, to, strlen(to));
3046 recp = validate_recipients(to, NULL, 0);
3048 if (!IsEmptyStr(subject)) {
3049 CM_SetField(msg, eMsgSubject, subject, strlen(subject));
3051 if (!IsEmptyStr(text)) {
3052 CM_SetField(msg, eMesageText, text, strlen(text));
3055 long msgnum = CtdlSubmitMsg(msg, recp, room, 0);
3057 if (recp != NULL) free_recipients(recp);
3063 * Back end function used by CtdlMakeMessage() and similar functions
3065 StrBuf *CtdlReadMessageBodyBuf(char *terminator, /* token signalling EOT */
3067 size_t maxlen, /* maximum message length */
3068 StrBuf *exist, /* if non-null, append to it;
3069 exist is ALWAYS freed */
3070 int crlf /* CRLF newlines instead of LF */
3079 LineBuf = NewStrBufPlain(NULL, SIZ);
3080 if (exist == NULL) {
3081 Message = NewStrBufPlain(NULL, 4 * SIZ);
3084 Message = NewStrBufDup(exist);
3087 /* Do we need to change leading ".." to "." for SMTP escaping? */
3088 if ((tlen == 1) && (*terminator == '.')) {
3092 /* read in the lines of message text one by one */
3094 if (CtdlClientGetLine(LineBuf) < 0) {
3097 if ((StrLength(LineBuf) == tlen) && (!strcmp(ChrPtr(LineBuf), terminator))) {
3100 if ( (!flushing) && (!finished) ) {
3102 StrBufAppendBufPlain(LineBuf, HKEY("\r\n"), 0);
3105 StrBufAppendBufPlain(LineBuf, HKEY("\n"), 0);
3108 /* Unescape SMTP-style input of two dots at the beginning of the line */
3110 (StrLength(LineBuf) == 2) &&
3111 (!strcmp(ChrPtr(LineBuf), "..")))
3113 StrBufCutLeft(LineBuf, 1);
3116 StrBufAppendBuf(Message, LineBuf, 0);
3119 /* if we've hit the max msg length, flush the rest */
3120 if (StrLength(Message) >= maxlen) flushing = 1;
3122 } while (!finished);
3123 FreeStrBuf(&LineBuf);
3129 * Back end function used by CtdlMakeMessage() and similar functions
3131 char *CtdlReadMessageBody(char *terminator, /* token signalling EOT */
3133 size_t maxlen, /* maximum message length */
3134 StrBuf *exist, /* if non-null, append to it;
3135 exist is ALWAYS freed */
3136 int crlf /* CRLF newlines instead of LF */
3141 Message = CtdlReadMessageBodyBuf(terminator,
3147 if (Message == NULL)
3150 return SmashStrBuf(&Message);
3153 struct CtdlMessage *CtdlMakeMessage(
3154 struct ctdluser *author, /* author's user structure */
3155 char *recipient, /* NULL if it's not mail */
3156 char *recp_cc, /* NULL if it's not mail */
3157 char *room, /* room where it's going */
3158 int type, /* see MES_ types in header file */
3159 int format_type, /* variformat, plain text, MIME... */
3160 char *fake_name, /* who we're masquerading as */
3161 char *my_email, /* which of my email addresses to use (empty is ok) */
3162 char *subject, /* Subject (optional) */
3163 char *supplied_euid, /* ...or NULL if this is irrelevant */
3164 char *preformatted_text, /* ...or NULL to read text from client */
3165 char *references /* Thread references */
3168 return CtdlMakeMessageLen(
3169 author, /* author's user structure */
3170 recipient, /* NULL if it's not mail */
3171 (recipient)?strlen(recipient) : 0,
3172 recp_cc, /* NULL if it's not mail */
3173 (recp_cc)?strlen(recp_cc): 0,
3174 room, /* room where it's going */
3175 (room)?strlen(room): 0,
3176 type, /* see MES_ types in header file */
3177 format_type, /* variformat, plain text, MIME... */
3178 fake_name, /* who we're masquerading as */
3179 (fake_name)?strlen(fake_name): 0,
3180 my_email, /* which of my email addresses to use (empty is ok) */
3181 (my_email)?strlen(my_email): 0,
3182 subject, /* Subject (optional) */
3183 (subject)?strlen(subject): 0,
3184 supplied_euid, /* ...or NULL if this is irrelevant */
3185 (supplied_euid)?strlen(supplied_euid):0,
3186 preformatted_text, /* ...or NULL to read text from client */
3187 (preformatted_text)?strlen(preformatted_text) : 0,
3188 references, /* Thread references */
3189 (references)?strlen(references):0);
3194 * Build a binary message to be saved on disk.
3195 * (NOTE: if you supply 'preformatted_text', the buffer you give it
3196 * will become part of the message. This means you are no longer
3197 * responsible for managing that memory -- it will be freed along with
3198 * the rest of the fields when CM_Free() is called.)
3201 struct CtdlMessage *CtdlMakeMessageLen(
3202 struct ctdluser *author, /* author's user structure */
3203 char *recipient, /* NULL if it's not mail */
3205 char *recp_cc, /* NULL if it's not mail */
3207 char *room, /* room where it's going */
3209 int type, /* see MES_ types in header file */
3210 int format_type, /* variformat, plain text, MIME... */
3211 char *fake_name, /* who we're masquerading as */
3213 char *my_email, /* which of my email addresses to use (empty is ok) */
3215 char *subject, /* Subject (optional) */
3217 char *supplied_euid, /* ...or NULL if this is irrelevant */
3219 char *preformatted_text, /* ...or NULL to read text from client */
3221 char *references, /* Thread references */
3225 /* Don't confuse the poor folks if it's not routed mail. * /
3226 char dest_node[256] = "";*/
3229 struct CtdlMessage *msg;
3231 StrBuf *FakeEncAuthor = NULL;
3233 msg = malloc(sizeof(struct CtdlMessage));
3234 memset(msg, 0, sizeof(struct CtdlMessage));
3235 msg->cm_magic = CTDLMESSAGE_MAGIC;
3236 msg->cm_anon_type = type;
3237 msg->cm_format_type = format_type;
3239 if (recipient != NULL) rcplen = striplt(recipient);
3240 if (recp_cc != NULL) cclen = striplt(recp_cc);
3242 /* Path or Return-Path */
3244 CM_SetField(msg, eMessagePath, my_email, myelen);
3246 else if (!IsEmptyStr(author->fullname)) {
3247 CM_SetField(msg, eMessagePath, author->fullname, strlen(author->fullname));
3249 convert_spaces_to_underscores(msg->cm_fields[eMessagePath]);
3251 blen = snprintf(buf, sizeof buf, "%ld", (long)time(NULL));
3252 CM_SetField(msg, eTimestamp, buf, blen);
3255 FakeAuthor = NewStrBufPlain (fake_name, fnlen);
3258 FakeAuthor = NewStrBufPlain (author->fullname, -1);
3260 StrBufRFC2047encode(&FakeEncAuthor, FakeAuthor);
3261 CM_SetAsFieldSB(msg, eAuthor, &FakeEncAuthor);
3262 FreeStrBuf(&FakeAuthor);
3264 if (!!IsEmptyStr(CC->room.QRname)) {
3265 if (CC->room.QRflags & QR_MAILBOX) { /* room */
3266 CM_SetField(msg, eOriginalRoom, &CC->room.QRname[11], strlen(&CC->room.QRname[11]));
3269 CM_SetField(msg, eOriginalRoom, CC->room.QRname, strlen(CC->room.QRname));
3273 CM_SetField(msg, eNodeName, CtdlGetConfigStr("c_nodename"), strlen(CtdlGetConfigStr("c_nodename")));
3274 CM_SetField(msg, eHumanNode, CtdlGetConfigStr("c_humannode"), strlen(CtdlGetConfigStr("c_humannode")));
3277 CM_SetField(msg, eRecipient, recipient, rcplen);
3280 CM_SetField(msg, eCarbonCopY, recp_cc, cclen);
3284 CM_SetField(msg, erFc822Addr, my_email, myelen);
3286 else if ( (author == &CC->user) && (!IsEmptyStr(CC->cs_inet_email)) ) {
3287 CM_SetField(msg, erFc822Addr, CC->cs_inet_email, strlen(CC->cs_inet_email));
3290 if (subject != NULL) {
3292 length = striplt(subject);
3298 while ((subject[i] != '\0') &&
3299 (IsAscii = isascii(subject[i]) != 0 ))
3302 CM_SetField(msg, eMsgSubject, subject, subjlen);
3303 else /* ok, we've got utf8 in the string. */
3306 rfc2047Subj = rfc2047encode(subject, length);
3307 CM_SetAsField(msg, eMsgSubject, &rfc2047Subj, strlen(rfc2047Subj));
3314 CM_SetField(msg, eExclusiveID, supplied_euid, euidlen);
3318 CM_SetField(msg, eWeferences, references, reflen);
3321 if (preformatted_text != NULL) {
3322 CM_SetField(msg, eMesageText, preformatted_text, textlen);
3326 MsgBody = CtdlReadMessageBodyBuf(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
3327 if (MsgBody != NULL) {
3328 CM_SetAsFieldSB(msg, eMesageText, &MsgBody);
3339 * API function to delete messages which match a set of criteria
3340 * (returns the actual number of messages deleted)
3342 int CtdlDeleteMessages(const char *room_name, /* which room */
3343 long *dmsgnums, /* array of msg numbers to be deleted */
3344 int num_dmsgnums, /* number of msgs to be deleted, or 0 for "any" */
3345 char *content_type /* or "" for any. regular expressions expected. */
3348 struct ctdlroom qrbuf;
3349 struct cdbdata *cdbfr;
3350 long *msglist = NULL;
3351 long *dellist = NULL;
3354 int num_deleted = 0;
3356 struct MetaData smi;
3359 int need_to_free_re = 0;
3361 if (content_type) if (!IsEmptyStr(content_type)) {
3362 regcomp(&re, content_type, 0);
3363 need_to_free_re = 1;
3365 syslog(LOG_DEBUG, "msgbase: CtdlDeleteMessages(%s, %d msgs, %s)", room_name, num_dmsgnums, content_type);
3367 /* get room record, obtaining a lock... */
3368 if (CtdlGetRoomLock(&qrbuf, room_name) != 0) {
3369 syslog(LOG_ERR, "msgbase: CtdlDeleteMessages(): Room <%s> not found", room_name);
3370 if (need_to_free_re) regfree(&re);
3371 return(0); /* room not found */
3373 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
3375 if (cdbfr != NULL) {
3376 dellist = malloc(cdbfr->len);
3377 msglist = (long *) cdbfr->ptr;
3378 cdbfr->ptr = NULL; /* CtdlDeleteMessages() now owns this memory */
3379 num_msgs = cdbfr->len / sizeof(long);
3383 int have_contenttype = (content_type != NULL) && !IsEmptyStr(content_type);
3384 int have_delmsgs = (num_dmsgnums == 0) || (dmsgnums == NULL);
3385 int have_more_del = 1;
3387 num_msgs = sort_msglist(msglist, num_msgs);
3388 if (num_dmsgnums > 1)
3389 num_dmsgnums = sort_msglist(dmsgnums, num_dmsgnums);
3392 StrBuf *dbg = NewStrBuf();
3393 for (i = 0; i < num_dmsgnums; i++)
3394 StrBufAppendPrintf(dbg, ", %ld", dmsgnums[i]);
3395 syslog(LOG_DEBUG, "msgbase: Deleting before: %s", ChrPtr(dbg));
3400 while ((i < num_msgs) && (have_more_del)) {
3403 /* Set/clear a bit for each criterion */
3405 /* 0 messages in the list or a null list means that we are
3406 * interested in deleting any messages which meet the other criteria.
3409 delete_this |= 0x01;
3412 while ((i < num_msgs) && (msglist[i] < dmsgnums[j])) i++;
3417 if (msglist[i] == dmsgnums[j]) {
3418 delete_this |= 0x01;
3421 have_more_del = (j < num_dmsgnums);
3424 if (have_contenttype) {
3425 GetMetaData(&smi, msglist[i]);
3426 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) == 0) {
3427 delete_this |= 0x02;
3430 delete_this |= 0x02;
3433 /* Delete message only if all bits are set */
3434 if (delete_this == 0x03) {
3435 dellist[num_deleted++] = msglist[i];
3442 StrBuf *dbg = NewStrBuf();
3443 for (i = 0; i < num_deleted; i++)
3444 StrBufAppendPrintf(dbg, ", %ld", dellist[i]);
3445 syslog(LOG_DEBUG, "msgbase: Deleting: %s", ChrPtr(dbg));
3449 num_msgs = sort_msglist(msglist, num_msgs);
3450 cdb_store(CDB_MSGLISTS, &qrbuf.QRnumber, (int)sizeof(long),
3451 msglist, (int)(num_msgs * sizeof(long)));
3454 qrbuf.QRhighest = msglist[num_msgs - 1];
3456 qrbuf.QRhighest = 0;
3458 CtdlPutRoomLock(&qrbuf);
3460 /* Go through the messages we pulled out of the index, and decrement
3461 * their reference counts by 1. If this is the only room the message
3462 * was in, the reference count will reach zero and the message will
3463 * automatically be deleted from the database. We do this in a
3464 * separate pass because there might be plug-in hooks getting called,
3465 * and we don't want that happening during an S_ROOMS critical
3469 for (i=0; i<num_deleted; ++i) {
3470 PerformDeleteHooks(qrbuf.QRname, dellist[i]);
3472 AdjRefCountList(dellist, num_deleted, -1);
3474 /* Now free the memory we used, and go away. */
3475 if (msglist != NULL) free(msglist);
3476 if (dellist != NULL) free(dellist);
3477 syslog(LOG_DEBUG, "msgbase: %d message(s) deleted", num_deleted);
3478 if (need_to_free_re) regfree(&re);
3479 return (num_deleted);
3484 * GetMetaData() - Get the supplementary record for a message
3486 void GetMetaData(struct MetaData *smibuf, long msgnum)
3488 struct cdbdata *cdbsmi;
3491 memset(smibuf, 0, sizeof(struct MetaData));
3492 smibuf->meta_msgnum = msgnum;
3493 smibuf->meta_refcount = 1; /* Default reference count is 1 */
3495 /* Use the negative of the message number for its supp record index */
3496 TheIndex = (0L - msgnum);
3498 cdbsmi = cdb_fetch(CDB_MSGMAIN, &TheIndex, sizeof(long));
3499 if (cdbsmi == NULL) {
3500 return; /* record not found; leave it alone */
3502 memcpy(smibuf, cdbsmi->ptr,
3503 ((cdbsmi->len > sizeof(struct MetaData)) ?
3504 sizeof(struct MetaData) : cdbsmi->len)
3512 * PutMetaData() - (re)write supplementary record for a message
3514 void PutMetaData(struct MetaData *smibuf)
3518 /* Use the negative of the message number for the metadata db index */
3519 TheIndex = (0L - smibuf->meta_msgnum);
3521 cdb_store(CDB_MSGMAIN,
3522 &TheIndex, (int)sizeof(long),
3523 smibuf, (int)sizeof(struct MetaData)
3529 * Convenience function to process a big block of AdjRefCount() operations
3531 void AdjRefCountList(long *msgnum, long nmsg, int incr)
3535 for (i = 0; i < nmsg; i++) {
3536 AdjRefCount(msgnum[i], incr);
3542 * AdjRefCount - adjust the reference count for a message. We need to delete from disk any message whose reference count reaches zero.
3544 void AdjRefCount(long msgnum, int incr)
3546 struct MetaData smi;
3549 /* This is a *tight* critical section; please keep it that way, as
3550 * it may get called while nested in other critical sections.
3551 * Complicating this any further will surely cause deadlock!
3553 begin_critical_section(S_SUPPMSGMAIN);
3554 GetMetaData(&smi, msgnum);
3555 smi.meta_refcount += incr;
3557 end_critical_section(S_SUPPMSGMAIN);
3558 syslog(LOG_DEBUG, "msgbase: AdjRefCount() msg %ld ref count delta %+d, is now %d", msgnum, incr, smi.meta_refcount);
3560 /* If the reference count is now zero, delete both the message and its metadata record.
3562 if (smi.meta_refcount == 0) {
3563 syslog(LOG_DEBUG, "msgbase: deleting message <%ld>", msgnum);
3565 /* Call delete hooks with NULL room to show it has gone altogether */
3566 PerformDeleteHooks(NULL, msgnum);
3568 /* Remove from message base */
3570 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3571 cdb_delete(CDB_BIGMSGS, &delnum, (int)sizeof(long));
3573 /* Remove metadata record */
3574 delnum = (0L - msgnum);
3575 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3581 * Write a generic object to this room
3583 * Note: this could be much more efficient. Right now we use two temporary
3584 * files, and still pull the message into memory as with all others.
3586 void CtdlWriteObject(char *req_room, /* Room to stuff it in */
3587 char *content_type, /* MIME type of this object */
3588 char *raw_message, /* Data to be written */
3589 off_t raw_length, /* Size of raw_message */
3590 struct ctdluser *is_mailbox, /* Mailbox room? */
3591 int is_binary, /* Is encoding necessary? */
3592 int is_unique, /* Del others of this type? */
3593 unsigned int flags /* Internal save flags */
3596 struct ctdlroom qrbuf;
3597 char roomname[ROOMNAMELEN];
3598 struct CtdlMessage *msg;
3599 StrBuf *encoded_message = NULL;
3601 if (is_mailbox != NULL) {
3602 CtdlMailboxName(roomname, sizeof roomname, is_mailbox, req_room);
3605 safestrncpy(roomname, req_room, sizeof(roomname));
3608 syslog(LOG_DEBUG, "msfbase: raw length is %ld", (long)raw_length);
3611 encoded_message = NewStrBufPlain(NULL, (size_t) (((raw_length * 134) / 100) + 4096 ) );
3614 encoded_message = NewStrBufPlain(NULL, (size_t)(raw_length + 4096));
3617 StrBufAppendBufPlain(encoded_message, HKEY("Content-type: "), 0);
3618 StrBufAppendBufPlain(encoded_message, content_type, -1, 0);
3619 StrBufAppendBufPlain(encoded_message, HKEY("\n"), 0);
3622 StrBufAppendBufPlain(encoded_message, HKEY("Content-transfer-encoding: base64\n\n"), 0);
3625 StrBufAppendBufPlain(encoded_message, HKEY("Content-transfer-encoding: 7bit\n\n"), 0);
3629 StrBufBase64Append(encoded_message, NULL, raw_message, raw_length, 0);
3632 StrBufAppendBufPlain(encoded_message, raw_message, raw_length, 0);
3635 syslog(LOG_DEBUG, "msgbase: allocating");
3636 msg = malloc(sizeof(struct CtdlMessage));
3637 memset(msg, 0, sizeof(struct CtdlMessage));
3638 msg->cm_magic = CTDLMESSAGE_MAGIC;
3639 msg->cm_anon_type = MES_NORMAL;
3640 msg->cm_format_type = 4;
3641 CM_SetField(msg, eAuthor, CC->user.fullname, strlen(CC->user.fullname));
3642 CM_SetField(msg, eOriginalRoom, req_room, strlen(req_room));
3643 CM_SetField(msg, eNodeName, CtdlGetConfigStr("c_nodename"), strlen(CtdlGetConfigStr("c_nodename")));
3644 CM_SetField(msg, eHumanNode, CtdlGetConfigStr("c_humannode"), strlen(CtdlGetConfigStr("c_humannode")));
3645 msg->cm_flags = flags;
3647 CM_SetAsFieldSB(msg, eMesageText, &encoded_message);
3649 /* Create the requested room if we have to. */
3650 if (CtdlGetRoom(&qrbuf, roomname) != 0) {
3651 CtdlCreateRoom(roomname, ( (is_mailbox != NULL) ? 5 : 3 ), "", 0, 1, 0, VIEW_BBS);
3653 /* If the caller specified this object as unique, delete all
3654 * other objects of this type that are currently in the room.
3657 syslog(LOG_DEBUG, "msgbase: deleted %d other msgs of this type",
3658 CtdlDeleteMessages(roomname, NULL, 0, content_type)
3661 /* Now write the data */
3662 CtdlSubmitMsg(msg, NULL, roomname, 0);
3667 /************************************************************************/
3668 /* MODULE INITIALIZATION */
3669 /************************************************************************/
3671 CTDL_MODULE_INIT(msgbase)
3674 FillMsgKeyLookupTable();
3677 /* return our module id for the log */