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