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