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