* msgbase.c: when a summary mode message list is requested, and the room
[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_RUN_DIR
247                            "."
248 #else
249                            RUN_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                 return;
1070         }
1071 }
1072
1073 /*
1074  * Post callback function for multipart/alternative
1075  */
1076 void fixed_output_post(char *name, char *filename, char *partnum, char *disp,
1077                 void *content, char *cbtype, char *cbcharset, size_t length,
1078                 char *encoding, void *cbuserdata)
1079 {
1080         struct ma_info *ma;
1081         
1082         ma = (struct ma_info *)cbuserdata;
1083         lprintf(CTDL_DEBUG, "fixed_output_post() type=<%s>\n", cbtype); 
1084         if (!strcasecmp(cbtype, "multipart/alternative")) {
1085                 --ma->is_ma;
1086                 ma->did_print = 0;
1087         return;
1088         }
1089 }
1090
1091 /*
1092  * Inline callback function for mime parser that wants to display text
1093  */
1094 void fixed_output(char *name, char *filename, char *partnum, char *disp,
1095                 void *content, char *cbtype, char *cbcharset, size_t length,
1096                 char *encoding, void *cbuserdata)
1097 {
1098         char *ptr;
1099         char *wptr;
1100         size_t wlen;
1101         struct ma_info *ma;
1102
1103         ma = (struct ma_info *)cbuserdata;
1104
1105         lprintf(CTDL_DEBUG,
1106                 "fixed_output() part %s: %s (%s) (%ld bytes)\n",
1107                 partnum, filename, cbtype, (long)length);
1108
1109         /*
1110          * If we're in the middle of a multipart/alternative scope and
1111          * we've already printed another section, skip this one.
1112          */     
1113         if ( (ma->is_ma == 1) && (ma->did_print == 1) ) {
1114                 lprintf(CTDL_DEBUG, "Skipping part %s (%s)\n",
1115                         partnum, cbtype);
1116                 return;
1117         }
1118         ma->did_print = 1;
1119
1120         if ( (!strcasecmp(cbtype, "text/plain")) 
1121            || (strlen(cbtype)==0) ) {
1122                 wptr = content;
1123                 if (length > 0) {
1124                         client_write(wptr, length);
1125                         if (wptr[length-1] != '\n') {
1126                                 cprintf("\n");
1127                         }
1128                 }
1129         }
1130         else if (!strcasecmp(cbtype, "text/html")) {
1131                 ptr = html_to_ascii(content, length, 80, 0);
1132                 wlen = strlen(ptr);
1133                 client_write(ptr, wlen);
1134                 if (ptr[wlen-1] != '\n') {
1135                         cprintf("\n");
1136                 }
1137                 free(ptr);
1138         }
1139         else if (PerformFixedOutputHooks(cbtype, content, length)) {
1140                 /* above function returns nonzero if it handled the part */
1141         }
1142         else if (strncasecmp(cbtype, "multipart/", 10)) {
1143                 cprintf("Part %s: %s (%s) (%ld bytes)\r\n",
1144                         partnum, filename, cbtype, (long)length);
1145         }
1146 }
1147
1148 /*
1149  * The client is elegant and sophisticated and wants to be choosy about
1150  * MIME content types, so figure out which multipart/alternative part
1151  * we're going to send.
1152  */
1153 void choose_preferred(char *name, char *filename, char *partnum, char *disp,
1154                 void *content, char *cbtype, char *cbcharset, size_t length,
1155                 char *encoding, void *cbuserdata)
1156 {
1157         char buf[1024];
1158         int i;
1159         struct ma_info *ma;
1160         
1161         ma = (struct ma_info *)cbuserdata;
1162
1163         if (ma->is_ma > 0) {
1164                 for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
1165                         extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
1166                         if (!strcasecmp(buf, cbtype)) {
1167                                 if (num_tokens(partnum, '.') < 3) {
1168                                         safestrncpy(ma->chosen_part, partnum, sizeof ma->chosen_part);
1169                                 }
1170                         }
1171                 }
1172         }
1173 }
1174
1175 /*
1176  * Now that we've chosen our preferred part, output it.
1177  */
1178 void output_preferred(char *name, char *filename, char *partnum, char *disp,
1179                 void *content, char *cbtype, char *cbcharset, size_t length,
1180                 char *encoding, void *cbuserdata)
1181 {
1182         int i;
1183         char buf[128];
1184         int add_newline = 0;
1185         char *text_content;
1186         struct ma_info *ma;
1187         
1188         ma = (struct ma_info *)cbuserdata;
1189
1190         /* This is not the MIME part you're looking for... */
1191         if (strcasecmp(partnum, ma->chosen_part)) return;
1192
1193         /* If the content-type of this part is in our preferred formats
1194          * list, we can simply output it verbatim.
1195          */
1196         for (i=0; i<num_tokens(CC->preferred_formats, '|'); ++i) {
1197                 extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
1198                 if (!strcasecmp(buf, cbtype)) {
1199                         /* Yeah!  Go!  W00t!! */
1200
1201                         text_content = (char *)content;
1202                         if (text_content[length-1] != '\n') {
1203                                 ++add_newline;
1204                         }
1205
1206                         cprintf("Content-type: %s", cbtype);
1207                         if (strlen(cbcharset) > 0) {
1208                                 cprintf("; charset=%s", cbcharset);
1209                         }
1210                         cprintf("\nContent-length: %d\n",
1211                                 (int)(length + add_newline) );
1212                         if (strlen(encoding) > 0) {
1213                                 cprintf("Content-transfer-encoding: %s\n", encoding);
1214                         }
1215                         else {
1216                                 cprintf("Content-transfer-encoding: 7bit\n");
1217                         }
1218                         cprintf("\n");
1219                         client_write(content, length);
1220                         if (add_newline) cprintf("\n");
1221                         return;
1222                 }
1223         }
1224
1225         /* No translations required or possible: output as text/plain */
1226         cprintf("Content-type: text/plain\n\n");
1227         fixed_output(name, filename, partnum, disp, content, cbtype, cbcharset,
1228                         length, encoding, cbuserdata);
1229 }
1230
1231
1232 struct encapmsg {
1233         char desired_section[64];
1234         char *msg;
1235         size_t msglen;
1236 };
1237
1238
1239 /*
1240  * Callback function for
1241  */
1242 void extract_encapsulated_message(char *name, char *filename, char *partnum, char *disp,
1243                    void *content, char *cbtype, char *cbcharset, size_t length,
1244                    char *encoding, void *cbuserdata)
1245 {
1246         struct encapmsg *encap;
1247
1248         encap = (struct encapmsg *)cbuserdata;
1249
1250         /* Only proceed if this is the desired section... */
1251         if (!strcasecmp(encap->desired_section, partnum)) {
1252                 encap->msglen = length;
1253                 encap->msg = malloc(length + 2);
1254                 memcpy(encap->msg, content, length);
1255                 return;
1256         }
1257
1258 }
1259
1260
1261
1262
1263 /*
1264  * Get a message off disk.  (returns om_* values found in msgbase.h)
1265  * 
1266  */
1267 int CtdlOutputMsg(long msg_num,         /* message number (local) to fetch */
1268                 int mode,               /* how would you like that message? */
1269                 int headers_only,       /* eschew the message body? */
1270                 int do_proto,           /* do Citadel protocol responses? */
1271                 int crlf,               /* Use CRLF newlines instead of LF? */
1272                 char *section           /* NULL or a message/rfc822 section */
1273 ) {
1274         struct CtdlMessage *TheMessage = NULL;
1275         int retcode = om_no_such_msg;
1276         struct encapmsg encap;
1277
1278         lprintf(CTDL_DEBUG, "CtdlOutputMsg() msgnum=%ld, mode=%d, section=%s\n", 
1279                 msg_num, mode,
1280                 (section ? section : "<>")
1281         );
1282
1283         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
1284                 if (do_proto) cprintf("%d Not logged in.\n",
1285                         ERROR + NOT_LOGGED_IN);
1286                 return(om_not_logged_in);
1287         }
1288
1289         /* FIXME: check message id against msglist for this room */
1290
1291         /*
1292          * Fetch the message from disk.  If we're in any sort of headers
1293          * only mode, request that we don't even bother loading the body
1294          * into memory.
1295          */
1296         if ( (headers_only == HEADERS_FAST) || (headers_only == HEADERS_ONLY) ) {
1297                 TheMessage = CtdlFetchMessage(msg_num, 0);
1298         }
1299         else {
1300                 TheMessage = CtdlFetchMessage(msg_num, 1);
1301         }
1302
1303         if (TheMessage == NULL) {
1304                 if (do_proto) cprintf("%d Can't locate msg %ld on disk\n",
1305                         ERROR + MESSAGE_NOT_FOUND, msg_num);
1306                 return(om_no_such_msg);
1307         }
1308
1309         /* Here is the weird form of this command, to process only an
1310          * encapsulated message/rfc822 section.
1311          */
1312         if (section) if (strlen(section)>0) if (strcmp(section, "0")) {
1313                 memset(&encap, 0, sizeof encap);
1314                 safestrncpy(encap.desired_section, section, sizeof encap.desired_section);
1315                 mime_parser(TheMessage->cm_fields['M'],
1316                         NULL,
1317                         *extract_encapsulated_message,
1318                         NULL, NULL, (void *)&encap, 0
1319                 );
1320                 CtdlFreeMessage(TheMessage);
1321                 TheMessage = NULL;
1322
1323                 if (encap.msg) {
1324                         encap.msg[encap.msglen] = 0;
1325                         TheMessage = convert_internet_message(encap.msg);
1326                         encap.msg = NULL;       /* no free() here, TheMessage owns it now */
1327
1328                         /* Now we let it fall through to the bottom of this
1329                          * function, because TheMessage now contains the
1330                          * encapsulated message instead of the top-level
1331                          * message.  Isn't that neat?
1332                          */
1333
1334                 }
1335                 else {
1336                         if (do_proto) cprintf("%d msg %ld has no part %s\n",
1337                                 ERROR + MESSAGE_NOT_FOUND, msg_num, section);
1338                         retcode = om_no_such_msg;
1339                 }
1340
1341         }
1342
1343         /* Ok, output the message now */
1344         retcode = CtdlOutputPreLoadedMsg(
1345                 TheMessage, mode,
1346                 headers_only, do_proto, crlf);
1347         CtdlFreeMessage(TheMessage);
1348
1349         return(retcode);
1350 }
1351
1352
1353 /*
1354  * Get a message off disk.  (returns om_* values found in msgbase.h)
1355  * 
1356  */
1357 int CtdlOutputPreLoadedMsg(
1358                 struct CtdlMessage *TheMessage,
1359                 int mode,               /* how would you like that message? */
1360                 int headers_only,       /* eschew the message body? */
1361                 int do_proto,           /* do Citadel protocol responses? */
1362                 int crlf                /* Use CRLF newlines instead of LF? */
1363 ) {
1364         int i, k;
1365         char buf[SIZ];
1366         cit_uint8_t ch;
1367         char allkeys[30];
1368         char display_name[256];
1369         char *mptr;
1370         char *nl;       /* newline string */
1371         int suppress_f = 0;
1372         int subject_found = 0;
1373         struct ma_info ma;
1374
1375         /* Buffers needed for RFC822 translation.  These are all filled
1376          * using functions that are bounds-checked, and therefore we can
1377          * make them substantially smaller than SIZ.
1378          */
1379         char suser[100];
1380         char luser[100];
1381         char fuser[100];
1382         char snode[100];
1383         char lnode[100];
1384         char mid[100];
1385         char datestamp[100];
1386
1387         lprintf(CTDL_DEBUG, "CtdlOutputPreLoadedMsg(TheMessage=%s, %d, %d, %d, %d\n",
1388                 ((TheMessage == NULL) ? "NULL" : "not null"),
1389                 mode, headers_only, do_proto, crlf);
1390
1391         strcpy(mid, "unknown");
1392         nl = (crlf ? "\r\n" : "\n");
1393
1394         if (!is_valid_message(TheMessage)) {
1395                 lprintf(CTDL_ERR,
1396                         "ERROR: invalid preloaded message for output\n");
1397                 return(om_no_such_msg);
1398         }
1399
1400         /* Are we downloading a MIME component? */
1401         if (mode == MT_DOWNLOAD) {
1402                 if (TheMessage->cm_format_type != FMT_RFC822) {
1403                         if (do_proto)
1404                                 cprintf("%d This is not a MIME message.\n",
1405                                 ERROR + ILLEGAL_VALUE);
1406                 } else if (CC->download_fp != NULL) {
1407                         if (do_proto) cprintf(
1408                                 "%d You already have a download open.\n",
1409                                 ERROR + RESOURCE_BUSY);
1410                 } else {
1411                         /* Parse the message text component */
1412                         mptr = TheMessage->cm_fields['M'];
1413                         mime_parser(mptr, NULL, *mime_download, NULL, NULL, NULL, 0);
1414                         /* If there's no file open by this time, the requested
1415                          * section wasn't found, so print an error
1416                          */
1417                         if (CC->download_fp == NULL) {
1418                                 if (do_proto) cprintf(
1419                                         "%d Section %s not found.\n",
1420                                         ERROR + FILE_NOT_FOUND,
1421                                         CC->download_desired_section);
1422                         }
1423                 }
1424                 return((CC->download_fp != NULL) ? om_ok : om_mime_error);
1425         }
1426
1427         /* now for the user-mode message reading loops */
1428         if (do_proto) cprintf("%d msg:\n", LISTING_FOLLOWS);
1429
1430         /* Does the caller want to skip the headers? */
1431         if (headers_only == HEADERS_NONE) goto START_TEXT;
1432
1433         /* Tell the client which format type we're using. */
1434         if ( (mode == MT_CITADEL) && (do_proto) ) {
1435                 cprintf("type=%d\n", TheMessage->cm_format_type);
1436         }
1437
1438         /* nhdr=yes means that we're only displaying headers, no body */
1439         if ( (TheMessage->cm_anon_type == MES_ANONONLY)
1440            && (mode == MT_CITADEL)
1441            && (do_proto)
1442            ) {
1443                 cprintf("nhdr=yes\n");
1444         }
1445
1446         /* begin header processing loop for Citadel message format */
1447
1448         if ((mode == MT_CITADEL) || (mode == MT_MIME)) {
1449
1450                 safestrncpy(display_name, "<unknown>", sizeof display_name);
1451                 if (TheMessage->cm_fields['A']) {
1452                         strcpy(buf, TheMessage->cm_fields['A']);
1453                         if (TheMessage->cm_anon_type == MES_ANONONLY) {
1454                                 safestrncpy(display_name, "****", sizeof display_name);
1455                         }
1456                         else if (TheMessage->cm_anon_type == MES_ANONOPT) {
1457                                 safestrncpy(display_name, "anonymous", sizeof display_name);
1458                         }
1459                         else {
1460                                 safestrncpy(display_name, buf, sizeof display_name);
1461                         }
1462                         if ((is_room_aide())
1463                             && ((TheMessage->cm_anon_type == MES_ANONONLY)
1464                              || (TheMessage->cm_anon_type == MES_ANONOPT))) {
1465                                 size_t tmp = strlen(display_name);
1466                                 snprintf(&display_name[tmp],
1467                                          sizeof display_name - tmp,
1468                                          " [%s]", buf);
1469                         }
1470                 }
1471
1472                 /* Don't show Internet address for users on the
1473                  * local Citadel network.
1474                  */
1475                 suppress_f = 0;
1476                 if (TheMessage->cm_fields['N'] != NULL)
1477                    if (strlen(TheMessage->cm_fields['N']) > 0)
1478                       if (haschar(TheMessage->cm_fields['N'], '.') == 0) {
1479                         suppress_f = 1;
1480                 }
1481                 
1482                 /* Now spew the header fields in the order we like them. */
1483                 safestrncpy(allkeys, FORDER, sizeof allkeys);
1484                 for (i=0; i<strlen(allkeys); ++i) {
1485                         k = (int) allkeys[i];
1486                         if (k != 'M') {
1487                                 if ( (TheMessage->cm_fields[k] != NULL)
1488                                    && (msgkeys[k] != NULL) ) {
1489                                         if (k == 'A') {
1490                                                 if (do_proto) cprintf("%s=%s\n",
1491                                                         msgkeys[k],
1492                                                         display_name);
1493                                         }
1494                                         else if ((k == 'F') && (suppress_f)) {
1495                                                 /* do nothing */
1496                                         }
1497                                         /* Masquerade display name if needed */
1498                                         else {
1499                                                 if (do_proto) cprintf("%s=%s\n",
1500                                                         msgkeys[k],
1501                                                         TheMessage->cm_fields[k]
1502                                         );
1503                                         }
1504                                 }
1505                         }
1506                 }
1507
1508         }
1509
1510         /* begin header processing loop for RFC822 transfer format */
1511
1512         strcpy(suser, "");
1513         strcpy(luser, "");
1514         strcpy(fuser, "");
1515         strcpy(snode, NODENAME);
1516         strcpy(lnode, HUMANNODE);
1517         if (mode == MT_RFC822) {
1518                 for (i = 0; i < 256; ++i) {
1519                         if (TheMessage->cm_fields[i]) {
1520                                 mptr = TheMessage->cm_fields[i];
1521
1522                                 if (i == 'A') {
1523                                         safestrncpy(luser, mptr, sizeof luser);
1524                                         safestrncpy(suser, mptr, sizeof suser);
1525                                 }
1526                                 else if (i == 'Y') {
1527                                         cprintf("CC: %s%s", mptr, nl);
1528                                 }
1529                                 else if (i == 'U') {
1530                                         cprintf("Subject: %s%s", mptr, nl);
1531                                         subject_found = 1;
1532                                 }
1533                                 else if (i == 'I')
1534                                         safestrncpy(mid, mptr, sizeof mid);
1535                                 else if (i == 'H')
1536                                         safestrncpy(lnode, mptr, sizeof lnode);
1537                                 else if (i == 'F')
1538                                         safestrncpy(fuser, mptr, sizeof fuser);
1539                                 /* else if (i == 'O')
1540                                         cprintf("X-Citadel-Room: %s%s",
1541                                                 mptr, nl); */
1542                                 else if (i == 'N')
1543                                         safestrncpy(snode, mptr, sizeof snode);
1544                                 else if (i == 'R')
1545                                         cprintf("To: %s%s", mptr, nl);
1546                                 else if (i == 'T') {
1547                                         datestring(datestamp, sizeof datestamp,
1548                                                 atol(mptr), DATESTRING_RFC822);
1549                                         cprintf("Date: %s%s", datestamp, nl);
1550                                 }
1551                         }
1552                 }
1553                 if (subject_found == 0) {
1554                         cprintf("Subject: (no subject)%s", nl);
1555                 }
1556         }
1557
1558         for (i=0; i<strlen(suser); ++i) {
1559                 suser[i] = tolower(suser[i]);
1560                 if (!isalnum(suser[i])) suser[i]='_';
1561         }
1562
1563         if (mode == MT_RFC822) {
1564                 if (!strcasecmp(snode, NODENAME)) {
1565                         safestrncpy(snode, FQDN, sizeof snode);
1566                 }
1567
1568                 /* Construct a fun message id */
1569                 cprintf("Message-ID: <%s", mid);
1570                 if (strchr(mid, '@')==NULL) {
1571                         cprintf("@%s", snode);
1572                 }
1573                 cprintf(">%s", nl);
1574
1575                 if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONONLY)) {
1576                         cprintf("From: \"----\" <x@x.org>%s", nl);
1577                 }
1578                 else if (!is_room_aide() && (TheMessage->cm_anon_type == MES_ANONOPT)) {
1579                         cprintf("From: \"anonymous\" <x@x.org>%s", nl);
1580                 }
1581                 else if (strlen(fuser) > 0) {
1582                         cprintf("From: \"%s\" <%s>%s", luser, fuser, nl);
1583                 }
1584                 else {
1585                         cprintf("From: \"%s\" <%s@%s>%s", luser, suser, snode, nl);
1586                 }
1587
1588                 cprintf("Organization: %s%s", lnode, nl);
1589
1590                 /* Blank line signifying RFC822 end-of-headers */
1591                 if (TheMessage->cm_format_type != FMT_RFC822) {
1592                         cprintf("%s", nl);
1593                 }
1594         }
1595
1596         /* end header processing loop ... at this point, we're in the text */
1597 START_TEXT:
1598         if (headers_only == HEADERS_FAST) goto DONE;
1599         mptr = TheMessage->cm_fields['M'];
1600
1601         /* Tell the client about the MIME parts in this message */
1602         if (TheMessage->cm_format_type == FMT_RFC822) {
1603                 if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
1604                         memset(&ma, 0, sizeof(struct ma_info));
1605                         mime_parser(mptr, NULL,
1606                                 (do_proto ? *list_this_part : NULL),
1607                                 (do_proto ? *list_this_pref : NULL),
1608                                 (do_proto ? *list_this_suff : NULL),
1609                                 (void *)&ma, 0);
1610                 }
1611                 else if (mode == MT_RFC822) {   /* unparsed RFC822 dump */
1612                         char *start_of_text = NULL;
1613                         start_of_text = strstr(mptr, "\n\r\n");
1614                         if (start_of_text == NULL) start_of_text = strstr(mptr, "\n\n");
1615                         if (start_of_text == NULL) start_of_text = mptr;
1616                         ++start_of_text;
1617                         start_of_text = strstr(start_of_text, "\n");
1618                         ++start_of_text;
1619                         while (ch=*mptr, ch!=0) {
1620                                 if (ch==13) {
1621                                         /* do nothing */
1622                                 }
1623                                 else switch(headers_only) {
1624                                         case HEADERS_NONE:
1625                                                 if (mptr >= start_of_text) {
1626                                                         if (ch == 10) cprintf("%s", nl);
1627                                                         else cprintf("%c", ch);
1628                                                 }
1629                                                 break;
1630                                         case HEADERS_ONLY:
1631                                                 if (mptr < start_of_text) {
1632                                                         if (ch == 10) cprintf("%s", nl);
1633                                                         else cprintf("%c", ch);
1634                                                 }
1635                                                 break;
1636                                         default:
1637                                                 if (ch == 10) cprintf("%s", nl);
1638                                                 else cprintf("%c", ch);
1639                                                 break;
1640                                 }
1641                                 ++mptr;
1642                         }
1643                         goto DONE;
1644                 }
1645         }
1646
1647         if (headers_only == HEADERS_ONLY) {
1648                 goto DONE;
1649         }
1650
1651         /* signify start of msg text */
1652         if ( (mode == MT_CITADEL) || (mode == MT_MIME) ) {
1653                 if (do_proto) cprintf("text\n");
1654         }
1655
1656         /* If the format type on disk is 1 (fixed-format), then we want
1657          * everything to be output completely literally ... regardless of
1658          * what message transfer format is in use.
1659          */
1660         if (TheMessage->cm_format_type == FMT_FIXED) {
1661                 if (mode == MT_MIME) {
1662                         cprintf("Content-type: text/plain\n\n");
1663                 }
1664                 strcpy(buf, "");
1665                 while (ch = *mptr++, ch > 0) {
1666                         if (ch == 13)
1667                                 ch = 10;
1668                         if ((ch == 10) || (strlen(buf) > 250)) {
1669                                 cprintf("%s%s", buf, nl);
1670                                 strcpy(buf, "");
1671                         } else {
1672                                 buf[strlen(buf) + 1] = 0;
1673                                 buf[strlen(buf)] = ch;
1674                         }
1675                 }
1676                 if (strlen(buf) > 0)
1677                         cprintf("%s%s", buf, nl);
1678         }
1679
1680         /* If the message on disk is format 0 (Citadel vari-format), we
1681          * output using the formatter at 80 columns.  This is the final output
1682          * form if the transfer format is RFC822, but if the transfer format
1683          * is Citadel proprietary, it'll still work, because the indentation
1684          * for new paragraphs is correct and the client will reformat the
1685          * message to the reader's screen width.
1686          */
1687         if (TheMessage->cm_format_type == FMT_CITADEL) {
1688                 if (mode == MT_MIME) {
1689                         cprintf("Content-type: text/x-citadel-variformat\n\n");
1690                 }
1691                 memfmout(80, mptr, 0, nl);
1692         }
1693
1694         /* If the message on disk is format 4 (MIME), we've gotta hand it
1695          * off to the MIME parser.  The client has already been told that
1696          * this message is format 1 (fixed format), so the callback function
1697          * we use will display those parts as-is.
1698          */
1699         if (TheMessage->cm_format_type == FMT_RFC822) {
1700                 memset(&ma, 0, sizeof(struct ma_info));
1701
1702                 if (mode == MT_MIME) {
1703                         strcpy(ma.chosen_part, "1");
1704                         mime_parser(mptr, NULL,
1705                                 *choose_preferred, *fixed_output_pre,
1706                                 *fixed_output_post, (void *)&ma, 0);
1707                         mime_parser(mptr, NULL,
1708                                 *output_preferred, NULL, NULL, (void *)&ma, 0);
1709                 }
1710                 else {
1711                         mime_parser(mptr, NULL,
1712                                 *fixed_output, *fixed_output_pre,
1713                                 *fixed_output_post, (void *)&ma, 0);
1714                 }
1715
1716         }
1717
1718 DONE:   /* now we're done */
1719         if (do_proto) cprintf("000\n");
1720         return(om_ok);
1721 }
1722
1723
1724
1725 /*
1726  * display a message (mode 0 - Citadel proprietary)
1727  */
1728 void cmd_msg0(char *cmdbuf)
1729 {
1730         long msgid;
1731         int headers_only = HEADERS_ALL;
1732
1733         msgid = extract_long(cmdbuf, 0);
1734         headers_only = extract_int(cmdbuf, 1);
1735
1736         CtdlOutputMsg(msgid, MT_CITADEL, headers_only, 1, 0, NULL);
1737         return;
1738 }
1739
1740
1741 /*
1742  * display a message (mode 2 - RFC822)
1743  */
1744 void cmd_msg2(char *cmdbuf)
1745 {
1746         long msgid;
1747         int headers_only = HEADERS_ALL;
1748
1749         msgid = extract_long(cmdbuf, 0);
1750         headers_only = extract_int(cmdbuf, 1);
1751
1752         CtdlOutputMsg(msgid, MT_RFC822, headers_only, 1, 1, NULL);
1753 }
1754
1755
1756
1757 /* 
1758  * display a message (mode 3 - IGnet raw format - internal programs only)
1759  */
1760 void cmd_msg3(char *cmdbuf)
1761 {
1762         long msgnum;
1763         struct CtdlMessage *msg;
1764         struct ser_ret smr;
1765
1766         if (CC->internal_pgm == 0) {
1767                 cprintf("%d This command is for internal programs only.\n",
1768                         ERROR + HIGHER_ACCESS_REQUIRED);
1769                 return;
1770         }
1771
1772         msgnum = extract_long(cmdbuf, 0);
1773         msg = CtdlFetchMessage(msgnum, 1);
1774         if (msg == NULL) {
1775                 cprintf("%d Message %ld not found.\n", 
1776                         ERROR + MESSAGE_NOT_FOUND, msgnum);
1777                 return;
1778         }
1779
1780         serialize_message(&smr, msg);
1781         CtdlFreeMessage(msg);
1782
1783         if (smr.len == 0) {
1784                 cprintf("%d Unable to serialize message\n",
1785                         ERROR + INTERNAL_ERROR);
1786                 return;
1787         }
1788
1789         cprintf("%d %ld\n", BINARY_FOLLOWS, (long)smr.len);
1790         client_write((char *)smr.ser, (int)smr.len);
1791         free(smr.ser);
1792 }
1793
1794
1795
1796 /* 
1797  * Display a message using MIME content types
1798  */
1799 void cmd_msg4(char *cmdbuf)
1800 {
1801         long msgid;
1802         char section[64];
1803
1804         msgid = extract_long(cmdbuf, 0);
1805         extract_token(section, cmdbuf, 1, '|', sizeof section);
1806         CtdlOutputMsg(msgid, MT_MIME, 0, 1, 0, (section[0] ? section : NULL) );
1807 }
1808
1809
1810
1811 /* 
1812  * Client tells us its preferred message format(s)
1813  */
1814 void cmd_msgp(char *cmdbuf)
1815 {
1816         safestrncpy(CC->preferred_formats, cmdbuf,
1817                         sizeof(CC->preferred_formats));
1818         cprintf("%d ok\n", CIT_OK);
1819 }
1820
1821
1822 /*
1823  * Open a component of a MIME message as a download file 
1824  */
1825 void cmd_opna(char *cmdbuf)
1826 {
1827         long msgid;
1828         char desired_section[128];
1829
1830         msgid = extract_long(cmdbuf, 0);
1831         extract_token(desired_section, cmdbuf, 1, '|', sizeof desired_section);
1832         safestrncpy(CC->download_desired_section, desired_section,
1833                 sizeof CC->download_desired_section);
1834         CtdlOutputMsg(msgid, MT_DOWNLOAD, 0, 1, 1, NULL);
1835 }                       
1836
1837
1838 /*
1839  * Save a message pointer into a specified room
1840  * (Returns 0 for success, nonzero for failure)
1841  * roomname may be NULL to use the current room
1842  *
1843  * Note that the 'supplied_msg' field may be set to NULL, in which case
1844  * the message will be fetched from disk, by number, if we need to perform
1845  * replication checks.  This adds an additional database read, so if the
1846  * caller already has the message in memory then it should be supplied.
1847  */
1848 int CtdlSaveMsgPointerInRoom(char *roomname, long msgid, int do_repl_check,
1849                                 struct CtdlMessage *supplied_msg) {
1850         int i;
1851         char hold_rm[ROOMNAMELEN];
1852         struct cdbdata *cdbfr;
1853         int num_msgs;
1854         long *msglist;
1855         long highest_msg = 0L;
1856         struct CtdlMessage *msg = NULL;
1857
1858         /*lprintf(CTDL_DEBUG,
1859                 "CtdlSaveMsgPointerInRoom(room=%s, msgid=%ld, repl=%d)\n",
1860                 roomname, msgid, do_repl_check);*/
1861
1862         strcpy(hold_rm, CC->room.QRname);
1863
1864         /* Now the regular stuff */
1865         if (lgetroom(&CC->room,
1866            ((roomname != NULL) ? roomname : CC->room.QRname) )
1867            != 0) {
1868                 lprintf(CTDL_ERR, "No such room <%s>\n", roomname);
1869                 return(ERROR + ROOM_NOT_FOUND);
1870         }
1871
1872         cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
1873         if (cdbfr == NULL) {
1874                 msglist = NULL;
1875                 num_msgs = 0;
1876         } else {
1877                 msglist = (long *) cdbfr->ptr;
1878                 cdbfr->ptr = NULL;      /* CtdlSaveMsgPointerInRoom() now owns this memory */
1879                 num_msgs = cdbfr->len / sizeof(long);
1880                 cdb_free(cdbfr);
1881         }
1882
1883         /* Make sure the message doesn't already exist in this room.  It
1884          * is absolutely taboo to have more than one reference to the same
1885          * message in a room.
1886          */
1887         if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
1888                 if (msglist[i] == msgid) {
1889                         lputroom(&CC->room);    /* unlock the room */
1890                         getroom(&CC->room, hold_rm);
1891                         free(msglist);
1892                         return(ERROR + ALREADY_EXISTS);
1893                 }
1894         }
1895
1896         /* Now add the new message */
1897         ++num_msgs;
1898         msglist = realloc(msglist, (num_msgs * sizeof(long)));
1899
1900         if (msglist == NULL) {
1901                 lprintf(CTDL_ALERT, "ERROR: can't realloc message list!\n");
1902         }
1903         msglist[num_msgs - 1] = msgid;
1904
1905         /* Sort the message list, so all the msgid's are in order */
1906         num_msgs = sort_msglist(msglist, num_msgs);
1907
1908         /* Determine the highest message number */
1909         highest_msg = msglist[num_msgs - 1];
1910
1911         /* Write it back to disk. */
1912         cdb_store(CDB_MSGLISTS, &CC->room.QRnumber, (int)sizeof(long),
1913                   msglist, (int)(num_msgs * sizeof(long)));
1914
1915         /* Free up the memory we used. */
1916         free(msglist);
1917
1918         /* Update the highest-message pointer and unlock the room. */
1919         CC->room.QRhighest = highest_msg;
1920         lputroom(&CC->room);
1921
1922         /* Perform replication checks if necessary */
1923         if ( (DoesThisRoomNeedEuidIndexing(&CC->room)) && (do_repl_check) ) {
1924                 if (supplied_msg != NULL) {
1925                         msg = supplied_msg;
1926                 }
1927                 else {
1928                         msg = CtdlFetchMessage(msgid, 0);
1929                 }
1930
1931                 if (msg != NULL) {
1932                         ReplicationChecks(msg);
1933                 }
1934
1935         }
1936
1937         /* If the message has an Exclusive ID, index that... */
1938         if (msg != NULL) {
1939                 if (msg->cm_fields['E'] != NULL) {
1940                         index_message_by_euid(msg->cm_fields['E'],
1941                                                 &CC->room, msgid);
1942                 }
1943         }
1944
1945         /* Free up the memory we may have allocated */
1946         if ( (msg != NULL) && (msg != supplied_msg) ) {
1947                 CtdlFreeMessage(msg);
1948         }
1949
1950         /* Go back to the room we were in before we wandered here... */
1951         getroom(&CC->room, hold_rm);
1952
1953         /* Bump the reference count for this message. */
1954         AdjRefCount(msgid, +1);
1955
1956         /* Return success. */
1957         return (0);
1958 }
1959
1960
1961
1962 /*
1963  * Message base operation to save a new message to the message store
1964  * (returns new message number)
1965  *
1966  * This is the back end for CtdlSubmitMsg() and should not be directly
1967  * called by server-side modules.
1968  *
1969  */
1970 long send_message(struct CtdlMessage *msg) {
1971         long newmsgid;
1972         long retval;
1973         char msgidbuf[256];
1974         struct ser_ret smr;
1975         int is_bigmsg = 0;
1976         char *holdM = NULL;
1977
1978         /* Get a new message number */
1979         newmsgid = get_new_message_number();
1980         snprintf(msgidbuf, sizeof msgidbuf, "%ld@%s", newmsgid, config.c_fqdn);
1981
1982         /* Generate an ID if we don't have one already */
1983         if (msg->cm_fields['I']==NULL) {
1984                 msg->cm_fields['I'] = strdup(msgidbuf);
1985         }
1986
1987         /* If the message is big, set its body aside for storage elsewhere */
1988         if (msg->cm_fields['M'] != NULL) {
1989                 if (strlen(msg->cm_fields['M']) > BIGMSG) {
1990                         is_bigmsg = 1;
1991                         holdM = msg->cm_fields['M'];
1992                         msg->cm_fields['M'] = NULL;
1993                 }
1994         }
1995
1996         /* Serialize our data structure for storage in the database */  
1997         serialize_message(&smr, msg);
1998
1999         if (is_bigmsg) {
2000                 msg->cm_fields['M'] = holdM;
2001         }
2002
2003         if (smr.len == 0) {
2004                 cprintf("%d Unable to serialize message\n",
2005                         ERROR + INTERNAL_ERROR);
2006                 return (-1L);
2007         }
2008
2009         /* Write our little bundle of joy into the message base */
2010         if (cdb_store(CDB_MSGMAIN, &newmsgid, (int)sizeof(long),
2011                       smr.ser, smr.len) < 0) {
2012                 lprintf(CTDL_ERR, "Can't store message\n");
2013                 retval = 0L;
2014         } else {
2015                 if (is_bigmsg) {
2016                         cdb_store(CDB_BIGMSGS,
2017                                 &newmsgid,
2018                                 (int)sizeof(long),
2019                                 holdM,
2020                                 (strlen(holdM) + 1)
2021                         );
2022                 }
2023                 retval = newmsgid;
2024         }
2025
2026         /* Free the memory we used for the serialized message */
2027         free(smr.ser);
2028
2029         /* Return the *local* message ID to the caller
2030          * (even if we're storing an incoming network message)
2031          */
2032         return(retval);
2033 }
2034
2035
2036
2037 /*
2038  * Serialize a struct CtdlMessage into the format used on disk and network.
2039  * 
2040  * This function loads up a "struct ser_ret" (defined in server.h) which
2041  * contains the length of the serialized message and a pointer to the
2042  * serialized message in memory.  THE LATTER MUST BE FREED BY THE CALLER.
2043  */
2044 void serialize_message(struct ser_ret *ret,             /* return values */
2045                         struct CtdlMessage *msg)        /* unserialized msg */
2046 {
2047         size_t wlen;
2048         int i;
2049         static char *forder = FORDER;
2050
2051         if (is_valid_message(msg) == 0) return;         /* self check */
2052
2053         ret->len = 3;
2054         for (i=0; i<26; ++i) if (msg->cm_fields[(int)forder[i]] != NULL)
2055                 ret->len = ret->len +
2056                         strlen(msg->cm_fields[(int)forder[i]]) + 2;
2057
2058         lprintf(CTDL_DEBUG, "serialize_message() calling malloc(%ld)\n", (long)ret->len);
2059         ret->ser = malloc(ret->len);
2060         if (ret->ser == NULL) {
2061                 ret->len = 0;
2062                 return;
2063         }
2064
2065         ret->ser[0] = 0xFF;
2066         ret->ser[1] = msg->cm_anon_type;
2067         ret->ser[2] = msg->cm_format_type;
2068         wlen = 3;
2069
2070         for (i=0; i<26; ++i) if (msg->cm_fields[(int)forder[i]] != NULL) {
2071                 ret->ser[wlen++] = (char)forder[i];
2072                 strcpy((char *)&ret->ser[wlen], msg->cm_fields[(int)forder[i]]);
2073                 wlen = wlen + strlen(msg->cm_fields[(int)forder[i]]) + 1;
2074         }
2075         if (ret->len != wlen) lprintf(CTDL_ERR, "ERROR: len=%ld wlen=%ld\n",
2076                 (long)ret->len, (long)wlen);
2077
2078         return;
2079 }
2080
2081
2082
2083 /*
2084  * Check to see if any messages already exist in the current room which
2085  * carry the same Exclusive ID as this one.  If any are found, delete them.
2086  */
2087 void ReplicationChecks(struct CtdlMessage *msg) {
2088         long old_msgnum = (-1L);
2089
2090         if (DoesThisRoomNeedEuidIndexing(&CC->room) == 0) return;
2091
2092         lprintf(CTDL_DEBUG, "Performing replication checks in <%s>\n",
2093                 CC->room.QRname);
2094
2095         /* No exclusive id?  Don't do anything. */
2096         if (msg == NULL) return;
2097         if (msg->cm_fields['E'] == NULL) return;
2098         if (strlen(msg->cm_fields['E']) == 0) return;
2099         /*lprintf(CTDL_DEBUG, "Exclusive ID: <%s> for room <%s>\n",
2100                 msg->cm_fields['E'], CC->room.QRname);*/
2101
2102         old_msgnum = locate_message_by_euid(msg->cm_fields['E'], &CC->room);
2103         if (old_msgnum > 0L) {
2104                 lprintf(CTDL_DEBUG, "ReplicationChecks() replacing message %ld\n", old_msgnum);
2105                 CtdlDeleteMessages(CC->room.QRname, old_msgnum, "", 0);
2106         }
2107 }
2108
2109
2110
2111 /*
2112  * Save a message to disk and submit it into the delivery system.
2113  */
2114 long CtdlSubmitMsg(struct CtdlMessage *msg,     /* message to save */
2115                 struct recptypes *recps,        /* recipients (if mail) */
2116                 char *force                     /* force a particular room? */
2117 ) {
2118         char submit_filename[128];
2119         char generated_timestamp[32];
2120         char hold_rm[ROOMNAMELEN];
2121         char actual_rm[ROOMNAMELEN];
2122         char force_room[ROOMNAMELEN];
2123         char content_type[SIZ];                 /* We have to learn this */
2124         char recipient[SIZ];
2125         long newmsgid;
2126         char *mptr = NULL;
2127         struct ctdluser userbuf;
2128         int a, i;
2129         struct MetaData smi;
2130         FILE *network_fp = NULL;
2131         static int seqnum = 1;
2132         struct CtdlMessage *imsg = NULL;
2133         char *instr;
2134         struct ser_ret smr;
2135         char *hold_R, *hold_D;
2136         char *collected_addresses = NULL;
2137         struct addresses_to_be_filed *aptr = NULL;
2138
2139         lprintf(CTDL_DEBUG, "CtdlSubmitMsg() called\n");
2140         if (is_valid_message(msg) == 0) return(-1);     /* self check */
2141
2142         /* If this message has no timestamp, we take the liberty of
2143          * giving it one, right now.
2144          */
2145         if (msg->cm_fields['T'] == NULL) {
2146                 snprintf(generated_timestamp, sizeof generated_timestamp, "%ld", (long)time(NULL));
2147                 msg->cm_fields['T'] = strdup(generated_timestamp);
2148         }
2149
2150         /* If this message has no path, we generate one.
2151          */
2152         if (msg->cm_fields['P'] == NULL) {
2153                 if (msg->cm_fields['A'] != NULL) {
2154                         msg->cm_fields['P'] = strdup(msg->cm_fields['A']);
2155                         for (a=0; a<strlen(msg->cm_fields['P']); ++a) {
2156                                 if (isspace(msg->cm_fields['P'][a])) {
2157                                         msg->cm_fields['P'][a] = ' ';
2158                                 }
2159                         }
2160                 }
2161                 else {
2162                         msg->cm_fields['P'] = strdup("unknown");
2163                 }
2164         }
2165
2166         if (force == NULL) {
2167                 strcpy(force_room, "");
2168         }
2169         else {
2170                 strcpy(force_room, force);
2171         }
2172
2173         /* Learn about what's inside, because it's what's inside that counts */
2174         if (msg->cm_fields['M'] == NULL) {
2175                 lprintf(CTDL_ERR, "ERROR: attempt to save message with NULL body\n");
2176                 return(-2);
2177         }
2178
2179         switch (msg->cm_format_type) {
2180         case 0:
2181                 strcpy(content_type, "text/x-citadel-variformat");
2182                 break;
2183         case 1:
2184                 strcpy(content_type, "text/plain");
2185                 break;
2186         case 4:
2187                 strcpy(content_type, "text/plain");
2188                 mptr = bmstrcasestr(msg->cm_fields['M'], "Content-type: ");
2189                 if (mptr != NULL) {
2190                         safestrncpy(content_type, &mptr[14], 
2191                                         sizeof content_type);
2192                         for (a = 0; a < strlen(content_type); ++a) {
2193                                 if ((content_type[a] == ';')
2194                                     || (content_type[a] == ' ')
2195                                     || (content_type[a] == 13)
2196                                     || (content_type[a] == 10)) {
2197                                         content_type[a] = 0;
2198                                 }
2199                         }
2200                 }
2201         }
2202
2203         /* Goto the correct room */
2204         lprintf(CTDL_DEBUG, "Selected room %s\n", (recps) ? CC->room.QRname : SENTITEMS);
2205         strcpy(hold_rm, CC->room.QRname);
2206         strcpy(actual_rm, CC->room.QRname);
2207         if (recps != NULL) {
2208                 strcpy(actual_rm, SENTITEMS);
2209         }
2210
2211         /* If the user is a twit, move to the twit room for posting */
2212         if (TWITDETECT) {
2213                 if (CC->user.axlevel == 2) {
2214                         strcpy(hold_rm, actual_rm);
2215                         strcpy(actual_rm, config.c_twitroom);
2216                         lprintf(CTDL_DEBUG, "Diverting to twit room\n");
2217                 }
2218         }
2219
2220         /* ...or if this message is destined for Aide> then go there. */
2221         if (strlen(force_room) > 0) {
2222                 strcpy(actual_rm, force_room);
2223         }
2224
2225         lprintf(CTDL_DEBUG, "Final selection: %s\n", actual_rm);
2226         if (strcasecmp(actual_rm, CC->room.QRname)) {
2227                 /* getroom(&CC->room, actual_rm); */
2228                 usergoto(actual_rm, 0, 1, NULL, NULL);
2229         }
2230
2231         /*
2232          * If this message has no O (room) field, generate one.
2233          */
2234         if (msg->cm_fields['O'] == NULL) {
2235                 msg->cm_fields['O'] = strdup(CC->room.QRname);
2236         }
2237
2238         /* Perform "before save" hooks (aborting if any return nonzero) */
2239         lprintf(CTDL_DEBUG, "Performing before-save hooks\n");
2240         if (PerformMessageHooks(msg, EVT_BEFORESAVE) > 0) return(-3);
2241
2242         /*
2243          * If this message has an Exclusive ID, and the room is replication
2244          * checking enabled, then do replication checks.
2245          */
2246         if (DoesThisRoomNeedEuidIndexing(&CC->room)) {
2247                 ReplicationChecks(msg);
2248         }
2249
2250         /* Save it to disk */
2251         lprintf(CTDL_DEBUG, "Saving to disk\n");
2252         newmsgid = send_message(msg);
2253         if (newmsgid <= 0L) return(-5);
2254
2255         /* Write a supplemental message info record.  This doesn't have to
2256          * be a critical section because nobody else knows about this message
2257          * yet.
2258          */
2259         lprintf(CTDL_DEBUG, "Creating MetaData record\n");
2260         memset(&smi, 0, sizeof(struct MetaData));
2261         smi.meta_msgnum = newmsgid;
2262         smi.meta_refcount = 0;
2263         safestrncpy(smi.meta_content_type, content_type,
2264                         sizeof smi.meta_content_type);
2265
2266         /* As part of the new metadata record, measure how
2267          * big this message will be when displayed as RFC822.
2268          * Both POP and IMAP use this, and it's best to just take the hit now
2269          * instead of having to potentially measure thousands of messages when
2270          * a mailbox is opened later.
2271          */
2272
2273         if (CC->redirect_buffer != NULL) {
2274                 lprintf(CTDL_ALERT, "CC->redirect_buffer is not NULL during message submission!\n");
2275                 abort();
2276         }
2277         CC->redirect_buffer = malloc(SIZ);
2278         CC->redirect_len = 0;
2279         CC->redirect_alloc = SIZ;
2280         CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1);
2281         smi.meta_rfc822_length = CC->redirect_len;
2282         free(CC->redirect_buffer);
2283         CC->redirect_buffer = NULL;
2284         CC->redirect_len = 0;
2285         CC->redirect_alloc = 0;
2286
2287         PutMetaData(&smi);
2288
2289         /* Now figure out where to store the pointers */
2290         lprintf(CTDL_DEBUG, "Storing pointers\n");
2291
2292         /* If this is being done by the networker delivering a private
2293          * message, we want to BYPASS saving the sender's copy (because there
2294          * is no local sender; it would otherwise go to the Trashcan).
2295          */
2296         if ((!CC->internal_pgm) || (recps == NULL)) {
2297                 if (CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 1, msg) != 0) {
2298                         lprintf(CTDL_ERR, "ERROR saving message pointer!\n");
2299                         CtdlSaveMsgPointerInRoom(config.c_aideroom, newmsgid, 0, msg);
2300                 }
2301         }
2302
2303         /* For internet mail, drop a copy in the outbound queue room */
2304         if (recps != NULL)
2305          if (recps->num_internet > 0) {
2306                 CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, newmsgid, 0, msg);
2307         }
2308
2309         /* If other rooms are specified, drop them there too. */
2310         if (recps != NULL)
2311          if (recps->num_room > 0)
2312           for (i=0; i<num_tokens(recps->recp_room, '|'); ++i) {
2313                 extract_token(recipient, recps->recp_room, i,
2314                                         '|', sizeof recipient);
2315                 lprintf(CTDL_DEBUG, "Delivering to room <%s>\n", recipient);
2316                 CtdlSaveMsgPointerInRoom(recipient, newmsgid, 0, msg);
2317         }
2318
2319         /* Bump this user's messages posted counter. */
2320         lprintf(CTDL_DEBUG, "Updating user\n");
2321         lgetuser(&CC->user, CC->curr_user);
2322         CC->user.posted = CC->user.posted + 1;
2323         lputuser(&CC->user);
2324
2325         /* If this is private, local mail, make a copy in the
2326          * recipient's mailbox and bump the reference count.
2327          */
2328         if (recps != NULL)
2329          if (recps->num_local > 0)
2330           for (i=0; i<num_tokens(recps->recp_local, '|'); ++i) {
2331                 extract_token(recipient, recps->recp_local, i,
2332                                         '|', sizeof recipient);
2333                 lprintf(CTDL_DEBUG, "Delivering private local mail to <%s>\n",
2334                         recipient);
2335                 if (getuser(&userbuf, recipient) == 0) {
2336                         MailboxName(actual_rm, sizeof actual_rm,
2337                                         &userbuf, MAILROOM);
2338                         CtdlSaveMsgPointerInRoom(actual_rm, newmsgid, 0, msg);
2339                         BumpNewMailCounter(userbuf.usernum);
2340                 }
2341                 else {
2342                         lprintf(CTDL_DEBUG, "No user <%s>\n", recipient);
2343                         CtdlSaveMsgPointerInRoom(config.c_aideroom,
2344                                 newmsgid, 0, msg);
2345                 }
2346         }
2347
2348         /* Perform "after save" hooks */
2349         lprintf(CTDL_DEBUG, "Performing after-save hooks\n");
2350         PerformMessageHooks(msg, EVT_AFTERSAVE);
2351
2352         /* For IGnet mail, we have to save a new copy into the spooler for
2353          * each recipient, with the R and D fields set to the recipient and
2354          * destination-node.  This has two ugly side effects: all other
2355          * recipients end up being unlisted in this recipient's copy of the
2356          * message, and it has to deliver multiple messages to the same
2357          * node.  We'll revisit this again in a year or so when everyone has
2358          * a network spool receiver that can handle the new style messages.
2359          */
2360         if (recps != NULL)
2361          if (recps->num_ignet > 0)
2362           for (i=0; i<num_tokens(recps->recp_ignet, '|'); ++i) {
2363                 extract_token(recipient, recps->recp_ignet, i,
2364                                 '|', sizeof recipient);
2365
2366                 hold_R = msg->cm_fields['R'];
2367                 hold_D = msg->cm_fields['D'];
2368                 msg->cm_fields['R'] = malloc(SIZ);
2369                 msg->cm_fields['D'] = malloc(128);
2370                 extract_token(msg->cm_fields['R'], recipient, 0, '@', SIZ);
2371                 extract_token(msg->cm_fields['D'], recipient, 1, '@', 128);
2372                 
2373                 serialize_message(&smr, msg);
2374                 if (smr.len > 0) {
2375                         snprintf(submit_filename, sizeof submit_filename,
2376 #ifndef HAVE_SPOOL_DIR
2377                                          "."
2378 #else
2379                                          SPOOL_DIR
2380 #endif
2381                                          "/network/spoolin/netmail.%04lx.%04x.%04x",
2382                                          (long) getpid(), CC->cs_pid, ++seqnum);
2383                         network_fp = fopen(submit_filename, "wb+");
2384                         if (network_fp != NULL) {
2385                                 fwrite(smr.ser, smr.len, 1, network_fp);
2386                                 fclose(network_fp);
2387                         }
2388                         free(smr.ser);
2389                 }
2390
2391                 free(msg->cm_fields['R']);
2392                 free(msg->cm_fields['D']);
2393                 msg->cm_fields['R'] = hold_R;
2394                 msg->cm_fields['D'] = hold_D;
2395         }
2396
2397         /* Go back to the room we started from */
2398         lprintf(CTDL_DEBUG, "Returning to original room %s\n", hold_rm);
2399         if (strcasecmp(hold_rm, CC->room.QRname))
2400                 /* getroom(&CC->room, hold_rm); */
2401                 usergoto(hold_rm, 0, 1, NULL, NULL);
2402
2403         /* For internet mail, generate delivery instructions.
2404          * Yes, this is recursive.  Deal with it.  Infinite recursion does
2405          * not happen because the delivery instructions message does not
2406          * contain a recipient.
2407          */
2408         if (recps != NULL)
2409          if (recps->num_internet > 0) {
2410                 lprintf(CTDL_DEBUG, "Generating delivery instructions\n");
2411                 instr = malloc(SIZ * 2);
2412                 snprintf(instr, SIZ * 2,
2413                         "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
2414                         "bounceto|%s@%s\n",
2415                         SPOOLMIME, newmsgid, (long)time(NULL),
2416                         msg->cm_fields['A'], msg->cm_fields['N']
2417                 );
2418
2419                 for (i=0; i<num_tokens(recps->recp_internet, '|'); ++i) {
2420                         size_t tmp = strlen(instr);
2421                         extract_token(recipient, recps->recp_internet,
2422                                 i, '|', sizeof recipient);
2423                         snprintf(&instr[tmp], SIZ * 2 - tmp,
2424                                  "remote|%s|0||\n", recipient);
2425                 }
2426
2427                 imsg = malloc(sizeof(struct CtdlMessage));
2428                 memset(imsg, 0, sizeof(struct CtdlMessage));
2429                 imsg->cm_magic = CTDLMESSAGE_MAGIC;
2430                 imsg->cm_anon_type = MES_NORMAL;
2431                 imsg->cm_format_type = FMT_RFC822;
2432                 imsg->cm_fields['A'] = strdup("Citadel");
2433                 imsg->cm_fields['M'] = instr;
2434                 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
2435                 CtdlFreeMessage(imsg);
2436         }
2437
2438         /*
2439          * Any addresses to harvest for someone's address book?
2440          */
2441         if ( (CC->logged_in) && (recps != NULL) ) {
2442                 collected_addresses = harvest_collected_addresses(msg);
2443         }
2444
2445         if (collected_addresses != NULL) {
2446                 begin_critical_section(S_ATBF);
2447                 aptr = (struct addresses_to_be_filed *)
2448                         malloc(sizeof(struct addresses_to_be_filed));
2449                 aptr->next = atbf;
2450                 MailboxName(actual_rm, sizeof actual_rm,
2451                         &CC->user, USERCONTACTSROOM);
2452                 aptr->roomname = strdup(actual_rm);
2453                 aptr->collected_addresses = collected_addresses;
2454                 atbf = aptr;
2455                 end_critical_section(S_ATBF);
2456         }
2457         return(newmsgid);
2458 }
2459
2460
2461
2462
2463
2464
2465
2466
2467 /*
2468  * Convenience function for generating small administrative messages.
2469  */
2470 void quickie_message(char *from, char *to, char *room, char *text, 
2471                         int format_type, char *subject)
2472 {
2473         struct CtdlMessage *msg;
2474         struct recptypes *recp = NULL;
2475
2476         msg = malloc(sizeof(struct CtdlMessage));
2477         memset(msg, 0, sizeof(struct CtdlMessage));
2478         msg->cm_magic = CTDLMESSAGE_MAGIC;
2479         msg->cm_anon_type = MES_NORMAL;
2480         msg->cm_format_type = format_type;
2481         msg->cm_fields['A'] = strdup(from);
2482         if (room != NULL) msg->cm_fields['O'] = strdup(room);
2483         msg->cm_fields['N'] = strdup(NODENAME);
2484         if (to != NULL) {
2485                 msg->cm_fields['R'] = strdup(to);
2486                 recp = validate_recipients(to);
2487         }
2488         if (subject != NULL) {
2489                 msg->cm_fields['U'] = strdup(subject);
2490         }
2491         msg->cm_fields['M'] = strdup(text);
2492
2493         CtdlSubmitMsg(msg, recp, room);
2494         CtdlFreeMessage(msg);
2495         if (recp != NULL) free(recp);
2496 }
2497
2498
2499
2500 /*
2501  * Back end function used by CtdlMakeMessage() and similar functions
2502  */
2503 char *CtdlReadMessageBody(char *terminator,     /* token signalling EOT */
2504                         size_t maxlen,          /* maximum message length */
2505                         char *exist,            /* if non-null, append to it;
2506                                                    exist is ALWAYS freed  */
2507                         int crlf                /* CRLF newlines instead of LF */
2508                         ) {
2509         char buf[1024];
2510         int linelen;
2511         size_t message_len = 0;
2512         size_t buffer_len = 0;
2513         char *ptr;
2514         char *m;
2515         int flushing = 0;
2516         int finished = 0;
2517
2518         if (exist == NULL) {
2519                 m = malloc(4096);
2520                 m[0] = 0;
2521                 buffer_len = 4096;
2522                 message_len = 0;
2523         }
2524         else {
2525                 message_len = strlen(exist);
2526                 buffer_len = message_len + 4096;
2527                 m = realloc(exist, buffer_len);
2528                 if (m == NULL) {
2529                         free(exist);
2530                         return m;
2531                 }
2532         }
2533
2534         /* flush the input if we have nowhere to store it */
2535         if (m == NULL) {
2536                 flushing = 1;
2537         }
2538
2539         /* read in the lines of message text one by one */
2540         do {
2541                 if (client_getln(buf, (sizeof buf - 3)) < 1) finished = 1;
2542                 if (!strcmp(buf, terminator)) finished = 1;
2543                 if (crlf) {
2544                         strcat(buf, "\r\n");
2545                 }
2546                 else {
2547                         strcat(buf, "\n");
2548                 }
2549
2550                 if ( (!flushing) && (!finished) ) {
2551                         /* Measure the line */
2552                         linelen = strlen(buf);
2553         
2554                         /* augment the buffer if we have to */
2555                         if ((message_len + linelen) >= buffer_len) {
2556                                 ptr = realloc(m, (buffer_len * 2) );
2557                                 if (ptr == NULL) {      /* flush if can't allocate */
2558                                         flushing = 1;
2559                                 } else {
2560                                         buffer_len = (buffer_len * 2);
2561                                         m = ptr;
2562                                         lprintf(CTDL_DEBUG, "buffer_len is now %ld\n", (long)buffer_len);
2563                                 }
2564                         }
2565         
2566                         /* Add the new line to the buffer.  NOTE: this loop must avoid
2567                         * using functions like strcat() and strlen() because they
2568                         * traverse the entire buffer upon every call, and doing that
2569                         * for a multi-megabyte message slows it down beyond usability.
2570                         */
2571                         strcpy(&m[message_len], buf);
2572                         message_len += linelen;
2573                 }
2574
2575                 /* if we've hit the max msg length, flush the rest */
2576                 if (message_len >= maxlen) flushing = 1;
2577
2578         } while (!finished);
2579         return(m);
2580 }
2581
2582
2583
2584
2585 /*
2586  * Build a binary message to be saved on disk.
2587  * (NOTE: if you supply 'preformatted_text', the buffer you give it
2588  * will become part of the message.  This means you are no longer
2589  * responsible for managing that memory -- it will be freed along with
2590  * the rest of the fields when CtdlFreeMessage() is called.)
2591  */
2592
2593 struct CtdlMessage *CtdlMakeMessage(
2594         struct ctdluser *author,        /* author's user structure */
2595         char *recipient,                /* NULL if it's not mail */
2596         char *recp_cc,                  /* NULL if it's not mail */
2597         char *room,                     /* room where it's going */
2598         int type,                       /* see MES_ types in header file */
2599         int format_type,                /* variformat, plain text, MIME... */
2600         char *fake_name,                /* who we're masquerading as */
2601         char *subject,                  /* Subject (optional) */
2602         char *preformatted_text         /* ...or NULL to read text from client */
2603 ) {
2604         char dest_node[SIZ];
2605         char buf[SIZ];
2606         struct CtdlMessage *msg;
2607
2608         msg = malloc(sizeof(struct CtdlMessage));
2609         memset(msg, 0, sizeof(struct CtdlMessage));
2610         msg->cm_magic = CTDLMESSAGE_MAGIC;
2611         msg->cm_anon_type = type;
2612         msg->cm_format_type = format_type;
2613
2614         /* Don't confuse the poor folks if it's not routed mail. */
2615         strcpy(dest_node, "");
2616
2617         striplt(recipient);
2618         striplt(recp_cc);
2619
2620         snprintf(buf, sizeof buf, "cit%ld", author->usernum);   /* Path */
2621         msg->cm_fields['P'] = strdup(buf);
2622
2623         snprintf(buf, sizeof buf, "%ld", (long)time(NULL));     /* timestamp */
2624         msg->cm_fields['T'] = strdup(buf);
2625
2626         if (fake_name[0])                                       /* author */
2627                 msg->cm_fields['A'] = strdup(fake_name);
2628         else
2629                 msg->cm_fields['A'] = strdup(author->fullname);
2630
2631         if (CC->room.QRflags & QR_MAILBOX) {            /* room */
2632                 msg->cm_fields['O'] = strdup(&CC->room.QRname[11]);
2633         }
2634         else {
2635                 msg->cm_fields['O'] = strdup(CC->room.QRname);
2636         }
2637
2638         msg->cm_fields['N'] = strdup(NODENAME);         /* nodename */
2639         msg->cm_fields['H'] = strdup(HUMANNODE);                /* hnodename */
2640
2641         if (recipient[0] != 0) {
2642                 msg->cm_fields['R'] = strdup(recipient);
2643         }
2644         if (recp_cc[0] != 0) {
2645                 msg->cm_fields['Y'] = strdup(recp_cc);
2646         }
2647         if (dest_node[0] != 0) {
2648                 msg->cm_fields['D'] = strdup(dest_node);
2649         }
2650
2651         if ( (author == &CC->user) && (strlen(CC->cs_inet_email) > 0) ) {
2652                 msg->cm_fields['F'] = strdup(CC->cs_inet_email);
2653         }
2654
2655         if (subject != NULL) {
2656                 striplt(subject);
2657                 if (strlen(subject) > 0) {
2658                         msg->cm_fields['U'] = strdup(subject);
2659                 }
2660         }
2661
2662         if (preformatted_text != NULL) {
2663                 msg->cm_fields['M'] = preformatted_text;
2664         }
2665         else {
2666                 msg->cm_fields['M'] = CtdlReadMessageBody("000",
2667                                         config.c_maxmsglen, NULL, 0);
2668         }
2669
2670         return(msg);
2671 }
2672
2673
2674 /*
2675  * Check to see whether we have permission to post a message in the current
2676  * room.  Returns a *CITADEL ERROR CODE* and puts a message in errmsgbuf, or
2677  * returns 0 on success.
2678  */
2679 int CtdlDoIHavePermissionToPostInThisRoom(char *errmsgbuf, size_t n) {
2680
2681         if (!(CC->logged_in)) {
2682                 snprintf(errmsgbuf, n, "Not logged in.");
2683                 return (ERROR + NOT_LOGGED_IN);
2684         }
2685
2686         if ((CC->user.axlevel < 2)
2687             && ((CC->room.QRflags & QR_MAILBOX) == 0)) {
2688                 snprintf(errmsgbuf, n, "Need to be validated to enter "
2689                                 "(except in %s> to sysop)", MAILROOM);
2690                 return (ERROR + HIGHER_ACCESS_REQUIRED);
2691         }
2692
2693         if ((CC->user.axlevel < 4)
2694            && (CC->room.QRflags & QR_NETWORK)) {
2695                 snprintf(errmsgbuf, n, "Need net privileges to enter here.");
2696                 return (ERROR + HIGHER_ACCESS_REQUIRED);
2697         }
2698
2699         if ((CC->user.axlevel < 6)
2700            && (CC->room.QRflags & QR_READONLY)) {
2701                 snprintf(errmsgbuf, n, "Sorry, this is a read-only room.");
2702                 return (ERROR + HIGHER_ACCESS_REQUIRED);
2703         }
2704
2705         strcpy(errmsgbuf, "Ok");
2706         return(0);
2707 }
2708
2709
2710 /*
2711  * Check to see if the specified user has Internet mail permission
2712  * (returns nonzero if permission is granted)
2713  */
2714 int CtdlCheckInternetMailPermission(struct ctdluser *who) {
2715
2716         /* Do not allow twits to send Internet mail */
2717         if (who->axlevel <= 2) return(0);
2718
2719         /* Globally enabled? */
2720         if (config.c_restrict == 0) return(1);
2721
2722         /* User flagged ok? */
2723         if (who->flags & US_INTERNET) return(2);
2724
2725         /* Aide level access? */
2726         if (who->axlevel >= 6) return(3);
2727
2728         /* No mail for you! */
2729         return(0);
2730 }
2731
2732
2733 /*
2734  * Validate recipients, count delivery types and errors, and handle aliasing
2735  * FIXME check for dupes!!!!!
2736  * Returns 0 if all addresses are ok, -1 if no addresses were specified,
2737  * or the number of addresses found invalid.
2738  */
2739 struct recptypes *validate_recipients(char *supplied_recipients) {
2740         struct recptypes *ret;
2741         char recipients[SIZ];
2742         char this_recp[256];
2743         char this_recp_cooked[256];
2744         char append[SIZ];
2745         int num_recps = 0;
2746         int i, j;
2747         int mailtype;
2748         int invalid;
2749         struct ctdluser tempUS;
2750         struct ctdlroom tempQR;
2751         int in_quotes = 0;
2752
2753         /* Initialize */
2754         ret = (struct recptypes *) malloc(sizeof(struct recptypes));
2755         if (ret == NULL) return(NULL);
2756         memset(ret, 0, sizeof(struct recptypes));
2757
2758         ret->num_local = 0;
2759         ret->num_internet = 0;
2760         ret->num_ignet = 0;
2761         ret->num_error = 0;
2762         ret->num_room = 0;
2763
2764         if (supplied_recipients == NULL) {
2765                 strcpy(recipients, "");
2766         }
2767         else {
2768                 safestrncpy(recipients, supplied_recipients, sizeof recipients);
2769         }
2770
2771         /* Change all valid separator characters to commas */
2772         for (i=0; i<strlen(recipients); ++i) {
2773                 if ((recipients[i] == ';') || (recipients[i] == '|')) {
2774                         recipients[i] = ',';
2775                 }
2776         }
2777
2778         /* Now start extracting recipients... */
2779
2780         while (strlen(recipients) > 0) {
2781
2782                 for (i=0; i<=strlen(recipients); ++i) {
2783                         if (recipients[i] == '\"') in_quotes = 1 - in_quotes;
2784                         if ( ( (recipients[i] == ',') && (!in_quotes) ) || (recipients[i] == 0) ) {
2785                                 safestrncpy(this_recp, recipients, i+1);
2786                                 this_recp[i] = 0;
2787                                 if (recipients[i] == ',') {
2788                                         strcpy(recipients, &recipients[i+1]);
2789                                 }
2790                                 else {
2791                                         strcpy(recipients, "");
2792                                 }
2793                                 break;
2794                         }
2795                 }
2796
2797                 striplt(this_recp);
2798                 lprintf(CTDL_DEBUG, "Evaluating recipient #%d: %s\n", num_recps, this_recp);
2799                 ++num_recps;
2800                 mailtype = alias(this_recp);
2801                 mailtype = alias(this_recp);
2802                 mailtype = alias(this_recp);
2803                 for (j=0; j<=strlen(this_recp); ++j) {
2804                         if (this_recp[j]=='_') {
2805                                 this_recp_cooked[j] = ' ';
2806                         }
2807                         else {
2808                                 this_recp_cooked[j] = this_recp[j];
2809                         }
2810                 }
2811                 invalid = 0;
2812                 switch(mailtype) {
2813                         case MES_LOCAL:
2814                                 if (!strcasecmp(this_recp, "sysop")) {
2815                                         ++ret->num_room;
2816                                         strcpy(this_recp, config.c_aideroom);
2817                                         if (strlen(ret->recp_room) > 0) {
2818                                                 strcat(ret->recp_room, "|");
2819                                         }
2820                                         strcat(ret->recp_room, this_recp);
2821                                 }
2822                                 else if (getuser(&tempUS, this_recp) == 0) {
2823                                         ++ret->num_local;
2824                                         strcpy(this_recp, tempUS.fullname);
2825                                         if (strlen(ret->recp_local) > 0) {
2826                                                 strcat(ret->recp_local, "|");
2827                                         }
2828                                         strcat(ret->recp_local, this_recp);
2829                                 }
2830                                 else if (getuser(&tempUS, this_recp_cooked) == 0) {
2831                                         ++ret->num_local;
2832                                         strcpy(this_recp, tempUS.fullname);
2833                                         if (strlen(ret->recp_local) > 0) {
2834                                                 strcat(ret->recp_local, "|");
2835                                         }
2836                                         strcat(ret->recp_local, this_recp);
2837                                 }
2838                                 else if ( (!strncasecmp(this_recp, "room_", 5))
2839                                       && (!getroom(&tempQR, &this_recp_cooked[5])) ) {
2840                                         ++ret->num_room;
2841                                         if (strlen(ret->recp_room) > 0) {
2842                                                 strcat(ret->recp_room, "|");
2843                                         }
2844                                         strcat(ret->recp_room, &this_recp_cooked[5]);
2845                                 }
2846                                 else {
2847                                         ++ret->num_error;
2848                                         invalid = 1;
2849                                 }
2850                                 break;
2851                         case MES_INTERNET:
2852                                 /* Yes, you're reading this correctly: if the target
2853                                  * domain points back to the local system or an attached
2854                                  * Citadel directory, the address is invalid.  That's
2855                                  * because if the address were valid, we would have
2856                                  * already translated it to a local address by now.
2857                                  */
2858                                 if (IsDirectory(this_recp)) {
2859                                         ++ret->num_error;
2860                                         invalid = 1;
2861                                 }
2862                                 else {
2863                                         ++ret->num_internet;
2864                                         if (strlen(ret->recp_internet) > 0) {
2865                                                 strcat(ret->recp_internet, "|");
2866                                         }
2867                                         strcat(ret->recp_internet, this_recp);
2868                                 }
2869                                 break;
2870                         case MES_IGNET:
2871                                 ++ret->num_ignet;
2872                                 if (strlen(ret->recp_ignet) > 0) {
2873                                         strcat(ret->recp_ignet, "|");
2874                                 }
2875                                 strcat(ret->recp_ignet, this_recp);
2876                                 break;
2877                         case MES_ERROR:
2878                                 ++ret->num_error;
2879                                 invalid = 1;
2880                                 break;
2881                 }
2882                 if (invalid) {
2883                         if (strlen(ret->errormsg) == 0) {
2884                                 snprintf(append, sizeof append,
2885                                          "Invalid recipient: %s",
2886                                          this_recp);
2887                         }
2888                         else {
2889                                 snprintf(append, sizeof append,
2890                                          ", %s", this_recp);
2891                         }
2892                         if ( (strlen(ret->errormsg) + strlen(append)) < SIZ) {
2893                                 strcat(ret->errormsg, append);
2894                         }
2895                 }
2896                 else {
2897                         if (strlen(ret->display_recp) == 0) {
2898                                 strcpy(append, this_recp);
2899                         }
2900                         else {
2901                                 snprintf(append, sizeof append, ", %s",
2902                                          this_recp);
2903                         }
2904                         if ( (strlen(ret->display_recp)+strlen(append)) < SIZ) {
2905                                 strcat(ret->display_recp, append);
2906                         }
2907                 }
2908         }
2909
2910         if ((ret->num_local + ret->num_internet + ret->num_ignet +
2911            ret->num_room + ret->num_error) == 0) {
2912                 ret->num_error = (-1);
2913                 strcpy(ret->errormsg, "No recipients specified.");
2914         }
2915
2916         lprintf(CTDL_DEBUG, "validate_recipients()\n");
2917         lprintf(CTDL_DEBUG, " local: %d <%s>\n", ret->num_local, ret->recp_local);
2918         lprintf(CTDL_DEBUG, "  room: %d <%s>\n", ret->num_room, ret->recp_room);
2919         lprintf(CTDL_DEBUG, "  inet: %d <%s>\n", ret->num_internet, ret->recp_internet);
2920         lprintf(CTDL_DEBUG, " ignet: %d <%s>\n", ret->num_ignet, ret->recp_ignet);
2921         lprintf(CTDL_DEBUG, " error: %d <%s>\n", ret->num_error, ret->errormsg);
2922
2923         return(ret);
2924 }
2925
2926
2927
2928 /*
2929  * message entry  -  mode 0 (normal)
2930  */
2931 void cmd_ent0(char *entargs)
2932 {
2933         int post = 0;
2934         char recp[SIZ];
2935         char cc[SIZ];
2936         char bcc[SIZ];
2937         char masquerade_as[SIZ];
2938         int anon_flag = 0;
2939         int format_type = 0;
2940         char newusername[SIZ];
2941         struct CtdlMessage *msg;
2942         int anonymous = 0;
2943         char errmsg[SIZ];
2944         int err = 0;
2945         struct recptypes *valid = NULL;
2946         struct recptypes *valid_to = NULL;
2947         struct recptypes *valid_cc = NULL;
2948         struct recptypes *valid_bcc = NULL;
2949         char subject[SIZ];
2950         int do_confirm = 0;
2951         long msgnum;
2952
2953         unbuffer_output();
2954
2955         post = extract_int(entargs, 0);
2956         extract_token(recp, entargs, 1, '|', sizeof recp);
2957         anon_flag = extract_int(entargs, 2);
2958         format_type = extract_int(entargs, 3);
2959         extract_token(subject, entargs, 4, '|', sizeof subject);
2960         do_confirm = extract_int(entargs, 6);
2961         extract_token(cc, entargs, 7, '|', sizeof cc);
2962         extract_token(bcc, entargs, 8, '|', sizeof bcc);
2963
2964         /* first check to make sure the request is valid. */
2965
2966         err = CtdlDoIHavePermissionToPostInThisRoom(errmsg, sizeof errmsg);
2967         if (err) {
2968                 cprintf("%d %s\n", err, errmsg);
2969                 return;
2970         }
2971
2972         /* Check some other permission type things. */
2973
2974         if (post == 2) {
2975                 if (CC->user.axlevel < 6) {
2976                         cprintf("%d You don't have permission to masquerade.\n",
2977                                 ERROR + HIGHER_ACCESS_REQUIRED);
2978                         return;
2979                 }
2980                 extract_token(newusername, entargs, 5, '|', sizeof newusername);
2981                 memset(CC->fake_postname, 0, sizeof(CC->fake_postname) );
2982                 safestrncpy(CC->fake_postname, newusername,
2983                         sizeof(CC->fake_postname) );
2984                 cprintf("%d ok\n", CIT_OK);
2985                 return;
2986         }
2987         CC->cs_flags |= CS_POSTING;
2988
2989         /* In the Mail> room we have to behave a little differently --
2990          * make sure the user has specified at least one recipient.  Then
2991          * validate the recipient(s).
2992          */
2993         if ( (CC->room.QRflags & QR_MAILBOX)
2994            && (!strcasecmp(&CC->room.QRname[11], MAILROOM)) ) {
2995
2996                 if (CC->user.axlevel < 2) {
2997                         strcpy(recp, "sysop");
2998                         strcpy(cc, "");
2999                         strcpy(bcc, "");
3000                 }
3001
3002                 valid_to = validate_recipients(recp);
3003                 if (valid_to->num_error > 0) {
3004                         cprintf("%d Invalid recipient (To)\n", ERROR + NO_SUCH_USER);
3005                         free(valid_to);
3006                         return;
3007                 }
3008
3009                 valid_cc = validate_recipients(cc);
3010                 if (valid_cc->num_error > 0) {
3011                         cprintf("%d Invalid recipient (CC)\n", ERROR + NO_SUCH_USER);
3012                         free(valid_to);
3013                         free(valid_cc);
3014                         return;
3015                 }
3016
3017                 valid_bcc = validate_recipients(bcc);
3018                 if (valid_bcc->num_error > 0) {
3019                         cprintf("%d Invalid recipient (BCC)\n", ERROR + NO_SUCH_USER);
3020                         free(valid_to);
3021                         free(valid_cc);
3022                         free(valid_bcc);
3023                         return;
3024                 }
3025
3026                 /* Recipient required, but none were specified */
3027                 if ( (valid_to->num_error < 0) && (valid_cc->num_error < 0) && (valid_bcc->num_error < 0) ) {
3028                         free(valid_to);
3029                         free(valid_cc);
3030                         free(valid_bcc);
3031                         cprintf("%d At least one recipient is required.\n", ERROR + NO_SUCH_USER);
3032                         return;
3033                 }
3034
3035                 if (valid_to->num_internet + valid_cc->num_internet + valid_bcc->num_internet > 0) {
3036                         if (CtdlCheckInternetMailPermission(&CC->user)==0) {
3037                                 cprintf("%d You do not have permission "
3038                                         "to send Internet mail.\n",
3039                                         ERROR + HIGHER_ACCESS_REQUIRED);
3040                                 free(valid_to);
3041                                 free(valid_cc);
3042                                 free(valid_bcc);
3043                                 return;
3044                         }
3045                 }
3046
3047                 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)
3048                    && (CC->user.axlevel < 4) ) {
3049                         cprintf("%d Higher access required for network mail.\n",
3050                                 ERROR + HIGHER_ACCESS_REQUIRED);
3051                         free(valid_to);
3052                         free(valid_cc);
3053                         free(valid_bcc);
3054                         return;
3055                 }
3056         
3057                 if ((RESTRICT_INTERNET == 1)
3058                     && (valid_to->num_internet + valid_cc->num_internet + valid_bcc->num_internet > 0)
3059                     && ((CC->user.flags & US_INTERNET) == 0)
3060                     && (!CC->internal_pgm)) {
3061                         cprintf("%d You don't have access to Internet mail.\n",
3062                                 ERROR + HIGHER_ACCESS_REQUIRED);
3063                         free(valid_to);
3064                         free(valid_cc);
3065                         free(valid_bcc);
3066                         return;
3067                 }
3068
3069         }
3070
3071         /* Is this a room which has anonymous-only or anonymous-option? */
3072         anonymous = MES_NORMAL;
3073         if (CC->room.QRflags & QR_ANONONLY) {
3074                 anonymous = MES_ANONONLY;
3075         }
3076         if (CC->room.QRflags & QR_ANONOPT) {
3077                 if (anon_flag == 1) {   /* only if the user requested it */
3078                         anonymous = MES_ANONOPT;
3079                 }
3080         }
3081
3082         if ((CC->room.QRflags & QR_MAILBOX) == 0) {
3083                 recp[0] = 0;
3084         }
3085
3086         /* If we're only checking the validity of the request, return
3087          * success without creating the message.
3088          */
3089         if (post == 0) {
3090                 cprintf("%d %s\n", CIT_OK,
3091                         ((valid_to != NULL) ? valid_to->display_recp : "") );
3092                 free(valid_to);
3093                 free(valid_cc);
3094                 free(valid_bcc);
3095                 return;
3096         }
3097
3098         /* We don't need these anymore because we'll do it differently below */
3099         free(valid_to);
3100         free(valid_cc);
3101         free(valid_bcc);
3102
3103         /* Handle author masquerading */
3104         if (CC->fake_postname[0]) {
3105                 strcpy(masquerade_as, CC->fake_postname);
3106         }
3107         else if (CC->fake_username[0]) {
3108                 strcpy(masquerade_as, CC->fake_username);
3109         }
3110         else {
3111                 strcpy(masquerade_as, "");
3112         }
3113
3114         /* Read in the message from the client. */
3115         if (do_confirm) {
3116                 cprintf("%d send message\n", START_CHAT_MODE);
3117         } else {
3118                 cprintf("%d send message\n", SEND_LISTING);
3119         }
3120
3121         msg = CtdlMakeMessage(&CC->user, recp, cc,
3122                 CC->room.QRname, anonymous, format_type,
3123                 masquerade_as, subject, NULL);
3124
3125         /* Put together one big recipients struct containing to/cc/bcc all in
3126          * one.  This is for the envelope.
3127          */
3128         char *all_recps = malloc(SIZ * 3);
3129         strcpy(all_recps, recp);
3130         if (strlen(cc) > 0) {
3131                 if (strlen(all_recps) > 0) {
3132                         strcat(all_recps, ",");
3133                 }
3134                 strcat(all_recps, cc);
3135         }
3136         if (strlen(bcc) > 0) {
3137                 if (strlen(all_recps) > 0) {
3138                         strcat(all_recps, ",");
3139                 }
3140                 strcat(all_recps, bcc);
3141         }
3142         if (strlen(all_recps) > 0) {
3143                 valid = validate_recipients(all_recps);
3144         }
3145         else {
3146                 valid = NULL;
3147         }
3148         free(all_recps);
3149
3150         if (msg != NULL) {
3151                 msgnum = CtdlSubmitMsg(msg, valid, "");
3152
3153                 if (do_confirm) {
3154                         cprintf("%ld\n", msgnum);
3155                         if (msgnum >= 0L) {
3156                                 cprintf("Message accepted.\n");
3157                         }
3158                         else {
3159                                 cprintf("Internal error.\n");
3160                         }
3161                         if (msg->cm_fields['E'] != NULL) {
3162                                 cprintf("%s\n", msg->cm_fields['E']);
3163                         } else {
3164                                 cprintf("\n");
3165                         }
3166                         cprintf("000\n");
3167                 }
3168
3169                 CtdlFreeMessage(msg);
3170         }
3171         CC->fake_postname[0] = '\0';
3172         if (valid != NULL) {
3173                 free(valid);
3174         }
3175         return;
3176 }
3177
3178
3179
3180 /*
3181  * API function to delete messages which match a set of criteria
3182  * (returns the actual number of messages deleted)
3183  */
3184 int CtdlDeleteMessages(char *room_name,         /* which room */
3185                         long dmsgnum,           /* or "0" for any */
3186                         char *content_type,     /* or "" for any */
3187                         int deferred            /* let TDAP sweep it later */
3188 )
3189 {
3190
3191         struct ctdlroom qrbuf;
3192         struct cdbdata *cdbfr;
3193         long *msglist = NULL;
3194         long *dellist = NULL;
3195         int num_msgs = 0;
3196         int i;
3197         int num_deleted = 0;
3198         int delete_this;
3199         struct MetaData smi;
3200
3201         lprintf(CTDL_DEBUG, "CtdlDeleteMessages(%s, %ld, %s, %d)\n",
3202                 room_name, dmsgnum, content_type, deferred);
3203
3204         /* get room record, obtaining a lock... */
3205         if (lgetroom(&qrbuf, room_name) != 0) {
3206                 lprintf(CTDL_ERR, "CtdlDeleteMessages(): Room <%s> not found\n",
3207                         room_name);
3208                 return (0);     /* room not found */
3209         }
3210         cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
3211
3212         if (cdbfr != NULL) {
3213                 dellist = malloc(cdbfr->len);
3214                 msglist = (long *) cdbfr->ptr;
3215                 cdbfr->ptr = NULL;      /* CtdlDeleteMessages() now owns this memory */
3216                 num_msgs = cdbfr->len / sizeof(long);
3217                 cdb_free(cdbfr);
3218         }
3219         if (num_msgs > 0) {
3220                 for (i = 0; i < num_msgs; ++i) {
3221                         delete_this = 0x00;
3222
3223                         /* Set/clear a bit for each criterion */
3224
3225                         if ((dmsgnum == 0L) || (msglist[i] == dmsgnum)) {
3226                                 delete_this |= 0x01;
3227                         }
3228                         if (strlen(content_type) == 0) {
3229                                 delete_this |= 0x02;
3230                         } else {
3231                                 GetMetaData(&smi, msglist[i]);
3232                                 if (!strcasecmp(smi.meta_content_type,
3233                                                 content_type)) {
3234                                         delete_this |= 0x02;
3235                                 }
3236                         }
3237
3238                         /* Delete message only if all bits are set */
3239                         if (delete_this == 0x03) {
3240                                 dellist[num_deleted++] = msglist[i];
3241                                 msglist[i] = 0L;
3242                         }
3243                 }
3244
3245                 num_msgs = sort_msglist(msglist, num_msgs);
3246                 cdb_store(CDB_MSGLISTS, &qrbuf.QRnumber, (int)sizeof(long),
3247                           msglist, (int)(num_msgs * sizeof(long)));
3248
3249                 qrbuf.QRhighest = msglist[num_msgs - 1];
3250         }
3251         lputroom(&qrbuf);
3252
3253         /*
3254          * If the delete operation is "deferred" (and technically, any delete
3255          * operation not performed by THE DREADED AUTO-PURGER ought to be
3256          * a deferred delete) then we save a pointer to the message in the
3257          * DELETED_MSGS_ROOM.  This will cause the reference count to remain
3258          * at least 1, which will save the user from having to synchronously
3259          * wait for various disk-intensive operations to complete.
3260          */
3261         if ( (deferred) && (num_deleted) ) {
3262                 for (i=0; i<num_deleted; ++i) {
3263                         CtdlCopyMsgToRoom(dellist[i], DELETED_MSGS_ROOM);
3264                 }
3265         }
3266
3267         /* Go through the messages we pulled out of the index, and decrement
3268          * their reference counts by 1.  If this is the only room the message
3269          * was in, the reference count will reach zero and the message will
3270          * automatically be deleted from the database.  We do this in a
3271          * separate pass because there might be plug-in hooks getting called,
3272          * and we don't want that happening during an S_ROOMS critical
3273          * section.
3274          */
3275         if (num_deleted) for (i=0; i<num_deleted; ++i) {
3276                 PerformDeleteHooks(qrbuf.QRname, dellist[i]);
3277                 AdjRefCount(dellist[i], -1);
3278         }
3279
3280         /* Now free the memory we used, and go away. */
3281         if (msglist != NULL) free(msglist);
3282         if (dellist != NULL) free(dellist);
3283         lprintf(CTDL_DEBUG, "%d message(s) deleted.\n", num_deleted);
3284         return (num_deleted);
3285 }
3286
3287
3288
3289 /*
3290  * Check whether the current user has permission to delete messages from
3291  * the current room (returns 1 for yes, 0 for no)
3292  */
3293 int CtdlDoIHavePermissionToDeleteMessagesFromThisRoom(void) {
3294         getuser(&CC->user, CC->curr_user);
3295         if ((CC->user.axlevel < 6)
3296             && (CC->user.usernum != CC->room.QRroomaide)
3297             && ((CC->room.QRflags & QR_MAILBOX) == 0)
3298             && (!(CC->internal_pgm))) {
3299                 return(0);
3300         }
3301         return(1);
3302 }
3303
3304
3305
3306 /*
3307  * Delete message from current room
3308  */
3309 void cmd_dele(char *delstr)
3310 {
3311         long delnum;
3312         int num_deleted;
3313
3314         if (CtdlDoIHavePermissionToDeleteMessagesFromThisRoom() == 0) {
3315                 cprintf("%d Higher access required.\n",
3316                         ERROR + HIGHER_ACCESS_REQUIRED);
3317                 return;
3318         }
3319         delnum = extract_long(delstr, 0);
3320
3321         num_deleted = CtdlDeleteMessages(CC->room.QRname, delnum, "", 1);
3322
3323         if (num_deleted) {
3324                 cprintf("%d %d message%s deleted.\n", CIT_OK,
3325                         num_deleted, ((num_deleted != 1) ? "s" : ""));
3326         } else {
3327                 cprintf("%d Message %ld not found.\n", ERROR + MESSAGE_NOT_FOUND, delnum);
3328         }
3329 }
3330
3331
3332 /*
3333  * Back end API function for moves and deletes
3334  */
3335 int CtdlCopyMsgToRoom(long msgnum, char *dest) {
3336         int err;
3337
3338         err = CtdlSaveMsgPointerInRoom(dest, msgnum, 1, NULL);
3339         if (err != 0) return(err);
3340
3341         return(0);
3342 }
3343
3344
3345
3346 /*
3347  * move or copy a message to another room
3348  */
3349 void cmd_move(char *args)
3350 {
3351         long num;
3352         char targ[ROOMNAMELEN];
3353         struct ctdlroom qtemp;
3354         int err;
3355         int is_copy = 0;
3356         int ra;
3357         int permit = 0;
3358
3359         num = extract_long(args, 0);
3360         extract_token(targ, args, 1, '|', sizeof targ);
3361         convert_room_name_macros(targ, sizeof targ);
3362         targ[ROOMNAMELEN - 1] = 0;
3363         is_copy = extract_int(args, 2);
3364
3365         if (getroom(&qtemp, targ) != 0) {
3366                 cprintf("%d '%s' does not exist.\n",
3367                         ERROR + ROOM_NOT_FOUND, targ);
3368                 return;
3369         }
3370
3371         getuser(&CC->user, CC->curr_user);
3372         CtdlRoomAccess(&qtemp, &CC->user, &ra, NULL);
3373
3374         /* Check for permission to perform this operation.
3375          * Remember: "CC->room" is source, "qtemp" is target.
3376          */
3377         permit = 0;
3378
3379         /* Aides can move/copy */
3380         if (CC->user.axlevel >= 6) permit = 1;
3381
3382         /* Room aides can move/copy */
3383         if (CC->user.usernum == CC->room.QRroomaide) permit = 1;
3384
3385         /* Permit move/copy from personal rooms */
3386         if ((CC->room.QRflags & QR_MAILBOX)
3387            && (qtemp.QRflags & QR_MAILBOX)) permit = 1;
3388
3389         /* Permit only copy from public to personal room */
3390         if ( (is_copy)
3391            && (!(CC->room.QRflags & QR_MAILBOX))
3392            && (qtemp.QRflags & QR_MAILBOX)) permit = 1;
3393
3394         /* User must have access to target room */
3395         if (!(ra & UA_KNOWN))  permit = 0;
3396
3397         if (!permit) {
3398                 cprintf("%d Higher access required.\n",
3399                         ERROR + HIGHER_ACCESS_REQUIRED);
3400                 return;
3401         }
3402
3403         err = CtdlCopyMsgToRoom(num, targ);
3404         if (err != 0) {
3405                 cprintf("%d Cannot store message in %s: error %d\n",
3406                         err, targ, err);
3407                 return;
3408         }
3409
3410         /* Now delete the message from the source room,
3411          * if this is a 'move' rather than a 'copy' operation.
3412          */
3413         if (is_copy == 0) {
3414                 CtdlDeleteMessages(CC->room.QRname, num, "", 0);
3415         }
3416
3417         cprintf("%d Message %s.\n", CIT_OK, (is_copy ? "copied" : "moved") );
3418 }
3419
3420
3421
3422 /*
3423  * GetMetaData()  -  Get the supplementary record for a message
3424  */
3425 void GetMetaData(struct MetaData *smibuf, long msgnum)
3426 {
3427
3428         struct cdbdata *cdbsmi;
3429         long TheIndex;
3430
3431         memset(smibuf, 0, sizeof(struct MetaData));
3432         smibuf->meta_msgnum = msgnum;
3433         smibuf->meta_refcount = 1;      /* Default reference count is 1 */
3434
3435         /* Use the negative of the message number for its supp record index */
3436         TheIndex = (0L - msgnum);
3437
3438         cdbsmi = cdb_fetch(CDB_MSGMAIN, &TheIndex, sizeof(long));
3439         if (cdbsmi == NULL) {
3440                 return;         /* record not found; go with defaults */
3441         }
3442         memcpy(smibuf, cdbsmi->ptr,
3443                ((cdbsmi->len > sizeof(struct MetaData)) ?
3444                 sizeof(struct MetaData) : cdbsmi->len));
3445         cdb_free(cdbsmi);
3446         return;
3447 }
3448
3449
3450 /*
3451  * PutMetaData()  -  (re)write supplementary record for a message
3452  */
3453 void PutMetaData(struct MetaData *smibuf)
3454 {
3455         long TheIndex;
3456
3457         /* Use the negative of the message number for the metadata db index */
3458         TheIndex = (0L - smibuf->meta_msgnum);
3459
3460         lprintf(CTDL_DEBUG, "PutMetaData(%ld) - ref count is %d\n",
3461                 smibuf->meta_msgnum, smibuf->meta_refcount);
3462
3463         cdb_store(CDB_MSGMAIN,
3464                   &TheIndex, (int)sizeof(long),
3465                   smibuf, (int)sizeof(struct MetaData));
3466
3467 }
3468
3469 /*
3470  * AdjRefCount  -  change the reference count for a message;
3471  *               delete the message if it reaches zero
3472  */
3473 void AdjRefCount(long msgnum, int incr)
3474 {
3475
3476         struct MetaData smi;
3477         long delnum;
3478
3479         /* This is a *tight* critical section; please keep it that way, as
3480          * it may get called while nested in other critical sections.  
3481          * Complicating this any further will surely cause deadlock!
3482          */
3483         begin_critical_section(S_SUPPMSGMAIN);
3484         GetMetaData(&smi, msgnum);
3485         smi.meta_refcount += incr;
3486         PutMetaData(&smi);
3487         end_critical_section(S_SUPPMSGMAIN);
3488         lprintf(CTDL_DEBUG, "msg %ld ref count incr %d, is now %d\n",
3489                 msgnum, incr, smi.meta_refcount);
3490
3491         /* If the reference count is now zero, delete the message
3492          * (and its supplementary record as well).
3493          */
3494         if (smi.meta_refcount == 0) {
3495                 lprintf(CTDL_DEBUG, "Deleting message <%ld>\n", msgnum);
3496
3497                 /* Remove from fulltext index */
3498                 if (config.c_enable_fulltext) {
3499                         ft_index_message(msgnum, 0);
3500                 }
3501
3502                 /* Remove from message base */
3503                 delnum = msgnum;
3504                 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3505                 cdb_delete(CDB_BIGMSGS, &delnum, (int)sizeof(long));
3506
3507                 /* Remove metadata record */
3508                 delnum = (0L - msgnum);
3509                 cdb_delete(CDB_MSGMAIN, &delnum, (int)sizeof(long));
3510         }
3511 }
3512
3513 /*
3514  * Write a generic object to this room
3515  *
3516  * Note: this could be much more efficient.  Right now we use two temporary
3517  * files, and still pull the message into memory as with all others.
3518  */
3519 void CtdlWriteObject(char *req_room,            /* Room to stuff it in */
3520                         char *content_type,     /* MIME type of this object */
3521                         char *tempfilename,     /* Where to fetch it from */
3522                         struct ctdluser *is_mailbox,    /* Mailbox room? */
3523                         int is_binary,          /* Is encoding necessary? */
3524                         int is_unique,          /* Del others of this type? */
3525                         unsigned int flags      /* Internal save flags */
3526                         )
3527 {
3528
3529         FILE *fp;
3530         struct ctdlroom qrbuf;
3531         char roomname[ROOMNAMELEN];
3532         struct CtdlMessage *msg;
3533
3534         char *raw_message = NULL;
3535         char *encoded_message = NULL;
3536         off_t raw_length = 0;
3537
3538         if (is_mailbox != NULL)
3539                 MailboxName(roomname, sizeof roomname, is_mailbox, req_room);
3540         else
3541                 safestrncpy(roomname, req_room, sizeof(roomname));
3542         lprintf(CTDL_DEBUG, "CtdlWriteObject() to <%s> (flags=%d)\n", roomname, flags);
3543
3544
3545         fp = fopen(tempfilename, "rb");
3546         if (fp == NULL) {
3547                 lprintf(CTDL_CRIT, "Cannot open %s: %s\n",
3548                         tempfilename, strerror(errno));
3549                 return;
3550         }
3551         fseek(fp, 0L, SEEK_END);
3552         raw_length = ftell(fp);
3553         rewind(fp);
3554         lprintf(CTDL_DEBUG, "Raw length is %ld\n", (long)raw_length);
3555
3556         raw_message = malloc((size_t)raw_length + 2);
3557         fread(raw_message, (size_t)raw_length, 1, fp);
3558         fclose(fp);
3559
3560         if (is_binary) {
3561                 encoded_message = malloc((size_t)
3562                         (((raw_length * 134) / 100) + 4096 ) );
3563         }
3564         else {
3565                 encoded_message = malloc((size_t)(raw_length + 4096));
3566         }
3567
3568         sprintf(encoded_message, "Content-type: %s\n", content_type);
3569
3570         if (is_binary) {
3571                 sprintf(&encoded_message[strlen(encoded_message)],
3572                         "Content-transfer-encoding: base64\n\n"
3573                 );
3574         }
3575         else {
3576                 sprintf(&encoded_message[strlen(encoded_message)],
3577                         "Content-transfer-encoding: 7bit\n\n"
3578                 );
3579         }
3580
3581         if (is_binary) {
3582                 CtdlEncodeBase64(
3583                         &encoded_message[strlen(encoded_message)],
3584                         raw_message,
3585                         (int)raw_length
3586                 );
3587         }
3588         else {
3589                 raw_message[raw_length] = 0;
3590                 memcpy(
3591                         &encoded_message[strlen(encoded_message)],
3592                         raw_message,
3593                         (int)(raw_length+1)
3594                 );
3595         }
3596
3597         free(raw_message);
3598
3599         lprintf(CTDL_DEBUG, "Allocating\n");
3600         msg = malloc(sizeof(struct CtdlMessage));
3601         memset(msg, 0, sizeof(struct CtdlMessage));
3602         msg->cm_magic = CTDLMESSAGE_MAGIC;
3603         msg->cm_anon_type = MES_NORMAL;
3604         msg->cm_format_type = 4;
3605         msg->cm_fields['A'] = strdup(CC->user.fullname);
3606         msg->cm_fields['O'] = strdup(req_room);
3607         msg->cm_fields['N'] = strdup(config.c_nodename);
3608         msg->cm_fields['H'] = strdup(config.c_humannode);
3609         msg->cm_flags = flags;
3610         
3611         msg->cm_fields['M'] = encoded_message;
3612
3613         /* Create the requested room if we have to. */
3614         if (getroom(&qrbuf, roomname) != 0) {
3615                 create_room(roomname, 
3616                         ( (is_mailbox != NULL) ? 5 : 3 ),
3617                         "", 0, 1, 0, VIEW_BBS);
3618         }
3619         /* If the caller specified this object as unique, delete all
3620          * other objects of this type that are currently in the room.
3621          */
3622         if (is_unique) {
3623                 lprintf(CTDL_DEBUG, "Deleted %d other msgs of this type\n",
3624                         CtdlDeleteMessages(roomname, 0L, content_type, 0)
3625                 );
3626         }
3627         /* Now write the data */
3628         CtdlSubmitMsg(msg, NULL, roomname);
3629         CtdlFreeMessage(msg);
3630 }
3631
3632
3633
3634
3635
3636
3637 void CtdlGetSysConfigBackend(long msgnum, void *userdata) {
3638         config_msgnum = msgnum;
3639 }
3640
3641
3642 char *CtdlGetSysConfig(char *sysconfname) {
3643         char hold_rm[ROOMNAMELEN];
3644         long msgnum;
3645         char *conf;
3646         struct CtdlMessage *msg;
3647         char buf[SIZ];
3648         
3649         strcpy(hold_rm, CC->room.QRname);
3650         if (getroom(&CC->room, SYSCONFIGROOM) != 0) {
3651                 getroom(&CC->room, hold_rm);
3652                 return NULL;
3653         }
3654
3655
3656         /* We want the last (and probably only) config in this room */
3657         begin_critical_section(S_CONFIG);
3658         config_msgnum = (-1L);
3659         CtdlForEachMessage(MSGS_LAST, 1, sysconfname, NULL,
3660                 CtdlGetSysConfigBackend, NULL);
3661         msgnum = config_msgnum;
3662         end_critical_section(S_CONFIG);
3663
3664         if (msgnum < 0L) {
3665                 conf = NULL;
3666         }
3667         else {
3668                 msg = CtdlFetchMessage(msgnum, 1);
3669                 if (msg != NULL) {
3670                         conf = strdup(msg->cm_fields['M']);
3671                         CtdlFreeMessage(msg);
3672                 }
3673                 else {
3674                         conf = NULL;
3675                 }
3676         }
3677
3678         getroom(&CC->room, hold_rm);
3679
3680         if (conf != NULL) do {
3681                 extract_token(buf, conf, 0, '\n', sizeof buf);
3682                 strcpy(conf, &conf[strlen(buf)+1]);
3683         } while ( (strlen(conf)>0) && (strlen(buf)>0) );
3684
3685         return(conf);
3686 }
3687
3688 void CtdlPutSysConfig(char *sysconfname, char *sysconfdata) {
3689         char temp[PATH_MAX];
3690         FILE *fp;
3691
3692         strcpy(temp, tmpnam(NULL));
3693
3694         fp = fopen(temp, "w");
3695         if (fp == NULL) return;
3696         fprintf(fp, "%s", sysconfdata);
3697         fclose(fp);
3698
3699         /* this handy API function does all the work for us */
3700         CtdlWriteObject(SYSCONFIGROOM, sysconfname, temp, NULL, 0, 1, 0);
3701         unlink(temp);
3702 }
3703
3704
3705 /*
3706  * Determine whether a given Internet address belongs to the current user
3707  */
3708 int CtdlIsMe(char *addr, int addr_buf_len)
3709 {
3710         struct recptypes *recp;
3711         int i;
3712
3713         recp = validate_recipients(addr);
3714         if (recp == NULL) return(0);
3715
3716         if (recp->num_local == 0) {
3717                 free(recp);
3718                 return(0);
3719         }
3720
3721         for (i=0; i<recp->num_local; ++i) {
3722                 extract_token(addr, recp->recp_local, i, '|', addr_buf_len);
3723                 if (!strcasecmp(addr, CC->user.fullname)) {
3724                         free(recp);
3725                         return(1);
3726                 }
3727         }
3728
3729         free(recp);
3730         return(0);
3731 }
3732
3733
3734 /*
3735  * Citadel protocol command to do the same
3736  */
3737 void cmd_isme(char *argbuf) {
3738         char addr[256];
3739
3740         if (CtdlAccessCheck(ac_logged_in)) return;
3741         extract_token(addr, argbuf, 0, '|', sizeof addr);
3742
3743         if (CtdlIsMe(addr, sizeof addr)) {
3744                 cprintf("%d %s\n", CIT_OK, addr);
3745         }
3746         else {
3747                 cprintf("%d Not you.\n", ERROR + ILLEGAL_VALUE);
3748         }
3749
3750 }