2 * Implements the message store.
4 * Copyright (c) 1987-2018 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
21 #include <libcitadel.h>
23 #include "ctdl_module.h"
24 #include "citserver.h"
27 #include "clientsocket.h"
31 #include "internet_addressing.h"
32 #include "euidindex.h"
34 #include "journaling.h"
36 struct addresses_to_be_filed *atbf = NULL;
38 /* This temp file holds the queue of operations for AdjRefCount() */
39 static FILE *arcfp = NULL;
40 void AdjRefCountList(long *msgnum, long nmsg, int incr);
43 * These are the four-character field headers we use when outputting
44 * messages in Citadel format (as opposed to RFC822 format).
47 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
48 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
49 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
50 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
51 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
52 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
53 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
54 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
56 "from", /* A -> eAuthor */
57 NULL, /* B -> eBig_message */
58 NULL, /* C -> eRemoteRoom FIXME no more ignet */
59 NULL, /* D -> eDestination FIXME no more ignet */
60 "exti", /* E -> eXclusivID */
61 "rfca", /* F -> erFc822Addr */
63 "hnod", /* H -> eHumanNode FIXME no more ignet */
64 "msgn", /* I -> emessageId */
65 "jrnl", /* J -> eJournal */
66 "rep2", /* K -> eReplyTo */
67 "list", /* L -> eListID */
68 "text", /* M -> eMesageText */
69 "node", /* N -> eNodeName FIXME no more ignet */
70 "room", /* O -> eOriginalRoom */
71 "path", /* P -> eMessagePath */
73 "rcpt", /* R -> eRecipient */
74 "spec", /* S -> eSpecialField FIXME we might not be using this anymore */
75 "time", /* T -> eTimestamp */
76 "subj", /* U -> eMsgSubject */
77 "nvto", /* V -> eenVelopeTo */
78 "wefw", /* W -> eWeferences */
80 "cccc", /* Y -> eCarbonCopY */
84 HashList *msgKeyLookup = NULL;
86 int GetFieldFromMnemonic(eMsgField *f, const char* c)
89 if (GetHash(msgKeyLookup, c, 4, &v)) {
96 void FillMsgKeyLookupTable(void)
100 msgKeyLookup = NewHash (1, FourHash);
102 for (i=0; i < 91; i++) {
103 if (msgkeys[i] != NULL) {
104 Put(msgKeyLookup, msgkeys[i], 4, (void*)i, reference_free_handler);
109 eMsgField FieldOrder[] = {
110 /* Important fields */
121 /* Semi-important fields */
127 /* G is not used yet, may become virus signature*/
130 /* Q is not used yet */
133 /* X is not used yet */
134 /* Z is not used yet */
141 /* Message text (MUST be last) */
143 /* Not saved to disk:
148 static const long NDiskFields = sizeof(FieldOrder) / sizeof(eMsgField);
151 int CM_IsEmpty(struct CtdlMessage *Msg, eMsgField which)
153 return !((Msg->cm_fields[which] != NULL) && (Msg->cm_fields[which][0] != '\0'));
157 void CM_SetField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
159 if (Msg->cm_fields[which] != NULL) {
160 free (Msg->cm_fields[which]);
162 Msg->cm_fields[which] = malloc(length + 1);
163 memcpy(Msg->cm_fields[which], buf, length);
164 Msg->cm_fields[which][length] = '\0';
165 Msg->cm_lengths[which] = length;
169 void CM_SetFieldLONG(struct CtdlMessage *Msg, eMsgField which, long lvalue)
173 len = snprintf(buf, sizeof(buf), "%ld", lvalue);
174 CM_SetField(Msg, which, buf, len);
178 void CM_CutFieldAt(struct CtdlMessage *Msg, eMsgField WhichToCut, long maxlen)
180 if (Msg->cm_fields[WhichToCut] == NULL)
183 if (Msg->cm_lengths[WhichToCut] > maxlen)
185 Msg->cm_fields[WhichToCut][maxlen] = '\0';
186 Msg->cm_lengths[WhichToCut] = maxlen;
191 void CM_FlushField(struct CtdlMessage *Msg, eMsgField which)
193 if (Msg->cm_fields[which] != NULL)
194 free (Msg->cm_fields[which]);
195 Msg->cm_fields[which] = NULL;
196 Msg->cm_lengths[which] = 0;
200 void CM_Flush(struct CtdlMessage *Msg)
204 if (CM_IsValidMsg(Msg) == 0) {
208 for (i = 0; i < 256; ++i) {
209 CM_FlushField(Msg, i);
214 void CM_CopyField(struct CtdlMessage *Msg, eMsgField WhichToPutTo, eMsgField WhichtToCopy)
217 if (Msg->cm_fields[WhichToPutTo] != NULL) {
218 free (Msg->cm_fields[WhichToPutTo]);
221 if (Msg->cm_fields[WhichtToCopy] != NULL) {
222 len = Msg->cm_lengths[WhichtToCopy];
223 Msg->cm_fields[WhichToPutTo] = malloc(len + 1);
224 memcpy(Msg->cm_fields[WhichToPutTo], Msg->cm_fields[WhichtToCopy], len);
225 Msg->cm_fields[WhichToPutTo][len] = '\0';
226 Msg->cm_lengths[WhichToPutTo] = len;
229 Msg->cm_fields[WhichToPutTo] = NULL;
230 Msg->cm_lengths[WhichToPutTo] = 0;
235 void CM_PrependToField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
237 if (Msg->cm_fields[which] != NULL) {
242 oldmsgsize = Msg->cm_lengths[which] + 1;
243 newmsgsize = length + oldmsgsize;
245 new = malloc(newmsgsize);
246 memcpy(new, buf, length);
247 memcpy(new + length, Msg->cm_fields[which], oldmsgsize);
248 free(Msg->cm_fields[which]);
249 Msg->cm_fields[which] = new;
250 Msg->cm_lengths[which] = newmsgsize - 1;
253 Msg->cm_fields[which] = malloc(length + 1);
254 memcpy(Msg->cm_fields[which], buf, length);
255 Msg->cm_fields[which][length] = '\0';
256 Msg->cm_lengths[which] = length;
261 void CM_SetAsField(struct CtdlMessage *Msg, eMsgField which, char **buf, long length)
263 if (Msg->cm_fields[which] != NULL) {
264 free (Msg->cm_fields[which]);
267 Msg->cm_fields[which] = *buf;
269 Msg->cm_lengths[which] = length;
273 void CM_SetAsFieldSB(struct CtdlMessage *Msg, eMsgField which, StrBuf **buf)
275 if (Msg->cm_fields[which] != NULL) {
276 free (Msg->cm_fields[which]);
279 Msg->cm_lengths[which] = StrLength(*buf);
280 Msg->cm_fields[which] = SmashStrBuf(buf);
284 void CM_GetAsField(struct CtdlMessage *Msg, eMsgField which, char **ret, long *retlen)
286 if (Msg->cm_fields[which] != NULL) {
287 *retlen = Msg->cm_lengths[which];
288 *ret = Msg->cm_fields[which];
289 Msg->cm_fields[which] = NULL;
290 Msg->cm_lengths[which] = 0;
300 * Returns 1 if the supplied pointer points to a valid Citadel message.
301 * If the pointer is NULL or the magic number check fails, returns 0.
303 int CM_IsValidMsg(struct CtdlMessage *msg) {
307 if ((msg->cm_magic) != CTDLMESSAGE_MAGIC) {
308 syslog(LOG_WARNING, "msgbase: CM_IsValidMsg() self-check failed");
315 void CM_FreeContents(struct CtdlMessage *msg)
319 for (i = 0; i < 256; ++i)
320 if (msg->cm_fields[i] != NULL) {
321 free(msg->cm_fields[i]);
322 msg->cm_lengths[i] = 0;
325 msg->cm_magic = 0; /* just in case */
330 * 'Destructor' for struct CtdlMessage
332 void CM_Free(struct CtdlMessage *msg)
334 if (CM_IsValidMsg(msg) == 0) {
335 if (msg != NULL) free (msg);
338 CM_FreeContents(msg);
343 int CM_DupField(eMsgField i, struct CtdlMessage *OrgMsg, struct CtdlMessage *NewMsg)
346 len = OrgMsg->cm_lengths[i];
347 NewMsg->cm_fields[i] = malloc(len + 1);
348 if (NewMsg->cm_fields[i] == NULL) {
351 memcpy(NewMsg->cm_fields[i], OrgMsg->cm_fields[i], len);
352 NewMsg->cm_fields[i][len] = '\0';
353 NewMsg->cm_lengths[i] = len;
358 struct CtdlMessage * CM_Duplicate(struct CtdlMessage *OrgMsg)
361 struct CtdlMessage *NewMsg;
363 if (CM_IsValidMsg(OrgMsg) == 0) {
366 NewMsg = (struct CtdlMessage *)malloc(sizeof(struct CtdlMessage));
367 if (NewMsg == NULL) {
371 memcpy(NewMsg, OrgMsg, sizeof(struct CtdlMessage));
373 memset(&NewMsg->cm_fields, 0, sizeof(char*) * 256);
375 for (i = 0; i < 256; ++i) {
376 if (OrgMsg->cm_fields[i] != NULL) {
377 if (!CM_DupField(i, OrgMsg, NewMsg)) {
388 /* Determine if a given message matches the fields in a message template.
389 * Return 0 for a successful match.
391 int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template) {
394 /* If there aren't any fields in the template, all messages will
397 if (template == NULL) return(0);
399 /* Null messages are bogus. */
400 if (msg == NULL) return(1);
402 for (i='A'; i<='Z'; ++i) {
403 if (template->cm_fields[i] != NULL) {
404 if (msg->cm_fields[i] == NULL) {
405 /* Considered equal if temmplate is empty string */
406 if (IsEmptyStr(template->cm_fields[i])) continue;
409 if ((template->cm_lengths[i] != msg->cm_lengths[i]) ||
410 (strcasecmp(msg->cm_fields[i], template->cm_fields[i])))
415 /* All compares succeeded: we have a match! */
422 * Retrieve the "seen" message list for the current room.
424 void CtdlGetSeen(char *buf, int which_set) {
427 /* Learn about the user and room in question */
428 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
430 if (which_set == ctdlsetseen_seen)
431 safestrncpy(buf, vbuf.v_seen, SIZ);
432 if (which_set == ctdlsetseen_answered)
433 safestrncpy(buf, vbuf.v_answered, SIZ);
439 * Manipulate the "seen msgs" string (or other message set strings)
441 void CtdlSetSeen(long *target_msgnums, int num_target_msgnums,
442 int target_setting, int which_set,
443 struct ctdluser *which_user, struct ctdlroom *which_room) {
444 struct cdbdata *cdbfr;
449 long hi = (-1L); /// TODO: we just write here. y?
458 char *is_set; /* actually an array of booleans */
460 /* Don't bother doing *anything* if we were passed a list of zero messages */
461 if (num_target_msgnums < 1) {
465 /* If no room was specified, we go with the current room. */
467 which_room = &CC->room;
470 /* If no user was specified, we go with the current user. */
472 which_user = &CC->user;
475 syslog(LOG_DEBUG, "msgbase: CtdlSetSeen(%d msgs starting with %ld, %s, %d) in <%s>",
476 num_target_msgnums, target_msgnums[0],
477 (target_setting ? "SET" : "CLEAR"),
481 /* Learn about the user and room in question */
482 CtdlGetRelationship(&vbuf, which_user, which_room);
484 /* Load the message list */
485 cdbfr = cdb_fetch(CDB_MSGLISTS, &which_room->QRnumber, sizeof(long));
487 msglist = (long *) cdbfr->ptr;
488 cdbfr->ptr = NULL; /* CtdlSetSeen() now owns this memory */
489 num_msgs = cdbfr->len / sizeof(long);
492 return; /* No messages at all? No further action. */
495 is_set = malloc(num_msgs * sizeof(char));
496 memset(is_set, 0, (num_msgs * sizeof(char)) );
498 /* Decide which message set we're manipulating */
500 case ctdlsetseen_seen:
501 vset = NewStrBufPlain(vbuf.v_seen, -1);
503 case ctdlsetseen_answered:
504 vset = NewStrBufPlain(vbuf.v_answered, -1);
511 #if 0 /* This is a special diagnostic section. Do not allow it to run during normal operation. */
512 syslog(LOG_DEBUG, "There are %d messages in the room.\n", num_msgs);
513 for (i=0; i<num_msgs; ++i) {
514 if ((i > 0) && (msglist[i] <= msglist[i-1])) abort();
516 syslog(LOG_DEBUG, "We are twiddling %d of them.\n", num_target_msgnums);
517 for (k=0; k<num_target_msgnums; ++k) {
518 if ((k > 0) && (target_msgnums[k] <= target_msgnums[k-1])) abort();
522 /* Translate the existing sequence set into an array of booleans */
523 setstr = NewStrBuf();
527 while (StrBufExtract_NextToken(setstr, vset, &pvset, ',') >= 0) {
529 StrBufExtract_token(lostr, setstr, 0, ':');
530 if (StrBufNum_tokens(setstr, ':') >= 2) {
531 StrBufExtract_token(histr, setstr, 1, ':');
535 StrBufAppendBuf(histr, lostr, 0);
538 if (!strcmp(ChrPtr(histr), "*")) {
545 for (i = 0; i < num_msgs; ++i) {
546 if ((msglist[i] >= lo) && (msglist[i] <= hi)) {
556 /* Now translate the array of booleans back into a sequence set */
562 for (i=0; i<num_msgs; ++i) {
566 for (k=0; k<num_target_msgnums; ++k) {
567 if (msglist[i] == target_msgnums[k]) {
568 is_seen = target_setting;
572 if ((was_seen == 0) && (is_seen == 1)) {
575 else if ((was_seen == 1) && (is_seen == 0)) {
578 if (StrLength(vset) > 0) {
579 StrBufAppendBufPlain(vset, HKEY(","), 0);
582 StrBufAppendPrintf(vset, "%ld", hi);
585 StrBufAppendPrintf(vset, "%ld:%ld", lo, hi);
589 if ((is_seen) && (i == num_msgs - 1)) {
590 if (StrLength(vset) > 0) {
591 StrBufAppendBufPlain(vset, HKEY(","), 0);
593 if ((i==0) || (was_seen == 0)) {
594 StrBufAppendPrintf(vset, "%ld", msglist[i]);
597 StrBufAppendPrintf(vset, "%ld:%ld", lo, msglist[i]);
605 * We will have to stuff this string back into a 4096 byte buffer, so if it's
606 * larger than that now, truncate it by removing tokens from the beginning.
607 * The limit of 100 iterations is there to prevent an infinite loop in case
608 * something unexpected happens.
610 int number_of_truncations = 0;
611 while ( (StrLength(vset) > SIZ) && (number_of_truncations < 100) ) {
612 StrBufRemove_token(vset, 0, ',');
613 ++number_of_truncations;
617 * If we're truncating the sequence set of messages marked with the 'seen' flag,
618 * we want the earliest messages (the truncated ones) to be marked, not unmarked.
619 * Otherwise messages at the beginning will suddenly appear to be 'unseen'.
621 if ( (which_set == ctdlsetseen_seen) && (number_of_truncations > 0) ) {
623 first_tok = NewStrBuf();
624 StrBufExtract_token(first_tok, vset, 0, ',');
625 StrBufRemove_token(vset, 0, ',');
627 if (StrBufNum_tokens(first_tok, ':') > 1) {
628 StrBufRemove_token(first_tok, 0, ':');
632 new_set = NewStrBuf();
633 StrBufAppendBufPlain(new_set, HKEY("1:"), 0);
634 StrBufAppendBuf(new_set, first_tok, 0);
635 StrBufAppendBufPlain(new_set, HKEY(":"), 0);
636 StrBufAppendBuf(new_set, vset, 0);
639 FreeStrBuf(&first_tok);
643 /* Decide which message set we're manipulating */
645 case ctdlsetseen_seen:
646 safestrncpy(vbuf.v_seen, ChrPtr(vset), sizeof vbuf.v_seen);
648 case ctdlsetseen_answered:
649 safestrncpy(vbuf.v_answered, ChrPtr(vset), sizeof vbuf.v_answered);
655 CtdlSetRelationship(&vbuf, which_user, which_room);
661 * API function to perform an operation for each qualifying message in the
662 * current room. (Returns the number of messages processed.)
664 int CtdlForEachMessage(int mode, long ref, char *search_string,
666 struct CtdlMessage *compare,
667 ForEachMsgCallback CallBack,
672 struct cdbdata *cdbfr;
673 long *msglist = NULL;
675 int num_processed = 0;
678 struct CtdlMessage *msg = NULL;
681 int printed_lastold = 0;
682 int num_search_msgs = 0;
683 long *search_msgs = NULL;
685 int need_to_free_re = 0;
688 if ((content_type) && (!IsEmptyStr(content_type))) {
689 regcomp(&re, content_type, 0);
693 /* Learn about the user and room in question */
694 if (server_shutting_down) {
695 if (need_to_free_re) regfree(&re);
698 CtdlGetUser(&CC->user, CC->curr_user);
700 if (server_shutting_down) {
701 if (need_to_free_re) regfree(&re);
704 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
706 if (server_shutting_down) {
707 if (need_to_free_re) regfree(&re);
711 /* Load the message list */
712 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
714 if (need_to_free_re) regfree(&re);
715 return 0; /* No messages at all? No further action. */
718 msglist = (long *) cdbfr->ptr;
719 num_msgs = cdbfr->len / sizeof(long);
721 cdbfr->ptr = NULL; /* clear this so that cdb_free() doesn't free it */
722 cdb_free(cdbfr); /* we own this memory now */
725 * Now begin the traversal.
727 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
729 /* If the caller is looking for a specific MIME type, filter
730 * out all messages which are not of the type requested.
732 if ((content_type != NULL) && (!IsEmptyStr(content_type))) {
734 /* This call to GetMetaData() sits inside this loop
735 * so that we only do the extra database read per msg
736 * if we need to. Doing the extra read all the time
737 * really kills the server. If we ever need to use
738 * metadata for another search criterion, we need to
739 * move the read somewhere else -- but still be smart
740 * enough to only do the read if the caller has
741 * specified something that will need it.
743 if (server_shutting_down) {
744 if (need_to_free_re) regfree(&re);
748 GetMetaData(&smi, msglist[a]);
750 /* if (strcasecmp(smi.meta_content_type, content_type)) { old non-regex way */
751 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) != 0) {
757 num_msgs = sort_msglist(msglist, num_msgs);
759 /* If a template was supplied, filter out the messages which
760 * don't match. (This could induce some delays!)
763 if (compare != NULL) {
764 for (a = 0; a < num_msgs; ++a) {
765 if (server_shutting_down) {
766 if (need_to_free_re) regfree(&re);
770 msg = CtdlFetchMessage(msglist[a], 1, 1);
772 if (CtdlMsgCmp(msg, compare)) {
781 /* If a search string was specified, get a message list from
782 * the full text index and remove messages which aren't on both
786 * Since the lists are sorted and strictly ascending, and the
787 * output list is guaranteed to be shorter than or equal to the
788 * input list, we overwrite the bottom of the input list. This
789 * eliminates the need to memmove big chunks of the list over and
792 if ( (num_msgs > 0) && (mode == MSGS_SEARCH) && (search_string) ) {
794 /* Call search module via hook mechanism.
795 * NULL means use any search function available.
796 * otherwise replace with a char * to name of search routine
798 CtdlModuleDoSearch(&num_search_msgs, &search_msgs, search_string, "fulltext");
800 if (num_search_msgs > 0) {
804 orig_num_msgs = num_msgs;
806 for (i=0; i<orig_num_msgs; ++i) {
807 for (j=0; j<num_search_msgs; ++j) {
808 if (msglist[i] == search_msgs[j]) {
809 msglist[num_msgs++] = msglist[i];
815 num_msgs = 0; /* No messages qualify */
817 if (search_msgs != NULL) free(search_msgs);
819 /* Now that we've purged messages which don't contain the search
820 * string, treat a MSGS_SEARCH just like a MSGS_ALL from this
827 * Now iterate through the message list, according to the
828 * criteria supplied by the caller.
831 for (a = 0; a < num_msgs; ++a) {
832 if (server_shutting_down) {
833 if (need_to_free_re) regfree(&re);
835 return num_processed;
837 thismsg = msglist[a];
838 if (mode == MSGS_ALL) {
842 is_seen = is_msg_in_sequence_set(
843 vbuf.v_seen, thismsg);
844 if (is_seen) lastold = thismsg;
850 || ((mode == MSGS_OLD) && (is_seen))
851 || ((mode == MSGS_NEW) && (!is_seen))
852 || ((mode == MSGS_LAST) && (a >= (num_msgs - ref)))
853 || ((mode == MSGS_FIRST) && (a < ref))
854 || ((mode == MSGS_GT) && (thismsg > ref))
855 || ((mode == MSGS_LT) && (thismsg < ref))
856 || ((mode == MSGS_EQ) && (thismsg == ref))
859 if ((mode == MSGS_NEW) && (CC->user.flags & US_LASTOLD) && (lastold > 0L) && (printed_lastold == 0) && (!is_seen)) {
861 CallBack(lastold, userdata);
865 if (CallBack) CallBack(thismsg, userdata);
869 if (need_to_free_re) regfree(&re);
872 * We cache the most recent msglist in order to do security checks later
874 if (CC->client_socket > 0) {
875 if (CC->cached_msglist != NULL) {
876 free(CC->cached_msglist);
878 CC->cached_msglist = msglist;
879 CC->cached_num_msgs = num_msgs;
885 return num_processed;
891 * memfmout() - Citadel text formatter and paginator.
892 * Although the original purpose of this routine was to format
893 * text to the reader's screen width, all we're really using it
894 * for here is to format text out to 80 columns before sending it
895 * to the client. The client software may reformat it again.
898 char *mptr, /* where are we going to get our text from? */
899 const char *nl /* string to terminate lines with */
902 unsigned char ch = 0;
909 while (ch=*(mptr++), ch != 0) {
912 if (client_write(outbuf, len) == -1)
914 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
918 if (client_write(nl, nllen) == -1)
920 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
925 else if (ch == '\r') {
926 /* Ignore carriage returns. Newlines are always LF or CRLF but never CR. */
928 else if (isspace(ch)) {
929 if (column > 72) { /* Beyond 72 columns, break on the next space */
930 if (client_write(outbuf, len) == -1)
932 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
936 if (client_write(nl, nllen) == -1)
938 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
951 if (column > 1000) { /* Beyond 1000 columns, break anywhere */
952 if (client_write(outbuf, len) == -1)
954 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
958 if (client_write(nl, nllen) == -1)
960 syslog(LOG_ERR, "msgbase: memfmout(): aborting due to write failure");
968 if (client_write(outbuf, len) == -1)
970 syslog(LOG_ERR, "msgbase: memfmout() aborting due to write failure");
973 client_write(nl, nllen);
981 * Callback function for mime parser that simply lists the part
983 void list_this_part(char *name, char *filename, char *partnum, char *disp,
984 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
985 char *cbid, void *cbuserdata)
989 ma = (struct ma_info *)cbuserdata;
990 if (ma->is_ma == 0) {
991 cprintf("part=%s|%s|%s|%s|%s|%ld|%s|%s\n",
1004 * Callback function for multipart prefix
1006 void list_this_pref(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 (!strcasecmp(cbtype, "multipart/alternative")) {
1017 if (ma->is_ma == 0) {
1018 cprintf("pref=%s|%s\n", partnum, cbtype);
1023 * Callback function for multipart sufffix
1025 void list_this_suff(char *name, char *filename, char *partnum, char *disp,
1026 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1027 char *cbid, void *cbuserdata)
1031 ma = (struct ma_info *)cbuserdata;
1032 if (ma->is_ma == 0) {
1033 cprintf("suff=%s|%s\n", partnum, cbtype);
1035 if (!strcasecmp(cbtype, "multipart/alternative")) {
1042 * Callback function for mime parser that opens a section for downloading
1043 * we use serv_files function here:
1045 extern void OpenCmdResult(char *filename, const char *mime_type);
1046 void mime_download(char *name, char *filename, char *partnum, char *disp,
1047 void *content, char *cbtype, char *cbcharset, size_t length,
1048 char *encoding, char *cbid, void *cbuserdata)
1052 /* Silently go away if there's already a download open. */
1053 if (CC->download_fp != NULL)
1057 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
1058 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
1060 CC->download_fp = tmpfile();
1061 if (CC->download_fp == NULL) {
1062 syslog(LOG_EMERG, "msgbase: mime_download() couldn't write: %m");
1063 cprintf("%d cannot open temporary file: %s\n", ERROR + INTERNAL_ERROR, strerror(errno));
1067 rv = fwrite(content, length, 1, CC->download_fp);
1069 syslog(LOG_EMERG, "msgbase: mime_download() Couldn't write: %m");
1070 cprintf("%d unable to write tempfile.\n", ERROR + TOO_BIG);
1071 fclose(CC->download_fp);
1072 CC->download_fp = NULL;
1075 fflush(CC->download_fp);
1076 rewind(CC->download_fp);
1078 OpenCmdResult(filename, cbtype);
1085 * Callback function for mime parser that outputs a section all at once.
1086 * We can specify the desired section by part number *or* content-id.
1088 void mime_spew_section(char *name, char *filename, char *partnum, char *disp,
1089 void *content, char *cbtype, char *cbcharset, size_t length,
1090 char *encoding, char *cbid, void *cbuserdata)
1092 int *found_it = (int *)cbuserdata;
1095 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
1096 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
1099 cprintf("%d %d|-1|%s|%s|%s\n",
1106 client_write(content, length);
1110 struct CtdlMessage *CtdlDeserializeMessage(long msgnum, int with_body, const char *Buffer, long Length)
1112 struct CtdlMessage *ret = NULL;
1114 const char *upper_bound;
1116 cit_uint8_t field_header;
1120 upper_bound = Buffer + Length;
1122 /* Parse the three bytes that begin EVERY message on disk.
1123 * The first is always 0xFF, the on-disk magic number.
1124 * The second is the anonymous/public type byte.
1125 * The third is the format type byte (vari, fixed, or MIME).
1129 syslog(LOG_ERR, "msgbase: message %ld appears to be corrupted", msgnum);
1132 ret = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1133 memset(ret, 0, sizeof(struct CtdlMessage));
1135 ret->cm_magic = CTDLMESSAGE_MAGIC;
1136 ret->cm_anon_type = *mptr++; /* Anon type byte */
1137 ret->cm_format_type = *mptr++; /* Format type byte */
1140 * The rest is zero or more arbitrary fields. Load them in.
1141 * We're done when we encounter either a zero-length field or
1142 * have just processed the 'M' (message text) field.
1145 field_header = '\0';
1148 /* work around possibly buggy messages: */
1149 while (field_header == '\0')
1151 if (mptr >= upper_bound) {
1154 field_header = *mptr++;
1156 if (mptr >= upper_bound) {
1159 which = field_header;
1162 CM_SetField(ret, which, mptr, len);
1164 mptr += len + 1; /* advance to next field */
1166 } while ((mptr < upper_bound) && (field_header != 'M'));
1173 * Load a message from disk into memory.
1174 * This is used by CtdlOutputMsg() and other fetch functions.
1176 * NOTE: Caller is responsible for freeing the returned CtdlMessage struct
1177 * using the CM_Free(); function.
1179 struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body, int run_msg_hooks)
1181 struct cdbdata *dmsgtext;
1182 struct CtdlMessage *ret = NULL;
1184 syslog(LOG_DEBUG, "msgbase: CtdlFetchMessage(%ld, %d)", msgnum, with_body);
1185 dmsgtext = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
1186 if (dmsgtext == NULL) {
1187 syslog(LOG_ERR, "msgbase: CtdlFetchMessage(%ld, %d) Failed!", msgnum, with_body);
1191 if (dmsgtext->ptr[dmsgtext->len - 1] != '\0')
1193 syslog(LOG_ERR, "msgbase: CtdlFetchMessage(%ld, %d) Forcefully terminating message!!", msgnum, with_body);
1194 dmsgtext->ptr[dmsgtext->len - 1] = '\0';
1197 ret = CtdlDeserializeMessage(msgnum, with_body, dmsgtext->ptr, dmsgtext->len);
1205 /* Always make sure there's something in the msg text field. If
1206 * it's NULL, the message text is most likely stored separately,
1207 * so go ahead and fetch that. Failing that, just set a dummy
1208 * body so other code doesn't barf.
1210 if ( (CM_IsEmpty(ret, eMesageText)) && (with_body) ) {
1211 dmsgtext = cdb_fetch(CDB_BIGMSGS, &msgnum, sizeof(long));
1212 if (dmsgtext != NULL) {
1213 CM_SetAsField(ret, eMesageText, &dmsgtext->ptr, dmsgtext->len - 1);
1217 if (CM_IsEmpty(ret, eMesageText)) {
1218 CM_SetField(ret, eMesageText, HKEY("\r\n\r\n (no text)\r\n"));
1221 /* Perform "before read" hooks (aborting if any return nonzero) */
1222 if (run_msg_hooks && (PerformMessageHooks(ret, NULL, EVT_BEFOREREAD) > 0)) {
1233 * Pre callback function for multipart/alternative
1235 * NOTE: this differs from the standard behavior for a reason. Normally when
1236 * displaying multipart/alternative you want to show the _last_ usable
1237 * format in the message. Here we show the _first_ one, because it's
1238 * usually text/plain. Since this set of functions is designed for text
1239 * output to non-MIME-aware clients, this is the desired behavior.
1242 void fixed_output_pre(char *name, char *filename, char *partnum, char *disp,
1243 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1244 char *cbid, void *cbuserdata)
1248 ma = (struct ma_info *)cbuserdata;
1249 syslog(LOG_DEBUG, "msgbase: fixed_output_pre() type=<%s>", cbtype);
1250 if (!strcasecmp(cbtype, "multipart/alternative")) {
1254 if (!strcasecmp(cbtype, "message/rfc822")) {
1260 * Post callback function for multipart/alternative
1262 void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
1263 void *content, char *cbtype, char *cbcharset, size_t length,
1264 char *encoding, char *cbid, void *cbuserdata)
1268 ma = (struct ma_info *)cbuserdata;
1269 syslog(LOG_DEBUG, "msgbase: fixed_output_post() type=<%s>", cbtype);
1270 if (!strcasecmp(cbtype, "multipart/alternative")) {
1274 if (!strcasecmp(cbtype, "message/rfc822")) {
1280 * Inline callback function for mime parser that wants to display text
1282 void fixed_output(char *name, char *filename, char *partnum, char *disp,
1283 void *content, char *cbtype, char *cbcharset, size_t length,
1284 char *encoding, char *cbid, void *cbuserdata)
1291 ma = (struct ma_info *)cbuserdata;
1294 "msgbase: fixed_output() part %s: %s (%s) (%ld bytes)",
1295 partnum, filename, cbtype, (long)length
1299 * If we're in the middle of a multipart/alternative scope and
1300 * we've already printed another section, skip this one.
1302 if ( (ma->is_ma) && (ma->did_print) ) {
1303 syslog(LOG_DEBUG, "msgbase: skipping part %s (%s)", partnum, cbtype);
1308 if ( (!strcasecmp(cbtype, "text/plain"))
1309 || (IsEmptyStr(cbtype)) ) {
1312 client_write(wptr, length);
1313 if (wptr[length-1] != '\n') {
1320 if (!strcasecmp(cbtype, "text/html")) {
1321 ptr = html_to_ascii(content, length, 80, 0);
1323 client_write(ptr, wlen);
1324 if ((wlen > 0) && (ptr[wlen-1] != '\n')) {
1331 if (ma->use_fo_hooks) {
1332 if (PerformFixedOutputHooks(cbtype, content, length)) {
1333 /* above function returns nonzero if it handled the part */
1338 if (strncasecmp(cbtype, "multipart/", 10)) {
1339 cprintf("Part %s: %s (%s) (%ld bytes)\r\n",
1340 partnum, filename, cbtype, (long)length);
1346 * The client is elegant and sophisticated and wants to be choosy about
1347 * MIME content types, so figure out which multipart/alternative part
1348 * we're going to send.
1350 * We use a system of weights. When we find a part that matches one of the
1351 * MIME types we've declared as preferential, we can store it in ma->chosen_part
1352 * and then set ma->chosen_pref to that MIME type's position in our preference
1353 * list. If we then hit another match, we only replace the first match if
1354 * the preference value is lower.
1356 void choose_preferred(char *name, char *filename, char *partnum, char *disp,
1357 void *content, char *cbtype, char *cbcharset, size_t length,
1358 char *encoding, char *cbid, void *cbuserdata)
1364 ma = (struct ma_info *)cbuserdata;
1366 // NOTE: REMOVING THIS CONDITIONAL FIXES BUG 220
1367 // http://bugzilla.citadel.org/show_bug.cgi?id=220
1368 // I don't know if there are any side effects! Please TEST TEST TEST
1369 //if (ma->is_ma > 0) {
1371 for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
1372 extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
1373 if ( (!strcasecmp(buf, cbtype)) && (!ma->freeze) ) {
1374 if (i < ma->chosen_pref) {
1375 syslog(LOG_DEBUG, "msgbase: setting chosen part to <%s>", partnum);
1376 safestrncpy(ma->chosen_part, partnum, sizeof ma->chosen_part);
1377 ma->chosen_pref = i;
1384 * Now that we've chosen our preferred part, output it.
1386 void output_preferred(char *name,
1400 int add_newline = 0;
1403 char *decoded = NULL;
1404 size_t bytes_decoded;
1407 ma = (struct ma_info *)cbuserdata;
1409 /* This is not the MIME part you're looking for... */
1410 if (strcasecmp(partnum, ma->chosen_part)) return;
1412 /* If the content-type of this part is in our preferred formats
1413 * list, we can simply output it verbatim.
1415 for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
1416 extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
1417 if (!strcasecmp(buf, cbtype)) {
1418 /* Yeah! Go! W00t!! */
1419 if (ma->dont_decode == 0)
1420 rc = mime_decode_now (content,
1426 break; /* Give us the chance, maybe theres another one. */
1428 if (rc == 0) text_content = (char *)content;
1430 text_content = decoded;
1431 length = bytes_decoded;
1434 if (text_content[length-1] != '\n') {
1437 cprintf("Content-type: %s", cbtype);
1438 if (!IsEmptyStr(cbcharset)) {
1439 cprintf("; charset=%s", cbcharset);
1441 cprintf("\nContent-length: %d\n",
1442 (int)(length + add_newline) );
1443 if (!IsEmptyStr(encoding)) {
1444 cprintf("Content-transfer-encoding: %s\n", encoding);
1447 cprintf("Content-transfer-encoding: 7bit\n");
1449 cprintf("X-Citadel-MSG4-Partnum: %s\n", partnum);
1451 if (client_write(text_content, length) == -1)
1453 syslog(LOG_ERR, "msgbase: output_preferred() aborting due to write failure");
1456 if (add_newline) cprintf("\n");
1457 if (decoded != NULL) free(decoded);
1462 /* No translations required or possible: output as text/plain */
1463 cprintf("Content-type: text/plain\n\n");
1465 if (ma->dont_decode == 0)
1466 rc = mime_decode_now (content,
1472 return; /* Give us the chance, maybe theres another one. */
1474 if (rc == 0) text_content = (char *)content;
1476 text_content = decoded;
1477 length = bytes_decoded;
1480 fixed_output(name, filename, partnum, disp, text_content, cbtype, cbcharset,
1481 length, encoding, cbid, cbuserdata);
1482 if (decoded != NULL) free(decoded);
1487 char desired_section[64];
1494 * Callback function for
1496 void extract_encapsulated_message(char *name, char *filename, char *partnum, char *disp,
1497 void *content, char *cbtype, char *cbcharset, size_t length,
1498 char *encoding, char *cbid, void *cbuserdata)
1500 struct encapmsg *encap;
1502 encap = (struct encapmsg *)cbuserdata;
1504 /* Only proceed if this is the desired section... */
1505 if (!strcasecmp(encap->desired_section, partnum)) {
1506 encap->msglen = length;
1507 encap->msg = malloc(length + 2);
1508 memcpy(encap->msg, content, length);
1515 * Determine whether the specified message exists in the cached_msglist
1516 * (This is a security check)
1518 int check_cached_msglist(long msgnum) {
1520 /* cases in which we skip the check */
1521 if (!CC) return om_ok; /* not a session */
1522 if (CC->client_socket <= 0) return om_ok; /* not a client session */
1523 if (CC->cached_msglist == NULL) return om_access_denied; /* no msglist fetched */
1524 if (CC->cached_num_msgs == 0) return om_access_denied; /* nothing to check */
1527 /* Do a binary search within the cached_msglist for the requested msgnum */
1529 int max = (CC->cached_num_msgs - 1);
1531 while (max >= min) {
1532 int middle = min + (max-min) / 2 ;
1533 if (msgnum == CC->cached_msglist[middle]) {
1536 if (msgnum > CC->cached_msglist[middle]) {
1544 return om_access_denied;
1550 * Get a message off disk. (returns om_* values found in msgbase.h)
1553 int CtdlOutputMsg(long msg_num, /* message number (local) to fetch */
1554 int mode, /* how would you like that message? */
1555 int headers_only, /* eschew the message body? */
1556 int do_proto, /* do Citadel protocol responses? */
1557 int crlf, /* Use CRLF newlines instead of LF? */
1558 char *section, /* NULL or a message/rfc822 section */
1559 int flags, /* various flags; see msgbase.h */
1564 struct CtdlMessage *TheMessage = NULL;
1565 int retcode = CIT_OK;
1566 struct encapmsg encap;
1569 syslog(LOG_DEBUG, "msgbase: CtdlOutputMsg(msgnum=%ld, mode=%d, section=%s)",
1571 (section ? section : "<>")
1574 r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
1577 if (r == om_not_logged_in) {
1578 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1581 cprintf("%d An unknown error has occurred.\n", ERROR);
1588 * Check to make sure the message is actually IN this room
1590 r = check_cached_msglist(msg_num);
1591 if (r == om_access_denied) {
1592 /* Not in the cache? We get ONE shot to check it again. */
1593 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, NULL, NULL);
1594 r = check_cached_msglist(msg_num);
1597 syslog(LOG_DEBUG, "msgbase: security check fail; message %ld is not in %s",
1598 msg_num, CC->room.QRname
1601 if (r == om_access_denied) {
1602 cprintf("%d message %ld was not found in this room\n",
1603 ERROR + HIGHER_ACCESS_REQUIRED,
1612 * Fetch the message from disk. If we're in HEADERS_FAST mode,
1613 * request that we don't even bother loading the body into memory.
1615 if (headers_only == HEADERS_FAST) {
1616 TheMessage = CtdlFetchMessage(msg_num, 0, 1);
1619 TheMessage = CtdlFetchMessage(msg_num, 1, 1);
1622 if (TheMessage == NULL) {
1623 if (do_proto) cprintf("%d Can't locate msg %ld on disk\n",
1624 ERROR + MESSAGE_NOT_FOUND, msg_num);
1625 return(om_no_such_msg);
1628 /* Here is the weird form of this command, to process only an
1629 * encapsulated message/rfc822 section.
1631 if (section) if (!IsEmptyStr(section)) if (strcmp(section, "0")) {
1632 memset(&encap, 0, sizeof encap);
1633 safestrncpy(encap.desired_section, section, sizeof encap.desired_section);
1634 mime_parser(CM_RANGE(TheMessage, eMesageText),
1635 *extract_encapsulated_message,
1636 NULL, NULL, (void *)&encap, 0
1639 if ((Author != NULL) && (*Author == NULL))
1642 CM_GetAsField(TheMessage, eAuthor, Author, &len);
1644 if ((Address != NULL) && (*Address == NULL))
1647 CM_GetAsField(TheMessage, erFc822Addr, Address, &len);
1649 if ((MessageID != NULL) && (*MessageID == NULL))
1652 CM_GetAsField(TheMessage, emessageId, MessageID, &len);
1654 CM_Free(TheMessage);
1658 encap.msg[encap.msglen] = 0;
1659 TheMessage = convert_internet_message(encap.msg);
1660 encap.msg = NULL; /* no free() here, TheMessage owns it now */
1662 /* Now we let it fall through to the bottom of this
1663 * function, because TheMessage now contains the
1664 * encapsulated message instead of the top-level
1665 * message. Isn't that neat?
1670 cprintf("%d msg %ld has no part %s\n",
1671 ERROR + MESSAGE_NOT_FOUND,
1675 retcode = om_no_such_msg;
1680 /* Ok, output the message now */
1681 if (retcode == CIT_OK)
1682 retcode = CtdlOutputPreLoadedMsg(TheMessage, mode, headers_only, do_proto, crlf, flags);
1683 if ((Author != NULL) && (*Author == NULL))
1686 CM_GetAsField(TheMessage, eAuthor, Author, &len);
1688 if ((Address != NULL) && (*Address == NULL))
1691 CM_GetAsField(TheMessage, erFc822Addr, Address, &len);
1693 if ((MessageID != NULL) && (*MessageID == NULL))
1696 CM_GetAsField(TheMessage, emessageId, MessageID, &len);
1699 CM_Free(TheMessage);
1706 void OutputCtdlMsgHeaders(
1707 struct CtdlMessage *TheMessage,
1708 int do_proto) /* do Citadel protocol responses? */
1713 char display_name[256];
1715 /* begin header processing loop for Citadel message format */
1716 safestrncpy(display_name, "<unknown>", sizeof display_name);
1717 if (!CM_IsEmpty(TheMessage, eAuthor)) {
1718 strcpy(buf, TheMessage->cm_fields[eAuthor]);
1719 if (TheMessage->cm_anon_type == MES_ANONONLY) {
1720 safestrncpy(display_name, "****", sizeof display_name);
1722 else if (TheMessage->cm_anon_type == MES_ANONOPT) {
1723 safestrncpy(display_name, "anonymous", sizeof display_name);
1726 safestrncpy(display_name, buf, sizeof display_name);
1728 if ((is_room_aide())
1729 && ((TheMessage->cm_anon_type == MES_ANONONLY)
1730 || (TheMessage->cm_anon_type == MES_ANONOPT))) {
1731 size_t tmp = strlen(display_name);
1732 snprintf(&display_name[tmp],
1733 sizeof display_name - tmp,
1738 /* Don't show Internet address for users on the
1739 * local Citadel network.
1742 if (!CM_IsEmpty(TheMessage, eNodeName) &&
1743 (haschar(TheMessage->cm_fields[eNodeName], '.') == 0))
1748 /* Now spew the header fields in the order we like them. */
1749 for (i=0; i< NDiskFields; ++i) {
1751 Field = FieldOrder[i];
1752 if (Field != eMesageText) {
1753 if ( (!CM_IsEmpty(TheMessage, Field))
1754 && (msgkeys[Field] != NULL) ) {
1755 if ((Field == eenVelopeTo) ||
1756 (Field == eRecipient) ||
1757 (Field == eCarbonCopY)) {
1758 sanitize_truncated_recipient(TheMessage->cm_fields[Field]);
1760 if (Field == eAuthor) {
1761 if (do_proto) cprintf("%s=%s\n",
1765 else if ((Field == erFc822Addr) && (suppress_f)) {
1768 /* Masquerade display name if needed */
1770 if (do_proto) cprintf("%s=%s\n",
1772 TheMessage->cm_fields[Field]
1781 void OutputRFC822MsgHeaders(
1782 struct CtdlMessage *TheMessage,
1783 int flags, /* should the bessage be exported clean */
1784 const char *nl, int nlen,
1785 char *mid, long sizeof_mid,
1786 char *suser, long sizeof_suser,
1787 char *luser, long sizeof_luser,
1788 char *fuser, long sizeof_fuser,
1789 char *snode, long sizeof_snode)
1791 char datestamp[100];
1792 int subject_found = 0;
1799 for (i = 0; i < NDiskFields; ++i) {
1800 if (TheMessage->cm_fields[FieldOrder[i]]) {
1801 mptr = mpptr = TheMessage->cm_fields[FieldOrder[i]];
1802 switch (FieldOrder[i]) {
1804 safestrncpy(luser, mptr, sizeof_luser);
1805 safestrncpy(suser, mptr, sizeof_suser);
1808 if ((flags & QP_EADDR) != 0) {
1809 mptr = qp_encode_email_addrs(mptr);
1811 sanitize_truncated_recipient(mptr);
1812 cprintf("CC: %s%s", mptr, nl);
1815 cprintf("Return-Path: %s%s", mptr, nl);
1818 cprintf("List-ID: %s%s", mptr, nl);
1821 if ((flags & QP_EADDR) != 0)
1822 mptr = qp_encode_email_addrs(mptr);
1824 while ((*hptr != '\0') && isspace(*hptr))
1826 if (!IsEmptyStr(hptr))
1827 cprintf("Envelope-To: %s%s", hptr, nl);
1830 cprintf("Subject: %s%s", mptr, nl);
1834 safestrncpy(mid, mptr, sizeof_mid); /// TODO: detect @ here and copy @nodename in if not found.
1837 safestrncpy(fuser, mptr, sizeof_fuser);
1838 /* case eOriginalRoom:
1839 cprintf("X-Citadel-Room: %s%s",
1844 safestrncpy(snode, mptr, sizeof_snode);
1847 if (haschar(mptr, '@') == 0)
1849 sanitize_truncated_recipient(mptr);
1850 cprintf("To: %s@%s", mptr, CtdlGetConfigStr("c_fqdn"));
1855 if ((flags & QP_EADDR) != 0) {
1856 mptr = qp_encode_email_addrs(mptr);
1858 sanitize_truncated_recipient(mptr);
1859 cprintf("To: %s", mptr);
1864 datestring(datestamp, sizeof datestamp,
1865 atol(mptr), DATESTRING_RFC822);
1866 cprintf("Date: %s%s", datestamp, nl);
1869 cprintf("References: ");
1870 k = num_tokens(mptr, '|');
1871 for (j=0; j<k; ++j) {
1872 extract_token(buf, mptr, j, '|', sizeof buf);
1873 cprintf("<%s>", buf);
1884 while ((*hptr != '\0') && isspace(*hptr))
1886 if (!IsEmptyStr(hptr))
1887 cprintf("Reply-To: %s%s", mptr, nl);
1903 /* these don't map to mime message headers. */
1911 if (subject_found == 0) {
1912 cprintf("Subject: (no subject)%s", nl);
1917 void Dump_RFC822HeadersBody(
1918 struct CtdlMessage *TheMessage,
1919 int headers_only, /* eschew the message body? */
1920 int flags, /* should the bessage be exported clean? */
1922 const char *nl, int nlen)
1924 cit_uint8_t prev_ch;
1926 const char *StartOfText = StrBufNOTNULL;
1929 int nllen = strlen(nl);
1933 mptr = TheMessage->cm_fields[eMesageText];
1937 while (*mptr != '\0') {
1938 if (*mptr == '\r') {
1945 eoh = (*(mptr+1) == '\r') && (*(mptr+2) == '\n');
1947 eoh = *(mptr+1) == '\n';
1951 StartOfText = strchr(StartOfText, '\n');
1952 StartOfText = strchr(StartOfText, '\n');
1955 if (((headers_only == HEADERS_NONE) && (mptr >= StartOfText)) ||
1956 ((headers_only == HEADERS_ONLY) && (mptr < StartOfText)) ||
1957 ((headers_only != HEADERS_NONE) &&
1958 (headers_only != HEADERS_ONLY))
1960 if (*mptr == '\n') {
1961 memcpy(&outbuf[outlen], nl, nllen);
1963 outbuf[outlen] = '\0';
1966 outbuf[outlen++] = *mptr;
1970 if (flags & ESC_DOT)
1972 if ((prev_ch == '\n') &&
1974 ((*(mptr+1) == '\r') || (*(mptr+1) == '\n')))
1976 outbuf[outlen++] = '.';
1981 if (outlen > 1000) {
1982 if (client_write(outbuf, outlen) == -1)
1984 syslog(LOG_ERR, "msgbase: Dump_RFC822HeadersBody() aborting due to write failure");
1987 lfSent = (outbuf[outlen - 1] == '\n');
1992 client_write(outbuf, outlen);
1993 lfSent = (outbuf[outlen - 1] == '\n');
1996 client_write(nl, nlen);
2001 /* If the format type on disk is 1 (fixed-format), then we want
2002 * everything to be output completely literally ... regardless of
2003 * what message transfer format is in use.
2005 void DumpFormatFixed(
2006 struct CtdlMessage *TheMessage,
2007 int mode, /* how would you like that message? */
2008 const char *nl, int nllen)
2016 mptr = TheMessage->cm_fields[eMesageText];
2018 if (mode == MT_MIME) {
2019 cprintf("Content-type: text/plain\n\n");
2023 while (ch = *mptr++, ch > 0) {
2027 if ((buflen > 250) && (!xlline)){
2031 while ((buflen > 0) &&
2032 (!isspace(buf[buflen])))
2038 mptr -= tbuflen - buflen;
2043 /* if we reach the outer bounds of our buffer,
2044 abort without respect what whe purge. */
2047 (buflen > SIZ - nllen - 2)))
2051 memcpy (&buf[buflen], nl, nllen);
2055 if (client_write(buf, buflen) == -1)
2057 syslog(LOG_ERR, "msgbase: DumpFormatFixed() aborting due to write failure");
2069 if (!IsEmptyStr(buf))
2070 cprintf("%s%s", buf, nl);
2074 * Get a message off disk. (returns om_* values found in msgbase.h)
2076 int CtdlOutputPreLoadedMsg(
2077 struct CtdlMessage *TheMessage,
2078 int mode, /* how would you like that message? */
2079 int headers_only, /* eschew the message body? */
2080 int do_proto, /* do Citadel protocol responses? */
2081 int crlf, /* Use CRLF newlines instead of LF? */
2082 int flags /* should the bessage be exported clean? */
2085 const char *nl; /* newline string */
2089 /* Buffers needed for RFC822 translation. These are all filled
2090 * using functions that are bounds-checked, and therefore we can
2091 * make them substantially smaller than SIZ.
2099 syslog(LOG_DEBUG, "msgbase: CtdlOutputPreLoadedMsg(TheMessage=%s, %d, %d, %d, %d",
2100 ((TheMessage == NULL) ? "NULL" : "not null"),
2101 mode, headers_only, do_proto, crlf
2104 strcpy(mid, "unknown");
2105 nl = (crlf ? "\r\n" : "\n");
2106 nlen = crlf ? 2 : 1;
2108 if (!CM_IsValidMsg(TheMessage)) {
2109 syslog(LOG_ERR, "msgbase: error; invalid preloaded message for output");
2110 return(om_no_such_msg);
2113 /* Suppress envelope recipients if required to avoid disclosing BCC addresses.
2114 * Pad it with spaces in order to avoid changing the RFC822 length of the message.
2116 if ( (flags & SUPPRESS_ENV_TO) && (!CM_IsEmpty(TheMessage, eenVelopeTo)) ) {
2117 memset(TheMessage->cm_fields[eenVelopeTo], ' ', TheMessage->cm_lengths[eenVelopeTo]);
2120 /* Are we downloading a MIME component? */
2121 if (mode == MT_DOWNLOAD) {
2122 if (TheMessage->cm_format_type != FMT_RFC822) {
2124 cprintf("%d This is not a MIME message.\n",
2125 ERROR + ILLEGAL_VALUE);
2126 } else if (CC->download_fp != NULL) {
2127 if (do_proto) cprintf(
2128 "%d You already have a download open.\n",
2129 ERROR + RESOURCE_BUSY);
2131 /* Parse the message text component */
2132 mime_parser(CM_RANGE(TheMessage, eMesageText),
2133 *mime_download, NULL, NULL, NULL, 0);
2134 /* If there's no file open by this time, the requested
2135 * section wasn't found, so print an error
2137 if (CC->download_fp == NULL) {
2138 if (do_proto) cprintf(
2139 "%d Section %s not found.\n",
2140 ERROR + FILE_NOT_FOUND,
2141 CC->download_desired_section);
2144 return((CC->download_fp != NULL) ? om_ok : om_mime_error);
2147 /* MT_SPEW_SECTION is like MT_DOWNLOAD except it outputs the whole MIME part
2148 * in a single server operation instead of opening a download file.
2150 if (mode == MT_SPEW_SECTION) {
2151 if (TheMessage->cm_format_type != FMT_RFC822) {
2153 cprintf("%d This is not a MIME message.\n",
2154 ERROR + ILLEGAL_VALUE);
2156 /* Parse the message text component */
2159 mime_parser(CM_RANGE(TheMessage, eMesageText),
2160 *mime_spew_section, NULL, NULL, (void *)&found_it, 0);
2161 /* If section wasn't found, print an error
2164 if (do_proto) cprintf(
2165 "%d Section %s not found.\n",
2166 ERROR + FILE_NOT_FOUND,
2167 CC->download_desired_section);
2170 return((CC->download_fp != NULL) ? om_ok : om_mime_error);
2173 /* now for the user-mode message reading loops */
2174 if (do_proto) cprintf("%d msg:\n", LISTING_FOLLOWS);
2176 /* Does the caller want to skip the headers? */
2177 if (headers_only == HEADERS_NONE) goto START_TEXT;
2179 /* Tell the client which format type we're using. */
2180 if ( (mode == MT_CITADEL) && (do_proto) ) {
2181 cprintf("type=%d\n", TheMessage->cm_format_type);
2184 /* nhdr=yes means that we're only displaying headers, no body */
2185 if ( (TheMessage->cm_anon_type == MES_ANONONLY)
2186 && ((mode == MT_CITADEL) || (mode == MT_MIME))
2189 cprintf("nhdr=yes\n");
2192 if ((mode == MT_CITADEL) || (mode == MT_MIME))
2193 OutputCtdlMsgHeaders(TheMessage, do_proto);
2196 /* begin header processing loop for RFC822 transfer format */
2200 memcpy(snode, CtdlGetConfigStr("c_nodename"), strlen(CtdlGetConfigStr("c_nodename")) + 1);
2201 if (mode == MT_RFC822)
2202 OutputRFC822MsgHeaders(
2207 suser, sizeof(suser),
2208 luser, sizeof(luser),
2209 fuser, sizeof(fuser),
2210 snode, sizeof(snode)
2214 for (i=0; !IsEmptyStr(&suser[i]); ++i) {
2215 suser[i] = tolower(suser[i]);
2216 if (!isalnum(suser[i])) suser[i]='_';
2219 if (mode == MT_RFC822) {
2220 if (!strcasecmp(snode, NODENAME)) {
2221 safestrncpy(snode, FQDN, sizeof snode);
2224 /* Construct a fun message id */
2225 cprintf("Message-ID: <%s", mid);/// todo: this possibly breaks threadding mails.
2226 if (strchr(mid, '@')==NULL) {
2227 cprintf("@%s", snode);
2231 if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONONLY)) {
2232 cprintf("From: \"----\" <x@x.org>%s", nl);
2234 else if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONOPT)) {
2235 cprintf("From: \"anonymous\" <x@x.org>%s", nl);
2237 else if (!IsEmptyStr(fuser)) {
2238 cprintf("From: \"%s\" <%s>%s", luser, fuser, nl);
2241 cprintf("From: \"%s\" <%s@%s>%s", luser, suser, snode, nl);
2244 /* Blank line signifying RFC822 end-of-headers */
2245 if (TheMessage->cm_format_type != FMT_RFC822) {
2250 /* end header processing loop ... at this point, we're in the text */
2252 if (headers_only == HEADERS_FAST) goto DONE;
2254 /* Tell the client about the MIME parts in this message */
2255 if (TheMessage->cm_format_type == FMT_RFC822) {
2256 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2257 memset(&ma, 0, sizeof(struct ma_info));
2258 mime_parser(CM_RANGE(TheMessage, eMesageText),
2259 (do_proto ? *list_this_part : NULL),
2260 (do_proto ? *list_this_pref : NULL),
2261 (do_proto ? *list_this_suff : NULL),
2264 else if (mode == MT_RFC822) { /* unparsed RFC822 dump */
2265 Dump_RFC822HeadersBody(
2274 if (headers_only == HEADERS_ONLY) {
2278 /* signify start of msg text */
2279 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2280 if (do_proto) cprintf("text\n");
2283 if (TheMessage->cm_format_type == FMT_FIXED)
2286 mode, /* how would you like that message? */
2289 /* If the message on disk is format 0 (Citadel vari-format), we
2290 * output using the formatter at 80 columns. This is the final output
2291 * form if the transfer format is RFC822, but if the transfer format
2292 * is Citadel proprietary, it'll still work, because the indentation
2293 * for new paragraphs is correct and the client will reformat the
2294 * message to the reader's screen width.
2296 if (TheMessage->cm_format_type == FMT_CITADEL) {
2297 if (mode == MT_MIME) {
2298 cprintf("Content-type: text/x-citadel-variformat\n\n");
2300 memfmout(TheMessage->cm_fields[eMesageText], nl);
2303 /* If the message on disk is format 4 (MIME), we've gotta hand it
2304 * off to the MIME parser. The client has already been told that
2305 * this message is format 1 (fixed format), so the callback function
2306 * we use will display those parts as-is.
2308 if (TheMessage->cm_format_type == FMT_RFC822) {
2309 memset(&ma, 0, sizeof(struct ma_info));
2311 if (mode == MT_MIME) {
2312 ma.use_fo_hooks = 0;
2313 strcpy(ma.chosen_part, "1");
2314 ma.chosen_pref = 9999;
2315 ma.dont_decode = CC->msg4_dont_decode;
2316 mime_parser(CM_RANGE(TheMessage, eMesageText),
2317 *choose_preferred, *fixed_output_pre,
2318 *fixed_output_post, (void *)&ma, 1);
2319 mime_parser(CM_RANGE(TheMessage, eMesageText),
2320 *output_preferred, NULL, NULL, (void *)&ma, 1);
2323 ma.use_fo_hooks = 1;
2324 mime_parser(CM_RANGE(TheMessage, eMesageText),
2325 *fixed_output, *fixed_output_pre,
2326 *fixed_output_post, (void *)&ma, 0);
2331 DONE: /* now we're done */
2332 if (do_proto) cprintf("000\n");
2337 * Save one or more message pointers into a specified room
2338 * (Returns 0 for success, nonzero for failure)
2339 * roomname may be NULL to use the current room
2341 * Note that the 'supplied_msg' field may be set to NULL, in which case
2342 * the message will be fetched from disk, by number, if we need to perform
2343 * replication checks. This adds an additional database read, so if the
2344 * caller already has the message in memory then it should be supplied. (Obviously
2345 * this mode of operation only works if we're saving a single message.)
2347 int CtdlSaveMsgPointersInRoom(char *roomname, long newmsgidlist[], int num_newmsgs,
2348 int do_repl_check, struct CtdlMessage *supplied_msg, int suppress_refcount_adj
2351 char hold_rm[ROOMNAMELEN];
2352 struct cdbdata *cdbfr;
2355 long highest_msg = 0L;
2358 struct CtdlMessage *msg = NULL;
2360 long *msgs_to_be_merged = NULL;
2361 int num_msgs_to_be_merged = 0;
2364 "msgbase: CtdlSaveMsgPointersInRoom(room=%s, num_msgs=%d, repl=%d, suppress_rca=%d)",
2365 roomname, num_newmsgs, do_repl_check, suppress_refcount_adj
2368 strcpy(hold_rm, CC->room.QRname);
2371 if (newmsgidlist == NULL) return(ERROR + INTERNAL_ERROR);
2372 if (num_newmsgs < 1) return(ERROR + INTERNAL_ERROR);
2373 if (num_newmsgs > 1) supplied_msg = NULL;
2375 /* Now the regular stuff */
2376 if (CtdlGetRoomLock(&CC->room,
2377 ((roomname != NULL) ? roomname : CC->room.QRname) )
2379 syslog(LOG_ERR, "msgbase: no such room <%s>", roomname);
2380 return(ERROR + ROOM_NOT_FOUND);
2384 msgs_to_be_merged = malloc(sizeof(long) * num_newmsgs);
2385 num_msgs_to_be_merged = 0;
2388 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
2389 if (cdbfr == NULL) {
2393 msglist = (long *) cdbfr->ptr;
2394 cdbfr->ptr = NULL; /* CtdlSaveMsgPointerInRoom() now owns this memory */
2395 num_msgs = cdbfr->len / sizeof(long);
2400 /* Create a list of msgid's which were supplied by the caller, but do
2401 * not already exist in the target room. It is absolutely taboo to
2402 * have more than one reference to the same message in a room.
2404 for (i=0; i<num_newmsgs; ++i) {
2406 if (num_msgs > 0) for (j=0; j<num_msgs; ++j) {
2407 if (msglist[j] == newmsgidlist[i]) {
2412 msgs_to_be_merged[num_msgs_to_be_merged++] = newmsgidlist[i];
2416 syslog(LOG_DEBUG, "msgbase: %d unique messages to be merged", num_msgs_to_be_merged);
2419 * Now merge the new messages
2421 msglist = realloc(msglist, (sizeof(long) * (num_msgs + num_msgs_to_be_merged)) );
2422 if (msglist == NULL) {
2423 syslog(LOG_ALERT, "msgbase: ERROR; can't realloc message list!");
2424 free(msgs_to_be_merged);
2425 return (ERROR + INTERNAL_ERROR);
2427 memcpy(&msglist[num_msgs], msgs_to_be_merged, (sizeof(long) * num_msgs_to_be_merged) );
2428 num_msgs += num_msgs_to_be_merged;
2430 /* Sort the message list, so all the msgid's are in order */
2431 num_msgs = sort_msglist(msglist, num_msgs);
2433 /* Determine the highest message number */
2434 highest_msg = msglist[num_msgs - 1];
2436 /* Write it back to disk. */
2437 cdb_store(CDB_MSGLISTS, &CC->room.QRnumber, (int)sizeof(long),
2438 msglist, (int)(num_msgs * sizeof(long)));
2440 /* Free up the memory we used. */
2443 /* Update the highest-message pointer and unlock the room. */
2444 CC->room.QRhighest = highest_msg;
2445 CtdlPutRoomLock(&CC->room);
2447 /* Perform replication checks if necessary */
2448 if ( (DoesThisRoomNeedEuidIndexing(&CC->room)) && (do_repl_check) ) {
2449 syslog(LOG_DEBUG, "msgbase: CtdlSaveMsgPointerInRoom() doing repl checks");
2451 for (i=0; i<num_msgs_to_be_merged; ++i) {
2452 msgid = msgs_to_be_merged[i];
2454 if (supplied_msg != NULL) {
2458 msg = CtdlFetchMessage(msgid, 0, 1);
2462 ReplicationChecks(msg);
2464 /* If the message has an Exclusive ID, index that... */
2465 if (!CM_IsEmpty(msg, eExclusiveID)) {
2466 index_message_by_euid(msg->cm_fields[eExclusiveID], &CC->room, msgid);
2469 /* Free up the memory we may have allocated */
2470 if (msg != supplied_msg) {
2479 syslog(LOG_DEBUG, "msgbase: CtdlSaveMsgPointerInRoom() skips repl checks");
2482 /* Submit this room for processing by hooks */
2483 PerformRoomHooks(&CC->room);
2485 /* Go back to the room we were in before we wandered here... */
2486 CtdlGetRoom(&CC->room, hold_rm);
2488 /* Bump the reference count for all messages which were merged */
2489 if (!suppress_refcount_adj) {
2490 AdjRefCountList(msgs_to_be_merged, num_msgs_to_be_merged, +1);
2493 /* Free up memory... */
2494 if (msgs_to_be_merged != NULL) {
2495 free(msgs_to_be_merged);
2498 /* Return success. */
2504 * This is the same as CtdlSaveMsgPointersInRoom() but it only accepts
2507 int CtdlSaveMsgPointerInRoom(char *roomname, long msgid,
2508 int do_repl_check, struct CtdlMessage *supplied_msg)
2510 return CtdlSaveMsgPointersInRoom(roomname, &msgid, 1, do_repl_check, supplied_msg, 0);
2517 * Message base operation to save a new message to the message store
2518 * (returns new message number)
2520 * This is the back end for CtdlSubmitMsg() and should not be directly
2521 * called by server-side modules.
2524 long CtdlSaveThisMessage(struct CtdlMessage *msg, long msgid, int Reply) {
2532 * If the message is big, set its body aside for storage elsewhere
2533 * and we hide the message body from the serializer
2535 if (!CM_IsEmpty(msg, eMesageText) && msg->cm_lengths[eMesageText] > BIGMSG)
2538 holdM = msg->cm_fields[eMesageText];
2539 msg->cm_fields[eMesageText] = NULL;
2540 holdMLen = msg->cm_lengths[eMesageText];
2541 msg->cm_lengths[eMesageText] = 0;
2544 /* Serialize our data structure for storage in the database */
2545 CtdlSerializeMessage(&smr, msg);
2548 /* put the message body back into the message */
2549 msg->cm_fields[eMesageText] = holdM;
2550 msg->cm_lengths[eMesageText] = holdMLen;
2555 cprintf("%d Unable to serialize message\n",
2556 ERROR + INTERNAL_ERROR);
2559 syslog(LOG_ERR, "msgbase: CtdlSaveMessage() unable to serialize message");
2565 /* Write our little bundle of joy into the message base */
2566 retval = cdb_store(CDB_MSGMAIN, &msgid, (int)sizeof(long),
2569 syslog(LOG_ERR, "msgbase: can't store message %ld: %ld", msgid, retval);
2573 retval = cdb_store(CDB_BIGMSGS,
2580 syslog(LOG_ERR, "msgbase: failed to store message body for msgid %ld: %ld", msgid, retval);
2585 /* Free the memory we used for the serialized message */
2591 long send_message(struct CtdlMessage *msg) {
2597 /* Get a new message number */
2598 newmsgid = get_new_message_number();
2600 /* Generate an ID if we don't have one already */
2601 if (CM_IsEmpty(msg, emessageId)) {
2602 msgidbuflen = snprintf(msgidbuf, sizeof msgidbuf, "%08lX-%08lX@%s",
2603 (long unsigned int) time(NULL),
2604 (long unsigned int) newmsgid,
2605 CtdlGetConfigStr("c_fqdn")
2608 CM_SetField(msg, emessageId, msgidbuf, msgidbuflen);
2611 retval = CtdlSaveThisMessage(msg, newmsgid, 1);
2617 /* Return the *local* message ID to the caller
2618 * (even if we're storing an incoming network message)
2626 * Serialize a struct CtdlMessage into the format used on disk and network.
2628 * This function loads up a "struct ser_ret" (defined in server.h) which
2629 * contains the length of the serialized message and a pointer to the
2630 * serialized message in memory. THE LATTER MUST BE FREED BY THE CALLER.
2632 void CtdlSerializeMessage(struct ser_ret *ret, /* return values */
2633 struct CtdlMessage *msg) /* unserialized msg */
2639 * Check for valid message format
2641 if (CM_IsValidMsg(msg) == 0) {
2642 syslog(LOG_ERR, "msgbase: CtdlSerializeMessage() aborting due to invalid message");
2649 for (i=0; i < NDiskFields; ++i)
2650 if (msg->cm_fields[FieldOrder[i]] != NULL)
2651 ret->len += msg->cm_lengths[FieldOrder[i]] + 2;
2653 ret->ser = malloc(ret->len);
2654 if (ret->ser == NULL) {
2655 syslog(LOG_ERR, "msgbase: CtdlSerializeMessage() malloc(%ld) failed: %m", (long)ret->len);
2662 ret->ser[1] = msg->cm_anon_type;
2663 ret->ser[2] = msg->cm_format_type;
2666 for (i=0; i < NDiskFields; ++i)
2667 if (msg->cm_fields[FieldOrder[i]] != NULL)
2669 ret->ser[wlen++] = (char)FieldOrder[i];
2671 memcpy(&ret->ser[wlen],
2672 msg->cm_fields[FieldOrder[i]],
2673 msg->cm_lengths[FieldOrder[i]] + 1);
2675 wlen = wlen + msg->cm_lengths[FieldOrder[i]] + 1;
2678 if (ret->len != wlen) {
2679 syslog(LOG_ERR, "msgbase: ERROR; len=%ld wlen=%ld", (long)ret->len, (long)wlen);
2687 * Check to see if any messages already exist in the current room which
2688 * carry the same Exclusive ID as this one. If any are found, delete them.
2690 void ReplicationChecks(struct CtdlMessage *msg) {
2691 long old_msgnum = (-1L);
2693 if (DoesThisRoomNeedEuidIndexing(&CC->room) == 0) return;
2695 syslog(LOG_DEBUG, "msgbase: performing replication checks in <%s>", CC->room.QRname);
2697 /* No exclusive id? Don't do anything. */
2698 if (msg == NULL) return;
2699 if (CM_IsEmpty(msg, eExclusiveID)) return;
2701 /*syslog(LOG_DEBUG, "msgbase: exclusive ID: <%s> for room <%s>",
2702 msg->cm_fields[eExclusiveID], CC->room.QRname);*/
2704 old_msgnum = CtdlLocateMessageByEuid(msg->cm_fields[eExclusiveID], &CC->room);
2705 if (old_msgnum > 0L) {
2706 syslog(LOG_DEBUG, "msgbase: ReplicationChecks() replacing message %ld", old_msgnum);
2707 CtdlDeleteMessages(CC->room.QRname, &old_msgnum, 1, "");
2714 * Save a message to disk and submit it into the delivery system.
2716 long CtdlSubmitMsg(struct CtdlMessage *msg, /* message to save */
2717 recptypes *recps, /* recipients (if mail) */
2718 const char *force, /* force a particular room? */
2719 int flags /* should the message be exported clean? */
2722 char hold_rm[ROOMNAMELEN];
2723 char actual_rm[ROOMNAMELEN];
2724 char force_room[ROOMNAMELEN];
2725 char content_type[SIZ]; /* We have to learn this */
2726 char recipient[SIZ];
2727 char bounce_to[1024];
2730 const char *mptr = NULL;
2731 struct ctdluser userbuf;
2733 struct MetaData smi;
2734 char *collected_addresses = NULL;
2735 struct addresses_to_be_filed *aptr = NULL;
2736 StrBuf *saved_rfc822_version = NULL;
2737 int qualified_for_journaling = 0;
2739 syslog(LOG_DEBUG, "msgbase: CtdlSubmitMsg() called");
2740 if (CM_IsValidMsg(msg) == 0) return(-1); /* self check */
2742 /* If this message has no timestamp, we take the liberty of
2743 * giving it one, right now.
2745 if (CM_IsEmpty(msg, eTimestamp)) {
2746 CM_SetFieldLONG(msg, eTimestamp, time(NULL));
2749 /* If this message has no path, we generate one.
2751 if (CM_IsEmpty(msg, eMessagePath)) {
2752 if (!CM_IsEmpty(msg, eAuthor)) {
2753 CM_CopyField(msg, eMessagePath, eAuthor);
2754 for (a=0; !IsEmptyStr(&msg->cm_fields[eMessagePath][a]); ++a) {
2755 if (isspace(msg->cm_fields[eMessagePath][a])) {
2756 msg->cm_fields[eMessagePath][a] = ' ';
2761 CM_SetField(msg, eMessagePath, HKEY("unknown"));
2765 if (force == NULL) {
2766 force_room[0] = '\0';
2769 strcpy(force_room, force);
2772 /* Learn about what's inside, because it's what's inside that counts */
2773 if (CM_IsEmpty(msg, eMesageText)) {
2774 syslog(LOG_ERR, "msgbase: ERROR; attempt to save message with NULL body");
2778 switch (msg->cm_format_type) {
2780 strcpy(content_type, "text/x-citadel-variformat");
2783 strcpy(content_type, "text/plain");
2786 strcpy(content_type, "text/plain");
2787 mptr = bmstrcasestr(msg->cm_fields[eMesageText], "Content-type:");
2790 safestrncpy(content_type, &mptr[13], sizeof content_type);
2791 striplt(content_type);
2792 aptr = content_type;
2793 while (!IsEmptyStr(aptr)) {
2805 /* Goto the correct room */
2806 room = (recps) ? CC->room.QRname : SENTITEMS;
2807 syslog(LOG_DEBUG, "msgbase: selected room %s", room);
2808 strcpy(hold_rm, CC->room.QRname);
2809 strcpy(actual_rm, CC->room.QRname);
2810 if (recps != NULL) {
2811 strcpy(actual_rm, SENTITEMS);
2814 /* If the user is a twit, move to the twit room for posting */
2816 if (CC->user.axlevel == AxProbU) {
2817 strcpy(hold_rm, actual_rm);
2818 strcpy(actual_rm, CtdlGetConfigStr("c_twitroom"));
2819 syslog(LOG_DEBUG, "msgbase: diverting to twit room");
2823 /* ...or if this message is destined for Aide> then go there. */
2824 if (!IsEmptyStr(force_room)) {
2825 strcpy(actual_rm, force_room);
2828 syslog(LOG_DEBUG, "msgbase: final selection: %s (%s)", actual_rm, room);
2829 if (strcasecmp(actual_rm, CC->room.QRname)) {
2830 /* CtdlGetRoom(&CC->room, actual_rm); */
2831 CtdlUserGoto(actual_rm, 0, 1, NULL, NULL, NULL, NULL);
2835 * If this message has no O (room) field, generate one.
2837 if (CM_IsEmpty(msg, eOriginalRoom) && !IsEmptyStr(CC->room.QRname)) {
2838 CM_SetField(msg, eOriginalRoom, CC->room.QRname, strlen(CC->room.QRname));
2841 /* Perform "before save" hooks (aborting if any return nonzero) */
2842 syslog(LOG_DEBUG, "msgbase: performing before-save hooks");
2843 if (PerformMessageHooks(msg, recps, EVT_BEFORESAVE) > 0) return(-3);
2846 * If this message has an Exclusive ID, and the room is replication
2847 * checking enabled, then do replication checks.
2849 if (DoesThisRoomNeedEuidIndexing(&CC->room)) {
2850 ReplicationChecks(msg);
2853 /* Save it to disk */
2854 syslog(LOG_DEBUG, "msgbase: saving to disk");
2855 newmsgid = send_message(msg);
2856 if (newmsgid <= 0L) return(-5);
2858 /* Write a supplemental message info record. This doesn't have to
2859 * be a critical section because nobody else knows about this message
2862 syslog(LOG_DEBUG, "msgbase: creating metadata record");
2863 memset(&smi, 0, sizeof(struct MetaData));
2864 smi.meta_msgnum = newmsgid;
2865 smi.meta_refcount = 0;
2866 safestrncpy(smi.meta_content_type, content_type,
2867 sizeof smi.meta_content_type);
2870 * Measure how big this message will be when rendered as RFC822.
2871 * We do this for two reasons:
2872 * 1. We need the RFC822 length for the new metadata record, so the
2873 * POP and IMAP services don't have to calculate message lengths
2874 * while the user is waiting (multiplied by potentially hundreds
2875 * or thousands of messages).
2876 * 2. If journaling is enabled, we will need an RFC822 version of the
2877 * message to attach to the journalized copy.
2879 if (CC->redirect_buffer != NULL) {
2880 syslog(LOG_ALERT, "msgbase: CC->redirect_buffer is not NULL during message submission!");
2883 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
2884 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, QP_EADDR);
2885 smi.meta_rfc822_length = StrLength(CC->redirect_buffer);
2886 saved_rfc822_version = CC->redirect_buffer;
2887 CC->redirect_buffer = NULL;
2891 /* Now figure out where to store the pointers */
2892 syslog(LOG_DEBUG, "msgbase: storing pointers");
2894 /* If this is being done by the networker delivering a private
2895 * message, we want to BYPASS saving the sender's copy (because there
2896 * is no local sender; it would otherwise go to the Trashcan).
2898 if ((!CC->internal_pgm) || (recps == NULL)) {
2899 if (CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 1, msg) != 0) {
2900 syslog(LOG_ERR, "msgbase: ERROR saving message pointer!");
2901 CtdlSaveMsgPointerInRoom(CtdlGetConfigStr("c_aideroom"), newmsgid, 0, msg);
2905 /* For internet mail, drop a copy in the outbound queue room */
2906 if ((recps != NULL) && (recps->num_internet > 0)) {
2907 CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, newmsgid, 0, msg);
2910 /* If other rooms are specified, drop them there too. */
2911 if ((recps != NULL) && (recps->num_room > 0))
2912 for (i=0; i<num_tokens(recps->recp_room, '|'); ++i) {
2913 extract_token(recipient, recps->recp_room, i,
2914 '|', sizeof recipient);
2915 syslog(LOG_DEBUG, "msgbase: delivering to room <%s>", recipient);
2916 CtdlSaveMsgPointerInRoom(recipient, newmsgid, 0, msg);
2919 /* Bump this user's messages posted counter. */
2920 syslog(LOG_DEBUG, "msgbase: updating user");
2921 CtdlLockGetCurrentUser();
2922 CC->user.posted = CC->user.posted + 1;
2923 CtdlPutCurrentUserLock();
2925 /* Decide where bounces need to be delivered */
2926 if ((recps != NULL) && (recps->bounce_to == NULL))
2928 if (CC->logged_in) {
2929 snprintf(bounce_to, sizeof bounce_to, "%s@%s", CC->user.fullname, CtdlGetConfigStr("c_nodename"));
2932 snprintf(bounce_to, sizeof bounce_to, "%s@%s", msg->cm_fields[eAuthor], msg->cm_fields[eNodeName]);
2934 recps->bounce_to = bounce_to;
2937 CM_SetFieldLONG(msg, eVltMsgNum, newmsgid);
2940 /* If this is private, local mail, make a copy in the
2941 * recipient's mailbox and bump the reference count.
2943 if ((recps != NULL) && (recps->num_local > 0))
2948 pch = recps->recp_local;
2949 recps->recp_local = recipient;
2950 ntokens = num_tokens(pch, '|');
2951 for (i=0; i<ntokens; ++i)
2953 extract_token(recipient, pch, i, '|', sizeof recipient);
2954 syslog(LOG_DEBUG, "msgbase: delivering private local mail to <%s>", recipient);
2955 if (CtdlGetUser(&userbuf, recipient) == 0) {
2956 CtdlMailboxName(actual_rm, sizeof actual_rm, &userbuf, MAILROOM);
2957 CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0, msg);
2958 CtdlBumpNewMailCounter(userbuf.usernum);
2959 PerformMessageHooks(msg, recps, EVT_AFTERUSRMBOXSAVE);
2962 syslog(LOG_DEBUG, "msgbase: no user <%s>", recipient);
2963 CtdlSaveMsgPointerInRoom(CtdlGetConfigStr("c_aideroom"), newmsgid, 0, msg);
2966 recps->recp_local = pch;
2969 /* Perform "after save" hooks */
2970 syslog(LOG_DEBUG, "msgbase: performing after-save hooks");
2972 PerformMessageHooks(msg, recps, EVT_AFTERSAVE);
2973 CM_FlushField(msg, eVltMsgNum);
2975 /* Go back to the room we started from */
2976 syslog(LOG_DEBUG, "msgbase: returning to original room %s", hold_rm);
2977 if (strcasecmp(hold_rm, CC->room.QRname))
2978 CtdlUserGoto(hold_rm, 0, 1, NULL, NULL, NULL, NULL);
2981 * Any addresses to harvest for someone's address book?
2983 if ( (CC->logged_in) && (recps != NULL) ) {
2984 collected_addresses = harvest_collected_addresses(msg);
2987 if (collected_addresses != NULL) {
2988 aptr = (struct addresses_to_be_filed *)
2989 malloc(sizeof(struct addresses_to_be_filed));
2990 CtdlMailboxName(actual_rm, sizeof actual_rm,
2991 &CC->user, USERCONTACTSROOM);
2992 aptr->roomname = strdup(actual_rm);
2993 aptr->collected_addresses = collected_addresses;
2994 begin_critical_section(S_ATBF);
2997 end_critical_section(S_ATBF);
3001 * Determine whether this message qualifies for journaling.
3003 if (!CM_IsEmpty(msg, eJournal)) {
3004 qualified_for_journaling = 0;
3007 if (recps == NULL) {
3008 qualified_for_journaling = CtdlGetConfigInt("c_journal_pubmsgs");
3010 else if (recps->num_local + recps->num_ignet + recps->num_internet > 0) {
3011 qualified_for_journaling = CtdlGetConfigInt("c_journal_email");
3014 qualified_for_journaling = CtdlGetConfigInt("c_journal_pubmsgs");
3019 * Do we have to perform journaling? If so, hand off the saved
3020 * RFC822 version will be handed off to the journaler for background
3021 * submit. Otherwise, we have to free the memory ourselves.
3023 if (saved_rfc822_version != NULL) {
3024 if (qualified_for_journaling) {
3025 JournalBackgroundSubmit(msg, saved_rfc822_version, recps);
3028 FreeStrBuf(&saved_rfc822_version);
3032 if ((recps != NULL) && (recps->bounce_to == bounce_to))
3033 recps->bounce_to = NULL;
3041 * Convenience function for generating small administrative messages.
3043 long quickie_message(const char *from,
3044 const char *fromaddr,
3049 const char *subject)
3051 struct CtdlMessage *msg;
3052 recptypes *recp = NULL;
3054 msg = malloc(sizeof(struct CtdlMessage));
3055 memset(msg, 0, sizeof(struct CtdlMessage));
3056 msg->cm_magic = CTDLMESSAGE_MAGIC;
3057 msg->cm_anon_type = MES_NORMAL;
3058 msg->cm_format_type = format_type;
3060 if (!IsEmptyStr(from)) {
3061 CM_SetField(msg, eAuthor, from, strlen(from));
3063 else if (!IsEmptyStr(fromaddr)) {
3065 CM_SetField(msg, eAuthor, fromaddr, strlen(fromaddr));
3066 pAt = strchr(msg->cm_fields[eAuthor], '@');
3068 CM_CutFieldAt(msg, eAuthor, pAt - msg->cm_fields[eAuthor]);
3072 msg->cm_fields[eAuthor] = strdup("Citadel");
3075 if (!IsEmptyStr(fromaddr)) CM_SetField(msg, erFc822Addr, fromaddr, strlen(fromaddr));
3076 if (!IsEmptyStr(room)) CM_SetField(msg, eOriginalRoom, room, strlen(room));
3077 CM_SetField(msg, eNodeName, CtdlGetConfigStr("c_nodename"), strlen(CtdlGetConfigStr("c_nodename")));
3078 if (!IsEmptyStr(to)) {
3079 CM_SetField(msg, eRecipient, to, strlen(to));
3080 recp = validate_recipients(to, NULL, 0);
3082 if (!IsEmptyStr(subject)) {
3083 CM_SetField(msg, eMsgSubject, subject, strlen(subject));
3085 if (!IsEmptyStr(text)) {
3086 CM_SetField(msg, eMesageText, text, strlen(text));
3089 long msgnum = CtdlSubmitMsg(msg, recp, room, 0);
3091 if (recp != NULL) free_recipients(recp);
3097 * Back end function used by CtdlMakeMessage() and similar functions
3099 StrBuf *CtdlReadMessageBodyBuf(char *terminator, /* token signalling EOT */
3101 size_t maxlen, /* maximum message length */
3102 StrBuf *exist, /* if non-null, append to it;
3103 exist is ALWAYS freed */
3104 int crlf /* CRLF newlines instead of LF */
3113 LineBuf = NewStrBufPlain(NULL, SIZ);
3114 if (exist == NULL) {
3115 Message = NewStrBufPlain(NULL, 4 * SIZ);
3118 Message = NewStrBufDup(exist);
3121 /* Do we need to change leading ".." to "." for SMTP escaping? */
3122 if ((tlen == 1) && (*terminator == '.')) {
3126 /* read in the lines of message text one by one */
3128 if (CtdlClientGetLine(LineBuf) < 0) {
3131 if ((StrLength(LineBuf) == tlen) && (!strcmp(ChrPtr(LineBuf), terminator))) {
3134 if ( (!flushing) && (!finished) ) {
3136 StrBufAppendBufPlain(LineBuf, HKEY("\r\n"), 0);
3139 StrBufAppendBufPlain(LineBuf, HKEY("\n"), 0);
3142 /* Unescape SMTP-style input of two dots at the beginning of the line */
3144 (StrLength(LineBuf) == 2) &&
3145 (!strcmp(ChrPtr(LineBuf), "..")))
3147 StrBufCutLeft(LineBuf, 1);
3150 StrBufAppendBuf(Message, LineBuf, 0);
3153 /* if we've hit the max msg length, flush the rest */
3154 if (StrLength(Message) >= maxlen) flushing = 1;
3156 } while (!finished);
3157 FreeStrBuf(&LineBuf);
3163 * Back end function used by CtdlMakeMessage() and similar functions
3165 char *CtdlReadMessageBody(char *terminator, /* token signalling EOT */
3167 size_t maxlen, /* maximum message length */
3168 StrBuf *exist, /* if non-null, append to it;
3169 exist is ALWAYS freed */
3170 int crlf /* CRLF newlines instead of LF */
3175 Message = CtdlReadMessageBodyBuf(terminator,
3181 if (Message == NULL)
3184 return SmashStrBuf(&Message);
3187 struct CtdlMessage *CtdlMakeMessage(
3188 struct ctdluser *author, /* author's user structure */
3189 char *recipient, /* NULL if it's not mail */
3190 char *recp_cc, /* NULL if it's not mail */
3191 char *room, /* room where it's going */
3192 int type, /* see MES_ types in header file */
3193 int format_type, /* variformat, plain text, MIME... */
3194 char *fake_name, /* who we're masquerading as */
3195 char *my_email, /* which of my email addresses to use (empty is ok) */
3196 char *subject, /* Subject (optional) */
3197 char *supplied_euid, /* ...or NULL if this is irrelevant */
3198 char *preformatted_text, /* ...or NULL to read text from client */
3199 char *references /* Thread references */
3202 return CtdlMakeMessageLen(
3203 author, /* author's user structure */
3204 recipient, /* NULL if it's not mail */
3205 (recipient)?strlen(recipient) : 0,
3206 recp_cc, /* NULL if it's not mail */
3207 (recp_cc)?strlen(recp_cc): 0,
3208 room, /* room where it's going */
3209 (room)?strlen(room): 0,
3210 type, /* see MES_ types in header file */
3211 format_type, /* variformat, plain text, MIME... */
3212 fake_name, /* who we're masquerading as */
3213 (fake_name)?strlen(fake_name): 0,
3214 my_email, /* which of my email addresses to use (empty is ok) */
3215 (my_email)?strlen(my_email): 0,
3216 subject, /* Subject (optional) */
3217 (subject)?strlen(subject): 0,
3218 supplied_euid, /* ...or NULL if this is irrelevant */
3219 (supplied_euid)?strlen(supplied_euid):0,
3220 preformatted_text, /* ...or NULL to read text from client */
3221 (preformatted_text)?strlen(preformatted_text) : 0,
3222 references, /* Thread references */
3223 (references)?strlen(references):0);
3228 * Build a binary message to be saved on disk.
3229 * (NOTE: if you supply 'preformatted_text', the buffer you give it
3230 * will become part of the message. This means you are no longer
3231 * responsible for managing that memory -- it will be freed along with
3232 * the rest of the fields when CM_Free() is called.)
3235 struct CtdlMessage *CtdlMakeMessageLen(
3236 struct ctdluser *author, /* author's user structure */
3237 char *recipient, /* NULL if it's not mail */
3239 char *recp_cc, /* NULL if it's not mail */
3241 char *room, /* room where it's going */
3243 int type, /* see MES_ types in header file */
3244 int format_type, /* variformat, plain text, MIME... */
3245 char *fake_name, /* who we're masquerading as */
3247 char *my_email, /* which of my email addresses to use (empty is ok) */
3249 char *subject, /* Subject (optional) */
3251 char *supplied_euid, /* ...or NULL if this is irrelevant */
3253 char *preformatted_text, /* ...or NULL to read text from client */
3255 char *references, /* Thread references */
3259 /* Don't confuse the poor folks if it's not routed mail. * /
3260 char dest_node[256] = "";*/
3263 struct CtdlMessage *msg;
3265 StrBuf *FakeEncAuthor = NULL;
3267 msg = malloc(sizeof(struct CtdlMessage));
3268 memset(msg, 0, sizeof(struct CtdlMessage));
3269 msg->cm_magic = CTDLMESSAGE_MAGIC;
3270 msg->cm_anon_type = type;
3271 msg->cm_format_type = format_type;
3273 if (recipient != NULL) rcplen = striplt(recipient);
3274 if (recp_cc != NULL) cclen = striplt(recp_cc);
3276 /* Path or Return-Path */
3278 CM_SetField(msg, eMessagePath, my_email, myelen);
3280 else if (!IsEmptyStr(author->fullname)) {
3281 CM_SetField(msg, eMessagePath, author->fullname, strlen(author->fullname));
3283 convert_spaces_to_underscores(msg->cm_fields[eMessagePath]);
3285 blen = snprintf(buf, sizeof buf, "%ld", (long)time(NULL));
3286 CM_SetField(msg, eTimestamp, buf, blen);
3289 FakeAuthor = NewStrBufPlain (fake_name, fnlen);
3292 FakeAuthor = NewStrBufPlain (author->fullname, -1);
3294 StrBufRFC2047encode(&FakeEncAuthor, FakeAuthor);
3295 CM_SetAsFieldSB(msg, eAuthor, &FakeEncAuthor);
3296 FreeStrBuf(&FakeAuthor);
3298 if (!!IsEmptyStr(CC->room.QRname)) {
3299 if (CC->room.QRflags & QR_MAILBOX) { /* room */
3300 CM_SetField(msg, eOriginalRoom, &CC->room.QRname[11], strlen(&CC->room.QRname[11]));
3303 CM_SetField(msg, eOriginalRoom, CC->room.QRname, strlen(CC->room.QRname));
3307 CM_SetField(msg, eNodeName, CtdlGetConfigStr("c_nodename"), strlen(CtdlGetConfigStr("c_nodename")));
3308 CM_SetField(msg, eHumanNode, CtdlGetConfigStr("c_humannode"), strlen(CtdlGetConfigStr("c_humannode")));
3311 CM_SetField(msg, eRecipient, recipient, rcplen);
3314 CM_SetField(msg, eCarbonCopY, recp_cc, cclen);
3318 CM_SetField(msg, erFc822Addr, my_email, myelen);
3320 else if ( (author == &CC->user) && (!IsEmptyStr(CC->cs_inet_email)) ) {
3321 CM_SetField(msg, erFc822Addr, CC->cs_inet_email, strlen(CC->cs_inet_email));
3324 if (subject != NULL) {
3326 length = striplt(subject);
3332 while ((subject[i] != '\0') &&
3333 (IsAscii = isascii(subject[i]) != 0 ))
3336 CM_SetField(msg, eMsgSubject, subject, subjlen);
3337 else /* ok, we've got utf8 in the string. */
3340 rfc2047Subj = rfc2047encode(subject, length);
3341 CM_SetAsField(msg, eMsgSubject, &rfc2047Subj, strlen(rfc2047Subj));
3348 CM_SetField(msg, eExclusiveID, supplied_euid, euidlen);
3352 CM_SetField(msg, eWeferences, references, reflen);
3355 if (preformatted_text != NULL) {
3356 CM_SetField(msg, eMesageText, preformatted_text, textlen);
3360 MsgBody = CtdlReadMessageBodyBuf(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
3361 if (MsgBody != NULL) {
3362 CM_SetAsFieldSB(msg, eMesageText, &MsgBody);
3373 * API function to delete messages which match a set of criteria
3374 * (returns the actual number of messages deleted)
3376 int CtdlDeleteMessages(const char *room_name, /* which room */
3377 long *dmsgnums, /* array of msg numbers to be deleted */
3378 int num_dmsgnums, /* number of msgs to be deleted, or 0 for "any" */
3379 char *content_type /* or "" for any. regular expressions expected. */
3382 struct ctdlroom qrbuf;
3383 struct cdbdata *cdbfr;
3384 long *msglist = NULL;
3385 long *dellist = NULL;
3388 int num_deleted = 0;
3390 struct MetaData smi;
3393 int need_to_free_re = 0;
3395 if (content_type) if (!IsEmptyStr(content_type)) {
3396 regcomp(&re, content_type, 0);
3397 need_to_free_re = 1;
3399 syslog(LOG_DEBUG, "msgbase: CtdlDeleteMessages(%s, %d msgs, %s)", room_name, num_dmsgnums, content_type);
3401 /* get room record, obtaining a lock... */
3402 if (CtdlGetRoomLock(&qrbuf, room_name) != 0) {
3403 syslog(LOG_ERR, "msgbase: CtdlDeleteMessages(): Room <%s> not found", room_name);
3404 if (need_to_free_re) regfree(&re);
3405 return(0); /* room not found */
3407 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
3409 if (cdbfr != NULL) {
3410 dellist = malloc(cdbfr->len);
3411 msglist = (long *) cdbfr->ptr;
3412 cdbfr->ptr = NULL; /* CtdlDeleteMessages() now owns this memory */
3413 num_msgs = cdbfr->len / sizeof(long);
3417 int have_contenttype = (content_type != NULL) && !IsEmptyStr(content_type);
3418 int have_delmsgs = (num_dmsgnums == 0) || (dmsgnums == NULL);
3419 int have_more_del = 1;
3421 num_msgs = sort_msglist(msglist, num_msgs);
3422 if (num_dmsgnums > 1)
3423 num_dmsgnums = sort_msglist(dmsgnums, num_dmsgnums);
3426 StrBuf *dbg = NewStrBuf();
3427 for (i = 0; i < num_dmsgnums; i++)
3428 StrBufAppendPrintf(dbg, ", %ld", dmsgnums[i]);
3429 syslog(LOG_DEBUG, "msgbase: Deleting before: %s", ChrPtr(dbg));
3434 while ((i < num_msgs) && (have_more_del)) {
3437 /* Set/clear a bit for each criterion */
3439 /* 0 messages in the list or a null list means that we are
3440 * interested in deleting any messages which meet the other criteria.
3443 delete_this |= 0x01;
3446 while ((i < num_msgs) && (msglist[i] < dmsgnums[j])) i++;
3451 if (msglist[i] == dmsgnums[j]) {
3452 delete_this |= 0x01;
3455 have_more_del = (j < num_dmsgnums);
3458 if (have_contenttype) {
3459 GetMetaData(&smi, msglist[i]);
3460 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) == 0) {
3461 delete_this |= 0x02;
3464 delete_this |= 0x02;
3467 /* Delete message only if all bits are set */
3468 if (delete_this == 0x03) {
3469 dellist[num_deleted++] = msglist[i];
3476 StrBuf *dbg = NewStrBuf();
3477 for (i = 0; i < num_deleted; i++)
3478 StrBufAppendPrintf(dbg, ", %ld", dellist[i]);
3479 syslog(LOG_DEBUG, "msgbase: Deleting: %s", ChrPtr(dbg));
3483 num_msgs = sort_msglist(msglist, num_msgs);
3484 cdb_store(CDB_MSGLISTS, &qrbuf.QRnumber, (int)sizeof(long),
3485 msglist, (int)(num_msgs * sizeof(long)));
3488 qrbuf.QRhighest = msglist[num_msgs - 1];
3490 qrbuf.QRhighest = 0;
3492 CtdlPutRoomLock(&qrbuf);
3494 /* Go through the messages we pulled out of the index, and decrement
3495 * their reference counts by 1. If this is the only room the message
3496 * was in, the reference count will reach zero and the message will
3497 * automatically be deleted from the database. We do this in a
3498 * separate pass because there might be plug-in hooks getting called,
3499 * and we don't want that happening during an S_ROOMS critical
3503 for (i=0; i<num_deleted; ++i) {
3504 PerformDeleteHooks(qrbuf.QRname, dellist[i]);
3506 AdjRefCountList(dellist, num_deleted, -1);
3508 /* Now free the memory we used, and go away. */
3509 if (msglist != NULL) free(msglist);
3510 if (dellist != NULL) free(dellist);
3511 syslog(LOG_DEBUG, "msgbase: %d message(s) deleted", num_deleted);
3512 if (need_to_free_re) regfree(&re);
3513 return (num_deleted);
3518 * GetMetaData() - Get the supplementary record for a message
3520 void GetMetaData(struct MetaData *smibuf, long msgnum)
3523 struct cdbdata *cdbsmi;
3526 memset(smibuf, 0, sizeof(struct MetaData));
3527 smibuf->meta_msgnum = msgnum;
3528 smibuf->meta_refcount = 1; /* Default reference count is 1 */
3530 /* Use the negative of the message number for its supp record index */
3531 TheIndex = (0L - msgnum);
3533 cdbsmi = cdb_fetch(CDB_MSGMAIN, &TheIndex, sizeof(long));
3534 if (cdbsmi == NULL) {
3535 return; /* record not found; go with defaults */
3537 memcpy(smibuf, cdbsmi->ptr, // FIXME can we do this without a memcpy?
3538 ((cdbsmi->len > sizeof(struct MetaData)) ?
3539 sizeof(struct MetaData) : cdbsmi->len));
3546 * PutMetaData() - (re)write supplementary record for a message
3548 void PutMetaData(struct MetaData *smibuf)
3552 /* Use the negative of the message number for the metadata db index */
3553 TheIndex = (0L - smibuf->meta_msgnum);
3555 cdb_store(CDB_MSGMAIN,
3556 &TheIndex, (int)sizeof(long),
3557 smibuf, (int)sizeof(struct MetaData));
3562 * AdjRefCount - submit an adjustment to the reference count for a message.
3563 * (These are just queued -- we actually process them later.)
3565 void AdjRefCount(long msgnum, int incr)
3567 struct arcq new_arcq;
3570 syslog(LOG_DEBUG, "msgbase: AdjRefCount() msg %ld ref count delta %+d", msgnum, incr);
3572 begin_critical_section(S_SUPPMSGMAIN);
3573 if (arcfp == NULL) {
3574 arcfp = fopen(file_arcq, "ab+");
3575 chown(file_arcq, CTDLUID, (-1));
3576 chmod(file_arcq, 0600);
3578 end_critical_section(S_SUPPMSGMAIN);
3580 /* msgnum < 0 means that we're trying to close the file */
3582 syslog(LOG_DEBUG, "msgbase: closing the AdjRefCount queue file");
3583 begin_critical_section(S_SUPPMSGMAIN);
3584 if (arcfp != NULL) {
3588 end_critical_section(S_SUPPMSGMAIN);
3593 * If we can't open the queue, perform the operation synchronously.
3595 if (arcfp == NULL) {
3596 TDAP_AdjRefCount(msgnum, incr);
3600 new_arcq.arcq_msgnum = msgnum;
3601 new_arcq.arcq_delta = incr;
3602 rv = fwrite(&new_arcq, sizeof(struct arcq), 1, arcfp);
3604 syslog(LOG_EMERG, "%s: %m", file_arcq);
3611 void AdjRefCountList(long *msgnum, long nmsg, int incr)
3613 long i, the_size, offset;
3614 struct arcq *new_arcq;
3617 begin_critical_section(S_SUPPMSGMAIN);
3618 if (arcfp == NULL) {
3619 arcfp = fopen(file_arcq, "ab+");
3620 chown(file_arcq, CTDLUID, (-1));
3621 chmod(file_arcq, 0600);
3623 end_critical_section(S_SUPPMSGMAIN);
3626 * If we can't open the queue, perform the operation synchronously.
3628 if (arcfp == NULL) {
3629 for (i = 0; i < nmsg; i++)
3630 TDAP_AdjRefCount(msgnum[i], incr);
3634 the_size = sizeof(struct arcq) * nmsg;
3635 new_arcq = malloc(the_size);
3636 for (i = 0; i < nmsg; i++) {
3637 syslog(LOG_DEBUG, "msgbase: AdjRefCountList() msg %ld ref count delta %+d", msgnum[i], incr);
3638 new_arcq[i].arcq_msgnum = msgnum[i];
3639 new_arcq[i].arcq_delta = incr;
3643 while ((rv >= 0) && (offset < the_size))
3645 rv = fwrite(new_arcq + offset, 1, the_size - offset, arcfp);
3647 syslog(LOG_ERR, "%s: %m", file_arcq);
3661 * TDAP_ProcessAdjRefCountQueue()
3663 * Process the queue of message count adjustments that was created by calls
3664 * to AdjRefCount() ... by reading the queue and calling TDAP_AdjRefCount()
3665 * for each one. This should be an "off hours" operation.
3667 int TDAP_ProcessAdjRefCountQueue(void)
3669 char file_arcq_temp[PATH_MAX];
3672 struct arcq arcq_rec;
3673 int num_records_processed = 0;
3675 snprintf(file_arcq_temp, sizeof file_arcq_temp, "%s.%04x", file_arcq, rand());
3677 begin_critical_section(S_SUPPMSGMAIN);
3678 if (arcfp != NULL) {
3683 r = link(file_arcq, file_arcq_temp);
3685 syslog(LOG_ERR, "%s: %m", file_arcq_temp);
3686 end_critical_section(S_SUPPMSGMAIN);
3687 return(num_records_processed);
3691 end_critical_section(S_SUPPMSGMAIN);
3693 fp = fopen(file_arcq_temp, "rb");
3695 syslog(LOG_ERR, "%s: %m", file_arcq_temp);
3696 return(num_records_processed);
3699 while (fread(&arcq_rec, sizeof(struct arcq), 1, fp) == 1) {
3700 TDAP_AdjRefCount(arcq_rec.arcq_msgnum, arcq_rec.arcq_delta);
3701 ++num_records_processed;
3705 r = unlink(file_arcq_temp);
3707 syslog(LOG_ERR, "%s: %m", file_arcq_temp);
3710 return(num_records_processed);
3716 * TDAP_AdjRefCount - adjust the reference count for a message.
3717 * This one does it "for real" because it's called by
3718 * the autopurger function that processes the queue
3719 * created by AdjRefCount(). If a message's reference
3720 * count becomes zero, we also delete the message from
3721 * disk and de-index it.
3723 void TDAP_AdjRefCount(long msgnum, int incr)
3726 struct MetaData smi;
3729 /* This is a *tight* critical section; please keep it that way, as
3730 * it may get called while nested in other critical sections.
3731 * Complicating this any further will surely cause deadlock!
3733 begin_critical_section(S_SUPPMSGMAIN);
3734 GetMetaData(&smi, msgnum);
3735 smi.meta_refcount += incr;
3737 end_critical_section(S_SUPPMSGMAIN);
3738 syslog(LOG_DEBUG, "msgbase: TDAP_AdjRefCount() msg %ld ref count delta %+d, is now %d",
3739 msgnum, incr, smi.meta_refcount
3742 /* If the reference count is now zero, delete the message
3743 * (and its supplementary record as well).
3745 if (smi.meta_refcount == 0) {
3746 syslog(LOG_DEBUG, "msgbase: deleting message <%ld>", msgnum);
3748 /* Call delete hooks with NULL room to show it has gone altogether */
3749 PerformDeleteHooks(NULL, msgnum);
3751 /* Remove from message base */
3753 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3754 cdb_delete(CDB_BIGMSGS, &delnum, (int)sizeof(long));
3756 /* Remove metadata record */
3757 delnum = (0L - msgnum);
3758 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3764 * Write a generic object to this room
3766 * Note: this could be much more efficient. Right now we use two temporary
3767 * files, and still pull the message into memory as with all others.
3769 void CtdlWriteObject(char *req_room, /* Room to stuff it in */
3770 char *content_type, /* MIME type of this object */
3771 char *raw_message, /* Data to be written */
3772 off_t raw_length, /* Size of raw_message */
3773 struct ctdluser *is_mailbox, /* Mailbox room? */
3774 int is_binary, /* Is encoding necessary? */
3775 int is_unique, /* Del others of this type? */
3776 unsigned int flags /* Internal save flags */
3779 struct ctdlroom qrbuf;
3780 char roomname[ROOMNAMELEN];
3781 struct CtdlMessage *msg;
3782 StrBuf *encoded_message = NULL;
3784 if (is_mailbox != NULL) {
3785 CtdlMailboxName(roomname, sizeof roomname, is_mailbox, req_room);
3788 safestrncpy(roomname, req_room, sizeof(roomname));
3791 syslog(LOG_DEBUG, "msfbase: raw length is %ld", (long)raw_length);
3794 encoded_message = NewStrBufPlain(NULL, (size_t) (((raw_length * 134) / 100) + 4096 ) );
3797 encoded_message = NewStrBufPlain(NULL, (size_t)(raw_length + 4096));
3800 StrBufAppendBufPlain(encoded_message, HKEY("Content-type: "), 0);
3801 StrBufAppendBufPlain(encoded_message, content_type, -1, 0);
3802 StrBufAppendBufPlain(encoded_message, HKEY("\n"), 0);
3805 StrBufAppendBufPlain(encoded_message, HKEY("Content-transfer-encoding: base64\n\n"), 0);
3808 StrBufAppendBufPlain(encoded_message, HKEY("Content-transfer-encoding: 7bit\n\n"), 0);
3812 StrBufBase64Append(encoded_message, NULL, raw_message, raw_length, 0);
3815 StrBufAppendBufPlain(encoded_message, raw_message, raw_length, 0);
3818 syslog(LOG_DEBUG, "msgbase: allocating");
3819 msg = malloc(sizeof(struct CtdlMessage));
3820 memset(msg, 0, sizeof(struct CtdlMessage));
3821 msg->cm_magic = CTDLMESSAGE_MAGIC;
3822 msg->cm_anon_type = MES_NORMAL;
3823 msg->cm_format_type = 4;
3824 CM_SetField(msg, eAuthor, CC->user.fullname, strlen(CC->user.fullname));
3825 CM_SetField(msg, eOriginalRoom, req_room, strlen(req_room));
3826 CM_SetField(msg, eNodeName, CtdlGetConfigStr("c_nodename"), strlen(CtdlGetConfigStr("c_nodename")));
3827 CM_SetField(msg, eHumanNode, CtdlGetConfigStr("c_humannode"), strlen(CtdlGetConfigStr("c_humannode")));
3828 msg->cm_flags = flags;
3830 CM_SetAsFieldSB(msg, eMesageText, &encoded_message);
3832 /* Create the requested room if we have to. */
3833 if (CtdlGetRoom(&qrbuf, roomname) != 0) {
3834 CtdlCreateRoom(roomname,
3835 ( (is_mailbox != NULL) ? 5 : 3 ),
3836 "", 0, 1, 0, VIEW_BBS);
3838 /* If the caller specified this object as unique, delete all
3839 * other objects of this type that are currently in the room.
3842 syslog(LOG_DEBUG, "msgbase: deleted %d other msgs of this type",
3843 CtdlDeleteMessages(roomname, NULL, 0, content_type)
3846 /* Now write the data */
3847 CtdlSubmitMsg(msg, NULL, roomname, 0);
3853 /*****************************************************************************/
3854 /* MODULE INITIALIZATION STUFF */
3855 /*****************************************************************************/
3857 CTDL_MODULE_INIT(msgbase)
3860 FillMsgKeyLookupTable();
3863 /* return our Subversion id for the Log */