2 * Implements the message store.
4 * Copyright (c) 1987-2012 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
26 # include <sys/time.h>
39 #include <sys/types.h>
44 #include <libcitadel.h>
47 #include "serv_extensions.h"
51 #include "sysdep_decls.h"
52 #include "citserver.h"
59 #include "internet_addressing.h"
60 #include "euidindex.h"
61 #include "journaling.h"
62 #include "citadel_dirs.h"
63 #include "clientsocket.h"
66 #include "ctdl_module.h"
68 struct addresses_to_be_filed *atbf = NULL;
70 /* This temp file holds the queue of operations for AdjRefCount() */
71 static FILE *arcfp = NULL;
72 void AdjRefCountList(long *msgnum, long nmsg, int incr);
74 int MessageDebugEnabled = 0;
77 * These are the four-character field headers we use when outputting
78 * messages in Citadel format (as opposed to RFC822 format).
81 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
82 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
83 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
84 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
85 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
86 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
87 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
88 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
118 eMsgField FieldOrder[] = {
119 /* Important fields */
130 /* Semi-important fields */
136 /* G is not used yet, may become virus signature*/
139 /* Q is not used yet */
142 /* X is not used yet */
143 /* Z is not used yet */
150 /* Message text (MUST be last) */
152 /* Not saved to disk:
157 static const long NDiskFields = sizeof(FieldOrder) / sizeof(eMsgField);
159 int CM_IsEmpty(struct CtdlMessage *Msg, eMsgField which)
161 return !((Msg->cm_fields[which] != NULL) &&
162 (Msg->cm_fields[which][0] != '\0'));
165 void CM_SetField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
167 if (Msg->cm_fields[which] != NULL)
168 free (Msg->cm_fields[which]);
169 Msg->cm_fields[which] = malloc(length + 1);
170 memcpy(Msg->cm_fields[which], buf, length);
171 Msg->cm_fields[which][length] = '\0';
174 void CM_SetFieldLONG(struct CtdlMessage *Msg, eMsgField which, long lvalue)
178 len = snprintf(buf, sizeof(buf), "%ld", lvalue);
179 CM_SetField(Msg, which, buf, len);
181 void CM_CutFieldAt(struct CtdlMessage *Msg, eMsgField WhichToCut, long maxlen)
183 if (Msg->cm_fields[WhichToCut] == NULL)
186 if (strlen(Msg->cm_fields[WhichToCut]) > maxlen)
187 Msg->cm_fields[WhichToCut][maxlen] = '\0';
190 void CM_FlushField(struct CtdlMessage *Msg, eMsgField which)
192 if (Msg->cm_fields[which] != NULL)
193 free (Msg->cm_fields[which]);
194 Msg->cm_fields[which] = NULL;
197 void CM_CopyField(struct CtdlMessage *Msg, eMsgField WhichToPutTo, eMsgField WhichtToCopy)
200 if (Msg->cm_fields[WhichToPutTo] != NULL)
201 free (Msg->cm_fields[WhichToPutTo]);
203 if (Msg->cm_fields[WhichtToCopy] != NULL)
205 len = strlen(Msg->cm_fields[WhichtToCopy]);
206 Msg->cm_fields[WhichToPutTo] = malloc(len + 1);
207 memcpy(Msg->cm_fields[WhichToPutTo], Msg->cm_fields[WhichToPutTo], len);
208 Msg->cm_fields[WhichToPutTo][len] = '\0';
211 Msg->cm_fields[WhichToPutTo] = NULL;
215 void CM_PrependToField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
217 if (Msg->cm_fields[which] != NULL) {
222 oldmsgsize = strlen(Msg->cm_fields[which]) + 1;
223 newmsgsize = length + oldmsgsize;
225 new = malloc(newmsgsize);
226 memcpy(new, buf, length);
227 memcpy(new + length, Msg->cm_fields[which], oldmsgsize);
228 free(Msg->cm_fields[which]);
229 Msg->cm_fields[which] = new;
232 Msg->cm_fields[which] = malloc(length + 1);
233 memcpy(Msg->cm_fields[which], buf, length);
234 Msg->cm_fields[which][length] = '\0';
238 void CM_SetAsField(struct CtdlMessage *Msg, eMsgField which, char **buf, long length)
240 if (Msg->cm_fields[which] != NULL)
241 free (Msg->cm_fields[which]);
243 Msg->cm_fields[which] = *buf;
247 void CM_SetAsFieldSB(struct CtdlMessage *Msg, eMsgField which, StrBuf **buf)
249 if (Msg->cm_fields[which] != NULL)
250 free (Msg->cm_fields[which]);
252 Msg->cm_fields[which] = SmashStrBuf(buf);
255 void CM_GetAsField(struct CtdlMessage *Msg, eMsgField which, char **ret, long *retlen)
257 if (Msg->cm_fields[which] != NULL)
259 *retlen = strlen(Msg->cm_fields[which]);
260 *ret = Msg->cm_fields[which];
261 Msg->cm_fields[which] = NULL;
271 * Returns 1 if the supplied pointer points to a valid Citadel message.
272 * If the pointer is NULL or the magic number check fails, returns 0.
274 int CM_IsValidMsg(struct CtdlMessage *msg) {
277 if ((msg->cm_magic) != CTDLMESSAGE_MAGIC) {
278 struct CitContext *CCC = CC;
279 MSGM_syslog(LOG_WARNING, "CM_IsValidMsg() -- self-check failed\n");
285 void CM_FreeContents(struct CtdlMessage *msg)
289 for (i = 0; i < 256; ++i)
290 if (msg->cm_fields[i] != NULL) {
291 free(msg->cm_fields[i]);
294 msg->cm_magic = 0; /* just in case */
297 * 'Destructor' for struct CtdlMessage
299 void CM_Free(struct CtdlMessage *msg)
301 if (CM_IsValidMsg(msg) == 0)
303 if (msg != NULL) free (msg);
306 CM_FreeContents(msg);
310 int CM_DupField(eMsgField i, struct CtdlMessage *OrgMsg, struct CtdlMessage *NewMsg)
313 len = strlen(OrgMsg->cm_fields[i]);
314 NewMsg->cm_fields[i] = malloc(len + 1);
315 if (NewMsg->cm_fields[i] == NULL)
317 memcpy(NewMsg->cm_fields[i], OrgMsg->cm_fields[i], len);
318 NewMsg->cm_fields[i][len] = '\0';
322 struct CtdlMessage * CM_Duplicate(struct CtdlMessage *OrgMsg)
325 struct CtdlMessage *NewMsg;
327 if (CM_IsValidMsg(OrgMsg) == 0)
329 NewMsg = (struct CtdlMessage *)malloc(sizeof(struct CtdlMessage));
333 memcpy(NewMsg, OrgMsg, sizeof(struct CtdlMessage));
335 memset(&NewMsg->cm_fields, 0, sizeof(char*) * 256);
337 for (i = 0; i < 256; ++i)
339 if (OrgMsg->cm_fields[i] != NULL)
341 if (!CM_DupField(i, OrgMsg, NewMsg))
355 * Back end for the MSGS command: output message number only.
357 void simple_listing(long msgnum, void *userdata)
359 cprintf("%ld\n", msgnum);
365 * Back end for the MSGS command: output header summary.
367 void headers_listing(long msgnum, void *userdata)
369 struct CtdlMessage *msg;
371 msg = CtdlFetchMessage(msgnum, 0);
373 cprintf("%ld|0|||||\n", msgnum);
377 cprintf("%ld|%s|%s|%s|%s|%s|\n",
379 (!CM_IsEmpty(msg, eTimestamp) ? msg->cm_fields[eTimestamp] : "0"),
380 (!CM_IsEmpty(msg, eAuthor) ? msg->cm_fields[eAuthor] : ""),
381 (!CM_IsEmpty(msg, eNodeName) ? msg->cm_fields[eNodeName] : ""),
382 (!CM_IsEmpty(msg, erFc822Addr) ? msg->cm_fields[erFc822Addr] : ""),
383 (!CM_IsEmpty(msg, eMsgSubject) ? msg->cm_fields[eMsgSubject] : "")
389 * Back end for the MSGS command: output EUID header.
391 void headers_euid(long msgnum, void *userdata)
393 struct CtdlMessage *msg;
395 msg = CtdlFetchMessage(msgnum, 0);
397 cprintf("%ld||\n", msgnum);
401 cprintf("%ld|%s|%s\n",
403 (!CM_IsEmpty(msg, eExclusiveID) ? msg->cm_fields[eExclusiveID] : ""),
404 (!CM_IsEmpty(msg, eTimestamp) ? msg->cm_fields[eTimestamp] : "0"));
412 /* Determine if a given message matches the fields in a message template.
413 * Return 0 for a successful match.
415 int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template) {
418 /* If there aren't any fields in the template, all messages will
421 if (template == NULL) return(0);
423 /* Null messages are bogus. */
424 if (msg == NULL) return(1);
426 for (i='A'; i<='Z'; ++i) {
427 if (template->cm_fields[i] != NULL) {
428 if (msg->cm_fields[i] == NULL) {
429 /* Considered equal if temmplate is empty string */
430 if (IsEmptyStr(template->cm_fields[i])) continue;
433 if (strcasecmp(msg->cm_fields[i],
434 template->cm_fields[i])) return 1;
438 /* All compares succeeded: we have a match! */
445 * Retrieve the "seen" message list for the current room.
447 void CtdlGetSeen(char *buf, int which_set) {
448 struct CitContext *CCC = CC;
451 /* Learn about the user and room in question */
452 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
454 if (which_set == ctdlsetseen_seen)
455 safestrncpy(buf, vbuf.v_seen, SIZ);
456 if (which_set == ctdlsetseen_answered)
457 safestrncpy(buf, vbuf.v_answered, SIZ);
463 * Manipulate the "seen msgs" string (or other message set strings)
465 void CtdlSetSeen(long *target_msgnums, int num_target_msgnums,
466 int target_setting, int which_set,
467 struct ctdluser *which_user, struct ctdlroom *which_room) {
468 struct CitContext *CCC = CC;
469 struct cdbdata *cdbfr;
474 long hi = (-1L); /// TODO: we just write here. y?
483 char *is_set; /* actually an array of booleans */
485 /* Don't bother doing *anything* if we were passed a list of zero messages */
486 if (num_target_msgnums < 1) {
490 /* If no room was specified, we go with the current room. */
492 which_room = &CCC->room;
495 /* If no user was specified, we go with the current user. */
497 which_user = &CCC->user;
500 MSG_syslog(LOG_DEBUG, "CtdlSetSeen(%d msgs starting with %ld, %s, %d) in <%s>\n",
501 num_target_msgnums, target_msgnums[0],
502 (target_setting ? "SET" : "CLEAR"),
506 /* Learn about the user and room in question */
507 CtdlGetRelationship(&vbuf, which_user, which_room);
509 /* Load the message list */
510 cdbfr = cdb_fetch(CDB_MSGLISTS, &which_room->QRnumber, sizeof(long));
512 msglist = (long *) cdbfr->ptr;
513 cdbfr->ptr = NULL; /* CtdlSetSeen() now owns this memory */
514 num_msgs = cdbfr->len / sizeof(long);
517 return; /* No messages at all? No further action. */
520 is_set = malloc(num_msgs * sizeof(char));
521 memset(is_set, 0, (num_msgs * sizeof(char)) );
523 /* Decide which message set we're manipulating */
525 case ctdlsetseen_seen:
526 vset = NewStrBufPlain(vbuf.v_seen, -1);
528 case ctdlsetseen_answered:
529 vset = NewStrBufPlain(vbuf.v_answered, -1);
536 #if 0 /* This is a special diagnostic section. Do not allow it to run during normal operation. */
537 MSG_syslog(LOG_DEBUG, "There are %d messages in the room.\n", num_msgs);
538 for (i=0; i<num_msgs; ++i) {
539 if ((i > 0) && (msglist[i] <= msglist[i-1])) abort();
541 MSG_syslog(LOG_DEBUG, "We are twiddling %d of them.\n", num_target_msgnums);
542 for (k=0; k<num_target_msgnums; ++k) {
543 if ((k > 0) && (target_msgnums[k] <= target_msgnums[k-1])) abort();
547 MSG_syslog(LOG_DEBUG, "before update: %s\n", ChrPtr(vset));
549 /* Translate the existing sequence set into an array of booleans */
550 setstr = NewStrBuf();
554 while (StrBufExtract_NextToken(setstr, vset, &pvset, ',') >= 0) {
556 StrBufExtract_token(lostr, setstr, 0, ':');
557 if (StrBufNum_tokens(setstr, ':') >= 2) {
558 StrBufExtract_token(histr, setstr, 1, ':');
562 StrBufAppendBuf(histr, lostr, 0);
565 if (!strcmp(ChrPtr(histr), "*")) {
572 for (i = 0; i < num_msgs; ++i) {
573 if ((msglist[i] >= lo) && (msglist[i] <= hi)) {
583 /* Now translate the array of booleans back into a sequence set */
589 for (i=0; i<num_msgs; ++i) {
593 for (k=0; k<num_target_msgnums; ++k) {
594 if (msglist[i] == target_msgnums[k]) {
595 is_seen = target_setting;
599 if ((was_seen == 0) && (is_seen == 1)) {
602 else if ((was_seen == 1) && (is_seen == 0)) {
605 if (StrLength(vset) > 0) {
606 StrBufAppendBufPlain(vset, HKEY(","), 0);
609 StrBufAppendPrintf(vset, "%ld", hi);
612 StrBufAppendPrintf(vset, "%ld:%ld", lo, hi);
616 if ((is_seen) && (i == num_msgs - 1)) {
617 if (StrLength(vset) > 0) {
618 StrBufAppendBufPlain(vset, HKEY(","), 0);
620 if ((i==0) || (was_seen == 0)) {
621 StrBufAppendPrintf(vset, "%ld", msglist[i]);
624 StrBufAppendPrintf(vset, "%ld:%ld", lo, msglist[i]);
632 * We will have to stuff this string back into a 4096 byte buffer, so if it's
633 * larger than that now, truncate it by removing tokens from the beginning.
634 * The limit of 100 iterations is there to prevent an infinite loop in case
635 * something unexpected happens.
637 int number_of_truncations = 0;
638 while ( (StrLength(vset) > SIZ) && (number_of_truncations < 100) ) {
639 StrBufRemove_token(vset, 0, ',');
640 ++number_of_truncations;
644 * If we're truncating the sequence set of messages marked with the 'seen' flag,
645 * we want the earliest messages (the truncated ones) to be marked, not unmarked.
646 * Otherwise messages at the beginning will suddenly appear to be 'unseen'.
648 if ( (which_set == ctdlsetseen_seen) && (number_of_truncations > 0) ) {
650 first_tok = NewStrBuf();
651 StrBufExtract_token(first_tok, vset, 0, ',');
652 StrBufRemove_token(vset, 0, ',');
654 if (StrBufNum_tokens(first_tok, ':') > 1) {
655 StrBufRemove_token(first_tok, 0, ':');
659 new_set = NewStrBuf();
660 StrBufAppendBufPlain(new_set, HKEY("1:"), 0);
661 StrBufAppendBuf(new_set, first_tok, 0);
662 StrBufAppendBufPlain(new_set, HKEY(":"), 0);
663 StrBufAppendBuf(new_set, vset, 0);
666 FreeStrBuf(&first_tok);
670 MSG_syslog(LOG_DEBUG, " after update: %s\n", ChrPtr(vset));
672 /* Decide which message set we're manipulating */
674 case ctdlsetseen_seen:
675 safestrncpy(vbuf.v_seen, ChrPtr(vset), sizeof vbuf.v_seen);
677 case ctdlsetseen_answered:
678 safestrncpy(vbuf.v_answered, ChrPtr(vset), sizeof vbuf.v_answered);
684 CtdlSetRelationship(&vbuf, which_user, which_room);
690 * API function to perform an operation for each qualifying message in the
691 * current room. (Returns the number of messages processed.)
693 int CtdlForEachMessage(int mode, long ref, char *search_string,
695 struct CtdlMessage *compare,
696 ForEachMsgCallback CallBack,
699 struct CitContext *CCC = CC;
702 struct cdbdata *cdbfr;
703 long *msglist = NULL;
705 int num_processed = 0;
708 struct CtdlMessage *msg = NULL;
711 int printed_lastold = 0;
712 int num_search_msgs = 0;
713 long *search_msgs = NULL;
715 int need_to_free_re = 0;
718 if ((content_type) && (!IsEmptyStr(content_type))) {
719 regcomp(&re, content_type, 0);
723 /* Learn about the user and room in question */
724 if (server_shutting_down) {
725 if (need_to_free_re) regfree(&re);
728 CtdlGetUser(&CCC->user, CCC->curr_user);
730 if (server_shutting_down) {
731 if (need_to_free_re) regfree(&re);
734 CtdlGetRelationship(&vbuf, &CCC->user, &CCC->room);
736 if (server_shutting_down) {
737 if (need_to_free_re) regfree(&re);
741 /* Load the message list */
742 cdbfr = cdb_fetch(CDB_MSGLISTS, &CCC->room.QRnumber, sizeof(long));
744 if (need_to_free_re) regfree(&re);
745 return 0; /* No messages at all? No further action. */
748 msglist = (long *) cdbfr->ptr;
749 num_msgs = cdbfr->len / sizeof(long);
751 cdbfr->ptr = NULL; /* clear this so that cdb_free() doesn't free it */
752 cdb_free(cdbfr); /* we own this memory now */
755 * Now begin the traversal.
757 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
759 /* If the caller is looking for a specific MIME type, filter
760 * out all messages which are not of the type requested.
762 if ((content_type != NULL) && (!IsEmptyStr(content_type))) {
764 /* This call to GetMetaData() sits inside this loop
765 * so that we only do the extra database read per msg
766 * if we need to. Doing the extra read all the time
767 * really kills the server. If we ever need to use
768 * metadata for another search criterion, we need to
769 * move the read somewhere else -- but still be smart
770 * enough to only do the read if the caller has
771 * specified something that will need it.
773 if (server_shutting_down) {
774 if (need_to_free_re) regfree(&re);
778 GetMetaData(&smi, msglist[a]);
780 /* if (strcasecmp(smi.meta_content_type, content_type)) { old non-regex way */
781 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) != 0) {
787 num_msgs = sort_msglist(msglist, num_msgs);
789 /* If a template was supplied, filter out the messages which
790 * don't match. (This could induce some delays!)
793 if (compare != NULL) {
794 for (a = 0; a < num_msgs; ++a) {
795 if (server_shutting_down) {
796 if (need_to_free_re) regfree(&re);
800 msg = CtdlFetchMessage(msglist[a], 1);
802 if (CtdlMsgCmp(msg, compare)) {
811 /* If a search string was specified, get a message list from
812 * the full text index and remove messages which aren't on both
816 * Since the lists are sorted and strictly ascending, and the
817 * output list is guaranteed to be shorter than or equal to the
818 * input list, we overwrite the bottom of the input list. This
819 * eliminates the need to memmove big chunks of the list over and
822 if ( (num_msgs > 0) && (mode == MSGS_SEARCH) && (search_string) ) {
824 /* Call search module via hook mechanism.
825 * NULL means use any search function available.
826 * otherwise replace with a char * to name of search routine
828 CtdlModuleDoSearch(&num_search_msgs, &search_msgs, search_string, "fulltext");
830 if (num_search_msgs > 0) {
834 orig_num_msgs = num_msgs;
836 for (i=0; i<orig_num_msgs; ++i) {
837 for (j=0; j<num_search_msgs; ++j) {
838 if (msglist[i] == search_msgs[j]) {
839 msglist[num_msgs++] = msglist[i];
845 num_msgs = 0; /* No messages qualify */
847 if (search_msgs != NULL) free(search_msgs);
849 /* Now that we've purged messages which don't contain the search
850 * string, treat a MSGS_SEARCH just like a MSGS_ALL from this
857 * Now iterate through the message list, according to the
858 * criteria supplied by the caller.
861 for (a = 0; a < num_msgs; ++a) {
862 if (server_shutting_down) {
863 if (need_to_free_re) regfree(&re);
865 return num_processed;
867 thismsg = msglist[a];
868 if (mode == MSGS_ALL) {
872 is_seen = is_msg_in_sequence_set(
873 vbuf.v_seen, thismsg);
874 if (is_seen) lastold = thismsg;
880 || ((mode == MSGS_OLD) && (is_seen))
881 || ((mode == MSGS_NEW) && (!is_seen))
882 || ((mode == MSGS_LAST) && (a >= (num_msgs - ref)))
883 || ((mode == MSGS_FIRST) && (a < ref))
884 || ((mode == MSGS_GT) && (thismsg > ref))
885 || ((mode == MSGS_LT) && (thismsg < ref))
886 || ((mode == MSGS_EQ) && (thismsg == ref))
889 if ((mode == MSGS_NEW) && (CCC->user.flags & US_LASTOLD) && (lastold > 0L) && (printed_lastold == 0) && (!is_seen)) {
891 CallBack(lastold, userdata);
895 if (CallBack) CallBack(thismsg, userdata);
899 if (need_to_free_re) regfree(&re);
902 * We cache the most recent msglist in order to do security checks later
904 if (CCC->client_socket > 0) {
905 if (CCC->cached_msglist != NULL) {
906 free(CCC->cached_msglist);
908 CCC->cached_msglist = msglist;
909 CCC->cached_num_msgs = num_msgs;
915 return num_processed;
921 * cmd_msgs() - get list of message #'s in this room
922 * implements the MSGS server command using CtdlForEachMessage()
924 void cmd_msgs(char *cmdbuf)
933 int with_template = 0;
934 struct CtdlMessage *template = NULL;
935 char search_string[1024];
936 ForEachMsgCallback CallBack;
938 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
940 extract_token(which, cmdbuf, 0, '|', sizeof which);
941 cm_ref = extract_int(cmdbuf, 1);
942 extract_token(search_string, cmdbuf, 1, '|', sizeof search_string);
943 with_template = extract_int(cmdbuf, 2);
944 switch (extract_int(cmdbuf, 3))
948 CallBack = simple_listing;
951 CallBack = headers_listing;
954 CallBack = headers_euid;
959 if (!strncasecmp(which, "OLD", 3))
961 else if (!strncasecmp(which, "NEW", 3))
963 else if (!strncasecmp(which, "FIRST", 5))
965 else if (!strncasecmp(which, "LAST", 4))
967 else if (!strncasecmp(which, "GT", 2))
969 else if (!strncasecmp(which, "LT", 2))
971 else if (!strncasecmp(which, "SEARCH", 6))
976 if ( (mode == MSGS_SEARCH) && (!config.c_enable_fulltext) ) {
977 cprintf("%d Full text index is not enabled on this server.\n",
978 ERROR + CMD_NOT_SUPPORTED);
984 cprintf("%d Send template then receive message list\n",
986 template = (struct CtdlMessage *)
987 malloc(sizeof(struct CtdlMessage));
988 memset(template, 0, sizeof(struct CtdlMessage));
989 template->cm_magic = CTDLMESSAGE_MAGIC;
990 template->cm_anon_type = MES_NORMAL;
992 while(client_getln(buf, sizeof buf) >= 0 && strcmp(buf,"000")) {
994 extract_token(tfield, buf, 0, '|', sizeof tfield);
995 tValueLen = extract_token(tvalue, buf, 1, '|', sizeof tvalue);
996 for (i='A'; i<='Z'; ++i) if (msgkeys[i]!=NULL) {
997 if (!strcasecmp(tfield, msgkeys[i])) {
998 CM_SetField(template, i, tvalue, tValueLen);
1005 cprintf("%d \n", LISTING_FOLLOWS);
1008 CtdlForEachMessage(mode,
1009 ( (mode == MSGS_SEARCH) ? 0 : cm_ref ),
1010 ( (mode == MSGS_SEARCH) ? search_string : NULL ),
1015 if (template != NULL) CM_Free(template);
1021 * memfmout() - Citadel text formatter and paginator.
1022 * Although the original purpose of this routine was to format
1023 * text to the reader's screen width, all we're really using it
1024 * for here is to format text out to 80 columns before sending it
1025 * to the client. The client software may reformat it again.
1028 char *mptr, /* where are we going to get our text from? */
1029 const char *nl /* string to terminate lines with */
1031 struct CitContext *CCC = CC;
1033 unsigned char ch = 0;
1040 while (ch=*(mptr++), ch != 0) {
1043 if (client_write(outbuf, len) == -1)
1045 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
1049 if (client_write(nl, nllen) == -1)
1051 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
1056 else if (ch == '\r') {
1057 /* Ignore carriage returns. Newlines are always LF or CRLF but never CR. */
1059 else if (isspace(ch)) {
1060 if (column > 72) { /* Beyond 72 columns, break on the next space */
1061 if (client_write(outbuf, len) == -1)
1063 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
1067 if (client_write(nl, nllen) == -1)
1069 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
1082 if (column > 1000) { /* Beyond 1000 columns, break anywhere */
1083 if (client_write(outbuf, len) == -1)
1085 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
1089 if (client_write(nl, nllen) == -1)
1091 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
1099 if (client_write(outbuf, len) == -1)
1101 MSGM_syslog(LOG_ERR, "memfmout(): aborting due to write failure.\n");
1105 client_write(nl, nllen);
1113 * Callback function for mime parser that simply lists the part
1115 void list_this_part(char *name, char *filename, char *partnum, char *disp,
1116 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1117 char *cbid, void *cbuserdata)
1121 ma = (struct ma_info *)cbuserdata;
1122 if (ma->is_ma == 0) {
1123 cprintf("part=%s|%s|%s|%s|%s|%ld|%s|%s\n",
1136 * Callback function for multipart prefix
1138 void list_this_pref(char *name, char *filename, char *partnum, char *disp,
1139 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1140 char *cbid, void *cbuserdata)
1144 ma = (struct ma_info *)cbuserdata;
1145 if (!strcasecmp(cbtype, "multipart/alternative")) {
1149 if (ma->is_ma == 0) {
1150 cprintf("pref=%s|%s\n", partnum, cbtype);
1155 * Callback function for multipart sufffix
1157 void list_this_suff(char *name, char *filename, char *partnum, char *disp,
1158 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1159 char *cbid, void *cbuserdata)
1163 ma = (struct ma_info *)cbuserdata;
1164 if (ma->is_ma == 0) {
1165 cprintf("suff=%s|%s\n", partnum, cbtype);
1167 if (!strcasecmp(cbtype, "multipart/alternative")) {
1174 * Callback function for mime parser that opens a section for downloading
1176 void mime_download(char *name, char *filename, char *partnum, char *disp,
1177 void *content, char *cbtype, char *cbcharset, size_t length,
1178 char *encoding, char *cbid, void *cbuserdata)
1181 CitContext *CCC = MyContext();
1183 /* Silently go away if there's already a download open. */
1184 if (CCC->download_fp != NULL)
1188 (!IsEmptyStr(partnum) && (!strcasecmp(CCC->download_desired_section, partnum)))
1189 || (!IsEmptyStr(cbid) && (!strcasecmp(CCC->download_desired_section, cbid)))
1191 CCC->download_fp = tmpfile();
1192 if (CCC->download_fp == NULL) {
1193 MSG_syslog(LOG_EMERG, "mime_download(): Couldn't write: %s\n",
1195 cprintf("%d cannot open temporary file: %s\n",
1196 ERROR + INTERNAL_ERROR, strerror(errno));
1200 rv = fwrite(content, length, 1, CCC->download_fp);
1202 MSG_syslog(LOG_EMERG, "mime_download(): Couldn't write: %s\n",
1204 cprintf("%d unable to write tempfile.\n",
1206 fclose(CCC->download_fp);
1207 CCC->download_fp = NULL;
1210 fflush(CCC->download_fp);
1211 rewind(CCC->download_fp);
1213 OpenCmdResult(filename, cbtype);
1220 * Callback function for mime parser that outputs a section all at once.
1221 * We can specify the desired section by part number *or* content-id.
1223 void mime_spew_section(char *name, char *filename, char *partnum, char *disp,
1224 void *content, char *cbtype, char *cbcharset, size_t length,
1225 char *encoding, char *cbid, void *cbuserdata)
1227 int *found_it = (int *)cbuserdata;
1230 (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
1231 || (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
1234 cprintf("%d %d|-1|%s|%s|%s\n",
1241 client_write(content, length);
1247 * Load a message from disk into memory.
1248 * This is used by CtdlOutputMsg() and other fetch functions.
1250 * NOTE: Caller is responsible for freeing the returned CtdlMessage struct
1251 * using the CtdlMessageFree() function.
1253 struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body)
1255 struct CitContext *CCC = CC;
1256 struct cdbdata *dmsgtext;
1257 struct CtdlMessage *ret = NULL;
1261 cit_uint8_t field_header;
1263 MSG_syslog(LOG_DEBUG, "CtdlFetchMessage(%ld, %d)\n", msgnum, with_body);
1264 dmsgtext = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
1265 if (dmsgtext == NULL) {
1266 MSG_syslog(LOG_ERR, "CtdlFetchMessage(%ld, %d) Failed!\n", msgnum, with_body);
1269 mptr = dmsgtext->ptr;
1270 upper_bound = mptr + dmsgtext->len;
1272 /* Parse the three bytes that begin EVERY message on disk.
1273 * The first is always 0xFF, the on-disk magic number.
1274 * The second is the anonymous/public type byte.
1275 * The third is the format type byte (vari, fixed, or MIME).
1279 MSG_syslog(LOG_ERR, "Message %ld appears to be corrupted.\n", msgnum);
1283 ret = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1284 memset(ret, 0, sizeof(struct CtdlMessage));
1286 ret->cm_magic = CTDLMESSAGE_MAGIC;
1287 ret->cm_anon_type = *mptr++; /* Anon type byte */
1288 ret->cm_format_type = *mptr++; /* Format type byte */
1291 * The rest is zero or more arbitrary fields. Load them in.
1292 * We're done when we encounter either a zero-length field or
1293 * have just processed the 'M' (message text) field.
1297 if (mptr >= upper_bound) {
1300 field_header = *mptr++;
1302 CM_SetField(ret, field_header, mptr, len);
1304 mptr += len + 1; /* advance to next field */
1306 } while ((mptr < upper_bound) && (field_header != 'M'));
1310 /* Always make sure there's something in the msg text field. If
1311 * it's NULL, the message text is most likely stored separately,
1312 * so go ahead and fetch that. Failing that, just set a dummy
1313 * body so other code doesn't barf.
1315 if ( (CM_IsEmpty(ret, eMesageText)) && (with_body) ) {
1316 dmsgtext = cdb_fetch(CDB_BIGMSGS, &msgnum, sizeof(long));
1317 if (dmsgtext != NULL) {
1318 CM_SetAsField(ret, eMesageText, &dmsgtext->ptr, dmsgtext->len);
1322 if (CM_IsEmpty(ret, eMesageText)) {
1323 CM_SetField(ret, eMesageText, HKEY("\r\n\r\n (no text)\r\n"));
1326 /* Perform "before read" hooks (aborting if any return nonzero) */
1327 if (PerformMessageHooks(ret, EVT_BEFOREREAD) > 0) {
1338 * Pre callback function for multipart/alternative
1340 * NOTE: this differs from the standard behavior for a reason. Normally when
1341 * displaying multipart/alternative you want to show the _last_ usable
1342 * format in the message. Here we show the _first_ one, because it's
1343 * usually text/plain. Since this set of functions is designed for text
1344 * output to non-MIME-aware clients, this is the desired behavior.
1347 void fixed_output_pre(char *name, char *filename, char *partnum, char *disp,
1348 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1349 char *cbid, void *cbuserdata)
1351 struct CitContext *CCC = CC;
1354 ma = (struct ma_info *)cbuserdata;
1355 MSG_syslog(LOG_DEBUG, "fixed_output_pre() type=<%s>\n", cbtype);
1356 if (!strcasecmp(cbtype, "multipart/alternative")) {
1360 if (!strcasecmp(cbtype, "message/rfc822")) {
1366 * Post callback function for multipart/alternative
1368 void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
1369 void *content, char *cbtype, char *cbcharset, size_t length,
1370 char *encoding, char *cbid, void *cbuserdata)
1372 struct CitContext *CCC = CC;
1375 ma = (struct ma_info *)cbuserdata;
1376 MSG_syslog(LOG_DEBUG, "fixed_output_post() type=<%s>\n", cbtype);
1377 if (!strcasecmp(cbtype, "multipart/alternative")) {
1381 if (!strcasecmp(cbtype, "message/rfc822")) {
1387 * Inline callback function for mime parser that wants to display text
1389 void fixed_output(char *name, char *filename, char *partnum, char *disp,
1390 void *content, char *cbtype, char *cbcharset, size_t length,
1391 char *encoding, char *cbid, void *cbuserdata)
1393 struct CitContext *CCC = CC;
1399 ma = (struct ma_info *)cbuserdata;
1401 MSG_syslog(LOG_DEBUG,
1402 "fixed_output() part %s: %s (%s) (%ld bytes)\n",
1403 partnum, filename, cbtype, (long)length);
1406 * If we're in the middle of a multipart/alternative scope and
1407 * we've already printed another section, skip this one.
1409 if ( (ma->is_ma) && (ma->did_print) ) {
1410 MSG_syslog(LOG_DEBUG, "Skipping part %s (%s)\n", partnum, cbtype);
1415 if ( (!strcasecmp(cbtype, "text/plain"))
1416 || (IsEmptyStr(cbtype)) ) {
1419 client_write(wptr, length);
1420 if (wptr[length-1] != '\n') {
1427 if (!strcasecmp(cbtype, "text/html")) {
1428 ptr = html_to_ascii(content, length, 80, 0);
1430 client_write(ptr, wlen);
1431 if ((wlen > 0) && (ptr[wlen-1] != '\n')) {
1438 if (ma->use_fo_hooks) {
1439 if (PerformFixedOutputHooks(cbtype, content, length)) {
1440 /* above function returns nonzero if it handled the part */
1445 if (strncasecmp(cbtype, "multipart/", 10)) {
1446 cprintf("Part %s: %s (%s) (%ld bytes)\r\n",
1447 partnum, filename, cbtype, (long)length);
1453 * The client is elegant and sophisticated and wants to be choosy about
1454 * MIME content types, so figure out which multipart/alternative part
1455 * we're going to send.
1457 * We use a system of weights. When we find a part that matches one of the
1458 * MIME types we've declared as preferential, we can store it in ma->chosen_part
1459 * and then set ma->chosen_pref to that MIME type's position in our preference
1460 * list. If we then hit another match, we only replace the first match if
1461 * the preference value is lower.
1463 void choose_preferred(char *name, char *filename, char *partnum, char *disp,
1464 void *content, char *cbtype, char *cbcharset, size_t length,
1465 char *encoding, char *cbid, void *cbuserdata)
1467 struct CitContext *CCC = CC;
1472 ma = (struct ma_info *)cbuserdata;
1474 // NOTE: REMOVING THIS CONDITIONAL FIXES BUG 220
1475 // http://bugzilla.citadel.org/show_bug.cgi?id=220
1476 // I don't know if there are any side effects! Please TEST TEST TEST
1477 //if (ma->is_ma > 0) {
1479 for (i=0; i<num_tokens(CCC->preferred_formats, '|'); ++i) {
1480 extract_token(buf, CCC->preferred_formats, i, '|', sizeof buf);
1481 if ( (!strcasecmp(buf, cbtype)) && (!ma->freeze) ) {
1482 if (i < ma->chosen_pref) {
1483 MSG_syslog(LOG_DEBUG, "Setting chosen part: <%s>\n", partnum);
1484 safestrncpy(ma->chosen_part, partnum, sizeof ma->chosen_part);
1485 ma->chosen_pref = i;
1492 * Now that we've chosen our preferred part, output it.
1494 void output_preferred(char *name,
1506 struct CitContext *CCC = CC;
1509 int add_newline = 0;
1512 char *decoded = NULL;
1513 size_t bytes_decoded;
1516 ma = (struct ma_info *)cbuserdata;
1518 /* This is not the MIME part you're looking for... */
1519 if (strcasecmp(partnum, ma->chosen_part)) return;
1521 /* If the content-type of this part is in our preferred formats
1522 * list, we can simply output it verbatim.
1524 for (i=0; i<num_tokens(CCC->preferred_formats, '|'); ++i) {
1525 extract_token(buf, CCC->preferred_formats, i, '|', sizeof buf);
1526 if (!strcasecmp(buf, cbtype)) {
1527 /* Yeah! Go! W00t!! */
1528 if (ma->dont_decode == 0)
1529 rc = mime_decode_now (content,
1535 break; /* Give us the chance, maybe theres another one. */
1537 if (rc == 0) text_content = (char *)content;
1539 text_content = decoded;
1540 length = bytes_decoded;
1543 if (text_content[length-1] != '\n') {
1546 cprintf("Content-type: %s", cbtype);
1547 if (!IsEmptyStr(cbcharset)) {
1548 cprintf("; charset=%s", cbcharset);
1550 cprintf("\nContent-length: %d\n",
1551 (int)(length + add_newline) );
1552 if (!IsEmptyStr(encoding)) {
1553 cprintf("Content-transfer-encoding: %s\n", encoding);
1556 cprintf("Content-transfer-encoding: 7bit\n");
1558 cprintf("X-Citadel-MSG4-Partnum: %s\n", partnum);
1560 if (client_write(text_content, length) == -1)
1562 MSGM_syslog(LOG_ERR, "output_preferred(): aborting due to write failure.\n");
1565 if (add_newline) cprintf("\n");
1566 if (decoded != NULL) free(decoded);
1571 /* No translations required or possible: output as text/plain */
1572 cprintf("Content-type: text/plain\n\n");
1574 if (ma->dont_decode == 0)
1575 rc = mime_decode_now (content,
1581 return; /* Give us the chance, maybe theres another one. */
1583 if (rc == 0) text_content = (char *)content;
1585 text_content = decoded;
1586 length = bytes_decoded;
1589 fixed_output(name, filename, partnum, disp, text_content, cbtype, cbcharset,
1590 length, encoding, cbid, cbuserdata);
1591 if (decoded != NULL) free(decoded);
1596 char desired_section[64];
1603 * Callback function for
1605 void extract_encapsulated_message(char *name, char *filename, char *partnum, char *disp,
1606 void *content, char *cbtype, char *cbcharset, size_t length,
1607 char *encoding, char *cbid, void *cbuserdata)
1609 struct encapmsg *encap;
1611 encap = (struct encapmsg *)cbuserdata;
1613 /* Only proceed if this is the desired section... */
1614 if (!strcasecmp(encap->desired_section, partnum)) {
1615 encap->msglen = length;
1616 encap->msg = malloc(length + 2);
1617 memcpy(encap->msg, content, length);
1624 * Determine whether the specified message exists in the cached_msglist
1625 * (This is a security check)
1627 int check_cached_msglist(long msgnum) {
1628 struct CitContext *CCC = CC;
1630 /* cases in which we skip the check */
1631 if (!CCC) return om_ok; /* not a session */
1632 if (CCC->client_socket <= 0) return om_ok; /* not a client session */
1633 if (CCC->cached_msglist == NULL) return om_access_denied; /* no msglist fetched */
1634 if (CCC->cached_num_msgs == 0) return om_access_denied; /* nothing to check */
1637 /* Do a binary search within the cached_msglist for the requested msgnum */
1639 int max = (CC->cached_num_msgs - 1);
1641 while (max >= min) {
1642 int middle = min + (max-min) / 2 ;
1643 if (msgnum == CCC->cached_msglist[middle]) {
1646 if (msgnum > CC->cached_msglist[middle]) {
1654 return om_access_denied;
1660 * Get a message off disk. (returns om_* values found in msgbase.h)
1663 int CtdlOutputMsg(long msg_num, /* message number (local) to fetch */
1664 int mode, /* how would you like that message? */
1665 int headers_only, /* eschew the message body? */
1666 int do_proto, /* do Citadel protocol responses? */
1667 int crlf, /* Use CRLF newlines instead of LF? */
1668 char *section, /* NULL or a message/rfc822 section */
1669 int flags, /* various flags; see msgbase.h */
1673 struct CitContext *CCC = CC;
1674 struct CtdlMessage *TheMessage = NULL;
1675 int retcode = CIT_OK;
1676 struct encapmsg encap;
1679 MSG_syslog(LOG_DEBUG, "CtdlOutputMsg(msgnum=%ld, mode=%d, section=%s)\n",
1681 (section ? section : "<>")
1684 r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
1687 if (r == om_not_logged_in) {
1688 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1691 cprintf("%d An unknown error has occurred.\n", ERROR);
1698 * Check to make sure the message is actually IN this room
1700 r = check_cached_msglist(msg_num);
1701 if (r == om_access_denied) {
1702 /* Not in the cache? We get ONE shot to check it again. */
1703 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, NULL, NULL);
1704 r = check_cached_msglist(msg_num);
1707 MSG_syslog(LOG_DEBUG, "Security check fail: message %ld is not in %s\n",
1708 msg_num, CCC->room.QRname
1711 if (r == om_access_denied) {
1712 cprintf("%d message %ld was not found in this room\n",
1713 ERROR + HIGHER_ACCESS_REQUIRED,
1722 * Fetch the message from disk. If we're in HEADERS_FAST mode,
1723 * request that we don't even bother loading the body into memory.
1725 if (headers_only == HEADERS_FAST) {
1726 TheMessage = CtdlFetchMessage(msg_num, 0);
1729 TheMessage = CtdlFetchMessage(msg_num, 1);
1732 if (TheMessage == NULL) {
1733 if (do_proto) cprintf("%d Can't locate msg %ld on disk\n",
1734 ERROR + MESSAGE_NOT_FOUND, msg_num);
1735 return(om_no_such_msg);
1738 /* Here is the weird form of this command, to process only an
1739 * encapsulated message/rfc822 section.
1741 if (section) if (!IsEmptyStr(section)) if (strcmp(section, "0")) {
1742 memset(&encap, 0, sizeof encap);
1743 safestrncpy(encap.desired_section, section, sizeof encap.desired_section);
1744 mime_parser(TheMessage->cm_fields[eMesageText],
1746 *extract_encapsulated_message,
1747 NULL, NULL, (void *)&encap, 0
1750 if ((Author != NULL) && (*Author == NULL))
1752 *Author = TheMessage->cm_fields[eAuthor];
1753 TheMessage->cm_fields[eAuthor] = NULL;
1755 if ((Address != NULL) && (*Address == NULL))
1757 *Address = TheMessage->cm_fields[erFc822Addr];
1758 TheMessage->cm_fields[erFc822Addr] = NULL;
1760 CM_Free(TheMessage);
1764 encap.msg[encap.msglen] = 0;
1765 TheMessage = convert_internet_message(encap.msg);
1766 encap.msg = NULL; /* no free() here, TheMessage owns it now */
1768 /* Now we let it fall through to the bottom of this
1769 * function, because TheMessage now contains the
1770 * encapsulated message instead of the top-level
1771 * message. Isn't that neat?
1776 cprintf("%d msg %ld has no part %s\n",
1777 ERROR + MESSAGE_NOT_FOUND,
1781 retcode = om_no_such_msg;
1786 /* Ok, output the message now */
1787 if (retcode == CIT_OK)
1788 retcode = CtdlOutputPreLoadedMsg(TheMessage, mode, headers_only, do_proto, crlf, flags);
1789 if ((Author != NULL) && (*Author == NULL))
1791 *Author = TheMessage->cm_fields[eAuthor];
1792 TheMessage->cm_fields[eAuthor] = NULL;
1794 if ((Address != NULL) && (*Address == NULL))
1796 *Address = TheMessage->cm_fields[erFc822Addr];
1797 TheMessage->cm_fields[erFc822Addr] = NULL;
1800 CM_Free(TheMessage);
1807 void OutputCtdlMsgHeaders(
1808 struct CtdlMessage *TheMessage,
1809 int do_proto) /* do Citadel protocol responses? */
1814 char display_name[256];
1816 /* begin header processing loop for Citadel message format */
1817 safestrncpy(display_name, "<unknown>", sizeof display_name);
1818 if (!CM_IsEmpty(TheMessage, eAuthor)) {
1819 strcpy(buf, TheMessage->cm_fields[eAuthor]);
1820 if (TheMessage->cm_anon_type == MES_ANONONLY) {
1821 safestrncpy(display_name, "****", sizeof display_name);
1823 else if (TheMessage->cm_anon_type == MES_ANONOPT) {
1824 safestrncpy(display_name, "anonymous", sizeof display_name);
1827 safestrncpy(display_name, buf, sizeof display_name);
1829 if ((is_room_aide())
1830 && ((TheMessage->cm_anon_type == MES_ANONONLY)
1831 || (TheMessage->cm_anon_type == MES_ANONOPT))) {
1832 size_t tmp = strlen(display_name);
1833 snprintf(&display_name[tmp],
1834 sizeof display_name - tmp,
1839 /* Don't show Internet address for users on the
1840 * local Citadel network.
1843 if (!CM_IsEmpty(TheMessage, eNodeName) &&
1844 (haschar(TheMessage->cm_fields[eNodeName], '.') == 0))
1849 /* Now spew the header fields in the order we like them. */
1850 for (i=0; i< NDiskFields; ++i) {
1852 Field = FieldOrder[i];
1853 if (Field != eMesageText) {
1854 if ( (!CM_IsEmpty(TheMessage, Field))
1855 && (msgkeys[Field] != NULL) ) {
1856 if ((Field == eenVelopeTo) ||
1857 (Field == eRecipient) ||
1858 (Field == eCarbonCopY)) {
1859 sanitize_truncated_recipient(TheMessage->cm_fields[Field]);
1861 if (Field == eAuthor) {
1862 if (do_proto) cprintf("%s=%s\n",
1866 else if ((Field == erFc822Addr) && (suppress_f)) {
1869 /* Masquerade display name if needed */
1871 if (do_proto) cprintf("%s=%s\n",
1873 TheMessage->cm_fields[Field]
1882 void OutputRFC822MsgHeaders(
1883 struct CtdlMessage *TheMessage,
1884 int flags, /* should the bessage be exported clean */
1886 char *mid, long sizeof_mid,
1887 char *suser, long sizeof_suser,
1888 char *luser, long sizeof_luser,
1889 char *fuser, long sizeof_fuser,
1890 char *snode, long sizeof_snode)
1892 char datestamp[100];
1893 int subject_found = 0;
1900 for (i = 0; i < 256; ++i) {
1901 if (TheMessage->cm_fields[i]) {
1902 mptr = mpptr = TheMessage->cm_fields[i];
1905 safestrncpy(luser, mptr, sizeof_luser);
1906 safestrncpy(suser, mptr, sizeof_suser);
1908 else if (i == 'Y') {
1909 if ((flags & QP_EADDR) != 0) {
1910 mptr = qp_encode_email_addrs(mptr);
1912 sanitize_truncated_recipient(mptr);
1913 cprintf("CC: %s%s", mptr, nl);
1915 else if (i == 'P') {
1916 cprintf("Return-Path: %s%s", mptr, nl);
1918 else if (i == eListID) {
1919 cprintf("List-ID: %s%s", mptr, nl);
1921 else if (i == 'V') {
1922 if ((flags & QP_EADDR) != 0)
1923 mptr = qp_encode_email_addrs(mptr);
1925 while ((*hptr != '\0') && isspace(*hptr))
1927 if (!IsEmptyStr(hptr))
1928 cprintf("Envelope-To: %s%s", hptr, nl);
1930 else if (i == 'U') {
1931 cprintf("Subject: %s%s", mptr, nl);
1935 safestrncpy(mid, mptr, sizeof_mid); /// TODO: detect @ here and copy @nodename in if not found.
1936 else if (i == erFc822Addr)
1937 safestrncpy(fuser, mptr, sizeof_fuser);
1938 /* else if (i == 'O')
1939 cprintf("X-Citadel-Room: %s%s",
1942 safestrncpy(snode, mptr, sizeof_snode);
1945 if (haschar(mptr, '@') == 0)
1947 sanitize_truncated_recipient(mptr);
1948 cprintf("To: %s@%s", mptr, config.c_fqdn);
1953 if ((flags & QP_EADDR) != 0) {
1954 mptr = qp_encode_email_addrs(mptr);
1956 sanitize_truncated_recipient(mptr);
1957 cprintf("To: %s", mptr);
1961 else if (i == 'T') {
1962 datestring(datestamp, sizeof datestamp,
1963 atol(mptr), DATESTRING_RFC822);
1964 cprintf("Date: %s%s", datestamp, nl);
1966 else if (i == 'W') {
1967 cprintf("References: ");
1968 k = num_tokens(mptr, '|');
1969 for (j=0; j<k; ++j) {
1970 extract_token(buf, mptr, j, '|', sizeof buf);
1971 cprintf("<%s>", buf);
1980 else if (i == eReplyTo) {
1982 while ((*hptr != '\0') && isspace(*hptr))
1984 if (!IsEmptyStr(hptr))
1985 cprintf("Reply-To: %s%s", mptr, nl);
1991 if (subject_found == 0) {
1992 cprintf("Subject: (no subject)%s", nl);
1997 void Dump_RFC822HeadersBody(
1998 struct CtdlMessage *TheMessage,
1999 int headers_only, /* eschew the message body? */
2000 int flags, /* should the bessage be exported clean? */
2004 cit_uint8_t prev_ch;
2006 const char *StartOfText = StrBufNOTNULL;
2009 int nllen = strlen(nl);
2012 mptr = TheMessage->cm_fields[eMesageText];
2016 while (*mptr != '\0') {
2017 if (*mptr == '\r') {
2024 eoh = (*(mptr+1) == '\r') && (*(mptr+2) == '\n');
2026 eoh = *(mptr+1) == '\n';
2030 StartOfText = strchr(StartOfText, '\n');
2031 StartOfText = strchr(StartOfText, '\n');
2034 if (((headers_only == HEADERS_NONE) && (mptr >= StartOfText)) ||
2035 ((headers_only == HEADERS_ONLY) && (mptr < StartOfText)) ||
2036 ((headers_only != HEADERS_NONE) &&
2037 (headers_only != HEADERS_ONLY))
2039 if (*mptr == '\n') {
2040 memcpy(&outbuf[outlen], nl, nllen);
2042 outbuf[outlen] = '\0';
2045 outbuf[outlen++] = *mptr;
2049 if (flags & ESC_DOT)
2051 if ((prev_ch == '\n') &&
2053 ((*(mptr+1) == '\r') || (*(mptr+1) == '\n')))
2055 outbuf[outlen++] = '.';
2060 if (outlen > 1000) {
2061 if (client_write(outbuf, outlen) == -1)
2063 struct CitContext *CCC = CC;
2064 MSGM_syslog(LOG_ERR, "Dump_RFC822HeadersBody(): aborting due to write failure.\n");
2071 client_write(outbuf, outlen);
2077 /* If the format type on disk is 1 (fixed-format), then we want
2078 * everything to be output completely literally ... regardless of
2079 * what message transfer format is in use.
2081 void DumpFormatFixed(
2082 struct CtdlMessage *TheMessage,
2083 int mode, /* how would you like that message? */
2090 int nllen = strlen (nl);
2093 mptr = TheMessage->cm_fields[eMesageText];
2095 if (mode == MT_MIME) {
2096 cprintf("Content-type: text/plain\n\n");
2100 while (ch = *mptr++, ch > 0) {
2104 if ((buflen > 250) && (!xlline)){
2108 while ((buflen > 0) &&
2109 (!isspace(buf[buflen])))
2115 mptr -= tbuflen - buflen;
2120 /* if we reach the outer bounds of our buffer,
2121 abort without respect what whe purge. */
2124 (buflen > SIZ - nllen - 2)))
2128 memcpy (&buf[buflen], nl, nllen);
2132 if (client_write(buf, buflen) == -1)
2134 struct CitContext *CCC = CC;
2135 MSGM_syslog(LOG_ERR, "DumpFormatFixed(): aborting due to write failure.\n");
2147 if (!IsEmptyStr(buf))
2148 cprintf("%s%s", buf, nl);
2152 * Get a message off disk. (returns om_* values found in msgbase.h)
2154 int CtdlOutputPreLoadedMsg(
2155 struct CtdlMessage *TheMessage,
2156 int mode, /* how would you like that message? */
2157 int headers_only, /* eschew the message body? */
2158 int do_proto, /* do Citadel protocol responses? */
2159 int crlf, /* Use CRLF newlines instead of LF? */
2160 int flags /* should the bessage be exported clean? */
2162 struct CitContext *CCC = CC;
2165 const char *nl; /* newline string */
2168 /* Buffers needed for RFC822 translation. These are all filled
2169 * using functions that are bounds-checked, and therefore we can
2170 * make them substantially smaller than SIZ.
2178 MSG_syslog(LOG_DEBUG, "CtdlOutputPreLoadedMsg(TheMessage=%s, %d, %d, %d, %d\n",
2179 ((TheMessage == NULL) ? "NULL" : "not null"),
2180 mode, headers_only, do_proto, crlf);
2182 strcpy(mid, "unknown");
2183 nl = (crlf ? "\r\n" : "\n");
2185 if (!CM_IsValidMsg(TheMessage)) {
2186 MSGM_syslog(LOG_ERR,
2187 "ERROR: invalid preloaded message for output\n");
2189 return(om_no_such_msg);
2192 /* Suppress envelope recipients if required to avoid disclosing BCC addresses.
2193 * Pad it with spaces in order to avoid changing the RFC822 length of the message.
2195 if ( (flags & SUPPRESS_ENV_TO) && (!CM_IsEmpty(TheMessage, eenVelopeTo)) ) {
2196 memset(TheMessage->cm_fields[eenVelopeTo], ' ', strlen(TheMessage->cm_fields[eenVelopeTo]));
2199 /* Are we downloading a MIME component? */
2200 if (mode == MT_DOWNLOAD) {
2201 if (TheMessage->cm_format_type != FMT_RFC822) {
2203 cprintf("%d This is not a MIME message.\n",
2204 ERROR + ILLEGAL_VALUE);
2205 } else if (CCC->download_fp != NULL) {
2206 if (do_proto) cprintf(
2207 "%d You already have a download open.\n",
2208 ERROR + RESOURCE_BUSY);
2210 /* Parse the message text component */
2211 mptr = TheMessage->cm_fields[eMesageText];
2212 mime_parser(mptr, NULL, *mime_download, NULL, NULL, NULL, 0);
2213 /* If there's no file open by this time, the requested
2214 * section wasn't found, so print an error
2216 if (CCC->download_fp == NULL) {
2217 if (do_proto) cprintf(
2218 "%d Section %s not found.\n",
2219 ERROR + FILE_NOT_FOUND,
2220 CCC->download_desired_section);
2223 return((CCC->download_fp != NULL) ? om_ok : om_mime_error);
2226 /* MT_SPEW_SECTION is like MT_DOWNLOAD except it outputs the whole MIME part
2227 * in a single server operation instead of opening a download file.
2229 if (mode == MT_SPEW_SECTION) {
2230 if (TheMessage->cm_format_type != FMT_RFC822) {
2232 cprintf("%d This is not a MIME message.\n",
2233 ERROR + ILLEGAL_VALUE);
2235 /* Parse the message text component */
2238 mptr = TheMessage->cm_fields[eMesageText];
2239 mime_parser(mptr, NULL, *mime_spew_section, NULL, NULL, (void *)&found_it, 0);
2240 /* If section wasn't found, print an error
2243 if (do_proto) cprintf(
2244 "%d Section %s not found.\n",
2245 ERROR + FILE_NOT_FOUND,
2246 CCC->download_desired_section);
2249 return((CCC->download_fp != NULL) ? om_ok : om_mime_error);
2252 /* now for the user-mode message reading loops */
2253 if (do_proto) cprintf("%d msg:\n", LISTING_FOLLOWS);
2255 /* Does the caller want to skip the headers? */
2256 if (headers_only == HEADERS_NONE) goto START_TEXT;
2258 /* Tell the client which format type we're using. */
2259 if ( (mode == MT_CITADEL) && (do_proto) ) {
2260 cprintf("type=%d\n", TheMessage->cm_format_type);
2263 /* nhdr=yes means that we're only displaying headers, no body */
2264 if ( (TheMessage->cm_anon_type == MES_ANONONLY)
2265 && ((mode == MT_CITADEL) || (mode == MT_MIME))
2268 cprintf("nhdr=yes\n");
2271 if ((mode == MT_CITADEL) || (mode == MT_MIME))
2272 OutputCtdlMsgHeaders(TheMessage, do_proto);
2275 /* begin header processing loop for RFC822 transfer format */
2279 strcpy(snode, NODENAME);
2280 if (mode == MT_RFC822)
2281 OutputRFC822MsgHeaders(
2286 suser, sizeof(suser),
2287 luser, sizeof(luser),
2288 fuser, sizeof(fuser),
2289 snode, sizeof(snode)
2293 for (i=0; !IsEmptyStr(&suser[i]); ++i) {
2294 suser[i] = tolower(suser[i]);
2295 if (!isalnum(suser[i])) suser[i]='_';
2298 if (mode == MT_RFC822) {
2299 if (!strcasecmp(snode, NODENAME)) {
2300 safestrncpy(snode, FQDN, sizeof snode);
2303 /* Construct a fun message id */
2304 cprintf("Message-ID: <%s", mid);/// todo: this possibly breaks threadding mails.
2305 if (strchr(mid, '@')==NULL) {
2306 cprintf("@%s", snode);
2310 if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONONLY)) {
2311 cprintf("From: \"----\" <x@x.org>%s", nl);
2313 else if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONOPT)) {
2314 cprintf("From: \"anonymous\" <x@x.org>%s", nl);
2316 else if (!IsEmptyStr(fuser)) {
2317 cprintf("From: \"%s\" <%s>%s", luser, fuser, nl);
2320 cprintf("From: \"%s\" <%s@%s>%s", luser, suser, snode, nl);
2323 /* Blank line signifying RFC822 end-of-headers */
2324 if (TheMessage->cm_format_type != FMT_RFC822) {
2329 /* end header processing loop ... at this point, we're in the text */
2331 if (headers_only == HEADERS_FAST) goto DONE;
2333 /* Tell the client about the MIME parts in this message */
2334 if (TheMessage->cm_format_type == FMT_RFC822) {
2335 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2336 mptr = TheMessage->cm_fields[eMesageText];
2337 memset(&ma, 0, sizeof(struct ma_info));
2338 mime_parser(mptr, NULL,
2339 (do_proto ? *list_this_part : NULL),
2340 (do_proto ? *list_this_pref : NULL),
2341 (do_proto ? *list_this_suff : NULL),
2344 else if (mode == MT_RFC822) { /* unparsed RFC822 dump */
2345 Dump_RFC822HeadersBody(
2354 if (headers_only == HEADERS_ONLY) {
2358 /* signify start of msg text */
2359 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
2360 if (do_proto) cprintf("text\n");
2363 if (TheMessage->cm_format_type == FMT_FIXED)
2366 mode, /* how would you like that message? */
2369 /* If the message on disk is format 0 (Citadel vari-format), we
2370 * output using the formatter at 80 columns. This is the final output
2371 * form if the transfer format is RFC822, but if the transfer format
2372 * is Citadel proprietary, it'll still work, because the indentation
2373 * for new paragraphs is correct and the client will reformat the
2374 * message to the reader's screen width.
2376 if (TheMessage->cm_format_type == FMT_CITADEL) {
2377 mptr = TheMessage->cm_fields[eMesageText];
2379 if (mode == MT_MIME) {
2380 cprintf("Content-type: text/x-citadel-variformat\n\n");
2385 /* If the message on disk is format 4 (MIME), we've gotta hand it
2386 * off to the MIME parser. The client has already been told that
2387 * this message is format 1 (fixed format), so the callback function
2388 * we use will display those parts as-is.
2390 if (TheMessage->cm_format_type == FMT_RFC822) {
2391 memset(&ma, 0, sizeof(struct ma_info));
2393 if (mode == MT_MIME) {
2394 ma.use_fo_hooks = 0;
2395 strcpy(ma.chosen_part, "1");
2396 ma.chosen_pref = 9999;
2397 ma.dont_decode = CCC->msg4_dont_decode;
2398 mime_parser(mptr, NULL,
2399 *choose_preferred, *fixed_output_pre,
2400 *fixed_output_post, (void *)&ma, 1);
2401 mime_parser(mptr, NULL,
2402 *output_preferred, NULL, NULL, (void *)&ma, 1);
2405 ma.use_fo_hooks = 1;
2406 mime_parser(mptr, NULL,
2407 *fixed_output, *fixed_output_pre,
2408 *fixed_output_post, (void *)&ma, 0);
2413 DONE: /* now we're done */
2414 if (do_proto) cprintf("000\n");
2420 * display a message (mode 0 - Citadel proprietary)
2422 void cmd_msg0(char *cmdbuf)
2425 int headers_only = HEADERS_ALL;
2427 msgid = extract_long(cmdbuf, 0);
2428 headers_only = extract_int(cmdbuf, 1);
2430 CtdlOutputMsg(msgid, MT_CITADEL, headers_only, 1, 0, NULL, 0, NULL, NULL);
2436 * display a message (mode 2 - RFC822)
2438 void cmd_msg2(char *cmdbuf)
2441 int headers_only = HEADERS_ALL;
2443 msgid = extract_long(cmdbuf, 0);
2444 headers_only = extract_int(cmdbuf, 1);
2446 CtdlOutputMsg(msgid, MT_RFC822, headers_only, 1, 1, NULL, 0, NULL, NULL);
2452 * display a message (mode 3 - IGnet raw format - internal programs only)
2454 void cmd_msg3(char *cmdbuf)
2457 struct CtdlMessage *msg = NULL;
2460 if (CC->internal_pgm == 0) {
2461 cprintf("%d This command is for internal programs only.\n",
2462 ERROR + HIGHER_ACCESS_REQUIRED);
2466 msgnum = extract_long(cmdbuf, 0);
2467 msg = CtdlFetchMessage(msgnum, 1);
2469 cprintf("%d Message %ld not found.\n",
2470 ERROR + MESSAGE_NOT_FOUND, msgnum);
2474 serialize_message(&smr, msg);
2478 cprintf("%d Unable to serialize message\n",
2479 ERROR + INTERNAL_ERROR);
2483 cprintf("%d %ld\n", BINARY_FOLLOWS, (long)smr.len);
2484 client_write((char *)smr.ser, (int)smr.len);
2491 * Display a message using MIME content types
2493 void cmd_msg4(char *cmdbuf)
2498 msgid = extract_long(cmdbuf, 0);
2499 extract_token(section, cmdbuf, 1, '|', sizeof section);
2500 CtdlOutputMsg(msgid, MT_MIME, 0, 1, 0, (section[0] ? section : NULL) , 0, NULL, NULL);
2506 * Client tells us its preferred message format(s)
2508 void cmd_msgp(char *cmdbuf)
2510 if (!strcasecmp(cmdbuf, "dont_decode")) {
2511 CC->msg4_dont_decode = 1;
2512 cprintf("%d MSG4 will not pre-decode messages.\n", CIT_OK);
2515 safestrncpy(CC->preferred_formats, cmdbuf, sizeof(CC->preferred_formats));
2516 cprintf("%d Preferred MIME formats have been set.\n", CIT_OK);
2522 * Open a component of a MIME message as a download file
2524 void cmd_opna(char *cmdbuf)
2527 char desired_section[128];
2529 msgid = extract_long(cmdbuf, 0);
2530 extract_token(desired_section, cmdbuf, 1, '|', sizeof desired_section);
2531 safestrncpy(CC->download_desired_section, desired_section,
2532 sizeof CC->download_desired_section);
2533 CtdlOutputMsg(msgid, MT_DOWNLOAD, 0, 1, 1, NULL, 0, NULL, NULL);
2538 * Open a component of a MIME message and transmit it all at once
2540 void cmd_dlat(char *cmdbuf)
2543 char desired_section[128];
2545 msgid = extract_long(cmdbuf, 0);
2546 extract_token(desired_section, cmdbuf, 1, '|', sizeof desired_section);
2547 safestrncpy(CC->download_desired_section, desired_section,
2548 sizeof CC->download_desired_section);
2549 CtdlOutputMsg(msgid, MT_SPEW_SECTION, 0, 1, 1, NULL, 0, NULL, NULL);
2554 * Save one or more message pointers into a specified room
2555 * (Returns 0 for success, nonzero for failure)
2556 * roomname may be NULL to use the current room
2558 * Note that the 'supplied_msg' field may be set to NULL, in which case
2559 * the message will be fetched from disk, by number, if we need to perform
2560 * replication checks. This adds an additional database read, so if the
2561 * caller already has the message in memory then it should be supplied. (Obviously
2562 * this mode of operation only works if we're saving a single message.)
2564 int CtdlSaveMsgPointersInRoom(char *roomname, long newmsgidlist[], int num_newmsgs,
2565 int do_repl_check, struct CtdlMessage *supplied_msg, int suppress_refcount_adj
2567 struct CitContext *CCC = CC;
2569 char hold_rm[ROOMNAMELEN];
2570 struct cdbdata *cdbfr;
2573 long highest_msg = 0L;
2576 struct CtdlMessage *msg = NULL;
2578 long *msgs_to_be_merged = NULL;
2579 int num_msgs_to_be_merged = 0;
2581 MSG_syslog(LOG_DEBUG,
2582 "CtdlSaveMsgPointersInRoom(room=%s, num_msgs=%d, repl=%d, suppress_rca=%d)\n",
2583 roomname, num_newmsgs, do_repl_check, suppress_refcount_adj
2586 strcpy(hold_rm, CCC->room.QRname);
2589 if (newmsgidlist == NULL) return(ERROR + INTERNAL_ERROR);
2590 if (num_newmsgs < 1) return(ERROR + INTERNAL_ERROR);
2591 if (num_newmsgs > 1) supplied_msg = NULL;
2593 /* Now the regular stuff */
2594 if (CtdlGetRoomLock(&CCC->room,
2595 ((roomname != NULL) ? roomname : CCC->room.QRname) )
2597 MSG_syslog(LOG_ERR, "No such room <%s>\n", roomname);
2598 return(ERROR + ROOM_NOT_FOUND);
2602 msgs_to_be_merged = malloc(sizeof(long) * num_newmsgs);
2603 num_msgs_to_be_merged = 0;
2606 cdbfr = cdb_fetch(CDB_MSGLISTS, &CCC->room.QRnumber, sizeof(long));
2607 if (cdbfr == NULL) {
2611 msglist = (long *) cdbfr->ptr;
2612 cdbfr->ptr = NULL; /* CtdlSaveMsgPointerInRoom() now owns this memory */
2613 num_msgs = cdbfr->len / sizeof(long);
2618 /* Create a list of msgid's which were supplied by the caller, but do
2619 * not already exist in the target room. It is absolutely taboo to
2620 * have more than one reference to the same message in a room.
2622 for (i=0; i<num_newmsgs; ++i) {
2624 if (num_msgs > 0) for (j=0; j<num_msgs; ++j) {
2625 if (msglist[j] == newmsgidlist[i]) {
2630 msgs_to_be_merged[num_msgs_to_be_merged++] = newmsgidlist[i];
2634 MSG_syslog(LOG_DEBUG, "%d unique messages to be merged\n", num_msgs_to_be_merged);
2637 * Now merge the new messages
2639 msglist = realloc(msglist, (sizeof(long) * (num_msgs + num_msgs_to_be_merged)) );
2640 if (msglist == NULL) {
2641 MSGM_syslog(LOG_ALERT, "ERROR: can't realloc message list!\n");
2642 free(msgs_to_be_merged);
2643 return (ERROR + INTERNAL_ERROR);
2645 memcpy(&msglist[num_msgs], msgs_to_be_merged, (sizeof(long) * num_msgs_to_be_merged) );
2646 num_msgs += num_msgs_to_be_merged;
2648 /* Sort the message list, so all the msgid's are in order */
2649 num_msgs = sort_msglist(msglist, num_msgs);
2651 /* Determine the highest message number */
2652 highest_msg = msglist[num_msgs - 1];
2654 /* Write it back to disk. */
2655 cdb_store(CDB_MSGLISTS, &CCC->room.QRnumber, (int)sizeof(long),
2656 msglist, (int)(num_msgs * sizeof(long)));
2658 /* Free up the memory we used. */
2661 /* Update the highest-message pointer and unlock the room. */
2662 CCC->room.QRhighest = highest_msg;
2663 CtdlPutRoomLock(&CCC->room);
2665 /* Perform replication checks if necessary */
2666 if ( (DoesThisRoomNeedEuidIndexing(&CCC->room)) && (do_repl_check) ) {
2667 MSGM_syslog(LOG_DEBUG, "CtdlSaveMsgPointerInRoom() doing repl checks\n");
2669 for (i=0; i<num_msgs_to_be_merged; ++i) {
2670 msgid = msgs_to_be_merged[i];
2672 if (supplied_msg != NULL) {
2676 msg = CtdlFetchMessage(msgid, 0);
2680 ReplicationChecks(msg);
2682 /* If the message has an Exclusive ID, index that... */
2683 if (!CM_IsEmpty(msg, eExclusiveID)) {
2684 index_message_by_euid(msg->cm_fields[eExclusiveID], &CCC->room, msgid);
2687 /* Free up the memory we may have allocated */
2688 if (msg != supplied_msg) {
2697 MSGM_syslog(LOG_DEBUG, "CtdlSaveMsgPointerInRoom() skips repl checks\n");
2700 /* Submit this room for processing by hooks */
2701 PerformRoomHooks(&CCC->room);
2703 /* Go back to the room we were in before we wandered here... */
2704 CtdlGetRoom(&CCC->room, hold_rm);
2706 /* Bump the reference count for all messages which were merged */
2707 if (!suppress_refcount_adj) {
2708 AdjRefCountList(msgs_to_be_merged, num_msgs_to_be_merged, +1);
2711 /* Free up memory... */
2712 if (msgs_to_be_merged != NULL) {
2713 free(msgs_to_be_merged);
2716 /* Return success. */
2722 * This is the same as CtdlSaveMsgPointersInRoom() but it only accepts
2725 int CtdlSaveMsgPointerInRoom(char *roomname, long msgid,
2726 int do_repl_check, struct CtdlMessage *supplied_msg)
2728 return CtdlSaveMsgPointersInRoom(roomname, &msgid, 1, do_repl_check, supplied_msg, 0);
2735 * Message base operation to save a new message to the message store
2736 * (returns new message number)
2738 * This is the back end for CtdlSubmitMsg() and should not be directly
2739 * called by server-side modules.
2742 long send_message(struct CtdlMessage *msg) {
2743 struct CitContext *CCC = CC;
2752 /* Get a new message number */
2753 newmsgid = get_new_message_number();
2754 msgidbuflen = snprintf(msgidbuf, sizeof msgidbuf, "%08lX-%08lX@%s",
2755 (long unsigned int) time(NULL),
2756 (long unsigned int) newmsgid,
2760 /* Generate an ID if we don't have one already */
2761 if (CM_IsEmpty(msg, emessageId)) {
2762 CM_SetField(msg, emessageId, msgidbuf, msgidbuflen);
2765 /* If the message is big, set its body aside for storage elsewhere */
2766 if (!CM_IsEmpty(msg, eMesageText)) {
2767 if (strlen(msg->cm_fields[eMesageText]) > BIGMSG) {
2769 holdM = msg->cm_fields[eMesageText];
2770 msg->cm_fields[eMesageText] = NULL;
2774 /* Serialize our data structure for storage in the database */
2775 serialize_message(&smr, msg);
2778 msg->cm_fields[eMesageText] = holdM;
2782 cprintf("%d Unable to serialize message\n",
2783 ERROR + INTERNAL_ERROR);
2787 /* Write our little bundle of joy into the message base */
2788 if (cdb_store(CDB_MSGMAIN, &newmsgid, (int)sizeof(long),
2789 smr.ser, smr.len) < 0) {
2790 MSGM_syslog(LOG_ERR, "Can't store message\n");
2794 cdb_store(CDB_BIGMSGS,
2804 /* Free the memory we used for the serialized message */
2807 /* Return the *local* message ID to the caller
2808 * (even if we're storing an incoming network message)
2816 * Serialize a struct CtdlMessage into the format used on disk and network.
2818 * This function loads up a "struct ser_ret" (defined in server.h) which
2819 * contains the length of the serialized message and a pointer to the
2820 * serialized message in memory. THE LATTER MUST BE FREED BY THE CALLER.
2822 void serialize_message(struct ser_ret *ret, /* return values */
2823 struct CtdlMessage *msg) /* unserialized msg */
2825 struct CitContext *CCC = CC;
2826 size_t wlen, fieldlen;
2828 long lengths[NDiskFields];
2830 memset(lengths, 0, sizeof(lengths));
2833 * Check for valid message format
2835 if (CM_IsValidMsg(msg) == 0) {
2836 MSGM_syslog(LOG_ERR, "serialize_message() aborting due to invalid message\n");
2843 for (i=0; i < NDiskFields; ++i)
2844 if (msg->cm_fields[FieldOrder[i]] != NULL)
2846 lengths[i] = strlen(msg->cm_fields[FieldOrder[i]]);
2847 ret->len += lengths[i] + 2;
2850 ret->ser = malloc(ret->len);
2851 if (ret->ser == NULL) {
2852 MSG_syslog(LOG_ERR, "serialize_message() malloc(%ld) failed: %s\n",
2853 (long)ret->len, strerror(errno));
2860 ret->ser[1] = msg->cm_anon_type;
2861 ret->ser[2] = msg->cm_format_type;
2864 for (i=0; i < NDiskFields; ++i)
2865 if (msg->cm_fields[FieldOrder[i]] != NULL)
2867 fieldlen = lengths[i];
2868 ret->ser[wlen++] = (char)FieldOrder[i];
2870 memcpy(&ret->ser[wlen],
2871 msg->cm_fields[FieldOrder[i]],
2874 wlen = wlen + fieldlen + 1;
2877 if (ret->len != wlen) {
2878 MSG_syslog(LOG_ERR, "ERROR: len=%ld wlen=%ld\n",
2879 (long)ret->len, (long)wlen);
2887 * Check to see if any messages already exist in the current room which
2888 * carry the same Exclusive ID as this one. If any are found, delete them.
2890 void ReplicationChecks(struct CtdlMessage *msg) {
2891 struct CitContext *CCC = CC;
2892 long old_msgnum = (-1L);
2894 if (DoesThisRoomNeedEuidIndexing(&CCC->room) == 0) return;
2896 MSG_syslog(LOG_DEBUG, "Performing replication checks in <%s>\n",
2899 /* No exclusive id? Don't do anything. */
2900 if (msg == NULL) return;
2901 if (CM_IsEmpty(msg, eExclusiveID)) return;
2903 /*MSG_syslog(LOG_DEBUG, "Exclusive ID: <%s> for room <%s>\n",
2904 msg->cm_fields[eExclusiveID], CCC->room.QRname);*/
2906 old_msgnum = CtdlLocateMessageByEuid(msg->cm_fields[eExclusiveID], &CCC->room);
2907 if (old_msgnum > 0L) {
2908 MSG_syslog(LOG_DEBUG, "ReplicationChecks() replacing message %ld\n", old_msgnum);
2909 CtdlDeleteMessages(CCC->room.QRname, &old_msgnum, 1, "");
2916 * Save a message to disk and submit it into the delivery system.
2918 long CtdlSubmitMsg(struct CtdlMessage *msg, /* message to save */
2919 struct recptypes *recps, /* recipients (if mail) */
2920 const char *force, /* force a particular room? */
2921 int flags /* should the message be exported clean? */
2924 char submit_filename[128];
2925 char hold_rm[ROOMNAMELEN];
2926 char actual_rm[ROOMNAMELEN];
2927 char force_room[ROOMNAMELEN];
2928 char content_type[SIZ]; /* We have to learn this */
2929 char recipient[SIZ];
2932 const char *mptr = NULL;
2933 struct ctdluser userbuf;
2935 struct MetaData smi;
2936 FILE *network_fp = NULL;
2937 static int seqnum = 1;
2938 struct CtdlMessage *imsg = NULL;
2940 size_t instr_alloc = 0;
2942 char *hold_R, *hold_D;
2943 char *collected_addresses = NULL;
2944 struct addresses_to_be_filed *aptr = NULL;
2945 StrBuf *saved_rfc822_version = NULL;
2946 int qualified_for_journaling = 0;
2947 CitContext *CCC = MyContext();
2948 char bounce_to[1024] = "";
2951 MSGM_syslog(LOG_DEBUG, "CtdlSubmitMsg() called\n");
2952 if (CM_IsValidMsg(msg) == 0) return(-1); /* self check */
2954 /* If this message has no timestamp, we take the liberty of
2955 * giving it one, right now.
2957 if (CM_IsEmpty(msg, eTimestamp)) {
2958 CM_SetFieldLONG(msg, eTimestamp, time(NULL));
2961 /* If this message has no path, we generate one.
2963 if (CM_IsEmpty(msg, eMessagePath)) {
2964 if (!CM_IsEmpty(msg, eAuthor)) {
2965 CM_CopyField(msg, eMessagePath, eAuthor);
2966 for (a=0; !IsEmptyStr(&msg->cm_fields[eMessagePath][a]); ++a) {
2967 if (isspace(msg->cm_fields[eMessagePath][a])) {
2968 msg->cm_fields[eMessagePath][a] = ' ';
2973 CM_SetField(msg, eMessagePath, HKEY("unknown"));
2977 if (force == NULL) {
2978 force_room[0] = '\0';
2981 strcpy(force_room, force);
2984 /* Learn about what's inside, because it's what's inside that counts */
2985 if (CM_IsEmpty(msg, eMesageText)) {
2986 MSGM_syslog(LOG_ERR, "ERROR: attempt to save message with NULL body\n");
2990 switch (msg->cm_format_type) {
2992 strcpy(content_type, "text/x-citadel-variformat");
2995 strcpy(content_type, "text/plain");
2998 strcpy(content_type, "text/plain");
2999 mptr = bmstrcasestr(msg->cm_fields[eMesageText], "Content-type:");
3002 safestrncpy(content_type, &mptr[13], sizeof content_type);
3003 striplt(content_type);
3004 aptr = content_type;
3005 while (!IsEmptyStr(aptr)) {
3017 /* Goto the correct room */
3018 room = (recps) ? CCC->room.QRname : SENTITEMS;
3019 MSG_syslog(LOG_DEBUG, "Selected room %s\n", room);
3020 strcpy(hold_rm, CCC->room.QRname);
3021 strcpy(actual_rm, CCC->room.QRname);
3022 if (recps != NULL) {
3023 strcpy(actual_rm, SENTITEMS);
3026 /* If the user is a twit, move to the twit room for posting */
3028 if (CCC->user.axlevel == AxProbU) {
3029 strcpy(hold_rm, actual_rm);
3030 strcpy(actual_rm, config.c_twitroom);
3031 MSGM_syslog(LOG_DEBUG, "Diverting to twit room\n");
3035 /* ...or if this message is destined for Aide> then go there. */
3036 if (!IsEmptyStr(force_room)) {
3037 strcpy(actual_rm, force_room);
3040 MSG_syslog(LOG_INFO, "Final selection: %s (%s)\n", actual_rm, room);
3041 if (strcasecmp(actual_rm, CCC->room.QRname)) {
3042 /* CtdlGetRoom(&CCC->room, actual_rm); */
3043 CtdlUserGoto(actual_rm, 0, 1, NULL, NULL);
3047 * If this message has no O (room) field, generate one.
3049 if (CM_IsEmpty(msg, eOriginalRoom)) {
3050 CM_SetField(msg, eOriginalRoom, CCC->room.QRname, strlen(CCC->room.QRname));
3053 /* Perform "before save" hooks (aborting if any return nonzero) */
3054 MSGM_syslog(LOG_DEBUG, "Performing before-save hooks\n");
3055 if (PerformMessageHooks(msg, EVT_BEFORESAVE) > 0) return(-3);
3058 * If this message has an Exclusive ID, and the room is replication
3059 * checking enabled, then do replication checks.
3061 if (DoesThisRoomNeedEuidIndexing(&CCC->room)) {
3062 ReplicationChecks(msg);
3065 /* Save it to disk */
3066 MSGM_syslog(LOG_DEBUG, "Saving to disk\n");
3067 newmsgid = send_message(msg);
3068 if (newmsgid <= 0L) return(-5);
3070 /* Write a supplemental message info record. This doesn't have to
3071 * be a critical section because nobody else knows about this message
3074 MSGM_syslog(LOG_DEBUG, "Creating MetaData record\n");
3075 memset(&smi, 0, sizeof(struct MetaData));
3076 smi.meta_msgnum = newmsgid;
3077 smi.meta_refcount = 0;
3078 safestrncpy(smi.meta_content_type, content_type,
3079 sizeof smi.meta_content_type);
3082 * Measure how big this message will be when rendered as RFC822.
3083 * We do this for two reasons:
3084 * 1. We need the RFC822 length for the new metadata record, so the
3085 * POP and IMAP services don't have to calculate message lengths
3086 * while the user is waiting (multiplied by potentially hundreds
3087 * or thousands of messages).
3088 * 2. If journaling is enabled, we will need an RFC822 version of the
3089 * message to attach to the journalized copy.
3091 if (CCC->redirect_buffer != NULL) {
3092 MSGM_syslog(LOG_ALERT, "CCC->redirect_buffer is not NULL during message submission!\n");
3095 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
3096 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, QP_EADDR);
3097 smi.meta_rfc822_length = StrLength(CCC->redirect_buffer);
3098 saved_rfc822_version = CCC->redirect_buffer;
3099 CCC->redirect_buffer = NULL;
3103 /* Now figure out where to store the pointers */
3104 MSGM_syslog(LOG_DEBUG, "Storing pointers\n");
3106 /* If this is being done by the networker delivering a private
3107 * message, we want to BYPASS saving the sender's copy (because there
3108 * is no local sender; it would otherwise go to the Trashcan).
3110 if ((!CCC->internal_pgm) || (recps == NULL)) {
3111 if (CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 1, msg) != 0) {
3112 MSGM_syslog(LOG_ERR, "ERROR saving message pointer!\n");
3113 CtdlSaveMsgPointerInRoom(config.c_aideroom, newmsgid, 0, msg);
3117 /* For internet mail, drop a copy in the outbound queue room */
3118 if ((recps != NULL) && (recps->num_internet > 0)) {
3119 CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, newmsgid, 0, msg);
3122 /* If other rooms are specified, drop them there too. */
3123 if ((recps != NULL) && (recps->num_room > 0))
3124 for (i=0; i<num_tokens(recps->recp_room, '|'); ++i) {
3125 extract_token(recipient, recps->recp_room, i,
3126 '|', sizeof recipient);
3127 MSG_syslog(LOG_DEBUG, "Delivering to room <%s>\n", recipient);///// xxxx
3128 CtdlSaveMsgPointerInRoom(recipient, newmsgid, 0, msg);
3131 /* Bump this user's messages posted counter. */
3132 MSGM_syslog(LOG_DEBUG, "Updating user\n");
3133 CtdlGetUserLock(&CCC->user, CCC->curr_user);
3134 CCC->user.posted = CCC->user.posted + 1;
3135 CtdlPutUserLock(&CCC->user);
3137 /* Decide where bounces need to be delivered */
3138 if ((recps != NULL) && (recps->bounce_to != NULL)) {
3139 safestrncpy(bounce_to, recps->bounce_to, sizeof bounce_to);
3141 else if (CCC->logged_in) {
3142 snprintf(bounce_to, sizeof bounce_to, "%s@%s", CCC->user.fullname, config.c_nodename);
3145 snprintf(bounce_to, sizeof bounce_to, "%s@%s", msg->cm_fields[eAuthor], msg->cm_fields[eNodeName]);
3148 /* If this is private, local mail, make a copy in the
3149 * recipient's mailbox and bump the reference count.
3151 if ((recps != NULL) && (recps->num_local > 0))
3152 for (i=0; i<num_tokens(recps->recp_local, '|'); ++i) {
3154 recipientlen = extract_token(recipient,
3155 recps->recp_local, i,
3156 '|', sizeof recipient);
3157 MSG_syslog(LOG_DEBUG, "Delivering private local mail to <%s>\n",
3159 if (CtdlGetUser(&userbuf, recipient) == 0) {
3160 CtdlMailboxName(actual_rm, sizeof actual_rm, &userbuf, MAILROOM);
3161 CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0, msg);
3162 CtdlBumpNewMailCounter(userbuf.usernum);
3163 if (!IsEmptyStr(config.c_funambol_host) || !IsEmptyStr(config.c_pager_program)) {
3164 /* Generate a instruction message for the Funambol notification
3165 * server, in the same style as the SMTP queue
3169 instr = malloc(instr_alloc);
3170 instrlen = snprintf(
3172 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
3176 (long)time(NULL), //todo: time() is expensive!
3180 imsg = malloc(sizeof(struct CtdlMessage));
3181 memset(imsg, 0, sizeof(struct CtdlMessage));
3182 imsg->cm_magic = CTDLMESSAGE_MAGIC;
3183 imsg->cm_anon_type = MES_NORMAL;
3184 imsg->cm_format_type = FMT_RFC822;
3185 CM_SetField(imsg, eMsgSubject, HKEY("QMSG"));
3186 CM_SetField(imsg, eAuthor, HKEY("Citadel"));
3187 CM_SetField(imsg, eJournal, HKEY("do not journal"));
3188 CM_SetAsField(imsg, eMesageText, &instr, instrlen);
3189 CM_SetField(imsg, eExtnotify, recipient, recipientlen);
3190 CtdlSubmitMsg(imsg, NULL, FNBL_QUEUE_ROOM, 0);
3195 MSG_syslog(LOG_DEBUG, "No user <%s>\n", recipient);
3196 CtdlSaveMsgPointerInRoom(config.c_aideroom, newmsgid, 0, msg);
3200 /* Perform "after save" hooks */
3201 MSGM_syslog(LOG_DEBUG, "Performing after-save hooks\n");
3203 CM_SetFieldLONG(msg, eVltMsgNum, newmsgid);
3204 PerformMessageHooks(msg, EVT_AFTERSAVE);
3205 CM_FlushField(msg, eVltMsgNum);
3207 /* For IGnet mail, we have to save a new copy into the spooler for
3208 * each recipient, with the R and D fields set to the recipient and
3209 * destination-node. This has two ugly side effects: all other
3210 * recipients end up being unlisted in this recipient's copy of the
3211 * message, and it has to deliver multiple messages to the same
3212 * node. We'll revisit this again in a year or so when everyone has
3213 * a network spool receiver that can handle the new style messages.
3215 if ((recps != NULL) && (recps->num_ignet > 0))
3216 for (i=0; i<num_tokens(recps->recp_ignet, '|'); ++i) {
3217 extract_token(recipient, recps->recp_ignet, i,
3218 '|', sizeof recipient);
3220 hold_R = msg->cm_fields[eRecipient];
3221 hold_D = msg->cm_fields[eDestination];
3222 msg->cm_fields[eRecipient] = malloc(SIZ);
3223 msg->cm_fields[eDestination] = malloc(128);
3224 extract_token(msg->cm_fields[eRecipient], recipient, 0, '@', SIZ);
3225 extract_token(msg->cm_fields[eDestination], recipient, 1, '@', 128);
3227 serialize_message(&smr, msg);
3229 snprintf(submit_filename, sizeof submit_filename,
3230 "%s/netmail.%04lx.%04x.%04x",
3232 (long) getpid(), CCC->cs_pid, ++seqnum);
3233 network_fp = fopen(submit_filename, "wb+");
3234 if (network_fp != NULL) {
3235 rv = fwrite(smr.ser, smr.len, 1, network_fp);
3237 MSG_syslog(LOG_EMERG, "CtdlSubmitMsg(): Couldn't write network spool file: %s\n",
3245 free(msg->cm_fields[eRecipient]);
3246 free(msg->cm_fields[eDestination]);
3247 msg->cm_fields[eRecipient] = hold_R;
3248 msg->cm_fields[eDestination] = hold_D;
3251 /* Go back to the room we started from */
3252 MSG_syslog(LOG_DEBUG, "Returning to original room %s\n", hold_rm);
3253 if (strcasecmp(hold_rm, CCC->room.QRname))
3254 CtdlUserGoto(hold_rm, 0, 1, NULL, NULL);
3256 /* For internet mail, generate delivery instructions.
3257 * Yes, this is recursive. Deal with it. Infinite recursion does
3258 * not happen because the delivery instructions message does not
3259 * contain a recipient.
3261 if ((recps != NULL) && (recps->num_internet > 0)) {
3262 StrBuf *SpoolMsg = NewStrBuf();
3265 MSGM_syslog(LOG_DEBUG, "Generating delivery instructions\n");
3267 StrBufPrintf(SpoolMsg,
3268 "Content-type: "SPOOLMIME"\n"
3277 if (recps->envelope_from != NULL) {
3278 StrBufAppendBufPlain(SpoolMsg, HKEY("envelope_from|"), 0);
3279 StrBufAppendBufPlain(SpoolMsg, recps->envelope_from, -1, 0);
3280 StrBufAppendBufPlain(SpoolMsg, HKEY("\n"), 0);
3282 if (recps->sending_room != NULL) {
3283 StrBufAppendBufPlain(SpoolMsg, HKEY("source_room|"), 0);
3284 StrBufAppendBufPlain(SpoolMsg, recps->sending_room, -1, 0);
3285 StrBufAppendBufPlain(SpoolMsg, HKEY("\n"), 0);
3288 nTokens = num_tokens(recps->recp_internet, '|');
3289 for (i = 0; i < nTokens; i++) {
3291 len = extract_token(recipient, recps->recp_internet, i, '|', sizeof recipient);
3293 StrBufAppendBufPlain(SpoolMsg, HKEY("remote|"), 0);
3294 StrBufAppendBufPlain(SpoolMsg, recipient, len, 0);
3295 StrBufAppendBufPlain(SpoolMsg, HKEY("|0||\n"), 0);
3299 imsg = malloc(sizeof(struct CtdlMessage));
3300 memset(imsg, 0, sizeof(struct CtdlMessage));
3301 imsg->cm_magic = CTDLMESSAGE_MAGIC;
3302 imsg->cm_anon_type = MES_NORMAL;
3303 imsg->cm_format_type = FMT_RFC822;
3304 imsg->cm_fields[eMsgSubject] = strdup("QMSG");
3305 imsg->cm_fields[eAuthor] = strdup("Citadel");
3306 imsg->cm_fields[eJournal] = strdup("do not journal");
3307 imsg->cm_fields[eMesageText] = SmashStrBuf(&SpoolMsg); /* imsg owns this memory now */
3308 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM, QP_EADDR);
3313 * Any addresses to harvest for someone's address book?
3315 if ( (CCC->logged_in) && (recps != NULL) ) {
3316 collected_addresses = harvest_collected_addresses(msg);
3319 if (collected_addresses != NULL) {
3320 aptr = (struct addresses_to_be_filed *)
3321 malloc(sizeof(struct addresses_to_be_filed));
3322 CtdlMailboxName(actual_rm, sizeof actual_rm,
3323 &CCC->user, USERCONTACTSROOM);
3324 aptr->roomname = strdup(actual_rm);
3325 aptr->collected_addresses = collected_addresses;
3326 begin_critical_section(S_ATBF);
3329 end_critical_section(S_ATBF);
3333 * Determine whether this message qualifies for journaling.
3335 if (!CM_IsEmpty(msg, eJournal)) {
3336 qualified_for_journaling = 0;
3339 if (recps == NULL) {
3340 qualified_for_journaling = config.c_journal_pubmsgs;
3342 else if (recps->num_local + recps->num_ignet + recps->num_internet > 0) {
3343 qualified_for_journaling = config.c_journal_email;
3346 qualified_for_journaling = config.c_journal_pubmsgs;
3351 * Do we have to perform journaling? If so, hand off the saved
3352 * RFC822 version will be handed off to the journaler for background
3353 * submit. Otherwise, we have to free the memory ourselves.
3355 if (saved_rfc822_version != NULL) {
3356 if (qualified_for_journaling) {
3357 JournalBackgroundSubmit(msg, saved_rfc822_version, recps);
3360 FreeStrBuf(&saved_rfc822_version);
3370 * Convenience function for generating small administrative messages.
3372 void quickie_message(const char *from,
3373 const char *fromaddr,
3378 const char *subject)
3380 struct CtdlMessage *msg;
3381 struct recptypes *recp = NULL;
3383 msg = malloc(sizeof(struct CtdlMessage));
3384 memset(msg, 0, sizeof(struct CtdlMessage));
3385 msg->cm_magic = CTDLMESSAGE_MAGIC;
3386 msg->cm_anon_type = MES_NORMAL;
3387 msg->cm_format_type = format_type;
3390 msg->cm_fields[eAuthor] = strdup(from);
3392 else if (fromaddr != NULL) {
3393 msg->cm_fields[eAuthor] = strdup(fromaddr);
3394 if (strchr(msg->cm_fields[eAuthor], '@')) {
3395 *strchr(msg->cm_fields[eAuthor], '@') = 0;
3399 msg->cm_fields[eAuthor] = strdup("Citadel");
3402 if (fromaddr != NULL) msg->cm_fields[erFc822Addr] = strdup(fromaddr);
3403 if (room != NULL) msg->cm_fields[eOriginalRoom] = strdup(room);
3404 msg->cm_fields[eNodeName] = strdup(NODENAME);
3406 msg->cm_fields[eRecipient] = strdup(to);
3407 recp = validate_recipients(to, NULL, 0);
3409 if (subject != NULL) {
3410 msg->cm_fields[eMsgSubject] = strdup(subject);
3412 msg->cm_fields[eMesageText] = strdup(text);
3414 CtdlSubmitMsg(msg, recp, room, 0);
3416 if (recp != NULL) free_recipients(recp);
3419 void flood_protect_quickie_message(const char *from,
3420 const char *fromaddr,
3425 const char *subject,
3427 const char **CritStr,
3434 u_char rawdigest[MD5_DIGEST_LEN];
3435 struct MD5Context md5context;
3439 time_t tsday = NOW / (8*60*60); /* just care for a day... */
3441 tslen = snprintf(timestamp, sizeof(timestamp), "%ld", tsday);
3442 MD5Init(&md5context);
3444 for (i = 0; i < nCriterions; i++)
3445 MD5Update(&md5context,
3446 (const unsigned char*)CritStr[i], CritStrLen[i]);
3447 MD5Update(&md5context,
3448 (const unsigned char*)timestamp, tslen);
3449 MD5Final(rawdigest, &md5context);
3451 guid = NewStrBufPlain(NULL,
3452 MD5_DIGEST_LEN * 2 + 12);
3453 StrBufHexEscAppend(guid, NULL, rawdigest, MD5_DIGEST_LEN);
3454 StrBufAppendBufPlain(guid, HKEY("_fldpt"), 0);
3455 if (StrLength(guid) > 40)
3456 StrBufCutAt(guid, 40, NULL);
3458 if (CheckIfAlreadySeen("FPAideMessage",
3467 /* yes, we did. flood protection kicks in. */
3469 "not sending message again\n");
3473 /* no, this message isn't sent recently; go ahead. */
3474 quickie_message(from,
3485 * Back end function used by CtdlMakeMessage() and similar functions
3487 StrBuf *CtdlReadMessageBodyBuf(char *terminator, /* token signalling EOT */
3489 size_t maxlen, /* maximum message length */
3490 StrBuf *exist, /* if non-null, append to it;
3491 exist is ALWAYS freed */
3492 int crlf, /* CRLF newlines instead of LF */
3493 int *sock /* socket handle or 0 for this session's client socket */
3502 LineBuf = NewStrBufPlain(NULL, SIZ);
3503 if (exist == NULL) {
3504 Message = NewStrBufPlain(NULL, 4 * SIZ);
3507 Message = NewStrBufDup(exist);
3510 /* Do we need to change leading ".." to "." for SMTP escaping? */
3511 if ((tlen == 1) && (*terminator == '.')) {
3515 /* read in the lines of message text one by one */
3518 if ((CtdlSockGetLine(sock, LineBuf, 5) < 0) ||
3523 if (CtdlClientGetLine(LineBuf) < 0) finished = 1;
3525 if ((StrLength(LineBuf) == tlen) &&
3526 (!strcmp(ChrPtr(LineBuf), terminator)))
3529 if ( (!flushing) && (!finished) ) {
3531 StrBufAppendBufPlain(LineBuf, HKEY("\r\n"), 0);
3534 StrBufAppendBufPlain(LineBuf, HKEY("\n"), 0);
3537 /* Unescape SMTP-style input of two dots at the beginning of the line */
3539 (StrLength(LineBuf) == 2) &&
3540 (!strcmp(ChrPtr(LineBuf), "..")))
3542 StrBufCutLeft(LineBuf, 1);
3545 StrBufAppendBuf(Message, LineBuf, 0);
3548 /* if we've hit the max msg length, flush the rest */
3549 if (StrLength(Message) >= maxlen) flushing = 1;
3551 } while (!finished);
3552 FreeStrBuf(&LineBuf);
3556 void DeleteAsyncMsg(ReadAsyncMsg **Msg)
3560 FreeStrBuf(&(*Msg)->MsgBuf);
3566 ReadAsyncMsg *NewAsyncMsg(const char *terminator, /* token signalling EOT */
3568 size_t maxlen, /* maximum message length */
3569 size_t expectlen, /* if we expect a message, how long should it be? */
3570 StrBuf *exist, /* if non-null, append to it;
3571 exist is ALWAYS freed */
3572 long eLen, /* length of exist */
3573 int crlf /* CRLF newlines instead of LF */
3576 ReadAsyncMsg *NewMsg;
3578 NewMsg = (ReadAsyncMsg *)malloc(sizeof(ReadAsyncMsg));
3579 memset(NewMsg, 0, sizeof(ReadAsyncMsg));
3581 if (exist == NULL) {
3584 if (expectlen == 0) {
3588 len = expectlen + 10;
3590 NewMsg->MsgBuf = NewStrBufPlain(NULL, len);
3593 NewMsg->MsgBuf = NewStrBufDup(exist);
3595 /* Do we need to change leading ".." to "." for SMTP escaping? */
3596 if ((tlen == 1) && (*terminator == '.')) {
3600 NewMsg->terminator = terminator;
3601 NewMsg->tlen = tlen;
3603 NewMsg->maxlen = maxlen;
3605 NewMsg->crlf = crlf;
3611 * Back end function used by CtdlMakeMessage() and similar functions
3613 eReadState CtdlReadMessageBodyAsync(AsyncIO *IO)
3615 ReadAsyncMsg *ReadMsg;
3616 int MsgFinished = 0;
3617 eReadState Finished = eMustReadMore;
3622 const char *pch = ChrPtr(IO->SendBuf.Buf);
3623 const char *pchh = IO->SendBuf.ReadWritePointer;
3629 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
3630 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
3631 ((CitContext*)(IO->CitContext))->ServiceName,
3634 fd = fopen(fn, "a+");
3637 ReadMsg = IO->ReadMsg;
3639 /* read in the lines of message text one by one */
3641 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
3644 case eMustReadMore: /// read new from socket...
3646 if (IO->RecvBuf.ReadWritePointer != NULL) {
3647 nbytes = StrLength(IO->RecvBuf.Buf) - (IO->RecvBuf.ReadWritePointer - ChrPtr(IO->RecvBuf.Buf));
3648 fprintf(fd, "Read; Line unfinished: %ld Bytes still in buffer [", nbytes);
3650 fwrite(IO->RecvBuf.ReadWritePointer, nbytes, 1, fd);
3654 fprintf(fd, "BufferEmpty! \n");
3660 case eBufferNotEmpty: /* shouldn't happen... */
3661 case eReadSuccess: /// done for now...
3663 case eReadFail: /// WHUT?
3669 if ((StrLength(IO->IOBuf) == ReadMsg->tlen) &&
3670 (!strcmp(ChrPtr(IO->IOBuf), ReadMsg->terminator))) {
3673 fprintf(fd, "found Terminator; Message Size: %d\n", StrLength(ReadMsg->MsgBuf));
3676 else if (!ReadMsg->flushing) {
3679 fprintf(fd, "Read Line: [%d][%s]\n", StrLength(IO->IOBuf), ChrPtr(IO->IOBuf));
3682 /* Unescape SMTP-style input of two dots at the beginning of the line */
3683 if ((ReadMsg->dodot) &&
3684 (StrLength(IO->IOBuf) == 2) && /* TODO: do we just unescape lines with two dots or any line? */
3685 (!strcmp(ChrPtr(IO->IOBuf), "..")))
3688 fprintf(fd, "UnEscaped!\n");
3690 StrBufCutLeft(IO->IOBuf, 1);
3693 if (ReadMsg->crlf) {
3694 StrBufAppendBufPlain(IO->IOBuf, HKEY("\r\n"), 0);
3697 StrBufAppendBufPlain(IO->IOBuf, HKEY("\n"), 0);
3700 StrBufAppendBuf(ReadMsg->MsgBuf, IO->IOBuf, 0);
3703 /* if we've hit the max msg length, flush the rest */
3704 if (StrLength(ReadMsg->MsgBuf) >= ReadMsg->maxlen) ReadMsg->flushing = 1;
3706 } while (!MsgFinished);
3709 fprintf(fd, "Done with reading; %s.\n, ",
3710 (MsgFinished)?"Message Finished": "FAILED");
3714 return eReadSuccess;
3721 * Back end function used by CtdlMakeMessage() and similar functions
3723 char *CtdlReadMessageBody(char *terminator, /* token signalling EOT */
3725 size_t maxlen, /* maximum message length */
3726 StrBuf *exist, /* if non-null, append to it;
3727 exist is ALWAYS freed */
3728 int crlf, /* CRLF newlines instead of LF */
3729 int *sock /* socket handle or 0 for this session's client socket */
3734 Message = CtdlReadMessageBodyBuf(terminator,
3740 if (Message == NULL)
3743 return SmashStrBuf(&Message);
3748 * Build a binary message to be saved on disk.
3749 * (NOTE: if you supply 'preformatted_text', the buffer you give it
3750 * will become part of the message. This means you are no longer
3751 * responsible for managing that memory -- it will be freed along with
3752 * the rest of the fields when CM_Free() is called.)
3755 struct CtdlMessage *CtdlMakeMessage(
3756 struct ctdluser *author, /* author's user structure */
3757 char *recipient, /* NULL if it's not mail */
3758 char *recp_cc, /* NULL if it's not mail */
3759 char *room, /* room where it's going */
3760 int type, /* see MES_ types in header file */
3761 int format_type, /* variformat, plain text, MIME... */
3762 char *fake_name, /* who we're masquerading as */
3763 char *my_email, /* which of my email addresses to use (empty is ok) */
3764 char *subject, /* Subject (optional) */
3765 char *supplied_euid, /* ...or NULL if this is irrelevant */
3766 char *preformatted_text, /* ...or NULL to read text from client */
3767 char *references /* Thread references */
3769 char dest_node[256];
3771 struct CtdlMessage *msg;
3773 StrBuf *FakeEncAuthor = NULL;
3775 msg = malloc(sizeof(struct CtdlMessage));
3776 memset(msg, 0, sizeof(struct CtdlMessage));
3777 msg->cm_magic = CTDLMESSAGE_MAGIC;
3778 msg->cm_anon_type = type;
3779 msg->cm_format_type = format_type;
3781 /* Don't confuse the poor folks if it's not routed mail. */
3782 strcpy(dest_node, "");
3784 if (recipient != NULL) striplt(recipient);
3785 if (recp_cc != NULL) striplt(recp_cc);
3787 /* Path or Return-Path */
3788 if (my_email == NULL) my_email = "";
3790 if (!IsEmptyStr(my_email)) {
3791 msg->cm_fields[eMessagePath] = strdup(my_email);
3794 snprintf(buf, sizeof buf, "%s", author->fullname);
3795 msg->cm_fields[eMessagePath] = strdup(buf);
3797 convert_spaces_to_underscores(msg->cm_fields[eMessagePath]);
3799 snprintf(buf, sizeof buf, "%ld", (long)time(NULL)); /* timestamp */
3800 msg->cm_fields[eTimestamp] = strdup(buf);
3802 if ((fake_name != NULL) && (fake_name[0])) { /* author */
3803 FakeAuthor = NewStrBufPlain (fake_name, -1);
3806 FakeAuthor = NewStrBufPlain (author->fullname, -1);
3808 StrBufRFC2047encode(&FakeEncAuthor, FakeAuthor);
3809 msg->cm_fields[eAuthor] = SmashStrBuf(&FakeEncAuthor);
3810 FreeStrBuf(&FakeAuthor);
3812 if (CC->room.QRflags & QR_MAILBOX) { /* room */
3813 msg->cm_fields[eOriginalRoom] = strdup(&CC->room.QRname[11]);
3816 msg->cm_fields[eOriginalRoom] = strdup(CC->room.QRname);
3819 msg->cm_fields[eNodeName] = strdup(NODENAME); /* nodename */
3820 msg->cm_fields[eHumanNode] = strdup(HUMANNODE); /* hnodename */
3822 if ((recipient != NULL) && (recipient[0] != 0)) {
3823 msg->cm_fields[eRecipient] = strdup(recipient);
3825 if ((recp_cc != NULL) && (recp_cc[0] != 0)) {
3826 msg->cm_fields[eCarbonCopY] = strdup(recp_cc);
3828 if (dest_node[0] != 0) {
3829 msg->cm_fields[eDestination] = strdup(dest_node);
3832 if (!IsEmptyStr(my_email)) {
3833 msg->cm_fields[erFc822Addr] = strdup(my_email);
3835 else if ( (author == &CC->user) && (!IsEmptyStr(CC->cs_inet_email)) ) {
3836 msg->cm_fields[erFc822Addr] = strdup(CC->cs_inet_email);
3839 if (subject != NULL) {
3842 length = strlen(subject);
3848 while ((subject[i] != '\0') &&
3849 (IsAscii = isascii(subject[i]) != 0 ))
3852 msg->cm_fields[eMsgSubject] = strdup(subject);
3853 else /* ok, we've got utf8 in the string. */
3855 msg->cm_fields[eMsgSubject] = rfc2047encode(subject, length);
3861 if (supplied_euid != NULL) {
3862 msg->cm_fields[eExclusiveID] = strdup(supplied_euid);
3865 if ((references != NULL) && (!IsEmptyStr(references))) {
3866 if (msg->cm_fields[eWeferences] != NULL)
3867 free(msg->cm_fields[eWeferences]);
3868 msg->cm_fields[eWeferences] = strdup(references);
3871 if (preformatted_text != NULL) {
3872 msg->cm_fields[eMesageText] = preformatted_text;
3875 msg->cm_fields[eMesageText] = CtdlReadMessageBody(HKEY("000"), config.c_maxmsglen, NULL, 0, 0);
3884 * message entry - mode 0 (normal)
3886 void cmd_ent0(char *entargs)
3888 struct CitContext *CCC = CC;
3893 char supplied_euid[128];
3895 int format_type = 0;
3896 char newusername[256];
3897 char newuseremail[256];
3898 struct CtdlMessage *msg;
3902 struct recptypes *valid = NULL;
3903 struct recptypes *valid_to = NULL;
3904 struct recptypes *valid_cc = NULL;
3905 struct recptypes *valid_bcc = NULL;
3907 int subject_required = 0;
3912 int newuseremail_ok = 0;
3913 char references[SIZ];
3918 post = extract_int(entargs, 0);
3919 extract_token(recp, entargs, 1, '|', sizeof recp);
3920 anon_flag = extract_int(entargs, 2);
3921 format_type = extract_int(entargs, 3);
3922 extract_token(subject, entargs, 4, '|', sizeof subject);
3923 extract_token(newusername, entargs, 5, '|', sizeof newusername);
3924 do_confirm = extract_int(entargs, 6);
3925 extract_token(cc, entargs, 7, '|', sizeof cc);
3926 extract_token(bcc, entargs, 8, '|', sizeof bcc);
3927 switch(CC->room.QRdefaultview) {
3930 extract_token(supplied_euid, entargs, 9, '|', sizeof supplied_euid);
3933 supplied_euid[0] = 0;
3936 extract_token(newuseremail, entargs, 10, '|', sizeof newuseremail);
3937 extract_token(references, entargs, 11, '|', sizeof references);
3938 for (ptr=references; *ptr != 0; ++ptr) {
3939 if (*ptr == '!') *ptr = '|';
3942 /* first check to make sure the request is valid. */
3944 err = CtdlDoIHavePermissionToPostInThisRoom(
3949 (!IsEmptyStr(references)) /* is this a reply? or a top-level post? */
3953 cprintf("%d %s\n", err, errmsg);
3957 /* Check some other permission type things. */
3959 if (IsEmptyStr(newusername)) {
3960 strcpy(newusername, CCC->user.fullname);
3962 if ( (CCC->user.axlevel < AxAideU)
3963 && (strcasecmp(newusername, CCC->user.fullname))
3964 && (strcasecmp(newusername, CCC->cs_inet_fn))
3966 cprintf("%d You don't have permission to author messages as '%s'.\n",
3967 ERROR + HIGHER_ACCESS_REQUIRED,
3974 if (IsEmptyStr(newuseremail)) {
3975 newuseremail_ok = 1;
3978 if (!IsEmptyStr(newuseremail)) {
3979 if (!strcasecmp(newuseremail, CCC->cs_inet_email)) {
3980 newuseremail_ok = 1;
3982 else if (!IsEmptyStr(CCC->cs_inet_other_emails)) {
3983 j = num_tokens(CCC->cs_inet_other_emails, '|');
3984 for (i=0; i<j; ++i) {
3985 extract_token(buf, CCC->cs_inet_other_emails, i, '|', sizeof buf);
3986 if (!strcasecmp(newuseremail, buf)) {
3987 newuseremail_ok = 1;
3993 if (!newuseremail_ok) {
3994 cprintf("%d You don't have permission to author messages as '%s'.\n",
3995 ERROR + HIGHER_ACCESS_REQUIRED,
4001 CCC->cs_flags |= CS_POSTING;
4003 /* In mailbox rooms we have to behave a little differently --
4004 * make sure the user has specified at least one recipient. Then
4005 * validate the recipient(s). We do this for the Mail> room, as
4006 * well as any room which has the "Mailbox" view set - unless it
4007 * is the DRAFTS room which does not require recipients
4010 if ( ( ( (CCC->room.QRflags & QR_MAILBOX) && (!strcasecmp(&CCC->room.QRname[11], MAILROOM)) )
4011 || ( (CCC->room.QRflags & QR_MAILBOX) && (CCC->curr_view == VIEW_MAILBOX) )
4012 ) && (strcasecmp(&CCC->room.QRname[11], USERDRAFTROOM)) !=0 ) {
4013 if (CCC->user.axlevel < AxProbU) {
4014 strcpy(recp, "sysop");
4019 valid_to = validate_recipients(recp, NULL, 0);
4020 if (valid_to->num_error > 0) {
4021 cprintf("%d %s\n", ERROR + NO_SUCH_USER, valid_to->errormsg);
4022 free_recipients(valid_to);
4026 valid_cc = validate_recipients(cc, NULL, 0);
4027 if (valid_cc->num_error > 0) {
4028 cprintf("%d %s\n", ERROR + NO_SUCH_USER, valid_cc->errormsg);
4029 free_recipients(valid_to);
4030 free_recipients(valid_cc);
4034 valid_bcc = validate_recipients(bcc, NULL, 0);
4035 if (valid_bcc->num_error > 0) {
4036 cprintf("%d %s\n", ERROR + NO_SUCH_USER, valid_bcc->errormsg);
4037 free_recipients(valid_to);
4038 free_recipients(valid_cc);
4039 free_recipients(valid_bcc);
4043 /* Recipient required, but none were specified */
4044 if ( (valid_to->num_error < 0) && (valid_cc->num_error < 0) && (valid_bcc->num_error < 0) ) {
4045 free_recipients(valid_to);
4046 free_recipients(valid_cc);
4047 free_recipients(valid_bcc);
4048 cprintf("%d At least one recipient is required.\n", ERROR + NO_SUCH_USER);
4052 if (valid_to->num_internet + valid_cc->num_internet + valid_bcc->num_internet > 0) {
4053 if (CtdlCheckInternetMailPermission(&CCC->user)==0) {
4054 cprintf("%d You do not have permission "
4055 "to send Internet mail.\n",
4056 ERROR + HIGHER_ACCESS_REQUIRED);
4057 free_recipients(valid_to);
4058 free_recipients(valid_cc);
4059 free_recipients(valid_bcc);
4064 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)
4065 && (CCC->user.axlevel < AxNetU) ) {
4066 cprintf("%d Higher access required for network mail.\n",
4067 ERROR + HIGHER_ACCESS_REQUIRED);
4068 free_recipients(valid_to);
4069 free_recipients(valid_cc);
4070 free_recipients(valid_bcc);
4074 if ((RESTRICT_INTERNET == 1)
4075 && (valid_to->num_internet + valid_cc->num_internet + valid_bcc->num_internet > 0)
4076 && ((CCC->user.flags & US_INTERNET) == 0)
4077 && (!CCC->internal_pgm)) {
4078 cprintf("%d You don't have access to Internet mail.\n",
4079 ERROR + HIGHER_ACCESS_REQUIRED);
4080 free_recipients(valid_to);
4081 free_recipients(valid_cc);
4082 free_recipients(valid_bcc);
4088 /* Is this a room which has anonymous-only or anonymous-option? */
4089 anonymous = MES_NORMAL;
4090 if (CCC->room.QRflags & QR_ANONONLY) {
4091 anonymous = MES_ANONONLY;
4093 if (CCC->room.QRflags & QR_ANONOPT) {
4094 if (anon_flag == 1) { /* only if the user requested it */
4095 anonymous = MES_ANONOPT;
4099 if ((CCC->room.QRflags & QR_MAILBOX) == 0) {
4103 /* Recommend to the client that the use of a message subject is
4104 * strongly recommended in this room, if either the SUBJECTREQ flag
4105 * is set, or if there is one or more Internet email recipients.
4107 if (CCC->room.QRflags2 & QR2_SUBJECTREQ) subject_required = 1;
4108 if ((valid_to) && (valid_to->num_internet > 0)) subject_required = 1;
4109 if ((valid_cc) && (valid_cc->num_internet > 0)) subject_required = 1;
4110 if ((valid_bcc) && (valid_bcc->num_internet > 0)) subject_required = 1;
4112 /* If we're only checking the validity of the request, return
4113 * success without creating the message.
4116 cprintf("%d %s|%d\n", CIT_OK,
4117 ((valid_to != NULL) ? valid_to->display_recp : ""),
4119 free_recipients(valid_to);
4120 free_recipients(valid_cc);
4121 free_recipients(valid_bcc);
4125 /* We don't need these anymore because we'll do it differently below */
4126 free_recipients(valid_to);
4127 free_recipients(valid_cc);
4128 free_recipients(valid_bcc);
4130 /* Read in the message from the client. */
4132 cprintf("%d send message\n", START_CHAT_MODE);
4134 cprintf("%d send message\n", SEND_LISTING);
4137 msg = CtdlMakeMessage(&CCC->user, recp, cc,
4138 CCC->room.QRname, anonymous, format_type,
4139 newusername, newuseremail, subject,
4140 ((!IsEmptyStr(supplied_euid)) ? supplied_euid : NULL),
4143 /* Put together one big recipients struct containing to/cc/bcc all in
4144 * one. This is for the envelope.
4146 char *all_recps = malloc(SIZ * 3);
4147 strcpy(all_recps, recp);
4148 if (!IsEmptyStr(cc)) {
4149 if (!IsEmptyStr(all_recps)) {
4150 strcat(all_recps, ",");
4152 strcat(all_recps, cc);
4154 if (!IsEmptyStr(bcc)) {
4155 if (!IsEmptyStr(all_recps)) {
4156 strcat(all_recps, ",");
4158 strcat(all_recps, bcc);
4160 if (!IsEmptyStr(all_recps)) {
4161 valid = validate_recipients(all_recps, NULL, 0);
4168 if ((valid != NULL) && (valid->num_room == 1))
4170 /* posting into an ML room? set the envelope from
4171 * to the actual mail address so others get a valid
4174 msg->cm_fields[eenVelopeTo] = strdup(valid->recp_orgroom);
4178 msgnum = CtdlSubmitMsg(msg, valid, "", QP_EADDR);
4180 cprintf("%ld\n", msgnum);
4182 if (StrLength(CCC->StatusMessage) > 0) {
4183 cprintf("%s\n", ChrPtr(CCC->StatusMessage));
4185 else if (msgnum >= 0L) {
4186 client_write(HKEY("Message accepted.\n"));
4189 client_write(HKEY("Internal error.\n"));
4192 if (!CM_IsEmpty(msg, eExclusiveID)) {
4193 cprintf("%s\n", msg->cm_fields[eExclusiveID]);
4202 if (valid != NULL) {
4203 free_recipients(valid);
4211 * API function to delete messages which match a set of criteria
4212 * (returns the actual number of messages deleted)
4214 int CtdlDeleteMessages(char *room_name, /* which room */
4215 long *dmsgnums, /* array of msg numbers to be deleted */
4216 int num_dmsgnums, /* number of msgs to be deleted, or 0 for "any" */
4217 char *content_type /* or "" for any. regular expressions expected. */
4220 struct CitContext *CCC = CC;
4221 struct ctdlroom qrbuf;
4222 struct cdbdata *cdbfr;
4223 long *msglist = NULL;
4224 long *dellist = NULL;
4227 int num_deleted = 0;
4229 struct MetaData smi;
4232 int need_to_free_re = 0;
4234 if (content_type) if (!IsEmptyStr(content_type)) {
4235 regcomp(&re, content_type, 0);
4236 need_to_free_re = 1;
4238 MSG_syslog(LOG_DEBUG, " CtdlDeleteMessages(%s, %d msgs, %s)\n",
4239 room_name, num_dmsgnums, content_type);
4241 /* get room record, obtaining a lock... */
4242 if (CtdlGetRoomLock(&qrbuf, room_name) != 0) {
4243 MSG_syslog(LOG_ERR, " CtdlDeleteMessages(): Room <%s> not found\n",
4245 if (need_to_free_re) regfree(&re);
4246 return (0); /* room not found */
4248 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
4250 if (cdbfr != NULL) {
4251 dellist = malloc(cdbfr->len);
4252 msglist = (long *) cdbfr->ptr;
4253 cdbfr->ptr = NULL; /* CtdlDeleteMessages() now owns this memory */
4254 num_msgs = cdbfr->len / sizeof(long);
4258 int have_contenttype = (content_type != NULL) && !IsEmptyStr(content_type);
4259 int have_delmsgs = (num_dmsgnums == 0) || (dmsgnums == NULL);
4260 int have_more_del = 1;
4262 num_msgs = sort_msglist(msglist, num_msgs);
4263 if (num_dmsgnums > 1)
4264 num_dmsgnums = sort_msglist(dmsgnums, num_dmsgnums);
4267 StrBuf *dbg = NewStrBuf();
4268 for (i = 0; i < num_dmsgnums; i++)
4269 StrBufAppendPrintf(dbg, ", %ld", dmsgnums[i]);
4270 MSG_syslog(LOG_DEBUG, " Deleting before: %s", ChrPtr(dbg));
4275 while ((i < num_msgs) && (have_more_del)) {
4278 /* Set/clear a bit for each criterion */
4280 /* 0 messages in the list or a null list means that we are
4281 * interested in deleting any messages which meet the other criteria.
4284 delete_this |= 0x01;
4287 while ((i < num_msgs) && (msglist[i] < dmsgnums[j])) i++;
4292 if (msglist[i] == dmsgnums[j]) {
4293 delete_this |= 0x01;
4296 have_more_del = (j < num_dmsgnums);
4299 if (have_contenttype) {
4300 GetMetaData(&smi, msglist[i]);
4301 if (regexec(&re, smi.meta_content_type, 1, &pm, 0) == 0) {
4302 delete_this |= 0x02;
4305 delete_this |= 0x02;
4308 /* Delete message only if all bits are set */
4309 if (delete_this == 0x03) {
4310 dellist[num_deleted++] = msglist[i];
4317 StrBuf *dbg = NewStrBuf();
4318 for (i = 0; i < num_deleted; i++)
4319 StrBufAppendPrintf(dbg, ", %ld", dellist[i]);
4320 MSG_syslog(LOG_DEBUG, " Deleting: %s", ChrPtr(dbg));
4324 num_msgs = sort_msglist(msglist, num_msgs);
4325 cdb_store(CDB_MSGLISTS, &qrbuf.QRnumber, (int)sizeof(long),
4326 msglist, (int)(num_msgs * sizeof(long)));
4329 qrbuf.QRhighest = msglist[num_msgs - 1];
4331 qrbuf.QRhighest = 0;
4333 CtdlPutRoomLock(&qrbuf);
4335 /* Go through the messages we pulled out of the index, and decrement
4336 * their reference counts by 1. If this is the only room the message
4337 * was in, the reference count will reach zero and the message will
4338 * automatically be deleted from the database. We do this in a
4339 * separate pass because there might be plug-in hooks getting called,
4340 * and we don't want that happening during an S_ROOMS critical
4344 for (i=0; i<num_deleted; ++i) {
4345 PerformDeleteHooks(qrbuf.QRname, dellist[i]);
4347 AdjRefCountList(dellist, num_deleted, -1);
4349 /* Now free the memory we used, and go away. */
4350 if (msglist != NULL) free(msglist);
4351 if (dellist != NULL) free(dellist);
4352 MSG_syslog(LOG_DEBUG, " %d message(s) deleted.\n", num_deleted);
4353 if (need_to_free_re) regfree(&re);
4354 return (num_deleted);
4358 * Delete message from current room
4360 void cmd_dele(char *args)
4369 extract_token(msgset, args, 0, '|', sizeof msgset);
4370 num_msgs = num_tokens(msgset, ',');
4372 cprintf("%d Nothing to do.\n", CIT_OK);
4376 if (CtdlDoIHavePermissionToDeleteMessagesFromThisRoom() == 0) {
4377 cprintf("%d Higher access required.\n",
4378 ERROR + HIGHER_ACCESS_REQUIRED);
4383 * Build our message set to be moved/copied
4385 msgs = malloc(num_msgs * sizeof(long));
4386 for (i=0; i<num_msgs; ++i) {
4387 extract_token(msgtok, msgset, i, ',', sizeof msgtok);
4388 msgs[i] = atol(msgtok);
4391 num_deleted = CtdlDeleteMessages(CC->room.QRname, msgs, num_msgs, "");
4395 cprintf("%d %d message%s deleted.\n", CIT_OK,
4396 num_deleted, ((num_deleted != 1) ? "s" : ""));
4398 cprintf("%d Message not found.\n", ERROR + MESSAGE_NOT_FOUND);
4406 * move or copy a message to another room
4408 void cmd_move(char *args)
4415 char targ[ROOMNAMELEN];
4416 struct ctdlroom qtemp;
4423 extract_token(msgset, args, 0, '|', sizeof msgset);
4424 num_msgs = num_tokens(msgset, ',');
4426 cprintf("%d Nothing to do.\n", CIT_OK);
4430 extract_token(targ, args, 1, '|', sizeof targ);
4431 convert_room_name_macros(targ, sizeof targ);
4432 targ[ROOMNAMELEN - 1] = 0;
4433 is_copy = extract_int(args, 2);
4435 if (CtdlGetRoom(&qtemp, targ) != 0) {
4436 cprintf("%d '%s' does not exist.\n", ERROR + ROOM_NOT_FOUND, targ);
4440 if (!strcasecmp(qtemp.QRname, CC->room.QRname)) {
4441 cprintf("%d Source and target rooms are the same.\n", ERROR + ALREADY_EXISTS);
4445 CtdlGetUser(&CC->user, CC->curr_user);
4446 CtdlRoomAccess(&qtemp, &CC->user, &ra, NULL);
4448 /* Check for permission to perform this operation.
4449 * Remember: "CC->room" is source, "qtemp" is target.
4453 /* Admins can move/copy */
4454 if (CC->user.axlevel >= AxAideU) permit = 1;
4456 /* Room aides can move/copy */
4457 if (CC->user.usernum == CC->room.QRroomaide) permit = 1;
4459 /* Permit move/copy from personal rooms */
4460 if ((CC->room.QRflags & QR_MAILBOX)
4461 && (qtemp.QRflags & QR_MAILBOX)) permit = 1;
4463 /* Permit only copy from public to personal room */
4465 && (!(CC->room.QRflags & QR_MAILBOX))
4466 && (qtemp.QRflags & QR_MAILBOX)) permit = 1;
4468 /* Permit message removal from collaborative delete rooms */
4469 if (CC->room.QRflags2 & QR2_COLLABDEL) permit = 1;
4471 /* Users allowed to post into the target room may move into it too. */
4472 if ((CC->room.QRflags & QR_MAILBOX) &&
4473 (qtemp.QRflags & UA_POSTALLOWED)) permit = 1;
4475 /* User must have access to target room */
4476 if (!(ra & UA_KNOWN)) permit = 0;
4479 cprintf("%d Higher access required.\n",
4480 ERROR + HIGHER_ACCESS_REQUIRED);
4485 * Build our message set to be moved/copied
4487 msgs = malloc(num_msgs * sizeof(long));
4488 for (i=0; i<num_msgs; ++i) {
4489 extract_token(msgtok, msgset, i, ',', sizeof msgtok);
4490 msgs[i] = atol(msgtok);
4496 err = CtdlSaveMsgPointersInRoom(targ, msgs, num_msgs, 1, NULL, 0);
4498 cprintf("%d Cannot store message(s) in %s: error %d\n",
4504 /* Now delete the message from the source room,
4505 * if this is a 'move' rather than a 'copy' operation.
4508 CtdlDeleteMessages(CC->room.QRname, msgs, num_msgs, "");
4512 cprintf("%d Message(s) %s.\n", CIT_OK, (is_copy ? "copied" : "moved") );
4518 * GetMetaData() - Get the supplementary record for a message
4520 void GetMetaData(struct MetaData *smibuf, long msgnum)
4523 struct cdbdata *cdbsmi;
4526 memset(smibuf, 0, sizeof(struct MetaData));
4527 smibuf->meta_msgnum = msgnum;
4528 smibuf->meta_refcount = 1; /* Default reference count is 1 */
4530 /* Use the negative of the message number for its supp record index */
4531 TheIndex = (0L - msgnum);
4533 cdbsmi = cdb_fetch(CDB_MSGMAIN, &TheIndex, sizeof(long));
4534 if (cdbsmi == NULL) {
4535 return; /* record not found; go with defaults */
4537 memcpy(smibuf, cdbsmi->ptr,
4538 ((cdbsmi->len > sizeof(struct MetaData)) ?
4539 sizeof(struct MetaData) : cdbsmi->len));
4546 * PutMetaData() - (re)write supplementary record for a message
4548 void PutMetaData(struct MetaData *smibuf)
4552 /* Use the negative of the message number for the metadata db index */
4553 TheIndex = (0L - smibuf->meta_msgnum);
4555 cdb_store(CDB_MSGMAIN,
4556 &TheIndex, (int)sizeof(long),
4557 smibuf, (int)sizeof(struct MetaData));
4562 * AdjRefCount - submit an adjustment to the reference count for a message.
4563 * (These are just queued -- we actually process them later.)
4565 void AdjRefCount(long msgnum, int incr)
4567 struct CitContext *CCC = CC;
4568 struct arcq new_arcq;
4571 MSG_syslog(LOG_DEBUG, "AdjRefCount() msg %ld ref count delta %+d\n", msgnum, incr);
4573 begin_critical_section(S_SUPPMSGMAIN);
4574 if (arcfp == NULL) {
4575 arcfp = fopen(file_arcq, "ab+");
4576 chown(file_arcq, CTDLUID, (-1));
4577 chmod(file_arcq, 0600);
4579 end_critical_section(S_SUPPMSGMAIN);
4581 /* msgnum < 0 means that we're trying to close the file */
4583 MSGM_syslog(LOG_DEBUG, "Closing the AdjRefCount queue file\n");
4584 begin_critical_section(S_SUPPMSGMAIN);
4585 if (arcfp != NULL) {
4589 end_critical_section(S_SUPPMSGMAIN);
4594 * If we can't open the queue, perform the operation synchronously.
4596 if (arcfp == NULL) {
4597 TDAP_AdjRefCount(msgnum, incr);
4601 new_arcq.arcq_msgnum = msgnum;
4602 new_arcq.arcq_delta = incr;
4603 rv = fwrite(&new_arcq, sizeof(struct arcq), 1, arcfp);
4605 MSG_syslog(LOG_EMERG, "Couldn't write Refcount Queue File %s: %s\n",
4614 void AdjRefCountList(long *msgnum, long nmsg, int incr)
4616 struct CitContext *CCC = CC;
4617 long i, the_size, offset;
4618 struct arcq *new_arcq;
4621 MSG_syslog(LOG_DEBUG, "AdjRefCountList() msg %ld ref count delta %+d\n", nmsg, incr);
4623 begin_critical_section(S_SUPPMSGMAIN);
4624 if (arcfp == NULL) {
4625 arcfp = fopen(file_arcq, "ab+");
4626 chown(file_arcq, CTDLUID, (-1));
4627 chmod(file_arcq, 0600);
4629 end_critical_section(S_SUPPMSGMAIN);
4632 * If we can't open the queue, perform the operation synchronously.
4634 if (arcfp == NULL) {
4635 for (i = 0; i < nmsg; i++)
4636 TDAP_AdjRefCount(msgnum[i], incr);
4640 the_size = sizeof(struct arcq) * nmsg;
4641 new_arcq = malloc(the_size);
4642 for (i = 0; i < nmsg; i++) {
4643 new_arcq[i].arcq_msgnum = msgnum[i];
4644 new_arcq[i].arcq_delta = incr;
4648 while ((rv >= 0) && (offset < the_size))
4650 rv = fwrite(new_arcq + offset, 1, the_size - offset, arcfp);
4652 MSG_syslog(LOG_EMERG, "Couldn't write Refcount Queue File %s: %s\n",
4668 * TDAP_ProcessAdjRefCountQueue()
4670 * Process the queue of message count adjustments that was created by calls
4671 * to AdjRefCount() ... by reading the queue and calling TDAP_AdjRefCount()
4672 * for each one. This should be an "off hours" operation.
4674 int TDAP_ProcessAdjRefCountQueue(void)
4676 struct CitContext *CCC = CC;
4677 char file_arcq_temp[PATH_MAX];
4680 struct arcq arcq_rec;
4681 int num_records_processed = 0;
4683 snprintf(file_arcq_temp, sizeof file_arcq_temp, "%s.%04x", file_arcq, rand());
4685 begin_critical_section(S_SUPPMSGMAIN);
4686 if (arcfp != NULL) {
4691 r = link(file_arcq, file_arcq_temp);
4693 MSG_syslog(LOG_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
4694 end_critical_section(S_SUPPMSGMAIN);
4695 return(num_records_processed);
4699 end_critical_section(S_SUPPMSGMAIN);
4701 fp = fopen(file_arcq_temp, "rb");
4703 MSG_syslog(LOG_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
4704 return(num_records_processed);
4707 while (fread(&arcq_rec, sizeof(struct arcq), 1, fp) == 1) {
4708 TDAP_AdjRefCount(arcq_rec.arcq_msgnum, arcq_rec.arcq_delta);
4709 ++num_records_processed;
4713 r = unlink(file_arcq_temp);
4715 MSG_syslog(LOG_CRIT, "%s: %s\n", file_arcq_temp, strerror(errno));
4718 return(num_records_processed);
4724 * TDAP_AdjRefCount - adjust the reference count for a message.
4725 * This one does it "for real" because it's called by
4726 * the autopurger function that processes the queue
4727 * created by AdjRefCount(). If a message's reference
4728 * count becomes zero, we also delete the message from
4729 * disk and de-index it.
4731 void TDAP_AdjRefCount(long msgnum, int incr)
4733 struct CitContext *CCC = CC;
4735 struct MetaData smi;
4738 /* This is a *tight* critical section; please keep it that way, as
4739 * it may get called while nested in other critical sections.
4740 * Complicating this any further will surely cause deadlock!
4742 begin_critical_section(S_SUPPMSGMAIN);
4743 GetMetaData(&smi, msgnum);
4744 smi.meta_refcount += incr;
4746 end_critical_section(S_SUPPMSGMAIN);
4747 MSG_syslog(LOG_DEBUG, "TDAP_AdjRefCount() msg %ld ref count delta %+d, is now %d\n",
4748 msgnum, incr, smi.meta_refcount
4751 /* If the reference count is now zero, delete the message
4752 * (and its supplementary record as well).
4754 if (smi.meta_refcount == 0) {
4755 MSG_syslog(LOG_DEBUG, "Deleting message <%ld>\n", msgnum);
4757 /* Call delete hooks with NULL room to show it has gone altogether */
4758 PerformDeleteHooks(NULL, msgnum);
4760 /* Remove from message base */
4762 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
4763 cdb_delete(CDB_BIGMSGS, &delnum, (int)sizeof(long));
4765 /* Remove metadata record */
4766 delnum = (0L - msgnum);
4767 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
4773 * Write a generic object to this room
4775 * Note: this could be much more efficient. Right now we use two temporary
4776 * files, and still pull the message into memory as with all others.
4778 void CtdlWriteObject(char *req_room, /* Room to stuff it in */
4779 char *content_type, /* MIME type of this object */
4780 char *raw_message, /* Data to be written */
4781 off_t raw_length, /* Size of raw_message */
4782 struct ctdluser *is_mailbox, /* Mailbox room? */
4783 int is_binary, /* Is encoding necessary? */
4784 int is_unique, /* Del others of this type? */
4785 unsigned int flags /* Internal save flags */
4788 struct CitContext *CCC = CC;
4789 struct ctdlroom qrbuf;
4790 char roomname[ROOMNAMELEN];
4791 struct CtdlMessage *msg;
4792 char *encoded_message = NULL;
4794 if (is_mailbox != NULL) {
4795 CtdlMailboxName(roomname, sizeof roomname, is_mailbox, req_room);
4798 safestrncpy(roomname, req_room, sizeof(roomname));
4801 MSG_syslog(LOG_DEBUG, "Raw length is %ld\n", (long)raw_length);
4804 encoded_message = malloc((size_t) (((raw_length * 134) / 100) + 4096 ) );
4807 encoded_message = malloc((size_t)(raw_length + 4096));
4810 sprintf(encoded_message, "Content-type: %s\n", content_type);
4813 sprintf(&encoded_message[strlen(encoded_message)],
4814 "Content-transfer-encoding: base64\n\n"
4818 sprintf(&encoded_message[strlen(encoded_message)],
4819 "Content-transfer-encoding: 7bit\n\n"
4825 &encoded_message[strlen(encoded_message)],
4833 &encoded_message[strlen(encoded_message)],
4839 MSGM_syslog(LOG_DEBUG, "Allocating\n");
4840 msg = malloc(sizeof(struct CtdlMessage));
4841 memset(msg, 0, sizeof(struct CtdlMessage));
4842 msg->cm_magic = CTDLMESSAGE_MAGIC;
4843 msg->cm_anon_type = MES_NORMAL;
4844 msg->cm_format_type = 4;
4845 msg->cm_fields[eAuthor] = strdup(CCC->user.fullname);
4846 msg->cm_fields[eOriginalRoom] = strdup(req_room);
4847 msg->cm_fields[eNodeName] = strdup(config.c_nodename);
4848 msg->cm_fields[eHumanNode] = strdup(config.c_humannode);
4849 msg->cm_flags = flags;
4851 msg->cm_fields[eMesageText] = encoded_message;
4853 /* Create the requested room if we have to. */
4854 if (CtdlGetRoom(&qrbuf, roomname) != 0) {
4855 CtdlCreateRoom(roomname,
4856 ( (is_mailbox != NULL) ? 5 : 3 ),
4857 "", 0, 1, 0, VIEW_BBS);
4859 /* If the caller specified this object as unique, delete all
4860 * other objects of this type that are currently in the room.
4863 MSG_syslog(LOG_DEBUG, "Deleted %d other msgs of this type\n",
4864 CtdlDeleteMessages(roomname, NULL, 0, content_type)
4867 /* Now write the data */
4868 CtdlSubmitMsg(msg, NULL, roomname, 0);
4874 /*****************************************************************************/
4875 /* MODULE INITIALIZATION STUFF */
4876 /*****************************************************************************/
4877 void SetMessageDebugEnabled(const int n)
4879 MessageDebugEnabled = n;
4881 CTDL_MODULE_INIT(msgbase)
4884 CtdlRegisterDebugFlagHook(HKEY("messages"), SetMessageDebugEnabled, &MessageDebugEnabled);
4886 CtdlRegisterProtoHook(cmd_msgs, "MSGS", "Output a list of messages in the current room");
4887 CtdlRegisterProtoHook(cmd_msg0, "MSG0", "Output a message in plain text format");
4888 CtdlRegisterProtoHook(cmd_msg2, "MSG2", "Output a message in RFC822 format");
4889 CtdlRegisterProtoHook(cmd_msg3, "MSG3", "Output a message in raw format (deprecated)");
4890 CtdlRegisterProtoHook(cmd_msg4, "MSG4", "Output a message in the client's preferred format");
4891 CtdlRegisterProtoHook(cmd_msgp, "MSGP", "Select preferred format for MSG4 output");
4892 CtdlRegisterProtoHook(cmd_opna, "OPNA", "Open an attachment for download");
4893 CtdlRegisterProtoHook(cmd_dlat, "DLAT", "Download an attachment");
4894 CtdlRegisterProtoHook(cmd_ent0, "ENT0", "Enter a message");
4895 CtdlRegisterProtoHook(cmd_dele, "DELE", "Delete a message");
4896 CtdlRegisterProtoHook(cmd_move, "MOVE", "Move or copy a message to another room");
4899 /* return our Subversion id for the Log */