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.
18 #include <libcitadel.h>
22 #include "ctdl_module.h"
23 #include "citserver.h"
25 #include "clientsocket.h"
30 #include "internet_addressing.h"
31 #include "euidindex.h"
33 #include "journaling.h"
35 struct addresses_to_be_filed *atbf = NULL;
37 /* This temp file holds the queue of operations for AdjRefCount() */
38 static FILE *arcfp = NULL;
39 void AdjRefCountList(long *msgnum, long nmsg, int incr);
41 int MessageDebugEnabled = 0;
44 * These are the four-character field headers we use when outputting
45 * messages in Citadel format (as opposed to RFC822 format).
48 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
49 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
50 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
51 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
52 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
53 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
54 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
55 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
85 eMsgField FieldOrder[] = {
86 /* Important fields */
97 /* Semi-important fields */
103 /* G is not used yet, may become virus signature*/
106 /* Q is not used yet */
109 /* X is not used yet */
110 /* Z is not used yet */
117 /* Message text (MUST be last) */
119 /* Not saved to disk:
124 static const long NDiskFields = sizeof(FieldOrder) / sizeof(eMsgField);
126 int CM_IsEmpty(struct CtdlMessage *Msg, eMsgField which)
128 return !((Msg->cm_fields[which] != NULL) &&
129 (Msg->cm_fields[which][0] != '\0'));
132 void CM_SetField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
134 if (Msg->cm_fields[which] != NULL)
135 free (Msg->cm_fields[which]);
136 Msg->cm_fields[which] = malloc(length + 1);
137 memcpy(Msg->cm_fields[which], buf, length);
138 Msg->cm_fields[which][length] = '\0';
141 void CM_SetFieldLONG(struct CtdlMessage *Msg, eMsgField which, long lvalue)
145 len = snprintf(buf, sizeof(buf), "%ld", lvalue);
146 CM_SetField(Msg, which, buf, len);
148 void CM_CutFieldAt(struct CtdlMessage *Msg, eMsgField WhichToCut, long maxlen)
150 if (Msg->cm_fields[WhichToCut] == NULL)
153 if (strlen(Msg->cm_fields[WhichToCut]) > maxlen)
154 Msg->cm_fields[WhichToCut][maxlen] = '\0';
157 void CM_FlushField(struct CtdlMessage *Msg, eMsgField which)
159 if (Msg->cm_fields[which] != NULL)
160 free (Msg->cm_fields[which]);
161 Msg->cm_fields[which] = NULL;
163 void CM_Flush(struct CtdlMessage *Msg)
167 if (CM_IsValidMsg(Msg) == 0)
170 for (i = 0; i < 256; ++i)
172 CM_FlushField(Msg, i);
176 void CM_CopyField(struct CtdlMessage *Msg, eMsgField WhichToPutTo, eMsgField WhichtToCopy)
179 if (Msg->cm_fields[WhichToPutTo] != NULL)
180 free (Msg->cm_fields[WhichToPutTo]);
182 if (Msg->cm_fields[WhichtToCopy] != NULL)
184 len = strlen(Msg->cm_fields[WhichtToCopy]);
185 Msg->cm_fields[WhichToPutTo] = malloc(len + 1);
186 memcpy(Msg->cm_fields[WhichToPutTo], Msg->cm_fields[WhichtToCopy], len);
187 Msg->cm_fields[WhichToPutTo][len] = '\0';
190 Msg->cm_fields[WhichToPutTo] = NULL;
194 void CM_PrependToField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
196 if (Msg->cm_fields[which] != NULL) {
201 oldmsgsize = strlen(Msg->cm_fields[which]) + 1;
202 newmsgsize = length + oldmsgsize;
204 new = malloc(newmsgsize);
205 memcpy(new, buf, length);
206 memcpy(new + length, Msg->cm_fields[which], oldmsgsize);
207 free(Msg->cm_fields[which]);
208 Msg->cm_fields[which] = new;
211 Msg->cm_fields[which] = malloc(length + 1);
212 memcpy(Msg->cm_fields[which], buf, length);
213 Msg->cm_fields[which][length] = '\0';
217 void CM_SetAsField(struct CtdlMessage *Msg, eMsgField which, char **buf, long length)
219 if (Msg->cm_fields[which] != NULL)
220 free (Msg->cm_fields[which]);
222 Msg->cm_fields[which] = *buf;
226 void CM_SetAsFieldSB(struct CtdlMessage *Msg, eMsgField which, StrBuf **buf)
228 if (Msg->cm_fields[which] != NULL)
229 free (Msg->cm_fields[which]);
231 Msg->cm_fields[which] = SmashStrBuf(buf);
234 void CM_GetAsField(struct CtdlMessage *Msg, eMsgField which, char **ret, long *retlen)
236 if (Msg->cm_fields[which] != NULL)
238 *retlen = strlen(Msg->cm_fields[which]);
239 *ret = Msg->cm_fields[which];
240 Msg->cm_fields[which] = NULL;
250 * Returns 1 if the supplied pointer points to a valid Citadel message.
251 * If the pointer is NULL or the magic number check fails, returns 0.
253 int CM_IsValidMsg(struct CtdlMessage *msg) {
256 if ((msg->cm_magic) != CTDLMESSAGE_MAGIC) {
257 struct CitContext *CCC = CC;
258 MSGM_syslog(LOG_WARNING, "CM_IsValidMsg() -- self-check failed\n");
264 void CM_FreeContents(struct CtdlMessage *msg)
268 for (i = 0; i < 256; ++i)
269 if (msg->cm_fields[i] != NULL) {
270 free(msg->cm_fields[i]);
273 msg->cm_magic = 0; /* just in case */
276 * 'Destructor' for struct CtdlMessage
278 void CM_Free(struct CtdlMessage *msg)
280 if (CM_IsValidMsg(msg) == 0)
282 if (msg != NULL) free (msg);
285 CM_FreeContents(msg);
289 int CM_DupField(eMsgField i, struct CtdlMessage *OrgMsg, struct CtdlMessage *NewMsg)
292 len = strlen(OrgMsg->cm_fields[i]);
293 NewMsg->cm_fields[i] = malloc(len + 1);
294 if (NewMsg->cm_fields[i] == NULL)
296 memcpy(NewMsg->cm_fields[i], OrgMsg->cm_fields[i], len);
297 NewMsg->cm_fields[i][len] = '\0';
301 struct CtdlMessage * CM_Duplicate(struct CtdlMessage *OrgMsg)
304 struct CtdlMessage *NewMsg;
306 if (CM_IsValidMsg(OrgMsg) == 0)
308 NewMsg = (struct CtdlMessage *)malloc(sizeof(struct CtdlMessage));
312 memcpy(NewMsg, OrgMsg, sizeof(struct CtdlMessage));
314 memset(&NewMsg->cm_fields, 0, sizeof(char*) * 256);
316 for (i = 0; i < 256; ++i)
318 if (OrgMsg->cm_fields[i] != NULL)
320 if (!CM_DupField(i, OrgMsg, NewMsg))
335 /* Determine if a given message matches the fields in a message template.
336 * Return 0 for a successful match.
338 int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template) {
341 /* If there aren't any fields in the template, all messages will
344 if (template == NULL) return(0);
346 /* Null messages are bogus. */
347 if (msg == NULL) return(1);
349 for (i='A'; i<='Z'; ++i) {
350 if (template->cm_fields[i] != NULL) {
351 if (msg->cm_fields[i] == NULL) {
352 /* Considered equal if temmplate is empty string */
353 if (IsEmptyStr(template->cm_fields[i])) continue;
356 if (strcasecmp(msg->cm_fields[i],
357 template->cm_fields[i])) return 1;
361 /* All compares succeeded: we have a match! */
368 * Retrieve the "seen" message list for the current room.
370 void CtdlGetSeen(char *buf, int which_set) {
371 struct CitContext *CCC = CC;
374 /* Learn about the user and room in question */
375 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
377 if (which_set == ctdlsetseen_seen)
378 safestrncpy(buf, vbuf.v_seen, SIZ);
379 if (which_set == ctdlsetseen_answered)
380 safestrncpy(buf, vbuf.v_answered, SIZ);
386 * Manipulate the "seen msgs" string (or other message set strings)
388 void CtdlSetSeen(long *target_msgnums, int num_target_msgnums,
389 int target_setting, int which_set,
390 struct ctdluser *which_user, struct ctdlroom *which_room) {
391 struct CitContext *CCC = CC;
392 struct cdbdata *cdbfr;
397 long hi = (-1L); /// TODO: we just write here. y?
406 char *is_set; /* actually an array of booleans */
408 /* Don't bother doing *anything* if we were passed a list of zero messages */
409 if (num_target_msgnums < 1) {
413 /* If no room was specified, we go with the current room. */
415 which_room = &CCC->room;
418 /* If no user was specified, we go with the current user. */
420 which_user = &CCC->user;
423 MSG_syslog(LOG_DEBUG, "CtdlSetSeen(%d msgs starting with %ld, %s, %d) in <%s>\n",
424 num_target_msgnums, target_msgnums[0],
425 (target_setting ? "SET" : "CLEAR"),
429 /* Learn about the user and room in question */
430 CtdlGetRelationship(&vbuf, which_user, which_room);
432 /* Load the message list */
433 cdbfr = cdb_fetch(CDB_MSGLISTS, &which_room->QRnumber, sizeof(long));
435 msglist = (long *) cdbfr->ptr;
436 cdbfr->ptr = NULL; /* CtdlSetSeen() now owns this memory */
437 num_msgs = cdbfr->len / sizeof(long);
440 return; /* No messages at all? No further action. */
443 is_set = malloc(num_msgs * sizeof(char));
444 memset(is_set, 0, (num_msgs * sizeof(char)) );
446 /* Decide which message set we're manipulating */
448 case ctdlsetseen_seen:
449 vset = NewStrBufPlain(vbuf.v_seen, -1);
451 case ctdlsetseen_answered:
452 vset = NewStrBufPlain(vbuf.v_answered, -1);
459 #if 0 /* This is a special diagnostic section. Do not allow it to run during normal operation. */
460 MSG_syslog(LOG_DEBUG, "There are %d messages in the room.\n", num_msgs);
461 for (i=0; i<num_msgs; ++i) {
462 if ((i > 0) && (msglist[i] <= msglist[i-1])) abort();
464 MSG_syslog(LOG_DEBUG, "We are twiddling %d of them.\n", num_target_msgnums);
465 for (k=0; k<num_target_msgnums; ++k) {
466 if ((k > 0) && (target_msgnums[k] <= target_msgnums[k-1])) abort();
470 MSG_syslog(LOG_DEBUG, "before update: %s\n", ChrPtr(vset));
472 /* Translate the existing sequence set into an array of booleans */
473 setstr = NewStrBuf();
477 while (StrBufExtract_NextToken(setstr, vset, &pvset, ',') >= 0) {
479 StrBufExtract_token(lostr, setstr, 0, ':');
480 if (StrBufNum_tokens(setstr, ':') >= 2) {
481 StrBufExtract_token(histr, setstr, 1, ':');
485 StrBufAppendBuf(histr, lostr, 0);
488 if (!strcmp(ChrPtr(histr), "*")) {
495 for (i = 0; i < num_msgs; ++i) {
496 if ((msglist[i] >= lo) && (msglist[i] <= hi)) {
506 /* Now translate the array of booleans back into a sequence set */
512 for (i=0; i<num_msgs; ++i) {
516 for (k=0; k<num_target_msgnums; ++k) {
517 if (msglist[i] == target_msgnums[k]) {
518 is_seen = target_setting;
522 if ((was_seen == 0) && (is_seen == 1)) {
525 else if ((was_seen == 1) && (is_seen == 0)) {
528 if (StrLength(vset) > 0) {
529 StrBufAppendBufPlain(vset, HKEY(","), 0);
532 StrBufAppendPrintf(vset, "%ld", hi);
535 StrBufAppendPrintf(vset, "%ld:%ld", lo, hi);
539 if ((is_seen) && (i == num_msgs - 1)) {
540 if (StrLength(vset) > 0) {
541 StrBufAppendBufPlain(vset, HKEY(","), 0);
543 if ((i==0) || (was_seen == 0)) {
544 StrBufAppendPrintf(vset, "%ld", msglist[i]);
547 StrBufAppendPrintf(vset, "%ld:%ld", lo, msglist[i]);
555 * We will have to stuff this string back into a 4096 byte buffer, so if it's
556 * larger than that now, truncate it by removing tokens from the beginning.
557 * The limit of 100 iterations is there to prevent an infinite loop in case
558 * something unexpected happens.
560 int number_of_truncations = 0;
561 while ( (StrLength(vset) > SIZ) && (number_of_truncations < 100) ) {
562 StrBufRemove_token(vset, 0, ',');
563 ++number_of_truncations;
567 * If we're truncating the sequence set of messages marked with the 'seen' flag,
568 * we want the earliest messages (the truncated ones) to be marked, not unmarked.
569 * Otherwise messages at the beginning will suddenly appear to be 'unseen'.
571 if ( (which_set == ctdlsetseen_seen) && (number_of_truncations > 0) ) {
573 first_tok = NewStrBuf();
574 StrBufExtract_token(first_tok, vset, 0, ',');
575 StrBufRemove_token(vset, 0, ',');
577 if (StrBufNum_tokens(first_tok, ':') > 1) {
578 StrBufRemove_token(first_tok, 0, ':');
582 new_set = NewStrBuf();
583 StrBufAppendBufPlain(new_set, HKEY("1:"), 0);
584 StrBufAppendBuf(new_set, first_tok, 0);
585 StrBufAppendBufPlain(new_set, HKEY(":"), 0);
586 StrBufAppendBuf(new_set, vset, 0);
589 FreeStrBuf(&first_tok);
593 MSG_syslog(LOG_DEBUG, " after update: %s\n", ChrPtr(vset));
595 /* Decide which message set we're manipulating */
597 case ctdlsetseen_seen:
598 safestrncpy(vbuf.v_seen, ChrPtr(vset), sizeof vbuf.v_seen);
600 case ctdlsetseen_answered:
601 safestrncpy(vbuf.v_answered, ChrPtr(vset), sizeof vbuf.v_answered);
607 CtdlSetRelationship(&vbuf, which_user, which_room);
613 * API function to perform an operation for each qualifying message in the
614 * current room. (Returns the number of messages processed.)
616 int CtdlForEachMessage(int mode, long ref, char *search_string,
618 struct CtdlMessage *compare,
619 ForEachMsgCallback CallBack,
622 struct CitContext *CCC = CC;
625 struct cdbdata *cdbfr;
626 long *msglist = NULL;
628 int num_processed = 0;
631 struct CtdlMessage *msg = NULL;
634 int printed_lastold = 0;
635 int num_search_msgs = 0;
636 long *search_msgs = NULL;
638 int need_to_free_re = 0;
641 if ((content_type) && (!IsEmptyStr(content_type))) {
642 regcomp(&re, content_type, 0);
646 /* Learn about the user and room in question */
647 if (server_shutting_down) {
648 if (need_to_free_re) regfree(&re);
651 CtdlGetUser(&CCC->user, CCC->curr_user);
653 if (server_shutting_down) {
654 if (need_to_free_re) regfree(&re);
657 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
659 if (server_shutting_down) {
660 if (need_to_free_re) regfree(&re);
664 /* Load the message list */
665 cdbfr = cdb_fetch(CDB_MSGLISTS, &CCC->room.QRnumber, sizeof(long));
667 if (need_to_free_re) regfree(&re);
668 return 0; /* No messages at all? No further action. */
671 msglist = (long *) cdbfr->ptr;
672 num_msgs = cdbfr->len / sizeof(long);
674 cdbfr->ptr = NULL; /* clear this so that cdb_free() doesn't free it */
675 cdb_free(cdbfr); /* we own this memory now */
678 * Now begin the traversal.
680 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
682 /* If the caller is looking for a specific MIME type, filter
683 * out all messages which are not of the type requested.
685 if ((content_type != NULL) && (!IsEmptyStr(content_type))) {
687 /* This call to GetMetaData() sits inside this loop
688 * so that we only do the extra database read per msg
689 * if we need to. Doing the extra read all the time
690 * really kills the server. If we ever need to use
691 * metadata for another search criterion, we need to
692 * move the read somewhere else -- but still be smart
693 * enough to only do the read if the caller has
694 * specified something that will need it.
696 if (server_shutting_down) {
697 if (need_to_free_re) regfree(&re);
701 GetMetaData(&smi, msglist[a]);
703 /* if (strcasecmp(smi.meta_content_type, content_type)) { old non-regex way */
704 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) != 0) {
710 num_msgs = sort_msglist(msglist, num_msgs);
712 /* If a template was supplied, filter out the messages which
713 * don't match. (This could induce some delays!)
716 if (compare != NULL) {
717 for (a = 0; a < num_msgs; ++a) {
718 if (server_shutting_down) {
719 if (need_to_free_re) regfree(&re);
723 msg = CtdlFetchMessage(msglist[a], 1);
725 if (CtdlMsgCmp(msg, compare)) {
734 /* If a search string was specified, get a message list from
735 * the full text index and remove messages which aren't on both
739 * Since the lists are sorted and strictly ascending, and the
740 * output list is guaranteed to be shorter than or equal to the
741 * input list, we overwrite the bottom of the input list. This
742 * eliminates the need to memmove big chunks of the list over and
745 if ( (num_msgs > 0) && (mode == MSGS_SEARCH) && (search_string) ) {
747 /* Call search module via hook mechanism.
748 * NULL means use any search function available.
749 * otherwise replace with a char * to name of search routine
751 CtdlModuleDoSearch(&num_search_msgs, &search_msgs, search_string, "fulltext");
753 if (num_search_msgs > 0) {
757 orig_num_msgs = num_msgs;
759 for (i=0; i<orig_num_msgs; ++i) {
760 for (j=0; j<num_search_msgs; ++j) {
761 if (msglist[i] == search_msgs[j]) {
762 msglist[num_msgs++] = msglist[i];
768 num_msgs = 0; /* No messages qualify */
770 if (search_msgs != NULL) free(search_msgs);
772 /* Now that we've purged messages which don't contain the search
773 * string, treat a MSGS_SEARCH just like a MSGS_ALL from this
780 * Now iterate through the message list, according to the
781 * criteria supplied by the caller.
784 for (a = 0; a < num_msgs; ++a) {
785 if (server_shutting_down) {
786 if (need_to_free_re) regfree(&re);
788 return num_processed;
790 thismsg = msglist[a];
791 if (mode == MSGS_ALL) {
795 is_seen = is_msg_in_sequence_set(
796 vbuf.v_seen, thismsg);
797 if (is_seen) lastold = thismsg;
803 || ((mode == MSGS_OLD) && (is_seen))
804 || ((mode == MSGS_NEW) && (!is_seen))
805 || ((mode == MSGS_LAST) && (a >= (num_msgs - ref)))
806 || ((mode == MSGS_FIRST) && (a < ref))
807 || ((mode == MSGS_GT) && (thismsg > ref))
808 || ((mode == MSGS_LT) && (thismsg < ref))
809 || ((mode == MSGS_EQ) && (thismsg == ref))
812 if ((mode == MSGS_NEW) && (CCC->user.flags & US_LASTOLD) && (lastold > 0L) && (printed_lastold == 0) && (!is_seen)) {
814 CallBack(lastold, userdata);
818 if (CallBack) CallBack(thismsg, userdata);
822 if (need_to_free_re) regfree(&re);
825 * We cache the most recent msglist in order to do security checks later
827 if (CCC->client_socket > 0) {
828 if (CCC->cached_msglist != NULL) {
829 free(CCC->cached_msglist);
831 CCC->cached_msglist = msglist;
832 CCC->cached_num_msgs = num_msgs;
838 return num_processed;
844 * memfmout() - Citadel text formatter and paginator.
845 * Although the original purpose of this routine was to format
846 * text to the reader's screen width, all we're really using it
847 * for here is to format text out to 80 columns before sending it
848 * to the client. The client software may reformat it again.
851 char *mptr, /* where are we going to get our text from? */
852 const char *nl /* string to terminate lines with */
854 struct CitContext *CCC = CC;
856 unsigned char ch = 0;
863 while (ch=*(mptr++), ch != 0) {
866 if (client_write(outbuf, len) == -1)
868 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
872 if (client_write(nl, nllen) == -1)
874 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
879 else if (ch == '\r') {
880 /* Ignore carriage returns. Newlines are always LF or CRLF but never CR. */
882 else if (isspace(ch)) {
883 if (column > 72) { /* Beyond 72 columns, break on the next space */
884 if (client_write(outbuf, len) == -1)
886 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
890 if (client_write(nl, nllen) == -1)
892 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
905 if (column > 1000) { /* Beyond 1000 columns, break anywhere */
906 if (client_write(outbuf, len) == -1)
908 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
912 if (client_write(nl, nllen) == -1)
914 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
922 if (client_write(outbuf, len) == -1)
924 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
928 client_write(nl, nllen);
936 * Callback function for mime parser that simply lists the part
938 void list_this_part(char *name, char *filename, char *partnum, char *disp,
939 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
940 char *cbid, void *cbuserdata)
944 ma = (struct ma_info *)cbuserdata;
945 if (ma->is_ma == 0) {
946 cprintf("part=%s|%s|%s|%s|%s|%ld|%s|%s\n",
959 * Callback function for multipart prefix
961 void list_this_pref(char *name, char *filename, char *partnum, char *disp,
962 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
963 char *cbid, void *cbuserdata)
967 ma = (struct ma_info *)cbuserdata;
968 if (!strcasecmp(cbtype, "multipart/alternative")) {
972 if (ma->is_ma == 0) {
973 cprintf("pref=%s|%s\n", partnum, cbtype);
978 * Callback function for multipart sufffix
980 void list_this_suff(char *name, char *filename, char *partnum, char *disp,
981 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
982 char *cbid, void *cbuserdata)
986 ma = (struct ma_info *)cbuserdata;
987 if (ma->is_ma == 0) {
988 cprintf("suff=%s|%s\n", partnum, cbtype);
990 if (!strcasecmp(cbtype, "multipart/alternative")) {
997 * Callback function for mime parser that opens a section for downloading
998 * we use serv_files function here:
1000 extern void OpenCmdResult(char *filename, const char *mime_type);
1001 void mime_download(char *name, char *filename, char *partnum, char *disp,
1002 void *content, char *cbtype, char *cbcharset, size_t length,
1003 char *encoding, char *cbid, void *cbuserdata)
1006 CitContext *CCC = MyContext();
1008 /* Silently go away if there's already a download open. */
1009 if (CCC->download_fp != NULL)
1013 (!IsEmptyStr(partnum) && (!strcasecmp(CCC->download_desired_section, partnum)))
1014 || (!IsEmptyStr(cbid) && (!strcasecmp(CCC->download_desired_section, cbid)))
1016 CCC->download_fp = tmpfile();
1017 if (CCC->download_fp == NULL) {
1018 MSG_syslog(LOG_EMERG, "mime_download(): Couldn't write: %s\n",
1020 cprintf("%d cannot open temporary file: %s\n",
1021 ERROR + INTERNAL_ERROR, strerror(errno));
1025 rv = fwrite(content, length, 1, CCC->download_fp);
1027 MSG_syslog(LOG_EMERG, "mime_download(): Couldn't write: %s\n",
1029 cprintf("%d unable to write tempfile.\n",
1031 fclose(CCC->download_fp);
1032 CCC->download_fp = NULL;
1035 fflush(CCC->download_fp);
1036 rewind(CCC->download_fp);
1038 OpenCmdResult(filename, cbtype);
1045 * Callback function for mime parser that outputs a section all at once.
1046 * We can specify the desired section by part number *or* content-id.
1048 void mime_spew_section(char *name, char *filename, char *partnum, char *disp,
1049 void *content, char *cbtype, char *cbcharset, size_t length,
1050 char *encoding, char *cbid, void *cbuserdata)
1052 int *found_it = (int *)cbuserdata;
1055 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
1056 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
1059 cprintf("%d %d|-1|%s|%s|%s\n",
1066 client_write(content, length);
1072 * Load a message from disk into memory.
1073 * This is used by CtdlOutputMsg() and other fetch functions.
1075 * NOTE: Caller is responsible for freeing the returned CtdlMessage struct
1076 * using the CtdlMessageFree() function.
1078 struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body)
1080 struct CitContext *CCC = CC;
1081 struct cdbdata *dmsgtext;
1082 struct CtdlMessage *ret = NULL;
1086 cit_uint8_t field_header;
1088 MSG_syslog(LOG_DEBUG, "CtdlFetchMessage(%ld, %d)\n", msgnum, with_body);
1089 dmsgtext = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
1090 if (dmsgtext == NULL) {
1091 MSG_syslog(LOG_ERR, "CtdlFetchMessage(%ld, %d) Failed!\n", msgnum, with_body);
1094 mptr = dmsgtext->ptr;
1095 upper_bound = mptr + dmsgtext->len;
1097 /* Parse the three bytes that begin EVERY message on disk.
1098 * The first is always 0xFF, the on-disk magic number.
1099 * The second is the anonymous/public type byte.
1100 * The third is the format type byte (vari, fixed, or MIME).
1104 MSG_syslog(LOG_ERR, "Message %ld appears to be corrupted.\n", msgnum);
1108 ret = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1109 memset(ret, 0, sizeof(struct CtdlMessage));
1111 ret->cm_magic = CTDLMESSAGE_MAGIC;
1112 ret->cm_anon_type = *mptr++; /* Anon type byte */
1113 ret->cm_format_type = *mptr++; /* Format type byte */
1116 * The rest is zero or more arbitrary fields. Load them in.
1117 * We're done when we encounter either a zero-length field or
1118 * have just processed the 'M' (message text) field.
1122 if (mptr >= upper_bound) {
1125 field_header = *mptr++;
1127 CM_SetField(ret, field_header, mptr, len);
1129 mptr += len + 1; /* advance to next field */
1131 } while ((mptr < upper_bound) && (field_header != 'M'));
1135 /* Always make sure there's something in the msg text field. If
1136 * it's NULL, the message text is most likely stored separately,
1137 * so go ahead and fetch that. Failing that, just set a dummy
1138 * body so other code doesn't barf.
1140 if ( (CM_IsEmpty(ret, eMesageText)) && (with_body) ) {
1141 dmsgtext = cdb_fetch(CDB_BIGMSGS, &msgnum, sizeof(long));
1142 if (dmsgtext != NULL) {
1143 CM_SetAsField(ret, eMesageText, &dmsgtext->ptr, dmsgtext->len);
1147 if (CM_IsEmpty(ret, eMesageText)) {
1148 CM_SetField(ret, eMesageText, HKEY("\r\n\r\n (no text)\r\n"));
1151 /* Perform "before read" hooks (aborting if any return nonzero) */
1152 if (PerformMessageHooks(ret, EVT_BEFOREREAD) > 0) {
1163 * Pre callback function for multipart/alternative
1165 * NOTE: this differs from the standard behavior for a reason. Normally when
1166 * displaying multipart/alternative you want to show the _last_ usable
1167 * format in the message. Here we show the _first_ one, because it's
1168 * usually text/plain. Since this set of functions is designed for text
1169 * output to non-MIME-aware clients, this is the desired behavior.
1172 void fixed_output_pre(char *name, char *filename, char *partnum, char *disp,
1173 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1174 char *cbid, void *cbuserdata)
1176 struct CitContext *CCC = CC;
1179 ma = (struct ma_info *)cbuserdata;
1180 MSG_syslog(LOG_DEBUG, "fixed_output_pre() type=<%s>\n", cbtype);
1181 if (!strcasecmp(cbtype, "multipart/alternative")) {
1185 if (!strcasecmp(cbtype, "message/rfc822")) {
1191 * Post callback function for multipart/alternative
1193 void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
1194 void *content, char *cbtype, char *cbcharset, size_t length,
1195 char *encoding, char *cbid, void *cbuserdata)
1197 struct CitContext *CCC = CC;
1200 ma = (struct ma_info *)cbuserdata;
1201 MSG_syslog(LOG_DEBUG, "fixed_output_post() type=<%s>\n", cbtype);
1202 if (!strcasecmp(cbtype, "multipart/alternative")) {
1206 if (!strcasecmp(cbtype, "message/rfc822")) {
1212 * Inline callback function for mime parser that wants to display text
1214 void fixed_output(char *name, char *filename, char *partnum, char *disp,
1215 void *content, char *cbtype, char *cbcharset, size_t length,
1216 char *encoding, char *cbid, void *cbuserdata)
1218 struct CitContext *CCC = CC;
1224 ma = (struct ma_info *)cbuserdata;
1226 MSG_syslog(LOG_DEBUG,
1227 "fixed_output() part %s: %s (%s) (%ld bytes)\n",
1228 partnum, filename, cbtype, (long)length);
1231 * If we're in the middle of a multipart/alternative scope and
1232 * we've already printed another section, skip this one.
1234 if ( (ma->is_ma) && (ma->did_print) ) {
1235 MSG_syslog(LOG_DEBUG, "Skipping part %s (%s)\n", partnum, cbtype);
1240 if ( (!strcasecmp(cbtype, "text/plain"))
1241 || (IsEmptyStr(cbtype)) ) {
1244 client_write(wptr, length);
1245 if (wptr[length-1] != '\n') {
1252 if (!strcasecmp(cbtype, "text/html")) {
1253 ptr = html_to_ascii(content, length, 80, 0);
1255 client_write(ptr, wlen);
1256 if ((wlen > 0) && (ptr[wlen-1] != '\n')) {
1263 if (ma->use_fo_hooks) {
1264 if (PerformFixedOutputHooks(cbtype, content, length)) {
1265 /* above function returns nonzero if it handled the part */
1270 if (strncasecmp(cbtype, "multipart/", 10)) {
1271 cprintf("Part %s: %s (%s) (%ld bytes)\r\n",
1272 partnum, filename, cbtype, (long)length);
1278 * The client is elegant and sophisticated and wants to be choosy about
1279 * MIME content types, so figure out which multipart/alternative part
1280 * we're going to send.
1282 * We use a system of weights. When we find a part that matches one of the
1283 * MIME types we've declared as preferential, we can store it in ma->chosen_part
1284 * and then set ma->chosen_pref to that MIME type's position in our preference
1285 * list. If we then hit another match, we only replace the first match if
1286 * the preference value is lower.
1288 void choose_preferred(char *name, char *filename, char *partnum, char *disp,
1289 void *content, char *cbtype, char *cbcharset, size_t length,
1290 char *encoding, char *cbid, void *cbuserdata)
1292 struct CitContext *CCC = CC;
1297 ma = (struct ma_info *)cbuserdata;
1299 // NOTE: REMOVING THIS CONDITIONAL FIXES BUG 220
1300 // http://bugzilla.citadel.org/show_bug.cgi?id=220
1301 // I don't know if there are any side effects! Please TEST TEST TEST
1302 //if (ma->is_ma > 0) {
1304 for (i=0; i<num_tokens(CCC->preferred_formats, '|'); ++i) {
1305 extract_token(buf, CCC->preferred_formats, i, '|', sizeof buf);
1306 if ( (!strcasecmp(buf, cbtype)) && (!ma->freeze) ) {
1307 if (i < ma->chosen_pref) {
1308 MSG_syslog(LOG_DEBUG, "Setting chosen part: <%s>\n", partnum);
1309 safestrncpy(ma->chosen_part, partnum, sizeof ma->chosen_part);
1310 ma->chosen_pref = i;
1317 * Now that we've chosen our preferred part, output it.
1319 void output_preferred(char *name,
1331 struct CitContext *CCC = CC;
1334 int add_newline = 0;
1337 char *decoded = NULL;
1338 size_t bytes_decoded;
1341 ma = (struct ma_info *)cbuserdata;
1343 /* This is not the MIME part you're looking for... */
1344 if (strcasecmp(partnum, ma->chosen_part)) return;
1346 /* If the content-type of this part is in our preferred formats
1347 * list, we can simply output it verbatim.
1349 for (i=0; i<num_tokens(CCC->preferred_formats, '|'); ++i) {
1350 extract_token(buf, CCC->preferred_formats, i, '|', sizeof buf);
1351 if (!strcasecmp(buf, cbtype)) {
1352 /* Yeah! Go! W00t!! */
1353 if (ma->dont_decode == 0)
1354 rc = mime_decode_now (content,
1360 break; /* Give us the chance, maybe theres another one. */
1362 if (rc == 0) text_content = (char *)content;
1364 text_content = decoded;
1365 length = bytes_decoded;
1368 if (text_content[length-1] != '\n') {
1371 cprintf("Content-type: %s", cbtype);
1372 if (!IsEmptyStr(cbcharset)) {
1373 cprintf("; charset=%s", cbcharset);
1375 cprintf("\nContent-length: %d\n",
1376 (int)(length + add_newline) );
1377 if (!IsEmptyStr(encoding)) {
1378 cprintf("Content-transfer-encoding: %s\n", encoding);
1381 cprintf("Content-transfer-encoding: 7bit\n");
1383 cprintf("X-Citadel-MSG4-Partnum: %s\n", partnum);
1385 if (client_write(text_content, length) == -1)
1387 MSGM_syslog(LOG_ERR, "output_preferred(): aborting due to write failure.\n");
1390 if (add_newline) cprintf("\n");
1391 if (decoded != NULL) free(decoded);
1396 /* No translations required or possible: output as text/plain */
1397 cprintf("Content-type: text/plain\n\n");
1399 if (ma->dont_decode == 0)
1400 rc = mime_decode_now (content,
1406 return; /* Give us the chance, maybe theres another one. */
1408 if (rc == 0) text_content = (char *)content;
1410 text_content = decoded;
1411 length = bytes_decoded;
1414 fixed_output(name, filename, partnum, disp, text_content, cbtype, cbcharset,
1415 length, encoding, cbid, cbuserdata);
1416 if (decoded != NULL) free(decoded);
1421 char desired_section[64];
1428 * Callback function for
1430 void extract_encapsulated_message(char *name, char *filename, char *partnum, char *disp,
1431 void *content, char *cbtype, char *cbcharset, size_t length,
1432 char *encoding, char *cbid, void *cbuserdata)
1434 struct encapmsg *encap;
1436 encap = (struct encapmsg *)cbuserdata;
1438 /* Only proceed if this is the desired section... */
1439 if (!strcasecmp(encap->desired_section, partnum)) {
1440 encap->msglen = length;
1441 encap->msg = malloc(length + 2);
1442 memcpy(encap->msg, content, length);
1449 * Determine whether the specified message exists in the cached_msglist
1450 * (This is a security check)
1452 int check_cached_msglist(long msgnum) {
1453 struct CitContext *CCC = CC;
1455 /* cases in which we skip the check */
1456 if (!CCC) return om_ok; /* not a session */
1457 if (CCC->client_socket <= 0) return om_ok; /* not a client session */
1458 if (CCC->cached_msglist == NULL) return om_access_denied; /* no msglist fetched */
1459 if (CCC->cached_num_msgs == 0) return om_access_denied; /* nothing to check */
1462 /* Do a binary search within the cached_msglist for the requested msgnum */
1464 int max = (CC->cached_num_msgs - 1);
1466 while (max >= min) {
1467 int middle = min + (max-min) / 2 ;
1468 if (msgnum == CCC->cached_msglist[middle]) {
1471 if (msgnum > CC->cached_msglist[middle]) {
1479 return om_access_denied;
1485 * Get a message off disk. (returns om_* values found in msgbase.h)
1488 int CtdlOutputMsg(long msg_num, /* message number (local) to fetch */
1489 int mode, /* how would you like that message? */
1490 int headers_only, /* eschew the message body? */
1491 int do_proto, /* do Citadel protocol responses? */
1492 int crlf, /* Use CRLF newlines instead of LF? */
1493 char *section, /* NULL or a message/rfc822 section */
1494 int flags, /* various flags; see msgbase.h */
1498 struct CitContext *CCC = CC;
1499 struct CtdlMessage *TheMessage = NULL;
1500 int retcode = CIT_OK;
1501 struct encapmsg encap;
1504 MSG_syslog(LOG_DEBUG, "CtdlOutputMsg(msgnum=%ld, mode=%d, section=%s)\n",
1506 (section ? section : "<>")
1509 r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
1512 if (r == om_not_logged_in) {
1513 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1516 cprintf("%d An unknown error has occurred.\n", ERROR);
1523 * Check to make sure the message is actually IN this room
1525 r = check_cached_msglist(msg_num);
1526 if (r == om_access_denied) {
1527 /* Not in the cache? We get ONE shot to check it again. */
1528 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, NULL, NULL);
1529 r = check_cached_msglist(msg_num);
1532 MSG_syslog(LOG_DEBUG, "Security check fail: message %ld is not in %s\n",
1533 msg_num, CCC->room.QRname
1536 if (r == om_access_denied) {
1537 cprintf("%d message %ld was not found in this room\n",
1538 ERROR + HIGHER_ACCESS_REQUIRED,
1547 * Fetch the message from disk. If we're in HEADERS_FAST mode,
1548 * request that we don't even bother loading the body into memory.
1550 if (headers_only == HEADERS_FAST) {
1551 TheMessage = CtdlFetchMessage(msg_num, 0);
1554 TheMessage = CtdlFetchMessage(msg_num, 1);
1557 if (TheMessage == NULL) {
1558 if (do_proto) cprintf("%d Can't locate msg %ld on disk\n",
1559 ERROR + MESSAGE_NOT_FOUND, msg_num);
1560 return(om_no_such_msg);
1563 /* Here is the weird form of this command, to process only an
1564 * encapsulated message/rfc822 section.
1566 if (section) if (!IsEmptyStr(section)) if (strcmp(section, "0")) {
1567 memset(&encap, 0, sizeof encap);
1568 safestrncpy(encap.desired_section, section, sizeof encap.desired_section);
1569 mime_parser(TheMessage->cm_fields[eMesageText],
1571 *extract_encapsulated_message,
1572 NULL, NULL, (void *)&encap, 0
1575 if ((Author != NULL) && (*Author == NULL))
1577 *Author = TheMessage->cm_fields[eAuthor];
1578 TheMessage->cm_fields[eAuthor] = NULL;
1580 if ((Address != NULL) && (*Address == NULL))
1582 *Address = TheMessage->cm_fields[erFc822Addr];
1583 TheMessage->cm_fields[erFc822Addr] = NULL;
1585 CM_Free(TheMessage);
1589 encap.msg[encap.msglen] = 0;
1590 TheMessage = convert_internet_message(encap.msg);
1591 encap.msg = NULL; /* no free() here, TheMessage owns it now */
1593 /* Now we let it fall through to the bottom of this
1594 * function, because TheMessage now contains the
1595 * encapsulated message instead of the top-level
1596 * message. Isn't that neat?
1601 cprintf("%d msg %ld has no part %s\n",
1602 ERROR + MESSAGE_NOT_FOUND,
1606 retcode = om_no_such_msg;
1611 /* Ok, output the message now */
1612 if (retcode == CIT_OK)
1613 retcode = CtdlOutputPreLoadedMsg(TheMessage, mode, headers_only, do_proto, crlf, flags);
1614 if ((Author != NULL) && (*Author == NULL))
1616 *Author = TheMessage->cm_fields[eAuthor];
1617 TheMessage->cm_fields[eAuthor] = NULL;
1619 if ((Address != NULL) && (*Address == NULL))
1621 *Address = TheMessage->cm_fields[erFc822Addr];
1622 TheMessage->cm_fields[erFc822Addr] = NULL;
1625 CM_Free(TheMessage);
1632 void OutputCtdlMsgHeaders(
1633 struct CtdlMessage *TheMessage,
1634 int do_proto) /* do Citadel protocol responses? */
1639 char display_name[256];
1641 /* begin header processing loop for Citadel message format */
1642 safestrncpy(display_name, "<unknown>", sizeof display_name);
1643 if (!CM_IsEmpty(TheMessage, eAuthor)) {
1644 strcpy(buf, TheMessage->cm_fields[eAuthor]);
1645 if (TheMessage->cm_anon_type == MES_ANONONLY) {
1646 safestrncpy(display_name, "****", sizeof display_name);
1648 else if (TheMessage->cm_anon_type == MES_ANONOPT) {
1649 safestrncpy(display_name, "anonymous", sizeof display_name);
1652 safestrncpy(display_name, buf, sizeof display_name);
1654 if ((is_room_aide())
1655 && ((TheMessage->cm_anon_type == MES_ANONONLY)
1656 || (TheMessage->cm_anon_type == MES_ANONOPT))) {
1657 size_t tmp = strlen(display_name);
1658 snprintf(&display_name[tmp],
1659 sizeof display_name - tmp,
1664 /* Don't show Internet address for users on the
1665 * local Citadel network.
1668 if (!CM_IsEmpty(TheMessage, eNodeName) &&
1669 (haschar(TheMessage->cm_fields[eNodeName], '.') == 0))
1674 /* Now spew the header fields in the order we like them. */
1675 for (i=0; i< NDiskFields; ++i) {
1677 Field = FieldOrder[i];
1678 if (Field != eMesageText) {
1679 if ( (!CM_IsEmpty(TheMessage, Field))
1680 && (msgkeys[Field] != NULL) ) {
1681 if ((Field == eenVelopeTo) ||
1682 (Field == eRecipient) ||
1683 (Field == eCarbonCopY)) {
1684 sanitize_truncated_recipient(TheMessage->cm_fields[Field]);
1686 if (Field == eAuthor) {
1687 if (do_proto) cprintf("%s=%s\n",
1691 else if ((Field == erFc822Addr) && (suppress_f)) {
1694 /* Masquerade display name if needed */
1696 if (do_proto) cprintf("%s=%s\n",
1698 TheMessage->cm_fields[Field]
1707 void OutputRFC822MsgHeaders(
1708 struct CtdlMessage *TheMessage,
1709 int flags, /* should the bessage be exported clean */
1711 char *mid, long sizeof_mid,
1712 char *suser, long sizeof_suser,
1713 char *luser, long sizeof_luser,
1714 char *fuser, long sizeof_fuser,
1715 char *snode, long sizeof_snode)
1717 char datestamp[100];
1718 int subject_found = 0;
1725 for (i = 0; i < 256; ++i) {
1726 if (TheMessage->cm_fields[i]) {
1727 mptr = mpptr = TheMessage->cm_fields[i];
1730 safestrncpy(luser, mptr, sizeof_luser);
1731 safestrncpy(suser, mptr, sizeof_suser);
1733 else if (i == 'Y') {
1734 if ((flags & QP_EADDR) != 0) {
1735 mptr = qp_encode_email_addrs(mptr);
1737 sanitize_truncated_recipient(mptr);
1738 cprintf("CC: %s%s", mptr, nl);
1740 else if (i == 'P') {
1741 cprintf("Return-Path: %s%s", mptr, nl);
1743 else if (i == eListID) {
1744 cprintf("List-ID: %s%s", mptr, nl);
1746 else if (i == 'V') {
1747 if ((flags & QP_EADDR) != 0)
1748 mptr = qp_encode_email_addrs(mptr);
1750 while ((*hptr != '\0') && isspace(*hptr))
1752 if (!IsEmptyStr(hptr))
1753 cprintf("Envelope-To: %s%s", hptr, nl);
1755 else if (i == 'U') {
1756 cprintf("Subject: %s%s", mptr, nl);
1760 safestrncpy(mid, mptr, sizeof_mid); /// TODO: detect @ here and copy @nodename in if not found.
1761 else if (i == erFc822Addr)
1762 safestrncpy(fuser, mptr, sizeof_fuser);
1763 /* else if (i == 'O')
1764 cprintf("X-Citadel-Room: %s%s",
1767 safestrncpy(snode, mptr, sizeof_snode);
1770 if (haschar(mptr, '@') == 0)
1772 sanitize_truncated_recipient(mptr);
1773 cprintf("To: %s@%s", mptr, config.c_fqdn);
1778 if ((flags & QP_EADDR) != 0) {
1779 mptr = qp_encode_email_addrs(mptr);
1781 sanitize_truncated_recipient(mptr);
1782 cprintf("To: %s", mptr);
1786 else if (i == 'T') {
1787 datestring(datestamp, sizeof datestamp,
1788 atol(mptr), DATESTRING_RFC822);
1789 cprintf("Date: %s%s", datestamp, nl);
1791 else if (i == 'W') {
1792 cprintf("References: ");
1793 k = num_tokens(mptr, '|');
1794 for (j=0; j<k; ++j) {
1795 extract_token(buf, mptr, j, '|', sizeof buf);
1796 cprintf("<%s>", buf);
1805 else if (i == eReplyTo) {
1807 while ((*hptr != '\0') && isspace(*hptr))
1809 if (!IsEmptyStr(hptr))
1810 cprintf("Reply-To: %s%s", mptr, nl);
1816 if (subject_found == 0) {
1817 cprintf("Subject: (no subject)%s", nl);
1822 void Dump_RFC822HeadersBody(
1823 struct CtdlMessage *TheMessage,
1824 int headers_only, /* eschew the message body? */
1825 int flags, /* should the bessage be exported clean? */
1829 cit_uint8_t prev_ch;
1831 const char *StartOfText = StrBufNOTNULL;
1834 int nllen = strlen(nl);
1837 mptr = TheMessage->cm_fields[eMesageText];
1841 while (*mptr != '\0') {
1842 if (*mptr == '\r') {
1849 eoh = (*(mptr+1) == '\r') && (*(mptr+2) == '\n');
1851 eoh = *(mptr+1) == '\n';
1855 StartOfText = strchr(StartOfText, '\n');
1856 StartOfText = strchr(StartOfText, '\n');
1859 if (((headers_only == HEADERS_NONE) && (mptr >= StartOfText)) ||
1860 ((headers_only == HEADERS_ONLY) && (mptr < StartOfText)) ||
1861 ((headers_only != HEADERS_NONE) &&
1862 (headers_only != HEADERS_ONLY))
1864 if (*mptr == '\n') {
1865 memcpy(&outbuf[outlen], nl, nllen);
1867 outbuf[outlen] = '\0';
1870 outbuf[outlen++] = *mptr;
1874 if (flags & ESC_DOT)
1876 if ((prev_ch == '\n') &&
1878 ((*(mptr+1) == '\r') || (*(mptr+1) == '\n')))
1880 outbuf[outlen++] = '.';
1885 if (outlen > 1000) {
1886 if (client_write(outbuf, outlen) == -1)
1888 struct CitContext *CCC = CC;
1889 MSGM_syslog(LOG_ERR, "Dump_RFC822HeadersBody(): aborting due to write failure.\n");
1896 client_write(outbuf, outlen);
1902 /* If the format type on disk is 1 (fixed-format), then we want
1903 * everything to be output completely literally ... regardless of
1904 * what message transfer format is in use.
1906 void DumpFormatFixed(
1907 struct CtdlMessage *TheMessage,
1908 int mode, /* how would you like that message? */
1915 int nllen = strlen (nl);
1918 mptr = TheMessage->cm_fields[eMesageText];
1920 if (mode == MT_MIME) {
1921 cprintf("Content-type: text/plain\n\n");
1925 while (ch = *mptr++, ch > 0) {
1929 if ((buflen > 250) && (!xlline)){
1933 while ((buflen > 0) &&
1934 (!isspace(buf[buflen])))
1940 mptr -= tbuflen - buflen;
1945 /* if we reach the outer bounds of our buffer,
1946 abort without respect what whe purge. */
1949 (buflen > SIZ - nllen - 2)))
1953 memcpy (&buf[buflen], nl, nllen);
1957 if (client_write(buf, buflen) == -1)
1959 struct CitContext *CCC = CC;
1960 MSGM_syslog(LOG_ERR, "DumpFormatFixed(): aborting due to write failure.\n");
1972 if (!IsEmptyStr(buf))
1973 cprintf("%s%s", buf, nl);
1977 * Get a message off disk. (returns om_* values found in msgbase.h)
1979 int CtdlOutputPreLoadedMsg(
1980 struct CtdlMessage *TheMessage,
1981 int mode, /* how would you like that message? */
1982 int headers_only, /* eschew the message body? */
1983 int do_proto, /* do Citadel protocol responses? */
1984 int crlf, /* Use CRLF newlines instead of LF? */
1985 int flags /* should the bessage be exported clean? */
1987 struct CitContext *CCC = CC;
1990 const char *nl; /* newline string */
1993 /* Buffers needed for RFC822 translation. These are all filled
1994 * using functions that are bounds-checked, and therefore we can
1995 * make them substantially smaller than SIZ.
2003 MSG_syslog(LOG_DEBUG, "CtdlOutputPreLoadedMsg(TheMessage=%s, %d, %d, %d, %d\n",
2004 ((TheMessage == NULL) ? "NULL" : "not null"),
2005 mode, headers_only, do_proto, crlf);
2007 strcpy(mid, "unknown");
2008 nl = (crlf ? "\r\n" : "\n");
2010 if (!CM_IsValidMsg(TheMessage)) {
2011 MSGM_syslog(LOG_ERR,
2012 "ERROR: invalid preloaded message for output\n");
2014 return(om_no_such_msg);
2017 /* Suppress envelope recipients if required to avoid disclosing BCC addresses.
2018 * Pad it with spaces in order to avoid changing the RFC822 length of the message.
2020 if ( (flags & SUPPRESS_ENV_TO) && (!CM_IsEmpty(TheMessage, eenVelopeTo)) ) {
2021 memset(TheMessage->cm_fields[eenVelopeTo], ' ', strlen(TheMessage->cm_fields[eenVelopeTo]));
2024 /* Are we downloading a MIME component? */
2025 if (mode == MT_DOWNLOAD) {
2026 if (TheMessage->cm_format_type != FMT_RFC822) {
2028 cprintf("%d This is not a MIME message.\n",
2029 ERROR + ILLEGAL_VALUE);
2030 } else if (CCC->download_fp != NULL) {
2031 if (do_proto) cprintf(
2032 "%d You already have a download open.\n",
2033 ERROR + RESOURCE_BUSY);
2035 /* Parse the message text component */
2036 mptr = TheMessage->cm_fields[eMesageText];
2037 mime_parser(mptr, NULL, *mime_download, NULL, NULL, NULL, 0);
2038 /* If there's no file open by this time, the requested
2039 * section wasn't found, so print an error
2041 if (CCC->download_fp == NULL) {
2042 if (do_proto) cprintf(
2043 "%d Section %s not found.\n",
2044 ERROR + FILE_NOT_FOUND,
2045 CCC->download_desired_section);
2048 return((CCC->download_fp != NULL) ? om_ok : om_mime_error);
2051 /* MT_SPEW_SECTION is like MT_DOWNLOAD except it outputs the whole MIME part
2052 * in a single server operation instead of opening a download file.
2054 if (mode == MT_SPEW_SECTION) {
2055 if (TheMessage->cm_format_type != FMT_RFC822) {
2057 cprintf("%d This is not a MIME message.\n",
2058 ERROR + ILLEGAL_VALUE);
2060 /* Parse the message text component */
2063 mptr = TheMessage->cm_fields[eMesageText];
2064 mime_parser(mptr, NULL, *mime_spew_section, NULL, NULL, (void *)&found_it, 0);
2065 /* If section wasn't found, print an error
2068 if (do_proto) cprintf(
2069 "%d Section %s not found.\n",
2070 ERROR + FILE_NOT_FOUND,
2071 CCC->download_desired_section);
2074 return((CCC->download_fp != NULL) ? om_ok : om_mime_error);
2077 /* now for the user-mode message reading loops */
2078 if (do_proto) cprintf("%d msg:\n", LISTING_FOLLOWS);
2080 /* Does the caller want to skip the headers? */
2081 if (headers_only == HEADERS_NONE) goto START_TEXT;
2083 /* Tell the client which format type we're using. */
2084 if ( (mode == MT_CITADEL) && (do_proto) ) {
2085 cprintf("type=%d\n", TheMessage->cm_format_type);
2088 /* nhdr=yes means that we're only displaying headers, no body */
2089 if ( (TheMessage->cm_anon_type == MES_ANONONLY)
2090 && ((mode == MT_CITADEL) || (mode == MT_MIME))
2093 cprintf("nhdr=yes\n");
2096 if ((mode == MT_CITADEL) || (mode == MT_MIME))
2097 OutputCtdlMsgHeaders(TheMessage, do_proto);
2100 /* begin header processing loop for RFC822 transfer format */
2104 strcpy(snode, NODENAME);
2105 if (mode == MT_RFC822)
2106 OutputRFC822MsgHeaders(
2111 suser, sizeof(suser),
2112 luser, sizeof(luser),
2113 fuser, sizeof(fuser),
2114 snode, sizeof(snode)
2118 for (i=0; !IsEmptyStr(&suser[i]); ++i) {
2119 suser[i] = tolower(suser[i]);
2120 if (!isalnum(suser[i])) suser[i]='_';
2123 if (mode == MT_RFC822) {
2124 if (!strcasecmp(snode, NODENAME)) {
2125 safestrncpy(snode, FQDN, sizeof snode);
2128 /* Construct a fun message id */
2129 cprintf("Message-ID: <%s", mid);/// todo: this possibly breaks threadding mails.
2130 if (strchr(mid, '@')==NULL) {
2131 cprintf("@%s", snode);
2135 if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONONLY)) {
2136 cprintf("From: \"----\" <x@x.org>%s", nl);
2138 else if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONOPT)) {
2139 cprintf("From: \"anonymous\" <x@x.org>%s", nl);
2141 else if (!IsEmptyStr(fuser)) {
2142 cprintf("From: \"%s\" <%s>%s", luser, fuser, nl);
2145 cprintf("From: \"%s\" <%s@%s>%s", luser, suser, snode, nl);
2148 /* Blank line signifying RFC822 end-of-headers */
2149 if (TheMessage->cm_format_type != FMT_RFC822) {
2154 /* end header processing loop ... at this point, we're in the text */
2156 if (headers_only == HEADERS_FAST) goto DONE;
2158 /* Tell the client about the MIME parts in this message */
2159 if (TheMessage->cm_format_type == FMT_RFC822) {
2160 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2161 mptr = TheMessage->cm_fields[eMesageText];
2162 memset(&ma, 0, sizeof(struct ma_info));
2163 mime_parser(mptr, NULL,
2164 (do_proto ? *list_this_part : NULL),
2165 (do_proto ? *list_this_pref : NULL),
2166 (do_proto ? *list_this_suff : NULL),
2169 else if (mode == MT_RFC822) { /* unparsed RFC822 dump */
2170 Dump_RFC822HeadersBody(
2179 if (headers_only == HEADERS_ONLY) {
2183 /* signify start of msg text */
2184 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2185 if (do_proto) cprintf("text\n");
2188 if (TheMessage->cm_format_type == FMT_FIXED)
2191 mode, /* how would you like that message? */
2194 /* If the message on disk is format 0 (Citadel vari-format), we
2195 * output using the formatter at 80 columns. This is the final output
2196 * form if the transfer format is RFC822, but if the transfer format
2197 * is Citadel proprietary, it'll still work, because the indentation
2198 * for new paragraphs is correct and the client will reformat the
2199 * message to the reader's screen width.
2201 if (TheMessage->cm_format_type == FMT_CITADEL) {
2202 mptr = TheMessage->cm_fields[eMesageText];
2204 if (mode == MT_MIME) {
2205 cprintf("Content-type: text/x-citadel-variformat\n\n");
2210 /* If the message on disk is format 4 (MIME), we've gotta hand it
2211 * off to the MIME parser. The client has already been told that
2212 * this message is format 1 (fixed format), so the callback function
2213 * we use will display those parts as-is.
2215 if (TheMessage->cm_format_type == FMT_RFC822) {
2216 memset(&ma, 0, sizeof(struct ma_info));
2218 if (mode == MT_MIME) {
2219 ma.use_fo_hooks = 0;
2220 strcpy(ma.chosen_part, "1");
2221 ma.chosen_pref = 9999;
2222 ma.dont_decode = CCC->msg4_dont_decode;
2223 mime_parser(mptr, NULL,
2224 *choose_preferred, *fixed_output_pre,
2225 *fixed_output_post, (void *)&ma, 1);
2226 mime_parser(mptr, NULL,
2227 *output_preferred, NULL, NULL, (void *)&ma, 1);
2230 ma.use_fo_hooks = 1;
2231 mime_parser(mptr, NULL,
2232 *fixed_output, *fixed_output_pre,
2233 *fixed_output_post, (void *)&ma, 0);
2238 DONE: /* now we're done */
2239 if (do_proto) cprintf("000\n");
2244 * Save one or more message pointers into a specified room
2245 * (Returns 0 for success, nonzero for failure)
2246 * roomname may be NULL to use the current room
2248 * Note that the 'supplied_msg' field may be set to NULL, in which case
2249 * the message will be fetched from disk, by number, if we need to perform
2250 * replication checks. This adds an additional database read, so if the
2251 * caller already has the message in memory then it should be supplied. (Obviously
2252 * this mode of operation only works if we're saving a single message.)
2254 int CtdlSaveMsgPointersInRoom(char *roomname, long newmsgidlist[], int num_newmsgs,
2255 int do_repl_check, struct CtdlMessage *supplied_msg, int suppress_refcount_adj
2257 struct CitContext *CCC = CC;
2259 char hold_rm[ROOMNAMELEN];
2260 struct cdbdata *cdbfr;
2263 long highest_msg = 0L;
2266 struct CtdlMessage *msg = NULL;
2268 long *msgs_to_be_merged = NULL;
2269 int num_msgs_to_be_merged = 0;
2271 MSG_syslog(LOG_DEBUG,
2272 "CtdlSaveMsgPointersInRoom(room=%s, num_msgs=%d, repl=%d, suppress_rca=%d)\n",
2273 roomname, num_newmsgs, do_repl_check, suppress_refcount_adj
2276 strcpy(hold_rm, CCC->room.QRname);
2279 if (newmsgidlist == NULL) return(ERROR + INTERNAL_ERROR);
2280 if (num_newmsgs < 1) return(ERROR + INTERNAL_ERROR);
2281 if (num_newmsgs > 1) supplied_msg = NULL;
2283 /* Now the regular stuff */
2284 if (CtdlGetRoomLock(&CCC->room,
2285 ((roomname != NULL) ? roomname : CCC->room.QRname) )
2287 MSG_syslog(LOG_ERR, "No such room <%s>\n", roomname);
2288 return(ERROR + ROOM_NOT_FOUND);
2292 msgs_to_be_merged = malloc(sizeof(long) * num_newmsgs);
2293 num_msgs_to_be_merged = 0;
2296 cdbfr = cdb_fetch(CDB_MSGLISTS, &CCC->room.QRnumber, sizeof(long));
2297 if (cdbfr == NULL) {
2301 msglist = (long *) cdbfr->ptr;
2302 cdbfr->ptr = NULL; /* CtdlSaveMsgPointerInRoom() now owns this memory */
2303 num_msgs = cdbfr->len / sizeof(long);
2308 /* Create a list of msgid's which were supplied by the caller, but do
2309 * not already exist in the target room. It is absolutely taboo to
2310 * have more than one reference to the same message in a room.
2312 for (i=0; i<num_newmsgs; ++i) {
2314 if (num_msgs > 0) for (j=0; j<num_msgs; ++j) {
2315 if (msglist[j] == newmsgidlist[i]) {
2320 msgs_to_be_merged[num_msgs_to_be_merged++] = newmsgidlist[i];
2324 MSG_syslog(LOG_DEBUG, "%d unique messages to be merged\n", num_msgs_to_be_merged);
2327 * Now merge the new messages
2329 msglist = realloc(msglist, (sizeof(long) * (num_msgs + num_msgs_to_be_merged)) );
2330 if (msglist == NULL) {
2331 MSGM_syslog(LOG_ALERT, "ERROR: can't realloc message list!\n");
2332 free(msgs_to_be_merged);
2333 return (ERROR + INTERNAL_ERROR);
2335 memcpy(&msglist[num_msgs], msgs_to_be_merged, (sizeof(long) * num_msgs_to_be_merged) );
2336 num_msgs += num_msgs_to_be_merged;
2338 /* Sort the message list, so all the msgid's are in order */
2339 num_msgs = sort_msglist(msglist, num_msgs);
2341 /* Determine the highest message number */
2342 highest_msg = msglist[num_msgs - 1];
2344 /* Write it back to disk. */
2345 cdb_store(CDB_MSGLISTS, &CCC->room.QRnumber, (int)sizeof(long),
2346 msglist, (int)(num_msgs * sizeof(long)));
2348 /* Free up the memory we used. */
2351 /* Update the highest-message pointer and unlock the room. */
2352 CCC->room.QRhighest = highest_msg;
2353 CtdlPutRoomLock(&CCC->room);
2355 /* Perform replication checks if necessary */
2356 if ( (DoesThisRoomNeedEuidIndexing(&CCC->room)) && (do_repl_check) ) {
2357 MSGM_syslog(LOG_DEBUG, "CtdlSaveMsgPointerInRoom() doing repl checks\n");
2359 for (i=0; i<num_msgs_to_be_merged; ++i) {
2360 msgid = msgs_to_be_merged[i];
2362 if (supplied_msg != NULL) {
2366 msg = CtdlFetchMessage(msgid, 0);
2370 ReplicationChecks(msg);
2372 /* If the message has an Exclusive ID, index that... */
2373 if (!CM_IsEmpty(msg, eExclusiveID)) {
2374 index_message_by_euid(msg->cm_fields[eExclusiveID], &CCC->room, msgid);
2377 /* Free up the memory we may have allocated */
2378 if (msg != supplied_msg) {
2387 MSGM_syslog(LOG_DEBUG, "CtdlSaveMsgPointerInRoom() skips repl checks\n");
2390 /* Submit this room for processing by hooks */
2391 PerformRoomHooks(&CCC->room);
2393 /* Go back to the room we were in before we wandered here... */
2394 CtdlGetRoom(&CCC->room, hold_rm);
2396 /* Bump the reference count for all messages which were merged */
2397 if (!suppress_refcount_adj) {
2398 AdjRefCountList(msgs_to_be_merged, num_msgs_to_be_merged, +1);
2401 /* Free up memory... */
2402 if (msgs_to_be_merged != NULL) {
2403 free(msgs_to_be_merged);
2406 /* Return success. */
2412 * This is the same as CtdlSaveMsgPointersInRoom() but it only accepts
2415 int CtdlSaveMsgPointerInRoom(char *roomname, long msgid,
2416 int do_repl_check, struct CtdlMessage *supplied_msg)
2418 return CtdlSaveMsgPointersInRoom(roomname, &msgid, 1, do_repl_check, supplied_msg, 0);
2425 * Message base operation to save a new message to the message store
2426 * (returns new message number)
2428 * This is the back end for CtdlSubmitMsg() and should not be directly
2429 * called by server-side modules.
2432 long send_message(struct CtdlMessage *msg) {
2433 struct CitContext *CCC = CC;
2442 /* Get a new message number */
2443 newmsgid = get_new_message_number();
2444 msgidbuflen = snprintf(msgidbuf, sizeof msgidbuf, "%08lX-%08lX@%s",
2445 (long unsigned int) time(NULL),
2446 (long unsigned int) newmsgid,
2450 /* Generate an ID if we don't have one already */
2451 if (CM_IsEmpty(msg, emessageId)) {
2452 CM_SetField(msg, emessageId, msgidbuf, msgidbuflen);
2455 /* If the message is big, set its body aside for storage elsewhere */
2456 if (!CM_IsEmpty(msg, eMesageText)) {
2457 if (strlen(msg->cm_fields[eMesageText]) > BIGMSG) {
2459 holdM = msg->cm_fields[eMesageText];
2460 msg->cm_fields[eMesageText] = NULL;
2464 /* Serialize our data structure for storage in the database */
2465 CtdlSerializeMessage(&smr, msg);
2468 msg->cm_fields[eMesageText] = holdM;
2472 cprintf("%d Unable to serialize message\n",
2473 ERROR + INTERNAL_ERROR);
2477 /* Write our little bundle of joy into the message base */
2478 if (cdb_store(CDB_MSGMAIN, &newmsgid, (int)sizeof(long),
2479 smr.ser, smr.len) < 0) {
2480 MSGM_syslog(LOG_ERR, "Can't store message\n");
2484 cdb_store(CDB_BIGMSGS,
2494 /* Free the memory we used for the serialized message */
2497 /* Return the *local* message ID to the caller
2498 * (even if we're storing an incoming network message)
2506 * Serialize a struct CtdlMessage into the format used on disk and network.
2508 * This function loads up a "struct ser_ret" (defined in server.h) which
2509 * contains the length of the serialized message and a pointer to the
2510 * serialized message in memory. THE LATTER MUST BE FREED BY THE CALLER.
2512 void CtdlSerializeMessage(struct ser_ret *ret, /* return values */
2513 struct CtdlMessage *msg) /* unserialized msg */
2515 struct CitContext *CCC = CC;
2516 size_t wlen, fieldlen;
2518 long lengths[NDiskFields];
2520 memset(lengths, 0, sizeof(lengths));
2523 * Check for valid message format
2525 if (CM_IsValidMsg(msg) == 0) {
2526 MSGM_syslog(LOG_ERR, "CtdlSerializeMessage() aborting due to invalid message\n");
2533 for (i=0; i < NDiskFields; ++i)
2534 if (msg->cm_fields[FieldOrder[i]] != NULL)
2536 lengths[i] = strlen(msg->cm_fields[FieldOrder[i]]);
2537 ret->len += lengths[i] + 2;
2540 ret->ser = malloc(ret->len);
2541 if (ret->ser == NULL) {
2542 MSG_syslog(LOG_ERR, "CtdlSerializeMessage() malloc(%ld) failed: %s\n",
2543 (long)ret->len, strerror(errno));
2550 ret->ser[1] = msg->cm_anon_type;
2551 ret->ser[2] = msg->cm_format_type;
2554 for (i=0; i < NDiskFields; ++i)
2555 if (msg->cm_fields[FieldOrder[i]] != NULL)
2557 fieldlen = lengths[i];
2558 ret->ser[wlen++] = (char)FieldOrder[i];
2560 memcpy(&ret->ser[wlen],
2561 msg->cm_fields[FieldOrder[i]],
2564 wlen = wlen + fieldlen + 1;
2567 if (ret->len != wlen) {
2568 MSG_syslog(LOG_ERR, "ERROR: len=%ld wlen=%ld\n",
2569 (long)ret->len, (long)wlen);
2577 * Check to see if any messages already exist in the current room which
2578 * carry the same Exclusive ID as this one. If any are found, delete them.
2580 void ReplicationChecks(struct CtdlMessage *msg) {
2581 struct CitContext *CCC = CC;
2582 long old_msgnum = (-1L);
2584 if (DoesThisRoomNeedEuidIndexing(&CCC->room) == 0) return;
2586 MSG_syslog(LOG_DEBUG, "Performing replication checks in <%s>\n",
2589 /* No exclusive id? Don't do anything. */
2590 if (msg == NULL) return;
2591 if (CM_IsEmpty(msg, eExclusiveID)) return;
2593 /*MSG_syslog(LOG_DEBUG, "Exclusive ID: <%s> for room <%s>\n",
2594 msg->cm_fields[eExclusiveID], CCC->room.QRname);*/
2596 old_msgnum = CtdlLocateMessageByEuid(msg->cm_fields[eExclusiveID], &CCC->room);
2597 if (old_msgnum > 0L) {
2598 MSG_syslog(LOG_DEBUG, "ReplicationChecks() replacing message %ld\n", old_msgnum);
2599 CtdlDeleteMessages(CCC->room.QRname, &old_msgnum, 1, "");
2606 * Save a message to disk and submit it into the delivery system.
2608 long CtdlSubmitMsg(struct CtdlMessage *msg, /* message to save */
2609 struct recptypes *recps, /* recipients (if mail) */
2610 const char *force, /* force a particular room? */
2611 int flags /* should the message be exported clean? */
2614 char submit_filename[128];
2615 char hold_rm[ROOMNAMELEN];
2616 char actual_rm[ROOMNAMELEN];
2617 char force_room[ROOMNAMELEN];
2618 char content_type[SIZ]; /* We have to learn this */
2619 char recipient[SIZ];
2622 const char *mptr = NULL;
2623 struct ctdluser userbuf;
2625 struct MetaData smi;
2626 FILE *network_fp = NULL;
2627 static int seqnum = 1;
2628 struct CtdlMessage *imsg = NULL;
2630 size_t instr_alloc = 0;
2632 char *hold_R, *hold_D;
2633 char *collected_addresses = NULL;
2634 struct addresses_to_be_filed *aptr = NULL;
2635 StrBuf *saved_rfc822_version = NULL;
2636 int qualified_for_journaling = 0;
2637 CitContext *CCC = MyContext();
2638 char bounce_to[1024] = "";
2641 MSGM_syslog(LOG_DEBUG, "CtdlSubmitMsg() called\n");
2642 if (CM_IsValidMsg(msg) == 0) return(-1); /* self check */
2644 /* If this message has no timestamp, we take the liberty of
2645 * giving it one, right now.
2647 if (CM_IsEmpty(msg, eTimestamp)) {
2648 CM_SetFieldLONG(msg, eTimestamp, time(NULL));
2651 /* If this message has no path, we generate one.
2653 if (CM_IsEmpty(msg, eMessagePath)) {
2654 if (!CM_IsEmpty(msg, eAuthor)) {
2655 CM_CopyField(msg, eMessagePath, eAuthor);
2656 for (a=0; !IsEmptyStr(&msg->cm_fields[eMessagePath][a]); ++a) {
2657 if (isspace(msg->cm_fields[eMessagePath][a])) {
2658 msg->cm_fields[eMessagePath][a] = ' ';
2663 CM_SetField(msg, eMessagePath, HKEY("unknown"));
2667 if (force == NULL) {
2668 force_room[0] = '\0';
2671 strcpy(force_room, force);
2674 /* Learn about what's inside, because it's what's inside that counts */
2675 if (CM_IsEmpty(msg, eMesageText)) {
2676 MSGM_syslog(LOG_ERR, "ERROR: attempt to save message with NULL body\n");
2680 switch (msg->cm_format_type) {
2682 strcpy(content_type, "text/x-citadel-variformat");
2685 strcpy(content_type, "text/plain");
2688 strcpy(content_type, "text/plain");
2689 mptr = bmstrcasestr(msg->cm_fields[eMesageText], "Content-type:");
2692 safestrncpy(content_type, &mptr[13], sizeof content_type);
2693 striplt(content_type);
2694 aptr = content_type;
2695 while (!IsEmptyStr(aptr)) {
2707 /* Goto the correct room */
2708 room = (recps) ? CCC->room.QRname : SENTITEMS;
2709 MSG_syslog(LOG_DEBUG, "Selected room %s\n", room);
2710 strcpy(hold_rm, CCC->room.QRname);
2711 strcpy(actual_rm, CCC->room.QRname);
2712 if (recps != NULL) {
2713 strcpy(actual_rm, SENTITEMS);
2716 /* If the user is a twit, move to the twit room for posting */
2718 if (CCC->user.axlevel == AxProbU) {
2719 strcpy(hold_rm, actual_rm);
2720 strcpy(actual_rm, config.c_twitroom);
2721 MSGM_syslog(LOG_DEBUG, "Diverting to twit room\n");
2725 /* ...or if this message is destined for Aide> then go there. */
2726 if (!IsEmptyStr(force_room)) {
2727 strcpy(actual_rm, force_room);
2730 MSG_syslog(LOG_INFO, "Final selection: %s (%s)\n", actual_rm, room);
2731 if (strcasecmp(actual_rm, CCC->room.QRname)) {
2732 /* CtdlGetRoom(&CCC->room, actual_rm); */
2733 CtdlUserGoto(actual_rm, 0, 1, NULL, NULL);
2737 * If this message has no O (room) field, generate one.
2739 if (CM_IsEmpty(msg, eOriginalRoom)) {
2740 CM_SetField(msg, eOriginalRoom, CCC->room.QRname, strlen(CCC->room.QRname));
2743 /* Perform "before save" hooks (aborting if any return nonzero) */
2744 MSGM_syslog(LOG_DEBUG, "Performing before-save hooks\n");
2745 if (PerformMessageHooks(msg, EVT_BEFORESAVE) > 0) return(-3);
2748 * If this message has an Exclusive ID, and the room is replication
2749 * checking enabled, then do replication checks.
2751 if (DoesThisRoomNeedEuidIndexing(&CCC->room)) {
2752 ReplicationChecks(msg);
2755 /* Save it to disk */
2756 MSGM_syslog(LOG_DEBUG, "Saving to disk\n");
2757 newmsgid = send_message(msg);
2758 if (newmsgid <= 0L) return(-5);
2760 /* Write a supplemental message info record. This doesn't have to
2761 * be a critical section because nobody else knows about this message
2764 MSGM_syslog(LOG_DEBUG, "Creating MetaData record\n");
2765 memset(&smi, 0, sizeof(struct MetaData));
2766 smi.meta_msgnum = newmsgid;
2767 smi.meta_refcount = 0;
2768 safestrncpy(smi.meta_content_type, content_type,
2769 sizeof smi.meta_content_type);
2772 * Measure how big this message will be when rendered as RFC822.
2773 * We do this for two reasons:
2774 * 1. We need the RFC822 length for the new metadata record, so the
2775 * POP and IMAP services don't have to calculate message lengths
2776 * while the user is waiting (multiplied by potentially hundreds
2777 * or thousands of messages).
2778 * 2. If journaling is enabled, we will need an RFC822 version of the
2779 * message to attach to the journalized copy.
2781 if (CCC->redirect_buffer != NULL) {
2782 MSGM_syslog(LOG_ALERT, "CCC->redirect_buffer is not NULL during message submission!\n");
2785 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
2786 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, QP_EADDR);
2787 smi.meta_rfc822_length = StrLength(CCC->redirect_buffer);
2788 saved_rfc822_version = CCC->redirect_buffer;
2789 CCC->redirect_buffer = NULL;
2793 /* Now figure out where to store the pointers */
2794 MSGM_syslog(LOG_DEBUG, "Storing pointers\n");
2796 /* If this is being done by the networker delivering a private
2797 * message, we want to BYPASS saving the sender's copy (because there
2798 * is no local sender; it would otherwise go to the Trashcan).
2800 if ((!CCC->internal_pgm) || (recps == NULL)) {
2801 if (CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 1, msg) != 0) {
2802 MSGM_syslog(LOG_ERR, "ERROR saving message pointer!\n");
2803 CtdlSaveMsgPointerInRoom(config.c_aideroom, newmsgid, 0, msg);
2807 /* For internet mail, drop a copy in the outbound queue room */
2808 if ((recps != NULL) && (recps->num_internet > 0)) {
2809 CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, newmsgid, 0, msg);
2812 /* If other rooms are specified, drop them there too. */
2813 if ((recps != NULL) && (recps->num_room > 0))
2814 for (i=0; i<num_tokens(recps->recp_room, '|'); ++i) {
2815 extract_token(recipient, recps->recp_room, i,
2816 '|', sizeof recipient);
2817 MSG_syslog(LOG_DEBUG, "Delivering to room <%s>\n", recipient);///// xxxx
2818 CtdlSaveMsgPointerInRoom(recipient, newmsgid, 0, msg);
2821 /* Bump this user's messages posted counter. */
2822 MSGM_syslog(LOG_DEBUG, "Updating user\n");
2823 CtdlGetUserLock(&CCC->user, CCC->curr_user);
2824 CCC->user.posted = CCC->user.posted + 1;
2825 CtdlPutUserLock(&CCC->user);
2827 /* Decide where bounces need to be delivered */
2828 if ((recps != NULL) && (recps->bounce_to != NULL)) {
2829 safestrncpy(bounce_to, recps->bounce_to, sizeof bounce_to);
2831 else if (CCC->logged_in) {
2832 snprintf(bounce_to, sizeof bounce_to, "%s@%s", CCC->user.fullname, config.c_nodename);
2835 snprintf(bounce_to, sizeof bounce_to, "%s@%s", msg->cm_fields[eAuthor], msg->cm_fields[eNodeName]);
2838 /* If this is private, local mail, make a copy in the
2839 * recipient's mailbox and bump the reference count.
2841 if ((recps != NULL) && (recps->num_local > 0))
2842 for (i=0; i<num_tokens(recps->recp_local, '|'); ++i) {
2844 recipientlen = extract_token(recipient,
2845 recps->recp_local, i,
2846 '|', sizeof recipient);
2847 MSG_syslog(LOG_DEBUG, "Delivering private local mail to <%s>\n",
2849 if (CtdlGetUser(&userbuf, recipient) == 0) {
2850 CtdlMailboxName(actual_rm, sizeof actual_rm, &userbuf, MAILROOM);
2851 CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0, msg);
2852 CtdlBumpNewMailCounter(userbuf.usernum);
2853 if (!IsEmptyStr(config.c_funambol_host) || !IsEmptyStr(config.c_pager_program)) {
2854 /* Generate a instruction message for the Funambol notification
2855 * server, in the same style as the SMTP queue
2859 instr = malloc(instr_alloc);
2860 instrlen = snprintf(
2862 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
2866 (long)time(NULL), //todo: time() is expensive!
2870 imsg = malloc(sizeof(struct CtdlMessage));
2871 memset(imsg, 0, sizeof(struct CtdlMessage));
2872 imsg->cm_magic = CTDLMESSAGE_MAGIC;
2873 imsg->cm_anon_type = MES_NORMAL;
2874 imsg->cm_format_type = FMT_RFC822;
2875 CM_SetField(imsg, eMsgSubject, HKEY("QMSG"));
2876 CM_SetField(imsg, eAuthor, HKEY("Citadel"));
2877 CM_SetField(imsg, eJournal, HKEY("do not journal"));
2878 CM_SetAsField(imsg, eMesageText, &instr, instrlen);
2879 CM_SetField(imsg, eExtnotify, recipient, recipientlen);
2880 CtdlSubmitMsg(imsg, NULL, FNBL_QUEUE_ROOM, 0);
2885 MSG_syslog(LOG_DEBUG, "No user <%s>\n", recipient);
2886 CtdlSaveMsgPointerInRoom(config.c_aideroom, newmsgid, 0, msg);
2890 /* Perform "after save" hooks */
2891 MSGM_syslog(LOG_DEBUG, "Performing after-save hooks\n");
2893 CM_SetFieldLONG(msg, eVltMsgNum, newmsgid);
2894 PerformMessageHooks(msg, EVT_AFTERSAVE);
2895 CM_FlushField(msg, eVltMsgNum);
2897 /* For IGnet mail, we have to save a new copy into the spooler for
2898 * each recipient, with the R and D fields set to the recipient and
2899 * destination-node. This has two ugly side effects: all other
2900 * recipients end up being unlisted in this recipient's copy of the
2901 * message, and it has to deliver multiple messages to the same
2902 * node. We'll revisit this again in a year or so when everyone has
2903 * a network spool receiver that can handle the new style messages.
2905 if ((recps != NULL) && (recps->num_ignet > 0))
2906 for (i=0; i<num_tokens(recps->recp_ignet, '|'); ++i) {
2907 extract_token(recipient, recps->recp_ignet, i,
2908 '|', sizeof recipient);
2910 hold_R = msg->cm_fields[eRecipient];
2911 hold_D = msg->cm_fields[eDestination];
2912 msg->cm_fields[eRecipient] = malloc(SIZ);
2913 msg->cm_fields[eDestination] = malloc(128);
2914 extract_token(msg->cm_fields[eRecipient], recipient, 0, '@', SIZ);
2915 extract_token(msg->cm_fields[eDestination], recipient, 1, '@', 128);
2917 CtdlSerializeMessage(&smr, msg);
2919 snprintf(submit_filename, sizeof submit_filename,
2920 "%s/netmail.%04lx.%04x.%04x",
2922 (long) getpid(), CCC->cs_pid, ++seqnum);
2923 network_fp = fopen(submit_filename, "wb+");
2924 if (network_fp != NULL) {
2925 rv = fwrite(smr.ser, smr.len, 1, network_fp);
2927 MSG_syslog(LOG_EMERG, "CtdlSubmitMsg(): Couldn't write network spool file: %s\n",
2935 free(msg->cm_fields[eRecipient]);
2936 free(msg->cm_fields[eDestination]);
2937 msg->cm_fields[eRecipient] = hold_R;
2938 msg->cm_fields[eDestination] = hold_D;
2941 /* Go back to the room we started from */
2942 MSG_syslog(LOG_DEBUG, "Returning to original room %s\n", hold_rm);
2943 if (strcasecmp(hold_rm, CCC->room.QRname))
2944 CtdlUserGoto(hold_rm, 0, 1, NULL, NULL);
2946 /* For internet mail, generate delivery instructions.
2947 * Yes, this is recursive. Deal with it. Infinite recursion does
2948 * not happen because the delivery instructions message does not
2949 * contain a recipient.
2951 if ((recps != NULL) && (recps->num_internet > 0)) {
2952 StrBuf *SpoolMsg = NewStrBuf();
2955 MSGM_syslog(LOG_DEBUG, "Generating delivery instructions\n");
2957 StrBufPrintf(SpoolMsg,
2958 "Content-type: "SPOOLMIME"\n"
2967 if (recps->envelope_from != NULL) {
2968 StrBufAppendBufPlain(SpoolMsg, HKEY("envelope_from|"), 0);
2969 StrBufAppendBufPlain(SpoolMsg, recps->envelope_from, -1, 0);
2970 StrBufAppendBufPlain(SpoolMsg, HKEY("\n"), 0);
2972 if (recps->sending_room != NULL) {
2973 StrBufAppendBufPlain(SpoolMsg, HKEY("source_room|"), 0);
2974 StrBufAppendBufPlain(SpoolMsg, recps->sending_room, -1, 0);
2975 StrBufAppendBufPlain(SpoolMsg, HKEY("\n"), 0);
2978 nTokens = num_tokens(recps->recp_internet, '|');
2979 for (i = 0; i < nTokens; i++) {
2981 len = extract_token(recipient, recps->recp_internet, i, '|', sizeof recipient);
2983 StrBufAppendBufPlain(SpoolMsg, HKEY("remote|"), 0);
2984 StrBufAppendBufPlain(SpoolMsg, recipient, len, 0);
2985 StrBufAppendBufPlain(SpoolMsg, HKEY("|0||\n"), 0);
2989 imsg = malloc(sizeof(struct CtdlMessage));
2990 memset(imsg, 0, sizeof(struct CtdlMessage));
2991 imsg->cm_magic = CTDLMESSAGE_MAGIC;
2992 imsg->cm_anon_type = MES_NORMAL;
2993 imsg->cm_format_type = FMT_RFC822;
2994 imsg->cm_fields[eMsgSubject] = strdup("QMSG");
2995 imsg->cm_fields[eAuthor] = strdup("Citadel");
2996 imsg->cm_fields[eJournal] = strdup("do not journal");
2997 imsg->cm_fields[eMesageText] = SmashStrBuf(&SpoolMsg); /* imsg owns this memory now */
2998 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM, QP_EADDR);
3003 * Any addresses to harvest for someone's address book?
3005 if ( (CCC->logged_in) && (recps != NULL) ) {
3006 collected_addresses = harvest_collected_addresses(msg);
3009 if (collected_addresses != NULL) {
3010 aptr = (struct addresses_to_be_filed *)
3011 malloc(sizeof(struct addresses_to_be_filed));
3012 CtdlMailboxName(actual_rm, sizeof actual_rm,
3013 &CCC->user, USERCONTACTSROOM);
3014 aptr->roomname = strdup(actual_rm);
3015 aptr->collected_addresses = collected_addresses;
3016 begin_critical_section(S_ATBF);
3019 end_critical_section(S_ATBF);
3023 * Determine whether this message qualifies for journaling.
3025 if (!CM_IsEmpty(msg, eJournal)) {
3026 qualified_for_journaling = 0;
3029 if (recps == NULL) {
3030 qualified_for_journaling = config.c_journal_pubmsgs;
3032 else if (recps->num_local + recps->num_ignet + recps->num_internet > 0) {
3033 qualified_for_journaling = config.c_journal_email;
3036 qualified_for_journaling = config.c_journal_pubmsgs;
3041 * Do we have to perform journaling? If so, hand off the saved
3042 * RFC822 version will be handed off to the journaler for background
3043 * submit. Otherwise, we have to free the memory ourselves.
3045 if (saved_rfc822_version != NULL) {
3046 if (qualified_for_journaling) {
3047 JournalBackgroundSubmit(msg, saved_rfc822_version, recps);
3050 FreeStrBuf(&saved_rfc822_version);
3060 * Convenience function for generating small administrative messages.
3062 void quickie_message(const char *from,
3063 const char *fromaddr,
3068 const char *subject)
3070 struct CtdlMessage *msg;
3071 struct recptypes *recp = NULL;
3073 msg = malloc(sizeof(struct CtdlMessage));
3074 memset(msg, 0, sizeof(struct CtdlMessage));
3075 msg->cm_magic = CTDLMESSAGE_MAGIC;
3076 msg->cm_anon_type = MES_NORMAL;
3077 msg->cm_format_type = format_type;
3080 msg->cm_fields[eAuthor] = strdup(from);
3082 else if (fromaddr != NULL) {
3083 msg->cm_fields[eAuthor] = strdup(fromaddr);
3084 if (strchr(msg->cm_fields[eAuthor], '@')) {
3085 *strchr(msg->cm_fields[eAuthor], '@') = 0;
3089 msg->cm_fields[eAuthor] = strdup("Citadel");
3092 if (fromaddr != NULL) msg->cm_fields[erFc822Addr] = strdup(fromaddr);
3093 if (room != NULL) msg->cm_fields[eOriginalRoom] = strdup(room);
3094 msg->cm_fields[eNodeName] = strdup(NODENAME);
3096 msg->cm_fields[eRecipient] = strdup(to);
3097 recp = validate_recipients(to, NULL, 0);
3099 if (subject != NULL) {
3100 msg->cm_fields[eMsgSubject] = strdup(subject);
3102 msg->cm_fields[eMesageText] = strdup(text);
3104 CtdlSubmitMsg(msg, recp, room, 0);
3106 if (recp != NULL) free_recipients(recp);
3109 void flood_protect_quickie_message(const char *from,
3110 const char *fromaddr,
3115 const char *subject,
3117 const char **CritStr,
3124 u_char rawdigest[MD5_DIGEST_LEN];
3125 struct MD5Context md5context;
3129 time_t tsday = NOW / (8*60*60); /* just care for a day... */
3131 tslen = snprintf(timestamp, sizeof(timestamp), "%ld", tsday);
3132 MD5Init(&md5context);
3134 for (i = 0; i < nCriterions; i++)
3135 MD5Update(&md5context,
3136 (const unsigned char*)CritStr[i], CritStrLen[i]);
3137 MD5Update(&md5context,
3138 (const unsigned char*)timestamp, tslen);
3139 MD5Final(rawdigest, &md5context);
3141 guid = NewStrBufPlain(NULL,
3142 MD5_DIGEST_LEN * 2 + 12);
3143 StrBufHexEscAppend(guid, NULL, rawdigest, MD5_DIGEST_LEN);
3144 StrBufAppendBufPlain(guid, HKEY("_fldpt"), 0);
3145 if (StrLength(guid) > 40)
3146 StrBufCutAt(guid, 40, NULL);
3148 if (CheckIfAlreadySeen("FPAideMessage",
3157 /* yes, we did. flood protection kicks in. */
3159 "not sending message again\n");
3163 /* no, this message isn't sent recently; go ahead. */
3164 quickie_message(from,
3175 * Back end function used by CtdlMakeMessage() and similar functions
3177 StrBuf *CtdlReadMessageBodyBuf(char *terminator, /* token signalling EOT */
3179 size_t maxlen, /* maximum message length */
3180 StrBuf *exist, /* if non-null, append to it;
3181 exist is ALWAYS freed */
3182 int crlf, /* CRLF newlines instead of LF */
3183 int *sock /* socket handle or 0 for this session's client socket */
3192 LineBuf = NewStrBufPlain(NULL, SIZ);
3193 if (exist == NULL) {
3194 Message = NewStrBufPlain(NULL, 4 * SIZ);
3197 Message = NewStrBufDup(exist);
3200 /* Do we need to change leading ".." to "." for SMTP escaping? */
3201 if ((tlen == 1) && (*terminator == '.')) {
3205 /* read in the lines of message text one by one */
3208 if ((CtdlSockGetLine(sock, LineBuf, 5) < 0) ||
3213 if (CtdlClientGetLine(LineBuf) < 0) finished = 1;
3215 if ((StrLength(LineBuf) == tlen) &&
3216 (!strcmp(ChrPtr(LineBuf), terminator)))
3219 if ( (!flushing) && (!finished) ) {
3221 StrBufAppendBufPlain(LineBuf, HKEY("\r\n"), 0);
3224 StrBufAppendBufPlain(LineBuf, HKEY("\n"), 0);
3227 /* Unescape SMTP-style input of two dots at the beginning of the line */
3229 (StrLength(LineBuf) == 2) &&
3230 (!strcmp(ChrPtr(LineBuf), "..")))
3232 StrBufCutLeft(LineBuf, 1);
3235 StrBufAppendBuf(Message, LineBuf, 0);
3238 /* if we've hit the max msg length, flush the rest */
3239 if (StrLength(Message) >= maxlen) flushing = 1;
3241 } while (!finished);
3242 FreeStrBuf(&LineBuf);
3246 void DeleteAsyncMsg(ReadAsyncMsg **Msg)
3250 FreeStrBuf(&(*Msg)->MsgBuf);
3256 ReadAsyncMsg *NewAsyncMsg(const char *terminator, /* token signalling EOT */
3258 size_t maxlen, /* maximum message length */
3259 size_t expectlen, /* if we expect a message, how long should it be? */
3260 StrBuf *exist, /* if non-null, append to it;
3261 exist is ALWAYS freed */
3262 long eLen, /* length of exist */
3263 int crlf /* CRLF newlines instead of LF */
3266 ReadAsyncMsg *NewMsg;
3268 NewMsg = (ReadAsyncMsg *)malloc(sizeof(ReadAsyncMsg));
3269 memset(NewMsg, 0, sizeof(ReadAsyncMsg));
3271 if (exist == NULL) {
3274 if (expectlen == 0) {
3278 len = expectlen + 10;
3280 NewMsg->MsgBuf = NewStrBufPlain(NULL, len);
3283 NewMsg->MsgBuf = NewStrBufDup(exist);
3285 /* Do we need to change leading ".." to "." for SMTP escaping? */
3286 if ((tlen == 1) && (*terminator == '.')) {
3290 NewMsg->terminator = terminator;
3291 NewMsg->tlen = tlen;
3293 NewMsg->maxlen = maxlen;
3295 NewMsg->crlf = crlf;
3301 * Back end function used by CtdlMakeMessage() and similar functions
3303 eReadState CtdlReadMessageBodyAsync(AsyncIO *IO)
3305 ReadAsyncMsg *ReadMsg;
3306 int MsgFinished = 0;
3307 eReadState Finished = eMustReadMore;
3312 const char *pch = ChrPtr(IO->SendBuf.Buf);
3313 const char *pchh = IO->SendBuf.ReadWritePointer;
3319 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
3320 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
3321 ((CitContext*)(IO->CitContext))->ServiceName,
3324 fd = fopen(fn, "a+");
3327 ReadMsg = IO->ReadMsg;
3329 /* read in the lines of message text one by one */
3331 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
3334 case eMustReadMore: /// read new from socket...
3336 if (IO->RecvBuf.ReadWritePointer != NULL) {
3337 nbytes = StrLength(IO->RecvBuf.Buf) - (IO->RecvBuf.ReadWritePointer - ChrPtr(IO->RecvBuf.Buf));
3338 fprintf(fd, "Read; Line unfinished: %ld Bytes still in buffer [", nbytes);
3340 fwrite(IO->RecvBuf.ReadWritePointer, nbytes, 1, fd);
3344 fprintf(fd, "BufferEmpty! \n");
3350 case eBufferNotEmpty: /* shouldn't happen... */
3351 case eReadSuccess: /// done for now...
3353 case eReadFail: /// WHUT?
3359 if ((StrLength(IO->IOBuf) == ReadMsg->tlen) &&
3360 (!strcmp(ChrPtr(IO->IOBuf), ReadMsg->terminator))) {
3363 fprintf(fd, "found Terminator; Message Size: %d\n", StrLength(ReadMsg->MsgBuf));
3366 else if (!ReadMsg->flushing) {
3369 fprintf(fd, "Read Line: [%d][%s]\n", StrLength(IO->IOBuf), ChrPtr(IO->IOBuf));
3372 /* Unescape SMTP-style input of two dots at the beginning of the line */
3373 if ((ReadMsg->dodot) &&
3374 (StrLength(IO->IOBuf) == 2) && /* TODO: do we just unescape lines with two dots or any line? */
3375 (!strcmp(ChrPtr(IO->IOBuf), "..")))
3378 fprintf(fd, "UnEscaped!\n");
3380 StrBufCutLeft(IO->IOBuf, 1);
3383 if (ReadMsg->crlf) {
3384 StrBufAppendBufPlain(IO->IOBuf, HKEY("\r\n"), 0);
3387 StrBufAppendBufPlain(IO->IOBuf, HKEY("\n"), 0);
3390 StrBufAppendBuf(ReadMsg->MsgBuf, IO->IOBuf, 0);
3393 /* if we've hit the max msg length, flush the rest */
3394 if (StrLength(ReadMsg->MsgBuf) >= ReadMsg->maxlen) ReadMsg->flushing = 1;
3396 } while (!MsgFinished);
3399 fprintf(fd, "Done with reading; %s.\n, ",
3400 (MsgFinished)?"Message Finished": "FAILED");
3404 return eReadSuccess;
3411 * Back end function used by CtdlMakeMessage() and similar functions
3413 char *CtdlReadMessageBody(char *terminator, /* token signalling EOT */
3415 size_t maxlen, /* maximum message length */
3416 StrBuf *exist, /* if non-null, append to it;
3417 exist is ALWAYS freed */
3418 int crlf, /* CRLF newlines instead of LF */
3419 int *sock /* socket handle or 0 for this session's client socket */
3424 Message = CtdlReadMessageBodyBuf(terminator,
3430 if (Message == NULL)
3433 return SmashStrBuf(&Message);
3438 * Build a binary message to be saved on disk.
3439 * (NOTE: if you supply 'preformatted_text', the buffer you give it
3440 * will become part of the message. This means you are no longer
3441 * responsible for managing that memory -- it will be freed along with
3442 * the rest of the fields when CM_Free() is called.)
3445 struct CtdlMessage *CtdlMakeMessage(
3446 struct ctdluser *author, /* author's user structure */
3447 char *recipient, /* NULL if it's not mail */
3448 char *recp_cc, /* NULL if it's not mail */
3449 char *room, /* room where it's going */
3450 int type, /* see MES_ types in header file */
3451 int format_type, /* variformat, plain text, MIME... */
3452 char *fake_name, /* who we're masquerading as */
3453 char *my_email, /* which of my email addresses to use (empty is ok) */
3454 char *subject, /* Subject (optional) */
3455 char *supplied_euid, /* ...or NULL if this is irrelevant */
3456 char *preformatted_text, /* ...or NULL to read text from client */
3457 char *references /* Thread references */
3459 char dest_node[256];
3461 struct CtdlMessage *msg;
3463 StrBuf *FakeEncAuthor = NULL;
3465 msg = malloc(sizeof(struct CtdlMessage));
3466 memset(msg, 0, sizeof(struct CtdlMessage));
3467 msg->cm_magic = CTDLMESSAGE_MAGIC;
3468 msg->cm_anon_type = type;
3469 msg->cm_format_type = format_type;
3471 /* Don't confuse the poor folks if it's not routed mail. */
3472 strcpy(dest_node, "");
3474 if (recipient != NULL) striplt(recipient);
3475 if (recp_cc != NULL) striplt(recp_cc);
3477 /* Path or Return-Path */
3478 if (my_email == NULL) my_email = "";
3480 if (!IsEmptyStr(my_email)) {
3481 msg->cm_fields[eMessagePath] = strdup(my_email);
3484 snprintf(buf, sizeof buf, "%s", author->fullname);
3485 msg->cm_fields[eMessagePath] = strdup(buf);
3487 convert_spaces_to_underscores(msg->cm_fields[eMessagePath]);
3489 snprintf(buf, sizeof buf, "%ld", (long)time(NULL)); /* timestamp */
3490 msg->cm_fields[eTimestamp] = strdup(buf);
3492 if ((fake_name != NULL) && (fake_name[0])) { /* author */
3493 FakeAuthor = NewStrBufPlain (fake_name, -1);
3496 FakeAuthor = NewStrBufPlain (author->fullname, -1);
3498 StrBufRFC2047encode(&FakeEncAuthor, FakeAuthor);
3499 msg->cm_fields[eAuthor] = SmashStrBuf(&FakeEncAuthor);
3500 FreeStrBuf(&FakeAuthor);
3502 if (CC->room.QRflags & QR_MAILBOX) { /* room */
3503 msg->cm_fields[eOriginalRoom] = strdup(&CC->room.QRname[11]);
3506 msg->cm_fields[eOriginalRoom] = strdup(CC->room.QRname);
3509 msg->cm_fields[eNodeName] = strdup(NODENAME); /* nodename */
3510 msg->cm_fields[eHumanNode] = strdup(HUMANNODE); /* hnodename */
3512 if ((recipient != NULL) && (recipient[0] != 0)) {
3513 msg->cm_fields[eRecipient] = strdup(recipient);
3515 if ((recp_cc != NULL) && (recp_cc[0] != 0)) {
3516 msg->cm_fields[eCarbonCopY] = strdup(recp_cc);
3518 if (dest_node[0] != 0) {
3519 msg->cm_fields[eDestination] = strdup(dest_node);
3522 if (!IsEmptyStr(my_email)) {
3523 msg->cm_fields[erFc822Addr] = strdup(my_email);
3525 else if ( (author == &CC->user) && (!IsEmptyStr(CC->cs_inet_email)) ) {
3526 msg->cm_fields[erFc822Addr] = strdup(CC->cs_inet_email);
3529 if (subject != NULL) {
3532 length = strlen(subject);
3538 while ((subject[i] != '\0') &&
3539 (IsAscii = isascii(subject[i]) != 0 ))
3542 msg->cm_fields[eMsgSubject] = strdup(subject);
3543 else /* ok, we've got utf8 in the string. */
3545 msg->cm_fields[eMsgSubject] = rfc2047encode(subject, length);
3551 if (supplied_euid != NULL) {
3552 msg->cm_fields[eExclusiveID] = strdup(supplied_euid);
3555 if ((references != NULL) && (!IsEmptyStr(references))) {
3556 if (msg->cm_fields[eWeferences] != NULL)
3557 free(msg->cm_fields[eWeferences]);
3558 msg->cm_fields[eWeferences] = strdup(references);
3561 if (preformatted_text != NULL) {
3562 msg->cm_fields[eMesageText] = preformatted_text;
3565 msg->cm_fields[eMesageText] = CtdlReadMessageBody(HKEY("000"), config.c_maxmsglen, NULL, 0, 0);
3575 * API function to delete messages which match a set of criteria
3576 * (returns the actual number of messages deleted)
3578 int CtdlDeleteMessages(char *room_name, /* which room */
3579 long *dmsgnums, /* array of msg numbers to be deleted */
3580 int num_dmsgnums, /* number of msgs to be deleted, or 0 for "any" */
3581 char *content_type /* or "" for any. regular expressions expected. */
3584 struct CitContext *CCC = CC;
3585 struct ctdlroom qrbuf;
3586 struct cdbdata *cdbfr;
3587 long *msglist = NULL;
3588 long *dellist = NULL;
3591 int num_deleted = 0;
3593 struct MetaData smi;
3596 int need_to_free_re = 0;
3598 if (content_type) if (!IsEmptyStr(content_type)) {
3599 regcomp(&re, content_type, 0);
3600 need_to_free_re = 1;
3602 MSG_syslog(LOG_DEBUG, " CtdlDeleteMessages(%s, %d msgs, %s)\n",
3603 room_name, num_dmsgnums, content_type);
3605 /* get room record, obtaining a lock... */
3606 if (CtdlGetRoomLock(&qrbuf, room_name) != 0) {
3607 MSG_syslog(LOG_ERR, " CtdlDeleteMessages(): Room <%s> not found\n",
3609 if (need_to_free_re) regfree(&re);
3610 return (0); /* room not found */
3612 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
3614 if (cdbfr != NULL) {
3615 dellist = malloc(cdbfr->len);
3616 msglist = (long *) cdbfr->ptr;
3617 cdbfr->ptr = NULL; /* CtdlDeleteMessages() now owns this memory */
3618 num_msgs = cdbfr->len / sizeof(long);
3622 int have_contenttype = (content_type != NULL) && !IsEmptyStr(content_type);
3623 int have_delmsgs = (num_dmsgnums == 0) || (dmsgnums == NULL);
3624 int have_more_del = 1;
3626 num_msgs = sort_msglist(msglist, num_msgs);
3627 if (num_dmsgnums > 1)
3628 num_dmsgnums = sort_msglist(dmsgnums, num_dmsgnums);
3631 StrBuf *dbg = NewStrBuf();
3632 for (i = 0; i < num_dmsgnums; i++)
3633 StrBufAppendPrintf(dbg, ", %ld", dmsgnums[i]);
3634 MSG_syslog(LOG_DEBUG, " Deleting before: %s", ChrPtr(dbg));
3639 while ((i < num_msgs) && (have_more_del)) {
3642 /* Set/clear a bit for each criterion */
3644 /* 0 messages in the list or a null list means that we are
3645 * interested in deleting any messages which meet the other criteria.
3648 delete_this |= 0x01;
3651 while ((i < num_msgs) && (msglist[i] < dmsgnums[j])) i++;
3656 if (msglist[i] == dmsgnums[j]) {
3657 delete_this |= 0x01;
3660 have_more_del = (j < num_dmsgnums);
3663 if (have_contenttype) {
3664 GetMetaData(&smi, msglist[i]);
3665 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) == 0) {
3666 delete_this |= 0x02;
3669 delete_this |= 0x02;
3672 /* Delete message only if all bits are set */
3673 if (delete_this == 0x03) {
3674 dellist[num_deleted++] = msglist[i];
3681 StrBuf *dbg = NewStrBuf();
3682 for (i = 0; i < num_deleted; i++)
3683 StrBufAppendPrintf(dbg, ", %ld", dellist[i]);
3684 MSG_syslog(LOG_DEBUG, " Deleting: %s", ChrPtr(dbg));
3688 num_msgs = sort_msglist(msglist, num_msgs);
3689 cdb_store(CDB_MSGLISTS, &qrbuf.QRnumber, (int)sizeof(long),
3690 msglist, (int)(num_msgs * sizeof(long)));
3693 qrbuf.QRhighest = msglist[num_msgs - 1];
3695 qrbuf.QRhighest = 0;
3697 CtdlPutRoomLock(&qrbuf);
3699 /* Go through the messages we pulled out of the index, and decrement
3700 * their reference counts by 1. If this is the only room the message
3701 * was in, the reference count will reach zero and the message will
3702 * automatically be deleted from the database. We do this in a
3703 * separate pass because there might be plug-in hooks getting called,
3704 * and we don't want that happening during an S_ROOMS critical
3708 for (i=0; i<num_deleted; ++i) {
3709 PerformDeleteHooks(qrbuf.QRname, dellist[i]);
3711 AdjRefCountList(dellist, num_deleted, -1);
3713 /* Now free the memory we used, and go away. */
3714 if (msglist != NULL) free(msglist);
3715 if (dellist != NULL) free(dellist);
3716 MSG_syslog(LOG_DEBUG, " %d message(s) deleted.\n", num_deleted);
3717 if (need_to_free_re) regfree(&re);
3718 return (num_deleted);
3725 * GetMetaData() - Get the supplementary record for a message
3727 void GetMetaData(struct MetaData *smibuf, long msgnum)
3730 struct cdbdata *cdbsmi;
3733 memset(smibuf, 0, sizeof(struct MetaData));
3734 smibuf->meta_msgnum = msgnum;
3735 smibuf->meta_refcount = 1; /* Default reference count is 1 */
3737 /* Use the negative of the message number for its supp record index */
3738 TheIndex = (0L - msgnum);
3740 cdbsmi = cdb_fetch(CDB_MSGMAIN, &TheIndex, sizeof(long));
3741 if (cdbsmi == NULL) {
3742 return; /* record not found; go with defaults */
3744 memcpy(smibuf, cdbsmi->ptr,
3745 ((cdbsmi->len > sizeof(struct MetaData)) ?
3746 sizeof(struct MetaData) : cdbsmi->len));
3753 * PutMetaData() - (re)write supplementary record for a message
3755 void PutMetaData(struct MetaData *smibuf)
3759 /* Use the negative of the message number for the metadata db index */
3760 TheIndex = (0L - smibuf->meta_msgnum);
3762 cdb_store(CDB_MSGMAIN,
3763 &TheIndex, (int)sizeof(long),
3764 smibuf, (int)sizeof(struct MetaData));
3769 * AdjRefCount - submit an adjustment to the reference count for a message.
3770 * (These are just queued -- we actually process them later.)
3772 void AdjRefCount(long msgnum, int incr)
3774 struct CitContext *CCC = CC;
3775 struct arcq new_arcq;
3778 MSG_syslog(LOG_DEBUG, "AdjRefCount() msg %ld ref count delta %+d\n", msgnum, incr);
3780 begin_critical_section(S_SUPPMSGMAIN);
3781 if (arcfp == NULL) {
3782 arcfp = fopen(file_arcq, "ab+");
3783 chown(file_arcq, CTDLUID, (-1));
3784 chmod(file_arcq, 0600);
3786 end_critical_section(S_SUPPMSGMAIN);
3788 /* msgnum < 0 means that we're trying to close the file */
3790 MSGM_syslog(LOG_DEBUG, "Closing the AdjRefCount queue file\n");
3791 begin_critical_section(S_SUPPMSGMAIN);
3792 if (arcfp != NULL) {
3796 end_critical_section(S_SUPPMSGMAIN);
3801 * If we can't open the queue, perform the operation synchronously.
3803 if (arcfp == NULL) {
3804 TDAP_AdjRefCount(msgnum, incr);
3808 new_arcq.arcq_msgnum = msgnum;
3809 new_arcq.arcq_delta = incr;
3810 rv = fwrite(&new_arcq, sizeof(struct arcq), 1, arcfp);
3812 MSG_syslog(LOG_EMERG, "Couldn't write Refcount Queue File %s: %s\n",
3821 void AdjRefCountList(long *msgnum, long nmsg, int incr)
3823 struct CitContext *CCC = CC;
3824 long i, the_size, offset;
3825 struct arcq *new_arcq;
3828 MSG_syslog(LOG_DEBUG, "AdjRefCountList() msg %ld ref count delta %+d\n", nmsg, incr);
3830 begin_critical_section(S_SUPPMSGMAIN);
3831 if (arcfp == NULL) {
3832 arcfp = fopen(file_arcq, "ab+");
3833 chown(file_arcq, CTDLUID, (-1));
3834 chmod(file_arcq, 0600);
3836 end_critical_section(S_SUPPMSGMAIN);
3839 * If we can't open the queue, perform the operation synchronously.
3841 if (arcfp == NULL) {
3842 for (i = 0; i < nmsg; i++)
3843 TDAP_AdjRefCount(msgnum[i], incr);
3847 the_size = sizeof(struct arcq) * nmsg;
3848 new_arcq = malloc(the_size);
3849 for (i = 0; i < nmsg; i++) {
3850 new_arcq[i].arcq_msgnum = msgnum[i];
3851 new_arcq[i].arcq_delta = incr;
3855 while ((rv >= 0) && (offset < the_size))
3857 rv = fwrite(new_arcq + offset, 1, the_size - offset, arcfp);
3859 MSG_syslog(LOG_EMERG, "Couldn't write Refcount Queue File %s: %s\n",
3875 * TDAP_ProcessAdjRefCountQueue()
3877 * Process the queue of message count adjustments that was created by calls
3878 * to AdjRefCount() ... by reading the queue and calling TDAP_AdjRefCount()
3879 * for each one. This should be an "off hours" operation.
3881 int TDAP_ProcessAdjRefCountQueue(void)
3883 struct CitContext *CCC = CC;
3884 char file_arcq_temp[PATH_MAX];
3887 struct arcq arcq_rec;
3888 int num_records_processed = 0;
3890 snprintf(file_arcq_temp, sizeof file_arcq_temp, "%s.%04x", file_arcq, rand());
3892 begin_critical_section(S_SUPPMSGMAIN);
3893 if (arcfp != NULL) {
3898 r = link(file_arcq, file_arcq_temp);
3900 MSG_syslog(LOG_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
3901 end_critical_section(S_SUPPMSGMAIN);
3902 return(num_records_processed);
3906 end_critical_section(S_SUPPMSGMAIN);
3908 fp = fopen(file_arcq_temp, "rb");
3910 MSG_syslog(LOG_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
3911 return(num_records_processed);
3914 while (fread(&arcq_rec, sizeof(struct arcq), 1, fp) == 1) {
3915 TDAP_AdjRefCount(arcq_rec.arcq_msgnum, arcq_rec.arcq_delta);
3916 ++num_records_processed;
3920 r = unlink(file_arcq_temp);
3922 MSG_syslog(LOG_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
3925 return(num_records_processed);
3931 * TDAP_AdjRefCount - adjust the reference count for a message.
3932 * This one does it "for real" because it's called by
3933 * the autopurger function that processes the queue
3934 * created by AdjRefCount(). If a message's reference
3935 * count becomes zero, we also delete the message from
3936 * disk and de-index it.
3938 void TDAP_AdjRefCount(long msgnum, int incr)
3940 struct CitContext *CCC = CC;
3942 struct MetaData smi;
3945 /* This is a *tight* critical section; please keep it that way, as
3946 * it may get called while nested in other critical sections.
3947 * Complicating this any further will surely cause deadlock!
3949 begin_critical_section(S_SUPPMSGMAIN);
3950 GetMetaData(&smi, msgnum);
3951 smi.meta_refcount += incr;
3953 end_critical_section(S_SUPPMSGMAIN);
3954 MSG_syslog(LOG_DEBUG, "TDAP_AdjRefCount() msg %ld ref count delta %+d, is now %d\n",
3955 msgnum, incr, smi.meta_refcount
3958 /* If the reference count is now zero, delete the message
3959 * (and its supplementary record as well).
3961 if (smi.meta_refcount == 0) {
3962 MSG_syslog(LOG_DEBUG, "Deleting message <%ld>\n", msgnum);
3964 /* Call delete hooks with NULL room to show it has gone altogether */
3965 PerformDeleteHooks(NULL, msgnum);
3967 /* Remove from message base */
3969 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3970 cdb_delete(CDB_BIGMSGS, &delnum, (int)sizeof(long));
3972 /* Remove metadata record */
3973 delnum = (0L - msgnum);
3974 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3980 * Write a generic object to this room
3982 * Note: this could be much more efficient. Right now we use two temporary
3983 * files, and still pull the message into memory as with all others.
3985 void CtdlWriteObject(char *req_room, /* Room to stuff it in */
3986 char *content_type, /* MIME type of this object */
3987 char *raw_message, /* Data to be written */
3988 off_t raw_length, /* Size of raw_message */
3989 struct ctdluser *is_mailbox, /* Mailbox room? */
3990 int is_binary, /* Is encoding necessary? */
3991 int is_unique, /* Del others of this type? */
3992 unsigned int flags /* Internal save flags */
3995 struct CitContext *CCC = CC;
3996 struct ctdlroom qrbuf;
3997 char roomname[ROOMNAMELEN];
3998 struct CtdlMessage *msg;
3999 char *encoded_message = NULL;
4001 if (is_mailbox != NULL) {
4002 CtdlMailboxName(roomname, sizeof roomname, is_mailbox, req_room);
4005 safestrncpy(roomname, req_room, sizeof(roomname));
4008 MSG_syslog(LOG_DEBUG, "Raw length is %ld\n", (long)raw_length);
4011 encoded_message = malloc((size_t) (((raw_length * 134) / 100) + 4096 ) );
4014 encoded_message = malloc((size_t)(raw_length + 4096));
4017 sprintf(encoded_message, "Content-type: %s\n", content_type);
4020 sprintf(&encoded_message[strlen(encoded_message)],
4021 "Content-transfer-encoding: base64\n\n"
4025 sprintf(&encoded_message[strlen(encoded_message)],
4026 "Content-transfer-encoding: 7bit\n\n"
4032 &encoded_message[strlen(encoded_message)],
4040 &encoded_message[strlen(encoded_message)],
4046 MSGM_syslog(LOG_DEBUG, "Allocating\n");
4047 msg = malloc(sizeof(struct CtdlMessage));
4048 memset(msg, 0, sizeof(struct CtdlMessage));
4049 msg->cm_magic = CTDLMESSAGE_MAGIC;
4050 msg->cm_anon_type = MES_NORMAL;
4051 msg->cm_format_type = 4;
4052 msg->cm_fields[eAuthor] = strdup(CCC->user.fullname);
4053 msg->cm_fields[eOriginalRoom] = strdup(req_room);
4054 msg->cm_fields[eNodeName] = strdup(config.c_nodename);
4055 msg->cm_fields[eHumanNode] = strdup(config.c_humannode);
4056 msg->cm_flags = flags;
4058 msg->cm_fields[eMesageText] = encoded_message;
4060 /* Create the requested room if we have to. */
4061 if (CtdlGetRoom(&qrbuf, roomname) != 0) {
4062 CtdlCreateRoom(roomname,
4063 ( (is_mailbox != NULL) ? 5 : 3 ),
4064 "", 0, 1, 0, VIEW_BBS);
4066 /* If the caller specified this object as unique, delete all
4067 * other objects of this type that are currently in the room.
4070 MSG_syslog(LOG_DEBUG, "Deleted %d other msgs of this type\n",
4071 CtdlDeleteMessages(roomname, NULL, 0, content_type)
4074 /* Now write the data */
4075 CtdlSubmitMsg(msg, NULL, roomname, 0);
4081 /*****************************************************************************/
4082 /* MODULE INITIALIZATION STUFF */
4083 /*****************************************************************************/
4084 void SetMessageDebugEnabled(const int n)
4086 MessageDebugEnabled = n;
4088 CTDL_MODULE_INIT(msgbase)
4091 CtdlRegisterDebugFlagHook(HKEY("messages"), SetMessageDebugEnabled, &MessageDebugEnabled);
4094 /* return our Subversion id for the Log */