2 * Implements the message store.
4 * Copyright (c) 1987-2021 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
80 HashList *msgKeyLookup = NULL;
82 int GetFieldFromMnemonic(eMsgField *f, const char* c)
85 if (GetHash(msgKeyLookup, c, 4, &v)) {
92 void FillMsgKeyLookupTable(void)
96 msgKeyLookup = NewHash (1, FourHash);
98 for (i=0; i < 91; i++) {
99 if (msgkeys[i] != NULL) {
100 Put(msgKeyLookup, msgkeys[i], 4, (void*)i, reference_free_handler);
106 eMsgField FieldOrder[] = {
107 /* Important fields */
115 /* Semi-important fields */
120 /* G is not used yet */
123 /* Q is not used yet */
125 /* X is not used yet */
126 /* Z is not used yet */
133 /* Message text (MUST be last) */
135 /* Not saved to disk:
140 static const long NDiskFields = sizeof(FieldOrder) / sizeof(eMsgField);
143 int CM_IsEmpty(struct CtdlMessage *Msg, eMsgField which) {
144 return !((Msg->cm_fields[which] != NULL) && (Msg->cm_fields[which][0] != '\0'));
148 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 if (length < 0) { // You can set the length to -1 to have CM_SetField measure it for you
153 length = strlen(buf);
155 Msg->cm_fields[which] = malloc(length + 1);
156 memcpy(Msg->cm_fields[which], buf, length);
157 Msg->cm_fields[which][length] = '\0';
158 Msg->cm_lengths[which] = length;
162 void CM_SetFieldLONG(struct CtdlMessage *Msg, eMsgField which, long lvalue) {
165 len = snprintf(buf, sizeof(buf), "%ld", lvalue);
166 CM_SetField(Msg, which, buf, len);
170 void CM_CutFieldAt(struct CtdlMessage *Msg, eMsgField WhichToCut, long maxlen) {
171 if (Msg->cm_fields[WhichToCut] == NULL)
174 if (Msg->cm_lengths[WhichToCut] > maxlen)
176 Msg->cm_fields[WhichToCut][maxlen] = '\0';
177 Msg->cm_lengths[WhichToCut] = maxlen;
182 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) {
193 if (CM_IsValidMsg(Msg) == 0) {
197 for (i = 0; i < 256; ++i) {
198 CM_FlushField(Msg, i);
203 void CM_CopyField(struct CtdlMessage *Msg, eMsgField WhichToPutTo, eMsgField WhichtToCopy) {
205 if (Msg->cm_fields[WhichToPutTo] != NULL) {
206 free (Msg->cm_fields[WhichToPutTo]);
209 if (Msg->cm_fields[WhichtToCopy] != NULL) {
210 len = Msg->cm_lengths[WhichtToCopy];
211 Msg->cm_fields[WhichToPutTo] = malloc(len + 1);
212 memcpy(Msg->cm_fields[WhichToPutTo], Msg->cm_fields[WhichtToCopy], len);
213 Msg->cm_fields[WhichToPutTo][len] = '\0';
214 Msg->cm_lengths[WhichToPutTo] = len;
217 Msg->cm_fields[WhichToPutTo] = NULL;
218 Msg->cm_lengths[WhichToPutTo] = 0;
223 void CM_PrependToField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length) {
224 if (Msg->cm_fields[which] != NULL) {
229 oldmsgsize = Msg->cm_lengths[which] + 1;
230 newmsgsize = length + oldmsgsize;
232 new = malloc(newmsgsize);
233 memcpy(new, buf, length);
234 memcpy(new + length, Msg->cm_fields[which], oldmsgsize);
235 free(Msg->cm_fields[which]);
236 Msg->cm_fields[which] = new;
237 Msg->cm_lengths[which] = newmsgsize - 1;
240 Msg->cm_fields[which] = malloc(length + 1);
241 memcpy(Msg->cm_fields[which], buf, length);
242 Msg->cm_fields[which][length] = '\0';
243 Msg->cm_lengths[which] = length;
248 void CM_SetAsField(struct CtdlMessage *Msg, eMsgField which, char **buf, long length) {
249 if (Msg->cm_fields[which] != NULL) {
250 free (Msg->cm_fields[which]);
253 Msg->cm_fields[which] = *buf;
255 if (length < 0) { // You can set the length to -1 to have CM_SetField measure it for you
256 Msg->cm_lengths[which] = strlen(Msg->cm_fields[which]);
259 Msg->cm_lengths[which] = length;
264 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) {
275 if (Msg->cm_fields[which] != NULL) {
276 *retlen = Msg->cm_lengths[which];
277 *ret = Msg->cm_fields[which];
278 Msg->cm_fields[which] = NULL;
279 Msg->cm_lengths[which] = 0;
289 * Returns 1 if the supplied pointer points to a valid Citadel message.
290 * If the pointer is NULL or the magic number check fails, returns 0.
292 int CM_IsValidMsg(struct CtdlMessage *msg) {
296 if ((msg->cm_magic) != CTDLMESSAGE_MAGIC) {
297 syslog(LOG_WARNING, "msgbase: CM_IsValidMsg() self-check failed");
304 void CM_FreeContents(struct CtdlMessage *msg) {
307 for (i = 0; i < 256; ++i)
308 if (msg->cm_fields[i] != NULL) {
309 free(msg->cm_fields[i]);
310 msg->cm_lengths[i] = 0;
313 msg->cm_magic = 0; /* just in case */
318 * 'Destructor' for struct CtdlMessage
320 void CM_Free(struct CtdlMessage *msg) {
321 if (CM_IsValidMsg(msg) == 0) {
322 if (msg != NULL) free (msg);
325 CM_FreeContents(msg);
330 int CM_DupField(eMsgField i, struct CtdlMessage *OrgMsg, struct CtdlMessage *NewMsg) {
332 len = OrgMsg->cm_lengths[i];
333 NewMsg->cm_fields[i] = malloc(len + 1);
334 if (NewMsg->cm_fields[i] == NULL) {
337 memcpy(NewMsg->cm_fields[i], OrgMsg->cm_fields[i], len);
338 NewMsg->cm_fields[i][len] = '\0';
339 NewMsg->cm_lengths[i] = len;
344 struct CtdlMessage *CM_Duplicate(struct CtdlMessage *OrgMsg) {
346 struct CtdlMessage *NewMsg;
348 if (CM_IsValidMsg(OrgMsg) == 0) {
351 NewMsg = (struct CtdlMessage *)malloc(sizeof(struct CtdlMessage));
352 if (NewMsg == NULL) {
356 memcpy(NewMsg, OrgMsg, sizeof(struct CtdlMessage));
358 memset(&NewMsg->cm_fields, 0, sizeof(char*) * 256);
360 for (i = 0; i < 256; ++i) {
361 if (OrgMsg->cm_fields[i] != NULL) {
362 if (!CM_DupField(i, OrgMsg, NewMsg)) {
373 /* Determine if a given message matches the fields in a message template.
374 * Return 0 for a successful match.
376 int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template) {
379 /* If there aren't any fields in the template, all messages will
382 if (template == NULL) return(0);
384 /* Null messages are bogus. */
385 if (msg == NULL) return(1);
387 for (i='A'; i<='Z'; ++i) {
388 if (template->cm_fields[i] != NULL) {
389 if (msg->cm_fields[i] == NULL) {
390 /* Considered equal if temmplate is empty string */
391 if (IsEmptyStr(template->cm_fields[i])) continue;
394 if ((template->cm_lengths[i] != msg->cm_lengths[i]) ||
395 (strcasecmp(msg->cm_fields[i], template->cm_fields[i])))
400 /* All compares succeeded: we have a match! */
406 * Retrieve the "seen" message list for the current room.
408 void CtdlGetSeen(char *buf, int which_set) {
411 /* Learn about the user and room in question */
412 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
414 if (which_set == ctdlsetseen_seen) {
415 safestrncpy(buf, vbuf.v_seen, SIZ);
417 if (which_set == ctdlsetseen_answered) {
418 safestrncpy(buf, vbuf.v_answered, SIZ);
424 * Manipulate the "seen msgs" string (or other message set strings)
426 void CtdlSetSeen(long *target_msgnums, int num_target_msgnums,
427 int target_setting, int which_set,
428 struct ctdluser *which_user, struct ctdlroom *which_room) {
429 struct cdbdata *cdbfr;
443 char *is_set; /* actually an array of booleans */
445 /* Don't bother doing *anything* if we were passed a list of zero messages */
446 if (num_target_msgnums < 1) {
450 /* If no room was specified, we go with the current room. */
452 which_room = &CC->room;
455 /* If no user was specified, we go with the current user. */
457 which_user = &CC->user;
460 syslog(LOG_DEBUG, "msgbase: CtdlSetSeen(%d msgs starting with %ld, %s, %d) in <%s>",
461 num_target_msgnums, target_msgnums[0],
462 (target_setting ? "SET" : "CLEAR"),
466 /* Learn about the user and room in question */
467 CtdlGetRelationship(&vbuf, which_user, which_room);
469 /* Load the message list */
470 cdbfr = cdb_fetch(CDB_MSGLISTS, &which_room->QRnumber, sizeof(long));
472 msglist = (long *) cdbfr->ptr;
473 cdbfr->ptr = NULL; /* CtdlSetSeen() now owns this memory */
474 num_msgs = cdbfr->len / sizeof(long);
478 return; /* No messages at all? No further action. */
481 is_set = malloc(num_msgs * sizeof(char));
482 memset(is_set, 0, (num_msgs * sizeof(char)) );
484 /* Decide which message set we're manipulating */
486 case ctdlsetseen_seen:
487 vset = NewStrBufPlain(vbuf.v_seen, -1);
489 case ctdlsetseen_answered:
490 vset = NewStrBufPlain(vbuf.v_answered, -1);
497 #if 0 /* This is a special diagnostic section. Do not allow it to run during normal operation. */
498 syslog(LOG_DEBUG, "There are %d messages in the room.\n", num_msgs);
499 for (i=0; i<num_msgs; ++i) {
500 if ((i > 0) && (msglist[i] <= msglist[i-1])) abort();
502 syslog(LOG_DEBUG, "We are twiddling %d of them.\n", num_target_msgnums);
503 for (k=0; k<num_target_msgnums; ++k) {
504 if ((k > 0) && (target_msgnums[k] <= target_msgnums[k-1])) abort();
508 /* Translate the existing sequence set into an array of booleans */
509 setstr = NewStrBuf();
513 while (StrBufExtract_NextToken(setstr, vset, &pvset, ',') >= 0) {
515 StrBufExtract_token(lostr, setstr, 0, ':');
516 if (StrBufNum_tokens(setstr, ':') >= 2) {
517 StrBufExtract_token(histr, setstr, 1, ':');
521 StrBufAppendBuf(histr, lostr, 0);
524 if (!strcmp(ChrPtr(histr), "*")) {
531 for (i = 0; i < num_msgs; ++i) {
532 if ((msglist[i] >= lo) && (msglist[i] <= hi)) {
541 /* Now translate the array of booleans back into a sequence set */
547 for (i=0; i<num_msgs; ++i) {
551 for (k=0; k<num_target_msgnums; ++k) {
552 if (msglist[i] == target_msgnums[k]) {
553 is_seen = target_setting;
557 if ((was_seen == 0) && (is_seen == 1)) {
560 else if ((was_seen == 1) && (is_seen == 0)) {
563 if (StrLength(vset) > 0) {
564 StrBufAppendBufPlain(vset, HKEY(","), 0);
567 StrBufAppendPrintf(vset, "%ld", hi);
570 StrBufAppendPrintf(vset, "%ld:%ld", lo, hi);
574 if ((is_seen) && (i == num_msgs - 1)) {
575 if (StrLength(vset) > 0) {
576 StrBufAppendBufPlain(vset, HKEY(","), 0);
578 if ((i==0) || (was_seen == 0)) {
579 StrBufAppendPrintf(vset, "%ld", msglist[i]);
582 StrBufAppendPrintf(vset, "%ld:%ld", lo, msglist[i]);
590 * We will have to stuff this string back into a 4096 byte buffer, so if it's
591 * larger than that now, truncate it by removing tokens from the beginning.
592 * The limit of 100 iterations is there to prevent an infinite loop in case
593 * something unexpected happens.
595 int number_of_truncations = 0;
596 while ( (StrLength(vset) > SIZ) && (number_of_truncations < 100) ) {
597 StrBufRemove_token(vset, 0, ',');
598 ++number_of_truncations;
602 * If we're truncating the sequence set of messages marked with the 'seen' flag,
603 * we want the earliest messages (the truncated ones) to be marked, not unmarked.
604 * Otherwise messages at the beginning will suddenly appear to be 'unseen'.
606 if ( (which_set == ctdlsetseen_seen) && (number_of_truncations > 0) ) {
608 first_tok = NewStrBuf();
609 StrBufExtract_token(first_tok, vset, 0, ',');
610 StrBufRemove_token(vset, 0, ',');
612 if (StrBufNum_tokens(first_tok, ':') > 1) {
613 StrBufRemove_token(first_tok, 0, ':');
617 new_set = NewStrBuf();
618 StrBufAppendBufPlain(new_set, HKEY("1:"), 0);
619 StrBufAppendBuf(new_set, first_tok, 0);
620 StrBufAppendBufPlain(new_set, HKEY(":"), 0);
621 StrBufAppendBuf(new_set, vset, 0);
624 FreeStrBuf(&first_tok);
628 /* Decide which message set we're manipulating */
630 case ctdlsetseen_seen:
631 safestrncpy(vbuf.v_seen, ChrPtr(vset), sizeof vbuf.v_seen);
633 case ctdlsetseen_answered:
634 safestrncpy(vbuf.v_answered, ChrPtr(vset), sizeof vbuf.v_answered);
640 CtdlSetRelationship(&vbuf, which_user, which_room);
646 * API function to perform an operation for each qualifying message in the
647 * current room. (Returns the number of messages processed.)
649 int CtdlForEachMessage(int mode, long ref, char *search_string,
651 struct CtdlMessage *compare,
652 ForEachMsgCallback CallBack,
657 struct cdbdata *cdbfr;
658 long *msglist = NULL;
660 int num_processed = 0;
663 struct CtdlMessage *msg = NULL;
666 int printed_lastold = 0;
667 int num_search_msgs = 0;
668 long *search_msgs = NULL;
670 int need_to_free_re = 0;
673 if ((content_type) && (!IsEmptyStr(content_type))) {
674 regcomp(&re, content_type, 0);
678 /* Learn about the user and room in question */
679 if (server_shutting_down) {
680 if (need_to_free_re) regfree(&re);
683 CtdlGetUser(&CC->user, CC->curr_user);
685 if (server_shutting_down) {
686 if (need_to_free_re) regfree(&re);
689 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
691 if (server_shutting_down) {
692 if (need_to_free_re) regfree(&re);
696 /* Load the message list */
697 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
699 if (need_to_free_re) regfree(&re);
700 return 0; /* No messages at all? No further action. */
703 msglist = (long *) cdbfr->ptr;
704 num_msgs = cdbfr->len / sizeof(long);
706 cdbfr->ptr = NULL; /* clear this so that cdb_free() doesn't free it */
707 cdb_free(cdbfr); /* we own this memory now */
710 * Now begin the traversal.
712 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
714 /* If the caller is looking for a specific MIME type, filter
715 * out all messages which are not of the type requested.
717 if ((content_type != NULL) && (!IsEmptyStr(content_type))) {
719 /* This call to GetMetaData() sits inside this loop
720 * so that we only do the extra database read per msg
721 * if we need to. Doing the extra read all the time
722 * really kills the server. If we ever need to use
723 * metadata for another search criterion, we need to
724 * move the read somewhere else -- but still be smart
725 * enough to only do the read if the caller has
726 * specified something that will need it.
728 if (server_shutting_down) {
729 if (need_to_free_re) regfree(&re);
733 GetMetaData(&smi, msglist[a]);
735 /* if (strcasecmp(smi.meta_content_type, content_type)) { old non-regex way */
736 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) != 0) {
742 num_msgs = sort_msglist(msglist, num_msgs);
744 /* If a template was supplied, filter out the messages which
745 * don't match. (This could induce some delays!)
748 if (compare != NULL) {
749 for (a = 0; a < num_msgs; ++a) {
750 if (server_shutting_down) {
751 if (need_to_free_re) regfree(&re);
755 msg = CtdlFetchMessage(msglist[a], 1);
757 if (CtdlMsgCmp(msg, compare)) {
766 /* If a search string was specified, get a message list from
767 * the full text index and remove messages which aren't on both
771 * Since the lists are sorted and strictly ascending, and the
772 * output list is guaranteed to be shorter than or equal to the
773 * input list, we overwrite the bottom of the input list. This
774 * eliminates the need to memmove big chunks of the list over and
777 if ( (num_msgs > 0) && (mode == MSGS_SEARCH) && (search_string) ) {
779 /* Call search module via hook mechanism.
780 * NULL means use any search function available.
781 * otherwise replace with a char * to name of search routine
783 CtdlModuleDoSearch(&num_search_msgs, &search_msgs, search_string, "fulltext");
785 if (num_search_msgs > 0) {
789 orig_num_msgs = num_msgs;
791 for (i=0; i<orig_num_msgs; ++i) {
792 for (j=0; j<num_search_msgs; ++j) {
793 if (msglist[i] == search_msgs[j]) {
794 msglist[num_msgs++] = msglist[i];
800 num_msgs = 0; /* No messages qualify */
802 if (search_msgs != NULL) free(search_msgs);
804 /* Now that we've purged messages which don't contain the search
805 * string, treat a MSGS_SEARCH just like a MSGS_ALL from this
812 * Now iterate through the message list, according to the
813 * criteria supplied by the caller.
816 for (a = 0; a < num_msgs; ++a) {
817 if (server_shutting_down) {
818 if (need_to_free_re) regfree(&re);
820 return num_processed;
822 thismsg = msglist[a];
823 if (mode == MSGS_ALL) {
827 is_seen = is_msg_in_sequence_set(
828 vbuf.v_seen, thismsg);
829 if (is_seen) lastold = thismsg;
835 || ((mode == MSGS_OLD) && (is_seen))
836 || ((mode == MSGS_NEW) && (!is_seen))
837 || ((mode == MSGS_LAST) && (a >= (num_msgs - ref)))
838 || ((mode == MSGS_FIRST) && (a < ref))
839 || ((mode == MSGS_GT) && (thismsg > ref))
840 || ((mode == MSGS_LT) && (thismsg < ref))
841 || ((mode == MSGS_EQ) && (thismsg == ref))
844 if ((mode == MSGS_NEW) && (CC->user.flags & US_LASTOLD) && (lastold > 0L) && (printed_lastold == 0) && (!is_seen)) {
846 CallBack(lastold, userdata);
852 CallBack(thismsg, userdata);
857 if (need_to_free_re) regfree(&re);
860 * We cache the most recent msglist in order to do security checks later
862 if (CC->client_socket > 0) {
863 if (CC->cached_msglist != NULL) {
864 free(CC->cached_msglist);
866 CC->cached_msglist = msglist;
867 CC->cached_num_msgs = num_msgs;
873 return num_processed;
878 * memfmout() - Citadel text formatter and paginator.
879 * Although the original purpose of this routine was to format
880 * text to the reader's screen width, all we're really using it
881 * for here is to format text out to 80 columns before sending it
882 * to the client. The client software may reformat it again.
885 char *mptr, /* where are we going to get our text from? */
886 const char *nl /* string to terminate lines with */
889 unsigned char ch = 0;
896 while (ch=*(mptr++), ch != 0) {
899 if (client_write(outbuf, len) == -1) {
900 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
904 if (client_write(nl, nllen) == -1) {
905 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
910 else if (ch == '\r') {
911 /* Ignore carriage returns. Newlines are always LF or CRLF but never CR. */
913 else if (isspace(ch)) {
914 if (column > 72) { /* Beyond 72 columns, break on the next space */
915 if (client_write(outbuf, len) == -1) {
916 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
920 if (client_write(nl, nllen) == -1) {
921 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
934 if (column > 1000) { /* Beyond 1000 columns, break anywhere */
935 if (client_write(outbuf, len) == -1) {
936 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
940 if (client_write(nl, nllen) == -1) {
941 syslog(LOG_ERR, "msgbase: memfmout(): aborting due to write failure");
949 if (client_write(outbuf, len) == -1) {
950 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
953 client_write(nl, nllen);
960 * Callback function for mime parser that simply lists the part
962 void list_this_part(char *name, char *filename, char *partnum, char *disp,
963 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
964 char *cbid, void *cbuserdata)
968 ma = (struct ma_info *)cbuserdata;
969 if (ma->is_ma == 0) {
970 cprintf("part=%s|%s|%s|%s|%s|%ld|%s|%s\n",
984 * Callback function for multipart prefix
986 void list_this_pref(char *name, char *filename, char *partnum, char *disp,
987 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
988 char *cbid, void *cbuserdata)
992 ma = (struct ma_info *)cbuserdata;
993 if (!strcasecmp(cbtype, "multipart/alternative")) {
997 if (ma->is_ma == 0) {
998 cprintf("pref=%s|%s\n", partnum, cbtype);
1004 * Callback function for multipart sufffix
1006 void list_this_suff(char *name, char *filename, char *partnum, char *disp,
1007 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1008 char *cbid, void *cbuserdata)
1012 ma = (struct ma_info *)cbuserdata;
1013 if (ma->is_ma == 0) {
1014 cprintf("suff=%s|%s\n", partnum, cbtype);
1016 if (!strcasecmp(cbtype, "multipart/alternative")) {
1023 * Callback function for mime parser that opens a section for downloading
1024 * we use serv_files function here:
1026 extern void OpenCmdResult(char *filename, const char *mime_type);
1027 void mime_download(char *name, char *filename, char *partnum, char *disp,
1028 void *content, char *cbtype, char *cbcharset, size_t length,
1029 char *encoding, char *cbid, void *cbuserdata)
1033 /* Silently go away if there's already a download open. */
1034 if (CC->download_fp != NULL)
1038 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
1039 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
1041 CC->download_fp = tmpfile();
1042 if (CC->download_fp == NULL) {
1043 syslog(LOG_ERR, "msgbase: mime_download() couldn't write: %m");
1044 cprintf("%d cannot open temporary file: %s\n", ERROR + INTERNAL_ERROR, strerror(errno));
1048 rv = fwrite(content, length, 1, CC->download_fp);
1050 syslog(LOG_ERR, "msgbase: mime_download() Couldn't write: %m");
1051 cprintf("%d unable to write tempfile.\n", ERROR + TOO_BIG);
1052 fclose(CC->download_fp);
1053 CC->download_fp = NULL;
1056 fflush(CC->download_fp);
1057 rewind(CC->download_fp);
1059 OpenCmdResult(filename, cbtype);
1065 * Callback function for mime parser that outputs a section all at once.
1066 * We can specify the desired section by part number *or* content-id.
1068 void mime_spew_section(char *name, char *filename, char *partnum, char *disp,
1069 void *content, char *cbtype, char *cbcharset, size_t length,
1070 char *encoding, char *cbid, void *cbuserdata)
1072 int *found_it = (int *)cbuserdata;
1075 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
1076 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
1079 cprintf("%d %d|-1|%s|%s|%s\n",
1086 client_write(content, length);
1091 struct CtdlMessage *CtdlDeserializeMessage(long msgnum, int with_body, const char *Buffer, long Length)
1093 struct CtdlMessage *ret = NULL;
1095 const char *upper_bound;
1097 cit_uint8_t field_header;
1101 upper_bound = Buffer + Length;
1106 /* Parse the three bytes that begin EVERY message on disk.
1107 * The first is always 0xFF, the on-disk magic number.
1108 * The second is the anonymous/public type byte.
1109 * The third is the format type byte (vari, fixed, or MIME).
1113 syslog(LOG_ERR, "msgbase: message %ld appears to be corrupted", msgnum);
1116 ret = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1117 memset(ret, 0, sizeof(struct CtdlMessage));
1119 ret->cm_magic = CTDLMESSAGE_MAGIC;
1120 ret->cm_anon_type = *mptr++; /* Anon type byte */
1121 ret->cm_format_type = *mptr++; /* Format type byte */
1124 * The rest is zero or more arbitrary fields. Load them in.
1125 * We're done when we encounter either a zero-length field or
1126 * have just processed the 'M' (message text) field.
1129 field_header = '\0';
1132 /* work around possibly buggy messages: */
1133 while (field_header == '\0') {
1134 if (mptr >= upper_bound) {
1137 field_header = *mptr++;
1139 if (mptr >= upper_bound) {
1142 which = field_header;
1145 CM_SetField(ret, which, mptr, len);
1147 mptr += len + 1; /* advance to next field */
1149 } while ((mptr < upper_bound) && (field_header != 'M'));
1156 * Load a message from disk into memory.
1157 * This is used by CtdlOutputMsg() and other fetch functions.
1159 * NOTE: Caller is responsible for freeing the returned CtdlMessage struct
1160 * using the CM_Free(); function.
1162 struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body)
1164 struct cdbdata *dmsgtext;
1165 struct CtdlMessage *ret = NULL;
1167 syslog(LOG_DEBUG, "msgbase: CtdlFetchMessage(%ld, %d)", msgnum, with_body);
1168 dmsgtext = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
1169 if (dmsgtext == NULL) {
1170 syslog(LOG_ERR, "msgbase: CtdlFetchMessage(%ld, %d) Failed!", msgnum, with_body);
1174 if (dmsgtext->ptr[dmsgtext->len - 1] != '\0') {
1175 syslog(LOG_ERR, "msgbase: CtdlFetchMessage(%ld, %d) Forcefully terminating message!!", msgnum, with_body);
1176 dmsgtext->ptr[dmsgtext->len - 1] = '\0';
1179 ret = CtdlDeserializeMessage(msgnum, with_body, dmsgtext->ptr, dmsgtext->len);
1187 /* Always make sure there's something in the msg text field. If
1188 * it's NULL, the message text is most likely stored separately,
1189 * so go ahead and fetch that. Failing that, just set a dummy
1190 * body so other code doesn't barf.
1192 if ( (CM_IsEmpty(ret, eMesageText)) && (with_body) ) {
1193 dmsgtext = cdb_fetch(CDB_BIGMSGS, &msgnum, sizeof(long));
1194 if (dmsgtext != NULL) {
1195 CM_SetAsField(ret, eMesageText, &dmsgtext->ptr, dmsgtext->len - 1);
1199 if (CM_IsEmpty(ret, eMesageText)) {
1200 CM_SetField(ret, eMesageText, HKEY("\r\n\r\n (no text)\r\n"));
1208 * Pre callback function for multipart/alternative
1210 * NOTE: this differs from the standard behavior for a reason. Normally when
1211 * displaying multipart/alternative you want to show the _last_ usable
1212 * format in the message. Here we show the _first_ one, because it's
1213 * usually text/plain. Since this set of functions is designed for text
1214 * output to non-MIME-aware clients, this is the desired behavior.
1217 void fixed_output_pre(char *name, char *filename, char *partnum, char *disp,
1218 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1219 char *cbid, void *cbuserdata)
1223 ma = (struct ma_info *)cbuserdata;
1224 syslog(LOG_DEBUG, "msgbase: fixed_output_pre() type=<%s>", cbtype);
1225 if (!strcasecmp(cbtype, "multipart/alternative")) {
1229 if (!strcasecmp(cbtype, "message/rfc822")) {
1236 * Post callback function for multipart/alternative
1238 void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
1239 void *content, char *cbtype, char *cbcharset, size_t length,
1240 char *encoding, char *cbid, void *cbuserdata)
1244 ma = (struct ma_info *)cbuserdata;
1245 syslog(LOG_DEBUG, "msgbase: fixed_output_post() type=<%s>", cbtype);
1246 if (!strcasecmp(cbtype, "multipart/alternative")) {
1250 if (!strcasecmp(cbtype, "message/rfc822")) {
1257 * Inline callback function for mime parser that wants to display text
1259 void fixed_output(char *name, char *filename, char *partnum, char *disp,
1260 void *content, char *cbtype, char *cbcharset, size_t length,
1261 char *encoding, char *cbid, void *cbuserdata)
1268 ma = (struct ma_info *)cbuserdata;
1271 "msgbase: fixed_output() part %s: %s (%s) (%ld bytes)",
1272 partnum, filename, cbtype, (long)length
1276 * If we're in the middle of a multipart/alternative scope and
1277 * we've already printed another section, skip this one.
1279 if ( (ma->is_ma) && (ma->did_print) ) {
1280 syslog(LOG_DEBUG, "msgbase: skipping part %s (%s)", partnum, cbtype);
1285 if ( (!strcasecmp(cbtype, "text/plain"))
1286 || (IsEmptyStr(cbtype)) ) {
1289 client_write(wptr, length);
1290 if (wptr[length-1] != '\n') {
1297 if (!strcasecmp(cbtype, "text/html")) {
1298 ptr = html_to_ascii(content, length, 80);
1300 client_write(ptr, wlen);
1301 if ((wlen > 0) && (ptr[wlen-1] != '\n')) {
1308 if (ma->use_fo_hooks) {
1309 if (PerformFixedOutputHooks(cbtype, content, length)) {
1310 /* above function returns nonzero if it handled the part */
1315 if (strncasecmp(cbtype, "multipart/", 10)) {
1316 cprintf("Part %s: %s (%s) (%ld bytes)\r\n",
1317 partnum, filename, cbtype, (long)length);
1324 * The client is elegant and sophisticated and wants to be choosy about
1325 * MIME content types, so figure out which multipart/alternative part
1326 * we're going to send.
1328 * We use a system of weights. When we find a part that matches one of the
1329 * MIME types we've declared as preferential, we can store it in ma->chosen_part
1330 * and then set ma->chosen_pref to that MIME type's position in our preference
1331 * list. If we then hit another match, we only replace the first match if
1332 * the preference value is lower.
1334 void choose_preferred(char *name, char *filename, char *partnum, char *disp,
1335 void *content, char *cbtype, char *cbcharset, size_t length,
1336 char *encoding, char *cbid, void *cbuserdata)
1342 ma = (struct ma_info *)cbuserdata;
1344 for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
1345 extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
1346 if ( (!strcasecmp(buf, cbtype)) && (!ma->freeze) ) {
1347 if (i < ma->chosen_pref) {
1348 syslog(LOG_DEBUG, "msgbase: setting chosen part to <%s>", partnum);
1349 safestrncpy(ma->chosen_part, partnum, sizeof ma->chosen_part);
1350 ma->chosen_pref = i;
1358 * Now that we've chosen our preferred part, output it.
1360 void output_preferred(char *name,
1374 int add_newline = 0;
1377 char *decoded = NULL;
1378 size_t bytes_decoded;
1381 ma = (struct ma_info *)cbuserdata;
1383 /* This is not the MIME part you're looking for... */
1384 if (strcasecmp(partnum, ma->chosen_part)) return;
1386 /* If the content-type of this part is in our preferred formats
1387 * list, we can simply output it verbatim.
1389 for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
1390 extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
1391 if (!strcasecmp(buf, cbtype)) {
1392 /* Yeah! Go! W00t!! */
1393 if (ma->dont_decode == 0)
1394 rc = mime_decode_now (content,
1400 break; /* Give us the chance, maybe theres another one. */
1402 if (rc == 0) text_content = (char *)content;
1404 text_content = decoded;
1405 length = bytes_decoded;
1408 if (text_content[length-1] != '\n') {
1411 cprintf("Content-type: %s", cbtype);
1412 if (!IsEmptyStr(cbcharset)) {
1413 cprintf("; charset=%s", cbcharset);
1415 cprintf("\nContent-length: %d\n",
1416 (int)(length + add_newline) );
1417 if (!IsEmptyStr(encoding)) {
1418 cprintf("Content-transfer-encoding: %s\n", encoding);
1421 cprintf("Content-transfer-encoding: 7bit\n");
1423 cprintf("X-Citadel-MSG4-Partnum: %s\n", partnum);
1425 if (client_write(text_content, length) == -1)
1427 syslog(LOG_ERR, "msgbase: output_preferred() aborting due to write failure");
1430 if (add_newline) cprintf("\n");
1431 if (decoded != NULL) free(decoded);
1436 /* No translations required or possible: output as text/plain */
1437 cprintf("Content-type: text/plain\n\n");
1439 if (ma->dont_decode == 0)
1440 rc = mime_decode_now (content,
1446 return; /* Give us the chance, maybe theres another one. */
1448 if (rc == 0) text_content = (char *)content;
1450 text_content = decoded;
1451 length = bytes_decoded;
1454 fixed_output(name, filename, partnum, disp, text_content, cbtype, cbcharset,
1455 length, encoding, cbid, cbuserdata);
1456 if (decoded != NULL) free(decoded);
1461 char desired_section[64];
1468 * Callback function for
1470 void extract_encapsulated_message(char *name, char *filename, char *partnum, char *disp,
1471 void *content, char *cbtype, char *cbcharset, size_t length,
1472 char *encoding, char *cbid, void *cbuserdata)
1474 struct encapmsg *encap;
1476 encap = (struct encapmsg *)cbuserdata;
1478 /* Only proceed if this is the desired section... */
1479 if (!strcasecmp(encap->desired_section, partnum)) {
1480 encap->msglen = length;
1481 encap->msg = malloc(length + 2);
1482 memcpy(encap->msg, content, length);
1489 * Determine whether the specified message exists in the cached_msglist
1490 * (This is a security check)
1492 int check_cached_msglist(long msgnum) {
1494 /* cases in which we skip the check */
1495 if (!CC) return om_ok; /* not a session */
1496 if (CC->client_socket <= 0) return om_ok; /* not a client session */
1497 if (CC->cached_msglist == NULL) return om_access_denied; /* no msglist fetched */
1498 if (CC->cached_num_msgs == 0) return om_access_denied; /* nothing to check */
1501 /* Do a binary search within the cached_msglist for the requested msgnum */
1503 int max = (CC->cached_num_msgs - 1);
1505 while (max >= min) {
1506 int middle = min + (max-min) / 2 ;
1507 if (msgnum == CC->cached_msglist[middle]) {
1510 if (msgnum > CC->cached_msglist[middle]) {
1518 return om_access_denied;
1523 * Get a message off disk. (returns om_* values found in msgbase.h)
1526 int CtdlOutputMsg(long msg_num, /* message number (local) to fetch */
1527 int mode, /* how would you like that message? */
1528 int headers_only, /* eschew the message body? */
1529 int do_proto, /* do Citadel protocol responses? */
1530 int crlf, /* Use CRLF newlines instead of LF? */
1531 char *section, /* NULL or a message/rfc822 section */
1532 int flags, /* various flags; see msgbase.h */
1537 struct CtdlMessage *TheMessage = NULL;
1538 int retcode = CIT_OK;
1539 struct encapmsg encap;
1542 syslog(LOG_DEBUG, "msgbase: CtdlOutputMsg(msgnum=%ld, mode=%d, section=%s)",
1544 (section ? section : "<>")
1547 r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
1550 if (r == om_not_logged_in) {
1551 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1554 cprintf("%d An unknown error has occurred.\n", ERROR);
1561 * Check to make sure the message is actually IN this room
1563 r = check_cached_msglist(msg_num);
1564 if (r == om_access_denied) {
1565 /* Not in the cache? We get ONE shot to check it again. */
1566 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, NULL, NULL);
1567 r = check_cached_msglist(msg_num);
1570 syslog(LOG_DEBUG, "msgbase: security check fail; message %ld is not in %s",
1571 msg_num, CC->room.QRname
1574 if (r == om_access_denied) {
1575 cprintf("%d message %ld was not found in this room\n",
1576 ERROR + HIGHER_ACCESS_REQUIRED,
1585 * Fetch the message from disk. If we're in HEADERS_FAST mode,
1586 * request that we don't even bother loading the body into memory.
1588 if (headers_only == HEADERS_FAST) {
1589 TheMessage = CtdlFetchMessage(msg_num, 0);
1592 TheMessage = CtdlFetchMessage(msg_num, 1);
1595 if (TheMessage == NULL) {
1596 if (do_proto) cprintf("%d Can't locate msg %ld on disk\n",
1597 ERROR + MESSAGE_NOT_FOUND, msg_num);
1598 return(om_no_such_msg);
1601 /* Here is the weird form of this command, to process only an
1602 * encapsulated message/rfc822 section.
1604 if (section) if (!IsEmptyStr(section)) if (strcmp(section, "0")) {
1605 memset(&encap, 0, sizeof encap);
1606 safestrncpy(encap.desired_section, section, sizeof encap.desired_section);
1607 mime_parser(CM_RANGE(TheMessage, eMesageText),
1608 *extract_encapsulated_message,
1609 NULL, NULL, (void *)&encap, 0
1612 if ((Author != NULL) && (*Author == NULL))
1615 CM_GetAsField(TheMessage, eAuthor, Author, &len);
1617 if ((Address != NULL) && (*Address == NULL))
1620 CM_GetAsField(TheMessage, erFc822Addr, Address, &len);
1622 if ((MessageID != NULL) && (*MessageID == NULL))
1625 CM_GetAsField(TheMessage, emessageId, MessageID, &len);
1627 CM_Free(TheMessage);
1631 encap.msg[encap.msglen] = 0;
1632 TheMessage = convert_internet_message(encap.msg);
1633 encap.msg = NULL; /* no free() here, TheMessage owns it now */
1635 /* Now we let it fall through to the bottom of this
1636 * function, because TheMessage now contains the
1637 * encapsulated message instead of the top-level
1638 * message. Isn't that neat?
1643 cprintf("%d msg %ld has no part %s\n",
1644 ERROR + MESSAGE_NOT_FOUND,
1648 retcode = om_no_such_msg;
1653 /* Ok, output the message now */
1654 if (retcode == CIT_OK)
1655 retcode = CtdlOutputPreLoadedMsg(TheMessage, mode, headers_only, do_proto, crlf, flags);
1656 if ((Author != NULL) && (*Author == NULL))
1659 CM_GetAsField(TheMessage, eAuthor, Author, &len);
1661 if ((Address != NULL) && (*Address == NULL))
1664 CM_GetAsField(TheMessage, erFc822Addr, Address, &len);
1666 if ((MessageID != NULL) && (*MessageID == NULL))
1669 CM_GetAsField(TheMessage, emessageId, MessageID, &len);
1672 CM_Free(TheMessage);
1678 void OutputCtdlMsgHeaders(struct CtdlMessage *TheMessage, int do_proto) {
1681 char display_name[256];
1683 /* begin header processing loop for Citadel message format */
1684 safestrncpy(display_name, "<unknown>", sizeof display_name);
1685 if (!CM_IsEmpty(TheMessage, eAuthor)) {
1686 strcpy(buf, TheMessage->cm_fields[eAuthor]);
1687 if (TheMessage->cm_anon_type == MES_ANONONLY) {
1688 safestrncpy(display_name, "****", sizeof display_name);
1690 else if (TheMessage->cm_anon_type == MES_ANONOPT) {
1691 safestrncpy(display_name, "anonymous", sizeof display_name);
1694 safestrncpy(display_name, buf, sizeof display_name);
1696 if ((is_room_aide())
1697 && ((TheMessage->cm_anon_type == MES_ANONONLY)
1698 || (TheMessage->cm_anon_type == MES_ANONOPT))) {
1699 size_t tmp = strlen(display_name);
1700 snprintf(&display_name[tmp],
1701 sizeof display_name - tmp,
1706 /* Now spew the header fields in the order we like them. */
1707 for (i=0; i< NDiskFields; ++i) {
1709 Field = FieldOrder[i];
1710 if (Field != eMesageText) {
1711 if ( (!CM_IsEmpty(TheMessage, Field)) && (msgkeys[Field] != NULL) ) {
1712 if ((Field == eenVelopeTo) || (Field == eRecipient) || (Field == eCarbonCopY)) {
1713 sanitize_truncated_recipient(TheMessage->cm_fields[Field]);
1715 if (Field == eAuthor) {
1717 cprintf("%s=%s\n", msgkeys[Field], display_name);
1720 /* Masquerade display name if needed */
1723 cprintf("%s=%s\n", msgkeys[Field], TheMessage->cm_fields[Field]);
1726 /* Give the client a hint about whether the message originated locally */
1727 if (Field == erFc822Addr) {
1728 if (IsDirectory(TheMessage->cm_fields[Field] ,0)) {
1729 cprintf("locl=yes\n"); // message originated locally.
1741 void OutputRFC822MsgHeaders(
1742 struct CtdlMessage *TheMessage,
1743 int flags, /* should the message be exported clean */
1744 const char *nl, int nlen,
1745 char *mid, long sizeof_mid,
1746 char *suser, long sizeof_suser,
1747 char *luser, long sizeof_luser,
1748 char *fuser, long sizeof_fuser,
1749 char *snode, long sizeof_snode)
1751 char datestamp[100];
1752 int subject_found = 0;
1759 for (i = 0; i < NDiskFields; ++i) {
1760 if (TheMessage->cm_fields[FieldOrder[i]]) {
1761 mptr = mpptr = TheMessage->cm_fields[FieldOrder[i]];
1762 switch (FieldOrder[i]) {
1764 safestrncpy(luser, mptr, sizeof_luser);
1765 safestrncpy(suser, mptr, sizeof_suser);
1768 if ((flags & QP_EADDR) != 0) {
1769 mptr = qp_encode_email_addrs(mptr);
1771 sanitize_truncated_recipient(mptr);
1772 cprintf("CC: %s%s", mptr, nl);
1775 cprintf("Return-Path: %s%s", mptr, nl);
1778 cprintf("List-ID: %s%s", mptr, nl);
1781 if ((flags & QP_EADDR) != 0)
1782 mptr = qp_encode_email_addrs(mptr);
1784 while ((*hptr != '\0') && isspace(*hptr))
1786 if (!IsEmptyStr(hptr))
1787 cprintf("Envelope-To: %s%s", hptr, nl);
1790 cprintf("Subject: %s%s", mptr, nl);
1794 safestrncpy(mid, mptr, sizeof_mid);
1797 safestrncpy(fuser, mptr, sizeof_fuser);
1799 if (haschar(mptr, '@') == 0) {
1800 sanitize_truncated_recipient(mptr);
1801 cprintf("To: %s@%s", mptr, CtdlGetConfigStr("c_fqdn"));
1805 if ((flags & QP_EADDR) != 0) {
1806 mptr = qp_encode_email_addrs(mptr);
1808 sanitize_truncated_recipient(mptr);
1809 cprintf("To: %s", mptr);
1814 datestring(datestamp, sizeof datestamp, atol(mptr), DATESTRING_RFC822);
1815 cprintf("Date: %s%s", datestamp, nl);
1818 cprintf("References: ");
1819 k = num_tokens(mptr, '|');
1820 for (j=0; j<k; ++j) {
1821 extract_token(buf, mptr, j, '|', sizeof buf);
1822 cprintf("<%s>", buf);
1833 while ((*hptr != '\0') && isspace(*hptr))
1835 if (!IsEmptyStr(hptr))
1836 cprintf("Reply-To: %s%s", mptr, nl);
1848 /* these don't map to mime message headers. */
1851 if (mptr != mpptr) {
1856 if (subject_found == 0) {
1857 cprintf("Subject: (no subject)%s", nl);
1862 void Dump_RFC822HeadersBody(
1863 struct CtdlMessage *TheMessage,
1864 int headers_only, /* eschew the message body? */
1865 int flags, /* should the bessage be exported clean? */
1866 const char *nl, int nlen)
1868 cit_uint8_t prev_ch;
1870 const char *StartOfText = StrBufNOTNULL;
1873 int nllen = strlen(nl);
1877 mptr = TheMessage->cm_fields[eMesageText];
1880 while (*mptr != '\0') {
1881 if (*mptr == '\r') {
1888 eoh = (*(mptr+1) == '\r') && (*(mptr+2) == '\n');
1890 eoh = *(mptr+1) == '\n';
1894 StartOfText = strchr(StartOfText, '\n');
1895 StartOfText = strchr(StartOfText, '\n');
1898 if (((headers_only == HEADERS_NONE) && (mptr >= StartOfText)) ||
1899 ((headers_only == HEADERS_ONLY) && (mptr < StartOfText)) ||
1900 ((headers_only != HEADERS_NONE) &&
1901 (headers_only != HEADERS_ONLY))
1903 if (*mptr == '\n') {
1904 memcpy(&outbuf[outlen], nl, nllen);
1906 outbuf[outlen] = '\0';
1909 outbuf[outlen++] = *mptr;
1913 if (flags & ESC_DOT) {
1914 if ((prev_ch == '\n') && (*mptr == '.') && ((*(mptr+1) == '\r') || (*(mptr+1) == '\n'))) {
1915 outbuf[outlen++] = '.';
1920 if (outlen > 1000) {
1921 if (client_write(outbuf, outlen) == -1) {
1922 syslog(LOG_ERR, "msgbase: Dump_RFC822HeadersBody() aborting due to write failure");
1925 lfSent = (outbuf[outlen - 1] == '\n');
1930 client_write(outbuf, outlen);
1931 lfSent = (outbuf[outlen - 1] == '\n');
1934 client_write(nl, nlen);
1938 /* If the format type on disk is 1 (fixed-format), then we want
1939 * everything to be output completely literally ... regardless of
1940 * what message transfer format is in use.
1942 void DumpFormatFixed(
1943 struct CtdlMessage *TheMessage,
1944 int mode, /* how would you like that message? */
1945 const char *nl, int nllen)
1953 mptr = TheMessage->cm_fields[eMesageText];
1955 if (mode == MT_MIME) {
1956 cprintf("Content-type: text/plain\n\n");
1960 while (ch = *mptr++, ch > 0) {
1964 if ((buflen > 250) && (!xlline)){
1968 while ((buflen > 0) &&
1969 (!isspace(buf[buflen])))
1975 mptr -= tbuflen - buflen;
1981 /* if we reach the outer bounds of our buffer, abort without respect for what we purge. */
1982 if (xlline && ((isspace(ch)) || (buflen > SIZ - nllen - 2))) {
1987 memcpy (&buf[buflen], nl, nllen);
1991 if (client_write(buf, buflen) == -1) {
1992 syslog(LOG_ERR, "msgbase: DumpFormatFixed() aborting due to write failure");
2004 if (!IsEmptyStr(buf)) {
2005 cprintf("%s%s", buf, nl);
2011 * Get a message off disk. (returns om_* values found in msgbase.h)
2013 int CtdlOutputPreLoadedMsg(
2014 struct CtdlMessage *TheMessage,
2015 int mode, /* how would you like that message? */
2016 int headers_only, /* eschew the message body? */
2017 int do_proto, /* do Citadel protocol responses? */
2018 int crlf, /* Use CRLF newlines instead of LF? */
2019 int flags /* should the bessage be exported clean? */
2022 const char *nl; /* newline string */
2026 /* Buffers needed for RFC822 translation. These are all filled
2027 * using functions that are bounds-checked, and therefore we can
2028 * make them substantially smaller than SIZ.
2036 syslog(LOG_DEBUG, "msgbase: CtdlOutputPreLoadedMsg(TheMessage=%s, %d, %d, %d, %d",
2037 ((TheMessage == NULL) ? "NULL" : "not null"),
2038 mode, headers_only, do_proto, crlf
2041 strcpy(mid, "unknown");
2042 nl = (crlf ? "\r\n" : "\n");
2043 nlen = crlf ? 2 : 1;
2045 if (!CM_IsValidMsg(TheMessage)) {
2046 syslog(LOG_ERR, "msgbase: error; invalid preloaded message for output");
2047 return(om_no_such_msg);
2050 /* Suppress envelope recipients if required to avoid disclosing BCC addresses.
2051 * Pad it with spaces in order to avoid changing the RFC822 length of the message.
2053 if ( (flags & SUPPRESS_ENV_TO) && (!CM_IsEmpty(TheMessage, eenVelopeTo)) ) {
2054 memset(TheMessage->cm_fields[eenVelopeTo], ' ', TheMessage->cm_lengths[eenVelopeTo]);
2057 /* Are we downloading a MIME component? */
2058 if (mode == MT_DOWNLOAD) {
2059 if (TheMessage->cm_format_type != FMT_RFC822) {
2061 cprintf("%d This is not a MIME message.\n",
2062 ERROR + ILLEGAL_VALUE);
2063 } else if (CC->download_fp != NULL) {
2064 if (do_proto) cprintf(
2065 "%d You already have a download open.\n",
2066 ERROR + RESOURCE_BUSY);
2068 /* Parse the message text component */
2069 mime_parser(CM_RANGE(TheMessage, eMesageText),
2070 *mime_download, NULL, NULL, NULL, 0);
2071 /* If there's no file open by this time, the requested
2072 * section wasn't found, so print an error
2074 if (CC->download_fp == NULL) {
2075 if (do_proto) cprintf(
2076 "%d Section %s not found.\n",
2077 ERROR + FILE_NOT_FOUND,
2078 CC->download_desired_section);
2081 return((CC->download_fp != NULL) ? om_ok : om_mime_error);
2084 /* MT_SPEW_SECTION is like MT_DOWNLOAD except it outputs the whole MIME part
2085 * in a single server operation instead of opening a download file.
2087 if (mode == MT_SPEW_SECTION) {
2088 if (TheMessage->cm_format_type != FMT_RFC822) {
2090 cprintf("%d This is not a MIME message.\n",
2091 ERROR + ILLEGAL_VALUE);
2093 /* Parse the message text component */
2096 mime_parser(CM_RANGE(TheMessage, eMesageText),
2097 *mime_spew_section, NULL, NULL, (void *)&found_it, 0);
2098 /* If section wasn't found, print an error
2101 if (do_proto) cprintf(
2102 "%d Section %s not found.\n",
2103 ERROR + FILE_NOT_FOUND,
2104 CC->download_desired_section);
2107 return((CC->download_fp != NULL) ? om_ok : om_mime_error);
2110 /* now for the user-mode message reading loops */
2111 if (do_proto) cprintf("%d msg:\n", LISTING_FOLLOWS);
2113 /* Does the caller want to skip the headers? */
2114 if (headers_only == HEADERS_NONE) goto START_TEXT;
2116 /* Tell the client which format type we're using. */
2117 if ( (mode == MT_CITADEL) && (do_proto) ) {
2118 cprintf("type=%d\n", TheMessage->cm_format_type); // Tell the client which format type we're using.
2121 /* nhdr=yes means that we're only displaying headers, no body */
2122 if ( (TheMessage->cm_anon_type == MES_ANONONLY)
2123 && ((mode == MT_CITADEL) || (mode == MT_MIME))
2126 cprintf("nhdr=yes\n");
2129 if ((mode == MT_CITADEL) || (mode == MT_MIME)) {
2130 OutputCtdlMsgHeaders(TheMessage, do_proto);
2133 /* 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);
2451 * Message base operation to save a new message to the message store
2452 * (returns new message number)
2454 * This is the back end for CtdlSubmitMsg() and should not be directly
2455 * called by server-side modules.
2458 long CtdlSaveThisMessage(struct CtdlMessage *msg, long msgid, int Reply) {
2466 * If the message is big, set its body aside for storage elsewhere
2467 * and we hide the message body from the serializer
2469 if (!CM_IsEmpty(msg, eMesageText) && msg->cm_lengths[eMesageText] > BIGMSG) {
2471 holdM = msg->cm_fields[eMesageText];
2472 msg->cm_fields[eMesageText] = NULL;
2473 holdMLen = msg->cm_lengths[eMesageText];
2474 msg->cm_lengths[eMesageText] = 0;
2477 /* Serialize our data structure for storage in the database */
2478 CtdlSerializeMessage(&smr, msg);
2481 /* put the message body back into the message */
2482 msg->cm_fields[eMesageText] = holdM;
2483 msg->cm_lengths[eMesageText] = holdMLen;
2488 cprintf("%d Unable to serialize message\n",
2489 ERROR + INTERNAL_ERROR);
2492 syslog(LOG_ERR, "msgbase: CtdlSaveMessage() unable to serialize message");
2498 /* Write our little bundle of joy into the message base */
2499 retval = cdb_store(CDB_MSGMAIN, &msgid, (int)sizeof(long), smr.ser, smr.len);
2501 syslog(LOG_ERR, "msgbase: can't store message %ld: %ld", msgid, retval);
2505 retval = cdb_store(CDB_BIGMSGS,
2512 syslog(LOG_ERR, "msgbase: failed to store message body for msgid %ld: %ld", msgid, retval);
2517 /* Free the memory we used for the serialized message */
2524 long send_message(struct CtdlMessage *msg) {
2530 /* Get a new message number */
2531 newmsgid = get_new_message_number();
2533 /* Generate an ID if we don't have one already */
2534 if (CM_IsEmpty(msg, emessageId)) {
2535 msgidbuflen = snprintf(msgidbuf, sizeof msgidbuf, "%08lX-%08lX@%s",
2536 (long unsigned int) time(NULL),
2537 (long unsigned int) newmsgid,
2538 CtdlGetConfigStr("c_fqdn")
2541 CM_SetField(msg, emessageId, msgidbuf, msgidbuflen);
2544 retval = CtdlSaveThisMessage(msg, newmsgid, 1);
2550 /* Return the *local* message ID to the caller
2551 * (even if we're storing an incoming network message)
2558 * Serialize a struct CtdlMessage into the format used on disk and network.
2560 * This function loads up a "struct ser_ret" (defined in server.h) which
2561 * contains the length of the serialized message and a pointer to the
2562 * serialized message in memory. THE LATTER MUST BE FREED BY THE CALLER.
2564 void CtdlSerializeMessage(struct ser_ret *ret, /* return values */
2565 struct CtdlMessage *msg) /* unserialized msg */
2571 * Check for valid message format
2573 if (CM_IsValidMsg(msg) == 0) {
2574 syslog(LOG_ERR, "msgbase: CtdlSerializeMessage() aborting due to invalid message");
2581 for (i=0; i < NDiskFields; ++i)
2582 if (msg->cm_fields[FieldOrder[i]] != NULL)
2583 ret->len += msg->cm_lengths[FieldOrder[i]] + 2;
2585 ret->ser = malloc(ret->len);
2586 if (ret->ser == NULL) {
2587 syslog(LOG_ERR, "msgbase: CtdlSerializeMessage() malloc(%ld) failed: %m", (long)ret->len);
2594 ret->ser[1] = msg->cm_anon_type;
2595 ret->ser[2] = msg->cm_format_type;
2598 for (i=0; i < NDiskFields; ++i) {
2599 if (msg->cm_fields[FieldOrder[i]] != NULL) {
2600 ret->ser[wlen++] = (char)FieldOrder[i];
2602 memcpy(&ret->ser[wlen],
2603 msg->cm_fields[FieldOrder[i]],
2604 msg->cm_lengths[FieldOrder[i]] + 1);
2606 wlen = wlen + msg->cm_lengths[FieldOrder[i]] + 1;
2610 if (ret->len != wlen) {
2611 syslog(LOG_ERR, "msgbase: ERROR; len=%ld wlen=%ld", (long)ret->len, (long)wlen);
2619 * Check to see if any messages already exist in the current room which
2620 * carry the same Exclusive ID as this one. If any are found, delete them.
2622 void ReplicationChecks(struct CtdlMessage *msg) {
2623 long old_msgnum = (-1L);
2625 if (DoesThisRoomNeedEuidIndexing(&CC->room) == 0) return;
2627 syslog(LOG_DEBUG, "msgbase: performing replication checks in <%s>", CC->room.QRname);
2629 /* No exclusive id? Don't do anything. */
2630 if (msg == NULL) return;
2631 if (CM_IsEmpty(msg, eExclusiveID)) return;
2633 /*syslog(LOG_DEBUG, "msgbase: exclusive ID: <%s> for room <%s>",
2634 msg->cm_fields[eExclusiveID], CC->room.QRname);*/
2636 old_msgnum = CtdlLocateMessageByEuid(msg->cm_fields[eExclusiveID], &CC->room);
2637 if (old_msgnum > 0L) {
2638 syslog(LOG_DEBUG, "msgbase: ReplicationChecks() replacing message %ld", old_msgnum);
2639 CtdlDeleteMessages(CC->room.QRname, &old_msgnum, 1, "");
2645 * Save a message to disk and submit it into the delivery system.
2647 long CtdlSubmitMsg(struct CtdlMessage *msg, /* message to save */
2648 struct recptypes *recps, /* recipients (if mail) */
2649 const char *force /* force a particular room? */
2651 char hold_rm[ROOMNAMELEN];
2652 char actual_rm[ROOMNAMELEN];
2653 char force_room[ROOMNAMELEN];
2654 char content_type[SIZ]; /* We have to learn this */
2655 char recipient[SIZ];
2656 char bounce_to[1024];
2659 const char *mptr = NULL;
2660 struct ctdluser userbuf;
2662 struct MetaData smi;
2663 char *collected_addresses = NULL;
2664 struct addresses_to_be_filed *aptr = NULL;
2665 StrBuf *saved_rfc822_version = NULL;
2666 int qualified_for_journaling = 0;
2668 syslog(LOG_DEBUG, "msgbase: CtdlSubmitMsg() called");
2669 if (CM_IsValidMsg(msg) == 0) return(-1); /* self check */
2671 /* If this message has no timestamp, we take the liberty of
2672 * giving it one, right now.
2674 if (CM_IsEmpty(msg, eTimestamp)) {
2675 CM_SetFieldLONG(msg, eTimestamp, time(NULL));
2678 /* If this message has no path, we generate one.
2680 if (CM_IsEmpty(msg, eMessagePath)) {
2681 if (!CM_IsEmpty(msg, eAuthor)) {
2682 CM_CopyField(msg, eMessagePath, eAuthor);
2683 for (a=0; !IsEmptyStr(&msg->cm_fields[eMessagePath][a]); ++a) {
2684 if (isspace(msg->cm_fields[eMessagePath][a])) {
2685 msg->cm_fields[eMessagePath][a] = ' ';
2690 CM_SetField(msg, eMessagePath, HKEY("unknown"));
2694 if (force == NULL) {
2695 force_room[0] = '\0';
2698 strcpy(force_room, force);
2701 /* Learn about what's inside, because it's what's inside that counts */
2702 if (CM_IsEmpty(msg, eMesageText)) {
2703 syslog(LOG_ERR, "msgbase: ERROR; attempt to save message with NULL body");
2707 switch (msg->cm_format_type) {
2709 strcpy(content_type, "text/x-citadel-variformat");
2712 strcpy(content_type, "text/plain");
2715 strcpy(content_type, "text/plain");
2716 mptr = bmstrcasestr(msg->cm_fields[eMesageText], "Content-type:");
2719 safestrncpy(content_type, &mptr[13], sizeof content_type);
2720 striplt(content_type);
2721 aptr = content_type;
2722 while (!IsEmptyStr(aptr)) {
2734 /* Goto the correct room */
2735 room = (recps) ? CC->room.QRname : SENTITEMS;
2736 syslog(LOG_DEBUG, "msgbase: selected room %s", room);
2737 strcpy(hold_rm, CC->room.QRname);
2738 strcpy(actual_rm, CC->room.QRname);
2739 if (recps != NULL) {
2740 strcpy(actual_rm, SENTITEMS);
2743 /* If the user is a twit, move to the twit room for posting */
2745 if (CC->user.axlevel == AxProbU) {
2746 strcpy(hold_rm, actual_rm);
2747 strcpy(actual_rm, CtdlGetConfigStr("c_twitroom"));
2748 syslog(LOG_DEBUG, "msgbase: diverting to twit room");
2752 /* ...or if this message is destined for Aide> then go there. */
2753 if (!IsEmptyStr(force_room)) {
2754 strcpy(actual_rm, force_room);
2757 syslog(LOG_DEBUG, "msgbase: final selection: %s (%s)", actual_rm, room);
2758 if (strcasecmp(actual_rm, CC->room.QRname)) {
2759 /* CtdlGetRoom(&CC->room, actual_rm); */
2760 CtdlUserGoto(actual_rm, 0, 1, NULL, NULL, NULL, NULL);
2764 * If this message has no O (room) field, generate one.
2766 if (CM_IsEmpty(msg, eOriginalRoom) && !IsEmptyStr(CC->room.QRname)) {
2767 CM_SetField(msg, eOriginalRoom, CC->room.QRname, -1);
2770 /* Perform "before save" hooks (aborting if any return nonzero) */
2771 syslog(LOG_DEBUG, "msgbase: performing before-save hooks");
2772 if (PerformMessageHooks(msg, recps, EVT_BEFORESAVE) > 0) return(-3);
2775 * If this message has an Exclusive ID, and the room is replication
2776 * checking enabled, then do replication checks.
2778 if (DoesThisRoomNeedEuidIndexing(&CC->room)) {
2779 ReplicationChecks(msg);
2782 /* Save it to disk */
2783 syslog(LOG_DEBUG, "msgbase: saving to disk");
2784 newmsgid = send_message(msg);
2785 if (newmsgid <= 0L) return(-5);
2787 /* Write a supplemental message info record. This doesn't have to
2788 * be a critical section because nobody else knows about this message
2791 syslog(LOG_DEBUG, "msgbase: creating metadata record");
2792 memset(&smi, 0, sizeof(struct MetaData));
2793 smi.meta_msgnum = newmsgid;
2794 smi.meta_refcount = 0;
2795 safestrncpy(smi.meta_content_type, content_type, sizeof smi.meta_content_type);
2798 * Measure how big this message will be when rendered as RFC822.
2799 * We do this for two reasons:
2800 * 1. We need the RFC822 length for the new metadata record, so the
2801 * POP and IMAP services don't have to calculate message lengths
2802 * while the user is waiting (multiplied by potentially hundreds
2803 * or thousands of messages).
2804 * 2. If journaling is enabled, we will need an RFC822 version of the
2805 * message to attach to the journalized copy.
2807 if (CC->redirect_buffer != NULL) {
2808 syslog(LOG_ALERT, "msgbase: CC->redirect_buffer is not NULL during message submission!");
2811 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
2812 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, QP_EADDR);
2813 smi.meta_rfc822_length = StrLength(CC->redirect_buffer);
2814 saved_rfc822_version = CC->redirect_buffer;
2815 CC->redirect_buffer = NULL;
2819 /* Now figure out where to store the pointers */
2820 syslog(LOG_DEBUG, "msgbase: storing pointers");
2822 /* If this is being done by the networker delivering a private
2823 * message, we want to BYPASS saving the sender's copy (because there
2824 * is no local sender; it would otherwise go to the Trashcan).
2826 if ((!CC->internal_pgm) || (recps == NULL)) {
2827 if (CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 1, msg) != 0) {
2828 syslog(LOG_ERR, "msgbase: ERROR saving message pointer %ld in %s", newmsgid, actual_rm);
2829 CtdlSaveMsgPointerInRoom(CtdlGetConfigStr("c_aideroom"), newmsgid, 0, msg);
2833 /* For internet mail, drop a copy in the outbound queue room */
2834 if ((recps != NULL) && (recps->num_internet > 0)) {
2835 CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, newmsgid, 0, msg);
2838 /* If other rooms are specified, drop them there too. */
2839 if ((recps != NULL) && (recps->num_room > 0)) {
2840 for (i=0; i<num_tokens(recps->recp_room, '|'); ++i) {
2841 extract_token(recipient, recps->recp_room, i, '|', sizeof recipient);
2842 syslog(LOG_DEBUG, "msgbase: delivering to room <%s>", recipient);
2843 CtdlSaveMsgPointerInRoom(recipient, newmsgid, 0, msg);
2847 /* Bump this user's messages posted counter. */
2848 syslog(LOG_DEBUG, "msgbase: updating user");
2849 CtdlLockGetCurrentUser();
2850 CC->user.posted = CC->user.posted + 1;
2851 CtdlPutCurrentUserLock();
2853 /* Decide where bounces need to be delivered */
2854 if ((recps != NULL) && (recps->bounce_to == NULL)) {
2855 if (CC->logged_in) {
2856 strcpy(bounce_to, CC->user.fullname);
2858 else if (!IsEmptyStr(msg->cm_fields[eAuthor])){
2859 strcpy(bounce_to, msg->cm_fields[eAuthor]);
2861 recps->bounce_to = bounce_to;
2864 CM_SetFieldLONG(msg, eVltMsgNum, newmsgid);
2866 /* If this is private, local mail, make a copy in the
2867 * recipient's mailbox and bump the reference count.
2869 if ((recps != NULL) && (recps->num_local > 0)) {
2873 pch = recps->recp_local;
2874 recps->recp_local = recipient;
2875 ntokens = num_tokens(pch, '|');
2876 for (i=0; i<ntokens; ++i) {
2877 extract_token(recipient, pch, i, '|', sizeof recipient);
2878 syslog(LOG_DEBUG, "msgbase: delivering private local mail to <%s>", recipient);
2879 if (CtdlGetUser(&userbuf, recipient) == 0) {
2880 CtdlMailboxName(actual_rm, sizeof actual_rm, &userbuf, MAILROOM);
2881 CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0, msg);
2882 CtdlBumpNewMailCounter(userbuf.usernum);
2883 PerformMessageHooks(msg, recps, EVT_AFTERUSRMBOXSAVE);
2886 syslog(LOG_DEBUG, "msgbase: no user <%s>", recipient);
2887 CtdlSaveMsgPointerInRoom(CtdlGetConfigStr("c_aideroom"), newmsgid, 0, msg);
2890 recps->recp_local = pch;
2893 /* Perform "after save" hooks */
2894 syslog(LOG_DEBUG, "msgbase: performing after-save hooks");
2896 PerformMessageHooks(msg, recps, EVT_AFTERSAVE);
2897 CM_FlushField(msg, eVltMsgNum);
2899 /* Go back to the room we started from */
2900 syslog(LOG_DEBUG, "msgbase: returning to original room %s", hold_rm);
2901 if (strcasecmp(hold_rm, CC->room.QRname)) {
2902 CtdlUserGoto(hold_rm, 0, 1, NULL, NULL, NULL, NULL);
2906 * Any addresses to harvest for someone's address book?
2908 if ( (CC->logged_in) && (recps != NULL) ) {
2909 collected_addresses = harvest_collected_addresses(msg);
2912 if (collected_addresses != NULL) {
2913 aptr = (struct addresses_to_be_filed *) malloc(sizeof(struct addresses_to_be_filed));
2914 CtdlMailboxName(actual_rm, sizeof actual_rm, &CC->user, USERCONTACTSROOM);
2915 aptr->roomname = strdup(actual_rm);
2916 aptr->collected_addresses = collected_addresses;
2917 begin_critical_section(S_ATBF);
2920 end_critical_section(S_ATBF);
2924 * Determine whether this message qualifies for journaling.
2926 if (!CM_IsEmpty(msg, eJournal)) {
2927 qualified_for_journaling = 0;
2930 if (recps == NULL) {
2931 qualified_for_journaling = CtdlGetConfigInt("c_journal_pubmsgs");
2933 else if (recps->num_local + recps->num_ignet + recps->num_internet > 0) {
2934 qualified_for_journaling = CtdlGetConfigInt("c_journal_email");
2937 qualified_for_journaling = CtdlGetConfigInt("c_journal_pubmsgs");
2942 * Do we have to perform journaling? If so, hand off the saved
2943 * RFC822 version will be handed off to the journaler for background
2944 * submit. Otherwise, we have to free the memory ourselves.
2946 if (saved_rfc822_version != NULL) {
2947 if (qualified_for_journaling) {
2948 JournalBackgroundSubmit(msg, saved_rfc822_version, recps);
2951 FreeStrBuf(&saved_rfc822_version);
2955 if ((recps != NULL) && (recps->bounce_to == bounce_to))
2956 recps->bounce_to = NULL;
2964 * Convenience function for generating small administrative messages.
2966 long quickie_message(const char *from,
2967 const char *fromaddr,
2972 const char *subject)
2974 struct CtdlMessage *msg;
2975 struct recptypes *recp = NULL;
2977 msg = malloc(sizeof(struct CtdlMessage));
2978 memset(msg, 0, sizeof(struct CtdlMessage));
2979 msg->cm_magic = CTDLMESSAGE_MAGIC;
2980 msg->cm_anon_type = MES_NORMAL;
2981 msg->cm_format_type = format_type;
2983 if (!IsEmptyStr(from)) {
2984 CM_SetField(msg, eAuthor, from, -1);
2986 else if (!IsEmptyStr(fromaddr)) {
2988 CM_SetField(msg, eAuthor, fromaddr, -1);
2989 pAt = strchr(msg->cm_fields[eAuthor], '@');
2991 CM_CutFieldAt(msg, eAuthor, pAt - msg->cm_fields[eAuthor]);
2995 msg->cm_fields[eAuthor] = strdup("Citadel");
2998 if (!IsEmptyStr(fromaddr)) CM_SetField(msg, erFc822Addr, fromaddr, -1);
2999 if (!IsEmptyStr(room)) CM_SetField(msg, eOriginalRoom, room, -1);
3000 if (!IsEmptyStr(to)) {
3001 CM_SetField(msg, eRecipient, to, -1);
3002 recp = validate_recipients(to, NULL, 0);
3004 if (!IsEmptyStr(subject)) {
3005 CM_SetField(msg, eMsgSubject, subject, -1);
3007 if (!IsEmptyStr(text)) {
3008 CM_SetField(msg, eMesageText, text, -1);
3011 long msgnum = CtdlSubmitMsg(msg, recp, room);
3013 if (recp != NULL) free_recipients(recp);
3019 * Back end function used by CtdlMakeMessage() and similar functions
3021 StrBuf *CtdlReadMessageBodyBuf(char *terminator, /* token signalling EOT */
3023 size_t maxlen, /* maximum message length */
3024 StrBuf *exist, /* if non-null, append to it;
3025 exist is ALWAYS freed */
3026 int crlf /* CRLF newlines instead of LF */
3034 LineBuf = NewStrBufPlain(NULL, SIZ);
3035 if (exist == NULL) {
3036 Message = NewStrBufPlain(NULL, 4 * SIZ);
3039 Message = NewStrBufDup(exist);
3042 /* Do we need to change leading ".." to "." for SMTP escaping? */
3043 if ((tlen == 1) && (*terminator == '.')) {
3047 /* read in the lines of message text one by one */
3049 if (CtdlClientGetLine(LineBuf) < 0) {
3052 if ((StrLength(LineBuf) == tlen) && (!strcmp(ChrPtr(LineBuf), terminator))) {
3055 if ( (!flushing) && (!finished) ) {
3057 StrBufAppendBufPlain(LineBuf, HKEY("\r\n"), 0);
3060 StrBufAppendBufPlain(LineBuf, HKEY("\n"), 0);
3063 /* Unescape SMTP-style input of two dots at the beginning of the line */
3064 if ((dotdot) && (StrLength(LineBuf) > 1) && (ChrPtr(LineBuf)[0] == '.')) {
3065 StrBufCutLeft(LineBuf, 1);
3067 StrBufAppendBuf(Message, LineBuf, 0);
3070 /* if we've hit the max msg length, flush the rest */
3071 if (StrLength(Message) >= maxlen) flushing = 1;
3073 } while (!finished);
3074 FreeStrBuf(&LineBuf);
3080 * Back end function used by CtdlMakeMessage() and similar functions
3082 char *CtdlReadMessageBody(char *terminator, /* token signalling EOT */
3084 size_t maxlen, /* maximum message length */
3085 StrBuf *exist, /* if non-null, append to it;
3086 exist is ALWAYS freed */
3087 int crlf /* CRLF newlines instead of LF */
3092 Message = CtdlReadMessageBodyBuf(terminator,
3098 if (Message == NULL)
3101 return SmashStrBuf(&Message);
3105 struct CtdlMessage *CtdlMakeMessage(
3106 struct ctdluser *author, /* author's user structure */
3107 char *recipient, /* NULL if it's not mail */
3108 char *recp_cc, /* NULL if it's not mail */
3109 char *room, /* room where it's going */
3110 int type, /* see MES_ types in header file */
3111 int format_type, /* variformat, plain text, MIME... */
3112 char *fake_name, /* who we're masquerading as */
3113 char *my_email, /* which of my email addresses to use (empty is ok) */
3114 char *subject, /* Subject (optional) */
3115 char *supplied_euid, /* ...or NULL if this is irrelevant */
3116 char *preformatted_text, /* ...or NULL to read text from client */
3117 char *references /* Thread references */
3119 return CtdlMakeMessageLen(
3120 author, /* author's user structure */
3121 recipient, /* NULL if it's not mail */
3122 (recipient)?strlen(recipient) : 0,
3123 recp_cc, /* NULL if it's not mail */
3124 (recp_cc)?strlen(recp_cc): 0,
3125 room, /* room where it's going */
3126 (room)?strlen(room): 0,
3127 type, /* see MES_ types in header file */
3128 format_type, /* variformat, plain text, MIME... */
3129 fake_name, /* who we're masquerading as */
3130 (fake_name)?strlen(fake_name): 0,
3131 my_email, /* which of my email addresses to use (empty is ok) */
3132 (my_email)?strlen(my_email): 0,
3133 subject, /* Subject (optional) */
3134 (subject)?strlen(subject): 0,
3135 supplied_euid, /* ...or NULL if this is irrelevant */
3136 (supplied_euid)?strlen(supplied_euid):0,
3137 preformatted_text, /* ...or NULL to read text from client */
3138 (preformatted_text)?strlen(preformatted_text) : 0,
3139 references, /* Thread references */
3140 (references)?strlen(references):0);
3146 * Build a binary message to be saved on disk.
3147 * (NOTE: if you supply 'preformatted_text', the buffer you give it
3148 * will become part of the message. This means you are no longer
3149 * responsible for managing that memory -- it will be freed along with
3150 * the rest of the fields when CM_Free() is called.)
3152 struct CtdlMessage *CtdlMakeMessageLen(
3153 struct ctdluser *author, /* author's user structure */
3154 char *recipient, /* NULL if it's not mail */
3156 char *recp_cc, /* NULL if it's not mail */
3158 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 */
3164 char *my_email, /* which of my email addresses to use (empty is ok) */
3166 char *subject, /* Subject (optional) */
3168 char *supplied_euid, /* ...or NULL if this is irrelevant */
3170 char *preformatted_text, /* ...or NULL to read text from client */
3172 char *references, /* Thread references */
3177 struct CtdlMessage *msg;
3179 StrBuf *FakeEncAuthor = NULL;
3181 msg = malloc(sizeof(struct CtdlMessage));
3182 memset(msg, 0, sizeof(struct CtdlMessage));
3183 msg->cm_magic = CTDLMESSAGE_MAGIC;
3184 msg->cm_anon_type = type;
3185 msg->cm_format_type = format_type;
3187 if (recipient != NULL) rcplen = striplt(recipient);
3188 if (recp_cc != NULL) cclen = striplt(recp_cc);
3190 /* Path or Return-Path */
3192 CM_SetField(msg, eMessagePath, my_email, myelen);
3194 else if (!IsEmptyStr(author->fullname)) {
3195 CM_SetField(msg, eMessagePath, author->fullname, -1);
3197 convert_spaces_to_underscores(msg->cm_fields[eMessagePath]);
3199 blen = snprintf(buf, sizeof buf, "%ld", (long)time(NULL));
3200 CM_SetField(msg, eTimestamp, buf, blen);
3203 FakeAuthor = NewStrBufPlain (fake_name, fnlen);
3206 FakeAuthor = NewStrBufPlain (author->fullname, -1);
3208 StrBufRFC2047encode(&FakeEncAuthor, FakeAuthor);
3209 CM_SetAsFieldSB(msg, eAuthor, &FakeEncAuthor);
3210 FreeStrBuf(&FakeAuthor);
3212 if (!!IsEmptyStr(CC->room.QRname)) {
3213 if (CC->room.QRflags & QR_MAILBOX) { /* room */
3214 CM_SetField(msg, eOriginalRoom, &CC->room.QRname[11], -1);
3217 CM_SetField(msg, eOriginalRoom, CC->room.QRname, -1);
3222 CM_SetField(msg, eRecipient, recipient, rcplen);
3225 CM_SetField(msg, eCarbonCopY, recp_cc, cclen);
3229 CM_SetField(msg, erFc822Addr, my_email, myelen);
3231 else if ( (author == &CC->user) && (!IsEmptyStr(CC->cs_inet_email)) ) {
3232 CM_SetField(msg, erFc822Addr, CC->cs_inet_email, -1);
3235 if (subject != NULL) {
3237 length = striplt(subject);
3243 while ((subject[i] != '\0') &&
3244 (IsAscii = isascii(subject[i]) != 0 ))
3247 CM_SetField(msg, eMsgSubject, subject, subjlen);
3248 else /* ok, we've got utf8 in the string. */
3251 rfc2047Subj = rfc2047encode(subject, length);
3252 CM_SetAsField(msg, eMsgSubject, &rfc2047Subj, strlen(rfc2047Subj));
3259 CM_SetField(msg, eExclusiveID, supplied_euid, euidlen);
3263 CM_SetField(msg, eWeferences, references, reflen);
3266 if (preformatted_text != NULL) {
3267 CM_SetField(msg, eMesageText, preformatted_text, textlen);
3271 MsgBody = CtdlReadMessageBodyBuf(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
3272 if (MsgBody != NULL) {
3273 CM_SetAsFieldSB(msg, eMesageText, &MsgBody);
3282 * API function to delete messages which match a set of criteria
3283 * (returns the actual number of messages deleted)
3285 int CtdlDeleteMessages(const char *room_name, // which room
3286 long *dmsgnums, // array of msg numbers to be deleted
3287 int num_dmsgnums, // number of msgs to be deleted, or 0 for "any"
3288 char *content_type // or "" for any. regular expressions expected.
3290 struct ctdlroom qrbuf;
3291 struct cdbdata *cdbfr;
3292 long *msglist = NULL;
3293 long *dellist = NULL;
3296 int num_deleted = 0;
3298 struct MetaData smi;
3301 int need_to_free_re = 0;
3303 if (content_type) if (!IsEmptyStr(content_type)) {
3304 regcomp(&re, content_type, 0);
3305 need_to_free_re = 1;
3307 syslog(LOG_DEBUG, "msgbase: CtdlDeleteMessages(%s, %d msgs, %s)", room_name, num_dmsgnums, content_type);
3309 /* get room record, obtaining a lock... */
3310 if (CtdlGetRoomLock(&qrbuf, room_name) != 0) {
3311 syslog(LOG_ERR, "msgbase: CtdlDeleteMessages(): Room <%s> not found", room_name);
3312 if (need_to_free_re) regfree(&re);
3313 return(0); /* room not found */
3315 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
3317 if (cdbfr != NULL) {
3318 dellist = malloc(cdbfr->len);
3319 msglist = (long *) cdbfr->ptr;
3320 cdbfr->ptr = NULL; /* CtdlDeleteMessages() now owns this memory */
3321 num_msgs = cdbfr->len / sizeof(long);
3325 int have_contenttype = (content_type != NULL) && !IsEmptyStr(content_type);
3326 int have_delmsgs = (num_dmsgnums == 0) || (dmsgnums == NULL);
3327 int have_more_del = 1;
3329 num_msgs = sort_msglist(msglist, num_msgs);
3330 if (num_dmsgnums > 1)
3331 num_dmsgnums = sort_msglist(dmsgnums, num_dmsgnums);
3334 StrBuf *dbg = NewStrBuf();
3335 for (i = 0; i < num_dmsgnums; i++)
3336 StrBufAppendPrintf(dbg, ", %ld", dmsgnums[i]);
3337 syslog(LOG_DEBUG, "msgbase: Deleting before: %s", ChrPtr(dbg));
3342 while ((i < num_msgs) && (have_more_del)) {
3345 /* Set/clear a bit for each criterion */
3347 /* 0 messages in the list or a null list means that we are
3348 * interested in deleting any messages which meet the other criteria.
3351 delete_this |= 0x01;
3354 while ((i < num_msgs) && (msglist[i] < dmsgnums[j])) i++;
3359 if (msglist[i] == dmsgnums[j]) {
3360 delete_this |= 0x01;
3363 have_more_del = (j < num_dmsgnums);
3366 if (have_contenttype) {
3367 GetMetaData(&smi, msglist[i]);
3368 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) == 0) {
3369 delete_this |= 0x02;
3372 delete_this |= 0x02;
3375 /* Delete message only if all bits are set */
3376 if (delete_this == 0x03) {
3377 dellist[num_deleted++] = msglist[i];
3384 StrBuf *dbg = NewStrBuf();
3385 for (i = 0; i < num_deleted; i++)
3386 StrBufAppendPrintf(dbg, ", %ld", dellist[i]);
3387 syslog(LOG_DEBUG, "msgbase: Deleting: %s", ChrPtr(dbg));
3391 num_msgs = sort_msglist(msglist, num_msgs);
3392 cdb_store(CDB_MSGLISTS, &qrbuf.QRnumber, (int)sizeof(long),
3393 msglist, (int)(num_msgs * sizeof(long)));
3396 qrbuf.QRhighest = msglist[num_msgs - 1];
3398 qrbuf.QRhighest = 0;
3400 CtdlPutRoomLock(&qrbuf);
3402 /* Go through the messages we pulled out of the index, and decrement
3403 * their reference counts by 1. If this is the only room the message
3404 * was in, the reference count will reach zero and the message will
3405 * automatically be deleted from the database. We do this in a
3406 * separate pass because there might be plug-in hooks getting called,
3407 * and we don't want that happening during an S_ROOMS critical
3411 for (i=0; i<num_deleted; ++i) {
3412 PerformDeleteHooks(qrbuf.QRname, dellist[i]);
3414 AdjRefCountList(dellist, num_deleted, -1);
3416 /* Now free the memory we used, and go away. */
3417 if (msglist != NULL) free(msglist);
3418 if (dellist != NULL) free(dellist);
3419 syslog(LOG_DEBUG, "msgbase: %d message(s) deleted", num_deleted);
3420 if (need_to_free_re) regfree(&re);
3421 return (num_deleted);
3426 * GetMetaData() - Get the supplementary record for a message
3428 void GetMetaData(struct MetaData *smibuf, long msgnum)
3430 struct cdbdata *cdbsmi;
3433 memset(smibuf, 0, sizeof(struct MetaData));
3434 smibuf->meta_msgnum = msgnum;
3435 smibuf->meta_refcount = 1; /* Default reference count is 1 */
3437 /* Use the negative of the message number for its supp record index */
3438 TheIndex = (0L - msgnum);
3440 cdbsmi = cdb_fetch(CDB_MSGMAIN, &TheIndex, sizeof(long));
3441 if (cdbsmi == NULL) {
3442 return; /* record not found; leave it alone */
3444 memcpy(smibuf, cdbsmi->ptr,
3445 ((cdbsmi->len > sizeof(struct MetaData)) ?
3446 sizeof(struct MetaData) : cdbsmi->len)
3454 * PutMetaData() - (re)write supplementary record for a message
3456 void PutMetaData(struct MetaData *smibuf)
3460 /* Use the negative of the message number for the metadata db index */
3461 TheIndex = (0L - smibuf->meta_msgnum);
3463 cdb_store(CDB_MSGMAIN,
3464 &TheIndex, (int)sizeof(long),
3465 smibuf, (int)sizeof(struct MetaData)
3471 * Convenience function to process a big block of AdjRefCount() operations
3473 void AdjRefCountList(long *msgnum, long nmsg, int incr)
3477 for (i = 0; i < nmsg; i++) {
3478 AdjRefCount(msgnum[i], incr);
3484 * AdjRefCount - adjust the reference count for a message. We need to delete from disk any message whose reference count reaches zero.
3486 void AdjRefCount(long msgnum, int incr)
3488 struct MetaData smi;
3491 /* This is a *tight* critical section; please keep it that way, as
3492 * it may get called while nested in other critical sections.
3493 * Complicating this any further will surely cause deadlock!
3495 begin_critical_section(S_SUPPMSGMAIN);
3496 GetMetaData(&smi, msgnum);
3497 smi.meta_refcount += incr;
3499 end_critical_section(S_SUPPMSGMAIN);
3500 syslog(LOG_DEBUG, "msgbase: AdjRefCount() msg %ld ref count delta %+d, is now %d", msgnum, incr, smi.meta_refcount);
3502 /* If the reference count is now zero, delete both the message and its metadata record.
3504 if (smi.meta_refcount == 0) {
3505 syslog(LOG_DEBUG, "msgbase: deleting message <%ld>", msgnum);
3507 /* Call delete hooks with NULL room to show it has gone altogether */
3508 PerformDeleteHooks(NULL, msgnum);
3510 /* Remove from message base */
3512 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3513 cdb_delete(CDB_BIGMSGS, &delnum, (int)sizeof(long));
3515 /* Remove metadata record */
3516 delnum = (0L - msgnum);
3517 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3523 * Write a generic object to this room
3525 * Note: this could be much more efficient. Right now we use two temporary
3526 * files, and still pull the message into memory as with all others.
3528 void CtdlWriteObject(char *req_room, /* Room to stuff it in */
3529 char *content_type, /* MIME type of this object */
3530 char *raw_message, /* Data to be written */
3531 off_t raw_length, /* Size of raw_message */
3532 struct ctdluser *is_mailbox, /* Mailbox room? */
3533 int is_binary, /* Is encoding necessary? */
3534 int is_unique, /* Del others of this type? */
3535 unsigned int flags /* Internal save flags */
3537 struct ctdlroom qrbuf;
3538 char roomname[ROOMNAMELEN];
3539 struct CtdlMessage *msg;
3540 StrBuf *encoded_message = NULL;
3542 if (is_mailbox != NULL) {
3543 CtdlMailboxName(roomname, sizeof roomname, is_mailbox, req_room);
3546 safestrncpy(roomname, req_room, sizeof(roomname));
3549 syslog(LOG_DEBUG, "msfbase: raw length is %ld", (long)raw_length);
3552 encoded_message = NewStrBufPlain(NULL, (size_t) (((raw_length * 134) / 100) + 4096 ) );
3555 encoded_message = NewStrBufPlain(NULL, (size_t)(raw_length + 4096));
3558 StrBufAppendBufPlain(encoded_message, HKEY("Content-type: "), 0);
3559 StrBufAppendBufPlain(encoded_message, content_type, -1, 0);
3560 StrBufAppendBufPlain(encoded_message, HKEY("\n"), 0);
3563 StrBufAppendBufPlain(encoded_message, HKEY("Content-transfer-encoding: base64\n\n"), 0);
3566 StrBufAppendBufPlain(encoded_message, HKEY("Content-transfer-encoding: 7bit\n\n"), 0);
3570 StrBufBase64Append(encoded_message, NULL, raw_message, raw_length, 0);
3573 StrBufAppendBufPlain(encoded_message, raw_message, raw_length, 0);
3576 syslog(LOG_DEBUG, "msgbase: allocating");
3577 msg = malloc(sizeof(struct CtdlMessage));
3578 memset(msg, 0, sizeof(struct CtdlMessage));
3579 msg->cm_magic = CTDLMESSAGE_MAGIC;
3580 msg->cm_anon_type = MES_NORMAL;
3581 msg->cm_format_type = 4;
3582 CM_SetField(msg, eAuthor, CC->user.fullname, -1);
3583 CM_SetField(msg, eOriginalRoom, req_room, -1);
3584 msg->cm_flags = flags;
3586 CM_SetAsFieldSB(msg, eMesageText, &encoded_message);
3588 /* Create the requested room if we have to. */
3589 if (CtdlGetRoom(&qrbuf, roomname) != 0) {
3590 CtdlCreateRoom(roomname, ( (is_mailbox != NULL) ? 5 : 3 ), "", 0, 1, 0, VIEW_BBS);
3592 /* If the caller specified this object as unique, delete all
3593 * other objects of this type that are currently in the room.
3596 syslog(LOG_DEBUG, "msgbase: deleted %d other msgs of this type",
3597 CtdlDeleteMessages(roomname, NULL, 0, content_type)
3600 /* Now write the data */
3601 CtdlSubmitMsg(msg, NULL, roomname);
3606 /************************************************************************/
3607 /* MODULE INITIALIZATION */
3608 /************************************************************************/
3610 CTDL_MODULE_INIT(msgbase)
3613 FillMsgKeyLookupTable();
3616 /* return our module id for the log */