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