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