2 * Implements the message store.
4 * Copyright (c) 1987-2015 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.
18 #include <libcitadel.h>
22 #include "ctdl_module.h"
23 #include "citserver.h"
26 #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);
42 int MessageDebugEnabled = 0;
45 * These are the four-character field headers we use when outputting
46 * messages in Citadel format (as opposed to RFC822 format).
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,
55 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
56 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
58 "from", /* A -> eAuthor */
59 NULL, /* B -> eBig_message */
60 NULL, /* C -> eRemoteRoom */
61 NULL, /* D -> eDestination */
62 "exti", /* E -> eXclusivID */
63 "rfca", /* F -> erFc822Addr */
65 "hnod", /* H -> eHumanNode */
66 "msgn", /* I -> emessageId */
67 "jrnl", /* J -> eJournal */
68 "rep2", /* K -> eReplyTo */
69 "list", /* L -> eListID */
70 "text", /* M -> eMesageText */
71 "node", /* N -> eNodeName */
72 "room", /* O -> eOriginalRoom */
73 "path", /* P -> eMessagePath */
75 "rcpt", /* R -> eRecipient */
76 "spec", /* S -> eSpecialField */
77 "time", /* T -> eTimestamp */
78 "subj", /* U -> eMsgSubject */
79 "nvto", /* V -> eenVelopeTo */
80 "wefw", /* W -> eWeferences */
82 "cccc", /* Y -> eCarbonCopY */
86 eMsgField FieldOrder[] = {
87 /* Important fields */
98 /* Semi-important fields */
104 /* G is not used yet, may become virus signature*/
107 /* Q is not used yet */
110 /* X is not used yet */
111 /* Z is not used yet */
118 /* Message text (MUST be last) */
120 /* Not saved to disk:
125 static const long NDiskFields = sizeof(FieldOrder) / sizeof(eMsgField);
127 int CM_IsEmpty(struct CtdlMessage *Msg, eMsgField which)
129 return !((Msg->cm_fields[which] != NULL) &&
130 (Msg->cm_fields[which][0] != '\0'));
133 void CM_SetField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
135 if (Msg->cm_fields[which] != NULL)
136 free (Msg->cm_fields[which]);
137 Msg->cm_fields[which] = malloc(length + 1);
138 memcpy(Msg->cm_fields[which], buf, length);
139 Msg->cm_fields[which][length] = '\0';
140 Msg->cm_lengths[which] = length;
143 void CM_SetFieldLONG(struct CtdlMessage *Msg, eMsgField which, long lvalue)
147 len = snprintf(buf, sizeof(buf), "%ld", lvalue);
148 CM_SetField(Msg, which, buf, len);
150 void CM_CutFieldAt(struct CtdlMessage *Msg, eMsgField WhichToCut, long maxlen)
152 if (Msg->cm_fields[WhichToCut] == NULL)
155 if (Msg->cm_lengths[WhichToCut] > maxlen)
157 Msg->cm_fields[WhichToCut][maxlen] = '\0';
158 Msg->cm_lengths[WhichToCut] = maxlen;
162 void CM_FlushField(struct CtdlMessage *Msg, eMsgField which)
164 if (Msg->cm_fields[which] != NULL)
165 free (Msg->cm_fields[which]);
166 Msg->cm_fields[which] = NULL;
167 Msg->cm_lengths[which] = 0;
169 void CM_Flush(struct CtdlMessage *Msg)
173 if (CM_IsValidMsg(Msg) == 0)
176 for (i = 0; i < 256; ++i)
178 CM_FlushField(Msg, i);
182 void CM_CopyField(struct CtdlMessage *Msg, eMsgField WhichToPutTo, eMsgField WhichtToCopy)
185 if (Msg->cm_fields[WhichToPutTo] != NULL)
186 free (Msg->cm_fields[WhichToPutTo]);
188 if (Msg->cm_fields[WhichtToCopy] != NULL)
190 len = Msg->cm_lengths[WhichtToCopy];
191 Msg->cm_fields[WhichToPutTo] = malloc(len + 1);
192 memcpy(Msg->cm_fields[WhichToPutTo], Msg->cm_fields[WhichtToCopy], len);
193 Msg->cm_fields[WhichToPutTo][len] = '\0';
194 Msg->cm_lengths[WhichToPutTo] = len;
198 Msg->cm_fields[WhichToPutTo] = NULL;
199 Msg->cm_lengths[WhichToPutTo] = 0;
204 void CM_PrependToField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
206 if (Msg->cm_fields[which] != NULL) {
211 oldmsgsize = Msg->cm_lengths[which] + 1;
212 newmsgsize = length + oldmsgsize;
214 new = malloc(newmsgsize);
215 memcpy(new, buf, length);
216 memcpy(new + length, Msg->cm_fields[which], oldmsgsize);
217 free(Msg->cm_fields[which]);
218 Msg->cm_fields[which] = new;
219 Msg->cm_lengths[which] = newmsgsize - 1;
222 Msg->cm_fields[which] = malloc(length + 1);
223 memcpy(Msg->cm_fields[which], buf, length);
224 Msg->cm_fields[which][length] = '\0';
225 Msg->cm_lengths[which] = length;
229 void CM_SetAsField(struct CtdlMessage *Msg, eMsgField which, char **buf, long length)
231 if (Msg->cm_fields[which] != NULL)
232 free (Msg->cm_fields[which]);
234 Msg->cm_fields[which] = *buf;
236 Msg->cm_lengths[which] = length;
239 void CM_SetAsFieldSB(struct CtdlMessage *Msg, eMsgField which, StrBuf **buf)
241 if (Msg->cm_fields[which] != NULL)
242 free (Msg->cm_fields[which]);
244 Msg->cm_lengths[which] = StrLength(*buf);
245 Msg->cm_fields[which] = SmashStrBuf(buf);
248 void CM_GetAsField(struct CtdlMessage *Msg, eMsgField which, char **ret, long *retlen)
250 if (Msg->cm_fields[which] != NULL)
252 *retlen = Msg->cm_lengths[which];
253 *ret = Msg->cm_fields[which];
254 Msg->cm_fields[which] = NULL;
255 Msg->cm_lengths[which] = 0;
265 * Returns 1 if the supplied pointer points to a valid Citadel message.
266 * If the pointer is NULL or the magic number check fails, returns 0.
268 int CM_IsValidMsg(struct CtdlMessage *msg) {
271 if ((msg->cm_magic) != CTDLMESSAGE_MAGIC) {
272 struct CitContext *CCC = CC;
273 MSGM_syslog(LOG_WARNING, "CM_IsValidMsg() -- self-check failed\n");
279 void CM_FreeContents(struct CtdlMessage *msg)
283 for (i = 0; i < 256; ++i)
284 if (msg->cm_fields[i] != NULL) {
285 free(msg->cm_fields[i]);
286 msg->cm_lengths[i] = 0;
289 msg->cm_magic = 0; /* just in case */
292 * 'Destructor' for struct CtdlMessage
294 void CM_Free(struct CtdlMessage *msg)
296 if (CM_IsValidMsg(msg) == 0)
298 if (msg != NULL) free (msg);
301 CM_FreeContents(msg);
305 int CM_DupField(eMsgField i, struct CtdlMessage *OrgMsg, struct CtdlMessage *NewMsg)
308 len = OrgMsg->cm_lengths[i];
309 NewMsg->cm_fields[i] = malloc(len + 1);
310 if (NewMsg->cm_fields[i] == NULL)
312 memcpy(NewMsg->cm_fields[i], OrgMsg->cm_fields[i], len);
313 NewMsg->cm_fields[i][len] = '\0';
314 NewMsg->cm_lengths[i] = len;
318 struct CtdlMessage * CM_Duplicate(struct CtdlMessage *OrgMsg)
321 struct CtdlMessage *NewMsg;
323 if (CM_IsValidMsg(OrgMsg) == 0)
325 NewMsg = (struct CtdlMessage *)malloc(sizeof(struct CtdlMessage));
329 memcpy(NewMsg, OrgMsg, sizeof(struct CtdlMessage));
331 memset(&NewMsg->cm_fields, 0, sizeof(char*) * 256);
333 for (i = 0; i < 256; ++i)
335 if (OrgMsg->cm_fields[i] != NULL)
337 if (!CM_DupField(i, OrgMsg, NewMsg))
352 /* Determine if a given message matches the fields in a message template.
353 * Return 0 for a successful match.
355 int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template) {
358 /* If there aren't any fields in the template, all messages will
361 if (template == NULL) return(0);
363 /* Null messages are bogus. */
364 if (msg == NULL) return(1);
366 for (i='A'; i<='Z'; ++i) {
367 if (template->cm_fields[i] != NULL) {
368 if (msg->cm_fields[i] == NULL) {
369 /* Considered equal if temmplate is empty string */
370 if (IsEmptyStr(template->cm_fields[i])) continue;
373 if ((template->cm_lengths[i] != msg->cm_lengths[i]) ||
374 (strcasecmp(msg->cm_fields[i], template->cm_fields[i])))
379 /* All compares succeeded: we have a match! */
386 * Retrieve the "seen" message list for the current room.
388 void CtdlGetSeen(char *buf, int which_set) {
389 struct CitContext *CCC = CC;
392 /* Learn about the user and room in question */
393 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
395 if (which_set == ctdlsetseen_seen)
396 safestrncpy(buf, vbuf.v_seen, SIZ);
397 if (which_set == ctdlsetseen_answered)
398 safestrncpy(buf, vbuf.v_answered, SIZ);
404 * Manipulate the "seen msgs" string (or other message set strings)
406 void CtdlSetSeen(long *target_msgnums, int num_target_msgnums,
407 int target_setting, int which_set,
408 struct ctdluser *which_user, struct ctdlroom *which_room) {
409 struct CitContext *CCC = CC;
410 struct cdbdata *cdbfr;
415 long hi = (-1L); /// TODO: we just write here. y?
424 char *is_set; /* actually an array of booleans */
426 /* Don't bother doing *anything* if we were passed a list of zero messages */
427 if (num_target_msgnums < 1) {
431 /* If no room was specified, we go with the current room. */
433 which_room = &CCC->room;
436 /* If no user was specified, we go with the current user. */
438 which_user = &CCC->user;
441 MSG_syslog(LOG_DEBUG, "CtdlSetSeen(%d msgs starting with %ld, %s, %d) in <%s>\n",
442 num_target_msgnums, target_msgnums[0],
443 (target_setting ? "SET" : "CLEAR"),
447 /* Learn about the user and room in question */
448 CtdlGetRelationship(&vbuf, which_user, which_room);
450 /* Load the message list */
451 cdbfr = cdb_fetch(CDB_MSGLISTS, &which_room->QRnumber, sizeof(long));
453 msglist = (long *) cdbfr->ptr;
454 cdbfr->ptr = NULL; /* CtdlSetSeen() now owns this memory */
455 num_msgs = cdbfr->len / sizeof(long);
458 return; /* No messages at all? No further action. */
461 is_set = malloc(num_msgs * sizeof(char));
462 memset(is_set, 0, (num_msgs * sizeof(char)) );
464 /* Decide which message set we're manipulating */
466 case ctdlsetseen_seen:
467 vset = NewStrBufPlain(vbuf.v_seen, -1);
469 case ctdlsetseen_answered:
470 vset = NewStrBufPlain(vbuf.v_answered, -1);
477 #if 0 /* This is a special diagnostic section. Do not allow it to run during normal operation. */
478 MSG_syslog(LOG_DEBUG, "There are %d messages in the room.\n", num_msgs);
479 for (i=0; i<num_msgs; ++i) {
480 if ((i > 0) && (msglist[i] <= msglist[i-1])) abort();
482 MSG_syslog(LOG_DEBUG, "We are twiddling %d of them.\n", num_target_msgnums);
483 for (k=0; k<num_target_msgnums; ++k) {
484 if ((k > 0) && (target_msgnums[k] <= target_msgnums[k-1])) abort();
488 MSG_syslog(LOG_DEBUG, "before update: %s\n", ChrPtr(vset));
490 /* Translate the existing sequence set into an array of booleans */
491 setstr = NewStrBuf();
495 while (StrBufExtract_NextToken(setstr, vset, &pvset, ',') >= 0) {
497 StrBufExtract_token(lostr, setstr, 0, ':');
498 if (StrBufNum_tokens(setstr, ':') >= 2) {
499 StrBufExtract_token(histr, setstr, 1, ':');
503 StrBufAppendBuf(histr, lostr, 0);
506 if (!strcmp(ChrPtr(histr), "*")) {
513 for (i = 0; i < num_msgs; ++i) {
514 if ((msglist[i] >= lo) && (msglist[i] <= hi)) {
524 /* Now translate the array of booleans back into a sequence set */
530 for (i=0; i<num_msgs; ++i) {
534 for (k=0; k<num_target_msgnums; ++k) {
535 if (msglist[i] == target_msgnums[k]) {
536 is_seen = target_setting;
540 if ((was_seen == 0) && (is_seen == 1)) {
543 else if ((was_seen == 1) && (is_seen == 0)) {
546 if (StrLength(vset) > 0) {
547 StrBufAppendBufPlain(vset, HKEY(","), 0);
550 StrBufAppendPrintf(vset, "%ld", hi);
553 StrBufAppendPrintf(vset, "%ld:%ld", lo, hi);
557 if ((is_seen) && (i == num_msgs - 1)) {
558 if (StrLength(vset) > 0) {
559 StrBufAppendBufPlain(vset, HKEY(","), 0);
561 if ((i==0) || (was_seen == 0)) {
562 StrBufAppendPrintf(vset, "%ld", msglist[i]);
565 StrBufAppendPrintf(vset, "%ld:%ld", lo, msglist[i]);
573 * We will have to stuff this string back into a 4096 byte buffer, so if it's
574 * larger than that now, truncate it by removing tokens from the beginning.
575 * The limit of 100 iterations is there to prevent an infinite loop in case
576 * something unexpected happens.
578 int number_of_truncations = 0;
579 while ( (StrLength(vset) > SIZ) && (number_of_truncations < 100) ) {
580 StrBufRemove_token(vset, 0, ',');
581 ++number_of_truncations;
585 * If we're truncating the sequence set of messages marked with the 'seen' flag,
586 * we want the earliest messages (the truncated ones) to be marked, not unmarked.
587 * Otherwise messages at the beginning will suddenly appear to be 'unseen'.
589 if ( (which_set == ctdlsetseen_seen) && (number_of_truncations > 0) ) {
591 first_tok = NewStrBuf();
592 StrBufExtract_token(first_tok, vset, 0, ',');
593 StrBufRemove_token(vset, 0, ',');
595 if (StrBufNum_tokens(first_tok, ':') > 1) {
596 StrBufRemove_token(first_tok, 0, ':');
600 new_set = NewStrBuf();
601 StrBufAppendBufPlain(new_set, HKEY("1:"), 0);
602 StrBufAppendBuf(new_set, first_tok, 0);
603 StrBufAppendBufPlain(new_set, HKEY(":"), 0);
604 StrBufAppendBuf(new_set, vset, 0);
607 FreeStrBuf(&first_tok);
611 MSG_syslog(LOG_DEBUG, " after update: %s\n", ChrPtr(vset));
613 /* Decide which message set we're manipulating */
615 case ctdlsetseen_seen:
616 safestrncpy(vbuf.v_seen, ChrPtr(vset), sizeof vbuf.v_seen);
618 case ctdlsetseen_answered:
619 safestrncpy(vbuf.v_answered, ChrPtr(vset), sizeof vbuf.v_answered);
625 CtdlSetRelationship(&vbuf, which_user, which_room);
631 * API function to perform an operation for each qualifying message in the
632 * current room. (Returns the number of messages processed.)
634 int CtdlForEachMessage(int mode, long ref, char *search_string,
636 struct CtdlMessage *compare,
637 ForEachMsgCallback CallBack,
640 struct CitContext *CCC = CC;
643 struct cdbdata *cdbfr;
644 long *msglist = NULL;
646 int num_processed = 0;
649 struct CtdlMessage *msg = NULL;
652 int printed_lastold = 0;
653 int num_search_msgs = 0;
654 long *search_msgs = NULL;
656 int need_to_free_re = 0;
659 if ((content_type) && (!IsEmptyStr(content_type))) {
660 regcomp(&re, content_type, 0);
664 /* Learn about the user and room in question */
665 if (server_shutting_down) {
666 if (need_to_free_re) regfree(&re);
669 CtdlGetUser(&CCC->user, CCC->curr_user);
671 if (server_shutting_down) {
672 if (need_to_free_re) regfree(&re);
675 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
677 if (server_shutting_down) {
678 if (need_to_free_re) regfree(&re);
682 /* Load the message list */
683 cdbfr = cdb_fetch(CDB_MSGLISTS, &CCC->room.QRnumber, sizeof(long));
685 if (need_to_free_re) regfree(&re);
686 return 0; /* No messages at all? No further action. */
689 msglist = (long *) cdbfr->ptr;
690 num_msgs = cdbfr->len / sizeof(long);
692 cdbfr->ptr = NULL; /* clear this so that cdb_free() doesn't free it */
693 cdb_free(cdbfr); /* we own this memory now */
696 * Now begin the traversal.
698 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
700 /* If the caller is looking for a specific MIME type, filter
701 * out all messages which are not of the type requested.
703 if ((content_type != NULL) && (!IsEmptyStr(content_type))) {
705 /* This call to GetMetaData() sits inside this loop
706 * so that we only do the extra database read per msg
707 * if we need to. Doing the extra read all the time
708 * really kills the server. If we ever need to use
709 * metadata for another search criterion, we need to
710 * move the read somewhere else -- but still be smart
711 * enough to only do the read if the caller has
712 * specified something that will need it.
714 if (server_shutting_down) {
715 if (need_to_free_re) regfree(&re);
719 GetMetaData(&smi, msglist[a]);
721 /* if (strcasecmp(smi.meta_content_type, content_type)) { old non-regex way */
722 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) != 0) {
728 num_msgs = sort_msglist(msglist, num_msgs);
730 /* If a template was supplied, filter out the messages which
731 * don't match. (This could induce some delays!)
734 if (compare != NULL) {
735 for (a = 0; a < num_msgs; ++a) {
736 if (server_shutting_down) {
737 if (need_to_free_re) regfree(&re);
741 msg = CtdlFetchMessage(msglist[a], 1, 1);
743 if (CtdlMsgCmp(msg, compare)) {
752 /* If a search string was specified, get a message list from
753 * the full text index and remove messages which aren't on both
757 * Since the lists are sorted and strictly ascending, and the
758 * output list is guaranteed to be shorter than or equal to the
759 * input list, we overwrite the bottom of the input list. This
760 * eliminates the need to memmove big chunks of the list over and
763 if ( (num_msgs > 0) && (mode == MSGS_SEARCH) && (search_string) ) {
765 /* Call search module via hook mechanism.
766 * NULL means use any search function available.
767 * otherwise replace with a char * to name of search routine
769 CtdlModuleDoSearch(&num_search_msgs, &search_msgs, search_string, "fulltext");
771 if (num_search_msgs > 0) {
775 orig_num_msgs = num_msgs;
777 for (i=0; i<orig_num_msgs; ++i) {
778 for (j=0; j<num_search_msgs; ++j) {
779 if (msglist[i] == search_msgs[j]) {
780 msglist[num_msgs++] = msglist[i];
786 num_msgs = 0; /* No messages qualify */
788 if (search_msgs != NULL) free(search_msgs);
790 /* Now that we've purged messages which don't contain the search
791 * string, treat a MSGS_SEARCH just like a MSGS_ALL from this
798 * Now iterate through the message list, according to the
799 * criteria supplied by the caller.
802 for (a = 0; a < num_msgs; ++a) {
803 if (server_shutting_down) {
804 if (need_to_free_re) regfree(&re);
806 return num_processed;
808 thismsg = msglist[a];
809 if (mode == MSGS_ALL) {
813 is_seen = is_msg_in_sequence_set(
814 vbuf.v_seen, thismsg);
815 if (is_seen) lastold = thismsg;
821 || ((mode == MSGS_OLD) && (is_seen))
822 || ((mode == MSGS_NEW) && (!is_seen))
823 || ((mode == MSGS_LAST) && (a >= (num_msgs - ref)))
824 || ((mode == MSGS_FIRST) && (a < ref))
825 || ((mode == MSGS_GT) && (thismsg > ref))
826 || ((mode == MSGS_LT) && (thismsg < ref))
827 || ((mode == MSGS_EQ) && (thismsg == ref))
830 if ((mode == MSGS_NEW) && (CCC->user.flags & US_LASTOLD) && (lastold > 0L) && (printed_lastold == 0) && (!is_seen)) {
832 CallBack(lastold, userdata);
836 if (CallBack) CallBack(thismsg, userdata);
840 if (need_to_free_re) regfree(&re);
843 * We cache the most recent msglist in order to do security checks later
845 if (CCC->client_socket > 0) {
846 if (CCC->cached_msglist != NULL) {
847 free(CCC->cached_msglist);
849 CCC->cached_msglist = msglist;
850 CCC->cached_num_msgs = num_msgs;
856 return num_processed;
862 * memfmout() - Citadel text formatter and paginator.
863 * Although the original purpose of this routine was to format
864 * text to the reader's screen width, all we're really using it
865 * for here is to format text out to 80 columns before sending it
866 * to the client. The client software may reformat it again.
869 char *mptr, /* where are we going to get our text from? */
870 const char *nl /* string to terminate lines with */
872 struct CitContext *CCC = CC;
874 unsigned char ch = 0;
881 while (ch=*(mptr++), ch != 0) {
884 if (client_write(outbuf, len) == -1)
886 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
890 if (client_write(nl, nllen) == -1)
892 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
897 else if (ch == '\r') {
898 /* Ignore carriage returns. Newlines are always LF or CRLF but never CR. */
900 else if (isspace(ch)) {
901 if (column > 72) { /* Beyond 72 columns, break on the next space */
902 if (client_write(outbuf, len) == -1)
904 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
908 if (client_write(nl, nllen) == -1)
910 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
923 if (column > 1000) { /* Beyond 1000 columns, break anywhere */
924 if (client_write(outbuf, len) == -1)
926 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
930 if (client_write(nl, nllen) == -1)
932 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
940 if (client_write(outbuf, len) == -1)
942 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
945 client_write(nl, nllen);
953 * Callback function for mime parser that simply lists the part
955 void list_this_part(char *name, char *filename, char *partnum, char *disp,
956 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
957 char *cbid, void *cbuserdata)
961 ma = (struct ma_info *)cbuserdata;
962 if (ma->is_ma == 0) {
963 cprintf("part=%s|%s|%s|%s|%s|%ld|%s|%s\n",
976 * Callback function for multipart prefix
978 void list_this_pref(char *name, char *filename, char *partnum, char *disp,
979 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
980 char *cbid, void *cbuserdata)
984 ma = (struct ma_info *)cbuserdata;
985 if (!strcasecmp(cbtype, "multipart/alternative")) {
989 if (ma->is_ma == 0) {
990 cprintf("pref=%s|%s\n", partnum, cbtype);
995 * Callback function for multipart sufffix
997 void list_this_suff(char *name, char *filename, char *partnum, char *disp,
998 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
999 char *cbid, void *cbuserdata)
1003 ma = (struct ma_info *)cbuserdata;
1004 if (ma->is_ma == 0) {
1005 cprintf("suff=%s|%s\n", partnum, cbtype);
1007 if (!strcasecmp(cbtype, "multipart/alternative")) {
1014 * Callback function for mime parser that opens a section for downloading
1015 * we use serv_files function here:
1017 extern void OpenCmdResult(char *filename, const char *mime_type);
1018 void mime_download(char *name, char *filename, char *partnum, char *disp,
1019 void *content, char *cbtype, char *cbcharset, size_t length,
1020 char *encoding, char *cbid, void *cbuserdata)
1023 CitContext *CCC = MyContext();
1025 /* Silently go away if there's already a download open. */
1026 if (CCC->download_fp != NULL)
1030 (!IsEmptyStr(partnum) && (!strcasecmp(CCC->download_desired_section, partnum)))
1031 || (!IsEmptyStr(cbid) && (!strcasecmp(CCC->download_desired_section, cbid)))
1033 CCC->download_fp = tmpfile();
1034 if (CCC->download_fp == NULL) {
1035 MSG_syslog(LOG_EMERG, "mime_download(): Couldn't write: %s\n",
1037 cprintf("%d cannot open temporary file: %s\n",
1038 ERROR + INTERNAL_ERROR, strerror(errno));
1042 rv = fwrite(content, length, 1, CCC->download_fp);
1044 MSG_syslog(LOG_EMERG, "mime_download(): Couldn't write: %s\n",
1046 cprintf("%d unable to write tempfile.\n",
1048 fclose(CCC->download_fp);
1049 CCC->download_fp = NULL;
1052 fflush(CCC->download_fp);
1053 rewind(CCC->download_fp);
1055 OpenCmdResult(filename, cbtype);
1062 * Callback function for mime parser that outputs a section all at once.
1063 * We can specify the desired section by part number *or* content-id.
1065 void mime_spew_section(char *name, char *filename, char *partnum, char *disp,
1066 void *content, char *cbtype, char *cbcharset, size_t length,
1067 char *encoding, char *cbid, void *cbuserdata)
1069 int *found_it = (int *)cbuserdata;
1072 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
1073 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
1076 cprintf("%d %d|-1|%s|%s|%s\n",
1083 client_write(content, length);
1087 struct CtdlMessage *CtdlDeserializeMessage(long msgnum, int with_body, const char *Buffer, long Length)
1089 struct CitContext *CCC = CC;
1090 struct CtdlMessage *ret = NULL;
1092 const char *upper_bound;
1094 cit_uint8_t field_header;
1098 upper_bound = Buffer + Length;
1100 /* Parse the three bytes that begin EVERY message on disk.
1101 * The first is always 0xFF, the on-disk magic number.
1102 * The second is the anonymous/public type byte.
1103 * The third is the format type byte (vari, fixed, or MIME).
1107 MSG_syslog(LOG_ERR, "Message %ld appears to be corrupted.\n", msgnum);
1110 ret = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1111 memset(ret, 0, sizeof(struct CtdlMessage));
1113 ret->cm_magic = CTDLMESSAGE_MAGIC;
1114 ret->cm_anon_type = *mptr++; /* Anon type byte */
1115 ret->cm_format_type = *mptr++; /* Format type byte */
1118 * The rest is zero or more arbitrary fields. Load them in.
1119 * We're done when we encounter either a zero-length field or
1120 * have just processed the 'M' (message text) field.
1123 field_header = '\0';
1126 /* work around possibly buggy messages: */
1127 while (field_header == '\0')
1129 if (mptr >= upper_bound) {
1132 field_header = *mptr++;
1134 if (mptr >= upper_bound) {
1137 which = field_header;
1140 CM_SetField(ret, which, mptr, len);
1142 mptr += len + 1; /* advance to next field */
1144 } while ((mptr < upper_bound) && (field_header != 'M'));
1151 * Load a message from disk into memory.
1152 * This is used by CtdlOutputMsg() and other fetch functions.
1154 * NOTE: Caller is responsible for freeing the returned CtdlMessage struct
1155 * using the CtdlMessageFree() function.
1157 struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body, int run_msg_hooks)
1159 struct CitContext *CCC = CC;
1160 struct cdbdata *dmsgtext;
1161 struct CtdlMessage *ret = NULL;
1163 MSG_syslog(LOG_DEBUG, "CtdlFetchMessage(%ld, %d)\n", msgnum, with_body);
1164 dmsgtext = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
1165 if (dmsgtext == NULL) {
1166 MSG_syslog(LOG_ERR, "CtdlFetchMessage(%ld, %d) Failed!\n", msgnum, with_body);
1170 if (dmsgtext->ptr[dmsgtext->len - 1] != '\0')
1172 MSG_syslog(LOG_ERR, "CtdlFetchMessage(%ld, %d) Forcefully terminating message!!\n", msgnum, with_body);
1173 dmsgtext->ptr[dmsgtext->len - 1] = '\0';
1176 ret = CtdlDeserializeMessage(msgnum, with_body, dmsgtext->ptr, dmsgtext->len);
1184 /* Always make sure there's something in the msg text field. If
1185 * it's NULL, the message text is most likely stored separately,
1186 * so go ahead and fetch that. Failing that, just set a dummy
1187 * body so other code doesn't barf.
1189 if ( (CM_IsEmpty(ret, eMesageText)) && (with_body) ) {
1190 dmsgtext = cdb_fetch(CDB_BIGMSGS, &msgnum, sizeof(long));
1191 if (dmsgtext != NULL) {
1192 CM_SetAsField(ret, eMesageText, &dmsgtext->ptr, dmsgtext->len - 1);
1196 if (CM_IsEmpty(ret, eMesageText)) {
1197 CM_SetField(ret, eMesageText, HKEY("\r\n\r\n (no text)\r\n"));
1200 /* Perform "before read" hooks (aborting if any return nonzero) */
1201 if (run_msg_hooks && (PerformMessageHooks(ret, NULL, EVT_BEFOREREAD) > 0)) {
1212 * Pre callback function for multipart/alternative
1214 * NOTE: this differs from the standard behavior for a reason. Normally when
1215 * displaying multipart/alternative you want to show the _last_ usable
1216 * format in the message. Here we show the _first_ one, because it's
1217 * usually text/plain. Since this set of functions is designed for text
1218 * output to non-MIME-aware clients, this is the desired behavior.
1221 void fixed_output_pre(char *name, char *filename, char *partnum, char *disp,
1222 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1223 char *cbid, void *cbuserdata)
1225 struct CitContext *CCC = CC;
1228 ma = (struct ma_info *)cbuserdata;
1229 MSG_syslog(LOG_DEBUG, "fixed_output_pre() type=<%s>\n", cbtype);
1230 if (!strcasecmp(cbtype, "multipart/alternative")) {
1234 if (!strcasecmp(cbtype, "message/rfc822")) {
1240 * Post callback function for multipart/alternative
1242 void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
1243 void *content, char *cbtype, char *cbcharset, size_t length,
1244 char *encoding, char *cbid, void *cbuserdata)
1246 struct CitContext *CCC = CC;
1249 ma = (struct ma_info *)cbuserdata;
1250 MSG_syslog(LOG_DEBUG, "fixed_output_post() type=<%s>\n", cbtype);
1251 if (!strcasecmp(cbtype, "multipart/alternative")) {
1255 if (!strcasecmp(cbtype, "message/rfc822")) {
1261 * Inline callback function for mime parser that wants to display text
1263 void fixed_output(char *name, char *filename, char *partnum, char *disp,
1264 void *content, char *cbtype, char *cbcharset, size_t length,
1265 char *encoding, char *cbid, void *cbuserdata)
1267 struct CitContext *CCC = CC;
1273 ma = (struct ma_info *)cbuserdata;
1275 MSG_syslog(LOG_DEBUG,
1276 "fixed_output() part %s: %s (%s) (%ld bytes)\n",
1277 partnum, filename, cbtype, (long)length);
1280 * If we're in the middle of a multipart/alternative scope and
1281 * we've already printed another section, skip this one.
1283 if ( (ma->is_ma) && (ma->did_print) ) {
1284 MSG_syslog(LOG_DEBUG, "Skipping part %s (%s)\n", partnum, cbtype);
1289 if ( (!strcasecmp(cbtype, "text/plain"))
1290 || (IsEmptyStr(cbtype)) ) {
1293 client_write(wptr, length);
1294 if (wptr[length-1] != '\n') {
1301 if (!strcasecmp(cbtype, "text/html")) {
1302 ptr = html_to_ascii(content, length, 80, 0);
1304 client_write(ptr, wlen);
1305 if ((wlen > 0) && (ptr[wlen-1] != '\n')) {
1312 if (ma->use_fo_hooks) {
1313 if (PerformFixedOutputHooks(cbtype, content, length)) {
1314 /* above function returns nonzero if it handled the part */
1319 if (strncasecmp(cbtype, "multipart/", 10)) {
1320 cprintf("Part %s: %s (%s) (%ld bytes)\r\n",
1321 partnum, filename, cbtype, (long)length);
1327 * The client is elegant and sophisticated and wants to be choosy about
1328 * MIME content types, so figure out which multipart/alternative part
1329 * we're going to send.
1331 * We use a system of weights. When we find a part that matches one of the
1332 * MIME types we've declared as preferential, we can store it in ma->chosen_part
1333 * and then set ma->chosen_pref to that MIME type's position in our preference
1334 * list. If we then hit another match, we only replace the first match if
1335 * the preference value is lower.
1337 void choose_preferred(char *name, char *filename, char *partnum, char *disp,
1338 void *content, char *cbtype, char *cbcharset, size_t length,
1339 char *encoding, char *cbid, void *cbuserdata)
1341 struct CitContext *CCC = CC;
1346 ma = (struct ma_info *)cbuserdata;
1348 // NOTE: REMOVING THIS CONDITIONAL FIXES BUG 220
1349 // http://bugzilla.citadel.org/show_bug.cgi?id=220
1350 // I don't know if there are any side effects! Please TEST TEST TEST
1351 //if (ma->is_ma > 0) {
1353 for (i=0; i<num_tokens(CCC->preferred_formats, '|'); ++i) {
1354 extract_token(buf, CCC->preferred_formats, i, '|', sizeof buf);
1355 if ( (!strcasecmp(buf, cbtype)) && (!ma->freeze) ) {
1356 if (i < ma->chosen_pref) {
1357 MSG_syslog(LOG_DEBUG, "Setting chosen part: <%s>\n", partnum);
1358 safestrncpy(ma->chosen_part, partnum, sizeof ma->chosen_part);
1359 ma->chosen_pref = i;
1366 * Now that we've chosen our preferred part, output it.
1368 void output_preferred(char *name,
1380 struct CitContext *CCC = CC;
1383 int add_newline = 0;
1386 char *decoded = NULL;
1387 size_t bytes_decoded;
1390 ma = (struct ma_info *)cbuserdata;
1392 /* This is not the MIME part you're looking for... */
1393 if (strcasecmp(partnum, ma->chosen_part)) return;
1395 /* If the content-type of this part is in our preferred formats
1396 * list, we can simply output it verbatim.
1398 for (i=0; i<num_tokens(CCC->preferred_formats, '|'); ++i) {
1399 extract_token(buf, CCC->preferred_formats, i, '|', sizeof buf);
1400 if (!strcasecmp(buf, cbtype)) {
1401 /* Yeah! Go! W00t!! */
1402 if (ma->dont_decode == 0)
1403 rc = mime_decode_now (content,
1409 break; /* Give us the chance, maybe theres another one. */
1411 if (rc == 0) text_content = (char *)content;
1413 text_content = decoded;
1414 length = bytes_decoded;
1417 if (text_content[length-1] != '\n') {
1420 cprintf("Content-type: %s", cbtype);
1421 if (!IsEmptyStr(cbcharset)) {
1422 cprintf("; charset=%s", cbcharset);
1424 cprintf("\nContent-length: %d\n",
1425 (int)(length + add_newline) );
1426 if (!IsEmptyStr(encoding)) {
1427 cprintf("Content-transfer-encoding: %s\n", encoding);
1430 cprintf("Content-transfer-encoding: 7bit\n");
1432 cprintf("X-Citadel-MSG4-Partnum: %s\n", partnum);
1434 if (client_write(text_content, length) == -1)
1436 MSGM_syslog(LOG_ERR, "output_preferred(): aborting due to write failure.\n");
1439 if (add_newline) cprintf("\n");
1440 if (decoded != NULL) free(decoded);
1445 /* No translations required or possible: output as text/plain */
1446 cprintf("Content-type: text/plain\n\n");
1448 if (ma->dont_decode == 0)
1449 rc = mime_decode_now (content,
1455 return; /* Give us the chance, maybe theres another one. */
1457 if (rc == 0) text_content = (char *)content;
1459 text_content = decoded;
1460 length = bytes_decoded;
1463 fixed_output(name, filename, partnum, disp, text_content, cbtype, cbcharset,
1464 length, encoding, cbid, cbuserdata);
1465 if (decoded != NULL) free(decoded);
1470 char desired_section[64];
1477 * Callback function for
1479 void extract_encapsulated_message(char *name, char *filename, char *partnum, char *disp,
1480 void *content, char *cbtype, char *cbcharset, size_t length,
1481 char *encoding, char *cbid, void *cbuserdata)
1483 struct encapmsg *encap;
1485 encap = (struct encapmsg *)cbuserdata;
1487 /* Only proceed if this is the desired section... */
1488 if (!strcasecmp(encap->desired_section, partnum)) {
1489 encap->msglen = length;
1490 encap->msg = malloc(length + 2);
1491 memcpy(encap->msg, content, length);
1498 * Determine whether the specified message exists in the cached_msglist
1499 * (This is a security check)
1501 int check_cached_msglist(long msgnum) {
1502 struct CitContext *CCC = CC;
1504 /* cases in which we skip the check */
1505 if (!CCC) return om_ok; /* not a session */
1506 if (CCC->client_socket <= 0) return om_ok; /* not a client session */
1507 if (CCC->cached_msglist == NULL) return om_access_denied; /* no msglist fetched */
1508 if (CCC->cached_num_msgs == 0) return om_access_denied; /* nothing to check */
1511 /* Do a binary search within the cached_msglist for the requested msgnum */
1513 int max = (CC->cached_num_msgs - 1);
1515 while (max >= min) {
1516 int middle = min + (max-min) / 2 ;
1517 if (msgnum == CCC->cached_msglist[middle]) {
1520 if (msgnum > CC->cached_msglist[middle]) {
1528 return om_access_denied;
1534 * Get a message off disk. (returns om_* values found in msgbase.h)
1537 int CtdlOutputMsg(long msg_num, /* message number (local) to fetch */
1538 int mode, /* how would you like that message? */
1539 int headers_only, /* eschew the message body? */
1540 int do_proto, /* do Citadel protocol responses? */
1541 int crlf, /* Use CRLF newlines instead of LF? */
1542 char *section, /* NULL or a message/rfc822 section */
1543 int flags, /* various flags; see msgbase.h */
1548 struct CitContext *CCC = CC;
1549 struct CtdlMessage *TheMessage = NULL;
1550 int retcode = CIT_OK;
1551 struct encapmsg encap;
1554 MSG_syslog(LOG_DEBUG, "CtdlOutputMsg(msgnum=%ld, mode=%d, section=%s)\n",
1556 (section ? section : "<>")
1559 r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
1562 if (r == om_not_logged_in) {
1563 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1566 cprintf("%d An unknown error has occurred.\n", ERROR);
1573 * Check to make sure the message is actually IN this room
1575 r = check_cached_msglist(msg_num);
1576 if (r == om_access_denied) {
1577 /* Not in the cache? We get ONE shot to check it again. */
1578 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, NULL, NULL);
1579 r = check_cached_msglist(msg_num);
1582 MSG_syslog(LOG_DEBUG, "Security check fail: message %ld is not in %s\n",
1583 msg_num, CCC->room.QRname
1586 if (r == om_access_denied) {
1587 cprintf("%d message %ld was not found in this room\n",
1588 ERROR + HIGHER_ACCESS_REQUIRED,
1597 * Fetch the message from disk. If we're in HEADERS_FAST mode,
1598 * request that we don't even bother loading the body into memory.
1600 if (headers_only == HEADERS_FAST) {
1601 TheMessage = CtdlFetchMessage(msg_num, 0, 1);
1604 TheMessage = CtdlFetchMessage(msg_num, 1, 1);
1607 if (TheMessage == NULL) {
1608 if (do_proto) cprintf("%d Can't locate msg %ld on disk\n",
1609 ERROR + MESSAGE_NOT_FOUND, msg_num);
1610 return(om_no_such_msg);
1613 /* Here is the weird form of this command, to process only an
1614 * encapsulated message/rfc822 section.
1616 if (section) if (!IsEmptyStr(section)) if (strcmp(section, "0")) {
1617 memset(&encap, 0, sizeof encap);
1618 safestrncpy(encap.desired_section, section, sizeof encap.desired_section);
1619 mime_parser(CM_RANGE(TheMessage, eMesageText),
1620 *extract_encapsulated_message,
1621 NULL, NULL, (void *)&encap, 0
1624 if ((Author != NULL) && (*Author == NULL))
1627 CM_GetAsField(TheMessage, eAuthor, Author, &len);
1629 if ((Address != NULL) && (*Address == NULL))
1632 CM_GetAsField(TheMessage, erFc822Addr, Address, &len);
1634 if ((MessageID != NULL) && (*MessageID == NULL))
1637 CM_GetAsField(TheMessage, emessageId, MessageID, &len);
1639 CM_Free(TheMessage);
1643 encap.msg[encap.msglen] = 0;
1644 TheMessage = convert_internet_message(encap.msg);
1645 encap.msg = NULL; /* no free() here, TheMessage owns it now */
1647 /* Now we let it fall through to the bottom of this
1648 * function, because TheMessage now contains the
1649 * encapsulated message instead of the top-level
1650 * message. Isn't that neat?
1655 cprintf("%d msg %ld has no part %s\n",
1656 ERROR + MESSAGE_NOT_FOUND,
1660 retcode = om_no_such_msg;
1665 /* Ok, output the message now */
1666 if (retcode == CIT_OK)
1667 retcode = CtdlOutputPreLoadedMsg(TheMessage, mode, headers_only, do_proto, crlf, flags);
1668 if ((Author != NULL) && (*Author == NULL))
1671 CM_GetAsField(TheMessage, eAuthor, Author, &len);
1673 if ((Address != NULL) && (*Address == NULL))
1676 CM_GetAsField(TheMessage, erFc822Addr, Address, &len);
1678 if ((MessageID != NULL) && (*MessageID == NULL))
1681 CM_GetAsField(TheMessage, emessageId, MessageID, &len);
1684 CM_Free(TheMessage);
1691 void OutputCtdlMsgHeaders(
1692 struct CtdlMessage *TheMessage,
1693 int do_proto) /* do Citadel protocol responses? */
1698 char display_name[256];
1700 /* begin header processing loop for Citadel message format */
1701 safestrncpy(display_name, "<unknown>", sizeof display_name);
1702 if (!CM_IsEmpty(TheMessage, eAuthor)) {
1703 strcpy(buf, TheMessage->cm_fields[eAuthor]);
1704 if (TheMessage->cm_anon_type == MES_ANONONLY) {
1705 safestrncpy(display_name, "****", sizeof display_name);
1707 else if (TheMessage->cm_anon_type == MES_ANONOPT) {
1708 safestrncpy(display_name, "anonymous", sizeof display_name);
1711 safestrncpy(display_name, buf, sizeof display_name);
1713 if ((is_room_aide())
1714 && ((TheMessage->cm_anon_type == MES_ANONONLY)
1715 || (TheMessage->cm_anon_type == MES_ANONOPT))) {
1716 size_t tmp = strlen(display_name);
1717 snprintf(&display_name[tmp],
1718 sizeof display_name - tmp,
1723 /* Don't show Internet address for users on the
1724 * local Citadel network.
1727 if (!CM_IsEmpty(TheMessage, eNodeName) &&
1728 (haschar(TheMessage->cm_fields[eNodeName], '.') == 0))
1733 /* Now spew the header fields in the order we like them. */
1734 for (i=0; i< NDiskFields; ++i) {
1736 Field = FieldOrder[i];
1737 if (Field != eMesageText) {
1738 if ( (!CM_IsEmpty(TheMessage, Field))
1739 && (msgkeys[Field] != NULL) ) {
1740 if ((Field == eenVelopeTo) ||
1741 (Field == eRecipient) ||
1742 (Field == eCarbonCopY)) {
1743 sanitize_truncated_recipient(TheMessage->cm_fields[Field]);
1745 if (Field == eAuthor) {
1746 if (do_proto) cprintf("%s=%s\n",
1750 else if ((Field == erFc822Addr) && (suppress_f)) {
1753 /* Masquerade display name if needed */
1755 if (do_proto) cprintf("%s=%s\n",
1757 TheMessage->cm_fields[Field]
1766 void OutputRFC822MsgHeaders(
1767 struct CtdlMessage *TheMessage,
1768 int flags, /* should the bessage be exported clean */
1769 const char *nl, int nlen,
1770 char *mid, long sizeof_mid,
1771 char *suser, long sizeof_suser,
1772 char *luser, long sizeof_luser,
1773 char *fuser, long sizeof_fuser,
1774 char *snode, long sizeof_snode)
1776 char datestamp[100];
1777 int subject_found = 0;
1784 for (i = 0; i < NDiskFields; ++i) {
1785 if (TheMessage->cm_fields[FieldOrder[i]]) {
1786 mptr = mpptr = TheMessage->cm_fields[FieldOrder[i]];
1787 switch (FieldOrder[i]) {
1789 safestrncpy(luser, mptr, sizeof_luser);
1790 safestrncpy(suser, mptr, sizeof_suser);
1793 if ((flags & QP_EADDR) != 0) {
1794 mptr = qp_encode_email_addrs(mptr);
1796 sanitize_truncated_recipient(mptr);
1797 cprintf("CC: %s%s", mptr, nl);
1800 cprintf("Return-Path: %s%s", mptr, nl);
1803 cprintf("List-ID: %s%s", mptr, nl);
1806 if ((flags & QP_EADDR) != 0)
1807 mptr = qp_encode_email_addrs(mptr);
1809 while ((*hptr != '\0') && isspace(*hptr))
1811 if (!IsEmptyStr(hptr))
1812 cprintf("Envelope-To: %s%s", hptr, nl);
1815 cprintf("Subject: %s%s", mptr, nl);
1819 safestrncpy(mid, mptr, sizeof_mid); /// TODO: detect @ here and copy @nodename in if not found.
1822 safestrncpy(fuser, mptr, sizeof_fuser);
1823 /* case eOriginalRoom:
1824 cprintf("X-Citadel-Room: %s%s",
1829 safestrncpy(snode, mptr, sizeof_snode);
1832 if (haschar(mptr, '@') == 0)
1834 sanitize_truncated_recipient(mptr);
1835 cprintf("To: %s@%s", mptr, CtdlGetConfigStr("c_fqdn"));
1840 if ((flags & QP_EADDR) != 0) {
1841 mptr = qp_encode_email_addrs(mptr);
1843 sanitize_truncated_recipient(mptr);
1844 cprintf("To: %s", mptr);
1849 datestring(datestamp, sizeof datestamp,
1850 atol(mptr), DATESTRING_RFC822);
1851 cprintf("Date: %s%s", datestamp, nl);
1854 cprintf("References: ");
1855 k = num_tokens(mptr, '|');
1856 for (j=0; j<k; ++j) {
1857 extract_token(buf, mptr, j, '|', sizeof buf);
1858 cprintf("<%s>", buf);
1869 while ((*hptr != '\0') && isspace(*hptr))
1871 if (!IsEmptyStr(hptr))
1872 cprintf("Reply-To: %s%s", mptr, nl);
1888 /* these don't map to mime message headers. */
1896 if (subject_found == 0) {
1897 cprintf("Subject: (no subject)%s", nl);
1902 void Dump_RFC822HeadersBody(
1903 struct CtdlMessage *TheMessage,
1904 int headers_only, /* eschew the message body? */
1905 int flags, /* should the bessage be exported clean? */
1907 const char *nl, int nlen)
1909 cit_uint8_t prev_ch;
1911 const char *StartOfText = StrBufNOTNULL;
1914 int nllen = strlen(nl);
1918 mptr = TheMessage->cm_fields[eMesageText];
1922 while (*mptr != '\0') {
1923 if (*mptr == '\r') {
1930 eoh = (*(mptr+1) == '\r') && (*(mptr+2) == '\n');
1932 eoh = *(mptr+1) == '\n';
1936 StartOfText = strchr(StartOfText, '\n');
1937 StartOfText = strchr(StartOfText, '\n');
1940 if (((headers_only == HEADERS_NONE) && (mptr >= StartOfText)) ||
1941 ((headers_only == HEADERS_ONLY) && (mptr < StartOfText)) ||
1942 ((headers_only != HEADERS_NONE) &&
1943 (headers_only != HEADERS_ONLY))
1945 if (*mptr == '\n') {
1946 memcpy(&outbuf[outlen], nl, nllen);
1948 outbuf[outlen] = '\0';
1951 outbuf[outlen++] = *mptr;
1955 if (flags & ESC_DOT)
1957 if ((prev_ch == '\n') &&
1959 ((*(mptr+1) == '\r') || (*(mptr+1) == '\n')))
1961 outbuf[outlen++] = '.';
1966 if (outlen > 1000) {
1967 if (client_write(outbuf, outlen) == -1)
1969 struct CitContext *CCC = CC;
1970 MSGM_syslog(LOG_ERR, "Dump_RFC822HeadersBody(): aborting due to write failure.\n");
1973 lfSent = (outbuf[outlen - 1] == '\n');
1978 client_write(outbuf, outlen);
1979 lfSent = (outbuf[outlen - 1] == '\n');
1982 client_write(nl, nlen);
1987 /* If the format type on disk is 1 (fixed-format), then we want
1988 * everything to be output completely literally ... regardless of
1989 * what message transfer format is in use.
1991 void DumpFormatFixed(
1992 struct CtdlMessage *TheMessage,
1993 int mode, /* how would you like that message? */
1994 const char *nl, int nllen)
2002 mptr = TheMessage->cm_fields[eMesageText];
2004 if (mode == MT_MIME) {
2005 cprintf("Content-type: text/plain\n\n");
2009 while (ch = *mptr++, ch > 0) {
2013 if ((buflen > 250) && (!xlline)){
2017 while ((buflen > 0) &&
2018 (!isspace(buf[buflen])))
2024 mptr -= tbuflen - buflen;
2029 /* if we reach the outer bounds of our buffer,
2030 abort without respect what whe purge. */
2033 (buflen > SIZ - nllen - 2)))
2037 memcpy (&buf[buflen], nl, nllen);
2041 if (client_write(buf, buflen) == -1)
2043 struct CitContext *CCC = CC;
2044 MSGM_syslog(LOG_ERR, "DumpFormatFixed(): aborting due to write failure.\n");
2056 if (!IsEmptyStr(buf))
2057 cprintf("%s%s", buf, nl);
2061 * Get a message off disk. (returns om_* values found in msgbase.h)
2063 int CtdlOutputPreLoadedMsg(
2064 struct CtdlMessage *TheMessage,
2065 int mode, /* how would you like that message? */
2066 int headers_only, /* eschew the message body? */
2067 int do_proto, /* do Citadel protocol responses? */
2068 int crlf, /* Use CRLF newlines instead of LF? */
2069 int flags /* should the bessage be exported clean? */
2071 struct CitContext *CCC = CC;
2073 const char *nl; /* newline string */
2077 /* Buffers needed for RFC822 translation. These are all filled
2078 * using functions that are bounds-checked, and therefore we can
2079 * make them substantially smaller than SIZ.
2087 MSG_syslog(LOG_DEBUG, "CtdlOutputPreLoadedMsg(TheMessage=%s, %d, %d, %d, %d\n",
2088 ((TheMessage == NULL) ? "NULL" : "not null"),
2089 mode, headers_only, do_proto, crlf);
2091 strcpy(mid, "unknown");
2092 nl = (crlf ? "\r\n" : "\n");
2093 nlen = crlf ? 2 : 1;
2095 if (!CM_IsValidMsg(TheMessage)) {
2096 MSGM_syslog(LOG_ERR,
2097 "ERROR: invalid preloaded message for output\n");
2099 return(om_no_such_msg);
2102 /* Suppress envelope recipients if required to avoid disclosing BCC addresses.
2103 * Pad it with spaces in order to avoid changing the RFC822 length of the message.
2105 if ( (flags & SUPPRESS_ENV_TO) && (!CM_IsEmpty(TheMessage, eenVelopeTo)) ) {
2106 memset(TheMessage->cm_fields[eenVelopeTo], ' ', TheMessage->cm_lengths[eenVelopeTo]);
2109 /* Are we downloading a MIME component? */
2110 if (mode == MT_DOWNLOAD) {
2111 if (TheMessage->cm_format_type != FMT_RFC822) {
2113 cprintf("%d This is not a MIME message.\n",
2114 ERROR + ILLEGAL_VALUE);
2115 } else if (CCC->download_fp != NULL) {
2116 if (do_proto) cprintf(
2117 "%d You already have a download open.\n",
2118 ERROR + RESOURCE_BUSY);
2120 /* Parse the message text component */
2121 mime_parser(CM_RANGE(TheMessage, eMesageText),
2122 *mime_download, NULL, NULL, NULL, 0);
2123 /* If there's no file open by this time, the requested
2124 * section wasn't found, so print an error
2126 if (CCC->download_fp == NULL) {
2127 if (do_proto) cprintf(
2128 "%d Section %s not found.\n",
2129 ERROR + FILE_NOT_FOUND,
2130 CCC->download_desired_section);
2133 return((CCC->download_fp != NULL) ? om_ok : om_mime_error);
2136 /* MT_SPEW_SECTION is like MT_DOWNLOAD except it outputs the whole MIME part
2137 * in a single server operation instead of opening a download file.
2139 if (mode == MT_SPEW_SECTION) {
2140 if (TheMessage->cm_format_type != FMT_RFC822) {
2142 cprintf("%d This is not a MIME message.\n",
2143 ERROR + ILLEGAL_VALUE);
2145 /* Parse the message text component */
2148 mime_parser(CM_RANGE(TheMessage, eMesageText),
2149 *mime_spew_section, NULL, NULL, (void *)&found_it, 0);
2150 /* If section wasn't found, print an error
2153 if (do_proto) cprintf(
2154 "%d Section %s not found.\n",
2155 ERROR + FILE_NOT_FOUND,
2156 CCC->download_desired_section);
2159 return((CCC->download_fp != NULL) ? om_ok : om_mime_error);
2162 /* now for the user-mode message reading loops */
2163 if (do_proto) cprintf("%d msg:\n", LISTING_FOLLOWS);
2165 /* Does the caller want to skip the headers? */
2166 if (headers_only == HEADERS_NONE) goto START_TEXT;
2168 /* Tell the client which format type we're using. */
2169 if ( (mode == MT_CITADEL) && (do_proto) ) {
2170 cprintf("type=%d\n", TheMessage->cm_format_type);
2173 /* nhdr=yes means that we're only displaying headers, no body */
2174 if ( (TheMessage->cm_anon_type == MES_ANONONLY)
2175 && ((mode == MT_CITADEL) || (mode == MT_MIME))
2178 cprintf("nhdr=yes\n");
2181 if ((mode == MT_CITADEL) || (mode == MT_MIME))
2182 OutputCtdlMsgHeaders(TheMessage, do_proto);
2185 /* begin header processing loop for RFC822 transfer format */
2189 memcpy(snode, CtdlGetConfigStr("c_nodename"), strlen(CtdlGetConfigStr("c_nodename")) + 1);
2190 if (mode == MT_RFC822)
2191 OutputRFC822MsgHeaders(
2196 suser, sizeof(suser),
2197 luser, sizeof(luser),
2198 fuser, sizeof(fuser),
2199 snode, sizeof(snode)
2203 for (i=0; !IsEmptyStr(&suser[i]); ++i) {
2204 suser[i] = tolower(suser[i]);
2205 if (!isalnum(suser[i])) suser[i]='_';
2208 if (mode == MT_RFC822) {
2209 if (!strcasecmp(snode, NODENAME)) {
2210 safestrncpy(snode, FQDN, sizeof snode);
2213 /* Construct a fun message id */
2214 cprintf("Message-ID: <%s", mid);/// todo: this possibly breaks threadding mails.
2215 if (strchr(mid, '@')==NULL) {
2216 cprintf("@%s", snode);
2220 if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONONLY)) {
2221 cprintf("From: \"----\" <x@x.org>%s", nl);
2223 else if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONOPT)) {
2224 cprintf("From: \"anonymous\" <x@x.org>%s", nl);
2226 else if (!IsEmptyStr(fuser)) {
2227 cprintf("From: \"%s\" <%s>%s", luser, fuser, nl);
2230 cprintf("From: \"%s\" <%s@%s>%s", luser, suser, snode, nl);
2233 /* Blank line signifying RFC822 end-of-headers */
2234 if (TheMessage->cm_format_type != FMT_RFC822) {
2239 /* end header processing loop ... at this point, we're in the text */
2241 if (headers_only == HEADERS_FAST) goto DONE;
2243 /* Tell the client about the MIME parts in this message */
2244 if (TheMessage->cm_format_type == FMT_RFC822) {
2245 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2246 memset(&ma, 0, sizeof(struct ma_info));
2247 mime_parser(CM_RANGE(TheMessage, eMesageText),
2248 (do_proto ? *list_this_part : NULL),
2249 (do_proto ? *list_this_pref : NULL),
2250 (do_proto ? *list_this_suff : NULL),
2253 else if (mode == MT_RFC822) { /* unparsed RFC822 dump */
2254 Dump_RFC822HeadersBody(
2263 if (headers_only == HEADERS_ONLY) {
2267 /* signify start of msg text */
2268 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2269 if (do_proto) cprintf("text\n");
2272 if (TheMessage->cm_format_type == FMT_FIXED)
2275 mode, /* how would you like that message? */
2278 /* If the message on disk is format 0 (Citadel vari-format), we
2279 * output using the formatter at 80 columns. This is the final output
2280 * form if the transfer format is RFC822, but if the transfer format
2281 * is Citadel proprietary, it'll still work, because the indentation
2282 * for new paragraphs is correct and the client will reformat the
2283 * message to the reader's screen width.
2285 if (TheMessage->cm_format_type == FMT_CITADEL) {
2286 if (mode == MT_MIME) {
2287 cprintf("Content-type: text/x-citadel-variformat\n\n");
2289 memfmout(TheMessage->cm_fields[eMesageText], nl);
2292 /* If the message on disk is format 4 (MIME), we've gotta hand it
2293 * off to the MIME parser. The client has already been told that
2294 * this message is format 1 (fixed format), so the callback function
2295 * we use will display those parts as-is.
2297 if (TheMessage->cm_format_type == FMT_RFC822) {
2298 memset(&ma, 0, sizeof(struct ma_info));
2300 if (mode == MT_MIME) {
2301 ma.use_fo_hooks = 0;
2302 strcpy(ma.chosen_part, "1");
2303 ma.chosen_pref = 9999;
2304 ma.dont_decode = CCC->msg4_dont_decode;
2305 mime_parser(CM_RANGE(TheMessage, eMesageText),
2306 *choose_preferred, *fixed_output_pre,
2307 *fixed_output_post, (void *)&ma, 1);
2308 mime_parser(CM_RANGE(TheMessage, eMesageText),
2309 *output_preferred, NULL, NULL, (void *)&ma, 1);
2312 ma.use_fo_hooks = 1;
2313 mime_parser(CM_RANGE(TheMessage, eMesageText),
2314 *fixed_output, *fixed_output_pre,
2315 *fixed_output_post, (void *)&ma, 0);
2320 DONE: /* now we're done */
2321 if (do_proto) cprintf("000\n");
2326 * Save one or more message pointers into a specified room
2327 * (Returns 0 for success, nonzero for failure)
2328 * roomname may be NULL to use the current room
2330 * Note that the 'supplied_msg' field may be set to NULL, in which case
2331 * the message will be fetched from disk, by number, if we need to perform
2332 * replication checks. This adds an additional database read, so if the
2333 * caller already has the message in memory then it should be supplied. (Obviously
2334 * this mode of operation only works if we're saving a single message.)
2336 int CtdlSaveMsgPointersInRoom(char *roomname, long newmsgidlist[], int num_newmsgs,
2337 int do_repl_check, struct CtdlMessage *supplied_msg, int suppress_refcount_adj
2339 struct CitContext *CCC = CC;
2341 char hold_rm[ROOMNAMELEN];
2342 struct cdbdata *cdbfr;
2345 long highest_msg = 0L;
2348 struct CtdlMessage *msg = NULL;
2350 long *msgs_to_be_merged = NULL;
2351 int num_msgs_to_be_merged = 0;
2353 MSG_syslog(LOG_DEBUG,
2354 "CtdlSaveMsgPointersInRoom(room=%s, num_msgs=%d, repl=%d, suppress_rca=%d)\n",
2355 roomname, num_newmsgs, do_repl_check, suppress_refcount_adj
2358 strcpy(hold_rm, CCC->room.QRname);
2361 if (newmsgidlist == NULL) return(ERROR + INTERNAL_ERROR);
2362 if (num_newmsgs < 1) return(ERROR + INTERNAL_ERROR);
2363 if (num_newmsgs > 1) supplied_msg = NULL;
2365 /* Now the regular stuff */
2366 if (CtdlGetRoomLock(&CCC->room,
2367 ((roomname != NULL) ? roomname : CCC->room.QRname) )
2369 MSG_syslog(LOG_ERR, "No such room <%s>\n", roomname);
2370 return(ERROR + ROOM_NOT_FOUND);
2374 msgs_to_be_merged = malloc(sizeof(long) * num_newmsgs);
2375 num_msgs_to_be_merged = 0;
2378 cdbfr = cdb_fetch(CDB_MSGLISTS, &CCC->room.QRnumber, sizeof(long));
2379 if (cdbfr == NULL) {
2383 msglist = (long *) cdbfr->ptr;
2384 cdbfr->ptr = NULL; /* CtdlSaveMsgPointerInRoom() now owns this memory */
2385 num_msgs = cdbfr->len / sizeof(long);
2390 /* Create a list of msgid's which were supplied by the caller, but do
2391 * not already exist in the target room. It is absolutely taboo to
2392 * have more than one reference to the same message in a room.
2394 for (i=0; i<num_newmsgs; ++i) {
2396 if (num_msgs > 0) for (j=0; j<num_msgs; ++j) {
2397 if (msglist[j] == newmsgidlist[i]) {
2402 msgs_to_be_merged[num_msgs_to_be_merged++] = newmsgidlist[i];
2406 MSG_syslog(LOG_DEBUG, "%d unique messages to be merged\n", num_msgs_to_be_merged);
2409 * Now merge the new messages
2411 msglist = realloc(msglist, (sizeof(long) * (num_msgs + num_msgs_to_be_merged)) );
2412 if (msglist == NULL) {
2413 MSGM_syslog(LOG_ALERT, "ERROR: can't realloc message list!\n");
2414 free(msgs_to_be_merged);
2415 return (ERROR + INTERNAL_ERROR);
2417 memcpy(&msglist[num_msgs], msgs_to_be_merged, (sizeof(long) * num_msgs_to_be_merged) );
2418 num_msgs += num_msgs_to_be_merged;
2420 /* Sort the message list, so all the msgid's are in order */
2421 num_msgs = sort_msglist(msglist, num_msgs);
2423 /* Determine the highest message number */
2424 highest_msg = msglist[num_msgs - 1];
2426 /* Write it back to disk. */
2427 cdb_store(CDB_MSGLISTS, &CCC->room.QRnumber, (int)sizeof(long),
2428 msglist, (int)(num_msgs * sizeof(long)));
2430 /* Free up the memory we used. */
2433 /* Update the highest-message pointer and unlock the room. */
2434 CCC->room.QRhighest = highest_msg;
2435 CtdlPutRoomLock(&CCC->room);
2437 /* Perform replication checks if necessary */
2438 if ( (DoesThisRoomNeedEuidIndexing(&CCC->room)) && (do_repl_check) ) {
2439 MSGM_syslog(LOG_DEBUG, "CtdlSaveMsgPointerInRoom() doing repl checks\n");
2441 for (i=0; i<num_msgs_to_be_merged; ++i) {
2442 msgid = msgs_to_be_merged[i];
2444 if (supplied_msg != NULL) {
2448 msg = CtdlFetchMessage(msgid, 0, 1);
2452 ReplicationChecks(msg);
2454 /* If the message has an Exclusive ID, index that... */
2455 if (!CM_IsEmpty(msg, eExclusiveID)) {
2456 index_message_by_euid(msg->cm_fields[eExclusiveID], &CCC->room, msgid);
2459 /* Free up the memory we may have allocated */
2460 if (msg != supplied_msg) {
2469 MSGM_syslog(LOG_DEBUG, "CtdlSaveMsgPointerInRoom() skips repl checks\n");
2472 /* Submit this room for processing by hooks */
2473 PerformRoomHooks(&CCC->room);
2475 /* Go back to the room we were in before we wandered here... */
2476 CtdlGetRoom(&CCC->room, hold_rm);
2478 /* Bump the reference count for all messages which were merged */
2479 if (!suppress_refcount_adj) {
2480 AdjRefCountList(msgs_to_be_merged, num_msgs_to_be_merged, +1);
2483 /* Free up memory... */
2484 if (msgs_to_be_merged != NULL) {
2485 free(msgs_to_be_merged);
2488 /* Return success. */
2494 * This is the same as CtdlSaveMsgPointersInRoom() but it only accepts
2497 int CtdlSaveMsgPointerInRoom(char *roomname, long msgid,
2498 int do_repl_check, struct CtdlMessage *supplied_msg)
2500 return CtdlSaveMsgPointersInRoom(roomname, &msgid, 1, do_repl_check, supplied_msg, 0);
2507 * Message base operation to save a new message to the message store
2508 * (returns new message number)
2510 * This is the back end for CtdlSubmitMsg() and should not be directly
2511 * called by server-side modules.
2514 long CtdlSaveThisMessage(struct CtdlMessage *msg, long msgid, int Reply) {
2515 struct CitContext *CCC = CC;
2523 * If the message is big, set its body aside for storage elsewhere
2524 * and we hide the message body from the serializer
2526 if (!CM_IsEmpty(msg, eMesageText) && msg->cm_lengths[eMesageText] > BIGMSG)
2529 holdM = msg->cm_fields[eMesageText];
2530 msg->cm_fields[eMesageText] = NULL;
2531 holdMLen = msg->cm_lengths[eMesageText];
2532 msg->cm_lengths[eMesageText] = 0;
2535 /* Serialize our data structure for storage in the database */
2536 CtdlSerializeMessage(&smr, msg);
2539 /* put the message body back into the message */
2540 msg->cm_fields[eMesageText] = holdM;
2541 msg->cm_lengths[eMesageText] = holdMLen;
2546 cprintf("%d Unable to serialize message\n",
2547 ERROR + INTERNAL_ERROR);
2550 MSGM_syslog(LOG_ERR, "CtdlSaveMessage() unable to serialize message");
2556 /* Write our little bundle of joy into the message base */
2557 retval = cdb_store(CDB_MSGMAIN, &msgid, (int)sizeof(long),
2560 MSG_syslog(LOG_ERR, "Can't store message %ld: %ld", msgid, retval);
2564 retval = cdb_store(CDB_BIGMSGS,
2571 MSG_syslog(LOG_ERR, "failed to store message body for msgid %ld: %ld",
2577 /* Free the memory we used for the serialized message */
2583 long send_message(struct CtdlMessage *msg) {
2589 /* Get a new message number */
2590 newmsgid = get_new_message_number();
2592 /* Generate an ID if we don't have one already */
2593 if (CM_IsEmpty(msg, emessageId)) {
2594 msgidbuflen = snprintf(msgidbuf, sizeof msgidbuf, "%08lX-%08lX@%s",
2595 (long unsigned int) time(NULL),
2596 (long unsigned int) newmsgid,
2597 CtdlGetConfigStr("c_fqdn")
2600 CM_SetField(msg, emessageId, msgidbuf, msgidbuflen);
2603 retval = CtdlSaveThisMessage(msg, newmsgid, 1);
2609 /* Return the *local* message ID to the caller
2610 * (even if we're storing an incoming network message)
2618 * Serialize a struct CtdlMessage into the format used on disk and network.
2620 * This function loads up a "struct ser_ret" (defined in server.h) which
2621 * contains the length of the serialized message and a pointer to the
2622 * serialized message in memory. THE LATTER MUST BE FREED BY THE CALLER.
2624 void CtdlSerializeMessage(struct ser_ret *ret, /* return values */
2625 struct CtdlMessage *msg) /* unserialized msg */
2627 struct CitContext *CCC = CC;
2632 * Check for valid message format
2634 if (CM_IsValidMsg(msg) == 0) {
2635 MSGM_syslog(LOG_ERR, "CtdlSerializeMessage() aborting due to invalid message\n");
2642 for (i=0; i < NDiskFields; ++i)
2643 if (msg->cm_fields[FieldOrder[i]] != NULL)
2644 ret->len += msg->cm_lengths[FieldOrder[i]] + 2;
2646 ret->ser = malloc(ret->len);
2647 if (ret->ser == NULL) {
2648 MSG_syslog(LOG_ERR, "CtdlSerializeMessage() malloc(%ld) failed: %s\n",
2649 (long)ret->len, strerror(errno));
2656 ret->ser[1] = msg->cm_anon_type;
2657 ret->ser[2] = msg->cm_format_type;
2660 for (i=0; i < NDiskFields; ++i)
2661 if (msg->cm_fields[FieldOrder[i]] != NULL)
2663 ret->ser[wlen++] = (char)FieldOrder[i];
2665 memcpy(&ret->ser[wlen],
2666 msg->cm_fields[FieldOrder[i]],
2667 msg->cm_lengths[FieldOrder[i]] + 1);
2669 wlen = wlen + msg->cm_lengths[FieldOrder[i]] + 1;
2672 if (ret->len != wlen) {
2673 MSG_syslog(LOG_ERR, "ERROR: len=%ld wlen=%ld\n",
2674 (long)ret->len, (long)wlen);
2682 * Check to see if any messages already exist in the current room which
2683 * carry the same Exclusive ID as this one. If any are found, delete them.
2685 void ReplicationChecks(struct CtdlMessage *msg) {
2686 struct CitContext *CCC = CC;
2687 long old_msgnum = (-1L);
2689 if (DoesThisRoomNeedEuidIndexing(&CCC->room) == 0) return;
2691 MSG_syslog(LOG_DEBUG, "Performing replication checks in <%s>\n",
2694 /* No exclusive id? Don't do anything. */
2695 if (msg == NULL) return;
2696 if (CM_IsEmpty(msg, eExclusiveID)) return;
2698 /*MSG_syslog(LOG_DEBUG, "Exclusive ID: <%s> for room <%s>\n",
2699 msg->cm_fields[eExclusiveID], CCC->room.QRname);*/
2701 old_msgnum = CtdlLocateMessageByEuid(msg->cm_fields[eExclusiveID], &CCC->room);
2702 if (old_msgnum > 0L) {
2703 MSG_syslog(LOG_DEBUG, "ReplicationChecks() replacing message %ld\n", old_msgnum);
2704 CtdlDeleteMessages(CCC->room.QRname, &old_msgnum, 1, "");
2711 * Save a message to disk and submit it into the delivery system.
2713 long CtdlSubmitMsg(struct CtdlMessage *msg, /* message to save */
2714 recptypes *recps, /* recipients (if mail) */
2715 const char *force, /* force a particular room? */
2716 int flags /* should the message be exported clean? */
2719 char hold_rm[ROOMNAMELEN];
2720 char actual_rm[ROOMNAMELEN];
2721 char force_room[ROOMNAMELEN];
2722 char content_type[SIZ]; /* We have to learn this */
2723 char recipient[SIZ];
2724 char bounce_to[1024];
2727 const char *mptr = NULL;
2728 struct ctdluser userbuf;
2730 struct MetaData smi;
2731 char *collected_addresses = NULL;
2732 struct addresses_to_be_filed *aptr = NULL;
2733 StrBuf *saved_rfc822_version = NULL;
2734 int qualified_for_journaling = 0;
2735 CitContext *CCC = MyContext();
2737 MSGM_syslog(LOG_DEBUG, "CtdlSubmitMsg() called\n");
2738 if (CM_IsValidMsg(msg) == 0) return(-1); /* self check */
2740 /* If this message has no timestamp, we take the liberty of
2741 * giving it one, right now.
2743 if (CM_IsEmpty(msg, eTimestamp)) {
2744 CM_SetFieldLONG(msg, eTimestamp, time(NULL));
2747 /* If this message has no path, we generate one.
2749 if (CM_IsEmpty(msg, eMessagePath)) {
2750 if (!CM_IsEmpty(msg, eAuthor)) {
2751 CM_CopyField(msg, eMessagePath, eAuthor);
2752 for (a=0; !IsEmptyStr(&msg->cm_fields[eMessagePath][a]); ++a) {
2753 if (isspace(msg->cm_fields[eMessagePath][a])) {
2754 msg->cm_fields[eMessagePath][a] = ' ';
2759 CM_SetField(msg, eMessagePath, HKEY("unknown"));
2763 if (force == NULL) {
2764 force_room[0] = '\0';
2767 strcpy(force_room, force);
2770 /* Learn about what's inside, because it's what's inside that counts */
2771 if (CM_IsEmpty(msg, eMesageText)) {
2772 MSGM_syslog(LOG_ERR, "ERROR: attempt to save message with NULL body\n");
2776 switch (msg->cm_format_type) {
2778 strcpy(content_type, "text/x-citadel-variformat");
2781 strcpy(content_type, "text/plain");
2784 strcpy(content_type, "text/plain");
2785 mptr = bmstrcasestr(msg->cm_fields[eMesageText], "Content-type:");
2788 safestrncpy(content_type, &mptr[13], sizeof content_type);
2789 striplt(content_type);
2790 aptr = content_type;
2791 while (!IsEmptyStr(aptr)) {
2803 /* Goto the correct room */
2804 room = (recps) ? CCC->room.QRname : SENTITEMS;
2805 MSG_syslog(LOG_DEBUG, "Selected room %s\n", room);
2806 strcpy(hold_rm, CCC->room.QRname);
2807 strcpy(actual_rm, CCC->room.QRname);
2808 if (recps != NULL) {
2809 strcpy(actual_rm, SENTITEMS);
2812 /* If the user is a twit, move to the twit room for posting */
2814 if (CCC->user.axlevel == AxProbU) {
2815 strcpy(hold_rm, actual_rm);
2816 strcpy(actual_rm, CtdlGetConfigStr("c_twitroom"));
2817 MSGM_syslog(LOG_DEBUG, "Diverting to twit room\n");
2821 /* ...or if this message is destined for Aide> then go there. */
2822 if (!IsEmptyStr(force_room)) {
2823 strcpy(actual_rm, force_room);
2826 MSG_syslog(LOG_INFO, "Final selection: %s (%s)\n", actual_rm, room);
2827 if (strcasecmp(actual_rm, CCC->room.QRname)) {
2828 /* CtdlGetRoom(&CCC->room, actual_rm); */
2829 CtdlUserGoto(actual_rm, 0, 1, NULL, NULL, NULL, NULL);
2833 * If this message has no O (room) field, generate one.
2835 if (CM_IsEmpty(msg, eOriginalRoom) && !IsEmptyStr(CCC->room.QRname)) {
2836 CM_SetField(msg, eOriginalRoom, CCC->room.QRname, strlen(CCC->room.QRname));
2839 /* Perform "before save" hooks (aborting if any return nonzero) */
2840 MSGM_syslog(LOG_DEBUG, "Performing before-save hooks\n");
2841 if (PerformMessageHooks(msg, recps, EVT_BEFORESAVE) > 0) return(-3);
2844 * If this message has an Exclusive ID, and the room is replication
2845 * checking enabled, then do replication checks.
2847 if (DoesThisRoomNeedEuidIndexing(&CCC->room)) {
2848 ReplicationChecks(msg);
2851 /* Save it to disk */
2852 MSGM_syslog(LOG_DEBUG, "Saving to disk\n");
2853 newmsgid = send_message(msg);
2854 if (newmsgid <= 0L) return(-5);
2856 /* Write a supplemental message info record. This doesn't have to
2857 * be a critical section because nobody else knows about this message
2860 MSGM_syslog(LOG_DEBUG, "Creating MetaData record\n");
2861 memset(&smi, 0, sizeof(struct MetaData));
2862 smi.meta_msgnum = newmsgid;
2863 smi.meta_refcount = 0;
2864 safestrncpy(smi.meta_content_type, content_type,
2865 sizeof smi.meta_content_type);
2868 * Measure how big this message will be when rendered as RFC822.
2869 * We do this for two reasons:
2870 * 1. We need the RFC822 length for the new metadata record, so the
2871 * POP and IMAP services don't have to calculate message lengths
2872 * while the user is waiting (multiplied by potentially hundreds
2873 * or thousands of messages).
2874 * 2. If journaling is enabled, we will need an RFC822 version of the
2875 * message to attach to the journalized copy.
2877 if (CCC->redirect_buffer != NULL) {
2878 MSGM_syslog(LOG_ALERT, "CCC->redirect_buffer is not NULL during message submission!\n");
2881 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
2882 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, QP_EADDR);
2883 smi.meta_rfc822_length = StrLength(CCC->redirect_buffer);
2884 saved_rfc822_version = CCC->redirect_buffer;
2885 CCC->redirect_buffer = NULL;
2889 /* Now figure out where to store the pointers */
2890 MSGM_syslog(LOG_DEBUG, "Storing pointers\n");
2892 /* If this is being done by the networker delivering a private
2893 * message, we want to BYPASS saving the sender's copy (because there
2894 * is no local sender; it would otherwise go to the Trashcan).
2896 if ((!CCC->internal_pgm) || (recps == NULL)) {
2897 if (CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 1, msg) != 0) {
2898 MSGM_syslog(LOG_ERR, "ERROR saving message pointer!\n");
2899 CtdlSaveMsgPointerInRoom(CtdlGetConfigStr("c_aideroom"), newmsgid, 0, msg);
2903 /* For internet mail, drop a copy in the outbound queue room */
2904 if ((recps != NULL) && (recps->num_internet > 0)) {
2905 CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, newmsgid, 0, msg);
2908 /* If other rooms are specified, drop them there too. */
2909 if ((recps != NULL) && (recps->num_room > 0))
2910 for (i=0; i<num_tokens(recps->recp_room, '|'); ++i) {
2911 extract_token(recipient, recps->recp_room, i,
2912 '|', sizeof recipient);
2913 MSG_syslog(LOG_DEBUG, "Delivering to room <%s>\n", recipient);///// xxxx
2914 CtdlSaveMsgPointerInRoom(recipient, newmsgid, 0, msg);
2917 /* Bump this user's messages posted counter. */
2918 MSGM_syslog(LOG_DEBUG, "Updating user\n");
2919 CtdlLockGetCurrentUser();
2920 CCC->user.posted = CCC->user.posted + 1;
2921 CtdlPutCurrentUserLock();
2923 /* Decide where bounces need to be delivered */
2924 if ((recps != NULL) && (recps->bounce_to == NULL))
2927 snprintf(bounce_to, sizeof bounce_to, "%s@%s",
2928 CCC->user.fullname, CtdlGetConfigStr("c_nodename"));
2930 snprintf(bounce_to, sizeof bounce_to, "%s@%s",
2931 msg->cm_fields[eAuthor], msg->cm_fields[eNodeName]);
2932 recps->bounce_to = bounce_to;
2935 CM_SetFieldLONG(msg, eVltMsgNum, newmsgid);
2938 /* If this is private, local mail, make a copy in the
2939 * recipient's mailbox and bump the reference count.
2941 if ((recps != NULL) && (recps->num_local > 0))
2946 pch = recps->recp_local;
2947 recps->recp_local = recipient;
2948 ntokens = num_tokens(pch, '|');
2949 for (i=0; i<ntokens; ++i)
2951 extract_token(recipient, pch, i, '|', sizeof recipient);
2952 MSG_syslog(LOG_DEBUG, "Delivering private local mail to <%s>\n", recipient);
2953 if (CtdlGetUser(&userbuf, recipient) == 0) {
2954 CtdlMailboxName(actual_rm, sizeof actual_rm, &userbuf, MAILROOM);
2955 CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0, msg);
2956 CtdlBumpNewMailCounter(userbuf.usernum);
2957 PerformMessageHooks(msg, recps, EVT_AFTERUSRMBOXSAVE);
2960 MSG_syslog(LOG_DEBUG, "No user <%s>\n", recipient);
2961 CtdlSaveMsgPointerInRoom(CtdlGetConfigStr("c_aideroom"), newmsgid, 0, msg);
2964 recps->recp_local = pch;
2967 /* Perform "after save" hooks */
2968 MSGM_syslog(LOG_DEBUG, "Performing after-save hooks\n");
2970 PerformMessageHooks(msg, recps, EVT_AFTERSAVE);
2971 CM_FlushField(msg, eVltMsgNum);
2973 /* Go back to the room we started from */
2974 MSG_syslog(LOG_DEBUG, "Returning to original room %s\n", hold_rm);
2975 if (strcasecmp(hold_rm, CCC->room.QRname))
2976 CtdlUserGoto(hold_rm, 0, 1, NULL, NULL, NULL, NULL);
2979 * Any addresses to harvest for someone's address book?
2981 if ( (CCC->logged_in) && (recps != NULL) ) {
2982 collected_addresses = harvest_collected_addresses(msg);
2985 if (collected_addresses != NULL) {
2986 aptr = (struct addresses_to_be_filed *)
2987 malloc(sizeof(struct addresses_to_be_filed));
2988 CtdlMailboxName(actual_rm, sizeof actual_rm,
2989 &CCC->user, USERCONTACTSROOM);
2990 aptr->roomname = strdup(actual_rm);
2991 aptr->collected_addresses = collected_addresses;
2992 begin_critical_section(S_ATBF);
2995 end_critical_section(S_ATBF);
2999 * Determine whether this message qualifies for journaling.
3001 if (!CM_IsEmpty(msg, eJournal)) {
3002 qualified_for_journaling = 0;
3005 if (recps == NULL) {
3006 qualified_for_journaling = CtdlGetConfigInt("c_journal_pubmsgs");
3008 else if (recps->num_local + recps->num_ignet + recps->num_internet > 0) {
3009 qualified_for_journaling = CtdlGetConfigInt("c_journal_email");
3012 qualified_for_journaling = CtdlGetConfigInt("c_journal_pubmsgs");
3017 * Do we have to perform journaling? If so, hand off the saved
3018 * RFC822 version will be handed off to the journaler for background
3019 * submit. Otherwise, we have to free the memory ourselves.
3021 if (saved_rfc822_version != NULL) {
3022 if (qualified_for_journaling) {
3023 JournalBackgroundSubmit(msg, saved_rfc822_version, recps);
3026 FreeStrBuf(&saved_rfc822_version);
3030 if ((recps != NULL) && (recps->bounce_to == bounce_to))
3031 recps->bounce_to = NULL;
3039 * Convenience function for generating small administrative messages.
3041 void quickie_message(const char *from,
3042 const char *fromaddr,
3047 const char *subject)
3049 struct CtdlMessage *msg;
3050 recptypes *recp = NULL;
3052 msg = malloc(sizeof(struct CtdlMessage));
3053 memset(msg, 0, sizeof(struct CtdlMessage));
3054 msg->cm_magic = CTDLMESSAGE_MAGIC;
3055 msg->cm_anon_type = MES_NORMAL;
3056 msg->cm_format_type = format_type;
3058 if (!IsEmptyStr(from)) {
3059 CM_SetField(msg, eAuthor, from, strlen(from));
3061 else if (!IsEmptyStr(fromaddr)) {
3063 CM_SetField(msg, eAuthor, fromaddr, strlen(fromaddr));
3064 pAt = strchr(msg->cm_fields[eAuthor], '@');
3066 CM_CutFieldAt(msg, eAuthor, pAt - msg->cm_fields[eAuthor]);
3070 msg->cm_fields[eAuthor] = strdup("Citadel");
3073 if (!IsEmptyStr(fromaddr)) CM_SetField(msg, erFc822Addr, fromaddr, strlen(fromaddr));
3074 if (!IsEmptyStr(room)) CM_SetField(msg, eOriginalRoom, room, strlen(room));
3075 CM_SetField(msg, eNodeName, CtdlGetConfigStr("c_nodename"), strlen(CtdlGetConfigStr("c_nodename")));
3076 if (!IsEmptyStr(to)) {
3077 CM_SetField(msg, eRecipient, to, strlen(to));
3078 recp = validate_recipients(to, NULL, 0);
3080 if (!IsEmptyStr(subject)) {
3081 CM_SetField(msg, eMsgSubject, subject, strlen(subject));
3083 if (!IsEmptyStr(text)) {
3084 CM_SetField(msg, eMesageText, text, strlen(text));
3087 CtdlSubmitMsg(msg, recp, room, 0);
3089 if (recp != NULL) free_recipients(recp);
3092 void flood_protect_quickie_message(const char *from,
3093 const char *fromaddr,
3098 const char *subject,
3100 const char **CritStr,
3101 const long *CritStrLen,
3107 u_char rawdigest[MD5_DIGEST_LEN];
3108 struct MD5Context md5context;
3112 static const time_t tsday = (8*60*60); /* just care for a day... */
3115 tslen = snprintf(timestamp, sizeof(timestamp), "%ld", tsday);
3116 MD5Init(&md5context);
3118 for (i = 0; i < nCriterions; i++)
3119 MD5Update(&md5context,
3120 (const unsigned char*)CritStr[i], CritStrLen[i]);
3121 MD5Update(&md5context,
3122 (const unsigned char*)timestamp, tslen);
3123 MD5Final(rawdigest, &md5context);
3125 guid = NewStrBufPlain(NULL,
3126 MD5_DIGEST_LEN * 2 + 12);
3127 StrBufHexEscAppend(guid, NULL, rawdigest, MD5_DIGEST_LEN);
3128 StrBufAppendBufPlain(guid, HKEY("_fldpt"), 0);
3129 if (StrLength(guid) > 40)
3130 StrBufCutAt(guid, 40, NULL);
3132 seenstamp = CheckIfAlreadySeen("FPAideMessage",
3139 if ((seenstamp > 0) && (seenstamp < tsday))
3142 /* yes, we did. flood protection kicks in. */
3144 "not sending message again - %ld < %ld \n", seenstamp, tsday);
3150 "sending message. %ld >= %ld", seenstamp, tsday);
3152 /* no, this message isn't sent recently; go ahead. */
3153 quickie_message(from,
3165 * Back end function used by CtdlMakeMessage() and similar functions
3167 StrBuf *CtdlReadMessageBodyBuf(char *terminator, /* token signalling EOT */
3169 size_t maxlen, /* maximum message length */
3170 StrBuf *exist, /* if non-null, append to it;
3171 exist is ALWAYS freed */
3172 int crlf, /* CRLF newlines instead of LF */
3173 int *sock /* socket handle or 0 for this session's client socket */
3182 LineBuf = NewStrBufPlain(NULL, SIZ);
3183 if (exist == NULL) {
3184 Message = NewStrBufPlain(NULL, 4 * SIZ);
3187 Message = NewStrBufDup(exist);
3190 /* Do we need to change leading ".." to "." for SMTP escaping? */
3191 if ((tlen == 1) && (*terminator == '.')) {
3195 /* read in the lines of message text one by one */
3198 if ((CtdlSockGetLine(sock, LineBuf, 5) < 0) ||
3203 if (CtdlClientGetLine(LineBuf) < 0) finished = 1;
3205 if ((StrLength(LineBuf) == tlen) &&
3206 (!strcmp(ChrPtr(LineBuf), terminator)))
3209 if ( (!flushing) && (!finished) ) {
3211 StrBufAppendBufPlain(LineBuf, HKEY("\r\n"), 0);
3214 StrBufAppendBufPlain(LineBuf, HKEY("\n"), 0);
3217 /* Unescape SMTP-style input of two dots at the beginning of the line */
3219 (StrLength(LineBuf) == 2) &&
3220 (!strcmp(ChrPtr(LineBuf), "..")))
3222 StrBufCutLeft(LineBuf, 1);
3225 StrBufAppendBuf(Message, LineBuf, 0);
3228 /* if we've hit the max msg length, flush the rest */
3229 if (StrLength(Message) >= maxlen) flushing = 1;
3231 } while (!finished);
3232 FreeStrBuf(&LineBuf);
3236 void DeleteAsyncMsg(ReadAsyncMsg **Msg)
3240 FreeStrBuf(&(*Msg)->MsgBuf);
3246 ReadAsyncMsg *NewAsyncMsg(const char *terminator, /* token signalling EOT */
3248 size_t maxlen, /* maximum message length */
3249 size_t expectlen, /* if we expect a message, how long should it be? */
3250 StrBuf *exist, /* if non-null, append to it;
3251 exist is ALWAYS freed */
3252 long eLen, /* length of exist */
3253 int crlf /* CRLF newlines instead of LF */
3256 ReadAsyncMsg *NewMsg;
3258 NewMsg = (ReadAsyncMsg *)malloc(sizeof(ReadAsyncMsg));
3259 memset(NewMsg, 0, sizeof(ReadAsyncMsg));
3261 if (exist == NULL) {
3264 if (expectlen == 0) {
3268 len = expectlen + 10;
3270 NewMsg->MsgBuf = NewStrBufPlain(NULL, len);
3273 NewMsg->MsgBuf = NewStrBufDup(exist);
3275 /* Do we need to change leading ".." to "." for SMTP escaping? */
3276 if ((tlen == 1) && (*terminator == '.')) {
3280 NewMsg->terminator = terminator;
3281 NewMsg->tlen = tlen;
3283 NewMsg->maxlen = maxlen;
3285 NewMsg->crlf = crlf;
3291 * Back end function used by CtdlMakeMessage() and similar functions
3293 eReadState CtdlReadMessageBodyAsync(AsyncIO *IO)
3295 ReadAsyncMsg *ReadMsg;
3296 int MsgFinished = 0;
3297 eReadState Finished = eMustReadMore;
3302 const char *pch = ChrPtr(IO->SendBuf.Buf);
3303 const char *pchh = IO->SendBuf.ReadWritePointer;
3309 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
3310 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
3311 ((CitContext*)(IO->CitContext))->ServiceName,
3314 fd = fopen(fn, "a+");
3316 syslog(LOG_EMERG, "failed to open file %s: %s", fn, strerror(errno));
3322 ReadMsg = IO->ReadMsg;
3324 /* read in the lines of message text one by one */
3326 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
3329 case eMustReadMore: /// read new from socket...
3331 if (IO->RecvBuf.ReadWritePointer != NULL) {
3332 nbytes = StrLength(IO->RecvBuf.Buf) - (IO->RecvBuf.ReadWritePointer - ChrPtr(IO->RecvBuf.Buf));
3333 fprintf(fd, "Read; Line unfinished: %ld Bytes still in buffer [", nbytes);
3335 fwrite(IO->RecvBuf.ReadWritePointer, nbytes, 1, fd);
3339 fprintf(fd, "BufferEmpty! \n");
3345 case eBufferNotEmpty: /* shouldn't happen... */
3346 case eReadSuccess: /// done for now...
3348 case eReadFail: /// WHUT?
3354 if ((StrLength(IO->IOBuf) == ReadMsg->tlen) &&
3355 (!strcmp(ChrPtr(IO->IOBuf), ReadMsg->terminator))) {
3358 fprintf(fd, "found Terminator; Message Size: %d\n", StrLength(ReadMsg->MsgBuf));
3361 else if (!ReadMsg->flushing) {
3364 fprintf(fd, "Read Line: [%d][%s]\n", StrLength(IO->IOBuf), ChrPtr(IO->IOBuf));
3367 /* Unescape SMTP-style input of two dots at the beginning of the line */
3368 if ((ReadMsg->dodot) &&
3369 (StrLength(IO->IOBuf) == 2) && /* TODO: do we just unescape lines with two dots or any line? */
3370 (!strcmp(ChrPtr(IO->IOBuf), "..")))
3373 fprintf(fd, "UnEscaped!\n");
3375 StrBufCutLeft(IO->IOBuf, 1);
3378 if (ReadMsg->crlf) {
3379 StrBufAppendBufPlain(IO->IOBuf, HKEY("\r\n"), 0);
3382 StrBufAppendBufPlain(IO->IOBuf, HKEY("\n"), 0);
3385 StrBufAppendBuf(ReadMsg->MsgBuf, IO->IOBuf, 0);
3388 /* if we've hit the max msg length, flush the rest */
3389 if (StrLength(ReadMsg->MsgBuf) >= ReadMsg->maxlen) ReadMsg->flushing = 1;
3391 } while (!MsgFinished);
3394 fprintf(fd, "Done with reading; %s.\n, ",
3395 (MsgFinished)?"Message Finished": "FAILED");
3399 return eReadSuccess;
3406 * Back end function used by CtdlMakeMessage() and similar functions
3408 char *CtdlReadMessageBody(char *terminator, /* token signalling EOT */
3410 size_t maxlen, /* maximum message length */
3411 StrBuf *exist, /* if non-null, append to it;
3412 exist is ALWAYS freed */
3413 int crlf, /* CRLF newlines instead of LF */
3414 int *sock /* socket handle or 0 for this session's client socket */
3419 Message = CtdlReadMessageBodyBuf(terminator,
3425 if (Message == NULL)
3428 return SmashStrBuf(&Message);
3431 struct CtdlMessage *CtdlMakeMessage(
3432 struct ctdluser *author, /* author's user structure */
3433 char *recipient, /* NULL if it's not mail */
3434 char *recp_cc, /* NULL if it's not mail */
3435 char *room, /* room where it's going */
3436 int type, /* see MES_ types in header file */
3437 int format_type, /* variformat, plain text, MIME... */
3438 char *fake_name, /* who we're masquerading as */
3439 char *my_email, /* which of my email addresses to use (empty is ok) */
3440 char *subject, /* Subject (optional) */
3441 char *supplied_euid, /* ...or NULL if this is irrelevant */
3442 char *preformatted_text, /* ...or NULL to read text from client */
3443 char *references /* Thread references */
3446 return CtdlMakeMessageLen(
3447 author, /* author's user structure */
3448 recipient, /* NULL if it's not mail */
3449 (recipient)?strlen(recipient) : 0,
3450 recp_cc, /* NULL if it's not mail */
3451 (recp_cc)?strlen(recp_cc): 0,
3452 room, /* room where it's going */
3453 (room)?strlen(room): 0,
3454 type, /* see MES_ types in header file */
3455 format_type, /* variformat, plain text, MIME... */
3456 fake_name, /* who we're masquerading as */
3457 (fake_name)?strlen(fake_name): 0,
3458 my_email, /* which of my email addresses to use (empty is ok) */
3459 (my_email)?strlen(my_email): 0,
3460 subject, /* Subject (optional) */
3461 (subject)?strlen(subject): 0,
3462 supplied_euid, /* ...or NULL if this is irrelevant */
3463 (supplied_euid)?strlen(supplied_euid):0,
3464 preformatted_text, /* ...or NULL to read text from client */
3465 (preformatted_text)?strlen(preformatted_text) : 0,
3466 references, /* Thread references */
3467 (references)?strlen(references):0);
3472 * Build a binary message to be saved on disk.
3473 * (NOTE: if you supply 'preformatted_text', the buffer you give it
3474 * will become part of the message. This means you are no longer
3475 * responsible for managing that memory -- it will be freed along with
3476 * the rest of the fields when CM_Free() is called.)
3479 struct CtdlMessage *CtdlMakeMessageLen(
3480 struct ctdluser *author, /* author's user structure */
3481 char *recipient, /* NULL if it's not mail */
3483 char *recp_cc, /* NULL if it's not mail */
3485 char *room, /* room where it's going */
3487 int type, /* see MES_ types in header file */
3488 int format_type, /* variformat, plain text, MIME... */
3489 char *fake_name, /* who we're masquerading as */
3491 char *my_email, /* which of my email addresses to use (empty is ok) */
3493 char *subject, /* Subject (optional) */
3495 char *supplied_euid, /* ...or NULL if this is irrelevant */
3497 char *preformatted_text, /* ...or NULL to read text from client */
3499 char *references, /* Thread references */
3503 struct CitContext *CCC = CC;
3504 /* Don't confuse the poor folks if it's not routed mail. * /
3505 char dest_node[256] = "";*/
3508 struct CtdlMessage *msg;
3510 StrBuf *FakeEncAuthor = NULL;
3512 msg = malloc(sizeof(struct CtdlMessage));
3513 memset(msg, 0, sizeof(struct CtdlMessage));
3514 msg->cm_magic = CTDLMESSAGE_MAGIC;
3515 msg->cm_anon_type = type;
3516 msg->cm_format_type = format_type;
3518 if (recipient != NULL) rcplen = striplt(recipient);
3519 if (recp_cc != NULL) cclen = striplt(recp_cc);
3521 /* Path or Return-Path */
3523 CM_SetField(msg, eMessagePath, my_email, myelen);
3525 else if (!IsEmptyStr(author->fullname)) {
3526 CM_SetField(msg, eMessagePath, author->fullname, strlen(author->fullname));
3528 convert_spaces_to_underscores(msg->cm_fields[eMessagePath]);
3530 blen = snprintf(buf, sizeof buf, "%ld", (long)time(NULL));
3531 CM_SetField(msg, eTimestamp, buf, blen);
3534 FakeAuthor = NewStrBufPlain (fake_name, fnlen);
3537 FakeAuthor = NewStrBufPlain (author->fullname, -1);
3539 StrBufRFC2047encode(&FakeEncAuthor, FakeAuthor);
3540 CM_SetAsFieldSB(msg, eAuthor, &FakeEncAuthor);
3541 FreeStrBuf(&FakeAuthor);
3543 if (!!IsEmptyStr(CCC->room.QRname)) {
3544 if (CCC->room.QRflags & QR_MAILBOX) { /* room */
3545 CM_SetField(msg, eOriginalRoom, &CCC->room.QRname[11], strlen(&CCC->room.QRname[11]));
3548 CM_SetField(msg, eOriginalRoom, CCC->room.QRname, strlen(CCC->room.QRname));
3552 CM_SetField(msg, eNodeName, CtdlGetConfigStr("c_nodename"), strlen(CtdlGetConfigStr("c_nodename")));
3553 CM_SetField(msg, eHumanNode, CtdlGetConfigStr("c_humannode"), strlen(CtdlGetConfigStr("c_humannode")));
3556 CM_SetField(msg, eRecipient, recipient, rcplen);
3559 CM_SetField(msg, eCarbonCopY, recp_cc, cclen);
3563 CM_SetField(msg, erFc822Addr, my_email, myelen);
3565 else if ( (author == &CCC->user) && (!IsEmptyStr(CCC->cs_inet_email)) ) {
3566 CM_SetField(msg, erFc822Addr, CCC->cs_inet_email, strlen(CCC->cs_inet_email));
3569 if (subject != NULL) {
3571 length = striplt(subject);
3577 while ((subject[i] != '\0') &&
3578 (IsAscii = isascii(subject[i]) != 0 ))
3581 CM_SetField(msg, eMsgSubject, subject, subjlen);
3582 else /* ok, we've got utf8 in the string. */
3585 rfc2047Subj = rfc2047encode(subject, length);
3586 CM_SetAsField(msg, eMsgSubject, &rfc2047Subj, strlen(rfc2047Subj));
3593 CM_SetField(msg, eExclusiveID, supplied_euid, euidlen);
3597 CM_SetField(msg, eWeferences, references, reflen);
3600 if (preformatted_text != NULL) {
3601 CM_SetField(msg, eMesageText, preformatted_text, textlen);
3605 MsgBody = CtdlReadMessageBodyBuf(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0, 0);
3606 if (MsgBody != NULL) {
3607 CM_SetAsFieldSB(msg, eMesageText, &MsgBody);
3618 * API function to delete messages which match a set of criteria
3619 * (returns the actual number of messages deleted)
3621 int CtdlDeleteMessages(const char *room_name, /* which room */
3622 long *dmsgnums, /* array of msg numbers to be deleted */
3623 int num_dmsgnums, /* number of msgs to be deleted, or 0 for "any" */
3624 char *content_type /* or "" for any. regular expressions expected. */
3627 struct CitContext *CCC = CC;
3628 struct ctdlroom qrbuf;
3629 struct cdbdata *cdbfr;
3630 long *msglist = NULL;
3631 long *dellist = NULL;
3634 int num_deleted = 0;
3636 struct MetaData smi;
3639 int need_to_free_re = 0;
3641 if (content_type) if (!IsEmptyStr(content_type)) {
3642 regcomp(&re, content_type, 0);
3643 need_to_free_re = 1;
3645 MSG_syslog(LOG_DEBUG, " CtdlDeleteMessages(%s, %d msgs, %s)\n",
3646 room_name, num_dmsgnums, content_type);
3648 /* get room record, obtaining a lock... */
3649 if (CtdlGetRoomLock(&qrbuf, room_name) != 0) {
3650 MSG_syslog(LOG_ERR, " CtdlDeleteMessages(): Room <%s> not found\n",
3652 if (need_to_free_re) regfree(&re);
3653 return (0); /* room not found */
3655 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
3657 if (cdbfr != NULL) {
3658 dellist = malloc(cdbfr->len);
3659 msglist = (long *) cdbfr->ptr;
3660 cdbfr->ptr = NULL; /* CtdlDeleteMessages() now owns this memory */
3661 num_msgs = cdbfr->len / sizeof(long);
3665 int have_contenttype = (content_type != NULL) && !IsEmptyStr(content_type);
3666 int have_delmsgs = (num_dmsgnums == 0) || (dmsgnums == NULL);
3667 int have_more_del = 1;
3669 num_msgs = sort_msglist(msglist, num_msgs);
3670 if (num_dmsgnums > 1)
3671 num_dmsgnums = sort_msglist(dmsgnums, num_dmsgnums);
3674 StrBuf *dbg = NewStrBuf();
3675 for (i = 0; i < num_dmsgnums; i++)
3676 StrBufAppendPrintf(dbg, ", %ld", dmsgnums[i]);
3677 MSG_syslog(LOG_DEBUG, " Deleting before: %s", ChrPtr(dbg));
3682 while ((i < num_msgs) && (have_more_del)) {
3685 /* Set/clear a bit for each criterion */
3687 /* 0 messages in the list or a null list means that we are
3688 * interested in deleting any messages which meet the other criteria.
3691 delete_this |= 0x01;
3694 while ((i < num_msgs) && (msglist[i] < dmsgnums[j])) i++;
3699 if (msglist[i] == dmsgnums[j]) {
3700 delete_this |= 0x01;
3703 have_more_del = (j < num_dmsgnums);
3706 if (have_contenttype) {
3707 GetMetaData(&smi, msglist[i]);
3708 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) == 0) {
3709 delete_this |= 0x02;
3712 delete_this |= 0x02;
3715 /* Delete message only if all bits are set */
3716 if (delete_this == 0x03) {
3717 dellist[num_deleted++] = msglist[i];
3724 StrBuf *dbg = NewStrBuf();
3725 for (i = 0; i < num_deleted; i++)
3726 StrBufAppendPrintf(dbg, ", %ld", dellist[i]);
3727 MSG_syslog(LOG_DEBUG, " Deleting: %s", ChrPtr(dbg));
3731 num_msgs = sort_msglist(msglist, num_msgs);
3732 cdb_store(CDB_MSGLISTS, &qrbuf.QRnumber, (int)sizeof(long),
3733 msglist, (int)(num_msgs * sizeof(long)));
3736 qrbuf.QRhighest = msglist[num_msgs - 1];
3738 qrbuf.QRhighest = 0;
3740 CtdlPutRoomLock(&qrbuf);
3742 /* Go through the messages we pulled out of the index, and decrement
3743 * their reference counts by 1. If this is the only room the message
3744 * was in, the reference count will reach zero and the message will
3745 * automatically be deleted from the database. We do this in a
3746 * separate pass because there might be plug-in hooks getting called,
3747 * and we don't want that happening during an S_ROOMS critical
3751 for (i=0; i<num_deleted; ++i) {
3752 PerformDeleteHooks(qrbuf.QRname, dellist[i]);
3754 AdjRefCountList(dellist, num_deleted, -1);
3756 /* Now free the memory we used, and go away. */
3757 if (msglist != NULL) free(msglist);
3758 if (dellist != NULL) free(dellist);
3759 MSG_syslog(LOG_DEBUG, " %d message(s) deleted.\n", num_deleted);
3760 if (need_to_free_re) regfree(&re);
3761 return (num_deleted);
3768 * GetMetaData() - Get the supplementary record for a message
3770 void GetMetaData(struct MetaData *smibuf, long msgnum)
3773 struct cdbdata *cdbsmi;
3776 memset(smibuf, 0, sizeof(struct MetaData));
3777 smibuf->meta_msgnum = msgnum;
3778 smibuf->meta_refcount = 1; /* Default reference count is 1 */
3780 /* Use the negative of the message number for its supp record index */
3781 TheIndex = (0L - msgnum);
3783 cdbsmi = cdb_fetch(CDB_MSGMAIN, &TheIndex, sizeof(long));
3784 if (cdbsmi == NULL) {
3785 return; /* record not found; go with defaults */
3787 memcpy(smibuf, cdbsmi->ptr,
3788 ((cdbsmi->len > sizeof(struct MetaData)) ?
3789 sizeof(struct MetaData) : cdbsmi->len));
3796 * PutMetaData() - (re)write supplementary record for a message
3798 void PutMetaData(struct MetaData *smibuf)
3802 /* Use the negative of the message number for the metadata db index */
3803 TheIndex = (0L - smibuf->meta_msgnum);
3805 cdb_store(CDB_MSGMAIN,
3806 &TheIndex, (int)sizeof(long),
3807 smibuf, (int)sizeof(struct MetaData));
3812 * AdjRefCount - submit an adjustment to the reference count for a message.
3813 * (These are just queued -- we actually process them later.)
3815 void AdjRefCount(long msgnum, int incr)
3817 struct CitContext *CCC = CC;
3818 struct arcq new_arcq;
3821 MSG_syslog(LOG_DEBUG, "AdjRefCount() msg %ld ref count delta %+d\n", msgnum, incr);
3823 begin_critical_section(S_SUPPMSGMAIN);
3824 if (arcfp == NULL) {
3825 arcfp = fopen(file_arcq, "ab+");
3826 chown(file_arcq, CTDLUID, (-1));
3827 chmod(file_arcq, 0600);
3829 end_critical_section(S_SUPPMSGMAIN);
3831 /* msgnum < 0 means that we're trying to close the file */
3833 MSGM_syslog(LOG_DEBUG, "Closing the AdjRefCount queue file\n");
3834 begin_critical_section(S_SUPPMSGMAIN);
3835 if (arcfp != NULL) {
3839 end_critical_section(S_SUPPMSGMAIN);
3844 * If we can't open the queue, perform the operation synchronously.
3846 if (arcfp == NULL) {
3847 TDAP_AdjRefCount(msgnum, incr);
3851 new_arcq.arcq_msgnum = msgnum;
3852 new_arcq.arcq_delta = incr;
3853 rv = fwrite(&new_arcq, sizeof(struct arcq), 1, arcfp);
3855 MSG_syslog(LOG_EMERG, "Couldn't write Refcount Queue File %s: %s\n",
3864 void AdjRefCountList(long *msgnum, long nmsg, int incr)
3866 struct CitContext *CCC = CC;
3867 long i, the_size, offset;
3868 struct arcq *new_arcq;
3871 MSG_syslog(LOG_DEBUG, "AdjRefCountList() msg %ld ref count delta %+d\n", nmsg, incr);
3873 begin_critical_section(S_SUPPMSGMAIN);
3874 if (arcfp == NULL) {
3875 arcfp = fopen(file_arcq, "ab+");
3876 chown(file_arcq, CTDLUID, (-1));
3877 chmod(file_arcq, 0600);
3879 end_critical_section(S_SUPPMSGMAIN);
3882 * If we can't open the queue, perform the operation synchronously.
3884 if (arcfp == NULL) {
3885 for (i = 0; i < nmsg; i++)
3886 TDAP_AdjRefCount(msgnum[i], incr);
3890 the_size = sizeof(struct arcq) * nmsg;
3891 new_arcq = malloc(the_size);
3892 for (i = 0; i < nmsg; i++) {
3893 new_arcq[i].arcq_msgnum = msgnum[i];
3894 new_arcq[i].arcq_delta = incr;
3898 while ((rv >= 0) && (offset < the_size))
3900 rv = fwrite(new_arcq + offset, 1, the_size - offset, arcfp);
3902 MSG_syslog(LOG_EMERG, "Couldn't write Refcount Queue File %s: %s\n",
3918 * TDAP_ProcessAdjRefCountQueue()
3920 * Process the queue of message count adjustments that was created by calls
3921 * to AdjRefCount() ... by reading the queue and calling TDAP_AdjRefCount()
3922 * for each one. This should be an "off hours" operation.
3924 int TDAP_ProcessAdjRefCountQueue(void)
3926 struct CitContext *CCC = CC;
3927 char file_arcq_temp[PATH_MAX];
3930 struct arcq arcq_rec;
3931 int num_records_processed = 0;
3933 snprintf(file_arcq_temp, sizeof file_arcq_temp, "%s.%04x", file_arcq, rand());
3935 begin_critical_section(S_SUPPMSGMAIN);
3936 if (arcfp != NULL) {
3941 r = link(file_arcq, file_arcq_temp);
3943 MSG_syslog(LOG_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
3944 end_critical_section(S_SUPPMSGMAIN);
3945 return(num_records_processed);
3949 end_critical_section(S_SUPPMSGMAIN);
3951 fp = fopen(file_arcq_temp, "rb");
3953 MSG_syslog(LOG_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
3954 return(num_records_processed);
3957 while (fread(&arcq_rec, sizeof(struct arcq), 1, fp) == 1) {
3958 TDAP_AdjRefCount(arcq_rec.arcq_msgnum, arcq_rec.arcq_delta);
3959 ++num_records_processed;
3963 r = unlink(file_arcq_temp);
3965 MSG_syslog(LOG_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
3968 return(num_records_processed);
3974 * TDAP_AdjRefCount - adjust the reference count for a message.
3975 * This one does it "for real" because it's called by
3976 * the autopurger function that processes the queue
3977 * created by AdjRefCount(). If a message's reference
3978 * count becomes zero, we also delete the message from
3979 * disk and de-index it.
3981 void TDAP_AdjRefCount(long msgnum, int incr)
3983 struct CitContext *CCC = CC;
3985 struct MetaData smi;
3988 /* This is a *tight* critical section; please keep it that way, as
3989 * it may get called while nested in other critical sections.
3990 * Complicating this any further will surely cause deadlock!
3992 begin_critical_section(S_SUPPMSGMAIN);
3993 GetMetaData(&smi, msgnum);
3994 smi.meta_refcount += incr;
3996 end_critical_section(S_SUPPMSGMAIN);
3997 MSG_syslog(LOG_DEBUG, "TDAP_AdjRefCount() msg %ld ref count delta %+d, is now %d\n",
3998 msgnum, incr, smi.meta_refcount
4001 /* If the reference count is now zero, delete the message
4002 * (and its supplementary record as well).
4004 if (smi.meta_refcount == 0) {
4005 MSG_syslog(LOG_DEBUG, "Deleting message <%ld>\n", msgnum);
4007 /* Call delete hooks with NULL room to show it has gone altogether */
4008 PerformDeleteHooks(NULL, msgnum);
4010 /* Remove from message base */
4012 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
4013 cdb_delete(CDB_BIGMSGS, &delnum, (int)sizeof(long));
4015 /* Remove metadata record */
4016 delnum = (0L - msgnum);
4017 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
4023 * Write a generic object to this room
4025 * Note: this could be much more efficient. Right now we use two temporary
4026 * files, and still pull the message into memory as with all others.
4028 void CtdlWriteObject(char *req_room, /* Room to stuff it in */
4029 char *content_type, /* MIME type of this object */
4030 char *raw_message, /* Data to be written */
4031 off_t raw_length, /* Size of raw_message */
4032 struct ctdluser *is_mailbox, /* Mailbox room? */
4033 int is_binary, /* Is encoding necessary? */
4034 int is_unique, /* Del others of this type? */
4035 unsigned int flags /* Internal save flags */
4038 struct CitContext *CCC = CC;
4039 struct ctdlroom qrbuf;
4040 char roomname[ROOMNAMELEN];
4041 struct CtdlMessage *msg;
4042 StrBuf *encoded_message = NULL;
4044 if (is_mailbox != NULL) {
4045 CtdlMailboxName(roomname, sizeof roomname, is_mailbox, req_room);
4048 safestrncpy(roomname, req_room, sizeof(roomname));
4051 MSG_syslog(LOG_DEBUG, "Raw length is %ld\n", (long)raw_length);
4054 encoded_message = NewStrBufPlain(NULL, (size_t) (((raw_length * 134) / 100) + 4096 ) );
4057 encoded_message = NewStrBufPlain(NULL, (size_t)(raw_length + 4096));
4060 StrBufAppendBufPlain(encoded_message, HKEY("Content-type: "), 0);
4061 StrBufAppendBufPlain(encoded_message, content_type, -1, 0);
4062 StrBufAppendBufPlain(encoded_message, HKEY("\n"), 0);
4065 StrBufAppendBufPlain(encoded_message, HKEY("Content-transfer-encoding: base64\n\n"), 0);
4068 StrBufAppendBufPlain(encoded_message, HKEY("Content-transfer-encoding: 7bit\n\n"), 0);
4072 StrBufBase64Append(encoded_message, NULL, raw_message, raw_length, 0);
4075 StrBufAppendBufPlain(encoded_message, raw_message, raw_length, 0);
4078 MSGM_syslog(LOG_DEBUG, "Allocating\n");
4079 msg = malloc(sizeof(struct CtdlMessage));
4080 memset(msg, 0, sizeof(struct CtdlMessage));
4081 msg->cm_magic = CTDLMESSAGE_MAGIC;
4082 msg->cm_anon_type = MES_NORMAL;
4083 msg->cm_format_type = 4;
4084 CM_SetField(msg, eAuthor, CCC->user.fullname, strlen(CCC->user.fullname));
4085 CM_SetField(msg, eOriginalRoom, req_room, strlen(req_room));
4086 CM_SetField(msg, eNodeName, CtdlGetConfigStr("c_nodename"), strlen(CtdlGetConfigStr("c_nodename")));
4087 CM_SetField(msg, eHumanNode, CtdlGetConfigStr("c_humannode"), strlen(CtdlGetConfigStr("c_humannode")));
4088 msg->cm_flags = flags;
4090 CM_SetAsFieldSB(msg, eMesageText, &encoded_message);
4092 /* Create the requested room if we have to. */
4093 if (CtdlGetRoom(&qrbuf, roomname) != 0) {
4094 CtdlCreateRoom(roomname,
4095 ( (is_mailbox != NULL) ? 5 : 3 ),
4096 "", 0, 1, 0, VIEW_BBS);
4098 /* If the caller specified this object as unique, delete all
4099 * other objects of this type that are currently in the room.
4102 MSG_syslog(LOG_DEBUG, "Deleted %d other msgs of this type\n",
4103 CtdlDeleteMessages(roomname, NULL, 0, content_type)
4106 /* Now write the data */
4107 CtdlSubmitMsg(msg, NULL, roomname, 0);
4113 /*****************************************************************************/
4114 /* MODULE INITIALIZATION STUFF */
4115 /*****************************************************************************/
4116 void SetMessageDebugEnabled(const int n)
4118 MessageDebugEnabled = n;
4120 CTDL_MODULE_INIT(msgbase)
4123 CtdlRegisterDebugFlagHook(HKEY("messages"), SetMessageDebugEnabled, &MessageDebugEnabled);
4126 /* return our Subversion id for the Log */