* CtdlSetSeen() -- when new vset overflows its size, trim it in such a way
[citadel.git] / citadel / msgbase.c
1 /*
2  * $Id$
3  *
4  * Implements the message store.
5  *
6  */
7
8 #ifdef DLL_EXPORT
9 #define IN_LIBCIT
10 #endif
11
12 #include "sysdep.h"
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <fcntl.h>
17
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # if HAVE_SYS_TIME_H
23 #  include <sys/time.h>
24 # else
25 #  include <time.h>
26 # endif
27 #endif
28
29
30 #include <ctype.h>
31 #include <string.h>
32 #include <limits.h>
33 #include <errno.h>
34 #include <stdarg.h>
35 #include <sys/stat.h>
36 #include "citadel.h"
37 #include "server.h"
38 #include "serv_extensions.h"
39 #include "database.h"
40 #include "msgbase.h"
41 #include "support.h"
42 #include "sysdep_decls.h"
43 #include "citserver.h"
44 #include "room_ops.h"
45 #include "user_ops.h"
46 #include "file_ops.h"
47 #include "config.h"
48 #include "control.h"
49 #include "tools.h"
50 #include "mime_parser.h"
51 #include "html.h"
52 #include "genstamp.h"
53 #include "internet_addressing.h"
54 #include "serv_fulltext.h"
55
56 long config_msgnum;
57
58
59 /* 
60  * This really belongs in serv_network.c, but I don't know how to export
61  * symbols between modules.
62  */
63 struct FilterList *filterlist = NULL;
64
65
66 /*
67  * These are the four-character field headers we use when outputting
68  * messages in Citadel format (as opposed to RFC822 format).
69  */
70 char *msgkeys[] = {
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, 
79         NULL, 
80         "from",
81         NULL, NULL, NULL,
82         "exti",
83         "rfca",
84         NULL, 
85         "hnod",
86         "msgn",
87         NULL, NULL, NULL,
88         "text",
89         "node",
90         "room",
91         "path",
92         NULL,
93         "rcpt",
94         "spec",
95         "time",
96         "subj",
97         NULL,
98         NULL,
99         NULL,
100         NULL,
101         NULL
102 };
103
104 /*
105  * This function is self explanatory.
106  * (What can I say, I'm in a weird mood today...)
107  */
108 void remove_any_whitespace_to_the_left_or_right_of_at_symbol(char *name)
109 {
110         int i;
111
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]);
116                                 --i;
117                         }
118                         while (isspace(name[i + 1])) {
119                                 strcpy(&name[i + 1], &name[i + 2]);
120                         }
121                 }
122         }
123 }
124
125
126 /*
127  * Aliasing for network mail.
128  * (Error messages have been commented out, because this is a server.)
129  */
130 int alias(char *name)
131 {                               /* process alias and routing info for mail */
132         FILE *fp;
133         int a, i;
134         char aaa[SIZ], bbb[SIZ];
135         char *ignetcfg = NULL;
136         char *ignetmap = NULL;
137         int at = 0;
138         char node[64];
139         char testnode[64];
140         char buf[SIZ];
141
142         striplt(name);
143         remove_any_whitespace_to_the_left_or_right_of_at_symbol(name);
144
145         fp = fopen("network/mail.aliases", "r");
146         if (fp == NULL) {
147                 fp = fopen("/dev/null", "r");
148         }
149         if (fp == NULL) {
150                 return (MES_ERROR);
151         }
152         strcpy(aaa, "");
153         strcpy(bbb, "");
154         while (fgets(aaa, sizeof aaa, fp) != NULL) {
155                 while (isspace(name[0]))
156                         strcpy(name, &name[1]);
157                 aaa[strlen(aaa) - 1] = 0;
158                 strcpy(bbb, "");
159                 for (a = 0; a < strlen(aaa); ++a) {
160                         if (aaa[a] == ',') {
161                                 strcpy(bbb, &aaa[a + 1]);
162                                 aaa[a] = 0;
163                         }
164                 }
165                 if (!strcasecmp(name, aaa))
166                         strcpy(name, bbb);
167         }
168         fclose(fp);
169
170         /* Hit the Global Address Book */
171         if (CtdlDirectoryLookup(aaa, name) == 0) {
172                 strcpy(name, aaa);
173         }
174
175         lprintf(CTDL_INFO, "Mail is being forwarded to %s\n", name);
176
177         /* Change "user @ xxx" to "user" if xxx is an alias for this host */
178         for (a=0; a<strlen(name); ++a) {
179                 if (name[a] == '@') {
180                         if (CtdlHostAlias(&name[a+1]) == hostalias_localhost) {
181                                 name[a] = 0;
182                                 lprintf(CTDL_INFO, "Changed to <%s>\n", name);
183                         }
184                 }
185         }
186
187         /* determine local or remote type, see citadel.h */
188         at = haschar(name, '@');
189         if (at == 0) return(MES_LOCAL);         /* no @'s - local address */
190         if (at > 1) return(MES_ERROR);          /* >1 @'s - invalid address */
191         remove_any_whitespace_to_the_left_or_right_of_at_symbol(name);
192
193         /* figure out the delivery mode */
194         extract_token(node, name, 1, '@', sizeof node);
195
196         /* If there are one or more dots in the nodename, we assume that it
197          * is an FQDN and will attempt SMTP delivery to the Internet.
198          */
199         if (haschar(node, '.') > 0) {
200                 return(MES_INTERNET);
201         }
202
203         /* Otherwise we look in the IGnet maps for a valid Citadel node.
204          * Try directly-connected nodes first...
205          */
206         ignetcfg = CtdlGetSysConfig(IGNETCFG);
207         for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
208                 extract_token(buf, ignetcfg, i, '\n', sizeof buf);
209                 extract_token(testnode, buf, 0, '|', sizeof testnode);
210                 if (!strcasecmp(node, testnode)) {
211                         free(ignetcfg);
212                         return(MES_IGNET);
213                 }
214         }
215         free(ignetcfg);
216
217         /*
218          * Then try nodes that are two or more hops away.
219          */
220         ignetmap = CtdlGetSysConfig(IGNETMAP);
221         for (i=0; i<num_tokens(ignetmap, '\n'); ++i) {
222                 extract_token(buf, ignetmap, i, '\n', sizeof buf);
223                 extract_token(testnode, buf, 0, '|', sizeof testnode);
224                 if (!strcasecmp(node, testnode)) {
225                         free(ignetmap);
226                         return(MES_IGNET);
227                 }
228         }
229         free(ignetmap);
230
231         /* If we get to this point it's an invalid node name */
232         return (MES_ERROR);
233 }
234
235
236 void get_mm(void)
237 {
238         FILE *fp;
239
240         fp = fopen("citadel.control", "r");
241         if (fp == NULL) {
242                 lprintf(CTDL_CRIT, "Cannot open citadel.control: %s\n",
243                         strerror(errno));
244                 exit(errno);
245         }
246         fread((char *) &CitControl, sizeof(struct CitControl), 1, fp);
247         fclose(fp);
248 }
249
250
251
252 void simple_listing(long msgnum, void *userdata)
253 {
254         cprintf("%ld\n", msgnum);
255 }
256
257
258
259 /* Determine if a given message matches the fields in a message template.
260  * Return 0 for a successful match.
261  */
262 int CtdlMsgCmp(struct CtdlMessage *msg, struct CtdlMessage *template) {
263         int i;
264
265         /* If there aren't any fields in the template, all messages will
266          * match.
267          */
268         if (template == NULL) return(0);
269
270         /* Null messages are bogus. */
271         if (msg == NULL) return(1);
272
273         for (i='A'; i<='Z'; ++i) {
274                 if (template->cm_fields[i] != NULL) {
275                         if (msg->cm_fields[i] == NULL) {
276                                 return 1;
277                         }
278                         if (strcasecmp(msg->cm_fields[i],
279                                 template->cm_fields[i])) return 1;
280                 }
281         }
282
283         /* All compares succeeded: we have a match! */
284         return 0;
285 }
286
287
288
289 /*
290  * Retrieve the "seen" message list for the current room.
291  */
292 void CtdlGetSeen(char *buf, int which_set) {
293         struct visit vbuf;
294
295         /* Learn about the user and room in question */
296         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
297
298         if (which_set == ctdlsetseen_seen)
299                 safestrncpy(buf, vbuf.v_seen, SIZ);
300         if (which_set == ctdlsetseen_answered)
301                 safestrncpy(buf, vbuf.v_answered, SIZ);
302 }
303
304
305
306 /*
307  * Manipulate the "seen msgs" string (or other message set strings)
308  */
309 void CtdlSetSeen(long target_msgnum, int target_setting, int which_set,
310                 struct ctdluser *which_user, struct ctdlroom *which_room) {
311         struct cdbdata *cdbfr;
312         int i, j;
313         int is_seen = 0;
314         int was_seen = 1;
315         long lo = (-1L);
316         long hi = (-1L);
317         long t = (-1L);
318         int trimming = 0;
319         struct visit vbuf;
320         long *msglist;
321         int num_msgs = 0;
322         char vset[SIZ];
323         char *is_set;   /* actually an array of booleans */
324         int num_sets;
325         int s;
326         char setstr[SIZ], lostr[SIZ], histr[SIZ];
327
328         lprintf(CTDL_DEBUG, "CtdlSetSeen(%ld, %d, %d)\n",
329                 target_msgnum, target_setting, which_set);
330
331         /* Learn about the user and room in question */
332         CtdlGetRelationship(&vbuf,
333                 ((which_user != NULL) ? which_user : &CC->user),
334                 ((which_room != NULL) ? which_room : &CC->room)
335         );
336
337         /* Load the message list */
338         cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
339         if (cdbfr != NULL) {
340                 msglist = malloc(cdbfr->len);
341                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
342                 num_msgs = cdbfr->len / sizeof(long);
343                 cdb_free(cdbfr);
344         } else {
345                 return; /* No messages at all?  No further action. */
346         }
347
348         is_set = malloc(num_msgs * sizeof(char));
349         memset(is_set, 0, (num_msgs * sizeof(char)) );
350
351         /* Decide which message set we're manipulating */
352         switch(which_set) {
353                 case ctdlsetseen_seen:
354                         safestrncpy(vset, vbuf.v_seen, sizeof vset);
355                         break;
356                 case ctdlsetseen_answered:
357                         safestrncpy(vset, vbuf.v_answered, sizeof vset);
358                         break;
359         }
360
361         lprintf(CTDL_DEBUG, "before optimize: %s\n", vset);
362
363         /* Translate the existing sequence set into an array of booleans */
364         num_sets = num_tokens(vset, ',');
365         for (s=0; s<num_sets; ++s) {
366                 extract_token(setstr, vset, s, ',', sizeof setstr);
367
368                 extract_token(lostr, setstr, 0, ':', sizeof lostr);
369                 if (num_tokens(setstr, ':') >= 2) {
370                         extract_token(histr, setstr, 1, ':', sizeof histr);
371                         if (!strcmp(histr, "*")) {
372                                 snprintf(histr, sizeof histr, "%ld", LONG_MAX);
373                         }
374                 }
375                 else {
376                         strcpy(histr, lostr);
377                 }
378                 lo = atol(lostr);
379                 hi = atol(histr);
380
381                 for (i = 0; i < num_msgs; ++i) {
382                         if ((msglist[i] >= lo) && (msglist[i] <= hi)) {
383                                 is_set[i] = 1;
384                         }
385                 }
386         }
387
388         /* Now translate the array of booleans back into a sequence set */
389         strcpy(vset, "");
390
391         for (i=0; i<num_msgs; ++i) {
392                 is_seen = 0;
393
394                 if (msglist[i] == target_msgnum) {
395                         is_seen = target_setting;
396                 }
397                 else {
398                         if (is_set[i]) {
399                                 is_seen = 1;
400                         }
401                 }
402
403                 if (is_seen == 1) {
404                         if (lo < 0L) lo = msglist[i];
405                         hi = msglist[i];
406                 }
407                 if (  ((is_seen == 0) && (was_seen == 1))
408                    || ((is_seen == 1) && (i == num_msgs-1)) ) {
409                         size_t tmp;
410
411                         j=9;
412                         trimming = 0;
413                         while ( (strlen(vset) + 20) > sizeof vset) {
414                                 remove_token(vset, 0, ',');
415                                 trimming = 1;
416                                 if (j--) break; /* loop no more than 9 times */
417                         }
418                         if ( (trimming) && (which_set == ctdlsetseen_seen) ) {
419                                 t = atol(vset);
420                                 if (t<2) t=2;
421                                 --t;
422                                 snprintf(lostr, sizeof lostr,
423                                         "1:%ld,%s", t, vset);
424                                 safestrncpy(vset, lostr, sizeof vset);
425                         }
426                         tmp = strlen(vset);
427                         if (tmp > 0) {
428                                 strcat(vset, ",");
429                                 tmp++;
430                         }
431                         if (lo == hi) {
432                                 snprintf(&vset[tmp], sizeof vset - tmp,
433                                          "%ld", lo);
434                         }
435                         else {
436                                 snprintf(&vset[tmp], sizeof vset - tmp,
437                                          "%ld:%ld", lo, hi);
438                         }
439                         lo = (-1L);
440                         hi = (-1L);
441                 }
442                 was_seen = is_seen;
443         }
444
445         /* Decide which message set we're manipulating */
446         switch (which_set) {
447                 case ctdlsetseen_seen:
448                         safestrncpy(vbuf.v_seen, vset, sizeof vbuf.v_seen);
449                         break;
450                 case ctdlsetseen_answered:
451                         safestrncpy(vbuf.v_answered, vset,
452                                                 sizeof vbuf.v_answered);
453                         break;
454         }
455         free(is_set);
456
457         lprintf(CTDL_DEBUG, " after optimize: %s\n", vset);
458         free(msglist);
459         CtdlSetRelationship(&vbuf,
460                 ((which_user != NULL) ? which_user : &CC->user),
461                 ((which_room != NULL) ? which_room : &CC->room)
462         );
463 }
464
465
466 /*
467  * API function to perform an operation for each qualifying message in the
468  * current room.  (Returns the number of messages processed.)
469  */
470 int CtdlForEachMessage(int mode, long ref,
471                         char *content_type,
472                         struct CtdlMessage *compare,
473                         void (*CallBack) (long, void *),
474                         void *userdata)
475 {
476
477         int a;
478         struct visit vbuf;
479         struct cdbdata *cdbfr;
480         long *msglist = NULL;
481         int num_msgs = 0;
482         int num_processed = 0;
483         long thismsg;
484         struct MetaData smi;
485         struct CtdlMessage *msg;
486         int is_seen;
487         long lastold = 0L;
488         int printed_lastold = 0;
489
490         /* Learn about the user and room in question */
491         get_mm();
492         getuser(&CC->user, CC->curr_user);
493         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
494
495         /* Load the message list */
496         cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
497         if (cdbfr != NULL) {
498                 msglist = malloc(cdbfr->len);
499                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
500                 num_msgs = cdbfr->len / sizeof(long);
501                 cdb_free(cdbfr);
502         } else {
503                 return 0;       /* No messages at all?  No further action. */
504         }
505
506
507         /*
508          * Now begin the traversal.
509          */
510         if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
511
512                 /* If the caller is looking for a specific MIME type, filter
513                  * out all messages which are not of the type requested.
514                  */
515                 if (content_type != NULL) if (strlen(content_type) > 0) {
516
517                         /* This call to GetMetaData() sits inside this loop
518                          * so that we only do the extra database read per msg
519                          * if we need to.  Doing the extra read all the time
520                          * really kills the server.  If we ever need to use
521                          * metadata for another search criterion, we need to
522                          * move the read somewhere else -- but still be smart
523                          * enough to only do the read if the caller has
524                          * specified something that will need it.
525                          */
526                         GetMetaData(&smi, msglist[a]);
527
528                         if (strcasecmp(smi.meta_content_type, content_type)) {
529                                 msglist[a] = 0L;
530                         }
531                 }
532         }
533
534         num_msgs = sort_msglist(msglist, num_msgs);
535
536         /* If a template was supplied, filter out the messages which
537          * don't match.  (This could induce some delays!)
538          */
539         if (num_msgs > 0) {
540                 if (compare != NULL) {
541                         for (a = 0; a < num_msgs; ++a) {
542                                 msg = CtdlFetchMessage(msglist[a], 1);
543                                 if (msg != NULL) {
544                                         if (CtdlMsgCmp(msg, compare)) {
545                                                 msglist[a] = 0L;
546                                         }
547                                         CtdlFreeMessage(msg);
548                                 }
549                         }
550                 }
551         }
552
553         
554         /*
555          * Now iterate through the message list, according to the
556          * criteria supplied by the caller.
557          */
558         if (num_msgs > 0)
559                 for (a = 0; a < num_msgs; ++a) {
560                         thismsg = msglist[a];
561                         is_seen = is_msg_in_sequence_set(vbuf.v_seen, thismsg);
562                         if (is_seen) lastold = thismsg;
563                         if ((thismsg > 0L)
564                             && (
565
566                                        (mode == MSGS_ALL)
567                                        || ((mode == MSGS_OLD) && (is_seen))
568                                        || ((mode == MSGS_NEW) && (!is_seen))
569                                        || ((mode == MSGS_LAST) && (a >= (num_msgs - ref)))
570                                    || ((mode == MSGS_FIRST) && (a < ref))
571                                 || ((mode == MSGS_GT) && (thismsg > ref))
572                                 || ((mode == MSGS_EQ) && (thismsg == ref))
573                             )
574                             ) {
575                                 if ((mode == MSGS_NEW) && (CC->user.flags & US_LASTOLD) && (lastold > 0L) && (printed_lastold == 0) && (!is_seen)) {
576                                         if (CallBack)
577                                                 CallBack(lastold, userdata);
578                                         printed_lastold = 1;
579                                         ++num_processed;
580                                 }
581                                 if (CallBack) CallBack(thismsg, userdata);
582                                 ++num_processed;
583                         }
584                 }
585         free(msglist);          /* Clean up */
586         return num_processed;
587 }
588
589
590
591 /*
592  * cmd_msgs()  -  get list of message #'s in this room
593  *              implements the MSGS server command using CtdlForEachMessage()
594  */
595 void cmd_msgs(char *cmdbuf)
596 {
597         int mode = 0;
598         char which[16];
599         char buf[256];
600         char tfield[256];
601         char tvalue[256];
602         int cm_ref = 0;
603         int i;
604         int with_template = 0;
605         struct CtdlMessage *template = NULL;
606
607         extract_token(which, cmdbuf, 0, '|', sizeof which);
608         cm_ref = extract_int(cmdbuf, 1);
609         with_template = extract_int(cmdbuf, 2);
610
611         mode = MSGS_ALL;
612         strcat(which, "   ");
613         if (!strncasecmp(which, "OLD", 3))
614                 mode = MSGS_OLD;
615         else if (!strncasecmp(which, "NEW", 3))
616                 mode = MSGS_NEW;
617         else if (!strncasecmp(which, "FIRST", 5))
618                 mode = MSGS_FIRST;
619         else if (!strncasecmp(which, "LAST", 4))
620                 mode = MSGS_LAST;
621         else if (!strncasecmp(which, "GT", 2))
622                 mode = MSGS_GT;
623
624         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
625                 cprintf("%d not logged in\n", ERROR + NOT_LOGGED_IN);
626                 return;
627         }
628
629         if (with_template) {
630                 unbuffer_output();
631                 cprintf("%d Send template then receive message list\n",
632                         START_CHAT_MODE);
633                 template = (struct CtdlMessage *)
634                         malloc(sizeof(struct CtdlMessage));
635                 memset(template, 0, sizeof(struct CtdlMessage));
636                 while(client_getln(buf, sizeof buf), strcmp(buf,"000")) {
637                         extract_token(tfield, buf, 0, '|', sizeof tfield);
638                         extract_token(tvalue, buf, 1, '|', sizeof tvalue);
639                         for (i='A'; i<='Z'; ++i) if (msgkeys[i]!=NULL) {
640                                 if (!strcasecmp(tfield, msgkeys[i])) {
641                                         template->cm_fields[i] =
642                                                 strdup(tvalue);
643                                 }
644                         }
645                 }
646                 buffer_output();
647         }
648         else {
649                 cprintf("%d Message list...\n", LISTING_FOLLOWS);
650         }
651
652         CtdlForEachMessage(mode, cm_ref,
653                 NULL, template, simple_listing, NULL);
654         if (template != NULL) CtdlFreeMessage(template);
655         cprintf("000\n");
656 }
657
658
659
660
661 /* 
662  * help_subst()  -  support routine for help file viewer
663  */
664 void help_subst(char *strbuf, char *source, char *dest)
665 {
666         char workbuf[SIZ];
667         int p;
668
669         while (p = pattern2(strbuf, source), (p >= 0)) {
670                 strcpy(workbuf, &strbuf[p + strlen(source)]);
671                 strcpy(&strbuf[p], dest);
672                 strcat(strbuf, workbuf);
673         }
674 }
675
676
677 void do_help_subst(char *buffer)
678 {
679         char buf2[16];
680
681         help_subst(buffer, "^nodename", config.c_nodename);
682         help_subst(buffer, "^humannode", config.c_humannode);
683         help_subst(buffer, "^fqdn", config.c_fqdn);
684         help_subst(buffer, "^username", CC->user.fullname);
685         snprintf(buf2, sizeof buf2, "%ld", CC->user.usernum);
686         help_subst(buffer, "^usernum", buf2);
687         help_subst(buffer, "^sysadm", config.c_sysadm);
688         help_subst(buffer, "^variantname", CITADEL);
689         snprintf(buf2, sizeof buf2, "%d", config.c_maxsessions);
690         help_subst(buffer, "^maxsessions", buf2);
691         help_subst(buffer, "^bbsdir", CTDLDIR);
692 }
693
694
695
696 /*
697  * memfmout()  -  Citadel text formatter and paginator.
698  *           Although the original purpose of this routine was to format
699  *           text to the reader's screen width, all we're really using it
700  *           for here is to format text out to 80 columns before sending it
701  *           to the client.  The client software may reformat it again.
702  */
703 void memfmout(
704         int width,              /* screen width to use */
705         char *mptr,             /* where are we going to get our text from? */
706         char subst,             /* nonzero if we should do substitutions */
707         char *nl)               /* string to terminate lines with */
708 {
709         int a, b, c;
710         int real = 0;
711         int old = 0;
712         cit_uint8_t ch;
713         char aaa[140];
714         char buffer[SIZ];
715
716         strcpy(aaa, "");
717         old = 255;
718         strcpy(buffer, "");
719         c = 1;                  /* c is the current pos */
720
721         do {
722                 if (subst) {
723                         while (ch = *mptr, ((ch != 0) && (strlen(buffer) < 126))) {
724                                 ch = *mptr++;
725                                 buffer[strlen(buffer) + 1] = 0;
726                                 buffer[strlen(buffer)] = ch;
727                         }
728
729                         if (buffer[0] == '^')
730                                 do_help_subst(buffer);
731
732                         buffer[strlen(buffer) + 1] = 0;
733                         a = buffer[0];
734                         strcpy(buffer, &buffer[1]);
735                 } else {
736                         ch = *mptr++;
737                 }
738
739                 old = real;
740                 real = ch;
741
742                 if (((ch == 13) || (ch == 10)) && (old != 13) && (old != 10))
743                         ch = 32;
744                 if (((old == 13) || (old == 10)) && (isspace(real))) {
745                         cprintf("%s", nl);
746                         c = 1;
747                 }
748                 if (ch > 126)
749                         continue;
750
751                 if (ch > 32) {
752                         if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
753                                 cprintf("%s%s", nl, aaa);
754                                 c = strlen(aaa);
755                                 aaa[0] = 0;
756                         }
757                         b = strlen(aaa);
758                         aaa[b] = ch;
759                         aaa[b + 1] = 0;
760                 }
761                 if (ch == 32) {
762                         if ((strlen(aaa) + c) > (width - 5)) {
763                                 cprintf("%s", nl);
764                                 c = 1;
765                         }
766                         cprintf("%s ", aaa);
767                         ++c;
768                         c = c + strlen(aaa);
769                         strcpy(aaa, "");
770                 }
771                 if ((ch == 13) || (ch == 10)) {
772                         cprintf("%s%s", aaa, nl);
773                         c = 1;
774                         strcpy(aaa, "");
775                 }
776
777         } while (ch > 0);
778
779         cprintf("%s%s", aaa, nl);
780 }
781
782
783
784 /*
785  * Callback function for mime parser that simply lists the part
786  */
787 void list_this_part(char *name, char *filename, char *partnum, char *disp,
788                     void *content, char *cbtype, size_t length, char *encoding,
789                     void *cbuserdata)
790 {
791
792         cprintf("part=%s|%s|%s|%s|%s|%ld\n",
793                 name, filename, partnum, disp, cbtype, (long)length);
794 }
795
796 /* 
797  * Callback function for multipart prefix
798  */
799 void list_this_pref(char *name, char *filename, char *partnum, char *disp,
800                     void *content, char *cbtype, size_t length, char *encoding,
801                     void *cbuserdata)
802 {
803         cprintf("pref=%s|%s\n", partnum, cbtype);
804 }
805
806 /* 
807  * Callback function for multipart sufffix
808  */
809 void list_this_suff(char *name, char *filename, char *partnum, char *disp,
810                     void *content, char *cbtype, size_t length, char *encoding,
811                     void *cbuserdata)
812 {
813         cprintf("suff=%s|%s\n", partnum, cbtype);
814 }
815
816
817 /*
818  * Callback function for mime parser that opens a section for downloading
819  */
820 void mime_download(char *name, char *filename, char *partnum, char *disp,
821                    void *content, char *cbtype, size_t length, char *encoding,
822                    void *cbuserdata)
823 {
824
825         /* Silently go away if there's already a download open... */
826         if (CC->download_fp != NULL)
827                 return;
828
829         /* ...or if this is not the desired section */
830         if (strcasecmp(CC->download_desired_section, partnum))
831                 return;
832
833         CC->download_fp = tmpfile();
834         if (CC->download_fp == NULL)
835                 return;
836
837         fwrite(content, length, 1, CC->download_fp);
838         fflush(CC->download_fp);
839         rewind(CC->download_fp);
840
841         OpenCmdResult(filename, cbtype);
842 }
843
844
845
846 /*
847  * Load a message from disk into memory.
848  * This is used by CtdlOutputMsg() and other fetch functions.
849  *
850  * NOTE: Caller is responsible for freeing the returned CtdlMessage struct
851  *       using the CtdlMessageFree() function.
852  */
853 struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body)
854 {
855         struct cdbdata *dmsgtext;
856         struct CtdlMessage *ret = NULL;
857         char *mptr;
858         char *upper_bound;
859         cit_uint8_t ch;
860         cit_uint8_t field_header;
861
862         lprintf(CTDL_DEBUG, "CtdlFetchMessage(%ld, %d)\n", msgnum, with_body);
863
864         dmsgtext = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
865         if (dmsgtext == NULL) {
866                 return NULL;
867         }
868         mptr = dmsgtext->ptr;
869         upper_bound = mptr + dmsgtext->len;
870
871         /* Parse the three bytes that begin EVERY message on disk.
872          * The first is always 0xFF, the on-disk magic number.
873          * The second is the anonymous/public type byte.
874          * The third is the format type byte (vari, fixed, or MIME).
875          */
876         ch = *mptr++;
877         if (ch != 255) {
878                 lprintf(CTDL_ERR,
879                         "Message %ld appears to be corrupted.\n",
880                         msgnum);
881                 cdb_free(dmsgtext);
882                 return NULL;
883         }
884         ret = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
885         memset(ret, 0, sizeof(struct CtdlMessage));
886
887         ret->cm_magic = CTDLMESSAGE_MAGIC;
888         ret->cm_anon_type = *mptr++;    /* Anon type byte */
889         ret->cm_format_type = *mptr++;  /* Format type byte */
890
891         /*
892          * The rest is zero or more arbitrary fields.  Load them in.
893          * We're done when we encounter either a zero-length field or
894          * have just processed the 'M' (message text) field.
895          */
896         do {
897                 if (mptr >= upper_bound) {
898                         break;
899                 }
900                 field_header = *mptr++;
901                 ret->cm_fields[field_header] = strdup(mptr);
902
903                 while (*mptr++ != 0);   /* advance to next field */
904
905         } while ((mptr < upper_bound) && (field_header != 'M'));
906
907         cdb_free(dmsgtext);
908
909         /* Always make sure there's something in the msg text field.  If
910          * it's NULL, the message text is most likely stored separately,
911          * so go ahead and fetch that.  Failing that, just set a dummy
912          * body so other code doesn't barf.
913          */
914         if ( (ret->cm_fields['M'] == NULL) && (with_body) ) {
915                 dmsgtext = cdb_fetch(CDB_BIGMSGS, &msgnum, sizeof(long));
916                 if (dmsgtext != NULL) {
917                         ret->cm_fields['M'] = strdup(dmsgtext->ptr);
918                         cdb_free(dmsgtext);
919                 }
920         }
921         if (ret->cm_fields['M'] == NULL) {
922                 ret->cm_fields['M'] = strdup("<no text>\n");
923         }
924
925         /* Perform "before read" hooks (aborting if any return nonzero) */
926         if (PerformMessageHooks(ret, EVT_BEFOREREAD) > 0) {
927                 CtdlFreeMessage(ret);
928                 return NULL;
929         }
930
931         return (ret);
932 }
933
934
935 /*
936  * Returns 1 if the supplied pointer points to a valid Citadel message.
937  * If the pointer is NULL or the magic number check fails, returns 0.
938  */
939 int is_valid_message(struct CtdlMessage *msg) {
940         if (msg == NULL)
941                 return 0;
942         if ((msg->cm_magic) != CTDLMESSAGE_MAGIC) {
943                 lprintf(CTDL_WARNING, "is_valid_message() -- self-check failed\n");
944                 return 0;
945         }
946         return 1;
947 }
948
949
950 /*
951  * 'Destructor' for struct CtdlMessage
952  */
953 void CtdlFreeMessage(struct CtdlMessage *msg)
954 {
955         int i;
956
957         if (is_valid_message(msg) == 0) return;
958
959         for (i = 0; i < 256; ++i)
960                 if (msg->cm_fields[i] != NULL) {
961                         free(msg->cm_fields[i]);
962                 }
963
964         msg->cm_magic = 0;      /* just in case */
965         free(msg);
966 }
967
968
969 /*
970  * Pre callback function for multipart/alternative
971  *
972  * NOTE: this differs from the standard behavior for a reason.  Normally when
973  *       displaying multipart/alternative you want to show the _last_ usable
974  *       format in the message.  Here we show the _first_ one, because it's
975  *       usually text/plain.  Since this set of functions is designed for text
976  *       output to non-MIME-aware clients, this is the desired behavior.
977  *
978  */
979 void fixed_output_pre(char *name, char *filename, char *partnum, char *disp,
980                 void *content, char *cbtype, size_t length, char *encoding,
981                 void *cbuserdata)
982 {
983         struct ma_info *ma;
984         
985         ma = (struct ma_info *)cbuserdata;
986         lprintf(CTDL_DEBUG, "fixed_output_pre() type=<%s>\n", cbtype);  
987         if (!strcasecmp(cbtype, "multipart/alternative")) {
988                 ++ma->is_ma;
989                 ma->did_print = 0;
990                 return;
991         }
992 }
993
994 /*
995  * Post callback function for multipart/alternative
996  */
997 void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
998                 void *content, char *cbtype, size_t length, char *encoding,
999                 void *cbuserdata)
1000 {
1001         struct ma_info *ma;
1002         
1003         ma = (struct ma_info *)cbuserdata;
1004         lprintf(CTDL_DEBUG, "fixed_output_post() type=<%s>\n", cbtype); 
1005         if (!strcasecmp(cbtype, "multipart/alternative")) {
1006                 --ma->is_ma;
1007                 ma->did_print = 0;
1008         return;
1009         }
1010 }
1011
1012 /*
1013  * Inline callback function for mime parser that wants to display text
1014  */
1015 void fixed_output(char *name, char *filename, char *partnum, char *disp,
1016                 void *content, char *cbtype, size_t length, char *encoding,
1017                 void *cbuserdata)
1018         {
1019                 char *ptr;
1020                 char *wptr;
1021                 size_t wlen;
1022                 struct ma_info *ma;
1023         
1024                 ma = (struct ma_info *)cbuserdata;
1025
1026                 lprintf(CTDL_DEBUG, "fixed_output() type=<%s>\n", cbtype);      
1027
1028                 /*
1029                  * If we're in the middle of a multipart/alternative scope and
1030                  * we've already printed another section, skip this one.
1031                  */     
1032                 if ( (ma->is_ma == 1) && (ma->did_print == 1) ) {
1033                         lprintf(CTDL_DEBUG, "Skipping part %s (%s)\n", partnum, cbtype);
1034                         return;
1035                 }
1036                 ma->did_print = 1;
1037         
1038                 if ( (!strcasecmp(cbtype, "text/plain")) 
1039                    || (strlen(cbtype)==0) ) {
1040                         wptr = content;
1041                         if (length > 0) {
1042                                 client_write(wptr, length);
1043                                 if (wptr[length-1] != '\n') {
1044                                         cprintf("\n");
1045                                 }
1046                         }
1047                 }
1048                 else if (!strcasecmp(cbtype, "text/html")) {
1049                         ptr = html_to_ascii(content, 80, 0);
1050                         wlen = strlen(ptr);
1051                         client_write(ptr, wlen);
1052                         if (ptr[wlen-1] != '\n') {
1053                                 cprintf("\n");
1054                         }
1055                         free(ptr);
1056                 }
1057                 else if (strncasecmp(cbtype, "multipart/", 10)) {
1058                         cprintf("Part %s: %s (%s) (%ld bytes)\r\n",
1059                                 partnum, filename, cbtype, (long)length);
1060                 }
1061         }
1062
1063 /*
1064  * The client is elegant and sophisticated and wants to be choosy about
1065  * MIME content types, so figure out which multipart/alternative part
1066  * we're going to send.
1067  */
1068 void choose_preferred(char *name, char *filename, char *partnum, char *disp,
1069                 void *content, char *cbtype, size_t length, char *encoding,
1070                 void *cbuserdata)
1071 {
1072         char buf[1024];
1073         int i;
1074         struct ma_info *ma;
1075         
1076         ma = (struct ma_info *)cbuserdata;
1077
1078         if (ma->is_ma > 0) {
1079                 for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
1080                         extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
1081                         if (!strcasecmp(buf, cbtype)) {
1082                                 strcpy(ma->chosen_part, partnum);
1083                         }
1084                 }
1085         }
1086 }
1087
1088 /*
1089  * Now that we've chosen our preferred part, output it.
1090  */
1091 void output_preferred(char *name, char *filename, char *partnum, char *disp,
1092                 void *content, char *cbtype, size_t length, char *encoding,
1093                 void *cbuserdata)
1094 {
1095         int i;
1096         char buf[128];
1097         int add_newline = 0;
1098         char *text_content;
1099         struct ma_info *ma;
1100         
1101         ma = (struct ma_info *)cbuserdata;
1102
1103         /* This is not the MIME part you're looking for... */
1104         if (strcasecmp(partnum, ma->chosen_part)) return;
1105
1106         /* If the content-type of this part is in our preferred formats
1107          * list, we can simply output it verbatim.
1108          */
1109         for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
1110                 extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
1111                 if (!strcasecmp(buf, cbtype)) {
1112                         /* Yeah!  Go!  W00t!! */
1113
1114                         text_content = (char *)content;
1115                         if (text_content[length-1] != '\n') {
1116                                 ++add_newline;
1117                         }
1118
1119                         cprintf("Content-type: %s\n", cbtype);
1120                         cprintf("Content-length: %d\n",
1121                                 (int)(length + add_newline) );
1122                         if (strlen(encoding) > 0) {
1123                                 cprintf("Content-transfer-encoding: %s\n", encoding);
1124                         }
1125                         else {
1126                                 cprintf("Content-transfer-encoding: 7bit\n");
1127                         }
1128                         cprintf("\n");
1129                         client_write(content, length);
1130                         if (add_newline) cprintf("\n");
1131                         return;
1132                 }
1133         }
1134
1135         /* No translations required or possible: output as text/plain */
1136         cprintf("Content-type: text/plain\n\n");
1137         fixed_output(name, filename, partnum, disp, content, cbtype,
1138                         length, encoding, cbuserdata);
1139 }
1140
1141
1142 /*
1143  * Get a message off disk.  (returns om_* values found in msgbase.h)
1144  * 
1145  */
1146 int CtdlOutputMsg(long msg_num,         /* message number (local) to fetch */
1147                 int mode,               /* how would you like that message? */
1148                 int headers_only,       /* eschew the message body? */
1149                 int do_proto,           /* do Citadel protocol responses? */
1150                 int crlf                /* Use CRLF newlines instead of LF? */
1151 ) {
1152         struct CtdlMessage *TheMessage = NULL;
1153         int retcode = om_no_such_msg;
1154
1155         lprintf(CTDL_DEBUG, "CtdlOutputMsg() msgnum=%ld, mode=%d\n", 
1156                 msg_num, mode);
1157
1158         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1159                 if (do_proto) cprintf("%d Not logged in.\n",
1160                         ERROR + NOT_LOGGED_IN);
1161                 return(om_not_logged_in);
1162         }
1163
1164         /* FIXME: check message id against msglist for this room */
1165
1166         /*
1167          * Fetch the message from disk.  If we're in any sort of headers
1168          * only mode, request that we don't even bother loading the body
1169          * into memory.
1170          */
1171         if ( (headers_only == HEADERS_FAST) || (headers_only == HEADERS_ONLY) ) {
1172                 TheMessage = CtdlFetchMessage(msg_num, 0);
1173         }
1174         else {
1175                 TheMessage = CtdlFetchMessage(msg_num, 1);
1176         }
1177
1178         if (TheMessage == NULL) {
1179                 if (do_proto) cprintf("%d Can't locate msg %ld on disk\n",
1180                         ERROR + MESSAGE_NOT_FOUND, msg_num);
1181                 return(om_no_such_msg);
1182         }
1183         
1184         retcode = CtdlOutputPreLoadedMsg(
1185                         TheMessage, msg_num, mode,
1186                         headers_only, do_proto, crlf);
1187
1188         CtdlFreeMessage(TheMessage);
1189
1190         return(retcode);
1191 }
1192
1193
1194 /*
1195  * Get a message off disk.  (returns om_* values found in msgbase.h)
1196  * 
1197  */
1198 int CtdlOutputPreLoadedMsg(
1199                 struct CtdlMessage *TheMessage,
1200                 long msg_num,
1201                 int mode,               /* how would you like that message? */
1202                 int headers_only,       /* eschew the message body? */
1203                 int do_proto,           /* do Citadel protocol responses? */
1204                 int crlf                /* Use CRLF newlines instead of LF? */
1205 ) {
1206         int i, k;
1207         char buf[SIZ];
1208         cit_uint8_t ch;
1209         char allkeys[30];
1210         char display_name[256];
1211         char *mptr;
1212         char *nl;       /* newline string */
1213         int suppress_f = 0;
1214         int subject_found = 0;
1215         struct ma_info *ma;
1216
1217         /* Buffers needed for RFC822 translation.  These are all filled
1218          * using functions that are bounds-checked, and therefore we can
1219          * make them substantially smaller than SIZ.
1220          */
1221         char suser[100];
1222         char luser[100];
1223         char fuser[100];
1224         char snode[100];
1225         char lnode[100];
1226         char mid[100];
1227         char datestamp[100];
1228
1229         lprintf(CTDL_DEBUG, "CtdlOutputPreLoadedMsg(TheMessage=%s, %ld, %d, %d, %d, %d\n",
1230                 ((TheMessage == NULL) ? "NULL" : "not null"),
1231                 msg_num,
1232                 mode, headers_only, do_proto, crlf);
1233
1234         snprintf(mid, sizeof mid, "%ld", msg_num);
1235         nl = (crlf ? "\r\n" : "\n");
1236
1237         if (!is_valid_message(TheMessage)) {
1238                 lprintf(CTDL_ERR,
1239                         "ERROR: invalid preloaded message for output\n");
1240                 return(om_no_such_msg);
1241         }
1242
1243         /* Are we downloading a MIME component? */
1244         if (mode == MT_DOWNLOAD) {
1245                 if (TheMessage->cm_format_type != FMT_RFC822) {
1246                         if (do_proto)
1247                                 cprintf("%d This is not a MIME message.\n",
1248                                 ERROR + ILLEGAL_VALUE);
1249                 } else if (CC->download_fp != NULL) {
1250                         if (do_proto) cprintf(
1251                                 "%d You already have a download open.\n",
1252                                 ERROR + RESOURCE_BUSY);
1253                 } else {
1254                         /* Parse the message text component */
1255                         mptr = TheMessage->cm_fields['M'];
1256                         ma = malloc(sizeof(struct ma_info));
1257                         memset(ma, 0, sizeof(struct ma_info));
1258                         mime_parser(mptr, NULL, *mime_download, NULL, NULL, (void *)ma, 0);
1259                         free(ma);
1260                         /* If there's no file open by this time, the requested
1261                          * section wasn't found, so print an error
1262                          */
1263                         if (CC->download_fp == NULL) {
1264                                 if (do_proto) cprintf(
1265                                         "%d Section %s not found.\n",
1266                                         ERROR + FILE_NOT_FOUND,
1267                                         CC->download_desired_section);
1268                         }
1269                 }
1270                 return((CC->download_fp != NULL) ? om_ok : om_mime_error);
1271         }
1272
1273         /* now for the user-mode message reading loops */
1274         if (do_proto) cprintf("%d Message %ld:\n", LISTING_FOLLOWS, msg_num);
1275
1276         /* Does the caller want to skip the headers? */
1277         if (headers_only == HEADERS_NONE) goto START_TEXT;
1278
1279         /* Tell the client which format type we're using. */
1280         if ( (mode == MT_CITADEL) && (do_proto) ) {
1281                 cprintf("type=%d\n", TheMessage->cm_format_type);
1282         }
1283
1284         /* nhdr=yes means that we're only displaying headers, no body */
1285         if ( (TheMessage->cm_anon_type == MES_ANONONLY)
1286            && (mode == MT_CITADEL)
1287            && (do_proto)
1288            ) {
1289                 cprintf("nhdr=yes\n");
1290         }
1291
1292         /* begin header processing loop for Citadel message format */
1293
1294         if ((mode == MT_CITADEL) || (mode == MT_MIME)) {
1295
1296                 safestrncpy(display_name, "<unknown>", sizeof display_name);
1297                 if (TheMessage->cm_fields['A']) {
1298                         strcpy(buf, TheMessage->cm_fields['A']);
1299                         if (TheMessage->cm_anon_type == MES_ANONONLY) {
1300                                 safestrncpy(display_name, "****", sizeof display_name);
1301                         }
1302                         else if (TheMessage->cm_anon_type == MES_ANONOPT) {
1303                                 safestrncpy(display_name, "anonymous", sizeof display_name);
1304                         }
1305                         else {
1306                                 safestrncpy(display_name, buf, sizeof display_name);
1307                         }
1308                         if ((is_room_aide())
1309                             && ((TheMessage->cm_anon_type == MES_ANONONLY)
1310                              || (TheMessage->cm_anon_type == MES_ANONOPT))) {
1311                                 size_t tmp = strlen(display_name);
1312                                 snprintf(&display_name[tmp],
1313                                          sizeof display_name - tmp,
1314                                          " [%s]", buf);
1315                         }
1316                 }
1317
1318                 /* Don't show Internet address for users on the
1319                  * local Citadel network.
1320                  */
1321                 suppress_f = 0;
1322                 if (TheMessage->cm_fields['N'] != NULL)
1323                    if (strlen(TheMessage->cm_fields['N']) > 0)
1324                       if (haschar(TheMessage->cm_fields['N'], '.') == 0) {
1325                         suppress_f = 1;
1326                 }
1327                 
1328                 /* Now spew the header fields in the order we like them. */
1329                 safestrncpy(allkeys, FORDER, sizeof allkeys);
1330                 for (i=0; i<strlen(allkeys); ++i) {
1331                         k = (int) allkeys[i];
1332                         if (k != 'M') {
1333                                 if ( (TheMessage->cm_fields[k] != NULL)
1334                                    && (msgkeys[k] != NULL) ) {
1335                                         if (k == 'A') {
1336                                                 if (do_proto) cprintf("%s=%s\n",
1337                                                         msgkeys[k],
1338                                                         display_name);
1339                                         }
1340                                         else if ((k == 'F') && (suppress_f)) {
1341                                                 /* do nothing */
1342                                         }
1343                                         /* Masquerade display name if needed */
1344                                         else {
1345                                                 if (do_proto) cprintf("%s=%s\n",
1346                                                         msgkeys[k],
1347                                                         TheMessage->cm_fields[k]
1348                                         );
1349                                         }
1350                                 }
1351                         }
1352                 }
1353
1354         }
1355
1356         /* begin header processing loop for RFC822 transfer format */
1357
1358         strcpy(suser, "");
1359         strcpy(luser, "");
1360         strcpy(fuser, "");
1361         strcpy(snode, NODENAME);
1362         strcpy(lnode, HUMANNODE);
1363         if (mode == MT_RFC822) {
1364                 for (i = 0; i < 256; ++i) {
1365                         if (TheMessage->cm_fields[i]) {
1366                                 mptr = TheMessage->cm_fields[i];
1367
1368                                 if (i == 'A') {
1369                                         safestrncpy(luser, mptr, sizeof luser);
1370                                         safestrncpy(suser, mptr, sizeof suser);
1371                                 }
1372                                 else if (i == 'U') {
1373                                         cprintf("Subject: %s%s", mptr, nl);
1374                                         subject_found = 1;
1375                                 }
1376                                 else if (i == 'I')
1377                                         safestrncpy(mid, mptr, sizeof mid);
1378                                 else if (i == 'H')
1379                                         safestrncpy(lnode, mptr, sizeof lnode);
1380                                 else if (i == 'F')
1381                                         safestrncpy(fuser, mptr, sizeof fuser);
1382                                 /* else if (i == 'O')
1383                                         cprintf("X-Citadel-Room: %s%s",
1384                                                 mptr, nl); */
1385                                 else if (i == 'N')
1386                                         safestrncpy(snode, mptr, sizeof snode);
1387                                 else if (i == 'R')
1388                                         cprintf("To: %s%s", mptr, nl);
1389                                 else if (i == 'T') {
1390                                         datestring(datestamp, sizeof datestamp,
1391                                                 atol(mptr), DATESTRING_RFC822);
1392                                         cprintf("Date: %s%s", datestamp, nl);
1393                                 }
1394                         }
1395                 }
1396                 if (subject_found == 0) {
1397                         cprintf("Subject: (no subject)%s", nl);
1398                 }
1399         }
1400
1401         for (i=0; i<strlen(suser); ++i) {
1402                 suser[i] = tolower(suser[i]);
1403                 if (!isalnum(suser[i])) suser[i]='_';
1404         }
1405
1406         if (mode == MT_RFC822) {
1407                 if (!strcasecmp(snode, NODENAME)) {
1408                         safestrncpy(snode, FQDN, sizeof snode);
1409                 }
1410
1411                 /* Construct a fun message id */
1412                 cprintf("Message-ID: <%s", mid);
1413                 if (strchr(mid, '@')==NULL) {
1414                         cprintf("@%s", snode);
1415                 }
1416                 cprintf(">%s", nl);
1417
1418                 if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONONLY)) {
1419                         // cprintf("From: x@x.org (----)%s", nl);
1420                         cprintf("From: \"----\" <x@x.org>%s", nl);
1421                 }
1422                 else if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONOPT)) {
1423                         // cprintf("From: x@x.org (anonymous)%s", nl);
1424                         cprintf("From: \"anonymous\" <x@x.org>%s", nl);
1425                 }
1426                 else if (strlen(fuser) > 0) {
1427                         // cprintf("From: %s (%s)%s", fuser, luser, nl);
1428                         cprintf("From: \"%s\" <%s>%s", luser, fuser, nl);
1429                 }
1430                 else {
1431                         // cprintf("From: %s@%s (%s)%s", suser, snode, luser, nl);
1432                         cprintf("From: \"%s\" <%s@%s>%s", luser, suser, snode, nl);
1433                 }
1434
1435                 cprintf("Organization: %s%s", lnode, nl);
1436
1437                 /* Blank line signifying RFC822 end-of-headers */
1438                 if (TheMessage->cm_format_type != FMT_RFC822) {
1439                         cprintf("%s", nl);
1440                 }
1441         }
1442
1443         /* end header processing loop ... at this point, we're in the text */
1444 START_TEXT:
1445         if (headers_only == HEADERS_FAST) goto DONE;
1446         mptr = TheMessage->cm_fields['M'];
1447
1448         /* Tell the client about the MIME parts in this message */
1449         if (TheMessage->cm_format_type == FMT_RFC822) {
1450                 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
1451                         mime_parser(mptr, NULL,
1452                                 (do_proto ? *list_this_part : NULL),
1453                                 (do_proto ? *list_this_pref : NULL),
1454                                 (do_proto ? *list_this_suff : NULL),
1455                                 NULL, 0);
1456                 }
1457                 else if (mode == MT_RFC822) {   /* unparsed RFC822 dump */
1458                         /* FIXME ... we have to put some code in here to avoid
1459                          * printing duplicate header information when both
1460                          * Citadel and RFC822 headers exist.  Preference should
1461                          * probably be given to the RFC822 headers.
1462                          */
1463                         int done_rfc822_hdrs = 0;
1464                         while (ch=*(mptr++), ch!=0) {
1465                                 if (ch==13) {
1466                                         /* do nothing */
1467                                 }
1468                                 else if (ch==10) {
1469                                         if (!done_rfc822_hdrs) {
1470                                                 if (headers_only != HEADERS_NONE) {
1471                                                         cprintf("%s", nl);
1472                                                 }
1473                                         }
1474                                         else {
1475                                                 if (headers_only != HEADERS_ONLY) {
1476                                                         cprintf("%s", nl);
1477                                                 }
1478                                         }
1479                                         if ((*(mptr) == 13) || (*(mptr) == 10)) {
1480                                                 done_rfc822_hdrs = 1;
1481                                         }
1482                                 }
1483                                 else {
1484                                         if (done_rfc822_hdrs) {
1485                                                 if (headers_only != HEADERS_NONE) {
1486                                                         cprintf("%c", ch);
1487                                                 }
1488                                         }
1489                                         else {
1490                                                 if (headers_only != HEADERS_ONLY) {
1491                                                         cprintf("%c", ch);
1492                                                 }
1493                                         }
1494                                         if ((*mptr == 13) || (*mptr == 10)) {
1495                                                 done_rfc822_hdrs = 1;
1496                                         }
1497                                 }
1498                         }
1499                         goto DONE;
1500                 }
1501         }
1502
1503         if (headers_only == HEADERS_ONLY) {
1504                 goto DONE;
1505         }
1506
1507         /* signify start of msg text */
1508         if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
1509                 if (do_proto) cprintf("text\n");
1510         }
1511
1512         /* If the format type on disk is 1 (fixed-format), then we want
1513          * everything to be output completely literally ... regardless of
1514          * what message transfer format is in use.
1515          */
1516         if (TheMessage->cm_format_type == FMT_FIXED) {
1517                 if (mode == MT_MIME) {
1518                         cprintf("Content-type: text/plain\n\n");
1519                 }
1520                 strcpy(buf, "");
1521                 while (ch = *mptr++, ch > 0) {
1522                         if (ch == 13)
1523                                 ch = 10;
1524                         if ((ch == 10) || (strlen(buf) > 250)) {
1525                                 cprintf("%s%s", buf, nl);
1526                                 strcpy(buf, "");
1527                         } else {
1528                                 buf[strlen(buf) + 1] = 0;
1529                                 buf[strlen(buf)] = ch;
1530                         }
1531                 }
1532                 if (strlen(buf) > 0)
1533                         cprintf("%s%s", buf, nl);
1534         }
1535
1536         /* If the message on disk is format 0 (Citadel vari-format), we
1537          * output using the formatter at 80 columns.  This is the final output
1538          * form if the transfer format is RFC822, but if the transfer format
1539          * is Citadel proprietary, it'll still work, because the indentation
1540          * for new paragraphs is correct and the client will reformat the
1541          * message to the reader's screen width.
1542          */
1543         if (TheMessage->cm_format_type == FMT_CITADEL) {
1544                 if (mode == MT_MIME) {
1545                         cprintf("Content-type: text/x-citadel-variformat\n\n");
1546                 }
1547                 memfmout(80, mptr, 0, nl);
1548         }
1549
1550         /* If the message on disk is format 4 (MIME), we've gotta hand it
1551          * off to the MIME parser.  The client has already been told that
1552          * this message is format 1 (fixed format), so the callback function
1553          * we use will display those parts as-is.
1554          */
1555         if (TheMessage->cm_format_type == FMT_RFC822) {
1556                 ma = malloc(sizeof(struct ma_info));
1557                 memset(ma, 0, sizeof(struct ma_info));
1558
1559                 if (mode == MT_MIME) {
1560                         strcpy(ma->chosen_part, "1");
1561                         mime_parser(mptr, NULL,
1562                                 *choose_preferred, *fixed_output_pre,
1563                                 *fixed_output_post, (void *)ma, 0);
1564                         mime_parser(mptr, NULL,
1565                                 *output_preferred, NULL, NULL, (void *)ma, 0);
1566                 }
1567                 else {
1568                         mime_parser(mptr, NULL,
1569                                 *fixed_output, *fixed_output_pre,
1570                                 *fixed_output_post, (void *)ma, 0);
1571                 }
1572
1573                 free(ma);
1574         }
1575
1576 DONE:   /* now we're done */
1577         if (do_proto) cprintf("000\n");
1578         return(om_ok);
1579 }
1580
1581
1582
1583 /*
1584  * display a message (mode 0 - Citadel proprietary)
1585  */
1586 void cmd_msg0(char *cmdbuf)
1587 {
1588         long msgid;
1589         int headers_only = HEADERS_ALL;
1590
1591         msgid = extract_long(cmdbuf, 0);
1592         headers_only = extract_int(cmdbuf, 1);
1593
1594         CtdlOutputMsg(msgid, MT_CITADEL, headers_only, 1, 0);
1595         return;
1596 }
1597
1598
1599 /*
1600  * display a message (mode 2 - RFC822)
1601  */
1602 void cmd_msg2(char *cmdbuf)
1603 {
1604         long msgid;
1605         int headers_only = HEADERS_ALL;
1606
1607         msgid = extract_long(cmdbuf, 0);
1608         headers_only = extract_int(cmdbuf, 1);
1609
1610         CtdlOutputMsg(msgid, MT_RFC822, headers_only, 1, 1);
1611 }
1612
1613
1614
1615 /* 
1616  * display a message (mode 3 - IGnet raw format - internal programs only)
1617  */
1618 void cmd_msg3(char *cmdbuf)
1619 {
1620         long msgnum;
1621         struct CtdlMessage *msg;
1622         struct ser_ret smr;
1623
1624         if (CC->internal_pgm == 0) {
1625                 cprintf("%d This command is for internal programs only.\n",
1626                         ERROR + HIGHER_ACCESS_REQUIRED);
1627                 return;
1628         }
1629
1630         msgnum = extract_long(cmdbuf, 0);
1631         msg = CtdlFetchMessage(msgnum, 1);
1632         if (msg == NULL) {
1633                 cprintf("%d Message %ld not found.\n", 
1634                         ERROR + MESSAGE_NOT_FOUND, msgnum);
1635                 return;
1636         }
1637
1638         serialize_message(&smr, msg);
1639         CtdlFreeMessage(msg);
1640
1641         if (smr.len == 0) {
1642                 cprintf("%d Unable to serialize message\n",
1643                         ERROR + INTERNAL_ERROR);
1644                 return;
1645         }
1646
1647         cprintf("%d %ld\n", BINARY_FOLLOWS, (long)smr.len);
1648         client_write((char *)smr.ser, (int)smr.len);
1649         free(smr.ser);
1650 }
1651
1652
1653
1654 /* 
1655  * Display a message using MIME content types
1656  */
1657 void cmd_msg4(char *cmdbuf)
1658 {
1659         long msgid;
1660
1661         msgid = extract_long(cmdbuf, 0);
1662         CtdlOutputMsg(msgid, MT_MIME, 0, 1, 0);
1663 }
1664
1665
1666
1667 /* 
1668  * Client tells us its preferred message format(s)
1669  */
1670 void cmd_msgp(char *cmdbuf)
1671 {
1672         safestrncpy(CC->preferred_formats, cmdbuf,
1673                         sizeof(CC->preferred_formats));
1674         cprintf("%d ok\n", CIT_OK);
1675 }
1676
1677
1678 /*
1679  * Open a component of a MIME message as a download file 
1680  */
1681 void cmd_opna(char *cmdbuf)
1682 {
1683         long msgid;
1684         char desired_section[128];
1685
1686         msgid = extract_long(cmdbuf, 0);
1687         extract_token(desired_section, cmdbuf, 1, '|', sizeof desired_section);
1688         safestrncpy(CC->download_desired_section, desired_section, sizeof CC->download_desired_section);
1689         CtdlOutputMsg(msgid, MT_DOWNLOAD, 0, 1, 1);
1690 }                       
1691
1692
1693 /*
1694  * Save a message pointer into a specified room
1695  * (Returns 0 for success, nonzero for failure)
1696  * roomname may be NULL to use the current room
1697  */
1698 int CtdlSaveMsgPointerInRoom(char *roomname, long msgid, int flags) {
1699         int i;
1700         char hold_rm[ROOMNAMELEN];
1701         struct cdbdata *cdbfr;
1702         int num_msgs;
1703         long *msglist;
1704         long highest_msg = 0L;
1705         struct CtdlMessage *msg = NULL;
1706
1707         lprintf(CTDL_DEBUG, "CtdlSaveMsgPointerInRoom(%s, %ld, %d)\n",
1708                 roomname, msgid, flags);
1709
1710         strcpy(hold_rm, CC->room.QRname);
1711
1712         /* We may need to check to see if this message is real */
1713         if (  (flags & SM_VERIFY_GOODNESS)
1714            || (flags & SM_DO_REPL_CHECK)
1715            ) {
1716                 msg = CtdlFetchMessage(msgid, 1);
1717                 if (msg == NULL) return(ERROR + ILLEGAL_VALUE);
1718         }
1719
1720         /* Perform replication checks if necessary */
1721         if ( (flags & SM_DO_REPL_CHECK) && (msg != NULL) ) {
1722
1723                 if (getroom(&CC->room,
1724                    ((roomname != NULL) ? roomname : CC->room.QRname) )
1725                    != 0) {
1726                         lprintf(CTDL_ERR, "No such room <%s>\n", roomname);
1727                         if (msg != NULL) CtdlFreeMessage(msg);
1728                         return(ERROR + ROOM_NOT_FOUND);
1729                 }
1730
1731                 if (ReplicationChecks(msg) != 0) {
1732                         getroom(&CC->room, hold_rm);
1733                         if (msg != NULL) CtdlFreeMessage(msg);
1734                         lprintf(CTDL_DEBUG,
1735                                 "Did replication, and newer exists\n");
1736                         return(0);
1737                 }
1738         }
1739
1740         /* Now the regular stuff */
1741         if (lgetroom(&CC->room,
1742            ((roomname != NULL) ? roomname : CC->room.QRname) )
1743            != 0) {
1744                 lprintf(CTDL_ERR, "No such room <%s>\n", roomname);
1745                 if (msg != NULL) CtdlFreeMessage(msg);
1746                 return(ERROR + ROOM_NOT_FOUND);
1747         }
1748
1749         cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
1750         if (cdbfr == NULL) {
1751                 msglist = NULL;
1752                 num_msgs = 0;
1753         } else {
1754                 msglist = malloc(cdbfr->len);
1755                 if (msglist == NULL)
1756                         lprintf(CTDL_ALERT, "ERROR malloc msglist!\n");
1757                 num_msgs = cdbfr->len / sizeof(long);
1758                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1759                 cdb_free(cdbfr);
1760         }
1761
1762
1763         /* Make sure the message doesn't already exist in this room.  It
1764          * is absolutely taboo to have more than one reference to the same
1765          * message in a room.
1766          */
1767         if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
1768                 if (msglist[i] == msgid) {
1769                         lputroom(&CC->room);    /* unlock the room */
1770                         getroom(&CC->room, hold_rm);
1771                         if (msg != NULL) CtdlFreeMessage(msg);
1772                         free(msglist);
1773                         return(ERROR + ALREADY_EXISTS);
1774                 }
1775         }
1776
1777         /* Now add the new message */
1778         ++num_msgs;
1779         msglist = realloc(msglist, (num_msgs * sizeof(long)));
1780
1781         if (msglist == NULL) {
1782                 lprintf(CTDL_ALERT, "ERROR: can't realloc message list!\n");
1783         }
1784         msglist[num_msgs - 1] = msgid;
1785
1786         /* Sort the message list, so all the msgid's are in order */
1787         num_msgs = sort_msglist(msglist, num_msgs);
1788
1789         /* Determine the highest message number */
1790         highest_msg = msglist[num_msgs - 1];
1791
1792         /* Write it back to disk. */
1793         cdb_store(CDB_MSGLISTS, &CC->room.QRnumber, (int)sizeof(long),
1794                   msglist, (int)(num_msgs * sizeof(long)));
1795
1796         /* Free up the memory we used. */
1797         free(msglist);
1798
1799         /* Update the highest-message pointer and unlock the room. */
1800         CC->room.QRhighest = highest_msg;
1801         lputroom(&CC->room);
1802         getroom(&CC->room, hold_rm);
1803
1804         /* Bump the reference count for this message. */
1805         if ((flags & SM_DONT_BUMP_REF)==0) {
1806                 AdjRefCount(msgid, +1);
1807         }
1808
1809         /* Return success. */
1810         if (msg != NULL) CtdlFreeMessage(msg);
1811         return (0);
1812 }
1813
1814
1815
1816 /*
1817  * Message base operation to save a new message to the message store
1818  * (returns new message number)
1819  *
1820  * This is the back end for CtdlSubmitMsg() and should not be directly
1821  * called by server-side modules.
1822  *
1823  */
1824 long send_message(struct CtdlMessage *msg) {
1825         long newmsgid;
1826         long retval;
1827         char msgidbuf[256];
1828         struct ser_ret smr;
1829         int is_bigmsg = 0;
1830         char *holdM = NULL;
1831
1832         /* Get a new message number */
1833         newmsgid = get_new_message_number();
1834         snprintf(msgidbuf, sizeof msgidbuf, "%ld@%s", newmsgid, config.c_fqdn);
1835
1836         /* Generate an ID if we don't have one already */
1837         if (msg->cm_fields['I']==NULL) {
1838                 msg->cm_fields['I'] = strdup(msgidbuf);
1839         }
1840
1841         /* If the message is big, set its body aside for storage elsewhere */
1842         if (msg->cm_fields['M'] != NULL) {
1843                 if (strlen(msg->cm_fields['M']) > BIGMSG) {
1844                         is_bigmsg = 1;
1845                         holdM = msg->cm_fields['M'];
1846                         msg->cm_fields['M'] = NULL;
1847                 }
1848         }
1849
1850         /* Serialize our data structure for storage in the database */  
1851         serialize_message(&smr, msg);
1852
1853         if (is_bigmsg) {
1854                 msg->cm_fields['M'] = holdM;
1855         }
1856
1857         if (smr.len == 0) {
1858                 cprintf("%d Unable to serialize message\n",
1859                         ERROR + INTERNAL_ERROR);
1860                 return (-1L);
1861         }
1862
1863         /* Write our little bundle of joy into the message base */
1864         if (cdb_store(CDB_MSGMAIN, &newmsgid, (int)sizeof(long),
1865                       smr.ser, smr.len) < 0) {
1866                 lprintf(CTDL_ERR, "Can't store message\n");
1867                 retval = 0L;
1868         } else {
1869                 if (is_bigmsg) {
1870                         cdb_store(CDB_BIGMSGS,
1871                                 &newmsgid,
1872                                 (int)sizeof(long),
1873                                 holdM,
1874                                 (strlen(holdM) + 1)
1875                         );
1876                 }
1877                 retval = newmsgid;
1878         }
1879
1880         /* Free the memory we used for the serialized message */
1881         free(smr.ser);
1882
1883         /* Return the *local* message ID to the caller
1884          * (even if we're storing an incoming network message)
1885          */
1886         return(retval);
1887 }
1888
1889
1890
1891 /*
1892  * Serialize a struct CtdlMessage into the format used on disk and network.
1893  * 
1894  * This function loads up a "struct ser_ret" (defined in server.h) which
1895  * contains the length of the serialized message and a pointer to the
1896  * serialized message in memory.  THE LATTER MUST BE FREED BY THE CALLER.
1897  */
1898 void serialize_message(struct ser_ret *ret,             /* return values */
1899                         struct CtdlMessage *msg)        /* unserialized msg */
1900 {
1901         size_t wlen;
1902         int i;
1903         static char *forder = FORDER;
1904
1905         if (is_valid_message(msg) == 0) return;         /* self check */
1906
1907         ret->len = 3;
1908         for (i=0; i<26; ++i) if (msg->cm_fields[(int)forder[i]] != NULL)
1909                 ret->len = ret->len +
1910                         strlen(msg->cm_fields[(int)forder[i]]) + 2;
1911
1912         lprintf(CTDL_DEBUG, "serialize_message() calling malloc(%ld)\n", (long)ret->len);
1913         ret->ser = malloc(ret->len);
1914         if (ret->ser == NULL) {
1915                 ret->len = 0;
1916                 return;
1917         }
1918
1919         ret->ser[0] = 0xFF;
1920         ret->ser[1] = msg->cm_anon_type;
1921         ret->ser[2] = msg->cm_format_type;
1922         wlen = 3;
1923
1924         for (i=0; i<26; ++i) if (msg->cm_fields[(int)forder[i]] != NULL) {
1925                 ret->ser[wlen++] = (char)forder[i];
1926                 strcpy((char *)&ret->ser[wlen], msg->cm_fields[(int)forder[i]]);
1927                 wlen = wlen + strlen(msg->cm_fields[(int)forder[i]]) + 1;
1928         }
1929         if (ret->len != wlen) lprintf(CTDL_ERR, "ERROR: len=%ld wlen=%ld\n",
1930                 (long)ret->len, (long)wlen);
1931
1932         return;
1933 }
1934
1935
1936
1937 /*
1938  * Back end for the ReplicationChecks() function
1939  */
1940 void check_repl(long msgnum, void *userdata) {
1941         lprintf(CTDL_DEBUG, "check_repl() replacing message %ld\n", msgnum);
1942         CtdlDeleteMessages(CC->room.QRname, msgnum, "");
1943 }
1944
1945
1946 /*
1947  * Check to see if any messages already exist which carry the same Exclusive ID
1948  * as this one.  If any are found, delete them.
1949  *
1950  */
1951 int ReplicationChecks(struct CtdlMessage *msg) {
1952         struct CtdlMessage *template;
1953         int abort_this = 0;
1954
1955         /* No exclusive id?  Don't do anything. */
1956         if (msg->cm_fields['E'] == NULL) return 0;
1957         if (strlen(msg->cm_fields['E']) == 0) return 0;
1958         lprintf(CTDL_DEBUG, "Exclusive ID: <%s>\n", msg->cm_fields['E']);
1959
1960         template = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1961         memset(template, 0, sizeof(struct CtdlMessage));
1962         template->cm_fields['E'] = strdup(msg->cm_fields['E']);
1963
1964         CtdlForEachMessage(MSGS_ALL, 0L, NULL, template, check_repl, NULL);
1965
1966         CtdlFreeMessage(template);
1967         lprintf(CTDL_DEBUG, "ReplicationChecks() returning %d\n", abort_this);
1968         return(abort_this);
1969 }
1970
1971
1972
1973
1974 /*
1975  * Save a message to disk and submit it into the delivery system.
1976  */
1977 long CtdlSubmitMsg(struct CtdlMessage *msg,     /* message to save */
1978                 struct recptypes *recps,        /* recipients (if mail) */
1979                 char *force                     /* force a particular room? */
1980 ) {
1981         char submit_filename[128];
1982         char generated_timestamp[32];
1983         char hold_rm[ROOMNAMELEN];
1984         char actual_rm[ROOMNAMELEN];
1985         char force_room[ROOMNAMELEN];
1986         char content_type[SIZ];                 /* We have to learn this */
1987         char recipient[SIZ];
1988         long newmsgid;
1989         char *mptr = NULL;
1990         struct ctdluser userbuf;
1991         int a, i;
1992         struct MetaData smi;
1993         FILE *network_fp = NULL;
1994         static int seqnum = 1;
1995         struct CtdlMessage *imsg = NULL;
1996         char *instr;
1997         struct ser_ret smr;
1998         char *hold_R, *hold_D;
1999
2000         lprintf(CTDL_DEBUG, "CtdlSubmitMsg() called\n");
2001         if (is_valid_message(msg) == 0) return(-1);     /* self check */
2002
2003         /* If this message has no timestamp, we take the liberty of
2004          * giving it one, right now.
2005          */
2006         if (msg->cm_fields['T'] == NULL) {
2007                 lprintf(CTDL_DEBUG, "Generating timestamp\n");
2008                 snprintf(generated_timestamp, sizeof generated_timestamp, "%ld", (long)time(NULL));
2009                 msg->cm_fields['T'] = strdup(generated_timestamp);
2010         }
2011
2012         /* If this message has no path, we generate one.
2013          */
2014         if (msg->cm_fields['P'] == NULL) {
2015                 lprintf(CTDL_DEBUG, "Generating path\n");
2016                 if (msg->cm_fields['A'] != NULL) {
2017                         msg->cm_fields['P'] = strdup(msg->cm_fields['A']);
2018                         for (a=0; a<strlen(msg->cm_fields['P']); ++a) {
2019                                 if (isspace(msg->cm_fields['P'][a])) {
2020                                         msg->cm_fields['P'][a] = ' ';
2021                                 }
2022                         }
2023                 }
2024                 else {
2025                         msg->cm_fields['P'] = strdup("unknown");
2026                 }
2027         }
2028
2029         if (force == NULL) {
2030                 strcpy(force_room, "");
2031         }
2032         else {
2033                 strcpy(force_room, force);
2034         }
2035
2036         /* Learn about what's inside, because it's what's inside that counts */
2037         lprintf(CTDL_DEBUG, "Learning what's inside\n");
2038         if (msg->cm_fields['M'] == NULL) {
2039                 lprintf(CTDL_ERR, "ERROR: attempt to save message with NULL body\n");
2040                 return(-2);
2041         }
2042
2043         switch (msg->cm_format_type) {
2044         case 0:
2045                 strcpy(content_type, "text/x-citadel-variformat");
2046                 break;
2047         case 1:
2048                 strcpy(content_type, "text/plain");
2049                 break;
2050         case 4:
2051                 strcpy(content_type, "text/plain");
2052                 mptr = bmstrstr(msg->cm_fields['M'],
2053                                 "Content-type: ", strncasecmp);
2054                 if (mptr != NULL) {
2055                         safestrncpy(content_type, &mptr[14], 
2056                                         sizeof content_type);
2057                         for (a = 0; a < strlen(content_type); ++a) {
2058                                 if ((content_type[a] == ';')
2059                                     || (content_type[a] == ' ')
2060                                     || (content_type[a] == 13)
2061                                     || (content_type[a] == 10)) {
2062                                         content_type[a] = 0;
2063                                 }
2064                         }
2065                 }
2066         }
2067
2068         /* Goto the correct room */
2069         lprintf(CTDL_DEBUG, "Selected room %s\n",
2070                 (recps) ? CC->room.QRname : SENTITEMS);
2071         strcpy(hold_rm, CC->room.QRname);
2072         strcpy(actual_rm, CC->room.QRname);
2073         if (recps != NULL) {
2074                 strcpy(actual_rm, SENTITEMS);
2075         }
2076
2077         /* If the user is a twit, move to the twit room for posting */
2078         lprintf(CTDL_DEBUG, "Handling twit stuff: %s\n",
2079                         (CC->user.axlevel == 2) ? config.c_twitroom : "OK");
2080         if (TWITDETECT) {
2081                 if (CC->user.axlevel == 2) {
2082                         strcpy(hold_rm, actual_rm);
2083                         strcpy(actual_rm, config.c_twitroom);
2084                 }
2085         }
2086
2087         /* ...or if this message is destined for Aide> then go there. */
2088         if (strlen(force_room) > 0) {
2089                 strcpy(actual_rm, force_room);
2090         }
2091
2092         lprintf(CTDL_DEBUG, "Final selection: %s\n", actual_rm);
2093         if (strcasecmp(actual_rm, CC->room.QRname)) {
2094                 /* getroom(&CC->room, actual_rm); */
2095                 usergoto(actual_rm, 0, 1, NULL, NULL);
2096         }
2097
2098         /*
2099          * If this message has no O (room) field, generate one.
2100          */
2101         if (msg->cm_fields['O'] == NULL) {
2102                 msg->cm_fields['O'] = strdup(CC->room.QRname);
2103         }
2104
2105         /* Perform "before save" hooks (aborting if any return nonzero) */
2106         lprintf(CTDL_DEBUG, "Performing before-save hooks\n");
2107         if (PerformMessageHooks(msg, EVT_BEFORESAVE) > 0) return(-3);
2108
2109         /* If this message has an Exclusive ID, perform replication checks */
2110         lprintf(CTDL_DEBUG, "Performing replication checks\n");
2111         if (ReplicationChecks(msg) > 0) return(-4);
2112
2113         /* Save it to disk */
2114         lprintf(CTDL_DEBUG, "Saving to disk\n");
2115         newmsgid = send_message(msg);
2116         if (newmsgid <= 0L) return(-5);
2117
2118         /* Write a supplemental message info record.  This doesn't have to
2119          * be a critical section because nobody else knows about this message
2120          * yet.
2121          */
2122         lprintf(CTDL_DEBUG, "Creating MetaData record\n");
2123         memset(&smi, 0, sizeof(struct MetaData));
2124         smi.meta_msgnum = newmsgid;
2125         smi.meta_refcount = 0;
2126         safestrncpy(smi.meta_content_type, content_type,
2127                         sizeof smi.meta_content_type);
2128
2129         /* As part of the new metadata record, measure how
2130          * big this message will be when displayed as RFC822.
2131          * Both POP and IMAP use this, and it's best to just take the hit now
2132          * instead of having to potentially measure thousands of messages when
2133          * a mailbox is opened later.
2134          */
2135
2136         if (CC->redirect_buffer != NULL) {
2137                 lprintf(CTDL_ALERT, "CC->redirect_buffer is not NULL during message submission!\n");
2138                 abort();
2139         }
2140         CC->redirect_buffer = malloc(SIZ);
2141         CC->redirect_len = 0;
2142         CC->redirect_alloc = SIZ;
2143         CtdlOutputPreLoadedMsg(msg, 0L, MT_RFC822, HEADERS_ALL, 0, 1);
2144         smi.meta_rfc822_length = CC->redirect_len;
2145         free(CC->redirect_buffer);
2146         CC->redirect_buffer = NULL;
2147         CC->redirect_len = 0;
2148         CC->redirect_alloc = 0;
2149
2150         PutMetaData(&smi);
2151
2152         /* Now figure out where to store the pointers */
2153         lprintf(CTDL_DEBUG, "Storing pointers\n");
2154
2155         /* If this is being done by the networker delivering a private
2156          * message, we want to BYPASS saving the sender's copy (because there
2157          * is no local sender; it would otherwise go to the Trashcan).
2158          */
2159         if ((!CC->internal_pgm) || (recps == NULL)) {
2160                 if (CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0) != 0) {
2161                         lprintf(CTDL_ERR, "ERROR saving message pointer!\n");
2162                         CtdlSaveMsgPointerInRoom(config.c_aideroom,
2163                                                         newmsgid, 0);
2164                 }
2165         }
2166
2167         /* For internet mail, drop a copy in the outbound queue room */
2168         if (recps != NULL)
2169          if (recps->num_internet > 0) {
2170                 CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, newmsgid, 0);
2171         }
2172
2173         /* If other rooms are specified, drop them there too. */
2174         if (recps != NULL)
2175          if (recps->num_room > 0)
2176           for (i=0; i<num_tokens(recps->recp_room, '|'); ++i) {
2177                 extract_token(recipient, recps->recp_room, i,
2178                                         '|', sizeof recipient);
2179                 lprintf(CTDL_DEBUG, "Delivering to room <%s>\n", recipient);
2180                 CtdlSaveMsgPointerInRoom(recipient, newmsgid, 0);
2181         }
2182
2183         /* Bump this user's messages posted counter. */
2184         lprintf(CTDL_DEBUG, "Updating user\n");
2185         lgetuser(&CC->user, CC->curr_user);
2186         CC->user.posted = CC->user.posted + 1;
2187         lputuser(&CC->user);
2188
2189         /* If this is private, local mail, make a copy in the
2190          * recipient's mailbox and bump the reference count.
2191          */
2192         if (recps != NULL)
2193          if (recps->num_local > 0)
2194           for (i=0; i<num_tokens(recps->recp_local, '|'); ++i) {
2195                 extract_token(recipient, recps->recp_local, i,
2196                                         '|', sizeof recipient);
2197                 lprintf(CTDL_DEBUG, "Delivering private local mail to <%s>\n",
2198                         recipient);
2199                 if (getuser(&userbuf, recipient) == 0) {
2200                         MailboxName(actual_rm, sizeof actual_rm,
2201                                         &userbuf, MAILROOM);
2202                         CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0);
2203                         BumpNewMailCounter(userbuf.usernum);
2204                 }
2205                 else {
2206                         lprintf(CTDL_DEBUG, "No user <%s>\n", recipient);
2207                         CtdlSaveMsgPointerInRoom(config.c_aideroom,
2208                                                         newmsgid, 0);
2209                 }
2210         }
2211
2212         /* Perform "after save" hooks */
2213         lprintf(CTDL_DEBUG, "Performing after-save hooks\n");
2214         PerformMessageHooks(msg, EVT_AFTERSAVE);
2215
2216         /* For IGnet mail, we have to save a new copy into the spooler for
2217          * each recipient, with the R and D fields set to the recipient and
2218          * destination-node.  This has two ugly side effects: all other
2219          * recipients end up being unlisted in this recipient's copy of the
2220          * message, and it has to deliver multiple messages to the same
2221          * node.  We'll revisit this again in a year or so when everyone has
2222          * a network spool receiver that can handle the new style messages.
2223          */
2224         if (recps != NULL)
2225          if (recps->num_ignet > 0)
2226           for (i=0; i<num_tokens(recps->recp_ignet, '|'); ++i) {
2227                 extract_token(recipient, recps->recp_ignet, i,
2228                                 '|', sizeof recipient);
2229
2230                 hold_R = msg->cm_fields['R'];
2231                 hold_D = msg->cm_fields['D'];
2232                 msg->cm_fields['R'] = malloc(SIZ);
2233                 msg->cm_fields['D'] = malloc(128);
2234                 extract_token(msg->cm_fields['R'], recipient, 0, '@', SIZ);
2235                 extract_token(msg->cm_fields['D'], recipient, 1, '@', 128);
2236                 
2237                 serialize_message(&smr, msg);
2238                 if (smr.len > 0) {
2239                         snprintf(submit_filename, sizeof submit_filename,
2240                                 "./network/spoolin/netmail.%04lx.%04x.%04x",
2241                                 (long) getpid(), CC->cs_pid, ++seqnum);
2242                         network_fp = fopen(submit_filename, "wb+");
2243                         if (network_fp != NULL) {
2244                                 fwrite(smr.ser, smr.len, 1, network_fp);
2245                                 fclose(network_fp);
2246                         }
2247                         free(smr.ser);
2248                 }
2249
2250                 free(msg->cm_fields['R']);
2251                 free(msg->cm_fields['D']);
2252                 msg->cm_fields['R'] = hold_R;
2253                 msg->cm_fields['D'] = hold_D;
2254         }
2255
2256         /* Go back to the room we started from */
2257         lprintf(CTDL_DEBUG, "Returning to original room %s\n", hold_rm);
2258         if (strcasecmp(hold_rm, CC->room.QRname))
2259                 /* getroom(&CC->room, hold_rm); */
2260                 usergoto(hold_rm, 0, 1, NULL, NULL);
2261
2262         /* For internet mail, generate delivery instructions.
2263          * Yes, this is recursive.  Deal with it.  Infinite recursion does
2264          * not happen because the delivery instructions message does not
2265          * contain a recipient.
2266          */
2267         if (recps != NULL)
2268          if (recps->num_internet > 0) {
2269                 lprintf(CTDL_DEBUG, "Generating delivery instructions\n");
2270                 instr = malloc(SIZ * 2);
2271                 snprintf(instr, SIZ * 2,
2272                         "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
2273                         "bounceto|%s@%s\n",
2274                         SPOOLMIME, newmsgid, (long)time(NULL),
2275                         msg->cm_fields['A'], msg->cm_fields['N']
2276                 );
2277
2278                 for (i=0; i<num_tokens(recps->recp_internet, '|'); ++i) {
2279                         size_t tmp = strlen(instr);
2280                         extract_token(recipient, recps->recp_internet, i, '|', sizeof recipient);
2281                         snprintf(&instr[tmp], SIZ * 2 - tmp,
2282                                  "remote|%s|0||\n", recipient);
2283                 }
2284
2285                 imsg = malloc(sizeof(struct CtdlMessage));
2286                 memset(imsg, 0, sizeof(struct CtdlMessage));
2287                 imsg->cm_magic = CTDLMESSAGE_MAGIC;
2288                 imsg->cm_anon_type = MES_NORMAL;
2289                 imsg->cm_format_type = FMT_RFC822;
2290                 imsg->cm_fields['A'] = strdup("Citadel");
2291                 imsg->cm_fields['M'] = instr;
2292                 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
2293                 CtdlFreeMessage(imsg);
2294         }
2295
2296         return(newmsgid);
2297 }
2298
2299
2300
2301 /*
2302  * Convenience function for generating small administrative messages.
2303  */
2304 void quickie_message(char *from, char *to, char *room, char *text, 
2305                         int format_type, char *subject)
2306 {
2307         struct CtdlMessage *msg;
2308         struct recptypes *recp = NULL;
2309
2310         msg = malloc(sizeof(struct CtdlMessage));
2311         memset(msg, 0, sizeof(struct CtdlMessage));
2312         msg->cm_magic = CTDLMESSAGE_MAGIC;
2313         msg->cm_anon_type = MES_NORMAL;
2314         msg->cm_format_type = format_type;
2315         msg->cm_fields['A'] = strdup(from);
2316         if (room != NULL) msg->cm_fields['O'] = strdup(room);
2317         msg->cm_fields['N'] = strdup(NODENAME);
2318         if (to != NULL) {
2319                 msg->cm_fields['R'] = strdup(to);
2320                 recp = validate_recipients(to);
2321         }
2322         if (subject != NULL) {
2323                 msg->cm_fields['U'] = strdup(subject);
2324         }
2325         msg->cm_fields['M'] = strdup(text);
2326
2327         CtdlSubmitMsg(msg, recp, room);
2328         CtdlFreeMessage(msg);
2329         if (recp != NULL) free(recp);
2330 }
2331
2332
2333
2334 /*
2335  * Back end function used by CtdlMakeMessage() and similar functions
2336  */
2337 char *CtdlReadMessageBody(char *terminator,     /* token signalling EOT */
2338                         size_t maxlen,          /* maximum message length */
2339                         char *exist,            /* if non-null, append to it;
2340                                                    exist is ALWAYS freed  */
2341                         int crlf                /* CRLF newlines instead of LF */
2342                         ) {
2343         char buf[1024];
2344         int linelen;
2345         size_t message_len = 0;
2346         size_t buffer_len = 0;
2347         char *ptr;
2348         char *m;
2349         int flushing = 0;
2350         int finished = 0;
2351
2352         if (exist == NULL) {
2353                 m = malloc(4096);
2354                 m[0] = 0;
2355                 buffer_len = 4096;
2356                 message_len = 0;
2357         }
2358         else {
2359                 message_len = strlen(exist);
2360                 buffer_len = message_len + 4096;
2361                 m = realloc(exist, buffer_len);
2362                 if (m == NULL) {
2363                         free(exist);
2364                         return m;
2365                 }
2366         }
2367
2368         /* flush the input if we have nowhere to store it */
2369         if (m == NULL) {
2370                 flushing = 1;
2371         }
2372
2373         /* read in the lines of message text one by one */
2374         do {
2375                 if (client_getln(buf, (sizeof buf - 3)) < 1) finished = 1;
2376                 if (!strcmp(buf, terminator)) finished = 1;
2377                 if (crlf) {
2378                         strcat(buf, "\r\n");
2379                 }
2380                 else {
2381                         strcat(buf, "\n");
2382                 }
2383
2384                 if ( (!flushing) && (!finished) ) {
2385                         /* Measure the line */
2386                         linelen = strlen(buf);
2387         
2388                         /* augment the buffer if we have to */
2389                         if ((message_len + linelen) >= buffer_len) {
2390                                 ptr = realloc(m, (buffer_len * 2) );
2391                                 if (ptr == NULL) {      /* flush if can't allocate */
2392                                         flushing = 1;
2393                                 } else {
2394                                         buffer_len = (buffer_len * 2);
2395                                         m = ptr;
2396                                         lprintf(CTDL_DEBUG, "buffer_len is now %ld\n", (long)buffer_len);
2397                                 }
2398                         }
2399         
2400                         /* Add the new line to the buffer.  NOTE: this loop must avoid
2401                         * using functions like strcat() and strlen() because they
2402                         * traverse the entire buffer upon every call, and doing that
2403                         * for a multi-megabyte message slows it down beyond usability.
2404                         */
2405                         strcpy(&m[message_len], buf);
2406                         message_len += linelen;
2407                 }
2408
2409                 /* if we've hit the max msg length, flush the rest */
2410                 if (message_len >= maxlen) flushing = 1;
2411
2412         } while (!finished);
2413         return(m);
2414 }
2415
2416
2417
2418
2419 /*
2420  * Build a binary message to be saved on disk.
2421  * (NOTE: if you supply 'preformatted_text', the buffer you give it
2422  * will become part of the message.  This means you are no longer
2423  * responsible for managing that memory -- it will be freed along with
2424  * the rest of the fields when CtdlFreeMessage() is called.)
2425  */
2426
2427 struct CtdlMessage *CtdlMakeMessage(
2428         struct ctdluser *author,        /* author's user structure */
2429         char *recipient,                /* NULL if it's not mail */
2430         char *room,                     /* room where it's going */
2431         int type,                       /* see MES_ types in header file */
2432         int format_type,                /* variformat, plain text, MIME... */
2433         char *fake_name,                /* who we're masquerading as */
2434         char *subject,                  /* Subject (optional) */
2435         char *preformatted_text         /* ...or NULL to read text from client */
2436 ) {
2437         char dest_node[SIZ];
2438         char buf[SIZ];
2439         struct CtdlMessage *msg;
2440
2441         msg = malloc(sizeof(struct CtdlMessage));
2442         memset(msg, 0, sizeof(struct CtdlMessage));
2443         msg->cm_magic = CTDLMESSAGE_MAGIC;
2444         msg->cm_anon_type = type;
2445         msg->cm_format_type = format_type;
2446
2447         /* Don't confuse the poor folks if it's not routed mail. */
2448         strcpy(dest_node, "");
2449
2450         striplt(recipient);
2451
2452         snprintf(buf, sizeof buf, "cit%ld", author->usernum);   /* Path */
2453         msg->cm_fields['P'] = strdup(buf);
2454
2455         snprintf(buf, sizeof buf, "%ld", (long)time(NULL));     /* timestamp */
2456         msg->cm_fields['T'] = strdup(buf);
2457
2458         if (fake_name[0])                                       /* author */
2459                 msg->cm_fields['A'] = strdup(fake_name);
2460         else
2461                 msg->cm_fields['A'] = strdup(author->fullname);
2462
2463         if (CC->room.QRflags & QR_MAILBOX) {            /* room */
2464                 msg->cm_fields['O'] = strdup(&CC->room.QRname[11]);
2465         }
2466         else {
2467                 msg->cm_fields['O'] = strdup(CC->room.QRname);
2468         }
2469
2470         msg->cm_fields['N'] = strdup(NODENAME);         /* nodename */
2471         msg->cm_fields['H'] = strdup(HUMANNODE);                /* hnodename */
2472
2473         if (recipient[0] != 0) {
2474                 msg->cm_fields['R'] = strdup(recipient);
2475         }
2476         if (dest_node[0] != 0) {
2477                 msg->cm_fields['D'] = strdup(dest_node);
2478         }
2479
2480         if ( (author == &CC->user) && (strlen(CC->cs_inet_email) > 0) ) {
2481                 msg->cm_fields['F'] = strdup(CC->cs_inet_email);
2482         }
2483
2484         if (subject != NULL) {
2485                 striplt(subject);
2486                 if (strlen(subject) > 0) {
2487                         msg->cm_fields['U'] = strdup(subject);
2488                 }
2489         }
2490
2491         if (preformatted_text != NULL) {
2492                 msg->cm_fields['M'] = preformatted_text;
2493         }
2494         else {
2495                 msg->cm_fields['M'] = CtdlReadMessageBody("000",
2496                                         config.c_maxmsglen, NULL, 0);
2497         }
2498
2499         return(msg);
2500 }
2501
2502
2503 /*
2504  * Check to see whether we have permission to post a message in the current
2505  * room.  Returns a *CITADEL ERROR CODE* and puts a message in errmsgbuf, or
2506  * returns 0 on success.
2507  */
2508 int CtdlDoIHavePermissionToPostInThisRoom(char *errmsgbuf, size_t n) {
2509
2510         if (!(CC->logged_in)) {
2511                 snprintf(errmsgbuf, n, "Not logged in.");
2512                 return (ERROR + NOT_LOGGED_IN);
2513         }
2514
2515         if ((CC->user.axlevel < 2)
2516             && ((CC->room.QRflags & QR_MAILBOX) == 0)) {
2517                 snprintf(errmsgbuf, n, "Need to be validated to enter "
2518                                 "(except in %s> to sysop)", MAILROOM);
2519                 return (ERROR + HIGHER_ACCESS_REQUIRED);
2520         }
2521
2522         if ((CC->user.axlevel < 4)
2523            && (CC->room.QRflags & QR_NETWORK)) {
2524                 snprintf(errmsgbuf, n, "Need net privileges to enter here.");
2525                 return (ERROR + HIGHER_ACCESS_REQUIRED);
2526         }
2527
2528         if ((CC->user.axlevel < 6)
2529            && (CC->room.QRflags & QR_READONLY)) {
2530                 snprintf(errmsgbuf, n, "Sorry, this is a read-only room.");
2531                 return (ERROR + HIGHER_ACCESS_REQUIRED);
2532         }
2533
2534         strcpy(errmsgbuf, "Ok");
2535         return(0);
2536 }
2537
2538
2539 /*
2540  * Check to see if the specified user has Internet mail permission
2541  * (returns nonzero if permission is granted)
2542  */
2543 int CtdlCheckInternetMailPermission(struct ctdluser *who) {
2544
2545         /* Do not allow twits to send Internet mail */
2546         if (who->axlevel <= 2) return(0);
2547
2548         /* Globally enabled? */
2549         if (config.c_restrict == 0) return(1);
2550
2551         /* User flagged ok? */
2552         if (who->flags & US_INTERNET) return(2);
2553
2554         /* Aide level access? */
2555         if (who->axlevel >= 6) return(3);
2556
2557         /* No mail for you! */
2558         return(0);
2559 }
2560
2561
2562
2563 /*
2564  * Validate recipients, count delivery types and errors, and handle aliasing
2565  * FIXME check for dupes!!!!!
2566  */
2567 struct recptypes *validate_recipients(char *recipients) {
2568         struct recptypes *ret;
2569         char this_recp[SIZ];
2570         char this_recp_cooked[SIZ];
2571         char append[SIZ];
2572         int num_recps;
2573         int i, j;
2574         int mailtype;
2575         int invalid;
2576         struct ctdluser tempUS;
2577         struct ctdlroom tempQR;
2578
2579         /* Initialize */
2580         ret = (struct recptypes *) malloc(sizeof(struct recptypes));
2581         if (ret == NULL) return(NULL);
2582         memset(ret, 0, sizeof(struct recptypes));
2583
2584         ret->num_local = 0;
2585         ret->num_internet = 0;
2586         ret->num_ignet = 0;
2587         ret->num_error = 0;
2588         ret->num_room = 0;
2589
2590         if (recipients == NULL) {
2591                 num_recps = 0;
2592         }
2593         else if (strlen(recipients) == 0) {
2594                 num_recps = 0;
2595         }
2596         else {
2597                 /* Change all valid separator characters to commas */
2598                 for (i=0; i<strlen(recipients); ++i) {
2599                         if ((recipients[i] == ';') || (recipients[i] == '|')) {
2600                                 recipients[i] = ',';
2601                         }
2602                 }
2603
2604                 /* Count 'em up */
2605                 num_recps = num_tokens(recipients, ',');
2606         }
2607
2608         if (num_recps > 0) for (i=0; i<num_recps; ++i) {
2609                 extract_token(this_recp, recipients, i, ',', sizeof this_recp);
2610                 striplt(this_recp);
2611                 lprintf(CTDL_DEBUG, "Evaluating recipient #%d <%s>\n", i, this_recp);
2612                 mailtype = alias(this_recp);
2613                 mailtype = alias(this_recp);
2614                 mailtype = alias(this_recp);
2615                 for (j=0; j<=strlen(this_recp); ++j) {
2616                         if (this_recp[j]=='_') {
2617                                 this_recp_cooked[j] = ' ';
2618                         }
2619                         else {
2620                                 this_recp_cooked[j] = this_recp[j];
2621                         }
2622                 }
2623                 invalid = 0;
2624                 switch(mailtype) {
2625                         case MES_LOCAL:
2626                                 if (!strcasecmp(this_recp, "sysop")) {
2627                                         ++ret->num_room;
2628                                         strcpy(this_recp, config.c_aideroom);
2629                                         if (strlen(ret->recp_room) > 0) {
2630                                                 strcat(ret->recp_room, "|");
2631                                         }
2632                                         strcat(ret->recp_room, this_recp);
2633                                 }
2634                                 else if (getuser(&tempUS, this_recp) == 0) {
2635                                         ++ret->num_local;
2636                                         strcpy(this_recp, tempUS.fullname);
2637                                         if (strlen(ret->recp_local) > 0) {
2638                                                 strcat(ret->recp_local, "|");
2639                                         }
2640                                         strcat(ret->recp_local, this_recp);
2641                                 }
2642                                 else if (getuser(&tempUS, this_recp_cooked) == 0) {
2643                                         ++ret->num_local;
2644                                         strcpy(this_recp, tempUS.fullname);
2645                                         if (strlen(ret->recp_local) > 0) {
2646                                                 strcat(ret->recp_local, "|");
2647                                         }
2648                                         strcat(ret->recp_local, this_recp);
2649                                 }
2650                                 else if ( (!strncasecmp(this_recp, "room_", 5))
2651                                       && (!getroom(&tempQR, &this_recp_cooked[5])) ) {
2652                                         ++ret->num_room;
2653                                         if (strlen(ret->recp_room) > 0) {
2654                                                 strcat(ret->recp_room, "|");
2655                                         }
2656                                         strcat(ret->recp_room, &this_recp_cooked[5]);
2657                                 }
2658                                 else {
2659                                         ++ret->num_error;
2660                                         invalid = 1;
2661                                 }
2662                                 break;
2663                         case MES_INTERNET:
2664                                 /* Yes, you're reading this correctly: if the target
2665                                  * domain points back to the local system or an attached
2666                                  * Citadel directory, the address is invalid.  That's
2667                                  * because if the address were valid, we would have
2668                                  * already translated it to a local address by now.
2669                                  */
2670                                 if (IsDirectory(this_recp)) {
2671                                         ++ret->num_error;
2672                                         invalid = 1;
2673                                 }
2674                                 else {
2675                                         ++ret->num_internet;
2676                                         if (strlen(ret->recp_internet) > 0) {
2677                                                 strcat(ret->recp_internet, "|");
2678                                         }
2679                                         strcat(ret->recp_internet, this_recp);
2680                                 }
2681                                 break;
2682                         case MES_IGNET:
2683                                 ++ret->num_ignet;
2684                                 if (strlen(ret->recp_ignet) > 0) {
2685                                         strcat(ret->recp_ignet, "|");
2686                                 }
2687                                 strcat(ret->recp_ignet, this_recp);
2688                                 break;
2689                         case MES_ERROR:
2690                                 ++ret->num_error;
2691                                 invalid = 1;
2692                                 break;
2693                 }
2694                 if (invalid) {
2695                         if (strlen(ret->errormsg) == 0) {
2696                                 snprintf(append, sizeof append,
2697                                          "Invalid recipient: %s",
2698                                          this_recp);
2699                         }
2700                         else {
2701                                 snprintf(append, sizeof append,
2702                                          ", %s", this_recp);
2703                         }
2704                         if ( (strlen(ret->errormsg) + strlen(append)) < SIZ) {
2705                                 strcat(ret->errormsg, append);
2706                         }
2707                 }
2708                 else {
2709                         if (strlen(ret->display_recp) == 0) {
2710                                 strcpy(append, this_recp);
2711                         }
2712                         else {
2713                                 snprintf(append, sizeof append, ", %s",
2714                                          this_recp);
2715                         }
2716                         if ( (strlen(ret->display_recp)+strlen(append)) < SIZ) {
2717                                 strcat(ret->display_recp, append);
2718                         }
2719                 }
2720         }
2721
2722         if ((ret->num_local + ret->num_internet + ret->num_ignet +
2723            ret->num_room + ret->num_error) == 0) {
2724                 ++ret->num_error;
2725                 strcpy(ret->errormsg, "No recipients specified.");
2726         }
2727
2728         lprintf(CTDL_DEBUG, "validate_recipients()\n");
2729         lprintf(CTDL_DEBUG, " local: %d <%s>\n", ret->num_local, ret->recp_local);
2730         lprintf(CTDL_DEBUG, "  room: %d <%s>\n", ret->num_room, ret->recp_room);
2731         lprintf(CTDL_DEBUG, "  inet: %d <%s>\n", ret->num_internet, ret->recp_internet);
2732         lprintf(CTDL_DEBUG, " ignet: %d <%s>\n", ret->num_ignet, ret->recp_ignet);
2733         lprintf(CTDL_DEBUG, " error: %d <%s>\n", ret->num_error, ret->errormsg);
2734
2735         return(ret);
2736 }
2737
2738
2739
2740 /*
2741  * message entry  -  mode 0 (normal)
2742  */
2743 void cmd_ent0(char *entargs)
2744 {
2745         int post = 0;
2746         char recp[SIZ];
2747         char masquerade_as[SIZ];
2748         int anon_flag = 0;
2749         int format_type = 0;
2750         char newusername[SIZ];
2751         struct CtdlMessage *msg;
2752         int anonymous = 0;
2753         char errmsg[SIZ];
2754         int err = 0;
2755         struct recptypes *valid = NULL;
2756         char subject[SIZ];
2757         int do_confirm = 0;
2758         long msgnum;
2759
2760         unbuffer_output();
2761
2762         post = extract_int(entargs, 0);
2763         extract_token(recp, entargs, 1, '|', sizeof recp);
2764         anon_flag = extract_int(entargs, 2);
2765         format_type = extract_int(entargs, 3);
2766         extract_token(subject, entargs, 4, '|', sizeof subject);
2767         do_confirm = extract_int(entargs, 6);
2768
2769         /* first check to make sure the request is valid. */
2770
2771         err = CtdlDoIHavePermissionToPostInThisRoom(errmsg, sizeof errmsg);
2772         if (err) {
2773                 cprintf("%d %s\n", err, errmsg);
2774                 return;
2775         }
2776
2777         /* Check some other permission type things. */
2778
2779         if (post == 2) {
2780                 if (CC->user.axlevel < 6) {
2781                         cprintf("%d You don't have permission to masquerade.\n",
2782                                 ERROR + HIGHER_ACCESS_REQUIRED);
2783                         return;
2784                 }
2785                 extract_token(newusername, entargs, 5, '|', sizeof newusername);
2786                 memset(CC->fake_postname, 0, sizeof(CC->fake_postname) );
2787                 safestrncpy(CC->fake_postname, newusername,
2788                         sizeof(CC->fake_postname) );
2789                 cprintf("%d ok\n", CIT_OK);
2790                 return;
2791         }
2792         CC->cs_flags |= CS_POSTING;
2793
2794         /* In the Mail> room we have to behave a little differently --
2795          * make sure the user has specified at least one recipient.  Then
2796          * validate the recipient(s).
2797          */
2798         if ( (CC->room.QRflags & QR_MAILBOX)
2799            && (!strcasecmp(&CC->room.QRname[11], MAILROOM)) ) {
2800
2801                 if (CC->user.axlevel < 2) {
2802                         strcpy(recp, "sysop");
2803                 }
2804
2805                 valid = validate_recipients(recp);
2806                 if (valid->num_error > 0) {
2807                         cprintf("%d %s\n",
2808                                 ERROR + NO_SUCH_USER, valid->errormsg);
2809                         free(valid);
2810                         return;
2811                 }
2812                 if (valid->num_internet > 0) {
2813                         if (CtdlCheckInternetMailPermission(&CC->user)==0) {
2814                                 cprintf("%d You do not have permission "
2815                                         "to send Internet mail.\n",
2816                                         ERROR + HIGHER_ACCESS_REQUIRED);
2817                                 free(valid);
2818                                 return;
2819                         }
2820                 }
2821
2822                 if ( ( (valid->num_internet + valid->num_ignet) > 0)
2823                    && (CC->user.axlevel < 4) ) {
2824                         cprintf("%d Higher access required for network mail.\n",
2825                                 ERROR + HIGHER_ACCESS_REQUIRED);
2826                         free(valid);
2827                         return;
2828                 }
2829         
2830                 if ((RESTRICT_INTERNET == 1) && (valid->num_internet > 0)
2831                     && ((CC->user.flags & US_INTERNET) == 0)
2832                     && (!CC->internal_pgm)) {
2833                         cprintf("%d You don't have access to Internet mail.\n",
2834                                 ERROR + HIGHER_ACCESS_REQUIRED);
2835                         free(valid);
2836                         return;
2837                 }
2838
2839         }
2840
2841         /* Is this a room which has anonymous-only or anonymous-option? */
2842         anonymous = MES_NORMAL;
2843         if (CC->room.QRflags & QR_ANONONLY) {
2844                 anonymous = MES_ANONONLY;
2845         }
2846         if (CC->room.QRflags & QR_ANONOPT) {
2847                 if (anon_flag == 1) {   /* only if the user requested it */
2848                         anonymous = MES_ANONOPT;
2849                 }
2850         }
2851
2852         if ((CC->room.QRflags & QR_MAILBOX) == 0) {
2853                 recp[0] = 0;
2854         }
2855
2856         /* If we're only checking the validity of the request, return
2857          * success without creating the message.
2858          */
2859         if (post == 0) {
2860                 cprintf("%d %s\n", CIT_OK,
2861                         ((valid != NULL) ? valid->display_recp : "") );
2862                 free(valid);
2863                 return;
2864         }
2865
2866         /* Handle author masquerading */
2867         if (CC->fake_postname[0]) {
2868                 strcpy(masquerade_as, CC->fake_postname);
2869         }
2870         else if (CC->fake_username[0]) {
2871                 strcpy(masquerade_as, CC->fake_username);
2872         }
2873         else {
2874                 strcpy(masquerade_as, "");
2875         }
2876
2877         /* Read in the message from the client. */
2878         if (do_confirm) {
2879                 cprintf("%d send message\n", START_CHAT_MODE);
2880         } else {
2881                 cprintf("%d send message\n", SEND_LISTING);
2882         }
2883         msg = CtdlMakeMessage(&CC->user, recp,
2884                 CC->room.QRname, anonymous, format_type,
2885                 masquerade_as, subject, NULL);
2886
2887         if (msg != NULL) {
2888                 msgnum = CtdlSubmitMsg(msg, valid, "");
2889
2890                 if (do_confirm) {
2891                         cprintf("%ld\n", msgnum);
2892                         if (msgnum >= 0L) {
2893                                 cprintf("Message accepted.\n");
2894                         }
2895                         else {
2896                                 cprintf("Internal error.\n");
2897                         }
2898                         if (msg->cm_fields['E'] != NULL) {
2899                                 cprintf("%s\n", msg->cm_fields['E']);
2900                         } else {
2901                                 cprintf("\n");
2902                         }
2903                         cprintf("000\n");
2904                 }
2905
2906                 CtdlFreeMessage(msg);
2907         }
2908         CC->fake_postname[0] = '\0';
2909         free(valid);
2910         return;
2911 }
2912
2913
2914
2915 /*
2916  * API function to delete messages which match a set of criteria
2917  * (returns the actual number of messages deleted)
2918  */
2919 int CtdlDeleteMessages(char *room_name,         /* which room */
2920                        long dmsgnum,            /* or "0" for any */
2921                        char *content_type       /* or "" for any */
2922 )
2923 {
2924
2925         struct ctdlroom qrbuf;
2926         struct cdbdata *cdbfr;
2927         long *msglist = NULL;
2928         long *dellist = NULL;
2929         int num_msgs = 0;
2930         int i;
2931         int num_deleted = 0;
2932         int delete_this;
2933         struct MetaData smi;
2934
2935         lprintf(CTDL_DEBUG, "CtdlDeleteMessages(%s, %ld, %s)\n",
2936                 room_name, dmsgnum, content_type);
2937
2938         /* get room record, obtaining a lock... */
2939         if (lgetroom(&qrbuf, room_name) != 0) {
2940                 lprintf(CTDL_ERR, "CtdlDeleteMessages(): Room <%s> not found\n",
2941                         room_name);
2942                 return (0);     /* room not found */
2943         }
2944         cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
2945
2946         if (cdbfr != NULL) {
2947                 msglist = malloc(cdbfr->len);
2948                 dellist = malloc(cdbfr->len);
2949                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
2950                 num_msgs = cdbfr->len / sizeof(long);
2951                 cdb_free(cdbfr);
2952         }
2953         if (num_msgs > 0) {
2954                 for (i = 0; i < num_msgs; ++i) {
2955                         delete_this = 0x00;
2956
2957                         /* Set/clear a bit for each criterion */
2958
2959                         if ((dmsgnum == 0L) || (msglist[i] == dmsgnum)) {
2960                                 delete_this |= 0x01;
2961                         }
2962                         if (strlen(content_type) == 0) {
2963                                 delete_this |= 0x02;
2964                         } else {
2965                                 GetMetaData(&smi, msglist[i]);
2966                                 if (!strcasecmp(smi.meta_content_type,
2967                                                 content_type)) {
2968                                         delete_this |= 0x02;
2969                                 }
2970                         }
2971
2972                         /* Delete message only if all bits are set */
2973                         if (delete_this == 0x03) {
2974                                 dellist[num_deleted++] = msglist[i];
2975                                 msglist[i] = 0L;
2976                         }
2977                 }
2978
2979                 num_msgs = sort_msglist(msglist, num_msgs);
2980                 cdb_store(CDB_MSGLISTS, &qrbuf.QRnumber, (int)sizeof(long),
2981                           msglist, (int)(num_msgs * sizeof(long)));
2982
2983                 qrbuf.QRhighest = msglist[num_msgs - 1];
2984         }
2985         lputroom(&qrbuf);
2986
2987         /* Go through the messages we pulled out of the index, and decrement
2988          * their reference counts by 1.  If this is the only room the message
2989          * was in, the reference count will reach zero and the message will
2990          * automatically be deleted from the database.  We do this in a
2991          * separate pass because there might be plug-in hooks getting called,
2992          * and we don't want that happening during an S_ROOMS critical
2993          * section.
2994          */
2995         if (num_deleted) for (i=0; i<num_deleted; ++i) {
2996                 PerformDeleteHooks(qrbuf.QRname, dellist[i]);
2997                 AdjRefCount(dellist[i], -1);
2998         }
2999
3000         /* Now free the memory we used, and go away. */
3001         if (msglist != NULL) free(msglist);
3002         if (dellist != NULL) free(dellist);
3003         lprintf(CTDL_DEBUG, "%d message(s) deleted.\n", num_deleted);
3004         return (num_deleted);
3005 }
3006
3007
3008
3009 /*
3010  * Check whether the current user has permission to delete messages from
3011  * the current room (returns 1 for yes, 0 for no)
3012  */
3013 int CtdlDoIHavePermissionToDeleteMessagesFromThisRoom(void) {
3014         getuser(&CC->user, CC->curr_user);
3015         if ((CC->user.axlevel < 6)
3016             && (CC->user.usernum != CC->room.QRroomaide)
3017             && ((CC->room.QRflags & QR_MAILBOX) == 0)
3018             && (!(CC->internal_pgm))) {
3019                 return(0);
3020         }
3021         return(1);
3022 }
3023
3024
3025
3026 /*
3027  * Delete message from current room
3028  */
3029 void cmd_dele(char *delstr)
3030 {
3031         long delnum;
3032         int num_deleted;
3033
3034         if (CtdlDoIHavePermissionToDeleteMessagesFromThisRoom() == 0) {
3035                 cprintf("%d Higher access required.\n",
3036                         ERROR + HIGHER_ACCESS_REQUIRED);
3037                 return;
3038         }
3039         delnum = extract_long(delstr, 0);
3040
3041         num_deleted = CtdlDeleteMessages(CC->room.QRname, delnum, "");
3042
3043         if (num_deleted) {
3044                 cprintf("%d %d message%s deleted.\n", CIT_OK,
3045                         num_deleted, ((num_deleted != 1) ? "s" : ""));
3046         } else {
3047                 cprintf("%d Message %ld not found.\n", ERROR + MESSAGE_NOT_FOUND, delnum);
3048         }
3049 }
3050
3051
3052 /*
3053  * Back end API function for moves and deletes
3054  */
3055 int CtdlCopyMsgToRoom(long msgnum, char *dest) {
3056         int err;
3057
3058         err = CtdlSaveMsgPointerInRoom(dest, msgnum,
3059                 (SM_VERIFY_GOODNESS | SM_DO_REPL_CHECK) );
3060         if (err != 0) return(err);
3061
3062         return(0);
3063 }
3064
3065
3066
3067 /*
3068  * move or copy a message to another room
3069  */
3070 void cmd_move(char *args)
3071 {
3072         long num;
3073         char targ[ROOMNAMELEN];
3074         struct ctdlroom qtemp;
3075         int err;
3076         int is_copy = 0;
3077         int ra;
3078         int permit = 0;
3079
3080         num = extract_long(args, 0);
3081         extract_token(targ, args, 1, '|', sizeof targ);
3082         targ[ROOMNAMELEN - 1] = 0;
3083         is_copy = extract_int(args, 2);
3084
3085         if (getroom(&qtemp, targ) != 0) {
3086                 cprintf("%d '%s' does not exist.\n",
3087                         ERROR + ROOM_NOT_FOUND, targ);
3088                 return;
3089         }
3090
3091         getuser(&CC->user, CC->curr_user);
3092         CtdlRoomAccess(&qtemp, &CC->user, &ra, NULL);
3093
3094         /* Check for permission to perform this operation.
3095          * Remember: "CC->room" is source, "qtemp" is target.
3096          */
3097         permit = 0;
3098
3099         /* Aides can move/copy */
3100         if (CC->user.axlevel >= 6) permit = 1;
3101
3102         /* Room aides can move/copy */
3103         if (CC->user.usernum == CC->room.QRroomaide) permit = 1;
3104
3105         /* Permit move/copy from personal rooms */
3106         if ((CC->room.QRflags & QR_MAILBOX)
3107            && (qtemp.QRflags & QR_MAILBOX)) permit = 1;
3108
3109         /* Permit only copy from public to personal room */
3110         if ( (is_copy)
3111            && (!(CC->room.QRflags & QR_MAILBOX))
3112            && (qtemp.QRflags & QR_MAILBOX)) permit = 1;
3113
3114         /* User must have access to target room */
3115         if (!(ra & UA_KNOWN))  permit = 0;
3116
3117         if (!permit) {
3118                 cprintf("%d Higher access required.\n",
3119                         ERROR + HIGHER_ACCESS_REQUIRED);
3120                 return;
3121         }
3122
3123         err = CtdlCopyMsgToRoom(num, targ);
3124         if (err != 0) {
3125                 cprintf("%d Cannot store message in %s: error %d\n",
3126                         err, targ, err);
3127                 return;
3128         }
3129
3130         /* Now delete the message from the source room,
3131          * if this is a 'move' rather than a 'copy' operation.
3132          */
3133         if (is_copy == 0) {
3134                 CtdlDeleteMessages(CC->room.QRname, num, "");
3135         }
3136
3137         cprintf("%d Message %s.\n", CIT_OK, (is_copy ? "copied" : "moved") );
3138 }
3139
3140
3141
3142 /*
3143  * GetMetaData()  -  Get the supplementary record for a message
3144  */
3145 void GetMetaData(struct MetaData *smibuf, long msgnum)
3146 {
3147
3148         struct cdbdata *cdbsmi;
3149         long TheIndex;
3150
3151         memset(smibuf, 0, sizeof(struct MetaData));
3152         smibuf->meta_msgnum = msgnum;
3153         smibuf->meta_refcount = 1;      /* Default reference count is 1 */
3154
3155         /* Use the negative of the message number for its supp record index */
3156         TheIndex = (0L - msgnum);
3157
3158         cdbsmi = cdb_fetch(CDB_MSGMAIN, &TheIndex, sizeof(long));
3159         if (cdbsmi == NULL) {
3160                 return;         /* record not found; go with defaults */
3161         }
3162         memcpy(smibuf, cdbsmi->ptr,
3163                ((cdbsmi->len > sizeof(struct MetaData)) ?
3164                 sizeof(struct MetaData) : cdbsmi->len));
3165         cdb_free(cdbsmi);
3166         return;
3167 }
3168
3169
3170 /*
3171  * PutMetaData()  -  (re)write supplementary record for a message
3172  */
3173 void PutMetaData(struct MetaData *smibuf)
3174 {
3175         long TheIndex;
3176
3177         /* Use the negative of the message number for the metadata db index */
3178         TheIndex = (0L - smibuf->meta_msgnum);
3179
3180         lprintf(CTDL_DEBUG, "PutMetaData(%ld) - ref count is %d\n",
3181                 smibuf->meta_msgnum, smibuf->meta_refcount);
3182
3183         cdb_store(CDB_MSGMAIN,
3184                   &TheIndex, (int)sizeof(long),
3185                   smibuf, (int)sizeof(struct MetaData));
3186
3187 }
3188
3189 /*
3190  * AdjRefCount  -  change the reference count for a message;
3191  *               delete the message if it reaches zero
3192  */
3193 void AdjRefCount(long msgnum, int incr)
3194 {
3195
3196         struct MetaData smi;
3197         long delnum;
3198
3199         /* This is a *tight* critical section; please keep it that way, as
3200          * it may get called while nested in other critical sections.  
3201          * Complicating this any further will surely cause deadlock!
3202          */
3203         begin_critical_section(S_SUPPMSGMAIN);
3204         GetMetaData(&smi, msgnum);
3205         smi.meta_refcount += incr;
3206         PutMetaData(&smi);
3207         end_critical_section(S_SUPPMSGMAIN);
3208         lprintf(CTDL_DEBUG, "msg %ld ref count incr %d, is now %d\n",
3209                 msgnum, incr, smi.meta_refcount);
3210
3211         /* If the reference count is now zero, delete the message
3212          * (and its supplementary record as well).
3213          * FIXME ... defer this so it doesn't keep the user waiting.
3214          */
3215         if (smi.meta_refcount == 0) {
3216                 lprintf(CTDL_DEBUG, "Deleting message <%ld>\n", msgnum);
3217
3218                 /* Remove from fulltext index */
3219                 if (config.c_enable_fulltext) {
3220                         ft_index_message(msgnum, 0);
3221                 }
3222
3223                 /* Remove from message base */
3224                 delnum = msgnum;
3225                 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3226                 cdb_delete(CDB_BIGMSGS, &delnum, (int)sizeof(long));
3227
3228                 /* Remove metadata record */
3229                 delnum = (0L - msgnum);
3230                 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3231         }
3232 }
3233
3234 /*
3235  * Write a generic object to this room
3236  *
3237  * Note: this could be much more efficient.  Right now we use two temporary
3238  * files, and still pull the message into memory as with all others.
3239  */
3240 void CtdlWriteObject(char *req_room,            /* Room to stuff it in */
3241                         char *content_type,     /* MIME type of this object */
3242                         char *tempfilename,     /* Where to fetch it from */
3243                         struct ctdluser *is_mailbox,    /* Mailbox room? */
3244                         int is_binary,          /* Is encoding necessary? */
3245                         int is_unique,          /* Del others of this type? */
3246                         unsigned int flags      /* Internal save flags */
3247                         )
3248 {
3249
3250         FILE *fp;
3251         struct ctdlroom qrbuf;
3252         char roomname[ROOMNAMELEN];
3253         struct CtdlMessage *msg;
3254
3255         char *raw_message = NULL;
3256         char *encoded_message = NULL;
3257         off_t raw_length = 0;
3258
3259         if (is_mailbox != NULL)
3260                 MailboxName(roomname, sizeof roomname, is_mailbox, req_room);
3261         else
3262                 safestrncpy(roomname, req_room, sizeof(roomname));
3263         lprintf(CTDL_DEBUG, "CtdlWriteObject() to <%s> (flags=%d)\n", roomname, flags);
3264
3265
3266         fp = fopen(tempfilename, "rb");
3267         if (fp == NULL) {
3268                 lprintf(CTDL_CRIT, "Cannot open %s: %s\n",
3269                         tempfilename, strerror(errno));
3270                 return;
3271         }
3272         fseek(fp, 0L, SEEK_END);
3273         raw_length = ftell(fp);
3274         rewind(fp);
3275         lprintf(CTDL_DEBUG, "Raw length is %ld\n", (long)raw_length);
3276
3277         raw_message = malloc((size_t)raw_length + 2);
3278         fread(raw_message, (size_t)raw_length, 1, fp);
3279         fclose(fp);
3280
3281         if (is_binary) {
3282                 encoded_message = malloc((size_t)
3283                         (((raw_length * 134) / 100) + 4096 ) );
3284         }
3285         else {
3286                 encoded_message = malloc((size_t)(raw_length + 4096));
3287         }
3288
3289         sprintf(encoded_message, "Content-type: %s\n", content_type);
3290
3291         if (is_binary) {
3292                 sprintf(&encoded_message[strlen(encoded_message)],
3293                         "Content-transfer-encoding: base64\n\n"
3294                 );
3295         }
3296         else {
3297                 sprintf(&encoded_message[strlen(encoded_message)],
3298                         "Content-transfer-encoding: 7bit\n\n"
3299                 );
3300         }
3301
3302         if (is_binary) {
3303                 CtdlEncodeBase64(
3304                         &encoded_message[strlen(encoded_message)],
3305                         raw_message,
3306                         (int)raw_length
3307                 );
3308         }
3309         else {
3310                 raw_message[raw_length] = 0;
3311                 memcpy(
3312                         &encoded_message[strlen(encoded_message)],
3313                         raw_message,
3314                         (int)(raw_length+1)
3315                 );
3316         }
3317
3318         free(raw_message);
3319
3320         lprintf(CTDL_DEBUG, "Allocating\n");
3321         msg = malloc(sizeof(struct CtdlMessage));
3322         memset(msg, 0, sizeof(struct CtdlMessage));
3323         msg->cm_magic = CTDLMESSAGE_MAGIC;
3324         msg->cm_anon_type = MES_NORMAL;
3325         msg->cm_format_type = 4;
3326         msg->cm_fields['A'] = strdup(CC->user.fullname);
3327         msg->cm_fields['O'] = strdup(req_room);
3328         msg->cm_fields['N'] = strdup(config.c_nodename);
3329         msg->cm_fields['H'] = strdup(config.c_humannode);
3330         msg->cm_flags = flags;
3331         
3332         msg->cm_fields['M'] = encoded_message;
3333
3334         /* Create the requested room if we have to. */
3335         if (getroom(&qrbuf, roomname) != 0) {
3336                 create_room(roomname, 
3337                         ( (is_mailbox != NULL) ? 5 : 3 ),
3338                         "", 0, 1, 0, VIEW_BBS);
3339         }
3340         /* If the caller specified this object as unique, delete all
3341          * other objects of this type that are currently in the room.
3342          */
3343         if (is_unique) {
3344                 lprintf(CTDL_DEBUG, "Deleted %d other msgs of this type\n",
3345                         CtdlDeleteMessages(roomname, 0L, content_type));
3346         }
3347         /* Now write the data */
3348         CtdlSubmitMsg(msg, NULL, roomname);
3349         CtdlFreeMessage(msg);
3350 }
3351
3352
3353
3354
3355
3356
3357 void CtdlGetSysConfigBackend(long msgnum, void *userdata) {
3358         config_msgnum = msgnum;
3359 }
3360
3361
3362 char *CtdlGetSysConfig(char *sysconfname) {
3363         char hold_rm[ROOMNAMELEN];
3364         long msgnum;
3365         char *conf;
3366         struct CtdlMessage *msg;
3367         char buf[SIZ];
3368         
3369         strcpy(hold_rm, CC->room.QRname);
3370         if (getroom(&CC->room, SYSCONFIGROOM) != 0) {
3371                 getroom(&CC->room, hold_rm);
3372                 return NULL;
3373         }
3374
3375
3376         /* We want the last (and probably only) config in this room */
3377         begin_critical_section(S_CONFIG);
3378         config_msgnum = (-1L);
3379         CtdlForEachMessage(MSGS_LAST, 1, sysconfname, NULL,
3380                 CtdlGetSysConfigBackend, NULL);
3381         msgnum = config_msgnum;
3382         end_critical_section(S_CONFIG);
3383
3384         if (msgnum < 0L) {
3385                 conf = NULL;
3386         }
3387         else {
3388                 msg = CtdlFetchMessage(msgnum, 1);
3389                 if (msg != NULL) {
3390                         conf = strdup(msg->cm_fields['M']);
3391                         CtdlFreeMessage(msg);
3392                 }
3393                 else {
3394                         conf = NULL;
3395                 }
3396         }
3397
3398         getroom(&CC->room, hold_rm);
3399
3400         if (conf != NULL) do {
3401                 extract_token(buf, conf, 0, '\n', sizeof buf);
3402                 strcpy(conf, &conf[strlen(buf)+1]);
3403         } while ( (strlen(conf)>0) && (strlen(buf)>0) );
3404
3405         return(conf);
3406 }
3407
3408 void CtdlPutSysConfig(char *sysconfname, char *sysconfdata) {
3409         char temp[PATH_MAX];
3410         FILE *fp;
3411
3412         strcpy(temp, tmpnam(NULL));
3413
3414         fp = fopen(temp, "w");
3415         if (fp == NULL) return;
3416         fprintf(fp, "%s", sysconfdata);
3417         fclose(fp);
3418
3419         /* this handy API function does all the work for us */
3420         CtdlWriteObject(SYSCONFIGROOM, sysconfname, temp, NULL, 0, 1, 0);
3421         unlink(temp);
3422 }
3423
3424
3425 /*
3426  * Determine whether a given Internet address belongs to the current user
3427  */
3428 int CtdlIsMe(char *addr, int addr_buf_len)
3429 {
3430         struct recptypes *recp;
3431         int i;
3432
3433         recp = validate_recipients(addr);
3434         if (recp == NULL) return(0);
3435
3436         if (recp->num_local == 0) {
3437                 free(recp);
3438                 return(0);
3439         }
3440
3441         for (i=0; i<recp->num_local; ++i) {
3442                 extract_token(addr, recp->recp_local, i, '|', addr_buf_len);
3443                 if (!strcasecmp(addr, CC->user.fullname)) {
3444                         free(recp);
3445                         return(1);
3446                 }
3447         }
3448
3449         free(recp);
3450         return(0);
3451 }
3452
3453
3454 /*
3455  * Citadel protocol command to do the same
3456  */
3457 void cmd_isme(char *argbuf) {
3458         char addr[256];
3459
3460         if (CtdlAccessCheck(ac_logged_in)) return;
3461         extract_token(addr, argbuf, 0, '|', sizeof addr);
3462
3463         if (CtdlIsMe(addr, sizeof addr)) {
3464                 cprintf("%d %s\n", CIT_OK, addr);
3465         }
3466         else {
3467                 cprintf("%d Not you.\n", ERROR + ILLEGAL_VALUE);
3468         }
3469
3470 }