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