2 * Implements the message store.
4 * Copyright (c) 1987-2016 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
18 #include <libcitadel.h>
22 #include "ctdl_module.h"
23 #include "citserver.h"
26 #include "clientsocket.h"
31 #include "internet_addressing.h"
32 #include "euidindex.h"
34 #include "journaling.h"
36 struct addresses_to_be_filed *atbf = NULL;
38 /* This temp file holds the queue of operations for AdjRefCount() */
39 static FILE *arcfp = NULL;
40 void AdjRefCountList(long *msgnum, long nmsg, int incr);
42 int MessageDebugEnabled = 0;
45 * These are the four-character field headers we use when outputting
46 * messages in Citadel format (as opposed to RFC822 format).
49 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
50 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
51 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
52 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
53 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
54 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
55 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
56 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
58 "from", /* A -> eAuthor */
59 NULL, /* B -> eBig_message */
60 NULL, /* C -> eRemoteRoom */
61 NULL, /* D -> eDestination */
62 "exti", /* E -> eXclusivID */
63 "rfca", /* F -> erFc822Addr */
65 "hnod", /* H -> eHumanNode */
66 "msgn", /* I -> emessageId */
67 "jrnl", /* J -> eJournal */
68 "rep2", /* K -> eReplyTo */
69 "list", /* L -> eListID */
70 "text", /* M -> eMesageText */
71 "node", /* N -> eNodeName */
72 "room", /* O -> eOriginalRoom */
73 "path", /* P -> eMessagePath */
75 "rcpt", /* R -> eRecipient */
76 "spec", /* S -> eSpecialField */
77 "time", /* T -> eTimestamp */
78 "subj", /* U -> eMsgSubject */
79 "nvto", /* V -> eenVelopeTo */
80 "wefw", /* W -> eWeferences */
82 "cccc", /* Y -> eCarbonCopY */
86 HashList *msgKeyLookup = NULL;
88 int GetFieldFromMnemonic(eMsgField *f, const char* c)
91 if (GetHash(msgKeyLookup, c, 4, &v)) {
98 void FillMsgKeyLookupTable(void)
102 msgKeyLookup = NewHash (1, FourHash);
104 for (i=0; i < 91; i++) {
105 if (msgkeys[i] != NULL) {
106 Put(msgKeyLookup, msgkeys[i], 4, (void*)i, reference_free_handler);
111 eMsgField FieldOrder[] = {
112 /* Important fields */
123 /* Semi-important fields */
129 /* G is not used yet, may become virus signature*/
132 /* Q is not used yet */
135 /* X is not used yet */
136 /* Z is not used yet */
143 /* Message text (MUST be last) */
145 /* Not saved to disk:
150 static const long NDiskFields = sizeof(FieldOrder) / sizeof(eMsgField);
152 int CM_IsEmpty(struct CtdlMessage *Msg, eMsgField which)
154 return !((Msg->cm_fields[which] != NULL) &&
155 (Msg->cm_fields[which][0] != '\0'));
158 void CM_SetField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
160 if (Msg->cm_fields[which] != NULL)
161 free (Msg->cm_fields[which]);
162 Msg->cm_fields[which] = malloc(length + 1);
163 memcpy(Msg->cm_fields[which], buf, length);
164 Msg->cm_fields[which][length] = '\0';
165 Msg->cm_lengths[which] = length;
168 void CM_SetFieldLONG(struct CtdlMessage *Msg, eMsgField which, long lvalue)
172 len = snprintf(buf, sizeof(buf), "%ld", lvalue);
173 CM_SetField(Msg, which, buf, len);
175 void CM_CutFieldAt(struct CtdlMessage *Msg, eMsgField WhichToCut, long maxlen)
177 if (Msg->cm_fields[WhichToCut] == NULL)
180 if (Msg->cm_lengths[WhichToCut] > maxlen)
182 Msg->cm_fields[WhichToCut][maxlen] = '\0';
183 Msg->cm_lengths[WhichToCut] = maxlen;
187 void CM_FlushField(struct CtdlMessage *Msg, eMsgField which)
189 if (Msg->cm_fields[which] != NULL)
190 free (Msg->cm_fields[which]);
191 Msg->cm_fields[which] = NULL;
192 Msg->cm_lengths[which] = 0;
194 void CM_Flush(struct CtdlMessage *Msg)
198 if (CM_IsValidMsg(Msg) == 0)
201 for (i = 0; i < 256; ++i)
203 CM_FlushField(Msg, i);
207 void CM_CopyField(struct CtdlMessage *Msg, eMsgField WhichToPutTo, eMsgField WhichtToCopy)
210 if (Msg->cm_fields[WhichToPutTo] != NULL)
211 free (Msg->cm_fields[WhichToPutTo]);
213 if (Msg->cm_fields[WhichtToCopy] != NULL)
215 len = Msg->cm_lengths[WhichtToCopy];
216 Msg->cm_fields[WhichToPutTo] = malloc(len + 1);
217 memcpy(Msg->cm_fields[WhichToPutTo], Msg->cm_fields[WhichtToCopy], len);
218 Msg->cm_fields[WhichToPutTo][len] = '\0';
219 Msg->cm_lengths[WhichToPutTo] = len;
223 Msg->cm_fields[WhichToPutTo] = NULL;
224 Msg->cm_lengths[WhichToPutTo] = 0;
229 void CM_PrependToField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
231 if (Msg->cm_fields[which] != NULL) {
236 oldmsgsize = Msg->cm_lengths[which] + 1;
237 newmsgsize = length + oldmsgsize;
239 new = malloc(newmsgsize);
240 memcpy(new, buf, length);
241 memcpy(new + length, Msg->cm_fields[which], oldmsgsize);
242 free(Msg->cm_fields[which]);
243 Msg->cm_fields[which] = new;
244 Msg->cm_lengths[which] = newmsgsize - 1;
247 Msg->cm_fields[which] = malloc(length + 1);
248 memcpy(Msg->cm_fields[which], buf, length);
249 Msg->cm_fields[which][length] = '\0';
250 Msg->cm_lengths[which] = length;
254 void CM_SetAsField(struct CtdlMessage *Msg, eMsgField which, char **buf, long length)
256 if (Msg->cm_fields[which] != NULL)
257 free (Msg->cm_fields[which]);
259 Msg->cm_fields[which] = *buf;
261 Msg->cm_lengths[which] = length;
264 void CM_SetAsFieldSB(struct CtdlMessage *Msg, eMsgField which, StrBuf **buf)
266 if (Msg->cm_fields[which] != NULL)
267 free (Msg->cm_fields[which]);
269 Msg->cm_lengths[which] = StrLength(*buf);
270 Msg->cm_fields[which] = SmashStrBuf(buf);
273 void CM_GetAsField(struct CtdlMessage *Msg, eMsgField which, char **ret, long *retlen)
275 if (Msg->cm_fields[which] != NULL)
277 *retlen = Msg->cm_lengths[which];
278 *ret = Msg->cm_fields[which];
279 Msg->cm_fields[which] = NULL;
280 Msg->cm_lengths[which] = 0;
290 * Returns 1 if the supplied pointer points to a valid Citadel message.
291 * If the pointer is NULL or the magic number check fails, returns 0.
293 int CM_IsValidMsg(struct CtdlMessage *msg) {
296 if ((msg->cm_magic) != CTDLMESSAGE_MAGIC) {
297 struct CitContext *CCC = CC;
298 MSGM_syslog(LOG_WARNING, "CM_IsValidMsg() -- self-check failed\n");
304 void CM_FreeContents(struct CtdlMessage *msg)
308 for (i = 0; i < 256; ++i)
309 if (msg->cm_fields[i] != NULL) {
310 free(msg->cm_fields[i]);
311 msg->cm_lengths[i] = 0;
314 msg->cm_magic = 0; /* just in case */
317 * 'Destructor' for struct CtdlMessage
319 void CM_Free(struct CtdlMessage *msg)
321 if (CM_IsValidMsg(msg) == 0)
323 if (msg != NULL) free (msg);
326 CM_FreeContents(msg);
330 int CM_DupField(eMsgField i, struct CtdlMessage *OrgMsg, struct CtdlMessage *NewMsg)
333 len = OrgMsg->cm_lengths[i];
334 NewMsg->cm_fields[i] = malloc(len + 1);
335 if (NewMsg->cm_fields[i] == NULL)
337 memcpy(NewMsg->cm_fields[i], OrgMsg->cm_fields[i], len);
338 NewMsg->cm_fields[i][len] = '\0';
339 NewMsg->cm_lengths[i] = len;
343 struct CtdlMessage * CM_Duplicate(struct CtdlMessage *OrgMsg)
346 struct CtdlMessage *NewMsg;
348 if (CM_IsValidMsg(OrgMsg) == 0)
350 NewMsg = (struct CtdlMessage *)malloc(sizeof(struct CtdlMessage));
354 memcpy(NewMsg, OrgMsg, sizeof(struct CtdlMessage));
356 memset(&NewMsg->cm_fields, 0, sizeof(char*) * 256);
358 for (i = 0; i < 256; ++i)
360 if (OrgMsg->cm_fields[i] != NULL)
362 if (!CM_DupField(i, OrgMsg, NewMsg))
377 /* Determine if a given message matches the fields in a message template.
378 * Return 0 for a successful match.
380 int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template) {
383 /* If there aren't any fields in the template, all messages will
386 if (template == NULL) return(0);
388 /* Null messages are bogus. */
389 if (msg == NULL) return(1);
391 for (i='A'; i<='Z'; ++i) {
392 if (template->cm_fields[i] != NULL) {
393 if (msg->cm_fields[i] == NULL) {
394 /* Considered equal if temmplate is empty string */
395 if (IsEmptyStr(template->cm_fields[i])) continue;
398 if ((template->cm_lengths[i] != msg->cm_lengths[i]) ||
399 (strcasecmp(msg->cm_fields[i], template->cm_fields[i])))
404 /* All compares succeeded: we have a match! */
411 * Retrieve the "seen" message list for the current room.
413 void CtdlGetSeen(char *buf, int which_set) {
414 struct CitContext *CCC = CC;
417 /* Learn about the user and room in question */
418 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
420 if (which_set == ctdlsetseen_seen)
421 safestrncpy(buf, vbuf.v_seen, SIZ);
422 if (which_set == ctdlsetseen_answered)
423 safestrncpy(buf, vbuf.v_answered, SIZ);
429 * Manipulate the "seen msgs" string (or other message set strings)
431 void CtdlSetSeen(long *target_msgnums, int num_target_msgnums,
432 int target_setting, int which_set,
433 struct ctdluser *which_user, struct ctdlroom *which_room) {
434 struct CitContext *CCC = CC;
435 struct cdbdata *cdbfr;
440 long hi = (-1L); /// TODO: we just write here. y?
449 char *is_set; /* actually an array of booleans */
451 /* Don't bother doing *anything* if we were passed a list of zero messages */
452 if (num_target_msgnums < 1) {
456 /* If no room was specified, we go with the current room. */
458 which_room = &CCC->room;
461 /* If no user was specified, we go with the current user. */
463 which_user = &CCC->user;
466 MSG_syslog(LOG_DEBUG, "CtdlSetSeen(%d msgs starting with %ld, %s, %d) in <%s>\n",
467 num_target_msgnums, target_msgnums[0],
468 (target_setting ? "SET" : "CLEAR"),
472 /* Learn about the user and room in question */
473 CtdlGetRelationship(&vbuf, which_user, which_room);
475 /* Load the message list */
476 cdbfr = cdb_fetch(CDB_MSGLISTS, &which_room->QRnumber, sizeof(long));
478 msglist = (long *) cdbfr->ptr;
479 cdbfr->ptr = NULL; /* CtdlSetSeen() now owns this memory */
480 num_msgs = cdbfr->len / sizeof(long);
483 return; /* No messages at all? No further action. */
486 is_set = malloc(num_msgs * sizeof(char));
487 memset(is_set, 0, (num_msgs * sizeof(char)) );
489 /* Decide which message set we're manipulating */
491 case ctdlsetseen_seen:
492 vset = NewStrBufPlain(vbuf.v_seen, -1);
494 case ctdlsetseen_answered:
495 vset = NewStrBufPlain(vbuf.v_answered, -1);
502 #if 0 /* This is a special diagnostic section. Do not allow it to run during normal operation. */
503 MSG_syslog(LOG_DEBUG, "There are %d messages in the room.\n", num_msgs);
504 for (i=0; i<num_msgs; ++i) {
505 if ((i > 0) && (msglist[i] <= msglist[i-1])) abort();
507 MSG_syslog(LOG_DEBUG, "We are twiddling %d of them.\n", num_target_msgnums);
508 for (k=0; k<num_target_msgnums; ++k) {
509 if ((k > 0) && (target_msgnums[k] <= target_msgnums[k-1])) abort();
513 MSG_syslog(LOG_DEBUG, "before update: %s\n", ChrPtr(vset));
515 /* Translate the existing sequence set into an array of booleans */
516 setstr = NewStrBuf();
520 while (StrBufExtract_NextToken(setstr, vset, &pvset, ',') >= 0) {
522 StrBufExtract_token(lostr, setstr, 0, ':');
523 if (StrBufNum_tokens(setstr, ':') >= 2) {
524 StrBufExtract_token(histr, setstr, 1, ':');
528 StrBufAppendBuf(histr, lostr, 0);
531 if (!strcmp(ChrPtr(histr), "*")) {
538 for (i = 0; i < num_msgs; ++i) {
539 if ((msglist[i] >= lo) && (msglist[i] <= hi)) {
549 /* Now translate the array of booleans back into a sequence set */
555 for (i=0; i<num_msgs; ++i) {
559 for (k=0; k<num_target_msgnums; ++k) {
560 if (msglist[i] == target_msgnums[k]) {
561 is_seen = target_setting;
565 if ((was_seen == 0) && (is_seen == 1)) {
568 else if ((was_seen == 1) && (is_seen == 0)) {
571 if (StrLength(vset) > 0) {
572 StrBufAppendBufPlain(vset, HKEY(","), 0);
575 StrBufAppendPrintf(vset, "%ld", hi);
578 StrBufAppendPrintf(vset, "%ld:%ld", lo, hi);
582 if ((is_seen) && (i == num_msgs - 1)) {
583 if (StrLength(vset) > 0) {
584 StrBufAppendBufPlain(vset, HKEY(","), 0);
586 if ((i==0) || (was_seen == 0)) {
587 StrBufAppendPrintf(vset, "%ld", msglist[i]);
590 StrBufAppendPrintf(vset, "%ld:%ld", lo, msglist[i]);
598 * We will have to stuff this string back into a 4096 byte buffer, so if it's
599 * larger than that now, truncate it by removing tokens from the beginning.
600 * The limit of 100 iterations is there to prevent an infinite loop in case
601 * something unexpected happens.
603 int number_of_truncations = 0;
604 while ( (StrLength(vset) > SIZ) && (number_of_truncations < 100) ) {
605 StrBufRemove_token(vset, 0, ',');
606 ++number_of_truncations;
610 * If we're truncating the sequence set of messages marked with the 'seen' flag,
611 * we want the earliest messages (the truncated ones) to be marked, not unmarked.
612 * Otherwise messages at the beginning will suddenly appear to be 'unseen'.
614 if ( (which_set == ctdlsetseen_seen) && (number_of_truncations > 0) ) {
616 first_tok = NewStrBuf();
617 StrBufExtract_token(first_tok, vset, 0, ',');
618 StrBufRemove_token(vset, 0, ',');
620 if (StrBufNum_tokens(first_tok, ':') > 1) {
621 StrBufRemove_token(first_tok, 0, ':');
625 new_set = NewStrBuf();
626 StrBufAppendBufPlain(new_set, HKEY("1:"), 0);
627 StrBufAppendBuf(new_set, first_tok, 0);
628 StrBufAppendBufPlain(new_set, HKEY(":"), 0);
629 StrBufAppendBuf(new_set, vset, 0);
632 FreeStrBuf(&first_tok);
636 MSG_syslog(LOG_DEBUG, " after update: %s\n", ChrPtr(vset));
638 /* Decide which message set we're manipulating */
640 case ctdlsetseen_seen:
641 safestrncpy(vbuf.v_seen, ChrPtr(vset), sizeof vbuf.v_seen);
643 case ctdlsetseen_answered:
644 safestrncpy(vbuf.v_answered, ChrPtr(vset), sizeof vbuf.v_answered);
650 CtdlSetRelationship(&vbuf, which_user, which_room);
656 * API function to perform an operation for each qualifying message in the
657 * current room. (Returns the number of messages processed.)
659 int CtdlForEachMessage(int mode, long ref, char *search_string,
661 struct CtdlMessage *compare,
662 ForEachMsgCallback CallBack,
665 struct CitContext *CCC = CC;
668 struct cdbdata *cdbfr;
669 long *msglist = NULL;
671 int num_processed = 0;
674 struct CtdlMessage *msg = NULL;
677 int printed_lastold = 0;
678 int num_search_msgs = 0;
679 long *search_msgs = NULL;
681 int need_to_free_re = 0;
684 if ((content_type) && (!IsEmptyStr(content_type))) {
685 regcomp(&re, content_type, 0);
689 /* Learn about the user and room in question */
690 if (server_shutting_down) {
691 if (need_to_free_re) regfree(&re);
694 CtdlGetUser(&CCC->user, CCC->curr_user);
696 if (server_shutting_down) {
697 if (need_to_free_re) regfree(&re);
700 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
702 if (server_shutting_down) {
703 if (need_to_free_re) regfree(&re);
707 /* Load the message list */
708 cdbfr = cdb_fetch(CDB_MSGLISTS, &CCC->room.QRnumber, sizeof(long));
710 if (need_to_free_re) regfree(&re);
711 return 0; /* No messages at all? No further action. */
714 msglist = (long *) cdbfr->ptr;
715 num_msgs = cdbfr->len / sizeof(long);
717 cdbfr->ptr = NULL; /* clear this so that cdb_free() doesn't free it */
718 cdb_free(cdbfr); /* we own this memory now */
721 * Now begin the traversal.
723 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
725 /* If the caller is looking for a specific MIME type, filter
726 * out all messages which are not of the type requested.
728 if ((content_type != NULL) && (!IsEmptyStr(content_type))) {
730 /* This call to GetMetaData() sits inside this loop
731 * so that we only do the extra database read per msg
732 * if we need to. Doing the extra read all the time
733 * really kills the server. If we ever need to use
734 * metadata for another search criterion, we need to
735 * move the read somewhere else -- but still be smart
736 * enough to only do the read if the caller has
737 * specified something that will need it.
739 if (server_shutting_down) {
740 if (need_to_free_re) regfree(&re);
744 GetMetaData(&smi, msglist[a]);
746 /* if (strcasecmp(smi.meta_content_type, content_type)) { old non-regex way */
747 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) != 0) {
753 num_msgs = sort_msglist(msglist, num_msgs);
755 /* If a template was supplied, filter out the messages which
756 * don't match. (This could induce some delays!)
759 if (compare != NULL) {
760 for (a = 0; a < num_msgs; ++a) {
761 if (server_shutting_down) {
762 if (need_to_free_re) regfree(&re);
766 msg = CtdlFetchMessage(msglist[a], 1, 1);
768 if (CtdlMsgCmp(msg, compare)) {
777 /* If a search string was specified, get a message list from
778 * the full text index and remove messages which aren't on both
782 * Since the lists are sorted and strictly ascending, and the
783 * output list is guaranteed to be shorter than or equal to the
784 * input list, we overwrite the bottom of the input list. This
785 * eliminates the need to memmove big chunks of the list over and
788 if ( (num_msgs > 0) && (mode == MSGS_SEARCH) && (search_string) ) {
790 /* Call search module via hook mechanism.
791 * NULL means use any search function available.
792 * otherwise replace with a char * to name of search routine
794 CtdlModuleDoSearch(&num_search_msgs, &search_msgs, search_string, "fulltext");
796 if (num_search_msgs > 0) {
800 orig_num_msgs = num_msgs;
802 for (i=0; i<orig_num_msgs; ++i) {
803 for (j=0; j<num_search_msgs; ++j) {
804 if (msglist[i] == search_msgs[j]) {
805 msglist[num_msgs++] = msglist[i];
811 num_msgs = 0; /* No messages qualify */
813 if (search_msgs != NULL) free(search_msgs);
815 /* Now that we've purged messages which don't contain the search
816 * string, treat a MSGS_SEARCH just like a MSGS_ALL from this
823 * Now iterate through the message list, according to the
824 * criteria supplied by the caller.
827 for (a = 0; a < num_msgs; ++a) {
828 if (server_shutting_down) {
829 if (need_to_free_re) regfree(&re);
831 return num_processed;
833 thismsg = msglist[a];
834 if (mode == MSGS_ALL) {
838 is_seen = is_msg_in_sequence_set(
839 vbuf.v_seen, thismsg);
840 if (is_seen) lastold = thismsg;
846 || ((mode == MSGS_OLD) && (is_seen))
847 || ((mode == MSGS_NEW) && (!is_seen))
848 || ((mode == MSGS_LAST) && (a >= (num_msgs - ref)))
849 || ((mode == MSGS_FIRST) && (a < ref))
850 || ((mode == MSGS_GT) && (thismsg > ref))
851 || ((mode == MSGS_LT) && (thismsg < ref))
852 || ((mode == MSGS_EQ) && (thismsg == ref))
855 if ((mode == MSGS_NEW) && (CCC->user.flags & US_LASTOLD) && (lastold > 0L) && (printed_lastold == 0) && (!is_seen)) {
857 CallBack(lastold, userdata);
861 if (CallBack) CallBack(thismsg, userdata);
865 if (need_to_free_re) regfree(&re);
868 * We cache the most recent msglist in order to do security checks later
870 if (CCC->client_socket > 0) {
871 if (CCC->cached_msglist != NULL) {
872 free(CCC->cached_msglist);
874 CCC->cached_msglist = msglist;
875 CCC->cached_num_msgs = num_msgs;
881 return num_processed;
887 * memfmout() - Citadel text formatter and paginator.
888 * Although the original purpose of this routine was to format
889 * text to the reader's screen width, all we're really using it
890 * for here is to format text out to 80 columns before sending it
891 * to the client. The client software may reformat it again.
894 char *mptr, /* where are we going to get our text from? */
895 const char *nl /* string to terminate lines with */
897 struct CitContext *CCC = CC;
899 unsigned char ch = 0;
906 while (ch=*(mptr++), ch != 0) {
909 if (client_write(outbuf, len) == -1)
911 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
915 if (client_write(nl, nllen) == -1)
917 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
922 else if (ch == '\r') {
923 /* Ignore carriage returns. Newlines are always LF or CRLF but never CR. */
925 else if (isspace(ch)) {
926 if (column > 72) { /* Beyond 72 columns, break on the next space */
927 if (client_write(outbuf, len) == -1)
929 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
933 if (client_write(nl, nllen) == -1)
935 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
948 if (column > 1000) { /* Beyond 1000 columns, break anywhere */
949 if (client_write(outbuf, len) == -1)
951 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
955 if (client_write(nl, nllen) == -1)
957 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
965 if (client_write(outbuf, len) == -1)
967 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
970 client_write(nl, nllen);
978 * Callback function for mime parser that simply lists the part
980 void list_this_part(char *name, char *filename, char *partnum, char *disp,
981 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
982 char *cbid, void *cbuserdata)
986 ma = (struct ma_info *)cbuserdata;
987 if (ma->is_ma == 0) {
988 cprintf("part=%s|%s|%s|%s|%s|%ld|%s|%s\n",
1001 * Callback function for multipart prefix
1003 void list_this_pref(char *name, char *filename, char *partnum, char *disp,
1004 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1005 char *cbid, void *cbuserdata)
1009 ma = (struct ma_info *)cbuserdata;
1010 if (!strcasecmp(cbtype, "multipart/alternative")) {
1014 if (ma->is_ma == 0) {
1015 cprintf("pref=%s|%s\n", partnum, cbtype);
1020 * Callback function for multipart sufffix
1022 void list_this_suff(char *name, char *filename, char *partnum, char *disp,
1023 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1024 char *cbid, void *cbuserdata)
1028 ma = (struct ma_info *)cbuserdata;
1029 if (ma->is_ma == 0) {
1030 cprintf("suff=%s|%s\n", partnum, cbtype);
1032 if (!strcasecmp(cbtype, "multipart/alternative")) {
1039 * Callback function for mime parser that opens a section for downloading
1040 * we use serv_files function here:
1042 extern void OpenCmdResult(char *filename, const char *mime_type);
1043 void mime_download(char *name, char *filename, char *partnum, char *disp,
1044 void *content, char *cbtype, char *cbcharset, size_t length,
1045 char *encoding, char *cbid, void *cbuserdata)
1048 CitContext *CCC = MyContext();
1050 /* Silently go away if there's already a download open. */
1051 if (CCC->download_fp != NULL)
1055 (!IsEmptyStr(partnum) && (!strcasecmp(CCC->download_desired_section, partnum)))
1056 || (!IsEmptyStr(cbid) && (!strcasecmp(CCC->download_desired_section, cbid)))
1058 CCC->download_fp = tmpfile();
1059 if (CCC->download_fp == NULL) {
1060 MSG_syslog(LOG_EMERG, "mime_download(): Couldn't write: %s\n",
1062 cprintf("%d cannot open temporary file: %s\n",
1063 ERROR + INTERNAL_ERROR, strerror(errno));
1067 rv = fwrite(content, length, 1, CCC->download_fp);
1069 MSG_syslog(LOG_EMERG, "mime_download(): Couldn't write: %s\n",
1071 cprintf("%d unable to write tempfile.\n",
1073 fclose(CCC->download_fp);
1074 CCC->download_fp = NULL;
1077 fflush(CCC->download_fp);
1078 rewind(CCC->download_fp);
1080 OpenCmdResult(filename, cbtype);
1087 * Callback function for mime parser that outputs a section all at once.
1088 * We can specify the desired section by part number *or* content-id.
1090 void mime_spew_section(char *name, char *filename, char *partnum, char *disp,
1091 void *content, char *cbtype, char *cbcharset, size_t length,
1092 char *encoding, char *cbid, void *cbuserdata)
1094 int *found_it = (int *)cbuserdata;
1097 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
1098 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
1101 cprintf("%d %d|-1|%s|%s|%s\n",
1108 client_write(content, length);
1112 struct CtdlMessage *CtdlDeserializeMessage(long msgnum, int with_body, const char *Buffer, long Length)
1114 struct CitContext *CCC = CC;
1115 struct CtdlMessage *ret = NULL;
1117 const char *upper_bound;
1119 cit_uint8_t field_header;
1123 upper_bound = Buffer + Length;
1125 /* Parse the three bytes that begin EVERY message on disk.
1126 * The first is always 0xFF, the on-disk magic number.
1127 * The second is the anonymous/public type byte.
1128 * The third is the format type byte (vari, fixed, or MIME).
1132 MSG_syslog(LOG_ERR, "Message %ld appears to be corrupted.\n", msgnum);
1135 ret = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1136 memset(ret, 0, sizeof(struct CtdlMessage));
1138 ret->cm_magic = CTDLMESSAGE_MAGIC;
1139 ret->cm_anon_type = *mptr++; /* Anon type byte */
1140 ret->cm_format_type = *mptr++; /* Format type byte */
1143 * The rest is zero or more arbitrary fields. Load them in.
1144 * We're done when we encounter either a zero-length field or
1145 * have just processed the 'M' (message text) field.
1148 field_header = '\0';
1151 /* work around possibly buggy messages: */
1152 while (field_header == '\0')
1154 if (mptr >= upper_bound) {
1157 field_header = *mptr++;
1159 if (mptr >= upper_bound) {
1162 which = field_header;
1165 CM_SetField(ret, which, mptr, len);
1167 mptr += len + 1; /* advance to next field */
1169 } while ((mptr < upper_bound) && (field_header != 'M'));
1176 * Load a message from disk into memory.
1177 * This is used by CtdlOutputMsg() and other fetch functions.
1179 * NOTE: Caller is responsible for freeing the returned CtdlMessage struct
1180 * using the CM_Free(); function.
1182 struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body, int run_msg_hooks)
1184 struct CitContext *CCC = CC;
1185 struct cdbdata *dmsgtext;
1186 struct CtdlMessage *ret = NULL;
1188 MSG_syslog(LOG_DEBUG, "CtdlFetchMessage(%ld, %d)\n", msgnum, with_body);
1189 dmsgtext = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
1190 if (dmsgtext == NULL) {
1191 MSG_syslog(LOG_ERR, "CtdlFetchMessage(%ld, %d) Failed!\n", msgnum, with_body);
1195 if (dmsgtext->ptr[dmsgtext->len - 1] != '\0')
1197 MSG_syslog(LOG_ERR, "CtdlFetchMessage(%ld, %d) Forcefully terminating message!!\n", msgnum, with_body);
1198 dmsgtext->ptr[dmsgtext->len - 1] = '\0';
1201 ret = CtdlDeserializeMessage(msgnum, with_body, dmsgtext->ptr, dmsgtext->len);
1209 /* Always make sure there's something in the msg text field. If
1210 * it's NULL, the message text is most likely stored separately,
1211 * so go ahead and fetch that. Failing that, just set a dummy
1212 * body so other code doesn't barf.
1214 if ( (CM_IsEmpty(ret, eMesageText)) && (with_body) ) {
1215 dmsgtext = cdb_fetch(CDB_BIGMSGS, &msgnum, sizeof(long));
1216 if (dmsgtext != NULL) {
1217 CM_SetAsField(ret, eMesageText, &dmsgtext->ptr, dmsgtext->len - 1);
1221 if (CM_IsEmpty(ret, eMesageText)) {
1222 CM_SetField(ret, eMesageText, HKEY("\r\n\r\n (no text)\r\n"));
1225 /* Perform "before read" hooks (aborting if any return nonzero) */
1226 if (run_msg_hooks && (PerformMessageHooks(ret, NULL, EVT_BEFOREREAD) > 0)) {
1237 * Pre callback function for multipart/alternative
1239 * NOTE: this differs from the standard behavior for a reason. Normally when
1240 * displaying multipart/alternative you want to show the _last_ usable
1241 * format in the message. Here we show the _first_ one, because it's
1242 * usually text/plain. Since this set of functions is designed for text
1243 * output to non-MIME-aware clients, this is the desired behavior.
1246 void fixed_output_pre(char *name, char *filename, char *partnum, char *disp,
1247 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1248 char *cbid, void *cbuserdata)
1250 struct CitContext *CCC = CC;
1253 ma = (struct ma_info *)cbuserdata;
1254 MSG_syslog(LOG_DEBUG, "fixed_output_pre() type=<%s>\n", cbtype);
1255 if (!strcasecmp(cbtype, "multipart/alternative")) {
1259 if (!strcasecmp(cbtype, "message/rfc822")) {
1265 * Post callback function for multipart/alternative
1267 void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
1268 void *content, char *cbtype, char *cbcharset, size_t length,
1269 char *encoding, char *cbid, void *cbuserdata)
1271 struct CitContext *CCC = CC;
1274 ma = (struct ma_info *)cbuserdata;
1275 MSG_syslog(LOG_DEBUG, "fixed_output_post() type=<%s>\n", cbtype);
1276 if (!strcasecmp(cbtype, "multipart/alternative")) {
1280 if (!strcasecmp(cbtype, "message/rfc822")) {
1286 * Inline callback function for mime parser that wants to display text
1288 void fixed_output(char *name, char *filename, char *partnum, char *disp,
1289 void *content, char *cbtype, char *cbcharset, size_t length,
1290 char *encoding, char *cbid, void *cbuserdata)
1292 struct CitContext *CCC = CC;
1298 ma = (struct ma_info *)cbuserdata;
1300 MSG_syslog(LOG_DEBUG,
1301 "fixed_output() part %s: %s (%s) (%ld bytes)\n",
1302 partnum, filename, cbtype, (long)length);
1305 * If we're in the middle of a multipart/alternative scope and
1306 * we've already printed another section, skip this one.
1308 if ( (ma->is_ma) && (ma->did_print) ) {
1309 MSG_syslog(LOG_DEBUG, "Skipping part %s (%s)\n", partnum, cbtype);
1314 if ( (!strcasecmp(cbtype, "text/plain"))
1315 || (IsEmptyStr(cbtype)) ) {
1318 client_write(wptr, length);
1319 if (wptr[length-1] != '\n') {
1326 if (!strcasecmp(cbtype, "text/html")) {
1327 ptr = html_to_ascii(content, length, 80, 0);
1329 client_write(ptr, wlen);
1330 if ((wlen > 0) && (ptr[wlen-1] != '\n')) {
1337 if (ma->use_fo_hooks) {
1338 if (PerformFixedOutputHooks(cbtype, content, length)) {
1339 /* above function returns nonzero if it handled the part */
1344 if (strncasecmp(cbtype, "multipart/", 10)) {
1345 cprintf("Part %s: %s (%s) (%ld bytes)\r\n",
1346 partnum, filename, cbtype, (long)length);
1352 * The client is elegant and sophisticated and wants to be choosy about
1353 * MIME content types, so figure out which multipart/alternative part
1354 * we're going to send.
1356 * We use a system of weights. When we find a part that matches one of the
1357 * MIME types we've declared as preferential, we can store it in ma->chosen_part
1358 * and then set ma->chosen_pref to that MIME type's position in our preference
1359 * list. If we then hit another match, we only replace the first match if
1360 * the preference value is lower.
1362 void choose_preferred(char *name, char *filename, char *partnum, char *disp,
1363 void *content, char *cbtype, char *cbcharset, size_t length,
1364 char *encoding, char *cbid, void *cbuserdata)
1366 struct CitContext *CCC = CC;
1371 ma = (struct ma_info *)cbuserdata;
1373 // NOTE: REMOVING THIS CONDITIONAL FIXES BUG 220
1374 // http://bugzilla.citadel.org/show_bug.cgi?id=220
1375 // I don't know if there are any side effects! Please TEST TEST TEST
1376 //if (ma->is_ma > 0) {
1378 for (i=0; i<num_tokens(CCC->preferred_formats, '|'); ++i) {
1379 extract_token(buf, CCC->preferred_formats, i, '|', sizeof buf);
1380 if ( (!strcasecmp(buf, cbtype)) && (!ma->freeze) ) {
1381 if (i < ma->chosen_pref) {
1382 MSG_syslog(LOG_DEBUG, "Setting chosen part: <%s>\n", partnum);
1383 safestrncpy(ma->chosen_part, partnum, sizeof ma->chosen_part);
1384 ma->chosen_pref = i;
1391 * Now that we've chosen our preferred part, output it.
1393 void output_preferred(char *name,
1405 struct CitContext *CCC = CC;
1408 int add_newline = 0;
1411 char *decoded = NULL;
1412 size_t bytes_decoded;
1415 ma = (struct ma_info *)cbuserdata;
1417 /* This is not the MIME part you're looking for... */
1418 if (strcasecmp(partnum, ma->chosen_part)) return;
1420 /* If the content-type of this part is in our preferred formats
1421 * list, we can simply output it verbatim.
1423 for (i=0; i<num_tokens(CCC->preferred_formats, '|'); ++i) {
1424 extract_token(buf, CCC->preferred_formats, i, '|', sizeof buf);
1425 if (!strcasecmp(buf, cbtype)) {
1426 /* Yeah! Go! W00t!! */
1427 if (ma->dont_decode == 0)
1428 rc = mime_decode_now (content,
1434 break; /* Give us the chance, maybe theres another one. */
1436 if (rc == 0) text_content = (char *)content;
1438 text_content = decoded;
1439 length = bytes_decoded;
1442 if (text_content[length-1] != '\n') {
1445 cprintf("Content-type: %s", cbtype);
1446 if (!IsEmptyStr(cbcharset)) {
1447 cprintf("; charset=%s", cbcharset);
1449 cprintf("\nContent-length: %d\n",
1450 (int)(length + add_newline) );
1451 if (!IsEmptyStr(encoding)) {
1452 cprintf("Content-transfer-encoding: %s\n", encoding);
1455 cprintf("Content-transfer-encoding: 7bit\n");
1457 cprintf("X-Citadel-MSG4-Partnum: %s\n", partnum);
1459 if (client_write(text_content, length) == -1)
1461 MSGM_syslog(LOG_ERR, "output_preferred(): aborting due to write failure.\n");
1464 if (add_newline) cprintf("\n");
1465 if (decoded != NULL) free(decoded);
1470 /* No translations required or possible: output as text/plain */
1471 cprintf("Content-type: text/plain\n\n");
1473 if (ma->dont_decode == 0)
1474 rc = mime_decode_now (content,
1480 return; /* Give us the chance, maybe theres another one. */
1482 if (rc == 0) text_content = (char *)content;
1484 text_content = decoded;
1485 length = bytes_decoded;
1488 fixed_output(name, filename, partnum, disp, text_content, cbtype, cbcharset,
1489 length, encoding, cbid, cbuserdata);
1490 if (decoded != NULL) free(decoded);
1495 char desired_section[64];
1502 * Callback function for
1504 void extract_encapsulated_message(char *name, char *filename, char *partnum, char *disp,
1505 void *content, char *cbtype, char *cbcharset, size_t length,
1506 char *encoding, char *cbid, void *cbuserdata)
1508 struct encapmsg *encap;
1510 encap = (struct encapmsg *)cbuserdata;
1512 /* Only proceed if this is the desired section... */
1513 if (!strcasecmp(encap->desired_section, partnum)) {
1514 encap->msglen = length;
1515 encap->msg = malloc(length + 2);
1516 memcpy(encap->msg, content, length);
1523 * Determine whether the specified message exists in the cached_msglist
1524 * (This is a security check)
1526 int check_cached_msglist(long msgnum) {
1527 struct CitContext *CCC = CC;
1529 /* cases in which we skip the check */
1530 if (!CCC) return om_ok; /* not a session */
1531 if (CCC->client_socket <= 0) return om_ok; /* not a client session */
1532 if (CCC->cached_msglist == NULL) return om_access_denied; /* no msglist fetched */
1533 if (CCC->cached_num_msgs == 0) return om_access_denied; /* nothing to check */
1536 /* Do a binary search within the cached_msglist for the requested msgnum */
1538 int max = (CC->cached_num_msgs - 1);
1540 while (max >= min) {
1541 int middle = min + (max-min) / 2 ;
1542 if (msgnum == CCC->cached_msglist[middle]) {
1545 if (msgnum > CC->cached_msglist[middle]) {
1553 return om_access_denied;
1559 * Get a message off disk. (returns om_* values found in msgbase.h)
1562 int CtdlOutputMsg(long msg_num, /* message number (local) to fetch */
1563 int mode, /* how would you like that message? */
1564 int headers_only, /* eschew the message body? */
1565 int do_proto, /* do Citadel protocol responses? */
1566 int crlf, /* Use CRLF newlines instead of LF? */
1567 char *section, /* NULL or a message/rfc822 section */
1568 int flags, /* various flags; see msgbase.h */
1573 struct CitContext *CCC = CC;
1574 struct CtdlMessage *TheMessage = NULL;
1575 int retcode = CIT_OK;
1576 struct encapmsg encap;
1579 MSG_syslog(LOG_DEBUG, "CtdlOutputMsg(msgnum=%ld, mode=%d, section=%s)\n",
1581 (section ? section : "<>")
1584 r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
1587 if (r == om_not_logged_in) {
1588 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1591 cprintf("%d An unknown error has occurred.\n", ERROR);
1598 * Check to make sure the message is actually IN this room
1600 r = check_cached_msglist(msg_num);
1601 if (r == om_access_denied) {
1602 /* Not in the cache? We get ONE shot to check it again. */
1603 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, NULL, NULL);
1604 r = check_cached_msglist(msg_num);
1607 MSG_syslog(LOG_DEBUG, "Security check fail: message %ld is not in %s\n",
1608 msg_num, CCC->room.QRname
1611 if (r == om_access_denied) {
1612 cprintf("%d message %ld was not found in this room\n",
1613 ERROR + HIGHER_ACCESS_REQUIRED,
1622 * Fetch the message from disk. If we're in HEADERS_FAST mode,
1623 * request that we don't even bother loading the body into memory.
1625 if (headers_only == HEADERS_FAST) {
1626 TheMessage = CtdlFetchMessage(msg_num, 0, 1);
1629 TheMessage = CtdlFetchMessage(msg_num, 1, 1);
1632 if (TheMessage == NULL) {
1633 if (do_proto) cprintf("%d Can't locate msg %ld on disk\n",
1634 ERROR + MESSAGE_NOT_FOUND, msg_num);
1635 return(om_no_such_msg);
1638 /* Here is the weird form of this command, to process only an
1639 * encapsulated message/rfc822 section.
1641 if (section) if (!IsEmptyStr(section)) if (strcmp(section, "0")) {
1642 memset(&encap, 0, sizeof encap);
1643 safestrncpy(encap.desired_section, section, sizeof encap.desired_section);
1644 mime_parser(CM_RANGE(TheMessage, eMesageText),
1645 *extract_encapsulated_message,
1646 NULL, NULL, (void *)&encap, 0
1649 if ((Author != NULL) && (*Author == NULL))
1652 CM_GetAsField(TheMessage, eAuthor, Author, &len);
1654 if ((Address != NULL) && (*Address == NULL))
1657 CM_GetAsField(TheMessage, erFc822Addr, Address, &len);
1659 if ((MessageID != NULL) && (*MessageID == NULL))
1662 CM_GetAsField(TheMessage, emessageId, MessageID, &len);
1664 CM_Free(TheMessage);
1668 encap.msg[encap.msglen] = 0;
1669 TheMessage = convert_internet_message(encap.msg);
1670 encap.msg = NULL; /* no free() here, TheMessage owns it now */
1672 /* Now we let it fall through to the bottom of this
1673 * function, because TheMessage now contains the
1674 * encapsulated message instead of the top-level
1675 * message. Isn't that neat?
1680 cprintf("%d msg %ld has no part %s\n",
1681 ERROR + MESSAGE_NOT_FOUND,
1685 retcode = om_no_such_msg;
1690 /* Ok, output the message now */
1691 if (retcode == CIT_OK)
1692 retcode = CtdlOutputPreLoadedMsg(TheMessage, mode, headers_only, do_proto, crlf, flags);
1693 if ((Author != NULL) && (*Author == NULL))
1696 CM_GetAsField(TheMessage, eAuthor, Author, &len);
1698 if ((Address != NULL) && (*Address == NULL))
1701 CM_GetAsField(TheMessage, erFc822Addr, Address, &len);
1703 if ((MessageID != NULL) && (*MessageID == NULL))
1706 CM_GetAsField(TheMessage, emessageId, MessageID, &len);
1709 CM_Free(TheMessage);
1716 void OutputCtdlMsgHeaders(
1717 struct CtdlMessage *TheMessage,
1718 int do_proto) /* do Citadel protocol responses? */
1723 char display_name[256];
1725 /* begin header processing loop for Citadel message format */
1726 safestrncpy(display_name, "<unknown>", sizeof display_name);
1727 if (!CM_IsEmpty(TheMessage, eAuthor)) {
1728 strcpy(buf, TheMessage->cm_fields[eAuthor]);
1729 if (TheMessage->cm_anon_type == MES_ANONONLY) {
1730 safestrncpy(display_name, "****", sizeof display_name);
1732 else if (TheMessage->cm_anon_type == MES_ANONOPT) {
1733 safestrncpy(display_name, "anonymous", sizeof display_name);
1736 safestrncpy(display_name, buf, sizeof display_name);
1738 if ((is_room_aide())
1739 && ((TheMessage->cm_anon_type == MES_ANONONLY)
1740 || (TheMessage->cm_anon_type == MES_ANONOPT))) {
1741 size_t tmp = strlen(display_name);
1742 snprintf(&display_name[tmp],
1743 sizeof display_name - tmp,
1748 /* Don't show Internet address for users on the
1749 * local Citadel network.
1752 if (!CM_IsEmpty(TheMessage, eNodeName) &&
1753 (haschar(TheMessage->cm_fields[eNodeName], '.') == 0))
1758 /* Now spew the header fields in the order we like them. */
1759 for (i=0; i< NDiskFields; ++i) {
1761 Field = FieldOrder[i];
1762 if (Field != eMesageText) {
1763 if ( (!CM_IsEmpty(TheMessage, Field))
1764 && (msgkeys[Field] != NULL) ) {
1765 if ((Field == eenVelopeTo) ||
1766 (Field == eRecipient) ||
1767 (Field == eCarbonCopY)) {
1768 sanitize_truncated_recipient(TheMessage->cm_fields[Field]);
1770 if (Field == eAuthor) {
1771 if (do_proto) cprintf("%s=%s\n",
1775 else if ((Field == erFc822Addr) && (suppress_f)) {
1778 /* Masquerade display name if needed */
1780 if (do_proto) cprintf("%s=%s\n",
1782 TheMessage->cm_fields[Field]
1791 void OutputRFC822MsgHeaders(
1792 struct CtdlMessage *TheMessage,
1793 int flags, /* should the bessage be exported clean */
1794 const char *nl, int nlen,
1795 char *mid, long sizeof_mid,
1796 char *suser, long sizeof_suser,
1797 char *luser, long sizeof_luser,
1798 char *fuser, long sizeof_fuser,
1799 char *snode, long sizeof_snode)
1801 char datestamp[100];
1802 int subject_found = 0;
1809 for (i = 0; i < NDiskFields; ++i) {
1810 if (TheMessage->cm_fields[FieldOrder[i]]) {
1811 mptr = mpptr = TheMessage->cm_fields[FieldOrder[i]];
1812 switch (FieldOrder[i]) {
1814 safestrncpy(luser, mptr, sizeof_luser);
1815 safestrncpy(suser, mptr, sizeof_suser);
1818 if ((flags & QP_EADDR) != 0) {
1819 mptr = qp_encode_email_addrs(mptr);
1821 sanitize_truncated_recipient(mptr);
1822 cprintf("CC: %s%s", mptr, nl);
1825 cprintf("Return-Path: %s%s", mptr, nl);
1828 cprintf("List-ID: %s%s", mptr, nl);
1831 if ((flags & QP_EADDR) != 0)
1832 mptr = qp_encode_email_addrs(mptr);
1834 while ((*hptr != '\0') && isspace(*hptr))
1836 if (!IsEmptyStr(hptr))
1837 cprintf("Envelope-To: %s%s", hptr, nl);
1840 cprintf("Subject: %s%s", mptr, nl);
1844 safestrncpy(mid, mptr, sizeof_mid); /// TODO: detect @ here and copy @nodename in if not found.
1847 safestrncpy(fuser, mptr, sizeof_fuser);
1848 /* case eOriginalRoom:
1849 cprintf("X-Citadel-Room: %s%s",
1854 safestrncpy(snode, mptr, sizeof_snode);
1857 if (haschar(mptr, '@') == 0)
1859 sanitize_truncated_recipient(mptr);
1860 cprintf("To: %s@%s", mptr, CtdlGetConfigStr("c_fqdn"));
1865 if ((flags & QP_EADDR) != 0) {
1866 mptr = qp_encode_email_addrs(mptr);
1868 sanitize_truncated_recipient(mptr);
1869 cprintf("To: %s", mptr);
1874 datestring(datestamp, sizeof datestamp,
1875 atol(mptr), DATESTRING_RFC822);
1876 cprintf("Date: %s%s", datestamp, nl);
1879 cprintf("References: ");
1880 k = num_tokens(mptr, '|');
1881 for (j=0; j<k; ++j) {
1882 extract_token(buf, mptr, j, '|', sizeof buf);
1883 cprintf("<%s>", buf);
1894 while ((*hptr != '\0') && isspace(*hptr))
1896 if (!IsEmptyStr(hptr))
1897 cprintf("Reply-To: %s%s", mptr, nl);
1913 /* these don't map to mime message headers. */
1921 if (subject_found == 0) {
1922 cprintf("Subject: (no subject)%s", nl);
1927 void Dump_RFC822HeadersBody(
1928 struct CtdlMessage *TheMessage,
1929 int headers_only, /* eschew the message body? */
1930 int flags, /* should the bessage be exported clean? */
1932 const char *nl, int nlen)
1934 cit_uint8_t prev_ch;
1936 const char *StartOfText = StrBufNOTNULL;
1939 int nllen = strlen(nl);
1943 mptr = TheMessage->cm_fields[eMesageText];
1947 while (*mptr != '\0') {
1948 if (*mptr == '\r') {
1955 eoh = (*(mptr+1) == '\r') && (*(mptr+2) == '\n');
1957 eoh = *(mptr+1) == '\n';
1961 StartOfText = strchr(StartOfText, '\n');
1962 StartOfText = strchr(StartOfText, '\n');
1965 if (((headers_only == HEADERS_NONE) && (mptr >= StartOfText)) ||
1966 ((headers_only == HEADERS_ONLY) && (mptr < StartOfText)) ||
1967 ((headers_only != HEADERS_NONE) &&
1968 (headers_only != HEADERS_ONLY))
1970 if (*mptr == '\n') {
1971 memcpy(&outbuf[outlen], nl, nllen);
1973 outbuf[outlen] = '\0';
1976 outbuf[outlen++] = *mptr;
1980 if (flags & ESC_DOT)
1982 if ((prev_ch == '\n') &&
1984 ((*(mptr+1) == '\r') || (*(mptr+1) == '\n')))
1986 outbuf[outlen++] = '.';
1991 if (outlen > 1000) {
1992 if (client_write(outbuf, outlen) == -1)
1994 struct CitContext *CCC = CC;
1995 MSGM_syslog(LOG_ERR, "Dump_RFC822HeadersBody(): aborting due to write failure.\n");
1998 lfSent = (outbuf[outlen - 1] == '\n');
2003 client_write(outbuf, outlen);
2004 lfSent = (outbuf[outlen - 1] == '\n');
2007 client_write(nl, nlen);
2012 /* If the format type on disk is 1 (fixed-format), then we want
2013 * everything to be output completely literally ... regardless of
2014 * what message transfer format is in use.
2016 void DumpFormatFixed(
2017 struct CtdlMessage *TheMessage,
2018 int mode, /* how would you like that message? */
2019 const char *nl, int nllen)
2027 mptr = TheMessage->cm_fields[eMesageText];
2029 if (mode == MT_MIME) {
2030 cprintf("Content-type: text/plain\n\n");
2034 while (ch = *mptr++, ch > 0) {
2038 if ((buflen > 250) && (!xlline)){
2042 while ((buflen > 0) &&
2043 (!isspace(buf[buflen])))
2049 mptr -= tbuflen - buflen;
2054 /* if we reach the outer bounds of our buffer,
2055 abort without respect what whe purge. */
2058 (buflen > SIZ - nllen - 2)))
2062 memcpy (&buf[buflen], nl, nllen);
2066 if (client_write(buf, buflen) == -1)
2068 struct CitContext *CCC = CC;
2069 MSGM_syslog(LOG_ERR, "DumpFormatFixed(): aborting due to write failure.\n");
2081 if (!IsEmptyStr(buf))
2082 cprintf("%s%s", buf, nl);
2086 * Get a message off disk. (returns om_* values found in msgbase.h)
2088 int CtdlOutputPreLoadedMsg(
2089 struct CtdlMessage *TheMessage,
2090 int mode, /* how would you like that message? */
2091 int headers_only, /* eschew the message body? */
2092 int do_proto, /* do Citadel protocol responses? */
2093 int crlf, /* Use CRLF newlines instead of LF? */
2094 int flags /* should the bessage be exported clean? */
2096 struct CitContext *CCC = CC;
2098 const char *nl; /* newline string */
2102 /* Buffers needed for RFC822 translation. These are all filled
2103 * using functions that are bounds-checked, and therefore we can
2104 * make them substantially smaller than SIZ.
2112 MSG_syslog(LOG_DEBUG, "CtdlOutputPreLoadedMsg(TheMessage=%s, %d, %d, %d, %d\n",
2113 ((TheMessage == NULL) ? "NULL" : "not null"),
2114 mode, headers_only, do_proto, crlf);
2116 strcpy(mid, "unknown");
2117 nl = (crlf ? "\r\n" : "\n");
2118 nlen = crlf ? 2 : 1;
2120 if (!CM_IsValidMsg(TheMessage)) {
2121 MSGM_syslog(LOG_ERR,
2122 "ERROR: invalid preloaded message for output\n");
2124 return(om_no_such_msg);
2127 /* Suppress envelope recipients if required to avoid disclosing BCC addresses.
2128 * Pad it with spaces in order to avoid changing the RFC822 length of the message.
2130 if ( (flags & SUPPRESS_ENV_TO) && (!CM_IsEmpty(TheMessage, eenVelopeTo)) ) {
2131 memset(TheMessage->cm_fields[eenVelopeTo], ' ', TheMessage->cm_lengths[eenVelopeTo]);
2134 /* Are we downloading a MIME component? */
2135 if (mode == MT_DOWNLOAD) {
2136 if (TheMessage->cm_format_type != FMT_RFC822) {
2138 cprintf("%d This is not a MIME message.\n",
2139 ERROR + ILLEGAL_VALUE);
2140 } else if (CCC->download_fp != NULL) {
2141 if (do_proto) cprintf(
2142 "%d You already have a download open.\n",
2143 ERROR + RESOURCE_BUSY);
2145 /* Parse the message text component */
2146 mime_parser(CM_RANGE(TheMessage, eMesageText),
2147 *mime_download, NULL, NULL, NULL, 0);
2148 /* If there's no file open by this time, the requested
2149 * section wasn't found, so print an error
2151 if (CCC->download_fp == NULL) {
2152 if (do_proto) cprintf(
2153 "%d Section %s not found.\n",
2154 ERROR + FILE_NOT_FOUND,
2155 CCC->download_desired_section);
2158 return((CCC->download_fp != NULL) ? om_ok : om_mime_error);
2161 /* MT_SPEW_SECTION is like MT_DOWNLOAD except it outputs the whole MIME part
2162 * in a single server operation instead of opening a download file.
2164 if (mode == MT_SPEW_SECTION) {
2165 if (TheMessage->cm_format_type != FMT_RFC822) {
2167 cprintf("%d This is not a MIME message.\n",
2168 ERROR + ILLEGAL_VALUE);
2170 /* Parse the message text component */
2173 mime_parser(CM_RANGE(TheMessage, eMesageText),
2174 *mime_spew_section, NULL, NULL, (void *)&found_it, 0);
2175 /* If section wasn't found, print an error
2178 if (do_proto) cprintf(
2179 "%d Section %s not found.\n",
2180 ERROR + FILE_NOT_FOUND,
2181 CCC->download_desired_section);
2184 return((CCC->download_fp != NULL) ? om_ok : om_mime_error);
2187 /* now for the user-mode message reading loops */
2188 if (do_proto) cprintf("%d msg:\n", LISTING_FOLLOWS);
2190 /* Does the caller want to skip the headers? */
2191 if (headers_only == HEADERS_NONE) goto START_TEXT;
2193 /* Tell the client which format type we're using. */
2194 if ( (mode == MT_CITADEL) && (do_proto) ) {
2195 cprintf("type=%d\n", TheMessage->cm_format_type);
2198 /* nhdr=yes means that we're only displaying headers, no body */
2199 if ( (TheMessage->cm_anon_type == MES_ANONONLY)
2200 && ((mode == MT_CITADEL) || (mode == MT_MIME))
2203 cprintf("nhdr=yes\n");
2206 if ((mode == MT_CITADEL) || (mode == MT_MIME))
2207 OutputCtdlMsgHeaders(TheMessage, do_proto);
2210 /* begin header processing loop for RFC822 transfer format */
2214 memcpy(snode, CtdlGetConfigStr("c_nodename"), strlen(CtdlGetConfigStr("c_nodename")) + 1);
2215 if (mode == MT_RFC822)
2216 OutputRFC822MsgHeaders(
2221 suser, sizeof(suser),
2222 luser, sizeof(luser),
2223 fuser, sizeof(fuser),
2224 snode, sizeof(snode)
2228 for (i=0; !IsEmptyStr(&suser[i]); ++i) {
2229 suser[i] = tolower(suser[i]);
2230 if (!isalnum(suser[i])) suser[i]='_';
2233 if (mode == MT_RFC822) {
2234 if (!strcasecmp(snode, NODENAME)) {
2235 safestrncpy(snode, FQDN, sizeof snode);
2238 /* Construct a fun message id */
2239 cprintf("Message-ID: <%s", mid);/// todo: this possibly breaks threadding mails.
2240 if (strchr(mid, '@')==NULL) {
2241 cprintf("@%s", snode);
2245 if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONONLY)) {
2246 cprintf("From: \"----\" <x@x.org>%s", nl);
2248 else if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONOPT)) {
2249 cprintf("From: \"anonymous\" <x@x.org>%s", nl);
2251 else if (!IsEmptyStr(fuser)) {
2252 cprintf("From: \"%s\" <%s>%s", luser, fuser, nl);
2255 cprintf("From: \"%s\" <%s@%s>%s", luser, suser, snode, nl);
2258 /* Blank line signifying RFC822 end-of-headers */
2259 if (TheMessage->cm_format_type != FMT_RFC822) {
2264 /* end header processing loop ... at this point, we're in the text */
2266 if (headers_only == HEADERS_FAST) goto DONE;
2268 /* Tell the client about the MIME parts in this message */
2269 if (TheMessage->cm_format_type == FMT_RFC822) {
2270 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2271 memset(&ma, 0, sizeof(struct ma_info));
2272 mime_parser(CM_RANGE(TheMessage, eMesageText),
2273 (do_proto ? *list_this_part : NULL),
2274 (do_proto ? *list_this_pref : NULL),
2275 (do_proto ? *list_this_suff : NULL),
2278 else if (mode == MT_RFC822) { /* unparsed RFC822 dump */
2279 Dump_RFC822HeadersBody(
2288 if (headers_only == HEADERS_ONLY) {
2292 /* signify start of msg text */
2293 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2294 if (do_proto) cprintf("text\n");
2297 if (TheMessage->cm_format_type == FMT_FIXED)
2300 mode, /* how would you like that message? */
2303 /* If the message on disk is format 0 (Citadel vari-format), we
2304 * output using the formatter at 80 columns. This is the final output
2305 * form if the transfer format is RFC822, but if the transfer format
2306 * is Citadel proprietary, it'll still work, because the indentation
2307 * for new paragraphs is correct and the client will reformat the
2308 * message to the reader's screen width.
2310 if (TheMessage->cm_format_type == FMT_CITADEL) {
2311 if (mode == MT_MIME) {
2312 cprintf("Content-type: text/x-citadel-variformat\n\n");
2314 memfmout(TheMessage->cm_fields[eMesageText], nl);
2317 /* If the message on disk is format 4 (MIME), we've gotta hand it
2318 * off to the MIME parser. The client has already been told that
2319 * this message is format 1 (fixed format), so the callback function
2320 * we use will display those parts as-is.
2322 if (TheMessage->cm_format_type == FMT_RFC822) {
2323 memset(&ma, 0, sizeof(struct ma_info));
2325 if (mode == MT_MIME) {
2326 ma.use_fo_hooks = 0;
2327 strcpy(ma.chosen_part, "1");
2328 ma.chosen_pref = 9999;
2329 ma.dont_decode = CCC->msg4_dont_decode;
2330 mime_parser(CM_RANGE(TheMessage, eMesageText),
2331 *choose_preferred, *fixed_output_pre,
2332 *fixed_output_post, (void *)&ma, 1);
2333 mime_parser(CM_RANGE(TheMessage, eMesageText),
2334 *output_preferred, NULL, NULL, (void *)&ma, 1);
2337 ma.use_fo_hooks = 1;
2338 mime_parser(CM_RANGE(TheMessage, eMesageText),
2339 *fixed_output, *fixed_output_pre,
2340 *fixed_output_post, (void *)&ma, 0);
2345 DONE: /* now we're done */
2346 if (do_proto) cprintf("000\n");
2351 * Save one or more message pointers into a specified room
2352 * (Returns 0 for success, nonzero for failure)
2353 * roomname may be NULL to use the current room
2355 * Note that the 'supplied_msg' field may be set to NULL, in which case
2356 * the message will be fetched from disk, by number, if we need to perform
2357 * replication checks. This adds an additional database read, so if the
2358 * caller already has the message in memory then it should be supplied. (Obviously
2359 * this mode of operation only works if we're saving a single message.)
2361 int CtdlSaveMsgPointersInRoom(char *roomname, long newmsgidlist[], int num_newmsgs,
2362 int do_repl_check, struct CtdlMessage *supplied_msg, int suppress_refcount_adj
2364 struct CitContext *CCC = CC;
2366 char hold_rm[ROOMNAMELEN];
2367 struct cdbdata *cdbfr;
2370 long highest_msg = 0L;
2373 struct CtdlMessage *msg = NULL;
2375 long *msgs_to_be_merged = NULL;
2376 int num_msgs_to_be_merged = 0;
2378 MSG_syslog(LOG_DEBUG,
2379 "CtdlSaveMsgPointersInRoom(room=%s, num_msgs=%d, repl=%d, suppress_rca=%d)\n",
2380 roomname, num_newmsgs, do_repl_check, suppress_refcount_adj
2383 strcpy(hold_rm, CCC->room.QRname);
2386 if (newmsgidlist == NULL) return(ERROR + INTERNAL_ERROR);
2387 if (num_newmsgs < 1) return(ERROR + INTERNAL_ERROR);
2388 if (num_newmsgs > 1) supplied_msg = NULL;
2390 /* Now the regular stuff */
2391 if (CtdlGetRoomLock(&CCC->room,
2392 ((roomname != NULL) ? roomname : CCC->room.QRname) )
2394 MSG_syslog(LOG_ERR, "No such room <%s>\n", roomname);
2395 return(ERROR + ROOM_NOT_FOUND);
2399 msgs_to_be_merged = malloc(sizeof(long) * num_newmsgs);
2400 num_msgs_to_be_merged = 0;
2403 cdbfr = cdb_fetch(CDB_MSGLISTS, &CCC->room.QRnumber, sizeof(long));
2404 if (cdbfr == NULL) {
2408 msglist = (long *) cdbfr->ptr;
2409 cdbfr->ptr = NULL; /* CtdlSaveMsgPointerInRoom() now owns this memory */
2410 num_msgs = cdbfr->len / sizeof(long);
2415 /* Create a list of msgid's which were supplied by the caller, but do
2416 * not already exist in the target room. It is absolutely taboo to
2417 * have more than one reference to the same message in a room.
2419 for (i=0; i<num_newmsgs; ++i) {
2421 if (num_msgs > 0) for (j=0; j<num_msgs; ++j) {
2422 if (msglist[j] == newmsgidlist[i]) {
2427 msgs_to_be_merged[num_msgs_to_be_merged++] = newmsgidlist[i];
2431 MSG_syslog(LOG_DEBUG, "%d unique messages to be merged\n", num_msgs_to_be_merged);
2434 * Now merge the new messages
2436 msglist = realloc(msglist, (sizeof(long) * (num_msgs + num_msgs_to_be_merged)) );
2437 if (msglist == NULL) {
2438 MSGM_syslog(LOG_ALERT, "ERROR: can't realloc message list!\n");
2439 free(msgs_to_be_merged);
2440 return (ERROR + INTERNAL_ERROR);
2442 memcpy(&msglist[num_msgs], msgs_to_be_merged, (sizeof(long) * num_msgs_to_be_merged) );
2443 num_msgs += num_msgs_to_be_merged;
2445 /* Sort the message list, so all the msgid's are in order */
2446 num_msgs = sort_msglist(msglist, num_msgs);
2448 /* Determine the highest message number */
2449 highest_msg = msglist[num_msgs - 1];
2451 /* Write it back to disk. */
2452 cdb_store(CDB_MSGLISTS, &CCC->room.QRnumber, (int)sizeof(long),
2453 msglist, (int)(num_msgs * sizeof(long)));
2455 /* Free up the memory we used. */
2458 /* Update the highest-message pointer and unlock the room. */
2459 CCC->room.QRhighest = highest_msg;
2460 CtdlPutRoomLock(&CCC->room);
2462 /* Perform replication checks if necessary */
2463 if ( (DoesThisRoomNeedEuidIndexing(&CCC->room)) && (do_repl_check) ) {
2464 MSGM_syslog(LOG_DEBUG, "CtdlSaveMsgPointerInRoom() doing repl checks\n");
2466 for (i=0; i<num_msgs_to_be_merged; ++i) {
2467 msgid = msgs_to_be_merged[i];
2469 if (supplied_msg != NULL) {
2473 msg = CtdlFetchMessage(msgid, 0, 1);
2477 ReplicationChecks(msg);
2479 /* If the message has an Exclusive ID, index that... */
2480 if (!CM_IsEmpty(msg, eExclusiveID)) {
2481 index_message_by_euid(msg->cm_fields[eExclusiveID], &CCC->room, msgid);
2484 /* Free up the memory we may have allocated */
2485 if (msg != supplied_msg) {
2494 MSGM_syslog(LOG_DEBUG, "CtdlSaveMsgPointerInRoom() skips repl checks\n");
2497 /* Submit this room for processing by hooks */
2498 PerformRoomHooks(&CCC->room);
2500 /* Go back to the room we were in before we wandered here... */
2501 CtdlGetRoom(&CCC->room, hold_rm);
2503 /* Bump the reference count for all messages which were merged */
2504 if (!suppress_refcount_adj) {
2505 AdjRefCountList(msgs_to_be_merged, num_msgs_to_be_merged, +1);
2508 /* Free up memory... */
2509 if (msgs_to_be_merged != NULL) {
2510 free(msgs_to_be_merged);
2513 /* Return success. */
2519 * This is the same as CtdlSaveMsgPointersInRoom() but it only accepts
2522 int CtdlSaveMsgPointerInRoom(char *roomname, long msgid,
2523 int do_repl_check, struct CtdlMessage *supplied_msg)
2525 return CtdlSaveMsgPointersInRoom(roomname, &msgid, 1, do_repl_check, supplied_msg, 0);
2532 * Message base operation to save a new message to the message store
2533 * (returns new message number)
2535 * This is the back end for CtdlSubmitMsg() and should not be directly
2536 * called by server-side modules.
2539 long CtdlSaveThisMessage(struct CtdlMessage *msg, long msgid, int Reply) {
2540 struct CitContext *CCC = CC;
2548 * If the message is big, set its body aside for storage elsewhere
2549 * and we hide the message body from the serializer
2551 if (!CM_IsEmpty(msg, eMesageText) && msg->cm_lengths[eMesageText] > BIGMSG)
2554 holdM = msg->cm_fields[eMesageText];
2555 msg->cm_fields[eMesageText] = NULL;
2556 holdMLen = msg->cm_lengths[eMesageText];
2557 msg->cm_lengths[eMesageText] = 0;
2560 /* Serialize our data structure for storage in the database */
2561 CtdlSerializeMessage(&smr, msg);
2564 /* put the message body back into the message */
2565 msg->cm_fields[eMesageText] = holdM;
2566 msg->cm_lengths[eMesageText] = holdMLen;
2571 cprintf("%d Unable to serialize message\n",
2572 ERROR + INTERNAL_ERROR);
2575 MSGM_syslog(LOG_ERR, "CtdlSaveMessage() unable to serialize message");
2581 /* Write our little bundle of joy into the message base */
2582 retval = cdb_store(CDB_MSGMAIN, &msgid, (int)sizeof(long),
2585 MSG_syslog(LOG_ERR, "Can't store message %ld: %ld", msgid, retval);
2589 retval = cdb_store(CDB_BIGMSGS,
2596 MSG_syslog(LOG_ERR, "failed to store message body for msgid %ld: %ld",
2602 /* Free the memory we used for the serialized message */
2608 long send_message(struct CtdlMessage *msg) {
2614 /* Get a new message number */
2615 newmsgid = get_new_message_number();
2617 /* Generate an ID if we don't have one already */
2618 if (CM_IsEmpty(msg, emessageId)) {
2619 msgidbuflen = snprintf(msgidbuf, sizeof msgidbuf, "%08lX-%08lX@%s",
2620 (long unsigned int) time(NULL),
2621 (long unsigned int) newmsgid,
2622 CtdlGetConfigStr("c_fqdn")
2625 CM_SetField(msg, emessageId, msgidbuf, msgidbuflen);
2628 retval = CtdlSaveThisMessage(msg, newmsgid, 1);
2634 /* Return the *local* message ID to the caller
2635 * (even if we're storing an incoming network message)
2643 * Serialize a struct CtdlMessage into the format used on disk and network.
2645 * This function loads up a "struct ser_ret" (defined in server.h) which
2646 * contains the length of the serialized message and a pointer to the
2647 * serialized message in memory. THE LATTER MUST BE FREED BY THE CALLER.
2649 void CtdlSerializeMessage(struct ser_ret *ret, /* return values */
2650 struct CtdlMessage *msg) /* unserialized msg */
2652 struct CitContext *CCC = CC;
2657 * Check for valid message format
2659 if (CM_IsValidMsg(msg) == 0) {
2660 MSGM_syslog(LOG_ERR, "CtdlSerializeMessage() aborting due to invalid message\n");
2667 for (i=0; i < NDiskFields; ++i)
2668 if (msg->cm_fields[FieldOrder[i]] != NULL)
2669 ret->len += msg->cm_lengths[FieldOrder[i]] + 2;
2671 ret->ser = malloc(ret->len);
2672 if (ret->ser == NULL) {
2673 MSG_syslog(LOG_ERR, "CtdlSerializeMessage() malloc(%ld) failed: %s\n",
2674 (long)ret->len, strerror(errno));
2681 ret->ser[1] = msg->cm_anon_type;
2682 ret->ser[2] = msg->cm_format_type;
2685 for (i=0; i < NDiskFields; ++i)
2686 if (msg->cm_fields[FieldOrder[i]] != NULL)
2688 ret->ser[wlen++] = (char)FieldOrder[i];
2690 memcpy(&ret->ser[wlen],
2691 msg->cm_fields[FieldOrder[i]],
2692 msg->cm_lengths[FieldOrder[i]] + 1);
2694 wlen = wlen + msg->cm_lengths[FieldOrder[i]] + 1;
2697 if (ret->len != wlen) {
2698 MSG_syslog(LOG_ERR, "ERROR: len=%ld wlen=%ld\n",
2699 (long)ret->len, (long)wlen);
2707 * Check to see if any messages already exist in the current room which
2708 * carry the same Exclusive ID as this one. If any are found, delete them.
2710 void ReplicationChecks(struct CtdlMessage *msg) {
2711 struct CitContext *CCC = CC;
2712 long old_msgnum = (-1L);
2714 if (DoesThisRoomNeedEuidIndexing(&CCC->room) == 0) return;
2716 MSG_syslog(LOG_DEBUG, "Performing replication checks in <%s>\n",
2719 /* No exclusive id? Don't do anything. */
2720 if (msg == NULL) return;
2721 if (CM_IsEmpty(msg, eExclusiveID)) return;
2723 /*MSG_syslog(LOG_DEBUG, "Exclusive ID: <%s> for room <%s>\n",
2724 msg->cm_fields[eExclusiveID], CCC->room.QRname);*/
2726 old_msgnum = CtdlLocateMessageByEuid(msg->cm_fields[eExclusiveID], &CCC->room);
2727 if (old_msgnum > 0L) {
2728 MSG_syslog(LOG_DEBUG, "ReplicationChecks() replacing message %ld\n", old_msgnum);
2729 CtdlDeleteMessages(CCC->room.QRname, &old_msgnum, 1, "");
2736 * Save a message to disk and submit it into the delivery system.
2738 long CtdlSubmitMsg(struct CtdlMessage *msg, /* message to save */
2739 recptypes *recps, /* recipients (if mail) */
2740 const char *force, /* force a particular room? */
2741 int flags /* should the message be exported clean? */
2744 char hold_rm[ROOMNAMELEN];
2745 char actual_rm[ROOMNAMELEN];
2746 char force_room[ROOMNAMELEN];
2747 char content_type[SIZ]; /* We have to learn this */
2748 char recipient[SIZ];
2749 char bounce_to[1024];
2752 const char *mptr = NULL;
2753 struct ctdluser userbuf;
2755 struct MetaData smi;
2756 char *collected_addresses = NULL;
2757 struct addresses_to_be_filed *aptr = NULL;
2758 StrBuf *saved_rfc822_version = NULL;
2759 int qualified_for_journaling = 0;
2760 CitContext *CCC = MyContext();
2762 MSGM_syslog(LOG_DEBUG, "CtdlSubmitMsg() called\n");
2763 if (CM_IsValidMsg(msg) == 0) return(-1); /* self check */
2765 /* If this message has no timestamp, we take the liberty of
2766 * giving it one, right now.
2768 if (CM_IsEmpty(msg, eTimestamp)) {
2769 CM_SetFieldLONG(msg, eTimestamp, time(NULL));
2772 /* If this message has no path, we generate one.
2774 if (CM_IsEmpty(msg, eMessagePath)) {
2775 if (!CM_IsEmpty(msg, eAuthor)) {
2776 CM_CopyField(msg, eMessagePath, eAuthor);
2777 for (a=0; !IsEmptyStr(&msg->cm_fields[eMessagePath][a]); ++a) {
2778 if (isspace(msg->cm_fields[eMessagePath][a])) {
2779 msg->cm_fields[eMessagePath][a] = ' ';
2784 CM_SetField(msg, eMessagePath, HKEY("unknown"));
2788 if (force == NULL) {
2789 force_room[0] = '\0';
2792 strcpy(force_room, force);
2795 /* Learn about what's inside, because it's what's inside that counts */
2796 if (CM_IsEmpty(msg, eMesageText)) {
2797 MSGM_syslog(LOG_ERR, "ERROR: attempt to save message with NULL body\n");
2801 switch (msg->cm_format_type) {
2803 strcpy(content_type, "text/x-citadel-variformat");
2806 strcpy(content_type, "text/plain");
2809 strcpy(content_type, "text/plain");
2810 mptr = bmstrcasestr(msg->cm_fields[eMesageText], "Content-type:");
2813 safestrncpy(content_type, &mptr[13], sizeof content_type);
2814 striplt(content_type);
2815 aptr = content_type;
2816 while (!IsEmptyStr(aptr)) {
2828 /* Goto the correct room */
2829 room = (recps) ? CCC->room.QRname : SENTITEMS;
2830 MSG_syslog(LOG_DEBUG, "Selected room %s\n", room);
2831 strcpy(hold_rm, CCC->room.QRname);
2832 strcpy(actual_rm, CCC->room.QRname);
2833 if (recps != NULL) {
2834 strcpy(actual_rm, SENTITEMS);
2837 /* If the user is a twit, move to the twit room for posting */
2839 if (CCC->user.axlevel == AxProbU) {
2840 strcpy(hold_rm, actual_rm);
2841 strcpy(actual_rm, CtdlGetConfigStr("c_twitroom"));
2842 MSGM_syslog(LOG_DEBUG, "Diverting to twit room\n");
2846 /* ...or if this message is destined for Aide> then go there. */
2847 if (!IsEmptyStr(force_room)) {
2848 strcpy(actual_rm, force_room);
2851 MSG_syslog(LOG_DEBUG, "Final selection: %s (%s)\n", actual_rm, room);
2852 if (strcasecmp(actual_rm, CCC->room.QRname)) {
2853 /* CtdlGetRoom(&CCC->room, actual_rm); */
2854 CtdlUserGoto(actual_rm, 0, 1, NULL, NULL, NULL, NULL);
2858 * If this message has no O (room) field, generate one.
2860 if (CM_IsEmpty(msg, eOriginalRoom) && !IsEmptyStr(CCC->room.QRname)) {
2861 CM_SetField(msg, eOriginalRoom, CCC->room.QRname, strlen(CCC->room.QRname));
2864 /* Perform "before save" hooks (aborting if any return nonzero) */
2865 MSGM_syslog(LOG_DEBUG, "Performing before-save hooks\n");
2866 if (PerformMessageHooks(msg, recps, EVT_BEFORESAVE) > 0) return(-3);
2869 * If this message has an Exclusive ID, and the room is replication
2870 * checking enabled, then do replication checks.
2872 if (DoesThisRoomNeedEuidIndexing(&CCC->room)) {
2873 ReplicationChecks(msg);
2876 /* Save it to disk */
2877 MSGM_syslog(LOG_DEBUG, "Saving to disk\n");
2878 newmsgid = send_message(msg);
2879 if (newmsgid <= 0L) return(-5);
2881 /* Write a supplemental message info record. This doesn't have to
2882 * be a critical section because nobody else knows about this message
2885 MSGM_syslog(LOG_DEBUG, "Creating MetaData record\n");
2886 memset(&smi, 0, sizeof(struct MetaData));
2887 smi.meta_msgnum = newmsgid;
2888 smi.meta_refcount = 0;
2889 safestrncpy(smi.meta_content_type, content_type,
2890 sizeof smi.meta_content_type);
2893 * Measure how big this message will be when rendered as RFC822.
2894 * We do this for two reasons:
2895 * 1. We need the RFC822 length for the new metadata record, so the
2896 * POP and IMAP services don't have to calculate message lengths
2897 * while the user is waiting (multiplied by potentially hundreds
2898 * or thousands of messages).
2899 * 2. If journaling is enabled, we will need an RFC822 version of the
2900 * message to attach to the journalized copy.
2902 if (CCC->redirect_buffer != NULL) {
2903 MSGM_syslog(LOG_ALERT, "CCC->redirect_buffer is not NULL during message submission!\n");
2906 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
2907 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, QP_EADDR);
2908 smi.meta_rfc822_length = StrLength(CCC->redirect_buffer);
2909 saved_rfc822_version = CCC->redirect_buffer;
2910 CCC->redirect_buffer = NULL;
2914 /* Now figure out where to store the pointers */
2915 MSGM_syslog(LOG_DEBUG, "Storing pointers\n");
2917 /* If this is being done by the networker delivering a private
2918 * message, we want to BYPASS saving the sender's copy (because there
2919 * is no local sender; it would otherwise go to the Trashcan).
2921 if ((!CCC->internal_pgm) || (recps == NULL)) {
2922 if (CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 1, msg) != 0) {
2923 MSGM_syslog(LOG_ERR, "ERROR saving message pointer!\n");
2924 CtdlSaveMsgPointerInRoom(CtdlGetConfigStr("c_aideroom"), newmsgid, 0, msg);
2928 /* For internet mail, drop a copy in the outbound queue room */
2929 if ((recps != NULL) && (recps->num_internet > 0)) {
2930 CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, newmsgid, 0, msg);
2933 /* If other rooms are specified, drop them there too. */
2934 if ((recps != NULL) && (recps->num_room > 0))
2935 for (i=0; i<num_tokens(recps->recp_room, '|'); ++i) {
2936 extract_token(recipient, recps->recp_room, i,
2937 '|', sizeof recipient);
2938 MSG_syslog(LOG_DEBUG, "Delivering to room <%s>\n", recipient);///// xxxx
2939 CtdlSaveMsgPointerInRoom(recipient, newmsgid, 0, msg);
2942 /* Bump this user's messages posted counter. */
2943 MSGM_syslog(LOG_DEBUG, "Updating user\n");
2944 CtdlLockGetCurrentUser();
2945 CCC->user.posted = CCC->user.posted + 1;
2946 CtdlPutCurrentUserLock();
2948 /* Decide where bounces need to be delivered */
2949 if ((recps != NULL) && (recps->bounce_to == NULL))
2952 snprintf(bounce_to, sizeof bounce_to, "%s@%s",
2953 CCC->user.fullname, CtdlGetConfigStr("c_nodename"));
2955 snprintf(bounce_to, sizeof bounce_to, "%s@%s",
2956 msg->cm_fields[eAuthor], msg->cm_fields[eNodeName]);
2957 recps->bounce_to = bounce_to;
2960 CM_SetFieldLONG(msg, eVltMsgNum, newmsgid);
2963 /* If this is private, local mail, make a copy in the
2964 * recipient's mailbox and bump the reference count.
2966 if ((recps != NULL) && (recps->num_local > 0))
2971 pch = recps->recp_local;
2972 recps->recp_local = recipient;
2973 ntokens = num_tokens(pch, '|');
2974 for (i=0; i<ntokens; ++i)
2976 extract_token(recipient, pch, i, '|', sizeof recipient);
2977 MSG_syslog(LOG_DEBUG, "Delivering private local mail to <%s>\n", recipient);
2978 if (CtdlGetUser(&userbuf, recipient) == 0) {
2979 CtdlMailboxName(actual_rm, sizeof actual_rm, &userbuf, MAILROOM);
2980 CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0, msg);
2981 CtdlBumpNewMailCounter(userbuf.usernum);
2982 PerformMessageHooks(msg, recps, EVT_AFTERUSRMBOXSAVE);
2985 MSG_syslog(LOG_DEBUG, "No user <%s>\n", recipient);
2986 CtdlSaveMsgPointerInRoom(CtdlGetConfigStr("c_aideroom"), newmsgid, 0, msg);
2989 recps->recp_local = pch;
2992 /* Perform "after save" hooks */
2993 MSGM_syslog(LOG_DEBUG, "Performing after-save hooks\n");
2995 PerformMessageHooks(msg, recps, EVT_AFTERSAVE);
2996 CM_FlushField(msg, eVltMsgNum);
2998 /* Go back to the room we started from */
2999 MSG_syslog(LOG_DEBUG, "Returning to original room %s\n", hold_rm);
3000 if (strcasecmp(hold_rm, CCC->room.QRname))
3001 CtdlUserGoto(hold_rm, 0, 1, NULL, NULL, NULL, NULL);
3004 * Any addresses to harvest for someone's address book?
3006 if ( (CCC->logged_in) && (recps != NULL) ) {
3007 collected_addresses = harvest_collected_addresses(msg);
3010 if (collected_addresses != NULL) {
3011 aptr = (struct addresses_to_be_filed *)
3012 malloc(sizeof(struct addresses_to_be_filed));
3013 CtdlMailboxName(actual_rm, sizeof actual_rm,
3014 &CCC->user, USERCONTACTSROOM);
3015 aptr->roomname = strdup(actual_rm);
3016 aptr->collected_addresses = collected_addresses;
3017 begin_critical_section(S_ATBF);
3020 end_critical_section(S_ATBF);
3024 * Determine whether this message qualifies for journaling.
3026 if (!CM_IsEmpty(msg, eJournal)) {
3027 qualified_for_journaling = 0;
3030 if (recps == NULL) {
3031 qualified_for_journaling = CtdlGetConfigInt("c_journal_pubmsgs");
3033 else if (recps->num_local + recps->num_ignet + recps->num_internet > 0) {
3034 qualified_for_journaling = CtdlGetConfigInt("c_journal_email");
3037 qualified_for_journaling = CtdlGetConfigInt("c_journal_pubmsgs");
3042 * Do we have to perform journaling? If so, hand off the saved
3043 * RFC822 version will be handed off to the journaler for background
3044 * submit. Otherwise, we have to free the memory ourselves.
3046 if (saved_rfc822_version != NULL) {
3047 if (qualified_for_journaling) {
3048 JournalBackgroundSubmit(msg, saved_rfc822_version, recps);
3051 FreeStrBuf(&saved_rfc822_version);
3055 if ((recps != NULL) && (recps->bounce_to == bounce_to))
3056 recps->bounce_to = NULL;
3064 * Convenience function for generating small administrative messages.
3066 long quickie_message(const char *from,
3067 const char *fromaddr,
3072 const char *subject)
3074 struct CtdlMessage *msg;
3075 recptypes *recp = NULL;
3077 msg = malloc(sizeof(struct CtdlMessage));
3078 memset(msg, 0, sizeof(struct CtdlMessage));
3079 msg->cm_magic = CTDLMESSAGE_MAGIC;
3080 msg->cm_anon_type = MES_NORMAL;
3081 msg->cm_format_type = format_type;
3083 if (!IsEmptyStr(from)) {
3084 CM_SetField(msg, eAuthor, from, strlen(from));
3086 else if (!IsEmptyStr(fromaddr)) {
3088 CM_SetField(msg, eAuthor, fromaddr, strlen(fromaddr));
3089 pAt = strchr(msg->cm_fields[eAuthor], '@');
3091 CM_CutFieldAt(msg, eAuthor, pAt - msg->cm_fields[eAuthor]);
3095 msg->cm_fields[eAuthor] = strdup("Citadel");
3098 if (!IsEmptyStr(fromaddr)) CM_SetField(msg, erFc822Addr, fromaddr, strlen(fromaddr));
3099 if (!IsEmptyStr(room)) CM_SetField(msg, eOriginalRoom, room, strlen(room));
3100 CM_SetField(msg, eNodeName, CtdlGetConfigStr("c_nodename"), strlen(CtdlGetConfigStr("c_nodename")));
3101 if (!IsEmptyStr(to)) {
3102 CM_SetField(msg, eRecipient, to, strlen(to));
3103 recp = validate_recipients(to, NULL, 0);
3105 if (!IsEmptyStr(subject)) {
3106 CM_SetField(msg, eMsgSubject, subject, strlen(subject));
3108 if (!IsEmptyStr(text)) {
3109 CM_SetField(msg, eMesageText, text, strlen(text));
3112 long msgnum = CtdlSubmitMsg(msg, recp, room, 0);
3114 if (recp != NULL) free_recipients(recp);
3119 void flood_protect_quickie_message(const char *from,
3120 const char *fromaddr,
3125 const char *subject,
3127 const char **CritStr,
3128 const long *CritStrLen,
3133 struct CitContext *CCC = CC;
3135 u_char rawdigest[MD5_DIGEST_LEN];
3136 struct MD5Context md5context;
3140 static const time_t tsday = (8*60*60); /* just care for a day... */
3143 tslen = snprintf(timestamp, sizeof(timestamp), "%ld", tsday);
3144 MD5Init(&md5context);
3146 for (i = 0; i < nCriterions; i++)
3147 MD5Update(&md5context,
3148 (const unsigned char*)CritStr[i], CritStrLen[i]);
3149 MD5Update(&md5context,
3150 (const unsigned char*)timestamp, tslen);
3151 MD5Final(rawdigest, &md5context);
3153 guid = NewStrBufPlain(NULL,
3154 MD5_DIGEST_LEN * 2 + 12);
3155 StrBufHexEscAppend(guid, NULL, rawdigest, MD5_DIGEST_LEN);
3156 StrBufAppendBufPlain(guid, HKEY("_fldpt"), 0);
3157 if (StrLength(guid) > 40)
3158 StrBufCutAt(guid, 40, NULL);
3160 seenstamp = CheckIfAlreadySeen("FPAideMessage",
3167 if ((seenstamp > 0) && (seenstamp < tsday))
3170 /* yes, we did. flood protection kicks in. */
3171 MSG_syslog(LOG_DEBUG,
3172 "not sending message again - %ld < %ld \n", seenstamp, tsday);
3177 MSG_syslog(LOG_DEBUG,
3178 "sending message. %ld >= %ld", seenstamp, tsday);
3180 /* no, this message isn't sent recently; go ahead. */
3181 quickie_message(from,
3193 * Back end function used by CtdlMakeMessage() and similar functions
3195 StrBuf *CtdlReadMessageBodyBuf(char *terminator, /* token signalling EOT */
3197 size_t maxlen, /* maximum message length */
3198 StrBuf *exist, /* if non-null, append to it;
3199 exist is ALWAYS freed */
3200 int crlf, /* CRLF newlines instead of LF */
3201 int *sock /* socket handle or 0 for this session's client socket */
3210 LineBuf = NewStrBufPlain(NULL, SIZ);
3211 if (exist == NULL) {
3212 Message = NewStrBufPlain(NULL, 4 * SIZ);
3215 Message = NewStrBufDup(exist);
3218 /* Do we need to change leading ".." to "." for SMTP escaping? */
3219 if ((tlen == 1) && (*terminator == '.')) {
3223 /* read in the lines of message text one by one */
3226 if ((CtdlSockGetLine(sock, LineBuf, 5) < 0) ||
3231 if (CtdlClientGetLine(LineBuf) < 0) finished = 1;
3233 if ((StrLength(LineBuf) == tlen) &&
3234 (!strcmp(ChrPtr(LineBuf), terminator)))
3237 if ( (!flushing) && (!finished) ) {
3239 StrBufAppendBufPlain(LineBuf, HKEY("\r\n"), 0);
3242 StrBufAppendBufPlain(LineBuf, HKEY("\n"), 0);
3245 /* Unescape SMTP-style input of two dots at the beginning of the line */
3247 (StrLength(LineBuf) == 2) &&
3248 (!strcmp(ChrPtr(LineBuf), "..")))
3250 StrBufCutLeft(LineBuf, 1);
3253 StrBufAppendBuf(Message, LineBuf, 0);
3256 /* if we've hit the max msg length, flush the rest */
3257 if (StrLength(Message) >= maxlen) flushing = 1;
3259 } while (!finished);
3260 FreeStrBuf(&LineBuf);
3264 void DeleteAsyncMsg(ReadAsyncMsg **Msg)
3268 FreeStrBuf(&(*Msg)->MsgBuf);
3274 ReadAsyncMsg *NewAsyncMsg(const char *terminator, /* token signalling EOT */
3276 size_t maxlen, /* maximum message length */
3277 size_t expectlen, /* if we expect a message, how long should it be? */
3278 StrBuf *exist, /* if non-null, append to it;
3279 exist is ALWAYS freed */
3280 long eLen, /* length of exist */
3281 int crlf /* CRLF newlines instead of LF */
3284 ReadAsyncMsg *NewMsg;
3286 NewMsg = (ReadAsyncMsg *)malloc(sizeof(ReadAsyncMsg));
3287 memset(NewMsg, 0, sizeof(ReadAsyncMsg));
3289 if (exist == NULL) {
3292 if (expectlen == 0) {
3296 len = expectlen + 10;
3298 NewMsg->MsgBuf = NewStrBufPlain(NULL, len);
3301 NewMsg->MsgBuf = NewStrBufDup(exist);
3303 /* Do we need to change leading ".." to "." for SMTP escaping? */
3304 if ((tlen == 1) && (*terminator == '.')) {
3308 NewMsg->terminator = terminator;
3309 NewMsg->tlen = tlen;
3311 NewMsg->maxlen = maxlen;
3313 NewMsg->crlf = crlf;
3319 * Back end function used by CtdlMakeMessage() and similar functions
3321 eReadState CtdlReadMessageBodyAsync(AsyncIO *IO)
3323 ReadAsyncMsg *ReadMsg;
3324 int MsgFinished = 0;
3325 eReadState Finished = eMustReadMore;
3330 const char *pch = ChrPtr(IO->SendBuf.Buf);
3331 const char *pchh = IO->SendBuf.ReadWritePointer;
3337 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
3338 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
3339 ((CitContext*)(IO->CitContext))->ServiceName,
3342 fd = fopen(fn, "a+");
3344 syslog(LOG_EMERG, "failed to open file %s: %s", fn, strerror(errno));
3350 ReadMsg = IO->ReadMsg;
3352 /* read in the lines of message text one by one */
3354 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
3357 case eMustReadMore: /// read new from socket...
3359 if (IO->RecvBuf.ReadWritePointer != NULL) {
3360 nbytes = StrLength(IO->RecvBuf.Buf) - (IO->RecvBuf.ReadWritePointer - ChrPtr(IO->RecvBuf.Buf));
3361 fprintf(fd, "Read; Line unfinished: %ld Bytes still in buffer [", nbytes);
3363 fwrite(IO->RecvBuf.ReadWritePointer, nbytes, 1, fd);
3367 fprintf(fd, "BufferEmpty! \n");
3373 case eBufferNotEmpty: /* shouldn't happen... */
3374 case eReadSuccess: /// done for now...
3376 case eReadFail: /// WHUT?
3382 if ((StrLength(IO->IOBuf) == ReadMsg->tlen) &&
3383 (!strcmp(ChrPtr(IO->IOBuf), ReadMsg->terminator))) {
3386 fprintf(fd, "found Terminator; Message Size: %d\n", StrLength(ReadMsg->MsgBuf));
3389 else if (!ReadMsg->flushing) {
3392 fprintf(fd, "Read Line: [%d][%s]\n", StrLength(IO->IOBuf), ChrPtr(IO->IOBuf));
3395 /* Unescape SMTP-style input of two dots at the beginning of the line */
3396 if ((ReadMsg->dodot) &&
3397 (StrLength(IO->IOBuf) == 2) && /* TODO: do we just unescape lines with two dots or any line? */
3398 (!strcmp(ChrPtr(IO->IOBuf), "..")))
3401 fprintf(fd, "UnEscaped!\n");
3403 StrBufCutLeft(IO->IOBuf, 1);
3406 if (ReadMsg->crlf) {
3407 StrBufAppendBufPlain(IO->IOBuf, HKEY("\r\n"), 0);
3410 StrBufAppendBufPlain(IO->IOBuf, HKEY("\n"), 0);
3413 StrBufAppendBuf(ReadMsg->MsgBuf, IO->IOBuf, 0);
3416 /* if we've hit the max msg length, flush the rest */
3417 if (StrLength(ReadMsg->MsgBuf) >= ReadMsg->maxlen) ReadMsg->flushing = 1;
3419 } while (!MsgFinished);
3422 fprintf(fd, "Done with reading; %s.\n, ",
3423 (MsgFinished)?"Message Finished": "FAILED");
3427 return eReadSuccess;
3434 * Back end function used by CtdlMakeMessage() and similar functions
3436 char *CtdlReadMessageBody(char *terminator, /* token signalling EOT */
3438 size_t maxlen, /* maximum message length */
3439 StrBuf *exist, /* if non-null, append to it;
3440 exist is ALWAYS freed */
3441 int crlf, /* CRLF newlines instead of LF */
3442 int *sock /* socket handle or 0 for this session's client socket */
3447 Message = CtdlReadMessageBodyBuf(terminator,
3453 if (Message == NULL)
3456 return SmashStrBuf(&Message);
3459 struct CtdlMessage *CtdlMakeMessage(
3460 struct ctdluser *author, /* author's user structure */
3461 char *recipient, /* NULL if it's not mail */
3462 char *recp_cc, /* NULL if it's not mail */
3463 char *room, /* room where it's going */
3464 int type, /* see MES_ types in header file */
3465 int format_type, /* variformat, plain text, MIME... */
3466 char *fake_name, /* who we're masquerading as */
3467 char *my_email, /* which of my email addresses to use (empty is ok) */
3468 char *subject, /* Subject (optional) */
3469 char *supplied_euid, /* ...or NULL if this is irrelevant */
3470 char *preformatted_text, /* ...or NULL to read text from client */
3471 char *references /* Thread references */
3474 return CtdlMakeMessageLen(
3475 author, /* author's user structure */
3476 recipient, /* NULL if it's not mail */
3477 (recipient)?strlen(recipient) : 0,
3478 recp_cc, /* NULL if it's not mail */
3479 (recp_cc)?strlen(recp_cc): 0,
3480 room, /* room where it's going */
3481 (room)?strlen(room): 0,
3482 type, /* see MES_ types in header file */
3483 format_type, /* variformat, plain text, MIME... */
3484 fake_name, /* who we're masquerading as */
3485 (fake_name)?strlen(fake_name): 0,
3486 my_email, /* which of my email addresses to use (empty is ok) */
3487 (my_email)?strlen(my_email): 0,
3488 subject, /* Subject (optional) */
3489 (subject)?strlen(subject): 0,
3490 supplied_euid, /* ...or NULL if this is irrelevant */
3491 (supplied_euid)?strlen(supplied_euid):0,
3492 preformatted_text, /* ...or NULL to read text from client */
3493 (preformatted_text)?strlen(preformatted_text) : 0,
3494 references, /* Thread references */
3495 (references)?strlen(references):0);
3500 * Build a binary message to be saved on disk.
3501 * (NOTE: if you supply 'preformatted_text', the buffer you give it
3502 * will become part of the message. This means you are no longer
3503 * responsible for managing that memory -- it will be freed along with
3504 * the rest of the fields when CM_Free() is called.)
3507 struct CtdlMessage *CtdlMakeMessageLen(
3508 struct ctdluser *author, /* author's user structure */
3509 char *recipient, /* NULL if it's not mail */
3511 char *recp_cc, /* NULL if it's not mail */
3513 char *room, /* room where it's going */
3515 int type, /* see MES_ types in header file */
3516 int format_type, /* variformat, plain text, MIME... */
3517 char *fake_name, /* who we're masquerading as */
3519 char *my_email, /* which of my email addresses to use (empty is ok) */
3521 char *subject, /* Subject (optional) */
3523 char *supplied_euid, /* ...or NULL if this is irrelevant */
3525 char *preformatted_text, /* ...or NULL to read text from client */
3527 char *references, /* Thread references */
3531 struct CitContext *CCC = CC;
3532 /* Don't confuse the poor folks if it's not routed mail. * /
3533 char dest_node[256] = "";*/
3536 struct CtdlMessage *msg;
3538 StrBuf *FakeEncAuthor = NULL;
3540 msg = malloc(sizeof(struct CtdlMessage));
3541 memset(msg, 0, sizeof(struct CtdlMessage));
3542 msg->cm_magic = CTDLMESSAGE_MAGIC;
3543 msg->cm_anon_type = type;
3544 msg->cm_format_type = format_type;
3546 if (recipient != NULL) rcplen = striplt(recipient);
3547 if (recp_cc != NULL) cclen = striplt(recp_cc);
3549 /* Path or Return-Path */
3551 CM_SetField(msg, eMessagePath, my_email, myelen);
3553 else if (!IsEmptyStr(author->fullname)) {
3554 CM_SetField(msg, eMessagePath, author->fullname, strlen(author->fullname));
3556 convert_spaces_to_underscores(msg->cm_fields[eMessagePath]);
3558 blen = snprintf(buf, sizeof buf, "%ld", (long)time(NULL));
3559 CM_SetField(msg, eTimestamp, buf, blen);
3562 FakeAuthor = NewStrBufPlain (fake_name, fnlen);
3565 FakeAuthor = NewStrBufPlain (author->fullname, -1);
3567 StrBufRFC2047encode(&FakeEncAuthor, FakeAuthor);
3568 CM_SetAsFieldSB(msg, eAuthor, &FakeEncAuthor);
3569 FreeStrBuf(&FakeAuthor);
3571 if (!!IsEmptyStr(CCC->room.QRname)) {
3572 if (CCC->room.QRflags & QR_MAILBOX) { /* room */
3573 CM_SetField(msg, eOriginalRoom, &CCC->room.QRname[11], strlen(&CCC->room.QRname[11]));
3576 CM_SetField(msg, eOriginalRoom, CCC->room.QRname, strlen(CCC->room.QRname));
3580 CM_SetField(msg, eNodeName, CtdlGetConfigStr("c_nodename"), strlen(CtdlGetConfigStr("c_nodename")));
3581 CM_SetField(msg, eHumanNode, CtdlGetConfigStr("c_humannode"), strlen(CtdlGetConfigStr("c_humannode")));
3584 CM_SetField(msg, eRecipient, recipient, rcplen);
3587 CM_SetField(msg, eCarbonCopY, recp_cc, cclen);
3591 CM_SetField(msg, erFc822Addr, my_email, myelen);
3593 else if ( (author == &CCC->user) && (!IsEmptyStr(CCC->cs_inet_email)) ) {
3594 CM_SetField(msg, erFc822Addr, CCC->cs_inet_email, strlen(CCC->cs_inet_email));
3597 if (subject != NULL) {
3599 length = striplt(subject);
3605 while ((subject[i] != '\0') &&
3606 (IsAscii = isascii(subject[i]) != 0 ))
3609 CM_SetField(msg, eMsgSubject, subject, subjlen);
3610 else /* ok, we've got utf8 in the string. */
3613 rfc2047Subj = rfc2047encode(subject, length);
3614 CM_SetAsField(msg, eMsgSubject, &rfc2047Subj, strlen(rfc2047Subj));
3621 CM_SetField(msg, eExclusiveID, supplied_euid, euidlen);
3625 CM_SetField(msg, eWeferences, references, reflen);
3628 if (preformatted_text != NULL) {
3629 CM_SetField(msg, eMesageText, preformatted_text, textlen);
3633 MsgBody = CtdlReadMessageBodyBuf(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0, 0);
3634 if (MsgBody != NULL) {
3635 CM_SetAsFieldSB(msg, eMesageText, &MsgBody);
3646 * API function to delete messages which match a set of criteria
3647 * (returns the actual number of messages deleted)
3649 int CtdlDeleteMessages(const char *room_name, /* which room */
3650 long *dmsgnums, /* array of msg numbers to be deleted */
3651 int num_dmsgnums, /* number of msgs to be deleted, or 0 for "any" */
3652 char *content_type /* or "" for any. regular expressions expected. */
3655 struct CitContext *CCC = CC;
3656 struct ctdlroom qrbuf;
3657 struct cdbdata *cdbfr;
3658 long *msglist = NULL;
3659 long *dellist = NULL;
3662 int num_deleted = 0;
3664 struct MetaData smi;
3667 int need_to_free_re = 0;
3669 if (content_type) if (!IsEmptyStr(content_type)) {
3670 regcomp(&re, content_type, 0);
3671 need_to_free_re = 1;
3673 MSG_syslog(LOG_DEBUG, " CtdlDeleteMessages(%s, %d msgs, %s)\n",
3674 room_name, num_dmsgnums, content_type);
3676 /* get room record, obtaining a lock... */
3677 if (CtdlGetRoomLock(&qrbuf, room_name) != 0) {
3678 MSG_syslog(LOG_ERR, " CtdlDeleteMessages(): Room <%s> not found\n",
3680 if (need_to_free_re) regfree(&re);
3681 return (0); /* room not found */
3683 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
3685 if (cdbfr != NULL) {
3686 dellist = malloc(cdbfr->len);
3687 msglist = (long *) cdbfr->ptr;
3688 cdbfr->ptr = NULL; /* CtdlDeleteMessages() now owns this memory */
3689 num_msgs = cdbfr->len / sizeof(long);
3693 int have_contenttype = (content_type != NULL) && !IsEmptyStr(content_type);
3694 int have_delmsgs = (num_dmsgnums == 0) || (dmsgnums == NULL);
3695 int have_more_del = 1;
3697 num_msgs = sort_msglist(msglist, num_msgs);
3698 if (num_dmsgnums > 1)
3699 num_dmsgnums = sort_msglist(dmsgnums, num_dmsgnums);
3702 StrBuf *dbg = NewStrBuf();
3703 for (i = 0; i < num_dmsgnums; i++)
3704 StrBufAppendPrintf(dbg, ", %ld", dmsgnums[i]);
3705 MSG_syslog(LOG_DEBUG, " Deleting before: %s", ChrPtr(dbg));
3710 while ((i < num_msgs) && (have_more_del)) {
3713 /* Set/clear a bit for each criterion */
3715 /* 0 messages in the list or a null list means that we are
3716 * interested in deleting any messages which meet the other criteria.
3719 delete_this |= 0x01;
3722 while ((i < num_msgs) && (msglist[i] < dmsgnums[j])) i++;
3727 if (msglist[i] == dmsgnums[j]) {
3728 delete_this |= 0x01;
3731 have_more_del = (j < num_dmsgnums);
3734 if (have_contenttype) {
3735 GetMetaData(&smi, msglist[i]);
3736 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) == 0) {
3737 delete_this |= 0x02;
3740 delete_this |= 0x02;
3743 /* Delete message only if all bits are set */
3744 if (delete_this == 0x03) {
3745 dellist[num_deleted++] = msglist[i];
3752 StrBuf *dbg = NewStrBuf();
3753 for (i = 0; i < num_deleted; i++)
3754 StrBufAppendPrintf(dbg, ", %ld", dellist[i]);
3755 MSG_syslog(LOG_DEBUG, " Deleting: %s", ChrPtr(dbg));
3759 num_msgs = sort_msglist(msglist, num_msgs);
3760 cdb_store(CDB_MSGLISTS, &qrbuf.QRnumber, (int)sizeof(long),
3761 msglist, (int)(num_msgs * sizeof(long)));
3764 qrbuf.QRhighest = msglist[num_msgs - 1];
3766 qrbuf.QRhighest = 0;
3768 CtdlPutRoomLock(&qrbuf);
3770 /* Go through the messages we pulled out of the index, and decrement
3771 * their reference counts by 1. If this is the only room the message
3772 * was in, the reference count will reach zero and the message will
3773 * automatically be deleted from the database. We do this in a
3774 * separate pass because there might be plug-in hooks getting called,
3775 * and we don't want that happening during an S_ROOMS critical
3779 for (i=0; i<num_deleted; ++i) {
3780 PerformDeleteHooks(qrbuf.QRname, dellist[i]);
3782 AdjRefCountList(dellist, num_deleted, -1);
3784 /* Now free the memory we used, and go away. */
3785 if (msglist != NULL) free(msglist);
3786 if (dellist != NULL) free(dellist);
3787 MSG_syslog(LOG_DEBUG, " %d message(s) deleted.\n", num_deleted);
3788 if (need_to_free_re) regfree(&re);
3789 return (num_deleted);
3796 * GetMetaData() - Get the supplementary record for a message
3798 void GetMetaData(struct MetaData *smibuf, long msgnum)
3801 struct cdbdata *cdbsmi;
3804 memset(smibuf, 0, sizeof(struct MetaData));
3805 smibuf->meta_msgnum = msgnum;
3806 smibuf->meta_refcount = 1; /* Default reference count is 1 */
3808 /* Use the negative of the message number for its supp record index */
3809 TheIndex = (0L - msgnum);
3811 cdbsmi = cdb_fetch(CDB_MSGMAIN, &TheIndex, sizeof(long));
3812 if (cdbsmi == NULL) {
3813 return; /* record not found; go with defaults */
3815 memcpy(smibuf, cdbsmi->ptr,
3816 ((cdbsmi->len > sizeof(struct MetaData)) ?
3817 sizeof(struct MetaData) : cdbsmi->len));
3824 * PutMetaData() - (re)write supplementary record for a message
3826 void PutMetaData(struct MetaData *smibuf)
3830 /* Use the negative of the message number for the metadata db index */
3831 TheIndex = (0L - smibuf->meta_msgnum);
3833 cdb_store(CDB_MSGMAIN,
3834 &TheIndex, (int)sizeof(long),
3835 smibuf, (int)sizeof(struct MetaData));
3840 * AdjRefCount - submit an adjustment to the reference count for a message.
3841 * (These are just queued -- we actually process them later.)
3843 void AdjRefCount(long msgnum, int incr)
3845 struct CitContext *CCC = CC;
3846 struct arcq new_arcq;
3849 MSG_syslog(LOG_DEBUG, "AdjRefCount() msg %ld ref count delta %+d\n", msgnum, incr);
3851 begin_critical_section(S_SUPPMSGMAIN);
3852 if (arcfp == NULL) {
3853 arcfp = fopen(file_arcq, "ab+");
3854 chown(file_arcq, CTDLUID, (-1));
3855 chmod(file_arcq, 0600);
3857 end_critical_section(S_SUPPMSGMAIN);
3859 /* msgnum < 0 means that we're trying to close the file */
3861 MSGM_syslog(LOG_DEBUG, "Closing the AdjRefCount queue file\n");
3862 begin_critical_section(S_SUPPMSGMAIN);
3863 if (arcfp != NULL) {
3867 end_critical_section(S_SUPPMSGMAIN);
3872 * If we can't open the queue, perform the operation synchronously.
3874 if (arcfp == NULL) {
3875 TDAP_AdjRefCount(msgnum, incr);
3879 new_arcq.arcq_msgnum = msgnum;
3880 new_arcq.arcq_delta = incr;
3881 rv = fwrite(&new_arcq, sizeof(struct arcq), 1, arcfp);
3883 MSG_syslog(LOG_EMERG, "Couldn't write Refcount Queue File %s: %s\n",
3892 void AdjRefCountList(long *msgnum, long nmsg, int incr)
3894 struct CitContext *CCC = CC;
3895 long i, the_size, offset;
3896 struct arcq *new_arcq;
3899 MSG_syslog(LOG_DEBUG, "AdjRefCountList() msg %ld ref count delta %+d\n", nmsg, incr);
3901 begin_critical_section(S_SUPPMSGMAIN);
3902 if (arcfp == NULL) {
3903 arcfp = fopen(file_arcq, "ab+");
3904 chown(file_arcq, CTDLUID, (-1));
3905 chmod(file_arcq, 0600);
3907 end_critical_section(S_SUPPMSGMAIN);
3910 * If we can't open the queue, perform the operation synchronously.
3912 if (arcfp == NULL) {
3913 for (i = 0; i < nmsg; i++)
3914 TDAP_AdjRefCount(msgnum[i], incr);
3918 the_size = sizeof(struct arcq) * nmsg;
3919 new_arcq = malloc(the_size);
3920 for (i = 0; i < nmsg; i++) {
3921 new_arcq[i].arcq_msgnum = msgnum[i];
3922 new_arcq[i].arcq_delta = incr;
3926 while ((rv >= 0) && (offset < the_size))
3928 rv = fwrite(new_arcq + offset, 1, the_size - offset, arcfp);
3930 MSG_syslog(LOG_EMERG, "Couldn't write Refcount Queue File %s: %s\n",
3946 * TDAP_ProcessAdjRefCountQueue()
3948 * Process the queue of message count adjustments that was created by calls
3949 * to AdjRefCount() ... by reading the queue and calling TDAP_AdjRefCount()
3950 * for each one. This should be an "off hours" operation.
3952 int TDAP_ProcessAdjRefCountQueue(void)
3954 struct CitContext *CCC = CC;
3955 char file_arcq_temp[PATH_MAX];
3958 struct arcq arcq_rec;
3959 int num_records_processed = 0;
3961 snprintf(file_arcq_temp, sizeof file_arcq_temp, "%s.%04x", file_arcq, rand());
3963 begin_critical_section(S_SUPPMSGMAIN);
3964 if (arcfp != NULL) {
3969 r = link(file_arcq, file_arcq_temp);
3971 MSG_syslog(LOG_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
3972 end_critical_section(S_SUPPMSGMAIN);
3973 return(num_records_processed);
3977 end_critical_section(S_SUPPMSGMAIN);
3979 fp = fopen(file_arcq_temp, "rb");
3981 MSG_syslog(LOG_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
3982 return(num_records_processed);
3985 while (fread(&arcq_rec, sizeof(struct arcq), 1, fp) == 1) {
3986 TDAP_AdjRefCount(arcq_rec.arcq_msgnum, arcq_rec.arcq_delta);
3987 ++num_records_processed;
3991 r = unlink(file_arcq_temp);
3993 MSG_syslog(LOG_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
3996 return(num_records_processed);
4002 * TDAP_AdjRefCount - adjust the reference count for a message.
4003 * This one does it "for real" because it's called by
4004 * the autopurger function that processes the queue
4005 * created by AdjRefCount(). If a message's reference
4006 * count becomes zero, we also delete the message from
4007 * disk and de-index it.
4009 void TDAP_AdjRefCount(long msgnum, int incr)
4011 struct CitContext *CCC = CC;
4013 struct MetaData smi;
4016 /* This is a *tight* critical section; please keep it that way, as
4017 * it may get called while nested in other critical sections.
4018 * Complicating this any further will surely cause deadlock!
4020 begin_critical_section(S_SUPPMSGMAIN);
4021 GetMetaData(&smi, msgnum);
4022 smi.meta_refcount += incr;
4024 end_critical_section(S_SUPPMSGMAIN);
4025 MSG_syslog(LOG_DEBUG, "TDAP_AdjRefCount() msg %ld ref count delta %+d, is now %d\n",
4026 msgnum, incr, smi.meta_refcount
4029 /* If the reference count is now zero, delete the message
4030 * (and its supplementary record as well).
4032 if (smi.meta_refcount == 0) {
4033 MSG_syslog(LOG_DEBUG, "Deleting message <%ld>\n", msgnum);
4035 /* Call delete hooks with NULL room to show it has gone altogether */
4036 PerformDeleteHooks(NULL, msgnum);
4038 /* Remove from message base */
4040 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
4041 cdb_delete(CDB_BIGMSGS, &delnum, (int)sizeof(long));
4043 /* Remove metadata record */
4044 delnum = (0L - msgnum);
4045 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
4051 * Write a generic object to this room
4053 * Note: this could be much more efficient. Right now we use two temporary
4054 * files, and still pull the message into memory as with all others.
4056 void CtdlWriteObject(char *req_room, /* Room to stuff it in */
4057 char *content_type, /* MIME type of this object */
4058 char *raw_message, /* Data to be written */
4059 off_t raw_length, /* Size of raw_message */
4060 struct ctdluser *is_mailbox, /* Mailbox room? */
4061 int is_binary, /* Is encoding necessary? */
4062 int is_unique, /* Del others of this type? */
4063 unsigned int flags /* Internal save flags */
4066 struct CitContext *CCC = CC;
4067 struct ctdlroom qrbuf;
4068 char roomname[ROOMNAMELEN];
4069 struct CtdlMessage *msg;
4070 StrBuf *encoded_message = NULL;
4072 if (is_mailbox != NULL) {
4073 CtdlMailboxName(roomname, sizeof roomname, is_mailbox, req_room);
4076 safestrncpy(roomname, req_room, sizeof(roomname));
4079 MSG_syslog(LOG_DEBUG, "Raw length is %ld\n", (long)raw_length);
4082 encoded_message = NewStrBufPlain(NULL, (size_t) (((raw_length * 134) / 100) + 4096 ) );
4085 encoded_message = NewStrBufPlain(NULL, (size_t)(raw_length + 4096));
4088 StrBufAppendBufPlain(encoded_message, HKEY("Content-type: "), 0);
4089 StrBufAppendBufPlain(encoded_message, content_type, -1, 0);
4090 StrBufAppendBufPlain(encoded_message, HKEY("\n"), 0);
4093 StrBufAppendBufPlain(encoded_message, HKEY("Content-transfer-encoding: base64\n\n"), 0);
4096 StrBufAppendBufPlain(encoded_message, HKEY("Content-transfer-encoding: 7bit\n\n"), 0);
4100 StrBufBase64Append(encoded_message, NULL, raw_message, raw_length, 0);
4103 StrBufAppendBufPlain(encoded_message, raw_message, raw_length, 0);
4106 MSGM_syslog(LOG_DEBUG, "Allocating\n");
4107 msg = malloc(sizeof(struct CtdlMessage));
4108 memset(msg, 0, sizeof(struct CtdlMessage));
4109 msg->cm_magic = CTDLMESSAGE_MAGIC;
4110 msg->cm_anon_type = MES_NORMAL;
4111 msg->cm_format_type = 4;
4112 CM_SetField(msg, eAuthor, CCC->user.fullname, strlen(CCC->user.fullname));
4113 CM_SetField(msg, eOriginalRoom, req_room, strlen(req_room));
4114 CM_SetField(msg, eNodeName, CtdlGetConfigStr("c_nodename"), strlen(CtdlGetConfigStr("c_nodename")));
4115 CM_SetField(msg, eHumanNode, CtdlGetConfigStr("c_humannode"), strlen(CtdlGetConfigStr("c_humannode")));
4116 msg->cm_flags = flags;
4118 CM_SetAsFieldSB(msg, eMesageText, &encoded_message);
4120 /* Create the requested room if we have to. */
4121 if (CtdlGetRoom(&qrbuf, roomname) != 0) {
4122 CtdlCreateRoom(roomname,
4123 ( (is_mailbox != NULL) ? 5 : 3 ),
4124 "", 0, 1, 0, VIEW_BBS);
4126 /* If the caller specified this object as unique, delete all
4127 * other objects of this type that are currently in the room.
4130 MSG_syslog(LOG_DEBUG, "Deleted %d other msgs of this type\n",
4131 CtdlDeleteMessages(roomname, NULL, 0, content_type)
4134 /* Now write the data */
4135 CtdlSubmitMsg(msg, NULL, roomname, 0);
4141 /*****************************************************************************/
4142 /* MODULE INITIALIZATION STUFF */
4143 /*****************************************************************************/
4144 void SetMessageDebugEnabled(const int n)
4146 MessageDebugEnabled = n;
4148 CTDL_MODULE_INIT(msgbase)
4151 FillMsgKeyLookupTable();
4152 CtdlRegisterDebugFlagHook(HKEY("messages"), SetMessageDebugEnabled, &MessageDebugEnabled);
4155 /* return our Subversion id for the Log */