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