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>
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 -> eRemoteRoom FIXME no more ignet */
54 NULL, /* D -> eDestination FIXME no more ignet */
55 "exti", /* E -> eXclusivID */
56 "rfca", /* F -> erFc822Addr */
58 "hnod", /* H -> eHumanNode FIXME no more ignet */
59 "msgn", /* I -> emessageId */
60 "jrnl", /* J -> eJournal */
61 "rep2", /* K -> eReplyTo */
62 "list", /* L -> eListID */
63 "text", /* M -> eMesageText */
64 "node", /* N -> eNodeName FIXME no more ignet */
65 "room", /* O -> eOriginalRoom */
66 "path", /* P -> eMessagePath */
68 "rcpt", /* R -> eRecipient */
69 "spec", /* S -> eSpecialField FIXME we might not be using this anymore */
70 "time", /* T -> eTimestamp */
71 "subj", /* U -> eMsgSubject */
72 "nvto", /* V -> eenVelopeTo */
73 "wefw", /* W -> eWeferences */
75 "cccc", /* Y -> eCarbonCopY */
79 HashList *msgKeyLookup = NULL;
81 int GetFieldFromMnemonic(eMsgField *f, const char* c)
84 if (GetHash(msgKeyLookup, c, 4, &v)) {
91 void FillMsgKeyLookupTable(void)
95 msgKeyLookup = NewHash (1, FourHash);
97 for (i=0; i < 91; i++) {
98 if (msgkeys[i] != NULL) {
99 Put(msgKeyLookup, msgkeys[i], 4, (void*)i, reference_free_handler);
104 eMsgField FieldOrder[] = {
105 /* Important fields */
116 /* Semi-important fields */
122 /* G is not used yet, may become virus signature*/
125 /* Q is not used yet */
128 /* X is not used yet */
129 /* Z is not used yet */
136 /* Message text (MUST be last) */
138 /* Not saved to disk:
143 static const long NDiskFields = sizeof(FieldOrder) / sizeof(eMsgField);
146 int CM_IsEmpty(struct CtdlMessage *Msg, eMsgField which)
148 return !((Msg->cm_fields[which] != NULL) && (Msg->cm_fields[which][0] != '\0'));
152 void CM_SetField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
154 if (Msg->cm_fields[which] != NULL) {
155 free (Msg->cm_fields[which]);
157 Msg->cm_fields[which] = malloc(length + 1);
158 memcpy(Msg->cm_fields[which], buf, length);
159 Msg->cm_fields[which][length] = '\0';
160 Msg->cm_lengths[which] = length;
164 void CM_SetFieldLONG(struct CtdlMessage *Msg, eMsgField which, long lvalue)
168 len = snprintf(buf, sizeof(buf), "%ld", lvalue);
169 CM_SetField(Msg, which, buf, len);
173 void CM_CutFieldAt(struct CtdlMessage *Msg, eMsgField WhichToCut, long maxlen)
175 if (Msg->cm_fields[WhichToCut] == NULL)
178 if (Msg->cm_lengths[WhichToCut] > maxlen)
180 Msg->cm_fields[WhichToCut][maxlen] = '\0';
181 Msg->cm_lengths[WhichToCut] = maxlen;
186 void CM_FlushField(struct CtdlMessage *Msg, eMsgField which)
188 if (Msg->cm_fields[which] != NULL)
189 free (Msg->cm_fields[which]);
190 Msg->cm_fields[which] = NULL;
191 Msg->cm_lengths[which] = 0;
195 void CM_Flush(struct CtdlMessage *Msg)
199 if (CM_IsValidMsg(Msg) == 0) {
203 for (i = 0; i < 256; ++i) {
204 CM_FlushField(Msg, i);
209 void CM_CopyField(struct CtdlMessage *Msg, eMsgField WhichToPutTo, eMsgField WhichtToCopy)
212 if (Msg->cm_fields[WhichToPutTo] != NULL) {
213 free (Msg->cm_fields[WhichToPutTo]);
216 if (Msg->cm_fields[WhichtToCopy] != NULL) {
217 len = Msg->cm_lengths[WhichtToCopy];
218 Msg->cm_fields[WhichToPutTo] = malloc(len + 1);
219 memcpy(Msg->cm_fields[WhichToPutTo], Msg->cm_fields[WhichtToCopy], len);
220 Msg->cm_fields[WhichToPutTo][len] = '\0';
221 Msg->cm_lengths[WhichToPutTo] = len;
224 Msg->cm_fields[WhichToPutTo] = NULL;
225 Msg->cm_lengths[WhichToPutTo] = 0;
230 void CM_PrependToField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
232 if (Msg->cm_fields[which] != NULL) {
237 oldmsgsize = Msg->cm_lengths[which] + 1;
238 newmsgsize = length + oldmsgsize;
240 new = malloc(newmsgsize);
241 memcpy(new, buf, length);
242 memcpy(new + length, Msg->cm_fields[which], oldmsgsize);
243 free(Msg->cm_fields[which]);
244 Msg->cm_fields[which] = new;
245 Msg->cm_lengths[which] = newmsgsize - 1;
248 Msg->cm_fields[which] = malloc(length + 1);
249 memcpy(Msg->cm_fields[which], buf, length);
250 Msg->cm_fields[which][length] = '\0';
251 Msg->cm_lengths[which] = length;
256 void CM_SetAsField(struct CtdlMessage *Msg, eMsgField which, char **buf, long length)
258 if (Msg->cm_fields[which] != NULL) {
259 free (Msg->cm_fields[which]);
262 Msg->cm_fields[which] = *buf;
264 Msg->cm_lengths[which] = length;
268 void CM_SetAsFieldSB(struct CtdlMessage *Msg, eMsgField which, StrBuf **buf)
270 if (Msg->cm_fields[which] != NULL) {
271 free (Msg->cm_fields[which]);
274 Msg->cm_lengths[which] = StrLength(*buf);
275 Msg->cm_fields[which] = SmashStrBuf(buf);
279 void CM_GetAsField(struct CtdlMessage *Msg, eMsgField which, char **ret, long *retlen)
281 if (Msg->cm_fields[which] != NULL) {
282 *retlen = Msg->cm_lengths[which];
283 *ret = Msg->cm_fields[which];
284 Msg->cm_fields[which] = NULL;
285 Msg->cm_lengths[which] = 0;
295 * Returns 1 if the supplied pointer points to a valid Citadel message.
296 * If the pointer is NULL or the magic number check fails, returns 0.
298 int CM_IsValidMsg(struct CtdlMessage *msg) {
302 if ((msg->cm_magic) != CTDLMESSAGE_MAGIC) {
303 syslog(LOG_WARNING, "msgbase: CM_IsValidMsg() self-check failed");
310 void CM_FreeContents(struct CtdlMessage *msg)
314 for (i = 0; i < 256; ++i)
315 if (msg->cm_fields[i] != NULL) {
316 free(msg->cm_fields[i]);
317 msg->cm_lengths[i] = 0;
320 msg->cm_magic = 0; /* just in case */
325 * 'Destructor' for struct CtdlMessage
327 void CM_Free(struct CtdlMessage *msg)
329 if (CM_IsValidMsg(msg) == 0) {
330 if (msg != NULL) free (msg);
333 CM_FreeContents(msg);
338 int CM_DupField(eMsgField i, struct CtdlMessage *OrgMsg, struct CtdlMessage *NewMsg)
341 len = OrgMsg->cm_lengths[i];
342 NewMsg->cm_fields[i] = malloc(len + 1);
343 if (NewMsg->cm_fields[i] == NULL) {
346 memcpy(NewMsg->cm_fields[i], OrgMsg->cm_fields[i], len);
347 NewMsg->cm_fields[i][len] = '\0';
348 NewMsg->cm_lengths[i] = len;
353 struct CtdlMessage * CM_Duplicate(struct CtdlMessage *OrgMsg)
356 struct CtdlMessage *NewMsg;
358 if (CM_IsValidMsg(OrgMsg) == 0) {
361 NewMsg = (struct CtdlMessage *)malloc(sizeof(struct CtdlMessage));
362 if (NewMsg == NULL) {
366 memcpy(NewMsg, OrgMsg, sizeof(struct CtdlMessage));
368 memset(&NewMsg->cm_fields, 0, sizeof(char*) * 256);
370 for (i = 0; i < 256; ++i) {
371 if (OrgMsg->cm_fields[i] != NULL) {
372 if (!CM_DupField(i, OrgMsg, NewMsg)) {
383 /* Determine if a given message matches the fields in a message template.
384 * Return 0 for a successful match.
386 int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template) {
389 /* If there aren't any fields in the template, all messages will
392 if (template == NULL) return(0);
394 /* Null messages are bogus. */
395 if (msg == NULL) return(1);
397 for (i='A'; i<='Z'; ++i) {
398 if (template->cm_fields[i] != NULL) {
399 if (msg->cm_fields[i] == NULL) {
400 /* Considered equal if temmplate is empty string */
401 if (IsEmptyStr(template->cm_fields[i])) continue;
404 if ((template->cm_lengths[i] != msg->cm_lengths[i]) ||
405 (strcasecmp(msg->cm_fields[i], template->cm_fields[i])))
410 /* All compares succeeded: we have a match! */
416 * Retrieve the "seen" message list for the current room.
418 void CtdlGetSeen(char *buf, int which_set) {
421 /* Learn about the user and room in question */
422 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
424 if (which_set == ctdlsetseen_seen) {
425 safestrncpy(buf, vbuf.v_seen, SIZ);
427 if (which_set == ctdlsetseen_answered) {
428 safestrncpy(buf, vbuf.v_answered, SIZ);
434 * Manipulate the "seen msgs" string (or other message set strings)
436 void CtdlSetSeen(long *target_msgnums, int num_target_msgnums,
437 int target_setting, int which_set,
438 struct ctdluser *which_user, struct ctdlroom *which_room) {
439 struct cdbdata *cdbfr;
453 char *is_set; /* actually an array of booleans */
455 /* Don't bother doing *anything* if we were passed a list of zero messages */
456 if (num_target_msgnums < 1) {
460 /* If no room was specified, we go with the current room. */
462 which_room = &CC->room;
465 /* If no user was specified, we go with the current user. */
467 which_user = &CC->user;
470 syslog(LOG_DEBUG, "msgbase: CtdlSetSeen(%d msgs starting with %ld, %s, %d) in <%s>",
471 num_target_msgnums, target_msgnums[0],
472 (target_setting ? "SET" : "CLEAR"),
476 /* Learn about the user and room in question */
477 CtdlGetRelationship(&vbuf, which_user, which_room);
479 /* Load the message list */
480 cdbfr = cdb_fetch(CDB_MSGLISTS, &which_room->QRnumber, sizeof(long));
482 msglist = (long *) cdbfr->ptr;
483 cdbfr->ptr = NULL; /* CtdlSetSeen() now owns this memory */
484 num_msgs = cdbfr->len / sizeof(long);
487 return; /* No messages at all? No further action. */
490 is_set = malloc(num_msgs * sizeof(char));
491 memset(is_set, 0, (num_msgs * sizeof(char)) );
493 /* Decide which message set we're manipulating */
495 case ctdlsetseen_seen:
496 vset = NewStrBufPlain(vbuf.v_seen, -1);
498 case ctdlsetseen_answered:
499 vset = NewStrBufPlain(vbuf.v_answered, -1);
506 #if 0 /* This is a special diagnostic section. Do not allow it to run during normal operation. */
507 syslog(LOG_DEBUG, "There are %d messages in the room.\n", num_msgs);
508 for (i=0; i<num_msgs; ++i) {
509 if ((i > 0) && (msglist[i] <= msglist[i-1])) abort();
511 syslog(LOG_DEBUG, "We are twiddling %d of them.\n", num_target_msgnums);
512 for (k=0; k<num_target_msgnums; ++k) {
513 if ((k > 0) && (target_msgnums[k] <= target_msgnums[k-1])) abort();
517 /* Translate the existing sequence set into an array of booleans */
518 setstr = NewStrBuf();
522 while (StrBufExtract_NextToken(setstr, vset, &pvset, ',') >= 0) {
524 StrBufExtract_token(lostr, setstr, 0, ':');
525 if (StrBufNum_tokens(setstr, ':') >= 2) {
526 StrBufExtract_token(histr, setstr, 1, ':');
530 StrBufAppendBuf(histr, lostr, 0);
533 if (!strcmp(ChrPtr(histr), "*")) {
540 for (i = 0; i < num_msgs; ++i) {
541 if ((msglist[i] >= lo) && (msglist[i] <= hi)) {
550 /* Now translate the array of booleans back into a sequence set */
556 for (i=0; i<num_msgs; ++i) {
560 for (k=0; k<num_target_msgnums; ++k) {
561 if (msglist[i] == target_msgnums[k]) {
562 is_seen = target_setting;
566 if ((was_seen == 0) && (is_seen == 1)) {
569 else if ((was_seen == 1) && (is_seen == 0)) {
572 if (StrLength(vset) > 0) {
573 StrBufAppendBufPlain(vset, HKEY(","), 0);
576 StrBufAppendPrintf(vset, "%ld", hi);
579 StrBufAppendPrintf(vset, "%ld:%ld", lo, hi);
583 if ((is_seen) && (i == num_msgs - 1)) {
584 if (StrLength(vset) > 0) {
585 StrBufAppendBufPlain(vset, HKEY(","), 0);
587 if ((i==0) || (was_seen == 0)) {
588 StrBufAppendPrintf(vset, "%ld", msglist[i]);
591 StrBufAppendPrintf(vset, "%ld:%ld", lo, msglist[i]);
599 * We will have to stuff this string back into a 4096 byte buffer, so if it's
600 * larger than that now, truncate it by removing tokens from the beginning.
601 * The limit of 100 iterations is there to prevent an infinite loop in case
602 * something unexpected happens.
604 int number_of_truncations = 0;
605 while ( (StrLength(vset) > SIZ) && (number_of_truncations < 100) ) {
606 StrBufRemove_token(vset, 0, ',');
607 ++number_of_truncations;
611 * If we're truncating the sequence set of messages marked with the 'seen' flag,
612 * we want the earliest messages (the truncated ones) to be marked, not unmarked.
613 * Otherwise messages at the beginning will suddenly appear to be 'unseen'.
615 if ( (which_set == ctdlsetseen_seen) && (number_of_truncations > 0) ) {
617 first_tok = NewStrBuf();
618 StrBufExtract_token(first_tok, vset, 0, ',');
619 StrBufRemove_token(vset, 0, ',');
621 if (StrBufNum_tokens(first_tok, ':') > 1) {
622 StrBufRemove_token(first_tok, 0, ':');
626 new_set = NewStrBuf();
627 StrBufAppendBufPlain(new_set, HKEY("1:"), 0);
628 StrBufAppendBuf(new_set, first_tok, 0);
629 StrBufAppendBufPlain(new_set, HKEY(":"), 0);
630 StrBufAppendBuf(new_set, vset, 0);
633 FreeStrBuf(&first_tok);
637 /* Decide which message set we're manipulating */
639 case ctdlsetseen_seen:
640 safestrncpy(vbuf.v_seen, ChrPtr(vset), sizeof vbuf.v_seen);
642 case ctdlsetseen_answered:
643 safestrncpy(vbuf.v_answered, ChrPtr(vset), sizeof vbuf.v_answered);
649 CtdlSetRelationship(&vbuf, which_user, which_room);
655 * API function to perform an operation for each qualifying message in the
656 * current room. (Returns the number of messages processed.)
658 int CtdlForEachMessage(int mode, long ref, char *search_string,
660 struct CtdlMessage *compare,
661 ForEachMsgCallback CallBack,
666 struct cdbdata *cdbfr;
667 long *msglist = NULL;
669 int num_processed = 0;
672 struct CtdlMessage *msg = NULL;
675 int printed_lastold = 0;
676 int num_search_msgs = 0;
677 long *search_msgs = NULL;
679 int need_to_free_re = 0;
682 if ((content_type) && (!IsEmptyStr(content_type))) {
683 regcomp(&re, content_type, 0);
687 /* Learn about the user and room in question */
688 if (server_shutting_down) {
689 if (need_to_free_re) regfree(&re);
692 CtdlGetUser(&CC->user, CC->curr_user);
694 if (server_shutting_down) {
695 if (need_to_free_re) regfree(&re);
698 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
700 if (server_shutting_down) {
701 if (need_to_free_re) regfree(&re);
705 /* Load the message list */
706 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
708 if (need_to_free_re) regfree(&re);
709 return 0; /* No messages at all? No further action. */
712 msglist = (long *) cdbfr->ptr;
713 num_msgs = cdbfr->len / sizeof(long);
715 cdbfr->ptr = NULL; /* clear this so that cdb_free() doesn't free it */
716 cdb_free(cdbfr); /* we own this memory now */
719 * Now begin the traversal.
721 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
723 /* If the caller is looking for a specific MIME type, filter
724 * out all messages which are not of the type requested.
726 if ((content_type != NULL) && (!IsEmptyStr(content_type))) {
728 /* This call to GetMetaData() sits inside this loop
729 * so that we only do the extra database read per msg
730 * if we need to. Doing the extra read all the time
731 * really kills the server. If we ever need to use
732 * metadata for another search criterion, we need to
733 * move the read somewhere else -- but still be smart
734 * enough to only do the read if the caller has
735 * specified something that will need it.
737 if (server_shutting_down) {
738 if (need_to_free_re) regfree(&re);
742 GetMetaData(&smi, msglist[a]);
744 /* if (strcasecmp(smi.meta_content_type, content_type)) { old non-regex way */
745 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) != 0) {
751 num_msgs = sort_msglist(msglist, num_msgs);
753 /* If a template was supplied, filter out the messages which
754 * don't match. (This could induce some delays!)
757 if (compare != NULL) {
758 for (a = 0; a < num_msgs; ++a) {
759 if (server_shutting_down) {
760 if (need_to_free_re) regfree(&re);
764 msg = CtdlFetchMessage(msglist[a], 1, 1);
766 if (CtdlMsgCmp(msg, compare)) {
775 /* If a search string was specified, get a message list from
776 * the full text index and remove messages which aren't on both
780 * Since the lists are sorted and strictly ascending, and the
781 * output list is guaranteed to be shorter than or equal to the
782 * input list, we overwrite the bottom of the input list. This
783 * eliminates the need to memmove big chunks of the list over and
786 if ( (num_msgs > 0) && (mode == MSGS_SEARCH) && (search_string) ) {
788 /* Call search module via hook mechanism.
789 * NULL means use any search function available.
790 * otherwise replace with a char * to name of search routine
792 CtdlModuleDoSearch(&num_search_msgs, &search_msgs, search_string, "fulltext");
794 if (num_search_msgs > 0) {
798 orig_num_msgs = num_msgs;
800 for (i=0; i<orig_num_msgs; ++i) {
801 for (j=0; j<num_search_msgs; ++j) {
802 if (msglist[i] == search_msgs[j]) {
803 msglist[num_msgs++] = msglist[i];
809 num_msgs = 0; /* No messages qualify */
811 if (search_msgs != NULL) free(search_msgs);
813 /* Now that we've purged messages which don't contain the search
814 * string, treat a MSGS_SEARCH just like a MSGS_ALL from this
821 * Now iterate through the message list, according to the
822 * criteria supplied by the caller.
825 for (a = 0; a < num_msgs; ++a) {
826 if (server_shutting_down) {
827 if (need_to_free_re) regfree(&re);
829 return num_processed;
831 thismsg = msglist[a];
832 if (mode == MSGS_ALL) {
836 is_seen = is_msg_in_sequence_set(
837 vbuf.v_seen, thismsg);
838 if (is_seen) lastold = thismsg;
844 || ((mode == MSGS_OLD) && (is_seen))
845 || ((mode == MSGS_NEW) && (!is_seen))
846 || ((mode == MSGS_LAST) && (a >= (num_msgs - ref)))
847 || ((mode == MSGS_FIRST) && (a < ref))
848 || ((mode == MSGS_GT) && (thismsg > ref))
849 || ((mode == MSGS_LT) && (thismsg < ref))
850 || ((mode == MSGS_EQ) && (thismsg == ref))
853 if ((mode == MSGS_NEW) && (CC->user.flags & US_LASTOLD) && (lastold > 0L) && (printed_lastold == 0) && (!is_seen)) {
855 CallBack(lastold, userdata);
861 CallBack(thismsg, userdata);
866 if (need_to_free_re) regfree(&re);
869 * We cache the most recent msglist in order to do security checks later
871 if (CC->client_socket > 0) {
872 if (CC->cached_msglist != NULL) {
873 free(CC->cached_msglist);
875 CC->cached_msglist = msglist;
876 CC->cached_num_msgs = num_msgs;
882 return num_processed;
887 * memfmout() - Citadel text formatter and paginator.
888 * Although the original purpose of this routine was to format
889 * text to the reader's screen width, all we're really using it
890 * for here is to format text out to 80 columns before sending it
891 * to the client. The client software may reformat it again.
894 char *mptr, /* where are we going to get our text from? */
895 const char *nl /* string to terminate lines with */
898 unsigned char ch = 0;
905 while (ch=*(mptr++), ch != 0) {
908 if (client_write(outbuf, len) == -1) {
909 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
913 if (client_write(nl, nllen) == -1) {
914 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
919 else if (ch == '\r') {
920 /* Ignore carriage returns. Newlines are always LF or CRLF but never CR. */
922 else if (isspace(ch)) {
923 if (column > 72) { /* Beyond 72 columns, break on the next space */
924 if (client_write(outbuf, len) == -1) {
925 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
929 if (client_write(nl, nllen) == -1) {
930 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
943 if (column > 1000) { /* Beyond 1000 columns, break anywhere */
944 if (client_write(outbuf, len) == -1) {
945 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
949 if (client_write(nl, nllen) == -1) {
950 syslog(LOG_ERR, "msgbase: memfmout(): aborting due to write failure");
958 if (client_write(outbuf, len) == -1) {
959 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
962 client_write(nl, nllen);
969 * Callback function for mime parser that simply lists the part
971 void list_this_part(char *name, char *filename, char *partnum, char *disp,
972 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
973 char *cbid, void *cbuserdata)
977 ma = (struct ma_info *)cbuserdata;
978 if (ma->is_ma == 0) {
979 cprintf("part=%s|%s|%s|%s|%s|%ld|%s|%s\n",
993 * Callback function for multipart prefix
995 void list_this_pref(char *name, char *filename, char *partnum, char *disp,
996 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
997 char *cbid, void *cbuserdata)
1001 ma = (struct ma_info *)cbuserdata;
1002 if (!strcasecmp(cbtype, "multipart/alternative")) {
1006 if (ma->is_ma == 0) {
1007 cprintf("pref=%s|%s\n", partnum, cbtype);
1013 * Callback function for multipart sufffix
1015 void list_this_suff(char *name, char *filename, char *partnum, char *disp,
1016 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1017 char *cbid, void *cbuserdata)
1021 ma = (struct ma_info *)cbuserdata;
1022 if (ma->is_ma == 0) {
1023 cprintf("suff=%s|%s\n", partnum, cbtype);
1025 if (!strcasecmp(cbtype, "multipart/alternative")) {
1032 * Callback function for mime parser that opens a section for downloading
1033 * we use serv_files function here:
1035 extern void OpenCmdResult(char *filename, const char *mime_type);
1036 void mime_download(char *name, char *filename, char *partnum, char *disp,
1037 void *content, char *cbtype, char *cbcharset, size_t length,
1038 char *encoding, char *cbid, void *cbuserdata)
1042 /* Silently go away if there's already a download open. */
1043 if (CC->download_fp != NULL)
1047 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
1048 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
1050 CC->download_fp = tmpfile();
1051 if (CC->download_fp == NULL) {
1052 syslog(LOG_EMERG, "msgbase: mime_download() couldn't write: %m");
1053 cprintf("%d cannot open temporary file: %s\n", ERROR + INTERNAL_ERROR, strerror(errno));
1057 rv = fwrite(content, length, 1, CC->download_fp);
1059 syslog(LOG_EMERG, "msgbase: mime_download() Couldn't write: %m");
1060 cprintf("%d unable to write tempfile.\n", ERROR + TOO_BIG);
1061 fclose(CC->download_fp);
1062 CC->download_fp = NULL;
1065 fflush(CC->download_fp);
1066 rewind(CC->download_fp);
1068 OpenCmdResult(filename, cbtype);
1074 * Callback function for mime parser that outputs a section all at once.
1075 * We can specify the desired section by part number *or* content-id.
1077 void mime_spew_section(char *name, char *filename, char *partnum, char *disp,
1078 void *content, char *cbtype, char *cbcharset, size_t length,
1079 char *encoding, char *cbid, void *cbuserdata)
1081 int *found_it = (int *)cbuserdata;
1084 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
1085 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
1088 cprintf("%d %d|-1|%s|%s|%s\n",
1095 client_write(content, length);
1100 struct CtdlMessage *CtdlDeserializeMessage(long msgnum, int with_body, const char *Buffer, long Length)
1102 struct CtdlMessage *ret = NULL;
1104 const char *upper_bound;
1106 cit_uint8_t field_header;
1110 upper_bound = Buffer + Length;
1115 /* Parse the three bytes that begin EVERY message on disk.
1116 * The first is always 0xFF, the on-disk magic number.
1117 * The second is the anonymous/public type byte.
1118 * The third is the format type byte (vari, fixed, or MIME).
1122 syslog(LOG_ERR, "msgbase: message %ld appears to be corrupted", msgnum);
1125 ret = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1126 memset(ret, 0, sizeof(struct CtdlMessage));
1128 ret->cm_magic = CTDLMESSAGE_MAGIC;
1129 ret->cm_anon_type = *mptr++; /* Anon type byte */
1130 ret->cm_format_type = *mptr++; /* Format type byte */
1133 * The rest is zero or more arbitrary fields. Load them in.
1134 * We're done when we encounter either a zero-length field or
1135 * have just processed the 'M' (message text) field.
1138 field_header = '\0';
1141 /* work around possibly buggy messages: */
1142 while (field_header == '\0') {
1143 if (mptr >= upper_bound) {
1146 field_header = *mptr++;
1148 if (mptr >= upper_bound) {
1151 which = field_header;
1154 CM_SetField(ret, which, mptr, len);
1156 mptr += len + 1; /* advance to next field */
1158 } while ((mptr < upper_bound) && (field_header != 'M'));
1165 * Load a message from disk into memory.
1166 * This is used by CtdlOutputMsg() and other fetch functions.
1168 * NOTE: Caller is responsible for freeing the returned CtdlMessage struct
1169 * using the CM_Free(); function.
1171 struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body, int run_msg_hooks)
1173 struct cdbdata *dmsgtext;
1174 struct CtdlMessage *ret = NULL;
1176 syslog(LOG_DEBUG, "msgbase: CtdlFetchMessage(%ld, %d)", msgnum, with_body);
1177 dmsgtext = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
1178 if (dmsgtext == NULL) {
1179 syslog(LOG_ERR, "msgbase: CtdlFetchMessage(%ld, %d) Failed!", msgnum, with_body);
1183 if (dmsgtext->ptr[dmsgtext->len - 1] != '\0') {
1184 syslog(LOG_ERR, "msgbase: CtdlFetchMessage(%ld, %d) Forcefully terminating message!!", msgnum, with_body);
1185 dmsgtext->ptr[dmsgtext->len - 1] = '\0';
1188 ret = CtdlDeserializeMessage(msgnum, with_body, dmsgtext->ptr, dmsgtext->len);
1196 /* Always make sure there's something in the msg text field. If
1197 * it's NULL, the message text is most likely stored separately,
1198 * so go ahead and fetch that. Failing that, just set a dummy
1199 * body so other code doesn't barf.
1201 if ( (CM_IsEmpty(ret, eMesageText)) && (with_body) ) {
1202 dmsgtext = cdb_fetch(CDB_BIGMSGS, &msgnum, sizeof(long));
1203 if (dmsgtext != NULL) {
1204 CM_SetAsField(ret, eMesageText, &dmsgtext->ptr, dmsgtext->len - 1);
1208 if (CM_IsEmpty(ret, eMesageText)) {
1209 CM_SetField(ret, eMesageText, HKEY("\r\n\r\n (no text)\r\n"));
1212 /* Perform "before read" hooks (aborting if any return nonzero) */
1213 if (run_msg_hooks && (PerformMessageHooks(ret, NULL, EVT_BEFOREREAD) > 0)) {
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, 1);
1607 TheMessage = CtdlFetchMessage(msg_num, 1, 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? */
1700 char display_name[256];
1702 /* begin header processing loop for Citadel message format */
1703 safestrncpy(display_name, "<unknown>", sizeof display_name);
1704 if (!CM_IsEmpty(TheMessage, eAuthor)) {
1705 strcpy(buf, TheMessage->cm_fields[eAuthor]);
1706 if (TheMessage->cm_anon_type == MES_ANONONLY) {
1707 safestrncpy(display_name, "****", sizeof display_name);
1709 else if (TheMessage->cm_anon_type == MES_ANONOPT) {
1710 safestrncpy(display_name, "anonymous", sizeof display_name);
1713 safestrncpy(display_name, buf, sizeof display_name);
1715 if ((is_room_aide())
1716 && ((TheMessage->cm_anon_type == MES_ANONONLY)
1717 || (TheMessage->cm_anon_type == MES_ANONOPT))) {
1718 size_t tmp = strlen(display_name);
1719 snprintf(&display_name[tmp],
1720 sizeof display_name - tmp,
1725 /* Don't show Internet address for users on the
1726 * local Citadel network.
1729 if (!CM_IsEmpty(TheMessage, eNodeName) && (haschar(TheMessage->cm_fields[eNodeName], '.') == 0)) {
1733 /* Now spew the header fields in the order we like them. */
1734 for (i=0; i< NDiskFields; ++i) {
1736 Field = FieldOrder[i];
1737 if (Field != eMesageText) {
1738 if ( (!CM_IsEmpty(TheMessage, Field))
1739 && (msgkeys[Field] != NULL) ) {
1740 if ((Field == eenVelopeTo) ||
1741 (Field == eRecipient) ||
1742 (Field == eCarbonCopY)) {
1743 sanitize_truncated_recipient(TheMessage->cm_fields[Field]);
1745 if (Field == eAuthor) {
1746 if (do_proto) cprintf("%s=%s\n",
1750 else if ((Field == erFc822Addr) && (suppress_f)) {
1753 /* Masquerade display name if needed */
1756 cprintf("%s=%s\n", msgkeys[Field], TheMessage->cm_fields[Field]);
1765 void OutputRFC822MsgHeaders(
1766 struct CtdlMessage *TheMessage,
1767 int flags, /* should the bessage be exported clean */
1768 const char *nl, int nlen,
1769 char *mid, long sizeof_mid,
1770 char *suser, long sizeof_suser,
1771 char *luser, long sizeof_luser,
1772 char *fuser, long sizeof_fuser,
1773 char *snode, long sizeof_snode)
1775 char datestamp[100];
1776 int subject_found = 0;
1783 for (i = 0; i < NDiskFields; ++i) {
1784 if (TheMessage->cm_fields[FieldOrder[i]]) {
1785 mptr = mpptr = TheMessage->cm_fields[FieldOrder[i]];
1786 switch (FieldOrder[i]) {
1788 safestrncpy(luser, mptr, sizeof_luser);
1789 safestrncpy(suser, mptr, sizeof_suser);
1792 if ((flags & QP_EADDR) != 0) {
1793 mptr = qp_encode_email_addrs(mptr);
1795 sanitize_truncated_recipient(mptr);
1796 cprintf("CC: %s%s", mptr, nl);
1799 cprintf("Return-Path: %s%s", mptr, nl);
1802 cprintf("List-ID: %s%s", mptr, nl);
1805 if ((flags & QP_EADDR) != 0)
1806 mptr = qp_encode_email_addrs(mptr);
1808 while ((*hptr != '\0') && isspace(*hptr))
1810 if (!IsEmptyStr(hptr))
1811 cprintf("Envelope-To: %s%s", hptr, nl);
1814 cprintf("Subject: %s%s", mptr, nl);
1818 safestrncpy(mid, mptr, sizeof_mid);
1821 safestrncpy(fuser, mptr, sizeof_fuser);
1823 safestrncpy(snode, mptr, sizeof_snode);
1826 if (haschar(mptr, '@') == 0) {
1827 sanitize_truncated_recipient(mptr);
1828 cprintf("To: %s@%s", mptr, CtdlGetConfigStr("c_fqdn"));
1832 if ((flags & QP_EADDR) != 0) {
1833 mptr = qp_encode_email_addrs(mptr);
1835 sanitize_truncated_recipient(mptr);
1836 cprintf("To: %s", mptr);
1841 datestring(datestamp, sizeof datestamp, atol(mptr), DATESTRING_RFC822);
1842 cprintf("Date: %s%s", datestamp, nl);
1845 cprintf("References: ");
1846 k = num_tokens(mptr, '|');
1847 for (j=0; j<k; ++j) {
1848 extract_token(buf, mptr, j, '|', sizeof buf);
1849 cprintf("<%s>", buf);
1860 while ((*hptr != '\0') && isspace(*hptr))
1862 if (!IsEmptyStr(hptr))
1863 cprintf("Reply-To: %s%s", mptr, nl);
1879 /* these don't map to mime message headers. */
1882 if (mptr != mpptr) {
1887 if (subject_found == 0) {
1888 cprintf("Subject: (no subject)%s", nl);
1893 void Dump_RFC822HeadersBody(
1894 struct CtdlMessage *TheMessage,
1895 int headers_only, /* eschew the message body? */
1896 int flags, /* should the bessage be exported clean? */
1897 const char *nl, int nlen)
1899 cit_uint8_t prev_ch;
1901 const char *StartOfText = StrBufNOTNULL;
1904 int nllen = strlen(nl);
1908 mptr = TheMessage->cm_fields[eMesageText];
1911 while (*mptr != '\0') {
1912 if (*mptr == '\r') {
1919 eoh = (*(mptr+1) == '\r') && (*(mptr+2) == '\n');
1921 eoh = *(mptr+1) == '\n';
1925 StartOfText = strchr(StartOfText, '\n');
1926 StartOfText = strchr(StartOfText, '\n');
1929 if (((headers_only == HEADERS_NONE) && (mptr >= StartOfText)) ||
1930 ((headers_only == HEADERS_ONLY) && (mptr < StartOfText)) ||
1931 ((headers_only != HEADERS_NONE) &&
1932 (headers_only != HEADERS_ONLY))
1934 if (*mptr == '\n') {
1935 memcpy(&outbuf[outlen], nl, nllen);
1937 outbuf[outlen] = '\0';
1940 outbuf[outlen++] = *mptr;
1944 if (flags & ESC_DOT) {
1945 if ((prev_ch == '\n') && (*mptr == '.') && ((*(mptr+1) == '\r') || (*(mptr+1) == '\n'))) {
1946 outbuf[outlen++] = '.';
1951 if (outlen > 1000) {
1952 if (client_write(outbuf, outlen) == -1) {
1953 syslog(LOG_ERR, "msgbase: Dump_RFC822HeadersBody() aborting due to write failure");
1956 lfSent = (outbuf[outlen - 1] == '\n');
1961 client_write(outbuf, outlen);
1962 lfSent = (outbuf[outlen - 1] == '\n');
1965 client_write(nl, nlen);
1969 /* If the format type on disk is 1 (fixed-format), then we want
1970 * everything to be output completely literally ... regardless of
1971 * what message transfer format is in use.
1973 void DumpFormatFixed(
1974 struct CtdlMessage *TheMessage,
1975 int mode, /* how would you like that message? */
1976 const char *nl, int nllen)
1984 mptr = TheMessage->cm_fields[eMesageText];
1986 if (mode == MT_MIME) {
1987 cprintf("Content-type: text/plain\n\n");
1991 while (ch = *mptr++, ch > 0) {
1995 if ((buflen > 250) && (!xlline)){
1999 while ((buflen > 0) &&
2000 (!isspace(buf[buflen])))
2006 mptr -= tbuflen - buflen;
2012 /* if we reach the outer bounds of our buffer, abort without respect for what we purge. */
2013 if (xlline && ((isspace(ch)) || (buflen > SIZ - nllen - 2))) {
2018 memcpy (&buf[buflen], nl, nllen);
2022 if (client_write(buf, buflen) == -1) {
2023 syslog(LOG_ERR, "msgbase: DumpFormatFixed() aborting due to write failure");
2035 if (!IsEmptyStr(buf)) {
2036 cprintf("%s%s", buf, nl);
2042 * Get a message off disk. (returns om_* values found in msgbase.h)
2044 int CtdlOutputPreLoadedMsg(
2045 struct CtdlMessage *TheMessage,
2046 int mode, /* how would you like that message? */
2047 int headers_only, /* eschew the message body? */
2048 int do_proto, /* do Citadel protocol responses? */
2049 int crlf, /* Use CRLF newlines instead of LF? */
2050 int flags /* should the bessage be exported clean? */
2053 const char *nl; /* newline string */
2057 /* Buffers needed for RFC822 translation. These are all filled
2058 * using functions that are bounds-checked, and therefore we can
2059 * make them substantially smaller than SIZ.
2067 syslog(LOG_DEBUG, "msgbase: CtdlOutputPreLoadedMsg(TheMessage=%s, %d, %d, %d, %d",
2068 ((TheMessage == NULL) ? "NULL" : "not null"),
2069 mode, headers_only, do_proto, crlf
2072 strcpy(mid, "unknown");
2073 nl = (crlf ? "\r\n" : "\n");
2074 nlen = crlf ? 2 : 1;
2076 if (!CM_IsValidMsg(TheMessage)) {
2077 syslog(LOG_ERR, "msgbase: error; invalid preloaded message for output");
2078 return(om_no_such_msg);
2081 /* Suppress envelope recipients if required to avoid disclosing BCC addresses.
2082 * Pad it with spaces in order to avoid changing the RFC822 length of the message.
2084 if ( (flags & SUPPRESS_ENV_TO) && (!CM_IsEmpty(TheMessage, eenVelopeTo)) ) {
2085 memset(TheMessage->cm_fields[eenVelopeTo], ' ', TheMessage->cm_lengths[eenVelopeTo]);
2088 /* Are we downloading a MIME component? */
2089 if (mode == MT_DOWNLOAD) {
2090 if (TheMessage->cm_format_type != FMT_RFC822) {
2092 cprintf("%d This is not a MIME message.\n",
2093 ERROR + ILLEGAL_VALUE);
2094 } else if (CC->download_fp != NULL) {
2095 if (do_proto) cprintf(
2096 "%d You already have a download open.\n",
2097 ERROR + RESOURCE_BUSY);
2099 /* Parse the message text component */
2100 mime_parser(CM_RANGE(TheMessage, eMesageText),
2101 *mime_download, NULL, NULL, NULL, 0);
2102 /* If there's no file open by this time, the requested
2103 * section wasn't found, so print an error
2105 if (CC->download_fp == NULL) {
2106 if (do_proto) cprintf(
2107 "%d Section %s not found.\n",
2108 ERROR + FILE_NOT_FOUND,
2109 CC->download_desired_section);
2112 return((CC->download_fp != NULL) ? om_ok : om_mime_error);
2115 /* MT_SPEW_SECTION is like MT_DOWNLOAD except it outputs the whole MIME part
2116 * in a single server operation instead of opening a download file.
2118 if (mode == MT_SPEW_SECTION) {
2119 if (TheMessage->cm_format_type != FMT_RFC822) {
2121 cprintf("%d This is not a MIME message.\n",
2122 ERROR + ILLEGAL_VALUE);
2124 /* Parse the message text component */
2127 mime_parser(CM_RANGE(TheMessage, eMesageText),
2128 *mime_spew_section, NULL, NULL, (void *)&found_it, 0);
2129 /* If section wasn't found, print an error
2132 if (do_proto) cprintf(
2133 "%d Section %s not found.\n",
2134 ERROR + FILE_NOT_FOUND,
2135 CC->download_desired_section);
2138 return((CC->download_fp != NULL) ? om_ok : om_mime_error);
2141 /* now for the user-mode message reading loops */
2142 if (do_proto) cprintf("%d msg:\n", LISTING_FOLLOWS);
2144 /* Does the caller want to skip the headers? */
2145 if (headers_only == HEADERS_NONE) goto START_TEXT;
2147 /* Tell the client which format type we're using. */
2148 if ( (mode == MT_CITADEL) && (do_proto) ) {
2149 cprintf("type=%d\n", TheMessage->cm_format_type);
2152 /* nhdr=yes means that we're only displaying headers, no body */
2153 if ( (TheMessage->cm_anon_type == MES_ANONONLY)
2154 && ((mode == MT_CITADEL) || (mode == MT_MIME))
2157 cprintf("nhdr=yes\n");
2160 if ((mode == MT_CITADEL) || (mode == MT_MIME))
2161 OutputCtdlMsgHeaders(TheMessage, do_proto);
2164 /* begin header processing loop for RFC822 transfer format */
2168 memcpy(snode, CtdlGetConfigStr("c_nodename"), strlen(CtdlGetConfigStr("c_nodename")) + 1);
2169 if (mode == MT_RFC822)
2170 OutputRFC822MsgHeaders(
2175 suser, sizeof(suser),
2176 luser, sizeof(luser),
2177 fuser, sizeof(fuser),
2178 snode, sizeof(snode)
2182 for (i=0; !IsEmptyStr(&suser[i]); ++i) {
2183 suser[i] = tolower(suser[i]);
2184 if (!isalnum(suser[i])) suser[i]='_';
2187 if (mode == MT_RFC822) {
2188 if (!strcasecmp(snode, NODENAME)) {
2189 safestrncpy(snode, FQDN, sizeof snode);
2192 /* Construct a fun message id */
2193 cprintf("Message-ID: <%s", mid);/// todo: this possibly breaks threadding mails.
2194 if (strchr(mid, '@')==NULL) {
2195 cprintf("@%s", snode);
2199 if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONONLY)) {
2200 cprintf("From: \"----\" <x@x.org>%s", nl);
2202 else if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONOPT)) {
2203 cprintf("From: \"anonymous\" <x@x.org>%s", nl);
2205 else if (!IsEmptyStr(fuser)) {
2206 cprintf("From: \"%s\" <%s>%s", luser, fuser, nl);
2209 cprintf("From: \"%s\" <%s@%s>%s", luser, suser, snode, nl);
2212 /* Blank line signifying RFC822 end-of-headers */
2213 if (TheMessage->cm_format_type != FMT_RFC822) {
2218 /* end header processing loop ... at this point, we're in the text */
2220 if (headers_only == HEADERS_FAST) goto DONE;
2222 /* Tell the client about the MIME parts in this message */
2223 if (TheMessage->cm_format_type == FMT_RFC822) {
2224 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2225 memset(&ma, 0, sizeof(struct ma_info));
2226 mime_parser(CM_RANGE(TheMessage, eMesageText),
2227 (do_proto ? *list_this_part : NULL),
2228 (do_proto ? *list_this_pref : NULL),
2229 (do_proto ? *list_this_suff : NULL),
2232 else if (mode == MT_RFC822) { /* unparsed RFC822 dump */
2233 Dump_RFC822HeadersBody(
2242 if (headers_only == HEADERS_ONLY) {
2246 /* signify start of msg text */
2247 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2248 if (do_proto) cprintf("text\n");
2251 if (TheMessage->cm_format_type == FMT_FIXED)
2254 mode, /* how would you like that message? */
2257 /* If the message on disk is format 0 (Citadel vari-format), we
2258 * output using the formatter at 80 columns. This is the final output
2259 * form if the transfer format is RFC822, but if the transfer format
2260 * is Citadel proprietary, it'll still work, because the indentation
2261 * for new paragraphs is correct and the client will reformat the
2262 * message to the reader's screen width.
2264 if (TheMessage->cm_format_type == FMT_CITADEL) {
2265 if (mode == MT_MIME) {
2266 cprintf("Content-type: text/x-citadel-variformat\n\n");
2268 memfmout(TheMessage->cm_fields[eMesageText], nl);
2271 /* If the message on disk is format 4 (MIME), we've gotta hand it
2272 * off to the MIME parser. The client has already been told that
2273 * this message is format 1 (fixed format), so the callback function
2274 * we use will display those parts as-is.
2276 if (TheMessage->cm_format_type == FMT_RFC822) {
2277 memset(&ma, 0, sizeof(struct ma_info));
2279 if (mode == MT_MIME) {
2280 ma.use_fo_hooks = 0;
2281 strcpy(ma.chosen_part, "1");
2282 ma.chosen_pref = 9999;
2283 ma.dont_decode = CC->msg4_dont_decode;
2284 mime_parser(CM_RANGE(TheMessage, eMesageText),
2285 *choose_preferred, *fixed_output_pre,
2286 *fixed_output_post, (void *)&ma, 1);
2287 mime_parser(CM_RANGE(TheMessage, eMesageText),
2288 *output_preferred, NULL, NULL, (void *)&ma, 1);
2291 ma.use_fo_hooks = 1;
2292 mime_parser(CM_RANGE(TheMessage, eMesageText),
2293 *fixed_output, *fixed_output_pre,
2294 *fixed_output_post, (void *)&ma, 0);
2299 DONE: /* now we're done */
2300 if (do_proto) cprintf("000\n");
2305 * Save one or more message pointers into a specified room
2306 * (Returns 0 for success, nonzero for failure)
2307 * roomname may be NULL to use the current room
2309 * Note that the 'supplied_msg' field may be set to NULL, in which case
2310 * the message will be fetched from disk, by number, if we need to perform
2311 * replication checks. This adds an additional database read, so if the
2312 * caller already has the message in memory then it should be supplied. (Obviously
2313 * this mode of operation only works if we're saving a single message.)
2315 int CtdlSaveMsgPointersInRoom(char *roomname, long newmsgidlist[], int num_newmsgs,
2316 int do_repl_check, struct CtdlMessage *supplied_msg, int suppress_refcount_adj
2319 char hold_rm[ROOMNAMELEN];
2320 struct cdbdata *cdbfr;
2323 long highest_msg = 0L;
2326 struct CtdlMessage *msg = NULL;
2328 long *msgs_to_be_merged = NULL;
2329 int num_msgs_to_be_merged = 0;
2332 "msgbase: CtdlSaveMsgPointersInRoom(room=%s, num_msgs=%d, repl=%d, suppress_rca=%d)",
2333 roomname, num_newmsgs, do_repl_check, suppress_refcount_adj
2336 strcpy(hold_rm, CC->room.QRname);
2339 if (newmsgidlist == NULL) return(ERROR + INTERNAL_ERROR);
2340 if (num_newmsgs < 1) return(ERROR + INTERNAL_ERROR);
2341 if (num_newmsgs > 1) supplied_msg = NULL;
2343 /* Now the regular stuff */
2344 if (CtdlGetRoomLock(&CC->room,
2345 ((roomname != NULL) ? roomname : CC->room.QRname) )
2347 syslog(LOG_ERR, "msgbase: no such room <%s>", roomname);
2348 return(ERROR + ROOM_NOT_FOUND);
2352 msgs_to_be_merged = malloc(sizeof(long) * num_newmsgs);
2353 num_msgs_to_be_merged = 0;
2356 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
2357 if (cdbfr == NULL) {
2361 msglist = (long *) cdbfr->ptr;
2362 cdbfr->ptr = NULL; /* CtdlSaveMsgPointerInRoom() now owns this memory */
2363 num_msgs = cdbfr->len / sizeof(long);
2368 /* Create a list of msgid's which were supplied by the caller, but do
2369 * not already exist in the target room. It is absolutely taboo to
2370 * have more than one reference to the same message in a room.
2372 for (i=0; i<num_newmsgs; ++i) {
2374 if (num_msgs > 0) for (j=0; j<num_msgs; ++j) {
2375 if (msglist[j] == newmsgidlist[i]) {
2380 msgs_to_be_merged[num_msgs_to_be_merged++] = newmsgidlist[i];
2384 syslog(LOG_DEBUG, "msgbase: %d unique messages to be merged", num_msgs_to_be_merged);
2387 * Now merge the new messages
2389 msglist = realloc(msglist, (sizeof(long) * (num_msgs + num_msgs_to_be_merged)) );
2390 if (msglist == NULL) {
2391 syslog(LOG_ALERT, "msgbase: ERROR; can't realloc message list!");
2392 free(msgs_to_be_merged);
2393 return (ERROR + INTERNAL_ERROR);
2395 memcpy(&msglist[num_msgs], msgs_to_be_merged, (sizeof(long) * num_msgs_to_be_merged) );
2396 num_msgs += num_msgs_to_be_merged;
2398 /* Sort the message list, so all the msgid's are in order */
2399 num_msgs = sort_msglist(msglist, num_msgs);
2401 /* Determine the highest message number */
2402 highest_msg = msglist[num_msgs - 1];
2404 /* Write it back to disk. */
2405 cdb_store(CDB_MSGLISTS, &CC->room.QRnumber, (int)sizeof(long),
2406 msglist, (int)(num_msgs * sizeof(long)));
2408 /* Free up the memory we used. */
2411 /* Update the highest-message pointer and unlock the room. */
2412 CC->room.QRhighest = highest_msg;
2413 CtdlPutRoomLock(&CC->room);
2415 /* Perform replication checks if necessary */
2416 if ( (DoesThisRoomNeedEuidIndexing(&CC->room)) && (do_repl_check) ) {
2417 syslog(LOG_DEBUG, "msgbase: CtdlSaveMsgPointerInRoom() doing repl checks");
2419 for (i=0; i<num_msgs_to_be_merged; ++i) {
2420 msgid = msgs_to_be_merged[i];
2422 if (supplied_msg != NULL) {
2426 msg = CtdlFetchMessage(msgid, 0, 1);
2430 ReplicationChecks(msg);
2432 /* If the message has an Exclusive ID, index that... */
2433 if (!CM_IsEmpty(msg, eExclusiveID)) {
2434 index_message_by_euid(msg->cm_fields[eExclusiveID], &CC->room, msgid);
2437 /* Free up the memory we may have allocated */
2438 if (msg != supplied_msg) {
2447 syslog(LOG_DEBUG, "msgbase: CtdlSaveMsgPointerInRoom() skips repl checks");
2450 /* Submit this room for processing by hooks */
2451 PerformRoomHooks(&CC->room);
2453 /* Go back to the room we were in before we wandered here... */
2454 CtdlGetRoom(&CC->room, hold_rm);
2456 /* Bump the reference count for all messages which were merged */
2457 if (!suppress_refcount_adj) {
2458 AdjRefCountList(msgs_to_be_merged, num_msgs_to_be_merged, +1);
2461 /* Free up memory... */
2462 if (msgs_to_be_merged != NULL) {
2463 free(msgs_to_be_merged);
2466 /* Return success. */
2472 * This is the same as CtdlSaveMsgPointersInRoom() but it only accepts
2475 int CtdlSaveMsgPointerInRoom(char *roomname, long msgid,
2476 int do_repl_check, struct CtdlMessage *supplied_msg)
2478 return CtdlSaveMsgPointersInRoom(roomname, &msgid, 1, do_repl_check, supplied_msg, 0);
2485 * Message base operation to save a new message to the message store
2486 * (returns new message number)
2488 * This is the back end for CtdlSubmitMsg() and should not be directly
2489 * called by server-side modules.
2492 long CtdlSaveThisMessage(struct CtdlMessage *msg, long msgid, int Reply) {
2500 * If the message is big, set its body aside for storage elsewhere
2501 * and we hide the message body from the serializer
2503 if (!CM_IsEmpty(msg, eMesageText) && msg->cm_lengths[eMesageText] > BIGMSG)
2506 holdM = msg->cm_fields[eMesageText];
2507 msg->cm_fields[eMesageText] = NULL;
2508 holdMLen = msg->cm_lengths[eMesageText];
2509 msg->cm_lengths[eMesageText] = 0;
2512 /* Serialize our data structure for storage in the database */
2513 CtdlSerializeMessage(&smr, msg);
2516 /* put the message body back into the message */
2517 msg->cm_fields[eMesageText] = holdM;
2518 msg->cm_lengths[eMesageText] = holdMLen;
2523 cprintf("%d Unable to serialize message\n",
2524 ERROR + INTERNAL_ERROR);
2527 syslog(LOG_ERR, "msgbase: CtdlSaveMessage() unable to serialize message");
2533 /* Write our little bundle of joy into the message base */
2534 retval = cdb_store(CDB_MSGMAIN, &msgid, (int)sizeof(long),
2537 syslog(LOG_ERR, "msgbase: can't store message %ld: %ld", msgid, retval);
2541 retval = cdb_store(CDB_BIGMSGS,
2548 syslog(LOG_ERR, "msgbase: failed to store message body for msgid %ld: %ld", msgid, retval);
2553 /* Free the memory we used for the serialized message */
2559 long send_message(struct CtdlMessage *msg) {
2565 /* Get a new message number */
2566 newmsgid = get_new_message_number();
2568 /* Generate an ID if we don't have one already */
2569 if (CM_IsEmpty(msg, emessageId)) {
2570 msgidbuflen = snprintf(msgidbuf, sizeof msgidbuf, "%08lX-%08lX@%s",
2571 (long unsigned int) time(NULL),
2572 (long unsigned int) newmsgid,
2573 CtdlGetConfigStr("c_fqdn")
2576 CM_SetField(msg, emessageId, msgidbuf, msgidbuflen);
2579 retval = CtdlSaveThisMessage(msg, newmsgid, 1);
2585 /* Return the *local* message ID to the caller
2586 * (even if we're storing an incoming network message)
2594 * Serialize a struct CtdlMessage into the format used on disk and network.
2596 * This function loads up a "struct ser_ret" (defined in server.h) which
2597 * contains the length of the serialized message and a pointer to the
2598 * serialized message in memory. THE LATTER MUST BE FREED BY THE CALLER.
2600 void CtdlSerializeMessage(struct ser_ret *ret, /* return values */
2601 struct CtdlMessage *msg) /* unserialized msg */
2607 * Check for valid message format
2609 if (CM_IsValidMsg(msg) == 0) {
2610 syslog(LOG_ERR, "msgbase: CtdlSerializeMessage() aborting due to invalid message");
2617 for (i=0; i < NDiskFields; ++i)
2618 if (msg->cm_fields[FieldOrder[i]] != NULL)
2619 ret->len += msg->cm_lengths[FieldOrder[i]] + 2;
2621 ret->ser = malloc(ret->len);
2622 if (ret->ser == NULL) {
2623 syslog(LOG_ERR, "msgbase: CtdlSerializeMessage() malloc(%ld) failed: %m", (long)ret->len);
2630 ret->ser[1] = msg->cm_anon_type;
2631 ret->ser[2] = msg->cm_format_type;
2634 for (i=0; i < NDiskFields; ++i)
2635 if (msg->cm_fields[FieldOrder[i]] != NULL)
2637 ret->ser[wlen++] = (char)FieldOrder[i];
2639 memcpy(&ret->ser[wlen],
2640 msg->cm_fields[FieldOrder[i]],
2641 msg->cm_lengths[FieldOrder[i]] + 1);
2643 wlen = wlen + msg->cm_lengths[FieldOrder[i]] + 1;
2646 if (ret->len != wlen) {
2647 syslog(LOG_ERR, "msgbase: ERROR; len=%ld wlen=%ld", (long)ret->len, (long)wlen);
2655 * Check to see if any messages already exist in the current room which
2656 * carry the same Exclusive ID as this one. If any are found, delete them.
2658 void ReplicationChecks(struct CtdlMessage *msg) {
2659 long old_msgnum = (-1L);
2661 if (DoesThisRoomNeedEuidIndexing(&CC->room) == 0) return;
2663 syslog(LOG_DEBUG, "msgbase: performing replication checks in <%s>", CC->room.QRname);
2665 /* No exclusive id? Don't do anything. */
2666 if (msg == NULL) return;
2667 if (CM_IsEmpty(msg, eExclusiveID)) return;
2669 /*syslog(LOG_DEBUG, "msgbase: exclusive ID: <%s> for room <%s>",
2670 msg->cm_fields[eExclusiveID], CC->room.QRname);*/
2672 old_msgnum = CtdlLocateMessageByEuid(msg->cm_fields[eExclusiveID], &CC->room);
2673 if (old_msgnum > 0L) {
2674 syslog(LOG_DEBUG, "msgbase: ReplicationChecks() replacing message %ld", old_msgnum);
2675 CtdlDeleteMessages(CC->room.QRname, &old_msgnum, 1, "");
2682 * Save a message to disk and submit it into the delivery system.
2684 long CtdlSubmitMsg(struct CtdlMessage *msg, /* message to save */
2685 recptypes *recps, /* recipients (if mail) */
2686 const char *force, /* force a particular room? */
2687 int flags /* should the message be exported clean? */
2690 char hold_rm[ROOMNAMELEN];
2691 char actual_rm[ROOMNAMELEN];
2692 char force_room[ROOMNAMELEN];
2693 char content_type[SIZ]; /* We have to learn this */
2694 char recipient[SIZ];
2695 char bounce_to[1024];
2698 const char *mptr = NULL;
2699 struct ctdluser userbuf;
2701 struct MetaData smi;
2702 char *collected_addresses = NULL;
2703 struct addresses_to_be_filed *aptr = NULL;
2704 StrBuf *saved_rfc822_version = NULL;
2705 int qualified_for_journaling = 0;
2707 syslog(LOG_DEBUG, "msgbase: CtdlSubmitMsg() called");
2708 if (CM_IsValidMsg(msg) == 0) return(-1); /* self check */
2710 /* If this message has no timestamp, we take the liberty of
2711 * giving it one, right now.
2713 if (CM_IsEmpty(msg, eTimestamp)) {
2714 CM_SetFieldLONG(msg, eTimestamp, time(NULL));
2717 /* If this message has no path, we generate one.
2719 if (CM_IsEmpty(msg, eMessagePath)) {
2720 if (!CM_IsEmpty(msg, eAuthor)) {
2721 CM_CopyField(msg, eMessagePath, eAuthor);
2722 for (a=0; !IsEmptyStr(&msg->cm_fields[eMessagePath][a]); ++a) {
2723 if (isspace(msg->cm_fields[eMessagePath][a])) {
2724 msg->cm_fields[eMessagePath][a] = ' ';
2729 CM_SetField(msg, eMessagePath, HKEY("unknown"));
2733 if (force == NULL) {
2734 force_room[0] = '\0';
2737 strcpy(force_room, force);
2740 /* Learn about what's inside, because it's what's inside that counts */
2741 if (CM_IsEmpty(msg, eMesageText)) {
2742 syslog(LOG_ERR, "msgbase: ERROR; attempt to save message with NULL body");
2746 switch (msg->cm_format_type) {
2748 strcpy(content_type, "text/x-citadel-variformat");
2751 strcpy(content_type, "text/plain");
2754 strcpy(content_type, "text/plain");
2755 mptr = bmstrcasestr(msg->cm_fields[eMesageText], "Content-type:");
2758 safestrncpy(content_type, &mptr[13], sizeof content_type);
2759 striplt(content_type);
2760 aptr = content_type;
2761 while (!IsEmptyStr(aptr)) {
2773 /* Goto the correct room */
2774 room = (recps) ? CC->room.QRname : SENTITEMS;
2775 syslog(LOG_DEBUG, "msgbase: selected room %s", room);
2776 strcpy(hold_rm, CC->room.QRname);
2777 strcpy(actual_rm, CC->room.QRname);
2778 if (recps != NULL) {
2779 strcpy(actual_rm, SENTITEMS);
2782 /* If the user is a twit, move to the twit room for posting */
2784 if (CC->user.axlevel == AxProbU) {
2785 strcpy(hold_rm, actual_rm);
2786 strcpy(actual_rm, CtdlGetConfigStr("c_twitroom"));
2787 syslog(LOG_DEBUG, "msgbase: diverting to twit room");
2791 /* ...or if this message is destined for Aide> then go there. */
2792 if (!IsEmptyStr(force_room)) {
2793 strcpy(actual_rm, force_room);
2796 syslog(LOG_DEBUG, "msgbase: final selection: %s (%s)", actual_rm, room);
2797 if (strcasecmp(actual_rm, CC->room.QRname)) {
2798 /* CtdlGetRoom(&CC->room, actual_rm); */
2799 CtdlUserGoto(actual_rm, 0, 1, NULL, NULL, NULL, NULL);
2803 * If this message has no O (room) field, generate one.
2805 if (CM_IsEmpty(msg, eOriginalRoom) && !IsEmptyStr(CC->room.QRname)) {
2806 CM_SetField(msg, eOriginalRoom, CC->room.QRname, strlen(CC->room.QRname));
2809 /* Perform "before save" hooks (aborting if any return nonzero) */
2810 syslog(LOG_DEBUG, "msgbase: performing before-save hooks");
2811 if (PerformMessageHooks(msg, recps, EVT_BEFORESAVE) > 0) return(-3);
2814 * If this message has an Exclusive ID, and the room is replication
2815 * checking enabled, then do replication checks.
2817 if (DoesThisRoomNeedEuidIndexing(&CC->room)) {
2818 ReplicationChecks(msg);
2821 /* Save it to disk */
2822 syslog(LOG_DEBUG, "msgbase: saving to disk");
2823 newmsgid = send_message(msg);
2824 if (newmsgid <= 0L) return(-5);
2826 /* Write a supplemental message info record. This doesn't have to
2827 * be a critical section because nobody else knows about this message
2830 syslog(LOG_DEBUG, "msgbase: creating metadata record");
2831 memset(&smi, 0, sizeof(struct MetaData));
2832 smi.meta_msgnum = newmsgid;
2833 smi.meta_refcount = 0;
2834 safestrncpy(smi.meta_content_type, content_type,
2835 sizeof smi.meta_content_type);
2838 * Measure how big this message will be when rendered as RFC822.
2839 * We do this for two reasons:
2840 * 1. We need the RFC822 length for the new metadata record, so the
2841 * POP and IMAP services don't have to calculate message lengths
2842 * while the user is waiting (multiplied by potentially hundreds
2843 * or thousands of messages).
2844 * 2. If journaling is enabled, we will need an RFC822 version of the
2845 * message to attach to the journalized copy.
2847 if (CC->redirect_buffer != NULL) {
2848 syslog(LOG_ALERT, "msgbase: CC->redirect_buffer is not NULL during message submission!");
2851 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
2852 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, QP_EADDR);
2853 smi.meta_rfc822_length = StrLength(CC->redirect_buffer);
2854 saved_rfc822_version = CC->redirect_buffer;
2855 CC->redirect_buffer = NULL;
2859 /* Now figure out where to store the pointers */
2860 syslog(LOG_DEBUG, "msgbase: storing pointers");
2862 /* If this is being done by the networker delivering a private
2863 * message, we want to BYPASS saving the sender's copy (because there
2864 * is no local sender; it would otherwise go to the Trashcan).
2866 if ((!CC->internal_pgm) || (recps == NULL)) {
2867 if (CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 1, msg) != 0) {
2868 syslog(LOG_ERR, "msgbase: ERROR saving message pointer!");
2869 CtdlSaveMsgPointerInRoom(CtdlGetConfigStr("c_aideroom"), newmsgid, 0, msg);
2873 /* For internet mail, drop a copy in the outbound queue room */
2874 if ((recps != NULL) && (recps->num_internet > 0)) {
2875 CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, newmsgid, 0, msg);
2878 /* If other rooms are specified, drop them there too. */
2879 if ((recps != NULL) && (recps->num_room > 0))
2880 for (i=0; i<num_tokens(recps->recp_room, '|'); ++i) {
2881 extract_token(recipient, recps->recp_room, i,
2882 '|', sizeof recipient);
2883 syslog(LOG_DEBUG, "msgbase: delivering to room <%s>", recipient);
2884 CtdlSaveMsgPointerInRoom(recipient, newmsgid, 0, msg);
2887 /* Bump this user's messages posted counter. */
2888 syslog(LOG_DEBUG, "msgbase: updating user");
2889 CtdlLockGetCurrentUser();
2890 CC->user.posted = CC->user.posted + 1;
2891 CtdlPutCurrentUserLock();
2893 /* Decide where bounces need to be delivered */
2894 if ((recps != NULL) && (recps->bounce_to == NULL))
2896 if (CC->logged_in) {
2897 snprintf(bounce_to, sizeof bounce_to, "%s@%s", CC->user.fullname, CtdlGetConfigStr("c_nodename"));
2900 snprintf(bounce_to, sizeof bounce_to, "%s@%s", msg->cm_fields[eAuthor], msg->cm_fields[eNodeName]);
2902 recps->bounce_to = bounce_to;
2905 CM_SetFieldLONG(msg, eVltMsgNum, newmsgid);
2908 /* If this is private, local mail, make a copy in the
2909 * recipient's mailbox and bump the reference count.
2911 if ((recps != NULL) && (recps->num_local > 0))
2916 pch = recps->recp_local;
2917 recps->recp_local = recipient;
2918 ntokens = num_tokens(pch, '|');
2919 for (i=0; i<ntokens; ++i)
2921 extract_token(recipient, pch, i, '|', sizeof recipient);
2922 syslog(LOG_DEBUG, "msgbase: delivering private local mail to <%s>", recipient);
2923 if (CtdlGetUser(&userbuf, recipient) == 0) {
2924 CtdlMailboxName(actual_rm, sizeof actual_rm, &userbuf, MAILROOM);
2925 CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0, msg);
2926 CtdlBumpNewMailCounter(userbuf.usernum);
2927 PerformMessageHooks(msg, recps, EVT_AFTERUSRMBOXSAVE);
2930 syslog(LOG_DEBUG, "msgbase: no user <%s>", recipient);
2931 CtdlSaveMsgPointerInRoom(CtdlGetConfigStr("c_aideroom"), newmsgid, 0, msg);
2934 recps->recp_local = pch;
2937 /* Perform "after save" hooks */
2938 syslog(LOG_DEBUG, "msgbase: performing after-save hooks");
2940 PerformMessageHooks(msg, recps, EVT_AFTERSAVE);
2941 CM_FlushField(msg, eVltMsgNum);
2943 /* Go back to the room we started from */
2944 syslog(LOG_DEBUG, "msgbase: returning to original room %s", hold_rm);
2945 if (strcasecmp(hold_rm, CC->room.QRname))
2946 CtdlUserGoto(hold_rm, 0, 1, NULL, NULL, NULL, NULL);
2949 * Any addresses to harvest for someone's address book?
2951 if ( (CC->logged_in) && (recps != NULL) ) {
2952 collected_addresses = harvest_collected_addresses(msg);
2955 if (collected_addresses != NULL) {
2956 aptr = (struct addresses_to_be_filed *)
2957 malloc(sizeof(struct addresses_to_be_filed));
2958 CtdlMailboxName(actual_rm, sizeof actual_rm,
2959 &CC->user, USERCONTACTSROOM);
2960 aptr->roomname = strdup(actual_rm);
2961 aptr->collected_addresses = collected_addresses;
2962 begin_critical_section(S_ATBF);
2965 end_critical_section(S_ATBF);
2969 * Determine whether this message qualifies for journaling.
2971 if (!CM_IsEmpty(msg, eJournal)) {
2972 qualified_for_journaling = 0;
2975 if (recps == NULL) {
2976 qualified_for_journaling = CtdlGetConfigInt("c_journal_pubmsgs");
2978 else if (recps->num_local + recps->num_ignet + recps->num_internet > 0) {
2979 qualified_for_journaling = CtdlGetConfigInt("c_journal_email");
2982 qualified_for_journaling = CtdlGetConfigInt("c_journal_pubmsgs");
2987 * Do we have to perform journaling? If so, hand off the saved
2988 * RFC822 version will be handed off to the journaler for background
2989 * submit. Otherwise, we have to free the memory ourselves.
2991 if (saved_rfc822_version != NULL) {
2992 if (qualified_for_journaling) {
2993 JournalBackgroundSubmit(msg, saved_rfc822_version, recps);
2996 FreeStrBuf(&saved_rfc822_version);
3000 if ((recps != NULL) && (recps->bounce_to == bounce_to))
3001 recps->bounce_to = NULL;
3009 * Convenience function for generating small administrative messages.
3011 long quickie_message(const char *from,
3012 const char *fromaddr,
3017 const char *subject)
3019 struct CtdlMessage *msg;
3020 recptypes *recp = NULL;
3022 msg = malloc(sizeof(struct CtdlMessage));
3023 memset(msg, 0, sizeof(struct CtdlMessage));
3024 msg->cm_magic = CTDLMESSAGE_MAGIC;
3025 msg->cm_anon_type = MES_NORMAL;
3026 msg->cm_format_type = format_type;
3028 if (!IsEmptyStr(from)) {
3029 CM_SetField(msg, eAuthor, from, strlen(from));
3031 else if (!IsEmptyStr(fromaddr)) {
3033 CM_SetField(msg, eAuthor, fromaddr, strlen(fromaddr));
3034 pAt = strchr(msg->cm_fields[eAuthor], '@');
3036 CM_CutFieldAt(msg, eAuthor, pAt - msg->cm_fields[eAuthor]);
3040 msg->cm_fields[eAuthor] = strdup("Citadel");
3043 if (!IsEmptyStr(fromaddr)) CM_SetField(msg, erFc822Addr, fromaddr, strlen(fromaddr));
3044 if (!IsEmptyStr(room)) CM_SetField(msg, eOriginalRoom, room, strlen(room));
3045 CM_SetField(msg, eNodeName, CtdlGetConfigStr("c_nodename"), strlen(CtdlGetConfigStr("c_nodename")));
3046 if (!IsEmptyStr(to)) {
3047 CM_SetField(msg, eRecipient, to, strlen(to));
3048 recp = validate_recipients(to, NULL, 0);
3050 if (!IsEmptyStr(subject)) {
3051 CM_SetField(msg, eMsgSubject, subject, strlen(subject));
3053 if (!IsEmptyStr(text)) {
3054 CM_SetField(msg, eMesageText, text, strlen(text));
3057 long msgnum = CtdlSubmitMsg(msg, recp, room, 0);
3059 if (recp != NULL) free_recipients(recp);
3065 * Back end function used by CtdlMakeMessage() and similar functions
3067 StrBuf *CtdlReadMessageBodyBuf(char *terminator, /* token signalling EOT */
3069 size_t maxlen, /* maximum message length */
3070 StrBuf *exist, /* if non-null, append to it;
3071 exist is ALWAYS freed */
3072 int crlf /* CRLF newlines instead of LF */
3081 LineBuf = NewStrBufPlain(NULL, SIZ);
3082 if (exist == NULL) {
3083 Message = NewStrBufPlain(NULL, 4 * SIZ);
3086 Message = NewStrBufDup(exist);
3089 /* Do we need to change leading ".." to "." for SMTP escaping? */
3090 if ((tlen == 1) && (*terminator == '.')) {
3094 /* read in the lines of message text one by one */
3096 if (CtdlClientGetLine(LineBuf) < 0) {
3099 if ((StrLength(LineBuf) == tlen) && (!strcmp(ChrPtr(LineBuf), terminator))) {
3102 if ( (!flushing) && (!finished) ) {
3104 StrBufAppendBufPlain(LineBuf, HKEY("\r\n"), 0);
3107 StrBufAppendBufPlain(LineBuf, HKEY("\n"), 0);
3110 /* Unescape SMTP-style input of two dots at the beginning of the line */
3112 (StrLength(LineBuf) == 2) &&
3113 (!strcmp(ChrPtr(LineBuf), "..")))
3115 StrBufCutLeft(LineBuf, 1);
3118 StrBufAppendBuf(Message, LineBuf, 0);
3121 /* if we've hit the max msg length, flush the rest */
3122 if (StrLength(Message) >= maxlen) flushing = 1;
3124 } while (!finished);
3125 FreeStrBuf(&LineBuf);
3131 * Back end function used by CtdlMakeMessage() and similar functions
3133 char *CtdlReadMessageBody(char *terminator, /* token signalling EOT */
3135 size_t maxlen, /* maximum message length */
3136 StrBuf *exist, /* if non-null, append to it;
3137 exist is ALWAYS freed */
3138 int crlf /* CRLF newlines instead of LF */
3143 Message = CtdlReadMessageBodyBuf(terminator,
3149 if (Message == NULL)
3152 return SmashStrBuf(&Message);
3155 struct CtdlMessage *CtdlMakeMessage(
3156 struct ctdluser *author, /* author's user structure */
3157 char *recipient, /* NULL if it's not mail */
3158 char *recp_cc, /* NULL if it's not mail */
3159 char *room, /* room where it's going */
3160 int type, /* see MES_ types in header file */
3161 int format_type, /* variformat, plain text, MIME... */
3162 char *fake_name, /* who we're masquerading as */
3163 char *my_email, /* which of my email addresses to use (empty is ok) */
3164 char *subject, /* Subject (optional) */
3165 char *supplied_euid, /* ...or NULL if this is irrelevant */
3166 char *preformatted_text, /* ...or NULL to read text from client */
3167 char *references /* Thread references */
3170 return CtdlMakeMessageLen(
3171 author, /* author's user structure */
3172 recipient, /* NULL if it's not mail */
3173 (recipient)?strlen(recipient) : 0,
3174 recp_cc, /* NULL if it's not mail */
3175 (recp_cc)?strlen(recp_cc): 0,
3176 room, /* room where it's going */
3177 (room)?strlen(room): 0,
3178 type, /* see MES_ types in header file */
3179 format_type, /* variformat, plain text, MIME... */
3180 fake_name, /* who we're masquerading as */
3181 (fake_name)?strlen(fake_name): 0,
3182 my_email, /* which of my email addresses to use (empty is ok) */
3183 (my_email)?strlen(my_email): 0,
3184 subject, /* Subject (optional) */
3185 (subject)?strlen(subject): 0,
3186 supplied_euid, /* ...or NULL if this is irrelevant */
3187 (supplied_euid)?strlen(supplied_euid):0,
3188 preformatted_text, /* ...or NULL to read text from client */
3189 (preformatted_text)?strlen(preformatted_text) : 0,
3190 references, /* Thread references */
3191 (references)?strlen(references):0);
3196 * Build a binary message to be saved on disk.
3197 * (NOTE: if you supply 'preformatted_text', the buffer you give it
3198 * will become part of the message. This means you are no longer
3199 * responsible for managing that memory -- it will be freed along with
3200 * the rest of the fields when CM_Free() is called.)
3203 struct CtdlMessage *CtdlMakeMessageLen(
3204 struct ctdluser *author, /* author's user structure */
3205 char *recipient, /* NULL if it's not mail */
3207 char *recp_cc, /* NULL if it's not mail */
3209 char *room, /* room where it's going */
3211 int type, /* see MES_ types in header file */
3212 int format_type, /* variformat, plain text, MIME... */
3213 char *fake_name, /* who we're masquerading as */
3215 char *my_email, /* which of my email addresses to use (empty is ok) */
3217 char *subject, /* Subject (optional) */
3219 char *supplied_euid, /* ...or NULL if this is irrelevant */
3221 char *preformatted_text, /* ...or NULL to read text from client */
3223 char *references, /* Thread references */
3227 /* Don't confuse the poor folks if it's not routed mail. * /
3228 char dest_node[256] = "";*/
3231 struct CtdlMessage *msg;
3233 StrBuf *FakeEncAuthor = NULL;
3235 msg = malloc(sizeof(struct CtdlMessage));
3236 memset(msg, 0, sizeof(struct CtdlMessage));
3237 msg->cm_magic = CTDLMESSAGE_MAGIC;
3238 msg->cm_anon_type = type;
3239 msg->cm_format_type = format_type;
3241 if (recipient != NULL) rcplen = striplt(recipient);
3242 if (recp_cc != NULL) cclen = striplt(recp_cc);
3244 /* Path or Return-Path */
3246 CM_SetField(msg, eMessagePath, my_email, myelen);
3248 else if (!IsEmptyStr(author->fullname)) {
3249 CM_SetField(msg, eMessagePath, author->fullname, strlen(author->fullname));
3251 convert_spaces_to_underscores(msg->cm_fields[eMessagePath]);
3253 blen = snprintf(buf, sizeof buf, "%ld", (long)time(NULL));
3254 CM_SetField(msg, eTimestamp, buf, blen);
3257 FakeAuthor = NewStrBufPlain (fake_name, fnlen);
3260 FakeAuthor = NewStrBufPlain (author->fullname, -1);
3262 StrBufRFC2047encode(&FakeEncAuthor, FakeAuthor);
3263 CM_SetAsFieldSB(msg, eAuthor, &FakeEncAuthor);
3264 FreeStrBuf(&FakeAuthor);
3266 if (!!IsEmptyStr(CC->room.QRname)) {
3267 if (CC->room.QRflags & QR_MAILBOX) { /* room */
3268 CM_SetField(msg, eOriginalRoom, &CC->room.QRname[11], strlen(&CC->room.QRname[11]));
3271 CM_SetField(msg, eOriginalRoom, CC->room.QRname, strlen(CC->room.QRname));
3275 CM_SetField(msg, eNodeName, CtdlGetConfigStr("c_nodename"), strlen(CtdlGetConfigStr("c_nodename")));
3276 CM_SetField(msg, eHumanNode, CtdlGetConfigStr("c_humannode"), strlen(CtdlGetConfigStr("c_humannode")));
3279 CM_SetField(msg, eRecipient, recipient, rcplen);
3282 CM_SetField(msg, eCarbonCopY, recp_cc, cclen);
3286 CM_SetField(msg, erFc822Addr, my_email, myelen);
3288 else if ( (author == &CC->user) && (!IsEmptyStr(CC->cs_inet_email)) ) {
3289 CM_SetField(msg, erFc822Addr, CC->cs_inet_email, strlen(CC->cs_inet_email));
3292 if (subject != NULL) {
3294 length = striplt(subject);
3300 while ((subject[i] != '\0') &&
3301 (IsAscii = isascii(subject[i]) != 0 ))
3304 CM_SetField(msg, eMsgSubject, subject, subjlen);
3305 else /* ok, we've got utf8 in the string. */
3308 rfc2047Subj = rfc2047encode(subject, length);
3309 CM_SetAsField(msg, eMsgSubject, &rfc2047Subj, strlen(rfc2047Subj));
3316 CM_SetField(msg, eExclusiveID, supplied_euid, euidlen);
3320 CM_SetField(msg, eWeferences, references, reflen);
3323 if (preformatted_text != NULL) {
3324 CM_SetField(msg, eMesageText, preformatted_text, textlen);
3328 MsgBody = CtdlReadMessageBodyBuf(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
3329 if (MsgBody != NULL) {
3330 CM_SetAsFieldSB(msg, eMesageText, &MsgBody);
3341 * API function to delete messages which match a set of criteria
3342 * (returns the actual number of messages deleted)
3344 int CtdlDeleteMessages(const char *room_name, /* which room */
3345 long *dmsgnums, /* array of msg numbers to be deleted */
3346 int num_dmsgnums, /* number of msgs to be deleted, or 0 for "any" */
3347 char *content_type /* or "" for any. regular expressions expected. */
3350 struct ctdlroom qrbuf;
3351 struct cdbdata *cdbfr;
3352 long *msglist = NULL;
3353 long *dellist = NULL;
3356 int num_deleted = 0;
3358 struct MetaData smi;
3361 int need_to_free_re = 0;
3363 if (content_type) if (!IsEmptyStr(content_type)) {
3364 regcomp(&re, content_type, 0);
3365 need_to_free_re = 1;
3367 syslog(LOG_DEBUG, "msgbase: CtdlDeleteMessages(%s, %d msgs, %s)", room_name, num_dmsgnums, content_type);
3369 /* get room record, obtaining a lock... */
3370 if (CtdlGetRoomLock(&qrbuf, room_name) != 0) {
3371 syslog(LOG_ERR, "msgbase: CtdlDeleteMessages(): Room <%s> not found", room_name);
3372 if (need_to_free_re) regfree(&re);
3373 return(0); /* room not found */
3375 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
3377 if (cdbfr != NULL) {
3378 dellist = malloc(cdbfr->len);
3379 msglist = (long *) cdbfr->ptr;
3380 cdbfr->ptr = NULL; /* CtdlDeleteMessages() now owns this memory */
3381 num_msgs = cdbfr->len / sizeof(long);
3385 int have_contenttype = (content_type != NULL) && !IsEmptyStr(content_type);
3386 int have_delmsgs = (num_dmsgnums == 0) || (dmsgnums == NULL);
3387 int have_more_del = 1;
3389 num_msgs = sort_msglist(msglist, num_msgs);
3390 if (num_dmsgnums > 1)
3391 num_dmsgnums = sort_msglist(dmsgnums, num_dmsgnums);
3394 StrBuf *dbg = NewStrBuf();
3395 for (i = 0; i < num_dmsgnums; i++)
3396 StrBufAppendPrintf(dbg, ", %ld", dmsgnums[i]);
3397 syslog(LOG_DEBUG, "msgbase: Deleting before: %s", ChrPtr(dbg));
3402 while ((i < num_msgs) && (have_more_del)) {
3405 /* Set/clear a bit for each criterion */
3407 /* 0 messages in the list or a null list means that we are
3408 * interested in deleting any messages which meet the other criteria.
3411 delete_this |= 0x01;
3414 while ((i < num_msgs) && (msglist[i] < dmsgnums[j])) i++;
3419 if (msglist[i] == dmsgnums[j]) {
3420 delete_this |= 0x01;
3423 have_more_del = (j < num_dmsgnums);
3426 if (have_contenttype) {
3427 GetMetaData(&smi, msglist[i]);
3428 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) == 0) {
3429 delete_this |= 0x02;
3432 delete_this |= 0x02;
3435 /* Delete message only if all bits are set */
3436 if (delete_this == 0x03) {
3437 dellist[num_deleted++] = msglist[i];
3444 StrBuf *dbg = NewStrBuf();
3445 for (i = 0; i < num_deleted; i++)
3446 StrBufAppendPrintf(dbg, ", %ld", dellist[i]);
3447 syslog(LOG_DEBUG, "msgbase: Deleting: %s", ChrPtr(dbg));
3451 num_msgs = sort_msglist(msglist, num_msgs);
3452 cdb_store(CDB_MSGLISTS, &qrbuf.QRnumber, (int)sizeof(long),
3453 msglist, (int)(num_msgs * sizeof(long)));
3456 qrbuf.QRhighest = msglist[num_msgs - 1];
3458 qrbuf.QRhighest = 0;
3460 CtdlPutRoomLock(&qrbuf);
3462 /* Go through the messages we pulled out of the index, and decrement
3463 * their reference counts by 1. If this is the only room the message
3464 * was in, the reference count will reach zero and the message will
3465 * automatically be deleted from the database. We do this in a
3466 * separate pass because there might be plug-in hooks getting called,
3467 * and we don't want that happening during an S_ROOMS critical
3471 for (i=0; i<num_deleted; ++i) {
3472 PerformDeleteHooks(qrbuf.QRname, dellist[i]);
3474 AdjRefCountList(dellist, num_deleted, -1);
3476 /* Now free the memory we used, and go away. */
3477 if (msglist != NULL) free(msglist);
3478 if (dellist != NULL) free(dellist);
3479 syslog(LOG_DEBUG, "msgbase: %d message(s) deleted", num_deleted);
3480 if (need_to_free_re) regfree(&re);
3481 return (num_deleted);
3486 * GetMetaData() - Get the supplementary record for a message
3488 void GetMetaData(struct MetaData *smibuf, long msgnum)
3490 struct cdbdata *cdbsmi;
3493 memset(smibuf, 0, sizeof(struct MetaData));
3494 smibuf->meta_msgnum = msgnum;
3495 smibuf->meta_refcount = 1; /* Default reference count is 1 */
3497 /* Use the negative of the message number for its supp record index */
3498 TheIndex = (0L - msgnum);
3500 cdbsmi = cdb_fetch(CDB_MSGMAIN, &TheIndex, sizeof(long));
3501 if (cdbsmi == NULL) {
3502 return; /* record not found; leave it alone */
3504 memcpy(smibuf, cdbsmi->ptr,
3505 ((cdbsmi->len > sizeof(struct MetaData)) ?
3506 sizeof(struct MetaData) : cdbsmi->len)
3514 * PutMetaData() - (re)write supplementary record for a message
3516 void PutMetaData(struct MetaData *smibuf)
3520 /* Use the negative of the message number for the metadata db index */
3521 TheIndex = (0L - smibuf->meta_msgnum);
3523 cdb_store(CDB_MSGMAIN,
3524 &TheIndex, (int)sizeof(long),
3525 smibuf, (int)sizeof(struct MetaData)
3531 * Convenience function to process a big block of AdjRefCount() operations
3533 void AdjRefCountList(long *msgnum, long nmsg, int incr)
3537 for (i = 0; i < nmsg; i++) {
3538 AdjRefCount(msgnum[i], incr);
3544 * AdjRefCount - adjust the reference count for a message. We need to delete from disk any message whose reference count reaches zero.
3546 void AdjRefCount(long msgnum, int incr)
3548 struct MetaData smi;
3551 /* This is a *tight* critical section; please keep it that way, as
3552 * it may get called while nested in other critical sections.
3553 * Complicating this any further will surely cause deadlock!
3555 begin_critical_section(S_SUPPMSGMAIN);
3556 GetMetaData(&smi, msgnum);
3557 smi.meta_refcount += incr;
3559 end_critical_section(S_SUPPMSGMAIN);
3560 syslog(LOG_DEBUG, "msgbase: AdjRefCount() msg %ld ref count delta %+d, is now %d", msgnum, incr, smi.meta_refcount);
3562 /* If the reference count is now zero, delete both the message and its metadata record.
3564 if (smi.meta_refcount == 0) {
3565 syslog(LOG_DEBUG, "msgbase: deleting message <%ld>", msgnum);
3567 /* Call delete hooks with NULL room to show it has gone altogether */
3568 PerformDeleteHooks(NULL, msgnum);
3570 /* Remove from message base */
3572 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3573 cdb_delete(CDB_BIGMSGS, &delnum, (int)sizeof(long));
3575 /* Remove metadata record */
3576 delnum = (0L - msgnum);
3577 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3583 * Write a generic object to this room
3585 * Note: this could be much more efficient. Right now we use two temporary
3586 * files, and still pull the message into memory as with all others.
3588 void CtdlWriteObject(char *req_room, /* Room to stuff it in */
3589 char *content_type, /* MIME type of this object */
3590 char *raw_message, /* Data to be written */
3591 off_t raw_length, /* Size of raw_message */
3592 struct ctdluser *is_mailbox, /* Mailbox room? */
3593 int is_binary, /* Is encoding necessary? */
3594 int is_unique, /* Del others of this type? */
3595 unsigned int flags /* Internal save flags */
3598 struct ctdlroom qrbuf;
3599 char roomname[ROOMNAMELEN];
3600 struct CtdlMessage *msg;
3601 StrBuf *encoded_message = NULL;
3603 if (is_mailbox != NULL) {
3604 CtdlMailboxName(roomname, sizeof roomname, is_mailbox, req_room);
3607 safestrncpy(roomname, req_room, sizeof(roomname));
3610 syslog(LOG_DEBUG, "msfbase: raw length is %ld", (long)raw_length);
3613 encoded_message = NewStrBufPlain(NULL, (size_t) (((raw_length * 134) / 100) + 4096 ) );
3616 encoded_message = NewStrBufPlain(NULL, (size_t)(raw_length + 4096));
3619 StrBufAppendBufPlain(encoded_message, HKEY("Content-type: "), 0);
3620 StrBufAppendBufPlain(encoded_message, content_type, -1, 0);
3621 StrBufAppendBufPlain(encoded_message, HKEY("\n"), 0);
3624 StrBufAppendBufPlain(encoded_message, HKEY("Content-transfer-encoding: base64\n\n"), 0);
3627 StrBufAppendBufPlain(encoded_message, HKEY("Content-transfer-encoding: 7bit\n\n"), 0);
3631 StrBufBase64Append(encoded_message, NULL, raw_message, raw_length, 0);
3634 StrBufAppendBufPlain(encoded_message, raw_message, raw_length, 0);
3637 syslog(LOG_DEBUG, "msgbase: allocating");
3638 msg = malloc(sizeof(struct CtdlMessage));
3639 memset(msg, 0, sizeof(struct CtdlMessage));
3640 msg->cm_magic = CTDLMESSAGE_MAGIC;
3641 msg->cm_anon_type = MES_NORMAL;
3642 msg->cm_format_type = 4;
3643 CM_SetField(msg, eAuthor, CC->user.fullname, strlen(CC->user.fullname));
3644 CM_SetField(msg, eOriginalRoom, req_room, strlen(req_room));
3645 CM_SetField(msg, eNodeName, CtdlGetConfigStr("c_nodename"), strlen(CtdlGetConfigStr("c_nodename")));
3646 CM_SetField(msg, eHumanNode, CtdlGetConfigStr("c_humannode"), strlen(CtdlGetConfigStr("c_humannode")));
3647 msg->cm_flags = flags;
3649 CM_SetAsFieldSB(msg, eMesageText, &encoded_message);
3651 /* Create the requested room if we have to. */
3652 if (CtdlGetRoom(&qrbuf, roomname) != 0) {
3653 CtdlCreateRoom(roomname, ( (is_mailbox != NULL) ? 5 : 3 ), "", 0, 1, 0, VIEW_BBS);
3655 /* If the caller specified this object as unique, delete all
3656 * other objects of this type that are currently in the room.
3659 syslog(LOG_DEBUG, "msgbase: deleted %d other msgs of this type",
3660 CtdlDeleteMessages(roomname, NULL, 0, content_type)
3663 /* Now write the data */
3664 CtdlSubmitMsg(msg, NULL, roomname, 0);
3669 /************************************************************************/
3670 /* MODULE INITIALIZATION */
3671 /************************************************************************/
3673 CTDL_MODULE_INIT(msgbase)
3676 FillMsgKeyLookupTable();
3679 /* return our module id for the log */