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