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