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';
139 Msg->cm_lengths[which] = length;
142 void CM_SetFieldLONG(struct CtdlMessage *Msg, eMsgField which, long lvalue)
146 len = snprintf(buf, sizeof(buf), "%ld", lvalue);
147 CM_SetField(Msg, which, buf, len);
149 void CM_CutFieldAt(struct CtdlMessage *Msg, eMsgField WhichToCut, long maxlen)
151 if (Msg->cm_fields[WhichToCut] == NULL)
154 if (Msg->cm_lengths[WhichToCut] > maxlen)
156 Msg->cm_fields[WhichToCut][maxlen] = '\0';
157 Msg->cm_lengths[WhichToCut] = maxlen;
161 void CM_FlushField(struct CtdlMessage *Msg, eMsgField which)
163 if (Msg->cm_fields[which] != NULL)
164 free (Msg->cm_fields[which]);
165 Msg->cm_fields[which] = NULL;
166 Msg->cm_lengths[which] = 0;
168 void CM_Flush(struct CtdlMessage *Msg)
172 if (CM_IsValidMsg(Msg) == 0)
175 for (i = 0; i < 256; ++i)
177 CM_FlushField(Msg, i);
181 void CM_CopyField(struct CtdlMessage *Msg, eMsgField WhichToPutTo, eMsgField WhichtToCopy)
184 if (Msg->cm_fields[WhichToPutTo] != NULL)
185 free (Msg->cm_fields[WhichToPutTo]);
187 if (Msg->cm_fields[WhichtToCopy] != NULL)
189 len = Msg->cm_lengths[WhichtToCopy];
190 Msg->cm_fields[WhichToPutTo] = malloc(len + 1);
191 memcpy(Msg->cm_fields[WhichToPutTo], Msg->cm_fields[WhichtToCopy], len);
192 Msg->cm_fields[WhichToPutTo][len] = '\0';
193 Msg->cm_lengths[WhichToPutTo] = len;
197 Msg->cm_fields[WhichToPutTo] = NULL;
198 Msg->cm_lengths[WhichToPutTo] = 0;
203 void CM_PrependToField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
205 if (Msg->cm_fields[which] != NULL) {
210 oldmsgsize = Msg->cm_lengths[which] + 1;
211 newmsgsize = length + oldmsgsize;
213 new = malloc(newmsgsize);
214 memcpy(new, buf, length);
215 memcpy(new + length, Msg->cm_fields[which], oldmsgsize);
216 free(Msg->cm_fields[which]);
217 Msg->cm_fields[which] = new;
218 Msg->cm_lengths[which] = newmsgsize - 1;
221 Msg->cm_fields[which] = malloc(length + 1);
222 memcpy(Msg->cm_fields[which], buf, length);
223 Msg->cm_fields[which][length] = '\0';
224 Msg->cm_lengths[which] = length;
228 void CM_SetAsField(struct CtdlMessage *Msg, eMsgField which, char **buf, long length)
230 if (Msg->cm_fields[which] != NULL)
231 free (Msg->cm_fields[which]);
233 Msg->cm_fields[which] = *buf;
235 Msg->cm_lengths[which] = length;
238 void CM_SetAsFieldSB(struct CtdlMessage *Msg, eMsgField which, StrBuf **buf)
240 if (Msg->cm_fields[which] != NULL)
241 free (Msg->cm_fields[which]);
243 Msg->cm_lengths[which] = StrLength(*buf);
244 Msg->cm_fields[which] = SmashStrBuf(buf);
247 void CM_GetAsField(struct CtdlMessage *Msg, eMsgField which, char **ret, long *retlen)
249 if (Msg->cm_fields[which] != NULL)
251 *retlen = Msg->cm_lengths[which];
252 *ret = Msg->cm_fields[which];
253 Msg->cm_fields[which] = NULL;
254 Msg->cm_lengths[which] = 0;
264 * Returns 1 if the supplied pointer points to a valid Citadel message.
265 * If the pointer is NULL or the magic number check fails, returns 0.
267 int CM_IsValidMsg(struct CtdlMessage *msg) {
270 if ((msg->cm_magic) != CTDLMESSAGE_MAGIC) {
271 struct CitContext *CCC = CC;
272 MSGM_syslog(LOG_WARNING, "CM_IsValidMsg() -- self-check failed\n");
278 void CM_FreeContents(struct CtdlMessage *msg)
282 for (i = 0; i < 256; ++i)
283 if (msg->cm_fields[i] != NULL) {
284 free(msg->cm_fields[i]);
285 msg->cm_lengths[i] = 0;
288 msg->cm_magic = 0; /* just in case */
291 * 'Destructor' for struct CtdlMessage
293 void CM_Free(struct CtdlMessage *msg)
295 if (CM_IsValidMsg(msg) == 0)
297 if (msg != NULL) free (msg);
300 CM_FreeContents(msg);
304 int CM_DupField(eMsgField i, struct CtdlMessage *OrgMsg, struct CtdlMessage *NewMsg)
307 len = OrgMsg->cm_lengths[i];
308 NewMsg->cm_fields[i] = malloc(len + 1);
309 if (NewMsg->cm_fields[i] == NULL)
311 memcpy(NewMsg->cm_fields[i], OrgMsg->cm_fields[i], len);
312 NewMsg->cm_fields[i][len] = '\0';
313 NewMsg->cm_lengths[i] = len;
317 struct CtdlMessage * CM_Duplicate(struct CtdlMessage *OrgMsg)
320 struct CtdlMessage *NewMsg;
322 if (CM_IsValidMsg(OrgMsg) == 0)
324 NewMsg = (struct CtdlMessage *)malloc(sizeof(struct CtdlMessage));
328 memcpy(NewMsg, OrgMsg, sizeof(struct CtdlMessage));
330 memset(&NewMsg->cm_fields, 0, sizeof(char*) * 256);
332 for (i = 0; i < 256; ++i)
334 if (OrgMsg->cm_fields[i] != NULL)
336 if (!CM_DupField(i, OrgMsg, NewMsg))
351 /* Determine if a given message matches the fields in a message template.
352 * Return 0 for a successful match.
354 int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template) {
357 /* If there aren't any fields in the template, all messages will
360 if (template == NULL) return(0);
362 /* Null messages are bogus. */
363 if (msg == NULL) return(1);
365 for (i='A'; i<='Z'; ++i) {
366 if (template->cm_fields[i] != NULL) {
367 if (msg->cm_fields[i] == NULL) {
368 /* Considered equal if temmplate is empty string */
369 if (IsEmptyStr(template->cm_fields[i])) continue;
372 if ((template->cm_lengths[i] != msg->cm_lengths[i]) ||
373 (strcasecmp(msg->cm_fields[i], template->cm_fields[i])))
378 /* All compares succeeded: we have a match! */
385 * Retrieve the "seen" message list for the current room.
387 void CtdlGetSeen(char *buf, int which_set) {
388 struct CitContext *CCC = CC;
391 /* Learn about the user and room in question */
392 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
394 if (which_set == ctdlsetseen_seen)
395 safestrncpy(buf, vbuf.v_seen, SIZ);
396 if (which_set == ctdlsetseen_answered)
397 safestrncpy(buf, vbuf.v_answered, SIZ);
403 * Manipulate the "seen msgs" string (or other message set strings)
405 void CtdlSetSeen(long *target_msgnums, int num_target_msgnums,
406 int target_setting, int which_set,
407 struct ctdluser *which_user, struct ctdlroom *which_room) {
408 struct CitContext *CCC = CC;
409 struct cdbdata *cdbfr;
414 long hi = (-1L); /// TODO: we just write here. y?
423 char *is_set; /* actually an array of booleans */
425 /* Don't bother doing *anything* if we were passed a list of zero messages */
426 if (num_target_msgnums < 1) {
430 /* If no room was specified, we go with the current room. */
432 which_room = &CCC->room;
435 /* If no user was specified, we go with the current user. */
437 which_user = &CCC->user;
440 MSG_syslog(LOG_DEBUG, "CtdlSetSeen(%d msgs starting with %ld, %s, %d) in <%s>\n",
441 num_target_msgnums, target_msgnums[0],
442 (target_setting ? "SET" : "CLEAR"),
446 /* Learn about the user and room in question */
447 CtdlGetRelationship(&vbuf, which_user, which_room);
449 /* Load the message list */
450 cdbfr = cdb_fetch(CDB_MSGLISTS, &which_room->QRnumber, sizeof(long));
452 msglist = (long *) cdbfr->ptr;
453 cdbfr->ptr = NULL; /* CtdlSetSeen() now owns this memory */
454 num_msgs = cdbfr->len / sizeof(long);
457 return; /* No messages at all? No further action. */
460 is_set = malloc(num_msgs * sizeof(char));
461 memset(is_set, 0, (num_msgs * sizeof(char)) );
463 /* Decide which message set we're manipulating */
465 case ctdlsetseen_seen:
466 vset = NewStrBufPlain(vbuf.v_seen, -1);
468 case ctdlsetseen_answered:
469 vset = NewStrBufPlain(vbuf.v_answered, -1);
476 #if 0 /* This is a special diagnostic section. Do not allow it to run during normal operation. */
477 MSG_syslog(LOG_DEBUG, "There are %d messages in the room.\n", num_msgs);
478 for (i=0; i<num_msgs; ++i) {
479 if ((i > 0) && (msglist[i] <= msglist[i-1])) abort();
481 MSG_syslog(LOG_DEBUG, "We are twiddling %d of them.\n", num_target_msgnums);
482 for (k=0; k<num_target_msgnums; ++k) {
483 if ((k > 0) && (target_msgnums[k] <= target_msgnums[k-1])) abort();
487 MSG_syslog(LOG_DEBUG, "before update: %s\n", ChrPtr(vset));
489 /* Translate the existing sequence set into an array of booleans */
490 setstr = NewStrBuf();
494 while (StrBufExtract_NextToken(setstr, vset, &pvset, ',') >= 0) {
496 StrBufExtract_token(lostr, setstr, 0, ':');
497 if (StrBufNum_tokens(setstr, ':') >= 2) {
498 StrBufExtract_token(histr, setstr, 1, ':');
502 StrBufAppendBuf(histr, lostr, 0);
505 if (!strcmp(ChrPtr(histr), "*")) {
512 for (i = 0; i < num_msgs; ++i) {
513 if ((msglist[i] >= lo) && (msglist[i] <= hi)) {
523 /* Now translate the array of booleans back into a sequence set */
529 for (i=0; i<num_msgs; ++i) {
533 for (k=0; k<num_target_msgnums; ++k) {
534 if (msglist[i] == target_msgnums[k]) {
535 is_seen = target_setting;
539 if ((was_seen == 0) && (is_seen == 1)) {
542 else if ((was_seen == 1) && (is_seen == 0)) {
545 if (StrLength(vset) > 0) {
546 StrBufAppendBufPlain(vset, HKEY(","), 0);
549 StrBufAppendPrintf(vset, "%ld", hi);
552 StrBufAppendPrintf(vset, "%ld:%ld", lo, hi);
556 if ((is_seen) && (i == num_msgs - 1)) {
557 if (StrLength(vset) > 0) {
558 StrBufAppendBufPlain(vset, HKEY(","), 0);
560 if ((i==0) || (was_seen == 0)) {
561 StrBufAppendPrintf(vset, "%ld", msglist[i]);
564 StrBufAppendPrintf(vset, "%ld:%ld", lo, msglist[i]);
572 * We will have to stuff this string back into a 4096 byte buffer, so if it's
573 * larger than that now, truncate it by removing tokens from the beginning.
574 * The limit of 100 iterations is there to prevent an infinite loop in case
575 * something unexpected happens.
577 int number_of_truncations = 0;
578 while ( (StrLength(vset) > SIZ) && (number_of_truncations < 100) ) {
579 StrBufRemove_token(vset, 0, ',');
580 ++number_of_truncations;
584 * If we're truncating the sequence set of messages marked with the 'seen' flag,
585 * we want the earliest messages (the truncated ones) to be marked, not unmarked.
586 * Otherwise messages at the beginning will suddenly appear to be 'unseen'.
588 if ( (which_set == ctdlsetseen_seen) && (number_of_truncations > 0) ) {
590 first_tok = NewStrBuf();
591 StrBufExtract_token(first_tok, vset, 0, ',');
592 StrBufRemove_token(vset, 0, ',');
594 if (StrBufNum_tokens(first_tok, ':') > 1) {
595 StrBufRemove_token(first_tok, 0, ':');
599 new_set = NewStrBuf();
600 StrBufAppendBufPlain(new_set, HKEY("1:"), 0);
601 StrBufAppendBuf(new_set, first_tok, 0);
602 StrBufAppendBufPlain(new_set, HKEY(":"), 0);
603 StrBufAppendBuf(new_set, vset, 0);
606 FreeStrBuf(&first_tok);
610 MSG_syslog(LOG_DEBUG, " after update: %s\n", ChrPtr(vset));
612 /* Decide which message set we're manipulating */
614 case ctdlsetseen_seen:
615 safestrncpy(vbuf.v_seen, ChrPtr(vset), sizeof vbuf.v_seen);
617 case ctdlsetseen_answered:
618 safestrncpy(vbuf.v_answered, ChrPtr(vset), sizeof vbuf.v_answered);
624 CtdlSetRelationship(&vbuf, which_user, which_room);
630 * API function to perform an operation for each qualifying message in the
631 * current room. (Returns the number of messages processed.)
633 int CtdlForEachMessage(int mode, long ref, char *search_string,
635 struct CtdlMessage *compare,
636 ForEachMsgCallback CallBack,
639 struct CitContext *CCC = CC;
642 struct cdbdata *cdbfr;
643 long *msglist = NULL;
645 int num_processed = 0;
648 struct CtdlMessage *msg = NULL;
651 int printed_lastold = 0;
652 int num_search_msgs = 0;
653 long *search_msgs = NULL;
655 int need_to_free_re = 0;
658 if ((content_type) && (!IsEmptyStr(content_type))) {
659 regcomp(&re, content_type, 0);
663 /* Learn about the user and room in question */
664 if (server_shutting_down) {
665 if (need_to_free_re) regfree(&re);
668 CtdlGetUser(&CCC->user, CCC->curr_user);
670 if (server_shutting_down) {
671 if (need_to_free_re) regfree(&re);
674 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
676 if (server_shutting_down) {
677 if (need_to_free_re) regfree(&re);
681 /* Load the message list */
682 cdbfr = cdb_fetch(CDB_MSGLISTS, &CCC->room.QRnumber, sizeof(long));
684 if (need_to_free_re) regfree(&re);
685 return 0; /* No messages at all? No further action. */
688 msglist = (long *) cdbfr->ptr;
689 num_msgs = cdbfr->len / sizeof(long);
691 cdbfr->ptr = NULL; /* clear this so that cdb_free() doesn't free it */
692 cdb_free(cdbfr); /* we own this memory now */
695 * Now begin the traversal.
697 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
699 /* If the caller is looking for a specific MIME type, filter
700 * out all messages which are not of the type requested.
702 if ((content_type != NULL) && (!IsEmptyStr(content_type))) {
704 /* This call to GetMetaData() sits inside this loop
705 * so that we only do the extra database read per msg
706 * if we need to. Doing the extra read all the time
707 * really kills the server. If we ever need to use
708 * metadata for another search criterion, we need to
709 * move the read somewhere else -- but still be smart
710 * enough to only do the read if the caller has
711 * specified something that will need it.
713 if (server_shutting_down) {
714 if (need_to_free_re) regfree(&re);
718 GetMetaData(&smi, msglist[a]);
720 /* if (strcasecmp(smi.meta_content_type, content_type)) { old non-regex way */
721 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) != 0) {
727 num_msgs = sort_msglist(msglist, num_msgs);
729 /* If a template was supplied, filter out the messages which
730 * don't match. (This could induce some delays!)
733 if (compare != NULL) {
734 for (a = 0; a < num_msgs; ++a) {
735 if (server_shutting_down) {
736 if (need_to_free_re) regfree(&re);
740 msg = CtdlFetchMessage(msglist[a], 1);
742 if (CtdlMsgCmp(msg, compare)) {
751 /* If a search string was specified, get a message list from
752 * the full text index and remove messages which aren't on both
756 * Since the lists are sorted and strictly ascending, and the
757 * output list is guaranteed to be shorter than or equal to the
758 * input list, we overwrite the bottom of the input list. This
759 * eliminates the need to memmove big chunks of the list over and
762 if ( (num_msgs > 0) && (mode == MSGS_SEARCH) && (search_string) ) {
764 /* Call search module via hook mechanism.
765 * NULL means use any search function available.
766 * otherwise replace with a char * to name of search routine
768 CtdlModuleDoSearch(&num_search_msgs, &search_msgs, search_string, "fulltext");
770 if (num_search_msgs > 0) {
774 orig_num_msgs = num_msgs;
776 for (i=0; i<orig_num_msgs; ++i) {
777 for (j=0; j<num_search_msgs; ++j) {
778 if (msglist[i] == search_msgs[j]) {
779 msglist[num_msgs++] = msglist[i];
785 num_msgs = 0; /* No messages qualify */
787 if (search_msgs != NULL) free(search_msgs);
789 /* Now that we've purged messages which don't contain the search
790 * string, treat a MSGS_SEARCH just like a MSGS_ALL from this
797 * Now iterate through the message list, according to the
798 * criteria supplied by the caller.
801 for (a = 0; a < num_msgs; ++a) {
802 if (server_shutting_down) {
803 if (need_to_free_re) regfree(&re);
805 return num_processed;
807 thismsg = msglist[a];
808 if (mode == MSGS_ALL) {
812 is_seen = is_msg_in_sequence_set(
813 vbuf.v_seen, thismsg);
814 if (is_seen) lastold = thismsg;
820 || ((mode == MSGS_OLD) && (is_seen))
821 || ((mode == MSGS_NEW) && (!is_seen))
822 || ((mode == MSGS_LAST) && (a >= (num_msgs - ref)))
823 || ((mode == MSGS_FIRST) && (a < ref))
824 || ((mode == MSGS_GT) && (thismsg > ref))
825 || ((mode == MSGS_LT) && (thismsg < ref))
826 || ((mode == MSGS_EQ) && (thismsg == ref))
829 if ((mode == MSGS_NEW) && (CCC->user.flags & US_LASTOLD) && (lastold > 0L) && (printed_lastold == 0) && (!is_seen)) {
831 CallBack(lastold, userdata);
835 if (CallBack) CallBack(thismsg, userdata);
839 if (need_to_free_re) regfree(&re);
842 * We cache the most recent msglist in order to do security checks later
844 if (CCC->client_socket > 0) {
845 if (CCC->cached_msglist != NULL) {
846 free(CCC->cached_msglist);
848 CCC->cached_msglist = msglist;
849 CCC->cached_num_msgs = num_msgs;
855 return num_processed;
861 * memfmout() - Citadel text formatter and paginator.
862 * Although the original purpose of this routine was to format
863 * text to the reader's screen width, all we're really using it
864 * for here is to format text out to 80 columns before sending it
865 * to the client. The client software may reformat it again.
868 char *mptr, /* where are we going to get our text from? */
869 const char *nl /* string to terminate lines with */
871 struct CitContext *CCC = CC;
873 unsigned char ch = 0;
880 while (ch=*(mptr++), ch != 0) {
883 if (client_write(outbuf, len) == -1)
885 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
889 if (client_write(nl, nllen) == -1)
891 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
896 else if (ch == '\r') {
897 /* Ignore carriage returns. Newlines are always LF or CRLF but never CR. */
899 else if (isspace(ch)) {
900 if (column > 72) { /* Beyond 72 columns, break on the next space */
901 if (client_write(outbuf, len) == -1)
903 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
907 if (client_write(nl, nllen) == -1)
909 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
922 if (column > 1000) { /* Beyond 1000 columns, break anywhere */
923 if (client_write(outbuf, len) == -1)
925 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
929 if (client_write(nl, nllen) == -1)
931 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
939 if (client_write(outbuf, len) == -1)
941 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
944 client_write(nl, nllen);
952 * Callback function for mime parser that simply lists the part
954 void list_this_part(char *name, char *filename, char *partnum, char *disp,
955 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
956 char *cbid, void *cbuserdata)
960 ma = (struct ma_info *)cbuserdata;
961 if (ma->is_ma == 0) {
962 cprintf("part=%s|%s|%s|%s|%s|%ld|%s|%s\n",
975 * Callback function for multipart prefix
977 void list_this_pref(char *name, char *filename, char *partnum, char *disp,
978 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
979 char *cbid, void *cbuserdata)
983 ma = (struct ma_info *)cbuserdata;
984 if (!strcasecmp(cbtype, "multipart/alternative")) {
988 if (ma->is_ma == 0) {
989 cprintf("pref=%s|%s\n", partnum, cbtype);
994 * Callback function for multipart sufffix
996 void list_this_suff(char *name, char *filename, char *partnum, char *disp,
997 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
998 char *cbid, void *cbuserdata)
1002 ma = (struct ma_info *)cbuserdata;
1003 if (ma->is_ma == 0) {
1004 cprintf("suff=%s|%s\n", partnum, cbtype);
1006 if (!strcasecmp(cbtype, "multipart/alternative")) {
1013 * Callback function for mime parser that opens a section for downloading
1014 * we use serv_files function here:
1016 extern void OpenCmdResult(char *filename, const char *mime_type);
1017 void mime_download(char *name, char *filename, char *partnum, char *disp,
1018 void *content, char *cbtype, char *cbcharset, size_t length,
1019 char *encoding, char *cbid, void *cbuserdata)
1022 CitContext *CCC = MyContext();
1024 /* Silently go away if there's already a download open. */
1025 if (CCC->download_fp != NULL)
1029 (!IsEmptyStr(partnum) && (!strcasecmp(CCC->download_desired_section, partnum)))
1030 || (!IsEmptyStr(cbid) && (!strcasecmp(CCC->download_desired_section, cbid)))
1032 CCC->download_fp = tmpfile();
1033 if (CCC->download_fp == NULL) {
1034 MSG_syslog(LOG_EMERG, "mime_download(): Couldn't write: %s\n",
1036 cprintf("%d cannot open temporary file: %s\n",
1037 ERROR + INTERNAL_ERROR, strerror(errno));
1041 rv = fwrite(content, length, 1, CCC->download_fp);
1043 MSG_syslog(LOG_EMERG, "mime_download(): Couldn't write: %s\n",
1045 cprintf("%d unable to write tempfile.\n",
1047 fclose(CCC->download_fp);
1048 CCC->download_fp = NULL;
1051 fflush(CCC->download_fp);
1052 rewind(CCC->download_fp);
1054 OpenCmdResult(filename, cbtype);
1061 * Callback function for mime parser that outputs a section all at once.
1062 * We can specify the desired section by part number *or* content-id.
1064 void mime_spew_section(char *name, char *filename, char *partnum, char *disp,
1065 void *content, char *cbtype, char *cbcharset, size_t length,
1066 char *encoding, char *cbid, void *cbuserdata)
1068 int *found_it = (int *)cbuserdata;
1071 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
1072 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
1075 cprintf("%d %d|-1|%s|%s|%s\n",
1082 client_write(content, length);
1088 * Load a message from disk into memory.
1089 * This is used by CtdlOutputMsg() and other fetch functions.
1091 * NOTE: Caller is responsible for freeing the returned CtdlMessage struct
1092 * using the CtdlMessageFree() function.
1094 struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body)
1096 struct CitContext *CCC = CC;
1097 struct cdbdata *dmsgtext;
1098 struct CtdlMessage *ret = NULL;
1102 cit_uint8_t field_header;
1105 MSG_syslog(LOG_DEBUG, "CtdlFetchMessage(%ld, %d)\n", msgnum, with_body);
1106 dmsgtext = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
1107 if (dmsgtext == NULL) {
1108 MSG_syslog(LOG_ERR, "CtdlFetchMessage(%ld, %d) Failed!\n", msgnum, with_body);
1111 mptr = dmsgtext->ptr;
1112 upper_bound = mptr + dmsgtext->len;
1114 /* Parse the three bytes that begin EVERY message on disk.
1115 * The first is always 0xFF, the on-disk magic number.
1116 * The second is the anonymous/public type byte.
1117 * The third is the format type byte (vari, fixed, or MIME).
1121 MSG_syslog(LOG_ERR, "Message %ld appears to be corrupted.\n", msgnum);
1125 ret = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1126 memset(ret, 0, sizeof(struct CtdlMessage));
1128 ret->cm_magic = CTDLMESSAGE_MAGIC;
1129 ret->cm_anon_type = *mptr++; /* Anon type byte */
1130 ret->cm_format_type = *mptr++; /* Format type byte */
1133 * The rest is zero or more arbitrary fields. Load them in.
1134 * We're done when we encounter either a zero-length field or
1135 * have just processed the 'M' (message text) field.
1139 if (mptr >= upper_bound) {
1142 field_header = *mptr++;
1143 which = field_header;
1145 CM_SetField(ret, which, mptr, len);
1147 mptr += len + 1; /* advance to next field */
1149 } while ((mptr < upper_bound) && (field_header != 'M'));
1153 /* Always make sure there's something in the msg text field. If
1154 * it's NULL, the message text is most likely stored separately,
1155 * so go ahead and fetch that. Failing that, just set a dummy
1156 * body so other code doesn't barf.
1158 if ( (CM_IsEmpty(ret, eMesageText)) && (with_body) ) {
1159 dmsgtext = cdb_fetch(CDB_BIGMSGS, &msgnum, sizeof(long));
1160 if (dmsgtext != NULL) {
1161 CM_SetAsField(ret, eMesageText, &dmsgtext->ptr, dmsgtext->len);
1165 if (CM_IsEmpty(ret, eMesageText)) {
1166 CM_SetField(ret, eMesageText, HKEY("\r\n\r\n (no text)\r\n"));
1169 /* Perform "before read" hooks (aborting if any return nonzero) */
1170 if (PerformMessageHooks(ret, NULL, EVT_BEFOREREAD) > 0) {
1181 * Pre callback function for multipart/alternative
1183 * NOTE: this differs from the standard behavior for a reason. Normally when
1184 * displaying multipart/alternative you want to show the _last_ usable
1185 * format in the message. Here we show the _first_ one, because it's
1186 * usually text/plain. Since this set of functions is designed for text
1187 * output to non-MIME-aware clients, this is the desired behavior.
1190 void fixed_output_pre(char *name, char *filename, char *partnum, char *disp,
1191 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1192 char *cbid, void *cbuserdata)
1194 struct CitContext *CCC = CC;
1197 ma = (struct ma_info *)cbuserdata;
1198 MSG_syslog(LOG_DEBUG, "fixed_output_pre() type=<%s>\n", cbtype);
1199 if (!strcasecmp(cbtype, "multipart/alternative")) {
1203 if (!strcasecmp(cbtype, "message/rfc822")) {
1209 * Post callback function for multipart/alternative
1211 void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
1212 void *content, char *cbtype, char *cbcharset, size_t length,
1213 char *encoding, char *cbid, void *cbuserdata)
1215 struct CitContext *CCC = CC;
1218 ma = (struct ma_info *)cbuserdata;
1219 MSG_syslog(LOG_DEBUG, "fixed_output_post() type=<%s>\n", cbtype);
1220 if (!strcasecmp(cbtype, "multipart/alternative")) {
1224 if (!strcasecmp(cbtype, "message/rfc822")) {
1230 * Inline callback function for mime parser that wants to display text
1232 void fixed_output(char *name, char *filename, char *partnum, char *disp,
1233 void *content, char *cbtype, char *cbcharset, size_t length,
1234 char *encoding, char *cbid, void *cbuserdata)
1236 struct CitContext *CCC = CC;
1242 ma = (struct ma_info *)cbuserdata;
1244 MSG_syslog(LOG_DEBUG,
1245 "fixed_output() part %s: %s (%s) (%ld bytes)\n",
1246 partnum, filename, cbtype, (long)length);
1249 * If we're in the middle of a multipart/alternative scope and
1250 * we've already printed another section, skip this one.
1252 if ( (ma->is_ma) && (ma->did_print) ) {
1253 MSG_syslog(LOG_DEBUG, "Skipping part %s (%s)\n", partnum, cbtype);
1258 if ( (!strcasecmp(cbtype, "text/plain"))
1259 || (IsEmptyStr(cbtype)) ) {
1262 client_write(wptr, length);
1263 if (wptr[length-1] != '\n') {
1270 if (!strcasecmp(cbtype, "text/html")) {
1271 ptr = html_to_ascii(content, length, 80, 0);
1273 client_write(ptr, wlen);
1274 if ((wlen > 0) && (ptr[wlen-1] != '\n')) {
1281 if (ma->use_fo_hooks) {
1282 if (PerformFixedOutputHooks(cbtype, content, length)) {
1283 /* above function returns nonzero if it handled the part */
1288 if (strncasecmp(cbtype, "multipart/", 10)) {
1289 cprintf("Part %s: %s (%s) (%ld bytes)\r\n",
1290 partnum, filename, cbtype, (long)length);
1296 * The client is elegant and sophisticated and wants to be choosy about
1297 * MIME content types, so figure out which multipart/alternative part
1298 * we're going to send.
1300 * We use a system of weights. When we find a part that matches one of the
1301 * MIME types we've declared as preferential, we can store it in ma->chosen_part
1302 * and then set ma->chosen_pref to that MIME type's position in our preference
1303 * list. If we then hit another match, we only replace the first match if
1304 * the preference value is lower.
1306 void choose_preferred(char *name, char *filename, char *partnum, char *disp,
1307 void *content, char *cbtype, char *cbcharset, size_t length,
1308 char *encoding, char *cbid, void *cbuserdata)
1310 struct CitContext *CCC = CC;
1315 ma = (struct ma_info *)cbuserdata;
1317 // NOTE: REMOVING THIS CONDITIONAL FIXES BUG 220
1318 // http://bugzilla.citadel.org/show_bug.cgi?id=220
1319 // I don't know if there are any side effects! Please TEST TEST TEST
1320 //if (ma->is_ma > 0) {
1322 for (i=0; i<num_tokens(CCC->preferred_formats, '|'); ++i) {
1323 extract_token(buf, CCC->preferred_formats, i, '|', sizeof buf);
1324 if ( (!strcasecmp(buf, cbtype)) && (!ma->freeze) ) {
1325 if (i < ma->chosen_pref) {
1326 MSG_syslog(LOG_DEBUG, "Setting chosen part: <%s>\n", partnum);
1327 safestrncpy(ma->chosen_part, partnum, sizeof ma->chosen_part);
1328 ma->chosen_pref = i;
1335 * Now that we've chosen our preferred part, output it.
1337 void output_preferred(char *name,
1349 struct CitContext *CCC = CC;
1352 int add_newline = 0;
1355 char *decoded = NULL;
1356 size_t bytes_decoded;
1359 ma = (struct ma_info *)cbuserdata;
1361 /* This is not the MIME part you're looking for... */
1362 if (strcasecmp(partnum, ma->chosen_part)) return;
1364 /* If the content-type of this part is in our preferred formats
1365 * list, we can simply output it verbatim.
1367 for (i=0; i<num_tokens(CCC->preferred_formats, '|'); ++i) {
1368 extract_token(buf, CCC->preferred_formats, i, '|', sizeof buf);
1369 if (!strcasecmp(buf, cbtype)) {
1370 /* Yeah! Go! W00t!! */
1371 if (ma->dont_decode == 0)
1372 rc = mime_decode_now (content,
1378 break; /* Give us the chance, maybe theres another one. */
1380 if (rc == 0) text_content = (char *)content;
1382 text_content = decoded;
1383 length = bytes_decoded;
1386 if (text_content[length-1] != '\n') {
1389 cprintf("Content-type: %s", cbtype);
1390 if (!IsEmptyStr(cbcharset)) {
1391 cprintf("; charset=%s", cbcharset);
1393 cprintf("\nContent-length: %d\n",
1394 (int)(length + add_newline) );
1395 if (!IsEmptyStr(encoding)) {
1396 cprintf("Content-transfer-encoding: %s\n", encoding);
1399 cprintf("Content-transfer-encoding: 7bit\n");
1401 cprintf("X-Citadel-MSG4-Partnum: %s\n", partnum);
1403 if (client_write(text_content, length) == -1)
1405 MSGM_syslog(LOG_ERR, "output_preferred(): aborting due to write failure.\n");
1408 if (add_newline) cprintf("\n");
1409 if (decoded != NULL) free(decoded);
1414 /* No translations required or possible: output as text/plain */
1415 cprintf("Content-type: text/plain\n\n");
1417 if (ma->dont_decode == 0)
1418 rc = mime_decode_now (content,
1424 return; /* Give us the chance, maybe theres another one. */
1426 if (rc == 0) text_content = (char *)content;
1428 text_content = decoded;
1429 length = bytes_decoded;
1432 fixed_output(name, filename, partnum, disp, text_content, cbtype, cbcharset,
1433 length, encoding, cbid, cbuserdata);
1434 if (decoded != NULL) free(decoded);
1439 char desired_section[64];
1446 * Callback function for
1448 void extract_encapsulated_message(char *name, char *filename, char *partnum, char *disp,
1449 void *content, char *cbtype, char *cbcharset, size_t length,
1450 char *encoding, char *cbid, void *cbuserdata)
1452 struct encapmsg *encap;
1454 encap = (struct encapmsg *)cbuserdata;
1456 /* Only proceed if this is the desired section... */
1457 if (!strcasecmp(encap->desired_section, partnum)) {
1458 encap->msglen = length;
1459 encap->msg = malloc(length + 2);
1460 memcpy(encap->msg, content, length);
1467 * Determine whether the specified message exists in the cached_msglist
1468 * (This is a security check)
1470 int check_cached_msglist(long msgnum) {
1471 struct CitContext *CCC = CC;
1473 /* cases in which we skip the check */
1474 if (!CCC) return om_ok; /* not a session */
1475 if (CCC->client_socket <= 0) return om_ok; /* not a client session */
1476 if (CCC->cached_msglist == NULL) return om_access_denied; /* no msglist fetched */
1477 if (CCC->cached_num_msgs == 0) return om_access_denied; /* nothing to check */
1480 /* Do a binary search within the cached_msglist for the requested msgnum */
1482 int max = (CC->cached_num_msgs - 1);
1484 while (max >= min) {
1485 int middle = min + (max-min) / 2 ;
1486 if (msgnum == CCC->cached_msglist[middle]) {
1489 if (msgnum > CC->cached_msglist[middle]) {
1497 return om_access_denied;
1503 * Get a message off disk. (returns om_* values found in msgbase.h)
1506 int CtdlOutputMsg(long msg_num, /* message number (local) to fetch */
1507 int mode, /* how would you like that message? */
1508 int headers_only, /* eschew the message body? */
1509 int do_proto, /* do Citadel protocol responses? */
1510 int crlf, /* Use CRLF newlines instead of LF? */
1511 char *section, /* NULL or a message/rfc822 section */
1512 int flags, /* various flags; see msgbase.h */
1516 struct CitContext *CCC = CC;
1517 struct CtdlMessage *TheMessage = NULL;
1518 int retcode = CIT_OK;
1519 struct encapmsg encap;
1522 MSG_syslog(LOG_DEBUG, "CtdlOutputMsg(msgnum=%ld, mode=%d, section=%s)\n",
1524 (section ? section : "<>")
1527 r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
1530 if (r == om_not_logged_in) {
1531 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1534 cprintf("%d An unknown error has occurred.\n", ERROR);
1541 * Check to make sure the message is actually IN this room
1543 r = check_cached_msglist(msg_num);
1544 if (r == om_access_denied) {
1545 /* Not in the cache? We get ONE shot to check it again. */
1546 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, NULL, NULL);
1547 r = check_cached_msglist(msg_num);
1550 MSG_syslog(LOG_DEBUG, "Security check fail: message %ld is not in %s\n",
1551 msg_num, CCC->room.QRname
1554 if (r == om_access_denied) {
1555 cprintf("%d message %ld was not found in this room\n",
1556 ERROR + HIGHER_ACCESS_REQUIRED,
1565 * Fetch the message from disk. If we're in HEADERS_FAST mode,
1566 * request that we don't even bother loading the body into memory.
1568 if (headers_only == HEADERS_FAST) {
1569 TheMessage = CtdlFetchMessage(msg_num, 0);
1572 TheMessage = CtdlFetchMessage(msg_num, 1);
1575 if (TheMessage == NULL) {
1576 if (do_proto) cprintf("%d Can't locate msg %ld on disk\n",
1577 ERROR + MESSAGE_NOT_FOUND, msg_num);
1578 return(om_no_such_msg);
1581 /* Here is the weird form of this command, to process only an
1582 * encapsulated message/rfc822 section.
1584 if (section) if (!IsEmptyStr(section)) if (strcmp(section, "0")) {
1585 memset(&encap, 0, sizeof encap);
1586 safestrncpy(encap.desired_section, section, sizeof encap.desired_section);
1587 mime_parser(CM_RANGE(TheMessage, eMesageText),
1588 *extract_encapsulated_message,
1589 NULL, NULL, (void *)&encap, 0
1592 if ((Author != NULL) && (*Author == NULL))
1595 CM_GetAsField(TheMessage, eAuthor, Author, &len);
1597 if ((Address != NULL) && (*Address == NULL))
1600 CM_GetAsField(TheMessage, erFc822Addr, Address, &len);
1602 CM_Free(TheMessage);
1606 encap.msg[encap.msglen] = 0;
1607 TheMessage = convert_internet_message(encap.msg);
1608 encap.msg = NULL; /* no free() here, TheMessage owns it now */
1610 /* Now we let it fall through to the bottom of this
1611 * function, because TheMessage now contains the
1612 * encapsulated message instead of the top-level
1613 * message. Isn't that neat?
1618 cprintf("%d msg %ld has no part %s\n",
1619 ERROR + MESSAGE_NOT_FOUND,
1623 retcode = om_no_such_msg;
1628 /* Ok, output the message now */
1629 if (retcode == CIT_OK)
1630 retcode = CtdlOutputPreLoadedMsg(TheMessage, mode, headers_only, do_proto, crlf, flags);
1631 if ((Author != NULL) && (*Author == NULL))
1634 CM_GetAsField(TheMessage, eAuthor, Author, &len);
1636 if ((Address != NULL) && (*Address == NULL))
1639 CM_GetAsField(TheMessage, erFc822Addr, Address, &len);
1642 CM_Free(TheMessage);
1649 void OutputCtdlMsgHeaders(
1650 struct CtdlMessage *TheMessage,
1651 int do_proto) /* do Citadel protocol responses? */
1656 char display_name[256];
1658 /* begin header processing loop for Citadel message format */
1659 safestrncpy(display_name, "<unknown>", sizeof display_name);
1660 if (!CM_IsEmpty(TheMessage, eAuthor)) {
1661 strcpy(buf, TheMessage->cm_fields[eAuthor]);
1662 if (TheMessage->cm_anon_type == MES_ANONONLY) {
1663 safestrncpy(display_name, "****", sizeof display_name);
1665 else if (TheMessage->cm_anon_type == MES_ANONOPT) {
1666 safestrncpy(display_name, "anonymous", sizeof display_name);
1669 safestrncpy(display_name, buf, sizeof display_name);
1671 if ((is_room_aide())
1672 && ((TheMessage->cm_anon_type == MES_ANONONLY)
1673 || (TheMessage->cm_anon_type == MES_ANONOPT))) {
1674 size_t tmp = strlen(display_name);
1675 snprintf(&display_name[tmp],
1676 sizeof display_name - tmp,
1681 /* Don't show Internet address for users on the
1682 * local Citadel network.
1685 if (!CM_IsEmpty(TheMessage, eNodeName) &&
1686 (haschar(TheMessage->cm_fields[eNodeName], '.') == 0))
1691 /* Now spew the header fields in the order we like them. */
1692 for (i=0; i< NDiskFields; ++i) {
1694 Field = FieldOrder[i];
1695 if (Field != eMesageText) {
1696 if ( (!CM_IsEmpty(TheMessage, Field))
1697 && (msgkeys[Field] != NULL) ) {
1698 if ((Field == eenVelopeTo) ||
1699 (Field == eRecipient) ||
1700 (Field == eCarbonCopY)) {
1701 sanitize_truncated_recipient(TheMessage->cm_fields[Field]);
1703 if (Field == eAuthor) {
1704 if (do_proto) cprintf("%s=%s\n",
1708 else if ((Field == erFc822Addr) && (suppress_f)) {
1711 /* Masquerade display name if needed */
1713 if (do_proto) cprintf("%s=%s\n",
1715 TheMessage->cm_fields[Field]
1724 void OutputRFC822MsgHeaders(
1725 struct CtdlMessage *TheMessage,
1726 int flags, /* should the bessage be exported clean */
1728 char *mid, long sizeof_mid,
1729 char *suser, long sizeof_suser,
1730 char *luser, long sizeof_luser,
1731 char *fuser, long sizeof_fuser,
1732 char *snode, long sizeof_snode)
1734 char datestamp[100];
1735 int subject_found = 0;
1742 for (i = 0; i < 256; ++i) {
1743 if (TheMessage->cm_fields[i]) {
1744 mptr = mpptr = TheMessage->cm_fields[i];
1747 safestrncpy(luser, mptr, sizeof_luser);
1748 safestrncpy(suser, mptr, sizeof_suser);
1750 else if (i == 'Y') {
1751 if ((flags & QP_EADDR) != 0) {
1752 mptr = qp_encode_email_addrs(mptr);
1754 sanitize_truncated_recipient(mptr);
1755 cprintf("CC: %s%s", mptr, nl);
1757 else if (i == 'P') {
1758 cprintf("Return-Path: %s%s", mptr, nl);
1760 else if (i == eListID) {
1761 cprintf("List-ID: %s%s", mptr, nl);
1763 else if (i == 'V') {
1764 if ((flags & QP_EADDR) != 0)
1765 mptr = qp_encode_email_addrs(mptr);
1767 while ((*hptr != '\0') && isspace(*hptr))
1769 if (!IsEmptyStr(hptr))
1770 cprintf("Envelope-To: %s%s", hptr, nl);
1772 else if (i == 'U') {
1773 cprintf("Subject: %s%s", mptr, nl);
1777 safestrncpy(mid, mptr, sizeof_mid); /// TODO: detect @ here and copy @nodename in if not found.
1778 else if (i == erFc822Addr)
1779 safestrncpy(fuser, mptr, sizeof_fuser);
1780 /* else if (i == 'O')
1781 cprintf("X-Citadel-Room: %s%s",
1784 safestrncpy(snode, mptr, sizeof_snode);
1787 if (haschar(mptr, '@') == 0)
1789 sanitize_truncated_recipient(mptr);
1790 cprintf("To: %s@%s", mptr, config.c_fqdn);
1795 if ((flags & QP_EADDR) != 0) {
1796 mptr = qp_encode_email_addrs(mptr);
1798 sanitize_truncated_recipient(mptr);
1799 cprintf("To: %s", mptr);
1803 else if (i == 'T') {
1804 datestring(datestamp, sizeof datestamp,
1805 atol(mptr), DATESTRING_RFC822);
1806 cprintf("Date: %s%s", datestamp, nl);
1808 else if (i == 'W') {
1809 cprintf("References: ");
1810 k = num_tokens(mptr, '|');
1811 for (j=0; j<k; ++j) {
1812 extract_token(buf, mptr, j, '|', sizeof buf);
1813 cprintf("<%s>", buf);
1822 else if (i == eReplyTo) {
1824 while ((*hptr != '\0') && isspace(*hptr))
1826 if (!IsEmptyStr(hptr))
1827 cprintf("Reply-To: %s%s", mptr, nl);
1833 if (subject_found == 0) {
1834 cprintf("Subject: (no subject)%s", nl);
1839 void Dump_RFC822HeadersBody(
1840 struct CtdlMessage *TheMessage,
1841 int headers_only, /* eschew the message body? */
1842 int flags, /* should the bessage be exported clean? */
1846 cit_uint8_t prev_ch;
1848 const char *StartOfText = StrBufNOTNULL;
1851 int nllen = strlen(nl);
1854 mptr = TheMessage->cm_fields[eMesageText];
1858 while (*mptr != '\0') {
1859 if (*mptr == '\r') {
1866 eoh = (*(mptr+1) == '\r') && (*(mptr+2) == '\n');
1868 eoh = *(mptr+1) == '\n';
1872 StartOfText = strchr(StartOfText, '\n');
1873 StartOfText = strchr(StartOfText, '\n');
1876 if (((headers_only == HEADERS_NONE) && (mptr >= StartOfText)) ||
1877 ((headers_only == HEADERS_ONLY) && (mptr < StartOfText)) ||
1878 ((headers_only != HEADERS_NONE) &&
1879 (headers_only != HEADERS_ONLY))
1881 if (*mptr == '\n') {
1882 memcpy(&outbuf[outlen], nl, nllen);
1884 outbuf[outlen] = '\0';
1887 outbuf[outlen++] = *mptr;
1891 if (flags & ESC_DOT)
1893 if ((prev_ch == '\n') &&
1895 ((*(mptr+1) == '\r') || (*(mptr+1) == '\n')))
1897 outbuf[outlen++] = '.';
1902 if (outlen > 1000) {
1903 if (client_write(outbuf, outlen) == -1)
1905 struct CitContext *CCC = CC;
1906 MSGM_syslog(LOG_ERR, "Dump_RFC822HeadersBody(): aborting due to write failure.\n");
1913 client_write(outbuf, outlen);
1919 /* If the format type on disk is 1 (fixed-format), then we want
1920 * everything to be output completely literally ... regardless of
1921 * what message transfer format is in use.
1923 void DumpFormatFixed(
1924 struct CtdlMessage *TheMessage,
1925 int mode, /* how would you like that message? */
1932 int nllen = strlen (nl);
1935 mptr = TheMessage->cm_fields[eMesageText];
1937 if (mode == MT_MIME) {
1938 cprintf("Content-type: text/plain\n\n");
1942 while (ch = *mptr++, ch > 0) {
1946 if ((buflen > 250) && (!xlline)){
1950 while ((buflen > 0) &&
1951 (!isspace(buf[buflen])))
1957 mptr -= tbuflen - buflen;
1962 /* if we reach the outer bounds of our buffer,
1963 abort without respect what whe purge. */
1966 (buflen > SIZ - nllen - 2)))
1970 memcpy (&buf[buflen], nl, nllen);
1974 if (client_write(buf, buflen) == -1)
1976 struct CitContext *CCC = CC;
1977 MSGM_syslog(LOG_ERR, "DumpFormatFixed(): aborting due to write failure.\n");
1989 if (!IsEmptyStr(buf))
1990 cprintf("%s%s", buf, nl);
1994 * Get a message off disk. (returns om_* values found in msgbase.h)
1996 int CtdlOutputPreLoadedMsg(
1997 struct CtdlMessage *TheMessage,
1998 int mode, /* how would you like that message? */
1999 int headers_only, /* eschew the message body? */
2000 int do_proto, /* do Citadel protocol responses? */
2001 int crlf, /* Use CRLF newlines instead of LF? */
2002 int flags /* should the bessage be exported clean? */
2004 struct CitContext *CCC = CC;
2006 const char *nl; /* newline string */
2009 /* Buffers needed for RFC822 translation. These are all filled
2010 * using functions that are bounds-checked, and therefore we can
2011 * make them substantially smaller than SIZ.
2019 MSG_syslog(LOG_DEBUG, "CtdlOutputPreLoadedMsg(TheMessage=%s, %d, %d, %d, %d\n",
2020 ((TheMessage == NULL) ? "NULL" : "not null"),
2021 mode, headers_only, do_proto, crlf);
2023 strcpy(mid, "unknown");
2024 nl = (crlf ? "\r\n" : "\n");
2026 if (!CM_IsValidMsg(TheMessage)) {
2027 MSGM_syslog(LOG_ERR,
2028 "ERROR: invalid preloaded message for output\n");
2030 return(om_no_such_msg);
2033 /* Suppress envelope recipients if required to avoid disclosing BCC addresses.
2034 * Pad it with spaces in order to avoid changing the RFC822 length of the message.
2036 if ( (flags & SUPPRESS_ENV_TO) && (!CM_IsEmpty(TheMessage, eenVelopeTo)) ) {
2037 memset(TheMessage->cm_fields[eenVelopeTo], ' ', TheMessage->cm_lengths[eenVelopeTo]);
2040 /* Are we downloading a MIME component? */
2041 if (mode == MT_DOWNLOAD) {
2042 if (TheMessage->cm_format_type != FMT_RFC822) {
2044 cprintf("%d This is not a MIME message.\n",
2045 ERROR + ILLEGAL_VALUE);
2046 } else if (CCC->download_fp != NULL) {
2047 if (do_proto) cprintf(
2048 "%d You already have a download open.\n",
2049 ERROR + RESOURCE_BUSY);
2051 /* Parse the message text component */
2052 mime_parser(CM_RANGE(TheMessage, eMesageText),
2053 *mime_download, NULL, NULL, NULL, 0);
2054 /* If there's no file open by this time, the requested
2055 * section wasn't found, so print an error
2057 if (CCC->download_fp == NULL) {
2058 if (do_proto) cprintf(
2059 "%d Section %s not found.\n",
2060 ERROR + FILE_NOT_FOUND,
2061 CCC->download_desired_section);
2064 return((CCC->download_fp != NULL) ? om_ok : om_mime_error);
2067 /* MT_SPEW_SECTION is like MT_DOWNLOAD except it outputs the whole MIME part
2068 * in a single server operation instead of opening a download file.
2070 if (mode == MT_SPEW_SECTION) {
2071 if (TheMessage->cm_format_type != FMT_RFC822) {
2073 cprintf("%d This is not a MIME message.\n",
2074 ERROR + ILLEGAL_VALUE);
2076 /* Parse the message text component */
2079 mime_parser(CM_RANGE(TheMessage, eMesageText),
2080 *mime_spew_section, NULL, NULL, (void *)&found_it, 0);
2081 /* If section wasn't found, print an error
2084 if (do_proto) cprintf(
2085 "%d Section %s not found.\n",
2086 ERROR + FILE_NOT_FOUND,
2087 CCC->download_desired_section);
2090 return((CCC->download_fp != NULL) ? om_ok : om_mime_error);
2093 /* now for the user-mode message reading loops */
2094 if (do_proto) cprintf("%d msg:\n", LISTING_FOLLOWS);
2096 /* Does the caller want to skip the headers? */
2097 if (headers_only == HEADERS_NONE) goto START_TEXT;
2099 /* Tell the client which format type we're using. */
2100 if ( (mode == MT_CITADEL) && (do_proto) ) {
2101 cprintf("type=%d\n", TheMessage->cm_format_type);
2104 /* nhdr=yes means that we're only displaying headers, no body */
2105 if ( (TheMessage->cm_anon_type == MES_ANONONLY)
2106 && ((mode == MT_CITADEL) || (mode == MT_MIME))
2109 cprintf("nhdr=yes\n");
2112 if ((mode == MT_CITADEL) || (mode == MT_MIME))
2113 OutputCtdlMsgHeaders(TheMessage, do_proto);
2116 /* begin header processing loop for RFC822 transfer format */
2120 memcpy(snode, CFG_KEY(c_nodename) + 1);
2121 if (mode == MT_RFC822)
2122 OutputRFC822MsgHeaders(
2127 suser, sizeof(suser),
2128 luser, sizeof(luser),
2129 fuser, sizeof(fuser),
2130 snode, sizeof(snode)
2134 for (i=0; !IsEmptyStr(&suser[i]); ++i) {
2135 suser[i] = tolower(suser[i]);
2136 if (!isalnum(suser[i])) suser[i]='_';
2139 if (mode == MT_RFC822) {
2140 if (!strcasecmp(snode, NODENAME)) {
2141 safestrncpy(snode, FQDN, sizeof snode);
2144 /* Construct a fun message id */
2145 cprintf("Message-ID: <%s", mid);/// todo: this possibly breaks threadding mails.
2146 if (strchr(mid, '@')==NULL) {
2147 cprintf("@%s", snode);
2151 if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONONLY)) {
2152 cprintf("From: \"----\" <x@x.org>%s", nl);
2154 else if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONOPT)) {
2155 cprintf("From: \"anonymous\" <x@x.org>%s", nl);
2157 else if (!IsEmptyStr(fuser)) {
2158 cprintf("From: \"%s\" <%s>%s", luser, fuser, nl);
2161 cprintf("From: \"%s\" <%s@%s>%s", luser, suser, snode, nl);
2164 /* Blank line signifying RFC822 end-of-headers */
2165 if (TheMessage->cm_format_type != FMT_RFC822) {
2170 /* end header processing loop ... at this point, we're in the text */
2172 if (headers_only == HEADERS_FAST) goto DONE;
2174 /* Tell the client about the MIME parts in this message */
2175 if (TheMessage->cm_format_type == FMT_RFC822) {
2176 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2177 memset(&ma, 0, sizeof(struct ma_info));
2178 mime_parser(CM_RANGE(TheMessage, eMesageText),
2179 (do_proto ? *list_this_part : NULL),
2180 (do_proto ? *list_this_pref : NULL),
2181 (do_proto ? *list_this_suff : NULL),
2184 else if (mode == MT_RFC822) { /* unparsed RFC822 dump */
2185 Dump_RFC822HeadersBody(
2194 if (headers_only == HEADERS_ONLY) {
2198 /* signify start of msg text */
2199 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2200 if (do_proto) cprintf("text\n");
2203 if (TheMessage->cm_format_type == FMT_FIXED)
2206 mode, /* how would you like that message? */
2209 /* If the message on disk is format 0 (Citadel vari-format), we
2210 * output using the formatter at 80 columns. This is the final output
2211 * form if the transfer format is RFC822, but if the transfer format
2212 * is Citadel proprietary, it'll still work, because the indentation
2213 * for new paragraphs is correct and the client will reformat the
2214 * message to the reader's screen width.
2216 if (TheMessage->cm_format_type == FMT_CITADEL) {
2217 if (mode == MT_MIME) {
2218 cprintf("Content-type: text/x-citadel-variformat\n\n");
2220 memfmout(TheMessage->cm_fields[eMesageText], nl);
2223 /* If the message on disk is format 4 (MIME), we've gotta hand it
2224 * off to the MIME parser. The client has already been told that
2225 * this message is format 1 (fixed format), so the callback function
2226 * we use will display those parts as-is.
2228 if (TheMessage->cm_format_type == FMT_RFC822) {
2229 memset(&ma, 0, sizeof(struct ma_info));
2231 if (mode == MT_MIME) {
2232 ma.use_fo_hooks = 0;
2233 strcpy(ma.chosen_part, "1");
2234 ma.chosen_pref = 9999;
2235 ma.dont_decode = CCC->msg4_dont_decode;
2236 mime_parser(CM_RANGE(TheMessage, eMesageText),
2237 *choose_preferred, *fixed_output_pre,
2238 *fixed_output_post, (void *)&ma, 1);
2239 mime_parser(CM_RANGE(TheMessage, eMesageText),
2240 *output_preferred, NULL, NULL, (void *)&ma, 1);
2243 ma.use_fo_hooks = 1;
2244 mime_parser(CM_RANGE(TheMessage, eMesageText),
2245 *fixed_output, *fixed_output_pre,
2246 *fixed_output_post, (void *)&ma, 0);
2251 DONE: /* now we're done */
2252 if (do_proto) cprintf("000\n");
2257 * Save one or more message pointers into a specified room
2258 * (Returns 0 for success, nonzero for failure)
2259 * roomname may be NULL to use the current room
2261 * Note that the 'supplied_msg' field may be set to NULL, in which case
2262 * the message will be fetched from disk, by number, if we need to perform
2263 * replication checks. This adds an additional database read, so if the
2264 * caller already has the message in memory then it should be supplied. (Obviously
2265 * this mode of operation only works if we're saving a single message.)
2267 int CtdlSaveMsgPointersInRoom(char *roomname, long newmsgidlist[], int num_newmsgs,
2268 int do_repl_check, struct CtdlMessage *supplied_msg, int suppress_refcount_adj
2270 struct CitContext *CCC = CC;
2272 char hold_rm[ROOMNAMELEN];
2273 struct cdbdata *cdbfr;
2276 long highest_msg = 0L;
2279 struct CtdlMessage *msg = NULL;
2281 long *msgs_to_be_merged = NULL;
2282 int num_msgs_to_be_merged = 0;
2284 MSG_syslog(LOG_DEBUG,
2285 "CtdlSaveMsgPointersInRoom(room=%s, num_msgs=%d, repl=%d, suppress_rca=%d)\n",
2286 roomname, num_newmsgs, do_repl_check, suppress_refcount_adj
2289 strcpy(hold_rm, CCC->room.QRname);
2292 if (newmsgidlist == NULL) return(ERROR + INTERNAL_ERROR);
2293 if (num_newmsgs < 1) return(ERROR + INTERNAL_ERROR);
2294 if (num_newmsgs > 1) supplied_msg = NULL;
2296 /* Now the regular stuff */
2297 if (CtdlGetRoomLock(&CCC->room,
2298 ((roomname != NULL) ? roomname : CCC->room.QRname) )
2300 MSG_syslog(LOG_ERR, "No such room <%s>\n", roomname);
2301 return(ERROR + ROOM_NOT_FOUND);
2305 msgs_to_be_merged = malloc(sizeof(long) * num_newmsgs);
2306 num_msgs_to_be_merged = 0;
2309 cdbfr = cdb_fetch(CDB_MSGLISTS, &CCC->room.QRnumber, sizeof(long));
2310 if (cdbfr == NULL) {
2314 msglist = (long *) cdbfr->ptr;
2315 cdbfr->ptr = NULL; /* CtdlSaveMsgPointerInRoom() now owns this memory */
2316 num_msgs = cdbfr->len / sizeof(long);
2321 /* Create a list of msgid's which were supplied by the caller, but do
2322 * not already exist in the target room. It is absolutely taboo to
2323 * have more than one reference to the same message in a room.
2325 for (i=0; i<num_newmsgs; ++i) {
2327 if (num_msgs > 0) for (j=0; j<num_msgs; ++j) {
2328 if (msglist[j] == newmsgidlist[i]) {
2333 msgs_to_be_merged[num_msgs_to_be_merged++] = newmsgidlist[i];
2337 MSG_syslog(LOG_DEBUG, "%d unique messages to be merged\n", num_msgs_to_be_merged);
2340 * Now merge the new messages
2342 msglist = realloc(msglist, (sizeof(long) * (num_msgs + num_msgs_to_be_merged)) );
2343 if (msglist == NULL) {
2344 MSGM_syslog(LOG_ALERT, "ERROR: can't realloc message list!\n");
2345 free(msgs_to_be_merged);
2346 return (ERROR + INTERNAL_ERROR);
2348 memcpy(&msglist[num_msgs], msgs_to_be_merged, (sizeof(long) * num_msgs_to_be_merged) );
2349 num_msgs += num_msgs_to_be_merged;
2351 /* Sort the message list, so all the msgid's are in order */
2352 num_msgs = sort_msglist(msglist, num_msgs);
2354 /* Determine the highest message number */
2355 highest_msg = msglist[num_msgs - 1];
2357 /* Write it back to disk. */
2358 cdb_store(CDB_MSGLISTS, &CCC->room.QRnumber, (int)sizeof(long),
2359 msglist, (int)(num_msgs * sizeof(long)));
2361 /* Free up the memory we used. */
2364 /* Update the highest-message pointer and unlock the room. */
2365 CCC->room.QRhighest = highest_msg;
2366 CtdlPutRoomLock(&CCC->room);
2368 /* Perform replication checks if necessary */
2369 if ( (DoesThisRoomNeedEuidIndexing(&CCC->room)) && (do_repl_check) ) {
2370 MSGM_syslog(LOG_DEBUG, "CtdlSaveMsgPointerInRoom() doing repl checks\n");
2372 for (i=0; i<num_msgs_to_be_merged; ++i) {
2373 msgid = msgs_to_be_merged[i];
2375 if (supplied_msg != NULL) {
2379 msg = CtdlFetchMessage(msgid, 0);
2383 ReplicationChecks(msg);
2385 /* If the message has an Exclusive ID, index that... */
2386 if (!CM_IsEmpty(msg, eExclusiveID)) {
2387 index_message_by_euid(msg->cm_fields[eExclusiveID], &CCC->room, msgid);
2390 /* Free up the memory we may have allocated */
2391 if (msg != supplied_msg) {
2400 MSGM_syslog(LOG_DEBUG, "CtdlSaveMsgPointerInRoom() skips repl checks\n");
2403 /* Submit this room for processing by hooks */
2404 PerformRoomHooks(&CCC->room);
2406 /* Go back to the room we were in before we wandered here... */
2407 CtdlGetRoom(&CCC->room, hold_rm);
2409 /* Bump the reference count for all messages which were merged */
2410 if (!suppress_refcount_adj) {
2411 AdjRefCountList(msgs_to_be_merged, num_msgs_to_be_merged, +1);
2414 /* Free up memory... */
2415 if (msgs_to_be_merged != NULL) {
2416 free(msgs_to_be_merged);
2419 /* Return success. */
2425 * This is the same as CtdlSaveMsgPointersInRoom() but it only accepts
2428 int CtdlSaveMsgPointerInRoom(char *roomname, long msgid,
2429 int do_repl_check, struct CtdlMessage *supplied_msg)
2431 return CtdlSaveMsgPointersInRoom(roomname, &msgid, 1, do_repl_check, supplied_msg, 0);
2438 * Message base operation to save a new message to the message store
2439 * (returns new message number)
2441 * This is the back end for CtdlSubmitMsg() and should not be directly
2442 * called by server-side modules.
2445 long send_message(struct CtdlMessage *msg) {
2446 struct CitContext *CCC = CC;
2456 /* Get a new message number */
2457 newmsgid = get_new_message_number();
2458 msgidbuflen = snprintf(msgidbuf, sizeof msgidbuf, "%08lX-%08lX@%s",
2459 (long unsigned int) time(NULL),
2460 (long unsigned int) newmsgid,
2464 /* Generate an ID if we don't have one already */
2465 if (CM_IsEmpty(msg, emessageId)) {
2466 CM_SetField(msg, emessageId, msgidbuf, msgidbuflen);
2469 /* If the message is big, set its body aside for storage elsewhere */
2470 if (!CM_IsEmpty(msg, eMesageText)) {
2471 if (msg->cm_lengths[eMesageText] > BIGMSG) {
2473 holdM = msg->cm_fields[eMesageText];
2474 msg->cm_fields[eMesageText] = NULL;
2475 holdMLen = msg->cm_lengths[eMesageText];
2476 msg->cm_lengths[eMesageText] = 0;
2480 /* Serialize our data structure for storage in the database */
2481 CtdlSerializeMessage(&smr, msg);
2484 msg->cm_fields[eMesageText] = holdM;
2485 msg->cm_lengths[eMesageText] = holdMLen;
2489 cprintf("%d Unable to serialize message\n",
2490 ERROR + INTERNAL_ERROR);
2494 /* Write our little bundle of joy into the message base */
2495 if (cdb_store(CDB_MSGMAIN, &newmsgid, (int)sizeof(long),
2496 smr.ser, smr.len) < 0) {
2497 MSGM_syslog(LOG_ERR, "Can't store message\n");
2501 cdb_store(CDB_BIGMSGS,
2511 /* Free the memory we used for the serialized message */
2514 /* Return the *local* message ID to the caller
2515 * (even if we're storing an incoming network message)
2523 * Serialize a struct CtdlMessage into the format used on disk and network.
2525 * This function loads up a "struct ser_ret" (defined in server.h) which
2526 * contains the length of the serialized message and a pointer to the
2527 * serialized message in memory. THE LATTER MUST BE FREED BY THE CALLER.
2529 void CtdlSerializeMessage(struct ser_ret *ret, /* return values */
2530 struct CtdlMessage *msg) /* unserialized msg */
2532 struct CitContext *CCC = CC;
2537 * Check for valid message format
2539 if (CM_IsValidMsg(msg) == 0) {
2540 MSGM_syslog(LOG_ERR, "CtdlSerializeMessage() aborting due to invalid message\n");
2547 for (i=0; i < NDiskFields; ++i)
2548 if (msg->cm_fields[FieldOrder[i]] != NULL)
2549 ret->len += msg->cm_lengths[FieldOrder[i]] + 2;
2551 ret->ser = malloc(ret->len);
2552 if (ret->ser == NULL) {
2553 MSG_syslog(LOG_ERR, "CtdlSerializeMessage() malloc(%ld) failed: %s\n",
2554 (long)ret->len, strerror(errno));
2561 ret->ser[1] = msg->cm_anon_type;
2562 ret->ser[2] = msg->cm_format_type;
2565 for (i=0; i < NDiskFields; ++i)
2566 if (msg->cm_fields[FieldOrder[i]] != NULL)
2568 ret->ser[wlen++] = (char)FieldOrder[i];
2570 memcpy(&ret->ser[wlen],
2571 msg->cm_fields[FieldOrder[i]],
2572 msg->cm_lengths[FieldOrder[i]] + 1);
2574 wlen = wlen + msg->cm_lengths[FieldOrder[i]] + 1;
2577 if (ret->len != wlen) {
2578 MSG_syslog(LOG_ERR, "ERROR: len=%ld wlen=%ld\n",
2579 (long)ret->len, (long)wlen);
2587 * Check to see if any messages already exist in the current room which
2588 * carry the same Exclusive ID as this one. If any are found, delete them.
2590 void ReplicationChecks(struct CtdlMessage *msg) {
2591 struct CitContext *CCC = CC;
2592 long old_msgnum = (-1L);
2594 if (DoesThisRoomNeedEuidIndexing(&CCC->room) == 0) return;
2596 MSG_syslog(LOG_DEBUG, "Performing replication checks in <%s>\n",
2599 /* No exclusive id? Don't do anything. */
2600 if (msg == NULL) return;
2601 if (CM_IsEmpty(msg, eExclusiveID)) return;
2603 /*MSG_syslog(LOG_DEBUG, "Exclusive ID: <%s> for room <%s>\n",
2604 msg->cm_fields[eExclusiveID], CCC->room.QRname);*/
2606 old_msgnum = CtdlLocateMessageByEuid(msg->cm_fields[eExclusiveID], &CCC->room);
2607 if (old_msgnum > 0L) {
2608 MSG_syslog(LOG_DEBUG, "ReplicationChecks() replacing message %ld\n", old_msgnum);
2609 CtdlDeleteMessages(CCC->room.QRname, &old_msgnum, 1, "");
2616 * Save a message to disk and submit it into the delivery system.
2618 long CtdlSubmitMsg(struct CtdlMessage *msg, /* message to save */
2619 recptypes *recps, /* recipients (if mail) */
2620 const char *force, /* force a particular room? */
2621 int flags /* should the message be exported clean? */
2624 char hold_rm[ROOMNAMELEN];
2625 char actual_rm[ROOMNAMELEN];
2626 char force_room[ROOMNAMELEN];
2627 char content_type[SIZ]; /* We have to learn this */
2628 char recipient[SIZ];
2629 char bounce_to[1024];
2632 const char *mptr = NULL;
2633 struct ctdluser userbuf;
2635 struct MetaData smi;
2636 char *collected_addresses = NULL;
2637 struct addresses_to_be_filed *aptr = NULL;
2638 StrBuf *saved_rfc822_version = NULL;
2639 int qualified_for_journaling = 0;
2640 CitContext *CCC = MyContext();
2642 MSGM_syslog(LOG_DEBUG, "CtdlSubmitMsg() called\n");
2643 if (CM_IsValidMsg(msg) == 0) return(-1); /* self check */
2645 /* If this message has no timestamp, we take the liberty of
2646 * giving it one, right now.
2648 if (CM_IsEmpty(msg, eTimestamp)) {
2649 CM_SetFieldLONG(msg, eTimestamp, time(NULL));
2652 /* If this message has no path, we generate one.
2654 if (CM_IsEmpty(msg, eMessagePath)) {
2655 if (!CM_IsEmpty(msg, eAuthor)) {
2656 CM_CopyField(msg, eMessagePath, eAuthor);
2657 for (a=0; !IsEmptyStr(&msg->cm_fields[eMessagePath][a]); ++a) {
2658 if (isspace(msg->cm_fields[eMessagePath][a])) {
2659 msg->cm_fields[eMessagePath][a] = ' ';
2664 CM_SetField(msg, eMessagePath, HKEY("unknown"));
2668 if (force == NULL) {
2669 force_room[0] = '\0';
2672 strcpy(force_room, force);
2675 /* Learn about what's inside, because it's what's inside that counts */
2676 if (CM_IsEmpty(msg, eMesageText)) {
2677 MSGM_syslog(LOG_ERR, "ERROR: attempt to save message with NULL body\n");
2681 switch (msg->cm_format_type) {
2683 strcpy(content_type, "text/x-citadel-variformat");
2686 strcpy(content_type, "text/plain");
2689 strcpy(content_type, "text/plain");
2690 mptr = bmstrcasestr(msg->cm_fields[eMesageText], "Content-type:");
2693 safestrncpy(content_type, &mptr[13], sizeof content_type);
2694 striplt(content_type);
2695 aptr = content_type;
2696 while (!IsEmptyStr(aptr)) {
2708 /* Goto the correct room */
2709 room = (recps) ? CCC->room.QRname : SENTITEMS;
2710 MSG_syslog(LOG_DEBUG, "Selected room %s\n", room);
2711 strcpy(hold_rm, CCC->room.QRname);
2712 strcpy(actual_rm, CCC->room.QRname);
2713 if (recps != NULL) {
2714 strcpy(actual_rm, SENTITEMS);
2717 /* If the user is a twit, move to the twit room for posting */
2719 if (CCC->user.axlevel == AxProbU) {
2720 strcpy(hold_rm, actual_rm);
2721 strcpy(actual_rm, config.c_twitroom);
2722 MSGM_syslog(LOG_DEBUG, "Diverting to twit room\n");
2726 /* ...or if this message is destined for Aide> then go there. */
2727 if (!IsEmptyStr(force_room)) {
2728 strcpy(actual_rm, force_room);
2731 MSG_syslog(LOG_INFO, "Final selection: %s (%s)\n", actual_rm, room);
2732 if (strcasecmp(actual_rm, CCC->room.QRname)) {
2733 /* CtdlGetRoom(&CCC->room, actual_rm); */
2734 CtdlUserGoto(actual_rm, 0, 1, NULL, NULL);
2738 * If this message has no O (room) field, generate one.
2740 if (CM_IsEmpty(msg, eOriginalRoom)) {
2741 CM_SetField(msg, eOriginalRoom, CCC->room.QRname, strlen(CCC->room.QRname));
2744 /* Perform "before save" hooks (aborting if any return nonzero) */
2745 MSGM_syslog(LOG_DEBUG, "Performing before-save hooks\n");
2746 if (PerformMessageHooks(msg, recps, EVT_BEFORESAVE) > 0) return(-3);
2749 * If this message has an Exclusive ID, and the room is replication
2750 * checking enabled, then do replication checks.
2752 if (DoesThisRoomNeedEuidIndexing(&CCC->room)) {
2753 ReplicationChecks(msg);
2756 /* Save it to disk */
2757 MSGM_syslog(LOG_DEBUG, "Saving to disk\n");
2758 newmsgid = send_message(msg);
2759 if (newmsgid <= 0L) return(-5);
2761 /* Write a supplemental message info record. This doesn't have to
2762 * be a critical section because nobody else knows about this message
2765 MSGM_syslog(LOG_DEBUG, "Creating MetaData record\n");
2766 memset(&smi, 0, sizeof(struct MetaData));
2767 smi.meta_msgnum = newmsgid;
2768 smi.meta_refcount = 0;
2769 safestrncpy(smi.meta_content_type, content_type,
2770 sizeof smi.meta_content_type);
2773 * Measure how big this message will be when rendered as RFC822.
2774 * We do this for two reasons:
2775 * 1. We need the RFC822 length for the new metadata record, so the
2776 * POP and IMAP services don't have to calculate message lengths
2777 * while the user is waiting (multiplied by potentially hundreds
2778 * or thousands of messages).
2779 * 2. If journaling is enabled, we will need an RFC822 version of the
2780 * message to attach to the journalized copy.
2782 if (CCC->redirect_buffer != NULL) {
2783 MSGM_syslog(LOG_ALERT, "CCC->redirect_buffer is not NULL during message submission!\n");
2786 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
2787 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, QP_EADDR);
2788 smi.meta_rfc822_length = StrLength(CCC->redirect_buffer);
2789 saved_rfc822_version = CCC->redirect_buffer;
2790 CCC->redirect_buffer = NULL;
2794 /* Now figure out where to store the pointers */
2795 MSGM_syslog(LOG_DEBUG, "Storing pointers\n");
2797 /* If this is being done by the networker delivering a private
2798 * message, we want to BYPASS saving the sender's copy (because there
2799 * is no local sender; it would otherwise go to the Trashcan).
2801 if ((!CCC->internal_pgm) || (recps == NULL)) {
2802 if (CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 1, msg) != 0) {
2803 MSGM_syslog(LOG_ERR, "ERROR saving message pointer!\n");
2804 CtdlSaveMsgPointerInRoom(config.c_aideroom, newmsgid, 0, msg);
2808 /* For internet mail, drop a copy in the outbound queue room */
2809 if ((recps != NULL) && (recps->num_internet > 0)) {
2810 CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, newmsgid, 0, msg);
2813 /* If other rooms are specified, drop them there too. */
2814 if ((recps != NULL) && (recps->num_room > 0))
2815 for (i=0; i<num_tokens(recps->recp_room, '|'); ++i) {
2816 extract_token(recipient, recps->recp_room, i,
2817 '|', sizeof recipient);
2818 MSG_syslog(LOG_DEBUG, "Delivering to room <%s>\n", recipient);///// xxxx
2819 CtdlSaveMsgPointerInRoom(recipient, newmsgid, 0, msg);
2822 /* Bump this user's messages posted counter. */
2823 MSGM_syslog(LOG_DEBUG, "Updating user\n");
2824 CtdlGetUserLock(&CCC->user, CCC->curr_user);
2825 CCC->user.posted = CCC->user.posted + 1;
2826 CtdlPutUserLock(&CCC->user);
2828 /* Decide where bounces need to be delivered */
2829 if ((recps != NULL) && (recps->bounce_to == NULL))
2832 snprintf(bounce_to, sizeof bounce_to, "%s@%s",
2833 CCC->user.fullname, config.c_nodename);
2835 snprintf(bounce_to, sizeof bounce_to, "%s@%s",
2836 msg->cm_fields[eAuthor], msg->cm_fields[eNodeName]);
2837 recps->bounce_to = bounce_to;
2840 CM_SetFieldLONG(msg, eVltMsgNum, newmsgid);
2843 /* If this is private, local mail, make a copy in the
2844 * recipient's mailbox and bump the reference count.
2846 if ((recps != NULL) && (recps->num_local > 0))
2851 pch = recps->recp_local;
2852 recps->recp_local = recipient;
2853 ntokens = num_tokens(pch, '|');
2854 for (i=0; i<ntokens; ++i)
2856 extract_token(recipient, pch, i, '|', sizeof recipient);
2857 MSG_syslog(LOG_DEBUG, "Delivering private local mail to <%s>\n", recipient);
2858 if (CtdlGetUser(&userbuf, recipient) == 0) {
2859 CtdlMailboxName(actual_rm, sizeof actual_rm, &userbuf, MAILROOM);
2860 CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0, msg);
2861 CtdlBumpNewMailCounter(userbuf.usernum);
2862 PerformMessageHooks(msg, recps, EVT_AFTERUSRMBOXSAVE);
2865 MSG_syslog(LOG_DEBUG, "No user <%s>\n", recipient);
2866 CtdlSaveMsgPointerInRoom(config.c_aideroom, newmsgid, 0, msg);
2869 recps->recp_local = pch;
2872 /* Perform "after save" hooks */
2873 MSGM_syslog(LOG_DEBUG, "Performing after-save hooks\n");
2875 PerformMessageHooks(msg, recps, EVT_AFTERSAVE);
2876 CM_FlushField(msg, eVltMsgNum);
2878 /* Go back to the room we started from */
2879 MSG_syslog(LOG_DEBUG, "Returning to original room %s\n", hold_rm);
2880 if (strcasecmp(hold_rm, CCC->room.QRname))
2881 CtdlUserGoto(hold_rm, 0, 1, NULL, NULL);
2884 * Any addresses to harvest for someone's address book?
2886 if ( (CCC->logged_in) && (recps != NULL) ) {
2887 collected_addresses = harvest_collected_addresses(msg);
2890 if (collected_addresses != NULL) {
2891 aptr = (struct addresses_to_be_filed *)
2892 malloc(sizeof(struct addresses_to_be_filed));
2893 CtdlMailboxName(actual_rm, sizeof actual_rm,
2894 &CCC->user, USERCONTACTSROOM);
2895 aptr->roomname = strdup(actual_rm);
2896 aptr->collected_addresses = collected_addresses;
2897 begin_critical_section(S_ATBF);
2900 end_critical_section(S_ATBF);
2904 * Determine whether this message qualifies for journaling.
2906 if (!CM_IsEmpty(msg, eJournal)) {
2907 qualified_for_journaling = 0;
2910 if (recps == NULL) {
2911 qualified_for_journaling = config.c_journal_pubmsgs;
2913 else if (recps->num_local + recps->num_ignet + recps->num_internet > 0) {
2914 qualified_for_journaling = config.c_journal_email;
2917 qualified_for_journaling = config.c_journal_pubmsgs;
2922 * Do we have to perform journaling? If so, hand off the saved
2923 * RFC822 version will be handed off to the journaler for background
2924 * submit. Otherwise, we have to free the memory ourselves.
2926 if (saved_rfc822_version != NULL) {
2927 if (qualified_for_journaling) {
2928 JournalBackgroundSubmit(msg, saved_rfc822_version, recps);
2931 FreeStrBuf(&saved_rfc822_version);
2935 if ((recps != NULL) && (recps->bounce_to == bounce_to))
2936 recps->bounce_to = NULL;
2944 * Convenience function for generating small administrative messages.
2946 void quickie_message(const char *from,
2947 const char *fromaddr,
2952 const char *subject)
2954 struct CtdlMessage *msg;
2955 recptypes *recp = NULL;
2957 msg = malloc(sizeof(struct CtdlMessage));
2958 memset(msg, 0, sizeof(struct CtdlMessage));
2959 msg->cm_magic = CTDLMESSAGE_MAGIC;
2960 msg->cm_anon_type = MES_NORMAL;
2961 msg->cm_format_type = format_type;
2964 CM_SetField(msg, eAuthor, from, strlen(from));
2966 else if (fromaddr != NULL) {
2968 CM_SetField(msg, eAuthor, fromaddr, strlen(fromaddr));
2969 pAt = strchr(msg->cm_fields[eAuthor], '@');
2971 CM_CutFieldAt(msg, eAuthor, pAt - msg->cm_fields[eAuthor]);
2975 msg->cm_fields[eAuthor] = strdup("Citadel");
2978 if (fromaddr != NULL) CM_SetField(msg, erFc822Addr, fromaddr, strlen(fromaddr));
2979 if (room != NULL) CM_SetField(msg, eOriginalRoom, room, strlen(room));
2980 CM_SetField(msg, eNodeName, CFG_KEY(c_nodename));
2982 CM_SetField(msg, eRecipient, to, strlen(to));
2983 recp = validate_recipients(to, NULL, 0);
2985 if (subject != NULL) {
2986 CM_SetField(msg, eMsgSubject, subject, strlen(subject));
2988 CM_SetField(msg, eMesageText, text, strlen(text));
2990 CtdlSubmitMsg(msg, recp, room, 0);
2992 if (recp != NULL) free_recipients(recp);
2995 void flood_protect_quickie_message(const char *from,
2996 const char *fromaddr,
3001 const char *subject,
3003 const char **CritStr,
3010 u_char rawdigest[MD5_DIGEST_LEN];
3011 struct MD5Context md5context;
3015 time_t tsday = NOW / (8*60*60); /* just care for a day... */
3017 tslen = snprintf(timestamp, sizeof(timestamp), "%ld", tsday);
3018 MD5Init(&md5context);
3020 for (i = 0; i < nCriterions; i++)
3021 MD5Update(&md5context,
3022 (const unsigned char*)CritStr[i], CritStrLen[i]);
3023 MD5Update(&md5context,
3024 (const unsigned char*)timestamp, tslen);
3025 MD5Final(rawdigest, &md5context);
3027 guid = NewStrBufPlain(NULL,
3028 MD5_DIGEST_LEN * 2 + 12);
3029 StrBufHexEscAppend(guid, NULL, rawdigest, MD5_DIGEST_LEN);
3030 StrBufAppendBufPlain(guid, HKEY("_fldpt"), 0);
3031 if (StrLength(guid) > 40)
3032 StrBufCutAt(guid, 40, NULL);
3034 if (CheckIfAlreadySeen("FPAideMessage",
3043 /* yes, we did. flood protection kicks in. */
3045 "not sending message again\n");
3049 /* no, this message isn't sent recently; go ahead. */
3050 quickie_message(from,
3061 * Back end function used by CtdlMakeMessage() and similar functions
3063 StrBuf *CtdlReadMessageBodyBuf(char *terminator, /* token signalling EOT */
3065 size_t maxlen, /* maximum message length */
3066 StrBuf *exist, /* if non-null, append to it;
3067 exist is ALWAYS freed */
3068 int crlf, /* CRLF newlines instead of LF */
3069 int *sock /* socket handle or 0 for this session's client socket */
3078 LineBuf = NewStrBufPlain(NULL, SIZ);
3079 if (exist == NULL) {
3080 Message = NewStrBufPlain(NULL, 4 * SIZ);
3083 Message = NewStrBufDup(exist);
3086 /* Do we need to change leading ".." to "." for SMTP escaping? */
3087 if ((tlen == 1) && (*terminator == '.')) {
3091 /* read in the lines of message text one by one */
3094 if ((CtdlSockGetLine(sock, LineBuf, 5) < 0) ||
3099 if (CtdlClientGetLine(LineBuf) < 0) finished = 1;
3101 if ((StrLength(LineBuf) == tlen) &&
3102 (!strcmp(ChrPtr(LineBuf), terminator)))
3105 if ( (!flushing) && (!finished) ) {
3107 StrBufAppendBufPlain(LineBuf, HKEY("\r\n"), 0);
3110 StrBufAppendBufPlain(LineBuf, HKEY("\n"), 0);
3113 /* Unescape SMTP-style input of two dots at the beginning of the line */
3115 (StrLength(LineBuf) == 2) &&
3116 (!strcmp(ChrPtr(LineBuf), "..")))
3118 StrBufCutLeft(LineBuf, 1);
3121 StrBufAppendBuf(Message, LineBuf, 0);
3124 /* if we've hit the max msg length, flush the rest */
3125 if (StrLength(Message) >= maxlen) flushing = 1;
3127 } while (!finished);
3128 FreeStrBuf(&LineBuf);
3132 void DeleteAsyncMsg(ReadAsyncMsg **Msg)
3136 FreeStrBuf(&(*Msg)->MsgBuf);
3142 ReadAsyncMsg *NewAsyncMsg(const char *terminator, /* token signalling EOT */
3144 size_t maxlen, /* maximum message length */
3145 size_t expectlen, /* if we expect a message, how long should it be? */
3146 StrBuf *exist, /* if non-null, append to it;
3147 exist is ALWAYS freed */
3148 long eLen, /* length of exist */
3149 int crlf /* CRLF newlines instead of LF */
3152 ReadAsyncMsg *NewMsg;
3154 NewMsg = (ReadAsyncMsg *)malloc(sizeof(ReadAsyncMsg));
3155 memset(NewMsg, 0, sizeof(ReadAsyncMsg));
3157 if (exist == NULL) {
3160 if (expectlen == 0) {
3164 len = expectlen + 10;
3166 NewMsg->MsgBuf = NewStrBufPlain(NULL, len);
3169 NewMsg->MsgBuf = NewStrBufDup(exist);
3171 /* Do we need to change leading ".." to "." for SMTP escaping? */
3172 if ((tlen == 1) && (*terminator == '.')) {
3176 NewMsg->terminator = terminator;
3177 NewMsg->tlen = tlen;
3179 NewMsg->maxlen = maxlen;
3181 NewMsg->crlf = crlf;
3187 * Back end function used by CtdlMakeMessage() and similar functions
3189 eReadState CtdlReadMessageBodyAsync(AsyncIO *IO)
3191 ReadAsyncMsg *ReadMsg;
3192 int MsgFinished = 0;
3193 eReadState Finished = eMustReadMore;
3198 const char *pch = ChrPtr(IO->SendBuf.Buf);
3199 const char *pchh = IO->SendBuf.ReadWritePointer;
3205 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
3206 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
3207 ((CitContext*)(IO->CitContext))->ServiceName,
3210 fd = fopen(fn, "a+");
3213 ReadMsg = IO->ReadMsg;
3215 /* read in the lines of message text one by one */
3217 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
3220 case eMustReadMore: /// read new from socket...
3222 if (IO->RecvBuf.ReadWritePointer != NULL) {
3223 nbytes = StrLength(IO->RecvBuf.Buf) - (IO->RecvBuf.ReadWritePointer - ChrPtr(IO->RecvBuf.Buf));
3224 fprintf(fd, "Read; Line unfinished: %ld Bytes still in buffer [", nbytes);
3226 fwrite(IO->RecvBuf.ReadWritePointer, nbytes, 1, fd);
3230 fprintf(fd, "BufferEmpty! \n");
3236 case eBufferNotEmpty: /* shouldn't happen... */
3237 case eReadSuccess: /// done for now...
3239 case eReadFail: /// WHUT?
3245 if ((StrLength(IO->IOBuf) == ReadMsg->tlen) &&
3246 (!strcmp(ChrPtr(IO->IOBuf), ReadMsg->terminator))) {
3249 fprintf(fd, "found Terminator; Message Size: %d\n", StrLength(ReadMsg->MsgBuf));
3252 else if (!ReadMsg->flushing) {
3255 fprintf(fd, "Read Line: [%d][%s]\n", StrLength(IO->IOBuf), ChrPtr(IO->IOBuf));
3258 /* Unescape SMTP-style input of two dots at the beginning of the line */
3259 if ((ReadMsg->dodot) &&
3260 (StrLength(IO->IOBuf) == 2) && /* TODO: do we just unescape lines with two dots or any line? */
3261 (!strcmp(ChrPtr(IO->IOBuf), "..")))
3264 fprintf(fd, "UnEscaped!\n");
3266 StrBufCutLeft(IO->IOBuf, 1);
3269 if (ReadMsg->crlf) {
3270 StrBufAppendBufPlain(IO->IOBuf, HKEY("\r\n"), 0);
3273 StrBufAppendBufPlain(IO->IOBuf, HKEY("\n"), 0);
3276 StrBufAppendBuf(ReadMsg->MsgBuf, IO->IOBuf, 0);
3279 /* if we've hit the max msg length, flush the rest */
3280 if (StrLength(ReadMsg->MsgBuf) >= ReadMsg->maxlen) ReadMsg->flushing = 1;
3282 } while (!MsgFinished);
3285 fprintf(fd, "Done with reading; %s.\n, ",
3286 (MsgFinished)?"Message Finished": "FAILED");
3290 return eReadSuccess;
3297 * Back end function used by CtdlMakeMessage() and similar functions
3299 char *CtdlReadMessageBody(char *terminator, /* token signalling EOT */
3301 size_t maxlen, /* maximum message length */
3302 StrBuf *exist, /* if non-null, append to it;
3303 exist is ALWAYS freed */
3304 int crlf, /* CRLF newlines instead of LF */
3305 int *sock /* socket handle or 0 for this session's client socket */
3310 Message = CtdlReadMessageBodyBuf(terminator,
3316 if (Message == NULL)
3319 return SmashStrBuf(&Message);
3322 struct CtdlMessage *CtdlMakeMessage(
3323 struct ctdluser *author, /* author's user structure */
3324 char *recipient, /* NULL if it's not mail */
3325 char *recp_cc, /* NULL if it's not mail */
3326 char *room, /* room where it's going */
3327 int type, /* see MES_ types in header file */
3328 int format_type, /* variformat, plain text, MIME... */
3329 char *fake_name, /* who we're masquerading as */
3330 char *my_email, /* which of my email addresses to use (empty is ok) */
3331 char *subject, /* Subject (optional) */
3332 char *supplied_euid, /* ...or NULL if this is irrelevant */
3333 char *preformatted_text, /* ...or NULL to read text from client */
3334 char *references /* Thread references */
3337 return CtdlMakeMessageLen(
3338 author, /* author's user structure */
3339 recipient, /* NULL if it's not mail */
3340 (recipient)?strlen(recipient) : 0,
3341 recp_cc, /* NULL if it's not mail */
3342 (recp_cc)?strlen(recp_cc): 0,
3343 room, /* room where it's going */
3344 (room)?strlen(room): 0,
3345 type, /* see MES_ types in header file */
3346 format_type, /* variformat, plain text, MIME... */
3347 fake_name, /* who we're masquerading as */
3348 (fake_name)?strlen(fake_name): 0,
3349 my_email, /* which of my email addresses to use (empty is ok) */
3350 (my_email)?strlen(my_email): 0,
3351 subject, /* Subject (optional) */
3352 (subject)?strlen(subject): 0,
3353 supplied_euid, /* ...or NULL if this is irrelevant */
3354 (supplied_euid)?strlen(supplied_euid):0,
3355 preformatted_text, /* ...or NULL to read text from client */
3356 (preformatted_text)?strlen(preformatted_text) : 0,
3357 references, /* Thread references */
3358 (references)?strlen(references):0);
3363 * Build a binary message to be saved on disk.
3364 * (NOTE: if you supply 'preformatted_text', the buffer you give it
3365 * will become part of the message. This means you are no longer
3366 * responsible for managing that memory -- it will be freed along with
3367 * the rest of the fields when CM_Free() is called.)
3370 struct CtdlMessage *CtdlMakeMessageLen(
3371 struct ctdluser *author, /* author's user structure */
3372 char *recipient, /* NULL if it's not mail */
3374 char *recp_cc, /* NULL if it's not mail */
3376 char *room, /* room where it's going */
3378 int type, /* see MES_ types in header file */
3379 int format_type, /* variformat, plain text, MIME... */
3380 char *fake_name, /* who we're masquerading as */
3382 char *my_email, /* which of my email addresses to use (empty is ok) */
3384 char *subject, /* Subject (optional) */
3386 char *supplied_euid, /* ...or NULL if this is irrelevant */
3388 char *preformatted_text, /* ...or NULL to read text from client */
3390 char *references, /* Thread references */
3394 struct CitContext *CCC = CC;
3395 /* Don't confuse the poor folks if it's not routed mail. * /
3396 char dest_node[256] = "";*/
3399 struct CtdlMessage *msg;
3401 StrBuf *FakeEncAuthor = NULL;
3403 msg = malloc(sizeof(struct CtdlMessage));
3404 memset(msg, 0, sizeof(struct CtdlMessage));
3405 msg->cm_magic = CTDLMESSAGE_MAGIC;
3406 msg->cm_anon_type = type;
3407 msg->cm_format_type = format_type;
3409 if (recipient != NULL) rcplen = striplt(recipient);
3410 if (recp_cc != NULL) cclen = striplt(recp_cc);
3412 /* Path or Return-Path */
3414 CM_SetField(msg, eMessagePath, my_email, myelen);
3417 CM_SetField(msg, eMessagePath, author->fullname, strlen(author->fullname));
3419 convert_spaces_to_underscores(msg->cm_fields[eMessagePath]);
3421 blen = snprintf(buf, sizeof buf, "%ld", (long)time(NULL));
3422 CM_SetField(msg, eTimestamp, buf, blen);
3425 FakeAuthor = NewStrBufPlain (fake_name, fnlen);
3428 FakeAuthor = NewStrBufPlain (author->fullname, -1);
3430 StrBufRFC2047encode(&FakeEncAuthor, FakeAuthor);
3431 CM_SetAsFieldSB(msg, eAuthor, &FakeEncAuthor);
3433 if (CCC->room.QRflags & QR_MAILBOX) { /* room */
3434 CM_SetField(msg, eOriginalRoom, &CCC->room.QRname[11], strlen(&CCC->room.QRname[11]));
3437 CM_SetField(msg, eOriginalRoom, CCC->room.QRname, strlen(CCC->room.QRname));
3440 CM_SetField(msg, eNodeName, CFG_KEY(c_nodename));
3441 CM_SetField(msg, eHumanNode, CFG_KEY(c_humannode));
3444 CM_SetField(msg, eRecipient, recipient, rcplen);
3447 CM_SetField(msg, eCarbonCopY, recp_cc, cclen);
3451 CM_SetField(msg, erFc822Addr, my_email, myelen);
3453 else if ( (author == &CCC->user) && (!IsEmptyStr(CCC->cs_inet_email)) ) {
3454 CM_SetField(msg, erFc822Addr, CCC->cs_inet_email, strlen(CCC->cs_inet_email));
3457 if (subject != NULL) {
3459 length = striplt(subject);
3465 while ((subject[i] != '\0') &&
3466 (IsAscii = isascii(subject[i]) != 0 ))
3469 CM_SetField(msg, eMsgSubject, subject, subjlen);
3470 else /* ok, we've got utf8 in the string. */
3473 rfc2047Subj = rfc2047encode(subject, length);
3474 CM_SetAsField(msg, eMsgSubject, &rfc2047Subj, strlen(rfc2047Subj));
3481 CM_SetField(msg, eExclusiveID, supplied_euid, euidlen);
3485 CM_SetField(msg, eWeferences, references, reflen);
3488 if (preformatted_text != NULL) {
3489 CM_SetField(msg, eMesageText, preformatted_text, textlen);
3492 preformatted_text = CtdlReadMessageBody(HKEY("000"), config.c_maxmsglen, NULL, 0, 0);
3493 if (preformatted_text != NULL) {
3494 CM_SetField(msg, eMesageText, preformatted_text, strlen(preformatted_text));
3505 * API function to delete messages which match a set of criteria
3506 * (returns the actual number of messages deleted)
3508 int CtdlDeleteMessages(char *room_name, /* which room */
3509 long *dmsgnums, /* array of msg numbers to be deleted */
3510 int num_dmsgnums, /* number of msgs to be deleted, or 0 for "any" */
3511 char *content_type /* or "" for any. regular expressions expected. */
3514 struct CitContext *CCC = CC;
3515 struct ctdlroom qrbuf;
3516 struct cdbdata *cdbfr;
3517 long *msglist = NULL;
3518 long *dellist = NULL;
3521 int num_deleted = 0;
3523 struct MetaData smi;
3526 int need_to_free_re = 0;
3528 if (content_type) if (!IsEmptyStr(content_type)) {
3529 regcomp(&re, content_type, 0);
3530 need_to_free_re = 1;
3532 MSG_syslog(LOG_DEBUG, " CtdlDeleteMessages(%s, %d msgs, %s)\n",
3533 room_name, num_dmsgnums, content_type);
3535 /* get room record, obtaining a lock... */
3536 if (CtdlGetRoomLock(&qrbuf, room_name) != 0) {
3537 MSG_syslog(LOG_ERR, " CtdlDeleteMessages(): Room <%s> not found\n",
3539 if (need_to_free_re) regfree(&re);
3540 return (0); /* room not found */
3542 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
3544 if (cdbfr != NULL) {
3545 dellist = malloc(cdbfr->len);
3546 msglist = (long *) cdbfr->ptr;
3547 cdbfr->ptr = NULL; /* CtdlDeleteMessages() now owns this memory */
3548 num_msgs = cdbfr->len / sizeof(long);
3552 int have_contenttype = (content_type != NULL) && !IsEmptyStr(content_type);
3553 int have_delmsgs = (num_dmsgnums == 0) || (dmsgnums == NULL);
3554 int have_more_del = 1;
3556 num_msgs = sort_msglist(msglist, num_msgs);
3557 if (num_dmsgnums > 1)
3558 num_dmsgnums = sort_msglist(dmsgnums, num_dmsgnums);
3561 StrBuf *dbg = NewStrBuf();
3562 for (i = 0; i < num_dmsgnums; i++)
3563 StrBufAppendPrintf(dbg, ", %ld", dmsgnums[i]);
3564 MSG_syslog(LOG_DEBUG, " Deleting before: %s", ChrPtr(dbg));
3569 while ((i < num_msgs) && (have_more_del)) {
3572 /* Set/clear a bit for each criterion */
3574 /* 0 messages in the list or a null list means that we are
3575 * interested in deleting any messages which meet the other criteria.
3578 delete_this |= 0x01;
3581 while ((i < num_msgs) && (msglist[i] < dmsgnums[j])) i++;
3586 if (msglist[i] == dmsgnums[j]) {
3587 delete_this |= 0x01;
3590 have_more_del = (j < num_dmsgnums);
3593 if (have_contenttype) {
3594 GetMetaData(&smi, msglist[i]);
3595 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) == 0) {
3596 delete_this |= 0x02;
3599 delete_this |= 0x02;
3602 /* Delete message only if all bits are set */
3603 if (delete_this == 0x03) {
3604 dellist[num_deleted++] = msglist[i];
3611 StrBuf *dbg = NewStrBuf();
3612 for (i = 0; i < num_deleted; i++)
3613 StrBufAppendPrintf(dbg, ", %ld", dellist[i]);
3614 MSG_syslog(LOG_DEBUG, " Deleting: %s", ChrPtr(dbg));
3618 num_msgs = sort_msglist(msglist, num_msgs);
3619 cdb_store(CDB_MSGLISTS, &qrbuf.QRnumber, (int)sizeof(long),
3620 msglist, (int)(num_msgs * sizeof(long)));
3623 qrbuf.QRhighest = msglist[num_msgs - 1];
3625 qrbuf.QRhighest = 0;
3627 CtdlPutRoomLock(&qrbuf);
3629 /* Go through the messages we pulled out of the index, and decrement
3630 * their reference counts by 1. If this is the only room the message
3631 * was in, the reference count will reach zero and the message will
3632 * automatically be deleted from the database. We do this in a
3633 * separate pass because there might be plug-in hooks getting called,
3634 * and we don't want that happening during an S_ROOMS critical
3638 for (i=0; i<num_deleted; ++i) {
3639 PerformDeleteHooks(qrbuf.QRname, dellist[i]);
3641 AdjRefCountList(dellist, num_deleted, -1);
3643 /* Now free the memory we used, and go away. */
3644 if (msglist != NULL) free(msglist);
3645 if (dellist != NULL) free(dellist);
3646 MSG_syslog(LOG_DEBUG, " %d message(s) deleted.\n", num_deleted);
3647 if (need_to_free_re) regfree(&re);
3648 return (num_deleted);
3655 * GetMetaData() - Get the supplementary record for a message
3657 void GetMetaData(struct MetaData *smibuf, long msgnum)
3660 struct cdbdata *cdbsmi;
3663 memset(smibuf, 0, sizeof(struct MetaData));
3664 smibuf->meta_msgnum = msgnum;
3665 smibuf->meta_refcount = 1; /* Default reference count is 1 */
3667 /* Use the negative of the message number for its supp record index */
3668 TheIndex = (0L - msgnum);
3670 cdbsmi = cdb_fetch(CDB_MSGMAIN, &TheIndex, sizeof(long));
3671 if (cdbsmi == NULL) {
3672 return; /* record not found; go with defaults */
3674 memcpy(smibuf, cdbsmi->ptr,
3675 ((cdbsmi->len > sizeof(struct MetaData)) ?
3676 sizeof(struct MetaData) : cdbsmi->len));
3683 * PutMetaData() - (re)write supplementary record for a message
3685 void PutMetaData(struct MetaData *smibuf)
3689 /* Use the negative of the message number for the metadata db index */
3690 TheIndex = (0L - smibuf->meta_msgnum);
3692 cdb_store(CDB_MSGMAIN,
3693 &TheIndex, (int)sizeof(long),
3694 smibuf, (int)sizeof(struct MetaData));
3699 * AdjRefCount - submit an adjustment to the reference count for a message.
3700 * (These are just queued -- we actually process them later.)
3702 void AdjRefCount(long msgnum, int incr)
3704 struct CitContext *CCC = CC;
3705 struct arcq new_arcq;
3708 MSG_syslog(LOG_DEBUG, "AdjRefCount() msg %ld ref count delta %+d\n", msgnum, incr);
3710 begin_critical_section(S_SUPPMSGMAIN);
3711 if (arcfp == NULL) {
3712 arcfp = fopen(file_arcq, "ab+");
3713 chown(file_arcq, CTDLUID, (-1));
3714 chmod(file_arcq, 0600);
3716 end_critical_section(S_SUPPMSGMAIN);
3718 /* msgnum < 0 means that we're trying to close the file */
3720 MSGM_syslog(LOG_DEBUG, "Closing the AdjRefCount queue file\n");
3721 begin_critical_section(S_SUPPMSGMAIN);
3722 if (arcfp != NULL) {
3726 end_critical_section(S_SUPPMSGMAIN);
3731 * If we can't open the queue, perform the operation synchronously.
3733 if (arcfp == NULL) {
3734 TDAP_AdjRefCount(msgnum, incr);
3738 new_arcq.arcq_msgnum = msgnum;
3739 new_arcq.arcq_delta = incr;
3740 rv = fwrite(&new_arcq, sizeof(struct arcq), 1, arcfp);
3742 MSG_syslog(LOG_EMERG, "Couldn't write Refcount Queue File %s: %s\n",
3751 void AdjRefCountList(long *msgnum, long nmsg, int incr)
3753 struct CitContext *CCC = CC;
3754 long i, the_size, offset;
3755 struct arcq *new_arcq;
3758 MSG_syslog(LOG_DEBUG, "AdjRefCountList() msg %ld ref count delta %+d\n", nmsg, incr);
3760 begin_critical_section(S_SUPPMSGMAIN);
3761 if (arcfp == NULL) {
3762 arcfp = fopen(file_arcq, "ab+");
3763 chown(file_arcq, CTDLUID, (-1));
3764 chmod(file_arcq, 0600);
3766 end_critical_section(S_SUPPMSGMAIN);
3769 * If we can't open the queue, perform the operation synchronously.
3771 if (arcfp == NULL) {
3772 for (i = 0; i < nmsg; i++)
3773 TDAP_AdjRefCount(msgnum[i], incr);
3777 the_size = sizeof(struct arcq) * nmsg;
3778 new_arcq = malloc(the_size);
3779 for (i = 0; i < nmsg; i++) {
3780 new_arcq[i].arcq_msgnum = msgnum[i];
3781 new_arcq[i].arcq_delta = incr;
3785 while ((rv >= 0) && (offset < the_size))
3787 rv = fwrite(new_arcq + offset, 1, the_size - offset, arcfp);
3789 MSG_syslog(LOG_EMERG, "Couldn't write Refcount Queue File %s: %s\n",
3805 * TDAP_ProcessAdjRefCountQueue()
3807 * Process the queue of message count adjustments that was created by calls
3808 * to AdjRefCount() ... by reading the queue and calling TDAP_AdjRefCount()
3809 * for each one. This should be an "off hours" operation.
3811 int TDAP_ProcessAdjRefCountQueue(void)
3813 struct CitContext *CCC = CC;
3814 char file_arcq_temp[PATH_MAX];
3817 struct arcq arcq_rec;
3818 int num_records_processed = 0;
3820 snprintf(file_arcq_temp, sizeof file_arcq_temp, "%s.%04x", file_arcq, rand());
3822 begin_critical_section(S_SUPPMSGMAIN);
3823 if (arcfp != NULL) {
3828 r = link(file_arcq, file_arcq_temp);
3830 MSG_syslog(LOG_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
3831 end_critical_section(S_SUPPMSGMAIN);
3832 return(num_records_processed);
3836 end_critical_section(S_SUPPMSGMAIN);
3838 fp = fopen(file_arcq_temp, "rb");
3840 MSG_syslog(LOG_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
3841 return(num_records_processed);
3844 while (fread(&arcq_rec, sizeof(struct arcq), 1, fp) == 1) {
3845 TDAP_AdjRefCount(arcq_rec.arcq_msgnum, arcq_rec.arcq_delta);
3846 ++num_records_processed;
3850 r = unlink(file_arcq_temp);
3852 MSG_syslog(LOG_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
3855 return(num_records_processed);
3861 * TDAP_AdjRefCount - adjust the reference count for a message.
3862 * This one does it "for real" because it's called by
3863 * the autopurger function that processes the queue
3864 * created by AdjRefCount(). If a message's reference
3865 * count becomes zero, we also delete the message from
3866 * disk and de-index it.
3868 void TDAP_AdjRefCount(long msgnum, int incr)
3870 struct CitContext *CCC = CC;
3872 struct MetaData smi;
3875 /* This is a *tight* critical section; please keep it that way, as
3876 * it may get called while nested in other critical sections.
3877 * Complicating this any further will surely cause deadlock!
3879 begin_critical_section(S_SUPPMSGMAIN);
3880 GetMetaData(&smi, msgnum);
3881 smi.meta_refcount += incr;
3883 end_critical_section(S_SUPPMSGMAIN);
3884 MSG_syslog(LOG_DEBUG, "TDAP_AdjRefCount() msg %ld ref count delta %+d, is now %d\n",
3885 msgnum, incr, smi.meta_refcount
3888 /* If the reference count is now zero, delete the message
3889 * (and its supplementary record as well).
3891 if (smi.meta_refcount == 0) {
3892 MSG_syslog(LOG_DEBUG, "Deleting message <%ld>\n", msgnum);
3894 /* Call delete hooks with NULL room to show it has gone altogether */
3895 PerformDeleteHooks(NULL, msgnum);
3897 /* Remove from message base */
3899 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3900 cdb_delete(CDB_BIGMSGS, &delnum, (int)sizeof(long));
3902 /* Remove metadata record */
3903 delnum = (0L - msgnum);
3904 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3910 * Write a generic object to this room
3912 * Note: this could be much more efficient. Right now we use two temporary
3913 * files, and still pull the message into memory as with all others.
3915 void CtdlWriteObject(char *req_room, /* Room to stuff it in */
3916 char *content_type, /* MIME type of this object */
3917 char *raw_message, /* Data to be written */
3918 off_t raw_length, /* Size of raw_message */
3919 struct ctdluser *is_mailbox, /* Mailbox room? */
3920 int is_binary, /* Is encoding necessary? */
3921 int is_unique, /* Del others of this type? */
3922 unsigned int flags /* Internal save flags */
3925 struct CitContext *CCC = CC;
3926 struct ctdlroom qrbuf;
3927 char roomname[ROOMNAMELEN];
3928 struct CtdlMessage *msg;
3929 StrBuf *encoded_message = NULL;
3931 if (is_mailbox != NULL) {
3932 CtdlMailboxName(roomname, sizeof roomname, is_mailbox, req_room);
3935 safestrncpy(roomname, req_room, sizeof(roomname));
3938 MSG_syslog(LOG_DEBUG, "Raw length is %ld\n", (long)raw_length);
3941 encoded_message = NewStrBufPlain(NULL, (size_t) (((raw_length * 134) / 100) + 4096 ) );
3944 encoded_message = NewStrBufPlain(NULL, (size_t)(raw_length + 4096));
3947 StrBufAppendBufPlain(encoded_message, HKEY("Content-type: "), 0);
3948 StrBufAppendBufPlain(encoded_message, content_type, -1, 0);
3949 StrBufAppendBufPlain(encoded_message, HKEY("\n"), 0);
3952 StrBufAppendBufPlain(encoded_message, HKEY("Content-transfer-encoding: base64\n\n"), 0);
3955 StrBufAppendBufPlain(encoded_message, HKEY("Content-transfer-encoding: 7bit\n\n"), 0);
3959 StrBufBase64Append(encoded_message, NULL, raw_message, raw_length, 0);
3962 StrBufAppendBufPlain(encoded_message, raw_message, raw_length, 0);
3965 MSGM_syslog(LOG_DEBUG, "Allocating\n");
3966 msg = malloc(sizeof(struct CtdlMessage));
3967 memset(msg, 0, sizeof(struct CtdlMessage));
3968 msg->cm_magic = CTDLMESSAGE_MAGIC;
3969 msg->cm_anon_type = MES_NORMAL;
3970 msg->cm_format_type = 4;
3971 CM_SetField(msg, eAuthor, CCC->user.fullname, strlen(CCC->user.fullname));
3972 CM_SetField(msg, eOriginalRoom, req_room, strlen(req_room));
3973 CM_SetField(msg, eNodeName, CFG_KEY(c_nodename));
3974 CM_SetField(msg, eHumanNode, CFG_KEY(c_humannode));
3975 msg->cm_flags = flags;
3977 CM_SetAsFieldSB(msg, eMesageText, &encoded_message);
3979 /* Create the requested room if we have to. */
3980 if (CtdlGetRoom(&qrbuf, roomname) != 0) {
3981 CtdlCreateRoom(roomname,
3982 ( (is_mailbox != NULL) ? 5 : 3 ),
3983 "", 0, 1, 0, VIEW_BBS);
3985 /* If the caller specified this object as unique, delete all
3986 * other objects of this type that are currently in the room.
3989 MSG_syslog(LOG_DEBUG, "Deleted %d other msgs of this type\n",
3990 CtdlDeleteMessages(roomname, NULL, 0, content_type)
3993 /* Now write the data */
3994 CtdlSubmitMsg(msg, NULL, roomname, 0);
4000 /*****************************************************************************/
4001 /* MODULE INITIALIZATION STUFF */
4002 /*****************************************************************************/
4003 void SetMessageDebugEnabled(const int n)
4005 MessageDebugEnabled = n;
4007 CTDL_MODULE_INIT(msgbase)
4010 CtdlRegisterDebugFlagHook(HKEY("messages"), SetMessageDebugEnabled, &MessageDebugEnabled);
4013 /* return our Subversion id for the Log */