2 * Implements the message store.
4 * Copyright (c) 1987-2012 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
26 # include <sys/time.h>
39 #include <sys/types.h>
44 #include <libcitadel.h>
47 #include "serv_extensions.h"
51 #include "sysdep_decls.h"
52 #include "citserver.h"
59 #include "internet_addressing.h"
60 #include "euidindex.h"
61 #include "journaling.h"
62 #include "citadel_dirs.h"
63 #include "clientsocket.h"
66 #include "ctdl_module.h"
68 struct addresses_to_be_filed *atbf = NULL;
70 /* This temp file holds the queue of operations for AdjRefCount() */
71 static FILE *arcfp = NULL;
72 void AdjRefCountList(long *msgnum, long nmsg, int incr);
74 int MessageDebugEnabled = 0;
77 * These are the four-character field headers we use when outputting
78 * messages in Citadel format (as opposed to RFC822 format).
81 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
82 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
83 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
84 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
85 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
86 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
87 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
88 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
118 eMsgField FieldOrder[] = {
119 /* Important fields */
130 /* Semi-important fields */
136 /* G is not used yet, may become virus signature*/
139 /* Q is not used yet */
142 /* X is not used yet */
143 /* Z is not used yet */
150 /* Message text (MUST be last) */
152 /* Not saved to disk:
157 static const long NDiskFields = sizeof(FieldOrder) / sizeof(eMsgField);
159 int CM_IsEmpty(struct CtdlMessage *Msg, eMsgField which)
161 return !((Msg->cm_fields[which] != NULL) &&
162 (Msg->cm_fields[which][0] != '\0'));
165 void CM_SetField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
167 if (Msg->cm_fields[which] != NULL)
168 free (Msg->cm_fields[which]);
169 Msg->cm_fields[which] = malloc(length + 1);
170 memcpy(Msg->cm_fields[which], buf, length);
171 Msg->cm_fields[which][length] = '\0';
174 void CM_SetFieldLONG(struct CtdlMessage *Msg, eMsgField which, long lvalue)
178 len = snprintf(buf, sizeof(buf), "%ld", lvalue);
179 CM_SetField(Msg, which, buf, len);
181 void CM_CutFieldAt(struct CtdlMessage *Msg, eMsgField WhichToCut, long maxlen)
183 if (Msg->cm_fields[WhichToCut] == NULL)
186 if (strlen(Msg->cm_fields[WhichToCut]) > maxlen)
187 Msg->cm_fields[WhichToCut][maxlen] = '\0';
190 void CM_FlushField(struct CtdlMessage *Msg, eMsgField which)
192 if (Msg->cm_fields[which] != NULL)
193 free (Msg->cm_fields[which]);
194 Msg->cm_fields[which] = NULL;
196 void CM_Flush(struct CtdlMessage *Msg)
200 if (CM_IsValidMsg(Msg) == 0)
203 for (i = 0; i < 256; ++i)
205 CM_FlushField(Msg, i);
209 void CM_CopyField(struct CtdlMessage *Msg, eMsgField WhichToPutTo, eMsgField WhichtToCopy)
212 if (Msg->cm_fields[WhichToPutTo] != NULL)
213 free (Msg->cm_fields[WhichToPutTo]);
215 if (Msg->cm_fields[WhichtToCopy] != NULL)
217 len = strlen(Msg->cm_fields[WhichtToCopy]);
218 Msg->cm_fields[WhichToPutTo] = malloc(len + 1);
219 memcpy(Msg->cm_fields[WhichToPutTo], Msg->cm_fields[WhichtToCopy], len);
220 Msg->cm_fields[WhichToPutTo][len] = '\0';
223 Msg->cm_fields[WhichToPutTo] = NULL;
227 void CM_PrependToField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
229 if (Msg->cm_fields[which] != NULL) {
234 oldmsgsize = strlen(Msg->cm_fields[which]) + 1;
235 newmsgsize = length + oldmsgsize;
237 new = malloc(newmsgsize);
238 memcpy(new, buf, length);
239 memcpy(new + length, Msg->cm_fields[which], oldmsgsize);
240 free(Msg->cm_fields[which]);
241 Msg->cm_fields[which] = new;
244 Msg->cm_fields[which] = malloc(length + 1);
245 memcpy(Msg->cm_fields[which], buf, length);
246 Msg->cm_fields[which][length] = '\0';
250 void CM_SetAsField(struct CtdlMessage *Msg, eMsgField which, char **buf, long length)
252 if (Msg->cm_fields[which] != NULL)
253 free (Msg->cm_fields[which]);
255 Msg->cm_fields[which] = *buf;
259 void CM_SetAsFieldSB(struct CtdlMessage *Msg, eMsgField which, StrBuf **buf)
261 if (Msg->cm_fields[which] != NULL)
262 free (Msg->cm_fields[which]);
264 Msg->cm_fields[which] = SmashStrBuf(buf);
267 void CM_GetAsField(struct CtdlMessage *Msg, eMsgField which, char **ret, long *retlen)
269 if (Msg->cm_fields[which] != NULL)
271 *retlen = strlen(Msg->cm_fields[which]);
272 *ret = Msg->cm_fields[which];
273 Msg->cm_fields[which] = NULL;
283 * Returns 1 if the supplied pointer points to a valid Citadel message.
284 * If the pointer is NULL or the magic number check fails, returns 0.
286 int CM_IsValidMsg(struct CtdlMessage *msg) {
289 if ((msg->cm_magic) != CTDLMESSAGE_MAGIC) {
290 struct CitContext *CCC = CC;
291 MSGM_syslog(LOG_WARNING, "CM_IsValidMsg() -- self-check failed\n");
297 void CM_FreeContents(struct CtdlMessage *msg)
301 for (i = 0; i < 256; ++i)
302 if (msg->cm_fields[i] != NULL) {
303 free(msg->cm_fields[i]);
306 msg->cm_magic = 0; /* just in case */
309 * 'Destructor' for struct CtdlMessage
311 void CM_Free(struct CtdlMessage *msg)
313 if (CM_IsValidMsg(msg) == 0)
315 if (msg != NULL) free (msg);
318 CM_FreeContents(msg);
322 int CM_DupField(eMsgField i, struct CtdlMessage *OrgMsg, struct CtdlMessage *NewMsg)
325 len = strlen(OrgMsg->cm_fields[i]);
326 NewMsg->cm_fields[i] = malloc(len + 1);
327 if (NewMsg->cm_fields[i] == NULL)
329 memcpy(NewMsg->cm_fields[i], OrgMsg->cm_fields[i], len);
330 NewMsg->cm_fields[i][len] = '\0';
334 struct CtdlMessage * CM_Duplicate(struct CtdlMessage *OrgMsg)
337 struct CtdlMessage *NewMsg;
339 if (CM_IsValidMsg(OrgMsg) == 0)
341 NewMsg = (struct CtdlMessage *)malloc(sizeof(struct CtdlMessage));
345 memcpy(NewMsg, OrgMsg, sizeof(struct CtdlMessage));
347 memset(&NewMsg->cm_fields, 0, sizeof(char*) * 256);
349 for (i = 0; i < 256; ++i)
351 if (OrgMsg->cm_fields[i] != NULL)
353 if (!CM_DupField(i, OrgMsg, NewMsg))
368 /* Determine if a given message matches the fields in a message template.
369 * Return 0 for a successful match.
371 int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template) {
374 /* If there aren't any fields in the template, all messages will
377 if (template == NULL) return(0);
379 /* Null messages are bogus. */
380 if (msg == NULL) return(1);
382 for (i='A'; i<='Z'; ++i) {
383 if (template->cm_fields[i] != NULL) {
384 if (msg->cm_fields[i] == NULL) {
385 /* Considered equal if temmplate is empty string */
386 if (IsEmptyStr(template->cm_fields[i])) continue;
389 if (strcasecmp(msg->cm_fields[i],
390 template->cm_fields[i])) return 1;
394 /* All compares succeeded: we have a match! */
401 * Retrieve the "seen" message list for the current room.
403 void CtdlGetSeen(char *buf, int which_set) {
404 struct CitContext *CCC = CC;
407 /* Learn about the user and room in question */
408 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
410 if (which_set == ctdlsetseen_seen)
411 safestrncpy(buf, vbuf.v_seen, SIZ);
412 if (which_set == ctdlsetseen_answered)
413 safestrncpy(buf, vbuf.v_answered, SIZ);
419 * Manipulate the "seen msgs" string (or other message set strings)
421 void CtdlSetSeen(long *target_msgnums, int num_target_msgnums,
422 int target_setting, int which_set,
423 struct ctdluser *which_user, struct ctdlroom *which_room) {
424 struct CitContext *CCC = CC;
425 struct cdbdata *cdbfr;
430 long hi = (-1L); /// TODO: we just write here. y?
439 char *is_set; /* actually an array of booleans */
441 /* Don't bother doing *anything* if we were passed a list of zero messages */
442 if (num_target_msgnums < 1) {
446 /* If no room was specified, we go with the current room. */
448 which_room = &CCC->room;
451 /* If no user was specified, we go with the current user. */
453 which_user = &CCC->user;
456 MSG_syslog(LOG_DEBUG, "CtdlSetSeen(%d msgs starting with %ld, %s, %d) in <%s>\n",
457 num_target_msgnums, target_msgnums[0],
458 (target_setting ? "SET" : "CLEAR"),
462 /* Learn about the user and room in question */
463 CtdlGetRelationship(&vbuf, which_user, which_room);
465 /* Load the message list */
466 cdbfr = cdb_fetch(CDB_MSGLISTS, &which_room->QRnumber, sizeof(long));
468 msglist = (long *) cdbfr->ptr;
469 cdbfr->ptr = NULL; /* CtdlSetSeen() now owns this memory */
470 num_msgs = cdbfr->len / sizeof(long);
473 return; /* No messages at all? No further action. */
476 is_set = malloc(num_msgs * sizeof(char));
477 memset(is_set, 0, (num_msgs * sizeof(char)) );
479 /* Decide which message set we're manipulating */
481 case ctdlsetseen_seen:
482 vset = NewStrBufPlain(vbuf.v_seen, -1);
484 case ctdlsetseen_answered:
485 vset = NewStrBufPlain(vbuf.v_answered, -1);
492 #if 0 /* This is a special diagnostic section. Do not allow it to run during normal operation. */
493 MSG_syslog(LOG_DEBUG, "There are %d messages in the room.\n", num_msgs);
494 for (i=0; i<num_msgs; ++i) {
495 if ((i > 0) && (msglist[i] <= msglist[i-1])) abort();
497 MSG_syslog(LOG_DEBUG, "We are twiddling %d of them.\n", num_target_msgnums);
498 for (k=0; k<num_target_msgnums; ++k) {
499 if ((k > 0) && (target_msgnums[k] <= target_msgnums[k-1])) abort();
503 MSG_syslog(LOG_DEBUG, "before update: %s\n", ChrPtr(vset));
505 /* Translate the existing sequence set into an array of booleans */
506 setstr = NewStrBuf();
510 while (StrBufExtract_NextToken(setstr, vset, &pvset, ',') >= 0) {
512 StrBufExtract_token(lostr, setstr, 0, ':');
513 if (StrBufNum_tokens(setstr, ':') >= 2) {
514 StrBufExtract_token(histr, setstr, 1, ':');
518 StrBufAppendBuf(histr, lostr, 0);
521 if (!strcmp(ChrPtr(histr), "*")) {
528 for (i = 0; i < num_msgs; ++i) {
529 if ((msglist[i] >= lo) && (msglist[i] <= hi)) {
539 /* Now translate the array of booleans back into a sequence set */
545 for (i=0; i<num_msgs; ++i) {
549 for (k=0; k<num_target_msgnums; ++k) {
550 if (msglist[i] == target_msgnums[k]) {
551 is_seen = target_setting;
555 if ((was_seen == 0) && (is_seen == 1)) {
558 else if ((was_seen == 1) && (is_seen == 0)) {
561 if (StrLength(vset) > 0) {
562 StrBufAppendBufPlain(vset, HKEY(","), 0);
565 StrBufAppendPrintf(vset, "%ld", hi);
568 StrBufAppendPrintf(vset, "%ld:%ld", lo, hi);
572 if ((is_seen) && (i == num_msgs - 1)) {
573 if (StrLength(vset) > 0) {
574 StrBufAppendBufPlain(vset, HKEY(","), 0);
576 if ((i==0) || (was_seen == 0)) {
577 StrBufAppendPrintf(vset, "%ld", msglist[i]);
580 StrBufAppendPrintf(vset, "%ld:%ld", lo, msglist[i]);
588 * We will have to stuff this string back into a 4096 byte buffer, so if it's
589 * larger than that now, truncate it by removing tokens from the beginning.
590 * The limit of 100 iterations is there to prevent an infinite loop in case
591 * something unexpected happens.
593 int number_of_truncations = 0;
594 while ( (StrLength(vset) > SIZ) && (number_of_truncations < 100) ) {
595 StrBufRemove_token(vset, 0, ',');
596 ++number_of_truncations;
600 * If we're truncating the sequence set of messages marked with the 'seen' flag,
601 * we want the earliest messages (the truncated ones) to be marked, not unmarked.
602 * Otherwise messages at the beginning will suddenly appear to be 'unseen'.
604 if ( (which_set == ctdlsetseen_seen) && (number_of_truncations > 0) ) {
606 first_tok = NewStrBuf();
607 StrBufExtract_token(first_tok, vset, 0, ',');
608 StrBufRemove_token(vset, 0, ',');
610 if (StrBufNum_tokens(first_tok, ':') > 1) {
611 StrBufRemove_token(first_tok, 0, ':');
615 new_set = NewStrBuf();
616 StrBufAppendBufPlain(new_set, HKEY("1:"), 0);
617 StrBufAppendBuf(new_set, first_tok, 0);
618 StrBufAppendBufPlain(new_set, HKEY(":"), 0);
619 StrBufAppendBuf(new_set, vset, 0);
622 FreeStrBuf(&first_tok);
626 MSG_syslog(LOG_DEBUG, " after update: %s\n", ChrPtr(vset));
628 /* Decide which message set we're manipulating */
630 case ctdlsetseen_seen:
631 safestrncpy(vbuf.v_seen, ChrPtr(vset), sizeof vbuf.v_seen);
633 case ctdlsetseen_answered:
634 safestrncpy(vbuf.v_answered, ChrPtr(vset), sizeof vbuf.v_answered);
640 CtdlSetRelationship(&vbuf, which_user, which_room);
646 * API function to perform an operation for each qualifying message in the
647 * current room. (Returns the number of messages processed.)
649 int CtdlForEachMessage(int mode, long ref, char *search_string,
651 struct CtdlMessage *compare,
652 ForEachMsgCallback CallBack,
655 struct CitContext *CCC = CC;
658 struct cdbdata *cdbfr;
659 long *msglist = NULL;
661 int num_processed = 0;
664 struct CtdlMessage *msg = NULL;
667 int printed_lastold = 0;
668 int num_search_msgs = 0;
669 long *search_msgs = NULL;
671 int need_to_free_re = 0;
674 if ((content_type) && (!IsEmptyStr(content_type))) {
675 regcomp(&re, content_type, 0);
679 /* Learn about the user and room in question */
680 if (server_shutting_down) {
681 if (need_to_free_re) regfree(&re);
684 CtdlGetUser(&CCC->user, CCC->curr_user);
686 if (server_shutting_down) {
687 if (need_to_free_re) regfree(&re);
690 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
692 if (server_shutting_down) {
693 if (need_to_free_re) regfree(&re);
697 /* Load the message list */
698 cdbfr = cdb_fetch(CDB_MSGLISTS, &CCC->room.QRnumber, sizeof(long));
700 if (need_to_free_re) regfree(&re);
701 return 0; /* No messages at all? No further action. */
704 msglist = (long *) cdbfr->ptr;
705 num_msgs = cdbfr->len / sizeof(long);
707 cdbfr->ptr = NULL; /* clear this so that cdb_free() doesn't free it */
708 cdb_free(cdbfr); /* we own this memory now */
711 * Now begin the traversal.
713 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
715 /* If the caller is looking for a specific MIME type, filter
716 * out all messages which are not of the type requested.
718 if ((content_type != NULL) && (!IsEmptyStr(content_type))) {
720 /* This call to GetMetaData() sits inside this loop
721 * so that we only do the extra database read per msg
722 * if we need to. Doing the extra read all the time
723 * really kills the server. If we ever need to use
724 * metadata for another search criterion, we need to
725 * move the read somewhere else -- but still be smart
726 * enough to only do the read if the caller has
727 * specified something that will need it.
729 if (server_shutting_down) {
730 if (need_to_free_re) regfree(&re);
734 GetMetaData(&smi, msglist[a]);
736 /* if (strcasecmp(smi.meta_content_type, content_type)) { old non-regex way */
737 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) != 0) {
743 num_msgs = sort_msglist(msglist, num_msgs);
745 /* If a template was supplied, filter out the messages which
746 * don't match. (This could induce some delays!)
749 if (compare != NULL) {
750 for (a = 0; a < num_msgs; ++a) {
751 if (server_shutting_down) {
752 if (need_to_free_re) regfree(&re);
756 msg = CtdlFetchMessage(msglist[a], 1);
758 if (CtdlMsgCmp(msg, compare)) {
767 /* If a search string was specified, get a message list from
768 * the full text index and remove messages which aren't on both
772 * Since the lists are sorted and strictly ascending, and the
773 * output list is guaranteed to be shorter than or equal to the
774 * input list, we overwrite the bottom of the input list. This
775 * eliminates the need to memmove big chunks of the list over and
778 if ( (num_msgs > 0) && (mode == MSGS_SEARCH) && (search_string) ) {
780 /* Call search module via hook mechanism.
781 * NULL means use any search function available.
782 * otherwise replace with a char * to name of search routine
784 CtdlModuleDoSearch(&num_search_msgs, &search_msgs, search_string, "fulltext");
786 if (num_search_msgs > 0) {
790 orig_num_msgs = num_msgs;
792 for (i=0; i<orig_num_msgs; ++i) {
793 for (j=0; j<num_search_msgs; ++j) {
794 if (msglist[i] == search_msgs[j]) {
795 msglist[num_msgs++] = msglist[i];
801 num_msgs = 0; /* No messages qualify */
803 if (search_msgs != NULL) free(search_msgs);
805 /* Now that we've purged messages which don't contain the search
806 * string, treat a MSGS_SEARCH just like a MSGS_ALL from this
813 * Now iterate through the message list, according to the
814 * criteria supplied by the caller.
817 for (a = 0; a < num_msgs; ++a) {
818 if (server_shutting_down) {
819 if (need_to_free_re) regfree(&re);
821 return num_processed;
823 thismsg = msglist[a];
824 if (mode == MSGS_ALL) {
828 is_seen = is_msg_in_sequence_set(
829 vbuf.v_seen, thismsg);
830 if (is_seen) lastold = thismsg;
836 || ((mode == MSGS_OLD) && (is_seen))
837 || ((mode == MSGS_NEW) && (!is_seen))
838 || ((mode == MSGS_LAST) && (a >= (num_msgs - ref)))
839 || ((mode == MSGS_FIRST) && (a < ref))
840 || ((mode == MSGS_GT) && (thismsg > ref))
841 || ((mode == MSGS_LT) && (thismsg < ref))
842 || ((mode == MSGS_EQ) && (thismsg == ref))
845 if ((mode == MSGS_NEW) && (CCC->user.flags & US_LASTOLD) && (lastold > 0L) && (printed_lastold == 0) && (!is_seen)) {
847 CallBack(lastold, userdata);
851 if (CallBack) CallBack(thismsg, userdata);
855 if (need_to_free_re) regfree(&re);
858 * We cache the most recent msglist in order to do security checks later
860 if (CCC->client_socket > 0) {
861 if (CCC->cached_msglist != NULL) {
862 free(CCC->cached_msglist);
864 CCC->cached_msglist = msglist;
865 CCC->cached_num_msgs = num_msgs;
871 return num_processed;
877 * memfmout() - Citadel text formatter and paginator.
878 * Although the original purpose of this routine was to format
879 * text to the reader's screen width, all we're really using it
880 * for here is to format text out to 80 columns before sending it
881 * to the client. The client software may reformat it again.
884 char *mptr, /* where are we going to get our text from? */
885 const char *nl /* string to terminate lines with */
887 struct CitContext *CCC = CC;
889 unsigned char ch = 0;
896 while (ch=*(mptr++), ch != 0) {
899 if (client_write(outbuf, len) == -1)
901 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
905 if (client_write(nl, nllen) == -1)
907 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
912 else if (ch == '\r') {
913 /* Ignore carriage returns. Newlines are always LF or CRLF but never CR. */
915 else if (isspace(ch)) {
916 if (column > 72) { /* Beyond 72 columns, break on the next space */
917 if (client_write(outbuf, len) == -1)
919 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
923 if (client_write(nl, nllen) == -1)
925 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
938 if (column > 1000) { /* Beyond 1000 columns, break anywhere */
939 if (client_write(outbuf, len) == -1)
941 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
945 if (client_write(nl, nllen) == -1)
947 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
955 if (client_write(outbuf, len) == -1)
957 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
961 client_write(nl, nllen);
969 * Callback function for mime parser that simply lists the part
971 void list_this_part(char *name, char *filename, char *partnum, char *disp,
972 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
973 char *cbid, void *cbuserdata)
977 ma = (struct ma_info *)cbuserdata;
978 if (ma->is_ma == 0) {
979 cprintf("part=%s|%s|%s|%s|%s|%ld|%s|%s\n",
992 * Callback function for multipart prefix
994 void list_this_pref(char *name, char *filename, char *partnum, char *disp,
995 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
996 char *cbid, void *cbuserdata)
1000 ma = (struct ma_info *)cbuserdata;
1001 if (!strcasecmp(cbtype, "multipart/alternative")) {
1005 if (ma->is_ma == 0) {
1006 cprintf("pref=%s|%s\n", partnum, cbtype);
1011 * Callback function for multipart sufffix
1013 void list_this_suff(char *name, char *filename, char *partnum, char *disp,
1014 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1015 char *cbid, void *cbuserdata)
1019 ma = (struct ma_info *)cbuserdata;
1020 if (ma->is_ma == 0) {
1021 cprintf("suff=%s|%s\n", partnum, cbtype);
1023 if (!strcasecmp(cbtype, "multipart/alternative")) {
1030 * Callback function for mime parser that opens a section for downloading
1032 void mime_download(char *name, char *filename, char *partnum, char *disp,
1033 void *content, char *cbtype, char *cbcharset, size_t length,
1034 char *encoding, char *cbid, void *cbuserdata)
1037 CitContext *CCC = MyContext();
1039 /* Silently go away if there's already a download open. */
1040 if (CCC->download_fp != NULL)
1044 (!IsEmptyStr(partnum) && (!strcasecmp(CCC->download_desired_section, partnum)))
1045 || (!IsEmptyStr(cbid) && (!strcasecmp(CCC->download_desired_section, cbid)))
1047 CCC->download_fp = tmpfile();
1048 if (CCC->download_fp == NULL) {
1049 MSG_syslog(LOG_EMERG, "mime_download(): Couldn't write: %s\n",
1051 cprintf("%d cannot open temporary file: %s\n",
1052 ERROR + INTERNAL_ERROR, strerror(errno));
1056 rv = fwrite(content, length, 1, CCC->download_fp);
1058 MSG_syslog(LOG_EMERG, "mime_download(): Couldn't write: %s\n",
1060 cprintf("%d unable to write tempfile.\n",
1062 fclose(CCC->download_fp);
1063 CCC->download_fp = NULL;
1066 fflush(CCC->download_fp);
1067 rewind(CCC->download_fp);
1069 OpenCmdResult(filename, cbtype);
1076 * Callback function for mime parser that outputs a section all at once.
1077 * We can specify the desired section by part number *or* content-id.
1079 void mime_spew_section(char *name, char *filename, char *partnum, char *disp,
1080 void *content, char *cbtype, char *cbcharset, size_t length,
1081 char *encoding, char *cbid, void *cbuserdata)
1083 int *found_it = (int *)cbuserdata;
1086 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
1087 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
1090 cprintf("%d %d|-1|%s|%s|%s\n",
1097 client_write(content, length);
1103 * Load a message from disk into memory.
1104 * This is used by CtdlOutputMsg() and other fetch functions.
1106 * NOTE: Caller is responsible for freeing the returned CtdlMessage struct
1107 * using the CtdlMessageFree() function.
1109 struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body)
1111 struct CitContext *CCC = CC;
1112 struct cdbdata *dmsgtext;
1113 struct CtdlMessage *ret = NULL;
1117 cit_uint8_t field_header;
1119 MSG_syslog(LOG_DEBUG, "CtdlFetchMessage(%ld, %d)\n", msgnum, with_body);
1120 dmsgtext = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
1121 if (dmsgtext == NULL) {
1122 MSG_syslog(LOG_ERR, "CtdlFetchMessage(%ld, %d) Failed!\n", msgnum, with_body);
1125 mptr = dmsgtext->ptr;
1126 upper_bound = mptr + dmsgtext->len;
1128 /* Parse the three bytes that begin EVERY message on disk.
1129 * The first is always 0xFF, the on-disk magic number.
1130 * The second is the anonymous/public type byte.
1131 * The third is the format type byte (vari, fixed, or MIME).
1135 MSG_syslog(LOG_ERR, "Message %ld appears to be corrupted.\n", msgnum);
1139 ret = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1140 memset(ret, 0, sizeof(struct CtdlMessage));
1142 ret->cm_magic = CTDLMESSAGE_MAGIC;
1143 ret->cm_anon_type = *mptr++; /* Anon type byte */
1144 ret->cm_format_type = *mptr++; /* Format type byte */
1147 * The rest is zero or more arbitrary fields. Load them in.
1148 * We're done when we encounter either a zero-length field or
1149 * have just processed the 'M' (message text) field.
1153 if (mptr >= upper_bound) {
1156 field_header = *mptr++;
1158 CM_SetField(ret, field_header, mptr, len);
1160 mptr += len + 1; /* advance to next field */
1162 } while ((mptr < upper_bound) && (field_header != 'M'));
1166 /* Always make sure there's something in the msg text field. If
1167 * it's NULL, the message text is most likely stored separately,
1168 * so go ahead and fetch that. Failing that, just set a dummy
1169 * body so other code doesn't barf.
1171 if ( (CM_IsEmpty(ret, eMesageText)) && (with_body) ) {
1172 dmsgtext = cdb_fetch(CDB_BIGMSGS, &msgnum, sizeof(long));
1173 if (dmsgtext != NULL) {
1174 CM_SetAsField(ret, eMesageText, &dmsgtext->ptr, dmsgtext->len);
1178 if (CM_IsEmpty(ret, eMesageText)) {
1179 CM_SetField(ret, eMesageText, HKEY("\r\n\r\n (no text)\r\n"));
1182 /* Perform "before read" hooks (aborting if any return nonzero) */
1183 if (PerformMessageHooks(ret, EVT_BEFOREREAD) > 0) {
1194 * Pre callback function for multipart/alternative
1196 * NOTE: this differs from the standard behavior for a reason. Normally when
1197 * displaying multipart/alternative you want to show the _last_ usable
1198 * format in the message. Here we show the _first_ one, because it's
1199 * usually text/plain. Since this set of functions is designed for text
1200 * output to non-MIME-aware clients, this is the desired behavior.
1203 void fixed_output_pre(char *name, char *filename, char *partnum, char *disp,
1204 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1205 char *cbid, void *cbuserdata)
1207 struct CitContext *CCC = CC;
1210 ma = (struct ma_info *)cbuserdata;
1211 MSG_syslog(LOG_DEBUG, "fixed_output_pre() type=<%s>\n", cbtype);
1212 if (!strcasecmp(cbtype, "multipart/alternative")) {
1216 if (!strcasecmp(cbtype, "message/rfc822")) {
1222 * Post callback function for multipart/alternative
1224 void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
1225 void *content, char *cbtype, char *cbcharset, size_t length,
1226 char *encoding, char *cbid, void *cbuserdata)
1228 struct CitContext *CCC = CC;
1231 ma = (struct ma_info *)cbuserdata;
1232 MSG_syslog(LOG_DEBUG, "fixed_output_post() type=<%s>\n", cbtype);
1233 if (!strcasecmp(cbtype, "multipart/alternative")) {
1237 if (!strcasecmp(cbtype, "message/rfc822")) {
1243 * Inline callback function for mime parser that wants to display text
1245 void fixed_output(char *name, char *filename, char *partnum, char *disp,
1246 void *content, char *cbtype, char *cbcharset, size_t length,
1247 char *encoding, char *cbid, void *cbuserdata)
1249 struct CitContext *CCC = CC;
1255 ma = (struct ma_info *)cbuserdata;
1257 MSG_syslog(LOG_DEBUG,
1258 "fixed_output() part %s: %s (%s) (%ld bytes)\n",
1259 partnum, filename, cbtype, (long)length);
1262 * If we're in the middle of a multipart/alternative scope and
1263 * we've already printed another section, skip this one.
1265 if ( (ma->is_ma) && (ma->did_print) ) {
1266 MSG_syslog(LOG_DEBUG, "Skipping part %s (%s)\n", partnum, cbtype);
1271 if ( (!strcasecmp(cbtype, "text/plain"))
1272 || (IsEmptyStr(cbtype)) ) {
1275 client_write(wptr, length);
1276 if (wptr[length-1] != '\n') {
1283 if (!strcasecmp(cbtype, "text/html")) {
1284 ptr = html_to_ascii(content, length, 80, 0);
1286 client_write(ptr, wlen);
1287 if ((wlen > 0) && (ptr[wlen-1] != '\n')) {
1294 if (ma->use_fo_hooks) {
1295 if (PerformFixedOutputHooks(cbtype, content, length)) {
1296 /* above function returns nonzero if it handled the part */
1301 if (strncasecmp(cbtype, "multipart/", 10)) {
1302 cprintf("Part %s: %s (%s) (%ld bytes)\r\n",
1303 partnum, filename, cbtype, (long)length);
1309 * The client is elegant and sophisticated and wants to be choosy about
1310 * MIME content types, so figure out which multipart/alternative part
1311 * we're going to send.
1313 * We use a system of weights. When we find a part that matches one of the
1314 * MIME types we've declared as preferential, we can store it in ma->chosen_part
1315 * and then set ma->chosen_pref to that MIME type's position in our preference
1316 * list. If we then hit another match, we only replace the first match if
1317 * the preference value is lower.
1319 void choose_preferred(char *name, char *filename, char *partnum, char *disp,
1320 void *content, char *cbtype, char *cbcharset, size_t length,
1321 char *encoding, char *cbid, void *cbuserdata)
1323 struct CitContext *CCC = CC;
1328 ma = (struct ma_info *)cbuserdata;
1330 // NOTE: REMOVING THIS CONDITIONAL FIXES BUG 220
1331 // http://bugzilla.citadel.org/show_bug.cgi?id=220
1332 // I don't know if there are any side effects! Please TEST TEST TEST
1333 //if (ma->is_ma > 0) {
1335 for (i=0; i<num_tokens(CCC->preferred_formats, '|'); ++i) {
1336 extract_token(buf, CCC->preferred_formats, i, '|', sizeof buf);
1337 if ( (!strcasecmp(buf, cbtype)) && (!ma->freeze) ) {
1338 if (i < ma->chosen_pref) {
1339 MSG_syslog(LOG_DEBUG, "Setting chosen part: <%s>\n", partnum);
1340 safestrncpy(ma->chosen_part, partnum, sizeof ma->chosen_part);
1341 ma->chosen_pref = i;
1348 * Now that we've chosen our preferred part, output it.
1350 void output_preferred(char *name,
1362 struct CitContext *CCC = CC;
1365 int add_newline = 0;
1368 char *decoded = NULL;
1369 size_t bytes_decoded;
1372 ma = (struct ma_info *)cbuserdata;
1374 /* This is not the MIME part you're looking for... */
1375 if (strcasecmp(partnum, ma->chosen_part)) return;
1377 /* If the content-type of this part is in our preferred formats
1378 * list, we can simply output it verbatim.
1380 for (i=0; i<num_tokens(CCC->preferred_formats, '|'); ++i) {
1381 extract_token(buf, CCC->preferred_formats, i, '|', sizeof buf);
1382 if (!strcasecmp(buf, cbtype)) {
1383 /* Yeah! Go! W00t!! */
1384 if (ma->dont_decode == 0)
1385 rc = mime_decode_now (content,
1391 break; /* Give us the chance, maybe theres another one. */
1393 if (rc == 0) text_content = (char *)content;
1395 text_content = decoded;
1396 length = bytes_decoded;
1399 if (text_content[length-1] != '\n') {
1402 cprintf("Content-type: %s", cbtype);
1403 if (!IsEmptyStr(cbcharset)) {
1404 cprintf("; charset=%s", cbcharset);
1406 cprintf("\nContent-length: %d\n",
1407 (int)(length + add_newline) );
1408 if (!IsEmptyStr(encoding)) {
1409 cprintf("Content-transfer-encoding: %s\n", encoding);
1412 cprintf("Content-transfer-encoding: 7bit\n");
1414 cprintf("X-Citadel-MSG4-Partnum: %s\n", partnum);
1416 if (client_write(text_content, length) == -1)
1418 MSGM_syslog(LOG_ERR, "output_preferred(): aborting due to write failure.\n");
1421 if (add_newline) cprintf("\n");
1422 if (decoded != NULL) free(decoded);
1427 /* No translations required or possible: output as text/plain */
1428 cprintf("Content-type: text/plain\n\n");
1430 if (ma->dont_decode == 0)
1431 rc = mime_decode_now (content,
1437 return; /* Give us the chance, maybe theres another one. */
1439 if (rc == 0) text_content = (char *)content;
1441 text_content = decoded;
1442 length = bytes_decoded;
1445 fixed_output(name, filename, partnum, disp, text_content, cbtype, cbcharset,
1446 length, encoding, cbid, cbuserdata);
1447 if (decoded != NULL) free(decoded);
1452 char desired_section[64];
1459 * Callback function for
1461 void extract_encapsulated_message(char *name, char *filename, char *partnum, char *disp,
1462 void *content, char *cbtype, char *cbcharset, size_t length,
1463 char *encoding, char *cbid, void *cbuserdata)
1465 struct encapmsg *encap;
1467 encap = (struct encapmsg *)cbuserdata;
1469 /* Only proceed if this is the desired section... */
1470 if (!strcasecmp(encap->desired_section, partnum)) {
1471 encap->msglen = length;
1472 encap->msg = malloc(length + 2);
1473 memcpy(encap->msg, content, length);
1480 * Determine whether the specified message exists in the cached_msglist
1481 * (This is a security check)
1483 int check_cached_msglist(long msgnum) {
1484 struct CitContext *CCC = CC;
1486 /* cases in which we skip the check */
1487 if (!CCC) return om_ok; /* not a session */
1488 if (CCC->client_socket <= 0) return om_ok; /* not a client session */
1489 if (CCC->cached_msglist == NULL) return om_access_denied; /* no msglist fetched */
1490 if (CCC->cached_num_msgs == 0) return om_access_denied; /* nothing to check */
1493 /* Do a binary search within the cached_msglist for the requested msgnum */
1495 int max = (CC->cached_num_msgs - 1);
1497 while (max >= min) {
1498 int middle = min + (max-min) / 2 ;
1499 if (msgnum == CCC->cached_msglist[middle]) {
1502 if (msgnum > CC->cached_msglist[middle]) {
1510 return om_access_denied;
1516 * Get a message off disk. (returns om_* values found in msgbase.h)
1519 int CtdlOutputMsg(long msg_num, /* message number (local) to fetch */
1520 int mode, /* how would you like that message? */
1521 int headers_only, /* eschew the message body? */
1522 int do_proto, /* do Citadel protocol responses? */
1523 int crlf, /* Use CRLF newlines instead of LF? */
1524 char *section, /* NULL or a message/rfc822 section */
1525 int flags, /* various flags; see msgbase.h */
1529 struct CitContext *CCC = CC;
1530 struct CtdlMessage *TheMessage = NULL;
1531 int retcode = CIT_OK;
1532 struct encapmsg encap;
1535 MSG_syslog(LOG_DEBUG, "CtdlOutputMsg(msgnum=%ld, mode=%d, section=%s)\n",
1537 (section ? section : "<>")
1540 r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
1543 if (r == om_not_logged_in) {
1544 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1547 cprintf("%d An unknown error has occurred.\n", ERROR);
1554 * Check to make sure the message is actually IN this room
1556 r = check_cached_msglist(msg_num);
1557 if (r == om_access_denied) {
1558 /* Not in the cache? We get ONE shot to check it again. */
1559 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, NULL, NULL);
1560 r = check_cached_msglist(msg_num);
1563 MSG_syslog(LOG_DEBUG, "Security check fail: message %ld is not in %s\n",
1564 msg_num, CCC->room.QRname
1567 if (r == om_access_denied) {
1568 cprintf("%d message %ld was not found in this room\n",
1569 ERROR + HIGHER_ACCESS_REQUIRED,
1578 * Fetch the message from disk. If we're in HEADERS_FAST mode,
1579 * request that we don't even bother loading the body into memory.
1581 if (headers_only == HEADERS_FAST) {
1582 TheMessage = CtdlFetchMessage(msg_num, 0);
1585 TheMessage = CtdlFetchMessage(msg_num, 1);
1588 if (TheMessage == NULL) {
1589 if (do_proto) cprintf("%d Can't locate msg %ld on disk\n",
1590 ERROR + MESSAGE_NOT_FOUND, msg_num);
1591 return(om_no_such_msg);
1594 /* Here is the weird form of this command, to process only an
1595 * encapsulated message/rfc822 section.
1597 if (section) if (!IsEmptyStr(section)) if (strcmp(section, "0")) {
1598 memset(&encap, 0, sizeof encap);
1599 safestrncpy(encap.desired_section, section, sizeof encap.desired_section);
1600 mime_parser(TheMessage->cm_fields[eMesageText],
1602 *extract_encapsulated_message,
1603 NULL, NULL, (void *)&encap, 0
1606 if ((Author != NULL) && (*Author == NULL))
1608 *Author = TheMessage->cm_fields[eAuthor];
1609 TheMessage->cm_fields[eAuthor] = NULL;
1611 if ((Address != NULL) && (*Address == NULL))
1613 *Address = TheMessage->cm_fields[erFc822Addr];
1614 TheMessage->cm_fields[erFc822Addr] = NULL;
1616 CM_Free(TheMessage);
1620 encap.msg[encap.msglen] = 0;
1621 TheMessage = convert_internet_message(encap.msg);
1622 encap.msg = NULL; /* no free() here, TheMessage owns it now */
1624 /* Now we let it fall through to the bottom of this
1625 * function, because TheMessage now contains the
1626 * encapsulated message instead of the top-level
1627 * message. Isn't that neat?
1632 cprintf("%d msg %ld has no part %s\n",
1633 ERROR + MESSAGE_NOT_FOUND,
1637 retcode = om_no_such_msg;
1642 /* Ok, output the message now */
1643 if (retcode == CIT_OK)
1644 retcode = CtdlOutputPreLoadedMsg(TheMessage, mode, headers_only, do_proto, crlf, flags);
1645 if ((Author != NULL) && (*Author == NULL))
1647 *Author = TheMessage->cm_fields[eAuthor];
1648 TheMessage->cm_fields[eAuthor] = NULL;
1650 if ((Address != NULL) && (*Address == NULL))
1652 *Address = TheMessage->cm_fields[erFc822Addr];
1653 TheMessage->cm_fields[erFc822Addr] = NULL;
1656 CM_Free(TheMessage);
1663 void OutputCtdlMsgHeaders(
1664 struct CtdlMessage *TheMessage,
1665 int do_proto) /* do Citadel protocol responses? */
1670 char display_name[256];
1672 /* begin header processing loop for Citadel message format */
1673 safestrncpy(display_name, "<unknown>", sizeof display_name);
1674 if (!CM_IsEmpty(TheMessage, eAuthor)) {
1675 strcpy(buf, TheMessage->cm_fields[eAuthor]);
1676 if (TheMessage->cm_anon_type == MES_ANONONLY) {
1677 safestrncpy(display_name, "****", sizeof display_name);
1679 else if (TheMessage->cm_anon_type == MES_ANONOPT) {
1680 safestrncpy(display_name, "anonymous", sizeof display_name);
1683 safestrncpy(display_name, buf, sizeof display_name);
1685 if ((is_room_aide())
1686 && ((TheMessage->cm_anon_type == MES_ANONONLY)
1687 || (TheMessage->cm_anon_type == MES_ANONOPT))) {
1688 size_t tmp = strlen(display_name);
1689 snprintf(&display_name[tmp],
1690 sizeof display_name - tmp,
1695 /* Don't show Internet address for users on the
1696 * local Citadel network.
1699 if (!CM_IsEmpty(TheMessage, eNodeName) &&
1700 (haschar(TheMessage->cm_fields[eNodeName], '.') == 0))
1705 /* Now spew the header fields in the order we like them. */
1706 for (i=0; i< NDiskFields; ++i) {
1708 Field = FieldOrder[i];
1709 if (Field != eMesageText) {
1710 if ( (!CM_IsEmpty(TheMessage, Field))
1711 && (msgkeys[Field] != NULL) ) {
1712 if ((Field == eenVelopeTo) ||
1713 (Field == eRecipient) ||
1714 (Field == eCarbonCopY)) {
1715 sanitize_truncated_recipient(TheMessage->cm_fields[Field]);
1717 if (Field == eAuthor) {
1718 if (do_proto) cprintf("%s=%s\n",
1722 else if ((Field == erFc822Addr) && (suppress_f)) {
1725 /* Masquerade display name if needed */
1727 if (do_proto) cprintf("%s=%s\n",
1729 TheMessage->cm_fields[Field]
1738 void OutputRFC822MsgHeaders(
1739 struct CtdlMessage *TheMessage,
1740 int flags, /* should the bessage be exported clean */
1742 char *mid, long sizeof_mid,
1743 char *suser, long sizeof_suser,
1744 char *luser, long sizeof_luser,
1745 char *fuser, long sizeof_fuser,
1746 char *snode, long sizeof_snode)
1748 char datestamp[100];
1749 int subject_found = 0;
1756 for (i = 0; i < 256; ++i) {
1757 if (TheMessage->cm_fields[i]) {
1758 mptr = mpptr = TheMessage->cm_fields[i];
1761 safestrncpy(luser, mptr, sizeof_luser);
1762 safestrncpy(suser, mptr, sizeof_suser);
1764 else if (i == 'Y') {
1765 if ((flags & QP_EADDR) != 0) {
1766 mptr = qp_encode_email_addrs(mptr);
1768 sanitize_truncated_recipient(mptr);
1769 cprintf("CC: %s%s", mptr, nl);
1771 else if (i == 'P') {
1772 cprintf("Return-Path: %s%s", mptr, nl);
1774 else if (i == eListID) {
1775 cprintf("List-ID: %s%s", mptr, nl);
1777 else if (i == 'V') {
1778 if ((flags & QP_EADDR) != 0)
1779 mptr = qp_encode_email_addrs(mptr);
1781 while ((*hptr != '\0') && isspace(*hptr))
1783 if (!IsEmptyStr(hptr))
1784 cprintf("Envelope-To: %s%s", hptr, nl);
1786 else if (i == 'U') {
1787 cprintf("Subject: %s%s", mptr, nl);
1791 safestrncpy(mid, mptr, sizeof_mid); /// TODO: detect @ here and copy @nodename in if not found.
1792 else if (i == erFc822Addr)
1793 safestrncpy(fuser, mptr, sizeof_fuser);
1794 /* else if (i == 'O')
1795 cprintf("X-Citadel-Room: %s%s",
1798 safestrncpy(snode, mptr, sizeof_snode);
1801 if (haschar(mptr, '@') == 0)
1803 sanitize_truncated_recipient(mptr);
1804 cprintf("To: %s@%s", mptr, config.c_fqdn);
1809 if ((flags & QP_EADDR) != 0) {
1810 mptr = qp_encode_email_addrs(mptr);
1812 sanitize_truncated_recipient(mptr);
1813 cprintf("To: %s", mptr);
1817 else if (i == 'T') {
1818 datestring(datestamp, sizeof datestamp,
1819 atol(mptr), DATESTRING_RFC822);
1820 cprintf("Date: %s%s", datestamp, nl);
1822 else if (i == 'W') {
1823 cprintf("References: ");
1824 k = num_tokens(mptr, '|');
1825 for (j=0; j<k; ++j) {
1826 extract_token(buf, mptr, j, '|', sizeof buf);
1827 cprintf("<%s>", buf);
1836 else if (i == eReplyTo) {
1838 while ((*hptr != '\0') && isspace(*hptr))
1840 if (!IsEmptyStr(hptr))
1841 cprintf("Reply-To: %s%s", mptr, nl);
1847 if (subject_found == 0) {
1848 cprintf("Subject: (no subject)%s", nl);
1853 void Dump_RFC822HeadersBody(
1854 struct CtdlMessage *TheMessage,
1855 int headers_only, /* eschew the message body? */
1856 int flags, /* should the bessage be exported clean? */
1860 cit_uint8_t prev_ch;
1862 const char *StartOfText = StrBufNOTNULL;
1865 int nllen = strlen(nl);
1868 mptr = TheMessage->cm_fields[eMesageText];
1872 while (*mptr != '\0') {
1873 if (*mptr == '\r') {
1880 eoh = (*(mptr+1) == '\r') && (*(mptr+2) == '\n');
1882 eoh = *(mptr+1) == '\n';
1886 StartOfText = strchr(StartOfText, '\n');
1887 StartOfText = strchr(StartOfText, '\n');
1890 if (((headers_only == HEADERS_NONE) && (mptr >= StartOfText)) ||
1891 ((headers_only == HEADERS_ONLY) && (mptr < StartOfText)) ||
1892 ((headers_only != HEADERS_NONE) &&
1893 (headers_only != HEADERS_ONLY))
1895 if (*mptr == '\n') {
1896 memcpy(&outbuf[outlen], nl, nllen);
1898 outbuf[outlen] = '\0';
1901 outbuf[outlen++] = *mptr;
1905 if (flags & ESC_DOT)
1907 if ((prev_ch == '\n') &&
1909 ((*(mptr+1) == '\r') || (*(mptr+1) == '\n')))
1911 outbuf[outlen++] = '.';
1916 if (outlen > 1000) {
1917 if (client_write(outbuf, outlen) == -1)
1919 struct CitContext *CCC = CC;
1920 MSGM_syslog(LOG_ERR, "Dump_RFC822HeadersBody(): aborting due to write failure.\n");
1927 client_write(outbuf, outlen);
1933 /* If the format type on disk is 1 (fixed-format), then we want
1934 * everything to be output completely literally ... regardless of
1935 * what message transfer format is in use.
1937 void DumpFormatFixed(
1938 struct CtdlMessage *TheMessage,
1939 int mode, /* how would you like that message? */
1946 int nllen = strlen (nl);
1949 mptr = TheMessage->cm_fields[eMesageText];
1951 if (mode == MT_MIME) {
1952 cprintf("Content-type: text/plain\n\n");
1956 while (ch = *mptr++, ch > 0) {
1960 if ((buflen > 250) && (!xlline)){
1964 while ((buflen > 0) &&
1965 (!isspace(buf[buflen])))
1971 mptr -= tbuflen - buflen;
1976 /* if we reach the outer bounds of our buffer,
1977 abort without respect what whe purge. */
1980 (buflen > SIZ - nllen - 2)))
1984 memcpy (&buf[buflen], nl, nllen);
1988 if (client_write(buf, buflen) == -1)
1990 struct CitContext *CCC = CC;
1991 MSGM_syslog(LOG_ERR, "DumpFormatFixed(): aborting due to write failure.\n");
2003 if (!IsEmptyStr(buf))
2004 cprintf("%s%s", buf, nl);
2008 * Get a message off disk. (returns om_* values found in msgbase.h)
2010 int CtdlOutputPreLoadedMsg(
2011 struct CtdlMessage *TheMessage,
2012 int mode, /* how would you like that message? */
2013 int headers_only, /* eschew the message body? */
2014 int do_proto, /* do Citadel protocol responses? */
2015 int crlf, /* Use CRLF newlines instead of LF? */
2016 int flags /* should the bessage be exported clean? */
2018 struct CitContext *CCC = CC;
2021 const char *nl; /* newline string */
2024 /* Buffers needed for RFC822 translation. These are all filled
2025 * using functions that are bounds-checked, and therefore we can
2026 * make them substantially smaller than SIZ.
2034 MSG_syslog(LOG_DEBUG, "CtdlOutputPreLoadedMsg(TheMessage=%s, %d, %d, %d, %d\n",
2035 ((TheMessage == NULL) ? "NULL" : "not null"),
2036 mode, headers_only, do_proto, crlf);
2038 strcpy(mid, "unknown");
2039 nl = (crlf ? "\r\n" : "\n");
2041 if (!CM_IsValidMsg(TheMessage)) {
2042 MSGM_syslog(LOG_ERR,
2043 "ERROR: invalid preloaded message for output\n");
2045 return(om_no_such_msg);
2048 /* Suppress envelope recipients if required to avoid disclosing BCC addresses.
2049 * Pad it with spaces in order to avoid changing the RFC822 length of the message.
2051 if ( (flags & SUPPRESS_ENV_TO) && (!CM_IsEmpty(TheMessage, eenVelopeTo)) ) {
2052 memset(TheMessage->cm_fields[eenVelopeTo], ' ', strlen(TheMessage->cm_fields[eenVelopeTo]));
2055 /* Are we downloading a MIME component? */
2056 if (mode == MT_DOWNLOAD) {
2057 if (TheMessage->cm_format_type != FMT_RFC822) {
2059 cprintf("%d This is not a MIME message.\n",
2060 ERROR + ILLEGAL_VALUE);
2061 } else if (CCC->download_fp != NULL) {
2062 if (do_proto) cprintf(
2063 "%d You already have a download open.\n",
2064 ERROR + RESOURCE_BUSY);
2066 /* Parse the message text component */
2067 mptr = TheMessage->cm_fields[eMesageText];
2068 mime_parser(mptr, NULL, *mime_download, NULL, NULL, NULL, 0);
2069 /* If there's no file open by this time, the requested
2070 * section wasn't found, so print an error
2072 if (CCC->download_fp == NULL) {
2073 if (do_proto) cprintf(
2074 "%d Section %s not found.\n",
2075 ERROR + FILE_NOT_FOUND,
2076 CCC->download_desired_section);
2079 return((CCC->download_fp != NULL) ? om_ok : om_mime_error);
2082 /* MT_SPEW_SECTION is like MT_DOWNLOAD except it outputs the whole MIME part
2083 * in a single server operation instead of opening a download file.
2085 if (mode == MT_SPEW_SECTION) {
2086 if (TheMessage->cm_format_type != FMT_RFC822) {
2088 cprintf("%d This is not a MIME message.\n",
2089 ERROR + ILLEGAL_VALUE);
2091 /* Parse the message text component */
2094 mptr = TheMessage->cm_fields[eMesageText];
2095 mime_parser(mptr, NULL, *mime_spew_section, NULL, NULL, (void *)&found_it, 0);
2096 /* If section wasn't found, print an error
2099 if (do_proto) cprintf(
2100 "%d Section %s not found.\n",
2101 ERROR + FILE_NOT_FOUND,
2102 CCC->download_desired_section);
2105 return((CCC->download_fp != NULL) ? om_ok : om_mime_error);
2108 /* now for the user-mode message reading loops */
2109 if (do_proto) cprintf("%d msg:\n", LISTING_FOLLOWS);
2111 /* Does the caller want to skip the headers? */
2112 if (headers_only == HEADERS_NONE) goto START_TEXT;
2114 /* Tell the client which format type we're using. */
2115 if ( (mode == MT_CITADEL) && (do_proto) ) {
2116 cprintf("type=%d\n", TheMessage->cm_format_type);
2119 /* nhdr=yes means that we're only displaying headers, no body */
2120 if ( (TheMessage->cm_anon_type == MES_ANONONLY)
2121 && ((mode == MT_CITADEL) || (mode == MT_MIME))
2124 cprintf("nhdr=yes\n");
2127 if ((mode == MT_CITADEL) || (mode == MT_MIME))
2128 OutputCtdlMsgHeaders(TheMessage, do_proto);
2131 /* begin header processing loop for RFC822 transfer format */
2135 strcpy(snode, NODENAME);
2136 if (mode == MT_RFC822)
2137 OutputRFC822MsgHeaders(
2142 suser, sizeof(suser),
2143 luser, sizeof(luser),
2144 fuser, sizeof(fuser),
2145 snode, sizeof(snode)
2149 for (i=0; !IsEmptyStr(&suser[i]); ++i) {
2150 suser[i] = tolower(suser[i]);
2151 if (!isalnum(suser[i])) suser[i]='_';
2154 if (mode == MT_RFC822) {
2155 if (!strcasecmp(snode, NODENAME)) {
2156 safestrncpy(snode, FQDN, sizeof snode);
2159 /* Construct a fun message id */
2160 cprintf("Message-ID: <%s", mid);/// todo: this possibly breaks threadding mails.
2161 if (strchr(mid, '@')==NULL) {
2162 cprintf("@%s", snode);
2166 if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONONLY)) {
2167 cprintf("From: \"----\" <x@x.org>%s", nl);
2169 else if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONOPT)) {
2170 cprintf("From: \"anonymous\" <x@x.org>%s", nl);
2172 else if (!IsEmptyStr(fuser)) {
2173 cprintf("From: \"%s\" <%s>%s", luser, fuser, nl);
2176 cprintf("From: \"%s\" <%s@%s>%s", luser, suser, snode, nl);
2179 /* Blank line signifying RFC822 end-of-headers */
2180 if (TheMessage->cm_format_type != FMT_RFC822) {
2185 /* end header processing loop ... at this point, we're in the text */
2187 if (headers_only == HEADERS_FAST) goto DONE;
2189 /* Tell the client about the MIME parts in this message */
2190 if (TheMessage->cm_format_type == FMT_RFC822) {
2191 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2192 mptr = TheMessage->cm_fields[eMesageText];
2193 memset(&ma, 0, sizeof(struct ma_info));
2194 mime_parser(mptr, NULL,
2195 (do_proto ? *list_this_part : NULL),
2196 (do_proto ? *list_this_pref : NULL),
2197 (do_proto ? *list_this_suff : NULL),
2200 else if (mode == MT_RFC822) { /* unparsed RFC822 dump */
2201 Dump_RFC822HeadersBody(
2210 if (headers_only == HEADERS_ONLY) {
2214 /* signify start of msg text */
2215 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2216 if (do_proto) cprintf("text\n");
2219 if (TheMessage->cm_format_type == FMT_FIXED)
2222 mode, /* how would you like that message? */
2225 /* If the message on disk is format 0 (Citadel vari-format), we
2226 * output using the formatter at 80 columns. This is the final output
2227 * form if the transfer format is RFC822, but if the transfer format
2228 * is Citadel proprietary, it'll still work, because the indentation
2229 * for new paragraphs is correct and the client will reformat the
2230 * message to the reader's screen width.
2232 if (TheMessage->cm_format_type == FMT_CITADEL) {
2233 mptr = TheMessage->cm_fields[eMesageText];
2235 if (mode == MT_MIME) {
2236 cprintf("Content-type: text/x-citadel-variformat\n\n");
2241 /* If the message on disk is format 4 (MIME), we've gotta hand it
2242 * off to the MIME parser. The client has already been told that
2243 * this message is format 1 (fixed format), so the callback function
2244 * we use will display those parts as-is.
2246 if (TheMessage->cm_format_type == FMT_RFC822) {
2247 memset(&ma, 0, sizeof(struct ma_info));
2249 if (mode == MT_MIME) {
2250 ma.use_fo_hooks = 0;
2251 strcpy(ma.chosen_part, "1");
2252 ma.chosen_pref = 9999;
2253 ma.dont_decode = CCC->msg4_dont_decode;
2254 mime_parser(mptr, NULL,
2255 *choose_preferred, *fixed_output_pre,
2256 *fixed_output_post, (void *)&ma, 1);
2257 mime_parser(mptr, NULL,
2258 *output_preferred, NULL, NULL, (void *)&ma, 1);
2261 ma.use_fo_hooks = 1;
2262 mime_parser(mptr, NULL,
2263 *fixed_output, *fixed_output_pre,
2264 *fixed_output_post, (void *)&ma, 0);
2269 DONE: /* now we're done */
2270 if (do_proto) cprintf("000\n");
2275 * Save one or more message pointers into a specified room
2276 * (Returns 0 for success, nonzero for failure)
2277 * roomname may be NULL to use the current room
2279 * Note that the 'supplied_msg' field may be set to NULL, in which case
2280 * the message will be fetched from disk, by number, if we need to perform
2281 * replication checks. This adds an additional database read, so if the
2282 * caller already has the message in memory then it should be supplied. (Obviously
2283 * this mode of operation only works if we're saving a single message.)
2285 int CtdlSaveMsgPointersInRoom(char *roomname, long newmsgidlist[], int num_newmsgs,
2286 int do_repl_check, struct CtdlMessage *supplied_msg, int suppress_refcount_adj
2288 struct CitContext *CCC = CC;
2290 char hold_rm[ROOMNAMELEN];
2291 struct cdbdata *cdbfr;
2294 long highest_msg = 0L;
2297 struct CtdlMessage *msg = NULL;
2299 long *msgs_to_be_merged = NULL;
2300 int num_msgs_to_be_merged = 0;
2302 MSG_syslog(LOG_DEBUG,
2303 "CtdlSaveMsgPointersInRoom(room=%s, num_msgs=%d, repl=%d, suppress_rca=%d)\n",
2304 roomname, num_newmsgs, do_repl_check, suppress_refcount_adj
2307 strcpy(hold_rm, CCC->room.QRname);
2310 if (newmsgidlist == NULL) return(ERROR + INTERNAL_ERROR);
2311 if (num_newmsgs < 1) return(ERROR + INTERNAL_ERROR);
2312 if (num_newmsgs > 1) supplied_msg = NULL;
2314 /* Now the regular stuff */
2315 if (CtdlGetRoomLock(&CCC->room,
2316 ((roomname != NULL) ? roomname : CCC->room.QRname) )
2318 MSG_syslog(LOG_ERR, "No such room <%s>\n", roomname);
2319 return(ERROR + ROOM_NOT_FOUND);
2323 msgs_to_be_merged = malloc(sizeof(long) * num_newmsgs);
2324 num_msgs_to_be_merged = 0;
2327 cdbfr = cdb_fetch(CDB_MSGLISTS, &CCC->room.QRnumber, sizeof(long));
2328 if (cdbfr == NULL) {
2332 msglist = (long *) cdbfr->ptr;
2333 cdbfr->ptr = NULL; /* CtdlSaveMsgPointerInRoom() now owns this memory */
2334 num_msgs = cdbfr->len / sizeof(long);
2339 /* Create a list of msgid's which were supplied by the caller, but do
2340 * not already exist in the target room. It is absolutely taboo to
2341 * have more than one reference to the same message in a room.
2343 for (i=0; i<num_newmsgs; ++i) {
2345 if (num_msgs > 0) for (j=0; j<num_msgs; ++j) {
2346 if (msglist[j] == newmsgidlist[i]) {
2351 msgs_to_be_merged[num_msgs_to_be_merged++] = newmsgidlist[i];
2355 MSG_syslog(LOG_DEBUG, "%d unique messages to be merged\n", num_msgs_to_be_merged);
2358 * Now merge the new messages
2360 msglist = realloc(msglist, (sizeof(long) * (num_msgs + num_msgs_to_be_merged)) );
2361 if (msglist == NULL) {
2362 MSGM_syslog(LOG_ALERT, "ERROR: can't realloc message list!\n");
2363 free(msgs_to_be_merged);
2364 return (ERROR + INTERNAL_ERROR);
2366 memcpy(&msglist[num_msgs], msgs_to_be_merged, (sizeof(long) * num_msgs_to_be_merged) );
2367 num_msgs += num_msgs_to_be_merged;
2369 /* Sort the message list, so all the msgid's are in order */
2370 num_msgs = sort_msglist(msglist, num_msgs);
2372 /* Determine the highest message number */
2373 highest_msg = msglist[num_msgs - 1];
2375 /* Write it back to disk. */
2376 cdb_store(CDB_MSGLISTS, &CCC->room.QRnumber, (int)sizeof(long),
2377 msglist, (int)(num_msgs * sizeof(long)));
2379 /* Free up the memory we used. */
2382 /* Update the highest-message pointer and unlock the room. */
2383 CCC->room.QRhighest = highest_msg;
2384 CtdlPutRoomLock(&CCC->room);
2386 /* Perform replication checks if necessary */
2387 if ( (DoesThisRoomNeedEuidIndexing(&CCC->room)) && (do_repl_check) ) {
2388 MSGM_syslog(LOG_DEBUG, "CtdlSaveMsgPointerInRoom() doing repl checks\n");
2390 for (i=0; i<num_msgs_to_be_merged; ++i) {
2391 msgid = msgs_to_be_merged[i];
2393 if (supplied_msg != NULL) {
2397 msg = CtdlFetchMessage(msgid, 0);
2401 ReplicationChecks(msg);
2403 /* If the message has an Exclusive ID, index that... */
2404 if (!CM_IsEmpty(msg, eExclusiveID)) {
2405 index_message_by_euid(msg->cm_fields[eExclusiveID], &CCC->room, msgid);
2408 /* Free up the memory we may have allocated */
2409 if (msg != supplied_msg) {
2418 MSGM_syslog(LOG_DEBUG, "CtdlSaveMsgPointerInRoom() skips repl checks\n");
2421 /* Submit this room for processing by hooks */
2422 PerformRoomHooks(&CCC->room);
2424 /* Go back to the room we were in before we wandered here... */
2425 CtdlGetRoom(&CCC->room, hold_rm);
2427 /* Bump the reference count for all messages which were merged */
2428 if (!suppress_refcount_adj) {
2429 AdjRefCountList(msgs_to_be_merged, num_msgs_to_be_merged, +1);
2432 /* Free up memory... */
2433 if (msgs_to_be_merged != NULL) {
2434 free(msgs_to_be_merged);
2437 /* Return success. */
2443 * This is the same as CtdlSaveMsgPointersInRoom() but it only accepts
2446 int CtdlSaveMsgPointerInRoom(char *roomname, long msgid,
2447 int do_repl_check, struct CtdlMessage *supplied_msg)
2449 return CtdlSaveMsgPointersInRoom(roomname, &msgid, 1, do_repl_check, supplied_msg, 0);
2456 * Message base operation to save a new message to the message store
2457 * (returns new message number)
2459 * This is the back end for CtdlSubmitMsg() and should not be directly
2460 * called by server-side modules.
2463 long send_message(struct CtdlMessage *msg) {
2464 struct CitContext *CCC = CC;
2473 /* Get a new message number */
2474 newmsgid = get_new_message_number();
2475 msgidbuflen = snprintf(msgidbuf, sizeof msgidbuf, "%08lX-%08lX@%s",
2476 (long unsigned int) time(NULL),
2477 (long unsigned int) newmsgid,
2481 /* Generate an ID if we don't have one already */
2482 if (CM_IsEmpty(msg, emessageId)) {
2483 CM_SetField(msg, emessageId, msgidbuf, msgidbuflen);
2486 /* If the message is big, set its body aside for storage elsewhere */
2487 if (!CM_IsEmpty(msg, eMesageText)) {
2488 if (strlen(msg->cm_fields[eMesageText]) > BIGMSG) {
2490 holdM = msg->cm_fields[eMesageText];
2491 msg->cm_fields[eMesageText] = NULL;
2495 /* Serialize our data structure for storage in the database */
2496 CtdlSerializeMessage(&smr, msg);
2499 msg->cm_fields[eMesageText] = holdM;
2503 cprintf("%d Unable to serialize message\n",
2504 ERROR + INTERNAL_ERROR);
2508 /* Write our little bundle of joy into the message base */
2509 if (cdb_store(CDB_MSGMAIN, &newmsgid, (int)sizeof(long),
2510 smr.ser, smr.len) < 0) {
2511 MSGM_syslog(LOG_ERR, "Can't store message\n");
2515 cdb_store(CDB_BIGMSGS,
2525 /* Free the memory we used for the serialized message */
2528 /* Return the *local* message ID to the caller
2529 * (even if we're storing an incoming network message)
2537 * Serialize a struct CtdlMessage into the format used on disk and network.
2539 * This function loads up a "struct ser_ret" (defined in server.h) which
2540 * contains the length of the serialized message and a pointer to the
2541 * serialized message in memory. THE LATTER MUST BE FREED BY THE CALLER.
2543 void CtdlSerializeMessage(struct ser_ret *ret, /* return values */
2544 struct CtdlMessage *msg) /* unserialized msg */
2546 struct CitContext *CCC = CC;
2547 size_t wlen, fieldlen;
2549 long lengths[NDiskFields];
2551 memset(lengths, 0, sizeof(lengths));
2554 * Check for valid message format
2556 if (CM_IsValidMsg(msg) == 0) {
2557 MSGM_syslog(LOG_ERR, "CtdlSerializeMessage() aborting due to invalid message\n");
2564 for (i=0; i < NDiskFields; ++i)
2565 if (msg->cm_fields[FieldOrder[i]] != NULL)
2567 lengths[i] = strlen(msg->cm_fields[FieldOrder[i]]);
2568 ret->len += lengths[i] + 2;
2571 ret->ser = malloc(ret->len);
2572 if (ret->ser == NULL) {
2573 MSG_syslog(LOG_ERR, "CtdlSerializeMessage() malloc(%ld) failed: %s\n",
2574 (long)ret->len, strerror(errno));
2581 ret->ser[1] = msg->cm_anon_type;
2582 ret->ser[2] = msg->cm_format_type;
2585 for (i=0; i < NDiskFields; ++i)
2586 if (msg->cm_fields[FieldOrder[i]] != NULL)
2588 fieldlen = lengths[i];
2589 ret->ser[wlen++] = (char)FieldOrder[i];
2591 memcpy(&ret->ser[wlen],
2592 msg->cm_fields[FieldOrder[i]],
2595 wlen = wlen + fieldlen + 1;
2598 if (ret->len != wlen) {
2599 MSG_syslog(LOG_ERR, "ERROR: len=%ld wlen=%ld\n",
2600 (long)ret->len, (long)wlen);
2608 * Check to see if any messages already exist in the current room which
2609 * carry the same Exclusive ID as this one. If any are found, delete them.
2611 void ReplicationChecks(struct CtdlMessage *msg) {
2612 struct CitContext *CCC = CC;
2613 long old_msgnum = (-1L);
2615 if (DoesThisRoomNeedEuidIndexing(&CCC->room) == 0) return;
2617 MSG_syslog(LOG_DEBUG, "Performing replication checks in <%s>\n",
2620 /* No exclusive id? Don't do anything. */
2621 if (msg == NULL) return;
2622 if (CM_IsEmpty(msg, eExclusiveID)) return;
2624 /*MSG_syslog(LOG_DEBUG, "Exclusive ID: <%s> for room <%s>\n",
2625 msg->cm_fields[eExclusiveID], CCC->room.QRname);*/
2627 old_msgnum = CtdlLocateMessageByEuid(msg->cm_fields[eExclusiveID], &CCC->room);
2628 if (old_msgnum > 0L) {
2629 MSG_syslog(LOG_DEBUG, "ReplicationChecks() replacing message %ld\n", old_msgnum);
2630 CtdlDeleteMessages(CCC->room.QRname, &old_msgnum, 1, "");
2637 * Save a message to disk and submit it into the delivery system.
2639 long CtdlSubmitMsg(struct CtdlMessage *msg, /* message to save */
2640 struct recptypes *recps, /* recipients (if mail) */
2641 const char *force, /* force a particular room? */
2642 int flags /* should the message be exported clean? */
2645 char submit_filename[128];
2646 char hold_rm[ROOMNAMELEN];
2647 char actual_rm[ROOMNAMELEN];
2648 char force_room[ROOMNAMELEN];
2649 char content_type[SIZ]; /* We have to learn this */
2650 char recipient[SIZ];
2653 const char *mptr = NULL;
2654 struct ctdluser userbuf;
2656 struct MetaData smi;
2657 FILE *network_fp = NULL;
2658 static int seqnum = 1;
2659 struct CtdlMessage *imsg = NULL;
2661 size_t instr_alloc = 0;
2663 char *hold_R, *hold_D;
2664 char *collected_addresses = NULL;
2665 struct addresses_to_be_filed *aptr = NULL;
2666 StrBuf *saved_rfc822_version = NULL;
2667 int qualified_for_journaling = 0;
2668 CitContext *CCC = MyContext();
2669 char bounce_to[1024] = "";
2672 MSGM_syslog(LOG_DEBUG, "CtdlSubmitMsg() called\n");
2673 if (CM_IsValidMsg(msg) == 0) return(-1); /* self check */
2675 /* If this message has no timestamp, we take the liberty of
2676 * giving it one, right now.
2678 if (CM_IsEmpty(msg, eTimestamp)) {
2679 CM_SetFieldLONG(msg, eTimestamp, time(NULL));
2682 /* If this message has no path, we generate one.
2684 if (CM_IsEmpty(msg, eMessagePath)) {
2685 if (!CM_IsEmpty(msg, eAuthor)) {
2686 CM_CopyField(msg, eMessagePath, eAuthor);
2687 for (a=0; !IsEmptyStr(&msg->cm_fields[eMessagePath][a]); ++a) {
2688 if (isspace(msg->cm_fields[eMessagePath][a])) {
2689 msg->cm_fields[eMessagePath][a] = ' ';
2694 CM_SetField(msg, eMessagePath, HKEY("unknown"));
2698 if (force == NULL) {
2699 force_room[0] = '\0';
2702 strcpy(force_room, force);
2705 /* Learn about what's inside, because it's what's inside that counts */
2706 if (CM_IsEmpty(msg, eMesageText)) {
2707 MSGM_syslog(LOG_ERR, "ERROR: attempt to save message with NULL body\n");
2711 switch (msg->cm_format_type) {
2713 strcpy(content_type, "text/x-citadel-variformat");
2716 strcpy(content_type, "text/plain");
2719 strcpy(content_type, "text/plain");
2720 mptr = bmstrcasestr(msg->cm_fields[eMesageText], "Content-type:");
2723 safestrncpy(content_type, &mptr[13], sizeof content_type);
2724 striplt(content_type);
2725 aptr = content_type;
2726 while (!IsEmptyStr(aptr)) {
2738 /* Goto the correct room */
2739 room = (recps) ? CCC->room.QRname : SENTITEMS;
2740 MSG_syslog(LOG_DEBUG, "Selected room %s\n", room);
2741 strcpy(hold_rm, CCC->room.QRname);
2742 strcpy(actual_rm, CCC->room.QRname);
2743 if (recps != NULL) {
2744 strcpy(actual_rm, SENTITEMS);
2747 /* If the user is a twit, move to the twit room for posting */
2749 if (CCC->user.axlevel == AxProbU) {
2750 strcpy(hold_rm, actual_rm);
2751 strcpy(actual_rm, config.c_twitroom);
2752 MSGM_syslog(LOG_DEBUG, "Diverting to twit room\n");
2756 /* ...or if this message is destined for Aide> then go there. */
2757 if (!IsEmptyStr(force_room)) {
2758 strcpy(actual_rm, force_room);
2761 MSG_syslog(LOG_INFO, "Final selection: %s (%s)\n", actual_rm, room);
2762 if (strcasecmp(actual_rm, CCC->room.QRname)) {
2763 /* CtdlGetRoom(&CCC->room, actual_rm); */
2764 CtdlUserGoto(actual_rm, 0, 1, NULL, NULL);
2768 * If this message has no O (room) field, generate one.
2770 if (CM_IsEmpty(msg, eOriginalRoom)) {
2771 CM_SetField(msg, eOriginalRoom, CCC->room.QRname, strlen(CCC->room.QRname));
2774 /* Perform "before save" hooks (aborting if any return nonzero) */
2775 MSGM_syslog(LOG_DEBUG, "Performing before-save hooks\n");
2776 if (PerformMessageHooks(msg, EVT_BEFORESAVE) > 0) return(-3);
2779 * If this message has an Exclusive ID, and the room is replication
2780 * checking enabled, then do replication checks.
2782 if (DoesThisRoomNeedEuidIndexing(&CCC->room)) {
2783 ReplicationChecks(msg);
2786 /* Save it to disk */
2787 MSGM_syslog(LOG_DEBUG, "Saving to disk\n");
2788 newmsgid = send_message(msg);
2789 if (newmsgid <= 0L) return(-5);
2791 /* Write a supplemental message info record. This doesn't have to
2792 * be a critical section because nobody else knows about this message
2795 MSGM_syslog(LOG_DEBUG, "Creating MetaData record\n");
2796 memset(&smi, 0, sizeof(struct MetaData));
2797 smi.meta_msgnum = newmsgid;
2798 smi.meta_refcount = 0;
2799 safestrncpy(smi.meta_content_type, content_type,
2800 sizeof smi.meta_content_type);
2803 * Measure how big this message will be when rendered as RFC822.
2804 * We do this for two reasons:
2805 * 1. We need the RFC822 length for the new metadata record, so the
2806 * POP and IMAP services don't have to calculate message lengths
2807 * while the user is waiting (multiplied by potentially hundreds
2808 * or thousands of messages).
2809 * 2. If journaling is enabled, we will need an RFC822 version of the
2810 * message to attach to the journalized copy.
2812 if (CCC->redirect_buffer != NULL) {
2813 MSGM_syslog(LOG_ALERT, "CCC->redirect_buffer is not NULL during message submission!\n");
2816 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
2817 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, QP_EADDR);
2818 smi.meta_rfc822_length = StrLength(CCC->redirect_buffer);
2819 saved_rfc822_version = CCC->redirect_buffer;
2820 CCC->redirect_buffer = NULL;
2824 /* Now figure out where to store the pointers */
2825 MSGM_syslog(LOG_DEBUG, "Storing pointers\n");
2827 /* If this is being done by the networker delivering a private
2828 * message, we want to BYPASS saving the sender's copy (because there
2829 * is no local sender; it would otherwise go to the Trashcan).
2831 if ((!CCC->internal_pgm) || (recps == NULL)) {
2832 if (CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 1, msg) != 0) {
2833 MSGM_syslog(LOG_ERR, "ERROR saving message pointer!\n");
2834 CtdlSaveMsgPointerInRoom(config.c_aideroom, newmsgid, 0, msg);
2838 /* For internet mail, drop a copy in the outbound queue room */
2839 if ((recps != NULL) && (recps->num_internet > 0)) {
2840 CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, newmsgid, 0, msg);
2843 /* If other rooms are specified, drop them there too. */
2844 if ((recps != NULL) && (recps->num_room > 0))
2845 for (i=0; i<num_tokens(recps->recp_room, '|'); ++i) {
2846 extract_token(recipient, recps->recp_room, i,
2847 '|', sizeof recipient);
2848 MSG_syslog(LOG_DEBUG, "Delivering to room <%s>\n", recipient);///// xxxx
2849 CtdlSaveMsgPointerInRoom(recipient, newmsgid, 0, msg);
2852 /* Bump this user's messages posted counter. */
2853 MSGM_syslog(LOG_DEBUG, "Updating user\n");
2854 CtdlGetUserLock(&CCC->user, CCC->curr_user);
2855 CCC->user.posted = CCC->user.posted + 1;
2856 CtdlPutUserLock(&CCC->user);
2858 /* Decide where bounces need to be delivered */
2859 if ((recps != NULL) && (recps->bounce_to != NULL)) {
2860 safestrncpy(bounce_to, recps->bounce_to, sizeof bounce_to);
2862 else if (CCC->logged_in) {
2863 snprintf(bounce_to, sizeof bounce_to, "%s@%s", CCC->user.fullname, config.c_nodename);
2866 snprintf(bounce_to, sizeof bounce_to, "%s@%s", msg->cm_fields[eAuthor], msg->cm_fields[eNodeName]);
2869 /* If this is private, local mail, make a copy in the
2870 * recipient's mailbox and bump the reference count.
2872 if ((recps != NULL) && (recps->num_local > 0))
2873 for (i=0; i<num_tokens(recps->recp_local, '|'); ++i) {
2875 recipientlen = extract_token(recipient,
2876 recps->recp_local, i,
2877 '|', sizeof recipient);
2878 MSG_syslog(LOG_DEBUG, "Delivering private local mail to <%s>\n",
2880 if (CtdlGetUser(&userbuf, recipient) == 0) {
2881 CtdlMailboxName(actual_rm, sizeof actual_rm, &userbuf, MAILROOM);
2882 CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0, msg);
2883 CtdlBumpNewMailCounter(userbuf.usernum);
2884 if (!IsEmptyStr(config.c_funambol_host) || !IsEmptyStr(config.c_pager_program)) {
2885 /* Generate a instruction message for the Funambol notification
2886 * server, in the same style as the SMTP queue
2890 instr = malloc(instr_alloc);
2891 instrlen = snprintf(
2893 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
2897 (long)time(NULL), //todo: time() is expensive!
2901 imsg = malloc(sizeof(struct CtdlMessage));
2902 memset(imsg, 0, sizeof(struct CtdlMessage));
2903 imsg->cm_magic = CTDLMESSAGE_MAGIC;
2904 imsg->cm_anon_type = MES_NORMAL;
2905 imsg->cm_format_type = FMT_RFC822;
2906 CM_SetField(imsg, eMsgSubject, HKEY("QMSG"));
2907 CM_SetField(imsg, eAuthor, HKEY("Citadel"));
2908 CM_SetField(imsg, eJournal, HKEY("do not journal"));
2909 CM_SetAsField(imsg, eMesageText, &instr, instrlen);
2910 CM_SetField(imsg, eExtnotify, recipient, recipientlen);
2911 CtdlSubmitMsg(imsg, NULL, FNBL_QUEUE_ROOM, 0);
2916 MSG_syslog(LOG_DEBUG, "No user <%s>\n", recipient);
2917 CtdlSaveMsgPointerInRoom(config.c_aideroom, newmsgid, 0, msg);
2921 /* Perform "after save" hooks */
2922 MSGM_syslog(LOG_DEBUG, "Performing after-save hooks\n");
2924 CM_SetFieldLONG(msg, eVltMsgNum, newmsgid);
2925 PerformMessageHooks(msg, EVT_AFTERSAVE);
2926 CM_FlushField(msg, eVltMsgNum);
2928 /* For IGnet mail, we have to save a new copy into the spooler for
2929 * each recipient, with the R and D fields set to the recipient and
2930 * destination-node. This has two ugly side effects: all other
2931 * recipients end up being unlisted in this recipient's copy of the
2932 * message, and it has to deliver multiple messages to the same
2933 * node. We'll revisit this again in a year or so when everyone has
2934 * a network spool receiver that can handle the new style messages.
2936 if ((recps != NULL) && (recps->num_ignet > 0))
2937 for (i=0; i<num_tokens(recps->recp_ignet, '|'); ++i) {
2938 extract_token(recipient, recps->recp_ignet, i,
2939 '|', sizeof recipient);
2941 hold_R = msg->cm_fields[eRecipient];
2942 hold_D = msg->cm_fields[eDestination];
2943 msg->cm_fields[eRecipient] = malloc(SIZ);
2944 msg->cm_fields[eDestination] = malloc(128);
2945 extract_token(msg->cm_fields[eRecipient], recipient, 0, '@', SIZ);
2946 extract_token(msg->cm_fields[eDestination], recipient, 1, '@', 128);
2948 CtdlSerializeMessage(&smr, msg);
2950 snprintf(submit_filename, sizeof submit_filename,
2951 "%s/netmail.%04lx.%04x.%04x",
2953 (long) getpid(), CCC->cs_pid, ++seqnum);
2954 network_fp = fopen(submit_filename, "wb+");
2955 if (network_fp != NULL) {
2956 rv = fwrite(smr.ser, smr.len, 1, network_fp);
2958 MSG_syslog(LOG_EMERG, "CtdlSubmitMsg(): Couldn't write network spool file: %s\n",
2966 free(msg->cm_fields[eRecipient]);
2967 free(msg->cm_fields[eDestination]);
2968 msg->cm_fields[eRecipient] = hold_R;
2969 msg->cm_fields[eDestination] = hold_D;
2972 /* Go back to the room we started from */
2973 MSG_syslog(LOG_DEBUG, "Returning to original room %s\n", hold_rm);
2974 if (strcasecmp(hold_rm, CCC->room.QRname))
2975 CtdlUserGoto(hold_rm, 0, 1, NULL, NULL);
2977 /* For internet mail, generate delivery instructions.
2978 * Yes, this is recursive. Deal with it. Infinite recursion does
2979 * not happen because the delivery instructions message does not
2980 * contain a recipient.
2982 if ((recps != NULL) && (recps->num_internet > 0)) {
2983 StrBuf *SpoolMsg = NewStrBuf();
2986 MSGM_syslog(LOG_DEBUG, "Generating delivery instructions\n");
2988 StrBufPrintf(SpoolMsg,
2989 "Content-type: "SPOOLMIME"\n"
2998 if (recps->envelope_from != NULL) {
2999 StrBufAppendBufPlain(SpoolMsg, HKEY("envelope_from|"), 0);
3000 StrBufAppendBufPlain(SpoolMsg, recps->envelope_from, -1, 0);
3001 StrBufAppendBufPlain(SpoolMsg, HKEY("\n"), 0);
3003 if (recps->sending_room != NULL) {
3004 StrBufAppendBufPlain(SpoolMsg, HKEY("source_room|"), 0);
3005 StrBufAppendBufPlain(SpoolMsg, recps->sending_room, -1, 0);
3006 StrBufAppendBufPlain(SpoolMsg, HKEY("\n"), 0);
3009 nTokens = num_tokens(recps->recp_internet, '|');
3010 for (i = 0; i < nTokens; i++) {
3012 len = extract_token(recipient, recps->recp_internet, i, '|', sizeof recipient);
3014 StrBufAppendBufPlain(SpoolMsg, HKEY("remote|"), 0);
3015 StrBufAppendBufPlain(SpoolMsg, recipient, len, 0);
3016 StrBufAppendBufPlain(SpoolMsg, HKEY("|0||\n"), 0);
3020 imsg = malloc(sizeof(struct CtdlMessage));
3021 memset(imsg, 0, sizeof(struct CtdlMessage));
3022 imsg->cm_magic = CTDLMESSAGE_MAGIC;
3023 imsg->cm_anon_type = MES_NORMAL;
3024 imsg->cm_format_type = FMT_RFC822;
3025 imsg->cm_fields[eMsgSubject] = strdup("QMSG");
3026 imsg->cm_fields[eAuthor] = strdup("Citadel");
3027 imsg->cm_fields[eJournal] = strdup("do not journal");
3028 imsg->cm_fields[eMesageText] = SmashStrBuf(&SpoolMsg); /* imsg owns this memory now */
3029 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM, QP_EADDR);
3034 * Any addresses to harvest for someone's address book?
3036 if ( (CCC->logged_in) && (recps != NULL) ) {
3037 collected_addresses = harvest_collected_addresses(msg);
3040 if (collected_addresses != NULL) {
3041 aptr = (struct addresses_to_be_filed *)
3042 malloc(sizeof(struct addresses_to_be_filed));
3043 CtdlMailboxName(actual_rm, sizeof actual_rm,
3044 &CCC->user, USERCONTACTSROOM);
3045 aptr->roomname = strdup(actual_rm);
3046 aptr->collected_addresses = collected_addresses;
3047 begin_critical_section(S_ATBF);
3050 end_critical_section(S_ATBF);
3054 * Determine whether this message qualifies for journaling.
3056 if (!CM_IsEmpty(msg, eJournal)) {
3057 qualified_for_journaling = 0;
3060 if (recps == NULL) {
3061 qualified_for_journaling = config.c_journal_pubmsgs;
3063 else if (recps->num_local + recps->num_ignet + recps->num_internet > 0) {
3064 qualified_for_journaling = config.c_journal_email;
3067 qualified_for_journaling = config.c_journal_pubmsgs;
3072 * Do we have to perform journaling? If so, hand off the saved
3073 * RFC822 version will be handed off to the journaler for background
3074 * submit. Otherwise, we have to free the memory ourselves.
3076 if (saved_rfc822_version != NULL) {
3077 if (qualified_for_journaling) {
3078 JournalBackgroundSubmit(msg, saved_rfc822_version, recps);
3081 FreeStrBuf(&saved_rfc822_version);
3091 * Convenience function for generating small administrative messages.
3093 void quickie_message(const char *from,
3094 const char *fromaddr,
3099 const char *subject)
3101 struct CtdlMessage *msg;
3102 struct recptypes *recp = NULL;
3104 msg = malloc(sizeof(struct CtdlMessage));
3105 memset(msg, 0, sizeof(struct CtdlMessage));
3106 msg->cm_magic = CTDLMESSAGE_MAGIC;
3107 msg->cm_anon_type = MES_NORMAL;
3108 msg->cm_format_type = format_type;
3111 msg->cm_fields[eAuthor] = strdup(from);
3113 else if (fromaddr != NULL) {
3114 msg->cm_fields[eAuthor] = strdup(fromaddr);
3115 if (strchr(msg->cm_fields[eAuthor], '@')) {
3116 *strchr(msg->cm_fields[eAuthor], '@') = 0;
3120 msg->cm_fields[eAuthor] = strdup("Citadel");
3123 if (fromaddr != NULL) msg->cm_fields[erFc822Addr] = strdup(fromaddr);
3124 if (room != NULL) msg->cm_fields[eOriginalRoom] = strdup(room);
3125 msg->cm_fields[eNodeName] = strdup(NODENAME);
3127 msg->cm_fields[eRecipient] = strdup(to);
3128 recp = validate_recipients(to, NULL, 0);
3130 if (subject != NULL) {
3131 msg->cm_fields[eMsgSubject] = strdup(subject);
3133 msg->cm_fields[eMesageText] = strdup(text);
3135 CtdlSubmitMsg(msg, recp, room, 0);
3137 if (recp != NULL) free_recipients(recp);
3140 void flood_protect_quickie_message(const char *from,
3141 const char *fromaddr,
3146 const char *subject,
3148 const char **CritStr,
3155 u_char rawdigest[MD5_DIGEST_LEN];
3156 struct MD5Context md5context;
3160 time_t tsday = NOW / (8*60*60); /* just care for a day... */
3162 tslen = snprintf(timestamp, sizeof(timestamp), "%ld", tsday);
3163 MD5Init(&md5context);
3165 for (i = 0; i < nCriterions; i++)
3166 MD5Update(&md5context,
3167 (const unsigned char*)CritStr[i], CritStrLen[i]);
3168 MD5Update(&md5context,
3169 (const unsigned char*)timestamp, tslen);
3170 MD5Final(rawdigest, &md5context);
3172 guid = NewStrBufPlain(NULL,
3173 MD5_DIGEST_LEN * 2 + 12);
3174 StrBufHexEscAppend(guid, NULL, rawdigest, MD5_DIGEST_LEN);
3175 StrBufAppendBufPlain(guid, HKEY("_fldpt"), 0);
3176 if (StrLength(guid) > 40)
3177 StrBufCutAt(guid, 40, NULL);
3179 if (CheckIfAlreadySeen("FPAideMessage",
3188 /* yes, we did. flood protection kicks in. */
3190 "not sending message again\n");
3194 /* no, this message isn't sent recently; go ahead. */
3195 quickie_message(from,
3206 * Back end function used by CtdlMakeMessage() and similar functions
3208 StrBuf *CtdlReadMessageBodyBuf(char *terminator, /* token signalling EOT */
3210 size_t maxlen, /* maximum message length */
3211 StrBuf *exist, /* if non-null, append to it;
3212 exist is ALWAYS freed */
3213 int crlf, /* CRLF newlines instead of LF */
3214 int *sock /* socket handle or 0 for this session's client socket */
3223 LineBuf = NewStrBufPlain(NULL, SIZ);
3224 if (exist == NULL) {
3225 Message = NewStrBufPlain(NULL, 4 * SIZ);
3228 Message = NewStrBufDup(exist);
3231 /* Do we need to change leading ".." to "." for SMTP escaping? */
3232 if ((tlen == 1) && (*terminator == '.')) {
3236 /* read in the lines of message text one by one */
3239 if ((CtdlSockGetLine(sock, LineBuf, 5) < 0) ||
3244 if (CtdlClientGetLine(LineBuf) < 0) finished = 1;
3246 if ((StrLength(LineBuf) == tlen) &&
3247 (!strcmp(ChrPtr(LineBuf), terminator)))
3250 if ( (!flushing) && (!finished) ) {
3252 StrBufAppendBufPlain(LineBuf, HKEY("\r\n"), 0);
3255 StrBufAppendBufPlain(LineBuf, HKEY("\n"), 0);
3258 /* Unescape SMTP-style input of two dots at the beginning of the line */
3260 (StrLength(LineBuf) == 2) &&
3261 (!strcmp(ChrPtr(LineBuf), "..")))
3263 StrBufCutLeft(LineBuf, 1);
3266 StrBufAppendBuf(Message, LineBuf, 0);
3269 /* if we've hit the max msg length, flush the rest */
3270 if (StrLength(Message) >= maxlen) flushing = 1;
3272 } while (!finished);
3273 FreeStrBuf(&LineBuf);
3277 void DeleteAsyncMsg(ReadAsyncMsg **Msg)
3281 FreeStrBuf(&(*Msg)->MsgBuf);
3287 ReadAsyncMsg *NewAsyncMsg(const char *terminator, /* token signalling EOT */
3289 size_t maxlen, /* maximum message length */
3290 size_t expectlen, /* if we expect a message, how long should it be? */
3291 StrBuf *exist, /* if non-null, append to it;
3292 exist is ALWAYS freed */
3293 long eLen, /* length of exist */
3294 int crlf /* CRLF newlines instead of LF */
3297 ReadAsyncMsg *NewMsg;
3299 NewMsg = (ReadAsyncMsg *)malloc(sizeof(ReadAsyncMsg));
3300 memset(NewMsg, 0, sizeof(ReadAsyncMsg));
3302 if (exist == NULL) {
3305 if (expectlen == 0) {
3309 len = expectlen + 10;
3311 NewMsg->MsgBuf = NewStrBufPlain(NULL, len);
3314 NewMsg->MsgBuf = NewStrBufDup(exist);
3316 /* Do we need to change leading ".." to "." for SMTP escaping? */
3317 if ((tlen == 1) && (*terminator == '.')) {
3321 NewMsg->terminator = terminator;
3322 NewMsg->tlen = tlen;
3324 NewMsg->maxlen = maxlen;
3326 NewMsg->crlf = crlf;
3332 * Back end function used by CtdlMakeMessage() and similar functions
3334 eReadState CtdlReadMessageBodyAsync(AsyncIO *IO)
3336 ReadAsyncMsg *ReadMsg;
3337 int MsgFinished = 0;
3338 eReadState Finished = eMustReadMore;
3343 const char *pch = ChrPtr(IO->SendBuf.Buf);
3344 const char *pchh = IO->SendBuf.ReadWritePointer;
3350 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
3351 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
3352 ((CitContext*)(IO->CitContext))->ServiceName,
3355 fd = fopen(fn, "a+");
3358 ReadMsg = IO->ReadMsg;
3360 /* read in the lines of message text one by one */
3362 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
3365 case eMustReadMore: /// read new from socket...
3367 if (IO->RecvBuf.ReadWritePointer != NULL) {
3368 nbytes = StrLength(IO->RecvBuf.Buf) - (IO->RecvBuf.ReadWritePointer - ChrPtr(IO->RecvBuf.Buf));
3369 fprintf(fd, "Read; Line unfinished: %ld Bytes still in buffer [", nbytes);
3371 fwrite(IO->RecvBuf.ReadWritePointer, nbytes, 1, fd);
3375 fprintf(fd, "BufferEmpty! \n");
3381 case eBufferNotEmpty: /* shouldn't happen... */
3382 case eReadSuccess: /// done for now...
3384 case eReadFail: /// WHUT?
3390 if ((StrLength(IO->IOBuf) == ReadMsg->tlen) &&
3391 (!strcmp(ChrPtr(IO->IOBuf), ReadMsg->terminator))) {
3394 fprintf(fd, "found Terminator; Message Size: %d\n", StrLength(ReadMsg->MsgBuf));
3397 else if (!ReadMsg->flushing) {
3400 fprintf(fd, "Read Line: [%d][%s]\n", StrLength(IO->IOBuf), ChrPtr(IO->IOBuf));
3403 /* Unescape SMTP-style input of two dots at the beginning of the line */
3404 if ((ReadMsg->dodot) &&
3405 (StrLength(IO->IOBuf) == 2) && /* TODO: do we just unescape lines with two dots or any line? */
3406 (!strcmp(ChrPtr(IO->IOBuf), "..")))
3409 fprintf(fd, "UnEscaped!\n");
3411 StrBufCutLeft(IO->IOBuf, 1);
3414 if (ReadMsg->crlf) {
3415 StrBufAppendBufPlain(IO->IOBuf, HKEY("\r\n"), 0);
3418 StrBufAppendBufPlain(IO->IOBuf, HKEY("\n"), 0);
3421 StrBufAppendBuf(ReadMsg->MsgBuf, IO->IOBuf, 0);
3424 /* if we've hit the max msg length, flush the rest */
3425 if (StrLength(ReadMsg->MsgBuf) >= ReadMsg->maxlen) ReadMsg->flushing = 1;
3427 } while (!MsgFinished);
3430 fprintf(fd, "Done with reading; %s.\n, ",
3431 (MsgFinished)?"Message Finished": "FAILED");
3435 return eReadSuccess;
3442 * Back end function used by CtdlMakeMessage() and similar functions
3444 char *CtdlReadMessageBody(char *terminator, /* token signalling EOT */
3446 size_t maxlen, /* maximum message length */
3447 StrBuf *exist, /* if non-null, append to it;
3448 exist is ALWAYS freed */
3449 int crlf, /* CRLF newlines instead of LF */
3450 int *sock /* socket handle or 0 for this session's client socket */
3455 Message = CtdlReadMessageBodyBuf(terminator,
3461 if (Message == NULL)
3464 return SmashStrBuf(&Message);
3469 * Build a binary message to be saved on disk.
3470 * (NOTE: if you supply 'preformatted_text', the buffer you give it
3471 * will become part of the message. This means you are no longer
3472 * responsible for managing that memory -- it will be freed along with
3473 * the rest of the fields when CM_Free() is called.)
3476 struct CtdlMessage *CtdlMakeMessage(
3477 struct ctdluser *author, /* author's user structure */
3478 char *recipient, /* NULL if it's not mail */
3479 char *recp_cc, /* NULL if it's not mail */
3480 char *room, /* room where it's going */
3481 int type, /* see MES_ types in header file */
3482 int format_type, /* variformat, plain text, MIME... */
3483 char *fake_name, /* who we're masquerading as */
3484 char *my_email, /* which of my email addresses to use (empty is ok) */
3485 char *subject, /* Subject (optional) */
3486 char *supplied_euid, /* ...or NULL if this is irrelevant */
3487 char *preformatted_text, /* ...or NULL to read text from client */
3488 char *references /* Thread references */
3490 char dest_node[256];
3492 struct CtdlMessage *msg;
3494 StrBuf *FakeEncAuthor = NULL;
3496 msg = malloc(sizeof(struct CtdlMessage));
3497 memset(msg, 0, sizeof(struct CtdlMessage));
3498 msg->cm_magic = CTDLMESSAGE_MAGIC;
3499 msg->cm_anon_type = type;
3500 msg->cm_format_type = format_type;
3502 /* Don't confuse the poor folks if it's not routed mail. */
3503 strcpy(dest_node, "");
3505 if (recipient != NULL) striplt(recipient);
3506 if (recp_cc != NULL) striplt(recp_cc);
3508 /* Path or Return-Path */
3509 if (my_email == NULL) my_email = "";
3511 if (!IsEmptyStr(my_email)) {
3512 msg->cm_fields[eMessagePath] = strdup(my_email);
3515 snprintf(buf, sizeof buf, "%s", author->fullname);
3516 msg->cm_fields[eMessagePath] = strdup(buf);
3518 convert_spaces_to_underscores(msg->cm_fields[eMessagePath]);
3520 snprintf(buf, sizeof buf, "%ld", (long)time(NULL)); /* timestamp */
3521 msg->cm_fields[eTimestamp] = strdup(buf);
3523 if ((fake_name != NULL) && (fake_name[0])) { /* author */
3524 FakeAuthor = NewStrBufPlain (fake_name, -1);
3527 FakeAuthor = NewStrBufPlain (author->fullname, -1);
3529 StrBufRFC2047encode(&FakeEncAuthor, FakeAuthor);
3530 msg->cm_fields[eAuthor] = SmashStrBuf(&FakeEncAuthor);
3531 FreeStrBuf(&FakeAuthor);
3533 if (CC->room.QRflags & QR_MAILBOX) { /* room */
3534 msg->cm_fields[eOriginalRoom] = strdup(&CC->room.QRname[11]);
3537 msg->cm_fields[eOriginalRoom] = strdup(CC->room.QRname);
3540 msg->cm_fields[eNodeName] = strdup(NODENAME); /* nodename */
3541 msg->cm_fields[eHumanNode] = strdup(HUMANNODE); /* hnodename */
3543 if ((recipient != NULL) && (recipient[0] != 0)) {
3544 msg->cm_fields[eRecipient] = strdup(recipient);
3546 if ((recp_cc != NULL) && (recp_cc[0] != 0)) {
3547 msg->cm_fields[eCarbonCopY] = strdup(recp_cc);
3549 if (dest_node[0] != 0) {
3550 msg->cm_fields[eDestination] = strdup(dest_node);
3553 if (!IsEmptyStr(my_email)) {
3554 msg->cm_fields[erFc822Addr] = strdup(my_email);
3556 else if ( (author == &CC->user) && (!IsEmptyStr(CC->cs_inet_email)) ) {
3557 msg->cm_fields[erFc822Addr] = strdup(CC->cs_inet_email);
3560 if (subject != NULL) {
3563 length = strlen(subject);
3569 while ((subject[i] != '\0') &&
3570 (IsAscii = isascii(subject[i]) != 0 ))
3573 msg->cm_fields[eMsgSubject] = strdup(subject);
3574 else /* ok, we've got utf8 in the string. */
3576 msg->cm_fields[eMsgSubject] = rfc2047encode(subject, length);
3582 if (supplied_euid != NULL) {
3583 msg->cm_fields[eExclusiveID] = strdup(supplied_euid);
3586 if ((references != NULL) && (!IsEmptyStr(references))) {
3587 if (msg->cm_fields[eWeferences] != NULL)
3588 free(msg->cm_fields[eWeferences]);
3589 msg->cm_fields[eWeferences] = strdup(references);
3592 if (preformatted_text != NULL) {
3593 msg->cm_fields[eMesageText] = preformatted_text;
3596 msg->cm_fields[eMesageText] = CtdlReadMessageBody(HKEY("000"), config.c_maxmsglen, NULL, 0, 0);
3606 * API function to delete messages which match a set of criteria
3607 * (returns the actual number of messages deleted)
3609 int CtdlDeleteMessages(char *room_name, /* which room */
3610 long *dmsgnums, /* array of msg numbers to be deleted */
3611 int num_dmsgnums, /* number of msgs to be deleted, or 0 for "any" */
3612 char *content_type /* or "" for any. regular expressions expected. */
3615 struct CitContext *CCC = CC;
3616 struct ctdlroom qrbuf;
3617 struct cdbdata *cdbfr;
3618 long *msglist = NULL;
3619 long *dellist = NULL;
3622 int num_deleted = 0;
3624 struct MetaData smi;
3627 int need_to_free_re = 0;
3629 if (content_type) if (!IsEmptyStr(content_type)) {
3630 regcomp(&re, content_type, 0);
3631 need_to_free_re = 1;
3633 MSG_syslog(LOG_DEBUG, " CtdlDeleteMessages(%s, %d msgs, %s)\n",
3634 room_name, num_dmsgnums, content_type);
3636 /* get room record, obtaining a lock... */
3637 if (CtdlGetRoomLock(&qrbuf, room_name) != 0) {
3638 MSG_syslog(LOG_ERR, " CtdlDeleteMessages(): Room <%s> not found\n",
3640 if (need_to_free_re) regfree(&re);
3641 return (0); /* room not found */
3643 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
3645 if (cdbfr != NULL) {
3646 dellist = malloc(cdbfr->len);
3647 msglist = (long *) cdbfr->ptr;
3648 cdbfr->ptr = NULL; /* CtdlDeleteMessages() now owns this memory */
3649 num_msgs = cdbfr->len / sizeof(long);
3653 int have_contenttype = (content_type != NULL) && !IsEmptyStr(content_type);
3654 int have_delmsgs = (num_dmsgnums == 0) || (dmsgnums == NULL);
3655 int have_more_del = 1;
3657 num_msgs = sort_msglist(msglist, num_msgs);
3658 if (num_dmsgnums > 1)
3659 num_dmsgnums = sort_msglist(dmsgnums, num_dmsgnums);
3662 StrBuf *dbg = NewStrBuf();
3663 for (i = 0; i < num_dmsgnums; i++)
3664 StrBufAppendPrintf(dbg, ", %ld", dmsgnums[i]);
3665 MSG_syslog(LOG_DEBUG, " Deleting before: %s", ChrPtr(dbg));
3670 while ((i < num_msgs) && (have_more_del)) {
3673 /* Set/clear a bit for each criterion */
3675 /* 0 messages in the list or a null list means that we are
3676 * interested in deleting any messages which meet the other criteria.
3679 delete_this |= 0x01;
3682 while ((i < num_msgs) && (msglist[i] < dmsgnums[j])) i++;
3687 if (msglist[i] == dmsgnums[j]) {
3688 delete_this |= 0x01;
3691 have_more_del = (j < num_dmsgnums);
3694 if (have_contenttype) {
3695 GetMetaData(&smi, msglist[i]);
3696 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) == 0) {
3697 delete_this |= 0x02;
3700 delete_this |= 0x02;
3703 /* Delete message only if all bits are set */
3704 if (delete_this == 0x03) {
3705 dellist[num_deleted++] = msglist[i];
3712 StrBuf *dbg = NewStrBuf();
3713 for (i = 0; i < num_deleted; i++)
3714 StrBufAppendPrintf(dbg, ", %ld", dellist[i]);
3715 MSG_syslog(LOG_DEBUG, " Deleting: %s", ChrPtr(dbg));
3719 num_msgs = sort_msglist(msglist, num_msgs);
3720 cdb_store(CDB_MSGLISTS, &qrbuf.QRnumber, (int)sizeof(long),
3721 msglist, (int)(num_msgs * sizeof(long)));
3724 qrbuf.QRhighest = msglist[num_msgs - 1];
3726 qrbuf.QRhighest = 0;
3728 CtdlPutRoomLock(&qrbuf);
3730 /* Go through the messages we pulled out of the index, and decrement
3731 * their reference counts by 1. If this is the only room the message
3732 * was in, the reference count will reach zero and the message will
3733 * automatically be deleted from the database. We do this in a
3734 * separate pass because there might be plug-in hooks getting called,
3735 * and we don't want that happening during an S_ROOMS critical
3739 for (i=0; i<num_deleted; ++i) {
3740 PerformDeleteHooks(qrbuf.QRname, dellist[i]);
3742 AdjRefCountList(dellist, num_deleted, -1);
3744 /* Now free the memory we used, and go away. */
3745 if (msglist != NULL) free(msglist);
3746 if (dellist != NULL) free(dellist);
3747 MSG_syslog(LOG_DEBUG, " %d message(s) deleted.\n", num_deleted);
3748 if (need_to_free_re) regfree(&re);
3749 return (num_deleted);
3756 * GetMetaData() - Get the supplementary record for a message
3758 void GetMetaData(struct MetaData *smibuf, long msgnum)
3761 struct cdbdata *cdbsmi;
3764 memset(smibuf, 0, sizeof(struct MetaData));
3765 smibuf->meta_msgnum = msgnum;
3766 smibuf->meta_refcount = 1; /* Default reference count is 1 */
3768 /* Use the negative of the message number for its supp record index */
3769 TheIndex = (0L - msgnum);
3771 cdbsmi = cdb_fetch(CDB_MSGMAIN, &TheIndex, sizeof(long));
3772 if (cdbsmi == NULL) {
3773 return; /* record not found; go with defaults */
3775 memcpy(smibuf, cdbsmi->ptr,
3776 ((cdbsmi->len > sizeof(struct MetaData)) ?
3777 sizeof(struct MetaData) : cdbsmi->len));
3784 * PutMetaData() - (re)write supplementary record for a message
3786 void PutMetaData(struct MetaData *smibuf)
3790 /* Use the negative of the message number for the metadata db index */
3791 TheIndex = (0L - smibuf->meta_msgnum);
3793 cdb_store(CDB_MSGMAIN,
3794 &TheIndex, (int)sizeof(long),
3795 smibuf, (int)sizeof(struct MetaData));
3800 * AdjRefCount - submit an adjustment to the reference count for a message.
3801 * (These are just queued -- we actually process them later.)
3803 void AdjRefCount(long msgnum, int incr)
3805 struct CitContext *CCC = CC;
3806 struct arcq new_arcq;
3809 MSG_syslog(LOG_DEBUG, "AdjRefCount() msg %ld ref count delta %+d\n", msgnum, incr);
3811 begin_critical_section(S_SUPPMSGMAIN);
3812 if (arcfp == NULL) {
3813 arcfp = fopen(file_arcq, "ab+");
3814 chown(file_arcq, CTDLUID, (-1));
3815 chmod(file_arcq, 0600);
3817 end_critical_section(S_SUPPMSGMAIN);
3819 /* msgnum < 0 means that we're trying to close the file */
3821 MSGM_syslog(LOG_DEBUG, "Closing the AdjRefCount queue file\n");
3822 begin_critical_section(S_SUPPMSGMAIN);
3823 if (arcfp != NULL) {
3827 end_critical_section(S_SUPPMSGMAIN);
3832 * If we can't open the queue, perform the operation synchronously.
3834 if (arcfp == NULL) {
3835 TDAP_AdjRefCount(msgnum, incr);
3839 new_arcq.arcq_msgnum = msgnum;
3840 new_arcq.arcq_delta = incr;
3841 rv = fwrite(&new_arcq, sizeof(struct arcq), 1, arcfp);
3843 MSG_syslog(LOG_EMERG, "Couldn't write Refcount Queue File %s: %s\n",
3852 void AdjRefCountList(long *msgnum, long nmsg, int incr)
3854 struct CitContext *CCC = CC;
3855 long i, the_size, offset;
3856 struct arcq *new_arcq;
3859 MSG_syslog(LOG_DEBUG, "AdjRefCountList() msg %ld ref count delta %+d\n", nmsg, incr);
3861 begin_critical_section(S_SUPPMSGMAIN);
3862 if (arcfp == NULL) {
3863 arcfp = fopen(file_arcq, "ab+");
3864 chown(file_arcq, CTDLUID, (-1));
3865 chmod(file_arcq, 0600);
3867 end_critical_section(S_SUPPMSGMAIN);
3870 * If we can't open the queue, perform the operation synchronously.
3872 if (arcfp == NULL) {
3873 for (i = 0; i < nmsg; i++)
3874 TDAP_AdjRefCount(msgnum[i], incr);
3878 the_size = sizeof(struct arcq) * nmsg;
3879 new_arcq = malloc(the_size);
3880 for (i = 0; i < nmsg; i++) {
3881 new_arcq[i].arcq_msgnum = msgnum[i];
3882 new_arcq[i].arcq_delta = incr;
3886 while ((rv >= 0) && (offset < the_size))
3888 rv = fwrite(new_arcq + offset, 1, the_size - offset, arcfp);
3890 MSG_syslog(LOG_EMERG, "Couldn't write Refcount Queue File %s: %s\n",
3906 * TDAP_ProcessAdjRefCountQueue()
3908 * Process the queue of message count adjustments that was created by calls
3909 * to AdjRefCount() ... by reading the queue and calling TDAP_AdjRefCount()
3910 * for each one. This should be an "off hours" operation.
3912 int TDAP_ProcessAdjRefCountQueue(void)
3914 struct CitContext *CCC = CC;
3915 char file_arcq_temp[PATH_MAX];
3918 struct arcq arcq_rec;
3919 int num_records_processed = 0;
3921 snprintf(file_arcq_temp, sizeof file_arcq_temp, "%s.%04x", file_arcq, rand());
3923 begin_critical_section(S_SUPPMSGMAIN);
3924 if (arcfp != NULL) {
3929 r = link(file_arcq, file_arcq_temp);
3931 MSG_syslog(LOG_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
3932 end_critical_section(S_SUPPMSGMAIN);
3933 return(num_records_processed);
3937 end_critical_section(S_SUPPMSGMAIN);
3939 fp = fopen(file_arcq_temp, "rb");
3941 MSG_syslog(LOG_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
3942 return(num_records_processed);
3945 while (fread(&arcq_rec, sizeof(struct arcq), 1, fp) == 1) {
3946 TDAP_AdjRefCount(arcq_rec.arcq_msgnum, arcq_rec.arcq_delta);
3947 ++num_records_processed;
3951 r = unlink(file_arcq_temp);
3953 MSG_syslog(LOG_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
3956 return(num_records_processed);
3962 * TDAP_AdjRefCount - adjust the reference count for a message.
3963 * This one does it "for real" because it's called by
3964 * the autopurger function that processes the queue
3965 * created by AdjRefCount(). If a message's reference
3966 * count becomes zero, we also delete the message from
3967 * disk and de-index it.
3969 void TDAP_AdjRefCount(long msgnum, int incr)
3971 struct CitContext *CCC = CC;
3973 struct MetaData smi;
3976 /* This is a *tight* critical section; please keep it that way, as
3977 * it may get called while nested in other critical sections.
3978 * Complicating this any further will surely cause deadlock!
3980 begin_critical_section(S_SUPPMSGMAIN);
3981 GetMetaData(&smi, msgnum);
3982 smi.meta_refcount += incr;
3984 end_critical_section(S_SUPPMSGMAIN);
3985 MSG_syslog(LOG_DEBUG, "TDAP_AdjRefCount() msg %ld ref count delta %+d, is now %d\n",
3986 msgnum, incr, smi.meta_refcount
3989 /* If the reference count is now zero, delete the message
3990 * (and its supplementary record as well).
3992 if (smi.meta_refcount == 0) {
3993 MSG_syslog(LOG_DEBUG, "Deleting message <%ld>\n", msgnum);
3995 /* Call delete hooks with NULL room to show it has gone altogether */
3996 PerformDeleteHooks(NULL, msgnum);
3998 /* Remove from message base */
4000 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
4001 cdb_delete(CDB_BIGMSGS, &delnum, (int)sizeof(long));
4003 /* Remove metadata record */
4004 delnum = (0L - msgnum);
4005 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
4011 * Write a generic object to this room
4013 * Note: this could be much more efficient. Right now we use two temporary
4014 * files, and still pull the message into memory as with all others.
4016 void CtdlWriteObject(char *req_room, /* Room to stuff it in */
4017 char *content_type, /* MIME type of this object */
4018 char *raw_message, /* Data to be written */
4019 off_t raw_length, /* Size of raw_message */
4020 struct ctdluser *is_mailbox, /* Mailbox room? */
4021 int is_binary, /* Is encoding necessary? */
4022 int is_unique, /* Del others of this type? */
4023 unsigned int flags /* Internal save flags */
4026 struct CitContext *CCC = CC;
4027 struct ctdlroom qrbuf;
4028 char roomname[ROOMNAMELEN];
4029 struct CtdlMessage *msg;
4030 char *encoded_message = NULL;
4032 if (is_mailbox != NULL) {
4033 CtdlMailboxName(roomname, sizeof roomname, is_mailbox, req_room);
4036 safestrncpy(roomname, req_room, sizeof(roomname));
4039 MSG_syslog(LOG_DEBUG, "Raw length is %ld\n", (long)raw_length);
4042 encoded_message = malloc((size_t) (((raw_length * 134) / 100) + 4096 ) );
4045 encoded_message = malloc((size_t)(raw_length + 4096));
4048 sprintf(encoded_message, "Content-type: %s\n", content_type);
4051 sprintf(&encoded_message[strlen(encoded_message)],
4052 "Content-transfer-encoding: base64\n\n"
4056 sprintf(&encoded_message[strlen(encoded_message)],
4057 "Content-transfer-encoding: 7bit\n\n"
4063 &encoded_message[strlen(encoded_message)],
4071 &encoded_message[strlen(encoded_message)],
4077 MSGM_syslog(LOG_DEBUG, "Allocating\n");
4078 msg = malloc(sizeof(struct CtdlMessage));
4079 memset(msg, 0, sizeof(struct CtdlMessage));
4080 msg->cm_magic = CTDLMESSAGE_MAGIC;
4081 msg->cm_anon_type = MES_NORMAL;
4082 msg->cm_format_type = 4;
4083 msg->cm_fields[eAuthor] = strdup(CCC->user.fullname);
4084 msg->cm_fields[eOriginalRoom] = strdup(req_room);
4085 msg->cm_fields[eNodeName] = strdup(config.c_nodename);
4086 msg->cm_fields[eHumanNode] = strdup(config.c_humannode);
4087 msg->cm_flags = flags;
4089 msg->cm_fields[eMesageText] = encoded_message;
4091 /* Create the requested room if we have to. */
4092 if (CtdlGetRoom(&qrbuf, roomname) != 0) {
4093 CtdlCreateRoom(roomname,
4094 ( (is_mailbox != NULL) ? 5 : 3 ),
4095 "", 0, 1, 0, VIEW_BBS);
4097 /* If the caller specified this object as unique, delete all
4098 * other objects of this type that are currently in the room.
4101 MSG_syslog(LOG_DEBUG, "Deleted %d other msgs of this type\n",
4102 CtdlDeleteMessages(roomname, NULL, 0, content_type)
4105 /* Now write the data */
4106 CtdlSubmitMsg(msg, NULL, roomname, 0);
4112 /*****************************************************************************/
4113 /* MODULE INITIALIZATION STUFF */
4114 /*****************************************************************************/
4115 void SetMessageDebugEnabled(const int n)
4117 MessageDebugEnabled = n;
4119 CTDL_MODULE_INIT(msgbase)
4122 CtdlRegisterDebugFlagHook(HKEY("messages"), SetMessageDebugEnabled, &MessageDebugEnabled);
4125 /* return our Subversion id for the Log */