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