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