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"
61 * This really belongs in serv_network.c, but I don't know how to export
62 * symbols between modules.
64 struct FilterList *filterlist = NULL;
68 * These are the four-character field headers we use when outputting
69 * messages in Citadel format (as opposed to RFC822 format).
72 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
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,
106 * This function is self explanatory.
107 * (What can I say, I'm in a weird mood today...)
109 void remove_any_whitespace_to_the_left_or_right_of_at_symbol(char *name)
113 for (i = 0; i < strlen(name); ++i) {
114 if (name[i] == '@') {
115 while (isspace(name[i - 1]) && i > 0) {
116 strcpy(&name[i - 1], &name[i]);
119 while (isspace(name[i + 1])) {
120 strcpy(&name[i + 1], &name[i + 2]);
128 * Aliasing for network mail.
129 * (Error messages have been commented out, because this is a server.)
131 int alias(char *name)
132 { /* process alias and routing info for mail */
135 char aaa[SIZ], bbb[SIZ];
136 char *ignetcfg = NULL;
137 char *ignetmap = NULL;
144 remove_any_whitespace_to_the_left_or_right_of_at_symbol(name);
152 "mail.aliases", "r");
154 fp = fopen("/dev/null", "r");
161 while (fgets(aaa, sizeof aaa, fp) != NULL) {
162 while (isspace(name[0]))
163 strcpy(name, &name[1]);
164 aaa[strlen(aaa) - 1] = 0;
166 for (a = 0; a < strlen(aaa); ++a) {
168 strcpy(bbb, &aaa[a + 1]);
172 if (!strcasecmp(name, aaa))
177 /* Hit the Global Address Book */
178 if (CtdlDirectoryLookup(aaa, name, sizeof aaa) == 0) {
182 lprintf(CTDL_INFO, "Mail is being forwarded to %s\n", name);
184 /* Change "user @ xxx" to "user" if xxx is an alias for this host */
185 for (a=0; a<strlen(name); ++a) {
186 if (name[a] == '@') {
187 if (CtdlHostAlias(&name[a+1]) == hostalias_localhost) {
189 lprintf(CTDL_INFO, "Changed to <%s>\n", name);
194 /* determine local or remote type, see citadel.h */
195 at = haschar(name, '@');
196 if (at == 0) return(MES_LOCAL); /* no @'s - local address */
197 if (at > 1) return(MES_ERROR); /* >1 @'s - invalid address */
198 remove_any_whitespace_to_the_left_or_right_of_at_symbol(name);
200 /* figure out the delivery mode */
201 extract_token(node, name, 1, '@', sizeof node);
203 /* If there are one or more dots in the nodename, we assume that it
204 * is an FQDN and will attempt SMTP delivery to the Internet.
206 if (haschar(node, '.') > 0) {
207 return(MES_INTERNET);
210 /* Otherwise we look in the IGnet maps for a valid Citadel node.
211 * Try directly-connected nodes first...
213 ignetcfg = CtdlGetSysConfig(IGNETCFG);
214 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
215 extract_token(buf, ignetcfg, i, '\n', sizeof buf);
216 extract_token(testnode, buf, 0, '|', sizeof testnode);
217 if (!strcasecmp(node, testnode)) {
225 * Then try nodes that are two or more hops away.
227 ignetmap = CtdlGetSysConfig(IGNETMAP);
228 for (i=0; i<num_tokens(ignetmap, '\n'); ++i) {
229 extract_token(buf, ignetmap, i, '\n', sizeof buf);
230 extract_token(testnode, buf, 0, '|', sizeof testnode);
231 if (!strcasecmp(node, testnode)) {
238 /* If we get to this point it's an invalid node name */
253 "/citadel.control", "r");
255 lprintf(CTDL_CRIT, "Cannot open citadel.control: %s\n",
259 fread((char *) &CitControl, sizeof(struct CitControl), 1, fp);
265 * Back end for the MSGS command: output message number only.
267 void simple_listing(long msgnum, void *userdata)
269 cprintf("%ld\n", msgnum);
275 * Back end for the MSGS command: output header summary.
277 void headers_listing(long msgnum, void *userdata)
279 struct CtdlMessage *msg;
281 msg = CtdlFetchMessage(msgnum, 0);
283 cprintf("%ld|0|||||\n", msgnum);
287 cprintf("%ld|%s|%s|%s|%s|%s|\n",
289 (msg->cm_fields['T'] ? msg->cm_fields['T'] : "0"),
290 (msg->cm_fields['A'] ? msg->cm_fields['A'] : ""),
291 (msg->cm_fields['N'] ? msg->cm_fields['N'] : ""),
292 (msg->cm_fields['F'] ? msg->cm_fields['F'] : ""),
293 (msg->cm_fields['U'] ? msg->cm_fields['U'] : "")
295 CtdlFreeMessage(msg);
300 /* Determine if a given message matches the fields in a message template.
301 * Return 0 for a successful match.
303 int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template) {
306 /* If there aren't any fields in the template, all messages will
309 if (template == NULL) return(0);
311 /* Null messages are bogus. */
312 if (msg == NULL) return(1);
314 for (i='A'; i<='Z'; ++i) {
315 if (template->cm_fields[i] != NULL) {
316 if (msg->cm_fields[i] == NULL) {
319 if (strcasecmp(msg->cm_fields[i],
320 template->cm_fields[i])) return 1;
324 /* All compares succeeded: we have a match! */
331 * Retrieve the "seen" message list for the current room.
333 void CtdlGetSeen(char *buf, int which_set) {
336 /* Learn about the user and room in question */
337 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
339 if (which_set == ctdlsetseen_seen)
340 safestrncpy(buf, vbuf.v_seen, SIZ);
341 if (which_set == ctdlsetseen_answered)
342 safestrncpy(buf, vbuf.v_answered, SIZ);
348 * Manipulate the "seen msgs" string (or other message set strings)
350 void CtdlSetSeen(long target_msgnum, int target_setting, int which_set,
351 struct ctdluser *which_user, struct ctdlroom *which_room) {
352 struct cdbdata *cdbfr;
364 char *is_set; /* actually an array of booleans */
367 char setstr[SIZ], lostr[SIZ], histr[SIZ];
370 lprintf(CTDL_DEBUG, "CtdlSetSeen(%ld, %d, %d)\n",
371 target_msgnum, target_setting, which_set);
373 /* Learn about the user and room in question */
374 CtdlGetRelationship(&vbuf,
375 ((which_user != NULL) ? which_user : &CC->user),
376 ((which_room != NULL) ? which_room : &CC->room)
379 /* Load the message list */
380 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
382 msglist = malloc(cdbfr->len);
383 memcpy(msglist, cdbfr->ptr, cdbfr->len);
384 num_msgs = cdbfr->len / sizeof(long);
387 return; /* No messages at all? No further action. */
390 is_set = malloc(num_msgs * sizeof(char));
391 memset(is_set, 0, (num_msgs * sizeof(char)) );
393 /* Decide which message set we're manipulating */
395 case ctdlsetseen_seen:
396 safestrncpy(vset, vbuf.v_seen, sizeof vset);
398 case ctdlsetseen_answered:
399 safestrncpy(vset, vbuf.v_answered, sizeof vset);
403 /* lprintf(CTDL_DEBUG, "before optimize: %s\n", vset); */
405 /* Translate the existing sequence set into an array of booleans */
406 num_sets = num_tokens(vset, ',');
407 for (s=0; s<num_sets; ++s) {
408 extract_token(setstr, vset, s, ',', sizeof setstr);
410 extract_token(lostr, setstr, 0, ':', sizeof lostr);
411 if (num_tokens(setstr, ':') >= 2) {
412 extract_token(histr, setstr, 1, ':', sizeof histr);
413 if (!strcmp(histr, "*")) {
414 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
418 strcpy(histr, lostr);
423 for (i = 0; i < num_msgs; ++i) {
424 if ((msglist[i] >= lo) && (msglist[i] <= hi)) {
430 /* Now translate the array of booleans back into a sequence set */
435 for (i=0; i<num_msgs; ++i) {
438 if (msglist[i] == target_msgnum) {
439 is_seen = target_setting;
446 if (lo < 0L) lo = msglist[i];
450 if ( ((is_seen == 0) && (was_seen == 1))
451 || ((is_seen == 1) && (i == num_msgs-1)) ) {
453 /* begin trim-o-matic code */
456 while ( (strlen(vset) + 20) > sizeof vset) {
457 remove_token(vset, 0, ',');
459 if (j--) break; /* loop no more than 9 times */
461 if ( (trimming) && (which_set == ctdlsetseen_seen) ) {
465 snprintf(lostr, sizeof lostr,
466 "1:%ld,%s", t, vset);
467 safestrncpy(vset, lostr, sizeof vset);
469 /* end trim-o-matic code */
477 snprintf(&vset[tmp], (sizeof vset) - tmp,
481 snprintf(&vset[tmp], (sizeof vset) - tmp,
490 /* Decide which message set we're manipulating */
492 case ctdlsetseen_seen:
493 safestrncpy(vbuf.v_seen, vset, sizeof vbuf.v_seen);
495 case ctdlsetseen_answered:
496 safestrncpy(vbuf.v_answered, vset,
497 sizeof vbuf.v_answered);
502 /* lprintf(CTDL_DEBUG, " after optimize: %s\n", vset); */
504 CtdlSetRelationship(&vbuf,
505 ((which_user != NULL) ? which_user : &CC->user),
506 ((which_room != NULL) ? which_room : &CC->room)
512 * API function to perform an operation for each qualifying message in the
513 * current room. (Returns the number of messages processed.)
515 int CtdlForEachMessage(int mode, long ref,
517 struct CtdlMessage *compare,
518 void (*CallBack) (long, void *),
524 struct cdbdata *cdbfr;
525 long *msglist = NULL;
527 int num_processed = 0;
530 struct CtdlMessage *msg;
533 int printed_lastold = 0;
535 /* Learn about the user and room in question */
537 getuser(&CC->user, CC->curr_user);
538 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
540 /* Load the message list */
541 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
543 msglist = malloc(cdbfr->len);
544 memcpy(msglist, cdbfr->ptr, cdbfr->len);
545 num_msgs = cdbfr->len / sizeof(long);
548 return 0; /* No messages at all? No further action. */
553 * Now begin the traversal.
555 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
557 /* If the caller is looking for a specific MIME type, filter
558 * out all messages which are not of the type requested.
560 if (content_type != NULL) if (strlen(content_type) > 0) {
562 /* This call to GetMetaData() sits inside this loop
563 * so that we only do the extra database read per msg
564 * if we need to. Doing the extra read all the time
565 * really kills the server. If we ever need to use
566 * metadata for another search criterion, we need to
567 * move the read somewhere else -- but still be smart
568 * enough to only do the read if the caller has
569 * specified something that will need it.
571 GetMetaData(&smi, msglist[a]);
573 if (strcasecmp(smi.meta_content_type, content_type)) {
579 num_msgs = sort_msglist(msglist, num_msgs);
581 /* If a template was supplied, filter out the messages which
582 * don't match. (This could induce some delays!)
585 if (compare != NULL) {
586 for (a = 0; a < num_msgs; ++a) {
587 msg = CtdlFetchMessage(msglist[a], 1);
589 if (CtdlMsgCmp(msg, compare)) {
592 CtdlFreeMessage(msg);
600 * Now iterate through the message list, according to the
601 * criteria supplied by the caller.
604 for (a = 0; a < num_msgs; ++a) {
605 thismsg = msglist[a];
606 if (mode == MSGS_ALL) {
610 is_seen = is_msg_in_sequence_set(
611 vbuf.v_seen, thismsg);
612 if (is_seen) lastold = thismsg;
618 || ((mode == MSGS_OLD) && (is_seen))
619 || ((mode == MSGS_NEW) && (!is_seen))
620 || ((mode == MSGS_LAST) && (a >= (num_msgs - ref)))
621 || ((mode == MSGS_FIRST) && (a < ref))
622 || ((mode == MSGS_GT) && (thismsg > ref))
623 || ((mode == MSGS_EQ) && (thismsg == ref))
626 if ((mode == MSGS_NEW) && (CC->user.flags & US_LASTOLD) && (lastold > 0L) && (printed_lastold == 0) && (!is_seen)) {
628 CallBack(lastold, userdata);
632 if (CallBack) CallBack(thismsg, userdata);
636 free(msglist); /* Clean up */
637 return num_processed;
643 * cmd_msgs() - get list of message #'s in this room
644 * implements the MSGS server command using CtdlForEachMessage()
646 void cmd_msgs(char *cmdbuf)
655 int with_template = 0;
656 struct CtdlMessage *template = NULL;
657 int with_headers = 0;
659 extract_token(which, cmdbuf, 0, '|', sizeof which);
660 cm_ref = extract_int(cmdbuf, 1);
661 with_template = extract_int(cmdbuf, 2);
662 with_headers = extract_int(cmdbuf, 3);
666 if (!strncasecmp(which, "OLD", 3))
668 else if (!strncasecmp(which, "NEW", 3))
670 else if (!strncasecmp(which, "FIRST", 5))
672 else if (!strncasecmp(which, "LAST", 4))
674 else if (!strncasecmp(which, "GT", 2))
677 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
678 cprintf("%d not logged in\n", ERROR + NOT_LOGGED_IN);
684 cprintf("%d Send template then receive message list\n",
686 template = (struct CtdlMessage *)
687 malloc(sizeof(struct CtdlMessage));
688 memset(template, 0, sizeof(struct CtdlMessage));
689 while(client_getln(buf, sizeof buf), strcmp(buf,"000")) {
690 extract_token(tfield, buf, 0, '|', sizeof tfield);
691 extract_token(tvalue, buf, 1, '|', sizeof tvalue);
692 for (i='A'; i<='Z'; ++i) if (msgkeys[i]!=NULL) {
693 if (!strcasecmp(tfield, msgkeys[i])) {
694 template->cm_fields[i] =
702 cprintf("%d \n", LISTING_FOLLOWS);
705 CtdlForEachMessage(mode,
709 (with_headers ? headers_listing : simple_listing),
712 if (template != NULL) CtdlFreeMessage(template);
720 * help_subst() - support routine for help file viewer
722 void help_subst(char *strbuf, char *source, char *dest)
727 while (p = pattern2(strbuf, source), (p >= 0)) {
728 strcpy(workbuf, &strbuf[p + strlen(source)]);
729 strcpy(&strbuf[p], dest);
730 strcat(strbuf, workbuf);
735 void do_help_subst(char *buffer)
739 help_subst(buffer, "^nodename", config.c_nodename);
740 help_subst(buffer, "^humannode", config.c_humannode);
741 help_subst(buffer, "^fqdn", config.c_fqdn);
742 help_subst(buffer, "^username", CC->user.fullname);
743 snprintf(buf2, sizeof buf2, "%ld", CC->user.usernum);
744 help_subst(buffer, "^usernum", buf2);
745 help_subst(buffer, "^sysadm", config.c_sysadm);
746 help_subst(buffer, "^variantname", CITADEL);
747 snprintf(buf2, sizeof buf2, "%d", config.c_maxsessions);
748 help_subst(buffer, "^maxsessions", buf2);
749 help_subst(buffer, "^bbsdir", CTDLDIR);
755 * memfmout() - Citadel text formatter and paginator.
756 * Although the original purpose of this routine was to format
757 * text to the reader's screen width, all we're really using it
758 * for here is to format text out to 80 columns before sending it
759 * to the client. The client software may reformat it again.
762 int width, /* screen width to use */
763 char *mptr, /* where are we going to get our text from? */
764 char subst, /* nonzero if we should do substitutions */
765 char *nl) /* string to terminate lines with */
777 c = 1; /* c is the current pos */
781 while (ch = *mptr, ((ch != 0) && (strlen(buffer) < 126))) {
783 buffer[strlen(buffer) + 1] = 0;
784 buffer[strlen(buffer)] = ch;
787 if (buffer[0] == '^')
788 do_help_subst(buffer);
790 buffer[strlen(buffer) + 1] = 0;
792 strcpy(buffer, &buffer[1]);
800 if (((ch == 13) || (ch == 10)) && (old != 13) && (old != 10))
802 if (((old == 13) || (old == 10)) && (isspace(real))) {
810 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
811 cprintf("%s%s", nl, aaa);
820 if ((strlen(aaa) + c) > (width - 5)) {
829 if ((ch == 13) || (ch == 10)) {
830 cprintf("%s%s", aaa, nl);
837 cprintf("%s%s", aaa, nl);
843 * Callback function for mime parser that simply lists the part
845 void list_this_part(char *name, char *filename, char *partnum, char *disp,
846 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
851 ma = (struct ma_info *)cbuserdata;
852 if (ma->is_ma == 0) {
853 cprintf("part=%s|%s|%s|%s|%s|%ld\n",
854 name, filename, partnum, disp, cbtype, (long)length);
859 * Callback function for multipart prefix
861 void list_this_pref(char *name, char *filename, char *partnum, char *disp,
862 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
867 ma = (struct ma_info *)cbuserdata;
868 if (!strcasecmp(cbtype, "multipart/alternative")) {
872 if (ma->is_ma == 0) {
873 cprintf("pref=%s|%s\n", partnum, cbtype);
878 * Callback function for multipart sufffix
880 void list_this_suff(char *name, char *filename, char *partnum, char *disp,
881 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
886 ma = (struct ma_info *)cbuserdata;
887 if (ma->is_ma == 0) {
888 cprintf("suff=%s|%s\n", partnum, cbtype);
890 if (!strcasecmp(cbtype, "multipart/alternative")) {
897 * Callback function for mime parser that opens a section for downloading
899 void mime_download(char *name, char *filename, char *partnum, char *disp,
900 void *content, char *cbtype, char *cbcharset, size_t length,
901 char *encoding, void *cbuserdata)
904 /* Silently go away if there's already a download open... */
905 if (CC->download_fp != NULL)
908 /* ...or if this is not the desired section */
909 if (strcasecmp(CC->download_desired_section, partnum))
912 CC->download_fp = tmpfile();
913 if (CC->download_fp == NULL)
916 fwrite(content, length, 1, CC->download_fp);
917 fflush(CC->download_fp);
918 rewind(CC->download_fp);
920 OpenCmdResult(filename, cbtype);
926 * Load a message from disk into memory.
927 * This is used by CtdlOutputMsg() and other fetch functions.
929 * NOTE: Caller is responsible for freeing the returned CtdlMessage struct
930 * using the CtdlMessageFree() function.
932 struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body)
934 struct cdbdata *dmsgtext;
935 struct CtdlMessage *ret = NULL;
939 cit_uint8_t field_header;
941 lprintf(CTDL_DEBUG, "CtdlFetchMessage(%ld, %d)\n", msgnum, with_body);
943 dmsgtext = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
944 if (dmsgtext == NULL) {
947 mptr = dmsgtext->ptr;
948 upper_bound = mptr + dmsgtext->len;
950 /* Parse the three bytes that begin EVERY message on disk.
951 * The first is always 0xFF, the on-disk magic number.
952 * The second is the anonymous/public type byte.
953 * The third is the format type byte (vari, fixed, or MIME).
958 "Message %ld appears to be corrupted.\n",
963 ret = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
964 memset(ret, 0, sizeof(struct CtdlMessage));
966 ret->cm_magic = CTDLMESSAGE_MAGIC;
967 ret->cm_anon_type = *mptr++; /* Anon type byte */
968 ret->cm_format_type = *mptr++; /* Format type byte */
971 * The rest is zero or more arbitrary fields. Load them in.
972 * We're done when we encounter either a zero-length field or
973 * have just processed the 'M' (message text) field.
976 if (mptr >= upper_bound) {
979 field_header = *mptr++;
980 ret->cm_fields[field_header] = strdup(mptr);
982 while (*mptr++ != 0); /* advance to next field */
984 } while ((mptr < upper_bound) && (field_header != 'M'));
988 /* Always make sure there's something in the msg text field. If
989 * it's NULL, the message text is most likely stored separately,
990 * so go ahead and fetch that. Failing that, just set a dummy
991 * body so other code doesn't barf.
993 if ( (ret->cm_fields['M'] == NULL) && (with_body) ) {
994 dmsgtext = cdb_fetch(CDB_BIGMSGS, &msgnum, sizeof(long));
995 if (dmsgtext != NULL) {
996 ret->cm_fields['M'] = strdup(dmsgtext->ptr);
1000 if (ret->cm_fields['M'] == NULL) {
1001 ret->cm_fields['M'] = strdup("<no text>\n");
1004 /* Perform "before read" hooks (aborting if any return nonzero) */
1005 if (PerformMessageHooks(ret, EVT_BEFOREREAD) > 0) {
1006 CtdlFreeMessage(ret);
1015 * Returns 1 if the supplied pointer points to a valid Citadel message.
1016 * If the pointer is NULL or the magic number check fails, returns 0.
1018 int is_valid_message(struct CtdlMessage *msg) {
1021 if ((msg->cm_magic) != CTDLMESSAGE_MAGIC) {
1022 lprintf(CTDL_WARNING, "is_valid_message() -- self-check failed\n");
1030 * 'Destructor' for struct CtdlMessage
1032 void CtdlFreeMessage(struct CtdlMessage *msg)
1036 if (is_valid_message(msg) == 0) return;
1038 for (i = 0; i < 256; ++i)
1039 if (msg->cm_fields[i] != NULL) {
1040 free(msg->cm_fields[i]);
1043 msg->cm_magic = 0; /* just in case */
1049 * Pre callback function for multipart/alternative
1051 * NOTE: this differs from the standard behavior for a reason. Normally when
1052 * displaying multipart/alternative you want to show the _last_ usable
1053 * format in the message. Here we show the _first_ one, because it's
1054 * usually text/plain. Since this set of functions is designed for text
1055 * output to non-MIME-aware clients, this is the desired behavior.
1058 void fixed_output_pre(char *name, char *filename, char *partnum, char *disp,
1059 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1064 ma = (struct ma_info *)cbuserdata;
1065 lprintf(CTDL_DEBUG, "fixed_output_pre() type=<%s>\n", cbtype);
1066 if (!strcasecmp(cbtype, "multipart/alternative")) {
1074 * Post callback function for multipart/alternative
1076 void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
1077 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1082 ma = (struct ma_info *)cbuserdata;
1083 lprintf(CTDL_DEBUG, "fixed_output_post() type=<%s>\n", cbtype);
1084 if (!strcasecmp(cbtype, "multipart/alternative")) {
1092 * Inline callback function for mime parser that wants to display text
1094 void fixed_output(char *name, char *filename, char *partnum, char *disp,
1095 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1103 ma = (struct ma_info *)cbuserdata;
1105 lprintf(CTDL_DEBUG, "fixed_output() type=<%s>\n", cbtype);
1108 * If we're in the middle of a multipart/alternative scope and
1109 * we've already printed another section, skip this one.
1111 if ( (ma->is_ma == 1) && (ma->did_print == 1) ) {
1112 lprintf(CTDL_DEBUG, "Skipping part %s (%s)\n", partnum, cbtype);
1117 if ( (!strcasecmp(cbtype, "text/plain"))
1118 || (strlen(cbtype)==0) ) {
1121 client_write(wptr, length);
1122 if (wptr[length-1] != '\n') {
1127 else if (!strcasecmp(cbtype, "text/html")) {
1128 ptr = html_to_ascii(content, 80, 0);
1130 client_write(ptr, wlen);
1131 if (ptr[wlen-1] != '\n') {
1136 else if (strncasecmp(cbtype, "multipart/", 10)) {
1137 cprintf("Part %s: %s (%s) (%ld bytes)\r\n",
1138 partnum, filename, cbtype, (long)length);
1143 * The client is elegant and sophisticated and wants to be choosy about
1144 * MIME content types, so figure out which multipart/alternative part
1145 * we're going to send.
1147 void choose_preferred(char *name, char *filename, char *partnum, char *disp,
1148 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1155 ma = (struct ma_info *)cbuserdata;
1157 if (ma->is_ma > 0) {
1158 for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
1159 extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
1160 if (!strcasecmp(buf, cbtype)) {
1161 strcpy(ma->chosen_part, partnum);
1168 * Now that we've chosen our preferred part, output it.
1170 void output_preferred(char *name, char *filename, char *partnum, char *disp,
1171 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1176 int add_newline = 0;
1180 ma = (struct ma_info *)cbuserdata;
1182 /* This is not the MIME part you're looking for... */
1183 if (strcasecmp(partnum, ma->chosen_part)) return;
1185 /* If the content-type of this part is in our preferred formats
1186 * list, we can simply output it verbatim.
1188 for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
1189 extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
1190 if (!strcasecmp(buf, cbtype)) {
1191 /* Yeah! Go! W00t!! */
1193 text_content = (char *)content;
1194 if (text_content[length-1] != '\n') {
1198 cprintf("Content-type: %s", cbtype);
1199 if (strlen(cbcharset) > 0) {
1200 cprintf("; charset=%s", cbcharset);
1202 cprintf("\nContent-length: %d\n",
1203 (int)(length + add_newline) );
1204 if (strlen(encoding) > 0) {
1205 cprintf("Content-transfer-encoding: %s\n", encoding);
1208 cprintf("Content-transfer-encoding: 7bit\n");
1211 client_write(content, length);
1212 if (add_newline) cprintf("\n");
1217 /* No translations required or possible: output as text/plain */
1218 cprintf("Content-type: text/plain\n\n");
1219 fixed_output(name, filename, partnum, disp, content, cbtype, cbcharset,
1220 length, encoding, cbuserdata);
1225 * Get a message off disk. (returns om_* values found in msgbase.h)
1228 int CtdlOutputMsg(long msg_num, /* message number (local) to fetch */
1229 int mode, /* how would you like that message? */
1230 int headers_only, /* eschew the message body? */
1231 int do_proto, /* do Citadel protocol responses? */
1232 int crlf /* Use CRLF newlines instead of LF? */
1234 struct CtdlMessage *TheMessage = NULL;
1235 int retcode = om_no_such_msg;
1237 lprintf(CTDL_DEBUG, "CtdlOutputMsg() msgnum=%ld, mode=%d\n",
1240 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1241 if (do_proto) cprintf("%d Not logged in.\n",
1242 ERROR + NOT_LOGGED_IN);
1243 return(om_not_logged_in);
1246 /* FIXME: check message id against msglist for this room */
1249 * Fetch the message from disk. If we're in any sort of headers
1250 * only mode, request that we don't even bother loading the body
1253 if ( (headers_only == HEADERS_FAST) || (headers_only == HEADERS_ONLY) ) {
1254 TheMessage = CtdlFetchMessage(msg_num, 0);
1257 TheMessage = CtdlFetchMessage(msg_num, 1);
1260 if (TheMessage == NULL) {
1261 if (do_proto) cprintf("%d Can't locate msg %ld on disk\n",
1262 ERROR + MESSAGE_NOT_FOUND, msg_num);
1263 return(om_no_such_msg);
1266 retcode = CtdlOutputPreLoadedMsg(
1267 TheMessage, msg_num, mode,
1268 headers_only, do_proto, crlf);
1270 CtdlFreeMessage(TheMessage);
1277 * Get a message off disk. (returns om_* values found in msgbase.h)
1280 int CtdlOutputPreLoadedMsg(
1281 struct CtdlMessage *TheMessage,
1283 int mode, /* how would you like that message? */
1284 int headers_only, /* eschew the message body? */
1285 int do_proto, /* do Citadel protocol responses? */
1286 int crlf /* Use CRLF newlines instead of LF? */
1292 char display_name[256];
1294 char *nl; /* newline string */
1296 int subject_found = 0;
1299 /* Buffers needed for RFC822 translation. These are all filled
1300 * using functions that are bounds-checked, and therefore we can
1301 * make them substantially smaller than SIZ.
1309 char datestamp[100];
1311 lprintf(CTDL_DEBUG, "CtdlOutputPreLoadedMsg(TheMessage=%s, %ld, %d, %d, %d, %d\n",
1312 ((TheMessage == NULL) ? "NULL" : "not null"),
1314 mode, headers_only, do_proto, crlf);
1316 snprintf(mid, sizeof mid, "%ld", msg_num);
1317 nl = (crlf ? "\r\n" : "\n");
1319 if (!is_valid_message(TheMessage)) {
1321 "ERROR: invalid preloaded message for output\n");
1322 return(om_no_such_msg);
1325 /* Are we downloading a MIME component? */
1326 if (mode == MT_DOWNLOAD) {
1327 if (TheMessage->cm_format_type != FMT_RFC822) {
1329 cprintf("%d This is not a MIME message.\n",
1330 ERROR + ILLEGAL_VALUE);
1331 } else if (CC->download_fp != NULL) {
1332 if (do_proto) cprintf(
1333 "%d You already have a download open.\n",
1334 ERROR + RESOURCE_BUSY);
1336 /* Parse the message text component */
1337 mptr = TheMessage->cm_fields['M'];
1338 mime_parser(mptr, NULL, *mime_download, NULL, NULL, NULL, 0);
1339 /* If there's no file open by this time, the requested
1340 * section wasn't found, so print an error
1342 if (CC->download_fp == NULL) {
1343 if (do_proto) cprintf(
1344 "%d Section %s not found.\n",
1345 ERROR + FILE_NOT_FOUND,
1346 CC->download_desired_section);
1349 return((CC->download_fp != NULL) ? om_ok : om_mime_error);
1352 /* now for the user-mode message reading loops */
1353 if (do_proto) cprintf("%d Message %ld:\n", LISTING_FOLLOWS, msg_num);
1355 /* Does the caller want to skip the headers? */
1356 if (headers_only == HEADERS_NONE) goto START_TEXT;
1358 /* Tell the client which format type we're using. */
1359 if ( (mode == MT_CITADEL) && (do_proto) ) {
1360 cprintf("type=%d\n", TheMessage->cm_format_type);
1363 /* nhdr=yes means that we're only displaying headers, no body */
1364 if ( (TheMessage->cm_anon_type == MES_ANONONLY)
1365 && (mode == MT_CITADEL)
1368 cprintf("nhdr=yes\n");
1371 /* begin header processing loop for Citadel message format */
1373 if ((mode == MT_CITADEL) || (mode == MT_MIME)) {
1375 safestrncpy(display_name, "<unknown>", sizeof display_name);
1376 if (TheMessage->cm_fields['A']) {
1377 strcpy(buf, TheMessage->cm_fields['A']);
1378 if (TheMessage->cm_anon_type == MES_ANONONLY) {
1379 safestrncpy(display_name, "****", sizeof display_name);
1381 else if (TheMessage->cm_anon_type == MES_ANONOPT) {
1382 safestrncpy(display_name, "anonymous", sizeof display_name);
1385 safestrncpy(display_name, buf, sizeof display_name);
1387 if ((is_room_aide())
1388 && ((TheMessage->cm_anon_type == MES_ANONONLY)
1389 || (TheMessage->cm_anon_type == MES_ANONOPT))) {
1390 size_t tmp = strlen(display_name);
1391 snprintf(&display_name[tmp],
1392 sizeof display_name - tmp,
1397 /* Don't show Internet address for users on the
1398 * local Citadel network.
1401 if (TheMessage->cm_fields['N'] != NULL)
1402 if (strlen(TheMessage->cm_fields['N']) > 0)
1403 if (haschar(TheMessage->cm_fields['N'], '.') == 0) {
1407 /* Now spew the header fields in the order we like them. */
1408 safestrncpy(allkeys, FORDER, sizeof allkeys);
1409 for (i=0; i<strlen(allkeys); ++i) {
1410 k = (int) allkeys[i];
1412 if ( (TheMessage->cm_fields[k] != NULL)
1413 && (msgkeys[k] != NULL) ) {
1415 if (do_proto) cprintf("%s=%s\n",
1419 else if ((k == 'F') && (suppress_f)) {
1422 /* Masquerade display name if needed */
1424 if (do_proto) cprintf("%s=%s\n",
1426 TheMessage->cm_fields[k]
1435 /* begin header processing loop for RFC822 transfer format */
1440 strcpy(snode, NODENAME);
1441 strcpy(lnode, HUMANNODE);
1442 if (mode == MT_RFC822) {
1443 for (i = 0; i < 256; ++i) {
1444 if (TheMessage->cm_fields[i]) {
1445 mptr = TheMessage->cm_fields[i];
1448 safestrncpy(luser, mptr, sizeof luser);
1449 safestrncpy(suser, mptr, sizeof suser);
1451 else if (i == 'Y') {
1452 cprintf("CC: %s%s", mptr, nl);
1454 else if (i == 'U') {
1455 cprintf("Subject: %s%s", mptr, nl);
1459 safestrncpy(mid, mptr, sizeof mid);
1461 safestrncpy(lnode, mptr, sizeof lnode);
1463 safestrncpy(fuser, mptr, sizeof fuser);
1464 /* else if (i == 'O')
1465 cprintf("X-Citadel-Room: %s%s",
1468 safestrncpy(snode, mptr, sizeof snode);
1470 cprintf("To: %s%s", mptr, nl);
1471 else if (i == 'T') {
1472 datestring(datestamp, sizeof datestamp,
1473 atol(mptr), DATESTRING_RFC822);
1474 cprintf("Date: %s%s", datestamp, nl);
1478 if (subject_found == 0) {
1479 cprintf("Subject: (no subject)%s", nl);
1483 for (i=0; i<strlen(suser); ++i) {
1484 suser[i] = tolower(suser[i]);
1485 if (!isalnum(suser[i])) suser[i]='_';
1488 if (mode == MT_RFC822) {
1489 if (!strcasecmp(snode, NODENAME)) {
1490 safestrncpy(snode, FQDN, sizeof snode);
1493 /* Construct a fun message id */
1494 cprintf("Message-ID: <%s", mid);
1495 if (strchr(mid, '@')==NULL) {
1496 cprintf("@%s", snode);
1500 if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONONLY)) {
1501 cprintf("From: \"----\" <x@x.org>%s", nl);
1503 else if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONOPT)) {
1504 cprintf("From: \"anonymous\" <x@x.org>%s", nl);
1506 else if (strlen(fuser) > 0) {
1507 cprintf("From: \"%s\" <%s>%s", luser, fuser, nl);
1510 cprintf("From: \"%s\" <%s@%s>%s", luser, suser, snode, nl);
1513 cprintf("Organization: %s%s", lnode, nl);
1515 /* Blank line signifying RFC822 end-of-headers */
1516 if (TheMessage->cm_format_type != FMT_RFC822) {
1521 /* end header processing loop ... at this point, we're in the text */
1523 if (headers_only == HEADERS_FAST) goto DONE;
1524 mptr = TheMessage->cm_fields['M'];
1526 /* Tell the client about the MIME parts in this message */
1527 if (TheMessage->cm_format_type == FMT_RFC822) {
1528 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
1529 memset(&ma, 0, sizeof(struct ma_info));
1530 mime_parser(mptr, NULL,
1531 (do_proto ? *list_this_part : NULL),
1532 (do_proto ? *list_this_pref : NULL),
1533 (do_proto ? *list_this_suff : NULL),
1536 else if (mode == MT_RFC822) { /* unparsed RFC822 dump */
1537 /* FIXME ... we have to put some code in here to avoid
1538 * printing duplicate header information when both
1539 * Citadel and RFC822 headers exist. Preference should
1540 * probably be given to the RFC822 headers.
1542 int done_rfc822_hdrs = 0;
1543 while (ch=*(mptr++), ch!=0) {
1548 if (!done_rfc822_hdrs) {
1549 if (headers_only != HEADERS_NONE) {
1554 if (headers_only != HEADERS_ONLY) {
1558 if ((*(mptr) == 13) || (*(mptr) == 10)) {
1559 done_rfc822_hdrs = 1;
1563 if (done_rfc822_hdrs) {
1564 if (headers_only != HEADERS_NONE) {
1569 if (headers_only != HEADERS_ONLY) {
1573 if ((*mptr == 13) || (*mptr == 10)) {
1574 done_rfc822_hdrs = 1;
1582 if (headers_only == HEADERS_ONLY) {
1586 /* signify start of msg text */
1587 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
1588 if (do_proto) cprintf("text\n");
1591 /* If the format type on disk is 1 (fixed-format), then we want
1592 * everything to be output completely literally ... regardless of
1593 * what message transfer format is in use.
1595 if (TheMessage->cm_format_type == FMT_FIXED) {
1596 if (mode == MT_MIME) {
1597 cprintf("Content-type: text/plain\n\n");
1600 while (ch = *mptr++, ch > 0) {
1603 if ((ch == 10) || (strlen(buf) > 250)) {
1604 cprintf("%s%s", buf, nl);
1607 buf[strlen(buf) + 1] = 0;
1608 buf[strlen(buf)] = ch;
1611 if (strlen(buf) > 0)
1612 cprintf("%s%s", buf, nl);
1615 /* If the message on disk is format 0 (Citadel vari-format), we
1616 * output using the formatter at 80 columns. This is the final output
1617 * form if the transfer format is RFC822, but if the transfer format
1618 * is Citadel proprietary, it'll still work, because the indentation
1619 * for new paragraphs is correct and the client will reformat the
1620 * message to the reader's screen width.
1622 if (TheMessage->cm_format_type == FMT_CITADEL) {
1623 if (mode == MT_MIME) {
1624 cprintf("Content-type: text/x-citadel-variformat\n\n");
1626 memfmout(80, mptr, 0, nl);
1629 /* If the message on disk is format 4 (MIME), we've gotta hand it
1630 * off to the MIME parser. The client has already been told that
1631 * this message is format 1 (fixed format), so the callback function
1632 * we use will display those parts as-is.
1634 if (TheMessage->cm_format_type == FMT_RFC822) {
1635 memset(&ma, 0, sizeof(struct ma_info));
1637 if (mode == MT_MIME) {
1638 strcpy(ma.chosen_part, "1");
1639 mime_parser(mptr, NULL,
1640 *choose_preferred, *fixed_output_pre,
1641 *fixed_output_post, (void *)&ma, 0);
1642 mime_parser(mptr, NULL,
1643 *output_preferred, NULL, NULL, (void *)&ma, 0);
1646 mime_parser(mptr, NULL,
1647 *fixed_output, *fixed_output_pre,
1648 *fixed_output_post, (void *)&ma, 0);
1653 DONE: /* now we're done */
1654 if (do_proto) cprintf("000\n");
1661 * display a message (mode 0 - Citadel proprietary)
1663 void cmd_msg0(char *cmdbuf)
1666 int headers_only = HEADERS_ALL;
1668 msgid = extract_long(cmdbuf, 0);
1669 headers_only = extract_int(cmdbuf, 1);
1671 CtdlOutputMsg(msgid, MT_CITADEL, headers_only, 1, 0);
1677 * display a message (mode 2 - RFC822)
1679 void cmd_msg2(char *cmdbuf)
1682 int headers_only = HEADERS_ALL;
1684 msgid = extract_long(cmdbuf, 0);
1685 headers_only = extract_int(cmdbuf, 1);
1687 CtdlOutputMsg(msgid, MT_RFC822, headers_only, 1, 1);
1693 * display a message (mode 3 - IGnet raw format - internal programs only)
1695 void cmd_msg3(char *cmdbuf)
1698 struct CtdlMessage *msg;
1701 if (CC->internal_pgm == 0) {
1702 cprintf("%d This command is for internal programs only.\n",
1703 ERROR + HIGHER_ACCESS_REQUIRED);
1707 msgnum = extract_long(cmdbuf, 0);
1708 msg = CtdlFetchMessage(msgnum, 1);
1710 cprintf("%d Message %ld not found.\n",
1711 ERROR + MESSAGE_NOT_FOUND, msgnum);
1715 serialize_message(&smr, msg);
1716 CtdlFreeMessage(msg);
1719 cprintf("%d Unable to serialize message\n",
1720 ERROR + INTERNAL_ERROR);
1724 cprintf("%d %ld\n", BINARY_FOLLOWS, (long)smr.len);
1725 client_write((char *)smr.ser, (int)smr.len);
1732 * Display a message using MIME content types
1734 void cmd_msg4(char *cmdbuf)
1738 msgid = extract_long(cmdbuf, 0);
1739 CtdlOutputMsg(msgid, MT_MIME, 0, 1, 0);
1745 * Client tells us its preferred message format(s)
1747 void cmd_msgp(char *cmdbuf)
1749 safestrncpy(CC->preferred_formats, cmdbuf,
1750 sizeof(CC->preferred_formats));
1751 cprintf("%d ok\n", CIT_OK);
1756 * Open a component of a MIME message as a download file
1758 void cmd_opna(char *cmdbuf)
1761 char desired_section[128];
1763 msgid = extract_long(cmdbuf, 0);
1764 extract_token(desired_section, cmdbuf, 1, '|', sizeof desired_section);
1765 safestrncpy(CC->download_desired_section, desired_section, sizeof CC->download_desired_section);
1766 CtdlOutputMsg(msgid, MT_DOWNLOAD, 0, 1, 1);
1771 * Save a message pointer into a specified room
1772 * (Returns 0 for success, nonzero for failure)
1773 * roomname may be NULL to use the current room
1775 int CtdlSaveMsgPointerInRoom(char *roomname, long msgid, int flags) {
1777 char hold_rm[ROOMNAMELEN];
1778 struct cdbdata *cdbfr;
1781 long highest_msg = 0L;
1782 struct CtdlMessage *msg = NULL;
1784 lprintf(CTDL_DEBUG, "CtdlSaveMsgPointerInRoom(%s, %ld, %d)\n",
1785 roomname, msgid, flags);
1787 strcpy(hold_rm, CC->room.QRname);
1789 /* We may need to check to see if this message is real */
1790 if ( (flags & SM_VERIFY_GOODNESS)
1791 || (flags & SM_DO_REPL_CHECK)
1793 msg = CtdlFetchMessage(msgid, 1);
1794 if (msg == NULL) return(ERROR + ILLEGAL_VALUE);
1797 /* Perform replication checks if necessary */
1798 if ( (flags & SM_DO_REPL_CHECK) && (msg != NULL) ) {
1800 if (getroom(&CC->room,
1801 ((roomname != NULL) ? roomname : CC->room.QRname) )
1803 lprintf(CTDL_ERR, "No such room <%s>\n", roomname);
1804 if (msg != NULL) CtdlFreeMessage(msg);
1805 return(ERROR + ROOM_NOT_FOUND);
1808 if (ReplicationChecks(msg) != 0) {
1809 getroom(&CC->room, hold_rm);
1810 if (msg != NULL) CtdlFreeMessage(msg);
1812 "Did replication, and newer exists\n");
1817 /* Now the regular stuff */
1818 if (lgetroom(&CC->room,
1819 ((roomname != NULL) ? roomname : CC->room.QRname) )
1821 lprintf(CTDL_ERR, "No such room <%s>\n", roomname);
1822 if (msg != NULL) CtdlFreeMessage(msg);
1823 return(ERROR + ROOM_NOT_FOUND);
1826 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
1827 if (cdbfr == NULL) {
1831 msglist = malloc(cdbfr->len);
1832 if (msglist == NULL)
1833 lprintf(CTDL_ALERT, "ERROR malloc msglist!\n");
1834 num_msgs = cdbfr->len / sizeof(long);
1835 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1840 /* Make sure the message doesn't already exist in this room. It
1841 * is absolutely taboo to have more than one reference to the same
1842 * message in a room.
1844 if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
1845 if (msglist[i] == msgid) {
1846 lputroom(&CC->room); /* unlock the room */
1847 getroom(&CC->room, hold_rm);
1848 if (msg != NULL) CtdlFreeMessage(msg);
1850 return(ERROR + ALREADY_EXISTS);
1854 /* Now add the new message */
1856 msglist = realloc(msglist, (num_msgs * sizeof(long)));
1858 if (msglist == NULL) {
1859 lprintf(CTDL_ALERT, "ERROR: can't realloc message list!\n");
1861 msglist[num_msgs - 1] = msgid;
1863 /* Sort the message list, so all the msgid's are in order */
1864 num_msgs = sort_msglist(msglist, num_msgs);
1866 /* Determine the highest message number */
1867 highest_msg = msglist[num_msgs - 1];
1869 /* Write it back to disk. */
1870 cdb_store(CDB_MSGLISTS, &CC->room.QRnumber, (int)sizeof(long),
1871 msglist, (int)(num_msgs * sizeof(long)));
1873 /* Free up the memory we used. */
1876 /* Update the highest-message pointer and unlock the room. */
1877 CC->room.QRhighest = highest_msg;
1878 lputroom(&CC->room);
1879 getroom(&CC->room, hold_rm);
1881 /* Bump the reference count for this message. */
1882 if ((flags & SM_DONT_BUMP_REF)==0) {
1883 AdjRefCount(msgid, +1);
1886 /* Return success. */
1887 if (msg != NULL) CtdlFreeMessage(msg);
1894 * Message base operation to save a new message to the message store
1895 * (returns new message number)
1897 * This is the back end for CtdlSubmitMsg() and should not be directly
1898 * called by server-side modules.
1901 long send_message(struct CtdlMessage *msg) {
1909 /* Get a new message number */
1910 newmsgid = get_new_message_number();
1911 snprintf(msgidbuf, sizeof msgidbuf, "%ld@%s", newmsgid, config.c_fqdn);
1913 /* Generate an ID if we don't have one already */
1914 if (msg->cm_fields['I']==NULL) {
1915 msg->cm_fields['I'] = strdup(msgidbuf);
1918 /* If the message is big, set its body aside for storage elsewhere */
1919 if (msg->cm_fields['M'] != NULL) {
1920 if (strlen(msg->cm_fields['M']) > BIGMSG) {
1922 holdM = msg->cm_fields['M'];
1923 msg->cm_fields['M'] = NULL;
1927 /* Serialize our data structure for storage in the database */
1928 serialize_message(&smr, msg);
1931 msg->cm_fields['M'] = holdM;
1935 cprintf("%d Unable to serialize message\n",
1936 ERROR + INTERNAL_ERROR);
1940 /* Write our little bundle of joy into the message base */
1941 if (cdb_store(CDB_MSGMAIN, &newmsgid, (int)sizeof(long),
1942 smr.ser, smr.len) < 0) {
1943 lprintf(CTDL_ERR, "Can't store message\n");
1947 cdb_store(CDB_BIGMSGS,
1957 /* Free the memory we used for the serialized message */
1960 /* Return the *local* message ID to the caller
1961 * (even if we're storing an incoming network message)
1969 * Serialize a struct CtdlMessage into the format used on disk and network.
1971 * This function loads up a "struct ser_ret" (defined in server.h) which
1972 * contains the length of the serialized message and a pointer to the
1973 * serialized message in memory. THE LATTER MUST BE FREED BY THE CALLER.
1975 void serialize_message(struct ser_ret *ret, /* return values */
1976 struct CtdlMessage *msg) /* unserialized msg */
1980 static char *forder = FORDER;
1982 if (is_valid_message(msg) == 0) return; /* self check */
1985 for (i=0; i<26; ++i) if (msg->cm_fields[(int)forder[i]] != NULL)
1986 ret->len = ret->len +
1987 strlen(msg->cm_fields[(int)forder[i]]) + 2;
1989 lprintf(CTDL_DEBUG, "serialize_message() calling malloc(%ld)\n", (long)ret->len);
1990 ret->ser = malloc(ret->len);
1991 if (ret->ser == NULL) {
1997 ret->ser[1] = msg->cm_anon_type;
1998 ret->ser[2] = msg->cm_format_type;
2001 for (i=0; i<26; ++i) if (msg->cm_fields[(int)forder[i]] != NULL) {
2002 ret->ser[wlen++] = (char)forder[i];
2003 strcpy((char *)&ret->ser[wlen], msg->cm_fields[(int)forder[i]]);
2004 wlen = wlen + strlen(msg->cm_fields[(int)forder[i]]) + 1;
2006 if (ret->len != wlen) lprintf(CTDL_ERR, "ERROR: len=%ld wlen=%ld\n",
2007 (long)ret->len, (long)wlen);
2015 * Back end for the ReplicationChecks() function
2017 void check_repl(long msgnum, void *userdata) {
2018 lprintf(CTDL_DEBUG, "check_repl() replacing message %ld\n", msgnum);
2019 CtdlDeleteMessages(CC->room.QRname, msgnum, "", 0);
2024 * Check to see if any messages already exist which carry the same Exclusive ID
2025 * as this one. If any are found, delete them.
2028 int ReplicationChecks(struct CtdlMessage *msg) {
2029 struct CtdlMessage *template;
2032 /* No exclusive id? Don't do anything. */
2033 if (msg->cm_fields['E'] == NULL) return 0;
2034 if (strlen(msg->cm_fields['E']) == 0) return 0;
2035 lprintf(CTDL_DEBUG, "Exclusive ID: <%s>\n", msg->cm_fields['E']);
2037 template = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
2038 memset(template, 0, sizeof(struct CtdlMessage));
2039 template->cm_fields['E'] = strdup(msg->cm_fields['E']);
2041 CtdlForEachMessage(MSGS_ALL, 0L, NULL, template, check_repl, NULL);
2043 CtdlFreeMessage(template);
2044 lprintf(CTDL_DEBUG, "ReplicationChecks() returning %d\n", abort_this);
2051 * Turn an arbitrary RFC822 address into a struct vCard for possible
2052 * inclusion into an address book.
2054 struct vCard *vcard_new_from_rfc822_addr(char *addr) {
2056 char user[256], node[256], name[256], email[256], n[256], uid[256];
2060 if (v == NULL) return(NULL);
2062 process_rfc822_addr(addr, user, node, name);
2063 vcard_set_prop(v, "fn", name, 0);
2065 vcard_fn_to_n(n, name, sizeof n);
2066 vcard_set_prop(v, "n", n, 0);
2068 snprintf(email, sizeof email, "%s@%s", user, node);
2069 vcard_set_prop(v, "email;internet", email, 0);
2071 snprintf(uid, sizeof uid, "collected: %s %s@%s", name, user, node);
2072 for (i=0; i<strlen(uid); ++i) {
2073 if (isspace(uid[i])) uid[i] = '_';
2074 uid[i] = tolower(uid[i]);
2076 vcard_set_prop(v, "UID", uid, 0);
2083 * Save a message to disk and submit it into the delivery system.
2085 long CtdlSubmitMsg(struct CtdlMessage *msg, /* message to save */
2086 struct recptypes *recps, /* recipients (if mail) */
2087 char *force /* force a particular room? */
2089 char submit_filename[128];
2090 char generated_timestamp[32];
2091 char hold_rm[ROOMNAMELEN];
2092 char actual_rm[ROOMNAMELEN];
2093 char force_room[ROOMNAMELEN];
2094 char content_type[SIZ]; /* We have to learn this */
2095 char recipient[SIZ];
2098 struct ctdluser userbuf;
2100 struct MetaData smi;
2101 FILE *network_fp = NULL;
2102 static int seqnum = 1;
2103 struct CtdlMessage *imsg = NULL;
2104 struct CtdlMessage *vmsg = NULL;
2105 long vmsgnum = (-1L);
2107 struct vCard *v = NULL;
2110 char *hold_R, *hold_D;
2111 char *collected_addresses = NULL;
2113 lprintf(CTDL_DEBUG, "CtdlSubmitMsg() called\n");
2114 if (is_valid_message(msg) == 0) return(-1); /* self check */
2116 /* If this message has no timestamp, we take the liberty of
2117 * giving it one, right now.
2119 if (msg->cm_fields['T'] == NULL) {
2120 lprintf(CTDL_DEBUG, "Generating timestamp\n");
2121 snprintf(generated_timestamp, sizeof generated_timestamp, "%ld", (long)time(NULL));
2122 msg->cm_fields['T'] = strdup(generated_timestamp);
2125 /* If this message has no path, we generate one.
2127 if (msg->cm_fields['P'] == NULL) {
2128 lprintf(CTDL_DEBUG, "Generating path\n");
2129 if (msg->cm_fields['A'] != NULL) {
2130 msg->cm_fields['P'] = strdup(msg->cm_fields['A']);
2131 for (a=0; a<strlen(msg->cm_fields['P']); ++a) {
2132 if (isspace(msg->cm_fields['P'][a])) {
2133 msg->cm_fields['P'][a] = ' ';
2138 msg->cm_fields['P'] = strdup("unknown");
2142 if (force == NULL) {
2143 strcpy(force_room, "");
2146 strcpy(force_room, force);
2149 /* Learn about what's inside, because it's what's inside that counts */
2150 lprintf(CTDL_DEBUG, "Learning what's inside\n");
2151 if (msg->cm_fields['M'] == NULL) {
2152 lprintf(CTDL_ERR, "ERROR: attempt to save message with NULL body\n");
2156 switch (msg->cm_format_type) {
2158 strcpy(content_type, "text/x-citadel-variformat");
2161 strcpy(content_type, "text/plain");
2164 strcpy(content_type, "text/plain");
2165 mptr = bmstrcasestr(msg->cm_fields['M'], "Content-type: ");
2167 safestrncpy(content_type, &mptr[14],
2168 sizeof content_type);
2169 for (a = 0; a < strlen(content_type); ++a) {
2170 if ((content_type[a] == ';')
2171 || (content_type[a] == ' ')
2172 || (content_type[a] == 13)
2173 || (content_type[a] == 10)) {
2174 content_type[a] = 0;
2180 /* Goto the correct room */
2181 lprintf(CTDL_DEBUG, "Selected room %s\n",
2182 (recps) ? CC->room.QRname : SENTITEMS);
2183 strcpy(hold_rm, CC->room.QRname);
2184 strcpy(actual_rm, CC->room.QRname);
2185 if (recps != NULL) {
2186 strcpy(actual_rm, SENTITEMS);
2189 /* If the user is a twit, move to the twit room for posting */
2190 lprintf(CTDL_DEBUG, "Handling twit stuff: %s\n",
2191 (CC->user.axlevel == 2) ? config.c_twitroom : "OK");
2193 if (CC->user.axlevel == 2) {
2194 strcpy(hold_rm, actual_rm);
2195 strcpy(actual_rm, config.c_twitroom);
2199 /* ...or if this message is destined for Aide> then go there. */
2200 if (strlen(force_room) > 0) {
2201 strcpy(actual_rm, force_room);
2204 lprintf(CTDL_DEBUG, "Final selection: %s\n", actual_rm);
2205 if (strcasecmp(actual_rm, CC->room.QRname)) {
2206 /* getroom(&CC->room, actual_rm); */
2207 usergoto(actual_rm, 0, 1, NULL, NULL);
2211 * If this message has no O (room) field, generate one.
2213 if (msg->cm_fields['O'] == NULL) {
2214 msg->cm_fields['O'] = strdup(CC->room.QRname);
2217 /* Perform "before save" hooks (aborting if any return nonzero) */
2218 lprintf(CTDL_DEBUG, "Performing before-save hooks\n");
2219 if (PerformMessageHooks(msg, EVT_BEFORESAVE) > 0) return(-3);
2221 /* If this message has an Exclusive ID, perform replication checks */
2222 lprintf(CTDL_DEBUG, "Performing replication checks\n");
2223 if (ReplicationChecks(msg) > 0) return(-4);
2225 /* Save it to disk */
2226 lprintf(CTDL_DEBUG, "Saving to disk\n");
2227 newmsgid = send_message(msg);
2228 if (newmsgid <= 0L) return(-5);
2230 /* Write a supplemental message info record. This doesn't have to
2231 * be a critical section because nobody else knows about this message
2234 lprintf(CTDL_DEBUG, "Creating MetaData record\n");
2235 memset(&smi, 0, sizeof(struct MetaData));
2236 smi.meta_msgnum = newmsgid;
2237 smi.meta_refcount = 0;
2238 safestrncpy(smi.meta_content_type, content_type,
2239 sizeof smi.meta_content_type);
2241 /* As part of the new metadata record, measure how
2242 * big this message will be when displayed as RFC822.
2243 * Both POP and IMAP use this, and it's best to just take the hit now
2244 * instead of having to potentially measure thousands of messages when
2245 * a mailbox is opened later.
2248 if (CC->redirect_buffer != NULL) {
2249 lprintf(CTDL_ALERT, "CC->redirect_buffer is not NULL during message submission!\n");
2252 CC->redirect_buffer = malloc(SIZ);
2253 CC->redirect_len = 0;
2254 CC->redirect_alloc = SIZ;
2255 CtdlOutputPreLoadedMsg(msg, 0L, MT_RFC822, HEADERS_ALL, 0, 1);
2256 smi.meta_rfc822_length = CC->redirect_len;
2257 free(CC->redirect_buffer);
2258 CC->redirect_buffer = NULL;
2259 CC->redirect_len = 0;
2260 CC->redirect_alloc = 0;
2264 /* Now figure out where to store the pointers */
2265 lprintf(CTDL_DEBUG, "Storing pointers\n");
2267 /* If this is being done by the networker delivering a private
2268 * message, we want to BYPASS saving the sender's copy (because there
2269 * is no local sender; it would otherwise go to the Trashcan).
2271 if ((!CC->internal_pgm) || (recps == NULL)) {
2272 if (CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0) != 0) {
2273 lprintf(CTDL_ERR, "ERROR saving message pointer!\n");
2274 CtdlSaveMsgPointerInRoom(config.c_aideroom,
2279 /* For internet mail, drop a copy in the outbound queue room */
2281 if (recps->num_internet > 0) {
2282 CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, newmsgid, 0);
2285 /* If other rooms are specified, drop them there too. */
2287 if (recps->num_room > 0)
2288 for (i=0; i<num_tokens(recps->recp_room, '|'); ++i) {
2289 extract_token(recipient, recps->recp_room, i,
2290 '|', sizeof recipient);
2291 lprintf(CTDL_DEBUG, "Delivering to room <%s>\n", recipient);
2292 CtdlSaveMsgPointerInRoom(recipient, newmsgid, 0);
2295 /* Bump this user's messages posted counter. */
2296 lprintf(CTDL_DEBUG, "Updating user\n");
2297 lgetuser(&CC->user, CC->curr_user);
2298 CC->user.posted = CC->user.posted + 1;
2299 lputuser(&CC->user);
2301 /* If this is private, local mail, make a copy in the
2302 * recipient's mailbox and bump the reference count.
2305 if (recps->num_local > 0)
2306 for (i=0; i<num_tokens(recps->recp_local, '|'); ++i) {
2307 extract_token(recipient, recps->recp_local, i,
2308 '|', sizeof recipient);
2309 lprintf(CTDL_DEBUG, "Delivering private local mail to <%s>\n",
2311 if (getuser(&userbuf, recipient) == 0) {
2312 MailboxName(actual_rm, sizeof actual_rm,
2313 &userbuf, MAILROOM);
2314 CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0);
2315 BumpNewMailCounter(userbuf.usernum);
2318 lprintf(CTDL_DEBUG, "No user <%s>\n", recipient);
2319 CtdlSaveMsgPointerInRoom(config.c_aideroom,
2324 /* Perform "after save" hooks */
2325 lprintf(CTDL_DEBUG, "Performing after-save hooks\n");
2326 PerformMessageHooks(msg, EVT_AFTERSAVE);
2328 /* For IGnet mail, we have to save a new copy into the spooler for
2329 * each recipient, with the R and D fields set to the recipient and
2330 * destination-node. This has two ugly side effects: all other
2331 * recipients end up being unlisted in this recipient's copy of the
2332 * message, and it has to deliver multiple messages to the same
2333 * node. We'll revisit this again in a year or so when everyone has
2334 * a network spool receiver that can handle the new style messages.
2337 if (recps->num_ignet > 0)
2338 for (i=0; i<num_tokens(recps->recp_ignet, '|'); ++i) {
2339 extract_token(recipient, recps->recp_ignet, i,
2340 '|', sizeof recipient);
2342 hold_R = msg->cm_fields['R'];
2343 hold_D = msg->cm_fields['D'];
2344 msg->cm_fields['R'] = malloc(SIZ);
2345 msg->cm_fields['D'] = malloc(128);
2346 extract_token(msg->cm_fields['R'], recipient, 0, '@', SIZ);
2347 extract_token(msg->cm_fields['D'], recipient, 1, '@', 128);
2349 serialize_message(&smr, msg);
2351 snprintf(submit_filename, sizeof submit_filename,
2352 #ifndef HAVE_SPOOL_DIR
2357 "/network/spoolin/netmail.%04lx.%04x.%04x",
2358 (long) getpid(), CC->cs_pid, ++seqnum);
2359 network_fp = fopen(submit_filename, "wb+");
2360 if (network_fp != NULL) {
2361 fwrite(smr.ser, smr.len, 1, network_fp);
2367 free(msg->cm_fields['R']);
2368 free(msg->cm_fields['D']);
2369 msg->cm_fields['R'] = hold_R;
2370 msg->cm_fields['D'] = hold_D;
2373 /* Go back to the room we started from */
2374 lprintf(CTDL_DEBUG, "Returning to original room %s\n", hold_rm);
2375 if (strcasecmp(hold_rm, CC->room.QRname))
2376 /* getroom(&CC->room, hold_rm); */
2377 usergoto(hold_rm, 0, 1, NULL, NULL);
2379 /* For internet mail, generate delivery instructions.
2380 * Yes, this is recursive. Deal with it. Infinite recursion does
2381 * not happen because the delivery instructions message does not
2382 * contain a recipient.
2385 if (recps->num_internet > 0) {
2386 lprintf(CTDL_DEBUG, "Generating delivery instructions\n");
2387 instr = malloc(SIZ * 2);
2388 snprintf(instr, SIZ * 2,
2389 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
2391 SPOOLMIME, newmsgid, (long)time(NULL),
2392 msg->cm_fields['A'], msg->cm_fields['N']
2395 for (i=0; i<num_tokens(recps->recp_internet, '|'); ++i) {
2396 size_t tmp = strlen(instr);
2397 extract_token(recipient, recps->recp_internet, i, '|', sizeof recipient);
2398 snprintf(&instr[tmp], SIZ * 2 - tmp,
2399 "remote|%s|0||\n", recipient);
2402 imsg = malloc(sizeof(struct CtdlMessage));
2403 memset(imsg, 0, sizeof(struct CtdlMessage));
2404 imsg->cm_magic = CTDLMESSAGE_MAGIC;
2405 imsg->cm_anon_type = MES_NORMAL;
2406 imsg->cm_format_type = FMT_RFC822;
2407 imsg->cm_fields['A'] = strdup("Citadel");
2408 imsg->cm_fields['M'] = instr;
2409 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
2410 CtdlFreeMessage(imsg);
2414 * Any addresses to harvest for someone's address book?
2415 * (Don't do this if this is not a message with recipients, otherwise we will
2416 * send this function into infinite recursion!!!!!)
2417 * FIXME do this differently
2419 if ( (CC->logged_in) && (recps != NULL) ) {
2420 collected_addresses = harvest_collected_addresses(msg);
2423 if (collected_addresses != NULL) {
2424 for (i=0; i<num_tokens(collected_addresses, ','); ++i) {
2426 /* Make a vCard out of each address */
2427 extract_token(recipient, collected_addresses, i, ',', sizeof recipient);
2429 v = vcard_new_from_rfc822_addr(recipient);
2431 vmsg = malloc(sizeof(struct CtdlMessage));
2432 memset(vmsg, 0, sizeof(struct CtdlMessage));
2433 vmsg->cm_magic = CTDLMESSAGE_MAGIC;
2434 vmsg->cm_anon_type = MES_NORMAL;
2435 vmsg->cm_format_type = FMT_RFC822;
2436 vmsg->cm_fields['A'] = strdup("Citadel");
2437 vmsg->cm_fields['E'] = strdup(vcard_get_prop(v, "UID", 0, 0, 0));
2438 ser = vcard_serialize(v);
2440 vmsg->cm_fields['M'] = malloc(strlen(ser) + 1024);
2441 sprintf(vmsg->cm_fields['M'],
2442 "Content-type: text/x-vcard"
2443 "\r\n\r\n%s\r\n", ser);
2448 lprintf(CTDL_DEBUG, "Adding contact: %s\n", recipient);
2449 MailboxName(actual_rm, sizeof actual_rm, &CC->user, USERCONTACTSROOM);
2450 vmsgnum = CtdlSubmitMsg(vmsg, NULL, actual_rm);
2451 CtdlFreeMessage(vmsg);
2454 free(collected_addresses);
2467 * Convenience function for generating small administrative messages.
2469 void quickie_message(char *from, char *to, char *room, char *text,
2470 int format_type, char *subject)
2472 struct CtdlMessage *msg;
2473 struct recptypes *recp = NULL;
2475 msg = malloc(sizeof(struct CtdlMessage));
2476 memset(msg, 0, sizeof(struct CtdlMessage));
2477 msg->cm_magic = CTDLMESSAGE_MAGIC;
2478 msg->cm_anon_type = MES_NORMAL;
2479 msg->cm_format_type = format_type;
2480 msg->cm_fields['A'] = strdup(from);
2481 if (room != NULL) msg->cm_fields['O'] = strdup(room);
2482 msg->cm_fields['N'] = strdup(NODENAME);
2484 msg->cm_fields['R'] = strdup(to);
2485 recp = validate_recipients(to);
2487 if (subject != NULL) {
2488 msg->cm_fields['U'] = strdup(subject);
2490 msg->cm_fields['M'] = strdup(text);
2492 CtdlSubmitMsg(msg, recp, room);
2493 CtdlFreeMessage(msg);
2494 if (recp != NULL) free(recp);
2500 * Back end function used by CtdlMakeMessage() and similar functions
2502 char *CtdlReadMessageBody(char *terminator, /* token signalling EOT */
2503 size_t maxlen, /* maximum message length */
2504 char *exist, /* if non-null, append to it;
2505 exist is ALWAYS freed */
2506 int crlf /* CRLF newlines instead of LF */
2510 size_t message_len = 0;
2511 size_t buffer_len = 0;
2517 if (exist == NULL) {
2524 message_len = strlen(exist);
2525 buffer_len = message_len + 4096;
2526 m = realloc(exist, buffer_len);
2533 /* flush the input if we have nowhere to store it */
2538 /* read in the lines of message text one by one */
2540 if (client_getln(buf, (sizeof buf - 3)) < 1) finished = 1;
2541 if (!strcmp(buf, terminator)) finished = 1;
2543 strcat(buf, "\r\n");
2549 if ( (!flushing) && (!finished) ) {
2550 /* Measure the line */
2551 linelen = strlen(buf);
2553 /* augment the buffer if we have to */
2554 if ((message_len + linelen) >= buffer_len) {
2555 ptr = realloc(m, (buffer_len * 2) );
2556 if (ptr == NULL) { /* flush if can't allocate */
2559 buffer_len = (buffer_len * 2);
2561 lprintf(CTDL_DEBUG, "buffer_len is now %ld\n", (long)buffer_len);
2565 /* Add the new line to the buffer. NOTE: this loop must avoid
2566 * using functions like strcat() and strlen() because they
2567 * traverse the entire buffer upon every call, and doing that
2568 * for a multi-megabyte message slows it down beyond usability.
2570 strcpy(&m[message_len], buf);
2571 message_len += linelen;
2574 /* if we've hit the max msg length, flush the rest */
2575 if (message_len >= maxlen) flushing = 1;
2577 } while (!finished);
2585 * Build a binary message to be saved on disk.
2586 * (NOTE: if you supply 'preformatted_text', the buffer you give it
2587 * will become part of the message. This means you are no longer
2588 * responsible for managing that memory -- it will be freed along with
2589 * the rest of the fields when CtdlFreeMessage() is called.)
2592 struct CtdlMessage *CtdlMakeMessage(
2593 struct ctdluser *author, /* author's user structure */
2594 char *recipient, /* NULL if it's not mail */
2595 char *recp_cc, /* NULL if it's not mail */
2596 char *room, /* room where it's going */
2597 int type, /* see MES_ types in header file */
2598 int format_type, /* variformat, plain text, MIME... */
2599 char *fake_name, /* who we're masquerading as */
2600 char *subject, /* Subject (optional) */
2601 char *preformatted_text /* ...or NULL to read text from client */
2603 char dest_node[SIZ];
2605 struct CtdlMessage *msg;
2607 msg = malloc(sizeof(struct CtdlMessage));
2608 memset(msg, 0, sizeof(struct CtdlMessage));
2609 msg->cm_magic = CTDLMESSAGE_MAGIC;
2610 msg->cm_anon_type = type;
2611 msg->cm_format_type = format_type;
2613 /* Don't confuse the poor folks if it's not routed mail. */
2614 strcpy(dest_node, "");
2619 snprintf(buf, sizeof buf, "cit%ld", author->usernum); /* Path */
2620 msg->cm_fields['P'] = strdup(buf);
2622 snprintf(buf, sizeof buf, "%ld", (long)time(NULL)); /* timestamp */
2623 msg->cm_fields['T'] = strdup(buf);
2625 if (fake_name[0]) /* author */
2626 msg->cm_fields['A'] = strdup(fake_name);
2628 msg->cm_fields['A'] = strdup(author->fullname);
2630 if (CC->room.QRflags & QR_MAILBOX) { /* room */
2631 msg->cm_fields['O'] = strdup(&CC->room.QRname[11]);
2634 msg->cm_fields['O'] = strdup(CC->room.QRname);
2637 msg->cm_fields['N'] = strdup(NODENAME); /* nodename */
2638 msg->cm_fields['H'] = strdup(HUMANNODE); /* hnodename */
2640 if (recipient[0] != 0) {
2641 msg->cm_fields['R'] = strdup(recipient);
2643 if (recp_cc[0] != 0) {
2644 msg->cm_fields['Y'] = strdup(recp_cc);
2646 if (dest_node[0] != 0) {
2647 msg->cm_fields['D'] = strdup(dest_node);
2650 if ( (author == &CC->user) && (strlen(CC->cs_inet_email) > 0) ) {
2651 msg->cm_fields['F'] = strdup(CC->cs_inet_email);
2654 if (subject != NULL) {
2656 if (strlen(subject) > 0) {
2657 msg->cm_fields['U'] = strdup(subject);
2661 if (preformatted_text != NULL) {
2662 msg->cm_fields['M'] = preformatted_text;
2665 msg->cm_fields['M'] = CtdlReadMessageBody("000",
2666 config.c_maxmsglen, NULL, 0);
2674 * Check to see whether we have permission to post a message in the current
2675 * room. Returns a *CITADEL ERROR CODE* and puts a message in errmsgbuf, or
2676 * returns 0 on success.
2678 int CtdlDoIHavePermissionToPostInThisRoom(char *errmsgbuf, size_t n) {
2680 if (!(CC->logged_in)) {
2681 snprintf(errmsgbuf, n, "Not logged in.");
2682 return (ERROR + NOT_LOGGED_IN);
2685 if ((CC->user.axlevel < 2)
2686 && ((CC->room.QRflags & QR_MAILBOX) == 0)) {
2687 snprintf(errmsgbuf, n, "Need to be validated to enter "
2688 "(except in %s> to sysop)", MAILROOM);
2689 return (ERROR + HIGHER_ACCESS_REQUIRED);
2692 if ((CC->user.axlevel < 4)
2693 && (CC->room.QRflags & QR_NETWORK)) {
2694 snprintf(errmsgbuf, n, "Need net privileges to enter here.");
2695 return (ERROR + HIGHER_ACCESS_REQUIRED);
2698 if ((CC->user.axlevel < 6)
2699 && (CC->room.QRflags & QR_READONLY)) {
2700 snprintf(errmsgbuf, n, "Sorry, this is a read-only room.");
2701 return (ERROR + HIGHER_ACCESS_REQUIRED);
2704 strcpy(errmsgbuf, "Ok");
2710 * Check to see if the specified user has Internet mail permission
2711 * (returns nonzero if permission is granted)
2713 int CtdlCheckInternetMailPermission(struct ctdluser *who) {
2715 /* Do not allow twits to send Internet mail */
2716 if (who->axlevel <= 2) return(0);
2718 /* Globally enabled? */
2719 if (config.c_restrict == 0) return(1);
2721 /* User flagged ok? */
2722 if (who->flags & US_INTERNET) return(2);
2724 /* Aide level access? */
2725 if (who->axlevel >= 6) return(3);
2727 /* No mail for you! */
2733 * Validate recipients, count delivery types and errors, and handle aliasing
2734 * FIXME check for dupes!!!!!
2735 * Returns 0 if all addresses are ok, -1 if no addresses were specified,
2736 * or the number of addresses found invalid.
2738 struct recptypes *validate_recipients(char *recipients) {
2739 struct recptypes *ret;
2740 char this_recp[SIZ];
2741 char this_recp_cooked[SIZ];
2747 struct ctdluser tempUS;
2748 struct ctdlroom tempQR;
2751 ret = (struct recptypes *) malloc(sizeof(struct recptypes));
2752 if (ret == NULL) return(NULL);
2753 memset(ret, 0, sizeof(struct recptypes));
2756 ret->num_internet = 0;
2761 if (recipients == NULL) {
2764 else if (strlen(recipients) == 0) {
2768 /* Change all valid separator characters to commas */
2769 for (i=0; i<strlen(recipients); ++i) {
2770 if ((recipients[i] == ';') || (recipients[i] == '|')) {
2771 recipients[i] = ',';
2776 num_recps = num_tokens(recipients, ',');
2779 if (num_recps > 0) for (i=0; i<num_recps; ++i) {
2780 extract_token(this_recp, recipients, i, ',', sizeof this_recp);
2782 lprintf(CTDL_DEBUG, "Evaluating recipient #%d <%s>\n", i, this_recp);
2783 mailtype = alias(this_recp);
2784 mailtype = alias(this_recp);
2785 mailtype = alias(this_recp);
2786 for (j=0; j<=strlen(this_recp); ++j) {
2787 if (this_recp[j]=='_') {
2788 this_recp_cooked[j] = ' ';
2791 this_recp_cooked[j] = this_recp[j];
2797 if (!strcasecmp(this_recp, "sysop")) {
2799 strcpy(this_recp, config.c_aideroom);
2800 if (strlen(ret->recp_room) > 0) {
2801 strcat(ret->recp_room, "|");
2803 strcat(ret->recp_room, this_recp);
2805 else if (getuser(&tempUS, this_recp) == 0) {
2807 strcpy(this_recp, tempUS.fullname);
2808 if (strlen(ret->recp_local) > 0) {
2809 strcat(ret->recp_local, "|");
2811 strcat(ret->recp_local, this_recp);
2813 else if (getuser(&tempUS, this_recp_cooked) == 0) {
2815 strcpy(this_recp, tempUS.fullname);
2816 if (strlen(ret->recp_local) > 0) {
2817 strcat(ret->recp_local, "|");
2819 strcat(ret->recp_local, this_recp);
2821 else if ( (!strncasecmp(this_recp, "room_", 5))
2822 && (!getroom(&tempQR, &this_recp_cooked[5])) ) {
2824 if (strlen(ret->recp_room) > 0) {
2825 strcat(ret->recp_room, "|");
2827 strcat(ret->recp_room, &this_recp_cooked[5]);
2835 /* Yes, you're reading this correctly: if the target
2836 * domain points back to the local system or an attached
2837 * Citadel directory, the address is invalid. That's
2838 * because if the address were valid, we would have
2839 * already translated it to a local address by now.
2841 if (IsDirectory(this_recp)) {
2846 ++ret->num_internet;
2847 if (strlen(ret->recp_internet) > 0) {
2848 strcat(ret->recp_internet, "|");
2850 strcat(ret->recp_internet, this_recp);
2855 if (strlen(ret->recp_ignet) > 0) {
2856 strcat(ret->recp_ignet, "|");
2858 strcat(ret->recp_ignet, this_recp);
2866 if (strlen(ret->errormsg) == 0) {
2867 snprintf(append, sizeof append,
2868 "Invalid recipient: %s",
2872 snprintf(append, sizeof append,
2875 if ( (strlen(ret->errormsg) + strlen(append)) < SIZ) {
2876 strcat(ret->errormsg, append);
2880 if (strlen(ret->display_recp) == 0) {
2881 strcpy(append, this_recp);
2884 snprintf(append, sizeof append, ", %s",
2887 if ( (strlen(ret->display_recp)+strlen(append)) < SIZ) {
2888 strcat(ret->display_recp, append);
2893 if ((ret->num_local + ret->num_internet + ret->num_ignet +
2894 ret->num_room + ret->num_error) == 0) {
2895 ret->num_error = (-1);
2896 strcpy(ret->errormsg, "No recipients specified.");
2899 lprintf(CTDL_DEBUG, "validate_recipients()\n");
2900 lprintf(CTDL_DEBUG, " local: %d <%s>\n", ret->num_local, ret->recp_local);
2901 lprintf(CTDL_DEBUG, " room: %d <%s>\n", ret->num_room, ret->recp_room);
2902 lprintf(CTDL_DEBUG, " inet: %d <%s>\n", ret->num_internet, ret->recp_internet);
2903 lprintf(CTDL_DEBUG, " ignet: %d <%s>\n", ret->num_ignet, ret->recp_ignet);
2904 lprintf(CTDL_DEBUG, " error: %d <%s>\n", ret->num_error, ret->errormsg);
2912 * message entry - mode 0 (normal)
2914 void cmd_ent0(char *entargs)
2920 char masquerade_as[SIZ];
2922 int format_type = 0;
2923 char newusername[SIZ];
2924 struct CtdlMessage *msg;
2928 struct recptypes *valid = NULL;
2929 struct recptypes *valid_to = NULL;
2930 struct recptypes *valid_cc = NULL;
2931 struct recptypes *valid_bcc = NULL;
2938 post = extract_int(entargs, 0);
2939 extract_token(recp, entargs, 1, '|', sizeof recp);
2940 anon_flag = extract_int(entargs, 2);
2941 format_type = extract_int(entargs, 3);
2942 extract_token(subject, entargs, 4, '|', sizeof subject);
2943 do_confirm = extract_int(entargs, 6);
2944 extract_token(cc, entargs, 7, '|', sizeof cc);
2945 extract_token(bcc, entargs, 8, '|', sizeof bcc);
2947 /* first check to make sure the request is valid. */
2949 err = CtdlDoIHavePermissionToPostInThisRoom(errmsg, sizeof errmsg);
2951 cprintf("%d %s\n", err, errmsg);
2955 /* Check some other permission type things. */
2958 if (CC->user.axlevel < 6) {
2959 cprintf("%d You don't have permission to masquerade.\n",
2960 ERROR + HIGHER_ACCESS_REQUIRED);
2963 extract_token(newusername, entargs, 5, '|', sizeof newusername);
2964 memset(CC->fake_postname, 0, sizeof(CC->fake_postname) );
2965 safestrncpy(CC->fake_postname, newusername,
2966 sizeof(CC->fake_postname) );
2967 cprintf("%d ok\n", CIT_OK);
2970 CC->cs_flags |= CS_POSTING;
2972 /* In the Mail> room we have to behave a little differently --
2973 * make sure the user has specified at least one recipient. Then
2974 * validate the recipient(s).
2976 if ( (CC->room.QRflags & QR_MAILBOX)
2977 && (!strcasecmp(&CC->room.QRname[11], MAILROOM)) ) {
2979 if (CC->user.axlevel < 2) {
2980 strcpy(recp, "sysop");
2985 valid_to = validate_recipients(recp);
2986 if (valid_to->num_error > 0) {
2987 cprintf("%d Invalid recipient (To)\n", ERROR + NO_SUCH_USER);
2992 valid_cc = validate_recipients(cc);
2993 if (valid_cc->num_error > 0) {
2994 cprintf("%d Invalid recipient (CC)\n", ERROR + NO_SUCH_USER);
3000 valid_bcc = validate_recipients(bcc);
3001 if (valid_bcc->num_error > 0) {
3002 cprintf("%d Invalid recipient (BCC)\n", ERROR + NO_SUCH_USER);
3009 /* Recipient required, but none were specified */
3010 if ( (valid_to->num_error < 0) && (valid_cc->num_error < 0) && (valid_bcc->num_error < 0) ) {
3014 cprintf("%d At least one recipient is required.\n", ERROR + NO_SUCH_USER);
3018 if (valid_to->num_internet + valid_cc->num_internet + valid_bcc->num_internet > 0) {
3019 if (CtdlCheckInternetMailPermission(&CC->user)==0) {
3020 cprintf("%d You do not have permission "
3021 "to send Internet mail.\n",
3022 ERROR + HIGHER_ACCESS_REQUIRED);
3030 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)
3031 && (CC->user.axlevel < 4) ) {
3032 cprintf("%d Higher access required for network mail.\n",
3033 ERROR + HIGHER_ACCESS_REQUIRED);
3040 if ((RESTRICT_INTERNET == 1)
3041 && (valid_to->num_internet + valid_cc->num_internet + valid_bcc->num_internet > 0)
3042 && ((CC->user.flags & US_INTERNET) == 0)
3043 && (!CC->internal_pgm)) {
3044 cprintf("%d You don't have access to Internet mail.\n",
3045 ERROR + HIGHER_ACCESS_REQUIRED);
3054 /* Is this a room which has anonymous-only or anonymous-option? */
3055 anonymous = MES_NORMAL;
3056 if (CC->room.QRflags & QR_ANONONLY) {
3057 anonymous = MES_ANONONLY;
3059 if (CC->room.QRflags & QR_ANONOPT) {
3060 if (anon_flag == 1) { /* only if the user requested it */
3061 anonymous = MES_ANONOPT;
3065 if ((CC->room.QRflags & QR_MAILBOX) == 0) {
3069 /* If we're only checking the validity of the request, return
3070 * success without creating the message.
3073 cprintf("%d %s\n", CIT_OK,
3074 ((valid_to != NULL) ? valid_to->display_recp : "") );
3081 /* We don't need these anymore because we'll do it differently below */
3086 /* Handle author masquerading */
3087 if (CC->fake_postname[0]) {
3088 strcpy(masquerade_as, CC->fake_postname);
3090 else if (CC->fake_username[0]) {
3091 strcpy(masquerade_as, CC->fake_username);
3094 strcpy(masquerade_as, "");
3097 /* Read in the message from the client. */
3099 cprintf("%d send message\n", START_CHAT_MODE);
3101 cprintf("%d send message\n", SEND_LISTING);
3104 msg = CtdlMakeMessage(&CC->user, recp, cc,
3105 CC->room.QRname, anonymous, format_type,
3106 masquerade_as, subject, NULL);
3108 /* Put together one big recipients struct containing to/cc/bcc all in
3109 * one. This is for the envelope.
3111 char *all_recps = malloc(SIZ * 3);
3112 strcpy(all_recps, recp);
3113 if (strlen(cc) > 0) {
3114 if (strlen(all_recps) > 0) {
3115 strcat(all_recps, ",");
3117 strcat(all_recps, cc);
3119 if (strlen(bcc) > 0) {
3120 if (strlen(all_recps) > 0) {
3121 strcat(all_recps, ",");
3123 strcat(all_recps, bcc);
3125 if (strlen(all_recps) > 0) {
3126 valid = validate_recipients(all_recps);
3134 msgnum = CtdlSubmitMsg(msg, valid, "");
3137 cprintf("%ld\n", msgnum);
3139 cprintf("Message accepted.\n");
3142 cprintf("Internal error.\n");
3144 if (msg->cm_fields['E'] != NULL) {
3145 cprintf("%s\n", msg->cm_fields['E']);
3152 CtdlFreeMessage(msg);
3154 CC->fake_postname[0] = '\0';
3155 if (valid != NULL) {
3164 * API function to delete messages which match a set of criteria
3165 * (returns the actual number of messages deleted)
3167 int CtdlDeleteMessages(char *room_name, /* which room */
3168 long dmsgnum, /* or "0" for any */
3169 char *content_type, /* or "" for any */
3170 int deferred /* let TDAP sweep it later */
3174 struct ctdlroom qrbuf;
3175 struct cdbdata *cdbfr;
3176 long *msglist = NULL;
3177 long *dellist = NULL;
3180 int num_deleted = 0;
3182 struct MetaData smi;
3184 lprintf(CTDL_DEBUG, "CtdlDeleteMessages(%s, %ld, %s, %d)\n",
3185 room_name, dmsgnum, content_type, deferred);
3187 /* get room record, obtaining a lock... */
3188 if (lgetroom(&qrbuf, room_name) != 0) {
3189 lprintf(CTDL_ERR, "CtdlDeleteMessages(): Room <%s> not found\n",
3191 return (0); /* room not found */
3193 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
3195 if (cdbfr != NULL) {
3196 msglist = malloc(cdbfr->len);
3197 dellist = malloc(cdbfr->len);
3198 memcpy(msglist, cdbfr->ptr, cdbfr->len);
3199 num_msgs = cdbfr->len / sizeof(long);
3203 for (i = 0; i < num_msgs; ++i) {
3206 /* Set/clear a bit for each criterion */
3208 if ((dmsgnum == 0L) || (msglist[i] == dmsgnum)) {
3209 delete_this |= 0x01;
3211 if (strlen(content_type) == 0) {
3212 delete_this |= 0x02;
3214 GetMetaData(&smi, msglist[i]);
3215 if (!strcasecmp(smi.meta_content_type,
3217 delete_this |= 0x02;
3221 /* Delete message only if all bits are set */
3222 if (delete_this == 0x03) {
3223 dellist[num_deleted++] = msglist[i];
3228 num_msgs = sort_msglist(msglist, num_msgs);
3229 cdb_store(CDB_MSGLISTS, &qrbuf.QRnumber, (int)sizeof(long),
3230 msglist, (int)(num_msgs * sizeof(long)));
3232 qrbuf.QRhighest = msglist[num_msgs - 1];
3237 * If the delete operation is "deferred" (and technically, any delete
3238 * operation not performed by THE DREADED AUTO-PURGER ought to be
3239 * a deferred delete) then we save a pointer to the message in the
3240 * DELETED_MSGS_ROOM. This will cause the reference count to remain
3241 * at least 1, which will save the user from having to synchronously
3242 * wait for various disk-intensive operations to complete.
3244 if ( (deferred) && (num_deleted) ) {
3245 for (i=0; i<num_deleted; ++i) {
3246 CtdlCopyMsgToRoom(dellist[i], DELETED_MSGS_ROOM);
3250 /* Go through the messages we pulled out of the index, and decrement
3251 * their reference counts by 1. If this is the only room the message
3252 * was in, the reference count will reach zero and the message will
3253 * automatically be deleted from the database. We do this in a
3254 * separate pass because there might be plug-in hooks getting called,
3255 * and we don't want that happening during an S_ROOMS critical
3258 if (num_deleted) for (i=0; i<num_deleted; ++i) {
3259 PerformDeleteHooks(qrbuf.QRname, dellist[i]);
3260 AdjRefCount(dellist[i], -1);
3263 /* Now free the memory we used, and go away. */
3264 if (msglist != NULL) free(msglist);
3265 if (dellist != NULL) free(dellist);
3266 lprintf(CTDL_DEBUG, "%d message(s) deleted.\n", num_deleted);
3267 return (num_deleted);
3273 * Check whether the current user has permission to delete messages from
3274 * the current room (returns 1 for yes, 0 for no)
3276 int CtdlDoIHavePermissionToDeleteMessagesFromThisRoom(void) {
3277 getuser(&CC->user, CC->curr_user);
3278 if ((CC->user.axlevel < 6)
3279 && (CC->user.usernum != CC->room.QRroomaide)
3280 && ((CC->room.QRflags & QR_MAILBOX) == 0)
3281 && (!(CC->internal_pgm))) {
3290 * Delete message from current room
3292 void cmd_dele(char *delstr)
3297 if (CtdlDoIHavePermissionToDeleteMessagesFromThisRoom() == 0) {
3298 cprintf("%d Higher access required.\n",
3299 ERROR + HIGHER_ACCESS_REQUIRED);
3302 delnum = extract_long(delstr, 0);
3304 num_deleted = CtdlDeleteMessages(CC->room.QRname, delnum, "", 1);
3307 cprintf("%d %d message%s deleted.\n", CIT_OK,
3308 num_deleted, ((num_deleted != 1) ? "s" : ""));
3310 cprintf("%d Message %ld not found.\n", ERROR + MESSAGE_NOT_FOUND, delnum);
3316 * Back end API function for moves and deletes
3318 int CtdlCopyMsgToRoom(long msgnum, char *dest) {
3321 err = CtdlSaveMsgPointerInRoom(dest, msgnum,
3322 (SM_VERIFY_GOODNESS | SM_DO_REPL_CHECK) );
3323 if (err != 0) return(err);
3331 * move or copy a message to another room
3333 void cmd_move(char *args)
3336 char targ[ROOMNAMELEN];
3337 struct ctdlroom qtemp;
3343 num = extract_long(args, 0);
3344 extract_token(targ, args, 1, '|', sizeof targ);
3345 targ[ROOMNAMELEN - 1] = 0;
3346 is_copy = extract_int(args, 2);
3348 if (getroom(&qtemp, targ) != 0) {
3349 cprintf("%d '%s' does not exist.\n",
3350 ERROR + ROOM_NOT_FOUND, targ);
3354 getuser(&CC->user, CC->curr_user);
3355 CtdlRoomAccess(&qtemp, &CC->user, &ra, NULL);
3357 /* Check for permission to perform this operation.
3358 * Remember: "CC->room" is source, "qtemp" is target.
3362 /* Aides can move/copy */
3363 if (CC->user.axlevel >= 6) permit = 1;
3365 /* Room aides can move/copy */
3366 if (CC->user.usernum == CC->room.QRroomaide) permit = 1;
3368 /* Permit move/copy from personal rooms */
3369 if ((CC->room.QRflags & QR_MAILBOX)
3370 && (qtemp.QRflags & QR_MAILBOX)) permit = 1;
3372 /* Permit only copy from public to personal room */
3374 && (!(CC->room.QRflags & QR_MAILBOX))
3375 && (qtemp.QRflags & QR_MAILBOX)) permit = 1;
3377 /* User must have access to target room */
3378 if (!(ra & UA_KNOWN)) permit = 0;
3381 cprintf("%d Higher access required.\n",
3382 ERROR + HIGHER_ACCESS_REQUIRED);
3386 err = CtdlCopyMsgToRoom(num, targ);
3388 cprintf("%d Cannot store message in %s: error %d\n",
3393 /* Now delete the message from the source room,
3394 * if this is a 'move' rather than a 'copy' operation.
3397 CtdlDeleteMessages(CC->room.QRname, num, "", 0);
3400 cprintf("%d Message %s.\n", CIT_OK, (is_copy ? "copied" : "moved") );
3406 * GetMetaData() - Get the supplementary record for a message
3408 void GetMetaData(struct MetaData *smibuf, long msgnum)
3411 struct cdbdata *cdbsmi;
3414 memset(smibuf, 0, sizeof(struct MetaData));
3415 smibuf->meta_msgnum = msgnum;
3416 smibuf->meta_refcount = 1; /* Default reference count is 1 */
3418 /* Use the negative of the message number for its supp record index */
3419 TheIndex = (0L - msgnum);
3421 cdbsmi = cdb_fetch(CDB_MSGMAIN, &TheIndex, sizeof(long));
3422 if (cdbsmi == NULL) {
3423 return; /* record not found; go with defaults */
3425 memcpy(smibuf, cdbsmi->ptr,
3426 ((cdbsmi->len > sizeof(struct MetaData)) ?
3427 sizeof(struct MetaData) : cdbsmi->len));
3434 * PutMetaData() - (re)write supplementary record for a message
3436 void PutMetaData(struct MetaData *smibuf)
3440 /* Use the negative of the message number for the metadata db index */
3441 TheIndex = (0L - smibuf->meta_msgnum);
3443 lprintf(CTDL_DEBUG, "PutMetaData(%ld) - ref count is %d\n",
3444 smibuf->meta_msgnum, smibuf->meta_refcount);
3446 cdb_store(CDB_MSGMAIN,
3447 &TheIndex, (int)sizeof(long),
3448 smibuf, (int)sizeof(struct MetaData));
3453 * AdjRefCount - change the reference count for a message;
3454 * delete the message if it reaches zero
3456 void AdjRefCount(long msgnum, int incr)
3459 struct MetaData smi;
3462 /* This is a *tight* critical section; please keep it that way, as
3463 * it may get called while nested in other critical sections.
3464 * Complicating this any further will surely cause deadlock!
3466 begin_critical_section(S_SUPPMSGMAIN);
3467 GetMetaData(&smi, msgnum);
3468 smi.meta_refcount += incr;
3470 end_critical_section(S_SUPPMSGMAIN);
3471 lprintf(CTDL_DEBUG, "msg %ld ref count incr %d, is now %d\n",
3472 msgnum, incr, smi.meta_refcount);
3474 /* If the reference count is now zero, delete the message
3475 * (and its supplementary record as well).
3477 if (smi.meta_refcount == 0) {
3478 lprintf(CTDL_DEBUG, "Deleting message <%ld>\n", msgnum);
3480 /* Remove from fulltext index */
3481 if (config.c_enable_fulltext) {
3482 ft_index_message(msgnum, 0);
3485 /* Remove from message base */
3487 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3488 cdb_delete(CDB_BIGMSGS, &delnum, (int)sizeof(long));
3490 /* Remove metadata record */
3491 delnum = (0L - msgnum);
3492 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3497 * Write a generic object to this room
3499 * Note: this could be much more efficient. Right now we use two temporary
3500 * files, and still pull the message into memory as with all others.
3502 void CtdlWriteObject(char *req_room, /* Room to stuff it in */
3503 char *content_type, /* MIME type of this object */
3504 char *tempfilename, /* Where to fetch it from */
3505 struct ctdluser *is_mailbox, /* Mailbox room? */
3506 int is_binary, /* Is encoding necessary? */
3507 int is_unique, /* Del others of this type? */
3508 unsigned int flags /* Internal save flags */
3513 struct ctdlroom qrbuf;
3514 char roomname[ROOMNAMELEN];
3515 struct CtdlMessage *msg;
3517 char *raw_message = NULL;
3518 char *encoded_message = NULL;
3519 off_t raw_length = 0;
3521 if (is_mailbox != NULL)
3522 MailboxName(roomname, sizeof roomname, is_mailbox, req_room);
3524 safestrncpy(roomname, req_room, sizeof(roomname));
3525 lprintf(CTDL_DEBUG, "CtdlWriteObject() to <%s> (flags=%d)\n", roomname, flags);
3528 fp = fopen(tempfilename, "rb");
3530 lprintf(CTDL_CRIT, "Cannot open %s: %s\n",
3531 tempfilename, strerror(errno));
3534 fseek(fp, 0L, SEEK_END);
3535 raw_length = ftell(fp);
3537 lprintf(CTDL_DEBUG, "Raw length is %ld\n", (long)raw_length);
3539 raw_message = malloc((size_t)raw_length + 2);
3540 fread(raw_message, (size_t)raw_length, 1, fp);
3544 encoded_message = malloc((size_t)
3545 (((raw_length * 134) / 100) + 4096 ) );
3548 encoded_message = malloc((size_t)(raw_length + 4096));
3551 sprintf(encoded_message, "Content-type: %s\n", content_type);
3554 sprintf(&encoded_message[strlen(encoded_message)],
3555 "Content-transfer-encoding: base64\n\n"
3559 sprintf(&encoded_message[strlen(encoded_message)],
3560 "Content-transfer-encoding: 7bit\n\n"
3566 &encoded_message[strlen(encoded_message)],
3572 raw_message[raw_length] = 0;
3574 &encoded_message[strlen(encoded_message)],
3582 lprintf(CTDL_DEBUG, "Allocating\n");
3583 msg = malloc(sizeof(struct CtdlMessage));
3584 memset(msg, 0, sizeof(struct CtdlMessage));
3585 msg->cm_magic = CTDLMESSAGE_MAGIC;
3586 msg->cm_anon_type = MES_NORMAL;
3587 msg->cm_format_type = 4;
3588 msg->cm_fields['A'] = strdup(CC->user.fullname);
3589 msg->cm_fields['O'] = strdup(req_room);
3590 msg->cm_fields['N'] = strdup(config.c_nodename);
3591 msg->cm_fields['H'] = strdup(config.c_humannode);
3592 msg->cm_flags = flags;
3594 msg->cm_fields['M'] = encoded_message;
3596 /* Create the requested room if we have to. */
3597 if (getroom(&qrbuf, roomname) != 0) {
3598 create_room(roomname,
3599 ( (is_mailbox != NULL) ? 5 : 3 ),
3600 "", 0, 1, 0, VIEW_BBS);
3602 /* If the caller specified this object as unique, delete all
3603 * other objects of this type that are currently in the room.
3606 lprintf(CTDL_DEBUG, "Deleted %d other msgs of this type\n",
3607 CtdlDeleteMessages(roomname, 0L, content_type, 0)
3610 /* Now write the data */
3611 CtdlSubmitMsg(msg, NULL, roomname);
3612 CtdlFreeMessage(msg);
3620 void CtdlGetSysConfigBackend(long msgnum, void *userdata) {
3621 config_msgnum = msgnum;
3625 char *CtdlGetSysConfig(char *sysconfname) {
3626 char hold_rm[ROOMNAMELEN];
3629 struct CtdlMessage *msg;
3632 strcpy(hold_rm, CC->room.QRname);
3633 if (getroom(&CC->room, SYSCONFIGROOM) != 0) {
3634 getroom(&CC->room, hold_rm);
3639 /* We want the last (and probably only) config in this room */
3640 begin_critical_section(S_CONFIG);
3641 config_msgnum = (-1L);
3642 CtdlForEachMessage(MSGS_LAST, 1, sysconfname, NULL,
3643 CtdlGetSysConfigBackend, NULL);
3644 msgnum = config_msgnum;
3645 end_critical_section(S_CONFIG);
3651 msg = CtdlFetchMessage(msgnum, 1);
3653 conf = strdup(msg->cm_fields['M']);
3654 CtdlFreeMessage(msg);
3661 getroom(&CC->room, hold_rm);
3663 if (conf != NULL) do {
3664 extract_token(buf, conf, 0, '\n', sizeof buf);
3665 strcpy(conf, &conf[strlen(buf)+1]);
3666 } while ( (strlen(conf)>0) && (strlen(buf)>0) );
3671 void CtdlPutSysConfig(char *sysconfname, char *sysconfdata) {
3672 char temp[PATH_MAX];
3675 strcpy(temp, tmpnam(NULL));
3677 fp = fopen(temp, "w");
3678 if (fp == NULL) return;
3679 fprintf(fp, "%s", sysconfdata);
3682 /* this handy API function does all the work for us */
3683 CtdlWriteObject(SYSCONFIGROOM, sysconfname, temp, NULL, 0, 1, 0);
3689 * Determine whether a given Internet address belongs to the current user
3691 int CtdlIsMe(char *addr, int addr_buf_len)
3693 struct recptypes *recp;
3696 recp = validate_recipients(addr);
3697 if (recp == NULL) return(0);
3699 if (recp->num_local == 0) {
3704 for (i=0; i<recp->num_local; ++i) {
3705 extract_token(addr, recp->recp_local, i, '|', addr_buf_len);
3706 if (!strcasecmp(addr, CC->user.fullname)) {
3718 * Citadel protocol command to do the same
3720 void cmd_isme(char *argbuf) {
3723 if (CtdlAccessCheck(ac_logged_in)) return;
3724 extract_token(addr, argbuf, 0, '|', sizeof addr);
3726 if (CtdlIsMe(addr, sizeof addr)) {
3727 cprintf("%d %s\n", CIT_OK, addr);
3730 cprintf("%d Not you.\n", ERROR + ILLEGAL_VALUE);