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