8274ca1dac2bd08803625661f149ffcf071281a4
[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 int twitroom=-1;
32
33
34 /*
35  * Aliasing for network mail.
36  * (Error messages have been commented out, because this is a server.)
37  */
38 int alias(char *name)           /* process alias and routing info for mail */
39              {
40         FILE *fp;
41         int a,b;
42         char aaa[300],bbb[300];
43         
44         fp=fopen("network/mail.aliases","r");
45         if (fp==NULL) fp=fopen("/dev/null","r");
46         if (fp==NULL) return(M_ERROR);
47 GNA:    strcpy(aaa,""); strcpy(bbb,"");
48         do {
49                 a=getc(fp);
50                 if (a==',') a=0;
51                 if (a>0) {
52                         b=strlen(aaa);
53                         aaa[b]=a;
54                         aaa[b+1]=0;
55                         }
56                 } while(a>0);
57         do {
58                 a=getc(fp);
59                 if (a==10) a=0;
60                 if (a>0) {
61                         b=strlen(bbb);
62                         bbb[b]=a;
63                         bbb[b+1]=0;
64                         }
65                 } while(a>0);
66         if (a<0) {
67                 fclose(fp);
68                 goto DETYPE;
69                 }
70         if (strcasecmp(name,aaa)) goto GNA;
71         fclose(fp);
72         strcpy(name,bbb);
73         /* cprintf("*** Mail is being forwarded to %s\n",name); */
74
75 DETYPE: /* determine local or remote type, see citadel.h */
76         for (a=0; a<strlen(name); ++a) if (name[a]=='!') return(M_INTERNET);
77         for (a=0; a<strlen(name); ++a)
78                 if (name[a]=='@')
79                         for (b=a; b<strlen(name); ++b)
80                                 if (name[b]=='.') return(M_INTERNET);
81         b=0; for (a=0; a<strlen(name); ++a) if (name[a]=='@') ++b;
82         if (b>1) {
83                 /* cprintf("Too many @'s in address\n"); */
84                 return(M_ERROR);
85                 }
86         if (b==1) {
87                 for (a=0; a<strlen(name); ++a)
88                         if (name[a]=='@') strcpy(bbb,&name[a+1]);
89                 while (bbb[0]==32) strcpy(bbb,&bbb[1]);
90                 fp = fopen("network/mail.sysinfo","r");
91                 if (fp==NULL) return(M_ERROR);
92 GETSN:          do {
93                         a=getstring(fp,aaa);
94                         } while ((a>=0)&&(strcasecmp(aaa,bbb)));
95                 a=getstring(fp,aaa);
96                 if (!strncmp(aaa,"use ",4)) {
97                         strcpy(bbb,&aaa[4]);
98                         fseek(fp,0L,0);
99                         goto GETSN;
100                         }
101                 fclose(fp);
102                 if (!strncmp(aaa,"uum",3)) {
103                         strcpy(bbb,name);
104                         for (a=0; a<strlen(bbb); ++a) {
105                                 if (bbb[a]=='@') bbb[a]=0;
106                                 if (bbb[a]==' ') bbb[a]='_';
107                                 }
108                         while(bbb[strlen(bbb)-1]=='_') bbb[strlen(bbb)-1]=0;
109                         sprintf(name,&aaa[4],bbb);
110                         return(M_INTERNET);
111                         }
112                 if (!strncmp(aaa,"bin",3)) {
113                         strcpy(aaa,name); strcpy(bbb,name);
114                         while (aaa[strlen(aaa)-1]!='@') aaa[strlen(aaa)-1]=0;
115                         aaa[strlen(aaa)-1]=0;
116                         while (aaa[strlen(aaa)-1]==' ') aaa[strlen(aaa)-1]=0;
117                         while (bbb[0]!='@') strcpy(bbb,&bbb[1]);
118                         strcpy(bbb,&bbb[1]);
119                         while (bbb[0]==' ') strcpy(bbb,&bbb[1]);
120                         sprintf(name,"%s @%s",aaa,bbb);
121                         return(M_BINARY);
122                         }
123                 return(M_ERROR);
124                 }
125         return(M_LOCAL);
126         }
127
128
129 void get_mm(void) {
130         FILE *fp;
131
132         fp=fopen("citadel.control","r");
133         fread((char *)&CitControl,sizeof(struct CitControl),1,fp);
134         fclose(fp);
135         }
136
137 /*
138  * cmd_msgs()  -  get list of message #'s in this room
139  */
140 void cmd_msgs(char *cmdbuf)
141 {
142         int a;
143         int mode;
144         char which[256];
145         int cm_howmany;
146         long cm_gt;
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         if (CC->curr_rm < 0) {
172                 cprintf("%d no room\n",ERROR);
173                 return;
174                 }
175         get_mm();
176         get_msglist(CC->curr_rm);
177         getuser(&CC->usersupp,CC->curr_user);
178         cprintf("%d %d messages...\n",LISTING_FOLLOWS, CC->num_msgs);
179         if (CC->num_msgs != 0) {
180            for (a=0; a<(CC->num_msgs); ++a) 
181                if ((MessageFromList(a) >=0)
182                && ( 
183
184 (mode==MSGS_ALL)
185 || ((mode==MSGS_OLD) && (MessageFromList(a) <= CC->usersupp.lastseen[CC->curr_rm]))
186 || ((mode==MSGS_NEW) && (MessageFromList(a) > CC->usersupp.lastseen[CC->curr_rm]))
187 || ((mode==MSGS_NEW) && (MessageFromList(a) >= CC->usersupp.lastseen[CC->curr_rm])
188                      && (CC->usersupp.flags & US_LASTOLD))
189 || ((mode==MSGS_LAST)&& (a>=(CC->num_msgs-cm_howmany)))
190 || ((mode==MSGS_FIRST)&&(a<cm_howmany))
191 || ((mode==MSGS_GT) && (MessageFromList(a) > cm_gt))
192
193                         )
194                 ) {
195                         cprintf("%ld\n", MessageFromList(a));
196                         }
197            }
198         cprintf("000\n");
199         }
200
201
202
203 /* 
204  * help_subst()  -  support routine for help file viewer
205  */
206 void help_subst(char *strbuf, char *source, char *dest)
207 {
208         char workbuf[256];
209         int p;
210
211         while (p=pattern2(strbuf,source), (p>=0)) {
212                 strcpy(workbuf,&strbuf[p+strlen(source)]);
213                 strcpy(&strbuf[p],dest);
214                 strcat(strbuf,workbuf);
215                 }
216         }
217
218
219 void do_help_subst(char *buffer)
220 {
221         char buf2[16];
222
223         help_subst(buffer,"^nodename",config.c_nodename);
224         help_subst(buffer,"^humannode",config.c_humannode);
225         help_subst(buffer,"^fqdn",config.c_fqdn);
226         help_subst(buffer,"^username",CC->usersupp.fullname);
227         sprintf(buf2,"%ld",CC->usersupp.usernum);
228         help_subst(buffer,"^usernum",buf2);
229         help_subst(buffer,"^sysadm",config.c_sysadm);
230         help_subst(buffer,"^variantname",CITADEL);
231         sprintf(buf2,"%d",config.c_maxsessions);
232         help_subst(buffer,"^maxsessions",buf2);
233         }
234
235
236
237 /*
238  * memfmout()  -  Citadel text formatter and paginator.
239  *             Although the original purpose of this routine was to format
240  *             text to the reader's screen width, all we're really using it
241  *             for here is to format text out to 80 columns before sending it
242  *             to the client.  The client software may reformat it again.
243  */
244 void memfmout(int width, char *mptr, char subst)
245                         /* screen width to use */
246                         /* where are we going to get our text from? */
247                         /* nonzero if we should use hypertext mode */
248         {
249         int a,b,c,real,old;
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, int headers_only)
316 {
317         long msg_num;
318         int a,och,len;
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
325         struct cdbdata *dmsgtext;
326         char *mptr;
327
328         /* buffers needed for RFC822 translation */
329         char suser[256];
330         char luser[256];
331         char snode[256];
332         char lnode[256];
333         char mid[256];
334         long xtime;
335         /* */
336
337         msg_num = atol(msgid);
338
339
340         if ((!(CC->logged_in))&&(!(CC->internal_pgm))) {
341                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
342                 return;
343                 }
344         if (CC->curr_rm < 0) {
345                 cprintf("%d No room selected.\n",ERROR);
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=='P') cprintf("path=%s\n",buf);
432                 else if (ch=='U') cprintf("subj=%s\n",buf);
433                 else if (ch=='I') cprintf("msgn=%s\n",buf);
434                 else if (ch=='H') cprintf("hnod=%s\n",buf);
435                 else if (ch=='O') cprintf("room=%s\n",buf);
436                 else if (ch=='N') cprintf("node=%s\n",buf);
437                 else if (ch=='R') cprintf("rcpt=%s\n",buf);
438                 else if (ch=='T') cprintf("time=%s\n",buf);
439                 /* else cprintf("fld%c=%s\n",ch,buf); */
440                 }
441
442         /* begin header processing loop for RFC822 transfer format */
443
444         strcpy(suser, "");
445         strcpy(luser, "");
446         strcpy(snode, NODENAME);
447         strcpy(lnode, HUMANNODE);
448         if (mode == MT_RFC822) while(ch = *mptr++, (ch!='M' && ch!=0)) {
449                 buf[0] = 0;
450                 do {
451                         buf[strlen(buf)+1] = 0;
452                         rch = *mptr++;
453                         buf[strlen(buf)] = rch;
454                         } while (rch > 0);
455
456                 if (ch=='A') strcpy(luser, buf);
457                 else if (ch=='P') {
458                         cprintf("Path: %s\n",buf);
459                         for (a=0; a<strlen(buf); ++a) {
460                                 if (buf[a] == '!') {
461                                         strcpy(buf,&buf[a+1]);
462                                         a=0;
463                                         }
464                                 }
465                         strcpy(suser, buf);
466                         }
467                 else if (ch=='U') cprintf("Subject: %s\n",buf);
468                 else if (ch=='I') strcpy(mid, buf);
469                 else if (ch=='H') strcpy(lnode, buf);
470                 else if (ch=='O') cprintf("X-Citadel-Room: %s\n",buf);
471                 else if (ch=='N') strcpy(snode, buf);
472                 else if (ch=='R') cprintf("To: %s\n",buf);
473                 else if (ch=='T')  {
474                         xtime = atol(buf);
475                         cprintf("Date: %s", asctime(localtime(&xtime)));
476                         }
477                 }
478
479         if (mode == MT_RFC822) {
480                 if (!strcasecmp(snode, NODENAME)) {
481                         strcpy(snode, FQDN);
482                         }
483                 cprintf("Message-ID: <%s@%s>\n", mid, snode);
484                 PerformUserHooks(luser, (-1L), EVT_OUTPUTMSG);
485                 cprintf("From: %s@%s (%s)\n",
486                         suser, snode, luser);
487                 cprintf("Organization: %s\n", lnode);
488                 }
489
490         /* end header processing loop ... at this point, we're in the text */
491
492         if (ch==0) {
493                 cprintf("text\n*** ?Message truncated\n000\n");
494                 cdb_free(dmsgtext);
495                 return;
496                 }
497
498         if (headers_only) {
499                 /* give 'em a length */
500                 msg_len = 0L;
501                 while(och=ch, ch = *mptr++, ch>0) {
502                         ++msg_len;
503                         }
504                 cprintf("mlen=%ld\n", msg_len);
505                 cprintf("000\n");
506                 cdb_free(dmsgtext);
507                 return;
508                 }
509
510         /* signify start of msg text */
511         if (mode == MT_CITADEL) cprintf("text\n");
512         if (mode == MT_RFC822) cprintf("\n");
513
514         /* If the format type on disk is 1 (fixed-format), then we want
515          * everything to be output completely literally ... regardless of
516          * what message transfer format is in use.
517          */
518         if (format_type == 1) {
519                 och = 0;
520                 len = 0;
521                 while(och=ch, ch = *mptr++, ch>0) {
522                         if (ch == 13) ch = 10;
523                         ++len;
524                         /* if ((ch!=10)||(och!=10)) { */
525                                 cprintf("%c", ch);
526                                 if (ch==10) len = 0;
527                                 /* } */
528                         if (len>=250) {
529                                 len = 0;
530                                 /* cprintf("%c", ch); */
531                                 cprintf("%c", 10);
532                                 }
533                         }
534                 if (len!=0) cprintf("%c", 10);
535                 }
536         /* If the message on disk is format 0 (Citadel vari-format), we
537          * output using the formatter at 80 columns.  This is the final output
538          * form if the transfer format is RFC822, but if the transfer format
539          * is Citadel proprietary, it'll still work, because the indentation
540          * for new paragraphs is correct and the client will reformat the
541          * message to the reader's screen width.
542          */
543         if (format_type == 0) {
544                 memfmout(80,mptr,0);
545                 }
546
547
548         /* now we're done */
549         cprintf("000\n");
550         cdb_free(dmsgtext);
551         }
552
553
554 /*
555  * display a message (mode 0 - Citadel proprietary)
556  */
557 void cmd_msg0(char *cmdbuf)
558 {
559         char msgid[256];
560         int headers_only = 0;
561
562         extract(msgid,cmdbuf,0);
563         headers_only = extract_int(cmdbuf,1);
564
565         output_message(msgid,MT_CITADEL,headers_only);
566         }
567
568
569 /*
570  * display a message (mode 2 - RFC822)
571  */
572 void cmd_msg2(char *cmdbuf)
573 {
574         char msgid[256];
575         int headers_only = 0;
576
577         extract(msgid,cmdbuf,0);
578         headers_only = extract_int(cmdbuf,1);
579
580         output_message(msgid,MT_RFC822,headers_only);
581         }
582
583 /* 
584  * display a message (mode 3 - IGnet raw format - internal programs only)
585  */
586 void cmd_msg3(char *cmdbuf)
587 {
588         char msgid[256];
589         int headers_only = 0;
590
591         if (CC->internal_pgm == 0) {
592                 cprintf("%d This command is for internal programs only.\n",
593                         ERROR);
594                 return;
595                 }
596
597         extract(msgid,cmdbuf,0);
598         headers_only = extract_int(cmdbuf,1);
599
600         output_message(msgid,MT_RAW,headers_only);
601         }
602
603
604
605 /*
606  * Message base operation to send a message to the master file
607  * (returns new message number)
608  */
609 long send_message(char *message_in_memory,      /* pointer to buffer */
610                 size_t message_length,          /* length of buffer */
611                 int generate_id) {              /* 1 to generate an I field */
612
613         long newmsgid;
614
615         /* Get a new message number */
616         newmsgid = get_new_message_number();
617
618         /* Write our little bundle of joy into the message base */
619
620         lprintf(9, "Storing message %ld\n", newmsgid);
621         begin_critical_section(S_MSGMAIN);
622         if ( cdb_store(CDB_MSGMAIN, &newmsgid, sizeof(long),
623                         message_in_memory, message_length) < 0 ) {
624                 lprintf(2, "Can't store message\n");
625                 end_critical_section(S_MSGMAIN);
626                 return 0L;
627                 }
628         end_critical_section(S_MSGMAIN);
629
630         /* Finally, return the pointers */
631         return(newmsgid);
632         }
633
634
635
636
637
638
639 void loadtroom(void) {
640         struct quickroom qrbuf;
641         int a;
642         unsigned newflags;
643
644         /* first try to locate the twit room */
645         for (a=0; a<MAXROOMS; ++a) {
646                 getroom(&qrbuf,a);
647                 if (!strcasecmp(qrbuf.QRname,config.c_twitroom)) {
648                         twitroom = a;
649                         return;
650                         }
651                 }
652
653         /* if not found, try to create it  -  put it in the last slot */
654         twitroom = get_free_room_slot(-1);
655         if (twitroom>=0) {
656                 newflags = create_room(twitroom,config.c_twitroom,0,"",0);
657                 return;
658                 }
659
660         /* as a last resort, point to Aide> */
661         twitroom = 2;
662         }
663
664
665 /*
666  * this is a simple file copy routine.
667  */
668 void copy_file(char *from, char *to)
669 {
670         FILE *ffp,*tfp;
671         int a;
672
673         ffp=fopen(from,"r");
674         if (ffp==NULL) return;
675         tfp=fopen(to,"w");
676         if (tfp==NULL) {
677                 fclose(ffp);
678                 return;
679                 }
680         while (a=getc(ffp), a>=0) {
681                 putc(a,tfp);
682                 }
683         fclose(ffp);
684         fclose(tfp);
685         return;
686         }
687
688
689
690 /*
691  * message base operation to save a message and install its pointers
692  */
693 void save_message(char *mtmp,   /* file containing proper message */
694                 char *rec,      /* Recipient (if mail) */
695                 char mtsflag,   /* 0 for normal, 1 to force Aide> room */
696                 int mailtype,   /* local or remote type, see citadel.h */
697                 int generate_id) /* set to 1 to generate an 'I' field */
698 {
699         struct usersupp tempUS;
700         char aaa[100];
701         int hold_rm;
702         struct cdbdata *cdbmb;
703         long *dmailbox;
704         int dnum_mails;
705         long newmsgid;
706         char *message_in_memory;
707         struct stat statbuf;
708         size_t templen;
709         FILE *fp;
710
711         /* Measure the message */
712         lprintf(9, "Measuring the message\n");
713         stat(mtmp, &statbuf);
714         templen = statbuf.st_size;
715
716         /* Now read it into memory */
717         lprintf(9, "Allocating %ld bytes\n", templen);
718         message_in_memory = (char *) malloc(templen);
719         if (message_in_memory == NULL) {
720                 lprintf(2, "Can't allocate memory to save message!\n");
721                 return;
722                 }
723
724         lprintf(9, "Reading it into memory\n"); 
725         fp = fopen(mtmp, "rb");
726         fread(message_in_memory, templen, 1, fp);
727         fclose(fp);
728
729         newmsgid = send_message(message_in_memory, templen, generate_id);
730         free(message_in_memory);
731         if (newmsgid <= 0L) return;
732         hold_rm=(-1);
733
734         /* If the user is a twit, move to the twit room for posting... */
735         if (TWITDETECT) if (CC->usersupp.axlevel==2) {
736                 if (twitroom<0) loadtroom();
737                 hold_rm=CC->curr_rm;
738                 CC->curr_rm=twitroom;
739                 }
740
741         /* ...or if this message is destined for Aide> then go there. */
742         if (mtsflag) {
743                 hold_rm=CC->curr_rm;
744                 CC->curr_rm=2;
745                 }
746
747         /* This call to usergoto() changes rooms if necessary.  It also
748          * causes the latest message list to be read into memory.
749          */
750         usergoto(CC->curr_rm,0);
751
752         /* Store the message pointer, but NOT for sent mail! */
753         if (CC->curr_rm != 1) {
754
755                 /* read in the quickroom record, obtaining a lock... */
756                 lgetroom(&CC->quickroom,CC->curr_rm);
757                 get_msglist(CC->curr_rm);
758
759                 /* FIX here's where we have to to message expiry!! */
760
761                 /* Now add the new message */
762                 CC->num_msgs = CC->num_msgs + 1;
763                 CC->msglist = realloc(CC->msglist,
764                         ((CC->num_msgs) * sizeof(long)) );
765                 if (CC->msglist == NULL) {
766                         lprintf(3, "ERROR can't realloc message list!\n");
767                         }
768                 SetMessageInList(CC->num_msgs - 1, newmsgid);
769         
770                 /* Write it back to disk. */
771                 put_msglist(CC->curr_rm);
772         
773                 /* update quickroom */
774                 CC->quickroom.QRhighest = newmsgid;
775                 lputroom(&CC->quickroom,CC->curr_rm);
776                 }
777
778         /* Bump this user's messages posted counter.  Also, if the user is a
779          * twit, give them access to the twit room.
780          */
781         lgetuser(&CC->usersupp,CC->curr_user);
782         CC->usersupp.posted = CC->usersupp.posted + 1;
783         if (CC->curr_rm==twitroom) {
784                 CC->usersupp.generation[twitroom] = CC->quickroom.QRgen;
785                 }
786         lputuser(&CC->usersupp, CC->curr_user);
787
788         /* If mail, there's still more to do, if not, skip it. */
789         if ((CC->curr_rm!=1)||(mtsflag)) goto ENTFIN;
790
791         /* Network mail - send a copy to the network program. */
792         if (mailtype!=M_LOCAL) {
793                 sprintf(aaa,"./network/spoolin/nm.%d",getpid());
794                 copy_file(mtmp,aaa);
795                 system("exec nohup ./netproc >/dev/null 2>&1 &");
796                 }
797
798         /* Local mail - put a copy in the recipient's mailbox. */
799         /* FIX here's where we have to handle expiry, stuffed boxes, etc. */
800         if (mailtype == M_LOCAL) {
801                 if (lgetuser(&tempUS,rec)==0) {
802
803                         cdbmb = cdb_fetch(CDB_MAILBOXES,
804                                         &tempUS.usernum, sizeof(long));
805                         if (cdbmb != NULL) {
806                                 memcpy(dmailbox, cdbmb->ptr, cdbmb->len);
807                                 dnum_mails = cdbmb->len / sizeof(long);
808                                 cdb_free(cdbmb);
809                                 }
810                         else {
811                                 dmailbox = NULL;
812                                 dnum_mails = 0;
813                                 }
814         
815                         ++dnum_mails;
816                         if (dmailbox == NULL) {
817                                 dmailbox = malloc(sizeof(long) * dnum_mails);
818                                 }
819                         else {
820                                 dmailbox = realloc(dmailbox,
821                                                 sizeof(long) * dnum_mails);
822                                 }
823                         
824                         dmailbox[dnum_mails - 1] = newmsgid;
825                         cdb_store(CDB_MAILBOXES, &tempUS.usernum, sizeof(long),
826                                 dmailbox, (dnum_mails * sizeof(long)) );
827                         lputuser(&tempUS,rec);
828                         free(dmailbox);
829                         }
830                 }
831
832         /* If we've posted in a room other than the current room, then we
833          * have to now go back to the current room...
834          */
835 ENTFIN: if (hold_rm!=(-1)) {
836                 usergoto(hold_rm,0);
837                 }
838         unlink(mtmp);           /* delete the temporary file */
839         }
840
841
842 /*
843  * Generate an administrative message and post it in the Aide> room.
844  */
845 void aide_message(char *text)
846 {
847         long now;
848         FILE *fp;
849
850         time(&now);
851         fp=fopen(CC->temp,"wb");
852         fprintf(fp,"%c%c%c",255,MES_NORMAL,0);
853         fprintf(fp,"Psysop%c",0);
854         fprintf(fp,"T%ld%c",now,0);
855         fprintf(fp,"ACitadel%c",0);
856         fprintf(fp,"OAide%c",0);
857         fprintf(fp,"N%s%c",NODENAME,0);
858         fprintf(fp,"M%s\n%c",text,0);
859         fclose(fp);
860         save_message(CC->temp,"",1,M_LOCAL,1);
861         syslog(LOG_NOTICE,text);
862         }
863
864
865
866 /*
867  * Build a binary message to be saved on disk.
868  */
869 void make_message(char *filename, struct usersupp *author, char *recipient, char *room, int type, int net_type, int format_type, char *fake_name)
870                         /* temporary file name */
871                         /* author's usersupp structure */
872                         /* NULL if it's not mail */
873                         /* room where it's going */
874                         /* see MES_ types in header file */
875                         /* local or remote type, see citadel.h */
876                         /* format type (see citadel.h) */
877
878         FILE *fp;
879         int a;
880         long now;
881         char dest_node[32];
882         char buf[256];
883
884         /* Don't confuse the poor folks if it's not routed mail. */
885         strcpy(dest_node, "");
886
887         /* If net_type is M_BINARY, split out the destination node. */
888         if (net_type == M_BINARY) {
889                 strcpy(dest_node,NODENAME);
890                 for (a=0; a<strlen(recipient); ++a) {
891                         if (recipient[a]=='@') {
892                                 recipient[a]=0;
893                                 strcpy(dest_node,&recipient[a+1]);
894                                 }
895                         }
896                 }
897
898         /* if net_type is M_INTERNET, set the dest node to 'internet' */
899         if (net_type == M_INTERNET) {
900                 strcpy(dest_node,"internet");
901                 }
902
903         while (isspace(recipient[strlen(recipient)-1]))
904                 recipient[strlen(recipient)-1] = 0;
905
906         time(&now);
907         fp=fopen(filename,"w");
908         putc(255,fp);
909         putc(type,fp);  /* Normal or anonymous, see MES_ flags */
910         putc(format_type,fp);   /* Formatted or unformatted */
911         fprintf(fp,"Pcit%ld%c",author->usernum,0);      /* path */
912         fprintf(fp,"T%ld%c",now,0);                     /* date/time */
913         if (fake_name[0])
914            fprintf(fp,"A%s%c",fake_name,0);
915         else
916            fprintf(fp,"A%s%c",author->fullname,0);      /* author */
917         fprintf(fp,"O%s%c",CC->quickroom.QRname,0);     /* room */
918         fprintf(fp,"N%s%c",NODENAME,0);                 /* nodename */
919         fprintf(fp,"H%s%c",HUMANNODE,0);                /* human nodename */
920
921         if (recipient[0]!=0) fprintf(fp,"R%s%c",recipient,0);
922         if (dest_node[0]!=0) fprintf(fp,"D%s%c",dest_node,0);
923
924         putc('M',fp);
925
926         while (client_gets(buf), strcmp(buf,"000"))
927         {
928            fprintf(fp,"%s\n",buf);
929         }
930         syslog(LOG_INFO, "Closing message");
931         putc(0,fp);
932         fclose(fp);
933         }
934
935
936
937
938
939 /*
940  * message entry  -  mode 0 (normal) <bc>
941  */
942 void cmd_ent0(char *entargs)
943 {
944         int post = 0;
945         char recipient[256];
946         int anon_flag = 0;
947         int format_type = 0;
948         char newusername[256];          /* <bc> */
949
950         int a,b,e;
951         int mtsflag = 0;
952         struct usersupp tempUS;
953         char buf[256];
954
955         post = extract_int(entargs,0);
956         extract(recipient,entargs,1);
957         anon_flag = extract_int(entargs,2);
958         format_type = extract_int(entargs,3);
959
960         /* first check to make sure the request is valid. */
961
962         if (!(CC->logged_in)) {
963                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
964                 return;
965                 }
966         if (CC->curr_rm < 0) {
967                 cprintf("%d No room selected.\n",ERROR);
968                 return;
969                 }
970         if ((CC->usersupp.axlevel<2)&&(CC->curr_rm!=1)) {
971                 cprintf("%d Need to be validated to enter (except in Mail> to sysop)\n",
972                         ERROR+HIGHER_ACCESS_REQUIRED);
973                 return;
974                 }
975         if ((CC->usersupp.axlevel<4)&&(CC->quickroom.QRflags&QR_NETWORK)) {
976                 cprintf("%d Need net privileges to enter here.\n",
977                         ERROR+HIGHER_ACCESS_REQUIRED);
978                 return;
979                 }
980         if ((CC->usersupp.axlevel<6)&&(CC->quickroom.QRflags&QR_READONLY)) {
981                 cprintf("%d Sorry, this is a read-only room.\n",
982                         ERROR+HIGHER_ACCESS_REQUIRED);
983                 return;
984                 }
985
986         mtsflag=0;
987         
988                 
989         if (post==2) {                  /* <bc> */
990            if (CC->usersupp.axlevel<6)
991            {
992               cprintf("%d\nYou don't have sufficient permission to do an aide post.\n", ERROR+HIGHER_ACCESS_REQUIRED);
993               return;
994            }
995            extract(newusername,entargs,4);
996            bzero(CC->fake_postname, 32);
997            strcpy(CC->fake_postname, newusername);
998            cprintf("%d Ok\n",OK);
999            return;
1000         }
1001         
1002         CC->cs_flags |= CS_POSTING;
1003         
1004         buf[0]=0;
1005         if (CC->curr_rm==1) {
1006                 if (CC->usersupp.axlevel>=2) {
1007                         strcpy(buf,recipient);
1008                         }
1009                 else strcpy(buf,"sysop");
1010                 lprintf(9, "aliasing...\n");
1011                 e=alias(buf);                   /* alias and mail type */
1012                 lprintf(9,"...type is %d\n", e);
1013                 if ((buf[0]==0) || (e==M_ERROR)) {
1014                         cprintf("%d Unknown address - cannot send message.\n",
1015                                 ERROR+NO_SUCH_USER);
1016                         return;
1017                         }
1018                 if ((e!=M_LOCAL)&&(CC->usersupp.axlevel<4)) {
1019                         cprintf("%d Net privileges required for network mail.\n",
1020                                 ERROR+HIGHER_ACCESS_REQUIRED);
1021                         return;
1022                         }
1023                 if ((RESTRICT_INTERNET==1)&&(e==M_INTERNET)
1024                    &&((CC->usersupp.flags&US_INTERNET)==0)
1025                    &&(!CC->internal_pgm) ) {
1026                         cprintf("%d You don't have access to Internet mail.\n",
1027                                 ERROR+HIGHER_ACCESS_REQUIRED);
1028                         return;
1029                         }
1030                 if (!strcasecmp(buf,"sysop")) {
1031                         mtsflag=1;
1032                         goto SKFALL;
1033                         }
1034                 if (e!=M_LOCAL) goto SKFALL;    /* don't search local file  */
1035                 if (!strcasecmp(buf,CC->usersupp.fullname)) {
1036                         cprintf("%d Can't send mail to yourself!\n",
1037                                 ERROR+NO_SUCH_USER);
1038                         return;
1039                         }
1040
1041                 /* Check to make sure the user exists; also get the correct
1042                 * upper/lower casing of the name. 
1043                 */
1044                 lprintf(9, "checking validity of %s\n", buf);
1045                 a = getuser(&tempUS,buf);
1046                 lprintf(9, "getuser() returned %d\n", a);
1047                 if (a != 0) {
1048                         cprintf("%d No such user.\n",ERROR+NO_SUCH_USER);
1049                         return;
1050                         }
1051                 strcpy(buf,tempUS.fullname);
1052                 }
1053         
1054 SKFALL: b=MES_NORMAL;
1055         if (CC->quickroom.QRflags&QR_ANONONLY) b=MES_ANON;
1056         if (CC->quickroom.QRflags&QR_ANON2) {
1057                 if (anon_flag==1) b=MES_AN2;
1058                 }
1059         if (CC->curr_rm!=1) buf[0]=0;
1060
1061         /* If we're only checking the validity of the request, return
1062          * success without creating the message.
1063          */
1064         if (post==0) {
1065                 cprintf("%d %s\n",OK,buf);
1066                 return;
1067                 }
1068         
1069         cprintf("%d send message\n",SEND_LISTING);
1070         if (CC->fake_postname[0])
1071            make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, CC->fake_postname);
1072         else
1073            if (CC->fake_username[0])
1074               make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, CC->fake_username);
1075            else
1076               make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, "");
1077         save_message(CC->temp,buf,mtsflag,e,1);
1078         CC->fake_postname[0]='\0';
1079         return;
1080         }
1081
1082
1083
1084 /* 
1085  * message entry - mode 3 (raw)
1086  */
1087 void cmd_ent3(char *entargs)
1088 {
1089         char recp[256];
1090         char buf[256];
1091         int a, e;
1092         struct usersupp tempUS;
1093         long msglen;
1094         long bloklen;
1095         FILE *fp;
1096
1097         if (CC->internal_pgm == 0) {
1098                 cprintf("%d This command is for internal programs only.\n",
1099                         ERROR);
1100                 return;
1101                 }
1102
1103         if (CC->curr_rm < 0) {
1104                 cprintf("%d No room selected.\n",ERROR);
1105                 return;
1106                 }
1107
1108         if (CC->curr_rm == 1) { /* If we're in Mail, check the recipient */
1109                 extract(recp, entargs, 1);
1110                 lprintf(9, "aliasing...\n");
1111                 e=alias(recp);                  /* alias and mail type */
1112                 lprintf(9,"...type is %d\n", e);
1113                 if ((buf[0]==0) || (e==M_ERROR)) {
1114                         cprintf("%d Unknown address - cannot send message.\n",
1115                                 ERROR+NO_SUCH_USER);
1116                         return;
1117                         }
1118                 if (e == M_LOCAL) {
1119                         a = getuser(&tempUS,recp);
1120                         if (a!=0) {
1121                                 cprintf("%d No such user.\n", ERROR+NO_SUCH_USER);
1122                                 return;
1123                                 }
1124                         }
1125                 }
1126
1127         /* At this point, message has been approved. */
1128         if (extract_int(entargs, 0) == 0) {
1129                 cprintf("%d OK to send\n", OK);
1130                 return;
1131                 }
1132
1133         /* open a temp file to hold the message */
1134         fp = fopen(CC->temp, "wb");
1135         if (fp == NULL) {
1136                 cprintf("%d Cannot open %s: %s\n", 
1137                         ERROR + INTERNAL_ERROR,
1138                         CC->temp, strerror(errno) );
1139                 return;
1140                 }
1141
1142         msglen = extract_long(entargs, 2);
1143         cprintf("%d %ld\n", SEND_BINARY, msglen);
1144         while(msglen > 0L) {
1145                 bloklen = ((msglen >= 255L) ? 255 : msglen);
1146                 client_read(buf, (int)bloklen );
1147                 fwrite(buf, (int)bloklen, 1, fp);
1148                 msglen = msglen - bloklen;
1149                 }
1150         fclose(fp);
1151
1152         save_message(CC->temp, recp, 0, e, 0);
1153         }
1154
1155
1156 /*
1157  * Delete message from current room
1158  */
1159 void cmd_dele(char *delstr)
1160 {
1161         long delnum;
1162         int a,ok;
1163
1164         getuser(&CC->usersupp,CC->curr_user);
1165         if ((CC->usersupp.axlevel < 6)
1166            && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
1167                 cprintf("%d Higher access required.\n",
1168                         ERROR+HIGHER_ACCESS_REQUIRED);
1169                 return;
1170                 }
1171
1172         delnum = atol(delstr);
1173         if (CC->curr_rm==1) {
1174                 cprintf("%d Can't delete mail.\n",ERROR);
1175                 return;
1176                 }
1177         
1178         /* get room records, obtaining a lock... */
1179         lgetroom(&CC->quickroom,CC->curr_rm);
1180         get_msglist(CC->curr_rm);
1181
1182         ok = 0;
1183         if (CC->num_msgs > 0) for (a=0; a<(CC->num_msgs); ++a) {
1184                 if (MessageFromList(a) == delnum) {
1185                         SetMessageInList(a, 0L);
1186                         ok = 1;
1187                         }
1188                 }
1189
1190         CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
1191         CC->quickroom.QRhighest = MessageFromList(CC->num_msgs - 1);
1192
1193         put_msglist(CC->curr_rm);
1194         lputroom(&CC->quickroom,CC->curr_rm);
1195         if (ok==1) {
1196                 cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));
1197                 cprintf("%d Message deleted.\n",OK);
1198                 }
1199         else cprintf("%d No message %ld.\n",ERROR,delnum);
1200         } 
1201
1202
1203 /*
1204  * move a message to another room
1205  */
1206 void cmd_move(char *args)
1207 {
1208         long num;
1209         char targ[32];
1210         int a;
1211         int targ_slot;
1212         struct quickroom qtemp;
1213         int foundit;
1214         struct cdbdata *cdbtarg;
1215         long *targmsgs;
1216         int targ_count;
1217
1218         num = extract_long(args,0);
1219         extract(targ,args,1);
1220         
1221         if (CC->curr_rm < 0) {
1222                 cprintf("%d no room\n",ERROR);
1223                 return;
1224                 }
1225
1226         getuser(&CC->usersupp,CC->curr_user);
1227         if ((CC->usersupp.axlevel < 6)
1228            && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
1229                 cprintf("%d Higher access required.\n",
1230                         ERROR+HIGHER_ACCESS_REQUIRED);
1231                 return;
1232                 }
1233
1234         targ_slot = (-1);
1235         for (a=0; a<MAXROOMS; ++a) {
1236                 getroom(&qtemp,a);
1237                 if (!strcasecmp(qtemp.QRname,targ)) {
1238                         targ_slot = a;
1239                         a = MAXROOMS;
1240                         }
1241                 }
1242         if (targ_slot < 0) {
1243                 cprintf("%d '%s' does not exist.\n",ERROR,targ);
1244                 return;
1245                 }
1246
1247         /* yank the message out of the current room... */
1248         lgetroom(&CC->quickroom,CC->curr_rm);
1249         get_msglist(CC->curr_rm);
1250
1251         foundit = 0;
1252         for (a=0; a<(CC->num_msgs); ++a) {
1253                 if (MessageFromList(a) == num) {
1254                         foundit = 1;
1255                         SetMessageInList(a, 0L);
1256                         }
1257                 }
1258         if (foundit) {
1259                 CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
1260                 put_msglist(CC->curr_rm);
1261                 CC->quickroom.QRhighest = MessageFromList((CC->num_msgs)-1);
1262                 }
1263         lputroom(&CC->quickroom,CC->curr_rm);
1264         if (!foundit) {
1265                 cprintf("%d msg %ld does not exist.\n",ERROR,num);
1266                 return;
1267                 }
1268
1269         /* put the message into the target room */
1270         lgetroom(&qtemp,targ_slot);
1271         cdbtarg = cdb_fetch(CDB_MSGLISTS, &targ_slot, sizeof(int));
1272         if (cdbtarg != NULL) {
1273                 targmsgs = malloc(cdbtarg->len);
1274                 memcpy(targmsgs, cdbtarg->ptr, cdbtarg->len);
1275                 targ_count = cdbtarg->len / sizeof(long);
1276                 cdb_free(cdbtarg);
1277                 }
1278         else {
1279                 targmsgs = NULL;
1280                 targ_count = 0;
1281                 }
1282
1283         ++targ_count;
1284         targmsgs = realloc(targmsgs, ((CC->num_msgs) * sizeof(long)));
1285         targmsgs[targ_count - 1] = num;
1286         targ_count = sort_msglist(targmsgs, targ_count);
1287         qtemp.QRhighest = targmsgs[targ_count - 1];
1288         cdb_store(CDB_MSGLISTS, &targ_slot, sizeof(int),
1289                         targmsgs, targ_count * sizeof(long));
1290         free(targmsgs);
1291         lputroom(&qtemp,targ_slot);
1292         cprintf("%d ok\n",OK);
1293         }