cosmetic
[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  * cmd_msgs()  -  get list of message #'s in this room
186  */
187 void cmd_msgs(char *cmdbuf)
188 {
189         int a = 0;
190         int mode = 0;
191         char which[256];
192         int cm_howmany = 0;
193         long cm_gt = 0L;
194         struct visit vbuf;
195
196         extract(which, cmdbuf, 0);
197
198         mode = MSGS_ALL;
199         strcat(which, "   ");
200         if (!strncasecmp(which, "OLD", 3))
201                 mode = MSGS_OLD;
202         if (!strncasecmp(which, "NEW", 3))
203                 mode = MSGS_NEW;
204         if (!strncasecmp(which, "FIRST", 5)) {
205                 mode = MSGS_FIRST;
206                 cm_howmany = extract_int(cmdbuf, 1);
207         }
208         if (!strncasecmp(which, "LAST", 4)) {
209                 mode = MSGS_LAST;
210                 cm_howmany = extract_int(cmdbuf, 1);
211         }
212         if (!strncasecmp(which, "GT", 2)) {
213                 mode = MSGS_GT;
214                 cm_gt = extract_long(cmdbuf, 1);
215         }
216         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
217                 cprintf("%d not logged in\n", ERROR + NOT_LOGGED_IN);
218                 return;
219         }
220         get_mm();
221         get_msglist(&CC->quickroom);
222         getuser(&CC->usersupp, CC->curr_user);
223         CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
224
225         cprintf("%d Message list...\n", LISTING_FOLLOWS);
226         if (CC->num_msgs != 0) {
227                 for (a = 0; a < (CC->num_msgs); ++a)
228                         if ((MessageFromList(a) >= 0)
229                             && (
230
231                                        (mode == MSGS_ALL)
232                                        || ((mode == MSGS_OLD) && (MessageFromList(a) <= vbuf.v_lastseen))
233                                        || ((mode == MSGS_NEW) && (MessageFromList(a) > vbuf.v_lastseen))
234                                        || ((mode == MSGS_NEW) && (MessageFromList(a) >= vbuf.v_lastseen)
235                                     && (CC->usersupp.flags & US_LASTOLD))
236                                        || ((mode == MSGS_LAST) && (a >= (CC->num_msgs - cm_howmany)))
237                             || ((mode == MSGS_FIRST) && (a < cm_howmany))
238                                        || ((mode == MSGS_GT) && (MessageFromList(a) > cm_gt))
239                             )
240                             ) {
241                                 cprintf("%ld\n", MessageFromList(a));
242                         }
243         }
244         cprintf("000\n");
245 }
246
247
248
249 /* 
250  * help_subst()  -  support routine for help file viewer
251  */
252 void help_subst(char *strbuf, char *source, char *dest)
253 {
254         char workbuf[256];
255         int p;
256
257         while (p = pattern2(strbuf, source), (p >= 0)) {
258                 strcpy(workbuf, &strbuf[p + strlen(source)]);
259                 strcpy(&strbuf[p], dest);
260                 strcat(strbuf, workbuf);
261         }
262 }
263
264
265 void do_help_subst(char *buffer)
266 {
267         char buf2[16];
268
269         help_subst(buffer, "^nodename", config.c_nodename);
270         help_subst(buffer, "^humannode", config.c_humannode);
271         help_subst(buffer, "^fqdn", config.c_fqdn);
272         help_subst(buffer, "^username", CC->usersupp.fullname);
273         sprintf(buf2, "%ld", CC->usersupp.usernum);
274         help_subst(buffer, "^usernum", buf2);
275         help_subst(buffer, "^sysadm", config.c_sysadm);
276         help_subst(buffer, "^variantname", CITADEL);
277         sprintf(buf2, "%d", config.c_maxsessions);
278         help_subst(buffer, "^maxsessions", buf2);
279 }
280
281
282
283 /*
284  * memfmout()  -  Citadel text formatter and paginator.
285  *             Although the original purpose of this routine was to format
286  *             text to the reader's screen width, all we're really using it
287  *             for here is to format text out to 80 columns before sending it
288  *             to the client.  The client software may reformat it again.
289  */
290 void memfmout(int width, char *mptr, char subst)
291                         /* screen width to use */
292                         /* where are we going to get our text from? */
293                         /* nonzero if we should use hypertext mode */
294 {
295         int a, b, c;
296         int real = 0;
297         int old = 0;
298         CIT_UBYTE ch;
299         char aaa[140];
300         char buffer[256];
301
302         strcpy(aaa, "");
303         old = 255;
304         strcpy(buffer, "");
305         c = 1;                  /* c is the current pos */
306
307 FMTA:   if (subst) {
308                 while (ch = *mptr, ((ch != 0) && (strlen(buffer) < 126))) {
309                         ch = *mptr++;
310                         buffer[strlen(buffer) + 1] = 0;
311                         buffer[strlen(buffer)] = ch;
312                 }
313
314                 if (buffer[0] == '^')
315                         do_help_subst(buffer);
316
317                 buffer[strlen(buffer) + 1] = 0;
318                 a = buffer[0];
319                 strcpy(buffer, &buffer[1]);
320         } else
321                 ch = *mptr++;
322
323         old = real;
324         real = ch;
325         if (ch <= 0)
326                 goto FMTEND;
327
328         if (((ch == 13) || (ch == 10)) && (old != 13) && (old != 10))
329                 ch = 32;
330         if (((old == 13) || (old == 10)) && (isspace(real))) {
331                 cprintf("\n");
332                 c = 1;
333         }
334         if (ch > 126)
335                 goto FMTA;
336
337         if (ch > 32) {
338                 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
339                         cprintf("\n%s", aaa);
340                         c = strlen(aaa);
341                         aaa[0] = 0;
342                 }
343                 b = strlen(aaa);
344                 aaa[b] = ch;
345                 aaa[b + 1] = 0;
346         }
347         if (ch == 32) {
348                 if ((strlen(aaa) + c) > (width - 5)) {
349                         cprintf("\n");
350                         c = 1;
351                 }
352                 cprintf("%s ", aaa);
353                 ++c;
354                 c = c + strlen(aaa);
355                 strcpy(aaa, "");
356                 goto FMTA;
357         }
358         if ((ch == 13) || (ch == 10)) {
359                 cprintf("%s\n", aaa);
360                 c = 1;
361                 strcpy(aaa, "");
362                 goto FMTA;
363         }
364         goto FMTA;
365
366 FMTEND: cprintf("%s\n", aaa);
367 }
368
369
370
371 /*
372  * Callback function for mime parser that simply lists the part
373  */
374 void list_this_part(char *name, char *filename, char *partnum, char *disp,
375                     void *content, char *cbtype, size_t length)
376 {
377
378         cprintf("part=%s|%s|%s|%s|%s|%d\n",
379                 name, filename, partnum, disp, cbtype, length);
380 }
381
382
383 /*
384  * Callback function for mime parser that wants to display text
385  */
386 void fixed_output(char *name, char *filename, char *partnum, char *disp,
387                   void *content, char *cbtype, size_t length)
388 {
389
390         if (!strcasecmp(cbtype, "text/plain")) {
391                 client_write(content, length);
392         } else {
393                 cprintf("Part %s: %s (%s) (%d bytes)\n",
394                         partnum, filename, cbtype, length);
395         }
396 }
397
398
399 /*
400  * Callback function for mime parser that opens a section for downloading
401  */
402 void mime_download(char *name, char *filename, char *partnum, char *disp,
403                    void *content, char *cbtype, size_t length)
404 {
405
406         /* Silently go away if there's already a download open... */
407         if (CC->download_fp != NULL)
408                 return;
409
410         /* ...or if this is not the desired section */
411         if (strcasecmp(desired_section, partnum))
412                 return;
413
414         CC->download_fp = tmpfile();
415         if (CC->download_fp == NULL)
416                 return;
417
418         fwrite(content, length, 1, CC->download_fp);
419         fflush(CC->download_fp);
420         rewind(CC->download_fp);
421
422         OpenCmdResult(filename, cbtype);
423 }
424
425
426
427 /*
428  * Get a message off disk.  (return value is the message's timestamp)
429  * 
430  */
431 time_t output_message(char *msgid, int mode, int headers_only)
432 {
433         long msg_num;
434         int a;
435         CIT_UBYTE ch, rch;
436         CIT_UBYTE format_type, anon_flag;
437         char buf[1024];
438         long msg_len;
439         int msg_ok = 0;
440
441         struct cdbdata *dmsgtext;
442         char *mptr;
443
444         /* buffers needed for RFC822 translation */
445         char suser[256];
446         char luser[256];
447         char snode[256];
448         char lnode[256];
449         char mid[256];
450         time_t xtime = 0L;
451         /*                                       */
452
453         msg_num = atol(msgid);
454
455
456         if ((!(CC->logged_in)) && (!(CC->internal_pgm)) && (mode != MT_DATE)) {
457                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
458                 return (xtime);
459         }
460         /* We used to need to check in the current room's message list
461          * to determine where the message's disk position.  We no longer need
462          * to do this, but we do it anyway as a security measure, in order to
463          * prevent rogue clients from reading messages not in the current room.
464          */
465
466         msg_ok = 0;
467         if (CC->num_msgs > 0) {
468                 for (a = 0; a < CC->num_msgs; ++a) {
469                         if (MessageFromList(a) == msg_num) {
470                                 msg_ok = 1;
471                         }
472                 }
473         }
474         if (!msg_ok) {
475                 if (mode != MT_DATE)
476                         cprintf("%d Message %ld is not in this room.\n",
477                                 ERROR, msg_num);
478                 return (xtime);
479         }
480         dmsgtext = cdb_fetch(CDB_MSGMAIN, &msg_num, sizeof(long));
481
482         if (dmsgtext == NULL) {
483                 if (mode != MT_DATE)
484                         cprintf("%d Can't find message %ld\n",
485                                 ERROR + INTERNAL_ERROR);
486                 return (xtime);
487         }
488         msg_len = (long) dmsgtext->len;
489         mptr = dmsgtext->ptr;
490
491         /* this loop spews out the whole message if we're doing raw format */
492         if (mode == MT_RAW) {
493                 cprintf("%d %ld\n", BINARY_FOLLOWS, msg_len);
494                 client_write(dmsgtext->ptr, (int) msg_len);
495                 cdb_free(dmsgtext);
496                 return (xtime);
497         }
498         /* Otherwise, we'll start parsing it field by field... */
499         ch = *mptr++;
500         if (ch != 255) {
501                 cprintf("%d Illegal message format on disk\n",
502                         ERROR + INTERNAL_ERROR);
503                 cdb_free(dmsgtext);
504                 return (xtime);
505         }
506         anon_flag = *mptr++;
507         format_type = *mptr++;
508
509         /* Are we downloading a MIME component? */
510         if (mode == MT_DOWNLOAD) {
511                 if (format_type != 4) {
512                         cprintf("%d This is not a MIME message.\n",
513                                 ERROR);
514                 } else if (CC->download_fp != NULL) {
515                         cprintf("%d You already have a download open.\n",
516                                 ERROR);
517                 } else {
518                         /* Skip to the message body */
519                         while (ch = *mptr++, (ch != 'M' && ch != 0)) {
520                                 buf[0] = 0;
521                                 do {
522                                         buf[strlen(buf) + 1] = 0;
523                                         rch = *mptr++;
524                                         buf[strlen(buf)] = rch;
525                                 } while (rch > 0);
526                         }
527                         /* Now parse it */
528                         mime_parser(mptr, NULL, *mime_download);
529                         /* If there's no file open by this time, the requested
530                          * section wasn't found, so print an error
531                          */
532                         if (CC->download_fp == NULL) {
533                                 cprintf("%d Section %s not found.\n",
534                                         ERROR + FILE_NOT_FOUND,
535                                         desired_section);
536                         }
537                 }
538                 cdb_free(dmsgtext);
539                 return (xtime);
540         }
541         /* Are we just looking for the message date? */
542         if (mode == MT_DATE)
543                 while (ch = *mptr++, (ch != 'M' && ch != 0)) {
544                         buf[0] = 0;
545                         do {
546                                 buf[strlen(buf) + 1] = 0;
547                                 rch = *mptr++;
548                                 buf[strlen(buf)] = rch;
549                         } while (rch > 0);
550
551                         if (ch == 'T') {
552                                 xtime = atol(buf);
553                                 cdb_free(dmsgtext);
554                                 return (xtime);
555                         }
556                 }
557         /* now for the user-mode message reading loops */
558         cprintf("%d Message %ld:\n", LISTING_FOLLOWS, msg_num);
559
560         if (mode == MT_CITADEL)
561                 cprintf("type=%d\n", format_type);
562
563         if ((anon_flag == MES_ANON) && (mode == MT_CITADEL)) {
564                 cprintf("nhdr=yes\n");
565         }
566         /* begin header processing loop for Citadel message format */
567
568         if ((mode == MT_CITADEL) || (mode == MT_MIME))
569                 while (ch = *mptr++, (ch != 'M' && ch != 0)) {
570                         buf[0] = 0;
571                         do {
572                                 buf[strlen(buf) + 1] = 0;
573                                 rch = *mptr++;
574                                 buf[strlen(buf)] = rch;
575                         } while (rch > 0);
576
577                         if (ch == 'A') {
578                                 PerformUserHooks(buf, (-1L), EVT_OUTPUTMSG);
579                                 if (anon_flag == MES_ANON)
580                                         cprintf("from=****");
581                                 else if (anon_flag == MES_AN2)
582                                         cprintf("from=anonymous");
583                                 else
584                                         cprintf("from=%s", buf);
585                                 if ((is_room_aide()) && ((anon_flag == MES_ANON)
586                                               || (anon_flag == MES_AN2)))
587                                         cprintf(" [%s]", buf);
588                                 cprintf("\n");
589                         } else if (ch == 'P')
590                                 cprintf("path=%s\n", buf);
591                         else if (ch == 'U')
592                                 cprintf("subj=%s\n", buf);
593                         else if (ch == 'I')
594                                 cprintf("msgn=%s\n", buf);
595                         else if (ch == 'H')
596                                 cprintf("hnod=%s\n", buf);
597                         else if (ch == 'O')
598                                 cprintf("room=%s\n", buf);
599                         else if (ch == 'N')
600                                 cprintf("node=%s\n", buf);
601                         else if (ch == 'R')
602                                 cprintf("rcpt=%s\n", buf);
603                         else if (ch == 'T')
604                                 cprintf("time=%s\n", buf);
605                         /* else cprintf("fld%c=%s\n",ch,buf); */
606                 }
607         /* begin header processing loop for RFC822 transfer format */
608
609         strcpy(suser, "");
610         strcpy(luser, "");
611         strcpy(snode, NODENAME);
612         strcpy(lnode, HUMANNODE);
613         if (mode == MT_RFC822)
614                 while (ch = *mptr++, (ch != 'M' && ch != 0)) {
615                         buf[0] = 0;
616                         do {
617                                 buf[strlen(buf) + 1] = 0;
618                                 rch = *mptr++;
619                                 buf[strlen(buf)] = rch;
620                         } while (rch > 0);
621
622                         if (ch == 'A')
623                                 strcpy(luser, buf);
624                         else if (ch == 'P') {
625                                 cprintf("Path: %s\n", buf);
626                                 for (a = 0; a < strlen(buf); ++a) {
627                                         if (buf[a] == '!') {
628                                                 strcpy(buf, &buf[a + 1]);
629                                                 a = 0;
630                                         }
631                                 }
632                                 strcpy(suser, buf);
633                         } else if (ch == 'U')
634                                 cprintf("Subject: %s\n", buf);
635                         else if (ch == 'I')
636                                 strcpy(mid, buf);
637                         else if (ch == 'H')
638                                 strcpy(lnode, buf);
639                         else if (ch == 'O')
640                                 cprintf("X-Citadel-Room: %s\n", buf);
641                         else if (ch == 'N')
642                                 strcpy(snode, buf);
643                         else if (ch == 'R')
644                                 cprintf("To: %s\n", buf);
645                         else if (ch == 'T') {
646                                 xtime = atol(buf);
647                                 cprintf("Date: %s", asctime(localtime(&xtime)));
648                         }
649                 }
650         if (mode == MT_RFC822) {
651                 if (!strcasecmp(snode, NODENAME)) {
652                         strcpy(snode, FQDN);
653                 }
654                 cprintf("Message-ID: <%s@%s>\n", mid, snode);
655                 PerformUserHooks(luser, (-1L), EVT_OUTPUTMSG);
656                 cprintf("From: %s@%s (%s)\n",
657                         suser, snode, luser);
658                 cprintf("Organization: %s\n", lnode);
659         }
660         /* end header processing loop ... at this point, we're in the text */
661
662         if (ch == 0) {
663                 cprintf("text\n*** ?Message truncated\n000\n");
664                 cdb_free(dmsgtext);
665                 return (xtime);
666         }
667         /* do some sort of MIME output */
668         if (format_type == 4) {
669                 if ((mode == MT_CITADEL) || (mode == MT_MIME)) {
670                         mime_parser(mptr, NULL, *list_this_part);
671                 }
672                 if (mode == MT_MIME) {  /* If MT_MIME then it's parts only */
673                         cprintf("000\n");
674                         cdb_free(dmsgtext);
675                         return (xtime);
676                 }
677         }
678         if (headers_only) {
679                 /* give 'em a length */
680                 msg_len = 0L;
681                 while (ch = *mptr++, ch > 0) {
682                         ++msg_len;
683                 }
684                 cprintf("mlen=%ld\n", msg_len);
685                 cprintf("000\n");
686                 cdb_free(dmsgtext);
687                 return (xtime);
688         }
689         /* signify start of msg text */
690         if (mode == MT_CITADEL)
691                 cprintf("text\n");
692         if ((mode == MT_RFC822) && (format_type != 4))
693                 cprintf("\n");
694
695         /* If the format type on disk is 1 (fixed-format), then we want
696          * everything to be output completely literally ... regardless of
697          * what message transfer format is in use.
698          */
699         if (format_type == 1) {
700                 strcpy(buf, "");
701                 while (ch = *mptr++, ch > 0) {
702                         if (ch == 13)
703                                 ch = 10;
704                         if ((ch == 10) || (strlen(buf) > 250)) {
705                                 cprintf("%s\n", buf);
706                                 strcpy(buf, "");
707                         } else {
708                                 buf[strlen(buf) + 1] = 0;
709                                 buf[strlen(buf)] = ch;
710                         }
711                 }
712                 if (strlen(buf) > 0)
713                         cprintf("%s\n", buf);
714         }
715         /* If the message on disk is format 0 (Citadel vari-format), we
716          * output using the formatter at 80 columns.  This is the final output
717          * form if the transfer format is RFC822, but if the transfer format
718          * is Citadel proprietary, it'll still work, because the indentation
719          * for new paragraphs is correct and the client will reformat the
720          * message to the reader's screen width.
721          */
722         if (format_type == 0) {
723                 memfmout(80, mptr, 0);
724         }
725         /* If the message on disk is format 4 (MIME), we've gotta hand it
726          * off to the MIME parser.  The client has already been told that
727          * this message is format 1 (fixed format), so the callback function
728          * we use will display those parts as-is.
729          */
730         if (format_type == 4) {
731                 mime_parser(mptr, NULL, *fixed_output);
732         }
733         /* now we're done */
734         cprintf("000\n");
735         cdb_free(dmsgtext);
736         return (xtime);
737 }
738
739
740 /*
741  * display a message (mode 0 - Citadel proprietary)
742  */
743 void cmd_msg0(char *cmdbuf)
744 {
745         char msgid[256];
746         int headers_only = 0;
747
748         extract(msgid, cmdbuf, 0);
749         headers_only = extract_int(cmdbuf, 1);
750
751         output_message(msgid, MT_CITADEL, headers_only);
752         return;
753 }
754
755
756 /*
757  * display a message (mode 2 - RFC822)
758  */
759 void cmd_msg2(char *cmdbuf)
760 {
761         char msgid[256];
762         int headers_only = 0;
763
764         extract(msgid, cmdbuf, 0);
765         headers_only = extract_int(cmdbuf, 1);
766
767         output_message(msgid, MT_RFC822, headers_only);
768 }
769
770 /* 
771  * display a message (mode 3 - IGnet raw format - internal programs only)
772  */
773 void cmd_msg3(char *cmdbuf)
774 {
775         char msgid[256];
776         int headers_only = 0;
777
778         if (CC->internal_pgm == 0) {
779                 cprintf("%d This command is for internal programs only.\n",
780                         ERROR);
781                 return;
782         }
783         extract(msgid, cmdbuf, 0);
784         headers_only = extract_int(cmdbuf, 1);
785
786         output_message(msgid, MT_RAW, headers_only);
787 }
788
789 /* 
790  * display a message (mode 4 - MIME) (FIX ... still evolving, not complete)
791  */
792 void cmd_msg4(char *cmdbuf)
793 {
794         char msgid[256];
795
796         extract(msgid, cmdbuf, 0);
797
798         output_message(msgid, MT_MIME, 0);
799 }
800
801
802
803 /*
804  * Open a component of a MIME message as a download file 
805  */
806 void cmd_opna(char *cmdbuf)
807 {
808         char msgid[256];
809
810         CtdlAllocUserData(SYM_DESIRED_SECTION, 64);
811
812         extract(msgid, cmdbuf, 0);
813         extract(desired_section, cmdbuf, 1);
814
815         output_message(msgid, MT_DOWNLOAD, 0);
816 }
817
818
819
820 /*
821  * Message base operation to send a message to the master file
822  * (returns new message number)
823  */
824 long send_message(char *message_in_memory,      /* pointer to buffer */
825                   size_t message_length,        /* length of buffer */
826                   int generate_id)
827 {                               /* 1 to generate an I field */
828
829         long newmsgid;
830         char *actual_message;
831         size_t actual_length;
832         long retval;
833         char msgidbuf[32];
834
835         /* Get a new message number */
836         newmsgid = get_new_message_number();
837
838         if (generate_id) {
839                 sprintf(msgidbuf, "I%ld", newmsgid);
840                 actual_length = message_length + strlen(msgidbuf) + 1;
841                 actual_message = mallok(actual_length);
842                 memcpy(actual_message, message_in_memory, 3);
843                 memcpy(&actual_message[3], msgidbuf, (strlen(msgidbuf) + 1));
844                 memcpy(&actual_message[strlen(msgidbuf) + 4],
845                        &message_in_memory[3], message_length - 3);
846         } else {
847                 actual_message = message_in_memory;
848                 actual_length = message_length;
849         }
850
851         /* Write our little bundle of joy into the message base */
852         begin_critical_section(S_MSGMAIN);
853         if (cdb_store(CDB_MSGMAIN, &newmsgid, sizeof(long),
854                       actual_message, actual_length) < 0) {
855                 lprintf(2, "Can't store message\n");
856                 retval = 0L;
857         } else {
858                 retval = newmsgid;
859         }
860         end_critical_section(S_MSGMAIN);
861
862         if (generate_id) {
863                 phree(actual_message);
864         }
865         /* Finally, return the pointers */
866         return (retval);
867 }
868
869
870
871 /*
872  * this is a simple file copy routine.
873  */
874 void copy_file(char *from, char *to)
875 {
876         FILE *ffp, *tfp;
877         int a;
878
879         ffp = fopen(from, "r");
880         if (ffp == NULL)
881                 return;
882         tfp = fopen(to, "w");
883         if (tfp == NULL) {
884                 fclose(ffp);
885                 return;
886         }
887         while (a = getc(ffp), a >= 0) {
888                 putc(a, tfp);
889         }
890         fclose(ffp);
891         fclose(tfp);
892         return;
893 }
894
895
896
897 /*
898  * message base operation to save a message and install its pointers
899  */
900 void save_message(char *mtmp,   /* file containing proper message */
901                   char *rec,    /* Recipient (if mail) */
902                   char *force,  /* if non-zero length, force a room */
903                   int mailtype, /* local or remote type, see citadel.h */
904                   int generate_id)
905 {                               /* set to 1 to generate an 'I' field */
906         char aaa[100];
907         char hold_rm[ROOMNAMELEN];
908         char actual_rm[ROOMNAMELEN];
909         char force_room[ROOMNAMELEN];
910         char recipient[256];
911         long newmsgid;
912         char *message_in_memory;
913         struct stat statbuf;
914         size_t templen;
915         FILE *fp;
916         struct usersupp userbuf;
917         int a;
918         static int seqnum = 0;
919         int successful_local_recipients;
920         struct quickroom qtemp;
921
922         lprintf(9, "save_message(%s,%s,%s,%d,%d)\n",
923                 mtmp, rec, force, mailtype, generate_id);
924
925         strcpy(force_room, force);
926
927         /* Strip non-printable characters out of the recipient name */
928         strcpy(recipient, rec);
929         for (a = 0; a < strlen(recipient); ++a)
930                 if (!isprint(recipient[a]))
931                         strcpy(&recipient[a], &recipient[a + 1]);
932
933         /* Measure the message */
934         stat(mtmp, &statbuf);
935         templen = statbuf.st_size;
936
937         /* Now read it into memory */
938         message_in_memory = (char *) mallok(templen);
939         if (message_in_memory == NULL) {
940                 lprintf(2, "Can't allocate memory to save message!\n");
941                 return;
942         }
943         fp = fopen(mtmp, "rb");
944         fread(message_in_memory, templen, 1, fp);
945         fclose(fp);
946
947         newmsgid = send_message(message_in_memory, templen, generate_id);
948         phree(message_in_memory);
949         if (newmsgid <= 0L)
950                 return;
951
952         strcpy(actual_rm, CC->quickroom.QRname);
953         strcpy(hold_rm, "");
954
955         /* If the user is a twit, move to the twit room for posting... */
956         if (TWITDETECT)
957                 if (CC->usersupp.axlevel == 2) {
958                         strcpy(hold_rm, actual_rm);
959                         strcpy(actual_rm, config.c_twitroom);
960                 }
961         /* ...or if this message is destined for Aide> then go there. */
962         lprintf(9, "actual room forcing loop\n");
963         if (strlen(force_room) > 0) {
964                 strcpy(hold_rm, actual_rm);
965                 strcpy(actual_rm, force_room);
966         }
967         /* This call to usergoto() changes rooms if necessary.  It also
968          * causes the latest message list to be read into memory.
969          */
970         usergoto(actual_rm, 0);
971
972         /* read in the quickroom record, obtaining a lock... */
973         lgetroom(&CC->quickroom, actual_rm);
974
975         /* Fix an obscure bug */
976         if (!strcasecmp(CC->quickroom.QRname, AIDEROOM)) {
977                 CC->quickroom.QRflags = CC->quickroom.QRflags & ~QR_MAILBOX;
978         }
979         /* Add the message pointer to the room */
980         CC->quickroom.QRhighest = AddMessageToRoom(&CC->quickroom, newmsgid);
981
982         /* update quickroom */
983         lputroom(&CC->quickroom);
984
985         /* Network mail - send a copy to the network program. */
986         if ((strlen(recipient) > 0) && (mailtype != MES_LOCAL)) {
987                 sprintf(aaa, "./network/spoolin/netmail.%04lx.%04x.%04x",
988                         (long) getpid(), CC->cs_pid, ++seqnum);
989                 copy_file(mtmp, aaa);
990                 system("exec nohup ./netproc -i >/dev/null 2>&1 &");
991         }
992         /* Bump this user's messages posted counter. */
993         lgetuser(&CC->usersupp, CC->curr_user);
994         CC->usersupp.posted = CC->usersupp.posted + 1;
995         lputuser(&CC->usersupp);
996
997         /* If this is private, local mail, make a copy in the
998          * recipient's mailbox and bump the reference count.
999          */
1000         successful_local_recipients = 0;
1001         if ((strlen(recipient) > 0) && (mailtype == MES_LOCAL)) {
1002                 if (getuser(&userbuf, recipient) == 0) {
1003                         MailboxName(actual_rm, &userbuf, MAILROOM);
1004                         lprintf(9, "Targeting mailbox: <%s>\n", actual_rm);
1005                         if (lgetroom(&qtemp, actual_rm) == 0) {
1006                                 qtemp.QRhighest =
1007                                         AddMessageToRoom(&qtemp, newmsgid);
1008                                 lputroom(&qtemp);
1009                                 ++successful_local_recipients;
1010                         }
1011                 }
1012         }
1013         /* If we've posted in a room other than the current room, then we
1014          * have to now go back to the current room...
1015          */
1016         if (strlen(hold_rm) > 0) {
1017                 usergoto(hold_rm, 0);
1018         }
1019         unlink(mtmp);           /* delete the temporary file */
1020
1021         /* If the message was delivered to more than one location locally,
1022          * we have to bump the reference count accordingly.
1023          */
1024         if (successful_local_recipients != 0) {
1025                 AdjRefCount(newmsgid, successful_local_recipients);
1026         }
1027 }
1028
1029
1030 /*
1031  * Generate an administrative message and post it in the Aide> room.
1032  */
1033 void aide_message(char *text)
1034 {
1035         FILE *fp;
1036
1037         fp = fopen(CC->temp, "wb");
1038         fprintf(fp, "%c%c%c", 255, MES_NORMAL, 0);
1039         fprintf(fp, "Psysop%c", 0);
1040         fprintf(fp, "T%ld%c", (long) time(NULL), 0);
1041         fprintf(fp, "ACitadel%c", 0);
1042         fprintf(fp, "OAide%c", 0);
1043         fprintf(fp, "N%s%c", NODENAME, 0);
1044         fprintf(fp, "M%s\n%c", text, 0);
1045         fclose(fp);
1046         save_message(CC->temp, "", AIDEROOM, MES_LOCAL, 1);
1047         syslog(LOG_NOTICE, text);
1048 }
1049
1050
1051
1052 /*
1053  * Build a binary message to be saved on disk.
1054  */
1055 void make_message(
1056                          char *filename,        /* temporary file name */
1057                          struct usersupp *author,       /* author's usersupp structure */
1058                          char *recipient,       /* NULL if it's not mail */
1059                          char *room,    /* room where it's going */
1060                          int type,      /* see MES_ types in header file */
1061                          int net_type,  /* see MES_ types in header file */
1062                          int format_type,       /* local or remote (see citadel.h) */
1063                          char *fake_name)
1064 {                               /* who we're masquerading as */
1065
1066         FILE *fp;
1067         int a;
1068         time_t now;
1069         char dest_node[32];
1070         char buf[256];
1071
1072         /* Don't confuse the poor folks if it's not routed mail. */
1073         strcpy(dest_node, "");
1074
1075
1076         /* If net_type is MES_BINARY, split out the destination node. */
1077         if (net_type == MES_BINARY) {
1078                 strcpy(dest_node, NODENAME);
1079                 for (a = 0; a < strlen(recipient); ++a) {
1080                         if (recipient[a] == '@') {
1081                                 recipient[a] = 0;
1082                                 strcpy(dest_node, &recipient[a + 1]);
1083                         }
1084                 }
1085         }
1086         /* if net_type is MES_INTERNET, set the dest node to 'internet' */
1087         if (net_type == MES_INTERNET) {
1088                 strcpy(dest_node, "internet");
1089         }
1090         while (isspace(recipient[strlen(recipient) - 1]))
1091                 recipient[strlen(recipient) - 1] = 0;
1092
1093         time(&now);
1094         fp = fopen(filename, "w");
1095         putc(255, fp);
1096         putc(type, fp);         /* Normal or anonymous, see MES_ flags */
1097         putc(format_type, fp);  /* Formatted or unformatted */
1098         fprintf(fp, "Pcit%ld%c", author->usernum, 0);   /* path */
1099         fprintf(fp, "T%ld%c", (long) now, 0);   /* date/time */
1100         if (fake_name[0])
1101                 fprintf(fp, "A%s%c", fake_name, 0);
1102         else
1103                 fprintf(fp, "A%s%c", author->fullname, 0);      /* author */
1104
1105         if (CC->quickroom.QRflags & QR_MAILBOX) {       /* room */
1106                 fprintf(fp, "O%s%c", &CC->quickroom.QRname[11], 0);
1107         } else {
1108                 fprintf(fp, "O%s%c", CC->quickroom.QRname, 0);
1109         }
1110
1111         fprintf(fp, "N%s%c", NODENAME, 0);      /* nodename */
1112         fprintf(fp, "H%s%c", HUMANNODE, 0);     /* human nodename */
1113
1114         if (recipient[0] != 0)
1115                 fprintf(fp, "R%s%c", recipient, 0);
1116         if (dest_node[0] != 0)
1117                 fprintf(fp, "D%s%c", dest_node, 0);
1118
1119         putc('M', fp);
1120
1121         while (client_gets(buf), strcmp(buf, "000")) {
1122                 fprintf(fp, "%s\n", buf);
1123         }
1124         putc(0, fp);
1125         fclose(fp);
1126 }
1127
1128
1129
1130
1131
1132 /*
1133  * message entry  -  mode 0 (normal) <bc>
1134  */
1135 void cmd_ent0(char *entargs)
1136 {
1137         int post = 0;
1138         char recipient[256];
1139         int anon_flag = 0;
1140         int format_type = 0;
1141         char newusername[256];  /* <bc> */
1142
1143         int a, b;
1144         int e = 0;
1145         int mtsflag = 0;
1146         struct usersupp tempUS;
1147         char buf[256];
1148
1149         post = extract_int(entargs, 0);
1150         extract(recipient, entargs, 1);
1151         anon_flag = extract_int(entargs, 2);
1152         format_type = extract_int(entargs, 3);
1153
1154         /* first check to make sure the request is valid. */
1155
1156         if (!(CC->logged_in)) {
1157                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1158                 return;
1159         }
1160         if ((CC->usersupp.axlevel < 2) && ((CC->quickroom.QRflags & QR_MAILBOX) == 0)) {
1161                 cprintf("%d Need to be validated to enter ",
1162                         ERROR + HIGHER_ACCESS_REQUIRED);
1163                 cprintf("(except in %s> to sysop)\n", MAILROOM);
1164                 return;
1165         }
1166         if ((CC->usersupp.axlevel < 4) && (CC->quickroom.QRflags & QR_NETWORK)) {
1167                 cprintf("%d Need net privileges to enter here.\n",
1168                         ERROR + HIGHER_ACCESS_REQUIRED);
1169                 return;
1170         }
1171         if ((CC->usersupp.axlevel < 6) && (CC->quickroom.QRflags & QR_READONLY)) {
1172                 cprintf("%d Sorry, this is a read-only room.\n",
1173                         ERROR + HIGHER_ACCESS_REQUIRED);
1174                 return;
1175         }
1176         mtsflag = 0;
1177
1178
1179         if (post == 2) {        /* <bc> */
1180                 if (CC->usersupp.axlevel < 6) {
1181                         cprintf("%d You don't have permission to do an aide post.\n",
1182                                 ERROR + HIGHER_ACCESS_REQUIRED);
1183                         return;
1184                 }
1185                 extract(newusername, entargs, 4);
1186                 memset(CC->fake_postname, 0, 32);
1187                 strcpy(CC->fake_postname, newusername);
1188                 cprintf("%d Ok\n", OK);
1189                 return;
1190         }
1191         CC->cs_flags |= CS_POSTING;
1192
1193         buf[0] = 0;
1194         if (CC->quickroom.QRflags & QR_MAILBOX) {
1195                 if (CC->usersupp.axlevel >= 2) {
1196                         strcpy(buf, recipient);
1197                 } else
1198                         strcpy(buf, "sysop");
1199                 lprintf(9, "calling alias()\n");
1200                 e = alias(buf); /* alias and mail type */
1201                 lprintf(9, "alias() returned %d\n", e);
1202                 if ((buf[0] == 0) || (e == MES_ERROR)) {
1203                         cprintf("%d Unknown address - cannot send message.\n",
1204                                 ERROR + NO_SUCH_USER);
1205                         return;
1206                 }
1207                 if ((e != MES_LOCAL) && (CC->usersupp.axlevel < 4)) {
1208                         cprintf("%d Net privileges required for network mail.\n",
1209                                 ERROR + HIGHER_ACCESS_REQUIRED);
1210                         return;
1211                 }
1212                 if ((RESTRICT_INTERNET == 1) && (e == MES_INTERNET)
1213                     && ((CC->usersupp.flags & US_INTERNET) == 0)
1214                     && (!CC->internal_pgm)) {
1215                         cprintf("%d You don't have access to Internet mail.\n",
1216                                 ERROR + HIGHER_ACCESS_REQUIRED);
1217                         return;
1218                 }
1219                 if (!strcasecmp(buf, "sysop")) {
1220                         mtsflag = 1;
1221                         goto SKFALL;
1222                 }
1223                 if (e != MES_LOCAL)
1224                         goto SKFALL;    /* don't search local file  */
1225                 if (!strcasecmp(buf, CC->usersupp.fullname)) {
1226                         cprintf("%d Can't send mail to yourself!\n",
1227                                 ERROR + NO_SUCH_USER);
1228                         return;
1229                 }
1230                 /* Check to make sure the user exists; also get the correct
1231                    * upper/lower casing of the name. 
1232                  */
1233                 a = getuser(&tempUS, buf);
1234                 if (a != 0) {
1235                         cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1236                         return;
1237                 }
1238                 strcpy(buf, tempUS.fullname);
1239         }
1240       SKFALL:b = MES_NORMAL;
1241         if (CC->quickroom.QRflags & QR_ANONONLY)
1242                 b = MES_ANON;
1243         if (CC->quickroom.QRflags & QR_ANONOPT) {
1244                 if (anon_flag == 1)
1245                         b = MES_AN2;
1246         }
1247         if ((CC->quickroom.QRflags & QR_MAILBOX) == 0)
1248                 buf[0] = 0;
1249
1250         /* If we're only checking the validity of the request, return
1251          * success without creating the message.
1252          */
1253         if (post == 0) {
1254                 cprintf("%d %s\n", OK, buf);
1255                 return;
1256         }
1257         cprintf("%d send message\n", SEND_LISTING);
1258         if (CC->fake_postname[0])
1259                 make_message(CC->temp, &CC->usersupp, buf, CC->quickroom.QRname, b, e, format_type, CC->fake_postname);
1260         else if (CC->fake_username[0])
1261                 make_message(CC->temp, &CC->usersupp, buf, CC->quickroom.QRname, b, e, format_type, CC->fake_username);
1262         else
1263                 make_message(CC->temp, &CC->usersupp, buf, CC->quickroom.QRname, b, e, format_type, "");
1264         save_message(CC->temp, buf, (mtsflag ? AIDEROOM : ""), e, 1);
1265         CC->fake_postname[0] = '\0';
1266         return;
1267 }
1268
1269
1270
1271 /* 
1272  * message entry - mode 3 (raw)
1273  */
1274 void cmd_ent3(char *entargs)
1275 {
1276         char recp[256];
1277         char buf[256];
1278         int a;
1279         int e = 0;
1280         struct usersupp tempUS;
1281         long msglen;
1282         long bloklen;
1283         FILE *fp;
1284
1285         if (CC->internal_pgm == 0) {
1286                 cprintf("%d This command is for internal programs only.\n",
1287                         ERROR);
1288                 return;
1289         }
1290         /* See if there's a recipient, but make sure it's a real one */
1291         extract(recp, entargs, 1);
1292         for (a = 0; a < strlen(recp); ++a)
1293                 if (!isprint(recp[a]))
1294                         strcpy(&recp[a], &recp[a + 1]);
1295         while (isspace(recp[0]))
1296                 strcpy(recp, &recp[1]);
1297         while (isspace(recp[strlen(recp) - 1]))
1298                 recp[strlen(recp) - 1] = 0;
1299
1300         /* If we're in Mail, check the recipient */
1301         if (strlen(recp) > 0) {
1302                 e = alias(recp);        /* alias and mail type */
1303                 if ((recp[0] == 0) || (e == MES_ERROR)) {
1304                         cprintf("%d Unknown address - cannot send message.\n",
1305                                 ERROR + NO_SUCH_USER);
1306                         return;
1307                 }
1308                 if (e == MES_LOCAL) {
1309                         a = getuser(&tempUS, recp);
1310                         if (a != 0) {
1311                                 cprintf("%d No such user.\n",
1312                                         ERROR + NO_SUCH_USER);
1313                                 return;
1314                         }
1315                 }
1316         }
1317         /* At this point, message has been approved. */
1318         if (extract_int(entargs, 0) == 0) {
1319                 cprintf("%d OK to send\n", OK);
1320                 return;
1321         }
1322         /* open a temp file to hold the message */
1323         fp = fopen(CC->temp, "wb");
1324         if (fp == NULL) {
1325                 cprintf("%d Cannot open %s: %s\n",
1326                         ERROR + INTERNAL_ERROR,
1327                         CC->temp, strerror(errno));
1328                 return;
1329         }
1330         msglen = extract_long(entargs, 2);
1331         cprintf("%d %ld\n", SEND_BINARY, msglen);
1332         while (msglen > 0L) {
1333                 bloklen = ((msglen >= 255L) ? 255 : msglen);
1334                 client_read(buf, (int) bloklen);
1335                 fwrite(buf, (int) bloklen, 1, fp);
1336                 msglen = msglen - bloklen;
1337         }
1338         fclose(fp);
1339
1340         save_message(CC->temp, recp, "", e, 0);
1341 }
1342
1343
1344 /*
1345  * Delete message from current room
1346  */
1347 void cmd_dele(char *delstr)
1348 {
1349         long delnum;
1350         int a, ok;
1351
1352         getuser(&CC->usersupp, CC->curr_user);
1353         if ((CC->usersupp.axlevel < 6)
1354             && (CC->usersupp.usernum != CC->quickroom.QRroomaide)
1355             && ((CC->quickroom.QRflags & QR_MAILBOX) == 0)) {
1356                 cprintf("%d Higher access required.\n",
1357                         ERROR + HIGHER_ACCESS_REQUIRED);
1358                 return;
1359         }
1360         delnum = extract_long(delstr, 0);
1361
1362         /* get room records, obtaining a lock... */
1363         lgetroom(&CC->quickroom, CC->quickroom.QRname);
1364         get_msglist(&CC->quickroom);
1365
1366         ok = 0;
1367         if (CC->num_msgs > 0)
1368                 for (a = 0; a < (CC->num_msgs); ++a) {
1369                         if (MessageFromList(a) == delnum) {
1370                                 SetMessageInList(a, 0L);
1371                                 ok = 1;
1372                         }
1373                 }
1374         CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
1375         CC->quickroom.QRhighest = MessageFromList(CC->num_msgs - 1);
1376
1377         put_msglist(&CC->quickroom);
1378         lputroom(&CC->quickroom);
1379         if (ok == 1) {
1380                 AdjRefCount(delnum, -1);
1381                 cprintf("%d Message deleted.\n", OK);
1382         } else
1383                 cprintf("%d No message %ld.\n", ERROR, delnum);
1384 }
1385
1386
1387 /*
1388  * move a message to another room
1389  */
1390 void cmd_move(char *args)
1391 {
1392         long num;
1393         char targ[32];
1394         int a;
1395         struct quickroom qtemp;
1396         int foundit;
1397
1398         num = extract_long(args, 0);
1399         extract(targ, args, 1);
1400
1401         getuser(&CC->usersupp, CC->curr_user);
1402         if ((CC->usersupp.axlevel < 6)
1403             && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
1404                 cprintf("%d Higher access required.\n",
1405                         ERROR + HIGHER_ACCESS_REQUIRED);
1406                 return;
1407         }
1408         if (getroom(&qtemp, targ) != 0) {
1409                 cprintf("%d '%s' does not exist.\n", ERROR, targ);
1410                 return;
1411         }
1412         /* yank the message out of the current room... */
1413         lgetroom(&CC->quickroom, CC->quickroom.QRname);
1414         get_msglist(&CC->quickroom);
1415
1416         foundit = 0;
1417         for (a = 0; a < (CC->num_msgs); ++a) {
1418                 if (MessageFromList(a) == num) {
1419                         foundit = 1;
1420                         SetMessageInList(a, 0L);
1421                 }
1422         }
1423         if (foundit) {
1424                 CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
1425                 put_msglist(&CC->quickroom);
1426                 CC->quickroom.QRhighest = MessageFromList((CC->num_msgs) - 1);
1427         }
1428         lputroom(&CC->quickroom);
1429         if (!foundit) {
1430                 cprintf("%d msg %ld does not exist.\n", ERROR, num);
1431                 return;
1432         }
1433         /* put the message into the target room */
1434         lgetroom(&qtemp, targ);
1435         qtemp.QRhighest = AddMessageToRoom(&qtemp, num);
1436         lputroom(&qtemp);
1437
1438         cprintf("%d Message moved.\n", OK);
1439 }
1440
1441
1442
1443 /*
1444  * GetSuppMsgInfo()  -  Get the supplementary record for a message
1445  */
1446 void GetSuppMsgInfo(struct SuppMsgInfo *smibuf, long msgnum)
1447 {
1448
1449         struct cdbdata *cdbsmi;
1450         long TheIndex;
1451
1452         memset(smibuf, 0, sizeof(struct SuppMsgInfo));
1453         smibuf->smi_msgnum = msgnum;
1454         smibuf->smi_refcount = 1;       /* Default reference count is 1 */
1455
1456         /* Use the negative of the message number for its supp record index */
1457         TheIndex = (0L - msgnum);
1458
1459         cdbsmi = cdb_fetch(CDB_MSGMAIN, &TheIndex, sizeof(long));
1460         if (cdbsmi == NULL) {
1461                 return;         /* record not found; go with defaults */
1462         }
1463         memcpy(smibuf, cdbsmi->ptr,
1464                ((cdbsmi->len > sizeof(struct SuppMsgInfo)) ?
1465                 sizeof(struct SuppMsgInfo) : cdbsmi->len));
1466         cdb_free(cdbsmi);
1467         return;
1468 }
1469
1470
1471 /*
1472  * PutSuppMsgInfo()  -  (re)write supplementary record for a message
1473  */
1474 void PutSuppMsgInfo(struct SuppMsgInfo *smibuf)
1475 {
1476         long TheIndex;
1477
1478         /* Use the negative of the message number for its supp record index */
1479         TheIndex = (0L - smibuf->smi_msgnum);
1480
1481         cdb_store(CDB_MSGMAIN,
1482                   &TheIndex, sizeof(long),
1483                   smibuf, sizeof(struct SuppMsgInfo));
1484
1485 }
1486
1487
1488
1489 /*
1490  * AdjRefCount  -  change the reference count for a message;
1491  *                 delete the message if it reaches zero
1492  */
1493 void AdjRefCount(long msgnum, int incr)
1494 {
1495
1496         struct SuppMsgInfo smi;
1497         long delnum;
1498
1499         lprintf(9, "Adjust msg <%ld> ref count by <%d>\n",
1500                 msgnum, incr);
1501
1502         /* This is a *tight* critical section; please keep it that way, as
1503          * it may get called while nested in other critical sections.  
1504          * Complicating this any further will surely cause deadlock!
1505          */
1506         begin_critical_section(S_SUPPMSGMAIN);
1507         GetSuppMsgInfo(&smi, msgnum);
1508         smi.smi_refcount += incr;
1509         PutSuppMsgInfo(&smi);
1510         end_critical_section(S_SUPPMSGMAIN);
1511
1512         lprintf(9, "Ref count for message <%ld> after write is <%d>\n",
1513                 msgnum, smi.smi_refcount);
1514
1515         /* If the reference count is now zero, delete the message
1516          * (and its supplementary record as well).
1517          */
1518         if (smi.smi_refcount == 0) {
1519                 lprintf(9, "Deleting message <%ld>\n", msgnum);
1520                 delnum = msgnum;
1521                 cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));
1522                 delnum = (0L - msgnum);
1523                 cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));
1524         }
1525 }