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