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