Changeover to new room structure. See ChangeLog for details.
[citadel.git] / citadel / msgbase.c
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <stdio.h>
4 #include <fcntl.h>
5 #include <time.h>
6 #include <ctype.h>
7 #include <string.h>
8 #include <syslog.h>
9 #include <pthread.h>
10 #include "citadel.h"
11 #include "server.h"
12 #include <errno.h>
13 #include <sys/stat.h>
14 #include "database.h"
15 #include "msgbase.h"
16 #include "support.h"
17 #include "sysdep_decls.h"
18 #include "room_ops.h"
19 #include "user_ops.h"
20 #include "control.h"
21 #include "dynloader.h"
22
23 #define MSGS_ALL        0
24 #define MSGS_OLD        1
25 #define MSGS_NEW        2
26 #define MSGS_FIRST      3
27 #define MSGS_LAST       4
28 #define MSGS_GT         5
29
30 extern struct config config;
31
32
33 /*
34  * Aliasing for network mail.
35  * (Error messages have been commented out, because this is a server.)
36  */
37 int alias(char *name)           /* process alias and routing info for mail */
38              {
39         FILE *fp;
40         int a,b;
41         char aaa[300],bbb[300];
42         
43         fp=fopen("network/mail.aliases","r");
44         if (fp==NULL) fp=fopen("/dev/null","r");
45         if (fp==NULL) return(M_ERROR);
46 GNA:    strcpy(aaa,""); strcpy(bbb,"");
47         do {
48                 a=getc(fp);
49                 if (a==',') a=0;
50                 if (a>0) {
51                         b=strlen(aaa);
52                         aaa[b]=a;
53                         aaa[b+1]=0;
54                         }
55                 } while(a>0);
56         do {
57                 a=getc(fp);
58                 if (a==10) a=0;
59                 if (a>0) {
60                         b=strlen(bbb);
61                         bbb[b]=a;
62                         bbb[b+1]=0;
63                         }
64                 } while(a>0);
65         if (a<0) {
66                 fclose(fp);
67                 goto DETYPE;
68                 }
69         if (strcasecmp(name,aaa)) goto GNA;
70         fclose(fp);
71         strcpy(name,bbb);
72         lprintf(7, "Mail is being forwarded to %s\n", name);
73
74 DETYPE: /* determine local or remote type, see citadel.h */
75         for (a=0; a<strlen(name); ++a) if (name[a]=='!') return(M_INTERNET);
76         for (a=0; a<strlen(name); ++a)
77                 if (name[a]=='@')
78                         for (b=a; b<strlen(name); ++b)
79                                 if (name[b]=='.') return(M_INTERNET);
80         b=0; for (a=0; a<strlen(name); ++a) if (name[a]=='@') ++b;
81         if (b>1) {
82                 lprintf(7, "Too many @'s in address\n");
83                 return(M_ERROR);
84                 }
85         if (b==1) {
86                 for (a=0; a<strlen(name); ++a)
87                         if (name[a]=='@') strcpy(bbb,&name[a+1]);
88                 while (bbb[0]==32) strcpy(bbb,&bbb[1]);
89                 fp = fopen("network/mail.sysinfo","r");
90                 if (fp==NULL) return(M_ERROR);
91 GETSN:          do {
92                         a=getstring(fp,aaa);
93                         } while ((a>=0)&&(strcasecmp(aaa,bbb)));
94                 a=getstring(fp,aaa);
95                 if (!strncmp(aaa,"use ",4)) {
96                         strcpy(bbb,&aaa[4]);
97                         fseek(fp,0L,0);
98                         goto GETSN;
99                         }
100                 fclose(fp);
101                 if (!strncmp(aaa,"uum",3)) {
102                         strcpy(bbb,name);
103                         for (a=0; a<strlen(bbb); ++a) {
104                                 if (bbb[a]=='@') bbb[a]=0;
105                                 if (bbb[a]==' ') bbb[a]='_';
106                                 }
107                         while(bbb[strlen(bbb)-1]=='_') bbb[strlen(bbb)-1]=0;
108                         sprintf(name,&aaa[4],bbb);
109                         return(M_INTERNET);
110                         }
111                 if (!strncmp(aaa,"bin",3)) {
112                         strcpy(aaa,name); strcpy(bbb,name);
113                         while (aaa[strlen(aaa)-1]!='@') aaa[strlen(aaa)-1]=0;
114                         aaa[strlen(aaa)-1]=0;
115                         while (aaa[strlen(aaa)-1]==' ') aaa[strlen(aaa)-1]=0;
116                         while (bbb[0]!='@') strcpy(bbb,&bbb[1]);
117                         strcpy(bbb,&bbb[1]);
118                         while (bbb[0]==' ') strcpy(bbb,&bbb[1]);
119                         sprintf(name,"%s @%s",aaa,bbb);
120                         return(M_BINARY);
121                         }
122                 return(M_ERROR);
123                 }
124         return(M_LOCAL);
125         }
126
127
128 void get_mm(void) {
129         FILE *fp;
130
131         fp=fopen("citadel.control","r");
132         fread((char *)&CitControl,sizeof(struct CitControl),1,fp);
133         fclose(fp);
134         }
135
136 /*
137  * cmd_msgs()  -  get list of message #'s in this room
138  */
139 void cmd_msgs(char *cmdbuf)
140 {
141         int a = 0;
142         int mode = 0;
143         char which[256];
144         int cm_howmany = 0;
145         long cm_gt = 0L;
146         struct visit vbuf;
147
148         extract(which,cmdbuf,0);
149
150         mode = MSGS_ALL;
151         strcat(which,"   ");
152         if (!strncasecmp(which,"OLD",3))        mode = MSGS_OLD;
153         if (!strncasecmp(which,"NEW",3))        mode = MSGS_NEW;
154         if (!strncasecmp(which,"FIRST",5))      {
155                 mode = MSGS_FIRST;
156                 cm_howmany = extract_int(cmdbuf,1);
157                 }
158         if (!strncasecmp(which,"LAST",4))       {
159                 mode = MSGS_LAST;
160                 cm_howmany = extract_int(cmdbuf,1);
161                 }
162         if (!strncasecmp(which,"GT",2)) {
163                 mode = MSGS_GT;
164                 cm_gt = extract_long(cmdbuf,1);
165                 }
166
167         if ((!(CC->logged_in))&&(!(CC->internal_pgm))) {
168                 cprintf("%d not logged in\n",ERROR+NOT_LOGGED_IN);
169                 return;
170                 }
171         get_mm();
172         get_msglist(&CC->quickroom);
173         getuser(&CC->usersupp,CC->curr_user);
174         CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
175
176         cprintf("%d Message list...\n",LISTING_FOLLOWS);
177         if (CC->num_msgs != 0) {
178            for (a=0; a<(CC->num_msgs); ++a) 
179                if ((MessageFromList(a) >=0)
180                && ( 
181
182 (mode==MSGS_ALL)
183 || ((mode==MSGS_OLD) && (MessageFromList(a) <= vbuf.v_lastseen))
184 || ((mode==MSGS_NEW) && (MessageFromList(a) > vbuf.v_lastseen))
185 || ((mode==MSGS_NEW) && (MessageFromList(a) >= vbuf.v_lastseen)
186                      && (CC->usersupp.flags & US_LASTOLD))
187 || ((mode==MSGS_LAST)&& (a>=(CC->num_msgs-cm_howmany)))
188 || ((mode==MSGS_FIRST)&&(a<cm_howmany))
189 || ((mode==MSGS_GT) && (MessageFromList(a) > cm_gt))
190
191                         )
192                 ) {
193                         cprintf("%ld\n", MessageFromList(a));
194                         }
195            }
196         cprintf("000\n");
197         }
198
199
200
201 /* 
202  * help_subst()  -  support routine for help file viewer
203  */
204 void help_subst(char *strbuf, char *source, char *dest)
205 {
206         char workbuf[256];
207         int p;
208
209         while (p=pattern2(strbuf,source), (p>=0)) {
210                 strcpy(workbuf,&strbuf[p+strlen(source)]);
211                 strcpy(&strbuf[p],dest);
212                 strcat(strbuf,workbuf);
213                 }
214         }
215
216
217 void do_help_subst(char *buffer)
218 {
219         char buf2[16];
220
221         help_subst(buffer,"^nodename",config.c_nodename);
222         help_subst(buffer,"^humannode",config.c_humannode);
223         help_subst(buffer,"^fqdn",config.c_fqdn);
224         help_subst(buffer,"^username",CC->usersupp.fullname);
225         sprintf(buf2,"%ld",CC->usersupp.usernum);
226         help_subst(buffer,"^usernum",buf2);
227         help_subst(buffer,"^sysadm",config.c_sysadm);
228         help_subst(buffer,"^variantname",CITADEL);
229         sprintf(buf2,"%d",config.c_maxsessions);
230         help_subst(buffer,"^maxsessions",buf2);
231         }
232
233
234
235 /*
236  * memfmout()  -  Citadel text formatter and paginator.
237  *             Although the original purpose of this routine was to format
238  *             text to the reader's screen width, all we're really using it
239  *             for here is to format text out to 80 columns before sending it
240  *             to the client.  The client software may reformat it again.
241  */
242 void memfmout(int width, char *mptr, char subst)
243                         /* screen width to use */
244                         /* where are we going to get our text from? */
245                         /* nonzero if we should use hypertext mode */
246         {
247         int a,b,c;
248         int real = 0;
249         int old = 0;
250         CIT_UBYTE ch;
251         char aaa[140];
252         char buffer[256];
253         
254         strcpy(aaa,""); old=255;
255         strcpy(buffer,"");
256         c=1; /* c is the current pos */
257
258 FMTA:   if (subst) {
259                 while (ch=*mptr, ((ch!=0) && (strlen(buffer)<126) )) {
260                         ch=*mptr++;
261                         buffer[strlen(buffer)+1] = 0;
262                         buffer[strlen(buffer)] = ch;
263                         }
264
265                 if (buffer[0]=='^') do_help_subst(buffer);
266
267                 buffer[strlen(buffer)+1] = 0;
268                 a=buffer[0];
269                 strcpy(buffer,&buffer[1]);
270                 }
271         
272         else ch=*mptr++;
273
274         old=real;
275         real=ch;
276         if (ch<=0) goto FMTEND;
277         
278         if ( ((ch==13)||(ch==10)) && (old!=13) && (old!=10) ) ch=32;
279         if ( ((old==13)||(old==10)) && (isspace(real)) ) {
280                 cprintf("\n");
281                 c=1;
282                 }
283         if (ch>126) goto FMTA;
284
285         if (ch>32) {
286         if ( ((strlen(aaa)+c)>(width-5)) && (strlen(aaa)>(width-5)) )
287                 { cprintf("\n%s",aaa); c=strlen(aaa); aaa[0]=0;
288                 }
289          b=strlen(aaa); aaa[b]=ch; aaa[b+1]=0; }
290         if (ch==32) {
291                 if ((strlen(aaa)+c)>(width-5)) { 
292                         cprintf("\n");
293                         c=1;
294                         }
295                 cprintf("%s ",aaa); ++c; c=c+strlen(aaa);
296                 strcpy(aaa,"");
297                 goto FMTA;
298                 }
299         if ((ch==13)||(ch==10)) {
300                 cprintf("%s\n",aaa);
301                 c=1;
302                 strcpy(aaa,"");
303                 goto FMTA;
304                 }
305         goto FMTA;
306
307 FMTEND: cprintf("\n");
308         }
309
310
311 /*
312  * get a message off disk.
313  * 
314  */
315 void output_message(char *msgid, int mode,
316                         int headers_only, int desired_section) {
317         long msg_num;
318         int a;
319         CIT_UBYTE ch, rch;
320         CIT_UBYTE format_type,anon_flag;
321         char buf[1024];
322         long msg_len;
323         int msg_ok = 0;
324         char boundary[256];             /* attachment boundary */
325         char current_section = 0;       /* section currently being parsed */
326         int has_attachments = 0;
327
328         struct cdbdata *dmsgtext;
329         char *mptr;
330
331         /* buffers needed for RFC822 translation */
332         char suser[256];
333         char luser[256];
334         char snode[256];
335         char lnode[256];
336         char mid[256];
337         long xtime;
338         /* */
339
340         strcpy(boundary, "");
341         msg_num = atol(msgid);
342
343
344         if ((!(CC->logged_in))&&(!(CC->internal_pgm))) {
345                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
346                 return;
347                 }
348
349         /* We used to need to check in the current room's message list
350          * to determine where the message's disk position.  We no longer need
351          * to do this, but we do it anyway as a security measure, in order to
352          * prevent rogue clients from reading messages not in the current room.
353          */
354
355         msg_ok = 0;
356         if (CC->num_msgs > 0) {
357                 for (a=0; a<CC->num_msgs; ++a) {
358                         if (MessageFromList(a) == msg_num) {
359                                 msg_ok = 1;
360                                 }
361                         }
362                 }
363
364         if (!msg_ok) {
365                 cprintf("%d Message %ld is not in this room.\n",
366                         ERROR, msg_num);
367                 return;
368                 }
369         
370
371         dmsgtext = cdb_fetch(CDB_MSGMAIN, &msg_num, sizeof(long));
372         
373         if (dmsgtext == NULL) {
374                 cprintf("%d Can't find message %ld\n", ERROR+INTERNAL_ERROR);
375                 return;
376                 }
377
378         msg_len = (long) dmsgtext->len;
379         mptr = dmsgtext->ptr;
380         lprintf(9, "Returned message length is %ld\n", msg_len);
381
382         /* this loop spews out the whole message if we're doing raw format */
383         if (mode == MT_RAW) {
384                 cprintf("%d %ld\n", BINARY_FOLLOWS, msg_len);
385                 client_write(dmsgtext->ptr, (int) msg_len);
386                 cdb_free(dmsgtext);
387                 return;
388                 }
389
390         /* Otherwise, we'll start parsing it field by field... */
391         ch = *mptr++;
392         if (ch != 255) {
393                 cprintf("%d Illegal message format on disk\n",
394                         ERROR+INTERNAL_ERROR);
395                 cdb_free(dmsgtext);
396                 return;
397                 }
398
399         anon_flag = *mptr++;
400         format_type = *mptr++;
401
402         /* now for the user-mode message reading loops */
403         cprintf("%d Message %ld:\n",LISTING_FOLLOWS,msg_num);
404
405         if (mode == MT_CITADEL) cprintf("type=%d\n",format_type);
406
407         if ( (anon_flag == MES_ANON) && (mode == MT_CITADEL) ) {
408                 cprintf("nhdr=yes\n");
409                 }
410
411         /* begin header processing loop for Citadel message format */
412
413         if (mode == MT_CITADEL) while(ch = *mptr++, (ch!='M' && ch!=0)) {
414                 buf[0] = 0;
415                 do {
416                         buf[strlen(buf)+1] = 0;
417                         rch = *mptr++;
418                         buf[strlen(buf)] = rch;
419                         } while (rch > 0);
420
421                 if (ch=='A') {
422                         PerformUserHooks(buf, (-1L), EVT_OUTPUTMSG);
423                         if (anon_flag==MES_ANON) cprintf("from=****");
424                         else if (anon_flag==MES_AN2) cprintf("from=anonymous");
425                         else cprintf("from=%s",buf);
426                         if ((is_room_aide()) && ((anon_flag == MES_ANON)
427                            || (anon_flag == MES_AN2)))
428                                 cprintf(" [%s]",buf);
429                         cprintf("\n");
430                         }
431                 else if (ch=='Z') {
432                         has_attachments = 1;
433                         sprintf(boundary, "--%s", buf);
434                         }
435                 else if (ch=='P') cprintf("path=%s\n",buf);
436                 else if (ch=='U') cprintf("subj=%s\n",buf);
437                 else if (ch=='I') cprintf("msgn=%s\n",buf);
438                 else if (ch=='H') cprintf("hnod=%s\n",buf);
439                 else if (ch=='O') cprintf("room=%s\n",buf);
440                 else if (ch=='N') cprintf("node=%s\n",buf);
441                 else if (ch=='R') cprintf("rcpt=%s\n",buf);
442                 else if (ch=='T') cprintf("time=%s\n",buf);
443                 /* else cprintf("fld%c=%s\n",ch,buf); */
444                 }
445
446         /* begin header processing loop for RFC822 transfer format */
447
448         strcpy(suser, "");
449         strcpy(luser, "");
450         strcpy(snode, NODENAME);
451         strcpy(lnode, HUMANNODE);
452         if (mode == MT_RFC822) while(ch = *mptr++, (ch!='M' && ch!=0)) {
453                 buf[0] = 0;
454                 do {
455                         buf[strlen(buf)+1] = 0;
456                         rch = *mptr++;
457                         buf[strlen(buf)] = rch;
458                         } while (rch > 0);
459
460                 if (ch=='A') strcpy(luser, buf);
461                 else if (ch=='P') {
462                         cprintf("Path: %s\n",buf);
463                         for (a=0; a<strlen(buf); ++a) {
464                                 if (buf[a] == '!') {
465                                         strcpy(buf,&buf[a+1]);
466                                         a=0;
467                                         }
468                                 }
469                         strcpy(suser, buf);
470                         }
471                 else if (ch=='U') cprintf("Subject: %s\n",buf);
472                 else if (ch=='I') strcpy(mid, buf);
473                 else if (ch=='H') strcpy(lnode, buf);
474                 else if (ch=='O') cprintf("X-Citadel-Room: %s\n",buf);
475                 else if (ch=='N') strcpy(snode, buf);
476                 else if (ch=='R') cprintf("To: %s\n",buf);
477                 else if (ch=='T')  {
478                         xtime = atol(buf);
479                         cprintf("Date: %s", asctime(localtime(&xtime)));
480                         }
481                 }
482
483         if (mode == MT_RFC822) {
484                 if (!strcasecmp(snode, NODENAME)) {
485                         strcpy(snode, FQDN);
486                         }
487                 cprintf("Message-ID: <%s@%s>\n", mid, snode);
488                 PerformUserHooks(luser, (-1L), EVT_OUTPUTMSG);
489                 cprintf("From: %s@%s (%s)\n",
490                         suser, snode, luser);
491                 cprintf("Organization: %s\n", lnode);
492                 }
493
494         /* end header processing loop ... at this point, we're in the text */
495
496         if (ch==0) {
497                 cprintf("text\n*** ?Message truncated\n000\n");
498                 cdb_free(dmsgtext);
499                 return;
500                 }
501
502         if (headers_only) {
503                 /* give 'em a length */
504                 msg_len = 0L;
505                 while(ch = *mptr++, ch>0) {
506                         ++msg_len;
507                         }
508                 cprintf("mlen=%ld\n", msg_len);
509                 cprintf("000\n");
510                 cdb_free(dmsgtext);
511                 return;
512                 }
513
514         /* signify start of msg text */
515         if (mode == MT_CITADEL) cprintf("text\n");
516         if (mode == MT_RFC822) cprintf("\n");
517
518         /* If the format type on disk is 1 (fixed-format), then we want
519          * everything to be output completely literally ... regardless of
520          * what message transfer format is in use.
521          */
522         if (format_type == 1) {
523                 strcpy(buf, "");
524                 while(ch = *mptr++, ch>0) {
525                         if (ch == 13) ch = 10;
526                         if ( (ch == 10) || (strlen(buf)>250) ) {
527                                 if (has_attachments) if (!strncmp(buf, boundary, strlen(boundary))) {
528                                         ++current_section;
529                                         }
530                                 if (current_section == desired_section) {
531                                         if ( (has_attachments == 0) || (strncmp(buf, boundary, strlen(boundary)))) {
532                                                 cprintf("%s\n", buf);
533                                                 }
534                                         }
535                                 strcpy(buf, "");
536                                 }
537                         else {
538                                 buf[strlen(buf)+1] = 0;
539                                 buf[strlen(buf)] = ch;
540                                 }
541                         }
542                 if (strlen(buf)>0) cprintf("%s\n", buf);
543                 }
544         /* If the message on disk is format 0 (Citadel vari-format), we
545          * output using the formatter at 80 columns.  This is the final output
546          * form if the transfer format is RFC822, but if the transfer format
547          * is Citadel proprietary, it'll still work, because the indentation
548          * for new paragraphs is correct and the client will reformat the
549          * message to the reader's screen width.
550          */
551         if (format_type == 0) {
552                 memfmout(80,mptr,0);
553                 }
554
555
556         /* now we're done */
557         cprintf("000\n");
558         cdb_free(dmsgtext);
559         }
560
561
562 /*
563  * display a message (mode 0 - Citadel proprietary)
564  */
565 void cmd_msg0(char *cmdbuf)
566 {
567         char msgid[256];
568         int headers_only = 0;
569         int desired_section = 0;
570
571         extract(msgid,cmdbuf,0);
572         headers_only = extract_int(cmdbuf, 1);
573         desired_section = extract_int(cmdbuf, 2);
574
575         output_message(msgid,MT_CITADEL, headers_only, desired_section);
576         }
577
578
579 /*
580  * display a message (mode 2 - RFC822)
581  */
582 void cmd_msg2(char *cmdbuf)
583 {
584         char msgid[256];
585         int headers_only = 0;
586
587         extract(msgid,cmdbuf,0);
588         headers_only = extract_int(cmdbuf,1);
589
590         output_message(msgid,MT_RFC822,headers_only,0);
591         }
592
593 /* 
594  * display a message (mode 3 - IGnet raw format - internal programs only)
595  */
596 void cmd_msg3(char *cmdbuf)
597 {
598         char msgid[256];
599         int headers_only = 0;
600
601         if (CC->internal_pgm == 0) {
602                 cprintf("%d This command is for internal programs only.\n",
603                         ERROR);
604                 return;
605                 }
606
607         extract(msgid,cmdbuf,0);
608         headers_only = extract_int(cmdbuf,1);
609
610         output_message(msgid,MT_RAW,headers_only,0);
611         }
612
613
614
615 /*
616  * Message base operation to send a message to the master file
617  * (returns new message number)
618  */
619 long send_message(char *message_in_memory,      /* pointer to buffer */
620                 size_t message_length,          /* length of buffer */
621                 int generate_id) {              /* 1 to generate an I field */
622
623         long newmsgid;
624
625         /* Get a new message number */
626         newmsgid = get_new_message_number();
627
628         /* Write our little bundle of joy into the message base */
629
630         lprintf(9, "Storing message %ld\n", newmsgid);
631         begin_critical_section(S_MSGMAIN);
632         if ( cdb_store(CDB_MSGMAIN, &newmsgid, sizeof(long),
633                         message_in_memory, message_length) < 0 ) {
634                 lprintf(2, "Can't store message\n");
635                 end_critical_section(S_MSGMAIN);
636                 return 0L;
637                 }
638         end_critical_section(S_MSGMAIN);
639
640         /* Finally, return the pointers */
641         return(newmsgid);
642         }
643
644
645
646 /*
647  * this is a simple file copy routine.
648  */
649 void copy_file(char *from, char *to)
650 {
651         FILE *ffp,*tfp;
652         int a;
653
654         ffp=fopen(from,"r");
655         if (ffp==NULL) return;
656         tfp=fopen(to,"w");
657         if (tfp==NULL) {
658                 fclose(ffp);
659                 return;
660                 }
661         while (a=getc(ffp), a>=0) {
662                 putc(a,tfp);
663                 }
664         fclose(ffp);
665         fclose(tfp);
666         return;
667         }
668
669
670
671 /*
672  * message base operation to save a message and install its pointers
673  */
674 void save_message(char *mtmp,   /* file containing proper message */
675                 char *rec,      /* Recipient (if mail) */
676                 char mtsflag,   /* 0 for normal, 1 to force Aide> room */
677                 int mailtype,   /* local or remote type, see citadel.h */
678                 int generate_id) /* set to 1 to generate an 'I' field */
679 {
680         char aaa[100];
681         char hold_rm[ROOMNAMELEN];
682         long newmsgid;
683         char *message_in_memory;
684         struct stat statbuf;
685         size_t templen;
686         FILE *fp;
687
688         /* Measure the message */
689         lprintf(9, "Measuring the message\n");
690         stat(mtmp, &statbuf);
691         templen = statbuf.st_size;
692
693         /* Now read it into memory */
694         lprintf(9, "Allocating %ld bytes\n", templen);
695         message_in_memory = (char *) malloc(templen);
696         if (message_in_memory == NULL) {
697                 lprintf(2, "Can't allocate memory to save message!\n");
698                 return;
699                 }
700
701         lprintf(9, "Reading it into memory\n"); 
702         fp = fopen(mtmp, "rb");
703         fread(message_in_memory, templen, 1, fp);
704         fclose(fp);
705
706         newmsgid = send_message(message_in_memory, templen, generate_id);
707         free(message_in_memory);
708         if (newmsgid <= 0L) return;
709
710         strcpy(hold_rm, "");
711
712         /* If the user is a twit, move to the twit room for posting... */
713         if (TWITDETECT) if (CC->usersupp.axlevel==2) {
714                 strcpy(hold_rm, CC->cs_room);
715                 strcpy(CC->cs_room, config.c_twitroom);
716                 }
717
718         /* ...or if this message is destined for Aide> then go there. */
719         if (mtsflag) {
720                 strcpy(hold_rm, CC->cs_room);
721                 strcpy(CC->cs_room, "Aide");
722                 }
723
724         /* ...or if this is a private message, go to the target mailbox. */
725         /* FIX FIX FIX do this! */
726
727         /* This call to usergoto() changes rooms if necessary.  It also
728          * causes the latest message list to be read into memory.
729          */
730         lprintf(9, "Changing rooms if necessary...\n");
731         usergoto(CC->cs_room, 0);
732
733         /* read in the quickroom record, obtaining a lock... */
734         lprintf(9, "Reading/locking <%s>...\n", CC->cs_room);
735         lgetroom(&CC->quickroom, CC->cs_room);
736         lprintf(9, "Fetching message list...\n");
737         get_msglist(&CC->quickroom);
738
739         /* FIX here's where we have to to message expiry!! */
740
741         /* Now add the new message */
742         CC->num_msgs = CC->num_msgs + 1;
743         CC->msglist = realloc(CC->msglist,
744                 ((CC->num_msgs) * sizeof(long)) );
745         if (CC->msglist == NULL) {
746                 lprintf(3, "ERROR can't realloc message list!\n");
747                 }
748         SetMessageInList(CC->num_msgs - 1, newmsgid);
749
750         /* Write it back to disk. */
751         lprintf(9, "Writing message list...\n");
752         put_msglist(&CC->quickroom);
753
754         /* update quickroom */
755         CC->quickroom.QRhighest = newmsgid;
756         lprintf(9, "Writing/unlocking room <%s>...\n", CC->cs_room);
757         lputroom(&CC->quickroom,CC->cs_room);
758
759         /* Bump this user's messages posted counter.  Also, if the user is a
760          * twit, give them access to the twit room.
761          */
762         lgetuser(&CC->usersupp, CC->curr_user);
763         CC->usersupp.posted = CC->usersupp.posted + 1;
764         /* FIX if user is twit, grant access to twitroom here */
765         lputuser(&CC->usersupp, CC->curr_user);
766
767         /* Network mail - send a copy to the network program. */
768         if (mailtype!=M_LOCAL) {
769                 sprintf(aaa,"./network/spoolin/nm.%d",getpid());
770                 copy_file(mtmp,aaa);
771                 system("exec nohup ./netproc >/dev/null 2>&1 &");
772                 }
773
774         /* If we've posted in a room other than the current room, then we
775          * have to now go back to the current room...
776          */
777         if (strlen(hold_rm) > 0) {
778                 lprintf(9, "Returning to <%s>\n", hold_rm);
779                 usergoto(hold_rm, 0);
780                 }
781         lprintf(9, "Deleting temporary file\n");
782         unlink(mtmp);           /* delete the temporary file */
783         lprintf(9, "Finished with save_message()\n");
784         }
785
786
787 /*
788  * Generate an administrative message and post it in the Aide> room.
789  */
790 void aide_message(char *text)
791 {
792         long now;
793         FILE *fp;
794
795         time(&now);
796         fp=fopen(CC->temp,"wb");
797         fprintf(fp,"%c%c%c",255,MES_NORMAL,0);
798         fprintf(fp,"Psysop%c",0);
799         fprintf(fp,"T%ld%c",now,0);
800         fprintf(fp,"ACitadel%c",0);
801         fprintf(fp,"OAide%c",0);
802         fprintf(fp,"N%s%c",NODENAME,0);
803         fprintf(fp,"M%s\n%c",text,0);
804         fclose(fp);
805         save_message(CC->temp,"",1,M_LOCAL,1);
806         syslog(LOG_NOTICE,text);
807         }
808
809
810
811 /*
812  * Build a binary message to be saved on disk.
813  */
814 void make_message(
815         char *filename,                 /* temporary file name */
816         struct usersupp *author,        /* author's usersupp structure */
817         char *recipient,                /* NULL if it's not mail */
818         char *room,                     /* room where it's going */
819         int type,                       /* see MES_ types in header file */
820         int net_type,                   /* see MES_ types in header file */
821         int format_type,                /* local or remote (see citadel.h) */
822         char *fake_name,                /* who we're masquerading as */
823         char *boundary) {               /* boundary (if exist attachments) */
824
825         FILE *fp;
826         int a;
827         long now;
828         char dest_node[32];
829         char buf[256];
830
831         /* Don't confuse the poor folks if it's not routed mail. */
832         strcpy(dest_node, "");
833
834         /* If net_type is M_BINARY, split out the destination node. */
835         if (net_type == M_BINARY) {
836                 strcpy(dest_node,NODENAME);
837                 for (a=0; a<strlen(recipient); ++a) {
838                         if (recipient[a]=='@') {
839                                 recipient[a]=0;
840                                 strcpy(dest_node,&recipient[a+1]);
841                                 }
842                         }
843                 }
844
845         /* if net_type is M_INTERNET, set the dest node to 'internet' */
846         if (net_type == M_INTERNET) {
847                 strcpy(dest_node,"internet");
848                 }
849
850         while (isspace(recipient[strlen(recipient)-1]))
851                 recipient[strlen(recipient)-1] = 0;
852
853         time(&now);
854         fp=fopen(filename,"w");
855         putc(255,fp);
856         putc(type,fp);  /* Normal or anonymous, see MES_ flags */
857         putc(format_type,fp);   /* Formatted or unformatted */
858         fprintf(fp,"Pcit%ld%c",author->usernum,0);      /* path */
859         fprintf(fp,"T%ld%c",now,0);                     /* date/time */
860         if (fake_name[0])
861            fprintf(fp,"A%s%c",fake_name,0);
862         else
863            fprintf(fp,"A%s%c",author->fullname,0);      /* author */
864         fprintf(fp,"O%s%c",CC->quickroom.QRname,0);     /* room */
865         fprintf(fp,"N%s%c",NODENAME,0);                 /* nodename */
866         fprintf(fp,"H%s%c",HUMANNODE,0);                /* human nodename */
867
868         if (recipient[0]!=0) fprintf(fp, "R%s%c", recipient, 0);
869         if (dest_node[0]!=0) fprintf(fp, "D%s%c", dest_node, 0);
870         if (boundary[0]!=0) fprintf(fp, "Z%s%c", boundary, 0);
871
872         putc('M',fp);
873
874         while (client_gets(buf), strcmp(buf,"000"))
875         {
876            fprintf(fp,"%s\n",buf);
877         }
878         syslog(LOG_INFO, "Closing message");
879         putc(0,fp);
880         fclose(fp);
881         }
882
883
884
885
886
887 /*
888  * message entry  -  mode 0 (normal) <bc>
889  */
890 void cmd_ent0(char *entargs)
891 {
892         int post = 0;
893         char recipient[256];
894         int anon_flag = 0;
895         int format_type = 0;
896         char newusername[256];          /* <bc> */
897         char boundary[256];
898
899         int a,b;
900         int e = 0;
901         int mtsflag = 0;
902         struct usersupp tempUS;
903         char buf[256];
904
905         post = extract_int(entargs,0);
906         extract(recipient,entargs,1);
907         anon_flag = extract_int(entargs,2);
908         format_type = extract_int(entargs,3);
909         extract(boundary, entargs, 5);
910
911         /* first check to make sure the request is valid. */
912
913         if (!(CC->logged_in)) {
914                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
915                 return;
916                 }
917         if ((CC->usersupp.axlevel<2)&&((CC->quickroom.QRflags&QR_MAILBOX)==0)) {
918                 cprintf("%d Need to be validated to enter ",
919                         ERROR+HIGHER_ACCESS_REQUIRED);
920                 cprintf("(except in %s> to sysop)\n", MAILROOM);
921                 return;
922                 }
923         if ((CC->usersupp.axlevel<4)&&(CC->quickroom.QRflags&QR_NETWORK)) {
924                 cprintf("%d Need net privileges to enter here.\n",
925                         ERROR+HIGHER_ACCESS_REQUIRED);
926                 return;
927                 }
928         if ((CC->usersupp.axlevel<6)&&(CC->quickroom.QRflags&QR_READONLY)) {
929                 cprintf("%d Sorry, this is a read-only room.\n",
930                         ERROR+HIGHER_ACCESS_REQUIRED);
931                 return;
932                 }
933
934         mtsflag=0;
935         
936                 
937         if (post==2) {                  /* <bc> */
938            if (CC->usersupp.axlevel<6)
939            {
940               cprintf("%d You don't have permission to do an aide post.\n",
941                 ERROR+HIGHER_ACCESS_REQUIRED);
942               return;
943            }
944            extract(newusername,entargs,4);
945            bzero(CC->fake_postname, 32);
946            strcpy(CC->fake_postname, newusername);
947            cprintf("%d Ok\n",OK);
948            return;
949         }
950         
951         CC->cs_flags |= CS_POSTING;
952         
953         buf[0]=0;
954         if (CC->quickroom.QRflags & QR_MAILBOX) {
955                 if (CC->usersupp.axlevel>=2) {
956                         strcpy(buf,recipient);
957                         }
958                 else strcpy(buf,"sysop");
959                 lprintf(9, "aliasing...\n");
960                 e=alias(buf);                   /* alias and mail type */
961                 lprintf(9,"...type is %d\n", e);
962                 if ((buf[0]==0) || (e==M_ERROR)) {
963                         cprintf("%d Unknown address - cannot send message.\n",
964                                 ERROR+NO_SUCH_USER);
965                         return;
966                         }
967                 if ((e!=M_LOCAL)&&(CC->usersupp.axlevel<4)) {
968                         cprintf("%d Net privileges required for network mail.\n",
969                                 ERROR+HIGHER_ACCESS_REQUIRED);
970                         return;
971                         }
972                 if ((RESTRICT_INTERNET==1)&&(e==M_INTERNET)
973                    &&((CC->usersupp.flags&US_INTERNET)==0)
974                    &&(!CC->internal_pgm) ) {
975                         cprintf("%d You don't have access to Internet mail.\n",
976                                 ERROR+HIGHER_ACCESS_REQUIRED);
977                         return;
978                         }
979                 if (!strcasecmp(buf,"sysop")) {
980                         mtsflag=1;
981                         goto SKFALL;
982                         }
983                 if (e!=M_LOCAL) goto SKFALL;    /* don't search local file  */
984                 if (!strcasecmp(buf,CC->usersupp.fullname)) {
985                         cprintf("%d Can't send mail to yourself!\n",
986                                 ERROR+NO_SUCH_USER);
987                         return;
988                         }
989
990                 /* Check to make sure the user exists; also get the correct
991                 * upper/lower casing of the name. 
992                 */
993                 lprintf(9, "checking validity of %s\n", buf);
994                 a = getuser(&tempUS,buf);
995                 lprintf(9, "getuser() returned %d\n", a);
996                 if (a != 0) {
997                         cprintf("%d No such user.\n",ERROR+NO_SUCH_USER);
998                         return;
999                         }
1000                 strcpy(buf,tempUS.fullname);
1001                 }
1002         
1003 SKFALL: b=MES_NORMAL;
1004         if (CC->quickroom.QRflags&QR_ANONONLY) b=MES_ANON;
1005         if (CC->quickroom.QRflags&QR_ANONOPT) {
1006                 if (anon_flag==1) b=MES_AN2;
1007                 }
1008         if ((CC->quickroom.QRflags & QR_MAILBOX) == 0) buf[0]=0;
1009
1010         /* If we're only checking the validity of the request, return
1011          * success without creating the message.
1012          */
1013         if (post==0) {
1014                 cprintf("%d %s\n",OK,buf);
1015                 return;
1016                 }
1017         
1018         cprintf("%d send message\n",SEND_LISTING);
1019         if (CC->fake_postname[0])
1020            make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, CC->fake_postname, boundary);
1021         else
1022            if (CC->fake_username[0])
1023               make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, CC->fake_username, boundary);
1024            else
1025               make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, "", boundary);
1026         save_message(CC->temp,buf,mtsflag,e,1);
1027         CC->fake_postname[0]='\0';
1028         return;
1029         }
1030
1031
1032
1033 /* 
1034  * message entry - mode 3 (raw)
1035  */
1036 void cmd_ent3(char *entargs)
1037 {
1038         char recp[256];
1039         char buf[256];
1040         int a;
1041         int e = 0;
1042         struct usersupp tempUS;
1043         long msglen;
1044         long bloklen;
1045         FILE *fp;
1046
1047         if (CC->internal_pgm == 0) {
1048                 cprintf("%d This command is for internal programs only.\n",
1049                         ERROR);
1050                 return;
1051                 }
1052
1053         /* If we're in Mail, check the recipient */
1054         if (CC->quickroom.QRflags & QR_MAILBOX) {
1055                 extract(recp, entargs, 1);
1056                 lprintf(9, "aliasing...\n");
1057                 e=alias(recp);                  /* alias and mail type */
1058                 lprintf(9,"...type is %d\n", e);
1059                 if ((buf[0]==0) || (e==M_ERROR)) {
1060                         cprintf("%d Unknown address - cannot send message.\n",
1061                                 ERROR+NO_SUCH_USER);
1062                         return;
1063                         }
1064                 if (e == M_LOCAL) {
1065                         a = getuser(&tempUS,recp);
1066                         if (a!=0) {
1067                                 cprintf("%d No such user.\n", ERROR+NO_SUCH_USER);
1068                                 return;
1069                                 }
1070                         }
1071                 }
1072
1073         /* At this point, message has been approved. */
1074         if (extract_int(entargs, 0) == 0) {
1075                 cprintf("%d OK to send\n", OK);
1076                 return;
1077                 }
1078
1079         /* open a temp file to hold the message */
1080         fp = fopen(CC->temp, "wb");
1081         if (fp == NULL) {
1082                 cprintf("%d Cannot open %s: %s\n", 
1083                         ERROR + INTERNAL_ERROR,
1084                         CC->temp, strerror(errno) );
1085                 return;
1086                 }
1087
1088         msglen = extract_long(entargs, 2);
1089         cprintf("%d %ld\n", SEND_BINARY, msglen);
1090         while(msglen > 0L) {
1091                 bloklen = ((msglen >= 255L) ? 255 : msglen);
1092                 client_read(buf, (int)bloklen );
1093                 fwrite(buf, (int)bloklen, 1, fp);
1094                 msglen = msglen - bloklen;
1095                 }
1096         fclose(fp);
1097
1098         save_message(CC->temp, recp, 0, e, 0);
1099         }
1100
1101
1102 /*
1103  * Delete message from current room
1104  */
1105 void cmd_dele(char *delstr)
1106 {
1107         long delnum;
1108         int a,ok;
1109
1110         getuser(&CC->usersupp,CC->curr_user);
1111         if ((CC->usersupp.axlevel < 6)
1112            && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
1113                 cprintf("%d Higher access required.\n",
1114                         ERROR+HIGHER_ACCESS_REQUIRED);
1115                 return;
1116                 }
1117
1118         delnum = atol(delstr);
1119         if (CC->quickroom.QRflags & QR_MAILBOX) {
1120                 cprintf("%d Can't delete mail.\n",ERROR);
1121                 return;
1122                 }
1123         
1124         /* get room records, obtaining a lock... */
1125         lgetroom(&CC->quickroom,CC->cs_room);
1126         get_msglist(&CC->quickroom);
1127
1128         ok = 0;
1129         if (CC->num_msgs > 0) for (a=0; a<(CC->num_msgs); ++a) {
1130                 if (MessageFromList(a) == delnum) {
1131                         SetMessageInList(a, 0L);
1132                         ok = 1;
1133                         }
1134                 }
1135
1136         CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
1137         CC->quickroom.QRhighest = MessageFromList(CC->num_msgs - 1);
1138
1139         put_msglist(&CC->quickroom);
1140         lputroom(&CC->quickroom,CC->cs_room);
1141         if (ok==1) {
1142                 cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));
1143                 cprintf("%d Message deleted.\n",OK);
1144                 }
1145         else cprintf("%d No message %ld.\n",ERROR,delnum);
1146         } 
1147
1148
1149 /*
1150  * move a message to another room
1151  */
1152 void cmd_move(char *args)
1153 {
1154         long num;
1155         char targ[32];
1156         int a;
1157         struct quickroom qtemp;
1158         int foundit;
1159
1160         num = extract_long(args,0);
1161         extract(targ,args,1);
1162         
1163         getuser(&CC->usersupp,CC->curr_user);
1164         if ((CC->usersupp.axlevel < 6)
1165            && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
1166                 cprintf("%d Higher access required.\n",
1167                         ERROR+HIGHER_ACCESS_REQUIRED);
1168                 return;
1169                 }
1170
1171         if (getroom(&qtemp, targ) != 0) {
1172                 cprintf("%d '%s' does not exist.\n",ERROR,targ);
1173                 return;
1174                 }
1175
1176         /* yank the message out of the current room... */
1177         lgetroom(&CC->quickroom, CC->cs_room);
1178         get_msglist(&CC->quickroom);
1179
1180         foundit = 0;
1181         for (a=0; a<(CC->num_msgs); ++a) {
1182                 if (MessageFromList(a) == num) {
1183                         foundit = 1;
1184                         SetMessageInList(a, 0L);
1185                         }
1186                 }
1187         if (foundit) {
1188                 CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
1189                 put_msglist(&CC->quickroom);
1190                 CC->quickroom.QRhighest = MessageFromList((CC->num_msgs)-1);
1191                 }
1192         lputroom(&CC->quickroom,CC->cs_room);
1193         if (!foundit) {
1194                 cprintf("%d msg %ld does not exist.\n",ERROR,num);
1195                 return;
1196                 }
1197
1198         /* put the message into the target room */
1199
1200         cprintf("%d FIX FIX FIX implement this!!!!!\n", ERROR);
1201         }