2 * Implements the message store.
4 * Copyright (c) 1987-2012 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
26 # include <sys/time.h>
39 #include <sys/types.h>
44 #include <libcitadel.h>
47 #include "serv_extensions.h"
51 #include "sysdep_decls.h"
52 #include "citserver.h"
59 #include "internet_addressing.h"
60 #include "euidindex.h"
61 #include "journaling.h"
62 #include "citadel_dirs.h"
63 #include "clientsocket.h"
66 #include "ctdl_module.h"
69 struct addresses_to_be_filed *atbf = NULL;
71 /* This temp file holds the queue of operations for AdjRefCount() */
72 static FILE *arcfp = NULL;
73 void AdjRefCountList(long *msgnum, long nmsg, int incr);
75 int MessageDebugEnabled = 0;
78 * These are the four-character field headers we use when outputting
79 * messages in Citadel format (as opposed to RFC822 format).
82 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
83 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
84 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
85 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
86 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
87 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
88 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
89 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
119 eMsgField FieldOrder[] = {
120 /* Important fields */
131 /* Semi-important fields */
137 /* G is not used yet, may become virus signature*/
140 /* Q is not used yet */
143 /* X is not used yet */
144 /* Z is not used yet */
151 /* Message text (MUST be last) */
153 /* Not saved to disk:
158 static const long NDiskFields = sizeof(FieldOrder) / sizeof(eMsgField);
160 int CM_IsEmpty(struct CtdlMessage *Msg, eMsgField which)
162 return !((Msg->cm_fields[which] != NULL) &&
163 (Msg->cm_fields[which][0] != '\0'));
166 void CM_SetField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
168 if (Msg->cm_fields[which] != NULL)
169 free (Msg->cm_fields[which]);
170 Msg->cm_fields[which] = malloc(length + 1);
171 memcpy(Msg->cm_fields[which], buf, length);
172 Msg->cm_fields[which][length] = '\0';
175 void CM_SetFieldLONG(struct CtdlMessage *Msg, eMsgField which, long lvalue)
179 len = snprintf(buf, sizeof(buf), "%ld", lvalue);
180 CM_SetField(Msg, which, buf, len);
182 void CM_CutFieldAt(struct CtdlMessage *Msg, eMsgField WhichToCut, long maxlen)
184 if (Msg->cm_fields[WhichToCut] == NULL)
187 if (strlen(Msg->cm_fields[WhichToCut]) > maxlen)
188 Msg->cm_fields[WhichToCut][maxlen] = '\0';
191 void CM_FlushField(struct CtdlMessage *Msg, eMsgField which)
193 if (Msg->cm_fields[which] != NULL)
194 free (Msg->cm_fields[which]);
195 Msg->cm_fields[which] = NULL;
198 void CM_CopyField(struct CtdlMessage *Msg, eMsgField WhichToPutTo, eMsgField WhichtToCopy)
201 if (Msg->cm_fields[WhichToPutTo] != NULL)
202 free (Msg->cm_fields[WhichToPutTo]);
204 if (Msg->cm_fields[WhichtToCopy] != NULL)
206 len = strlen(Msg->cm_fields[WhichtToCopy]);
207 Msg->cm_fields[WhichToPutTo] = malloc(len + 1);
208 memcpy(Msg->cm_fields[WhichToPutTo], Msg->cm_fields[WhichToPutTo], len);
209 Msg->cm_fields[WhichToPutTo][len] = '\0';
212 Msg->cm_fields[WhichToPutTo] = NULL;
216 void CM_PrependToField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
218 if (Msg->cm_fields[which] != NULL) {
223 oldmsgsize = strlen(Msg->cm_fields[which]) + 1;
224 newmsgsize = length + oldmsgsize;
226 new = malloc(newmsgsize);
227 memcpy(new, buf, length);
228 memcpy(new + length, Msg->cm_fields[which], oldmsgsize);
229 free(Msg->cm_fields[which]);
230 Msg->cm_fields[which] = new;
233 Msg->cm_fields[which] = malloc(length + 1);
234 memcpy(Msg->cm_fields[which], buf, length);
235 Msg->cm_fields[which][length] = '\0';
239 void CM_SetAsField(struct CtdlMessage *Msg, eMsgField which, char **buf, long length)
241 if (Msg->cm_fields[which] != NULL)
242 free (Msg->cm_fields[which]);
244 Msg->cm_fields[which] = *buf;
248 void CM_SetAsFieldSB(struct CtdlMessage *Msg, eMsgField which, StrBuf **buf)
250 if (Msg->cm_fields[which] != NULL)
251 free (Msg->cm_fields[which]);
253 Msg->cm_fields[which] = SmashStrBuf(buf);
256 void CM_GetAsField(struct CtdlMessage *Msg, eMsgField which, char **ret, long *retlen)
258 if (Msg->cm_fields[which] != NULL)
260 *retlen = strlen(Msg->cm_fields[which]);
261 *ret = Msg->cm_fields[which];
262 Msg->cm_fields[which] = NULL;
272 * Returns 1 if the supplied pointer points to a valid Citadel message.
273 * If the pointer is NULL or the magic number check fails, returns 0.
275 int CM_IsValidMsg(struct CtdlMessage *msg) {
278 if ((msg->cm_magic) != CTDLMESSAGE_MAGIC) {
279 struct CitContext *CCC = CC;
280 MSGM_syslog(LOG_WARNING, "CM_IsValidMsg() -- self-check failed\n");
286 void CM_FreeContents(struct CtdlMessage *msg)
290 for (i = 0; i < 256; ++i)
291 if (msg->cm_fields[i] != NULL) {
292 free(msg->cm_fields[i]);
295 msg->cm_magic = 0; /* just in case */
298 * 'Destructor' for struct CtdlMessage
300 void CM_Free(struct CtdlMessage *msg)
302 if (CM_IsValidMsg(msg) == 0)
304 if (msg != NULL) free (msg);
307 CM_FreeContents(msg);
311 int CM_DupField(eMsgField i, struct CtdlMessage *OrgMsg, struct CtdlMessage *NewMsg)
314 len = strlen(OrgMsg->cm_fields[i]);
315 NewMsg->cm_fields[i] = malloc(len + 1);
316 if (NewMsg->cm_fields[i] == NULL)
318 memcpy(NewMsg->cm_fields[i], OrgMsg->cm_fields[i], len);
319 NewMsg->cm_fields[i][len] = '\0';
323 struct CtdlMessage * CM_Duplicate(struct CtdlMessage *OrgMsg)
326 struct CtdlMessage *NewMsg;
328 if (CM_IsValidMsg(OrgMsg) == 0)
330 NewMsg = (struct CtdlMessage *)malloc(sizeof(struct CtdlMessage));
334 memcpy(NewMsg, OrgMsg, sizeof(struct CtdlMessage));
336 memset(&NewMsg->cm_fields, 0, sizeof(char*) * 256);
338 for (i = 0; i < 256; ++i)
340 if (OrgMsg->cm_fields[i] != NULL)
342 if (!CM_DupField(i, OrgMsg, NewMsg))
356 * Back end for the MSGS command: output message number only.
358 void simple_listing(long msgnum, void *userdata)
360 cprintf("%ld\n", msgnum);
366 * Back end for the MSGS command: output header summary.
368 void headers_listing(long msgnum, void *userdata)
370 struct CtdlMessage *msg;
372 msg = CtdlFetchMessage(msgnum, 0);
374 cprintf("%ld|0|||||\n", msgnum);
378 cprintf("%ld|%s|%s|%s|%s|%s|\n",
380 (!CM_IsEmpty(msg, eTimestamp) ? msg->cm_fields[eTimestamp] : "0"),
381 (!CM_IsEmpty(msg, eAuthor) ? msg->cm_fields[eAuthor] : ""),
382 (!CM_IsEmpty(msg, eNodeName) ? msg->cm_fields[eNodeName] : ""),
383 (!CM_IsEmpty(msg, erFc822Addr) ? msg->cm_fields[erFc822Addr] : ""),
384 (!CM_IsEmpty(msg, eMsgSubject) ? msg->cm_fields[eMsgSubject] : "")
390 * Back end for the MSGS command: output EUID header.
392 void headers_euid(long msgnum, void *userdata)
394 struct CtdlMessage *msg;
396 msg = CtdlFetchMessage(msgnum, 0);
398 cprintf("%ld||\n", msgnum);
402 cprintf("%ld|%s|%s\n",
404 (!CM_IsEmpty(msg, eExclusiveID) ? msg->cm_fields[eExclusiveID] : ""),
405 (!CM_IsEmpty(msg, eTimestamp) ? msg->cm_fields[eTimestamp] : "0"));
413 /* Determine if a given message matches the fields in a message template.
414 * Return 0 for a successful match.
416 int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template) {
419 /* If there aren't any fields in the template, all messages will
422 if (template == NULL) return(0);
424 /* Null messages are bogus. */
425 if (msg == NULL) return(1);
427 for (i='A'; i<='Z'; ++i) {
428 if (template->cm_fields[i] != NULL) {
429 if (msg->cm_fields[i] == NULL) {
430 /* Considered equal if temmplate is empty string */
431 if (IsEmptyStr(template->cm_fields[i])) continue;
434 if (strcasecmp(msg->cm_fields[i],
435 template->cm_fields[i])) return 1;
439 /* All compares succeeded: we have a match! */
446 * Retrieve the "seen" message list for the current room.
448 void CtdlGetSeen(char *buf, int which_set) {
449 struct CitContext *CCC = CC;
452 /* Learn about the user and room in question */
453 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
455 if (which_set == ctdlsetseen_seen)
456 safestrncpy(buf, vbuf.v_seen, SIZ);
457 if (which_set == ctdlsetseen_answered)
458 safestrncpy(buf, vbuf.v_answered, SIZ);
464 * Manipulate the "seen msgs" string (or other message set strings)
466 void CtdlSetSeen(long *target_msgnums, int num_target_msgnums,
467 int target_setting, int which_set,
468 struct ctdluser *which_user, struct ctdlroom *which_room) {
469 struct CitContext *CCC = CC;
470 struct cdbdata *cdbfr;
475 long hi = (-1L); /// TODO: we just write here. y?
484 char *is_set; /* actually an array of booleans */
486 /* Don't bother doing *anything* if we were passed a list of zero messages */
487 if (num_target_msgnums < 1) {
491 /* If no room was specified, we go with the current room. */
493 which_room = &CCC->room;
496 /* If no user was specified, we go with the current user. */
498 which_user = &CCC->user;
501 MSG_syslog(LOG_DEBUG, "CtdlSetSeen(%d msgs starting with %ld, %s, %d) in <%s>\n",
502 num_target_msgnums, target_msgnums[0],
503 (target_setting ? "SET" : "CLEAR"),
507 /* Learn about the user and room in question */
508 CtdlGetRelationship(&vbuf, which_user, which_room);
510 /* Load the message list */
511 cdbfr = cdb_fetch(CDB_MSGLISTS, &which_room->QRnumber, sizeof(long));
513 msglist = (long *) cdbfr->ptr;
514 cdbfr->ptr = NULL; /* CtdlSetSeen() now owns this memory */
515 num_msgs = cdbfr->len / sizeof(long);
518 return; /* No messages at all? No further action. */
521 is_set = malloc(num_msgs * sizeof(char));
522 memset(is_set, 0, (num_msgs * sizeof(char)) );
524 /* Decide which message set we're manipulating */
526 case ctdlsetseen_seen:
527 vset = NewStrBufPlain(vbuf.v_seen, -1);
529 case ctdlsetseen_answered:
530 vset = NewStrBufPlain(vbuf.v_answered, -1);
537 #if 0 /* This is a special diagnostic section. Do not allow it to run during normal operation. */
538 MSG_syslog(LOG_DEBUG, "There are %d messages in the room.\n", num_msgs);
539 for (i=0; i<num_msgs; ++i) {
540 if ((i > 0) && (msglist[i] <= msglist[i-1])) abort();
542 MSG_syslog(LOG_DEBUG, "We are twiddling %d of them.\n", num_target_msgnums);
543 for (k=0; k<num_target_msgnums; ++k) {
544 if ((k > 0) && (target_msgnums[k] <= target_msgnums[k-1])) abort();
548 MSG_syslog(LOG_DEBUG, "before update: %s\n", ChrPtr(vset));
550 /* Translate the existing sequence set into an array of booleans */
551 setstr = NewStrBuf();
555 while (StrBufExtract_NextToken(setstr, vset, &pvset, ',') >= 0) {
557 StrBufExtract_token(lostr, setstr, 0, ':');
558 if (StrBufNum_tokens(setstr, ':') >= 2) {
559 StrBufExtract_token(histr, setstr, 1, ':');
563 StrBufAppendBuf(histr, lostr, 0);
566 if (!strcmp(ChrPtr(histr), "*")) {
573 for (i = 0; i < num_msgs; ++i) {
574 if ((msglist[i] >= lo) && (msglist[i] <= hi)) {
584 /* Now translate the array of booleans back into a sequence set */
590 for (i=0; i<num_msgs; ++i) {
594 for (k=0; k<num_target_msgnums; ++k) {
595 if (msglist[i] == target_msgnums[k]) {
596 is_seen = target_setting;
600 if ((was_seen == 0) && (is_seen == 1)) {
603 else if ((was_seen == 1) && (is_seen == 0)) {
606 if (StrLength(vset) > 0) {
607 StrBufAppendBufPlain(vset, HKEY(","), 0);
610 StrBufAppendPrintf(vset, "%ld", hi);
613 StrBufAppendPrintf(vset, "%ld:%ld", lo, hi);
617 if ((is_seen) && (i == num_msgs - 1)) {
618 if (StrLength(vset) > 0) {
619 StrBufAppendBufPlain(vset, HKEY(","), 0);
621 if ((i==0) || (was_seen == 0)) {
622 StrBufAppendPrintf(vset, "%ld", msglist[i]);
625 StrBufAppendPrintf(vset, "%ld:%ld", lo, msglist[i]);
633 * We will have to stuff this string back into a 4096 byte buffer, so if it's
634 * larger than that now, truncate it by removing tokens from the beginning.
635 * The limit of 100 iterations is there to prevent an infinite loop in case
636 * something unexpected happens.
638 int number_of_truncations = 0;
639 while ( (StrLength(vset) > SIZ) && (number_of_truncations < 100) ) {
640 StrBufRemove_token(vset, 0, ',');
641 ++number_of_truncations;
645 * If we're truncating the sequence set of messages marked with the 'seen' flag,
646 * we want the earliest messages (the truncated ones) to be marked, not unmarked.
647 * Otherwise messages at the beginning will suddenly appear to be 'unseen'.
649 if ( (which_set == ctdlsetseen_seen) && (number_of_truncations > 0) ) {
651 first_tok = NewStrBuf();
652 StrBufExtract_token(first_tok, vset, 0, ',');
653 StrBufRemove_token(vset, 0, ',');
655 if (StrBufNum_tokens(first_tok, ':') > 1) {
656 StrBufRemove_token(first_tok, 0, ':');
660 new_set = NewStrBuf();
661 StrBufAppendBufPlain(new_set, HKEY("1:"), 0);
662 StrBufAppendBuf(new_set, first_tok, 0);
663 StrBufAppendBufPlain(new_set, HKEY(":"), 0);
664 StrBufAppendBuf(new_set, vset, 0);
667 FreeStrBuf(&first_tok);
671 MSG_syslog(LOG_DEBUG, " after update: %s\n", ChrPtr(vset));
673 /* Decide which message set we're manipulating */
675 case ctdlsetseen_seen:
676 safestrncpy(vbuf.v_seen, ChrPtr(vset), sizeof vbuf.v_seen);
678 case ctdlsetseen_answered:
679 safestrncpy(vbuf.v_answered, ChrPtr(vset), sizeof vbuf.v_answered);
685 CtdlSetRelationship(&vbuf, which_user, which_room);
691 * API function to perform an operation for each qualifying message in the
692 * current room. (Returns the number of messages processed.)
694 int CtdlForEachMessage(int mode, long ref, char *search_string,
696 struct CtdlMessage *compare,
697 ForEachMsgCallback CallBack,
700 struct CitContext *CCC = CC;
703 struct cdbdata *cdbfr;
704 long *msglist = NULL;
706 int num_processed = 0;
709 struct CtdlMessage *msg = NULL;
712 int printed_lastold = 0;
713 int num_search_msgs = 0;
714 long *search_msgs = NULL;
716 int need_to_free_re = 0;
719 if ((content_type) && (!IsEmptyStr(content_type))) {
720 regcomp(&re, content_type, 0);
724 /* Learn about the user and room in question */
725 if (server_shutting_down) {
726 if (need_to_free_re) regfree(&re);
729 CtdlGetUser(&CCC->user, CCC->curr_user);
731 if (server_shutting_down) {
732 if (need_to_free_re) regfree(&re);
735 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
737 if (server_shutting_down) {
738 if (need_to_free_re) regfree(&re);
742 /* Load the message list */
743 cdbfr = cdb_fetch(CDB_MSGLISTS, &CCC->room.QRnumber, sizeof(long));
745 if (need_to_free_re) regfree(&re);
746 return 0; /* No messages at all? No further action. */
749 msglist = (long *) cdbfr->ptr;
750 num_msgs = cdbfr->len / sizeof(long);
752 cdbfr->ptr = NULL; /* clear this so that cdb_free() doesn't free it */
753 cdb_free(cdbfr); /* we own this memory now */
756 * Now begin the traversal.
758 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
760 /* If the caller is looking for a specific MIME type, filter
761 * out all messages which are not of the type requested.
763 if ((content_type != NULL) && (!IsEmptyStr(content_type))) {
765 /* This call to GetMetaData() sits inside this loop
766 * so that we only do the extra database read per msg
767 * if we need to. Doing the extra read all the time
768 * really kills the server. If we ever need to use
769 * metadata for another search criterion, we need to
770 * move the read somewhere else -- but still be smart
771 * enough to only do the read if the caller has
772 * specified something that will need it.
774 if (server_shutting_down) {
775 if (need_to_free_re) regfree(&re);
779 GetMetaData(&smi, msglist[a]);
781 /* if (strcasecmp(smi.meta_content_type, content_type)) { old non-regex way */
782 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) != 0) {
788 num_msgs = sort_msglist(msglist, num_msgs);
790 /* If a template was supplied, filter out the messages which
791 * don't match. (This could induce some delays!)
794 if (compare != NULL) {
795 for (a = 0; a < num_msgs; ++a) {
796 if (server_shutting_down) {
797 if (need_to_free_re) regfree(&re);
801 msg = CtdlFetchMessage(msglist[a], 1);
803 if (CtdlMsgCmp(msg, compare)) {
812 /* If a search string was specified, get a message list from
813 * the full text index and remove messages which aren't on both
817 * Since the lists are sorted and strictly ascending, and the
818 * output list is guaranteed to be shorter than or equal to the
819 * input list, we overwrite the bottom of the input list. This
820 * eliminates the need to memmove big chunks of the list over and
823 if ( (num_msgs > 0) && (mode == MSGS_SEARCH) && (search_string) ) {
825 /* Call search module via hook mechanism.
826 * NULL means use any search function available.
827 * otherwise replace with a char * to name of search routine
829 CtdlModuleDoSearch(&num_search_msgs, &search_msgs, search_string, "fulltext");
831 if (num_search_msgs > 0) {
835 orig_num_msgs = num_msgs;
837 for (i=0; i<orig_num_msgs; ++i) {
838 for (j=0; j<num_search_msgs; ++j) {
839 if (msglist[i] == search_msgs[j]) {
840 msglist[num_msgs++] = msglist[i];
846 num_msgs = 0; /* No messages qualify */
848 if (search_msgs != NULL) free(search_msgs);
850 /* Now that we've purged messages which don't contain the search
851 * string, treat a MSGS_SEARCH just like a MSGS_ALL from this
858 * Now iterate through the message list, according to the
859 * criteria supplied by the caller.
862 for (a = 0; a < num_msgs; ++a) {
863 if (server_shutting_down) {
864 if (need_to_free_re) regfree(&re);
866 return num_processed;
868 thismsg = msglist[a];
869 if (mode == MSGS_ALL) {
873 is_seen = is_msg_in_sequence_set(
874 vbuf.v_seen, thismsg);
875 if (is_seen) lastold = thismsg;
881 || ((mode == MSGS_OLD) && (is_seen))
882 || ((mode == MSGS_NEW) && (!is_seen))
883 || ((mode == MSGS_LAST) && (a >= (num_msgs - ref)))
884 || ((mode == MSGS_FIRST) && (a < ref))
885 || ((mode == MSGS_GT) && (thismsg > ref))
886 || ((mode == MSGS_LT) && (thismsg < ref))
887 || ((mode == MSGS_EQ) && (thismsg == ref))
890 if ((mode == MSGS_NEW) && (CCC->user.flags & US_LASTOLD) && (lastold > 0L) && (printed_lastold == 0) && (!is_seen)) {
892 CallBack(lastold, userdata);
896 if (CallBack) CallBack(thismsg, userdata);
900 if (need_to_free_re) regfree(&re);
903 * We cache the most recent msglist in order to do security checks later
905 if (CCC->client_socket > 0) {
906 if (CCC->cached_msglist != NULL) {
907 free(CCC->cached_msglist);
909 CCC->cached_msglist = msglist;
910 CCC->cached_num_msgs = num_msgs;
916 return num_processed;
922 * cmd_msgs() - get list of message #'s in this room
923 * implements the MSGS server command using CtdlForEachMessage()
925 void cmd_msgs(char *cmdbuf)
934 int with_template = 0;
935 struct CtdlMessage *template = NULL;
936 char search_string[1024];
937 ForEachMsgCallback CallBack;
939 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
941 extract_token(which, cmdbuf, 0, '|', sizeof which);
942 cm_ref = extract_int(cmdbuf, 1);
943 extract_token(search_string, cmdbuf, 1, '|', sizeof search_string);
944 with_template = extract_int(cmdbuf, 2);
945 switch (extract_int(cmdbuf, 3))
949 CallBack = simple_listing;
952 CallBack = headers_listing;
955 CallBack = headers_euid;
960 if (!strncasecmp(which, "OLD", 3))
962 else if (!strncasecmp(which, "NEW", 3))
964 else if (!strncasecmp(which, "FIRST", 5))
966 else if (!strncasecmp(which, "LAST", 4))
968 else if (!strncasecmp(which, "GT", 2))
970 else if (!strncasecmp(which, "LT", 2))
972 else if (!strncasecmp(which, "SEARCH", 6))
977 if ( (mode == MSGS_SEARCH) && (!config.c_enable_fulltext) ) {
978 cprintf("%d Full text index is not enabled on this server.\n",
979 ERROR + CMD_NOT_SUPPORTED);
985 cprintf("%d Send template then receive message list\n",
987 template = (struct CtdlMessage *)
988 malloc(sizeof(struct CtdlMessage));
989 memset(template, 0, sizeof(struct CtdlMessage));
990 template->cm_magic = CTDLMESSAGE_MAGIC;
991 template->cm_anon_type = MES_NORMAL;
993 while(client_getln(buf, sizeof buf) >= 0 && strcmp(buf,"000")) {
995 extract_token(tfield, buf, 0, '|', sizeof tfield);
996 tValueLen = extract_token(tvalue, buf, 1, '|', sizeof tvalue);
997 for (i='A'; i<='Z'; ++i) if (msgkeys[i]!=NULL) {
998 if (!strcasecmp(tfield, msgkeys[i])) {
999 CM_SetField(template, i, tvalue, tValueLen);
1006 cprintf("%d \n", LISTING_FOLLOWS);
1009 CtdlForEachMessage(mode,
1010 ( (mode == MSGS_SEARCH) ? 0 : cm_ref ),
1011 ( (mode == MSGS_SEARCH) ? search_string : NULL ),
1016 if (template != NULL) CM_Free(template);
1022 * memfmout() - Citadel text formatter and paginator.
1023 * Although the original purpose of this routine was to format
1024 * text to the reader's screen width, all we're really using it
1025 * for here is to format text out to 80 columns before sending it
1026 * to the client. The client software may reformat it again.
1029 char *mptr, /* where are we going to get our text from? */
1030 const char *nl /* string to terminate lines with */
1032 struct CitContext *CCC = CC;
1034 unsigned char ch = 0;
1041 while (ch=*(mptr++), ch != 0) {
1044 if (client_write(outbuf, len) == -1)
1046 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
1050 if (client_write(nl, nllen) == -1)
1052 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
1057 else if (ch == '\r') {
1058 /* Ignore carriage returns. Newlines are always LF or CRLF but never CR. */
1060 else if (isspace(ch)) {
1061 if (column > 72) { /* Beyond 72 columns, break on the next space */
1062 if (client_write(outbuf, len) == -1)
1064 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
1068 if (client_write(nl, nllen) == -1)
1070 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
1083 if (column > 1000) { /* Beyond 1000 columns, break anywhere */
1084 if (client_write(outbuf, len) == -1)
1086 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
1090 if (client_write(nl, nllen) == -1)
1092 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
1100 if (client_write(outbuf, len) == -1)
1102 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
1106 client_write(nl, nllen);
1114 * Callback function for mime parser that simply lists the part
1116 void list_this_part(char *name, char *filename, char *partnum, char *disp,
1117 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1118 char *cbid, void *cbuserdata)
1122 ma = (struct ma_info *)cbuserdata;
1123 if (ma->is_ma == 0) {
1124 cprintf("part=%s|%s|%s|%s|%s|%ld|%s|%s\n",
1137 * Callback function for multipart prefix
1139 void list_this_pref(char *name, char *filename, char *partnum, char *disp,
1140 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1141 char *cbid, void *cbuserdata)
1145 ma = (struct ma_info *)cbuserdata;
1146 if (!strcasecmp(cbtype, "multipart/alternative")) {
1150 if (ma->is_ma == 0) {
1151 cprintf("pref=%s|%s\n", partnum, cbtype);
1156 * Callback function for multipart sufffix
1158 void list_this_suff(char *name, char *filename, char *partnum, char *disp,
1159 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1160 char *cbid, void *cbuserdata)
1164 ma = (struct ma_info *)cbuserdata;
1165 if (ma->is_ma == 0) {
1166 cprintf("suff=%s|%s\n", partnum, cbtype);
1168 if (!strcasecmp(cbtype, "multipart/alternative")) {
1175 * Callback function for mime parser that opens a section for downloading
1177 void mime_download(char *name, char *filename, char *partnum, char *disp,
1178 void *content, char *cbtype, char *cbcharset, size_t length,
1179 char *encoding, char *cbid, void *cbuserdata)
1182 CitContext *CCC = MyContext();
1184 /* Silently go away if there's already a download open. */
1185 if (CCC->download_fp != NULL)
1189 (!IsEmptyStr(partnum) && (!strcasecmp(CCC->download_desired_section, partnum)))
1190 || (!IsEmptyStr(cbid) && (!strcasecmp(CCC->download_desired_section, cbid)))
1192 CCC->download_fp = tmpfile();
1193 if (CCC->download_fp == NULL) {
1194 MSG_syslog(LOG_EMERG, "mime_download(): Couldn't write: %s\n",
1196 cprintf("%d cannot open temporary file: %s\n",
1197 ERROR + INTERNAL_ERROR, strerror(errno));
1201 rv = fwrite(content, length, 1, CCC->download_fp);
1203 MSG_syslog(LOG_EMERG, "mime_download(): Couldn't write: %s\n",
1205 cprintf("%d unable to write tempfile.\n",
1207 fclose(CCC->download_fp);
1208 CCC->download_fp = NULL;
1211 fflush(CCC->download_fp);
1212 rewind(CCC->download_fp);
1214 OpenCmdResult(filename, cbtype);
1221 * Callback function for mime parser that outputs a section all at once.
1222 * We can specify the desired section by part number *or* content-id.
1224 void mime_spew_section(char *name, char *filename, char *partnum, char *disp,
1225 void *content, char *cbtype, char *cbcharset, size_t length,
1226 char *encoding, char *cbid, void *cbuserdata)
1228 int *found_it = (int *)cbuserdata;
1231 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
1232 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
1235 cprintf("%d %d|-1|%s|%s|%s\n",
1242 client_write(content, length);
1248 * Load a message from disk into memory.
1249 * This is used by CtdlOutputMsg() and other fetch functions.
1251 * NOTE: Caller is responsible for freeing the returned CtdlMessage struct
1252 * using the CtdlMessageFree() function.
1254 struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body)
1256 struct CitContext *CCC = CC;
1257 struct cdbdata *dmsgtext;
1258 struct CtdlMessage *ret = NULL;
1262 cit_uint8_t field_header;
1264 MSG_syslog(LOG_DEBUG, "CtdlFetchMessage(%ld, %d)\n", msgnum, with_body);
1265 dmsgtext = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
1266 if (dmsgtext == NULL) {
1267 MSG_syslog(LOG_ERR, "CtdlFetchMessage(%ld, %d) Failed!\n", msgnum, with_body);
1270 mptr = dmsgtext->ptr;
1271 upper_bound = mptr + dmsgtext->len;
1273 /* Parse the three bytes that begin EVERY message on disk.
1274 * The first is always 0xFF, the on-disk magic number.
1275 * The second is the anonymous/public type byte.
1276 * The third is the format type byte (vari, fixed, or MIME).
1280 MSG_syslog(LOG_ERR, "Message %ld appears to be corrupted.\n", msgnum);
1284 ret = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1285 memset(ret, 0, sizeof(struct CtdlMessage));
1287 ret->cm_magic = CTDLMESSAGE_MAGIC;
1288 ret->cm_anon_type = *mptr++; /* Anon type byte */
1289 ret->cm_format_type = *mptr++; /* Format type byte */
1292 * The rest is zero or more arbitrary fields. Load them in.
1293 * We're done when we encounter either a zero-length field or
1294 * have just processed the 'M' (message text) field.
1298 if (mptr >= upper_bound) {
1301 field_header = *mptr++;
1303 CM_SetField(ret, field_header, mptr, len);
1305 mptr += len + 1; /* advance to next field */
1307 } while ((mptr < upper_bound) && (field_header != 'M'));
1311 /* Always make sure there's something in the msg text field. If
1312 * it's NULL, the message text is most likely stored separately,
1313 * so go ahead and fetch that. Failing that, just set a dummy
1314 * body so other code doesn't barf.
1316 if ( (CM_IsEmpty(ret, eMesageText)) && (with_body) ) {
1317 dmsgtext = cdb_fetch(CDB_BIGMSGS, &msgnum, sizeof(long));
1318 if (dmsgtext != NULL) {
1319 CM_SetAsField(ret, eMesageText, &dmsgtext->ptr, dmsgtext->len);
1323 if (CM_IsEmpty(ret, eMesageText)) {
1324 CM_SetField(ret, eMesageText, HKEY("\r\n\r\n (no text)\r\n"));
1327 /* Perform "before read" hooks (aborting if any return nonzero) */
1328 if (PerformMessageHooks(ret, EVT_BEFOREREAD) > 0) {
1339 * Pre callback function for multipart/alternative
1341 * NOTE: this differs from the standard behavior for a reason. Normally when
1342 * displaying multipart/alternative you want to show the _last_ usable
1343 * format in the message. Here we show the _first_ one, because it's
1344 * usually text/plain. Since this set of functions is designed for text
1345 * output to non-MIME-aware clients, this is the desired behavior.
1348 void fixed_output_pre(char *name, char *filename, char *partnum, char *disp,
1349 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1350 char *cbid, void *cbuserdata)
1352 struct CitContext *CCC = CC;
1355 ma = (struct ma_info *)cbuserdata;
1356 MSG_syslog(LOG_DEBUG, "fixed_output_pre() type=<%s>\n", cbtype);
1357 if (!strcasecmp(cbtype, "multipart/alternative")) {
1361 if (!strcasecmp(cbtype, "message/rfc822")) {
1367 * Post callback function for multipart/alternative
1369 void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
1370 void *content, char *cbtype, char *cbcharset, size_t length,
1371 char *encoding, char *cbid, void *cbuserdata)
1373 struct CitContext *CCC = CC;
1376 ma = (struct ma_info *)cbuserdata;
1377 MSG_syslog(LOG_DEBUG, "fixed_output_post() type=<%s>\n", cbtype);
1378 if (!strcasecmp(cbtype, "multipart/alternative")) {
1382 if (!strcasecmp(cbtype, "message/rfc822")) {
1388 * Inline callback function for mime parser that wants to display text
1390 void fixed_output(char *name, char *filename, char *partnum, char *disp,
1391 void *content, char *cbtype, char *cbcharset, size_t length,
1392 char *encoding, char *cbid, void *cbuserdata)
1394 struct CitContext *CCC = CC;
1400 ma = (struct ma_info *)cbuserdata;
1402 MSG_syslog(LOG_DEBUG,
1403 "fixed_output() part %s: %s (%s) (%ld bytes)\n",
1404 partnum, filename, cbtype, (long)length);
1407 * If we're in the middle of a multipart/alternative scope and
1408 * we've already printed another section, skip this one.
1410 if ( (ma->is_ma) && (ma->did_print) ) {
1411 MSG_syslog(LOG_DEBUG, "Skipping part %s (%s)\n", partnum, cbtype);
1416 if ( (!strcasecmp(cbtype, "text/plain"))
1417 || (IsEmptyStr(cbtype)) ) {
1420 client_write(wptr, length);
1421 if (wptr[length-1] != '\n') {
1428 if (!strcasecmp(cbtype, "text/html")) {
1429 ptr = html_to_ascii(content, length, 80, 0);
1431 client_write(ptr, wlen);
1432 if ((wlen > 0) && (ptr[wlen-1] != '\n')) {
1439 if (ma->use_fo_hooks) {
1440 if (PerformFixedOutputHooks(cbtype, content, length)) {
1441 /* above function returns nonzero if it handled the part */
1446 if (strncasecmp(cbtype, "multipart/", 10)) {
1447 cprintf("Part %s: %s (%s) (%ld bytes)\r\n",
1448 partnum, filename, cbtype, (long)length);
1454 * The client is elegant and sophisticated and wants to be choosy about
1455 * MIME content types, so figure out which multipart/alternative part
1456 * we're going to send.
1458 * We use a system of weights. When we find a part that matches one of the
1459 * MIME types we've declared as preferential, we can store it in ma->chosen_part
1460 * and then set ma->chosen_pref to that MIME type's position in our preference
1461 * list. If we then hit another match, we only replace the first match if
1462 * the preference value is lower.
1464 void choose_preferred(char *name, char *filename, char *partnum, char *disp,
1465 void *content, char *cbtype, char *cbcharset, size_t length,
1466 char *encoding, char *cbid, void *cbuserdata)
1468 struct CitContext *CCC = CC;
1473 ma = (struct ma_info *)cbuserdata;
1475 // NOTE: REMOVING THIS CONDITIONAL FIXES BUG 220
1476 // http://bugzilla.citadel.org/show_bug.cgi?id=220
1477 // I don't know if there are any side effects! Please TEST TEST TEST
1478 //if (ma->is_ma > 0) {
1480 for (i=0; i<num_tokens(CCC->preferred_formats, '|'); ++i) {
1481 extract_token(buf, CCC->preferred_formats, i, '|', sizeof buf);
1482 if ( (!strcasecmp(buf, cbtype)) && (!ma->freeze) ) {
1483 if (i < ma->chosen_pref) {
1484 MSG_syslog(LOG_DEBUG, "Setting chosen part: <%s>\n", partnum);
1485 safestrncpy(ma->chosen_part, partnum, sizeof ma->chosen_part);
1486 ma->chosen_pref = i;
1493 * Now that we've chosen our preferred part, output it.
1495 void output_preferred(char *name,
1507 struct CitContext *CCC = CC;
1510 int add_newline = 0;
1513 char *decoded = NULL;
1514 size_t bytes_decoded;
1517 ma = (struct ma_info *)cbuserdata;
1519 /* This is not the MIME part you're looking for... */
1520 if (strcasecmp(partnum, ma->chosen_part)) return;
1522 /* If the content-type of this part is in our preferred formats
1523 * list, we can simply output it verbatim.
1525 for (i=0; i<num_tokens(CCC->preferred_formats, '|'); ++i) {
1526 extract_token(buf, CCC->preferred_formats, i, '|', sizeof buf);
1527 if (!strcasecmp(buf, cbtype)) {
1528 /* Yeah! Go! W00t!! */
1529 if (ma->dont_decode == 0)
1530 rc = mime_decode_now (content,
1536 break; /* Give us the chance, maybe theres another one. */
1538 if (rc == 0) text_content = (char *)content;
1540 text_content = decoded;
1541 length = bytes_decoded;
1544 if (text_content[length-1] != '\n') {
1547 cprintf("Content-type: %s", cbtype);
1548 if (!IsEmptyStr(cbcharset)) {
1549 cprintf("; charset=%s", cbcharset);
1551 cprintf("\nContent-length: %d\n",
1552 (int)(length + add_newline) );
1553 if (!IsEmptyStr(encoding)) {
1554 cprintf("Content-transfer-encoding: %s\n", encoding);
1557 cprintf("Content-transfer-encoding: 7bit\n");
1559 cprintf("X-Citadel-MSG4-Partnum: %s\n", partnum);
1561 if (client_write(text_content, length) == -1)
1563 MSGM_syslog(LOG_ERR, "output_preferred(): aborting due to write failure.\n");
1566 if (add_newline) cprintf("\n");
1567 if (decoded != NULL) free(decoded);
1572 /* No translations required or possible: output as text/plain */
1573 cprintf("Content-type: text/plain\n\n");
1575 if (ma->dont_decode == 0)
1576 rc = mime_decode_now (content,
1582 return; /* Give us the chance, maybe theres another one. */
1584 if (rc == 0) text_content = (char *)content;
1586 text_content = decoded;
1587 length = bytes_decoded;
1590 fixed_output(name, filename, partnum, disp, text_content, cbtype, cbcharset,
1591 length, encoding, cbid, cbuserdata);
1592 if (decoded != NULL) free(decoded);
1597 char desired_section[64];
1604 * Callback function for
1606 void extract_encapsulated_message(char *name, char *filename, char *partnum, char *disp,
1607 void *content, char *cbtype, char *cbcharset, size_t length,
1608 char *encoding, char *cbid, void *cbuserdata)
1610 struct encapmsg *encap;
1612 encap = (struct encapmsg *)cbuserdata;
1614 /* Only proceed if this is the desired section... */
1615 if (!strcasecmp(encap->desired_section, partnum)) {
1616 encap->msglen = length;
1617 encap->msg = malloc(length + 2);
1618 memcpy(encap->msg, content, length);
1625 * Determine whether the specified message exists in the cached_msglist
1626 * (This is a security check)
1628 int check_cached_msglist(long msgnum) {
1629 struct CitContext *CCC = CC;
1631 /* cases in which we skip the check */
1632 if (!CCC) return om_ok; /* not a session */
1633 if (CCC->client_socket <= 0) return om_ok; /* not a client session */
1634 if (CCC->cached_msglist == NULL) return om_access_denied; /* no msglist fetched */
1635 if (CCC->cached_num_msgs == 0) return om_access_denied; /* nothing to check */
1638 /* Do a binary search within the cached_msglist for the requested msgnum */
1640 int max = (CC->cached_num_msgs - 1);
1642 while (max >= min) {
1643 int middle = min + (max-min) / 2 ;
1644 if (msgnum == CCC->cached_msglist[middle]) {
1647 if (msgnum > CC->cached_msglist[middle]) {
1655 return om_access_denied;
1661 * Get a message off disk. (returns om_* values found in msgbase.h)
1664 int CtdlOutputMsg(long msg_num, /* message number (local) to fetch */
1665 int mode, /* how would you like that message? */
1666 int headers_only, /* eschew the message body? */
1667 int do_proto, /* do Citadel protocol responses? */
1668 int crlf, /* Use CRLF newlines instead of LF? */
1669 char *section, /* NULL or a message/rfc822 section */
1670 int flags, /* various flags; see msgbase.h */
1674 struct CitContext *CCC = CC;
1675 struct CtdlMessage *TheMessage = NULL;
1676 int retcode = CIT_OK;
1677 struct encapmsg encap;
1680 MSG_syslog(LOG_DEBUG, "CtdlOutputMsg(msgnum=%ld, mode=%d, section=%s)\n",
1682 (section ? section : "<>")
1685 r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
1688 if (r == om_not_logged_in) {
1689 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1692 cprintf("%d An unknown error has occurred.\n", ERROR);
1699 * Check to make sure the message is actually IN this room
1701 r = check_cached_msglist(msg_num);
1702 if (r == om_access_denied) {
1703 /* Not in the cache? We get ONE shot to check it again. */
1704 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, NULL, NULL);
1705 r = check_cached_msglist(msg_num);
1708 MSG_syslog(LOG_DEBUG, "Security check fail: message %ld is not in %s\n",
1709 msg_num, CCC->room.QRname
1712 if (r == om_access_denied) {
1713 cprintf("%d message %ld was not found in this room\n",
1714 ERROR + HIGHER_ACCESS_REQUIRED,
1723 * Fetch the message from disk. If we're in HEADERS_FAST mode,
1724 * request that we don't even bother loading the body into memory.
1726 if (headers_only == HEADERS_FAST) {
1727 TheMessage = CtdlFetchMessage(msg_num, 0);
1730 TheMessage = CtdlFetchMessage(msg_num, 1);
1733 if (TheMessage == NULL) {
1734 if (do_proto) cprintf("%d Can't locate msg %ld on disk\n",
1735 ERROR + MESSAGE_NOT_FOUND, msg_num);
1736 return(om_no_such_msg);
1739 /* Here is the weird form of this command, to process only an
1740 * encapsulated message/rfc822 section.
1742 if (section) if (!IsEmptyStr(section)) if (strcmp(section, "0")) {
1743 memset(&encap, 0, sizeof encap);
1744 safestrncpy(encap.desired_section, section, sizeof encap.desired_section);
1745 mime_parser(TheMessage->cm_fields[eMesageText],
1747 *extract_encapsulated_message,
1748 NULL, NULL, (void *)&encap, 0
1751 if ((Author != NULL) && (*Author == NULL))
1753 *Author = TheMessage->cm_fields[eAuthor];
1754 TheMessage->cm_fields[eAuthor] = NULL;
1756 if ((Address != NULL) && (*Address == NULL))
1758 *Address = TheMessage->cm_fields[erFc822Addr];
1759 TheMessage->cm_fields[erFc822Addr] = NULL;
1761 CM_Free(TheMessage);
1765 encap.msg[encap.msglen] = 0;
1766 TheMessage = convert_internet_message(encap.msg);
1767 encap.msg = NULL; /* no free() here, TheMessage owns it now */
1769 /* Now we let it fall through to the bottom of this
1770 * function, because TheMessage now contains the
1771 * encapsulated message instead of the top-level
1772 * message. Isn't that neat?
1777 cprintf("%d msg %ld has no part %s\n",
1778 ERROR + MESSAGE_NOT_FOUND,
1782 retcode = om_no_such_msg;
1787 /* Ok, output the message now */
1788 if (retcode == CIT_OK)
1789 retcode = CtdlOutputPreLoadedMsg(TheMessage, mode, headers_only, do_proto, crlf, flags);
1790 if ((Author != NULL) && (*Author == NULL))
1792 *Author = TheMessage->cm_fields[eAuthor];
1793 TheMessage->cm_fields[eAuthor] = NULL;
1795 if ((Address != NULL) && (*Address == NULL))
1797 *Address = TheMessage->cm_fields[erFc822Addr];
1798 TheMessage->cm_fields[erFc822Addr] = NULL;
1801 CM_Free(TheMessage);
1808 void OutputCtdlMsgHeaders(
1809 struct CtdlMessage *TheMessage,
1810 int do_proto) /* do Citadel protocol responses? */
1815 char display_name[256];
1817 /* begin header processing loop for Citadel message format */
1818 safestrncpy(display_name, "<unknown>", sizeof display_name);
1819 if (!CM_IsEmpty(TheMessage, eAuthor)) {
1820 strcpy(buf, TheMessage->cm_fields[eAuthor]);
1821 if (TheMessage->cm_anon_type == MES_ANONONLY) {
1822 safestrncpy(display_name, "****", sizeof display_name);
1824 else if (TheMessage->cm_anon_type == MES_ANONOPT) {
1825 safestrncpy(display_name, "anonymous", sizeof display_name);
1828 safestrncpy(display_name, buf, sizeof display_name);
1830 if ((is_room_aide())
1831 && ((TheMessage->cm_anon_type == MES_ANONONLY)
1832 || (TheMessage->cm_anon_type == MES_ANONOPT))) {
1833 size_t tmp = strlen(display_name);
1834 snprintf(&display_name[tmp],
1835 sizeof display_name - tmp,
1840 /* Don't show Internet address for users on the
1841 * local Citadel network.
1844 if (!CM_IsEmpty(TheMessage, eNodeName) &&
1845 (haschar(TheMessage->cm_fields[eNodeName], '.') == 0))
1850 /* Now spew the header fields in the order we like them. */
1851 for (i=0; i< NDiskFields; ++i) {
1853 Field = FieldOrder[i];
1854 if (Field != eMesageText) {
1855 if ( (!CM_IsEmpty(TheMessage, Field))
1856 && (msgkeys[Field] != NULL) ) {
1857 if ((Field == eenVelopeTo) ||
1858 (Field == eRecipient) ||
1859 (Field == eCarbonCopY)) {
1860 sanitize_truncated_recipient(TheMessage->cm_fields[Field]);
1862 if (Field == eAuthor) {
1863 if (do_proto) cprintf("%s=%s\n",
1867 else if ((Field == erFc822Addr) && (suppress_f)) {
1870 /* Masquerade display name if needed */
1872 if (do_proto) cprintf("%s=%s\n",
1874 TheMessage->cm_fields[Field]
1883 void OutputRFC822MsgHeaders(
1884 struct CtdlMessage *TheMessage,
1885 int flags, /* should the bessage be exported clean */
1887 char *mid, long sizeof_mid,
1888 char *suser, long sizeof_suser,
1889 char *luser, long sizeof_luser,
1890 char *fuser, long sizeof_fuser,
1891 char *snode, long sizeof_snode)
1893 char datestamp[100];
1894 int subject_found = 0;
1901 for (i = 0; i < 256; ++i) {
1902 if (TheMessage->cm_fields[i]) {
1903 mptr = mpptr = TheMessage->cm_fields[i];
1906 safestrncpy(luser, mptr, sizeof_luser);
1907 safestrncpy(suser, mptr, sizeof_suser);
1909 else if (i == 'Y') {
1910 if ((flags & QP_EADDR) != 0) {
1911 mptr = qp_encode_email_addrs(mptr);
1913 sanitize_truncated_recipient(mptr);
1914 cprintf("CC: %s%s", mptr, nl);
1916 else if (i == 'P') {
1917 cprintf("Return-Path: %s%s", mptr, nl);
1919 else if (i == eListID) {
1920 cprintf("List-ID: %s%s", mptr, nl);
1922 else if (i == 'V') {
1923 if ((flags & QP_EADDR) != 0)
1924 mptr = qp_encode_email_addrs(mptr);
1926 while ((*hptr != '\0') && isspace(*hptr))
1928 if (!IsEmptyStr(hptr))
1929 cprintf("Envelope-To: %s%s", hptr, nl);
1931 else if (i == 'U') {
1932 cprintf("Subject: %s%s", mptr, nl);
1936 safestrncpy(mid, mptr, sizeof_mid); /// TODO: detect @ here and copy @nodename in if not found.
1937 else if (i == erFc822Addr)
1938 safestrncpy(fuser, mptr, sizeof_fuser);
1939 /* else if (i == 'O')
1940 cprintf("X-Citadel-Room: %s%s",
1943 safestrncpy(snode, mptr, sizeof_snode);
1946 if (haschar(mptr, '@') == 0)
1948 sanitize_truncated_recipient(mptr);
1949 cprintf("To: %s@%s", mptr, config.c_fqdn);
1954 if ((flags & QP_EADDR) != 0) {
1955 mptr = qp_encode_email_addrs(mptr);
1957 sanitize_truncated_recipient(mptr);
1958 cprintf("To: %s", mptr);
1962 else if (i == 'T') {
1963 datestring(datestamp, sizeof datestamp,
1964 atol(mptr), DATESTRING_RFC822);
1965 cprintf("Date: %s%s", datestamp, nl);
1967 else if (i == 'W') {
1968 cprintf("References: ");
1969 k = num_tokens(mptr, '|');
1970 for (j=0; j<k; ++j) {
1971 extract_token(buf, mptr, j, '|', sizeof buf);
1972 cprintf("<%s>", buf);
1981 else if (i == eReplyTo) {
1983 while ((*hptr != '\0') && isspace(*hptr))
1985 if (!IsEmptyStr(hptr))
1986 cprintf("Reply-To: %s%s", mptr, nl);
1992 if (subject_found == 0) {
1993 cprintf("Subject: (no subject)%s", nl);
1998 void Dump_RFC822HeadersBody(
1999 struct CtdlMessage *TheMessage,
2000 int headers_only, /* eschew the message body? */
2001 int flags, /* should the bessage be exported clean? */
2005 cit_uint8_t prev_ch;
2007 const char *StartOfText = StrBufNOTNULL;
2010 int nllen = strlen(nl);
2013 mptr = TheMessage->cm_fields[eMesageText];
2017 while (*mptr != '\0') {
2018 if (*mptr == '\r') {
2025 eoh = (*(mptr+1) == '\r') && (*(mptr+2) == '\n');
2027 eoh = *(mptr+1) == '\n';
2031 StartOfText = strchr(StartOfText, '\n');
2032 StartOfText = strchr(StartOfText, '\n');
2035 if (((headers_only == HEADERS_NONE) && (mptr >= StartOfText)) ||
2036 ((headers_only == HEADERS_ONLY) && (mptr < StartOfText)) ||
2037 ((headers_only != HEADERS_NONE) &&
2038 (headers_only != HEADERS_ONLY))
2040 if (*mptr == '\n') {
2041 memcpy(&outbuf[outlen], nl, nllen);
2043 outbuf[outlen] = '\0';
2046 outbuf[outlen++] = *mptr;
2050 if (flags & ESC_DOT)
2052 if ((prev_ch == '\n') &&
2054 ((*(mptr+1) == '\r') || (*(mptr+1) == '\n')))
2056 outbuf[outlen++] = '.';
2061 if (outlen > 1000) {
2062 if (client_write(outbuf, outlen) == -1)
2064 struct CitContext *CCC = CC;
2065 MSGM_syslog(LOG_ERR, "Dump_RFC822HeadersBody(): aborting due to write failure.\n");
2072 client_write(outbuf, outlen);
2078 /* If the format type on disk is 1 (fixed-format), then we want
2079 * everything to be output completely literally ... regardless of
2080 * what message transfer format is in use.
2082 void DumpFormatFixed(
2083 struct CtdlMessage *TheMessage,
2084 int mode, /* how would you like that message? */
2091 int nllen = strlen (nl);
2094 mptr = TheMessage->cm_fields[eMesageText];
2096 if (mode == MT_MIME) {
2097 cprintf("Content-type: text/plain\n\n");
2101 while (ch = *mptr++, ch > 0) {
2105 if ((buflen > 250) && (!xlline)){
2109 while ((buflen > 0) &&
2110 (!isspace(buf[buflen])))
2116 mptr -= tbuflen - buflen;
2121 /* if we reach the outer bounds of our buffer,
2122 abort without respect what whe purge. */
2125 (buflen > SIZ - nllen - 2)))
2129 memcpy (&buf[buflen], nl, nllen);
2133 if (client_write(buf, buflen) == -1)
2135 struct CitContext *CCC = CC;
2136 MSGM_syslog(LOG_ERR, "DumpFormatFixed(): aborting due to write failure.\n");
2148 if (!IsEmptyStr(buf))
2149 cprintf("%s%s", buf, nl);
2153 * Get a message off disk. (returns om_* values found in msgbase.h)
2155 int CtdlOutputPreLoadedMsg(
2156 struct CtdlMessage *TheMessage,
2157 int mode, /* how would you like that message? */
2158 int headers_only, /* eschew the message body? */
2159 int do_proto, /* do Citadel protocol responses? */
2160 int crlf, /* Use CRLF newlines instead of LF? */
2161 int flags /* should the bessage be exported clean? */
2163 struct CitContext *CCC = CC;
2166 const char *nl; /* newline string */
2169 /* Buffers needed for RFC822 translation. These are all filled
2170 * using functions that are bounds-checked, and therefore we can
2171 * make them substantially smaller than SIZ.
2179 MSG_syslog(LOG_DEBUG, "CtdlOutputPreLoadedMsg(TheMessage=%s, %d, %d, %d, %d\n",
2180 ((TheMessage == NULL) ? "NULL" : "not null"),
2181 mode, headers_only, do_proto, crlf);
2183 strcpy(mid, "unknown");
2184 nl = (crlf ? "\r\n" : "\n");
2186 if (!CM_IsValidMsg(TheMessage)) {
2187 MSGM_syslog(LOG_ERR,
2188 "ERROR: invalid preloaded message for output\n");
2190 return(om_no_such_msg);
2193 /* Suppress envelope recipients if required to avoid disclosing BCC addresses.
2194 * Pad it with spaces in order to avoid changing the RFC822 length of the message.
2196 if ( (flags & SUPPRESS_ENV_TO) && (!CM_IsEmpty(TheMessage, eenVelopeTo)) ) {
2197 memset(TheMessage->cm_fields[eenVelopeTo], ' ', strlen(TheMessage->cm_fields[eenVelopeTo]));
2200 /* Are we downloading a MIME component? */
2201 if (mode == MT_DOWNLOAD) {
2202 if (TheMessage->cm_format_type != FMT_RFC822) {
2204 cprintf("%d This is not a MIME message.\n",
2205 ERROR + ILLEGAL_VALUE);
2206 } else if (CCC->download_fp != NULL) {
2207 if (do_proto) cprintf(
2208 "%d You already have a download open.\n",
2209 ERROR + RESOURCE_BUSY);
2211 /* Parse the message text component */
2212 mptr = TheMessage->cm_fields[eMesageText];
2213 mime_parser(mptr, NULL, *mime_download, NULL, NULL, NULL, 0);
2214 /* If there's no file open by this time, the requested
2215 * section wasn't found, so print an error
2217 if (CCC->download_fp == NULL) {
2218 if (do_proto) cprintf(
2219 "%d Section %s not found.\n",
2220 ERROR + FILE_NOT_FOUND,
2221 CCC->download_desired_section);
2224 return((CCC->download_fp != NULL) ? om_ok : om_mime_error);
2227 /* MT_SPEW_SECTION is like MT_DOWNLOAD except it outputs the whole MIME part
2228 * in a single server operation instead of opening a download file.
2230 if (mode == MT_SPEW_SECTION) {
2231 if (TheMessage->cm_format_type != FMT_RFC822) {
2233 cprintf("%d This is not a MIME message.\n",
2234 ERROR + ILLEGAL_VALUE);
2236 /* Parse the message text component */
2239 mptr = TheMessage->cm_fields[eMesageText];
2240 mime_parser(mptr, NULL, *mime_spew_section, NULL, NULL, (void *)&found_it, 0);
2241 /* If section wasn't found, print an error
2244 if (do_proto) cprintf(
2245 "%d Section %s not found.\n",
2246 ERROR + FILE_NOT_FOUND,
2247 CCC->download_desired_section);
2250 return((CCC->download_fp != NULL) ? om_ok : om_mime_error);
2253 /* now for the user-mode message reading loops */
2254 if (do_proto) cprintf("%d msg:\n", LISTING_FOLLOWS);
2256 /* Does the caller want to skip the headers? */
2257 if (headers_only == HEADERS_NONE) goto START_TEXT;
2259 /* Tell the client which format type we're using. */
2260 if ( (mode == MT_CITADEL) && (do_proto) ) {
2261 cprintf("type=%d\n", TheMessage->cm_format_type);
2264 /* nhdr=yes means that we're only displaying headers, no body */
2265 if ( (TheMessage->cm_anon_type == MES_ANONONLY)
2266 && ((mode == MT_CITADEL) || (mode == MT_MIME))
2269 cprintf("nhdr=yes\n");
2272 if ((mode == MT_CITADEL) || (mode == MT_MIME))
2273 OutputCtdlMsgHeaders(TheMessage, do_proto);
2276 /* begin header processing loop for RFC822 transfer format */
2280 strcpy(snode, NODENAME);
2281 if (mode == MT_RFC822)
2282 OutputRFC822MsgHeaders(
2287 suser, sizeof(suser),
2288 luser, sizeof(luser),
2289 fuser, sizeof(fuser),
2290 snode, sizeof(snode)
2294 for (i=0; !IsEmptyStr(&suser[i]); ++i) {
2295 suser[i] = tolower(suser[i]);
2296 if (!isalnum(suser[i])) suser[i]='_';
2299 if (mode == MT_RFC822) {
2300 if (!strcasecmp(snode, NODENAME)) {
2301 safestrncpy(snode, FQDN, sizeof snode);
2304 /* Construct a fun message id */
2305 cprintf("Message-ID: <%s", mid);/// todo: this possibly breaks threadding mails.
2306 if (strchr(mid, '@')==NULL) {
2307 cprintf("@%s", snode);
2311 if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONONLY)) {
2312 cprintf("From: \"----\" <x@x.org>%s", nl);
2314 else if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONOPT)) {
2315 cprintf("From: \"anonymous\" <x@x.org>%s", nl);
2317 else if (!IsEmptyStr(fuser)) {
2318 cprintf("From: \"%s\" <%s>%s", luser, fuser, nl);
2321 cprintf("From: \"%s\" <%s@%s>%s", luser, suser, snode, nl);
2324 /* Blank line signifying RFC822 end-of-headers */
2325 if (TheMessage->cm_format_type != FMT_RFC822) {
2330 /* end header processing loop ... at this point, we're in the text */
2332 if (headers_only == HEADERS_FAST) goto DONE;
2334 /* Tell the client about the MIME parts in this message */
2335 if (TheMessage->cm_format_type == FMT_RFC822) {
2336 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2337 mptr = TheMessage->cm_fields[eMesageText];
2338 memset(&ma, 0, sizeof(struct ma_info));
2339 mime_parser(mptr, NULL,
2340 (do_proto ? *list_this_part : NULL),
2341 (do_proto ? *list_this_pref : NULL),
2342 (do_proto ? *list_this_suff : NULL),
2345 else if (mode == MT_RFC822) { /* unparsed RFC822 dump */
2346 Dump_RFC822HeadersBody(
2355 if (headers_only == HEADERS_ONLY) {
2359 /* signify start of msg text */
2360 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2361 if (do_proto) cprintf("text\n");
2364 if (TheMessage->cm_format_type == FMT_FIXED)
2367 mode, /* how would you like that message? */
2370 /* If the message on disk is format 0 (Citadel vari-format), we
2371 * output using the formatter at 80 columns. This is the final output
2372 * form if the transfer format is RFC822, but if the transfer format
2373 * is Citadel proprietary, it'll still work, because the indentation
2374 * for new paragraphs is correct and the client will reformat the
2375 * message to the reader's screen width.
2377 if (TheMessage->cm_format_type == FMT_CITADEL) {
2378 mptr = TheMessage->cm_fields[eMesageText];
2380 if (mode == MT_MIME) {
2381 cprintf("Content-type: text/x-citadel-variformat\n\n");
2386 /* If the message on disk is format 4 (MIME), we've gotta hand it
2387 * off to the MIME parser. The client has already been told that
2388 * this message is format 1 (fixed format), so the callback function
2389 * we use will display those parts as-is.
2391 if (TheMessage->cm_format_type == FMT_RFC822) {
2392 memset(&ma, 0, sizeof(struct ma_info));
2394 if (mode == MT_MIME) {
2395 ma.use_fo_hooks = 0;
2396 strcpy(ma.chosen_part, "1");
2397 ma.chosen_pref = 9999;
2398 ma.dont_decode = CCC->msg4_dont_decode;
2399 mime_parser(mptr, NULL,
2400 *choose_preferred, *fixed_output_pre,
2401 *fixed_output_post, (void *)&ma, 1);
2402 mime_parser(mptr, NULL,
2403 *output_preferred, NULL, NULL, (void *)&ma, 1);
2406 ma.use_fo_hooks = 1;
2407 mime_parser(mptr, NULL,
2408 *fixed_output, *fixed_output_pre,
2409 *fixed_output_post, (void *)&ma, 0);
2414 DONE: /* now we're done */
2415 if (do_proto) cprintf("000\n");
2421 * display a message (mode 0 - Citadel proprietary)
2423 void cmd_msg0(char *cmdbuf)
2426 int headers_only = HEADERS_ALL;
2428 msgid = extract_long(cmdbuf, 0);
2429 headers_only = extract_int(cmdbuf, 1);
2431 CtdlOutputMsg(msgid, MT_CITADEL, headers_only, 1, 0, NULL, 0, NULL, NULL);
2437 * display a message (mode 2 - RFC822)
2439 void cmd_msg2(char *cmdbuf)
2442 int headers_only = HEADERS_ALL;
2444 msgid = extract_long(cmdbuf, 0);
2445 headers_only = extract_int(cmdbuf, 1);
2447 CtdlOutputMsg(msgid, MT_RFC822, headers_only, 1, 1, NULL, 0, NULL, NULL);
2453 * display a message (mode 3 - IGnet raw format - internal programs only)
2455 void cmd_msg3(char *cmdbuf)
2458 struct CtdlMessage *msg = NULL;
2461 if (CC->internal_pgm == 0) {
2462 cprintf("%d This command is for internal programs only.\n",
2463 ERROR + HIGHER_ACCESS_REQUIRED);
2467 msgnum = extract_long(cmdbuf, 0);
2468 msg = CtdlFetchMessage(msgnum, 1);
2470 cprintf("%d Message %ld not found.\n",
2471 ERROR + MESSAGE_NOT_FOUND, msgnum);
2475 serialize_message(&smr, msg);
2479 cprintf("%d Unable to serialize message\n",
2480 ERROR + INTERNAL_ERROR);
2484 cprintf("%d %ld\n", BINARY_FOLLOWS, (long)smr.len);
2485 client_write((char *)smr.ser, (int)smr.len);
2492 * Display a message using MIME content types
2494 void cmd_msg4(char *cmdbuf)
2499 msgid = extract_long(cmdbuf, 0);
2500 extract_token(section, cmdbuf, 1, '|', sizeof section);
2501 CtdlOutputMsg(msgid, MT_MIME, 0, 1, 0, (section[0] ? section : NULL) , 0, NULL, NULL);
2507 * Client tells us its preferred message format(s)
2509 void cmd_msgp(char *cmdbuf)
2511 if (!strcasecmp(cmdbuf, "dont_decode")) {
2512 CC->msg4_dont_decode = 1;
2513 cprintf("%d MSG4 will not pre-decode messages.\n", CIT_OK);
2516 safestrncpy(CC->preferred_formats, cmdbuf, sizeof(CC->preferred_formats));
2517 cprintf("%d Preferred MIME formats have been set.\n", CIT_OK);
2523 * Open a component of a MIME message as a download file
2525 void cmd_opna(char *cmdbuf)
2528 char desired_section[128];
2530 msgid = extract_long(cmdbuf, 0);
2531 extract_token(desired_section, cmdbuf, 1, '|', sizeof desired_section);
2532 safestrncpy(CC->download_desired_section, desired_section,
2533 sizeof CC->download_desired_section);
2534 CtdlOutputMsg(msgid, MT_DOWNLOAD, 0, 1, 1, NULL, 0, NULL, NULL);
2539 * Open a component of a MIME message and transmit it all at once
2541 void cmd_dlat(char *cmdbuf)
2544 char desired_section[128];
2546 msgid = extract_long(cmdbuf, 0);
2547 extract_token(desired_section, cmdbuf, 1, '|', sizeof desired_section);
2548 safestrncpy(CC->download_desired_section, desired_section,
2549 sizeof CC->download_desired_section);
2550 CtdlOutputMsg(msgid, MT_SPEW_SECTION, 0, 1, 1, NULL, 0, NULL, NULL);
2555 * Save one or more message pointers into a specified room
2556 * (Returns 0 for success, nonzero for failure)
2557 * roomname may be NULL to use the current room
2559 * Note that the 'supplied_msg' field may be set to NULL, in which case
2560 * the message will be fetched from disk, by number, if we need to perform
2561 * replication checks. This adds an additional database read, so if the
2562 * caller already has the message in memory then it should be supplied. (Obviously
2563 * this mode of operation only works if we're saving a single message.)
2565 int CtdlSaveMsgPointersInRoom(char *roomname, long newmsgidlist[], int num_newmsgs,
2566 int do_repl_check, struct CtdlMessage *supplied_msg, int suppress_refcount_adj
2568 struct CitContext *CCC = CC;
2570 char hold_rm[ROOMNAMELEN];
2571 struct cdbdata *cdbfr;
2574 long highest_msg = 0L;
2577 struct CtdlMessage *msg = NULL;
2579 long *msgs_to_be_merged = NULL;
2580 int num_msgs_to_be_merged = 0;
2582 MSG_syslog(LOG_DEBUG,
2583 "CtdlSaveMsgPointersInRoom(room=%s, num_msgs=%d, repl=%d, suppress_rca=%d)\n",
2584 roomname, num_newmsgs, do_repl_check, suppress_refcount_adj
2587 strcpy(hold_rm, CCC->room.QRname);
2590 if (newmsgidlist == NULL) return(ERROR + INTERNAL_ERROR);
2591 if (num_newmsgs < 1) return(ERROR + INTERNAL_ERROR);
2592 if (num_newmsgs > 1) supplied_msg = NULL;
2594 /* Now the regular stuff */
2595 if (CtdlGetRoomLock(&CCC->room,
2596 ((roomname != NULL) ? roomname : CCC->room.QRname) )
2598 MSG_syslog(LOG_ERR, "No such room <%s>\n", roomname);
2599 return(ERROR + ROOM_NOT_FOUND);
2603 msgs_to_be_merged = malloc(sizeof(long) * num_newmsgs);
2604 num_msgs_to_be_merged = 0;
2607 cdbfr = cdb_fetch(CDB_MSGLISTS, &CCC->room.QRnumber, sizeof(long));
2608 if (cdbfr == NULL) {
2612 msglist = (long *) cdbfr->ptr;
2613 cdbfr->ptr = NULL; /* CtdlSaveMsgPointerInRoom() now owns this memory */
2614 num_msgs = cdbfr->len / sizeof(long);
2619 /* Create a list of msgid's which were supplied by the caller, but do
2620 * not already exist in the target room. It is absolutely taboo to
2621 * have more than one reference to the same message in a room.
2623 for (i=0; i<num_newmsgs; ++i) {
2625 if (num_msgs > 0) for (j=0; j<num_msgs; ++j) {
2626 if (msglist[j] == newmsgidlist[i]) {
2631 msgs_to_be_merged[num_msgs_to_be_merged++] = newmsgidlist[i];
2635 MSG_syslog(LOG_DEBUG, "%d unique messages to be merged\n", num_msgs_to_be_merged);
2638 * Now merge the new messages
2640 msglist = realloc(msglist, (sizeof(long) * (num_msgs + num_msgs_to_be_merged)) );
2641 if (msglist == NULL) {
2642 MSGM_syslog(LOG_ALERT, "ERROR: can't realloc message list!\n");
2643 free(msgs_to_be_merged);
2644 return (ERROR + INTERNAL_ERROR);
2646 memcpy(&msglist[num_msgs], msgs_to_be_merged, (sizeof(long) * num_msgs_to_be_merged) );
2647 num_msgs += num_msgs_to_be_merged;
2649 /* Sort the message list, so all the msgid's are in order */
2650 num_msgs = sort_msglist(msglist, num_msgs);
2652 /* Determine the highest message number */
2653 highest_msg = msglist[num_msgs - 1];
2655 /* Write it back to disk. */
2656 cdb_store(CDB_MSGLISTS, &CCC->room.QRnumber, (int)sizeof(long),
2657 msglist, (int)(num_msgs * sizeof(long)));
2659 /* Free up the memory we used. */
2662 /* Update the highest-message pointer and unlock the room. */
2663 CCC->room.QRhighest = highest_msg;
2664 CtdlPutRoomLock(&CCC->room);
2666 /* Perform replication checks if necessary */
2667 if ( (DoesThisRoomNeedEuidIndexing(&CCC->room)) && (do_repl_check) ) {
2668 MSGM_syslog(LOG_DEBUG, "CtdlSaveMsgPointerInRoom() doing repl checks\n");
2670 for (i=0; i<num_msgs_to_be_merged; ++i) {
2671 msgid = msgs_to_be_merged[i];
2673 if (supplied_msg != NULL) {
2677 msg = CtdlFetchMessage(msgid, 0);
2681 ReplicationChecks(msg);
2683 /* If the message has an Exclusive ID, index that... */
2684 if (!CM_IsEmpty(msg, eExclusiveID)) {
2685 index_message_by_euid(msg->cm_fields[eExclusiveID], &CCC->room, msgid);
2688 /* Free up the memory we may have allocated */
2689 if (msg != supplied_msg) {
2698 MSGM_syslog(LOG_DEBUG, "CtdlSaveMsgPointerInRoom() skips repl checks\n");
2701 /* Submit this room for processing by hooks */
2702 PerformRoomHooks(&CCC->room);
2704 /* Go back to the room we were in before we wandered here... */
2705 CtdlGetRoom(&CCC->room, hold_rm);
2707 /* Bump the reference count for all messages which were merged */
2708 if (!suppress_refcount_adj) {
2709 AdjRefCountList(msgs_to_be_merged, num_msgs_to_be_merged, +1);
2712 /* Free up memory... */
2713 if (msgs_to_be_merged != NULL) {
2714 free(msgs_to_be_merged);
2717 /* Return success. */
2723 * This is the same as CtdlSaveMsgPointersInRoom() but it only accepts
2726 int CtdlSaveMsgPointerInRoom(char *roomname, long msgid,
2727 int do_repl_check, struct CtdlMessage *supplied_msg)
2729 return CtdlSaveMsgPointersInRoom(roomname, &msgid, 1, do_repl_check, supplied_msg, 0);
2736 * Message base operation to save a new message to the message store
2737 * (returns new message number)
2739 * This is the back end for CtdlSubmitMsg() and should not be directly
2740 * called by server-side modules.
2743 long send_message(struct CtdlMessage *msg) {
2744 struct CitContext *CCC = CC;
2753 /* Get a new message number */
2754 newmsgid = get_new_message_number();
2755 msgidbuflen = snprintf(msgidbuf, sizeof msgidbuf, "%08lX-%08lX@%s",
2756 (long unsigned int) time(NULL),
2757 (long unsigned int) newmsgid,
2761 /* Generate an ID if we don't have one already */
2762 if (CM_IsEmpty(msg, emessageId)) {
2763 CM_SetField(msg, emessageId, msgidbuf, msgidbuflen);
2766 /* If the message is big, set its body aside for storage elsewhere */
2767 if (!CM_IsEmpty(msg, eMesageText)) {
2768 if (strlen(msg->cm_fields[eMesageText]) > BIGMSG) {
2770 holdM = msg->cm_fields[eMesageText];
2771 msg->cm_fields[eMesageText] = NULL;
2775 /* Serialize our data structure for storage in the database */
2776 serialize_message(&smr, msg);
2779 msg->cm_fields[eMesageText] = holdM;
2783 cprintf("%d Unable to serialize message\n",
2784 ERROR + INTERNAL_ERROR);
2788 /* Write our little bundle of joy into the message base */
2789 if (cdb_store(CDB_MSGMAIN, &newmsgid, (int)sizeof(long),
2790 smr.ser, smr.len) < 0) {
2791 MSGM_syslog(LOG_ERR, "Can't store message\n");
2795 cdb_store(CDB_BIGMSGS,
2805 /* Free the memory we used for the serialized message */
2808 /* Return the *local* message ID to the caller
2809 * (even if we're storing an incoming network message)
2817 * Serialize a struct CtdlMessage into the format used on disk and network.
2819 * This function loads up a "struct ser_ret" (defined in server.h) which
2820 * contains the length of the serialized message and a pointer to the
2821 * serialized message in memory. THE LATTER MUST BE FREED BY THE CALLER.
2823 void serialize_message(struct ser_ret *ret, /* return values */
2824 struct CtdlMessage *msg) /* unserialized msg */
2826 struct CitContext *CCC = CC;
2827 size_t wlen, fieldlen;
2829 long lengths[NDiskFields];
2831 memset(lengths, 0, sizeof(lengths));
2834 * Check for valid message format
2836 if (CM_IsValidMsg(msg) == 0) {
2837 MSGM_syslog(LOG_ERR, "serialize_message() aborting due to invalid message\n");
2844 for (i=0; i < NDiskFields; ++i)
2845 if (msg->cm_fields[FieldOrder[i]] != NULL)
2847 lengths[i] = strlen(msg->cm_fields[FieldOrder[i]]);
2848 ret->len += lengths[i] + 2;
2851 ret->ser = malloc(ret->len);
2852 if (ret->ser == NULL) {
2853 MSG_syslog(LOG_ERR, "serialize_message() malloc(%ld) failed: %s\n",
2854 (long)ret->len, strerror(errno));
2861 ret->ser[1] = msg->cm_anon_type;
2862 ret->ser[2] = msg->cm_format_type;
2865 for (i=0; i < NDiskFields; ++i)
2866 if (msg->cm_fields[FieldOrder[i]] != NULL)
2868 fieldlen = lengths[i];
2869 ret->ser[wlen++] = (char)FieldOrder[i];
2871 memcpy(&ret->ser[wlen],
2872 msg->cm_fields[FieldOrder[i]],
2875 wlen = wlen + fieldlen + 1;
2878 if (ret->len != wlen) {
2879 MSG_syslog(LOG_ERR, "ERROR: len=%ld wlen=%ld\n",
2880 (long)ret->len, (long)wlen);
2888 * Check to see if any messages already exist in the current room which
2889 * carry the same Exclusive ID as this one. If any are found, delete them.
2891 void ReplicationChecks(struct CtdlMessage *msg) {
2892 struct CitContext *CCC = CC;
2893 long old_msgnum = (-1L);
2895 if (DoesThisRoomNeedEuidIndexing(&CCC->room) == 0) return;
2897 MSG_syslog(LOG_DEBUG, "Performing replication checks in <%s>\n",
2900 /* No exclusive id? Don't do anything. */
2901 if (msg == NULL) return;
2902 if (CM_IsEmpty(msg, eExclusiveID)) return;
2904 /*MSG_syslog(LOG_DEBUG, "Exclusive ID: <%s> for room <%s>\n",
2905 msg->cm_fields[eExclusiveID], CCC->room.QRname);*/
2907 old_msgnum = CtdlLocateMessageByEuid(msg->cm_fields[eExclusiveID], &CCC->room);
2908 if (old_msgnum > 0L) {
2909 MSG_syslog(LOG_DEBUG, "ReplicationChecks() replacing message %ld\n", old_msgnum);
2910 CtdlDeleteMessages(CCC->room.QRname, &old_msgnum, 1, "");
2917 * Save a message to disk and submit it into the delivery system.
2919 long CtdlSubmitMsg(struct CtdlMessage *msg, /* message to save */
2920 struct recptypes *recps, /* recipients (if mail) */
2921 const char *force, /* force a particular room? */
2922 int flags /* should the message be exported clean? */
2925 char submit_filename[128];
2926 char hold_rm[ROOMNAMELEN];
2927 char actual_rm[ROOMNAMELEN];
2928 char force_room[ROOMNAMELEN];
2929 char content_type[SIZ]; /* We have to learn this */
2930 char recipient[SIZ];
2933 const char *mptr = NULL;
2934 struct ctdluser userbuf;
2936 struct MetaData smi;
2937 FILE *network_fp = NULL;
2938 static int seqnum = 1;
2939 struct CtdlMessage *imsg = NULL;
2941 size_t instr_alloc = 0;
2943 char *hold_R, *hold_D;
2944 char *collected_addresses = NULL;
2945 struct addresses_to_be_filed *aptr = NULL;
2946 StrBuf *saved_rfc822_version = NULL;
2947 int qualified_for_journaling = 0;
2948 CitContext *CCC = MyContext();
2949 char bounce_to[1024] = "";
2952 MSGM_syslog(LOG_DEBUG, "CtdlSubmitMsg() called\n");
2953 if (CM_IsValidMsg(msg) == 0) return(-1); /* self check */
2955 /* If this message has no timestamp, we take the liberty of
2956 * giving it one, right now.
2958 if (CM_IsEmpty(msg, eTimestamp)) {
2959 CM_SetFieldLONG(msg, eTimestamp, time(NULL));
2962 /* If this message has no path, we generate one.
2964 if (CM_IsEmpty(msg, eMessagePath)) {
2965 if (!CM_IsEmpty(msg, eAuthor)) {
2966 CM_CopyField(msg, eMessagePath, eAuthor);
2967 for (a=0; !IsEmptyStr(&msg->cm_fields[eMessagePath][a]); ++a) {
2968 if (isspace(msg->cm_fields[eMessagePath][a])) {
2969 msg->cm_fields[eMessagePath][a] = ' ';
2974 CM_SetField(msg, eMessagePath, HKEY("unknown"));
2978 if (force == NULL) {
2979 force_room[0] = '\0';
2982 strcpy(force_room, force);
2985 /* Learn about what's inside, because it's what's inside that counts */
2986 if (CM_IsEmpty(msg, eMesageText)) {
2987 MSGM_syslog(LOG_ERR, "ERROR: attempt to save message with NULL body\n");
2991 switch (msg->cm_format_type) {
2993 strcpy(content_type, "text/x-citadel-variformat");
2996 strcpy(content_type, "text/plain");
2999 strcpy(content_type, "text/plain");
3000 mptr = bmstrcasestr(msg->cm_fields[eMesageText], "Content-type:");
3003 safestrncpy(content_type, &mptr[13], sizeof content_type);
3004 striplt(content_type);
3005 aptr = content_type;
3006 while (!IsEmptyStr(aptr)) {
3018 /* Goto the correct room */
3019 room = (recps) ? CCC->room.QRname : SENTITEMS;
3020 MSG_syslog(LOG_DEBUG, "Selected room %s\n", room);
3021 strcpy(hold_rm, CCC->room.QRname);
3022 strcpy(actual_rm, CCC->room.QRname);
3023 if (recps != NULL) {
3024 strcpy(actual_rm, SENTITEMS);
3027 /* If the user is a twit, move to the twit room for posting */
3029 if (CCC->user.axlevel == AxProbU) {
3030 strcpy(hold_rm, actual_rm);
3031 strcpy(actual_rm, config.c_twitroom);
3032 MSGM_syslog(LOG_DEBUG, "Diverting to twit room\n");
3036 /* ...or if this message is destined for Aide> then go there. */
3037 if (!IsEmptyStr(force_room)) {
3038 strcpy(actual_rm, force_room);
3041 MSG_syslog(LOG_INFO, "Final selection: %s (%s)\n", actual_rm, room);
3042 if (strcasecmp(actual_rm, CCC->room.QRname)) {
3043 /* CtdlGetRoom(&CCC->room, actual_rm); */
3044 CtdlUserGoto(actual_rm, 0, 1, NULL, NULL);
3048 * If this message has no O (room) field, generate one.
3050 if (CM_IsEmpty(msg, eOriginalRoom)) {
3051 CM_SetField(msg, eOriginalRoom, CCC->room.QRname, strlen(CCC->room.QRname));
3054 /* Perform "before save" hooks (aborting if any return nonzero) */
3055 MSGM_syslog(LOG_DEBUG, "Performing before-save hooks\n");
3056 if (PerformMessageHooks(msg, EVT_BEFORESAVE) > 0) return(-3);
3059 * If this message has an Exclusive ID, and the room is replication
3060 * checking enabled, then do replication checks.
3062 if (DoesThisRoomNeedEuidIndexing(&CCC->room)) {
3063 ReplicationChecks(msg);
3066 /* Save it to disk */
3067 MSGM_syslog(LOG_DEBUG, "Saving to disk\n");
3068 newmsgid = send_message(msg);
3069 if (newmsgid <= 0L) return(-5);
3071 /* Write a supplemental message info record. This doesn't have to
3072 * be a critical section because nobody else knows about this message
3075 MSGM_syslog(LOG_DEBUG, "Creating MetaData record\n");
3076 memset(&smi, 0, sizeof(struct MetaData));
3077 smi.meta_msgnum = newmsgid;
3078 smi.meta_refcount = 0;
3079 safestrncpy(smi.meta_content_type, content_type,
3080 sizeof smi.meta_content_type);
3083 * Measure how big this message will be when rendered as RFC822.
3084 * We do this for two reasons:
3085 * 1. We need the RFC822 length for the new metadata record, so the
3086 * POP and IMAP services don't have to calculate message lengths
3087 * while the user is waiting (multiplied by potentially hundreds
3088 * or thousands of messages).
3089 * 2. If journaling is enabled, we will need an RFC822 version of the
3090 * message to attach to the journalized copy.
3092 if (CCC->redirect_buffer != NULL) {
3093 MSGM_syslog(LOG_ALERT, "CCC->redirect_buffer is not NULL during message submission!\n");
3096 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
3097 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, QP_EADDR);
3098 smi.meta_rfc822_length = StrLength(CCC->redirect_buffer);
3099 saved_rfc822_version = CCC->redirect_buffer;
3100 CCC->redirect_buffer = NULL;
3104 /* Now figure out where to store the pointers */
3105 MSGM_syslog(LOG_DEBUG, "Storing pointers\n");
3107 /* If this is being done by the networker delivering a private
3108 * message, we want to BYPASS saving the sender's copy (because there
3109 * is no local sender; it would otherwise go to the Trashcan).
3111 if ((!CCC->internal_pgm) || (recps == NULL)) {
3112 if (CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 1, msg) != 0) {
3113 MSGM_syslog(LOG_ERR, "ERROR saving message pointer!\n");
3114 CtdlSaveMsgPointerInRoom(config.c_aideroom, newmsgid, 0, msg);
3118 /* For internet mail, drop a copy in the outbound queue room */
3119 if ((recps != NULL) && (recps->num_internet > 0)) {
3120 CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, newmsgid, 0, msg);
3123 /* If other rooms are specified, drop them there too. */
3124 if ((recps != NULL) && (recps->num_room > 0))
3125 for (i=0; i<num_tokens(recps->recp_room, '|'); ++i) {
3126 extract_token(recipient, recps->recp_room, i,
3127 '|', sizeof recipient);
3128 MSG_syslog(LOG_DEBUG, "Delivering to room <%s>\n", recipient);///// xxxx
3129 CtdlSaveMsgPointerInRoom(recipient, newmsgid, 0, msg);
3132 /* Bump this user's messages posted counter. */
3133 MSGM_syslog(LOG_DEBUG, "Updating user\n");
3134 CtdlGetUserLock(&CCC->user, CCC->curr_user);
3135 CCC->user.posted = CCC->user.posted + 1;
3136 CtdlPutUserLock(&CCC->user);
3138 /* Decide where bounces need to be delivered */
3139 if ((recps != NULL) && (recps->bounce_to != NULL)) {
3140 safestrncpy(bounce_to, recps->bounce_to, sizeof bounce_to);
3142 else if (CCC->logged_in) {
3143 snprintf(bounce_to, sizeof bounce_to, "%s@%s", CCC->user.fullname, config.c_nodename);
3146 snprintf(bounce_to, sizeof bounce_to, "%s@%s", msg->cm_fields[eAuthor], msg->cm_fields[eNodeName]);
3149 /* If this is private, local mail, make a copy in the
3150 * recipient's mailbox and bump the reference count.
3152 if ((recps != NULL) && (recps->num_local > 0))
3153 for (i=0; i<num_tokens(recps->recp_local, '|'); ++i) {
3155 recipientlen = extract_token(recipient,
3156 recps->recp_local, i,
3157 '|', sizeof recipient);
3158 MSG_syslog(LOG_DEBUG, "Delivering private local mail to <%s>\n",
3160 if (CtdlGetUser(&userbuf, recipient) == 0) {
3161 CtdlMailboxName(actual_rm, sizeof actual_rm, &userbuf, MAILROOM);
3162 CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0, msg);
3163 CtdlBumpNewMailCounter(userbuf.usernum);
3164 if (!IsEmptyStr(config.c_funambol_host) || !IsEmptyStr(config.c_pager_program)) {
3165 /* Generate a instruction message for the Funambol notification
3166 * server, in the same style as the SMTP queue
3170 instr = malloc(instr_alloc);
3171 instrlen = snprintf(
3173 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
3177 (long)time(NULL), //todo: time() is expensive!
3181 imsg = malloc(sizeof(struct CtdlMessage));
3182 memset(imsg, 0, sizeof(struct CtdlMessage));
3183 imsg->cm_magic = CTDLMESSAGE_MAGIC;
3184 imsg->cm_anon_type = MES_NORMAL;
3185 imsg->cm_format_type = FMT_RFC822;
3186 CM_SetField(imsg, eMsgSubject, HKEY("QMSG"));
3187 CM_SetField(imsg, eAuthor, HKEY("Citadel"));
3188 CM_SetField(imsg, eJournal, HKEY("do not journal"));
3189 CM_SetAsField(imsg, eMesageText, &instr, instrlen);
3190 CM_SetField(imsg, eExtnotify, recipient, recipientlen);
3191 CtdlSubmitMsg(imsg, NULL, FNBL_QUEUE_ROOM, 0);
3196 MSG_syslog(LOG_DEBUG, "No user <%s>\n", recipient);
3197 CtdlSaveMsgPointerInRoom(config.c_aideroom, newmsgid, 0, msg);
3201 /* Perform "after save" hooks */
3202 MSGM_syslog(LOG_DEBUG, "Performing after-save hooks\n");
3204 CM_SetFieldLONG(msg, eVltMsgNum, newmsgid);
3205 PerformMessageHooks(msg, EVT_AFTERSAVE);
3206 CM_FlushField(msg, eVltMsgNum);
3208 /* For IGnet mail, we have to save a new copy into the spooler for
3209 * each recipient, with the R and D fields set to the recipient and
3210 * destination-node. This has two ugly side effects: all other
3211 * recipients end up being unlisted in this recipient's copy of the
3212 * message, and it has to deliver multiple messages to the same
3213 * node. We'll revisit this again in a year or so when everyone has
3214 * a network spool receiver that can handle the new style messages.
3216 if ((recps != NULL) && (recps->num_ignet > 0))
3217 for (i=0; i<num_tokens(recps->recp_ignet, '|'); ++i) {
3218 extract_token(recipient, recps->recp_ignet, i,
3219 '|', sizeof recipient);
3221 hold_R = msg->cm_fields[eRecipient];
3222 hold_D = msg->cm_fields[eDestination];
3223 msg->cm_fields[eRecipient] = malloc(SIZ);
3224 msg->cm_fields[eDestination] = malloc(128);
3225 extract_token(msg->cm_fields[eRecipient], recipient, 0, '@', SIZ);
3226 extract_token(msg->cm_fields[eDestination], recipient, 1, '@', 128);
3228 serialize_message(&smr, msg);
3230 snprintf(submit_filename, sizeof submit_filename,
3231 "%s/netmail.%04lx.%04x.%04x",
3233 (long) getpid(), CCC->cs_pid, ++seqnum);
3234 network_fp = fopen(submit_filename, "wb+");
3235 if (network_fp != NULL) {
3236 rv = fwrite(smr.ser, smr.len, 1, network_fp);
3238 MSG_syslog(LOG_EMERG, "CtdlSubmitMsg(): Couldn't write network spool file: %s\n",
3246 free(msg->cm_fields[eRecipient]);
3247 free(msg->cm_fields[eDestination]);
3248 msg->cm_fields[eRecipient] = hold_R;
3249 msg->cm_fields[eDestination] = hold_D;
3252 /* Go back to the room we started from */
3253 MSG_syslog(LOG_DEBUG, "Returning to original room %s\n", hold_rm);
3254 if (strcasecmp(hold_rm, CCC->room.QRname))
3255 CtdlUserGoto(hold_rm, 0, 1, NULL, NULL);
3257 /* For internet mail, generate delivery instructions.
3258 * Yes, this is recursive. Deal with it. Infinite recursion does
3259 * not happen because the delivery instructions message does not
3260 * contain a recipient.
3262 if ((recps != NULL) && (recps->num_internet > 0)) {
3263 StrBuf *SpoolMsg = NewStrBuf();
3266 MSGM_syslog(LOG_DEBUG, "Generating delivery instructions\n");
3268 StrBufPrintf(SpoolMsg,
3269 "Content-type: "SPOOLMIME"\n"
3278 if (recps->envelope_from != NULL) {
3279 StrBufAppendBufPlain(SpoolMsg, HKEY("envelope_from|"), 0);
3280 StrBufAppendBufPlain(SpoolMsg, recps->envelope_from, -1, 0);
3281 StrBufAppendBufPlain(SpoolMsg, HKEY("\n"), 0);
3283 if (recps->sending_room != NULL) {
3284 StrBufAppendBufPlain(SpoolMsg, HKEY("source_room|"), 0);
3285 StrBufAppendBufPlain(SpoolMsg, recps->sending_room, -1, 0);
3286 StrBufAppendBufPlain(SpoolMsg, HKEY("\n"), 0);
3289 nTokens = num_tokens(recps->recp_internet, '|');
3290 for (i = 0; i < nTokens; i++) {
3292 len = extract_token(recipient, recps->recp_internet, i, '|', sizeof recipient);
3294 StrBufAppendBufPlain(SpoolMsg, HKEY("remote|"), 0);
3295 StrBufAppendBufPlain(SpoolMsg, recipient, len, 0);
3296 StrBufAppendBufPlain(SpoolMsg, HKEY("|0||\n"), 0);
3300 imsg = malloc(sizeof(struct CtdlMessage));
3301 memset(imsg, 0, sizeof(struct CtdlMessage));
3302 imsg->cm_magic = CTDLMESSAGE_MAGIC;
3303 imsg->cm_anon_type = MES_NORMAL;
3304 imsg->cm_format_type = FMT_RFC822;
3305 imsg->cm_fields[eMsgSubject] = strdup("QMSG");
3306 imsg->cm_fields[eAuthor] = strdup("Citadel");
3307 imsg->cm_fields[eJournal] = strdup("do not journal");
3308 imsg->cm_fields[eMesageText] = SmashStrBuf(&SpoolMsg); /* imsg owns this memory now */
3309 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM, QP_EADDR);
3314 * Any addresses to harvest for someone's address book?
3316 if ( (CCC->logged_in) && (recps != NULL) ) {
3317 collected_addresses = harvest_collected_addresses(msg);
3320 if (collected_addresses != NULL) {
3321 aptr = (struct addresses_to_be_filed *)
3322 malloc(sizeof(struct addresses_to_be_filed));
3323 CtdlMailboxName(actual_rm, sizeof actual_rm,
3324 &CCC->user, USERCONTACTSROOM);
3325 aptr->roomname = strdup(actual_rm);
3326 aptr->collected_addresses = collected_addresses;
3327 begin_critical_section(S_ATBF);
3330 end_critical_section(S_ATBF);
3334 * Determine whether this message qualifies for journaling.
3336 if (!CM_IsEmpty(msg, eJournal)) {
3337 qualified_for_journaling = 0;
3340 if (recps == NULL) {
3341 qualified_for_journaling = config.c_journal_pubmsgs;
3343 else if (recps->num_local + recps->num_ignet + recps->num_internet > 0) {
3344 qualified_for_journaling = config.c_journal_email;
3347 qualified_for_journaling = config.c_journal_pubmsgs;
3352 * Do we have to perform journaling? If so, hand off the saved
3353 * RFC822 version will be handed off to the journaler for background
3354 * submit. Otherwise, we have to free the memory ourselves.
3356 if (saved_rfc822_version != NULL) {
3357 if (qualified_for_journaling) {
3358 JournalBackgroundSubmit(msg, saved_rfc822_version, recps);
3361 FreeStrBuf(&saved_rfc822_version);
3371 * Convenience function for generating small administrative messages.
3373 void quickie_message(const char *from,
3374 const char *fromaddr,
3379 const char *subject)
3381 struct CtdlMessage *msg;
3382 struct recptypes *recp = NULL;
3384 msg = malloc(sizeof(struct CtdlMessage));
3385 memset(msg, 0, sizeof(struct CtdlMessage));
3386 msg->cm_magic = CTDLMESSAGE_MAGIC;
3387 msg->cm_anon_type = MES_NORMAL;
3388 msg->cm_format_type = format_type;
3391 msg->cm_fields[eAuthor] = strdup(from);
3393 else if (fromaddr != NULL) {
3394 msg->cm_fields[eAuthor] = strdup(fromaddr);
3395 if (strchr(msg->cm_fields[eAuthor], '@')) {
3396 *strchr(msg->cm_fields[eAuthor], '@') = 0;
3400 msg->cm_fields[eAuthor] = strdup("Citadel");
3403 if (fromaddr != NULL) msg->cm_fields[erFc822Addr] = strdup(fromaddr);
3404 if (room != NULL) msg->cm_fields[eOriginalRoom] = strdup(room);
3405 msg->cm_fields[eNodeName] = strdup(NODENAME);
3407 msg->cm_fields[eRecipient] = strdup(to);
3408 recp = validate_recipients(to, NULL, 0);
3410 if (subject != NULL) {
3411 msg->cm_fields[eMsgSubject] = strdup(subject);
3413 msg->cm_fields[eMesageText] = strdup(text);
3415 CtdlSubmitMsg(msg, recp, room, 0);
3417 if (recp != NULL) free_recipients(recp);
3420 void flood_protect_quickie_message(const char *from,
3421 const char *fromaddr,
3426 const char *subject,
3428 const char **CritStr,
3435 u_char rawdigest[MD5_DIGEST_LEN];
3436 struct MD5Context md5context;
3440 time_t tsday = NOW / (8*60*60); /* just care for a day... */
3442 tslen = snprintf(timestamp, sizeof(timestamp), "%ld", tsday);
3443 MD5Init(&md5context);
3445 for (i = 0; i < nCriterions; i++)
3446 MD5Update(&md5context,
3447 (const unsigned char*)CritStr[i], CritStrLen[i]);
3448 MD5Update(&md5context,
3449 (const unsigned char*)timestamp, tslen);
3450 MD5Final(rawdigest, &md5context);
3452 guid = NewStrBufPlain(NULL,
3453 MD5_DIGEST_LEN * 2 + 12);
3454 StrBufHexEscAppend(guid, NULL, rawdigest, MD5_DIGEST_LEN);
3455 StrBufAppendBufPlain(guid, HKEY("_fldpt"), 0);
3456 if (StrLength(guid) > 40)
3457 StrBufCutAt(guid, 40, NULL);
3459 if (CheckIfAlreadySeen("FPAideMessage",
3468 /* yes, we did. flood protection kicks in. */
3470 "not sending message again\n");
3474 /* no, this message isn't sent recently; go ahead. */
3475 quickie_message(from,
3486 * Back end function used by CtdlMakeMessage() and similar functions
3488 StrBuf *CtdlReadMessageBodyBuf(char *terminator, /* token signalling EOT */
3490 size_t maxlen, /* maximum message length */
3491 StrBuf *exist, /* if non-null, append to it;
3492 exist is ALWAYS freed */
3493 int crlf, /* CRLF newlines instead of LF */
3494 int *sock /* socket handle or 0 for this session's client socket */
3503 LineBuf = NewStrBufPlain(NULL, SIZ);
3504 if (exist == NULL) {
3505 Message = NewStrBufPlain(NULL, 4 * SIZ);
3508 Message = NewStrBufDup(exist);
3511 /* Do we need to change leading ".." to "." for SMTP escaping? */
3512 if ((tlen == 1) && (*terminator == '.')) {
3516 /* read in the lines of message text one by one */
3519 if ((CtdlSockGetLine(sock, LineBuf, 5) < 0) ||
3524 if (CtdlClientGetLine(LineBuf) < 0) finished = 1;
3526 if ((StrLength(LineBuf) == tlen) &&
3527 (!strcmp(ChrPtr(LineBuf), terminator)))
3530 if ( (!flushing) && (!finished) ) {
3532 StrBufAppendBufPlain(LineBuf, HKEY("\r\n"), 0);
3535 StrBufAppendBufPlain(LineBuf, HKEY("\n"), 0);
3538 /* Unescape SMTP-style input of two dots at the beginning of the line */
3540 (StrLength(LineBuf) == 2) &&
3541 (!strcmp(ChrPtr(LineBuf), "..")))
3543 StrBufCutLeft(LineBuf, 1);
3546 StrBufAppendBuf(Message, LineBuf, 0);
3549 /* if we've hit the max msg length, flush the rest */
3550 if (StrLength(Message) >= maxlen) flushing = 1;
3552 } while (!finished);
3553 FreeStrBuf(&LineBuf);
3557 void DeleteAsyncMsg(ReadAsyncMsg **Msg)
3561 FreeStrBuf(&(*Msg)->MsgBuf);
3567 ReadAsyncMsg *NewAsyncMsg(const char *terminator, /* token signalling EOT */
3569 size_t maxlen, /* maximum message length */
3570 size_t expectlen, /* if we expect a message, how long should it be? */
3571 StrBuf *exist, /* if non-null, append to it;
3572 exist is ALWAYS freed */
3573 long eLen, /* length of exist */
3574 int crlf /* CRLF newlines instead of LF */
3577 ReadAsyncMsg *NewMsg;
3579 NewMsg = (ReadAsyncMsg *)malloc(sizeof(ReadAsyncMsg));
3580 memset(NewMsg, 0, sizeof(ReadAsyncMsg));
3582 if (exist == NULL) {
3585 if (expectlen == 0) {
3589 len = expectlen + 10;
3591 NewMsg->MsgBuf = NewStrBufPlain(NULL, len);
3594 NewMsg->MsgBuf = NewStrBufDup(exist);
3596 /* Do we need to change leading ".." to "." for SMTP escaping? */
3597 if ((tlen == 1) && (*terminator == '.')) {
3601 NewMsg->terminator = terminator;
3602 NewMsg->tlen = tlen;
3604 NewMsg->maxlen = maxlen;
3606 NewMsg->crlf = crlf;
3612 * Back end function used by CtdlMakeMessage() and similar functions
3614 eReadState CtdlReadMessageBodyAsync(AsyncIO *IO)
3616 ReadAsyncMsg *ReadMsg;
3617 int MsgFinished = 0;
3618 eReadState Finished = eMustReadMore;
3623 const char *pch = ChrPtr(IO->SendBuf.Buf);
3624 const char *pchh = IO->SendBuf.ReadWritePointer;
3630 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
3631 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
3632 ((CitContext*)(IO->CitContext))->ServiceName,
3635 fd = fopen(fn, "a+");
3638 ReadMsg = IO->ReadMsg;
3640 /* read in the lines of message text one by one */
3642 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
3645 case eMustReadMore: /// read new from socket...
3647 if (IO->RecvBuf.ReadWritePointer != NULL) {
3648 nbytes = StrLength(IO->RecvBuf.Buf) - (IO->RecvBuf.ReadWritePointer - ChrPtr(IO->RecvBuf.Buf));
3649 fprintf(fd, "Read; Line unfinished: %ld Bytes still in buffer [", nbytes);
3651 fwrite(IO->RecvBuf.ReadWritePointer, nbytes, 1, fd);
3655 fprintf(fd, "BufferEmpty! \n");
3661 case eBufferNotEmpty: /* shouldn't happen... */
3662 case eReadSuccess: /// done for now...
3664 case eReadFail: /// WHUT?
3670 if ((StrLength(IO->IOBuf) == ReadMsg->tlen) &&
3671 (!strcmp(ChrPtr(IO->IOBuf), ReadMsg->terminator))) {
3674 fprintf(fd, "found Terminator; Message Size: %d\n", StrLength(ReadMsg->MsgBuf));
3677 else if (!ReadMsg->flushing) {
3680 fprintf(fd, "Read Line: [%d][%s]\n", StrLength(IO->IOBuf), ChrPtr(IO->IOBuf));
3683 /* Unescape SMTP-style input of two dots at the beginning of the line */
3684 if ((ReadMsg->dodot) &&
3685 (StrLength(IO->IOBuf) == 2) && /* TODO: do we just unescape lines with two dots or any line? */
3686 (!strcmp(ChrPtr(IO->IOBuf), "..")))
3689 fprintf(fd, "UnEscaped!\n");
3691 StrBufCutLeft(IO->IOBuf, 1);
3694 if (ReadMsg->crlf) {
3695 StrBufAppendBufPlain(IO->IOBuf, HKEY("\r\n"), 0);
3698 StrBufAppendBufPlain(IO->IOBuf, HKEY("\n"), 0);
3701 StrBufAppendBuf(ReadMsg->MsgBuf, IO->IOBuf, 0);
3704 /* if we've hit the max msg length, flush the rest */
3705 if (StrLength(ReadMsg->MsgBuf) >= ReadMsg->maxlen) ReadMsg->flushing = 1;
3707 } while (!MsgFinished);
3710 fprintf(fd, "Done with reading; %s.\n, ",
3711 (MsgFinished)?"Message Finished": "FAILED");
3715 return eReadSuccess;
3722 * Back end function used by CtdlMakeMessage() and similar functions
3724 char *CtdlReadMessageBody(char *terminator, /* token signalling EOT */
3726 size_t maxlen, /* maximum message length */
3727 StrBuf *exist, /* if non-null, append to it;
3728 exist is ALWAYS freed */
3729 int crlf, /* CRLF newlines instead of LF */
3730 int *sock /* socket handle or 0 for this session's client socket */
3735 Message = CtdlReadMessageBodyBuf(terminator,
3741 if (Message == NULL)
3744 return SmashStrBuf(&Message);
3749 * Build a binary message to be saved on disk.
3750 * (NOTE: if you supply 'preformatted_text', the buffer you give it
3751 * will become part of the message. This means you are no longer
3752 * responsible for managing that memory -- it will be freed along with
3753 * the rest of the fields when CM_Free() is called.)
3756 struct CtdlMessage *CtdlMakeMessage(
3757 struct ctdluser *author, /* author's user structure */
3758 char *recipient, /* NULL if it's not mail */
3759 char *recp_cc, /* NULL if it's not mail */
3760 char *room, /* room where it's going */
3761 int type, /* see MES_ types in header file */
3762 int format_type, /* variformat, plain text, MIME... */
3763 char *fake_name, /* who we're masquerading as */
3764 char *my_email, /* which of my email addresses to use (empty is ok) */
3765 char *subject, /* Subject (optional) */
3766 char *supplied_euid, /* ...or NULL if this is irrelevant */
3767 char *preformatted_text, /* ...or NULL to read text from client */
3768 char *references /* Thread references */
3770 char dest_node[256];
3772 struct CtdlMessage *msg;
3774 StrBuf *FakeEncAuthor = NULL;
3776 msg = malloc(sizeof(struct CtdlMessage));
3777 memset(msg, 0, sizeof(struct CtdlMessage));
3778 msg->cm_magic = CTDLMESSAGE_MAGIC;
3779 msg->cm_anon_type = type;
3780 msg->cm_format_type = format_type;
3782 /* Don't confuse the poor folks if it's not routed mail. */
3783 strcpy(dest_node, "");
3785 if (recipient != NULL) striplt(recipient);
3786 if (recp_cc != NULL) striplt(recp_cc);
3788 /* Path or Return-Path */
3789 if (my_email == NULL) my_email = "";
3791 if (!IsEmptyStr(my_email)) {
3792 msg->cm_fields[eMessagePath] = strdup(my_email);
3795 snprintf(buf, sizeof buf, "%s", author->fullname);
3796 msg->cm_fields[eMessagePath] = strdup(buf);
3798 convert_spaces_to_underscores(msg->cm_fields[eMessagePath]);
3800 snprintf(buf, sizeof buf, "%ld", (long)time(NULL)); /* timestamp */
3801 msg->cm_fields[eTimestamp] = strdup(buf);
3803 if ((fake_name != NULL) && (fake_name[0])) { /* author */
3804 FakeAuthor = NewStrBufPlain (fake_name, -1);
3807 FakeAuthor = NewStrBufPlain (author->fullname, -1);
3809 StrBufRFC2047encode(&FakeEncAuthor, FakeAuthor);
3810 msg->cm_fields[eAuthor] = SmashStrBuf(&FakeEncAuthor);
3811 FreeStrBuf(&FakeAuthor);
3813 if (CC->room.QRflags & QR_MAILBOX) { /* room */
3814 msg->cm_fields[eOriginalRoom] = strdup(&CC->room.QRname[11]);
3817 msg->cm_fields[eOriginalRoom] = strdup(CC->room.QRname);
3820 msg->cm_fields[eNodeName] = strdup(NODENAME); /* nodename */
3821 msg->cm_fields[eHumanNode] = strdup(HUMANNODE); /* hnodename */
3823 if ((recipient != NULL) && (recipient[0] != 0)) {
3824 msg->cm_fields[eRecipient] = strdup(recipient);
3826 if ((recp_cc != NULL) && (recp_cc[0] != 0)) {
3827 msg->cm_fields[eCarbonCopY] = strdup(recp_cc);
3829 if (dest_node[0] != 0) {
3830 msg->cm_fields[eDestination] = strdup(dest_node);
3833 if (!IsEmptyStr(my_email)) {
3834 msg->cm_fields[erFc822Addr] = strdup(my_email);
3836 else if ( (author == &CC->user) && (!IsEmptyStr(CC->cs_inet_email)) ) {
3837 msg->cm_fields[erFc822Addr] = strdup(CC->cs_inet_email);
3840 if (subject != NULL) {
3843 length = strlen(subject);
3849 while ((subject[i] != '\0') &&
3850 (IsAscii = isascii(subject[i]) != 0 ))
3853 msg->cm_fields[eMsgSubject] = strdup(subject);
3854 else /* ok, we've got utf8 in the string. */
3856 msg->cm_fields[eMsgSubject] = rfc2047encode(subject, length);
3862 if (supplied_euid != NULL) {
3863 msg->cm_fields[eExclusiveID] = strdup(supplied_euid);
3866 if ((references != NULL) && (!IsEmptyStr(references))) {
3867 if (msg->cm_fields[eWeferences] != NULL)
3868 free(msg->cm_fields[eWeferences]);
3869 msg->cm_fields[eWeferences] = strdup(references);
3872 if (preformatted_text != NULL) {
3873 msg->cm_fields[eMesageText] = preformatted_text;
3876 msg->cm_fields[eMesageText] = CtdlReadMessageBody(HKEY("000"), config.c_maxmsglen, NULL, 0, 0);
3885 * message entry - mode 0 (normal)
3887 void cmd_ent0(char *entargs)
3889 struct CitContext *CCC = CC;
3894 char supplied_euid[128];
3896 int format_type = 0;
3897 char newusername[256];
3898 char newuseremail[256];
3899 struct CtdlMessage *msg;
3903 struct recptypes *valid = NULL;
3904 struct recptypes *valid_to = NULL;
3905 struct recptypes *valid_cc = NULL;
3906 struct recptypes *valid_bcc = NULL;
3908 int subject_required = 0;
3913 int newuseremail_ok = 0;
3914 char references[SIZ];
3919 post = extract_int(entargs, 0);
3920 extract_token(recp, entargs, 1, '|', sizeof recp);
3921 anon_flag = extract_int(entargs, 2);
3922 format_type = extract_int(entargs, 3);
3923 extract_token(subject, entargs, 4, '|', sizeof subject);
3924 extract_token(newusername, entargs, 5, '|', sizeof newusername);
3925 do_confirm = extract_int(entargs, 6);
3926 extract_token(cc, entargs, 7, '|', sizeof cc);
3927 extract_token(bcc, entargs, 8, '|', sizeof bcc);
3928 switch(CC->room.QRdefaultview) {
3931 extract_token(supplied_euid, entargs, 9, '|', sizeof supplied_euid);
3934 supplied_euid[0] = 0;
3937 extract_token(newuseremail, entargs, 10, '|', sizeof newuseremail);
3938 extract_token(references, entargs, 11, '|', sizeof references);
3939 for (ptr=references; *ptr != 0; ++ptr) {
3940 if (*ptr == '!') *ptr = '|';
3943 /* first check to make sure the request is valid. */
3945 err = CtdlDoIHavePermissionToPostInThisRoom(
3950 (!IsEmptyStr(references)) /* is this a reply? or a top-level post? */
3954 cprintf("%d %s\n", err, errmsg);
3958 /* Check some other permission type things. */
3960 if (IsEmptyStr(newusername)) {
3961 strcpy(newusername, CCC->user.fullname);
3963 if ( (CCC->user.axlevel < AxAideU)
3964 && (strcasecmp(newusername, CCC->user.fullname))
3965 && (strcasecmp(newusername, CCC->cs_inet_fn))
3967 cprintf("%d You don't have permission to author messages as '%s'.\n",
3968 ERROR + HIGHER_ACCESS_REQUIRED,
3975 if (IsEmptyStr(newuseremail)) {
3976 newuseremail_ok = 1;
3979 if (!IsEmptyStr(newuseremail)) {
3980 if (!strcasecmp(newuseremail, CCC->cs_inet_email)) {
3981 newuseremail_ok = 1;
3983 else if (!IsEmptyStr(CCC->cs_inet_other_emails)) {
3984 j = num_tokens(CCC->cs_inet_other_emails, '|');
3985 for (i=0; i<j; ++i) {
3986 extract_token(buf, CCC->cs_inet_other_emails, i, '|', sizeof buf);
3987 if (!strcasecmp(newuseremail, buf)) {
3988 newuseremail_ok = 1;
3994 if (!newuseremail_ok) {
3995 cprintf("%d You don't have permission to author messages as '%s'.\n",
3996 ERROR + HIGHER_ACCESS_REQUIRED,
4002 CCC->cs_flags |= CS_POSTING;
4004 /* In mailbox rooms we have to behave a little differently --
4005 * make sure the user has specified at least one recipient. Then
4006 * validate the recipient(s). We do this for the Mail> room, as
4007 * well as any room which has the "Mailbox" view set - unless it
4008 * is the DRAFTS room which does not require recipients
4011 if ( ( ( (CCC->room.QRflags & QR_MAILBOX) && (!strcasecmp(&CCC->room.QRname[11], MAILROOM)) )
4012 || ( (CCC->room.QRflags & QR_MAILBOX) && (CCC->curr_view == VIEW_MAILBOX) )
4013 ) && (strcasecmp(&CCC->room.QRname[11], USERDRAFTROOM)) !=0 ) {
4014 if (CCC->user.axlevel < AxProbU) {
4015 strcpy(recp, "sysop");
4020 valid_to = validate_recipients(recp, NULL, 0);
4021 if (valid_to->num_error > 0) {
4022 cprintf("%d %s\n", ERROR + NO_SUCH_USER, valid_to->errormsg);
4023 free_recipients(valid_to);
4027 valid_cc = validate_recipients(cc, NULL, 0);
4028 if (valid_cc->num_error > 0) {
4029 cprintf("%d %s\n", ERROR + NO_SUCH_USER, valid_cc->errormsg);
4030 free_recipients(valid_to);
4031 free_recipients(valid_cc);
4035 valid_bcc = validate_recipients(bcc, NULL, 0);
4036 if (valid_bcc->num_error > 0) {
4037 cprintf("%d %s\n", ERROR + NO_SUCH_USER, valid_bcc->errormsg);
4038 free_recipients(valid_to);
4039 free_recipients(valid_cc);
4040 free_recipients(valid_bcc);
4044 /* Recipient required, but none were specified */
4045 if ( (valid_to->num_error < 0) && (valid_cc->num_error < 0) && (valid_bcc->num_error < 0) ) {
4046 free_recipients(valid_to);
4047 free_recipients(valid_cc);
4048 free_recipients(valid_bcc);
4049 cprintf("%d At least one recipient is required.\n", ERROR + NO_SUCH_USER);
4053 if (valid_to->num_internet + valid_cc->num_internet + valid_bcc->num_internet > 0) {
4054 if (CtdlCheckInternetMailPermission(&CCC->user)==0) {
4055 cprintf("%d You do not have permission "
4056 "to send Internet mail.\n",
4057 ERROR + HIGHER_ACCESS_REQUIRED);
4058 free_recipients(valid_to);
4059 free_recipients(valid_cc);
4060 free_recipients(valid_bcc);
4065 if ( ( (valid_to->num_internet + valid_to->num_ignet + valid_cc->num_internet + valid_cc->num_ignet + valid_bcc->num_internet + valid_bcc->num_ignet) > 0)
4066 && (CCC->user.axlevel < AxNetU) ) {
4067 cprintf("%d Higher access required for network mail.\n",
4068 ERROR + HIGHER_ACCESS_REQUIRED);
4069 free_recipients(valid_to);
4070 free_recipients(valid_cc);
4071 free_recipients(valid_bcc);
4075 if ((RESTRICT_INTERNET == 1)
4076 && (valid_to->num_internet + valid_cc->num_internet + valid_bcc->num_internet > 0)
4077 && ((CCC->user.flags & US_INTERNET) == 0)
4078 && (!CCC->internal_pgm)) {
4079 cprintf("%d You don't have access to Internet mail.\n",
4080 ERROR + HIGHER_ACCESS_REQUIRED);
4081 free_recipients(valid_to);
4082 free_recipients(valid_cc);
4083 free_recipients(valid_bcc);
4089 /* Is this a room which has anonymous-only or anonymous-option? */
4090 anonymous = MES_NORMAL;
4091 if (CCC->room.QRflags & QR_ANONONLY) {
4092 anonymous = MES_ANONONLY;
4094 if (CCC->room.QRflags & QR_ANONOPT) {
4095 if (anon_flag == 1) { /* only if the user requested it */
4096 anonymous = MES_ANONOPT;
4100 if ((CCC->room.QRflags & QR_MAILBOX) == 0) {
4104 /* Recommend to the client that the use of a message subject is
4105 * strongly recommended in this room, if either the SUBJECTREQ flag
4106 * is set, or if there is one or more Internet email recipients.
4108 if (CCC->room.QRflags2 & QR2_SUBJECTREQ) subject_required = 1;
4109 if ((valid_to) && (valid_to->num_internet > 0)) subject_required = 1;
4110 if ((valid_cc) && (valid_cc->num_internet > 0)) subject_required = 1;
4111 if ((valid_bcc) && (valid_bcc->num_internet > 0)) subject_required = 1;
4113 /* If we're only checking the validity of the request, return
4114 * success without creating the message.
4117 cprintf("%d %s|%d\n", CIT_OK,
4118 ((valid_to != NULL) ? valid_to->display_recp : ""),
4120 free_recipients(valid_to);
4121 free_recipients(valid_cc);
4122 free_recipients(valid_bcc);
4126 /* We don't need these anymore because we'll do it differently below */
4127 free_recipients(valid_to);
4128 free_recipients(valid_cc);
4129 free_recipients(valid_bcc);
4131 /* Read in the message from the client. */
4133 cprintf("%d send message\n", START_CHAT_MODE);
4135 cprintf("%d send message\n", SEND_LISTING);
4138 msg = CtdlMakeMessage(&CCC->user, recp, cc,
4139 CCC->room.QRname, anonymous, format_type,
4140 newusername, newuseremail, subject,
4141 ((!IsEmptyStr(supplied_euid)) ? supplied_euid : NULL),
4144 /* Put together one big recipients struct containing to/cc/bcc all in
4145 * one. This is for the envelope.
4147 char *all_recps = malloc(SIZ * 3);
4148 strcpy(all_recps, recp);
4149 if (!IsEmptyStr(cc)) {
4150 if (!IsEmptyStr(all_recps)) {
4151 strcat(all_recps, ",");
4153 strcat(all_recps, cc);
4155 if (!IsEmptyStr(bcc)) {
4156 if (!IsEmptyStr(all_recps)) {
4157 strcat(all_recps, ",");
4159 strcat(all_recps, bcc);
4161 if (!IsEmptyStr(all_recps)) {
4162 valid = validate_recipients(all_recps, NULL, 0);
4169 if ((valid != NULL) && (valid->num_room == 1))
4171 /* posting into an ML room? set the envelope from
4172 * to the actual mail address so others get a valid
4175 msg->cm_fields[eenVelopeTo] = strdup(valid->recp_orgroom);
4179 msgnum = CtdlSubmitMsg(msg, valid, "", QP_EADDR);
4181 cprintf("%ld\n", msgnum);
4183 if (StrLength(CCC->StatusMessage) > 0) {
4184 cprintf("%s\n", ChrPtr(CCC->StatusMessage));
4186 else if (msgnum >= 0L) {
4187 client_write(HKEY("Message accepted.\n"));
4190 client_write(HKEY("Internal error.\n"));
4193 if (!CM_IsEmpty(msg, eExclusiveID)) {
4194 cprintf("%s\n", msg->cm_fields[eExclusiveID]);
4203 if (valid != NULL) {
4204 free_recipients(valid);
4212 * API function to delete messages which match a set of criteria
4213 * (returns the actual number of messages deleted)
4215 int CtdlDeleteMessages(char *room_name, /* which room */
4216 long *dmsgnums, /* array of msg numbers to be deleted */
4217 int num_dmsgnums, /* number of msgs to be deleted, or 0 for "any" */
4218 char *content_type /* or "" for any. regular expressions expected. */
4221 struct CitContext *CCC = CC;
4222 struct ctdlroom qrbuf;
4223 struct cdbdata *cdbfr;
4224 long *msglist = NULL;
4225 long *dellist = NULL;
4228 int num_deleted = 0;
4230 struct MetaData smi;
4233 int need_to_free_re = 0;
4235 if (content_type) if (!IsEmptyStr(content_type)) {
4236 regcomp(&re, content_type, 0);
4237 need_to_free_re = 1;
4239 MSG_syslog(LOG_DEBUG, " CtdlDeleteMessages(%s, %d msgs, %s)\n",
4240 room_name, num_dmsgnums, content_type);
4242 /* get room record, obtaining a lock... */
4243 if (CtdlGetRoomLock(&qrbuf, room_name) != 0) {
4244 MSG_syslog(LOG_ERR, " CtdlDeleteMessages(): Room <%s> not found\n",
4246 if (need_to_free_re) regfree(&re);
4247 return (0); /* room not found */
4249 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
4251 if (cdbfr != NULL) {
4252 dellist = malloc(cdbfr->len);
4253 msglist = (long *) cdbfr->ptr;
4254 cdbfr->ptr = NULL; /* CtdlDeleteMessages() now owns this memory */
4255 num_msgs = cdbfr->len / sizeof(long);
4259 int have_contenttype = (content_type != NULL) && !IsEmptyStr(content_type);
4260 int have_delmsgs = (num_dmsgnums == 0) || (dmsgnums == NULL);
4261 int have_more_del = 1;
4263 num_msgs = sort_msglist(msglist, num_msgs);
4264 if (num_dmsgnums > 1)
4265 num_dmsgnums = sort_msglist(dmsgnums, num_dmsgnums);
4268 StrBuf *dbg = NewStrBuf();
4269 for (i = 0; i < num_dmsgnums; i++)
4270 StrBufAppendPrintf(dbg, ", %ld", dmsgnums[i]);
4271 MSG_syslog(LOG_DEBUG, " Deleting before: %s", ChrPtr(dbg));
4276 while ((i < num_msgs) && (have_more_del)) {
4279 /* Set/clear a bit for each criterion */
4281 /* 0 messages in the list or a null list means that we are
4282 * interested in deleting any messages which meet the other criteria.
4285 delete_this |= 0x01;
4288 while ((i < num_msgs) && (msglist[i] < dmsgnums[j])) i++;
4293 if (msglist[i] == dmsgnums[j]) {
4294 delete_this |= 0x01;
4297 have_more_del = (j < num_dmsgnums);
4300 if (have_contenttype) {
4301 GetMetaData(&smi, msglist[i]);
4302 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) == 0) {
4303 delete_this |= 0x02;
4306 delete_this |= 0x02;
4309 /* Delete message only if all bits are set */
4310 if (delete_this == 0x03) {
4311 dellist[num_deleted++] = msglist[i];
4318 StrBuf *dbg = NewStrBuf();
4319 for (i = 0; i < num_deleted; i++)
4320 StrBufAppendPrintf(dbg, ", %ld", dellist[i]);
4321 MSG_syslog(LOG_DEBUG, " Deleting: %s", ChrPtr(dbg));
4325 num_msgs = sort_msglist(msglist, num_msgs);
4326 cdb_store(CDB_MSGLISTS, &qrbuf.QRnumber, (int)sizeof(long),
4327 msglist, (int)(num_msgs * sizeof(long)));
4330 qrbuf.QRhighest = msglist[num_msgs - 1];
4332 qrbuf.QRhighest = 0;
4334 CtdlPutRoomLock(&qrbuf);
4336 /* Go through the messages we pulled out of the index, and decrement
4337 * their reference counts by 1. If this is the only room the message
4338 * was in, the reference count will reach zero and the message will
4339 * automatically be deleted from the database. We do this in a
4340 * separate pass because there might be plug-in hooks getting called,
4341 * and we don't want that happening during an S_ROOMS critical
4345 for (i=0; i<num_deleted; ++i) {
4346 PerformDeleteHooks(qrbuf.QRname, dellist[i]);
4348 AdjRefCountList(dellist, num_deleted, -1);
4350 /* Now free the memory we used, and go away. */
4351 if (msglist != NULL) free(msglist);
4352 if (dellist != NULL) free(dellist);
4353 MSG_syslog(LOG_DEBUG, " %d message(s) deleted.\n", num_deleted);
4354 if (need_to_free_re) regfree(&re);
4355 return (num_deleted);
4359 * Delete message from current room
4361 void cmd_dele(char *args)
4370 extract_token(msgset, args, 0, '|', sizeof msgset);
4371 num_msgs = num_tokens(msgset, ',');
4373 cprintf("%d Nothing to do.\n", CIT_OK);
4377 if (CtdlDoIHavePermissionToDeleteMessagesFromThisRoom() == 0) {
4378 cprintf("%d Higher access required.\n",
4379 ERROR + HIGHER_ACCESS_REQUIRED);
4384 * Build our message set to be moved/copied
4386 msgs = malloc(num_msgs * sizeof(long));
4387 for (i=0; i<num_msgs; ++i) {
4388 extract_token(msgtok, msgset, i, ',', sizeof msgtok);
4389 msgs[i] = atol(msgtok);
4392 num_deleted = CtdlDeleteMessages(CC->room.QRname, msgs, num_msgs, "");
4396 cprintf("%d %d message%s deleted.\n", CIT_OK,
4397 num_deleted, ((num_deleted != 1) ? "s" : ""));
4399 cprintf("%d Message not found.\n", ERROR + MESSAGE_NOT_FOUND);
4407 * move or copy a message to another room
4409 void cmd_move(char *args)
4416 char targ[ROOMNAMELEN];
4417 struct ctdlroom qtemp;
4424 extract_token(msgset, args, 0, '|', sizeof msgset);
4425 num_msgs = num_tokens(msgset, ',');
4427 cprintf("%d Nothing to do.\n", CIT_OK);
4431 extract_token(targ, args, 1, '|', sizeof targ);
4432 convert_room_name_macros(targ, sizeof targ);
4433 targ[ROOMNAMELEN - 1] = 0;
4434 is_copy = extract_int(args, 2);
4436 if (CtdlGetRoom(&qtemp, targ) != 0) {
4437 cprintf("%d '%s' does not exist.\n", ERROR + ROOM_NOT_FOUND, targ);
4441 if (!strcasecmp(qtemp.QRname, CC->room.QRname)) {
4442 cprintf("%d Source and target rooms are the same.\n", ERROR + ALREADY_EXISTS);
4446 CtdlGetUser(&CC->user, CC->curr_user);
4447 CtdlRoomAccess(&qtemp, &CC->user, &ra, NULL);
4449 /* Check for permission to perform this operation.
4450 * Remember: "CC->room" is source, "qtemp" is target.
4454 /* Admins can move/copy */
4455 if (CC->user.axlevel >= AxAideU) permit = 1;
4457 /* Room aides can move/copy */
4458 if (CC->user.usernum == CC->room.QRroomaide) permit = 1;
4460 /* Permit move/copy from personal rooms */
4461 if ((CC->room.QRflags & QR_MAILBOX)
4462 && (qtemp.QRflags & QR_MAILBOX)) permit = 1;
4464 /* Permit only copy from public to personal room */
4466 && (!(CC->room.QRflags & QR_MAILBOX))
4467 && (qtemp.QRflags & QR_MAILBOX)) permit = 1;
4469 /* Permit message removal from collaborative delete rooms */
4470 if (CC->room.QRflags2 & QR2_COLLABDEL) permit = 1;
4472 /* Users allowed to post into the target room may move into it too. */
4473 if ((CC->room.QRflags & QR_MAILBOX) &&
4474 (qtemp.QRflags & UA_POSTALLOWED)) permit = 1;
4476 /* User must have access to target room */
4477 if (!(ra & UA_KNOWN)) permit = 0;
4480 cprintf("%d Higher access required.\n",
4481 ERROR + HIGHER_ACCESS_REQUIRED);
4486 * Build our message set to be moved/copied
4488 msgs = malloc(num_msgs * sizeof(long));
4489 for (i=0; i<num_msgs; ++i) {
4490 extract_token(msgtok, msgset, i, ',', sizeof msgtok);
4491 msgs[i] = atol(msgtok);
4497 err = CtdlSaveMsgPointersInRoom(targ, msgs, num_msgs, 1, NULL, 0);
4499 cprintf("%d Cannot store message(s) in %s: error %d\n",
4505 /* Now delete the message from the source room,
4506 * if this is a 'move' rather than a 'copy' operation.
4509 CtdlDeleteMessages(CC->room.QRname, msgs, num_msgs, "");
4513 cprintf("%d Message(s) %s.\n", CIT_OK, (is_copy ? "copied" : "moved") );
4519 * GetMetaData() - Get the supplementary record for a message
4521 void GetMetaData(struct MetaData *smibuf, long msgnum)
4524 struct cdbdata *cdbsmi;
4527 memset(smibuf, 0, sizeof(struct MetaData));
4528 smibuf->meta_msgnum = msgnum;
4529 smibuf->meta_refcount = 1; /* Default reference count is 1 */
4531 /* Use the negative of the message number for its supp record index */
4532 TheIndex = (0L - msgnum);
4534 cdbsmi = cdb_fetch(CDB_MSGMAIN, &TheIndex, sizeof(long));
4535 if (cdbsmi == NULL) {
4536 return; /* record not found; go with defaults */
4538 memcpy(smibuf, cdbsmi->ptr,
4539 ((cdbsmi->len > sizeof(struct MetaData)) ?
4540 sizeof(struct MetaData) : cdbsmi->len));
4547 * PutMetaData() - (re)write supplementary record for a message
4549 void PutMetaData(struct MetaData *smibuf)
4553 /* Use the negative of the message number for the metadata db index */
4554 TheIndex = (0L - smibuf->meta_msgnum);
4556 cdb_store(CDB_MSGMAIN,
4557 &TheIndex, (int)sizeof(long),
4558 smibuf, (int)sizeof(struct MetaData));
4563 * AdjRefCount - submit an adjustment to the reference count for a message.
4564 * (These are just queued -- we actually process them later.)
4566 void AdjRefCount(long msgnum, int incr)
4568 struct CitContext *CCC = CC;
4569 struct arcq new_arcq;
4572 MSG_syslog(LOG_DEBUG, "AdjRefCount() msg %ld ref count delta %+d\n", msgnum, incr);
4574 begin_critical_section(S_SUPPMSGMAIN);
4575 if (arcfp == NULL) {
4576 arcfp = fopen(file_arcq, "ab+");
4577 chown(file_arcq, CTDLUID, (-1));
4578 chmod(file_arcq, 0600);
4580 end_critical_section(S_SUPPMSGMAIN);
4582 /* msgnum < 0 means that we're trying to close the file */
4584 MSGM_syslog(LOG_DEBUG, "Closing the AdjRefCount queue file\n");
4585 begin_critical_section(S_SUPPMSGMAIN);
4586 if (arcfp != NULL) {
4590 end_critical_section(S_SUPPMSGMAIN);
4595 * If we can't open the queue, perform the operation synchronously.
4597 if (arcfp == NULL) {
4598 TDAP_AdjRefCount(msgnum, incr);
4602 new_arcq.arcq_msgnum = msgnum;
4603 new_arcq.arcq_delta = incr;
4604 rv = fwrite(&new_arcq, sizeof(struct arcq), 1, arcfp);
4606 MSG_syslog(LOG_EMERG, "Couldn't write Refcount Queue File %s: %s\n",
4615 void AdjRefCountList(long *msgnum, long nmsg, int incr)
4617 struct CitContext *CCC = CC;
4618 long i, the_size, offset;
4619 struct arcq *new_arcq;
4622 MSG_syslog(LOG_DEBUG, "AdjRefCountList() msg %ld ref count delta %+d\n", nmsg, incr);
4624 begin_critical_section(S_SUPPMSGMAIN);
4625 if (arcfp == NULL) {
4626 arcfp = fopen(file_arcq, "ab+");
4627 chown(file_arcq, CTDLUID, (-1));
4628 chmod(file_arcq, 0600);
4630 end_critical_section(S_SUPPMSGMAIN);
4633 * If we can't open the queue, perform the operation synchronously.
4635 if (arcfp == NULL) {
4636 for (i = 0; i < nmsg; i++)
4637 TDAP_AdjRefCount(msgnum[i], incr);
4641 the_size = sizeof(struct arcq) * nmsg;
4642 new_arcq = malloc(the_size);
4643 for (i = 0; i < nmsg; i++) {
4644 new_arcq[i].arcq_msgnum = msgnum[i];
4645 new_arcq[i].arcq_delta = incr;
4649 while ((rv >= 0) && (offset < the_size))
4651 rv = fwrite(new_arcq + offset, 1, the_size - offset, arcfp);
4653 MSG_syslog(LOG_EMERG, "Couldn't write Refcount Queue File %s: %s\n",
4669 * TDAP_ProcessAdjRefCountQueue()
4671 * Process the queue of message count adjustments that was created by calls
4672 * to AdjRefCount() ... by reading the queue and calling TDAP_AdjRefCount()
4673 * for each one. This should be an "off hours" operation.
4675 int TDAP_ProcessAdjRefCountQueue(void)
4677 struct CitContext *CCC = CC;
4678 char file_arcq_temp[PATH_MAX];
4681 struct arcq arcq_rec;
4682 int num_records_processed = 0;
4684 snprintf(file_arcq_temp, sizeof file_arcq_temp, "%s.%04x", file_arcq, rand());
4686 begin_critical_section(S_SUPPMSGMAIN);
4687 if (arcfp != NULL) {
4692 r = link(file_arcq, file_arcq_temp);
4694 MSG_syslog(LOG_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
4695 end_critical_section(S_SUPPMSGMAIN);
4696 return(num_records_processed);
4700 end_critical_section(S_SUPPMSGMAIN);
4702 fp = fopen(file_arcq_temp, "rb");
4704 MSG_syslog(LOG_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
4705 return(num_records_processed);
4708 while (fread(&arcq_rec, sizeof(struct arcq), 1, fp) == 1) {
4709 TDAP_AdjRefCount(arcq_rec.arcq_msgnum, arcq_rec.arcq_delta);
4710 ++num_records_processed;
4714 r = unlink(file_arcq_temp);
4716 MSG_syslog(LOG_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
4719 return(num_records_processed);
4725 * TDAP_AdjRefCount - adjust the reference count for a message.
4726 * This one does it "for real" because it's called by
4727 * the autopurger function that processes the queue
4728 * created by AdjRefCount(). If a message's reference
4729 * count becomes zero, we also delete the message from
4730 * disk and de-index it.
4732 void TDAP_AdjRefCount(long msgnum, int incr)
4734 struct CitContext *CCC = CC;
4736 struct MetaData smi;
4739 /* This is a *tight* critical section; please keep it that way, as
4740 * it may get called while nested in other critical sections.
4741 * Complicating this any further will surely cause deadlock!
4743 begin_critical_section(S_SUPPMSGMAIN);
4744 GetMetaData(&smi, msgnum);
4745 smi.meta_refcount += incr;
4747 end_critical_section(S_SUPPMSGMAIN);
4748 MSG_syslog(LOG_DEBUG, "TDAP_AdjRefCount() msg %ld ref count delta %+d, is now %d\n",
4749 msgnum, incr, smi.meta_refcount
4752 /* If the reference count is now zero, delete the message
4753 * (and its supplementary record as well).
4755 if (smi.meta_refcount == 0) {
4756 MSG_syslog(LOG_DEBUG, "Deleting message <%ld>\n", msgnum);
4758 /* Call delete hooks with NULL room to show it has gone altogether */
4759 PerformDeleteHooks(NULL, msgnum);
4761 /* Remove from message base */
4763 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
4764 cdb_delete(CDB_BIGMSGS, &delnum, (int)sizeof(long));
4766 /* Remove metadata record */
4767 delnum = (0L - msgnum);
4768 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
4774 * Write a generic object to this room
4776 * Note: this could be much more efficient. Right now we use two temporary
4777 * files, and still pull the message into memory as with all others.
4779 void CtdlWriteObject(char *req_room, /* Room to stuff it in */
4780 char *content_type, /* MIME type of this object */
4781 char *raw_message, /* Data to be written */
4782 off_t raw_length, /* Size of raw_message */
4783 struct ctdluser *is_mailbox, /* Mailbox room? */
4784 int is_binary, /* Is encoding necessary? */
4785 int is_unique, /* Del others of this type? */
4786 unsigned int flags /* Internal save flags */
4789 struct CitContext *CCC = CC;
4790 struct ctdlroom qrbuf;
4791 char roomname[ROOMNAMELEN];
4792 struct CtdlMessage *msg;
4793 char *encoded_message = NULL;
4795 if (is_mailbox != NULL) {
4796 CtdlMailboxName(roomname, sizeof roomname, is_mailbox, req_room);
4799 safestrncpy(roomname, req_room, sizeof(roomname));
4802 MSG_syslog(LOG_DEBUG, "Raw length is %ld\n", (long)raw_length);
4805 encoded_message = malloc((size_t) (((raw_length * 134) / 100) + 4096 ) );
4808 encoded_message = malloc((size_t)(raw_length + 4096));
4811 sprintf(encoded_message, "Content-type: %s\n", content_type);
4814 sprintf(&encoded_message[strlen(encoded_message)],
4815 "Content-transfer-encoding: base64\n\n"
4819 sprintf(&encoded_message[strlen(encoded_message)],
4820 "Content-transfer-encoding: 7bit\n\n"
4826 &encoded_message[strlen(encoded_message)],
4834 &encoded_message[strlen(encoded_message)],
4840 MSGM_syslog(LOG_DEBUG, "Allocating\n");
4841 msg = malloc(sizeof(struct CtdlMessage));
4842 memset(msg, 0, sizeof(struct CtdlMessage));
4843 msg->cm_magic = CTDLMESSAGE_MAGIC;
4844 msg->cm_anon_type = MES_NORMAL;
4845 msg->cm_format_type = 4;
4846 msg->cm_fields[eAuthor] = strdup(CCC->user.fullname);
4847 msg->cm_fields[eOriginalRoom] = strdup(req_room);
4848 msg->cm_fields[eNodeName] = strdup(config.c_nodename);
4849 msg->cm_fields[eHumanNode] = strdup(config.c_humannode);
4850 msg->cm_flags = flags;
4852 msg->cm_fields[eMesageText] = encoded_message;
4854 /* Create the requested room if we have to. */
4855 if (CtdlGetRoom(&qrbuf, roomname) != 0) {
4856 CtdlCreateRoom(roomname,
4857 ( (is_mailbox != NULL) ? 5 : 3 ),
4858 "", 0, 1, 0, VIEW_BBS);
4860 /* If the caller specified this object as unique, delete all
4861 * other objects of this type that are currently in the room.
4864 MSG_syslog(LOG_DEBUG, "Deleted %d other msgs of this type\n",
4865 CtdlDeleteMessages(roomname, NULL, 0, content_type)
4868 /* Now write the data */
4869 CtdlSubmitMsg(msg, NULL, roomname, 0);
4878 void CtdlGetSysConfigBackend(long msgnum, void *userdata) {
4879 config_msgnum = msgnum;
4883 char *CtdlGetSysConfig(char *sysconfname) {
4884 char hold_rm[ROOMNAMELEN];
4887 struct CtdlMessage *msg;
4890 strcpy(hold_rm, CC->room.QRname);
4891 if (CtdlGetRoom(&CC->room, SYSCONFIGROOM) != 0) {
4892 CtdlGetRoom(&CC->room, hold_rm);
4897 /* We want the last (and probably only) config in this room */
4898 begin_critical_section(S_CONFIG);
4899 config_msgnum = (-1L);
4900 CtdlForEachMessage(MSGS_LAST, 1, NULL, sysconfname, NULL,
4901 CtdlGetSysConfigBackend, NULL);
4902 msgnum = config_msgnum;
4903 end_critical_section(S_CONFIG);
4909 msg = CtdlFetchMessage(msgnum, 1);
4911 conf = strdup(msg->cm_fields[eMesageText]);
4919 CtdlGetRoom(&CC->room, hold_rm);
4921 if (conf != NULL) do {
4922 extract_token(buf, conf, 0, '\n', sizeof buf);
4923 strcpy(conf, &conf[strlen(buf)+1]);
4924 } while ( (!IsEmptyStr(conf)) && (!IsEmptyStr(buf)) );
4930 void CtdlPutSysConfig(char *sysconfname, char *sysconfdata) {
4931 CtdlWriteObject(SYSCONFIGROOM, sysconfname, sysconfdata, (strlen(sysconfdata)+1), NULL, 0, 1, 0);
4936 /*****************************************************************************/
4937 /* MODULE INITIALIZATION STUFF */
4938 /*****************************************************************************/
4939 void SetMessageDebugEnabled(const int n)
4941 MessageDebugEnabled = n;
4943 CTDL_MODULE_INIT(msgbase)
4946 CtdlRegisterDebugFlagHook(HKEY("messages"), SetMessageDebugEnabled, &MessageDebugEnabled);
4948 CtdlRegisterProtoHook(cmd_msgs, "MSGS", "Output a list of messages in the current room");
4949 CtdlRegisterProtoHook(cmd_msg0, "MSG0", "Output a message in plain text format");
4950 CtdlRegisterProtoHook(cmd_msg2, "MSG2", "Output a message in RFC822 format");
4951 CtdlRegisterProtoHook(cmd_msg3, "MSG3", "Output a message in raw format (deprecated)");
4952 CtdlRegisterProtoHook(cmd_msg4, "MSG4", "Output a message in the client's preferred format");
4953 CtdlRegisterProtoHook(cmd_msgp, "MSGP", "Select preferred format for MSG4 output");
4954 CtdlRegisterProtoHook(cmd_opna, "OPNA", "Open an attachment for download");
4955 CtdlRegisterProtoHook(cmd_dlat, "DLAT", "Download an attachment");
4956 CtdlRegisterProtoHook(cmd_ent0, "ENT0", "Enter a message");
4957 CtdlRegisterProtoHook(cmd_dele, "DELE", "Delete a message");
4958 CtdlRegisterProtoHook(cmd_move, "MOVE", "Move or copy a message to another room");
4961 /* return our Subversion id for the Log */