]> code.citadel.org Git - citadel.git/blob - citadel/msgbase.c
Miscellaneous bug fixes. Also restored the ability to delete a user by
[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         FILE *fp;
598         long newmsgid;
599
600         char *message_in_memory;
601         size_t templen;
602
603         fp = fopen(filename, "rb");
604
605         /* Measure the message */
606         lprintf(9, "Measuring the message\n");
607         fseek(fp, 0L, SEEK_END);
608         templen = ftell(fp);
609
610         /* Now read it into memory */
611         lprintf(9, "Allocating %ld bytes\n", templen);
612         message_in_memory = (char *) malloc(templen);
613         if (message_in_memory == NULL) {
614                 lprintf(2, "Can't allocate memory to save message!\n");
615                 fclose(fp);     
616                 retbuf->smnumber = 0L;
617                 retbuf->smpos = 0L;
618                 return;
619                 }
620
621         lprintf(9, "Reading it into memory\n"); 
622         fseek(fp, 0L, SEEK_SET);
623         fread(message_in_memory, templen, 1, fp);
624         fclose(fp);
625
626         /* Get a new message number */
627         newmsgid = get_new_message_number();
628
629         /* Write our little bundle of joy into the message base */
630
631         lprintf(9, "Storing message %ld\n", newmsgid);
632         begin_critical_section(S_MSGMAIN);
633         if ( cdb_store(CDB_MSGMAIN, &newmsgid, sizeof(long),
634                         message_in_memory, templen) < 0 ) {
635                 lprintf(2, "Can't store message\n");
636                 retbuf->smnumber = 0L;
637                 retbuf->smpos = 0L;
638                 return;
639                 }
640         end_critical_section(S_MSGMAIN);
641         free(message_in_memory);
642
643
644         /* Finally, return the pointers */
645         retbuf->smnumber = newmsgid;
646         retbuf->smpos= 1L; /* FIX we really won't be needing this */
647         }
648
649
650
651
652
653
654 void loadtroom(void) {
655         struct quickroom qrbuf;
656         int a;
657         unsigned newflags;
658
659         /* first try to locate the twit room */
660         for (a=0; a<MAXROOMS; ++a) {
661                 getroom(&qrbuf,a);
662                 if (!strucmp(qrbuf.QRname,config.c_twitroom)) {
663                         twitroom = a;
664                         return;
665                         }
666                 }
667
668         /* if not found, try to create it  -  put it in the last slot */
669         twitroom = get_free_room_slot(-1);
670         if (twitroom>=0) {
671                 newflags = create_room(twitroom,config.c_twitroom,0,"",0);
672                 return;
673                 }
674
675         /* as a last resort, point to Aide> */
676         twitroom = 2;
677         }
678
679
680 /*
681  * this is a simple file copy routine.
682  */
683 void copy_file(char *from, char *to)
684 {
685         FILE *ffp,*tfp;
686         int a;
687
688         ffp=fopen(from,"r");
689         if (ffp==NULL) return;
690         tfp=fopen(to,"w");
691         if (tfp==NULL) {
692                 fclose(ffp);
693                 return;
694                 }
695         while (a=getc(ffp), a>=0) {
696                 putc(a,tfp);
697                 }
698         fclose(ffp);
699         fclose(tfp);
700         return;
701         }
702
703
704
705 /*
706  * message base operation to save a message and install its pointers
707  */
708 void save_message(char *mtmp,   /* file containing proper message */
709                 char *rec,      /* Recipient (if mail) */
710                 char mtsflag,   /* 0 for normal, 1 to force Aide> room */
711                 int mailtype,   /* local or remote type, see citadel.h */
712                 int generate_id) /* set to 1 to generate an 'I' field */
713 {
714         int a,e;
715         struct usersupp tempUS;
716         char aaa[100];
717         struct smreturn smreturn;
718         int hold_rm;
719
720         send_message(mtmp,&smreturn,generate_id);
721         hold_rm=(-1);
722
723         /* if the user is a twit, move to the twit room for posting */
724         if (TWITDETECT) if (CC->usersupp.axlevel==2) {
725                 if (twitroom<0) loadtroom();
726                 hold_rm=CC->curr_rm;
727                 CC->curr_rm=twitroom;
728                 }
729
730         /* and if this message is destined for Aide> then go there */
731         if (mtsflag) {
732                 hold_rm=CC->curr_rm;
733                 CC->curr_rm=2;
734                 }
735
736         /* this call to usergoto() changes rooms if necessary.  It also
737            causes the latest fullroom structure to be read into memory */
738         usergoto(CC->curr_rm,0);
739
740         /* Store the message pointer, but NOT for sent mail! */
741         if (CC->curr_rm != 1) {
742
743                 /* read in the quickroom record, obtaining a lock... */
744                 lgetroom(&CC->quickroom,CC->curr_rm);
745                 get_fullroom(&CC->fullroom,CC->curr_rm);
746
747                 /* Delete the oldest message if there is one */
748                 if (CC->fullroom.FRnum[0] != 0L) {
749                         cdb_delete(CDB_MSGMAIN,
750                                 &CC->fullroom.FRnum[0], sizeof(long));
751                         }
752
753                 /* Now scroll... */
754                 for (a=0; a<(MSGSPERRM-1); ++a) {
755                         CC->fullroom.FRnum[a]=CC->fullroom.FRnum[a+1];
756                         }
757         
758                 /* ...and add the new message */
759                 CC->fullroom.FRnum[MSGSPERRM-1]=smreturn.smnumber;
760         
761                 /* Write it back to disk. */
762                 put_fullroom(&CC->fullroom,CC->curr_rm);
763         
764                 /* update quickroom */
765                 CC->quickroom.QRhighest=CC->fullroom.FRnum[MSGSPERRM-1];
766                 lputroom(&CC->quickroom,CC->curr_rm);
767                 }
768
769         /* Bump this user's messages posted counter.  Also, if the user is a
770            twit, give them access to the twit room. */
771         lgetuser(&CC->usersupp,CC->curr_user);
772         CC->usersupp.posted = CC->usersupp.posted + 1;
773         if (CC->curr_rm==twitroom) CC->usersupp.generation[twitroom]=CC->quickroom.QRgen;
774         lputuser(&CC->usersupp,CC->curr_user);
775
776         /* if mail, there's still more to do, if not, skip it */
777         if ((CC->curr_rm!=1)||(mtsflag)) goto ENTFIN;
778
779         /* network mail - send a copy to the network program */
780         if (mailtype!=M_LOCAL) {
781                 sprintf(aaa,"./network/spoolin/nm.%d",getpid());
782                 copy_file(mtmp,aaa);
783                 system("exec nohup ./netproc >/dev/null 2>&1 &");
784                 }
785
786         /* local mail - put a copy in the recipient's mailbox */
787         if (mailtype==M_LOCAL) {
788                 if (lgetuser(&tempUS,rec)==0) {
789                         if (tempUS.mailnum[0] != 0L) {
790                                 cdb_delete(CDB_MSGMAIN, &tempUS.mailnum[0],
791                                         sizeof(long));
792                                 }
793                         for (e=0; e<(MAILSLOTS-1); ++e) {
794                                 tempUS.mailnum[e]=tempUS.mailnum[e+1];
795                                 }
796                         tempUS.mailnum[MAILSLOTS-1]=smreturn.smnumber;
797                         lputuser(&tempUS,rec);
798                         }
799                 }
800
801         /* if we've posted in a room other than the current room, then we
802            have to now go back to the current room... */
803 ENTFIN: if (hold_rm!=(-1)) {
804                 usergoto(hold_rm,0);
805                 }
806         unlink(mtmp);           /* delete the temporary file */
807         }
808
809
810 /*
811  * generate an administrative message and post it in the Aide> room
812  */
813 void aide_message(char *text)
814 {
815         long now;
816         FILE *fp;
817
818         time(&now);
819         fp=fopen(CC->temp,"wb");
820         fprintf(fp,"%c%c%c",255,MES_NORMAL,0);
821         fprintf(fp,"Psysop%c",0);
822         fprintf(fp,"T%ld%c",now,0);
823         fprintf(fp,"ACitadel%c",0);
824         fprintf(fp,"OAide%c",0);
825         fprintf(fp,"N%s%c",NODENAME,0);
826         fprintf(fp,"M%s\n%c",text,0);
827         fclose(fp);
828         save_message(CC->temp,"",1,M_LOCAL,1);
829         syslog(LOG_NOTICE,text);
830         }
831
832
833
834 /*
835  * build a binary message to be saved on disk
836  */
837 void make_message(char *filename, struct usersupp *author, char *recipient, char *room, int type, int net_type, int format_type, char *fake_name)
838                         /* temporary file name */
839                           /* author's usersupp structure */
840                         /* NULL if it's not mail */
841                         /* room where it's going */
842                         /* see MES_ types in header file */
843                         /* local or remote type, see citadel.h */
844                         /* format type (see citadel.h) */
845
846         FILE *fp;
847         int a;
848         long now;
849         char dest_node[32];
850         char buf[256];
851
852         /* don't confuse the poor folks if it's not routed mail. */
853         strcpy(dest_node,"");
854
855         /* if net_type is M_BINARY, split out the destination node */
856         if (net_type == M_BINARY) {
857                 strcpy(dest_node,NODENAME);
858                 for (a=0; a<strlen(recipient); ++a) {
859                         if (recipient[a]=='@') {
860                                 recipient[a]=0;
861                                 strcpy(dest_node,&recipient[a+1]);
862                                 }
863                         }
864                 }
865
866         /* if net_type is M_INTERNET, set the dest node to 'internet' */
867         if (net_type == M_INTERNET) {
868                 strcpy(dest_node,"internet");
869                 }
870
871         while (isspace(recipient[strlen(recipient)-1]))
872                 recipient[strlen(recipient)-1] = 0;
873
874         time(&now);
875         fp=fopen(filename,"w");
876         putc(255,fp);
877         putc(type,fp);  /* Normal or anonymous, see MES_ flags */
878         putc(format_type,fp);   /* Formatted or unformatted */
879         fprintf(fp,"Pcit%ld%c",author->usernum,0);      /* path */
880         fprintf(fp,"T%ld%c",now,0);                     /* date/time */
881         if (fake_name[0])
882            fprintf(fp,"A%s%c",fake_name,0);
883         else
884            fprintf(fp,"A%s%c",author->fullname,0);      /* author */
885         fprintf(fp,"O%s%c",CC->quickroom.QRname,0);     /* room */
886         fprintf(fp,"N%s%c",NODENAME,0);                 /* nodename */
887         fprintf(fp,"H%s%c",HUMANNODE,0);                /* human nodename */
888
889         if (recipient[0]!=0) fprintf(fp,"R%s%c",recipient,0);
890         if (dest_node[0]!=0) fprintf(fp,"D%s%c",dest_node,0);
891
892         putc('M',fp);
893
894         while (client_gets(buf), strcmp(buf,"000"))
895         {
896            fprintf(fp,"%s\n",buf);
897         }
898         syslog(LOG_INFO, "Closing message");
899         putc(0,fp);
900         fclose(fp);
901         }
902
903
904
905
906
907 /*
908  * message entry  -  mode 0 (normal) <bc>
909  */
910 void cmd_ent0(char *entargs)
911 {
912         int post = 0;
913         char recipient[256];
914         int anon_flag = 0;
915         int format_type = 0;
916         char newusername[256];          /* <bc> */
917
918         int a,b,e;
919         int mtsflag = 0;
920         struct usersupp tempUS;
921         char buf[256];
922
923         post = extract_int(entargs,0);
924         extract(recipient,entargs,1);
925         anon_flag = extract_int(entargs,2);
926         format_type = extract_int(entargs,3);
927
928         /* first check to make sure the request is valid. */
929
930         if (!(CC->logged_in)) {
931                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
932                 return;
933                 }
934         if (CC->curr_rm < 0) {
935                 cprintf("%d No room selected.\n",ERROR);
936                 return;
937                 }
938         if ((CC->usersupp.axlevel<2)&&(CC->curr_rm!=1)) {
939                 cprintf("%d Need to be validated to enter (except in Mail> to sysop)\n",
940                         ERROR+HIGHER_ACCESS_REQUIRED);
941                 return;
942                 }
943         if ((CC->usersupp.axlevel<4)&&(CC->quickroom.QRflags&QR_NETWORK)) {
944                 cprintf("%d Need net privileges to enter here.\n",
945                         ERROR+HIGHER_ACCESS_REQUIRED);
946                 return;
947                 }
948         if ((CC->usersupp.axlevel<6)&&(CC->quickroom.QRflags&QR_READONLY)) {
949                 cprintf("%d Sorry, this is a read-only room.\n",
950                         ERROR+HIGHER_ACCESS_REQUIRED);
951                 return;
952                 }
953
954         mtsflag=0;
955         
956                 
957         if (post==2) {                  /* <bc> */
958            if (CC->usersupp.axlevel<6)
959            {
960               cprintf("%d\nYou don't have sufficient permission to do an aide post.\n", ERROR+HIGHER_ACCESS_REQUIRED);
961               return;
962            }
963            extract(newusername,entargs,4);
964            bzero(CC->fake_postname, 32);
965            strcpy(CC->fake_postname, newusername);
966            cprintf("%d Ok\n",OK);
967            return;
968         }
969         
970         CC->cs_flags |= CS_POSTING;
971         
972         buf[0]=0;
973         if (CC->curr_rm==1) {
974                 if (CC->usersupp.axlevel>=2) {
975                         strcpy(buf,recipient);
976                         }
977                 else strcpy(buf,"sysop");
978                 lprintf(9, "aliasing...\n");
979                 e=alias(buf);                   /* alias and mail type */
980                 lprintf(9,"...type is %d\n", e);
981                 if ((buf[0]==0) || (e==M_ERROR)) {
982                         cprintf("%d Unknown address - cannot send message.\n",
983                                 ERROR+NO_SUCH_USER);
984                         return;
985                         }
986                 if ((e!=M_LOCAL)&&(CC->usersupp.axlevel<4)) {
987                         cprintf("%d Net privileges required for network mail.\n",
988                                 ERROR+HIGHER_ACCESS_REQUIRED);
989                         return;
990                         }
991                 if ((RESTRICT_INTERNET==1)&&(e==M_INTERNET)
992                    &&((CC->usersupp.flags&US_INTERNET)==0)
993                    &&(!CC->internal_pgm) ) {
994                         cprintf("%d You don't have access to Internet mail.\n",
995                                 ERROR+HIGHER_ACCESS_REQUIRED);
996                         return;
997                         }
998                 if (!strucmp(buf,"sysop")) {
999                         mtsflag=1;
1000                         goto SKFALL;
1001                         }
1002                 if (e!=M_LOCAL) goto SKFALL;    /* don't search local file  */
1003                 if (!strucmp(buf,CC->usersupp.fullname)) {
1004                         cprintf("%d Can't send mail to yourself!\n",
1005                                 ERROR+NO_SUCH_USER);
1006                         return;
1007                         }
1008
1009                 /* Check to make sure the user exists; also get the correct
1010                 * upper/lower casing of the name. 
1011                 */
1012                 lprintf(9, "checking validity of %s\n", buf);
1013                 a = getuser(&tempUS,buf);
1014                 lprintf(9, "getuser() returned %d\n", a);
1015                 if (a != 0) {
1016                         cprintf("%d No such user.\n",ERROR+NO_SUCH_USER);
1017                         return;
1018                         }
1019                 strcpy(buf,tempUS.fullname);
1020                 }
1021         
1022 SKFALL: b=MES_NORMAL;
1023         if (CC->quickroom.QRflags&QR_ANONONLY) b=MES_ANON;
1024         if (CC->quickroom.QRflags&QR_ANON2) {
1025                 if (anon_flag==1) b=MES_AN2;
1026                 }
1027         if (CC->curr_rm!=1) buf[0]=0;
1028
1029         /* If we're only checking the validity of the request, return
1030          * success without creating the message.
1031          */
1032         if (post==0) {
1033                 cprintf("%d %s\n",OK,buf);
1034                 return;
1035                 }
1036         
1037         cprintf("%d send message\n",SEND_LISTING);
1038         if (CC->fake_postname[0])
1039            make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, CC->fake_postname);
1040         else
1041            if (CC->fake_username[0])
1042               make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, CC->fake_username);
1043            else
1044               make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, "");
1045         save_message(CC->temp,buf,mtsflag,e,1);
1046         CC->fake_postname[0]='\0';
1047         return;
1048         }
1049
1050
1051
1052 /* 
1053  * message entry - mode 3 (raw)
1054  */
1055 void cmd_ent3(char *entargs)
1056 {
1057         char recp[256];
1058         char buf[256];
1059         int a, e;
1060         struct usersupp tempUS;
1061         long msglen;
1062         long bloklen;
1063         FILE *fp;
1064
1065         if (CC->internal_pgm == 0) {
1066                 cprintf("%d This command is for internal programs only.\n",
1067                         ERROR);
1068                 return;
1069                 }
1070
1071         if (CC->curr_rm < 0) {
1072                 cprintf("%d No room selected.\n",ERROR);
1073                 return;
1074                 }
1075
1076         if (CC->curr_rm == 1) { /* If we're in Mail, check the recipient */
1077                 extract(recp, entargs, 1);
1078                 lprintf(9, "aliasing...\n");
1079                 e=alias(recp);                  /* alias and mail type */
1080                 lprintf(9,"...type is %d\n", e);
1081                 if ((buf[0]==0) || (e==M_ERROR)) {
1082                         cprintf("%d Unknown address - cannot send message.\n",
1083                                 ERROR+NO_SUCH_USER);
1084                         return;
1085                         }
1086                 if (e == M_LOCAL) {
1087                         a = getuser(&tempUS,recp);
1088                         if (a!=0) {
1089                                 cprintf("%d No such user.\n", ERROR+NO_SUCH_USER);
1090                                 return;
1091                                 }
1092                         }
1093                 }
1094
1095         /* At this point, message has been approved. */
1096         if (extract_int(entargs, 0) == 0) {
1097                 cprintf("%d OK to send\n", OK);
1098                 return;
1099                 }
1100
1101         /* open a temp file to hold the message */
1102         fp = fopen(CC->temp, "wb");
1103         if (fp == NULL) {
1104                 cprintf("%d Cannot open %s: %s\n", 
1105                         ERROR + INTERNAL_ERROR,
1106                         CC->temp, strerror(errno) );
1107                 return;
1108                 }
1109
1110         msglen = extract_long(entargs, 2);
1111         cprintf("%d %ld\n", SEND_BINARY, msglen);
1112         while(msglen > 0L) {
1113                 bloklen = ((msglen >= 255L) ? 255 : msglen);
1114                 client_read(buf, (int)bloklen );
1115                 fwrite(buf, (int)bloklen, 1, fp);
1116                 msglen = msglen - bloklen;
1117                 }
1118         fclose(fp);
1119
1120         save_message(CC->temp, recp, 0, e, 0);
1121         }
1122
1123
1124 /*
1125  * Delete message from current room
1126  */
1127 void cmd_dele(char *delstr)
1128 {
1129         long delnum;
1130         int a,ok;
1131
1132         getuser(&CC->usersupp,CC->curr_user);
1133         if ((CC->usersupp.axlevel < 6)
1134            && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
1135                 cprintf("%d Higher access required.\n",
1136                         ERROR+HIGHER_ACCESS_REQUIRED);
1137                 return;
1138                 }
1139
1140         delnum = atol(delstr);
1141         if (CC->curr_rm==1) {
1142                 cprintf("%d Can't delete mail.\n",ERROR);
1143                 return;
1144                 }
1145         
1146         /* get room records, obtaining a lock... */
1147         lgetroom(&CC->quickroom,CC->curr_rm);
1148         get_fullroom(&CC->fullroom,CC->curr_rm);
1149
1150         ok = 0;
1151         for (a=0; a<MSGSPERRM; ++a)
1152                 if (CC->fullroom.FRnum[a]==delnum) {
1153                         CC->fullroom.FRnum[a]=0L;
1154                         ok = 1;
1155                         }
1156
1157         sort_fullroom(&CC->fullroom);
1158         CC->quickroom.QRhighest = CC->fullroom.FRnum[MSGSPERRM-1];
1159
1160         put_fullroom(&CC->fullroom,CC->curr_rm);
1161         lputroom(&CC->quickroom,CC->curr_rm);
1162         if (ok==1) {
1163                 cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));
1164                 cprintf("%d Message deleted.\n",OK);
1165                 }
1166         else cprintf("%d No message %ld.\n",ERROR,delnum);
1167         } 
1168
1169
1170 /*
1171  * move a message to another room
1172  */
1173 void cmd_move(char *args)
1174 {
1175         long num;
1176         char targ[32];
1177         int a;
1178         int targ_slot;
1179         struct quickroom qtemp;
1180         struct fullroom ftemp;
1181         int foundit;
1182
1183         num = extract_long(args,0);
1184         extract(targ,args,1);
1185         
1186         if (CC->curr_rm < 0) {
1187                 cprintf("%d no room\n",ERROR);
1188                 return;
1189                 }
1190
1191         getuser(&CC->usersupp,CC->curr_user);
1192         if ((CC->usersupp.axlevel < 6)
1193            && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
1194                 cprintf("%d Higher access required.\n",
1195                         ERROR+HIGHER_ACCESS_REQUIRED);
1196                 return;
1197                 }
1198
1199         targ_slot = (-1);
1200         for (a=0; a<MAXROOMS; ++a) {
1201                 getroom(&qtemp,a);
1202                 if (!strucmp(qtemp.QRname,targ)) {
1203                         targ_slot = a;
1204                         a = MAXROOMS;
1205                         }
1206                 }
1207         if (targ_slot < 0) {
1208                 cprintf("%d '%s' does not exist.\n",ERROR,targ);
1209                 return;
1210                 }
1211
1212         /* yank the message out of the current room... */
1213         lgetroom(&CC->quickroom,CC->curr_rm);
1214         get_fullroom(&CC->fullroom,CC->curr_rm);
1215
1216         foundit = 0;
1217         for (a=0; a<MSGSPERRM; ++a) {
1218                 if (CC->fullroom.FRnum[a] == num) {
1219                         foundit = 1;
1220                         CC->fullroom.FRnum[a] = 0L;
1221                         }
1222                 }
1223         if (foundit) {
1224                 sort_fullroom(&CC->fullroom);
1225                 put_fullroom(&CC->fullroom,CC->curr_rm);
1226                 CC->quickroom.QRhighest = CC->fullroom.FRnum[MSGSPERRM-1];
1227                 }
1228         lputroom(&CC->quickroom,CC->curr_rm);
1229         if (!foundit) {
1230                 cprintf("%d msg %ld does not exist.\n",ERROR,num);
1231                 return;
1232                 }
1233
1234         lgetroom(&qtemp,targ_slot);
1235         get_fullroom(&ftemp,targ_slot);
1236         if (CC->fullroom.FRnum[0] != 0L) {
1237                 cdb_delete(CDB_MSGMAIN, &CC->fullroom.FRnum[0], sizeof(long));
1238                 }
1239         for (a=0; a<MSGSPERRM-1; ++a) {
1240                 ftemp.FRnum[a]=ftemp.FRnum[a+1];
1241                 }
1242         ftemp.FRnum[MSGSPERRM-1] = num;
1243         sort_fullroom(&ftemp);
1244         qtemp.QRhighest = ftemp.FRnum[MSGSPERRM-1];
1245         put_fullroom(&ftemp,targ_slot);
1246         lputroom(&qtemp,targ_slot);
1247         cprintf("%d ok\n",OK);
1248         }