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