2 * Implements the message store.
4 * Copyright (c) 1987-2020 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
21 #include <libcitadel.h>
22 #include "ctdl_module.h"
23 #include "citserver.h"
26 #include "clientsocket.h"
30 #include "internet_addressing.h"
31 #include "euidindex.h"
33 #include "journaling.h"
35 struct addresses_to_be_filed *atbf = NULL;
38 * These are the four-character field headers we use when outputting
39 * messages in Citadel format (as opposed to RFC822 format).
42 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
43 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
44 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
45 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
46 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
47 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
48 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
49 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
51 "from", // A -> eAuthor
52 NULL, // B -> eBig_message
53 NULL, // C (formerly used as eRemoteRoom)
54 NULL, // D (formerly used as eDestination)
55 "exti", // E -> eXclusivID
56 "rfca", // F -> erFc822Addr
58 "hnod", // H (formerly used as eHumanNode)
59 "msgn", // I -> emessageId
60 "jrnl", // J -> eJournal
61 "rep2", // K -> eReplyTo
62 "list", // L -> eListID
63 "text", // M -> eMesageText
64 NULL, // N (formerly used as eNodeName)
65 "room", // O -> eOriginalRoom
66 "path", // P -> eMessagePath
68 "rcpt", // R -> eRecipient
69 NULL, // S (formerly used as eSpecialField)
70 "time", // T -> eTimestamp
71 "subj", // U -> eMsgSubject
72 "nvto", // V -> eenVelopeTo
73 "wefw", // W -> eWeferences
75 "cccc", // Y -> eCarbonCopY
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 */
113 /* Semi-important fields */
118 /* G is not used yet */
121 /* Q is not used yet */
123 /* X is not used yet */
124 /* Z is not used yet */
131 /* Message text (MUST be last) */
133 /* Not saved to disk:
138 static const long NDiskFields = sizeof(FieldOrder) / sizeof(eMsgField);
141 int CM_IsEmpty(struct CtdlMessage *Msg, eMsgField which)
143 return !((Msg->cm_fields[which] != NULL) && (Msg->cm_fields[which][0] != '\0'));
147 void CM_SetField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
149 if (Msg->cm_fields[which] != NULL) {
150 free (Msg->cm_fields[which]);
152 Msg->cm_fields[which] = malloc(length + 1);
153 memcpy(Msg->cm_fields[which], buf, length);
154 Msg->cm_fields[which][length] = '\0';
155 Msg->cm_lengths[which] = length;
159 void CM_SetFieldLONG(struct CtdlMessage *Msg, eMsgField which, long lvalue)
163 len = snprintf(buf, sizeof(buf), "%ld", lvalue);
164 CM_SetField(Msg, which, buf, len);
168 void CM_CutFieldAt(struct CtdlMessage *Msg, eMsgField WhichToCut, long maxlen)
170 if (Msg->cm_fields[WhichToCut] == NULL)
173 if (Msg->cm_lengths[WhichToCut] > maxlen)
175 Msg->cm_fields[WhichToCut][maxlen] = '\0';
176 Msg->cm_lengths[WhichToCut] = maxlen;
181 void CM_FlushField(struct CtdlMessage *Msg, eMsgField which)
183 if (Msg->cm_fields[which] != NULL)
184 free (Msg->cm_fields[which]);
185 Msg->cm_fields[which] = NULL;
186 Msg->cm_lengths[which] = 0;
190 void CM_Flush(struct CtdlMessage *Msg)
194 if (CM_IsValidMsg(Msg) == 0) {
198 for (i = 0; i < 256; ++i) {
199 CM_FlushField(Msg, i);
204 void CM_CopyField(struct CtdlMessage *Msg, eMsgField WhichToPutTo, eMsgField WhichtToCopy)
207 if (Msg->cm_fields[WhichToPutTo] != NULL) {
208 free (Msg->cm_fields[WhichToPutTo]);
211 if (Msg->cm_fields[WhichtToCopy] != NULL) {
212 len = Msg->cm_lengths[WhichtToCopy];
213 Msg->cm_fields[WhichToPutTo] = malloc(len + 1);
214 memcpy(Msg->cm_fields[WhichToPutTo], Msg->cm_fields[WhichtToCopy], len);
215 Msg->cm_fields[WhichToPutTo][len] = '\0';
216 Msg->cm_lengths[WhichToPutTo] = len;
219 Msg->cm_fields[WhichToPutTo] = NULL;
220 Msg->cm_lengths[WhichToPutTo] = 0;
225 void CM_PrependToField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
227 if (Msg->cm_fields[which] != NULL) {
232 oldmsgsize = Msg->cm_lengths[which] + 1;
233 newmsgsize = length + oldmsgsize;
235 new = malloc(newmsgsize);
236 memcpy(new, buf, length);
237 memcpy(new + length, Msg->cm_fields[which], oldmsgsize);
238 free(Msg->cm_fields[which]);
239 Msg->cm_fields[which] = new;
240 Msg->cm_lengths[which] = newmsgsize - 1;
243 Msg->cm_fields[which] = malloc(length + 1);
244 memcpy(Msg->cm_fields[which], buf, length);
245 Msg->cm_fields[which][length] = '\0';
246 Msg->cm_lengths[which] = length;
251 void CM_SetAsField(struct CtdlMessage *Msg, eMsgField which, char **buf, long length)
253 if (Msg->cm_fields[which] != NULL) {
254 free (Msg->cm_fields[which]);
257 Msg->cm_fields[which] = *buf;
259 Msg->cm_lengths[which] = length;
263 void CM_SetAsFieldSB(struct CtdlMessage *Msg, eMsgField which, StrBuf **buf)
265 if (Msg->cm_fields[which] != NULL) {
266 free (Msg->cm_fields[which]);
269 Msg->cm_lengths[which] = StrLength(*buf);
270 Msg->cm_fields[which] = SmashStrBuf(buf);
274 void CM_GetAsField(struct CtdlMessage *Msg, eMsgField which, char **ret, long *retlen)
276 if (Msg->cm_fields[which] != NULL) {
277 *retlen = Msg->cm_lengths[which];
278 *ret = Msg->cm_fields[which];
279 Msg->cm_fields[which] = NULL;
280 Msg->cm_lengths[which] = 0;
290 * Returns 1 if the supplied pointer points to a valid Citadel message.
291 * If the pointer is NULL or the magic number check fails, returns 0.
293 int CM_IsValidMsg(struct CtdlMessage *msg) {
297 if ((msg->cm_magic) != CTDLMESSAGE_MAGIC) {
298 syslog(LOG_WARNING, "msgbase: CM_IsValidMsg() self-check failed");
305 void CM_FreeContents(struct CtdlMessage *msg)
309 for (i = 0; i < 256; ++i)
310 if (msg->cm_fields[i] != NULL) {
311 free(msg->cm_fields[i]);
312 msg->cm_lengths[i] = 0;
315 msg->cm_magic = 0; /* just in case */
320 * 'Destructor' for struct CtdlMessage
322 void CM_Free(struct CtdlMessage *msg)
324 if (CM_IsValidMsg(msg) == 0) {
325 if (msg != NULL) free (msg);
328 CM_FreeContents(msg);
333 int CM_DupField(eMsgField i, struct CtdlMessage *OrgMsg, struct CtdlMessage *NewMsg)
336 len = OrgMsg->cm_lengths[i];
337 NewMsg->cm_fields[i] = malloc(len + 1);
338 if (NewMsg->cm_fields[i] == NULL) {
341 memcpy(NewMsg->cm_fields[i], OrgMsg->cm_fields[i], len);
342 NewMsg->cm_fields[i][len] = '\0';
343 NewMsg->cm_lengths[i] = len;
348 struct CtdlMessage * CM_Duplicate(struct CtdlMessage *OrgMsg)
351 struct CtdlMessage *NewMsg;
353 if (CM_IsValidMsg(OrgMsg) == 0) {
356 NewMsg = (struct CtdlMessage *)malloc(sizeof(struct CtdlMessage));
357 if (NewMsg == NULL) {
361 memcpy(NewMsg, OrgMsg, sizeof(struct CtdlMessage));
363 memset(&NewMsg->cm_fields, 0, sizeof(char*) * 256);
365 for (i = 0; i < 256; ++i) {
366 if (OrgMsg->cm_fields[i] != NULL) {
367 if (!CM_DupField(i, OrgMsg, NewMsg)) {
378 /* Determine if a given message matches the fields in a message template.
379 * Return 0 for a successful match.
381 int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template) {
384 /* If there aren't any fields in the template, all messages will
387 if (template == NULL) return(0);
389 /* Null messages are bogus. */
390 if (msg == NULL) return(1);
392 for (i='A'; i<='Z'; ++i) {
393 if (template->cm_fields[i] != NULL) {
394 if (msg->cm_fields[i] == NULL) {
395 /* Considered equal if temmplate is empty string */
396 if (IsEmptyStr(template->cm_fields[i])) continue;
399 if ((template->cm_lengths[i] != msg->cm_lengths[i]) ||
400 (strcasecmp(msg->cm_fields[i], template->cm_fields[i])))
405 /* All compares succeeded: we have a match! */
411 * Retrieve the "seen" message list for the current room.
413 void CtdlGetSeen(char *buf, int which_set) {
416 /* Learn about the user and room in question */
417 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
419 if (which_set == ctdlsetseen_seen) {
420 safestrncpy(buf, vbuf.v_seen, SIZ);
422 if (which_set == ctdlsetseen_answered) {
423 safestrncpy(buf, vbuf.v_answered, SIZ);
429 * Manipulate the "seen msgs" string (or other message set strings)
431 void CtdlSetSeen(long *target_msgnums, int num_target_msgnums,
432 int target_setting, int which_set,
433 struct ctdluser *which_user, struct ctdlroom *which_room) {
434 struct cdbdata *cdbfr;
448 char *is_set; /* actually an array of booleans */
450 /* Don't bother doing *anything* if we were passed a list of zero messages */
451 if (num_target_msgnums < 1) {
455 /* If no room was specified, we go with the current room. */
457 which_room = &CC->room;
460 /* If no user was specified, we go with the current user. */
462 which_user = &CC->user;
465 syslog(LOG_DEBUG, "msgbase: CtdlSetSeen(%d msgs starting with %ld, %s, %d) in <%s>",
466 num_target_msgnums, target_msgnums[0],
467 (target_setting ? "SET" : "CLEAR"),
471 /* Learn about the user and room in question */
472 CtdlGetRelationship(&vbuf, which_user, which_room);
474 /* Load the message list */
475 cdbfr = cdb_fetch(CDB_MSGLISTS, &which_room->QRnumber, sizeof(long));
477 msglist = (long *) cdbfr->ptr;
478 cdbfr->ptr = NULL; /* CtdlSetSeen() now owns this memory */
479 num_msgs = cdbfr->len / sizeof(long);
482 return; /* No messages at all? No further action. */
485 is_set = malloc(num_msgs * sizeof(char));
486 memset(is_set, 0, (num_msgs * sizeof(char)) );
488 /* Decide which message set we're manipulating */
490 case ctdlsetseen_seen:
491 vset = NewStrBufPlain(vbuf.v_seen, -1);
493 case ctdlsetseen_answered:
494 vset = NewStrBufPlain(vbuf.v_answered, -1);
501 #if 0 /* This is a special diagnostic section. Do not allow it to run during normal operation. */
502 syslog(LOG_DEBUG, "There are %d messages in the room.\n", num_msgs);
503 for (i=0; i<num_msgs; ++i) {
504 if ((i > 0) && (msglist[i] <= msglist[i-1])) abort();
506 syslog(LOG_DEBUG, "We are twiddling %d of them.\n", num_target_msgnums);
507 for (k=0; k<num_target_msgnums; ++k) {
508 if ((k > 0) && (target_msgnums[k] <= target_msgnums[k-1])) abort();
512 /* Translate the existing sequence set into an array of booleans */
513 setstr = NewStrBuf();
517 while (StrBufExtract_NextToken(setstr, vset, &pvset, ',') >= 0) {
519 StrBufExtract_token(lostr, setstr, 0, ':');
520 if (StrBufNum_tokens(setstr, ':') >= 2) {
521 StrBufExtract_token(histr, setstr, 1, ':');
525 StrBufAppendBuf(histr, lostr, 0);
528 if (!strcmp(ChrPtr(histr), "*")) {
535 for (i = 0; i < num_msgs; ++i) {
536 if ((msglist[i] >= lo) && (msglist[i] <= hi)) {
545 /* Now translate the array of booleans back into a sequence set */
551 for (i=0; i<num_msgs; ++i) {
555 for (k=0; k<num_target_msgnums; ++k) {
556 if (msglist[i] == target_msgnums[k]) {
557 is_seen = target_setting;
561 if ((was_seen == 0) && (is_seen == 1)) {
564 else if ((was_seen == 1) && (is_seen == 0)) {
567 if (StrLength(vset) > 0) {
568 StrBufAppendBufPlain(vset, HKEY(","), 0);
571 StrBufAppendPrintf(vset, "%ld", hi);
574 StrBufAppendPrintf(vset, "%ld:%ld", lo, hi);
578 if ((is_seen) && (i == num_msgs - 1)) {
579 if (StrLength(vset) > 0) {
580 StrBufAppendBufPlain(vset, HKEY(","), 0);
582 if ((i==0) || (was_seen == 0)) {
583 StrBufAppendPrintf(vset, "%ld", msglist[i]);
586 StrBufAppendPrintf(vset, "%ld:%ld", lo, msglist[i]);
594 * We will have to stuff this string back into a 4096 byte buffer, so if it's
595 * larger than that now, truncate it by removing tokens from the beginning.
596 * The limit of 100 iterations is there to prevent an infinite loop in case
597 * something unexpected happens.
599 int number_of_truncations = 0;
600 while ( (StrLength(vset) > SIZ) && (number_of_truncations < 100) ) {
601 StrBufRemove_token(vset, 0, ',');
602 ++number_of_truncations;
606 * If we're truncating the sequence set of messages marked with the 'seen' flag,
607 * we want the earliest messages (the truncated ones) to be marked, not unmarked.
608 * Otherwise messages at the beginning will suddenly appear to be 'unseen'.
610 if ( (which_set == ctdlsetseen_seen) && (number_of_truncations > 0) ) {
612 first_tok = NewStrBuf();
613 StrBufExtract_token(first_tok, vset, 0, ',');
614 StrBufRemove_token(vset, 0, ',');
616 if (StrBufNum_tokens(first_tok, ':') > 1) {
617 StrBufRemove_token(first_tok, 0, ':');
621 new_set = NewStrBuf();
622 StrBufAppendBufPlain(new_set, HKEY("1:"), 0);
623 StrBufAppendBuf(new_set, first_tok, 0);
624 StrBufAppendBufPlain(new_set, HKEY(":"), 0);
625 StrBufAppendBuf(new_set, vset, 0);
628 FreeStrBuf(&first_tok);
632 /* Decide which message set we're manipulating */
634 case ctdlsetseen_seen:
635 safestrncpy(vbuf.v_seen, ChrPtr(vset), sizeof vbuf.v_seen);
637 case ctdlsetseen_answered:
638 safestrncpy(vbuf.v_answered, ChrPtr(vset), sizeof vbuf.v_answered);
644 CtdlSetRelationship(&vbuf, which_user, which_room);
650 * API function to perform an operation for each qualifying message in the
651 * current room. (Returns the number of messages processed.)
653 int CtdlForEachMessage(int mode, long ref, char *search_string,
655 struct CtdlMessage *compare,
656 ForEachMsgCallback CallBack,
661 struct cdbdata *cdbfr;
662 long *msglist = NULL;
664 int num_processed = 0;
667 struct CtdlMessage *msg = NULL;
670 int printed_lastold = 0;
671 int num_search_msgs = 0;
672 long *search_msgs = NULL;
674 int need_to_free_re = 0;
677 if ((content_type) && (!IsEmptyStr(content_type))) {
678 regcomp(&re, content_type, 0);
682 /* Learn about the user and room in question */
683 if (server_shutting_down) {
684 if (need_to_free_re) regfree(&re);
687 CtdlGetUser(&CC->user, CC->curr_user);
689 if (server_shutting_down) {
690 if (need_to_free_re) regfree(&re);
693 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
695 if (server_shutting_down) {
696 if (need_to_free_re) regfree(&re);
700 /* Load the message list */
701 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
703 if (need_to_free_re) regfree(&re);
704 return 0; /* No messages at all? No further action. */
707 msglist = (long *) cdbfr->ptr;
708 num_msgs = cdbfr->len / sizeof(long);
710 cdbfr->ptr = NULL; /* clear this so that cdb_free() doesn't free it */
711 cdb_free(cdbfr); /* we own this memory now */
714 * Now begin the traversal.
716 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
718 /* If the caller is looking for a specific MIME type, filter
719 * out all messages which are not of the type requested.
721 if ((content_type != NULL) && (!IsEmptyStr(content_type))) {
723 /* This call to GetMetaData() sits inside this loop
724 * so that we only do the extra database read per msg
725 * if we need to. Doing the extra read all the time
726 * really kills the server. If we ever need to use
727 * metadata for another search criterion, we need to
728 * move the read somewhere else -- but still be smart
729 * enough to only do the read if the caller has
730 * specified something that will need it.
732 if (server_shutting_down) {
733 if (need_to_free_re) regfree(&re);
737 GetMetaData(&smi, msglist[a]);
739 /* if (strcasecmp(smi.meta_content_type, content_type)) { old non-regex way */
740 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) != 0) {
746 num_msgs = sort_msglist(msglist, num_msgs);
748 /* If a template was supplied, filter out the messages which
749 * don't match. (This could induce some delays!)
752 if (compare != NULL) {
753 for (a = 0; a < num_msgs; ++a) {
754 if (server_shutting_down) {
755 if (need_to_free_re) regfree(&re);
759 msg = CtdlFetchMessage(msglist[a], 1);
761 if (CtdlMsgCmp(msg, compare)) {
770 /* If a search string was specified, get a message list from
771 * the full text index and remove messages which aren't on both
775 * Since the lists are sorted and strictly ascending, and the
776 * output list is guaranteed to be shorter than or equal to the
777 * input list, we overwrite the bottom of the input list. This
778 * eliminates the need to memmove big chunks of the list over and
781 if ( (num_msgs > 0) && (mode == MSGS_SEARCH) && (search_string) ) {
783 /* Call search module via hook mechanism.
784 * NULL means use any search function available.
785 * otherwise replace with a char * to name of search routine
787 CtdlModuleDoSearch(&num_search_msgs, &search_msgs, search_string, "fulltext");
789 if (num_search_msgs > 0) {
793 orig_num_msgs = num_msgs;
795 for (i=0; i<orig_num_msgs; ++i) {
796 for (j=0; j<num_search_msgs; ++j) {
797 if (msglist[i] == search_msgs[j]) {
798 msglist[num_msgs++] = msglist[i];
804 num_msgs = 0; /* No messages qualify */
806 if (search_msgs != NULL) free(search_msgs);
808 /* Now that we've purged messages which don't contain the search
809 * string, treat a MSGS_SEARCH just like a MSGS_ALL from this
816 * Now iterate through the message list, according to the
817 * criteria supplied by the caller.
820 for (a = 0; a < num_msgs; ++a) {
821 if (server_shutting_down) {
822 if (need_to_free_re) regfree(&re);
824 return num_processed;
826 thismsg = msglist[a];
827 if (mode == MSGS_ALL) {
831 is_seen = is_msg_in_sequence_set(
832 vbuf.v_seen, thismsg);
833 if (is_seen) lastold = thismsg;
839 || ((mode == MSGS_OLD) && (is_seen))
840 || ((mode == MSGS_NEW) && (!is_seen))
841 || ((mode == MSGS_LAST) && (a >= (num_msgs - ref)))
842 || ((mode == MSGS_FIRST) && (a < ref))
843 || ((mode == MSGS_GT) && (thismsg > ref))
844 || ((mode == MSGS_LT) && (thismsg < ref))
845 || ((mode == MSGS_EQ) && (thismsg == ref))
848 if ((mode == MSGS_NEW) && (CC->user.flags & US_LASTOLD) && (lastold > 0L) && (printed_lastold == 0) && (!is_seen)) {
850 CallBack(lastold, userdata);
856 CallBack(thismsg, userdata);
861 if (need_to_free_re) regfree(&re);
864 * We cache the most recent msglist in order to do security checks later
866 if (CC->client_socket > 0) {
867 if (CC->cached_msglist != NULL) {
868 free(CC->cached_msglist);
870 CC->cached_msglist = msglist;
871 CC->cached_num_msgs = num_msgs;
877 return num_processed;
882 * memfmout() - Citadel text formatter and paginator.
883 * Although the original purpose of this routine was to format
884 * text to the reader's screen width, all we're really using it
885 * for here is to format text out to 80 columns before sending it
886 * to the client. The client software may reformat it again.
889 char *mptr, /* where are we going to get our text from? */
890 const char *nl /* string to terminate lines with */
893 unsigned char ch = 0;
900 while (ch=*(mptr++), ch != 0) {
903 if (client_write(outbuf, len) == -1) {
904 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
908 if (client_write(nl, nllen) == -1) {
909 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
914 else if (ch == '\r') {
915 /* Ignore carriage returns. Newlines are always LF or CRLF but never CR. */
917 else if (isspace(ch)) {
918 if (column > 72) { /* Beyond 72 columns, break on the next space */
919 if (client_write(outbuf, len) == -1) {
920 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
924 if (client_write(nl, nllen) == -1) {
925 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
938 if (column > 1000) { /* Beyond 1000 columns, break anywhere */
939 if (client_write(outbuf, len) == -1) {
940 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
944 if (client_write(nl, nllen) == -1) {
945 syslog(LOG_ERR, "msgbase: memfmout(): aborting due to write failure");
953 if (client_write(outbuf, len) == -1) {
954 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
957 client_write(nl, nllen);
964 * Callback function for mime parser that simply lists the part
966 void list_this_part(char *name, char *filename, char *partnum, char *disp,
967 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
968 char *cbid, void *cbuserdata)
972 ma = (struct ma_info *)cbuserdata;
973 if (ma->is_ma == 0) {
974 cprintf("part=%s|%s|%s|%s|%s|%ld|%s|%s\n",
988 * Callback function for multipart prefix
990 void list_this_pref(char *name, char *filename, char *partnum, char *disp,
991 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
992 char *cbid, void *cbuserdata)
996 ma = (struct ma_info *)cbuserdata;
997 if (!strcasecmp(cbtype, "multipart/alternative")) {
1001 if (ma->is_ma == 0) {
1002 cprintf("pref=%s|%s\n", partnum, cbtype);
1008 * Callback function for multipart sufffix
1010 void list_this_suff(char *name, char *filename, char *partnum, char *disp,
1011 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1012 char *cbid, void *cbuserdata)
1016 ma = (struct ma_info *)cbuserdata;
1017 if (ma->is_ma == 0) {
1018 cprintf("suff=%s|%s\n", partnum, cbtype);
1020 if (!strcasecmp(cbtype, "multipart/alternative")) {
1027 * Callback function for mime parser that opens a section for downloading
1028 * we use serv_files function here:
1030 extern void OpenCmdResult(char *filename, const char *mime_type);
1031 void mime_download(char *name, char *filename, char *partnum, char *disp,
1032 void *content, char *cbtype, char *cbcharset, size_t length,
1033 char *encoding, char *cbid, void *cbuserdata)
1037 /* Silently go away if there's already a download open. */
1038 if (CC->download_fp != NULL)
1042 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
1043 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
1045 CC->download_fp = tmpfile();
1046 if (CC->download_fp == NULL) {
1047 syslog(LOG_EMERG, "msgbase: mime_download() couldn't write: %m");
1048 cprintf("%d cannot open temporary file: %s\n", ERROR + INTERNAL_ERROR, strerror(errno));
1052 rv = fwrite(content, length, 1, CC->download_fp);
1054 syslog(LOG_EMERG, "msgbase: mime_download() Couldn't write: %m");
1055 cprintf("%d unable to write tempfile.\n", ERROR + TOO_BIG);
1056 fclose(CC->download_fp);
1057 CC->download_fp = NULL;
1060 fflush(CC->download_fp);
1061 rewind(CC->download_fp);
1063 OpenCmdResult(filename, cbtype);
1069 * Callback function for mime parser that outputs a section all at once.
1070 * We can specify the desired section by part number *or* content-id.
1072 void mime_spew_section(char *name, char *filename, char *partnum, char *disp,
1073 void *content, char *cbtype, char *cbcharset, size_t length,
1074 char *encoding, char *cbid, void *cbuserdata)
1076 int *found_it = (int *)cbuserdata;
1079 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
1080 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
1083 cprintf("%d %d|-1|%s|%s|%s\n",
1090 client_write(content, length);
1095 struct CtdlMessage *CtdlDeserializeMessage(long msgnum, int with_body, const char *Buffer, long Length)
1097 struct CtdlMessage *ret = NULL;
1099 const char *upper_bound;
1101 cit_uint8_t field_header;
1105 upper_bound = Buffer + Length;
1110 /* Parse the three bytes that begin EVERY message on disk.
1111 * The first is always 0xFF, the on-disk magic number.
1112 * The second is the anonymous/public type byte.
1113 * The third is the format type byte (vari, fixed, or MIME).
1117 syslog(LOG_ERR, "msgbase: message %ld appears to be corrupted", msgnum);
1120 ret = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1121 memset(ret, 0, sizeof(struct CtdlMessage));
1123 ret->cm_magic = CTDLMESSAGE_MAGIC;
1124 ret->cm_anon_type = *mptr++; /* Anon type byte */
1125 ret->cm_format_type = *mptr++; /* Format type byte */
1128 * The rest is zero or more arbitrary fields. Load them in.
1129 * We're done when we encounter either a zero-length field or
1130 * have just processed the 'M' (message text) field.
1133 field_header = '\0';
1136 /* work around possibly buggy messages: */
1137 while (field_header == '\0') {
1138 if (mptr >= upper_bound) {
1141 field_header = *mptr++;
1143 if (mptr >= upper_bound) {
1146 which = field_header;
1149 CM_SetField(ret, which, mptr, len);
1151 mptr += len + 1; /* advance to next field */
1153 } while ((mptr < upper_bound) && (field_header != 'M'));
1160 * Load a message from disk into memory.
1161 * This is used by CtdlOutputMsg() and other fetch functions.
1163 * NOTE: Caller is responsible for freeing the returned CtdlMessage struct
1164 * using the CM_Free(); function.
1166 struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body)
1168 struct cdbdata *dmsgtext;
1169 struct CtdlMessage *ret = NULL;
1171 syslog(LOG_DEBUG, "msgbase: CtdlFetchMessage(%ld, %d)", msgnum, with_body);
1172 dmsgtext = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
1173 if (dmsgtext == NULL) {
1174 syslog(LOG_ERR, "msgbase: CtdlFetchMessage(%ld, %d) Failed!", msgnum, with_body);
1178 if (dmsgtext->ptr[dmsgtext->len - 1] != '\0') {
1179 syslog(LOG_ERR, "msgbase: CtdlFetchMessage(%ld, %d) Forcefully terminating message!!", msgnum, with_body);
1180 dmsgtext->ptr[dmsgtext->len - 1] = '\0';
1183 ret = CtdlDeserializeMessage(msgnum, with_body, dmsgtext->ptr, dmsgtext->len);
1191 /* Always make sure there's something in the msg text field. If
1192 * it's NULL, the message text is most likely stored separately,
1193 * so go ahead and fetch that. Failing that, just set a dummy
1194 * body so other code doesn't barf.
1196 if ( (CM_IsEmpty(ret, eMesageText)) && (with_body) ) {
1197 dmsgtext = cdb_fetch(CDB_BIGMSGS, &msgnum, sizeof(long));
1198 if (dmsgtext != NULL) {
1199 CM_SetAsField(ret, eMesageText, &dmsgtext->ptr, dmsgtext->len - 1);
1203 if (CM_IsEmpty(ret, eMesageText)) {
1204 CM_SetField(ret, eMesageText, HKEY("\r\n\r\n (no text)\r\n"));
1212 * Pre callback function for multipart/alternative
1214 * NOTE: this differs from the standard behavior for a reason. Normally when
1215 * displaying multipart/alternative you want to show the _last_ usable
1216 * format in the message. Here we show the _first_ one, because it's
1217 * usually text/plain. Since this set of functions is designed for text
1218 * output to non-MIME-aware clients, this is the desired behavior.
1221 void fixed_output_pre(char *name, char *filename, char *partnum, char *disp,
1222 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1223 char *cbid, void *cbuserdata)
1227 ma = (struct ma_info *)cbuserdata;
1228 syslog(LOG_DEBUG, "msgbase: fixed_output_pre() type=<%s>", cbtype);
1229 if (!strcasecmp(cbtype, "multipart/alternative")) {
1233 if (!strcasecmp(cbtype, "message/rfc822")) {
1240 * Post callback function for multipart/alternative
1242 void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
1243 void *content, char *cbtype, char *cbcharset, size_t length,
1244 char *encoding, char *cbid, void *cbuserdata)
1248 ma = (struct ma_info *)cbuserdata;
1249 syslog(LOG_DEBUG, "msgbase: fixed_output_post() type=<%s>", cbtype);
1250 if (!strcasecmp(cbtype, "multipart/alternative")) {
1254 if (!strcasecmp(cbtype, "message/rfc822")) {
1261 * Inline callback function for mime parser that wants to display text
1263 void fixed_output(char *name, char *filename, char *partnum, char *disp,
1264 void *content, char *cbtype, char *cbcharset, size_t length,
1265 char *encoding, char *cbid, void *cbuserdata)
1272 ma = (struct ma_info *)cbuserdata;
1275 "msgbase: fixed_output() part %s: %s (%s) (%ld bytes)",
1276 partnum, filename, cbtype, (long)length
1280 * If we're in the middle of a multipart/alternative scope and
1281 * we've already printed another section, skip this one.
1283 if ( (ma->is_ma) && (ma->did_print) ) {
1284 syslog(LOG_DEBUG, "msgbase: skipping part %s (%s)", partnum, cbtype);
1289 if ( (!strcasecmp(cbtype, "text/plain"))
1290 || (IsEmptyStr(cbtype)) ) {
1293 client_write(wptr, length);
1294 if (wptr[length-1] != '\n') {
1301 if (!strcasecmp(cbtype, "text/html")) {
1302 ptr = html_to_ascii(content, length, 80);
1304 client_write(ptr, wlen);
1305 if ((wlen > 0) && (ptr[wlen-1] != '\n')) {
1312 if (ma->use_fo_hooks) {
1313 if (PerformFixedOutputHooks(cbtype, content, length)) {
1314 /* above function returns nonzero if it handled the part */
1319 if (strncasecmp(cbtype, "multipart/", 10)) {
1320 cprintf("Part %s: %s (%s) (%ld bytes)\r\n",
1321 partnum, filename, cbtype, (long)length);
1328 * The client is elegant and sophisticated and wants to be choosy about
1329 * MIME content types, so figure out which multipart/alternative part
1330 * we're going to send.
1332 * We use a system of weights. When we find a part that matches one of the
1333 * MIME types we've declared as preferential, we can store it in ma->chosen_part
1334 * and then set ma->chosen_pref to that MIME type's position in our preference
1335 * list. If we then hit another match, we only replace the first match if
1336 * the preference value is lower.
1338 void choose_preferred(char *name, char *filename, char *partnum, char *disp,
1339 void *content, char *cbtype, char *cbcharset, size_t length,
1340 char *encoding, char *cbid, void *cbuserdata)
1346 ma = (struct ma_info *)cbuserdata;
1348 for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
1349 extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
1350 if ( (!strcasecmp(buf, cbtype)) && (!ma->freeze) ) {
1351 if (i < ma->chosen_pref) {
1352 syslog(LOG_DEBUG, "msgbase: setting chosen part to <%s>", partnum);
1353 safestrncpy(ma->chosen_part, partnum, sizeof ma->chosen_part);
1354 ma->chosen_pref = i;
1362 * Now that we've chosen our preferred part, output it.
1364 void output_preferred(char *name,
1378 int add_newline = 0;
1381 char *decoded = NULL;
1382 size_t bytes_decoded;
1385 ma = (struct ma_info *)cbuserdata;
1387 /* This is not the MIME part you're looking for... */
1388 if (strcasecmp(partnum, ma->chosen_part)) return;
1390 /* If the content-type of this part is in our preferred formats
1391 * list, we can simply output it verbatim.
1393 for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
1394 extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
1395 if (!strcasecmp(buf, cbtype)) {
1396 /* Yeah! Go! W00t!! */
1397 if (ma->dont_decode == 0)
1398 rc = mime_decode_now (content,
1404 break; /* Give us the chance, maybe theres another one. */
1406 if (rc == 0) text_content = (char *)content;
1408 text_content = decoded;
1409 length = bytes_decoded;
1412 if (text_content[length-1] != '\n') {
1415 cprintf("Content-type: %s", cbtype);
1416 if (!IsEmptyStr(cbcharset)) {
1417 cprintf("; charset=%s", cbcharset);
1419 cprintf("\nContent-length: %d\n",
1420 (int)(length + add_newline) );
1421 if (!IsEmptyStr(encoding)) {
1422 cprintf("Content-transfer-encoding: %s\n", encoding);
1425 cprintf("Content-transfer-encoding: 7bit\n");
1427 cprintf("X-Citadel-MSG4-Partnum: %s\n", partnum);
1429 if (client_write(text_content, length) == -1)
1431 syslog(LOG_ERR, "msgbase: output_preferred() aborting due to write failure");
1434 if (add_newline) cprintf("\n");
1435 if (decoded != NULL) free(decoded);
1440 /* No translations required or possible: output as text/plain */
1441 cprintf("Content-type: text/plain\n\n");
1443 if (ma->dont_decode == 0)
1444 rc = mime_decode_now (content,
1450 return; /* Give us the chance, maybe theres another one. */
1452 if (rc == 0) text_content = (char *)content;
1454 text_content = decoded;
1455 length = bytes_decoded;
1458 fixed_output(name, filename, partnum, disp, text_content, cbtype, cbcharset,
1459 length, encoding, cbid, cbuserdata);
1460 if (decoded != NULL) free(decoded);
1465 char desired_section[64];
1472 * Callback function for
1474 void extract_encapsulated_message(char *name, char *filename, char *partnum, char *disp,
1475 void *content, char *cbtype, char *cbcharset, size_t length,
1476 char *encoding, char *cbid, void *cbuserdata)
1478 struct encapmsg *encap;
1480 encap = (struct encapmsg *)cbuserdata;
1482 /* Only proceed if this is the desired section... */
1483 if (!strcasecmp(encap->desired_section, partnum)) {
1484 encap->msglen = length;
1485 encap->msg = malloc(length + 2);
1486 memcpy(encap->msg, content, length);
1493 * Determine whether the specified message exists in the cached_msglist
1494 * (This is a security check)
1496 int check_cached_msglist(long msgnum) {
1498 /* cases in which we skip the check */
1499 if (!CC) return om_ok; /* not a session */
1500 if (CC->client_socket <= 0) return om_ok; /* not a client session */
1501 if (CC->cached_msglist == NULL) return om_access_denied; /* no msglist fetched */
1502 if (CC->cached_num_msgs == 0) return om_access_denied; /* nothing to check */
1505 /* Do a binary search within the cached_msglist for the requested msgnum */
1507 int max = (CC->cached_num_msgs - 1);
1509 while (max >= min) {
1510 int middle = min + (max-min) / 2 ;
1511 if (msgnum == CC->cached_msglist[middle]) {
1514 if (msgnum > CC->cached_msglist[middle]) {
1522 return om_access_denied;
1527 * Get a message off disk. (returns om_* values found in msgbase.h)
1530 int CtdlOutputMsg(long msg_num, /* message number (local) to fetch */
1531 int mode, /* how would you like that message? */
1532 int headers_only, /* eschew the message body? */
1533 int do_proto, /* do Citadel protocol responses? */
1534 int crlf, /* Use CRLF newlines instead of LF? */
1535 char *section, /* NULL or a message/rfc822 section */
1536 int flags, /* various flags; see msgbase.h */
1541 struct CtdlMessage *TheMessage = NULL;
1542 int retcode = CIT_OK;
1543 struct encapmsg encap;
1546 syslog(LOG_DEBUG, "msgbase: CtdlOutputMsg(msgnum=%ld, mode=%d, section=%s)",
1548 (section ? section : "<>")
1551 r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
1554 if (r == om_not_logged_in) {
1555 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1558 cprintf("%d An unknown error has occurred.\n", ERROR);
1565 * Check to make sure the message is actually IN this room
1567 r = check_cached_msglist(msg_num);
1568 if (r == om_access_denied) {
1569 /* Not in the cache? We get ONE shot to check it again. */
1570 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, NULL, NULL);
1571 r = check_cached_msglist(msg_num);
1574 syslog(LOG_DEBUG, "msgbase: security check fail; message %ld is not in %s",
1575 msg_num, CC->room.QRname
1578 if (r == om_access_denied) {
1579 cprintf("%d message %ld was not found in this room\n",
1580 ERROR + HIGHER_ACCESS_REQUIRED,
1589 * Fetch the message from disk. If we're in HEADERS_FAST mode,
1590 * request that we don't even bother loading the body into memory.
1592 if (headers_only == HEADERS_FAST) {
1593 TheMessage = CtdlFetchMessage(msg_num, 0);
1596 TheMessage = CtdlFetchMessage(msg_num, 1);
1599 if (TheMessage == NULL) {
1600 if (do_proto) cprintf("%d Can't locate msg %ld on disk\n",
1601 ERROR + MESSAGE_NOT_FOUND, msg_num);
1602 return(om_no_such_msg);
1605 /* Here is the weird form of this command, to process only an
1606 * encapsulated message/rfc822 section.
1608 if (section) if (!IsEmptyStr(section)) if (strcmp(section, "0")) {
1609 memset(&encap, 0, sizeof encap);
1610 safestrncpy(encap.desired_section, section, sizeof encap.desired_section);
1611 mime_parser(CM_RANGE(TheMessage, eMesageText),
1612 *extract_encapsulated_message,
1613 NULL, NULL, (void *)&encap, 0
1616 if ((Author != NULL) && (*Author == NULL))
1619 CM_GetAsField(TheMessage, eAuthor, Author, &len);
1621 if ((Address != NULL) && (*Address == NULL))
1624 CM_GetAsField(TheMessage, erFc822Addr, Address, &len);
1626 if ((MessageID != NULL) && (*MessageID == NULL))
1629 CM_GetAsField(TheMessage, emessageId, MessageID, &len);
1631 CM_Free(TheMessage);
1635 encap.msg[encap.msglen] = 0;
1636 TheMessage = convert_internet_message(encap.msg);
1637 encap.msg = NULL; /* no free() here, TheMessage owns it now */
1639 /* Now we let it fall through to the bottom of this
1640 * function, because TheMessage now contains the
1641 * encapsulated message instead of the top-level
1642 * message. Isn't that neat?
1647 cprintf("%d msg %ld has no part %s\n",
1648 ERROR + MESSAGE_NOT_FOUND,
1652 retcode = om_no_such_msg;
1657 /* Ok, output the message now */
1658 if (retcode == CIT_OK)
1659 retcode = CtdlOutputPreLoadedMsg(TheMessage, mode, headers_only, do_proto, crlf, flags);
1660 if ((Author != NULL) && (*Author == NULL))
1663 CM_GetAsField(TheMessage, eAuthor, Author, &len);
1665 if ((Address != NULL) && (*Address == NULL))
1668 CM_GetAsField(TheMessage, erFc822Addr, Address, &len);
1670 if ((MessageID != NULL) && (*MessageID == NULL))
1673 CM_GetAsField(TheMessage, emessageId, MessageID, &len);
1676 CM_Free(TheMessage);
1682 void OutputCtdlMsgHeaders(
1683 struct CtdlMessage *TheMessage,
1684 int do_proto) /* do Citadel protocol responses? */
1688 char display_name[256];
1690 /* begin header processing loop for Citadel message format */
1691 safestrncpy(display_name, "<unknown>", sizeof display_name);
1692 if (!CM_IsEmpty(TheMessage, eAuthor)) {
1693 strcpy(buf, TheMessage->cm_fields[eAuthor]);
1694 if (TheMessage->cm_anon_type == MES_ANONONLY) {
1695 safestrncpy(display_name, "****", sizeof display_name);
1697 else if (TheMessage->cm_anon_type == MES_ANONOPT) {
1698 safestrncpy(display_name, "anonymous", sizeof display_name);
1701 safestrncpy(display_name, buf, sizeof display_name);
1703 if ((is_room_aide())
1704 && ((TheMessage->cm_anon_type == MES_ANONONLY)
1705 || (TheMessage->cm_anon_type == MES_ANONOPT))) {
1706 size_t tmp = strlen(display_name);
1707 snprintf(&display_name[tmp],
1708 sizeof display_name - tmp,
1713 /* Now spew the header fields in the order we like them. */
1714 for (i=0; i< NDiskFields; ++i) {
1716 Field = FieldOrder[i];
1717 if (Field != eMesageText) {
1718 if ( (!CM_IsEmpty(TheMessage, Field))
1719 && (msgkeys[Field] != NULL) ) {
1720 if ((Field == eenVelopeTo) ||
1721 (Field == eRecipient) ||
1722 (Field == eCarbonCopY)) {
1723 sanitize_truncated_recipient(TheMessage->cm_fields[Field]);
1725 if (Field == eAuthor) {
1726 if (do_proto) cprintf("%s=%s\n",
1730 /* Masquerade display name if needed */
1733 cprintf("%s=%s\n", msgkeys[Field], TheMessage->cm_fields[Field]);
1742 void OutputRFC822MsgHeaders(
1743 struct CtdlMessage *TheMessage,
1744 int flags, /* should the message be exported clean */
1745 const char *nl, int nlen,
1746 char *mid, long sizeof_mid,
1747 char *suser, long sizeof_suser,
1748 char *luser, long sizeof_luser,
1749 char *fuser, long sizeof_fuser,
1750 char *snode, long sizeof_snode)
1752 char datestamp[100];
1753 int subject_found = 0;
1760 for (i = 0; i < NDiskFields; ++i) {
1761 if (TheMessage->cm_fields[FieldOrder[i]]) {
1762 mptr = mpptr = TheMessage->cm_fields[FieldOrder[i]];
1763 switch (FieldOrder[i]) {
1765 safestrncpy(luser, mptr, sizeof_luser);
1766 safestrncpy(suser, mptr, sizeof_suser);
1769 if ((flags & QP_EADDR) != 0) {
1770 mptr = qp_encode_email_addrs(mptr);
1772 sanitize_truncated_recipient(mptr);
1773 cprintf("CC: %s%s", mptr, nl);
1776 cprintf("Return-Path: %s%s", mptr, nl);
1779 cprintf("List-ID: %s%s", mptr, nl);
1782 if ((flags & QP_EADDR) != 0)
1783 mptr = qp_encode_email_addrs(mptr);
1785 while ((*hptr != '\0') && isspace(*hptr))
1787 if (!IsEmptyStr(hptr))
1788 cprintf("Envelope-To: %s%s", hptr, nl);
1791 cprintf("Subject: %s%s", mptr, nl);
1795 safestrncpy(mid, mptr, sizeof_mid);
1798 safestrncpy(fuser, mptr, sizeof_fuser);
1800 if (haschar(mptr, '@') == 0) {
1801 sanitize_truncated_recipient(mptr);
1802 cprintf("To: %s@%s", mptr, CtdlGetConfigStr("c_fqdn"));
1806 if ((flags & QP_EADDR) != 0) {
1807 mptr = qp_encode_email_addrs(mptr);
1809 sanitize_truncated_recipient(mptr);
1810 cprintf("To: %s", mptr);
1815 datestring(datestamp, sizeof datestamp, atol(mptr), DATESTRING_RFC822);
1816 cprintf("Date: %s%s", datestamp, nl);
1819 cprintf("References: ");
1820 k = num_tokens(mptr, '|');
1821 for (j=0; j<k; ++j) {
1822 extract_token(buf, mptr, j, '|', sizeof buf);
1823 cprintf("<%s>", buf);
1834 while ((*hptr != '\0') && isspace(*hptr))
1836 if (!IsEmptyStr(hptr))
1837 cprintf("Reply-To: %s%s", mptr, nl);
1849 /* these don't map to mime message headers. */
1852 if (mptr != mpptr) {
1857 if (subject_found == 0) {
1858 cprintf("Subject: (no subject)%s", nl);
1863 void Dump_RFC822HeadersBody(
1864 struct CtdlMessage *TheMessage,
1865 int headers_only, /* eschew the message body? */
1866 int flags, /* should the bessage be exported clean? */
1867 const char *nl, int nlen)
1869 cit_uint8_t prev_ch;
1871 const char *StartOfText = StrBufNOTNULL;
1874 int nllen = strlen(nl);
1878 mptr = TheMessage->cm_fields[eMesageText];
1881 while (*mptr != '\0') {
1882 if (*mptr == '\r') {
1889 eoh = (*(mptr+1) == '\r') && (*(mptr+2) == '\n');
1891 eoh = *(mptr+1) == '\n';
1895 StartOfText = strchr(StartOfText, '\n');
1896 StartOfText = strchr(StartOfText, '\n');
1899 if (((headers_only == HEADERS_NONE) && (mptr >= StartOfText)) ||
1900 ((headers_only == HEADERS_ONLY) && (mptr < StartOfText)) ||
1901 ((headers_only != HEADERS_NONE) &&
1902 (headers_only != HEADERS_ONLY))
1904 if (*mptr == '\n') {
1905 memcpy(&outbuf[outlen], nl, nllen);
1907 outbuf[outlen] = '\0';
1910 outbuf[outlen++] = *mptr;
1914 if (flags & ESC_DOT) {
1915 if ((prev_ch == '\n') && (*mptr == '.') && ((*(mptr+1) == '\r') || (*(mptr+1) == '\n'))) {
1916 outbuf[outlen++] = '.';
1921 if (outlen > 1000) {
1922 if (client_write(outbuf, outlen) == -1) {
1923 syslog(LOG_ERR, "msgbase: Dump_RFC822HeadersBody() aborting due to write failure");
1926 lfSent = (outbuf[outlen - 1] == '\n');
1931 client_write(outbuf, outlen);
1932 lfSent = (outbuf[outlen - 1] == '\n');
1935 client_write(nl, nlen);
1939 /* If the format type on disk is 1 (fixed-format), then we want
1940 * everything to be output completely literally ... regardless of
1941 * what message transfer format is in use.
1943 void DumpFormatFixed(
1944 struct CtdlMessage *TheMessage,
1945 int mode, /* how would you like that message? */
1946 const char *nl, int nllen)
1954 mptr = TheMessage->cm_fields[eMesageText];
1956 if (mode == MT_MIME) {
1957 cprintf("Content-type: text/plain\n\n");
1961 while (ch = *mptr++, ch > 0) {
1965 if ((buflen > 250) && (!xlline)){
1969 while ((buflen > 0) &&
1970 (!isspace(buf[buflen])))
1976 mptr -= tbuflen - buflen;
1982 /* if we reach the outer bounds of our buffer, abort without respect for what we purge. */
1983 if (xlline && ((isspace(ch)) || (buflen > SIZ - nllen - 2))) {
1988 memcpy (&buf[buflen], nl, nllen);
1992 if (client_write(buf, buflen) == -1) {
1993 syslog(LOG_ERR, "msgbase: DumpFormatFixed() aborting due to write failure");
2005 if (!IsEmptyStr(buf)) {
2006 cprintf("%s%s", buf, nl);
2012 * Get a message off disk. (returns om_* values found in msgbase.h)
2014 int CtdlOutputPreLoadedMsg(
2015 struct CtdlMessage *TheMessage,
2016 int mode, /* how would you like that message? */
2017 int headers_only, /* eschew the message body? */
2018 int do_proto, /* do Citadel protocol responses? */
2019 int crlf, /* Use CRLF newlines instead of LF? */
2020 int flags /* should the bessage be exported clean? */
2023 const char *nl; /* newline string */
2027 /* Buffers needed for RFC822 translation. These are all filled
2028 * using functions that are bounds-checked, and therefore we can
2029 * make them substantially smaller than SIZ.
2037 syslog(LOG_DEBUG, "msgbase: CtdlOutputPreLoadedMsg(TheMessage=%s, %d, %d, %d, %d",
2038 ((TheMessage == NULL) ? "NULL" : "not null"),
2039 mode, headers_only, do_proto, crlf
2042 strcpy(mid, "unknown");
2043 nl = (crlf ? "\r\n" : "\n");
2044 nlen = crlf ? 2 : 1;
2046 if (!CM_IsValidMsg(TheMessage)) {
2047 syslog(LOG_ERR, "msgbase: error; invalid preloaded message for output");
2048 return(om_no_such_msg);
2051 /* Suppress envelope recipients if required to avoid disclosing BCC addresses.
2052 * Pad it with spaces in order to avoid changing the RFC822 length of the message.
2054 if ( (flags & SUPPRESS_ENV_TO) && (!CM_IsEmpty(TheMessage, eenVelopeTo)) ) {
2055 memset(TheMessage->cm_fields[eenVelopeTo], ' ', TheMessage->cm_lengths[eenVelopeTo]);
2058 /* Are we downloading a MIME component? */
2059 if (mode == MT_DOWNLOAD) {
2060 if (TheMessage->cm_format_type != FMT_RFC822) {
2062 cprintf("%d This is not a MIME message.\n",
2063 ERROR + ILLEGAL_VALUE);
2064 } else if (CC->download_fp != NULL) {
2065 if (do_proto) cprintf(
2066 "%d You already have a download open.\n",
2067 ERROR + RESOURCE_BUSY);
2069 /* Parse the message text component */
2070 mime_parser(CM_RANGE(TheMessage, eMesageText),
2071 *mime_download, NULL, NULL, NULL, 0);
2072 /* If there's no file open by this time, the requested
2073 * section wasn't found, so print an error
2075 if (CC->download_fp == NULL) {
2076 if (do_proto) cprintf(
2077 "%d Section %s not found.\n",
2078 ERROR + FILE_NOT_FOUND,
2079 CC->download_desired_section);
2082 return((CC->download_fp != NULL) ? om_ok : om_mime_error);
2085 /* MT_SPEW_SECTION is like MT_DOWNLOAD except it outputs the whole MIME part
2086 * in a single server operation instead of opening a download file.
2088 if (mode == MT_SPEW_SECTION) {
2089 if (TheMessage->cm_format_type != FMT_RFC822) {
2091 cprintf("%d This is not a MIME message.\n",
2092 ERROR + ILLEGAL_VALUE);
2094 /* Parse the message text component */
2097 mime_parser(CM_RANGE(TheMessage, eMesageText),
2098 *mime_spew_section, NULL, NULL, (void *)&found_it, 0);
2099 /* If section wasn't found, print an error
2102 if (do_proto) cprintf(
2103 "%d Section %s not found.\n",
2104 ERROR + FILE_NOT_FOUND,
2105 CC->download_desired_section);
2108 return((CC->download_fp != NULL) ? om_ok : om_mime_error);
2111 /* now for the user-mode message reading loops */
2112 if (do_proto) cprintf("%d msg:\n", LISTING_FOLLOWS);
2114 /* Does the caller want to skip the headers? */
2115 if (headers_only == HEADERS_NONE) goto START_TEXT;
2117 /* Tell the client which format type we're using. */
2118 if ( (mode == MT_CITADEL) && (do_proto) ) {
2119 cprintf("type=%d\n", TheMessage->cm_format_type);
2122 /* nhdr=yes means that we're only displaying headers, no body */
2123 if ( (TheMessage->cm_anon_type == MES_ANONONLY)
2124 && ((mode == MT_CITADEL) || (mode == MT_MIME))
2127 cprintf("nhdr=yes\n");
2130 if ((mode == MT_CITADEL) || (mode == MT_MIME))
2131 OutputCtdlMsgHeaders(TheMessage, do_proto);
2134 /* begin header processing loop for RFC822 transfer format */
2138 if (mode == MT_RFC822)
2139 OutputRFC822MsgHeaders(
2144 suser, sizeof(suser),
2145 luser, sizeof(luser),
2146 fuser, sizeof(fuser),
2147 snode, sizeof(snode)
2151 for (i=0; !IsEmptyStr(&suser[i]); ++i) {
2152 suser[i] = tolower(suser[i]);
2153 if (!isalnum(suser[i])) suser[i]='_';
2156 if (mode == MT_RFC822) {
2157 /* Construct a fun message id */
2158 cprintf("Message-ID: <%s", mid);
2159 if (strchr(mid, '@')==NULL) {
2160 cprintf("@%s", snode);
2164 if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONONLY)) {
2165 cprintf("From: \"----\" <x@x.org>%s", nl);
2167 else if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONOPT)) {
2168 cprintf("From: \"anonymous\" <x@x.org>%s", nl);
2170 else if (!IsEmptyStr(fuser)) {
2171 cprintf("From: \"%s\" <%s>%s", luser, fuser, nl);
2174 cprintf("From: \"%s\" <%s@%s>%s", luser, suser, snode, nl);
2177 /* Blank line signifying RFC822 end-of-headers */
2178 if (TheMessage->cm_format_type != FMT_RFC822) {
2183 /* end header processing loop ... at this point, we're in the text */
2185 if (headers_only == HEADERS_FAST) goto DONE;
2187 /* Tell the client about the MIME parts in this message */
2188 if (TheMessage->cm_format_type == FMT_RFC822) {
2189 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2190 memset(&ma, 0, sizeof(struct ma_info));
2191 mime_parser(CM_RANGE(TheMessage, eMesageText),
2192 (do_proto ? *list_this_part : NULL),
2193 (do_proto ? *list_this_pref : NULL),
2194 (do_proto ? *list_this_suff : NULL),
2197 else if (mode == MT_RFC822) { /* unparsed RFC822 dump */
2198 Dump_RFC822HeadersBody(
2207 if (headers_only == HEADERS_ONLY) {
2211 /* signify start of msg text */
2212 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2213 if (do_proto) cprintf("text\n");
2216 if (TheMessage->cm_format_type == FMT_FIXED)
2219 mode, /* how would you like that message? */
2222 /* If the message on disk is format 0 (Citadel vari-format), we
2223 * output using the formatter at 80 columns. This is the final output
2224 * form if the transfer format is RFC822, but if the transfer format
2225 * is Citadel proprietary, it'll still work, because the indentation
2226 * for new paragraphs is correct and the client will reformat the
2227 * message to the reader's screen width.
2229 if (TheMessage->cm_format_type == FMT_CITADEL) {
2230 if (mode == MT_MIME) {
2231 cprintf("Content-type: text/x-citadel-variformat\n\n");
2233 memfmout(TheMessage->cm_fields[eMesageText], nl);
2236 /* If the message on disk is format 4 (MIME), we've gotta hand it
2237 * off to the MIME parser. The client has already been told that
2238 * this message is format 1 (fixed format), so the callback function
2239 * we use will display those parts as-is.
2241 if (TheMessage->cm_format_type == FMT_RFC822) {
2242 memset(&ma, 0, sizeof(struct ma_info));
2244 if (mode == MT_MIME) {
2245 ma.use_fo_hooks = 0;
2246 strcpy(ma.chosen_part, "1");
2247 ma.chosen_pref = 9999;
2248 ma.dont_decode = CC->msg4_dont_decode;
2249 mime_parser(CM_RANGE(TheMessage, eMesageText),
2250 *choose_preferred, *fixed_output_pre,
2251 *fixed_output_post, (void *)&ma, 1);
2252 mime_parser(CM_RANGE(TheMessage, eMesageText),
2253 *output_preferred, NULL, NULL, (void *)&ma, 1);
2256 ma.use_fo_hooks = 1;
2257 mime_parser(CM_RANGE(TheMessage, eMesageText),
2258 *fixed_output, *fixed_output_pre,
2259 *fixed_output_post, (void *)&ma, 0);
2264 DONE: /* now we're done */
2265 if (do_proto) cprintf("000\n");
2270 * Save one or more message pointers into a specified room
2271 * (Returns 0 for success, nonzero for failure)
2272 * roomname may be NULL to use the current room
2274 * Note that the 'supplied_msg' field may be set to NULL, in which case
2275 * the message will be fetched from disk, by number, if we need to perform
2276 * replication checks. This adds an additional database read, so if the
2277 * caller already has the message in memory then it should be supplied. (Obviously
2278 * this mode of operation only works if we're saving a single message.)
2280 int CtdlSaveMsgPointersInRoom(char *roomname, long newmsgidlist[], int num_newmsgs,
2281 int do_repl_check, struct CtdlMessage *supplied_msg, int suppress_refcount_adj
2284 char hold_rm[ROOMNAMELEN];
2285 struct cdbdata *cdbfr;
2288 long highest_msg = 0L;
2291 struct CtdlMessage *msg = NULL;
2293 long *msgs_to_be_merged = NULL;
2294 int num_msgs_to_be_merged = 0;
2297 "msgbase: CtdlSaveMsgPointersInRoom(room=%s, num_msgs=%d, repl=%d, suppress_rca=%d)",
2298 roomname, num_newmsgs, do_repl_check, suppress_refcount_adj
2301 strcpy(hold_rm, CC->room.QRname);
2304 if (newmsgidlist == NULL) return(ERROR + INTERNAL_ERROR);
2305 if (num_newmsgs < 1) return(ERROR + INTERNAL_ERROR);
2306 if (num_newmsgs > 1) supplied_msg = NULL;
2308 /* Now the regular stuff */
2309 if (CtdlGetRoomLock(&CC->room,
2310 ((roomname != NULL) ? roomname : CC->room.QRname) )
2312 syslog(LOG_ERR, "msgbase: no such room <%s>", roomname);
2313 return(ERROR + ROOM_NOT_FOUND);
2317 msgs_to_be_merged = malloc(sizeof(long) * num_newmsgs);
2318 num_msgs_to_be_merged = 0;
2321 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
2322 if (cdbfr == NULL) {
2326 msglist = (long *) cdbfr->ptr;
2327 cdbfr->ptr = NULL; /* CtdlSaveMsgPointerInRoom() now owns this memory */
2328 num_msgs = cdbfr->len / sizeof(long);
2333 /* Create a list of msgid's which were supplied by the caller, but do
2334 * not already exist in the target room. It is absolutely taboo to
2335 * have more than one reference to the same message in a room.
2337 for (i=0; i<num_newmsgs; ++i) {
2339 if (num_msgs > 0) for (j=0; j<num_msgs; ++j) {
2340 if (msglist[j] == newmsgidlist[i]) {
2345 msgs_to_be_merged[num_msgs_to_be_merged++] = newmsgidlist[i];
2349 syslog(LOG_DEBUG, "msgbase: %d unique messages to be merged", num_msgs_to_be_merged);
2352 * Now merge the new messages
2354 msglist = realloc(msglist, (sizeof(long) * (num_msgs + num_msgs_to_be_merged)) );
2355 if (msglist == NULL) {
2356 syslog(LOG_ALERT, "msgbase: ERROR; can't realloc message list!");
2357 free(msgs_to_be_merged);
2358 return (ERROR + INTERNAL_ERROR);
2360 memcpy(&msglist[num_msgs], msgs_to_be_merged, (sizeof(long) * num_msgs_to_be_merged) );
2361 num_msgs += num_msgs_to_be_merged;
2363 /* Sort the message list, so all the msgid's are in order */
2364 num_msgs = sort_msglist(msglist, num_msgs);
2366 /* Determine the highest message number */
2367 highest_msg = msglist[num_msgs - 1];
2369 /* Write it back to disk. */
2370 cdb_store(CDB_MSGLISTS, &CC->room.QRnumber, (int)sizeof(long),
2371 msglist, (int)(num_msgs * sizeof(long)));
2373 /* Free up the memory we used. */
2376 /* Update the highest-message pointer and unlock the room. */
2377 CC->room.QRhighest = highest_msg;
2378 CtdlPutRoomLock(&CC->room);
2380 /* Perform replication checks if necessary */
2381 if ( (DoesThisRoomNeedEuidIndexing(&CC->room)) && (do_repl_check) ) {
2382 syslog(LOG_DEBUG, "msgbase: CtdlSaveMsgPointerInRoom() doing repl checks");
2384 for (i=0; i<num_msgs_to_be_merged; ++i) {
2385 msgid = msgs_to_be_merged[i];
2387 if (supplied_msg != NULL) {
2391 msg = CtdlFetchMessage(msgid, 0);
2395 ReplicationChecks(msg);
2397 /* If the message has an Exclusive ID, index that... */
2398 if (!CM_IsEmpty(msg, eExclusiveID)) {
2399 index_message_by_euid(msg->cm_fields[eExclusiveID], &CC->room, msgid);
2402 /* Free up the memory we may have allocated */
2403 if (msg != supplied_msg) {
2412 syslog(LOG_DEBUG, "msgbase: CtdlSaveMsgPointerInRoom() skips repl checks");
2415 /* Submit this room for processing by hooks */
2416 int total_roomhook_errors = PerformRoomHooks(&CC->room);
2417 if (total_roomhook_errors) {
2418 syslog(LOG_WARNING, "msgbase: room hooks returned %d errors", total_roomhook_errors);
2421 /* Go back to the room we were in before we wandered here... */
2422 CtdlGetRoom(&CC->room, hold_rm);
2424 /* Bump the reference count for all messages which were merged */
2425 if (!suppress_refcount_adj) {
2426 AdjRefCountList(msgs_to_be_merged, num_msgs_to_be_merged, +1);
2429 /* Free up memory... */
2430 if (msgs_to_be_merged != NULL) {
2431 free(msgs_to_be_merged);
2434 /* Return success. */
2440 * This is the same as CtdlSaveMsgPointersInRoom() but it only accepts
2443 int CtdlSaveMsgPointerInRoom(char *roomname, long msgid,
2444 int do_repl_check, struct CtdlMessage *supplied_msg)
2446 return CtdlSaveMsgPointersInRoom(roomname, &msgid, 1, do_repl_check, supplied_msg, 0);
2453 * Message base operation to save a new message to the message store
2454 * (returns new message number)
2456 * This is the back end for CtdlSubmitMsg() and should not be directly
2457 * called by server-side modules.
2460 long CtdlSaveThisMessage(struct CtdlMessage *msg, long msgid, int Reply) {
2468 * If the message is big, set its body aside for storage elsewhere
2469 * and we hide the message body from the serializer
2471 if (!CM_IsEmpty(msg, eMesageText) && msg->cm_lengths[eMesageText] > BIGMSG)
2474 holdM = msg->cm_fields[eMesageText];
2475 msg->cm_fields[eMesageText] = NULL;
2476 holdMLen = msg->cm_lengths[eMesageText];
2477 msg->cm_lengths[eMesageText] = 0;
2480 /* Serialize our data structure for storage in the database */
2481 CtdlSerializeMessage(&smr, msg);
2484 /* put the message body back into the message */
2485 msg->cm_fields[eMesageText] = holdM;
2486 msg->cm_lengths[eMesageText] = holdMLen;
2491 cprintf("%d Unable to serialize message\n",
2492 ERROR + INTERNAL_ERROR);
2495 syslog(LOG_ERR, "msgbase: CtdlSaveMessage() unable to serialize message");
2501 /* Write our little bundle of joy into the message base */
2502 retval = cdb_store(CDB_MSGMAIN, &msgid, (int)sizeof(long),
2505 syslog(LOG_ERR, "msgbase: can't store message %ld: %ld", msgid, retval);
2509 retval = cdb_store(CDB_BIGMSGS,
2516 syslog(LOG_ERR, "msgbase: failed to store message body for msgid %ld: %ld", msgid, retval);
2521 /* Free the memory we used for the serialized message */
2527 long send_message(struct CtdlMessage *msg) {
2533 /* Get a new message number */
2534 newmsgid = get_new_message_number();
2536 /* Generate an ID if we don't have one already */
2537 if (CM_IsEmpty(msg, emessageId)) {
2538 msgidbuflen = snprintf(msgidbuf, sizeof msgidbuf, "%08lX-%08lX@%s",
2539 (long unsigned int) time(NULL),
2540 (long unsigned int) newmsgid,
2541 CtdlGetConfigStr("c_fqdn")
2544 CM_SetField(msg, emessageId, msgidbuf, msgidbuflen);
2547 retval = CtdlSaveThisMessage(msg, newmsgid, 1);
2553 /* Return the *local* message ID to the caller
2554 * (even if we're storing an incoming network message)
2562 * Serialize a struct CtdlMessage into the format used on disk and network.
2564 * This function loads up a "struct ser_ret" (defined in server.h) which
2565 * contains the length of the serialized message and a pointer to the
2566 * serialized message in memory. THE LATTER MUST BE FREED BY THE CALLER.
2568 void CtdlSerializeMessage(struct ser_ret *ret, /* return values */
2569 struct CtdlMessage *msg) /* unserialized msg */
2575 * Check for valid message format
2577 if (CM_IsValidMsg(msg) == 0) {
2578 syslog(LOG_ERR, "msgbase: CtdlSerializeMessage() aborting due to invalid message");
2585 for (i=0; i < NDiskFields; ++i)
2586 if (msg->cm_fields[FieldOrder[i]] != NULL)
2587 ret->len += msg->cm_lengths[FieldOrder[i]] + 2;
2589 ret->ser = malloc(ret->len);
2590 if (ret->ser == NULL) {
2591 syslog(LOG_ERR, "msgbase: CtdlSerializeMessage() malloc(%ld) failed: %m", (long)ret->len);
2598 ret->ser[1] = msg->cm_anon_type;
2599 ret->ser[2] = msg->cm_format_type;
2602 for (i=0; i < NDiskFields; ++i)
2603 if (msg->cm_fields[FieldOrder[i]] != NULL)
2605 ret->ser[wlen++] = (char)FieldOrder[i];
2607 memcpy(&ret->ser[wlen],
2608 msg->cm_fields[FieldOrder[i]],
2609 msg->cm_lengths[FieldOrder[i]] + 1);
2611 wlen = wlen + msg->cm_lengths[FieldOrder[i]] + 1;
2614 if (ret->len != wlen) {
2615 syslog(LOG_ERR, "msgbase: ERROR; len=%ld wlen=%ld", (long)ret->len, (long)wlen);
2623 * Check to see if any messages already exist in the current room which
2624 * carry the same Exclusive ID as this one. If any are found, delete them.
2626 void ReplicationChecks(struct CtdlMessage *msg) {
2627 long old_msgnum = (-1L);
2629 if (DoesThisRoomNeedEuidIndexing(&CC->room) == 0) return;
2631 syslog(LOG_DEBUG, "msgbase: performing replication checks in <%s>", CC->room.QRname);
2633 /* No exclusive id? Don't do anything. */
2634 if (msg == NULL) return;
2635 if (CM_IsEmpty(msg, eExclusiveID)) return;
2637 /*syslog(LOG_DEBUG, "msgbase: exclusive ID: <%s> for room <%s>",
2638 msg->cm_fields[eExclusiveID], CC->room.QRname);*/
2640 old_msgnum = CtdlLocateMessageByEuid(msg->cm_fields[eExclusiveID], &CC->room);
2641 if (old_msgnum > 0L) {
2642 syslog(LOG_DEBUG, "msgbase: ReplicationChecks() replacing message %ld", old_msgnum);
2643 CtdlDeleteMessages(CC->room.QRname, &old_msgnum, 1, "");
2650 * Save a message to disk and submit it into the delivery system.
2652 long CtdlSubmitMsg(struct CtdlMessage *msg, /* message to save */
2653 recptypes *recps, /* recipients (if mail) */
2654 const char *force, /* force a particular room? */
2655 int flags /* should the message be exported clean? */
2658 char hold_rm[ROOMNAMELEN];
2659 char actual_rm[ROOMNAMELEN];
2660 char force_room[ROOMNAMELEN];
2661 char content_type[SIZ]; /* We have to learn this */
2662 char recipient[SIZ];
2663 char bounce_to[1024];
2666 const char *mptr = NULL;
2667 struct ctdluser userbuf;
2669 struct MetaData smi;
2670 char *collected_addresses = NULL;
2671 struct addresses_to_be_filed *aptr = NULL;
2672 StrBuf *saved_rfc822_version = NULL;
2673 int qualified_for_journaling = 0;
2675 syslog(LOG_DEBUG, "msgbase: CtdlSubmitMsg() called");
2676 if (CM_IsValidMsg(msg) == 0) return(-1); /* self check */
2678 /* If this message has no timestamp, we take the liberty of
2679 * giving it one, right now.
2681 if (CM_IsEmpty(msg, eTimestamp)) {
2682 CM_SetFieldLONG(msg, eTimestamp, time(NULL));
2685 /* If this message has no path, we generate one.
2687 if (CM_IsEmpty(msg, eMessagePath)) {
2688 if (!CM_IsEmpty(msg, eAuthor)) {
2689 CM_CopyField(msg, eMessagePath, eAuthor);
2690 for (a=0; !IsEmptyStr(&msg->cm_fields[eMessagePath][a]); ++a) {
2691 if (isspace(msg->cm_fields[eMessagePath][a])) {
2692 msg->cm_fields[eMessagePath][a] = ' ';
2697 CM_SetField(msg, eMessagePath, HKEY("unknown"));
2701 if (force == NULL) {
2702 force_room[0] = '\0';
2705 strcpy(force_room, force);
2708 /* Learn about what's inside, because it's what's inside that counts */
2709 if (CM_IsEmpty(msg, eMesageText)) {
2710 syslog(LOG_ERR, "msgbase: ERROR; attempt to save message with NULL body");
2714 switch (msg->cm_format_type) {
2716 strcpy(content_type, "text/x-citadel-variformat");
2719 strcpy(content_type, "text/plain");
2722 strcpy(content_type, "text/plain");
2723 mptr = bmstrcasestr(msg->cm_fields[eMesageText], "Content-type:");
2726 safestrncpy(content_type, &mptr[13], sizeof content_type);
2727 striplt(content_type);
2728 aptr = content_type;
2729 while (!IsEmptyStr(aptr)) {
2741 /* Goto the correct room */
2742 room = (recps) ? CC->room.QRname : SENTITEMS;
2743 syslog(LOG_DEBUG, "msgbase: selected room %s", room);
2744 strcpy(hold_rm, CC->room.QRname);
2745 strcpy(actual_rm, CC->room.QRname);
2746 if (recps != NULL) {
2747 strcpy(actual_rm, SENTITEMS);
2750 /* If the user is a twit, move to the twit room for posting */
2752 if (CC->user.axlevel == AxProbU) {
2753 strcpy(hold_rm, actual_rm);
2754 strcpy(actual_rm, CtdlGetConfigStr("c_twitroom"));
2755 syslog(LOG_DEBUG, "msgbase: diverting to twit room");
2759 /* ...or if this message is destined for Aide> then go there. */
2760 if (!IsEmptyStr(force_room)) {
2761 strcpy(actual_rm, force_room);
2764 syslog(LOG_DEBUG, "msgbase: final selection: %s (%s)", actual_rm, room);
2765 if (strcasecmp(actual_rm, CC->room.QRname)) {
2766 /* CtdlGetRoom(&CC->room, actual_rm); */
2767 CtdlUserGoto(actual_rm, 0, 1, NULL, NULL, NULL, NULL);
2771 * If this message has no O (room) field, generate one.
2773 if (CM_IsEmpty(msg, eOriginalRoom) && !IsEmptyStr(CC->room.QRname)) {
2774 CM_SetField(msg, eOriginalRoom, CC->room.QRname, strlen(CC->room.QRname));
2777 /* Perform "before save" hooks (aborting if any return nonzero) */
2778 syslog(LOG_DEBUG, "msgbase: performing before-save hooks");
2779 if (PerformMessageHooks(msg, recps, EVT_BEFORESAVE) > 0) return(-3);
2782 * If this message has an Exclusive ID, and the room is replication
2783 * checking enabled, then do replication checks.
2785 if (DoesThisRoomNeedEuidIndexing(&CC->room)) {
2786 ReplicationChecks(msg);
2789 /* Save it to disk */
2790 syslog(LOG_DEBUG, "msgbase: saving to disk");
2791 newmsgid = send_message(msg);
2792 if (newmsgid <= 0L) return(-5);
2794 /* Write a supplemental message info record. This doesn't have to
2795 * be a critical section because nobody else knows about this message
2798 syslog(LOG_DEBUG, "msgbase: creating metadata record");
2799 memset(&smi, 0, sizeof(struct MetaData));
2800 smi.meta_msgnum = newmsgid;
2801 smi.meta_refcount = 0;
2802 safestrncpy(smi.meta_content_type, content_type,
2803 sizeof smi.meta_content_type);
2806 * Measure how big this message will be when rendered as RFC822.
2807 * We do this for two reasons:
2808 * 1. We need the RFC822 length for the new metadata record, so the
2809 * POP and IMAP services don't have to calculate message lengths
2810 * while the user is waiting (multiplied by potentially hundreds
2811 * or thousands of messages).
2812 * 2. If journaling is enabled, we will need an RFC822 version of the
2813 * message to attach to the journalized copy.
2815 if (CC->redirect_buffer != NULL) {
2816 syslog(LOG_ALERT, "msgbase: CC->redirect_buffer is not NULL during message submission!");
2819 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
2820 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, QP_EADDR);
2821 smi.meta_rfc822_length = StrLength(CC->redirect_buffer);
2822 saved_rfc822_version = CC->redirect_buffer;
2823 CC->redirect_buffer = NULL;
2827 /* Now figure out where to store the pointers */
2828 syslog(LOG_DEBUG, "msgbase: storing pointers");
2830 /* If this is being done by the networker delivering a private
2831 * message, we want to BYPASS saving the sender's copy (because there
2832 * is no local sender; it would otherwise go to the Trashcan).
2834 if ((!CC->internal_pgm) || (recps == NULL)) {
2835 if (CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 1, msg) != 0) {
2836 syslog(LOG_ERR, "msgbase: ERROR saving message pointer!");
2837 CtdlSaveMsgPointerInRoom(CtdlGetConfigStr("c_aideroom"), newmsgid, 0, msg);
2841 /* For internet mail, drop a copy in the outbound queue room */
2842 if ((recps != NULL) && (recps->num_internet > 0)) {
2843 CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, newmsgid, 0, msg);
2846 /* If other rooms are specified, drop them there too. */
2847 if ((recps != NULL) && (recps->num_room > 0))
2848 for (i=0; i<num_tokens(recps->recp_room, '|'); ++i) {
2849 extract_token(recipient, recps->recp_room, i,
2850 '|', sizeof recipient);
2851 syslog(LOG_DEBUG, "msgbase: delivering to room <%s>", recipient);
2852 CtdlSaveMsgPointerInRoom(recipient, newmsgid, 0, msg);
2855 /* Bump this user's messages posted counter. */
2856 syslog(LOG_DEBUG, "msgbase: updating user");
2857 CtdlLockGetCurrentUser();
2858 CC->user.posted = CC->user.posted + 1;
2859 CtdlPutCurrentUserLock();
2861 /* Decide where bounces need to be delivered */
2862 if ((recps != NULL) && (recps->bounce_to == NULL))
2864 if (CC->logged_in) {
2865 strcpy(bounce_to, CC->user.fullname);
2868 strcpy(bounce_to, msg->cm_fields[eAuthor]);
2870 recps->bounce_to = bounce_to;
2873 CM_SetFieldLONG(msg, eVltMsgNum, newmsgid);
2876 /* If this is private, local mail, make a copy in the
2877 * recipient's mailbox and bump the reference count.
2879 if ((recps != NULL) && (recps->num_local > 0))
2884 pch = recps->recp_local;
2885 recps->recp_local = recipient;
2886 ntokens = num_tokens(pch, '|');
2887 for (i=0; i<ntokens; ++i)
2889 extract_token(recipient, pch, i, '|', sizeof recipient);
2890 syslog(LOG_DEBUG, "msgbase: delivering private local mail to <%s>", recipient);
2891 if (CtdlGetUser(&userbuf, recipient) == 0) {
2892 CtdlMailboxName(actual_rm, sizeof actual_rm, &userbuf, MAILROOM);
2893 CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0, msg);
2894 CtdlBumpNewMailCounter(userbuf.usernum);
2895 PerformMessageHooks(msg, recps, EVT_AFTERUSRMBOXSAVE);
2898 syslog(LOG_DEBUG, "msgbase: no user <%s>", recipient);
2899 CtdlSaveMsgPointerInRoom(CtdlGetConfigStr("c_aideroom"), newmsgid, 0, msg);
2902 recps->recp_local = pch;
2905 /* Perform "after save" hooks */
2906 syslog(LOG_DEBUG, "msgbase: performing after-save hooks");
2908 PerformMessageHooks(msg, recps, EVT_AFTERSAVE);
2909 CM_FlushField(msg, eVltMsgNum);
2911 /* Go back to the room we started from */
2912 syslog(LOG_DEBUG, "msgbase: returning to original room %s", hold_rm);
2913 if (strcasecmp(hold_rm, CC->room.QRname))
2914 CtdlUserGoto(hold_rm, 0, 1, NULL, NULL, NULL, NULL);
2917 * Any addresses to harvest for someone's address book?
2919 if ( (CC->logged_in) && (recps != NULL) ) {
2920 collected_addresses = harvest_collected_addresses(msg);
2923 if (collected_addresses != NULL) {
2924 aptr = (struct addresses_to_be_filed *)
2925 malloc(sizeof(struct addresses_to_be_filed));
2926 CtdlMailboxName(actual_rm, sizeof actual_rm,
2927 &CC->user, USERCONTACTSROOM);
2928 aptr->roomname = strdup(actual_rm);
2929 aptr->collected_addresses = collected_addresses;
2930 begin_critical_section(S_ATBF);
2933 end_critical_section(S_ATBF);
2937 * Determine whether this message qualifies for journaling.
2939 if (!CM_IsEmpty(msg, eJournal)) {
2940 qualified_for_journaling = 0;
2943 if (recps == NULL) {
2944 qualified_for_journaling = CtdlGetConfigInt("c_journal_pubmsgs");
2946 else if (recps->num_local + recps->num_ignet + recps->num_internet > 0) {
2947 qualified_for_journaling = CtdlGetConfigInt("c_journal_email");
2950 qualified_for_journaling = CtdlGetConfigInt("c_journal_pubmsgs");
2955 * Do we have to perform journaling? If so, hand off the saved
2956 * RFC822 version will be handed off to the journaler for background
2957 * submit. Otherwise, we have to free the memory ourselves.
2959 if (saved_rfc822_version != NULL) {
2960 if (qualified_for_journaling) {
2961 JournalBackgroundSubmit(msg, saved_rfc822_version, recps);
2964 FreeStrBuf(&saved_rfc822_version);
2968 if ((recps != NULL) && (recps->bounce_to == bounce_to))
2969 recps->bounce_to = NULL;
2977 * Convenience function for generating small administrative messages.
2979 long quickie_message(const char *from,
2980 const char *fromaddr,
2985 const char *subject)
2987 struct CtdlMessage *msg;
2988 recptypes *recp = NULL;
2990 msg = malloc(sizeof(struct CtdlMessage));
2991 memset(msg, 0, sizeof(struct CtdlMessage));
2992 msg->cm_magic = CTDLMESSAGE_MAGIC;
2993 msg->cm_anon_type = MES_NORMAL;
2994 msg->cm_format_type = format_type;
2996 if (!IsEmptyStr(from)) {
2997 CM_SetField(msg, eAuthor, from, strlen(from));
2999 else if (!IsEmptyStr(fromaddr)) {
3001 CM_SetField(msg, eAuthor, fromaddr, strlen(fromaddr));
3002 pAt = strchr(msg->cm_fields[eAuthor], '@');
3004 CM_CutFieldAt(msg, eAuthor, pAt - msg->cm_fields[eAuthor]);
3008 msg->cm_fields[eAuthor] = strdup("Citadel");
3011 if (!IsEmptyStr(fromaddr)) CM_SetField(msg, erFc822Addr, fromaddr, strlen(fromaddr));
3012 if (!IsEmptyStr(room)) CM_SetField(msg, eOriginalRoom, room, strlen(room));
3013 if (!IsEmptyStr(to)) {
3014 CM_SetField(msg, eRecipient, to, strlen(to));
3015 recp = validate_recipients(to, NULL, 0);
3017 if (!IsEmptyStr(subject)) {
3018 CM_SetField(msg, eMsgSubject, subject, strlen(subject));
3020 if (!IsEmptyStr(text)) {
3021 CM_SetField(msg, eMesageText, text, strlen(text));
3024 long msgnum = CtdlSubmitMsg(msg, recp, room, 0);
3026 if (recp != NULL) free_recipients(recp);
3032 * Back end function used by CtdlMakeMessage() and similar functions
3034 StrBuf *CtdlReadMessageBodyBuf(char *terminator, /* token signalling EOT */
3036 size_t maxlen, /* maximum message length */
3037 StrBuf *exist, /* if non-null, append to it;
3038 exist is ALWAYS freed */
3039 int crlf /* CRLF newlines instead of LF */
3048 LineBuf = NewStrBufPlain(NULL, SIZ);
3049 if (exist == NULL) {
3050 Message = NewStrBufPlain(NULL, 4 * SIZ);
3053 Message = NewStrBufDup(exist);
3056 /* Do we need to change leading ".." to "." for SMTP escaping? */
3057 if ((tlen == 1) && (*terminator == '.')) {
3061 /* read in the lines of message text one by one */
3063 if (CtdlClientGetLine(LineBuf) < 0) {
3066 if ((StrLength(LineBuf) == tlen) && (!strcmp(ChrPtr(LineBuf), terminator))) {
3069 if ( (!flushing) && (!finished) ) {
3071 StrBufAppendBufPlain(LineBuf, HKEY("\r\n"), 0);
3074 StrBufAppendBufPlain(LineBuf, HKEY("\n"), 0);
3077 /* Unescape SMTP-style input of two dots at the beginning of the line */
3078 if ((dotdot) && (StrLength(LineBuf) > 1) && (ChrPtr(LineBuf)[0] == '.')) {
3079 StrBufCutLeft(LineBuf, 1);
3081 StrBufAppendBuf(Message, LineBuf, 0);
3084 /* if we've hit the max msg length, flush the rest */
3085 if (StrLength(Message) >= maxlen) flushing = 1;
3087 } while (!finished);
3088 FreeStrBuf(&LineBuf);
3094 * Back end function used by CtdlMakeMessage() and similar functions
3096 char *CtdlReadMessageBody(char *terminator, /* token signalling EOT */
3098 size_t maxlen, /* maximum message length */
3099 StrBuf *exist, /* if non-null, append to it;
3100 exist is ALWAYS freed */
3101 int crlf /* CRLF newlines instead of LF */
3106 Message = CtdlReadMessageBodyBuf(terminator,
3112 if (Message == NULL)
3115 return SmashStrBuf(&Message);
3118 struct CtdlMessage *CtdlMakeMessage(
3119 struct ctdluser *author, /* author's user structure */
3120 char *recipient, /* NULL if it's not mail */
3121 char *recp_cc, /* NULL if it's not mail */
3122 char *room, /* room where it's going */
3123 int type, /* see MES_ types in header file */
3124 int format_type, /* variformat, plain text, MIME... */
3125 char *fake_name, /* who we're masquerading as */
3126 char *my_email, /* which of my email addresses to use (empty is ok) */
3127 char *subject, /* Subject (optional) */
3128 char *supplied_euid, /* ...or NULL if this is irrelevant */
3129 char *preformatted_text, /* ...or NULL to read text from client */
3130 char *references /* Thread references */
3133 return CtdlMakeMessageLen(
3134 author, /* author's user structure */
3135 recipient, /* NULL if it's not mail */
3136 (recipient)?strlen(recipient) : 0,
3137 recp_cc, /* NULL if it's not mail */
3138 (recp_cc)?strlen(recp_cc): 0,
3139 room, /* room where it's going */
3140 (room)?strlen(room): 0,
3141 type, /* see MES_ types in header file */
3142 format_type, /* variformat, plain text, MIME... */
3143 fake_name, /* who we're masquerading as */
3144 (fake_name)?strlen(fake_name): 0,
3145 my_email, /* which of my email addresses to use (empty is ok) */
3146 (my_email)?strlen(my_email): 0,
3147 subject, /* Subject (optional) */
3148 (subject)?strlen(subject): 0,
3149 supplied_euid, /* ...or NULL if this is irrelevant */
3150 (supplied_euid)?strlen(supplied_euid):0,
3151 preformatted_text, /* ...or NULL to read text from client */
3152 (preformatted_text)?strlen(preformatted_text) : 0,
3153 references, /* Thread references */
3154 (references)?strlen(references):0);
3159 * Build a binary message to be saved on disk.
3160 * (NOTE: if you supply 'preformatted_text', the buffer you give it
3161 * will become part of the message. This means you are no longer
3162 * responsible for managing that memory -- it will be freed along with
3163 * the rest of the fields when CM_Free() is called.)
3166 struct CtdlMessage *CtdlMakeMessageLen(
3167 struct ctdluser *author, /* author's user structure */
3168 char *recipient, /* NULL if it's not mail */
3170 char *recp_cc, /* NULL if it's not mail */
3172 char *room, /* room where it's going */
3174 int type, /* see MES_ types in header file */
3175 int format_type, /* variformat, plain text, MIME... */
3176 char *fake_name, /* who we're masquerading as */
3178 char *my_email, /* which of my email addresses to use (empty is ok) */
3180 char *subject, /* Subject (optional) */
3182 char *supplied_euid, /* ...or NULL if this is irrelevant */
3184 char *preformatted_text, /* ...or NULL to read text from client */
3186 char *references, /* Thread references */
3190 /* Don't confuse the poor folks if it's not routed mail. * /
3191 char dest_node[256] = "";*/
3194 struct CtdlMessage *msg;
3196 StrBuf *FakeEncAuthor = NULL;
3198 msg = malloc(sizeof(struct CtdlMessage));
3199 memset(msg, 0, sizeof(struct CtdlMessage));
3200 msg->cm_magic = CTDLMESSAGE_MAGIC;
3201 msg->cm_anon_type = type;
3202 msg->cm_format_type = format_type;
3204 if (recipient != NULL) rcplen = striplt(recipient);
3205 if (recp_cc != NULL) cclen = striplt(recp_cc);
3207 /* Path or Return-Path */
3209 CM_SetField(msg, eMessagePath, my_email, myelen);
3211 else if (!IsEmptyStr(author->fullname)) {
3212 CM_SetField(msg, eMessagePath, author->fullname, strlen(author->fullname));
3214 convert_spaces_to_underscores(msg->cm_fields[eMessagePath]);
3216 blen = snprintf(buf, sizeof buf, "%ld", (long)time(NULL));
3217 CM_SetField(msg, eTimestamp, buf, blen);
3220 FakeAuthor = NewStrBufPlain (fake_name, fnlen);
3223 FakeAuthor = NewStrBufPlain (author->fullname, -1);
3225 StrBufRFC2047encode(&FakeEncAuthor, FakeAuthor);
3226 CM_SetAsFieldSB(msg, eAuthor, &FakeEncAuthor);
3227 FreeStrBuf(&FakeAuthor);
3229 if (!!IsEmptyStr(CC->room.QRname)) {
3230 if (CC->room.QRflags & QR_MAILBOX) { /* room */
3231 CM_SetField(msg, eOriginalRoom, &CC->room.QRname[11], strlen(&CC->room.QRname[11]));
3234 CM_SetField(msg, eOriginalRoom, CC->room.QRname, strlen(CC->room.QRname));
3239 CM_SetField(msg, eRecipient, recipient, rcplen);
3242 CM_SetField(msg, eCarbonCopY, recp_cc, cclen);
3246 CM_SetField(msg, erFc822Addr, my_email, myelen);
3248 else if ( (author == &CC->user) && (!IsEmptyStr(CC->cs_inet_email)) ) {
3249 CM_SetField(msg, erFc822Addr, CC->cs_inet_email, strlen(CC->cs_inet_email));
3252 if (subject != NULL) {
3254 length = striplt(subject);
3260 while ((subject[i] != '\0') &&
3261 (IsAscii = isascii(subject[i]) != 0 ))
3264 CM_SetField(msg, eMsgSubject, subject, subjlen);
3265 else /* ok, we've got utf8 in the string. */
3268 rfc2047Subj = rfc2047encode(subject, length);
3269 CM_SetAsField(msg, eMsgSubject, &rfc2047Subj, strlen(rfc2047Subj));
3276 CM_SetField(msg, eExclusiveID, supplied_euid, euidlen);
3280 CM_SetField(msg, eWeferences, references, reflen);
3283 if (preformatted_text != NULL) {
3284 CM_SetField(msg, eMesageText, preformatted_text, textlen);
3288 MsgBody = CtdlReadMessageBodyBuf(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
3289 if (MsgBody != NULL) {
3290 CM_SetAsFieldSB(msg, eMesageText, &MsgBody);
3301 * API function to delete messages which match a set of criteria
3302 * (returns the actual number of messages deleted)
3304 int CtdlDeleteMessages(const char *room_name, /* which room */
3305 long *dmsgnums, /* array of msg numbers to be deleted */
3306 int num_dmsgnums, /* number of msgs to be deleted, or 0 for "any" */
3307 char *content_type /* or "" for any. regular expressions expected. */
3310 struct ctdlroom qrbuf;
3311 struct cdbdata *cdbfr;
3312 long *msglist = NULL;
3313 long *dellist = NULL;
3316 int num_deleted = 0;
3318 struct MetaData smi;
3321 int need_to_free_re = 0;
3323 if (content_type) if (!IsEmptyStr(content_type)) {
3324 regcomp(&re, content_type, 0);
3325 need_to_free_re = 1;
3327 syslog(LOG_DEBUG, "msgbase: CtdlDeleteMessages(%s, %d msgs, %s)", room_name, num_dmsgnums, content_type);
3329 /* get room record, obtaining a lock... */
3330 if (CtdlGetRoomLock(&qrbuf, room_name) != 0) {
3331 syslog(LOG_ERR, "msgbase: CtdlDeleteMessages(): Room <%s> not found", room_name);
3332 if (need_to_free_re) regfree(&re);
3333 return(0); /* room not found */
3335 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
3337 if (cdbfr != NULL) {
3338 dellist = malloc(cdbfr->len);
3339 msglist = (long *) cdbfr->ptr;
3340 cdbfr->ptr = NULL; /* CtdlDeleteMessages() now owns this memory */
3341 num_msgs = cdbfr->len / sizeof(long);
3345 int have_contenttype = (content_type != NULL) && !IsEmptyStr(content_type);
3346 int have_delmsgs = (num_dmsgnums == 0) || (dmsgnums == NULL);
3347 int have_more_del = 1;
3349 num_msgs = sort_msglist(msglist, num_msgs);
3350 if (num_dmsgnums > 1)
3351 num_dmsgnums = sort_msglist(dmsgnums, num_dmsgnums);
3354 StrBuf *dbg = NewStrBuf();
3355 for (i = 0; i < num_dmsgnums; i++)
3356 StrBufAppendPrintf(dbg, ", %ld", dmsgnums[i]);
3357 syslog(LOG_DEBUG, "msgbase: Deleting before: %s", ChrPtr(dbg));
3362 while ((i < num_msgs) && (have_more_del)) {
3365 /* Set/clear a bit for each criterion */
3367 /* 0 messages in the list or a null list means that we are
3368 * interested in deleting any messages which meet the other criteria.
3371 delete_this |= 0x01;
3374 while ((i < num_msgs) && (msglist[i] < dmsgnums[j])) i++;
3379 if (msglist[i] == dmsgnums[j]) {
3380 delete_this |= 0x01;
3383 have_more_del = (j < num_dmsgnums);
3386 if (have_contenttype) {
3387 GetMetaData(&smi, msglist[i]);
3388 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) == 0) {
3389 delete_this |= 0x02;
3392 delete_this |= 0x02;
3395 /* Delete message only if all bits are set */
3396 if (delete_this == 0x03) {
3397 dellist[num_deleted++] = msglist[i];
3404 StrBuf *dbg = NewStrBuf();
3405 for (i = 0; i < num_deleted; i++)
3406 StrBufAppendPrintf(dbg, ", %ld", dellist[i]);
3407 syslog(LOG_DEBUG, "msgbase: Deleting: %s", ChrPtr(dbg));
3411 num_msgs = sort_msglist(msglist, num_msgs);
3412 cdb_store(CDB_MSGLISTS, &qrbuf.QRnumber, (int)sizeof(long),
3413 msglist, (int)(num_msgs * sizeof(long)));
3416 qrbuf.QRhighest = msglist[num_msgs - 1];
3418 qrbuf.QRhighest = 0;
3420 CtdlPutRoomLock(&qrbuf);
3422 /* Go through the messages we pulled out of the index, and decrement
3423 * their reference counts by 1. If this is the only room the message
3424 * was in, the reference count will reach zero and the message will
3425 * automatically be deleted from the database. We do this in a
3426 * separate pass because there might be plug-in hooks getting called,
3427 * and we don't want that happening during an S_ROOMS critical
3431 for (i=0; i<num_deleted; ++i) {
3432 PerformDeleteHooks(qrbuf.QRname, dellist[i]);
3434 AdjRefCountList(dellist, num_deleted, -1);
3436 /* Now free the memory we used, and go away. */
3437 if (msglist != NULL) free(msglist);
3438 if (dellist != NULL) free(dellist);
3439 syslog(LOG_DEBUG, "msgbase: %d message(s) deleted", num_deleted);
3440 if (need_to_free_re) regfree(&re);
3441 return (num_deleted);
3446 * GetMetaData() - Get the supplementary record for a message
3448 void GetMetaData(struct MetaData *smibuf, long msgnum)
3450 struct cdbdata *cdbsmi;
3453 memset(smibuf, 0, sizeof(struct MetaData));
3454 smibuf->meta_msgnum = msgnum;
3455 smibuf->meta_refcount = 1; /* Default reference count is 1 */
3457 /* Use the negative of the message number for its supp record index */
3458 TheIndex = (0L - msgnum);
3460 cdbsmi = cdb_fetch(CDB_MSGMAIN, &TheIndex, sizeof(long));
3461 if (cdbsmi == NULL) {
3462 return; /* record not found; leave it alone */
3464 memcpy(smibuf, cdbsmi->ptr,
3465 ((cdbsmi->len > sizeof(struct MetaData)) ?
3466 sizeof(struct MetaData) : cdbsmi->len)
3474 * PutMetaData() - (re)write supplementary record for a message
3476 void PutMetaData(struct MetaData *smibuf)
3480 /* Use the negative of the message number for the metadata db index */
3481 TheIndex = (0L - smibuf->meta_msgnum);
3483 cdb_store(CDB_MSGMAIN,
3484 &TheIndex, (int)sizeof(long),
3485 smibuf, (int)sizeof(struct MetaData)
3491 * Convenience function to process a big block of AdjRefCount() operations
3493 void AdjRefCountList(long *msgnum, long nmsg, int incr)
3497 for (i = 0; i < nmsg; i++) {
3498 AdjRefCount(msgnum[i], incr);
3504 * AdjRefCount - adjust the reference count for a message. We need to delete from disk any message whose reference count reaches zero.
3506 void AdjRefCount(long msgnum, int incr)
3508 struct MetaData smi;
3511 /* This is a *tight* critical section; please keep it that way, as
3512 * it may get called while nested in other critical sections.
3513 * Complicating this any further will surely cause deadlock!
3515 begin_critical_section(S_SUPPMSGMAIN);
3516 GetMetaData(&smi, msgnum);
3517 smi.meta_refcount += incr;
3519 end_critical_section(S_SUPPMSGMAIN);
3520 syslog(LOG_DEBUG, "msgbase: AdjRefCount() msg %ld ref count delta %+d, is now %d", msgnum, incr, smi.meta_refcount);
3522 /* If the reference count is now zero, delete both the message and its metadata record.
3524 if (smi.meta_refcount == 0) {
3525 syslog(LOG_DEBUG, "msgbase: deleting message <%ld>", msgnum);
3527 /* Call delete hooks with NULL room to show it has gone altogether */
3528 PerformDeleteHooks(NULL, msgnum);
3530 /* Remove from message base */
3532 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3533 cdb_delete(CDB_BIGMSGS, &delnum, (int)sizeof(long));
3535 /* Remove metadata record */
3536 delnum = (0L - msgnum);
3537 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3543 * Write a generic object to this room
3545 * Note: this could be much more efficient. Right now we use two temporary
3546 * files, and still pull the message into memory as with all others.
3548 void CtdlWriteObject(char *req_room, /* Room to stuff it in */
3549 char *content_type, /* MIME type of this object */
3550 char *raw_message, /* Data to be written */
3551 off_t raw_length, /* Size of raw_message */
3552 struct ctdluser *is_mailbox, /* Mailbox room? */
3553 int is_binary, /* Is encoding necessary? */
3554 int is_unique, /* Del others of this type? */
3555 unsigned int flags /* Internal save flags */
3558 struct ctdlroom qrbuf;
3559 char roomname[ROOMNAMELEN];
3560 struct CtdlMessage *msg;
3561 StrBuf *encoded_message = NULL;
3563 if (is_mailbox != NULL) {
3564 CtdlMailboxName(roomname, sizeof roomname, is_mailbox, req_room);
3567 safestrncpy(roomname, req_room, sizeof(roomname));
3570 syslog(LOG_DEBUG, "msfbase: raw length is %ld", (long)raw_length);
3573 encoded_message = NewStrBufPlain(NULL, (size_t) (((raw_length * 134) / 100) + 4096 ) );
3576 encoded_message = NewStrBufPlain(NULL, (size_t)(raw_length + 4096));
3579 StrBufAppendBufPlain(encoded_message, HKEY("Content-type: "), 0);
3580 StrBufAppendBufPlain(encoded_message, content_type, -1, 0);
3581 StrBufAppendBufPlain(encoded_message, HKEY("\n"), 0);
3584 StrBufAppendBufPlain(encoded_message, HKEY("Content-transfer-encoding: base64\n\n"), 0);
3587 StrBufAppendBufPlain(encoded_message, HKEY("Content-transfer-encoding: 7bit\n\n"), 0);
3591 StrBufBase64Append(encoded_message, NULL, raw_message, raw_length, 0);
3594 StrBufAppendBufPlain(encoded_message, raw_message, raw_length, 0);
3597 syslog(LOG_DEBUG, "msgbase: allocating");
3598 msg = malloc(sizeof(struct CtdlMessage));
3599 memset(msg, 0, sizeof(struct CtdlMessage));
3600 msg->cm_magic = CTDLMESSAGE_MAGIC;
3601 msg->cm_anon_type = MES_NORMAL;
3602 msg->cm_format_type = 4;
3603 CM_SetField(msg, eAuthor, CC->user.fullname, strlen(CC->user.fullname));
3604 CM_SetField(msg, eOriginalRoom, req_room, strlen(req_room));
3605 msg->cm_flags = flags;
3607 CM_SetAsFieldSB(msg, eMesageText, &encoded_message);
3609 /* Create the requested room if we have to. */
3610 if (CtdlGetRoom(&qrbuf, roomname) != 0) {
3611 CtdlCreateRoom(roomname, ( (is_mailbox != NULL) ? 5 : 3 ), "", 0, 1, 0, VIEW_BBS);
3613 /* If the caller specified this object as unique, delete all
3614 * other objects of this type that are currently in the room.
3617 syslog(LOG_DEBUG, "msgbase: deleted %d other msgs of this type",
3618 CtdlDeleteMessages(roomname, NULL, 0, content_type)
3621 /* Now write the data */
3622 CtdlSubmitMsg(msg, NULL, roomname, 0);
3627 /************************************************************************/
3628 /* MODULE INITIALIZATION */
3629 /************************************************************************/
3631 CTDL_MODULE_INIT(msgbase)
3634 FillMsgKeyLookupTable();
3637 /* return our module id for the log */