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"
60 * This really belongs in serv_network.c, but I don't know how to export
61 * symbols between modules.
63 struct FilterList *filterlist = NULL;
67 * These are the four-character field headers we use when outputting
68 * messages in Citadel format (as opposed to RFC822 format).
71 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
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,
105 * This function is self explanatory.
106 * (What can I say, I'm in a weird mood today...)
108 void remove_any_whitespace_to_the_left_or_right_of_at_symbol(char *name)
112 for (i = 0; i < strlen(name); ++i) {
113 if (name[i] == '@') {
114 while (isspace(name[i - 1]) && i > 0) {
115 strcpy(&name[i - 1], &name[i]);
118 while (isspace(name[i + 1])) {
119 strcpy(&name[i + 1], &name[i + 2]);
127 * Aliasing for network mail.
128 * (Error messages have been commented out, because this is a server.)
130 int alias(char *name)
131 { /* process alias and routing info for mail */
134 char aaa[SIZ], bbb[SIZ];
135 char *ignetcfg = NULL;
136 char *ignetmap = NULL;
143 remove_any_whitespace_to_the_left_or_right_of_at_symbol(name);
151 "mail.aliases", "r");
153 fp = fopen("/dev/null", "r");
160 while (fgets(aaa, sizeof aaa, fp) != NULL) {
161 while (isspace(name[0]))
162 strcpy(name, &name[1]);
163 aaa[strlen(aaa) - 1] = 0;
165 for (a = 0; a < strlen(aaa); ++a) {
167 strcpy(bbb, &aaa[a + 1]);
171 if (!strcasecmp(name, aaa))
176 /* Hit the Global Address Book */
177 if (CtdlDirectoryLookup(aaa, name, sizeof aaa) == 0) {
181 lprintf(CTDL_INFO, "Mail is being forwarded to %s\n", name);
183 /* Change "user @ xxx" to "user" if xxx is an alias for this host */
184 for (a=0; a<strlen(name); ++a) {
185 if (name[a] == '@') {
186 if (CtdlHostAlias(&name[a+1]) == hostalias_localhost) {
188 lprintf(CTDL_INFO, "Changed to <%s>\n", name);
193 /* determine local or remote type, see citadel.h */
194 at = haschar(name, '@');
195 if (at == 0) return(MES_LOCAL); /* no @'s - local address */
196 if (at > 1) return(MES_ERROR); /* >1 @'s - invalid address */
197 remove_any_whitespace_to_the_left_or_right_of_at_symbol(name);
199 /* figure out the delivery mode */
200 extract_token(node, name, 1, '@', sizeof node);
202 /* If there are one or more dots in the nodename, we assume that it
203 * is an FQDN and will attempt SMTP delivery to the Internet.
205 if (haschar(node, '.') > 0) {
206 return(MES_INTERNET);
209 /* Otherwise we look in the IGnet maps for a valid Citadel node.
210 * Try directly-connected nodes first...
212 ignetcfg = CtdlGetSysConfig(IGNETCFG);
213 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
214 extract_token(buf, ignetcfg, i, '\n', sizeof buf);
215 extract_token(testnode, buf, 0, '|', sizeof testnode);
216 if (!strcasecmp(node, testnode)) {
224 * Then try nodes that are two or more hops away.
226 ignetmap = CtdlGetSysConfig(IGNETMAP);
227 for (i=0; i<num_tokens(ignetmap, '\n'); ++i) {
228 extract_token(buf, ignetmap, i, '\n', sizeof buf);
229 extract_token(testnode, buf, 0, '|', sizeof testnode);
230 if (!strcasecmp(node, testnode)) {
237 /* If we get to this point it's an invalid node name */
252 "/citadel.control", "r");
254 lprintf(CTDL_CRIT, "Cannot open citadel.control: %s\n",
258 fread((char *) &CitControl, sizeof(struct CitControl), 1, fp);
264 * Back end for the MSGS command: output message number only.
266 void simple_listing(long msgnum, void *userdata)
268 cprintf("%ld\n", msgnum);
274 * Back end for the MSGS command: output header summary.
276 void headers_listing(long msgnum, void *userdata)
278 struct CtdlMessage *msg;
280 msg = CtdlFetchMessage(msgnum, 0);
282 cprintf("%ld|0|||||\n", msgnum);
286 cprintf("%ld|%s|%s|%s|%s|%s|\n",
288 (msg->cm_fields['T'] ? msg->cm_fields['T'] : "0"),
289 (msg->cm_fields['A'] ? msg->cm_fields['A'] : ""),
290 (msg->cm_fields['N'] ? msg->cm_fields['N'] : ""),
291 (msg->cm_fields['F'] ? msg->cm_fields['F'] : ""),
292 (msg->cm_fields['U'] ? msg->cm_fields['U'] : "")
294 CtdlFreeMessage(msg);
299 /* Determine if a given message matches the fields in a message template.
300 * Return 0 for a successful match.
302 int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template) {
305 /* If there aren't any fields in the template, all messages will
308 if (template == NULL) return(0);
310 /* Null messages are bogus. */
311 if (msg == NULL) return(1);
313 for (i='A'; i<='Z'; ++i) {
314 if (template->cm_fields[i] != NULL) {
315 if (msg->cm_fields[i] == NULL) {
318 if (strcasecmp(msg->cm_fields[i],
319 template->cm_fields[i])) return 1;
323 /* All compares succeeded: we have a match! */
330 * Retrieve the "seen" message list for the current room.
332 void CtdlGetSeen(char *buf, int which_set) {
335 /* Learn about the user and room in question */
336 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
338 if (which_set == ctdlsetseen_seen)
339 safestrncpy(buf, vbuf.v_seen, SIZ);
340 if (which_set == ctdlsetseen_answered)
341 safestrncpy(buf, vbuf.v_answered, SIZ);
347 * Manipulate the "seen msgs" string (or other message set strings)
349 void CtdlSetSeen(long target_msgnum, int target_setting, int which_set,
350 struct ctdluser *which_user, struct ctdlroom *which_room) {
351 struct cdbdata *cdbfr;
363 char *is_set; /* actually an array of booleans */
366 char setstr[SIZ], lostr[SIZ], histr[SIZ];
369 lprintf(CTDL_DEBUG, "CtdlSetSeen(%ld, %d, %d)\n",
370 target_msgnum, target_setting, which_set);
372 /* Learn about the user and room in question */
373 CtdlGetRelationship(&vbuf,
374 ((which_user != NULL) ? which_user : &CC->user),
375 ((which_room != NULL) ? which_room : &CC->room)
378 /* Load the message list */
379 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
381 msglist = malloc(cdbfr->len);
382 memcpy(msglist, cdbfr->ptr, cdbfr->len);
383 num_msgs = cdbfr->len / sizeof(long);
386 return; /* No messages at all? No further action. */
389 is_set = malloc(num_msgs * sizeof(char));
390 memset(is_set, 0, (num_msgs * sizeof(char)) );
392 /* Decide which message set we're manipulating */
394 case ctdlsetseen_seen:
395 safestrncpy(vset, vbuf.v_seen, sizeof vset);
397 case ctdlsetseen_answered:
398 safestrncpy(vset, vbuf.v_answered, sizeof vset);
402 /* lprintf(CTDL_DEBUG, "before optimize: %s\n", vset); */
404 /* Translate the existing sequence set into an array of booleans */
405 num_sets = num_tokens(vset, ',');
406 for (s=0; s<num_sets; ++s) {
407 extract_token(setstr, vset, s, ',', sizeof setstr);
409 extract_token(lostr, setstr, 0, ':', sizeof lostr);
410 if (num_tokens(setstr, ':') >= 2) {
411 extract_token(histr, setstr, 1, ':', sizeof histr);
412 if (!strcmp(histr, "*")) {
413 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
417 strcpy(histr, lostr);
422 for (i = 0; i < num_msgs; ++i) {
423 if ((msglist[i] >= lo) && (msglist[i] <= hi)) {
429 /* Now translate the array of booleans back into a sequence set */
434 for (i=0; i<num_msgs; ++i) {
437 if (msglist[i] == target_msgnum) {
438 is_seen = target_setting;
445 if (lo < 0L) lo = msglist[i];
449 if ( ((is_seen == 0) && (was_seen == 1))
450 || ((is_seen == 1) && (i == num_msgs-1)) ) {
452 /* begin trim-o-matic code */
455 while ( (strlen(vset) + 20) > sizeof vset) {
456 remove_token(vset, 0, ',');
458 if (j--) break; /* loop no more than 9 times */
460 if ( (trimming) && (which_set == ctdlsetseen_seen) ) {
464 snprintf(lostr, sizeof lostr,
465 "1:%ld,%s", t, vset);
466 safestrncpy(vset, lostr, sizeof vset);
468 /* end trim-o-matic code */
476 snprintf(&vset[tmp], (sizeof vset) - tmp,
480 snprintf(&vset[tmp], (sizeof vset) - tmp,
489 /* Decide which message set we're manipulating */
491 case ctdlsetseen_seen:
492 safestrncpy(vbuf.v_seen, vset, sizeof vbuf.v_seen);
494 case ctdlsetseen_answered:
495 safestrncpy(vbuf.v_answered, vset,
496 sizeof vbuf.v_answered);
501 /* lprintf(CTDL_DEBUG, " after optimize: %s\n", vset); */
503 CtdlSetRelationship(&vbuf,
504 ((which_user != NULL) ? which_user : &CC->user),
505 ((which_room != NULL) ? which_room : &CC->room)
511 * API function to perform an operation for each qualifying message in the
512 * current room. (Returns the number of messages processed.)
514 int CtdlForEachMessage(int mode, long ref,
516 struct CtdlMessage *compare,
517 void (*CallBack) (long, void *),
523 struct cdbdata *cdbfr;
524 long *msglist = NULL;
526 int num_processed = 0;
529 struct CtdlMessage *msg;
532 int printed_lastold = 0;
534 /* Learn about the user and room in question */
536 getuser(&CC->user, CC->curr_user);
537 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
539 /* Load the message list */
540 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
542 msglist = malloc(cdbfr->len);
543 memcpy(msglist, cdbfr->ptr, cdbfr->len);
544 num_msgs = cdbfr->len / sizeof(long);
547 return 0; /* No messages at all? No further action. */
552 * Now begin the traversal.
554 if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
556 /* If the caller is looking for a specific MIME type, filter
557 * out all messages which are not of the type requested.
559 if (content_type != NULL) if (strlen(content_type) > 0) {
561 /* This call to GetMetaData() sits inside this loop
562 * so that we only do the extra database read per msg
563 * if we need to. Doing the extra read all the time
564 * really kills the server. If we ever need to use
565 * metadata for another search criterion, we need to
566 * move the read somewhere else -- but still be smart
567 * enough to only do the read if the caller has
568 * specified something that will need it.
570 GetMetaData(&smi, msglist[a]);
572 if (strcasecmp(smi.meta_content_type, content_type)) {
578 num_msgs = sort_msglist(msglist, num_msgs);
580 /* If a template was supplied, filter out the messages which
581 * don't match. (This could induce some delays!)
584 if (compare != NULL) {
585 for (a = 0; a < num_msgs; ++a) {
586 msg = CtdlFetchMessage(msglist[a], 1);
588 if (CtdlMsgCmp(msg, compare)) {
591 CtdlFreeMessage(msg);
599 * Now iterate through the message list, according to the
600 * criteria supplied by the caller.
603 for (a = 0; a < num_msgs; ++a) {
604 thismsg = msglist[a];
605 if (mode == MSGS_ALL) {
609 is_seen = is_msg_in_sequence_set(
610 vbuf.v_seen, thismsg);
611 if (is_seen) lastold = thismsg;
617 || ((mode == MSGS_OLD) && (is_seen))
618 || ((mode == MSGS_NEW) && (!is_seen))
619 || ((mode == MSGS_LAST) && (a >= (num_msgs - ref)))
620 || ((mode == MSGS_FIRST) && (a < ref))
621 || ((mode == MSGS_GT) && (thismsg > ref))
622 || ((mode == MSGS_EQ) && (thismsg == ref))
625 if ((mode == MSGS_NEW) && (CC->user.flags & US_LASTOLD) && (lastold > 0L) && (printed_lastold == 0) && (!is_seen)) {
627 CallBack(lastold, userdata);
631 if (CallBack) CallBack(thismsg, userdata);
635 free(msglist); /* Clean up */
636 return num_processed;
642 * cmd_msgs() - get list of message #'s in this room
643 * implements the MSGS server command using CtdlForEachMessage()
645 void cmd_msgs(char *cmdbuf)
654 int with_template = 0;
655 struct CtdlMessage *template = NULL;
656 int with_headers = 0;
658 extract_token(which, cmdbuf, 0, '|', sizeof which);
659 cm_ref = extract_int(cmdbuf, 1);
660 with_template = extract_int(cmdbuf, 2);
661 with_headers = extract_int(cmdbuf, 3);
665 if (!strncasecmp(which, "OLD", 3))
667 else if (!strncasecmp(which, "NEW", 3))
669 else if (!strncasecmp(which, "FIRST", 5))
671 else if (!strncasecmp(which, "LAST", 4))
673 else if (!strncasecmp(which, "GT", 2))
676 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
677 cprintf("%d not logged in\n", ERROR + NOT_LOGGED_IN);
683 cprintf("%d Send template then receive message list\n",
685 template = (struct CtdlMessage *)
686 malloc(sizeof(struct CtdlMessage));
687 memset(template, 0, sizeof(struct CtdlMessage));
688 while(client_getln(buf, sizeof buf), strcmp(buf,"000")) {
689 extract_token(tfield, buf, 0, '|', sizeof tfield);
690 extract_token(tvalue, buf, 1, '|', sizeof tvalue);
691 for (i='A'; i<='Z'; ++i) if (msgkeys[i]!=NULL) {
692 if (!strcasecmp(tfield, msgkeys[i])) {
693 template->cm_fields[i] =
701 cprintf("%d \n", LISTING_FOLLOWS);
704 CtdlForEachMessage(mode,
708 (with_headers ? headers_listing : simple_listing),
711 if (template != NULL) CtdlFreeMessage(template);
719 * help_subst() - support routine for help file viewer
721 void help_subst(char *strbuf, char *source, char *dest)
726 while (p = pattern2(strbuf, source), (p >= 0)) {
727 strcpy(workbuf, &strbuf[p + strlen(source)]);
728 strcpy(&strbuf[p], dest);
729 strcat(strbuf, workbuf);
734 void do_help_subst(char *buffer)
738 help_subst(buffer, "^nodename", config.c_nodename);
739 help_subst(buffer, "^humannode", config.c_humannode);
740 help_subst(buffer, "^fqdn", config.c_fqdn);
741 help_subst(buffer, "^username", CC->user.fullname);
742 snprintf(buf2, sizeof buf2, "%ld", CC->user.usernum);
743 help_subst(buffer, "^usernum", buf2);
744 help_subst(buffer, "^sysadm", config.c_sysadm);
745 help_subst(buffer, "^variantname", CITADEL);
746 snprintf(buf2, sizeof buf2, "%d", config.c_maxsessions);
747 help_subst(buffer, "^maxsessions", buf2);
748 help_subst(buffer, "^bbsdir", CTDLDIR);
754 * memfmout() - Citadel text formatter and paginator.
755 * Although the original purpose of this routine was to format
756 * text to the reader's screen width, all we're really using it
757 * for here is to format text out to 80 columns before sending it
758 * to the client. The client software may reformat it again.
761 int width, /* screen width to use */
762 char *mptr, /* where are we going to get our text from? */
763 char subst, /* nonzero if we should do substitutions */
764 char *nl) /* string to terminate lines with */
776 c = 1; /* c is the current pos */
780 while (ch = *mptr, ((ch != 0) && (strlen(buffer) < 126))) {
782 buffer[strlen(buffer) + 1] = 0;
783 buffer[strlen(buffer)] = ch;
786 if (buffer[0] == '^')
787 do_help_subst(buffer);
789 buffer[strlen(buffer) + 1] = 0;
791 strcpy(buffer, &buffer[1]);
799 if (((ch == 13) || (ch == 10)) && (old != 13) && (old != 10))
801 if (((old == 13) || (old == 10)) && (isspace(real))) {
809 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
810 cprintf("%s%s", nl, aaa);
819 if ((strlen(aaa) + c) > (width - 5)) {
828 if ((ch == 13) || (ch == 10)) {
829 cprintf("%s%s", aaa, nl);
836 cprintf("%s%s", aaa, nl);
842 * Callback function for mime parser that simply lists the part
844 void list_this_part(char *name, char *filename, char *partnum, char *disp,
845 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
849 cprintf("part=%s|%s|%s|%s|%s|%ld\n",
850 name, filename, partnum, disp, cbtype, (long)length);
854 * Callback function for multipart prefix
856 void list_this_pref(char *name, char *filename, char *partnum, char *disp,
857 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
860 cprintf("pref=%s|%s\n", partnum, cbtype);
864 * Callback function for multipart sufffix
866 void list_this_suff(char *name, char *filename, char *partnum, char *disp,
867 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
870 cprintf("suff=%s|%s\n", partnum, cbtype);
875 * Callback function for mime parser that opens a section for downloading
877 void mime_download(char *name, char *filename, char *partnum, char *disp,
878 void *content, char *cbtype, char *cbcharset, size_t length,
879 char *encoding, void *cbuserdata)
882 /* Silently go away if there's already a download open... */
883 if (CC->download_fp != NULL)
886 /* ...or if this is not the desired section */
887 if (strcasecmp(CC->download_desired_section, partnum))
890 CC->download_fp = tmpfile();
891 if (CC->download_fp == NULL)
894 fwrite(content, length, 1, CC->download_fp);
895 fflush(CC->download_fp);
896 rewind(CC->download_fp);
898 OpenCmdResult(filename, cbtype);
904 * Load a message from disk into memory.
905 * This is used by CtdlOutputMsg() and other fetch functions.
907 * NOTE: Caller is responsible for freeing the returned CtdlMessage struct
908 * using the CtdlMessageFree() function.
910 struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body)
912 struct cdbdata *dmsgtext;
913 struct CtdlMessage *ret = NULL;
917 cit_uint8_t field_header;
919 lprintf(CTDL_DEBUG, "CtdlFetchMessage(%ld, %d)\n", msgnum, with_body);
921 dmsgtext = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
922 if (dmsgtext == NULL) {
925 mptr = dmsgtext->ptr;
926 upper_bound = mptr + dmsgtext->len;
928 /* Parse the three bytes that begin EVERY message on disk.
929 * The first is always 0xFF, the on-disk magic number.
930 * The second is the anonymous/public type byte.
931 * The third is the format type byte (vari, fixed, or MIME).
936 "Message %ld appears to be corrupted.\n",
941 ret = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
942 memset(ret, 0, sizeof(struct CtdlMessage));
944 ret->cm_magic = CTDLMESSAGE_MAGIC;
945 ret->cm_anon_type = *mptr++; /* Anon type byte */
946 ret->cm_format_type = *mptr++; /* Format type byte */
949 * The rest is zero or more arbitrary fields. Load them in.
950 * We're done when we encounter either a zero-length field or
951 * have just processed the 'M' (message text) field.
954 if (mptr >= upper_bound) {
957 field_header = *mptr++;
958 ret->cm_fields[field_header] = strdup(mptr);
960 while (*mptr++ != 0); /* advance to next field */
962 } while ((mptr < upper_bound) && (field_header != 'M'));
966 /* Always make sure there's something in the msg text field. If
967 * it's NULL, the message text is most likely stored separately,
968 * so go ahead and fetch that. Failing that, just set a dummy
969 * body so other code doesn't barf.
971 if ( (ret->cm_fields['M'] == NULL) && (with_body) ) {
972 dmsgtext = cdb_fetch(CDB_BIGMSGS, &msgnum, sizeof(long));
973 if (dmsgtext != NULL) {
974 ret->cm_fields['M'] = strdup(dmsgtext->ptr);
978 if (ret->cm_fields['M'] == NULL) {
979 ret->cm_fields['M'] = strdup("<no text>\n");
982 /* Perform "before read" hooks (aborting if any return nonzero) */
983 if (PerformMessageHooks(ret, EVT_BEFOREREAD) > 0) {
984 CtdlFreeMessage(ret);
993 * Returns 1 if the supplied pointer points to a valid Citadel message.
994 * If the pointer is NULL or the magic number check fails, returns 0.
996 int is_valid_message(struct CtdlMessage *msg) {
999 if ((msg->cm_magic) != CTDLMESSAGE_MAGIC) {
1000 lprintf(CTDL_WARNING, "is_valid_message() -- self-check failed\n");
1008 * 'Destructor' for struct CtdlMessage
1010 void CtdlFreeMessage(struct CtdlMessage *msg)
1014 if (is_valid_message(msg) == 0) return;
1016 for (i = 0; i < 256; ++i)
1017 if (msg->cm_fields[i] != NULL) {
1018 free(msg->cm_fields[i]);
1021 msg->cm_magic = 0; /* just in case */
1027 * Pre callback function for multipart/alternative
1029 * NOTE: this differs from the standard behavior for a reason. Normally when
1030 * displaying multipart/alternative you want to show the _last_ usable
1031 * format in the message. Here we show the _first_ one, because it's
1032 * usually text/plain. Since this set of functions is designed for text
1033 * output to non-MIME-aware clients, this is the desired behavior.
1036 void fixed_output_pre(char *name, char *filename, char *partnum, char *disp,
1037 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1042 ma = (struct ma_info *)cbuserdata;
1043 lprintf(CTDL_DEBUG, "fixed_output_pre() type=<%s>\n", cbtype);
1044 if (!strcasecmp(cbtype, "multipart/alternative")) {
1052 * Post callback function for multipart/alternative
1054 void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
1055 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1060 ma = (struct ma_info *)cbuserdata;
1061 lprintf(CTDL_DEBUG, "fixed_output_post() type=<%s>\n", cbtype);
1062 if (!strcasecmp(cbtype, "multipart/alternative")) {
1070 * Inline callback function for mime parser that wants to display text
1072 void fixed_output(char *name, char *filename, char *partnum, char *disp,
1073 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1081 ma = (struct ma_info *)cbuserdata;
1083 lprintf(CTDL_DEBUG, "fixed_output() type=<%s>\n", cbtype);
1086 * If we're in the middle of a multipart/alternative scope and
1087 * we've already printed another section, skip this one.
1089 if ( (ma->is_ma == 1) && (ma->did_print == 1) ) {
1090 lprintf(CTDL_DEBUG, "Skipping part %s (%s)\n", partnum, cbtype);
1095 if ( (!strcasecmp(cbtype, "text/plain"))
1096 || (strlen(cbtype)==0) ) {
1099 client_write(wptr, length);
1100 if (wptr[length-1] != '\n') {
1105 else if (!strcasecmp(cbtype, "text/html")) {
1106 ptr = html_to_ascii(content, 80, 0);
1108 client_write(ptr, wlen);
1109 if (ptr[wlen-1] != '\n') {
1114 else if (strncasecmp(cbtype, "multipart/", 10)) {
1115 cprintf("Part %s: %s (%s) (%ld bytes)\r\n",
1116 partnum, filename, cbtype, (long)length);
1121 * The client is elegant and sophisticated and wants to be choosy about
1122 * MIME content types, so figure out which multipart/alternative part
1123 * we're going to send.
1125 void choose_preferred(char *name, char *filename, char *partnum, char *disp,
1126 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1133 ma = (struct ma_info *)cbuserdata;
1135 if (ma->is_ma > 0) {
1136 for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
1137 extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
1138 if (!strcasecmp(buf, cbtype)) {
1139 strcpy(ma->chosen_part, partnum);
1146 * Now that we've chosen our preferred part, output it.
1148 void output_preferred(char *name, char *filename, char *partnum, char *disp,
1149 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
1154 int add_newline = 0;
1158 ma = (struct ma_info *)cbuserdata;
1160 /* This is not the MIME part you're looking for... */
1161 if (strcasecmp(partnum, ma->chosen_part)) return;
1163 /* If the content-type of this part is in our preferred formats
1164 * list, we can simply output it verbatim.
1166 for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
1167 extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
1168 if (!strcasecmp(buf, cbtype)) {
1169 /* Yeah! Go! W00t!! */
1171 text_content = (char *)content;
1172 if (text_content[length-1] != '\n') {
1176 cprintf("Content-type: %s", cbtype);
1177 if (strlen(cbcharset) > 0) {
1178 cprintf("; charset=%s", cbcharset);
1180 cprintf("\nContent-length: %d\n",
1181 (int)(length + add_newline) );
1182 if (strlen(encoding) > 0) {
1183 cprintf("Content-transfer-encoding: %s\n", encoding);
1186 cprintf("Content-transfer-encoding: 7bit\n");
1189 client_write(content, length);
1190 if (add_newline) cprintf("\n");
1195 /* No translations required or possible: output as text/plain */
1196 cprintf("Content-type: text/plain\n\n");
1197 fixed_output(name, filename, partnum, disp, content, cbtype, cbcharset,
1198 length, encoding, cbuserdata);
1203 * Get a message off disk. (returns om_* values found in msgbase.h)
1206 int CtdlOutputMsg(long msg_num, /* message number (local) to fetch */
1207 int mode, /* how would you like that message? */
1208 int headers_only, /* eschew the message body? */
1209 int do_proto, /* do Citadel protocol responses? */
1210 int crlf /* Use CRLF newlines instead of LF? */
1212 struct CtdlMessage *TheMessage = NULL;
1213 int retcode = om_no_such_msg;
1215 lprintf(CTDL_DEBUG, "CtdlOutputMsg() msgnum=%ld, mode=%d\n",
1218 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1219 if (do_proto) cprintf("%d Not logged in.\n",
1220 ERROR + NOT_LOGGED_IN);
1221 return(om_not_logged_in);
1224 /* FIXME: check message id against msglist for this room */
1227 * Fetch the message from disk. If we're in any sort of headers
1228 * only mode, request that we don't even bother loading the body
1231 if ( (headers_only == HEADERS_FAST) || (headers_only == HEADERS_ONLY) ) {
1232 TheMessage = CtdlFetchMessage(msg_num, 0);
1235 TheMessage = CtdlFetchMessage(msg_num, 1);
1238 if (TheMessage == NULL) {
1239 if (do_proto) cprintf("%d Can't locate msg %ld on disk\n",
1240 ERROR + MESSAGE_NOT_FOUND, msg_num);
1241 return(om_no_such_msg);
1244 retcode = CtdlOutputPreLoadedMsg(
1245 TheMessage, msg_num, mode,
1246 headers_only, do_proto, crlf);
1248 CtdlFreeMessage(TheMessage);
1255 * Get a message off disk. (returns om_* values found in msgbase.h)
1258 int CtdlOutputPreLoadedMsg(
1259 struct CtdlMessage *TheMessage,
1261 int mode, /* how would you like that message? */
1262 int headers_only, /* eschew the message body? */
1263 int do_proto, /* do Citadel protocol responses? */
1264 int crlf /* Use CRLF newlines instead of LF? */
1270 char display_name[256];
1272 char *nl; /* newline string */
1274 int subject_found = 0;
1277 /* Buffers needed for RFC822 translation. These are all filled
1278 * using functions that are bounds-checked, and therefore we can
1279 * make them substantially smaller than SIZ.
1287 char datestamp[100];
1289 lprintf(CTDL_DEBUG, "CtdlOutputPreLoadedMsg(TheMessage=%s, %ld, %d, %d, %d, %d\n",
1290 ((TheMessage == NULL) ? "NULL" : "not null"),
1292 mode, headers_only, do_proto, crlf);
1294 snprintf(mid, sizeof mid, "%ld", msg_num);
1295 nl = (crlf ? "\r\n" : "\n");
1297 if (!is_valid_message(TheMessage)) {
1299 "ERROR: invalid preloaded message for output\n");
1300 return(om_no_such_msg);
1303 /* Are we downloading a MIME component? */
1304 if (mode == MT_DOWNLOAD) {
1305 if (TheMessage->cm_format_type != FMT_RFC822) {
1307 cprintf("%d This is not a MIME message.\n",
1308 ERROR + ILLEGAL_VALUE);
1309 } else if (CC->download_fp != NULL) {
1310 if (do_proto) cprintf(
1311 "%d You already have a download open.\n",
1312 ERROR + RESOURCE_BUSY);
1314 /* Parse the message text component */
1315 mptr = TheMessage->cm_fields['M'];
1316 ma = malloc(sizeof(struct ma_info));
1317 memset(ma, 0, sizeof(struct ma_info));
1318 mime_parser(mptr, NULL, *mime_download, NULL, NULL, (void *)ma, 0);
1320 /* If there's no file open by this time, the requested
1321 * section wasn't found, so print an error
1323 if (CC->download_fp == NULL) {
1324 if (do_proto) cprintf(
1325 "%d Section %s not found.\n",
1326 ERROR + FILE_NOT_FOUND,
1327 CC->download_desired_section);
1330 return((CC->download_fp != NULL) ? om_ok : om_mime_error);
1333 /* now for the user-mode message reading loops */
1334 if (do_proto) cprintf("%d Message %ld:\n", LISTING_FOLLOWS, msg_num);
1336 /* Does the caller want to skip the headers? */
1337 if (headers_only == HEADERS_NONE) goto START_TEXT;
1339 /* Tell the client which format type we're using. */
1340 if ( (mode == MT_CITADEL) && (do_proto) ) {
1341 cprintf("type=%d\n", TheMessage->cm_format_type);
1344 /* nhdr=yes means that we're only displaying headers, no body */
1345 if ( (TheMessage->cm_anon_type == MES_ANONONLY)
1346 && (mode == MT_CITADEL)
1349 cprintf("nhdr=yes\n");
1352 /* begin header processing loop for Citadel message format */
1354 if ((mode == MT_CITADEL) || (mode == MT_MIME)) {
1356 safestrncpy(display_name, "<unknown>", sizeof display_name);
1357 if (TheMessage->cm_fields['A']) {
1358 strcpy(buf, TheMessage->cm_fields['A']);
1359 if (TheMessage->cm_anon_type == MES_ANONONLY) {
1360 safestrncpy(display_name, "****", sizeof display_name);
1362 else if (TheMessage->cm_anon_type == MES_ANONOPT) {
1363 safestrncpy(display_name, "anonymous", sizeof display_name);
1366 safestrncpy(display_name, buf, sizeof display_name);
1368 if ((is_room_aide())
1369 && ((TheMessage->cm_anon_type == MES_ANONONLY)
1370 || (TheMessage->cm_anon_type == MES_ANONOPT))) {
1371 size_t tmp = strlen(display_name);
1372 snprintf(&display_name[tmp],
1373 sizeof display_name - tmp,
1378 /* Don't show Internet address for users on the
1379 * local Citadel network.
1382 if (TheMessage->cm_fields['N'] != NULL)
1383 if (strlen(TheMessage->cm_fields['N']) > 0)
1384 if (haschar(TheMessage->cm_fields['N'], '.') == 0) {
1388 /* Now spew the header fields in the order we like them. */
1389 safestrncpy(allkeys, FORDER, sizeof allkeys);
1390 for (i=0; i<strlen(allkeys); ++i) {
1391 k = (int) allkeys[i];
1393 if ( (TheMessage->cm_fields[k] != NULL)
1394 && (msgkeys[k] != NULL) ) {
1396 if (do_proto) cprintf("%s=%s\n",
1400 else if ((k == 'F') && (suppress_f)) {
1403 /* Masquerade display name if needed */
1405 if (do_proto) cprintf("%s=%s\n",
1407 TheMessage->cm_fields[k]
1416 /* begin header processing loop for RFC822 transfer format */
1421 strcpy(snode, NODENAME);
1422 strcpy(lnode, HUMANNODE);
1423 if (mode == MT_RFC822) {
1424 for (i = 0; i < 256; ++i) {
1425 if (TheMessage->cm_fields[i]) {
1426 mptr = TheMessage->cm_fields[i];
1429 safestrncpy(luser, mptr, sizeof luser);
1430 safestrncpy(suser, mptr, sizeof suser);
1432 else if (i == 'Y') {
1433 cprintf("CC: %s%s", mptr, nl);
1435 else if (i == 'U') {
1436 cprintf("Subject: %s%s", mptr, nl);
1440 safestrncpy(mid, mptr, sizeof mid);
1442 safestrncpy(lnode, mptr, sizeof lnode);
1444 safestrncpy(fuser, mptr, sizeof fuser);
1445 /* else if (i == 'O')
1446 cprintf("X-Citadel-Room: %s%s",
1449 safestrncpy(snode, mptr, sizeof snode);
1451 cprintf("To: %s%s", mptr, nl);
1452 else if (i == 'T') {
1453 datestring(datestamp, sizeof datestamp,
1454 atol(mptr), DATESTRING_RFC822);
1455 cprintf("Date: %s%s", datestamp, nl);
1459 if (subject_found == 0) {
1460 cprintf("Subject: (no subject)%s", nl);
1464 for (i=0; i<strlen(suser); ++i) {
1465 suser[i] = tolower(suser[i]);
1466 if (!isalnum(suser[i])) suser[i]='_';
1469 if (mode == MT_RFC822) {
1470 if (!strcasecmp(snode, NODENAME)) {
1471 safestrncpy(snode, FQDN, sizeof snode);
1474 /* Construct a fun message id */
1475 cprintf("Message-ID: <%s", mid);
1476 if (strchr(mid, '@')==NULL) {
1477 cprintf("@%s", snode);
1481 if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONONLY)) {
1482 // cprintf("From: x@x.org (----)%s", nl);
1483 cprintf("From: \"----\" <x@x.org>%s", nl);
1485 else if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONOPT)) {
1486 // cprintf("From: x@x.org (anonymous)%s", nl);
1487 cprintf("From: \"anonymous\" <x@x.org>%s", nl);
1489 else if (strlen(fuser) > 0) {
1490 // cprintf("From: %s (%s)%s", fuser, luser, nl);
1491 cprintf("From: \"%s\" <%s>%s", luser, fuser, nl);
1494 // cprintf("From: %s@%s (%s)%s", suser, snode, luser, nl);
1495 cprintf("From: \"%s\" <%s@%s>%s", luser, suser, snode, nl);
1498 cprintf("Organization: %s%s", lnode, nl);
1500 /* Blank line signifying RFC822 end-of-headers */
1501 if (TheMessage->cm_format_type != FMT_RFC822) {
1506 /* end header processing loop ... at this point, we're in the text */
1508 if (headers_only == HEADERS_FAST) goto DONE;
1509 mptr = TheMessage->cm_fields['M'];
1511 /* Tell the client about the MIME parts in this message */
1512 if (TheMessage->cm_format_type == FMT_RFC822) {
1513 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
1514 mime_parser(mptr, NULL,
1515 (do_proto ? *list_this_part : NULL),
1516 (do_proto ? *list_this_pref : NULL),
1517 (do_proto ? *list_this_suff : NULL),
1520 else if (mode == MT_RFC822) { /* unparsed RFC822 dump */
1521 /* FIXME ... we have to put some code in here to avoid
1522 * printing duplicate header information when both
1523 * Citadel and RFC822 headers exist. Preference should
1524 * probably be given to the RFC822 headers.
1526 int done_rfc822_hdrs = 0;
1527 while (ch=*(mptr++), ch!=0) {
1532 if (!done_rfc822_hdrs) {
1533 if (headers_only != HEADERS_NONE) {
1538 if (headers_only != HEADERS_ONLY) {
1542 if ((*(mptr) == 13) || (*(mptr) == 10)) {
1543 done_rfc822_hdrs = 1;
1547 if (done_rfc822_hdrs) {
1548 if (headers_only != HEADERS_NONE) {
1553 if (headers_only != HEADERS_ONLY) {
1557 if ((*mptr == 13) || (*mptr == 10)) {
1558 done_rfc822_hdrs = 1;
1566 if (headers_only == HEADERS_ONLY) {
1570 /* signify start of msg text */
1571 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
1572 if (do_proto) cprintf("text\n");
1575 /* If the format type on disk is 1 (fixed-format), then we want
1576 * everything to be output completely literally ... regardless of
1577 * what message transfer format is in use.
1579 if (TheMessage->cm_format_type == FMT_FIXED) {
1580 if (mode == MT_MIME) {
1581 cprintf("Content-type: text/plain\n\n");
1584 while (ch = *mptr++, ch > 0) {
1587 if ((ch == 10) || (strlen(buf) > 250)) {
1588 cprintf("%s%s", buf, nl);
1591 buf[strlen(buf) + 1] = 0;
1592 buf[strlen(buf)] = ch;
1595 if (strlen(buf) > 0)
1596 cprintf("%s%s", buf, nl);
1599 /* If the message on disk is format 0 (Citadel vari-format), we
1600 * output using the formatter at 80 columns. This is the final output
1601 * form if the transfer format is RFC822, but if the transfer format
1602 * is Citadel proprietary, it'll still work, because the indentation
1603 * for new paragraphs is correct and the client will reformat the
1604 * message to the reader's screen width.
1606 if (TheMessage->cm_format_type == FMT_CITADEL) {
1607 if (mode == MT_MIME) {
1608 cprintf("Content-type: text/x-citadel-variformat\n\n");
1610 memfmout(80, mptr, 0, nl);
1613 /* If the message on disk is format 4 (MIME), we've gotta hand it
1614 * off to the MIME parser. The client has already been told that
1615 * this message is format 1 (fixed format), so the callback function
1616 * we use will display those parts as-is.
1618 if (TheMessage->cm_format_type == FMT_RFC822) {
1619 ma = malloc(sizeof(struct ma_info));
1620 memset(ma, 0, sizeof(struct ma_info));
1622 if (mode == MT_MIME) {
1623 strcpy(ma->chosen_part, "1");
1624 mime_parser(mptr, NULL,
1625 *choose_preferred, *fixed_output_pre,
1626 *fixed_output_post, (void *)ma, 0);
1627 mime_parser(mptr, NULL,
1628 *output_preferred, NULL, NULL, (void *)ma, 0);
1631 mime_parser(mptr, NULL,
1632 *fixed_output, *fixed_output_pre,
1633 *fixed_output_post, (void *)ma, 0);
1639 DONE: /* now we're done */
1640 if (do_proto) cprintf("000\n");
1647 * display a message (mode 0 - Citadel proprietary)
1649 void cmd_msg0(char *cmdbuf)
1652 int headers_only = HEADERS_ALL;
1654 msgid = extract_long(cmdbuf, 0);
1655 headers_only = extract_int(cmdbuf, 1);
1657 CtdlOutputMsg(msgid, MT_CITADEL, headers_only, 1, 0);
1663 * display a message (mode 2 - RFC822)
1665 void cmd_msg2(char *cmdbuf)
1668 int headers_only = HEADERS_ALL;
1670 msgid = extract_long(cmdbuf, 0);
1671 headers_only = extract_int(cmdbuf, 1);
1673 CtdlOutputMsg(msgid, MT_RFC822, headers_only, 1, 1);
1679 * display a message (mode 3 - IGnet raw format - internal programs only)
1681 void cmd_msg3(char *cmdbuf)
1684 struct CtdlMessage *msg;
1687 if (CC->internal_pgm == 0) {
1688 cprintf("%d This command is for internal programs only.\n",
1689 ERROR + HIGHER_ACCESS_REQUIRED);
1693 msgnum = extract_long(cmdbuf, 0);
1694 msg = CtdlFetchMessage(msgnum, 1);
1696 cprintf("%d Message %ld not found.\n",
1697 ERROR + MESSAGE_NOT_FOUND, msgnum);
1701 serialize_message(&smr, msg);
1702 CtdlFreeMessage(msg);
1705 cprintf("%d Unable to serialize message\n",
1706 ERROR + INTERNAL_ERROR);
1710 cprintf("%d %ld\n", BINARY_FOLLOWS, (long)smr.len);
1711 client_write((char *)smr.ser, (int)smr.len);
1718 * Display a message using MIME content types
1720 void cmd_msg4(char *cmdbuf)
1724 msgid = extract_long(cmdbuf, 0);
1725 CtdlOutputMsg(msgid, MT_MIME, 0, 1, 0);
1731 * Client tells us its preferred message format(s)
1733 void cmd_msgp(char *cmdbuf)
1735 safestrncpy(CC->preferred_formats, cmdbuf,
1736 sizeof(CC->preferred_formats));
1737 cprintf("%d ok\n", CIT_OK);
1742 * Open a component of a MIME message as a download file
1744 void cmd_opna(char *cmdbuf)
1747 char desired_section[128];
1749 msgid = extract_long(cmdbuf, 0);
1750 extract_token(desired_section, cmdbuf, 1, '|', sizeof desired_section);
1751 safestrncpy(CC->download_desired_section, desired_section, sizeof CC->download_desired_section);
1752 CtdlOutputMsg(msgid, MT_DOWNLOAD, 0, 1, 1);
1757 * Save a message pointer into a specified room
1758 * (Returns 0 for success, nonzero for failure)
1759 * roomname may be NULL to use the current room
1761 int CtdlSaveMsgPointerInRoom(char *roomname, long msgid, int flags) {
1763 char hold_rm[ROOMNAMELEN];
1764 struct cdbdata *cdbfr;
1767 long highest_msg = 0L;
1768 struct CtdlMessage *msg = NULL;
1770 lprintf(CTDL_DEBUG, "CtdlSaveMsgPointerInRoom(%s, %ld, %d)\n",
1771 roomname, msgid, flags);
1773 strcpy(hold_rm, CC->room.QRname);
1775 /* We may need to check to see if this message is real */
1776 if ( (flags & SM_VERIFY_GOODNESS)
1777 || (flags & SM_DO_REPL_CHECK)
1779 msg = CtdlFetchMessage(msgid, 1);
1780 if (msg == NULL) return(ERROR + ILLEGAL_VALUE);
1783 /* Perform replication checks if necessary */
1784 if ( (flags & SM_DO_REPL_CHECK) && (msg != NULL) ) {
1786 if (getroom(&CC->room,
1787 ((roomname != NULL) ? roomname : CC->room.QRname) )
1789 lprintf(CTDL_ERR, "No such room <%s>\n", roomname);
1790 if (msg != NULL) CtdlFreeMessage(msg);
1791 return(ERROR + ROOM_NOT_FOUND);
1794 if (ReplicationChecks(msg) != 0) {
1795 getroom(&CC->room, hold_rm);
1796 if (msg != NULL) CtdlFreeMessage(msg);
1798 "Did replication, and newer exists\n");
1803 /* Now the regular stuff */
1804 if (lgetroom(&CC->room,
1805 ((roomname != NULL) ? roomname : CC->room.QRname) )
1807 lprintf(CTDL_ERR, "No such room <%s>\n", roomname);
1808 if (msg != NULL) CtdlFreeMessage(msg);
1809 return(ERROR + ROOM_NOT_FOUND);
1812 cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
1813 if (cdbfr == NULL) {
1817 msglist = malloc(cdbfr->len);
1818 if (msglist == NULL)
1819 lprintf(CTDL_ALERT, "ERROR malloc msglist!\n");
1820 num_msgs = cdbfr->len / sizeof(long);
1821 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1826 /* Make sure the message doesn't already exist in this room. It
1827 * is absolutely taboo to have more than one reference to the same
1828 * message in a room.
1830 if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
1831 if (msglist[i] == msgid) {
1832 lputroom(&CC->room); /* unlock the room */
1833 getroom(&CC->room, hold_rm);
1834 if (msg != NULL) CtdlFreeMessage(msg);
1836 return(ERROR + ALREADY_EXISTS);
1840 /* Now add the new message */
1842 msglist = realloc(msglist, (num_msgs * sizeof(long)));
1844 if (msglist == NULL) {
1845 lprintf(CTDL_ALERT, "ERROR: can't realloc message list!\n");
1847 msglist[num_msgs - 1] = msgid;
1849 /* Sort the message list, so all the msgid's are in order */
1850 num_msgs = sort_msglist(msglist, num_msgs);
1852 /* Determine the highest message number */
1853 highest_msg = msglist[num_msgs - 1];
1855 /* Write it back to disk. */
1856 cdb_store(CDB_MSGLISTS, &CC->room.QRnumber, (int)sizeof(long),
1857 msglist, (int)(num_msgs * sizeof(long)));
1859 /* Free up the memory we used. */
1862 /* Update the highest-message pointer and unlock the room. */
1863 CC->room.QRhighest = highest_msg;
1864 lputroom(&CC->room);
1865 getroom(&CC->room, hold_rm);
1867 /* Bump the reference count for this message. */
1868 if ((flags & SM_DONT_BUMP_REF)==0) {
1869 AdjRefCount(msgid, +1);
1872 /* Return success. */
1873 if (msg != NULL) CtdlFreeMessage(msg);
1880 * Message base operation to save a new message to the message store
1881 * (returns new message number)
1883 * This is the back end for CtdlSubmitMsg() and should not be directly
1884 * called by server-side modules.
1887 long send_message(struct CtdlMessage *msg) {
1895 /* Get a new message number */
1896 newmsgid = get_new_message_number();
1897 snprintf(msgidbuf, sizeof msgidbuf, "%ld@%s", newmsgid, config.c_fqdn);
1899 /* Generate an ID if we don't have one already */
1900 if (msg->cm_fields['I']==NULL) {
1901 msg->cm_fields['I'] = strdup(msgidbuf);
1904 /* If the message is big, set its body aside for storage elsewhere */
1905 if (msg->cm_fields['M'] != NULL) {
1906 if (strlen(msg->cm_fields['M']) > BIGMSG) {
1908 holdM = msg->cm_fields['M'];
1909 msg->cm_fields['M'] = NULL;
1913 /* Serialize our data structure for storage in the database */
1914 serialize_message(&smr, msg);
1917 msg->cm_fields['M'] = holdM;
1921 cprintf("%d Unable to serialize message\n",
1922 ERROR + INTERNAL_ERROR);
1926 /* Write our little bundle of joy into the message base */
1927 if (cdb_store(CDB_MSGMAIN, &newmsgid, (int)sizeof(long),
1928 smr.ser, smr.len) < 0) {
1929 lprintf(CTDL_ERR, "Can't store message\n");
1933 cdb_store(CDB_BIGMSGS,
1943 /* Free the memory we used for the serialized message */
1946 /* Return the *local* message ID to the caller
1947 * (even if we're storing an incoming network message)
1955 * Serialize a struct CtdlMessage into the format used on disk and network.
1957 * This function loads up a "struct ser_ret" (defined in server.h) which
1958 * contains the length of the serialized message and a pointer to the
1959 * serialized message in memory. THE LATTER MUST BE FREED BY THE CALLER.
1961 void serialize_message(struct ser_ret *ret, /* return values */
1962 struct CtdlMessage *msg) /* unserialized msg */
1966 static char *forder = FORDER;
1968 if (is_valid_message(msg) == 0) return; /* self check */
1971 for (i=0; i<26; ++i) if (msg->cm_fields[(int)forder[i]] != NULL)
1972 ret->len = ret->len +
1973 strlen(msg->cm_fields[(int)forder[i]]) + 2;
1975 lprintf(CTDL_DEBUG, "serialize_message() calling malloc(%ld)\n", (long)ret->len);
1976 ret->ser = malloc(ret->len);
1977 if (ret->ser == NULL) {
1983 ret->ser[1] = msg->cm_anon_type;
1984 ret->ser[2] = msg->cm_format_type;
1987 for (i=0; i<26; ++i) if (msg->cm_fields[(int)forder[i]] != NULL) {
1988 ret->ser[wlen++] = (char)forder[i];
1989 strcpy((char *)&ret->ser[wlen], msg->cm_fields[(int)forder[i]]);
1990 wlen = wlen + strlen(msg->cm_fields[(int)forder[i]]) + 1;
1992 if (ret->len != wlen) lprintf(CTDL_ERR, "ERROR: len=%ld wlen=%ld\n",
1993 (long)ret->len, (long)wlen);
2001 * Back end for the ReplicationChecks() function
2003 void check_repl(long msgnum, void *userdata) {
2004 lprintf(CTDL_DEBUG, "check_repl() replacing message %ld\n", msgnum);
2005 CtdlDeleteMessages(CC->room.QRname, msgnum, "", 0);
2010 * Check to see if any messages already exist which carry the same Exclusive ID
2011 * as this one. If any are found, delete them.
2014 int ReplicationChecks(struct CtdlMessage *msg) {
2015 struct CtdlMessage *template;
2018 /* No exclusive id? Don't do anything. */
2019 if (msg->cm_fields['E'] == NULL) return 0;
2020 if (strlen(msg->cm_fields['E']) == 0) return 0;
2021 lprintf(CTDL_DEBUG, "Exclusive ID: <%s>\n", msg->cm_fields['E']);
2023 template = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
2024 memset(template, 0, sizeof(struct CtdlMessage));
2025 template->cm_fields['E'] = strdup(msg->cm_fields['E']);
2027 CtdlForEachMessage(MSGS_ALL, 0L, NULL, template, check_repl, NULL);
2029 CtdlFreeMessage(template);
2030 lprintf(CTDL_DEBUG, "ReplicationChecks() returning %d\n", abort_this);
2038 * Save a message to disk and submit it into the delivery system.
2040 long CtdlSubmitMsg(struct CtdlMessage *msg, /* message to save */
2041 struct recptypes *recps_to, /* To: recipients (if mail) */
2042 struct recptypes *recps_cc, /* Cc: recipients (if mail) */
2043 struct recptypes *recps_bcc, /* Bcc: recipients (if mail) */
2044 char *force /* force a particular room? */
2046 char submit_filename[128];
2047 char generated_timestamp[32];
2048 char hold_rm[ROOMNAMELEN];
2049 char actual_rm[ROOMNAMELEN];
2050 char force_room[ROOMNAMELEN];
2051 char content_type[SIZ]; /* We have to learn this */
2052 char recipient[SIZ];
2055 struct ctdluser userbuf;
2057 struct MetaData smi;
2058 FILE *network_fp = NULL;
2059 static int seqnum = 1;
2060 struct CtdlMessage *imsg = NULL;
2063 char *hold_R, *hold_D;
2064 int is_internet_mail = 0;
2065 int force_to_sent = 0; /* Force to 'sent items' room */
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 lprintf(CTDL_DEBUG, "Generating timestamp\n");
2076 snprintf(generated_timestamp, sizeof generated_timestamp, "%ld", (long)time(NULL));
2077 msg->cm_fields['T'] = strdup(generated_timestamp);
2080 /* If this message has no path, we generate one.
2082 if (msg->cm_fields['P'] == NULL) {
2083 lprintf(CTDL_DEBUG, "Generating path\n");
2084 if (msg->cm_fields['A'] != NULL) {
2085 msg->cm_fields['P'] = strdup(msg->cm_fields['A']);
2086 for (a=0; a<strlen(msg->cm_fields['P']); ++a) {
2087 if (isspace(msg->cm_fields['P'][a])) {
2088 msg->cm_fields['P'][a] = ' ';
2093 msg->cm_fields['P'] = strdup("unknown");
2097 if (force == NULL) {
2098 strcpy(force_room, "");
2101 strcpy(force_room, force);
2104 /* Learn about what's inside, because it's what's inside that counts */
2105 lprintf(CTDL_DEBUG, "Learning what's inside\n");
2106 if (msg->cm_fields['M'] == NULL) {
2107 lprintf(CTDL_ERR, "ERROR: attempt to save message with NULL body\n");
2111 switch (msg->cm_format_type) {
2113 strcpy(content_type, "text/x-citadel-variformat");
2116 strcpy(content_type, "text/plain");
2119 strcpy(content_type, "text/plain");
2120 mptr = bmstrcasestr(msg->cm_fields['M'], "Content-type: ");
2122 safestrncpy(content_type, &mptr[14],
2123 sizeof content_type);
2124 for (a = 0; a < strlen(content_type); ++a) {
2125 if ((content_type[a] == ';')
2126 || (content_type[a] == ' ')
2127 || (content_type[a] == 13)
2128 || (content_type[a] == 10)) {
2129 content_type[a] = 0;
2135 /* Goto the correct room */
2137 if (recps_to) force_to_sent = 1;
2138 if (recps_cc) force_to_sent = 1;
2139 if (recps_bcc) force_to_sent = 1;
2140 strcpy(hold_rm, CC->room.QRname);
2141 strcpy(actual_rm, CC->room.QRname);
2142 if (force_to_sent) {
2143 strcpy(actual_rm, SENTITEMS);
2145 lprintf(CTDL_DEBUG, "Selected room %s\n", actual_rm);
2147 /* If the user is a twit, move to the twit room for posting */
2148 lprintf(CTDL_DEBUG, "Handling twit stuff: %s\n",
2149 (CC->user.axlevel == 2) ? config.c_twitroom : "OK");
2151 if (CC->user.axlevel == 2) {
2152 strcpy(hold_rm, actual_rm);
2153 strcpy(actual_rm, config.c_twitroom);
2157 /* ...or if this message is destined for Aide> then go there. */
2158 if (strlen(force_room) > 0) {
2159 strcpy(actual_rm, force_room);
2162 lprintf(CTDL_DEBUG, "Final selection: %s\n", actual_rm);
2163 if (strcasecmp(actual_rm, CC->room.QRname)) {
2164 /* getroom(&CC->room, actual_rm); */
2165 usergoto(actual_rm, 0, 1, NULL, NULL);
2169 * If this message has no O (room) field, generate one.
2171 if (msg->cm_fields['O'] == NULL) {
2172 msg->cm_fields['O'] = strdup(CC->room.QRname);
2175 /* Perform "before save" hooks (aborting if any return nonzero) */
2176 lprintf(CTDL_DEBUG, "Performing before-save hooks\n");
2177 if (PerformMessageHooks(msg, EVT_BEFORESAVE) > 0) return(-3);
2179 /* If this message has an Exclusive ID, perform replication checks */
2180 lprintf(CTDL_DEBUG, "Performing replication checks\n");
2181 if (ReplicationChecks(msg) > 0) return(-4);
2183 /* Save it to disk */
2184 lprintf(CTDL_DEBUG, "Saving to disk\n");
2185 newmsgid = send_message(msg);
2186 if (newmsgid <= 0L) return(-5);
2188 /* Write a supplemental message info record. This doesn't have to
2189 * be a critical section because nobody else knows about this message
2192 lprintf(CTDL_DEBUG, "Creating MetaData record\n");
2193 memset(&smi, 0, sizeof(struct MetaData));
2194 smi.meta_msgnum = newmsgid;
2195 smi.meta_refcount = 0;
2196 safestrncpy(smi.meta_content_type, content_type,
2197 sizeof smi.meta_content_type);
2199 /* As part of the new metadata record, measure how
2200 * big this message will be when displayed as RFC822.
2201 * Both POP and IMAP use this, and it's best to just take the hit now
2202 * instead of having to potentially measure thousands of messages when
2203 * a mailbox is opened later.
2206 if (CC->redirect_buffer != NULL) {
2207 lprintf(CTDL_ALERT, "CC->redirect_buffer is not NULL during message submission!\n");
2210 CC->redirect_buffer = malloc(SIZ);
2211 CC->redirect_len = 0;
2212 CC->redirect_alloc = SIZ;
2213 CtdlOutputPreLoadedMsg(msg, 0L, MT_RFC822, HEADERS_ALL, 0, 1);
2214 smi.meta_rfc822_length = CC->redirect_len;
2215 free(CC->redirect_buffer);
2216 CC->redirect_buffer = NULL;
2217 CC->redirect_len = 0;
2218 CC->redirect_alloc = 0;
2222 /* Now figure out where to store the pointers */
2223 lprintf(CTDL_DEBUG, "Storing pointers\n");
2225 /* If this is being done by the networker delivering a private
2226 * message, we want to BYPASS saving the sender's copy (because there
2227 * is no local sender; it would otherwise go to the Trashcan).
2229 if ((!CC->internal_pgm) || ((recps_to == NULL) && (recps_cc == NULL) && (recps_bcc == NULL))) {
2230 if (CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0) != 0) {
2231 lprintf(CTDL_ERR, "ERROR saving message pointer!\n");
2232 CtdlSaveMsgPointerInRoom(config.c_aideroom,
2237 /* For internet mail, drop a copy in the outbound queue room */
2238 is_internet_mail = 0;
2239 if (recps_to != NULL) is_internet_mail = recps_to->num_internet;
2240 if (recps_cc != NULL) is_internet_mail = recps_cc->num_internet;
2241 if (recps_bcc != NULL) is_internet_mail = recps_bcc->num_internet;
2242 if (is_internet_mail) {
2243 CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, newmsgid, 0);
2246 /* If other rooms are specified in the To: field, drop them there too. */
2247 /******* FIXME FIXME ADD CC AND BCC HERE *********/
2248 if (recps_to != NULL)
2249 if (recps_to->num_room > 0)
2250 for (i=0; i<num_tokens(recps_to->recp_room, '|'); ++i) {
2251 extract_token(recipient, recps_to->recp_room, i,
2252 '|', sizeof recipient);
2253 lprintf(CTDL_DEBUG, "Delivering to room <%s>\n", recipient);
2254 CtdlSaveMsgPointerInRoom(recipient, newmsgid, 0);
2257 /* Bump this user's messages posted counter. */
2258 lprintf(CTDL_DEBUG, "Updating user\n");
2259 lgetuser(&CC->user, CC->curr_user);
2260 CC->user.posted = CC->user.posted + 1;
2261 lputuser(&CC->user);
2263 /* If this is private, local mail, make a copy in the
2264 * recipient's mailbox and bump the reference count.
2266 /******* FIXME FIXME ADD CC AND BCC HERE *********/
2267 if (recps_to != NULL)
2268 if (recps_to->num_local > 0)
2269 for (i=0; i<num_tokens(recps_to->recp_local, '|'); ++i) {
2270 extract_token(recipient, recps_to->recp_local, i,
2271 '|', sizeof recipient);
2272 lprintf(CTDL_DEBUG, "Delivering private local mail to <%s>\n",
2274 if (getuser(&userbuf, recipient) == 0) {
2275 MailboxName(actual_rm, sizeof actual_rm,
2276 &userbuf, MAILROOM);
2277 CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0);
2278 BumpNewMailCounter(userbuf.usernum);
2281 lprintf(CTDL_DEBUG, "No user <%s>\n", recipient);
2282 CtdlSaveMsgPointerInRoom(config.c_aideroom,
2287 /* Perform "after save" hooks */
2288 lprintf(CTDL_DEBUG, "Performing after-save hooks\n");
2289 PerformMessageHooks(msg, EVT_AFTERSAVE);
2291 /* For IGnet mail, we have to save a new copy into the spooler for
2292 * each recipient, with the R and D fields set to the recipient and
2293 * destination-node. This has two ugly side effects: all other
2294 * recipients end up being unlisted in this recipient's copy of the
2295 * message, and it has to deliver multiple messages to the same
2296 * node. We'll revisit this again in a year or so when everyone has
2297 * a network spool receiver that can handle the new style messages.
2299 /******* FIXME FIXME ADD CC AND BCC HERE *********/
2300 if (recps_to != NULL)
2301 if (recps_to->num_ignet > 0)
2302 for (i=0; i<num_tokens(recps_to->recp_ignet, '|'); ++i) {
2303 extract_token(recipient, recps_to->recp_ignet, i,
2304 '|', sizeof recipient);
2306 hold_R = msg->cm_fields['R'];
2307 hold_D = msg->cm_fields['D'];
2308 msg->cm_fields['R'] = malloc(SIZ);
2309 msg->cm_fields['D'] = malloc(128);
2310 extract_token(msg->cm_fields['R'], recipient, 0, '@', SIZ);
2311 extract_token(msg->cm_fields['D'], recipient, 1, '@', 128);
2313 serialize_message(&smr, msg);
2315 snprintf(submit_filename, sizeof submit_filename,
2316 #ifndef HAVE_SPOOL_DIR
2321 "/network/spoolin/netmail.%04lx.%04x.%04x",
2322 (long) getpid(), CC->cs_pid, ++seqnum);
2323 network_fp = fopen(submit_filename, "wb+");
2324 if (network_fp != NULL) {
2325 fwrite(smr.ser, smr.len, 1, network_fp);
2331 free(msg->cm_fields['R']);
2332 free(msg->cm_fields['D']);
2333 msg->cm_fields['R'] = hold_R;
2334 msg->cm_fields['D'] = hold_D;
2337 /* Go back to the room we started from */
2338 lprintf(CTDL_DEBUG, "Returning to original room %s\n", hold_rm);
2339 if (strcasecmp(hold_rm, CC->room.QRname))
2340 /* getroom(&CC->room, hold_rm); */
2341 usergoto(hold_rm, 0, 1, NULL, NULL);
2343 /* For internet mail, generate delivery instructions.
2344 * Yes, this is recursive. Deal with it. Infinite recursion does
2345 * not happen because the delivery instructions message does not
2346 * have any recipients.
2348 if (is_internet_mail) {
2349 lprintf(CTDL_DEBUG, "Generating delivery instructions\n");
2350 instr = malloc(SIZ * 4);
2351 snprintf(instr, SIZ * 4,
2352 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
2354 SPOOLMIME, newmsgid, (long)time(NULL),
2355 msg->cm_fields['A'], msg->cm_fields['N']
2358 if (recps_to) if (strlen(recps_to->recp_internet) > 0) {
2359 for (i=0; i<num_tokens(recps_to->recp_internet, '|'); ++i) {
2360 tmp = strlen(instr);
2361 extract_token(recipient, recps_to->recp_internet, i, '|', sizeof recipient);
2362 snprintf(&instr[tmp], SIZ * 4 - tmp,
2363 "remote|%s|0||\n", recipient);
2367 if (recps_cc) if (strlen(recps_cc->recp_internet) > 0) {
2368 for (i=0; i<num_tokens(recps_cc->recp_internet, '|'); ++i) {
2369 tmp = strlen(instr);
2370 extract_token(recipient, recps_cc->recp_internet, i, '|', sizeof recipient);
2371 snprintf(&instr[tmp], SIZ * 4 - tmp,
2372 "remote|%s|0||\n", recipient);
2376 if (recps_bcc) if (strlen(recps_bcc->recp_internet) > 0) {
2377 for (i=0; i<num_tokens(recps_bcc->recp_internet, '|'); ++i) {
2378 tmp = strlen(instr);
2379 extract_token(recipient, recps_bcc->recp_internet, i, '|', sizeof recipient);
2380 snprintf(&instr[tmp], SIZ * 4 - tmp,
2381 "remote|%s|0||\n", recipient);
2385 imsg = malloc(sizeof(struct CtdlMessage));
2386 memset(imsg, 0, sizeof(struct CtdlMessage));
2387 imsg->cm_magic = CTDLMESSAGE_MAGIC;
2388 imsg->cm_anon_type = MES_NORMAL;
2389 imsg->cm_format_type = FMT_RFC822;
2390 imsg->cm_fields['A'] = strdup("Citadel");
2391 imsg->cm_fields['M'] = instr;
2392 CtdlSubmitMsg(imsg, NULL, NULL, NULL, SMTP_SPOOLOUT_ROOM);
2393 CtdlFreeMessage(imsg);
2402 * Convenience function for generating small administrative messages.
2404 void quickie_message(char *from, char *to, char *room, char *text,
2405 int format_type, char *subject)
2407 struct CtdlMessage *msg;
2408 struct recptypes *recp = NULL;
2410 msg = malloc(sizeof(struct CtdlMessage));
2411 memset(msg, 0, sizeof(struct CtdlMessage));
2412 msg->cm_magic = CTDLMESSAGE_MAGIC;
2413 msg->cm_anon_type = MES_NORMAL;
2414 msg->cm_format_type = format_type;
2415 msg->cm_fields['A'] = strdup(from);
2416 if (room != NULL) msg->cm_fields['O'] = strdup(room);
2417 msg->cm_fields['N'] = strdup(NODENAME);
2419 msg->cm_fields['R'] = strdup(to);
2420 recp = validate_recipients(to);
2422 if (subject != NULL) {
2423 msg->cm_fields['U'] = strdup(subject);
2425 msg->cm_fields['M'] = strdup(text);
2427 CtdlSubmitMsg(msg, recp, NULL, NULL, room);
2428 CtdlFreeMessage(msg);
2429 if (recp != NULL) free(recp);
2435 * Back end function used by CtdlMakeMessage() and similar functions
2437 char *CtdlReadMessageBody(char *terminator, /* token signalling EOT */
2438 size_t maxlen, /* maximum message length */
2439 char *exist, /* if non-null, append to it;
2440 exist is ALWAYS freed */
2441 int crlf /* CRLF newlines instead of LF */
2445 size_t message_len = 0;
2446 size_t buffer_len = 0;
2452 if (exist == NULL) {
2459 message_len = strlen(exist);
2460 buffer_len = message_len + 4096;
2461 m = realloc(exist, buffer_len);
2468 /* flush the input if we have nowhere to store it */
2473 /* read in the lines of message text one by one */
2475 if (client_getln(buf, (sizeof buf - 3)) < 1) finished = 1;
2476 if (!strcmp(buf, terminator)) finished = 1;
2478 strcat(buf, "\r\n");
2484 if ( (!flushing) && (!finished) ) {
2485 /* Measure the line */
2486 linelen = strlen(buf);
2488 /* augment the buffer if we have to */
2489 if ((message_len + linelen) >= buffer_len) {
2490 ptr = realloc(m, (buffer_len * 2) );
2491 if (ptr == NULL) { /* flush if can't allocate */
2494 buffer_len = (buffer_len * 2);
2496 lprintf(CTDL_DEBUG, "buffer_len is now %ld\n", (long)buffer_len);
2500 /* Add the new line to the buffer. NOTE: this loop must avoid
2501 * using functions like strcat() and strlen() because they
2502 * traverse the entire buffer upon every call, and doing that
2503 * for a multi-megabyte message slows it down beyond usability.
2505 strcpy(&m[message_len], buf);
2506 message_len += linelen;
2509 /* if we've hit the max msg length, flush the rest */
2510 if (message_len >= maxlen) flushing = 1;
2512 } while (!finished);
2520 * Build a binary message to be saved on disk.
2521 * (NOTE: if you supply 'preformatted_text', the buffer you give it
2522 * will become part of the message. This means you are no longer
2523 * responsible for managing that memory -- it will be freed along with
2524 * the rest of the fields when CtdlFreeMessage() is called.)
2527 struct CtdlMessage *CtdlMakeMessage(
2528 struct ctdluser *author, /* author's user structure */
2529 char *recipient, /* NULL if it's not mail */
2530 char *room, /* room where it's going */
2531 int type, /* see MES_ types in header file */
2532 int format_type, /* variformat, plain text, MIME... */
2533 char *fake_name, /* who we're masquerading as */
2534 char *subject, /* Subject (optional) */
2535 char *preformatted_text /* ...or NULL to read text from client */
2537 char dest_node[SIZ];
2539 struct CtdlMessage *msg;
2541 msg = malloc(sizeof(struct CtdlMessage));
2542 memset(msg, 0, sizeof(struct CtdlMessage));
2543 msg->cm_magic = CTDLMESSAGE_MAGIC;
2544 msg->cm_anon_type = type;
2545 msg->cm_format_type = format_type;
2547 /* Don't confuse the poor folks if it's not routed mail. */
2548 strcpy(dest_node, "");
2552 snprintf(buf, sizeof buf, "cit%ld", author->usernum); /* Path */
2553 msg->cm_fields['P'] = strdup(buf);
2555 snprintf(buf, sizeof buf, "%ld", (long)time(NULL)); /* timestamp */
2556 msg->cm_fields['T'] = strdup(buf);
2558 if (fake_name[0]) /* author */
2559 msg->cm_fields['A'] = strdup(fake_name);
2561 msg->cm_fields['A'] = strdup(author->fullname);
2563 if (CC->room.QRflags & QR_MAILBOX) { /* room */
2564 msg->cm_fields['O'] = strdup(&CC->room.QRname[11]);
2567 msg->cm_fields['O'] = strdup(CC->room.QRname);
2570 msg->cm_fields['N'] = strdup(NODENAME); /* nodename */
2571 msg->cm_fields['H'] = strdup(HUMANNODE); /* hnodename */
2573 if (recipient[0] != 0) {
2574 msg->cm_fields['R'] = strdup(recipient);
2576 if (dest_node[0] != 0) {
2577 msg->cm_fields['D'] = strdup(dest_node);
2580 if ( (author == &CC->user) && (strlen(CC->cs_inet_email) > 0) ) {
2581 msg->cm_fields['F'] = strdup(CC->cs_inet_email);
2584 if (subject != NULL) {
2586 if (strlen(subject) > 0) {
2587 msg->cm_fields['U'] = strdup(subject);
2591 if (preformatted_text != NULL) {
2592 msg->cm_fields['M'] = preformatted_text;
2595 msg->cm_fields['M'] = CtdlReadMessageBody("000",
2596 config.c_maxmsglen, NULL, 0);
2604 * Check to see whether we have permission to post a message in the current
2605 * room. Returns a *CITADEL ERROR CODE* and puts a message in errmsgbuf, or
2606 * returns 0 on success.
2608 int CtdlDoIHavePermissionToPostInThisRoom(char *errmsgbuf, size_t n) {
2610 if (!(CC->logged_in)) {
2611 snprintf(errmsgbuf, n, "Not logged in.");
2612 return (ERROR + NOT_LOGGED_IN);
2615 if ((CC->user.axlevel < 2)
2616 && ((CC->room.QRflags & QR_MAILBOX) == 0)) {
2617 snprintf(errmsgbuf, n, "Need to be validated to enter "
2618 "(except in %s> to sysop)", MAILROOM);
2619 return (ERROR + HIGHER_ACCESS_REQUIRED);
2622 if ((CC->user.axlevel < 4)
2623 && (CC->room.QRflags & QR_NETWORK)) {
2624 snprintf(errmsgbuf, n, "Need net privileges to enter here.");
2625 return (ERROR + HIGHER_ACCESS_REQUIRED);
2628 if ((CC->user.axlevel < 6)
2629 && (CC->room.QRflags & QR_READONLY)) {
2630 snprintf(errmsgbuf, n, "Sorry, this is a read-only room.");
2631 return (ERROR + HIGHER_ACCESS_REQUIRED);
2634 strcpy(errmsgbuf, "Ok");
2640 * Check to see if the specified user has Internet mail permission
2641 * (returns nonzero if permission is granted)
2643 int CtdlCheckInternetMailPermission(struct ctdluser *who) {
2645 /* Do not allow twits to send Internet mail */
2646 if (who->axlevel <= 2) return(0);
2648 /* Globally enabled? */
2649 if (config.c_restrict == 0) return(1);
2651 /* User flagged ok? */
2652 if (who->flags & US_INTERNET) return(2);
2654 /* Aide level access? */
2655 if (who->axlevel >= 6) return(3);
2657 /* No mail for you! */
2664 * Validate recipients, count delivery types and errors, and handle aliasing
2665 * FIXME check for dupes!!!!!
2667 struct recptypes *validate_recipients(char *recipients) {
2668 struct recptypes *ret;
2669 char this_recp[SIZ];
2670 char this_recp_cooked[SIZ];
2676 struct ctdluser tempUS;
2677 struct ctdlroom tempQR;
2680 ret = (struct recptypes *) malloc(sizeof(struct recptypes));
2681 if (ret == NULL) return(NULL);
2682 memset(ret, 0, sizeof(struct recptypes));
2685 ret->num_internet = 0;
2690 if (recipients == NULL) {
2693 else if (strlen(recipients) == 0) {
2697 /* Change all valid separator characters to commas */
2698 for (i=0; i<strlen(recipients); ++i) {
2699 if ((recipients[i] == ';') || (recipients[i] == '|')) {
2700 recipients[i] = ',';
2705 num_recps = num_tokens(recipients, ',');
2708 if (num_recps > 0) for (i=0; i<num_recps; ++i) {
2709 extract_token(this_recp, recipients, i, ',', sizeof this_recp);
2711 lprintf(CTDL_DEBUG, "Evaluating recipient #%d <%s>\n", i, this_recp);
2712 mailtype = alias(this_recp);
2713 mailtype = alias(this_recp);
2714 mailtype = alias(this_recp);
2715 for (j=0; j<=strlen(this_recp); ++j) {
2716 if (this_recp[j]=='_') {
2717 this_recp_cooked[j] = ' ';
2720 this_recp_cooked[j] = this_recp[j];
2726 if (!strcasecmp(this_recp, "sysop")) {
2728 strcpy(this_recp, config.c_aideroom);
2729 if (strlen(ret->recp_room) > 0) {
2730 strcat(ret->recp_room, "|");
2732 strcat(ret->recp_room, this_recp);
2734 else if (getuser(&tempUS, this_recp) == 0) {
2736 strcpy(this_recp, tempUS.fullname);
2737 if (strlen(ret->recp_local) > 0) {
2738 strcat(ret->recp_local, "|");
2740 strcat(ret->recp_local, this_recp);
2742 else if (getuser(&tempUS, this_recp_cooked) == 0) {
2744 strcpy(this_recp, tempUS.fullname);
2745 if (strlen(ret->recp_local) > 0) {
2746 strcat(ret->recp_local, "|");
2748 strcat(ret->recp_local, this_recp);
2750 else if ( (!strncasecmp(this_recp, "room_", 5))
2751 && (!getroom(&tempQR, &this_recp_cooked[5])) ) {
2753 if (strlen(ret->recp_room) > 0) {
2754 strcat(ret->recp_room, "|");
2756 strcat(ret->recp_room, &this_recp_cooked[5]);
2764 /* Yes, you're reading this correctly: if the target
2765 * domain points back to the local system or an attached
2766 * Citadel directory, the address is invalid. That's
2767 * because if the address were valid, we would have
2768 * already translated it to a local address by now.
2770 if (IsDirectory(this_recp)) {
2775 ++ret->num_internet;
2776 if (strlen(ret->recp_internet) > 0) {
2777 strcat(ret->recp_internet, "|");
2779 strcat(ret->recp_internet, this_recp);
2784 if (strlen(ret->recp_ignet) > 0) {
2785 strcat(ret->recp_ignet, "|");
2787 strcat(ret->recp_ignet, this_recp);
2795 if (strlen(ret->errormsg) == 0) {
2796 snprintf(append, sizeof append,
2797 "Invalid recipient: %s",
2801 snprintf(append, sizeof append,
2804 if ( (strlen(ret->errormsg) + strlen(append)) < SIZ) {
2805 strcat(ret->errormsg, append);
2809 if (strlen(ret->display_recp) == 0) {
2810 strcpy(append, this_recp);
2813 snprintf(append, sizeof append, ", %s",
2816 if ( (strlen(ret->display_recp)+strlen(append)) < SIZ) {
2817 strcat(ret->display_recp, append);
2822 if ((ret->num_local + ret->num_internet + ret->num_ignet +
2823 ret->num_room + ret->num_error) == 0) {
2825 strcpy(ret->errormsg, "No recipients specified.");
2828 lprintf(CTDL_DEBUG, "validate_recipients()\n");
2829 lprintf(CTDL_DEBUG, " local: %d <%s>\n", ret->num_local, ret->recp_local);
2830 lprintf(CTDL_DEBUG, " room: %d <%s>\n", ret->num_room, ret->recp_room);
2831 lprintf(CTDL_DEBUG, " inet: %d <%s>\n", ret->num_internet, ret->recp_internet);
2832 lprintf(CTDL_DEBUG, " ignet: %d <%s>\n", ret->num_ignet, ret->recp_ignet);
2833 lprintf(CTDL_DEBUG, " error: %d <%s>\n", ret->num_error, ret->errormsg);
2841 * message entry - mode 0 (normal)
2843 void cmd_ent0(char *entargs)
2849 char masquerade_as[SIZ];
2851 int format_type = 0;
2852 char newusername[SIZ];
2853 struct CtdlMessage *msg;
2857 struct recptypes *valid_to = NULL;
2858 struct recptypes *valid_cc = NULL;
2859 struct recptypes *valid_bcc = NULL;
2866 post = extract_int(entargs, 0);
2867 extract_token(recp, entargs, 1, '|', sizeof recp);
2868 anon_flag = extract_int(entargs, 2);
2869 format_type = extract_int(entargs, 3);
2870 extract_token(subject, entargs, 4, '|', sizeof subject);
2871 do_confirm = extract_int(entargs, 6);
2872 extract_token(cc, entargs, 7, '|', sizeof cc);
2873 extract_token(bcc, entargs, 8, '|', sizeof bcc);
2875 /* first check to make sure the request is valid. */
2877 err = CtdlDoIHavePermissionToPostInThisRoom(errmsg, sizeof errmsg);
2879 cprintf("%d %s\n", err, errmsg);
2883 /* Check some other permission type things. */
2886 if (CC->user.axlevel < 6) {
2887 cprintf("%d You don't have permission to masquerade.\n",
2888 ERROR + HIGHER_ACCESS_REQUIRED);
2891 extract_token(newusername, entargs, 5, '|', sizeof newusername);
2892 memset(CC->fake_postname, 0, sizeof(CC->fake_postname) );
2893 safestrncpy(CC->fake_postname, newusername,
2894 sizeof(CC->fake_postname) );
2895 cprintf("%d ok\n", CIT_OK);
2898 CC->cs_flags |= CS_POSTING;
2900 /* In the Mail> room we have to behave a little differently --
2901 * make sure the user has specified at least one recipient. Then
2902 * validate the recipient(s).
2904 if ( (CC->room.QRflags & QR_MAILBOX)
2905 && (!strcasecmp(&CC->room.QRname[11], MAILROOM)) ) {
2907 if (CC->user.axlevel < 2) {
2908 strcpy(recp, "sysop");
2911 valid_to = validate_recipients(recp);
2912 if (valid_to->num_error > 0) {
2914 ERROR + NO_SUCH_USER, valid_to->errormsg);
2919 valid_cc = validate_recipients(cc);
2920 if (valid_cc->num_error > 0) {
2922 ERROR + NO_SUCH_USER, valid_cc->errormsg);
2928 valid_bcc = validate_recipients(bcc);
2929 if (valid_bcc->num_error > 0) {
2931 ERROR + NO_SUCH_USER, valid_bcc->errormsg);
2938 if (valid_to->num_internet + valid_cc->num_internet + valid_bcc->num_internet > 0) {
2939 if (CtdlCheckInternetMailPermission(&CC->user)==0) {
2940 cprintf("%d You do not have permission "
2941 "to send Internet mail.\n",
2942 ERROR + HIGHER_ACCESS_REQUIRED);
2950 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)
2951 && (CC->user.axlevel < 4) ) {
2952 cprintf("%d Higher access required for network mail.\n",
2953 ERROR + HIGHER_ACCESS_REQUIRED);
2960 if ((RESTRICT_INTERNET == 1)
2961 && (valid_to->num_internet + valid_cc->num_internet + valid_bcc->num_internet > 0)
2962 && ((CC->user.flags & US_INTERNET) == 0)
2963 && (!CC->internal_pgm)) {
2964 cprintf("%d You don't have access to Internet mail.\n",
2965 ERROR + HIGHER_ACCESS_REQUIRED);
2974 /* Is this a room which has anonymous-only or anonymous-option? */
2975 anonymous = MES_NORMAL;
2976 if (CC->room.QRflags & QR_ANONONLY) {
2977 anonymous = MES_ANONONLY;
2979 if (CC->room.QRflags & QR_ANONOPT) {
2980 if (anon_flag == 1) { /* only if the user requested it */
2981 anonymous = MES_ANONOPT;
2985 if ((CC->room.QRflags & QR_MAILBOX) == 0) {
2989 /* If we're only checking the validity of the request, return
2990 * success without creating the message.
2993 cprintf("%d %s\n", CIT_OK,
2994 ((valid_to != NULL) ? valid_to->display_recp : "") );
3001 /* Handle author masquerading */
3002 if (CC->fake_postname[0]) {
3003 strcpy(masquerade_as, CC->fake_postname);
3005 else if (CC->fake_username[0]) {
3006 strcpy(masquerade_as, CC->fake_username);
3009 strcpy(masquerade_as, "");
3012 /* Read in the message from the client. */
3014 cprintf("%d send message\n", START_CHAT_MODE);
3016 cprintf("%d send message\n", SEND_LISTING);
3018 msg = CtdlMakeMessage(&CC->user, recp,
3019 CC->room.QRname, anonymous, format_type,
3020 masquerade_as, subject, NULL);
3023 msgnum = CtdlSubmitMsg(msg, valid_to, valid_cc, valid_bcc, "");
3026 cprintf("%ld\n", msgnum);
3028 cprintf("Message accepted.\n");
3031 cprintf("Internal error.\n");
3033 if (msg->cm_fields['E'] != NULL) {
3034 cprintf("%s\n", msg->cm_fields['E']);
3041 CtdlFreeMessage(msg);
3043 CC->fake_postname[0] = '\0';
3053 * API function to delete messages which match a set of criteria
3054 * (returns the actual number of messages deleted)
3056 int CtdlDeleteMessages(char *room_name, /* which room */
3057 long dmsgnum, /* or "0" for any */
3058 char *content_type, /* or "" for any */
3059 int deferred /* let TDAP sweep it later */
3063 struct ctdlroom qrbuf;
3064 struct cdbdata *cdbfr;
3065 long *msglist = NULL;
3066 long *dellist = NULL;
3069 int num_deleted = 0;
3071 struct MetaData smi;
3073 lprintf(CTDL_DEBUG, "CtdlDeleteMessages(%s, %ld, %s, %d)\n",
3074 room_name, dmsgnum, content_type, deferred);
3076 /* get room record, obtaining a lock... */
3077 if (lgetroom(&qrbuf, room_name) != 0) {
3078 lprintf(CTDL_ERR, "CtdlDeleteMessages(): Room <%s> not found\n",
3080 return (0); /* room not found */
3082 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
3084 if (cdbfr != NULL) {
3085 msglist = malloc(cdbfr->len);
3086 dellist = malloc(cdbfr->len);
3087 memcpy(msglist, cdbfr->ptr, cdbfr->len);
3088 num_msgs = cdbfr->len / sizeof(long);
3092 for (i = 0; i < num_msgs; ++i) {
3095 /* Set/clear a bit for each criterion */
3097 if ((dmsgnum == 0L) || (msglist[i] == dmsgnum)) {
3098 delete_this |= 0x01;
3100 if (strlen(content_type) == 0) {
3101 delete_this |= 0x02;
3103 GetMetaData(&smi, msglist[i]);
3104 if (!strcasecmp(smi.meta_content_type,
3106 delete_this |= 0x02;
3110 /* Delete message only if all bits are set */
3111 if (delete_this == 0x03) {
3112 dellist[num_deleted++] = msglist[i];
3117 num_msgs = sort_msglist(msglist, num_msgs);
3118 cdb_store(CDB_MSGLISTS, &qrbuf.QRnumber, (int)sizeof(long),
3119 msglist, (int)(num_msgs * sizeof(long)));
3121 qrbuf.QRhighest = msglist[num_msgs - 1];
3126 * If the delete operation is "deferred" (and technically, any delete
3127 * operation not performed by THE DREADED AUTO-PURGER ought to be
3128 * a deferred delete) then we save a pointer to the message in the
3129 * DELETED_MSGS_ROOM. This will cause the reference count to remain
3130 * at least 1, which will save the user from having to synchronously
3131 * wait for various disk-intensive operations to complete.
3133 if ( (deferred) && (num_deleted) ) {
3134 for (i=0; i<num_deleted; ++i) {
3135 CtdlCopyMsgToRoom(dellist[i], DELETED_MSGS_ROOM);
3139 /* Go through the messages we pulled out of the index, and decrement
3140 * their reference counts by 1. If this is the only room the message
3141 * was in, the reference count will reach zero and the message will
3142 * automatically be deleted from the database. We do this in a
3143 * separate pass because there might be plug-in hooks getting called,
3144 * and we don't want that happening during an S_ROOMS critical
3147 if (num_deleted) for (i=0; i<num_deleted; ++i) {
3148 PerformDeleteHooks(qrbuf.QRname, dellist[i]);
3149 AdjRefCount(dellist[i], -1);
3152 /* Now free the memory we used, and go away. */
3153 if (msglist != NULL) free(msglist);
3154 if (dellist != NULL) free(dellist);
3155 lprintf(CTDL_DEBUG, "%d message(s) deleted.\n", num_deleted);
3156 return (num_deleted);
3162 * Check whether the current user has permission to delete messages from
3163 * the current room (returns 1 for yes, 0 for no)
3165 int CtdlDoIHavePermissionToDeleteMessagesFromThisRoom(void) {
3166 getuser(&CC->user, CC->curr_user);
3167 if ((CC->user.axlevel < 6)
3168 && (CC->user.usernum != CC->room.QRroomaide)
3169 && ((CC->room.QRflags & QR_MAILBOX) == 0)
3170 && (!(CC->internal_pgm))) {
3179 * Delete message from current room
3181 void cmd_dele(char *delstr)
3186 if (CtdlDoIHavePermissionToDeleteMessagesFromThisRoom() == 0) {
3187 cprintf("%d Higher access required.\n",
3188 ERROR + HIGHER_ACCESS_REQUIRED);
3191 delnum = extract_long(delstr, 0);
3193 num_deleted = CtdlDeleteMessages(CC->room.QRname, delnum, "", 1);
3196 cprintf("%d %d message%s deleted.\n", CIT_OK,
3197 num_deleted, ((num_deleted != 1) ? "s" : ""));
3199 cprintf("%d Message %ld not found.\n", ERROR + MESSAGE_NOT_FOUND, delnum);
3205 * Back end API function for moves and deletes
3207 int CtdlCopyMsgToRoom(long msgnum, char *dest) {
3210 err = CtdlSaveMsgPointerInRoom(dest, msgnum,
3211 (SM_VERIFY_GOODNESS | SM_DO_REPL_CHECK) );
3212 if (err != 0) return(err);
3220 * move or copy a message to another room
3222 void cmd_move(char *args)
3225 char targ[ROOMNAMELEN];
3226 struct ctdlroom qtemp;
3232 num = extract_long(args, 0);
3233 extract_token(targ, args, 1, '|', sizeof targ);
3234 targ[ROOMNAMELEN - 1] = 0;
3235 is_copy = extract_int(args, 2);
3237 if (getroom(&qtemp, targ) != 0) {
3238 cprintf("%d '%s' does not exist.\n",
3239 ERROR + ROOM_NOT_FOUND, targ);
3243 getuser(&CC->user, CC->curr_user);
3244 CtdlRoomAccess(&qtemp, &CC->user, &ra, NULL);
3246 /* Check for permission to perform this operation.
3247 * Remember: "CC->room" is source, "qtemp" is target.
3251 /* Aides can move/copy */
3252 if (CC->user.axlevel >= 6) permit = 1;
3254 /* Room aides can move/copy */
3255 if (CC->user.usernum == CC->room.QRroomaide) permit = 1;
3257 /* Permit move/copy from personal rooms */
3258 if ((CC->room.QRflags & QR_MAILBOX)
3259 && (qtemp.QRflags & QR_MAILBOX)) permit = 1;
3261 /* Permit only copy from public to personal room */
3263 && (!(CC->room.QRflags & QR_MAILBOX))
3264 && (qtemp.QRflags & QR_MAILBOX)) permit = 1;
3266 /* User must have access to target room */
3267 if (!(ra & UA_KNOWN)) permit = 0;
3270 cprintf("%d Higher access required.\n",
3271 ERROR + HIGHER_ACCESS_REQUIRED);
3275 err = CtdlCopyMsgToRoom(num, targ);
3277 cprintf("%d Cannot store message in %s: error %d\n",
3282 /* Now delete the message from the source room,
3283 * if this is a 'move' rather than a 'copy' operation.
3286 CtdlDeleteMessages(CC->room.QRname, num, "", 0);
3289 cprintf("%d Message %s.\n", CIT_OK, (is_copy ? "copied" : "moved") );
3295 * GetMetaData() - Get the supplementary record for a message
3297 void GetMetaData(struct MetaData *smibuf, long msgnum)
3300 struct cdbdata *cdbsmi;
3303 memset(smibuf, 0, sizeof(struct MetaData));
3304 smibuf->meta_msgnum = msgnum;
3305 smibuf->meta_refcount = 1; /* Default reference count is 1 */
3307 /* Use the negative of the message number for its supp record index */
3308 TheIndex = (0L - msgnum);
3310 cdbsmi = cdb_fetch(CDB_MSGMAIN, &TheIndex, sizeof(long));
3311 if (cdbsmi == NULL) {
3312 return; /* record not found; go with defaults */
3314 memcpy(smibuf, cdbsmi->ptr,
3315 ((cdbsmi->len > sizeof(struct MetaData)) ?
3316 sizeof(struct MetaData) : cdbsmi->len));
3323 * PutMetaData() - (re)write supplementary record for a message
3325 void PutMetaData(struct MetaData *smibuf)
3329 /* Use the negative of the message number for the metadata db index */
3330 TheIndex = (0L - smibuf->meta_msgnum);
3332 lprintf(CTDL_DEBUG, "PutMetaData(%ld) - ref count is %d\n",
3333 smibuf->meta_msgnum, smibuf->meta_refcount);
3335 cdb_store(CDB_MSGMAIN,
3336 &TheIndex, (int)sizeof(long),
3337 smibuf, (int)sizeof(struct MetaData));
3342 * AdjRefCount - change the reference count for a message;
3343 * delete the message if it reaches zero
3345 void AdjRefCount(long msgnum, int incr)
3348 struct MetaData smi;
3351 /* This is a *tight* critical section; please keep it that way, as
3352 * it may get called while nested in other critical sections.
3353 * Complicating this any further will surely cause deadlock!
3355 begin_critical_section(S_SUPPMSGMAIN);
3356 GetMetaData(&smi, msgnum);
3357 smi.meta_refcount += incr;
3359 end_critical_section(S_SUPPMSGMAIN);
3360 lprintf(CTDL_DEBUG, "msg %ld ref count incr %d, is now %d\n",
3361 msgnum, incr, smi.meta_refcount);
3363 /* If the reference count is now zero, delete the message
3364 * (and its supplementary record as well).
3365 * FIXME ... defer this so it doesn't keep the user waiting.
3367 if (smi.meta_refcount == 0) {
3368 lprintf(CTDL_DEBUG, "Deleting message <%ld>\n", msgnum);
3370 /* Remove from fulltext index */
3371 if (config.c_enable_fulltext) {
3372 ft_index_message(msgnum, 0);
3375 /* Remove from message base */
3377 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3378 cdb_delete(CDB_BIGMSGS, &delnum, (int)sizeof(long));
3380 /* Remove metadata record */
3381 delnum = (0L - msgnum);
3382 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3387 * Write a generic object to this room
3389 * Note: this could be much more efficient. Right now we use two temporary
3390 * files, and still pull the message into memory as with all others.
3392 void CtdlWriteObject(char *req_room, /* Room to stuff it in */
3393 char *content_type, /* MIME type of this object */
3394 char *tempfilename, /* Where to fetch it from */
3395 struct ctdluser *is_mailbox, /* Mailbox room? */
3396 int is_binary, /* Is encoding necessary? */
3397 int is_unique, /* Del others of this type? */
3398 unsigned int flags /* Internal save flags */
3403 struct ctdlroom qrbuf;
3404 char roomname[ROOMNAMELEN];
3405 struct CtdlMessage *msg;
3407 char *raw_message = NULL;
3408 char *encoded_message = NULL;
3409 off_t raw_length = 0;
3411 if (is_mailbox != NULL)
3412 MailboxName(roomname, sizeof roomname, is_mailbox, req_room);
3414 safestrncpy(roomname, req_room, sizeof(roomname));
3415 lprintf(CTDL_DEBUG, "CtdlWriteObject() to <%s> (flags=%d)\n", roomname, flags);
3418 fp = fopen(tempfilename, "rb");
3420 lprintf(CTDL_CRIT, "Cannot open %s: %s\n",
3421 tempfilename, strerror(errno));
3424 fseek(fp, 0L, SEEK_END);
3425 raw_length = ftell(fp);
3427 lprintf(CTDL_DEBUG, "Raw length is %ld\n", (long)raw_length);
3429 raw_message = malloc((size_t)raw_length + 2);
3430 fread(raw_message, (size_t)raw_length, 1, fp);
3434 encoded_message = malloc((size_t)
3435 (((raw_length * 134) / 100) + 4096 ) );
3438 encoded_message = malloc((size_t)(raw_length + 4096));
3441 sprintf(encoded_message, "Content-type: %s\n", content_type);
3444 sprintf(&encoded_message[strlen(encoded_message)],
3445 "Content-transfer-encoding: base64\n\n"
3449 sprintf(&encoded_message[strlen(encoded_message)],
3450 "Content-transfer-encoding: 7bit\n\n"
3456 &encoded_message[strlen(encoded_message)],
3462 raw_message[raw_length] = 0;
3464 &encoded_message[strlen(encoded_message)],
3472 lprintf(CTDL_DEBUG, "Allocating\n");
3473 msg = malloc(sizeof(struct CtdlMessage));
3474 memset(msg, 0, sizeof(struct CtdlMessage));
3475 msg->cm_magic = CTDLMESSAGE_MAGIC;
3476 msg->cm_anon_type = MES_NORMAL;
3477 msg->cm_format_type = 4;
3478 msg->cm_fields['A'] = strdup(CC->user.fullname);
3479 msg->cm_fields['O'] = strdup(req_room);
3480 msg->cm_fields['N'] = strdup(config.c_nodename);
3481 msg->cm_fields['H'] = strdup(config.c_humannode);
3482 msg->cm_flags = flags;
3484 msg->cm_fields['M'] = encoded_message;
3486 /* Create the requested room if we have to. */
3487 if (getroom(&qrbuf, roomname) != 0) {
3488 create_room(roomname,
3489 ( (is_mailbox != NULL) ? 5 : 3 ),
3490 "", 0, 1, 0, VIEW_BBS);
3492 /* If the caller specified this object as unique, delete all
3493 * other objects of this type that are currently in the room.
3496 lprintf(CTDL_DEBUG, "Deleted %d other msgs of this type\n",
3497 CtdlDeleteMessages(roomname, 0L, content_type, 0)
3500 /* Now write the data */
3501 CtdlSubmitMsg(msg, NULL, NULL, NULL, roomname);
3502 CtdlFreeMessage(msg);
3510 void CtdlGetSysConfigBackend(long msgnum, void *userdata) {
3511 config_msgnum = msgnum;
3515 char *CtdlGetSysConfig(char *sysconfname) {
3516 char hold_rm[ROOMNAMELEN];
3519 struct CtdlMessage *msg;
3522 strcpy(hold_rm, CC->room.QRname);
3523 if (getroom(&CC->room, SYSCONFIGROOM) != 0) {
3524 getroom(&CC->room, hold_rm);
3529 /* We want the last (and probably only) config in this room */
3530 begin_critical_section(S_CONFIG);
3531 config_msgnum = (-1L);
3532 CtdlForEachMessage(MSGS_LAST, 1, sysconfname, NULL,
3533 CtdlGetSysConfigBackend, NULL);
3534 msgnum = config_msgnum;
3535 end_critical_section(S_CONFIG);
3541 msg = CtdlFetchMessage(msgnum, 1);
3543 conf = strdup(msg->cm_fields['M']);
3544 CtdlFreeMessage(msg);
3551 getroom(&CC->room, hold_rm);
3553 if (conf != NULL) do {
3554 extract_token(buf, conf, 0, '\n', sizeof buf);
3555 strcpy(conf, &conf[strlen(buf)+1]);
3556 } while ( (strlen(conf)>0) && (strlen(buf)>0) );
3561 void CtdlPutSysConfig(char *sysconfname, char *sysconfdata) {
3562 char temp[PATH_MAX];
3565 strcpy(temp, tmpnam(NULL));
3567 fp = fopen(temp, "w");
3568 if (fp == NULL) return;
3569 fprintf(fp, "%s", sysconfdata);
3572 /* this handy API function does all the work for us */
3573 CtdlWriteObject(SYSCONFIGROOM, sysconfname, temp, NULL, 0, 1, 0);
3579 * Determine whether a given Internet address belongs to the current user
3581 int CtdlIsMe(char *addr, int addr_buf_len)
3583 struct recptypes *recp;
3586 recp = validate_recipients(addr);
3587 if (recp == NULL) return(0);
3589 if (recp->num_local == 0) {
3594 for (i=0; i<recp->num_local; ++i) {
3595 extract_token(addr, recp->recp_local, i, '|', addr_buf_len);
3596 if (!strcasecmp(addr, CC->user.fullname)) {
3608 * Citadel protocol command to do the same
3610 void cmd_isme(char *argbuf) {
3613 if (CtdlAccessCheck(ac_logged_in)) return;
3614 extract_token(addr, argbuf, 0, '|', sizeof addr);
3616 if (CtdlIsMe(addr, sizeof addr)) {
3617 cprintf("%d %s\n", CIT_OK, addr);
3620 cprintf("%d Not you.\n", ERROR + ILLEGAL_VALUE);