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