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