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