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