8e48ee92e3bfc6e3ed175074d11dc2e8b56013e9
[citadel.git] / citadel / msgbase.c
1 /* $Id$ */
2 #include "sysdep.h"
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <fcntl.h>
7 #include <time.h>
8 #include <ctype.h>
9 #include <string.h>
10 #include <syslog.h>
11 #ifdef HAVE_PTHREAD_H
12 #include <pthread.h>
13 #endif
14 #include <limits.h>
15 #include "citadel.h"
16 #include "server.h"
17 #include <errno.h>
18 #include <sys/stat.h>
19 #include "database.h"
20 #include "msgbase.h"
21 #include "support.h"
22 #include "sysdep_decls.h"
23 #include "citserver.h"
24 #include "room_ops.h"
25 #include "user_ops.h"
26 #include "file_ops.h"
27 #include "control.h"
28 #include "dynloader.h"
29 #include "tools.h"
30 #include "mime_parser.h"
31 #include "html.h"
32
33 #define desired_section ((char *)CtdlGetUserData(SYM_DESIRED_SECTION))
34 #define ma ((struct ma_info *)CtdlGetUserData(SYM_MA_INFO))
35
36 extern struct config config;
37
38 /*
39  * This function is self explanatory.
40  * (What can I say, I'm in a weird mood today...)
41  */
42 void remove_any_whitespace_to_the_left_or_right_of_at_symbol(char *name)
43 {
44         int i;
45
46         for (i = 0; i < strlen(name); ++i)
47                 if (name[i] == '@') {
48                         if (i > 0)
49                                 if (isspace(name[i - 1])) {
50                                         strcpy(&name[i - 1], &name[i]);
51                                         i = 0;
52                                 }
53                         while (isspace(name[i + 1])) {
54                                 strcpy(&name[i + 1], &name[i + 2]);
55                         }
56                 }
57 }
58
59
60 /*
61  * Aliasing for network mail.
62  * (Error messages have been commented out, because this is a server.)
63  */
64 int alias(char *name)
65 {                               /* process alias and routing info for mail */
66         FILE *fp;
67         int a, b;
68         char aaa[300], bbb[300];
69
70         lprintf(9, "alias() called for <%s>\n", name);
71
72         remove_any_whitespace_to_the_left_or_right_of_at_symbol(name);
73
74         fp = fopen("network/mail.aliases", "r");
75         if (fp == NULL)
76                 fp = fopen("/dev/null", "r");
77         if (fp == NULL)
78                 return (MES_ERROR);
79         strcpy(aaa, "");
80         strcpy(bbb, "");
81         while (fgets(aaa, sizeof aaa, fp) != NULL) {
82                 while (isspace(name[0]))
83                         strcpy(name, &name[1]);
84                 aaa[strlen(aaa) - 1] = 0;
85                 strcpy(bbb, "");
86                 for (a = 0; a < strlen(aaa); ++a) {
87                         if (aaa[a] == ',') {
88                                 strcpy(bbb, &aaa[a + 1]);
89                                 aaa[a] = 0;
90                         }
91                 }
92                 if (!strcasecmp(name, aaa))
93                         strcpy(name, bbb);
94         }
95         fclose(fp);
96         lprintf(7, "Mail is being forwarded to %s\n", name);
97
98         /* determine local or remote type, see citadel.h */
99         for (a = 0; a < strlen(name); ++a)
100                 if (name[a] == '!')
101                         return (MES_INTERNET);
102         for (a = 0; a < strlen(name); ++a)
103                 if (name[a] == '@')
104                         for (b = a; b < strlen(name); ++b)
105                                 if (name[b] == '.')
106                                         return (MES_INTERNET);
107         b = 0;
108         for (a = 0; a < strlen(name); ++a)
109                 if (name[a] == '@')
110                         ++b;
111         if (b > 1) {
112                 lprintf(7, "Too many @'s in address\n");
113                 return (MES_ERROR);
114         }
115         if (b == 1) {
116                 for (a = 0; a < strlen(name); ++a)
117                         if (name[a] == '@')
118                                 strcpy(bbb, &name[a + 1]);
119                 while (bbb[0] == 32)
120                         strcpy(bbb, &bbb[1]);
121                 fp = fopen("network/mail.sysinfo", "r");
122                 if (fp == NULL)
123                         return (MES_ERROR);
124               GETSN:do {
125                         a = getstring(fp, aaa);
126                 } while ((a >= 0) && (strcasecmp(aaa, bbb)));
127                 a = getstring(fp, aaa);
128                 if (!strncmp(aaa, "use ", 4)) {
129                         strcpy(bbb, &aaa[4]);
130                         fseek(fp, 0L, 0);
131                         goto GETSN;
132                 }
133                 fclose(fp);
134                 if (!strncmp(aaa, "uum", 3)) {
135                         strcpy(bbb, name);
136                         for (a = 0; a < strlen(bbb); ++a) {
137                                 if (bbb[a] == '@')
138                                         bbb[a] = 0;
139                                 if (bbb[a] == ' ')
140                                         bbb[a] = '_';
141                         }
142                         while (bbb[strlen(bbb) - 1] == '_')
143                                 bbb[strlen(bbb) - 1] = 0;
144                         sprintf(name, &aaa[4], bbb);
145                         return (MES_INTERNET);
146                 }
147                 if (!strncmp(aaa, "bin", 3)) {
148                         strcpy(aaa, name);
149                         strcpy(bbb, name);
150                         while (aaa[strlen(aaa) - 1] != '@')
151                                 aaa[strlen(aaa) - 1] = 0;
152                         aaa[strlen(aaa) - 1] = 0;
153                         while (aaa[strlen(aaa) - 1] == ' ')
154                                 aaa[strlen(aaa) - 1] = 0;
155                         while (bbb[0] != '@')
156                                 strcpy(bbb, &bbb[1]);
157                         strcpy(bbb, &bbb[1]);
158                         while (bbb[0] == ' ')
159                                 strcpy(bbb, &bbb[1]);
160                         sprintf(name, "%s @%s", aaa, bbb);
161                         return (MES_BINARY);
162                 }
163                 return (MES_ERROR);
164         }
165         return (MES_LOCAL);
166 }
167
168
169 void get_mm(void)
170 {
171         FILE *fp;
172
173         fp = fopen("citadel.control", "r");
174         fread((char *) &CitControl, sizeof(struct CitControl), 1, fp);
175         fclose(fp);
176 }
177
178
179
180 void simple_listing(long msgnum)
181 {
182         cprintf("%ld\n", msgnum);
183 }
184
185
186 /*
187  * API function to perform an operation for each qualifying message in the
188  * current room.
189  */
190 void CtdlForEachMessage(int mode, long ref,
191                         char *content_type,
192                         void (*CallBack) (long msgnum))
193 {
194
195         int a;
196         struct visit vbuf;
197         struct cdbdata *cdbfr;
198         long *msglist = NULL;
199         int num_msgs = 0;
200         long thismsg;
201         struct SuppMsgInfo smi;
202
203         /* Learn about the user and room in question */
204         get_mm();
205         getuser(&CC->usersupp, CC->curr_user);
206         CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
207
208         /* Load the message list */
209         cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->quickroom.QRnumber, sizeof(long));
210         if (cdbfr != NULL) {
211                 msglist = mallok(cdbfr->len);
212                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
213                 num_msgs = cdbfr->len / sizeof(long);
214                 cdb_free(cdbfr);
215         } else {
216                 return;         /* No messages at all?  No further action. */
217         }
218
219
220         /* If the caller is looking for a specific MIME type, then filter
221          * out all messages which are not of the type requested.
222          */
223         if (num_msgs > 0)
224                 if (content_type != NULL)
225                         if (strlen(content_type) > 0)
226                                 for (a = 0; a < num_msgs; ++a) {
227                                         GetSuppMsgInfo(&smi, msglist[a]);
228                                         if (strcasecmp(smi.smi_content_type, content_type)) {
229                                                 msglist[a] = 0L;
230                                         }
231                                 }
232
233         num_msgs = sort_msglist(msglist, num_msgs);
234         
235         /*
236          * Now iterate through the message list, according to the
237          * criteria supplied by the caller.
238          */
239         if (num_msgs > 0)
240                 for (a = 0; a < num_msgs; ++a) {
241                         thismsg = msglist[a];
242                         if ((thismsg > 0)
243                             && (
244
245                                        (mode == MSGS_ALL)
246                                        || ((mode == MSGS_OLD) && (thismsg <= vbuf.v_lastseen))
247                                        || ((mode == MSGS_NEW) && (thismsg > vbuf.v_lastseen))
248                                        || ((mode == MSGS_NEW) && (thismsg >= vbuf.v_lastseen)
249                                     && (CC->usersupp.flags & US_LASTOLD))
250                                        || ((mode == MSGS_LAST) && (a >= (num_msgs - ref)))
251                                    || ((mode == MSGS_FIRST) && (a < ref))
252                                 || ((mode == MSGS_GT) && (thismsg > ref))
253                             )
254                             ) {
255                                 CallBack(thismsg);
256                         }
257                 }
258         phree(msglist);         /* Clean up */
259 }
260
261
262
263 /*
264  * cmd_msgs()  -  get list of message #'s in this room
265  *                implements the MSGS server command using CtdlForEachMessage()
266  */
267 void cmd_msgs(char *cmdbuf)
268 {
269         int mode = 0;
270         char which[256];
271         int cm_ref = 0;
272
273         extract(which, cmdbuf, 0);
274         cm_ref = extract_int(cmdbuf, 1);
275
276         mode = MSGS_ALL;
277         strcat(which, "   ");
278         if (!strncasecmp(which, "OLD", 3))
279                 mode = MSGS_OLD;
280         else if (!strncasecmp(which, "NEW", 3))
281                 mode = MSGS_NEW;
282         else if (!strncasecmp(which, "FIRST", 5))
283                 mode = MSGS_FIRST;
284         else if (!strncasecmp(which, "LAST", 4))
285                 mode = MSGS_LAST;
286         else if (!strncasecmp(which, "GT", 2))
287                 mode = MSGS_GT;
288
289         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
290                 cprintf("%d not logged in\n", ERROR + NOT_LOGGED_IN);
291                 return;
292         }
293         cprintf("%d Message list...\n", LISTING_FOLLOWS);
294         CtdlForEachMessage(mode, cm_ref, NULL, simple_listing);
295         cprintf("000\n");
296 }
297
298
299
300
301 /* 
302  * help_subst()  -  support routine for help file viewer
303  */
304 void help_subst(char *strbuf, char *source, char *dest)
305 {
306         char workbuf[256];
307         int p;
308
309         while (p = pattern2(strbuf, source), (p >= 0)) {
310                 strcpy(workbuf, &strbuf[p + strlen(source)]);
311                 strcpy(&strbuf[p], dest);
312                 strcat(strbuf, workbuf);
313         }
314 }
315
316
317 void do_help_subst(char *buffer)
318 {
319         char buf2[16];
320
321         help_subst(buffer, "^nodename", config.c_nodename);
322         help_subst(buffer, "^humannode", config.c_humannode);
323         help_subst(buffer, "^fqdn", config.c_fqdn);
324         help_subst(buffer, "^username", CC->usersupp.fullname);
325         sprintf(buf2, "%ld", CC->usersupp.usernum);
326         help_subst(buffer, "^usernum", buf2);
327         help_subst(buffer, "^sysadm", config.c_sysadm);
328         help_subst(buffer, "^variantname", CITADEL);
329         sprintf(buf2, "%d", config.c_maxsessions);
330         help_subst(buffer, "^maxsessions", buf2);
331 }
332
333
334
335 /*
336  * memfmout()  -  Citadel text formatter and paginator.
337  *             Although the original purpose of this routine was to format
338  *             text to the reader's screen width, all we're really using it
339  *             for here is to format text out to 80 columns before sending it
340  *             to the client.  The client software may reformat it again.
341  */
342 void memfmout(int width, char *mptr, char subst)
343                         /* screen width to use */
344                         /* where are we going to get our text from? */
345                         /* nonzero if we should use hypertext mode */
346 {
347         int a, b, c;
348         int real = 0;
349         int old = 0;
350         CIT_UBYTE ch;
351         char aaa[140];
352         char buffer[256];
353
354         strcpy(aaa, "");
355         old = 255;
356         strcpy(buffer, "");
357         c = 1;                  /* c is the current pos */
358
359 FMTA:   if (subst) {
360                 while (ch = *mptr, ((ch != 0) && (strlen(buffer) < 126))) {
361                         ch = *mptr++;
362                         buffer[strlen(buffer) + 1] = 0;
363                         buffer[strlen(buffer)] = ch;
364                 }
365
366                 if (buffer[0] == '^')
367                         do_help_subst(buffer);
368
369                 buffer[strlen(buffer) + 1] = 0;
370                 a = buffer[0];
371                 strcpy(buffer, &buffer[1]);
372         } else {
373                 ch = *mptr++;
374         }
375
376         old = real;
377         real = ch;
378         if (ch <= 0)
379                 goto FMTEND;
380
381         if (((ch == 13) || (ch == 10)) && (old != 13) && (old != 10))
382                 ch = 32;
383         if (((old == 13) || (old == 10)) && (isspace(real))) {
384                 cprintf("\n");
385                 c = 1;
386         }
387         if (ch > 126)
388                 goto FMTA;
389
390         if (ch > 32) {
391                 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
392                         cprintf("\n%s", aaa);
393                         c = strlen(aaa);
394                         aaa[0] = 0;
395                 }
396                 b = strlen(aaa);
397                 aaa[b] = ch;
398                 aaa[b + 1] = 0;
399         }
400         if (ch == 32) {
401                 if ((strlen(aaa) + c) > (width - 5)) {
402                         cprintf("\n");
403                         c = 1;
404                 }
405                 cprintf("%s ", aaa);
406                 ++c;
407                 c = c + strlen(aaa);
408                 strcpy(aaa, "");
409                 goto FMTA;
410         }
411         if ((ch == 13) || (ch == 10)) {
412                 cprintf("%s\n", aaa);
413                 c = 1;
414                 strcpy(aaa, "");
415                 goto FMTA;
416         }
417         goto FMTA;
418
419 FMTEND: cprintf("%s\n", aaa);
420 }
421
422
423
424 /*
425  * Callback function for mime parser that simply lists the part
426  */
427 void list_this_part(char *name, char *filename, char *partnum, char *disp,
428                     void *content, char *cbtype, size_t length)
429 {
430
431         cprintf("part=%s|%s|%s|%s|%s|%d\n",
432                 name, filename, partnum, disp, cbtype, length);
433 }
434
435
436 /*
437  * Callback function for mime parser that wants to display text
438  */
439 void fixed_output(char *name, char *filename, char *partnum, char *disp,
440                   void *content, char *cbtype, size_t length)
441 {
442         char *ptr;
443
444         if (!strcasecmp(cbtype, "multipart/alternative")) {
445                 strcpy(ma->prefix, partnum);
446                 strcat(ma->prefix, ".");
447                 ma->is_ma = 1;
448                 ma->did_print = 0;
449                 return;
450         }
451
452         if ( (!strncasecmp(partnum, ma->prefix, strlen(ma->prefix)))
453            && (ma->is_ma == 1) 
454            && (ma->did_print == 1) ) {
455                 lprintf(9, "Skipping part %s (%s)\n", partnum, cbtype);
456                 return;
457         }
458
459         ma->did_print = 1;
460
461         if (!strcasecmp(cbtype, "text/plain")) {
462                 client_write(content, length);
463         }
464         else if (!strcasecmp(cbtype, "text/html")) {
465                 ptr = html_to_ascii(content, 80, 0);
466                 client_write(ptr, strlen(ptr));
467                 phree(ptr);
468         }
469         else if (strncasecmp(cbtype, "multipart/", 10)) {
470                 cprintf("Part %s: %s (%s) (%d bytes)\n",
471                         partnum, filename, cbtype, length);
472         }
473 }
474
475
476 /*
477  * Callback function for mime parser that opens a section for downloading
478  */
479 void mime_download(char *name, char *filename, char *partnum, char *disp,
480                    void *content, char *cbtype, size_t length)
481 {
482
483         /* Silently go away if there's already a download open... */
484         if (CC->download_fp != NULL)
485                 return;
486
487         /* ...or if this is not the desired section */
488         if (strcasecmp(desired_section, partnum))
489                 return;
490
491         CC->download_fp = tmpfile();
492         if (CC->download_fp == NULL)
493                 return;
494
495         fwrite(content, length, 1, CC->download_fp);
496         fflush(CC->download_fp);
497         rewind(CC->download_fp);
498
499         OpenCmdResult(filename, cbtype);
500 }
501
502
503
504 /*
505  * Load a message from disk into memory.
506  * This is used by output_message() and other fetch functions.
507  *
508  * NOTE: Caller is responsible for freeing the returned CtdlMessage struct
509  *       using the CtdlMessageFree() function.
510  */
511 struct CtdlMessage *CtdlFetchMessage(long msgnum)
512 {
513         struct cdbdata *dmsgtext;
514         struct CtdlMessage *ret = NULL;
515         char *mptr;
516         CIT_UBYTE ch;
517         CIT_UBYTE field_header;
518         size_t field_length;
519
520
521         dmsgtext = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
522         if (dmsgtext == NULL) {
523                 lprintf(9, "CtdlFetchMessage(%ld) failed.\n");
524                 return NULL;
525         }
526         mptr = dmsgtext->ptr;
527
528         /* Parse the three bytes that begin EVERY message on disk.
529          * The first is always 0xFF, the on-disk magic number.
530          * The second is the anonymous/public type byte.
531          * The third is the format type byte (vari, fixed, or MIME).
532          */
533         ch = *mptr++;
534         if (ch != 255) {
535                 lprintf(5, "Message %ld appears to be corrupted.\n", msgnum);
536                 cdb_free(dmsgtext);
537                 return NULL;
538         }
539         ret = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
540         memset(ret, 0, sizeof(struct CtdlMessage));
541
542         ret->cm_magic = CTDLMESSAGE_MAGIC;
543         ret->cm_anon_type = *mptr++;    /* Anon type byte */
544         ret->cm_format_type = *mptr++;  /* Format type byte */
545
546         /*
547          * The rest is zero or more arbitrary fields.  Load them in.
548          * We're done when we encounter either a zero-length field or
549          * have just processed the 'M' (message text) field.
550          */
551         do {
552                 field_length = strlen(mptr);
553                 if (field_length == 0)
554                         break;
555                 field_header = *mptr++;
556                 ret->cm_fields[field_header] = mallok(field_length);
557                 strcpy(ret->cm_fields[field_header], mptr);
558
559                 while (*mptr++ != 0);   /* advance to next field */
560
561         } while ((field_length > 0) && (field_header != 'M'));
562
563         cdb_free(dmsgtext);
564
565         /* Perform "before read" hooks (aborting if any return nonzero) */
566         if (PerformMessageHooks(ret, EVT_BEFOREREAD) > 0) {
567                 CtdlFreeMessage(ret);
568                 return NULL;
569         }
570
571         return (ret);
572 }
573
574
575 /*
576  * Returns 1 if the supplied pointer points to a valid Citadel message.
577  * If the pointer is NULL or the magic number check fails, returns 0.
578  */
579 int is_valid_message(struct CtdlMessage *msg) {
580         if (msg == NULL)
581                 return 0;
582         if ((msg->cm_magic) != CTDLMESSAGE_MAGIC) {
583                 lprintf(3, "is_valid_message() -- self-check failed\n");
584                 return 0;
585         }
586         return 1;
587 }
588
589
590 /*
591  * 'Destructor' for struct CtdlMessage
592  */
593 void CtdlFreeMessage(struct CtdlMessage *msg)
594 {
595         int i;
596
597         if (is_valid_message(msg) == 0) return;
598
599         for (i = 0; i < 256; ++i)
600                 if (msg->cm_fields[i] != NULL)
601                         phree(msg->cm_fields[i]);
602
603         msg->cm_magic = 0;      /* just in case */
604         phree(msg);
605 }
606
607
608
609 /*
610  * Get a message off disk.  (return value is the message's timestamp)
611  * 
612  */
613 void output_message(char *msgid, int mode, int headers_only)
614 {
615         long msg_num;
616         int a, i;
617         char buf[1024];
618         time_t xtime;
619         CIT_UBYTE ch;
620
621         struct CtdlMessage *TheMessage = NULL;
622
623         char *mptr;
624
625         /* buffers needed for RFC822 translation */
626         char suser[256];
627         char luser[256];
628         char snode[256];
629         char lnode[256];
630         char mid[256];
631         /*                                       */
632
633         msg_num = atol(msgid);
634
635         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
636                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
637                 return;
638         }
639
640         /* FIX ... small security issue
641          * We need to check to make sure the requested message is actually
642          * in the current room, and set msg_ok to 1 only if it is.  This
643          * functionality is currently missing because I'm in a hurry to replace
644          * broken production code with nonbroken pre-beta code.  :(   -- ajc
645          *
646          if (!msg_ok) {
647          cprintf("%d Message %ld is not in this room.\n",
648          ERROR, msg_num);
649          return;
650          }
651          */
652
653         /*
654          * Fetch the message from disk
655          */
656         TheMessage = CtdlFetchMessage(msg_num);
657         if (TheMessage == NULL) {
658                 cprintf("%d Can't locate msg %ld on disk\n", ERROR, msg_num);
659                 return;
660         }
661
662         /* Are we downloading a MIME component? */
663         if (mode == MT_DOWNLOAD) {
664                 if (TheMessage->cm_format_type != 4) {
665                         cprintf("%d This is not a MIME message.\n",
666                                 ERROR);
667                 } else if (CC->download_fp != NULL) {
668                         cprintf("%d You already have a download open.\n",
669                                 ERROR);
670                 } else {
671                         /* Parse the message text component */
672                         mptr = TheMessage->cm_fields['M'];
673                         mime_parser(mptr, NULL, *mime_download);
674                         /* If there's no file open by this time, the requested
675                          * section wasn't found, so print an error
676                          */
677                         if (CC->download_fp == NULL) {
678                                 cprintf("%d Section %s not found.\n",
679                                         ERROR + FILE_NOT_FOUND,
680                                         desired_section);
681                         }
682                 }
683                 CtdlFreeMessage(TheMessage);
684                 return;
685         }
686
687         /* now for the user-mode message reading loops */
688         cprintf("%d Message %ld:\n", LISTING_FOLLOWS, msg_num);
689
690         /* Tell the client which format type we're using.  If this is a
691          * MIME message, *lie* about it and tell the user it's fixed-format.
692          */
693         if (mode == MT_CITADEL) {
694                 if (TheMessage->cm_format_type == 4)
695                         cprintf("type=1\n");
696                 else
697                         cprintf("type=%d\n", TheMessage->cm_format_type);
698         }
699
700         /* nhdr=yes means that we're only displaying headers, no body */
701         if ((TheMessage->cm_anon_type == MES_ANON) && (mode == MT_CITADEL)) {
702                 cprintf("nhdr=yes\n");
703         }
704
705         /* begin header processing loop for Citadel message format */
706
707         if ((mode == MT_CITADEL) || (mode == MT_MIME)) {
708
709                 if (TheMessage->cm_fields['P']) {
710                         cprintf("path=%s\n", TheMessage->cm_fields['P']);
711                 }
712                 if (TheMessage->cm_fields['I']) {
713                         cprintf("msgn=%s\n", TheMessage->cm_fields['I']);
714                 }
715                 if (TheMessage->cm_fields['T']) {
716                         cprintf("time=%s\n", TheMessage->cm_fields['T']);
717                 }
718                 if (TheMessage->cm_fields['A']) {
719                         strcpy(buf, TheMessage->cm_fields['A']);
720                         PerformUserHooks(buf, (-1L), EVT_OUTPUTMSG);
721                         if (TheMessage->cm_anon_type == MES_ANON)
722                                 cprintf("from=****");
723                         else if (TheMessage->cm_anon_type == MES_AN2)
724                                 cprintf("from=anonymous");
725                         else
726                                 cprintf("from=%s", buf);
727                         if ((is_room_aide())
728                             && ((TheMessage->cm_anon_type == MES_ANON)
729                              || (TheMessage->cm_anon_type == MES_AN2))) {
730                                 cprintf(" [%s]", buf);
731                         }
732                         cprintf("\n");
733                 }
734                 if (TheMessage->cm_fields['O']) {
735                         cprintf("room=%s\n", TheMessage->cm_fields['O']);
736                 }
737                 if (TheMessage->cm_fields['N']) {
738                         cprintf("node=%s\n", TheMessage->cm_fields['N']);
739                 }
740                 if (TheMessage->cm_fields['H']) {
741                         cprintf("hnod=%s\n", TheMessage->cm_fields['H']);
742                 }
743                 if (TheMessage->cm_fields['R']) {
744                         cprintf("rcpt=%s\n", TheMessage->cm_fields['R']);
745                 }
746                 if (TheMessage->cm_fields['U']) {
747                         cprintf("subj=%s\n", TheMessage->cm_fields['U']);
748                 }
749         }
750
751         /* begin header processing loop for RFC822 transfer format */
752
753         strcpy(suser, "");
754         strcpy(luser, "");
755         strcpy(snode, NODENAME);
756         strcpy(lnode, HUMANNODE);
757         if (mode == MT_RFC822) {
758                 for (i = 0; i < 256; ++i) {
759                         if (TheMessage->cm_fields[i]) {
760                                 mptr = TheMessage->cm_fields[i];
761
762                                 if (i == 'A') {
763                                         strcpy(luser, mptr);
764                                 } else if (i == 'P') {
765                                         cprintf("Path: %s\n", mptr);
766                                         for (a = 0; a < strlen(mptr); ++a) {
767                                                 if (mptr[a] == '!') {
768                                                         strcpy(mptr, &mptr[a + 1]);
769                                                         a = 0;
770                                                 }
771                                         }
772                                         strcpy(suser, mptr);
773                                 } else if (i == 'U')
774                                         cprintf("Subject: %s\n", mptr);
775                                 else if (i == 'I')
776                                         strcpy(mid, mptr);
777                                 else if (i == 'H')
778                                         strcpy(lnode, mptr);
779                                 else if (i == 'O')
780                                         cprintf("X-Citadel-Room: %s\n", mptr);
781                                 else if (i == 'N')
782                                         strcpy(snode, mptr);
783                                 else if (i == 'R')
784                                         cprintf("To: %s\n", mptr);
785                                 else if (i == 'T') {
786                                         xtime = atol(mptr);
787                                         cprintf("Date: %s", asctime(localtime(&xtime)));
788                                 }
789                         }
790                 }
791         }
792
793         if (mode == MT_RFC822) {
794                 if (!strcasecmp(snode, NODENAME)) {
795                         strcpy(snode, FQDN);
796                 }
797                 cprintf("Message-ID: <%s@%s>\n", mid, snode);
798                 PerformUserHooks(luser, (-1L), EVT_OUTPUTMSG);
799                 cprintf("From: %s@%s (%s)\n", suser, snode, luser);
800                 cprintf("Organization: %s\n", lnode);
801         }
802
803         /* end header processing loop ... at this point, we're in the text */
804
805         mptr = TheMessage->cm_fields['M'];
806
807         /* Tell the client about the MIME parts in this message */
808         if (TheMessage->cm_format_type == 4) {  /* legacy textual dump */
809                 if (mode == MT_CITADEL) {
810                         mime_parser(mptr, NULL, *list_this_part);
811                 }
812                 else if (mode == MT_MIME) {     /* list parts only */
813                         mime_parser(mptr, NULL, *list_this_part);
814                         cprintf("000\n");
815                         CtdlFreeMessage(TheMessage);
816                         return;
817                 }
818         }
819
820         if (headers_only) {
821                 cprintf("000\n");
822                 CtdlFreeMessage(TheMessage);
823                 return;
824         }
825
826         /* signify start of msg text */
827         if (mode == MT_CITADEL)
828                 cprintf("text\n");
829         if ((mode == MT_RFC822) && (TheMessage->cm_format_type != 4))
830                 cprintf("\n");
831
832         /* If the format type on disk is 1 (fixed-format), then we want
833          * everything to be output completely literally ... regardless of
834          * what message transfer format is in use.
835          */
836         if (TheMessage->cm_format_type == 1) {
837                 strcpy(buf, "");
838                 while (ch = *mptr++, ch > 0) {
839                         if (ch == 13)
840                                 ch = 10;
841                         if ((ch == 10) || (strlen(buf) > 250)) {
842                                 cprintf("%s\n", buf);
843                                 strcpy(buf, "");
844                         } else {
845                                 buf[strlen(buf) + 1] = 0;
846                                 buf[strlen(buf)] = ch;
847                         }
848                 }
849                 if (strlen(buf) > 0)
850                         cprintf("%s\n", buf);
851         }
852
853         /* If the message on disk is format 0 (Citadel vari-format), we
854          * output using the formatter at 80 columns.  This is the final output
855          * form if the transfer format is RFC822, but if the transfer format
856          * is Citadel proprietary, it'll still work, because the indentation
857          * for new paragraphs is correct and the client will reformat the
858          * message to the reader's screen width.
859          */
860         if (TheMessage->cm_format_type == 0) {
861                 memfmout(80, mptr, 0);
862         }
863
864         /* If the message on disk is format 4 (MIME), we've gotta hand it
865          * off to the MIME parser.  The client has already been told that
866          * this message is format 1 (fixed format), so the callback function
867          * we use will display those parts as-is.
868          */
869         if (TheMessage->cm_format_type == 4) {
870                 CtdlAllocUserData(SYM_MA_INFO, sizeof(struct ma_info));
871                 memset(ma, 0, sizeof(struct ma_info));
872                 mime_parser(mptr, NULL, *fixed_output);
873         }
874
875         /* now we're done */
876         cprintf("000\n");
877         CtdlFreeMessage(TheMessage);
878         return;
879 }
880
881
882
883 /*
884  * display a message (mode 0 - Citadel proprietary)
885  */
886 void cmd_msg0(char *cmdbuf)
887 {
888         char msgid[256];
889         int headers_only = 0;
890
891         extract(msgid, cmdbuf, 0);
892         headers_only = extract_int(cmdbuf, 1);
893
894         output_message(msgid, MT_CITADEL, headers_only);
895         return;
896 }
897
898
899 /*
900  * display a message (mode 2 - RFC822)
901  */
902 void cmd_msg2(char *cmdbuf)
903 {
904         char msgid[256];
905         int headers_only = 0;
906
907         extract(msgid, cmdbuf, 0);
908         headers_only = extract_int(cmdbuf, 1);
909
910         output_message(msgid, MT_RFC822, headers_only);
911 }
912
913
914
915 /* 
916  * display a message (mode 3 - IGnet raw format - internal programs only)
917  */
918 void cmd_msg3(char *cmdbuf)
919 {
920         long msgnum;
921         struct CtdlMessage *msg;
922         struct ser_ret smr;
923
924         if (CC->internal_pgm == 0) {
925                 cprintf("%d This command is for internal programs only.\n",
926                         ERROR);
927                 return;
928         }
929
930         msgnum = extract_long(cmdbuf, 0);
931         msg = CtdlFetchMessage(msgnum);
932         if (msg == NULL) {
933                 cprintf("%d Message %ld not found.\n", 
934                         ERROR, msgnum);
935                 return;
936         }
937
938         serialize_message(&smr, msg);
939         CtdlFreeMessage(msg);
940
941         if (smr.len == 0) {
942                 cprintf("%d Unable to serialize message\n",
943                         ERROR+INTERNAL_ERROR);
944                 return;
945         }
946
947         cprintf("%d %ld\n", BINARY_FOLLOWS, smr.len);
948         client_write(smr.ser, smr.len);
949         phree(smr.ser);
950 }
951
952
953
954 /* 
955  * display a message (mode 4 - MIME) (FIX ... still evolving, not complete)
956  */
957 void cmd_msg4(char *cmdbuf)
958 {
959         char msgid[256];
960
961         extract(msgid, cmdbuf, 0);
962
963         output_message(msgid, MT_MIME, 0);
964 }
965
966 /*
967  * Open a component of a MIME message as a download file 
968  */
969 void cmd_opna(char *cmdbuf)
970 {
971         char msgid[256];
972
973         CtdlAllocUserData(SYM_DESIRED_SECTION, 64);
974
975         extract(msgid, cmdbuf, 0);
976         extract(desired_section, cmdbuf, 1);
977
978         output_message(msgid, MT_DOWNLOAD, 0);
979 }                       
980
981 /*
982  * Message base operation to send a message to the master file
983  * (returns new message number)
984  *
985  * This is the back end for CtdlSaveMsg() and should not be directly
986  * called by server-side modules.
987  *
988  */
989 long send_message(struct CtdlMessage *msg,      /* pointer to buffer */
990                 int generate_id,                /* generate 'I' field? */
991                 FILE *save_a_copy)              /* save a copy to disk? */
992 {
993         long newmsgid;
994         long retval;
995         char msgidbuf[32];
996         struct ser_ret smr;
997
998         /* Get a new message number */
999         newmsgid = get_new_message_number();
1000         sprintf(msgidbuf, "%ld", newmsgid);
1001
1002         if (generate_id) {
1003                 msg->cm_fields['I'] = strdoop(msgidbuf);
1004         }
1005         
1006         serialize_message(&smr, msg);
1007
1008         if (smr.len == 0) {
1009                 cprintf("%d Unable to serialize message\n",
1010                         ERROR+INTERNAL_ERROR);
1011                 return (-1L);
1012         }
1013
1014         /* Write our little bundle of joy into the message base */
1015         begin_critical_section(S_MSGMAIN);
1016         if (cdb_store(CDB_MSGMAIN, &newmsgid, sizeof(long),
1017                       smr.ser, smr.len) < 0) {
1018                 lprintf(2, "Can't store message\n");
1019                 retval = 0L;
1020         } else {
1021                 retval = newmsgid;
1022         }
1023         end_critical_section(S_MSGMAIN);
1024
1025         /* If the caller specified that a copy should be saved to a particular
1026          * file handle, do that now too.
1027          */
1028         if (save_a_copy != NULL) {
1029                 fwrite(smr.ser, smr.len, 1, save_a_copy);
1030         }
1031
1032         /* Free the memory we used for the serialized message */
1033         phree(smr.ser);
1034
1035         /* Return the *local* message ID to the caller
1036          * (even if we're storing an incoming network message)
1037          */
1038         return(retval);
1039 }
1040
1041
1042
1043 /*
1044  * Serialize a struct CtdlMessage into the format used on disk and network.
1045  * 
1046  * This function loads up a "struct ser_ret" (defined in server.h) which
1047  * contains the length of the serialized message and a pointer to the
1048  * serialized message in memory.  THE LATTER MUST BE FREED BY THE CALLER.
1049  */
1050 void serialize_message(struct ser_ret *ret,             /* return values */
1051                         struct CtdlMessage *msg)        /* unserialized msg */
1052 {
1053         size_t wlen;
1054         int i;
1055         static char *forder = FORDER;
1056
1057         lprintf(9, "serialize_message() called\n");
1058
1059         if (is_valid_message(msg) == 0) return;         /* self check */
1060
1061         lprintf(9, "magic number check OK.\n");
1062
1063         ret->len = 3;
1064         for (i=0; i<26; ++i) if (msg->cm_fields[(int)forder[i]] != NULL)
1065                 ret->len = ret->len +
1066                         strlen(msg->cm_fields[(int)forder[i]]) + 2;
1067
1068         lprintf(9, "message is %d bytes\n", ret->len);
1069
1070         lprintf(9, "calling malloc\n");
1071         ret->ser = mallok(ret->len);
1072         if (ret->ser == NULL) {
1073                 ret->len = 0;
1074                 return;
1075         }
1076
1077         ret->ser[0] = 0xFF;
1078         ret->ser[1] = msg->cm_anon_type;
1079         ret->ser[2] = msg->cm_format_type;
1080         wlen = 3;
1081         lprintf(9, "stuff\n");
1082
1083         for (i=0; i<26; ++i) if (msg->cm_fields[(int)forder[i]] != NULL) {
1084                 ret->ser[wlen++] = (char)forder[i];
1085                 strcpy(&ret->ser[wlen], msg->cm_fields[(int)forder[i]]);
1086                 wlen = wlen + strlen(msg->cm_fields[(int)forder[i]]) + 1;
1087         }
1088         if (ret->len != wlen) lprintf(3, "ERROR: len=%d wlen=%d\n",
1089                 ret->len, wlen);
1090
1091         return;
1092 }
1093
1094
1095
1096 /*
1097  * Save a message to disk
1098  */
1099 void CtdlSaveMsg(struct CtdlMessage *msg,       /* message to save */
1100                 char *rec,                      /* Recipient (mail) */
1101                 char *force,                    /* force a particular room? */
1102                 int mailtype,                   /* local or remote type */
1103                 int generate_id)                /* 1 = generate 'I' field */
1104 {
1105         char aaa[100];
1106         char hold_rm[ROOMNAMELEN];
1107         char actual_rm[ROOMNAMELEN];
1108         char force_room[ROOMNAMELEN];
1109         char content_type[256]; /* We have to learn this */
1110         char recipient[256];
1111         long newmsgid;
1112         char *mptr;
1113         struct usersupp userbuf;
1114         int a;
1115         int successful_local_recipients = 0;
1116         struct quickroom qtemp;
1117         struct SuppMsgInfo smi;
1118         FILE *network_fp = NULL;
1119         static int seqnum = 1;
1120
1121         lprintf(9, "CtdlSaveMsg() called\n");
1122         if (is_valid_message(msg) == 0) return;         /* self check */
1123
1124         /* If this message has no timestamp, we take the liberty of
1125          * giving it one, right now.
1126          */
1127         if (msg->cm_fields['T'] == NULL) {
1128                 sprintf(aaa, "%ld", time(NULL));
1129                 msg->cm_fields['T'] = strdoop(aaa);
1130         }
1131
1132         /* If this message has no path, we generate one.
1133          */
1134         if (msg->cm_fields['P'] == NULL) {
1135                 msg->cm_fields['P'] = strdoop(msg->cm_fields['A']);
1136                 for (a=0; a<strlen(msg->cm_fields['P']); ++a) {
1137                         if (isspace(msg->cm_fields['P'][a])) {
1138                                 msg->cm_fields['P'][a] = ' ';
1139                         }
1140                 }
1141         }
1142
1143         strcpy(force_room, force);
1144
1145         /* Strip non-printable characters out of the recipient name */
1146         strcpy(recipient, rec);
1147         for (a = 0; a < strlen(recipient); ++a)
1148                 if (!isprint(recipient[a]))
1149                         strcpy(&recipient[a], &recipient[a + 1]);
1150
1151         /* Learn about what's inside, because it's what's inside that counts */
1152
1153         switch (msg->cm_format_type) {
1154         case 0:
1155                 strcpy(content_type, "text/x-citadel-variformat");
1156                 break;
1157         case 1:
1158                 strcpy(content_type, "text/plain");
1159                 break;
1160         case 4:
1161                 strcpy(content_type, "text/plain");
1162                 /* advance past header fields */
1163                 mptr = msg->cm_fields['M'];
1164                 a = strlen(mptr);
1165                 while (--a) {
1166                         if (!strncasecmp(mptr, "Content-type: ", 14)) {
1167                                 safestrncpy(content_type, mptr,
1168                                             sizeof(content_type));
1169                                 strcpy(content_type, &content_type[14]);
1170                                 for (a = 0; a < strlen(content_type); ++a)
1171                                         if ((content_type[a] == ';')
1172                                             || (content_type[a] == ' ')
1173                                             || (content_type[a] == 13)
1174                                             || (content_type[a] == 10))
1175                                                 content_type[a] = 0;
1176                                 break;
1177                         }
1178                         ++mptr;
1179                 }
1180         }
1181
1182         /* Perform "before save" hooks (aborting if any return nonzero) */
1183         if (PerformMessageHooks(msg, EVT_BEFORESAVE) > 0) return;
1184
1185         /* Network mail - send a copy to the network program. */
1186         if ((strlen(recipient) > 0) && (mailtype != MES_LOCAL)) {
1187                 sprintf(aaa, "./network/spoolin/netmail.%04lx.%04x.%04x",
1188                         (long) getpid(), CC->cs_pid, ++seqnum);
1189                 lprintf(9, "Saving a copy to %s\n", aaa);
1190                 network_fp = fopen(aaa, "ab+");
1191                 if (network_fp == NULL)
1192                         lprintf(2, "ERROR: %s\n", strerror(errno));
1193         }
1194
1195         /* Save it to disk */
1196         newmsgid = send_message(msg, generate_id, network_fp);
1197         if (network_fp != NULL) {
1198                 fclose(network_fp);
1199                 system("exec nohup ./netproc -i >/dev/null 2>&1 &");
1200         }
1201         if (newmsgid <= 0L)
1202                 return;
1203
1204         strcpy(actual_rm, CC->quickroom.QRname);
1205         strcpy(hold_rm, "");
1206
1207         /* If this is being done by the networker delivering a private
1208          * message, we want to BYPASS saving the sender's copy (because there
1209          * is no local sender; it would otherwise go to the Trashcan).
1210          */
1211         if ((!CC->internal_pgm) || (strlen(recipient) == 0)) {
1212                 /* If the user is a twit, move to the twit room for posting */
1213                 if (TWITDETECT)
1214                         if (CC->usersupp.axlevel == 2) {
1215                                 strcpy(hold_rm, actual_rm);
1216                                 strcpy(actual_rm, config.c_twitroom);
1217                         }
1218                 /* ...or if this message is destined for Aide> then go there. */
1219                 if (strlen(force_room) > 0) {
1220                         strcpy(hold_rm, actual_rm);
1221                         strcpy(actual_rm, force_room);
1222                 }
1223                 /* This call to usergoto() changes rooms if necessary.  It also
1224                    * causes the latest message list to be read into memory.
1225                  */
1226                 usergoto(actual_rm, 0);
1227
1228                 /* read in the quickroom record, obtaining a lock... */
1229                 lgetroom(&CC->quickroom, actual_rm);
1230
1231                 /* Fix an obscure bug */
1232                 if (!strcasecmp(CC->quickroom.QRname, AIDEROOM)) {
1233                         CC->quickroom.QRflags =
1234                             CC->quickroom.QRflags & ~QR_MAILBOX;
1235                 }
1236                 /* Add the message pointer to the room */
1237                 CC->quickroom.QRhighest =
1238                     AddMessageToRoom(&CC->quickroom, newmsgid);
1239
1240                 /* update quickroom */
1241                 lputroom(&CC->quickroom);
1242                 ++successful_local_recipients;
1243         }
1244
1245         /* Bump this user's messages posted counter. */
1246         lgetuser(&CC->usersupp, CC->curr_user);
1247         CC->usersupp.posted = CC->usersupp.posted + 1;
1248         lputuser(&CC->usersupp);
1249
1250         /* If this is private, local mail, make a copy in the
1251          * recipient's mailbox and bump the reference count.
1252          */
1253         if ((strlen(recipient) > 0) && (mailtype == MES_LOCAL)) {
1254                 if (getuser(&userbuf, recipient) == 0) {
1255                         MailboxName(actual_rm, &userbuf, MAILROOM);
1256                         if (lgetroom(&qtemp, actual_rm) == 0) {
1257                                 qtemp.QRhighest =
1258                                     AddMessageToRoom(&qtemp, newmsgid);
1259                                 lputroom(&qtemp);
1260                                 ++successful_local_recipients;
1261                         }
1262                 }
1263         }
1264         /* If we've posted in a room other than the current room, then we
1265          * have to now go back to the current room...
1266          */
1267         if (strlen(hold_rm) > 0) {
1268                 usergoto(hold_rm, 0);
1269         }
1270
1271         /* Write a supplemental message info record.  This doesn't have to
1272          * be a critical section because nobody else knows about this message
1273          * yet.
1274          */
1275         memset(&smi, 0, sizeof(struct SuppMsgInfo));
1276         smi.smi_msgnum = newmsgid;
1277         smi.smi_refcount = successful_local_recipients;
1278         safestrncpy(smi.smi_content_type, content_type, 64);
1279         PutSuppMsgInfo(&smi);
1280
1281         /* Perform "after save" hooks */
1282         PerformMessageHooks(msg, EVT_AFTERSAVE);
1283 }
1284
1285
1286
1287 /*
1288  * Convenience function for generating small administrative messages.
1289  */
1290 void quickie_message(char *from, char *to, char *room, char *text)
1291 {
1292         struct CtdlMessage *msg;
1293
1294         msg = mallok(sizeof(struct CtdlMessage));
1295         memset(msg, 0, sizeof(struct CtdlMessage));
1296         msg->cm_magic = CTDLMESSAGE_MAGIC;
1297         msg->cm_anon_type = MES_NORMAL;
1298         msg->cm_format_type = 0;
1299         msg->cm_fields['A'] = strdoop(from);
1300         msg->cm_fields['O'] = strdoop(room);
1301         msg->cm_fields['N'] = strdoop(NODENAME);
1302         if (to != NULL)
1303                 msg->cm_fields['R'] = strdoop(to);
1304         msg->cm_fields['M'] = strdoop(text);
1305
1306         CtdlSaveMsg(msg, "", room, MES_LOCAL, 1);
1307         CtdlFreeMessage(msg);
1308         syslog(LOG_NOTICE, text);
1309 }
1310
1311
1312 /*
1313  * Build a binary message to be saved on disk.
1314  */
1315
1316 struct CtdlMessage *make_message(
1317         struct usersupp *author,        /* author's usersupp structure */
1318         char *recipient,                /* NULL if it's not mail */
1319         char *room,                     /* room where it's going */
1320         int type,                       /* see MES_ types in header file */
1321         int net_type,                   /* see MES_ types in header file */
1322         int format_type,                /* local or remote (see citadel.h) */
1323         char *fake_name)                /* who we're masquerading as */
1324 {
1325
1326         int a;
1327         char dest_node[32];
1328         char buf[256];
1329         size_t message_len = 0;
1330         size_t buffer_len = 0;
1331         char *ptr;
1332         struct CtdlMessage *msg;
1333
1334         msg = mallok(sizeof(struct CtdlMessage));
1335         memset(msg, 0, sizeof(struct CtdlMessage));
1336         msg->cm_magic = CTDLMESSAGE_MAGIC;
1337         msg->cm_anon_type = type;
1338         msg->cm_format_type = format_type;
1339
1340         /* Don't confuse the poor folks if it's not routed mail. */
1341         strcpy(dest_node, "");
1342
1343         /* If net_type is MES_BINARY, split out the destination node. */
1344         if (net_type == MES_BINARY) {
1345                 strcpy(dest_node, NODENAME);
1346                 for (a = 0; a < strlen(recipient); ++a) {
1347                         if (recipient[a] == '@') {
1348                                 recipient[a] = 0;
1349                                 strcpy(dest_node, &recipient[a + 1]);
1350                         }
1351                 }
1352         }
1353
1354         /* if net_type is MES_INTERNET, set the dest node to 'internet' */
1355         if (net_type == MES_INTERNET) {
1356                 strcpy(dest_node, "internet");
1357         }
1358
1359         while (isspace(recipient[strlen(recipient) - 1]))
1360                 recipient[strlen(recipient) - 1] = 0;
1361
1362         sprintf(buf, "cit%ld", author->usernum);                /* Path */
1363         msg->cm_fields['P'] = strdoop(buf);
1364
1365         sprintf(buf, "%ld", time(NULL));                        /* timestamp */
1366         msg->cm_fields['T'] = strdoop(buf);
1367
1368         if (fake_name[0])                                       /* author */
1369                 msg->cm_fields['A'] = strdoop(fake_name);
1370         else
1371                 msg->cm_fields['A'] = strdoop(author->fullname);
1372
1373         if (CC->quickroom.QRflags & QR_MAILBOX)                 /* room */
1374                 msg->cm_fields['O'] = strdoop(&CC->quickroom.QRname[11]);
1375         else
1376                 msg->cm_fields['O'] = strdoop(CC->quickroom.QRname);
1377
1378         msg->cm_fields['N'] = strdoop(NODENAME);                /* nodename */
1379         msg->cm_fields['H'] = strdoop(HUMANNODE);               /* hnodename */
1380
1381         if (recipient[0] != 0)
1382                 msg->cm_fields['R'] = strdoop(recipient);
1383         if (dest_node[0] != 0)
1384                 msg->cm_fields['D'] = strdoop(dest_node);
1385
1386         msg->cm_fields['M'] = mallok(4096);
1387         if (msg->cm_fields['M'] == NULL) {
1388                 while (client_gets(buf), strcmp(buf, "000")) ;; /* flush */
1389                 return(msg);
1390         } else {
1391                 buffer_len = 4096;
1392                 msg->cm_fields['M'][0] = 0;
1393                 message_len = 0;
1394         }
1395
1396         /* read in the lines of message text one by one */
1397         while (client_gets(buf), strcmp(buf, "000")) {
1398
1399                 /* augment the buffer if we have to */
1400                 if ((message_len + strlen(buf) + 2) > buffer_len) {
1401                         ptr = reallok(msg->cm_fields['M'], (buffer_len * 2) );
1402                         if (ptr == NULL) {      /* flush if can't allocate */
1403                                 while (client_gets(buf), strcmp(buf, "000")) ;;
1404                                 return(msg);
1405                         } else {
1406                                 buffer_len = (buffer_len * 2);
1407                                 msg->cm_fields['M'] = ptr;
1408                         }
1409                 }
1410
1411                 strcat(msg->cm_fields['M'], buf);
1412                 strcat(msg->cm_fields['M'], "\n");
1413
1414                 /* if we've hit the max msg length, flush the rest */
1415                 if (message_len >= config.c_maxmsglen) {
1416                         while (client_gets(buf), strcmp(buf, "000")) ;;
1417                         return(msg);
1418                 }
1419         }
1420
1421         return(msg);
1422 }
1423
1424
1425
1426
1427
1428 /*
1429  * message entry  -  mode 0 (normal)
1430  */
1431 void cmd_ent0(char *entargs)
1432 {
1433         int post = 0;
1434         char recipient[256];
1435         int anon_flag = 0;
1436         int format_type = 0;
1437         char newusername[256];
1438         struct CtdlMessage *msg;
1439         int a, b;
1440         int e = 0;
1441         int mtsflag = 0;
1442         struct usersupp tempUS;
1443         char buf[256];
1444
1445         post = extract_int(entargs, 0);
1446         extract(recipient, entargs, 1);
1447         anon_flag = extract_int(entargs, 2);
1448         format_type = extract_int(entargs, 3);
1449
1450         /* first check to make sure the request is valid. */
1451
1452         if (!(CC->logged_in)) {
1453                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1454                 return;
1455         }
1456         if ((CC->usersupp.axlevel < 2) && ((CC->quickroom.QRflags & QR_MAILBOX) == 0)) {
1457                 cprintf("%d Need to be validated to enter ",
1458                         ERROR + HIGHER_ACCESS_REQUIRED);
1459                 cprintf("(except in %s> to sysop)\n", MAILROOM);
1460                 return;
1461         }
1462         if ((CC->usersupp.axlevel < 4) && (CC->quickroom.QRflags & QR_NETWORK)) {
1463                 cprintf("%d Need net privileges to enter here.\n",
1464                         ERROR + HIGHER_ACCESS_REQUIRED);
1465                 return;
1466         }
1467         if ((CC->usersupp.axlevel < 6) && (CC->quickroom.QRflags & QR_READONLY)) {
1468                 cprintf("%d Sorry, this is a read-only room.\n",
1469                         ERROR + HIGHER_ACCESS_REQUIRED);
1470                 return;
1471         }
1472         mtsflag = 0;
1473
1474
1475         if (post == 2) {
1476                 if (CC->usersupp.axlevel < 6) {
1477                         cprintf("%d You don't have permission to masquerade.\n",
1478                                 ERROR + HIGHER_ACCESS_REQUIRED);
1479                         return;
1480                 }
1481                 extract(newusername, entargs, 4);
1482                 memset(CC->fake_postname, 0, 32);
1483                 strcpy(CC->fake_postname, newusername);
1484                 cprintf("%d Ok\n", OK);
1485                 return;
1486         }
1487         CC->cs_flags |= CS_POSTING;
1488
1489         buf[0] = 0;
1490         if (CC->quickroom.QRflags & QR_MAILBOX) {
1491                 if (CC->usersupp.axlevel >= 2) {
1492                         strcpy(buf, recipient);
1493                 } else
1494                         strcpy(buf, "sysop");
1495                 e = alias(buf); /* alias and mail type */
1496                 if ((buf[0] == 0) || (e == MES_ERROR)) {
1497                         cprintf("%d Unknown address - cannot send message.\n",
1498                                 ERROR + NO_SUCH_USER);
1499                         return;
1500                 }
1501                 if ((e != MES_LOCAL) && (CC->usersupp.axlevel < 4)) {
1502                         cprintf("%d Net privileges required for network mail.\n",
1503                                 ERROR + HIGHER_ACCESS_REQUIRED);
1504                         return;
1505                 }
1506                 if ((RESTRICT_INTERNET == 1) && (e == MES_INTERNET)
1507                     && ((CC->usersupp.flags & US_INTERNET) == 0)
1508                     && (!CC->internal_pgm)) {
1509                         cprintf("%d You don't have access to Internet mail.\n",
1510                                 ERROR + HIGHER_ACCESS_REQUIRED);
1511                         return;
1512                 }
1513                 if (!strcasecmp(buf, "sysop")) {
1514                         mtsflag = 1;
1515                         goto SKFALL;
1516                 }
1517                 if (e != MES_LOCAL)
1518                         goto SKFALL;    /* don't search local file  */
1519                 if (!strcasecmp(buf, CC->usersupp.fullname)) {
1520                         cprintf("%d Can't send mail to yourself!\n",
1521                                 ERROR + NO_SUCH_USER);
1522                         return;
1523                 }
1524                 /* Check to make sure the user exists; also get the correct
1525                  * upper/lower casing of the name. 
1526                  */
1527                 a = getuser(&tempUS, buf);
1528                 if (a != 0) {
1529                         cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1530                         return;
1531                 }
1532                 strcpy(buf, tempUS.fullname);
1533         }
1534
1535 SKFALL: b = MES_NORMAL;
1536         if (CC->quickroom.QRflags & QR_ANONONLY)
1537                 b = MES_ANON;
1538         if (CC->quickroom.QRflags & QR_ANONOPT) {
1539                 if (anon_flag == 1)
1540                         b = MES_AN2;
1541         }
1542         if ((CC->quickroom.QRflags & QR_MAILBOX) == 0)
1543                 buf[0] = 0;
1544
1545         /* If we're only checking the validity of the request, return
1546          * success without creating the message.
1547          */
1548         if (post == 0) {
1549                 cprintf("%d %s\n", OK, buf);
1550                 return;
1551         }
1552
1553         cprintf("%d send message\n", SEND_LISTING);
1554
1555         /* Read in the message from the client. */
1556         if (CC->fake_postname[0])
1557                 msg = make_message(&CC->usersupp, buf,
1558                         CC->quickroom.QRname, b, e, format_type,
1559                         CC->fake_postname);
1560         else if (CC->fake_username[0])
1561                 msg = make_message(&CC->usersupp, buf,
1562                         CC->quickroom.QRname, b, e, format_type,
1563                         CC->fake_username);
1564         else
1565                 msg = make_message(&CC->usersupp, buf,
1566                         CC->quickroom.QRname, b, e, format_type, "");
1567
1568         if (msg != NULL)
1569                 CtdlSaveMsg(msg, buf, (mtsflag ? AIDEROOM : ""), e, 1);
1570                 CtdlFreeMessage(msg);
1571         CC->fake_postname[0] = '\0';
1572         return;
1573 }
1574
1575
1576
1577 /* 
1578  * message entry - mode 3 (raw)
1579  */
1580 void cmd_ent3(char *entargs)
1581 {
1582         char recp[256];
1583         int a;
1584         int e = 0;
1585         unsigned char ch, which_field;
1586         struct usersupp tempUS;
1587         long msglen;
1588         struct CtdlMessage *msg;
1589         char *tempbuf;
1590
1591         if (CC->internal_pgm == 0) {
1592                 cprintf("%d This command is for internal programs only.\n",
1593                         ERROR);
1594                 return;
1595         }
1596
1597         /* See if there's a recipient, but make sure it's a real one */
1598         extract(recp, entargs, 1);
1599         for (a = 0; a < strlen(recp); ++a)
1600                 if (!isprint(recp[a]))
1601                         strcpy(&recp[a], &recp[a + 1]);
1602         while (isspace(recp[0]))
1603                 strcpy(recp, &recp[1]);
1604         while (isspace(recp[strlen(recp) - 1]))
1605                 recp[strlen(recp) - 1] = 0;
1606
1607         /* If we're in Mail, check the recipient */
1608         if (strlen(recp) > 0) {
1609                 e = alias(recp);        /* alias and mail type */
1610                 if ((recp[0] == 0) || (e == MES_ERROR)) {
1611                         cprintf("%d Unknown address - cannot send message.\n",
1612                                 ERROR + NO_SUCH_USER);
1613                         return;
1614                 }
1615                 if (e == MES_LOCAL) {
1616                         a = getuser(&tempUS, recp);
1617                         if (a != 0) {
1618                                 cprintf("%d No such user.\n",
1619                                         ERROR + NO_SUCH_USER);
1620                                 return;
1621                         }
1622                 }
1623         }
1624
1625         /* At this point, message has been approved. */
1626         if (extract_int(entargs, 0) == 0) {
1627                 cprintf("%d OK to send\n", OK);
1628                 return;
1629         }
1630
1631         msglen = extract_long(entargs, 2);
1632         msg = mallok(sizeof(struct CtdlMessage));
1633         if (msg == NULL) {
1634                 cprintf("%d Out of memory\n", ERROR+INTERNAL_ERROR);
1635                 return;
1636         }
1637
1638         memset(msg, 0, sizeof(struct CtdlMessage));
1639         tempbuf = mallok(msglen);
1640         if (tempbuf == NULL) {
1641                 cprintf("%d Out of memory\n", ERROR+INTERNAL_ERROR);
1642                 phree(msg);
1643                 return;
1644         }
1645
1646         cprintf("%d %ld\n", SEND_BINARY, msglen);
1647
1648         client_read(&ch, 1);                            /* 0xFF magic number */
1649         msg->cm_magic = CTDLMESSAGE_MAGIC;
1650         client_read(&ch, 1);                            /* anon type */
1651         msg->cm_anon_type = ch;
1652         client_read(&ch, 1);                            /* format type */
1653         msg->cm_format_type = ch;
1654         msglen = msglen - 3;
1655
1656         while (msglen > 0) {
1657                 client_read(&which_field, 1);
1658                 --msglen;
1659                 tempbuf[0] = 0;
1660                 do {
1661                         client_read(&ch, 1);
1662                         --msglen;
1663                         a = strlen(tempbuf);
1664                         tempbuf[a+1] = 0;
1665                         tempbuf[a] = ch;
1666                 } while ( (ch != 0) && (msglen > 0) );
1667                 msg->cm_fields[which_field] = strdoop(tempbuf);
1668         }
1669
1670         CtdlSaveMsg(msg, recp, "", e, 0);
1671         CtdlFreeMessage(msg);
1672         phree(tempbuf);
1673 }
1674
1675
1676 /*
1677  * API function to delete messages which match a set of criteria
1678  * (returns the actual number of messages deleted)
1679  */
1680 int CtdlDeleteMessages(char *room_name,         /* which room */
1681                        long dmsgnum,            /* or "0" for any */
1682                        char *content_type       /* or NULL for any */
1683 )
1684 {
1685
1686         struct quickroom qrbuf;
1687         struct cdbdata *cdbfr;
1688         long *msglist = NULL;
1689         int num_msgs = 0;
1690         int i;
1691         int num_deleted = 0;
1692         int delete_this;
1693         struct SuppMsgInfo smi;
1694
1695         lprintf(9, "CtdlDeleteMessages(%s, %ld, %s)\n",
1696                 room_name, dmsgnum, content_type);
1697
1698         /* get room record, obtaining a lock... */
1699         if (lgetroom(&qrbuf, room_name) != 0) {
1700                 lprintf(7, "CtdlDeleteMessages(): Room <%s> not found\n",
1701                         room_name);
1702                 return (0);     /* room not found */
1703         }
1704         cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
1705
1706         if (cdbfr != NULL) {
1707                 msglist = mallok(cdbfr->len);
1708                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1709                 num_msgs = cdbfr->len / sizeof(long);
1710                 cdb_free(cdbfr);
1711         }
1712         if (num_msgs > 0) {
1713                 for (i = 0; i < num_msgs; ++i) {
1714                         delete_this = 0x00;
1715
1716                         /* Set/clear a bit for each criterion */
1717
1718                         if ((dmsgnum == 0L) || (msglist[i] == dmsgnum)) {
1719                                 delete_this |= 0x01;
1720                         }
1721                         if (content_type == NULL) {
1722                                 delete_this |= 0x02;
1723                         } else {
1724                                 GetSuppMsgInfo(&smi, msglist[i]);
1725                                 if (!strcasecmp(smi.smi_content_type,
1726                                                 content_type)) {
1727                                         delete_this |= 0x02;
1728                                 }
1729                         }
1730
1731                         /* Delete message only if all bits are set */
1732                         if (delete_this == 0x03) {
1733                                 AdjRefCount(msglist[i], -1);
1734                                 msglist[i] = 0L;
1735                                 ++num_deleted;
1736                         }
1737                 }
1738
1739                 num_msgs = sort_msglist(msglist, num_msgs);
1740                 cdb_store(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long),
1741                           msglist, (num_msgs * sizeof(long)));
1742
1743                 qrbuf.QRhighest = msglist[num_msgs - 1];
1744                 phree(msglist);
1745         }
1746         lputroom(&qrbuf);
1747         lprintf(9, "%d message(s) deleted.\n", num_deleted);
1748         return (num_deleted);
1749 }
1750
1751
1752
1753 /*
1754  * Delete message from current room
1755  */
1756 void cmd_dele(char *delstr)
1757 {
1758         long delnum;
1759         int num_deleted;
1760
1761         getuser(&CC->usersupp, CC->curr_user);
1762         if ((CC->usersupp.axlevel < 6)
1763             && (CC->usersupp.usernum != CC->quickroom.QRroomaide)
1764             && ((CC->quickroom.QRflags & QR_MAILBOX) == 0)) {
1765                 cprintf("%d Higher access required.\n",
1766                         ERROR + HIGHER_ACCESS_REQUIRED);
1767                 return;
1768         }
1769         delnum = extract_long(delstr, 0);
1770
1771         num_deleted = CtdlDeleteMessages(CC->quickroom.QRname, delnum, NULL);
1772
1773         if (num_deleted) {
1774                 cprintf("%d %d message%s deleted.\n", OK,
1775                         num_deleted, ((num_deleted != 1) ? "s" : ""));
1776         } else {
1777                 cprintf("%d Message %ld not found.\n", ERROR, delnum);
1778         }
1779 }
1780
1781
1782 /*
1783  * move a message to another room
1784  */
1785 void cmd_move(char *args)
1786 {
1787         long num;
1788         char targ[32];
1789         struct quickroom qtemp;
1790         int foundit;
1791
1792         num = extract_long(args, 0);
1793         extract(targ, args, 1);
1794
1795         getuser(&CC->usersupp, CC->curr_user);
1796         if ((CC->usersupp.axlevel < 6)
1797             && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
1798                 cprintf("%d Higher access required.\n",
1799                         ERROR + HIGHER_ACCESS_REQUIRED);
1800                 return;
1801         }
1802         if (getroom(&qtemp, targ) != 0) {
1803                 cprintf("%d '%s' does not exist.\n", ERROR, targ);
1804                 return;
1805         }
1806         /* Bump the reference count, otherwise the message will be deleted
1807          * from disk when we remove it from the source room.
1808          */
1809         AdjRefCount(num, 1);
1810
1811         /* yank the message out of the current room... */
1812         foundit = CtdlDeleteMessages(CC->quickroom.QRname, num, NULL);
1813
1814         if (foundit) {
1815                 /* put the message into the target room */
1816                 lgetroom(&qtemp, targ);
1817                 qtemp.QRhighest = AddMessageToRoom(&qtemp, num);
1818                 lputroom(&qtemp);
1819                 cprintf("%d Message moved.\n", OK);
1820         } else {
1821                 AdjRefCount(num, (-1));         /* oops */
1822                 cprintf("%d msg %ld does not exist.\n", ERROR, num);
1823         }
1824 }
1825
1826
1827
1828 /*
1829  * GetSuppMsgInfo()  -  Get the supplementary record for a message
1830  */
1831 void GetSuppMsgInfo(struct SuppMsgInfo *smibuf, long msgnum)
1832 {
1833
1834         struct cdbdata *cdbsmi;
1835         long TheIndex;
1836
1837         memset(smibuf, 0, sizeof(struct SuppMsgInfo));
1838         smibuf->smi_msgnum = msgnum;
1839         smibuf->smi_refcount = 1;       /* Default reference count is 1 */
1840
1841         /* Use the negative of the message number for its supp record index */
1842         TheIndex = (0L - msgnum);
1843
1844         cdbsmi = cdb_fetch(CDB_MSGMAIN, &TheIndex, sizeof(long));
1845         if (cdbsmi == NULL) {
1846                 return;         /* record not found; go with defaults */
1847         }
1848         memcpy(smibuf, cdbsmi->ptr,
1849                ((cdbsmi->len > sizeof(struct SuppMsgInfo)) ?
1850                 sizeof(struct SuppMsgInfo) : cdbsmi->len));
1851         cdb_free(cdbsmi);
1852         return;
1853 }
1854
1855
1856 /*
1857  * PutSuppMsgInfo()  -  (re)write supplementary record for a message
1858  */
1859 void PutSuppMsgInfo(struct SuppMsgInfo *smibuf)
1860 {
1861         long TheIndex;
1862
1863         /* Use the negative of the message number for its supp record index */
1864         TheIndex = (0L - smibuf->smi_msgnum);
1865
1866         lprintf(9, "PuttSuppMsgInfo(%ld) - ref count is %d\n",
1867                 smibuf->smi_msgnum, smibuf->smi_refcount);
1868
1869         cdb_store(CDB_MSGMAIN,
1870                   &TheIndex, sizeof(long),
1871                   smibuf, sizeof(struct SuppMsgInfo));
1872
1873 }
1874
1875 /*
1876  * AdjRefCount  -  change the reference count for a message;
1877  *                 delete the message if it reaches zero
1878  */
1879 void AdjRefCount(long msgnum, int incr)
1880 {
1881
1882         struct SuppMsgInfo smi;
1883         long delnum;
1884
1885         /* This is a *tight* critical section; please keep it that way, as
1886          * it may get called while nested in other critical sections.  
1887          * Complicating this any further will surely cause deadlock!
1888          */
1889         begin_critical_section(S_SUPPMSGMAIN);
1890         GetSuppMsgInfo(&smi, msgnum);
1891         smi.smi_refcount += incr;
1892         PutSuppMsgInfo(&smi);
1893         end_critical_section(S_SUPPMSGMAIN);
1894
1895         lprintf(9, "Ref count for message <%ld> after write is <%d>\n",
1896                 msgnum, smi.smi_refcount);
1897
1898         /* If the reference count is now zero, delete the message
1899          * (and its supplementary record as well).
1900          */
1901         if (smi.smi_refcount == 0) {
1902                 lprintf(9, "Deleting message <%ld>\n", msgnum);
1903                 delnum = msgnum;
1904                 cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));
1905                 delnum = (0L - msgnum);
1906                 cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));
1907         }
1908 }
1909
1910 /*
1911  * Write a generic object to this room
1912  *
1913  * Note: this could be much more efficient.  Right now we use two temporary
1914  * files, and still pull the message into memory as with all others.
1915  */
1916 void CtdlWriteObject(char *req_room,            /* Room to stuff it in */
1917                         char *content_type,     /* MIME type of this object */
1918                         char *tempfilename,     /* Where to fetch it from */
1919                         struct usersupp *is_mailbox,    /* Mailbox room? */
1920                         int is_binary,          /* Is encoding necessary? */
1921                         int is_unique           /* Del others of this type? */
1922                         )
1923 {
1924
1925         FILE *fp, *tempfp;
1926         char filename[PATH_MAX];
1927         char cmdbuf[256];
1928         int ch;
1929         struct quickroom qrbuf;
1930         char roomname[ROOMNAMELEN];
1931         struct CtdlMessage *msg;
1932         size_t len;
1933
1934         if (is_mailbox != NULL)
1935                 MailboxName(roomname, is_mailbox, req_room);
1936         else
1937                 safestrncpy(roomname, req_room, sizeof(roomname));
1938         lprintf(9, "CtdlWriteObject() to <%s>\n", roomname);
1939
1940         strcpy(filename, tmpnam(NULL));
1941         fp = fopen(filename, "w");
1942         if (fp == NULL)
1943                 return;
1944
1945         tempfp = fopen(tempfilename, "r");
1946         if (tempfp == NULL) {
1947                 fclose(fp);
1948                 unlink(filename);
1949                 return;
1950         }
1951
1952         fprintf(fp, "Content-type: %s\n", content_type);
1953         lprintf(9, "Content-type: %s\n", content_type);
1954
1955         if (is_binary == 0) {
1956                 fprintf(fp, "Content-transfer-encoding: 7bit\n\n");
1957                 while (ch = getc(tempfp), ch > 0)
1958                         putc(ch, fp);
1959                 fclose(tempfp);
1960                 putc(0, fp);
1961                 fclose(fp);
1962         } else {
1963                 fprintf(fp, "Content-transfer-encoding: base64\n\n");
1964                 fclose(tempfp);
1965                 fclose(fp);
1966                 sprintf(cmdbuf, "./base64 -e <%s >>%s",
1967                         tempfilename, filename);
1968                 system(cmdbuf);
1969         }
1970
1971         lprintf(9, "Allocating\n");
1972         msg = mallok(sizeof(struct CtdlMessage));
1973         memset(msg, 0, sizeof(struct CtdlMessage));
1974         msg->cm_magic = CTDLMESSAGE_MAGIC;
1975         msg->cm_anon_type = MES_NORMAL;
1976         msg->cm_format_type = 4;
1977         msg->cm_fields['A'] = strdoop(CC->usersupp.fullname);
1978         msg->cm_fields['O'] = strdoop(req_room);
1979         msg->cm_fields['N'] = strdoop(config.c_nodename);
1980         msg->cm_fields['H'] = strdoop(config.c_humannode);
1981         
1982         lprintf(9, "Loading\n");
1983         fp = fopen(filename, "rb");
1984         fseek(fp, 0L, SEEK_END);
1985         len = ftell(fp);
1986         rewind(fp);
1987         msg->cm_fields['M'] = mallok(len);
1988         fread(msg->cm_fields['M'], len, 1, fp);
1989         fclose(fp);
1990         unlink(filename);
1991
1992         /* Create the requested room if we have to. */
1993         if (getroom(&qrbuf, roomname) != 0) {
1994                 create_room(roomname, 4, "", 0);
1995         }
1996         /* If the caller specified this object as unique, delete all
1997          * other objects of this type that are currently in the room.
1998          */
1999         if (is_unique) {
2000                 lprintf(9, "Deleted %d other msgs of this type\n",
2001                         CtdlDeleteMessages(roomname, 0L, content_type));
2002         }
2003         /* Now write the data */
2004         CtdlSaveMsg(msg, "", roomname, MES_LOCAL, 1);
2005         CtdlFreeMessage(msg);
2006 }