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