4 * Implements the message store.
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
23 # include <sys/time.h>
38 #include "serv_extensions.h"
42 #include "sysdep_decls.h"
43 #include "citserver.h"
50 #include "mime_parser.h"
53 #include "internet_addressing.h"
54 #include "serv_fulltext.h"
56 #include "euidindex.h"
59 struct addresses_to_be_filed *atbf = NULL;
62 * This really belongs in serv_network.c, but I don't know how to export
63 * symbols between modules.
65 struct FilterList *filterlist = NULL;
69 * These are the four-character field headers we use when outputting
70 * messages in Citadel format (as opposed to RFC822 format).
73 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
74 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
75 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
76 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
77 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
78 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
79 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
80 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
107 * This function is self explanatory.
108 * (What can I say, I'm in a weird mood today...)
110 void remove_any_whitespace_to_the_left_or_right_of_at_symbol(char *name)
114 for (i = 0; i < strlen(name); ++i) {
115 if (name[i] == '@') {
116 while (isspace(name[i - 1]) && i > 0) {
117 strcpy(&name[i - 1], &name[i]);
120 while (isspace(name[i + 1])) {
121 strcpy(&name[i + 1], &name[i + 2]);
129 * Aliasing for network mail.
130 * (Error messages have been commented out, because this is a server.)
132 int alias(char *name)
133 { /* process alias and routing info for mail */
136 char aaa[SIZ], bbb[SIZ];
137 char *ignetcfg = NULL;
138 char *ignetmap = NULL;
145 remove_any_whitespace_to_the_left_or_right_of_at_symbol(name);
153 "mail.aliases", "r");
155 fp = fopen("/dev/null", "r");
162 while (fgets(aaa, sizeof aaa, fp) != NULL) {
163 while (isspace(name[0]))
164 strcpy(name, &name[1]);
165 aaa[strlen(aaa) - 1] = 0;
167 for (a = 0; a < strlen(aaa); ++a) {
169 strcpy(bbb, &aaa[a + 1]);
173 if (!strcasecmp(name, aaa))
178 /* Hit the Global Address Book */
179 if (CtdlDirectoryLookup(aaa, name, sizeof aaa) == 0) {
183 lprintf(CTDL_INFO, "Mail is being forwarded to %s\n", name);
185 /* Change "user @ xxx" to "user" if xxx is an alias for this host */
186 for (a=0; a<strlen(name); ++a) {
187 if (name[a] == '@') {
188 if (CtdlHostAlias(&name[a+1]) == hostalias_localhost) {
190 lprintf(CTDL_INFO, "Changed to <%s>\n", name);
195 /* determine local or remote type, see citadel.h */
196 at = haschar(name, '@');
197 if (at == 0) return(MES_LOCAL); /* no @'s - local address */
198 if (at > 1) return(MES_ERROR); /* >1 @'s - invalid address */
199 remove_any_whitespace_to_the_left_or_right_of_at_symbol(name);
201 /* figure out the delivery mode */
202 extract_token(node, name, 1, '@', sizeof node);
204 /* If there are one or more dots in the nodename, we assume that it
205 * is an FQDN and will attempt SMTP delivery to the Internet.
207 if (haschar(node, '.') > 0) {
208 return(MES_INTERNET);
211 /* Otherwise we look in the IGnet maps for a valid Citadel node.
212 * Try directly-connected nodes first...
214 ignetcfg = CtdlGetSysConfig(IGNETCFG);
215 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
216 extract_token(buf, ignetcfg, i, '\n', sizeof buf);
217 extract_token(testnode, buf, 0, '|', sizeof testnode);
218 if (!strcasecmp(node, testnode)) {
226 * Then try nodes that are two or more hops away.
228 ignetmap = CtdlGetSysConfig(IGNETMAP);
229 for (i=0; i<num_tokens(ignetmap, '\n'); ++i) {
230 extract_token(buf, ignetmap, i, '\n', sizeof buf);
231 extract_token(testnode, buf, 0, '|', sizeof testnode);
232 if (!strcasecmp(node, testnode)) {
239 /* If we get to this point it's an invalid node name */
254 "/citadel.control", "r");
256 lprintf(CTDL_CRIT, "Cannot open citadel.control: %s\n",
260 fread((char *) &CitControl, sizeof(struct CitControl), 1, fp);
266 * Back end for the MSGS command: output message number only.
268 void simple_listing(long msgnum, void *userdata)
270 cprintf("%ld\n", msgnum);
276 * Back end for the MSGS command: output header summary.
278 void headers_listing(long msgnum, void *userdata)
280 struct CtdlMessage *msg;
282 msg = CtdlFetchMessage(msgnum, 0);
284 cprintf("%ld|0|||||\n", msgnum);
288 cprintf("%ld|%s|%s|%s|%s|%s|\n",
290 (msg->cm_fields['T'] ? msg->cm_fields['T'] : "0"),
291 (msg->cm_fields['A'] ? msg->cm_fields['A'] : ""),
292 (msg->cm_fields['N'] ? msg->cm_fields['N'] : ""),
293 (msg->cm_fields['F'] ? msg->cm_fields['F'] : ""),
294 (msg->cm_fields['U'] ? msg->cm_fields['U'] : "")
296 CtdlFreeMessage(msg);
301 /* Determine if a given message matches the fields in a message template.
302 * Return 0 for a successful match.
304 int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template) {
307 /* If there aren't any fields in the template, all messages will
310 if (template == NULL) return(0);
312 /* Null messages are bogus. */
313 if (msg == NULL) return(1);
315 for (i='A'; i<='Z'; ++i) {
316 if (template->cm_fields[i] != NULL) {
317 if (msg->cm_fields[i] == NULL) {
320 if (strcasecmp(msg->cm_fields[i],
321 template->cm_fields[i])) return 1;
325 /* All compares succeeded: we have a match! */
332 * Retrieve the "seen" message list for the current room.
334 void CtdlGetSeen(char *buf, int which_set) {
337 /* Learn about the user and room in question */
338 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
340 if (which_set == ctdlsetseen_seen)
341 safestrncpy(buf, vbuf.v_seen, SIZ);
342 if (which_set == ctdlsetseen_answered)
343 safestrncpy(buf, vbuf.v_answered, SIZ);
349 * Manipulate the "seen msgs" string (or other message set strings)
351 void CtdlSetSeen(long *target_msgnums, int num_target_msgnums,
352 int target_setting, int which_set,
353 struct ctdluser *which_user, struct ctdlroom *which_room) {
354 struct cdbdata *cdbfr;
366 char *is_set; /* actually an array of booleans */
369 char setstr[SIZ], lostr[SIZ], histr[SIZ];
372 lprintf(CTDL_DEBUG, "CtdlSetSeen(%d msgs starting with %ld, %d, %d)\n",
373 num_target_msgnums, target_msgnums[0],
374 target_setting, which_set);
376 /* Learn about the user and room in question */
377 CtdlGetRelationship(&vbuf,
378 ((which_user != NULL) ? which_user : &CC->user),
379 ((which_room != NULL) ? which_room : &CC->room)
382 /* Load the message list */
383 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
385 msglist = malloc(cdbfr->len);
386 memcpy(msglist, cdbfr->ptr, cdbfr->len);
387 num_msgs = cdbfr->len / sizeof(long);
390 return; /* No messages at all? No further action. */
393 is_set = malloc(num_msgs * sizeof(char));
394 memset(is_set, 0, (num_msgs * sizeof(char)) );
396 /* Decide which message set we're manipulating */
398 case ctdlsetseen_seen:
399 safestrncpy(vset, vbuf.v_seen, sizeof vset);
401 case ctdlsetseen_answered:
402 safestrncpy(vset, vbuf.v_answered, sizeof vset);
406 /* lprintf(CTDL_DEBUG, "before optimize: %s\n", vset); */
408 /* Translate the existing sequence set into an array of booleans */
409 num_sets = num_tokens(vset, ',');
410 for (s=0; s<num_sets; ++s) {
411 extract_token(setstr, vset, s, ',', sizeof setstr);
413 extract_token(lostr, setstr, 0, ':', sizeof lostr);
414 if (num_tokens(setstr, ':') >= 2) {
415 extract_token(histr, setstr, 1, ':', sizeof histr);
416 if (!strcmp(histr, "*")) {
417 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
421 strcpy(histr, lostr);
426 for (i = 0; i < num_msgs; ++i) {
427 if ((msglist[i] >= lo) && (msglist[i] <= hi)) {
433 /* Now translate the array of booleans back into a sequence set */
438 for (i=0; i<num_msgs; ++i) {
440 is_seen = is_set[i]; /* Default to existing setting */
442 for (k=0; k<num_target_msgnums; ++k) {
443 if (msglist[i] == target_msgnums[k]) {
444 is_seen = target_setting;
449 if (lo < 0L) lo = msglist[i];
453 if ( ((is_seen == 0) && (was_seen == 1))
454 || ((is_seen == 1) && (i == num_msgs-1)) ) {
456 /* begin trim-o-matic code */
459 while ( (strlen(vset) + 20) > sizeof vset) {
460 remove_token(vset, 0, ',');
462 if (j--) break; /* loop no more than 9 times */
464 if ( (trimming) && (which_set == ctdlsetseen_seen) ) {
468 snprintf(lostr, sizeof lostr,
469 "1:%ld,%s", t, vset);
470 safestrncpy(vset, lostr, sizeof vset);
472 /* end trim-o-matic code */
480 snprintf(&vset[tmp], (sizeof vset) - tmp,
484 snprintf(&vset[tmp], (sizeof vset) - tmp,
493 /* Decide which message set we're manipulating */
495 case ctdlsetseen_seen:
496 safestrncpy(vbuf.v_seen, vset, sizeof vbuf.v_seen);
498 case ctdlsetseen_answered:
499 safestrncpy(vbuf.v_answered, vset,
500 sizeof vbuf.v_answered);
505 /* lprintf(CTDL_DEBUG, " after optimize: %s\n", vset); */
507 CtdlSetRelationship(&vbuf,
508 ((which_user != NULL) ? which_user : &CC->user),
509 ((which_room != NULL) ? which_room : &CC->room)
515 * API function to perform an operation for each qualifying message in the
516 * current room. (Returns the number of messages processed.)
518 int CtdlForEachMessage(int mode, long ref,
520 struct CtdlMessage *compare,
521 void (*CallBack) (long, void *),
527 struct cdbdata *cdbfr;
528 long *msglist = NULL;
530 int num_processed = 0;
533 struct CtdlMessage *msg;
536 int printed_lastold = 0;
538 /* Learn about the user and room in question */
540 getuser(&CC->user, CC->curr_user);
541 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
543 /* Load the message list */
544 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
546 msglist = malloc(cdbfr->len);
547 memcpy(msglist, cdbfr->ptr, cdbfr->len);
548 num_msgs = cdbfr->len / sizeof(long);
551 return 0; /* No messages at all? No further action. */
556 * Now begin the traversal.
558 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
560 /* If the caller is looking for a specific MIME type, filter
561 * out all messages which are not of the type requested.
563 if (content_type != NULL) if (strlen(content_type) > 0) {
565 /* This call to GetMetaData() sits inside this loop
566 * so that we only do the extra database read per msg
567 * if we need to. Doing the extra read all the time
568 * really kills the server. If we ever need to use
569 * metadata for another search criterion, we need to
570 * move the read somewhere else -- but still be smart
571 * enough to only do the read if the caller has
572 * specified something that will need it.
574 GetMetaData(&smi, msglist[a]);
576 if (strcasecmp(smi.meta_content_type, content_type)) {
582 num_msgs = sort_msglist(msglist, num_msgs);
584 /* If a template was supplied, filter out the messages which
585 * don't match. (This could induce some delays!)
588 if (compare != NULL) {
589 for (a = 0; a < num_msgs; ++a) {
590 msg = CtdlFetchMessage(msglist[a], 1);
592 if (CtdlMsgCmp(msg, compare)) {
595 CtdlFreeMessage(msg);
603 * Now iterate through the message list, according to the
604 * criteria supplied by the caller.
607 for (a = 0; a < num_msgs; ++a) {
608 thismsg = msglist[a];
609 if (mode == MSGS_ALL) {
613 is_seen = is_msg_in_sequence_set(
614 vbuf.v_seen, thismsg);
615 if (is_seen) lastold = thismsg;
621 || ((mode == MSGS_OLD) && (is_seen))
622 || ((mode == MSGS_NEW) && (!is_seen))
623 || ((mode == MSGS_LAST) && (a >= (num_msgs - ref)))
624 || ((mode == MSGS_FIRST) && (a < ref))
625 || ((mode == MSGS_GT) && (thismsg > ref))
626 || ((mode == MSGS_EQ) && (thismsg == ref))
629 if ((mode == MSGS_NEW) && (CC->user.flags & US_LASTOLD) && (lastold > 0L) && (printed_lastold == 0) && (!is_seen)) {
631 CallBack(lastold, userdata);
635 if (CallBack) CallBack(thismsg, userdata);
639 free(msglist); /* Clean up */
640 return num_processed;
646 * cmd_msgs() - get list of message #'s in this room
647 * implements the MSGS server command using CtdlForEachMessage()
649 void cmd_msgs(char *cmdbuf)
658 int with_template = 0;
659 struct CtdlMessage *template = NULL;
660 int with_headers = 0;
662 extract_token(which, cmdbuf, 0, '|', sizeof which);
663 cm_ref = extract_int(cmdbuf, 1);
664 with_template = extract_int(cmdbuf, 2);
665 with_headers = extract_int(cmdbuf, 3);
669 if (!strncasecmp(which, "OLD", 3))
671 else if (!strncasecmp(which, "NEW", 3))
673 else if (!strncasecmp(which, "FIRST", 5))
675 else if (!strncasecmp(which, "LAST", 4))
677 else if (!strncasecmp(which, "GT", 2))
680 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
681 cprintf("%d not logged in\n", ERROR + NOT_LOGGED_IN);
687 cprintf("%d Send template then receive message list\n",
689 template = (struct CtdlMessage *)
690 malloc(sizeof(struct CtdlMessage));
691 memset(template, 0, sizeof(struct CtdlMessage));
692 while(client_getln(buf, sizeof buf), strcmp(buf,"000")) {
693 extract_token(tfield, buf, 0, '|', sizeof tfield);
694 extract_token(tvalue, buf, 1, '|', sizeof tvalue);
695 for (i='A'; i<='Z'; ++i) if (msgkeys[i]!=NULL) {
696 if (!strcasecmp(tfield, msgkeys[i])) {
697 template->cm_fields[i] =
705 cprintf("%d \n", LISTING_FOLLOWS);
708 CtdlForEachMessage(mode,
712 (with_headers ? headers_listing : simple_listing),
715 if (template != NULL) CtdlFreeMessage(template);
723 * help_subst() - support routine for help file viewer
725 void help_subst(char *strbuf, char *source, char *dest)
730 while (p = pattern2(strbuf, source), (p >= 0)) {
731 strcpy(workbuf, &strbuf[p + strlen(source)]);
732 strcpy(&strbuf[p], dest);
733 strcat(strbuf, workbuf);
738 void do_help_subst(char *buffer)
742 help_subst(buffer, "^nodename", config.c_nodename);
743 help_subst(buffer, "^humannode", config.c_humannode);
744 help_subst(buffer, "^fqdn", config.c_fqdn);
745 help_subst(buffer, "^username", CC->user.fullname);
746 snprintf(buf2, sizeof buf2, "%ld", CC->user.usernum);
747 help_subst(buffer, "^usernum", buf2);
748 help_subst(buffer, "^sysadm", config.c_sysadm);
749 help_subst(buffer, "^variantname", CITADEL);
750 snprintf(buf2, sizeof buf2, "%d", config.c_maxsessions);
751 help_subst(buffer, "^maxsessions", buf2);
752 help_subst(buffer, "^bbsdir", CTDLDIR);
758 * memfmout() - Citadel text formatter and paginator.
759 * Although the original purpose of this routine was to format
760 * text to the reader's screen width, all we're really using it
761 * for here is to format text out to 80 columns before sending it
762 * to the client. The client software may reformat it again.
765 int width, /* screen width to use */
766 char *mptr, /* where are we going to get our text from? */
767 char subst, /* nonzero if we should do substitutions */
768 char *nl) /* string to terminate lines with */
780 c = 1; /* c is the current pos */
784 while (ch = *mptr, ((ch != 0) && (strlen(buffer) < 126))) {
786 buffer[strlen(buffer) + 1] = 0;
787 buffer[strlen(buffer)] = ch;
790 if (buffer[0] == '^')
791 do_help_subst(buffer);
793 buffer[strlen(buffer) + 1] = 0;
795 strcpy(buffer, &buffer[1]);
803 if (((ch == 13) || (ch == 10)) && (old != 13) && (old != 10))
805 if (((old == 13) || (old == 10)) && (isspace(real))) {
813 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
814 cprintf("%s%s", nl, aaa);
823 if ((strlen(aaa) + c) > (width - 5)) {
832 if ((ch == 13) || (ch == 10)) {
833 cprintf("%s%s", aaa, nl);
840 cprintf("%s%s", aaa, nl);
846 * Callback function for mime parser that simply lists the part
848 void list_this_part(char *name, char *filename, char *partnum, char *disp,
849 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
854 ma = (struct ma_info *)cbuserdata;
855 if (ma->is_ma == 0) {
856 cprintf("part=%s|%s|%s|%s|%s|%ld\n",
857 name, filename, partnum, disp, cbtype, (long)length);
862 * Callback function for multipart prefix
864 void list_this_pref(char *name, char *filename, char *partnum, char *disp,
865 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
870 ma = (struct ma_info *)cbuserdata;
871 if (!strcasecmp(cbtype, "multipart/alternative")) {
875 if (ma->is_ma == 0) {
876 cprintf("pref=%s|%s\n", partnum, cbtype);
881 * Callback function for multipart sufffix
883 void list_this_suff(char *name, char *filename, char *partnum, char *disp,
884 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
889 ma = (struct ma_info *)cbuserdata;
890 if (ma->is_ma == 0) {
891 cprintf("suff=%s|%s\n", partnum, cbtype);
893 if (!strcasecmp(cbtype, "multipart/alternative")) {
900 * Callback function for mime parser that opens a section for downloading
902 void mime_download(char *name, char *filename, char *partnum, char *disp,
903 void *content, char *cbtype, char *cbcharset, size_t length,
904 char *encoding, void *cbuserdata)
907 /* Silently go away if there's already a download open... */
908 if (CC->download_fp != NULL)
911 /* ...or if this is not the desired section */
912 if (strcasecmp(CC->download_desired_section, partnum))
915 CC->download_fp = tmpfile();
916 if (CC->download_fp == NULL)
919 fwrite(content, length, 1, CC->download_fp);
920 fflush(CC->download_fp);
921 rewind(CC->download_fp);
923 OpenCmdResult(filename, cbtype);
929 * Load a message from disk into memory.
930 * This is used by CtdlOutputMsg() and other fetch functions.
932 * NOTE: Caller is responsible for freeing the returned CtdlMessage struct
933 * using the CtdlMessageFree() function.
935 struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body)
937 struct cdbdata *dmsgtext;
938 struct CtdlMessage *ret = NULL;
942 cit_uint8_t field_header;
944 lprintf(CTDL_DEBUG, "CtdlFetchMessage(%ld, %d)\n", msgnum, with_body);
946 dmsgtext = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
947 if (dmsgtext == NULL) {
950 mptr = dmsgtext->ptr;
951 upper_bound = mptr + dmsgtext->len;
953 /* Parse the three bytes that begin EVERY message on disk.
954 * The first is always 0xFF, the on-disk magic number.
955 * The second is the anonymous/public type byte.
956 * The third is the format type byte (vari, fixed, or MIME).
961 "Message %ld appears to be corrupted.\n",
966 ret = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
967 memset(ret, 0, sizeof(struct CtdlMessage));
969 ret->cm_magic = CTDLMESSAGE_MAGIC;
970 ret->cm_anon_type = *mptr++; /* Anon type byte */
971 ret->cm_format_type = *mptr++; /* Format type byte */
974 * The rest is zero or more arbitrary fields. Load them in.
975 * We're done when we encounter either a zero-length field or
976 * have just processed the 'M' (message text) field.
979 if (mptr >= upper_bound) {
982 field_header = *mptr++;
983 ret->cm_fields[field_header] = strdup(mptr);
985 while (*mptr++ != 0); /* advance to next field */
987 } while ((mptr < upper_bound) && (field_header != 'M'));
991 /* Always make sure there's something in the msg text field. If
992 * it's NULL, the message text is most likely stored separately,
993 * so go ahead and fetch that. Failing that, just set a dummy
994 * body so other code doesn't barf.
996 if ( (ret->cm_fields['M'] == NULL) && (with_body) ) {
997 dmsgtext = cdb_fetch(CDB_BIGMSGS, &msgnum, sizeof(long));
998 if (dmsgtext != NULL) {
999 ret->cm_fields['M'] = strdup(dmsgtext->ptr);
1003 if (ret->cm_fields['M'] == NULL) {
1004 ret->cm_fields['M'] = strdup("<no text>\n");
1007 /* Perform "before read" hooks (aborting if any return nonzero) */
1008 if (PerformMessageHooks(ret, EVT_BEFOREREAD) > 0) {
1009 CtdlFreeMessage(ret);
1018 * Returns 1 if the supplied pointer points to a valid Citadel message.
1019 * If the pointer is NULL or the magic number check fails, returns 0.
1021 int is_valid_message(struct CtdlMessage *msg) {
1024 if ((msg->cm_magic) != CTDLMESSAGE_MAGIC) {
1025 lprintf(CTDL_WARNING, "is_valid_message() -- self-check failed\n");
1033 * 'Destructor' for struct CtdlMessage
1035 void CtdlFreeMessage(struct CtdlMessage *msg)
1039 if (is_valid_message(msg) == 0) return;
1041 for (i = 0; i < 256; ++i)
1042 if (msg->cm_fields[i] != NULL) {
1043 free(msg->cm_fields[i]);
1046 msg->cm_magic = 0; /* just in case */
1052 * Pre callback function for multipart/alternative
1054 * NOTE: this differs from the standard behavior for a reason. Normally when
1055 * displaying multipart/alternative you want to show the _last_ usable
1056 * format in the message. Here we show the _first_ one, because it's
1057 * usually text/plain. Since this set of functions is designed for text
1058 * output to non-MIME-aware clients, this is the desired behavior.
1061 void fixed_output_pre(char *name, char *filename, char *partnum, char *disp,
1062 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1067 ma = (struct ma_info *)cbuserdata;
1068 lprintf(CTDL_DEBUG, "fixed_output_pre() type=<%s>\n", cbtype);
1069 if (!strcasecmp(cbtype, "multipart/alternative")) {
1077 * Post callback function for multipart/alternative
1079 void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
1080 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1085 ma = (struct ma_info *)cbuserdata;
1086 lprintf(CTDL_DEBUG, "fixed_output_post() type=<%s>\n", cbtype);
1087 if (!strcasecmp(cbtype, "multipart/alternative")) {
1095 * Inline callback function for mime parser that wants to display text
1097 void fixed_output(char *name, char *filename, char *partnum, char *disp,
1098 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1106 ma = (struct ma_info *)cbuserdata;
1108 lprintf(CTDL_DEBUG, "fixed_output() type=<%s>\n", cbtype);
1111 * If we're in the middle of a multipart/alternative scope and
1112 * we've already printed another section, skip this one.
1114 if ( (ma->is_ma == 1) && (ma->did_print == 1) ) {
1115 lprintf(CTDL_DEBUG, "Skipping part %s (%s)\n", partnum, cbtype);
1120 if ( (!strcasecmp(cbtype, "text/plain"))
1121 || (strlen(cbtype)==0) ) {
1124 client_write(wptr, length);
1125 if (wptr[length-1] != '\n') {
1130 else if (!strcasecmp(cbtype, "text/html")) {
1131 ptr = html_to_ascii(content, 80, 0);
1133 client_write(ptr, wlen);
1134 if (ptr[wlen-1] != '\n') {
1139 else if (strncasecmp(cbtype, "multipart/", 10)) {
1140 cprintf("Part %s: %s (%s) (%ld bytes)\r\n",
1141 partnum, filename, cbtype, (long)length);
1146 * The client is elegant and sophisticated and wants to be choosy about
1147 * MIME content types, so figure out which multipart/alternative part
1148 * we're going to send.
1150 void choose_preferred(char *name, char *filename, char *partnum, char *disp,
1151 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1158 ma = (struct ma_info *)cbuserdata;
1160 if (ma->is_ma > 0) {
1161 for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
1162 extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
1163 if (!strcasecmp(buf, cbtype)) {
1164 strcpy(ma->chosen_part, partnum);
1171 * Now that we've chosen our preferred part, output it.
1173 void output_preferred(char *name, char *filename, char *partnum, char *disp,
1174 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1179 int add_newline = 0;
1183 ma = (struct ma_info *)cbuserdata;
1185 /* This is not the MIME part you're looking for... */
1186 if (strcasecmp(partnum, ma->chosen_part)) return;
1188 /* If the content-type of this part is in our preferred formats
1189 * list, we can simply output it verbatim.
1191 for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
1192 extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
1193 if (!strcasecmp(buf, cbtype)) {
1194 /* Yeah! Go! W00t!! */
1196 text_content = (char *)content;
1197 if (text_content[length-1] != '\n') {
1201 cprintf("Content-type: %s", cbtype);
1202 if (strlen(cbcharset) > 0) {
1203 cprintf("; charset=%s", cbcharset);
1205 cprintf("\nContent-length: %d\n",
1206 (int)(length + add_newline) );
1207 if (strlen(encoding) > 0) {
1208 cprintf("Content-transfer-encoding: %s\n", encoding);
1211 cprintf("Content-transfer-encoding: 7bit\n");
1214 client_write(content, length);
1215 if (add_newline) cprintf("\n");
1220 /* No translations required or possible: output as text/plain */
1221 cprintf("Content-type: text/plain\n\n");
1222 fixed_output(name, filename, partnum, disp, content, cbtype, cbcharset,
1223 length, encoding, cbuserdata);
1228 * Get a message off disk. (returns om_* values found in msgbase.h)
1231 int CtdlOutputMsg(long msg_num, /* message number (local) to fetch */
1232 int mode, /* how would you like that message? */
1233 int headers_only, /* eschew the message body? */
1234 int do_proto, /* do Citadel protocol responses? */
1235 int crlf /* Use CRLF newlines instead of LF? */
1237 struct CtdlMessage *TheMessage = NULL;
1238 int retcode = om_no_such_msg;
1240 lprintf(CTDL_DEBUG, "CtdlOutputMsg() msgnum=%ld, mode=%d\n",
1243 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1244 if (do_proto) cprintf("%d Not logged in.\n",
1245 ERROR + NOT_LOGGED_IN);
1246 return(om_not_logged_in);
1249 /* FIXME: check message id against msglist for this room */
1252 * Fetch the message from disk. If we're in any sort of headers
1253 * only mode, request that we don't even bother loading the body
1256 if ( (headers_only == HEADERS_FAST) || (headers_only == HEADERS_ONLY) ) {
1257 TheMessage = CtdlFetchMessage(msg_num, 0);
1260 TheMessage = CtdlFetchMessage(msg_num, 1);
1263 if (TheMessage == NULL) {
1264 if (do_proto) cprintf("%d Can't locate msg %ld on disk\n",
1265 ERROR + MESSAGE_NOT_FOUND, msg_num);
1266 return(om_no_such_msg);
1269 retcode = CtdlOutputPreLoadedMsg(
1271 headers_only, do_proto, crlf);
1273 CtdlFreeMessage(TheMessage);
1280 * Get a message off disk. (returns om_* values found in msgbase.h)
1283 int CtdlOutputPreLoadedMsg(
1284 struct CtdlMessage *TheMessage,
1285 int mode, /* how would you like that message? */
1286 int headers_only, /* eschew the message body? */
1287 int do_proto, /* do Citadel protocol responses? */
1288 int crlf /* Use CRLF newlines instead of LF? */
1294 char display_name[256];
1296 char *nl; /* newline string */
1298 int subject_found = 0;
1301 /* Buffers needed for RFC822 translation. These are all filled
1302 * using functions that are bounds-checked, and therefore we can
1303 * make them substantially smaller than SIZ.
1311 char datestamp[100];
1313 lprintf(CTDL_DEBUG, "CtdlOutputPreLoadedMsg(TheMessage=%s, %d, %d, %d, %d\n",
1314 ((TheMessage == NULL) ? "NULL" : "not null"),
1315 mode, headers_only, do_proto, crlf);
1317 strcpy(mid, "unknown");
1318 nl = (crlf ? "\r\n" : "\n");
1320 if (!is_valid_message(TheMessage)) {
1322 "ERROR: invalid preloaded message for output\n");
1323 return(om_no_such_msg);
1326 /* Are we downloading a MIME component? */
1327 if (mode == MT_DOWNLOAD) {
1328 if (TheMessage->cm_format_type != FMT_RFC822) {
1330 cprintf("%d This is not a MIME message.\n",
1331 ERROR + ILLEGAL_VALUE);
1332 } else if (CC->download_fp != NULL) {
1333 if (do_proto) cprintf(
1334 "%d You already have a download open.\n",
1335 ERROR + RESOURCE_BUSY);
1337 /* Parse the message text component */
1338 mptr = TheMessage->cm_fields['M'];
1339 mime_parser(mptr, NULL, *mime_download, NULL, NULL, NULL, 0);
1340 /* If there's no file open by this time, the requested
1341 * section wasn't found, so print an error
1343 if (CC->download_fp == NULL) {
1344 if (do_proto) cprintf(
1345 "%d Section %s not found.\n",
1346 ERROR + FILE_NOT_FOUND,
1347 CC->download_desired_section);
1350 return((CC->download_fp != NULL) ? om_ok : om_mime_error);
1353 /* now for the user-mode message reading loops */
1354 if (do_proto) cprintf("%d msg:\n", LISTING_FOLLOWS);
1356 /* Does the caller want to skip the headers? */
1357 if (headers_only == HEADERS_NONE) goto START_TEXT;
1359 /* Tell the client which format type we're using. */
1360 if ( (mode == MT_CITADEL) && (do_proto) ) {
1361 cprintf("type=%d\n", TheMessage->cm_format_type);
1364 /* nhdr=yes means that we're only displaying headers, no body */
1365 if ( (TheMessage->cm_anon_type == MES_ANONONLY)
1366 && (mode == MT_CITADEL)
1369 cprintf("nhdr=yes\n");
1372 /* begin header processing loop for Citadel message format */
1374 if ((mode == MT_CITADEL) || (mode == MT_MIME)) {
1376 safestrncpy(display_name, "<unknown>", sizeof display_name);
1377 if (TheMessage->cm_fields['A']) {
1378 strcpy(buf, TheMessage->cm_fields['A']);
1379 if (TheMessage->cm_anon_type == MES_ANONONLY) {
1380 safestrncpy(display_name, "****", sizeof display_name);
1382 else if (TheMessage->cm_anon_type == MES_ANONOPT) {
1383 safestrncpy(display_name, "anonymous", sizeof display_name);
1386 safestrncpy(display_name, buf, sizeof display_name);
1388 if ((is_room_aide())
1389 && ((TheMessage->cm_anon_type == MES_ANONONLY)
1390 || (TheMessage->cm_anon_type == MES_ANONOPT))) {
1391 size_t tmp = strlen(display_name);
1392 snprintf(&display_name[tmp],
1393 sizeof display_name - tmp,
1398 /* Don't show Internet address for users on the
1399 * local Citadel network.
1402 if (TheMessage->cm_fields['N'] != NULL)
1403 if (strlen(TheMessage->cm_fields['N']) > 0)
1404 if (haschar(TheMessage->cm_fields['N'], '.') == 0) {
1408 /* Now spew the header fields in the order we like them. */
1409 safestrncpy(allkeys, FORDER, sizeof allkeys);
1410 for (i=0; i<strlen(allkeys); ++i) {
1411 k = (int) allkeys[i];
1413 if ( (TheMessage->cm_fields[k] != NULL)
1414 && (msgkeys[k] != NULL) ) {
1416 if (do_proto) cprintf("%s=%s\n",
1420 else if ((k == 'F') && (suppress_f)) {
1423 /* Masquerade display name if needed */
1425 if (do_proto) cprintf("%s=%s\n",
1427 TheMessage->cm_fields[k]
1436 /* begin header processing loop for RFC822 transfer format */
1441 strcpy(snode, NODENAME);
1442 strcpy(lnode, HUMANNODE);
1443 if (mode == MT_RFC822) {
1444 for (i = 0; i < 256; ++i) {
1445 if (TheMessage->cm_fields[i]) {
1446 mptr = TheMessage->cm_fields[i];
1449 safestrncpy(luser, mptr, sizeof luser);
1450 safestrncpy(suser, mptr, sizeof suser);
1452 else if (i == 'Y') {
1453 cprintf("CC: %s%s", mptr, nl);
1455 else if (i == 'U') {
1456 cprintf("Subject: %s%s", mptr, nl);
1460 safestrncpy(mid, mptr, sizeof mid);
1462 safestrncpy(lnode, mptr, sizeof lnode);
1464 safestrncpy(fuser, mptr, sizeof fuser);
1465 /* else if (i == 'O')
1466 cprintf("X-Citadel-Room: %s%s",
1469 safestrncpy(snode, mptr, sizeof snode);
1471 cprintf("To: %s%s", mptr, nl);
1472 else if (i == 'T') {
1473 datestring(datestamp, sizeof datestamp,
1474 atol(mptr), DATESTRING_RFC822);
1475 cprintf("Date: %s%s", datestamp, nl);
1479 if (subject_found == 0) {
1480 cprintf("Subject: (no subject)%s", nl);
1484 for (i=0; i<strlen(suser); ++i) {
1485 suser[i] = tolower(suser[i]);
1486 if (!isalnum(suser[i])) suser[i]='_';
1489 if (mode == MT_RFC822) {
1490 if (!strcasecmp(snode, NODENAME)) {
1491 safestrncpy(snode, FQDN, sizeof snode);
1494 /* Construct a fun message id */
1495 cprintf("Message-ID: <%s", mid);
1496 if (strchr(mid, '@')==NULL) {
1497 cprintf("@%s", snode);
1501 if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONONLY)) {
1502 cprintf("From: \"----\" <x@x.org>%s", nl);
1504 else if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONOPT)) {
1505 cprintf("From: \"anonymous\" <x@x.org>%s", nl);
1507 else if (strlen(fuser) > 0) {
1508 cprintf("From: \"%s\" <%s>%s", luser, fuser, nl);
1511 cprintf("From: \"%s\" <%s@%s>%s", luser, suser, snode, nl);
1514 cprintf("Organization: %s%s", lnode, nl);
1516 /* Blank line signifying RFC822 end-of-headers */
1517 if (TheMessage->cm_format_type != FMT_RFC822) {
1522 /* end header processing loop ... at this point, we're in the text */
1524 if (headers_only == HEADERS_FAST) goto DONE;
1525 mptr = TheMessage->cm_fields['M'];
1527 /* Tell the client about the MIME parts in this message */
1528 if (TheMessage->cm_format_type == FMT_RFC822) {
1529 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
1530 memset(&ma, 0, sizeof(struct ma_info));
1531 mime_parser(mptr, NULL,
1532 (do_proto ? *list_this_part : NULL),
1533 (do_proto ? *list_this_pref : NULL),
1534 (do_proto ? *list_this_suff : NULL),
1537 else if (mode == MT_RFC822) { /* unparsed RFC822 dump */
1538 char *start_of_text = NULL;
1539 start_of_text = strstr(mptr, "\n\r\n");
1540 if (start_of_text == NULL) start_of_text = strstr(mptr, "\n\n");
1541 if (start_of_text == NULL) start_of_text = mptr;
1543 start_of_text = strstr(start_of_text, "\n");
1545 while (ch=*mptr, ch!=0) {
1549 else switch(headers_only) {
1551 if (mptr >= start_of_text) {
1552 if (ch == 10) cprintf("%s", nl);
1553 else cprintf("%c", ch);
1557 if (mptr < start_of_text) {
1558 if (ch == 10) cprintf("%s", nl);
1559 else cprintf("%c", ch);
1563 if (ch == 10) cprintf("%s", nl);
1564 else cprintf("%c", ch);
1573 if (headers_only == HEADERS_ONLY) {
1577 /* signify start of msg text */
1578 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
1579 if (do_proto) cprintf("text\n");
1582 /* If the format type on disk is 1 (fixed-format), then we want
1583 * everything to be output completely literally ... regardless of
1584 * what message transfer format is in use.
1586 if (TheMessage->cm_format_type == FMT_FIXED) {
1587 if (mode == MT_MIME) {
1588 cprintf("Content-type: text/plain\n\n");
1591 while (ch = *mptr++, ch > 0) {
1594 if ((ch == 10) || (strlen(buf) > 250)) {
1595 cprintf("%s%s", buf, nl);
1598 buf[strlen(buf) + 1] = 0;
1599 buf[strlen(buf)] = ch;
1602 if (strlen(buf) > 0)
1603 cprintf("%s%s", buf, nl);
1606 /* If the message on disk is format 0 (Citadel vari-format), we
1607 * output using the formatter at 80 columns. This is the final output
1608 * form if the transfer format is RFC822, but if the transfer format
1609 * is Citadel proprietary, it'll still work, because the indentation
1610 * for new paragraphs is correct and the client will reformat the
1611 * message to the reader's screen width.
1613 if (TheMessage->cm_format_type == FMT_CITADEL) {
1614 if (mode == MT_MIME) {
1615 cprintf("Content-type: text/x-citadel-variformat\n\n");
1617 memfmout(80, mptr, 0, nl);
1620 /* If the message on disk is format 4 (MIME), we've gotta hand it
1621 * off to the MIME parser. The client has already been told that
1622 * this message is format 1 (fixed format), so the callback function
1623 * we use will display those parts as-is.
1625 if (TheMessage->cm_format_type == FMT_RFC822) {
1626 memset(&ma, 0, sizeof(struct ma_info));
1628 if (mode == MT_MIME) {
1629 strcpy(ma.chosen_part, "1");
1630 mime_parser(mptr, NULL,
1631 *choose_preferred, *fixed_output_pre,
1632 *fixed_output_post, (void *)&ma, 0);
1633 mime_parser(mptr, NULL,
1634 *output_preferred, NULL, NULL, (void *)&ma, 0);
1637 mime_parser(mptr, NULL,
1638 *fixed_output, *fixed_output_pre,
1639 *fixed_output_post, (void *)&ma, 0);
1644 DONE: /* now we're done */
1645 if (do_proto) cprintf("000\n");
1652 * display a message (mode 0 - Citadel proprietary)
1654 void cmd_msg0(char *cmdbuf)
1657 int headers_only = HEADERS_ALL;
1659 msgid = extract_long(cmdbuf, 0);
1660 headers_only = extract_int(cmdbuf, 1);
1662 CtdlOutputMsg(msgid, MT_CITADEL, headers_only, 1, 0);
1668 * display a message (mode 2 - RFC822)
1670 void cmd_msg2(char *cmdbuf)
1673 int headers_only = HEADERS_ALL;
1675 msgid = extract_long(cmdbuf, 0);
1676 headers_only = extract_int(cmdbuf, 1);
1678 CtdlOutputMsg(msgid, MT_RFC822, headers_only, 1, 1);
1684 * display a message (mode 3 - IGnet raw format - internal programs only)
1686 void cmd_msg3(char *cmdbuf)
1689 struct CtdlMessage *msg;
1692 if (CC->internal_pgm == 0) {
1693 cprintf("%d This command is for internal programs only.\n",
1694 ERROR + HIGHER_ACCESS_REQUIRED);
1698 msgnum = extract_long(cmdbuf, 0);
1699 msg = CtdlFetchMessage(msgnum, 1);
1701 cprintf("%d Message %ld not found.\n",
1702 ERROR + MESSAGE_NOT_FOUND, msgnum);
1706 serialize_message(&smr, msg);
1707 CtdlFreeMessage(msg);
1710 cprintf("%d Unable to serialize message\n",
1711 ERROR + INTERNAL_ERROR);
1715 cprintf("%d %ld\n", BINARY_FOLLOWS, (long)smr.len);
1716 client_write((char *)smr.ser, (int)smr.len);
1723 * Display a message using MIME content types
1725 void cmd_msg4(char *cmdbuf)
1729 msgid = extract_long(cmdbuf, 0);
1730 CtdlOutputMsg(msgid, MT_MIME, 0, 1, 0);
1736 * Client tells us its preferred message format(s)
1738 void cmd_msgp(char *cmdbuf)
1740 safestrncpy(CC->preferred_formats, cmdbuf,
1741 sizeof(CC->preferred_formats));
1742 cprintf("%d ok\n", CIT_OK);
1747 * Open a component of a MIME message as a download file
1749 void cmd_opna(char *cmdbuf)
1752 char desired_section[128];
1754 msgid = extract_long(cmdbuf, 0);
1755 extract_token(desired_section, cmdbuf, 1, '|', sizeof desired_section);
1756 safestrncpy(CC->download_desired_section, desired_section,
1757 sizeof CC->download_desired_section);
1758 CtdlOutputMsg(msgid, MT_DOWNLOAD, 0, 1, 1);
1763 * Save a message pointer into a specified room
1764 * (Returns 0 for success, nonzero for failure)
1765 * roomname may be NULL to use the current room
1767 * Note that the 'supplied_msg' field may be set to NULL, in which case
1768 * the message will be fetched from disk, by number, if we need to perform
1769 * replication checks. This adds an additional database read, so if the
1770 * caller already has the message in memory then it should be supplied.
1772 int CtdlSaveMsgPointerInRoom(char *roomname, long msgid, int do_repl_check,
1773 struct CtdlMessage *supplied_msg) {
1775 char hold_rm[ROOMNAMELEN];
1776 struct cdbdata *cdbfr;
1779 long highest_msg = 0L;
1780 struct CtdlMessage *msg = NULL;
1782 /*lprintf(CTDL_DEBUG,
1783 "CtdlSaveMsgPointerInRoom(room=%s, msgid=%ld, repl=%d)\n",
1784 roomname, msgid, do_repl_check);*/
1786 strcpy(hold_rm, CC->room.QRname);
1788 /* Now the regular stuff */
1789 if (lgetroom(&CC->room,
1790 ((roomname != NULL) ? roomname : CC->room.QRname) )
1792 lprintf(CTDL_ERR, "No such room <%s>\n", roomname);
1793 return(ERROR + ROOM_NOT_FOUND);
1796 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
1797 if (cdbfr == NULL) {
1801 msglist = malloc(cdbfr->len);
1802 if (msglist == NULL) {
1803 lprintf(CTDL_ALERT, "ERROR malloc msglist!\n");
1805 num_msgs = cdbfr->len / sizeof(long);
1806 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1811 /* Make sure the message doesn't already exist in this room. It
1812 * is absolutely taboo to have more than one reference to the same
1813 * message in a room.
1815 if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
1816 if (msglist[i] == msgid) {
1817 lputroom(&CC->room); /* unlock the room */
1818 getroom(&CC->room, hold_rm);
1820 return(ERROR + ALREADY_EXISTS);
1824 /* Now add the new message */
1826 msglist = realloc(msglist, (num_msgs * sizeof(long)));
1828 if (msglist == NULL) {
1829 lprintf(CTDL_ALERT, "ERROR: can't realloc message list!\n");
1831 msglist[num_msgs - 1] = msgid;
1833 /* Sort the message list, so all the msgid's are in order */
1834 num_msgs = sort_msglist(msglist, num_msgs);
1836 /* Determine the highest message number */
1837 highest_msg = msglist[num_msgs - 1];
1839 /* Write it back to disk. */
1840 cdb_store(CDB_MSGLISTS, &CC->room.QRnumber, (int)sizeof(long),
1841 msglist, (int)(num_msgs * sizeof(long)));
1843 /* Free up the memory we used. */
1847 /* Update the highest-message pointer and unlock the room. */
1848 CC->room.QRhighest = highest_msg;
1849 lputroom(&CC->room);
1851 /* Perform replication checks if necessary */
1852 if ( (DoesThisRoomNeedEuidIndexing(&CC->room)) && (do_repl_check) ) {
1853 if (supplied_msg != NULL) {
1857 msg = CtdlFetchMessage(msgid, 0);
1861 ReplicationChecks(msg);
1866 /* If the message has an Exclusive ID, index that... */
1868 if (msg->cm_fields['E'] != NULL) {
1869 index_message_by_euid(msg->cm_fields['E'],
1874 /* Free up the memory we may have allocated */
1875 if ( (msg != NULL) && (msg != supplied_msg) ) {
1876 CtdlFreeMessage(msg);
1879 /* Go back to the room we were in before we wandered here... */
1880 getroom(&CC->room, hold_rm);
1882 /* Bump the reference count for this message. */
1883 AdjRefCount(msgid, +1);
1885 /* Return success. */
1892 * Message base operation to save a new message to the message store
1893 * (returns new message number)
1895 * This is the back end for CtdlSubmitMsg() and should not be directly
1896 * called by server-side modules.
1899 long send_message(struct CtdlMessage *msg) {
1907 /* Get a new message number */
1908 newmsgid = get_new_message_number();
1909 snprintf(msgidbuf, sizeof msgidbuf, "%ld@%s", newmsgid, config.c_fqdn);
1911 /* Generate an ID if we don't have one already */
1912 if (msg->cm_fields['I']==NULL) {
1913 msg->cm_fields['I'] = strdup(msgidbuf);
1916 /* If the message is big, set its body aside for storage elsewhere */
1917 if (msg->cm_fields['M'] != NULL) {
1918 if (strlen(msg->cm_fields['M']) > BIGMSG) {
1920 holdM = msg->cm_fields['M'];
1921 msg->cm_fields['M'] = NULL;
1925 /* Serialize our data structure for storage in the database */
1926 serialize_message(&smr, msg);
1929 msg->cm_fields['M'] = holdM;
1933 cprintf("%d Unable to serialize message\n",
1934 ERROR + INTERNAL_ERROR);
1938 /* Write our little bundle of joy into the message base */
1939 if (cdb_store(CDB_MSGMAIN, &newmsgid, (int)sizeof(long),
1940 smr.ser, smr.len) < 0) {
1941 lprintf(CTDL_ERR, "Can't store message\n");
1945 cdb_store(CDB_BIGMSGS,
1955 /* Free the memory we used for the serialized message */
1958 /* Return the *local* message ID to the caller
1959 * (even if we're storing an incoming network message)
1967 * Serialize a struct CtdlMessage into the format used on disk and network.
1969 * This function loads up a "struct ser_ret" (defined in server.h) which
1970 * contains the length of the serialized message and a pointer to the
1971 * serialized message in memory. THE LATTER MUST BE FREED BY THE CALLER.
1973 void serialize_message(struct ser_ret *ret, /* return values */
1974 struct CtdlMessage *msg) /* unserialized msg */
1978 static char *forder = FORDER;
1980 if (is_valid_message(msg) == 0) return; /* self check */
1983 for (i=0; i<26; ++i) if (msg->cm_fields[(int)forder[i]] != NULL)
1984 ret->len = ret->len +
1985 strlen(msg->cm_fields[(int)forder[i]]) + 2;
1987 lprintf(CTDL_DEBUG, "serialize_message() calling malloc(%ld)\n", (long)ret->len);
1988 ret->ser = malloc(ret->len);
1989 if (ret->ser == NULL) {
1995 ret->ser[1] = msg->cm_anon_type;
1996 ret->ser[2] = msg->cm_format_type;
1999 for (i=0; i<26; ++i) if (msg->cm_fields[(int)forder[i]] != NULL) {
2000 ret->ser[wlen++] = (char)forder[i];
2001 strcpy((char *)&ret->ser[wlen], msg->cm_fields[(int)forder[i]]);
2002 wlen = wlen + strlen(msg->cm_fields[(int)forder[i]]) + 1;
2004 if (ret->len != wlen) lprintf(CTDL_ERR, "ERROR: len=%ld wlen=%ld\n",
2005 (long)ret->len, (long)wlen);
2013 * Check to see if any messages already exist in the current room which
2014 * carry the same Exclusive ID as this one. If any are found, delete them.
2016 void ReplicationChecks(struct CtdlMessage *msg) {
2017 long old_msgnum = (-1L);
2019 if (DoesThisRoomNeedEuidIndexing(&CC->room) == 0) return;
2021 lprintf(CTDL_DEBUG, "Performing replication checks in <%s>\n",
2024 /* No exclusive id? Don't do anything. */
2025 if (msg == NULL) return;
2026 if (msg->cm_fields['E'] == NULL) return;
2027 if (strlen(msg->cm_fields['E']) == 0) return;
2028 /*lprintf(CTDL_DEBUG, "Exclusive ID: <%s> for room <%s>\n",
2029 msg->cm_fields['E'], CC->room.QRname);*/
2031 old_msgnum = locate_message_by_euid(msg->cm_fields['E'], &CC->room);
2032 if (old_msgnum > 0L) {
2033 lprintf(CTDL_DEBUG, "ReplicationChecks() replacing message %ld\n", old_msgnum);
2034 CtdlDeleteMessages(CC->room.QRname, old_msgnum, "", 0);
2041 * Save a message to disk and submit it into the delivery system.
2043 long CtdlSubmitMsg(struct CtdlMessage *msg, /* message to save */
2044 struct recptypes *recps, /* recipients (if mail) */
2045 char *force /* force a particular room? */
2047 char submit_filename[128];
2048 char generated_timestamp[32];
2049 char hold_rm[ROOMNAMELEN];
2050 char actual_rm[ROOMNAMELEN];
2051 char force_room[ROOMNAMELEN];
2052 char content_type[SIZ]; /* We have to learn this */
2053 char recipient[SIZ];
2056 struct ctdluser userbuf;
2058 struct MetaData smi;
2059 FILE *network_fp = NULL;
2060 static int seqnum = 1;
2061 struct CtdlMessage *imsg = NULL;
2064 char *hold_R, *hold_D;
2065 char *collected_addresses = NULL;
2066 struct addresses_to_be_filed *aptr = NULL;
2068 lprintf(CTDL_DEBUG, "CtdlSubmitMsg() called\n");
2069 if (is_valid_message(msg) == 0) return(-1); /* self check */
2071 /* If this message has no timestamp, we take the liberty of
2072 * giving it one, right now.
2074 if (msg->cm_fields['T'] == NULL) {
2075 snprintf(generated_timestamp, sizeof generated_timestamp, "%ld", (long)time(NULL));
2076 msg->cm_fields['T'] = strdup(generated_timestamp);
2079 /* If this message has no path, we generate one.
2081 if (msg->cm_fields['P'] == NULL) {
2082 if (msg->cm_fields['A'] != NULL) {
2083 msg->cm_fields['P'] = strdup(msg->cm_fields['A']);
2084 for (a=0; a<strlen(msg->cm_fields['P']); ++a) {
2085 if (isspace(msg->cm_fields['P'][a])) {
2086 msg->cm_fields['P'][a] = ' ';
2091 msg->cm_fields['P'] = strdup("unknown");
2095 if (force == NULL) {
2096 strcpy(force_room, "");
2099 strcpy(force_room, force);
2102 /* Learn about what's inside, because it's what's inside that counts */
2103 if (msg->cm_fields['M'] == NULL) {
2104 lprintf(CTDL_ERR, "ERROR: attempt to save message with NULL body\n");
2108 switch (msg->cm_format_type) {
2110 strcpy(content_type, "text/x-citadel-variformat");
2113 strcpy(content_type, "text/plain");
2116 strcpy(content_type, "text/plain");
2117 mptr = bmstrcasestr(msg->cm_fields['M'], "Content-type: ");
2119 safestrncpy(content_type, &mptr[14],
2120 sizeof content_type);
2121 for (a = 0; a < strlen(content_type); ++a) {
2122 if ((content_type[a] == ';')
2123 || (content_type[a] == ' ')
2124 || (content_type[a] == 13)
2125 || (content_type[a] == 10)) {
2126 content_type[a] = 0;
2132 /* Goto the correct room */
2133 lprintf(CTDL_DEBUG, "Selected room %s\n", (recps) ? CC->room.QRname : SENTITEMS);
2134 strcpy(hold_rm, CC->room.QRname);
2135 strcpy(actual_rm, CC->room.QRname);
2136 if (recps != NULL) {
2137 strcpy(actual_rm, SENTITEMS);
2140 /* If the user is a twit, move to the twit room for posting */
2142 if (CC->user.axlevel == 2) {
2143 strcpy(hold_rm, actual_rm);
2144 strcpy(actual_rm, config.c_twitroom);
2145 lprintf(CTDL_DEBUG, "Diverting to twit room\n");
2149 /* ...or if this message is destined for Aide> then go there. */
2150 if (strlen(force_room) > 0) {
2151 strcpy(actual_rm, force_room);
2154 lprintf(CTDL_DEBUG, "Final selection: %s\n", actual_rm);
2155 if (strcasecmp(actual_rm, CC->room.QRname)) {
2156 /* getroom(&CC->room, actual_rm); */
2157 usergoto(actual_rm, 0, 1, NULL, NULL);
2161 * If this message has no O (room) field, generate one.
2163 if (msg->cm_fields['O'] == NULL) {
2164 msg->cm_fields['O'] = strdup(CC->room.QRname);
2167 /* Perform "before save" hooks (aborting if any return nonzero) */
2168 lprintf(CTDL_DEBUG, "Performing before-save hooks\n");
2169 if (PerformMessageHooks(msg, EVT_BEFORESAVE) > 0) return(-3);
2172 * If this message has an Exclusive ID, and the room is replication
2173 * checking enabled, then do replication checks.
2175 if (DoesThisRoomNeedEuidIndexing(&CC->room)) {
2176 ReplicationChecks(msg);
2179 /* Save it to disk */
2180 lprintf(CTDL_DEBUG, "Saving to disk\n");
2181 newmsgid = send_message(msg);
2182 if (newmsgid <= 0L) return(-5);
2184 /* Write a supplemental message info record. This doesn't have to
2185 * be a critical section because nobody else knows about this message
2188 lprintf(CTDL_DEBUG, "Creating MetaData record\n");
2189 memset(&smi, 0, sizeof(struct MetaData));
2190 smi.meta_msgnum = newmsgid;
2191 smi.meta_refcount = 0;
2192 safestrncpy(smi.meta_content_type, content_type,
2193 sizeof smi.meta_content_type);
2195 /* As part of the new metadata record, measure how
2196 * big this message will be when displayed as RFC822.
2197 * Both POP and IMAP use this, and it's best to just take the hit now
2198 * instead of having to potentially measure thousands of messages when
2199 * a mailbox is opened later.
2202 if (CC->redirect_buffer != NULL) {
2203 lprintf(CTDL_ALERT, "CC->redirect_buffer is not NULL during message submission!\n");
2206 CC->redirect_buffer = malloc(SIZ);
2207 CC->redirect_len = 0;
2208 CC->redirect_alloc = SIZ;
2209 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1);
2210 smi.meta_rfc822_length = CC->redirect_len;
2211 free(CC->redirect_buffer);
2212 CC->redirect_buffer = NULL;
2213 CC->redirect_len = 0;
2214 CC->redirect_alloc = 0;
2218 /* Now figure out where to store the pointers */
2219 lprintf(CTDL_DEBUG, "Storing pointers\n");
2221 /* If this is being done by the networker delivering a private
2222 * message, we want to BYPASS saving the sender's copy (because there
2223 * is no local sender; it would otherwise go to the Trashcan).
2225 if ((!CC->internal_pgm) || (recps == NULL)) {
2226 if (CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 1, msg) != 0) {
2227 lprintf(CTDL_ERR, "ERROR saving message pointer!\n");
2228 CtdlSaveMsgPointerInRoom(config.c_aideroom, newmsgid, 0, msg);
2232 /* For internet mail, drop a copy in the outbound queue room */
2234 if (recps->num_internet > 0) {
2235 CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, newmsgid, 0, msg);
2238 /* If other rooms are specified, drop them there too. */
2240 if (recps->num_room > 0)
2241 for (i=0; i<num_tokens(recps->recp_room, '|'); ++i) {
2242 extract_token(recipient, recps->recp_room, i,
2243 '|', sizeof recipient);
2244 lprintf(CTDL_DEBUG, "Delivering to room <%s>\n", recipient);
2245 CtdlSaveMsgPointerInRoom(recipient, newmsgid, 0, msg);
2248 /* Bump this user's messages posted counter. */
2249 lprintf(CTDL_DEBUG, "Updating user\n");
2250 lgetuser(&CC->user, CC->curr_user);
2251 CC->user.posted = CC->user.posted + 1;
2252 lputuser(&CC->user);
2254 /* If this is private, local mail, make a copy in the
2255 * recipient's mailbox and bump the reference count.
2258 if (recps->num_local > 0)
2259 for (i=0; i<num_tokens(recps->recp_local, '|'); ++i) {
2260 extract_token(recipient, recps->recp_local, i,
2261 '|', sizeof recipient);
2262 lprintf(CTDL_DEBUG, "Delivering private local mail to <%s>\n",
2264 if (getuser(&userbuf, recipient) == 0) {
2265 MailboxName(actual_rm, sizeof actual_rm,
2266 &userbuf, MAILROOM);
2267 CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0, msg);
2268 BumpNewMailCounter(userbuf.usernum);
2271 lprintf(CTDL_DEBUG, "No user <%s>\n", recipient);
2272 CtdlSaveMsgPointerInRoom(config.c_aideroom, newmsgid, 0, msg);
2276 /* Perform "after save" hooks */
2277 lprintf(CTDL_DEBUG, "Performing after-save hooks\n");
2278 PerformMessageHooks(msg, EVT_AFTERSAVE);
2280 /* For IGnet mail, we have to save a new copy into the spooler for
2281 * each recipient, with the R and D fields set to the recipient and
2282 * destination-node. This has two ugly side effects: all other
2283 * recipients end up being unlisted in this recipient's copy of the
2284 * message, and it has to deliver multiple messages to the same
2285 * node. We'll revisit this again in a year or so when everyone has
2286 * a network spool receiver that can handle the new style messages.
2289 if (recps->num_ignet > 0)
2290 for (i=0; i<num_tokens(recps->recp_ignet, '|'); ++i) {
2291 extract_token(recipient, recps->recp_ignet, i,
2292 '|', sizeof recipient);
2294 hold_R = msg->cm_fields['R'];
2295 hold_D = msg->cm_fields['D'];
2296 msg->cm_fields['R'] = malloc(SIZ);
2297 msg->cm_fields['D'] = malloc(128);
2298 extract_token(msg->cm_fields['R'], recipient, 0, '@', SIZ);
2299 extract_token(msg->cm_fields['D'], recipient, 1, '@', 128);
2301 serialize_message(&smr, msg);
2303 snprintf(submit_filename, sizeof submit_filename,
2304 #ifndef HAVE_SPOOL_DIR
2309 "/network/spoolin/netmail.%04lx.%04x.%04x",
2310 (long) getpid(), CC->cs_pid, ++seqnum);
2311 network_fp = fopen(submit_filename, "wb+");
2312 if (network_fp != NULL) {
2313 fwrite(smr.ser, smr.len, 1, network_fp);
2319 free(msg->cm_fields['R']);
2320 free(msg->cm_fields['D']);
2321 msg->cm_fields['R'] = hold_R;
2322 msg->cm_fields['D'] = hold_D;
2325 /* Go back to the room we started from */
2326 lprintf(CTDL_DEBUG, "Returning to original room %s\n", hold_rm);
2327 if (strcasecmp(hold_rm, CC->room.QRname))
2328 /* getroom(&CC->room, hold_rm); */
2329 usergoto(hold_rm, 0, 1, NULL, NULL);
2331 /* For internet mail, generate delivery instructions.
2332 * Yes, this is recursive. Deal with it. Infinite recursion does
2333 * not happen because the delivery instructions message does not
2334 * contain a recipient.
2337 if (recps->num_internet > 0) {
2338 lprintf(CTDL_DEBUG, "Generating delivery instructions\n");
2339 instr = malloc(SIZ * 2);
2340 snprintf(instr, SIZ * 2,
2341 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
2343 SPOOLMIME, newmsgid, (long)time(NULL),
2344 msg->cm_fields['A'], msg->cm_fields['N']
2347 for (i=0; i<num_tokens(recps->recp_internet, '|'); ++i) {
2348 size_t tmp = strlen(instr);
2349 extract_token(recipient, recps->recp_internet, i, '|', sizeof recipient);
2350 snprintf(&instr[tmp], SIZ * 2 - tmp,
2351 "remote|%s|0||\n", recipient);
2354 imsg = malloc(sizeof(struct CtdlMessage));
2355 memset(imsg, 0, sizeof(struct CtdlMessage));
2356 imsg->cm_magic = CTDLMESSAGE_MAGIC;
2357 imsg->cm_anon_type = MES_NORMAL;
2358 imsg->cm_format_type = FMT_RFC822;
2359 imsg->cm_fields['A'] = strdup("Citadel");
2360 imsg->cm_fields['M'] = instr;
2361 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
2362 CtdlFreeMessage(imsg);
2366 * Any addresses to harvest for someone's address book?
2368 if ( (CC->logged_in) && (recps != NULL) ) {
2369 collected_addresses = harvest_collected_addresses(msg);
2372 if (collected_addresses != NULL) {
2373 begin_critical_section(S_ATBF);
2374 aptr = (struct addresses_to_be_filed *) malloc(sizeof(struct addresses_to_be_filed));
2376 MailboxName(actual_rm, sizeof actual_rm, &CC->user, USERCONTACTSROOM);
2377 aptr->roomname = strdup(actual_rm);
2378 aptr->collected_addresses = collected_addresses;
2380 end_critical_section(S_ATBF);
2393 * Convenience function for generating small administrative messages.
2395 void quickie_message(char *from, char *to, char *room, char *text,
2396 int format_type, char *subject)
2398 struct CtdlMessage *msg;
2399 struct recptypes *recp = NULL;
2401 msg = malloc(sizeof(struct CtdlMessage));
2402 memset(msg, 0, sizeof(struct CtdlMessage));
2403 msg->cm_magic = CTDLMESSAGE_MAGIC;
2404 msg->cm_anon_type = MES_NORMAL;
2405 msg->cm_format_type = format_type;
2406 msg->cm_fields['A'] = strdup(from);
2407 if (room != NULL) msg->cm_fields['O'] = strdup(room);
2408 msg->cm_fields['N'] = strdup(NODENAME);
2410 msg->cm_fields['R'] = strdup(to);
2411 recp = validate_recipients(to);
2413 if (subject != NULL) {
2414 msg->cm_fields['U'] = strdup(subject);
2416 msg->cm_fields['M'] = strdup(text);
2418 CtdlSubmitMsg(msg, recp, room);
2419 CtdlFreeMessage(msg);
2420 if (recp != NULL) free(recp);
2426 * Back end function used by CtdlMakeMessage() and similar functions
2428 char *CtdlReadMessageBody(char *terminator, /* token signalling EOT */
2429 size_t maxlen, /* maximum message length */
2430 char *exist, /* if non-null, append to it;
2431 exist is ALWAYS freed */
2432 int crlf /* CRLF newlines instead of LF */
2436 size_t message_len = 0;
2437 size_t buffer_len = 0;
2443 if (exist == NULL) {
2450 message_len = strlen(exist);
2451 buffer_len = message_len + 4096;
2452 m = realloc(exist, buffer_len);
2459 /* flush the input if we have nowhere to store it */
2464 /* read in the lines of message text one by one */
2466 if (client_getln(buf, (sizeof buf - 3)) < 1) finished = 1;
2467 if (!strcmp(buf, terminator)) finished = 1;
2469 strcat(buf, "\r\n");
2475 if ( (!flushing) && (!finished) ) {
2476 /* Measure the line */
2477 linelen = strlen(buf);
2479 /* augment the buffer if we have to */
2480 if ((message_len + linelen) >= buffer_len) {
2481 ptr = realloc(m, (buffer_len * 2) );
2482 if (ptr == NULL) { /* flush if can't allocate */
2485 buffer_len = (buffer_len * 2);
2487 lprintf(CTDL_DEBUG, "buffer_len is now %ld\n", (long)buffer_len);
2491 /* Add the new line to the buffer. NOTE: this loop must avoid
2492 * using functions like strcat() and strlen() because they
2493 * traverse the entire buffer upon every call, and doing that
2494 * for a multi-megabyte message slows it down beyond usability.
2496 strcpy(&m[message_len], buf);
2497 message_len += linelen;
2500 /* if we've hit the max msg length, flush the rest */
2501 if (message_len >= maxlen) flushing = 1;
2503 } while (!finished);
2511 * Build a binary message to be saved on disk.
2512 * (NOTE: if you supply 'preformatted_text', the buffer you give it
2513 * will become part of the message. This means you are no longer
2514 * responsible for managing that memory -- it will be freed along with
2515 * the rest of the fields when CtdlFreeMessage() is called.)
2518 struct CtdlMessage *CtdlMakeMessage(
2519 struct ctdluser *author, /* author's user structure */
2520 char *recipient, /* NULL if it's not mail */
2521 char *recp_cc, /* NULL if it's not mail */
2522 char *room, /* room where it's going */
2523 int type, /* see MES_ types in header file */
2524 int format_type, /* variformat, plain text, MIME... */
2525 char *fake_name, /* who we're masquerading as */
2526 char *subject, /* Subject (optional) */
2527 char *preformatted_text /* ...or NULL to read text from client */
2529 char dest_node[SIZ];
2531 struct CtdlMessage *msg;
2533 msg = malloc(sizeof(struct CtdlMessage));
2534 memset(msg, 0, sizeof(struct CtdlMessage));
2535 msg->cm_magic = CTDLMESSAGE_MAGIC;
2536 msg->cm_anon_type = type;
2537 msg->cm_format_type = format_type;
2539 /* Don't confuse the poor folks if it's not routed mail. */
2540 strcpy(dest_node, "");
2545 snprintf(buf, sizeof buf, "cit%ld", author->usernum); /* Path */
2546 msg->cm_fields['P'] = strdup(buf);
2548 snprintf(buf, sizeof buf, "%ld", (long)time(NULL)); /* timestamp */
2549 msg->cm_fields['T'] = strdup(buf);
2551 if (fake_name[0]) /* author */
2552 msg->cm_fields['A'] = strdup(fake_name);
2554 msg->cm_fields['A'] = strdup(author->fullname);
2556 if (CC->room.QRflags & QR_MAILBOX) { /* room */
2557 msg->cm_fields['O'] = strdup(&CC->room.QRname[11]);
2560 msg->cm_fields['O'] = strdup(CC->room.QRname);
2563 msg->cm_fields['N'] = strdup(NODENAME); /* nodename */
2564 msg->cm_fields['H'] = strdup(HUMANNODE); /* hnodename */
2566 if (recipient[0] != 0) {
2567 msg->cm_fields['R'] = strdup(recipient);
2569 if (recp_cc[0] != 0) {
2570 msg->cm_fields['Y'] = strdup(recp_cc);
2572 if (dest_node[0] != 0) {
2573 msg->cm_fields['D'] = strdup(dest_node);
2576 if ( (author == &CC->user) && (strlen(CC->cs_inet_email) > 0) ) {
2577 msg->cm_fields['F'] = strdup(CC->cs_inet_email);
2580 if (subject != NULL) {
2582 if (strlen(subject) > 0) {
2583 msg->cm_fields['U'] = strdup(subject);
2587 if (preformatted_text != NULL) {
2588 msg->cm_fields['M'] = preformatted_text;
2591 msg->cm_fields['M'] = CtdlReadMessageBody("000",
2592 config.c_maxmsglen, NULL, 0);
2600 * Check to see whether we have permission to post a message in the current
2601 * room. Returns a *CITADEL ERROR CODE* and puts a message in errmsgbuf, or
2602 * returns 0 on success.
2604 int CtdlDoIHavePermissionToPostInThisRoom(char *errmsgbuf, size_t n) {
2606 if (!(CC->logged_in)) {
2607 snprintf(errmsgbuf, n, "Not logged in.");
2608 return (ERROR + NOT_LOGGED_IN);
2611 if ((CC->user.axlevel < 2)
2612 && ((CC->room.QRflags & QR_MAILBOX) == 0)) {
2613 snprintf(errmsgbuf, n, "Need to be validated to enter "
2614 "(except in %s> to sysop)", MAILROOM);
2615 return (ERROR + HIGHER_ACCESS_REQUIRED);
2618 if ((CC->user.axlevel < 4)
2619 && (CC->room.QRflags & QR_NETWORK)) {
2620 snprintf(errmsgbuf, n, "Need net privileges to enter here.");
2621 return (ERROR + HIGHER_ACCESS_REQUIRED);
2624 if ((CC->user.axlevel < 6)
2625 && (CC->room.QRflags & QR_READONLY)) {
2626 snprintf(errmsgbuf, n, "Sorry, this is a read-only room.");
2627 return (ERROR + HIGHER_ACCESS_REQUIRED);
2630 strcpy(errmsgbuf, "Ok");
2636 * Check to see if the specified user has Internet mail permission
2637 * (returns nonzero if permission is granted)
2639 int CtdlCheckInternetMailPermission(struct ctdluser *who) {
2641 /* Do not allow twits to send Internet mail */
2642 if (who->axlevel <= 2) return(0);
2644 /* Globally enabled? */
2645 if (config.c_restrict == 0) return(1);
2647 /* User flagged ok? */
2648 if (who->flags & US_INTERNET) return(2);
2650 /* Aide level access? */
2651 if (who->axlevel >= 6) return(3);
2653 /* No mail for you! */
2659 * Validate recipients, count delivery types and errors, and handle aliasing
2660 * FIXME check for dupes!!!!!
2661 * Returns 0 if all addresses are ok, -1 if no addresses were specified,
2662 * or the number of addresses found invalid.
2664 struct recptypes *validate_recipients(char *recipients) {
2665 struct recptypes *ret;
2666 char this_recp[SIZ];
2667 char this_recp_cooked[SIZ];
2673 struct ctdluser tempUS;
2674 struct ctdlroom tempQR;
2677 ret = (struct recptypes *) malloc(sizeof(struct recptypes));
2678 if (ret == NULL) return(NULL);
2679 memset(ret, 0, sizeof(struct recptypes));
2682 ret->num_internet = 0;
2687 if (recipients == NULL) {
2690 else if (strlen(recipients) == 0) {
2694 /* Change all valid separator characters to commas */
2695 for (i=0; i<strlen(recipients); ++i) {
2696 if ((recipients[i] == ';') || (recipients[i] == '|')) {
2697 recipients[i] = ',';
2702 num_recps = num_tokens(recipients, ',');
2705 if (num_recps > 0) for (i=0; i<num_recps; ++i) {
2706 extract_token(this_recp, recipients, i, ',', sizeof this_recp);
2708 lprintf(CTDL_DEBUG, "Evaluating recipient #%d <%s>\n", i, this_recp);
2709 mailtype = alias(this_recp);
2710 mailtype = alias(this_recp);
2711 mailtype = alias(this_recp);
2712 for (j=0; j<=strlen(this_recp); ++j) {
2713 if (this_recp[j]=='_') {
2714 this_recp_cooked[j] = ' ';
2717 this_recp_cooked[j] = this_recp[j];
2723 if (!strcasecmp(this_recp, "sysop")) {
2725 strcpy(this_recp, config.c_aideroom);
2726 if (strlen(ret->recp_room) > 0) {
2727 strcat(ret->recp_room, "|");
2729 strcat(ret->recp_room, this_recp);
2731 else if (getuser(&tempUS, this_recp) == 0) {
2733 strcpy(this_recp, tempUS.fullname);
2734 if (strlen(ret->recp_local) > 0) {
2735 strcat(ret->recp_local, "|");
2737 strcat(ret->recp_local, this_recp);
2739 else if (getuser(&tempUS, this_recp_cooked) == 0) {
2741 strcpy(this_recp, tempUS.fullname);
2742 if (strlen(ret->recp_local) > 0) {
2743 strcat(ret->recp_local, "|");
2745 strcat(ret->recp_local, this_recp);
2747 else if ( (!strncasecmp(this_recp, "room_", 5))
2748 && (!getroom(&tempQR, &this_recp_cooked[5])) ) {
2750 if (strlen(ret->recp_room) > 0) {
2751 strcat(ret->recp_room, "|");
2753 strcat(ret->recp_room, &this_recp_cooked[5]);
2761 /* Yes, you're reading this correctly: if the target
2762 * domain points back to the local system or an attached
2763 * Citadel directory, the address is invalid. That's
2764 * because if the address were valid, we would have
2765 * already translated it to a local address by now.
2767 if (IsDirectory(this_recp)) {
2772 ++ret->num_internet;
2773 if (strlen(ret->recp_internet) > 0) {
2774 strcat(ret->recp_internet, "|");
2776 strcat(ret->recp_internet, this_recp);
2781 if (strlen(ret->recp_ignet) > 0) {
2782 strcat(ret->recp_ignet, "|");
2784 strcat(ret->recp_ignet, this_recp);
2792 if (strlen(ret->errormsg) == 0) {
2793 snprintf(append, sizeof append,
2794 "Invalid recipient: %s",
2798 snprintf(append, sizeof append,
2801 if ( (strlen(ret->errormsg) + strlen(append)) < SIZ) {
2802 strcat(ret->errormsg, append);
2806 if (strlen(ret->display_recp) == 0) {
2807 strcpy(append, this_recp);
2810 snprintf(append, sizeof append, ", %s",
2813 if ( (strlen(ret->display_recp)+strlen(append)) < SIZ) {
2814 strcat(ret->display_recp, append);
2819 if ((ret->num_local + ret->num_internet + ret->num_ignet +
2820 ret->num_room + ret->num_error) == 0) {
2821 ret->num_error = (-1);
2822 strcpy(ret->errormsg, "No recipients specified.");
2825 lprintf(CTDL_DEBUG, "validate_recipients()\n");
2826 lprintf(CTDL_DEBUG, " local: %d <%s>\n", ret->num_local, ret->recp_local);
2827 lprintf(CTDL_DEBUG, " room: %d <%s>\n", ret->num_room, ret->recp_room);
2828 lprintf(CTDL_DEBUG, " inet: %d <%s>\n", ret->num_internet, ret->recp_internet);
2829 lprintf(CTDL_DEBUG, " ignet: %d <%s>\n", ret->num_ignet, ret->recp_ignet);
2830 lprintf(CTDL_DEBUG, " error: %d <%s>\n", ret->num_error, ret->errormsg);
2838 * message entry - mode 0 (normal)
2840 void cmd_ent0(char *entargs)
2846 char masquerade_as[SIZ];
2848 int format_type = 0;
2849 char newusername[SIZ];
2850 struct CtdlMessage *msg;
2854 struct recptypes *valid = NULL;
2855 struct recptypes *valid_to = NULL;
2856 struct recptypes *valid_cc = NULL;
2857 struct recptypes *valid_bcc = NULL;
2864 post = extract_int(entargs, 0);
2865 extract_token(recp, entargs, 1, '|', sizeof recp);
2866 anon_flag = extract_int(entargs, 2);
2867 format_type = extract_int(entargs, 3);
2868 extract_token(subject, entargs, 4, '|', sizeof subject);
2869 do_confirm = extract_int(entargs, 6);
2870 extract_token(cc, entargs, 7, '|', sizeof cc);
2871 extract_token(bcc, entargs, 8, '|', sizeof bcc);
2873 /* first check to make sure the request is valid. */
2875 err = CtdlDoIHavePermissionToPostInThisRoom(errmsg, sizeof errmsg);
2877 cprintf("%d %s\n", err, errmsg);
2881 /* Check some other permission type things. */
2884 if (CC->user.axlevel < 6) {
2885 cprintf("%d You don't have permission to masquerade.\n",
2886 ERROR + HIGHER_ACCESS_REQUIRED);
2889 extract_token(newusername, entargs, 5, '|', sizeof newusername);
2890 memset(CC->fake_postname, 0, sizeof(CC->fake_postname) );
2891 safestrncpy(CC->fake_postname, newusername,
2892 sizeof(CC->fake_postname) );
2893 cprintf("%d ok\n", CIT_OK);
2896 CC->cs_flags |= CS_POSTING;
2898 /* In the Mail> room we have to behave a little differently --
2899 * make sure the user has specified at least one recipient. Then
2900 * validate the recipient(s).
2902 if ( (CC->room.QRflags & QR_MAILBOX)
2903 && (!strcasecmp(&CC->room.QRname[11], MAILROOM)) ) {
2905 if (CC->user.axlevel < 2) {
2906 strcpy(recp, "sysop");
2911 valid_to = validate_recipients(recp);
2912 if (valid_to->num_error > 0) {
2913 cprintf("%d Invalid recipient (To)\n", ERROR + NO_SUCH_USER);
2918 valid_cc = validate_recipients(cc);
2919 if (valid_cc->num_error > 0) {
2920 cprintf("%d Invalid recipient (CC)\n", ERROR + NO_SUCH_USER);
2926 valid_bcc = validate_recipients(bcc);
2927 if (valid_bcc->num_error > 0) {
2928 cprintf("%d Invalid recipient (BCC)\n", ERROR + NO_SUCH_USER);
2935 /* Recipient required, but none were specified */
2936 if ( (valid_to->num_error < 0) && (valid_cc->num_error < 0) && (valid_bcc->num_error < 0) ) {
2940 cprintf("%d At least one recipient is required.\n", ERROR + NO_SUCH_USER);
2944 if (valid_to->num_internet + valid_cc->num_internet + valid_bcc->num_internet > 0) {
2945 if (CtdlCheckInternetMailPermission(&CC->user)==0) {
2946 cprintf("%d You do not have permission "
2947 "to send Internet mail.\n",
2948 ERROR + HIGHER_ACCESS_REQUIRED);
2956 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)
2957 && (CC->user.axlevel < 4) ) {
2958 cprintf("%d Higher access required for network mail.\n",
2959 ERROR + HIGHER_ACCESS_REQUIRED);
2966 if ((RESTRICT_INTERNET == 1)
2967 && (valid_to->num_internet + valid_cc->num_internet + valid_bcc->num_internet > 0)
2968 && ((CC->user.flags & US_INTERNET) == 0)
2969 && (!CC->internal_pgm)) {
2970 cprintf("%d You don't have access to Internet mail.\n",
2971 ERROR + HIGHER_ACCESS_REQUIRED);
2980 /* Is this a room which has anonymous-only or anonymous-option? */
2981 anonymous = MES_NORMAL;
2982 if (CC->room.QRflags & QR_ANONONLY) {
2983 anonymous = MES_ANONONLY;
2985 if (CC->room.QRflags & QR_ANONOPT) {
2986 if (anon_flag == 1) { /* only if the user requested it */
2987 anonymous = MES_ANONOPT;
2991 if ((CC->room.QRflags & QR_MAILBOX) == 0) {
2995 /* If we're only checking the validity of the request, return
2996 * success without creating the message.
2999 cprintf("%d %s\n", CIT_OK,
3000 ((valid_to != NULL) ? valid_to->display_recp : "") );
3007 /* We don't need these anymore because we'll do it differently below */
3012 /* Handle author masquerading */
3013 if (CC->fake_postname[0]) {
3014 strcpy(masquerade_as, CC->fake_postname);
3016 else if (CC->fake_username[0]) {
3017 strcpy(masquerade_as, CC->fake_username);
3020 strcpy(masquerade_as, "");
3023 /* Read in the message from the client. */
3025 cprintf("%d send message\n", START_CHAT_MODE);
3027 cprintf("%d send message\n", SEND_LISTING);
3030 msg = CtdlMakeMessage(&CC->user, recp, cc,
3031 CC->room.QRname, anonymous, format_type,
3032 masquerade_as, subject, NULL);
3034 /* Put together one big recipients struct containing to/cc/bcc all in
3035 * one. This is for the envelope.
3037 char *all_recps = malloc(SIZ * 3);
3038 strcpy(all_recps, recp);
3039 if (strlen(cc) > 0) {
3040 if (strlen(all_recps) > 0) {
3041 strcat(all_recps, ",");
3043 strcat(all_recps, cc);
3045 if (strlen(bcc) > 0) {
3046 if (strlen(all_recps) > 0) {
3047 strcat(all_recps, ",");
3049 strcat(all_recps, bcc);
3051 if (strlen(all_recps) > 0) {
3052 valid = validate_recipients(all_recps);
3060 msgnum = CtdlSubmitMsg(msg, valid, "");
3063 cprintf("%ld\n", msgnum);
3065 cprintf("Message accepted.\n");
3068 cprintf("Internal error.\n");
3070 if (msg->cm_fields['E'] != NULL) {
3071 cprintf("%s\n", msg->cm_fields['E']);
3078 CtdlFreeMessage(msg);
3080 CC->fake_postname[0] = '\0';
3081 if (valid != NULL) {
3090 * API function to delete messages which match a set of criteria
3091 * (returns the actual number of messages deleted)
3093 int CtdlDeleteMessages(char *room_name, /* which room */
3094 long dmsgnum, /* or "0" for any */
3095 char *content_type, /* or "" for any */
3096 int deferred /* let TDAP sweep it later */
3100 struct ctdlroom qrbuf;
3101 struct cdbdata *cdbfr;
3102 long *msglist = NULL;
3103 long *dellist = NULL;
3106 int num_deleted = 0;
3108 struct MetaData smi;
3110 lprintf(CTDL_DEBUG, "CtdlDeleteMessages(%s, %ld, %s, %d)\n",
3111 room_name, dmsgnum, content_type, deferred);
3113 /* get room record, obtaining a lock... */
3114 if (lgetroom(&qrbuf, room_name) != 0) {
3115 lprintf(CTDL_ERR, "CtdlDeleteMessages(): Room <%s> not found\n",
3117 return (0); /* room not found */
3119 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
3121 if (cdbfr != NULL) {
3122 msglist = malloc(cdbfr->len);
3123 dellist = malloc(cdbfr->len);
3124 memcpy(msglist, cdbfr->ptr, cdbfr->len);
3125 num_msgs = cdbfr->len / sizeof(long);
3129 for (i = 0; i < num_msgs; ++i) {
3132 /* Set/clear a bit for each criterion */
3134 if ((dmsgnum == 0L) || (msglist[i] == dmsgnum)) {
3135 delete_this |= 0x01;
3137 if (strlen(content_type) == 0) {
3138 delete_this |= 0x02;
3140 GetMetaData(&smi, msglist[i]);
3141 if (!strcasecmp(smi.meta_content_type,
3143 delete_this |= 0x02;
3147 /* Delete message only if all bits are set */
3148 if (delete_this == 0x03) {
3149 dellist[num_deleted++] = msglist[i];
3154 num_msgs = sort_msglist(msglist, num_msgs);
3155 cdb_store(CDB_MSGLISTS, &qrbuf.QRnumber, (int)sizeof(long),
3156 msglist, (int)(num_msgs * sizeof(long)));
3158 qrbuf.QRhighest = msglist[num_msgs - 1];
3163 * If the delete operation is "deferred" (and technically, any delete
3164 * operation not performed by THE DREADED AUTO-PURGER ought to be
3165 * a deferred delete) then we save a pointer to the message in the
3166 * DELETED_MSGS_ROOM. This will cause the reference count to remain
3167 * at least 1, which will save the user from having to synchronously
3168 * wait for various disk-intensive operations to complete.
3170 if ( (deferred) && (num_deleted) ) {
3171 for (i=0; i<num_deleted; ++i) {
3172 CtdlCopyMsgToRoom(dellist[i], DELETED_MSGS_ROOM);
3176 /* Go through the messages we pulled out of the index, and decrement
3177 * their reference counts by 1. If this is the only room the message
3178 * was in, the reference count will reach zero and the message will
3179 * automatically be deleted from the database. We do this in a
3180 * separate pass because there might be plug-in hooks getting called,
3181 * and we don't want that happening during an S_ROOMS critical
3184 if (num_deleted) for (i=0; i<num_deleted; ++i) {
3185 PerformDeleteHooks(qrbuf.QRname, dellist[i]);
3186 AdjRefCount(dellist[i], -1);
3189 /* Now free the memory we used, and go away. */
3190 if (msglist != NULL) free(msglist);
3191 if (dellist != NULL) free(dellist);
3192 lprintf(CTDL_DEBUG, "%d message(s) deleted.\n", num_deleted);
3193 return (num_deleted);
3199 * Check whether the current user has permission to delete messages from
3200 * the current room (returns 1 for yes, 0 for no)
3202 int CtdlDoIHavePermissionToDeleteMessagesFromThisRoom(void) {
3203 getuser(&CC->user, CC->curr_user);
3204 if ((CC->user.axlevel < 6)
3205 && (CC->user.usernum != CC->room.QRroomaide)
3206 && ((CC->room.QRflags & QR_MAILBOX) == 0)
3207 && (!(CC->internal_pgm))) {
3216 * Delete message from current room
3218 void cmd_dele(char *delstr)
3223 if (CtdlDoIHavePermissionToDeleteMessagesFromThisRoom() == 0) {
3224 cprintf("%d Higher access required.\n",
3225 ERROR + HIGHER_ACCESS_REQUIRED);
3228 delnum = extract_long(delstr, 0);
3230 num_deleted = CtdlDeleteMessages(CC->room.QRname, delnum, "", 1);
3233 cprintf("%d %d message%s deleted.\n", CIT_OK,
3234 num_deleted, ((num_deleted != 1) ? "s" : ""));
3236 cprintf("%d Message %ld not found.\n", ERROR + MESSAGE_NOT_FOUND, delnum);
3242 * Back end API function for moves and deletes
3244 int CtdlCopyMsgToRoom(long msgnum, char *dest) {
3247 err = CtdlSaveMsgPointerInRoom(dest, msgnum, 1, NULL);
3248 if (err != 0) return(err);
3256 * move or copy a message to another room
3258 void cmd_move(char *args)
3261 char targ[ROOMNAMELEN];
3262 struct ctdlroom qtemp;
3268 num = extract_long(args, 0);
3269 extract_token(targ, args, 1, '|', sizeof targ);
3270 targ[ROOMNAMELEN - 1] = 0;
3271 is_copy = extract_int(args, 2);
3273 if (getroom(&qtemp, targ) != 0) {
3274 cprintf("%d '%s' does not exist.\n",
3275 ERROR + ROOM_NOT_FOUND, targ);
3279 getuser(&CC->user, CC->curr_user);
3280 CtdlRoomAccess(&qtemp, &CC->user, &ra, NULL);
3282 /* Check for permission to perform this operation.
3283 * Remember: "CC->room" is source, "qtemp" is target.
3287 /* Aides can move/copy */
3288 if (CC->user.axlevel >= 6) permit = 1;
3290 /* Room aides can move/copy */
3291 if (CC->user.usernum == CC->room.QRroomaide) permit = 1;
3293 /* Permit move/copy from personal rooms */
3294 if ((CC->room.QRflags & QR_MAILBOX)
3295 && (qtemp.QRflags & QR_MAILBOX)) permit = 1;
3297 /* Permit only copy from public to personal room */
3299 && (!(CC->room.QRflags & QR_MAILBOX))
3300 && (qtemp.QRflags & QR_MAILBOX)) permit = 1;
3302 /* User must have access to target room */
3303 if (!(ra & UA_KNOWN)) permit = 0;
3306 cprintf("%d Higher access required.\n",
3307 ERROR + HIGHER_ACCESS_REQUIRED);
3311 err = CtdlCopyMsgToRoom(num, targ);
3313 cprintf("%d Cannot store message in %s: error %d\n",
3318 /* Now delete the message from the source room,
3319 * if this is a 'move' rather than a 'copy' operation.
3322 CtdlDeleteMessages(CC->room.QRname, num, "", 0);
3325 cprintf("%d Message %s.\n", CIT_OK, (is_copy ? "copied" : "moved") );
3331 * GetMetaData() - Get the supplementary record for a message
3333 void GetMetaData(struct MetaData *smibuf, long msgnum)
3336 struct cdbdata *cdbsmi;
3339 memset(smibuf, 0, sizeof(struct MetaData));
3340 smibuf->meta_msgnum = msgnum;
3341 smibuf->meta_refcount = 1; /* Default reference count is 1 */
3343 /* Use the negative of the message number for its supp record index */
3344 TheIndex = (0L - msgnum);
3346 cdbsmi = cdb_fetch(CDB_MSGMAIN, &TheIndex, sizeof(long));
3347 if (cdbsmi == NULL) {
3348 return; /* record not found; go with defaults */
3350 memcpy(smibuf, cdbsmi->ptr,
3351 ((cdbsmi->len > sizeof(struct MetaData)) ?
3352 sizeof(struct MetaData) : cdbsmi->len));
3359 * PutMetaData() - (re)write supplementary record for a message
3361 void PutMetaData(struct MetaData *smibuf)
3365 /* Use the negative of the message number for the metadata db index */
3366 TheIndex = (0L - smibuf->meta_msgnum);
3368 lprintf(CTDL_DEBUG, "PutMetaData(%ld) - ref count is %d\n",
3369 smibuf->meta_msgnum, smibuf->meta_refcount);
3371 cdb_store(CDB_MSGMAIN,
3372 &TheIndex, (int)sizeof(long),
3373 smibuf, (int)sizeof(struct MetaData));
3378 * AdjRefCount - change the reference count for a message;
3379 * delete the message if it reaches zero
3381 void AdjRefCount(long msgnum, int incr)
3384 struct MetaData smi;
3387 /* This is a *tight* critical section; please keep it that way, as
3388 * it may get called while nested in other critical sections.
3389 * Complicating this any further will surely cause deadlock!
3391 begin_critical_section(S_SUPPMSGMAIN);
3392 GetMetaData(&smi, msgnum);
3393 smi.meta_refcount += incr;
3395 end_critical_section(S_SUPPMSGMAIN);
3396 lprintf(CTDL_DEBUG, "msg %ld ref count incr %d, is now %d\n",
3397 msgnum, incr, smi.meta_refcount);
3399 /* If the reference count is now zero, delete the message
3400 * (and its supplementary record as well).
3402 if (smi.meta_refcount == 0) {
3403 lprintf(CTDL_DEBUG, "Deleting message <%ld>\n", msgnum);
3405 /* Remove from fulltext index */
3406 if (config.c_enable_fulltext) {
3407 ft_index_message(msgnum, 0);
3410 /* Remove from message base */
3412 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3413 cdb_delete(CDB_BIGMSGS, &delnum, (int)sizeof(long));
3415 /* Remove metadata record */
3416 delnum = (0L - msgnum);
3417 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3422 * Write a generic object to this room
3424 * Note: this could be much more efficient. Right now we use two temporary
3425 * files, and still pull the message into memory as with all others.
3427 void CtdlWriteObject(char *req_room, /* Room to stuff it in */
3428 char *content_type, /* MIME type of this object */
3429 char *tempfilename, /* Where to fetch it from */
3430 struct ctdluser *is_mailbox, /* Mailbox room? */
3431 int is_binary, /* Is encoding necessary? */
3432 int is_unique, /* Del others of this type? */
3433 unsigned int flags /* Internal save flags */
3438 struct ctdlroom qrbuf;
3439 char roomname[ROOMNAMELEN];
3440 struct CtdlMessage *msg;
3442 char *raw_message = NULL;
3443 char *encoded_message = NULL;
3444 off_t raw_length = 0;
3446 if (is_mailbox != NULL)
3447 MailboxName(roomname, sizeof roomname, is_mailbox, req_room);
3449 safestrncpy(roomname, req_room, sizeof(roomname));
3450 lprintf(CTDL_DEBUG, "CtdlWriteObject() to <%s> (flags=%d)\n", roomname, flags);
3453 fp = fopen(tempfilename, "rb");
3455 lprintf(CTDL_CRIT, "Cannot open %s: %s\n",
3456 tempfilename, strerror(errno));
3459 fseek(fp, 0L, SEEK_END);
3460 raw_length = ftell(fp);
3462 lprintf(CTDL_DEBUG, "Raw length is %ld\n", (long)raw_length);
3464 raw_message = malloc((size_t)raw_length + 2);
3465 fread(raw_message, (size_t)raw_length, 1, fp);
3469 encoded_message = malloc((size_t)
3470 (((raw_length * 134) / 100) + 4096 ) );
3473 encoded_message = malloc((size_t)(raw_length + 4096));
3476 sprintf(encoded_message, "Content-type: %s\n", content_type);
3479 sprintf(&encoded_message[strlen(encoded_message)],
3480 "Content-transfer-encoding: base64\n\n"
3484 sprintf(&encoded_message[strlen(encoded_message)],
3485 "Content-transfer-encoding: 7bit\n\n"
3491 &encoded_message[strlen(encoded_message)],
3497 raw_message[raw_length] = 0;
3499 &encoded_message[strlen(encoded_message)],
3507 lprintf(CTDL_DEBUG, "Allocating\n");
3508 msg = malloc(sizeof(struct CtdlMessage));
3509 memset(msg, 0, sizeof(struct CtdlMessage));
3510 msg->cm_magic = CTDLMESSAGE_MAGIC;
3511 msg->cm_anon_type = MES_NORMAL;
3512 msg->cm_format_type = 4;
3513 msg->cm_fields['A'] = strdup(CC->user.fullname);
3514 msg->cm_fields['O'] = strdup(req_room);
3515 msg->cm_fields['N'] = strdup(config.c_nodename);
3516 msg->cm_fields['H'] = strdup(config.c_humannode);
3517 msg->cm_flags = flags;
3519 msg->cm_fields['M'] = encoded_message;
3521 /* Create the requested room if we have to. */
3522 if (getroom(&qrbuf, roomname) != 0) {
3523 create_room(roomname,
3524 ( (is_mailbox != NULL) ? 5 : 3 ),
3525 "", 0, 1, 0, VIEW_BBS);
3527 /* If the caller specified this object as unique, delete all
3528 * other objects of this type that are currently in the room.
3531 lprintf(CTDL_DEBUG, "Deleted %d other msgs of this type\n",
3532 CtdlDeleteMessages(roomname, 0L, content_type, 0)
3535 /* Now write the data */
3536 CtdlSubmitMsg(msg, NULL, roomname);
3537 CtdlFreeMessage(msg);
3545 void CtdlGetSysConfigBackend(long msgnum, void *userdata) {
3546 config_msgnum = msgnum;
3550 char *CtdlGetSysConfig(char *sysconfname) {
3551 char hold_rm[ROOMNAMELEN];
3554 struct CtdlMessage *msg;
3557 strcpy(hold_rm, CC->room.QRname);
3558 if (getroom(&CC->room, SYSCONFIGROOM) != 0) {
3559 getroom(&CC->room, hold_rm);
3564 /* We want the last (and probably only) config in this room */
3565 begin_critical_section(S_CONFIG);
3566 config_msgnum = (-1L);
3567 CtdlForEachMessage(MSGS_LAST, 1, sysconfname, NULL,
3568 CtdlGetSysConfigBackend, NULL);
3569 msgnum = config_msgnum;
3570 end_critical_section(S_CONFIG);
3576 msg = CtdlFetchMessage(msgnum, 1);
3578 conf = strdup(msg->cm_fields['M']);
3579 CtdlFreeMessage(msg);
3586 getroom(&CC->room, hold_rm);
3588 if (conf != NULL) do {
3589 extract_token(buf, conf, 0, '\n', sizeof buf);
3590 strcpy(conf, &conf[strlen(buf)+1]);
3591 } while ( (strlen(conf)>0) && (strlen(buf)>0) );
3596 void CtdlPutSysConfig(char *sysconfname, char *sysconfdata) {
3597 char temp[PATH_MAX];
3600 strcpy(temp, tmpnam(NULL));
3602 fp = fopen(temp, "w");
3603 if (fp == NULL) return;
3604 fprintf(fp, "%s", sysconfdata);
3607 /* this handy API function does all the work for us */
3608 CtdlWriteObject(SYSCONFIGROOM, sysconfname, temp, NULL, 0, 1, 0);
3614 * Determine whether a given Internet address belongs to the current user
3616 int CtdlIsMe(char *addr, int addr_buf_len)
3618 struct recptypes *recp;
3621 recp = validate_recipients(addr);
3622 if (recp == NULL) return(0);
3624 if (recp->num_local == 0) {
3629 for (i=0; i<recp->num_local; ++i) {
3630 extract_token(addr, recp->recp_local, i, '|', addr_buf_len);
3631 if (!strcasecmp(addr, CC->user.fullname)) {
3643 * Citadel protocol command to do the same
3645 void cmd_isme(char *argbuf) {
3648 if (CtdlAccessCheck(ac_logged_in)) return;
3649 extract_token(addr, argbuf, 0, '|', sizeof addr);
3651 if (CtdlIsMe(addr, sizeof addr)) {
3652 cprintf("%d %s\n", CIT_OK, addr);
3655 cprintf("%d Not you.\n", ERROR + ILLEGAL_VALUE);