22 #include "sysdep_decls.h"
23 #include "citserver.h"
28 #include "dynloader.h"
30 #include "mime_parser.h"
39 #define desired_section ((char *)CtdlGetUserData(SYM_DESIRED_SECTION))
41 extern struct config config;
45 * This function is self explanatory.
46 * (What can I say, I'm in a weird mood today...)
48 void remove_any_whitespace_to_the_left_or_right_of_at_symbol(char *name) {
51 for (i=0; i<strlen(name); ++i) if (name[i]=='@') {
52 if (i>0) if (isspace(name[i-1])) {
53 strcpy(&name[i-1], &name[i]);
56 while (isspace(name[i+1])) {
57 strcpy(&name[i+1], &name[i+2]);
64 * Aliasing for network mail.
65 * (Error messages have been commented out, because this is a server.)
67 int alias(char *name) /* process alias and routing info for mail */
71 char aaa[300],bbb[300];
73 lprintf(9, "alias() called for <%s>\n", name);
75 remove_any_whitespace_to_the_left_or_right_of_at_symbol(name);
77 fp=fopen("network/mail.aliases","r");
78 if (fp==NULL) fp=fopen("/dev/null","r");
79 if (fp==NULL) return(MES_ERROR);
80 strcpy(aaa,""); strcpy(bbb,"");
81 while (fgets(aaa, sizeof aaa, fp)!=NULL) {
82 while (isspace(name[0])) strcpy(name, &name[1]);
83 aaa[strlen(aaa)-1] = 0;
85 for (a=0; a<strlen(aaa); ++a) {
87 strcpy(bbb, &aaa[a+1]);
91 if (!strcasecmp(name, aaa)) strcpy(name, bbb);
94 lprintf(7, "Mail is being forwarded to %s\n", name);
96 /* determine local or remote type, see citadel.h */
97 for (a=0; a<strlen(name); ++a) if (name[a]=='!') return(MES_INTERNET);
98 for (a=0; a<strlen(name); ++a)
100 for (b=a; b<strlen(name); ++b)
101 if (name[b]=='.') return(MES_INTERNET);
102 b=0; for (a=0; a<strlen(name); ++a) if (name[a]=='@') ++b;
104 lprintf(7, "Too many @'s in address\n");
108 for (a=0; a<strlen(name); ++a)
109 if (name[a]=='@') strcpy(bbb,&name[a+1]);
110 while (bbb[0]==32) strcpy(bbb,&bbb[1]);
111 fp = fopen("network/mail.sysinfo","r");
112 if (fp==NULL) return(MES_ERROR);
115 } while ((a>=0)&&(strcasecmp(aaa,bbb)));
117 if (!strncmp(aaa,"use ",4)) {
123 if (!strncmp(aaa,"uum",3)) {
125 for (a=0; a<strlen(bbb); ++a) {
126 if (bbb[a]=='@') bbb[a]=0;
127 if (bbb[a]==' ') bbb[a]='_';
129 while(bbb[strlen(bbb)-1]=='_') bbb[strlen(bbb)-1]=0;
130 sprintf(name,&aaa[4],bbb);
131 return(MES_INTERNET);
133 if (!strncmp(aaa,"bin",3)) {
134 strcpy(aaa,name); strcpy(bbb,name);
135 while (aaa[strlen(aaa)-1]!='@') aaa[strlen(aaa)-1]=0;
136 aaa[strlen(aaa)-1]=0;
137 while (aaa[strlen(aaa)-1]==' ') aaa[strlen(aaa)-1]=0;
138 while (bbb[0]!='@') strcpy(bbb,&bbb[1]);
140 while (bbb[0]==' ') strcpy(bbb,&bbb[1]);
141 sprintf(name,"%s @%s",aaa,bbb);
153 fp=fopen("citadel.control","r");
154 fread((char *)&CitControl,sizeof(struct CitControl),1,fp);
159 * cmd_msgs() - get list of message #'s in this room
161 void cmd_msgs(char *cmdbuf)
170 extract(which,cmdbuf,0);
174 if (!strncasecmp(which,"OLD",3)) mode = MSGS_OLD;
175 if (!strncasecmp(which,"NEW",3)) mode = MSGS_NEW;
176 if (!strncasecmp(which,"FIRST",5)) {
178 cm_howmany = extract_int(cmdbuf,1);
180 if (!strncasecmp(which,"LAST",4)) {
182 cm_howmany = extract_int(cmdbuf,1);
184 if (!strncasecmp(which,"GT",2)) {
186 cm_gt = extract_long(cmdbuf,1);
189 if ((!(CC->logged_in))&&(!(CC->internal_pgm))) {
190 cprintf("%d not logged in\n",ERROR+NOT_LOGGED_IN);
194 get_msglist(&CC->quickroom);
195 getuser(&CC->usersupp,CC->curr_user);
196 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
198 cprintf("%d Message list...\n",LISTING_FOLLOWS);
199 if (CC->num_msgs != 0) {
200 for (a=0; a<(CC->num_msgs); ++a)
201 if ((MessageFromList(a) >=0)
205 || ((mode==MSGS_OLD) && (MessageFromList(a) <= vbuf.v_lastseen))
206 || ((mode==MSGS_NEW) && (MessageFromList(a) > vbuf.v_lastseen))
207 || ((mode==MSGS_NEW) && (MessageFromList(a) >= vbuf.v_lastseen)
208 && (CC->usersupp.flags & US_LASTOLD))
209 || ((mode==MSGS_LAST)&& (a>=(CC->num_msgs-cm_howmany)))
210 || ((mode==MSGS_FIRST)&&(a<cm_howmany))
211 || ((mode==MSGS_GT) && (MessageFromList(a) > cm_gt))
215 cprintf("%ld\n", MessageFromList(a));
224 * help_subst() - support routine for help file viewer
226 void help_subst(char *strbuf, char *source, char *dest)
231 while (p=pattern2(strbuf,source), (p>=0)) {
232 strcpy(workbuf,&strbuf[p+strlen(source)]);
233 strcpy(&strbuf[p],dest);
234 strcat(strbuf,workbuf);
239 void do_help_subst(char *buffer)
243 help_subst(buffer,"^nodename",config.c_nodename);
244 help_subst(buffer,"^humannode",config.c_humannode);
245 help_subst(buffer,"^fqdn",config.c_fqdn);
246 help_subst(buffer,"^username",CC->usersupp.fullname);
247 sprintf(buf2,"%ld",CC->usersupp.usernum);
248 help_subst(buffer,"^usernum",buf2);
249 help_subst(buffer,"^sysadm",config.c_sysadm);
250 help_subst(buffer,"^variantname",CITADEL);
251 sprintf(buf2,"%d",config.c_maxsessions);
252 help_subst(buffer,"^maxsessions",buf2);
258 * memfmout() - Citadel text formatter and paginator.
259 * Although the original purpose of this routine was to format
260 * text to the reader's screen width, all we're really using it
261 * for here is to format text out to 80 columns before sending it
262 * to the client. The client software may reformat it again.
264 void memfmout(int width, char *mptr, char subst)
265 /* screen width to use */
266 /* where are we going to get our text from? */
267 /* nonzero if we should use hypertext mode */
276 strcpy(aaa,""); old=255;
278 c=1; /* c is the current pos */
281 while (ch=*mptr, ((ch!=0) && (strlen(buffer)<126) )) {
283 buffer[strlen(buffer)+1] = 0;
284 buffer[strlen(buffer)] = ch;
287 if (buffer[0]=='^') do_help_subst(buffer);
289 buffer[strlen(buffer)+1] = 0;
291 strcpy(buffer,&buffer[1]);
298 if (ch<=0) goto FMTEND;
300 if ( ((ch==13)||(ch==10)) && (old!=13) && (old!=10) ) ch=32;
301 if ( ((old==13)||(old==10)) && (isspace(real)) ) {
305 if (ch>126) goto FMTA;
308 if ( ((strlen(aaa)+c)>(width-5)) && (strlen(aaa)>(width-5)) )
309 { cprintf("\n%s",aaa); c=strlen(aaa); aaa[0]=0;
311 b=strlen(aaa); aaa[b]=ch; aaa[b+1]=0; }
313 if ((strlen(aaa)+c)>(width-5)) {
317 cprintf("%s ",aaa); ++c; c=c+strlen(aaa);
321 if ((ch==13)||(ch==10)) {
329 FMTEND: cprintf("%s\n", aaa);
335 * Callback function for mime parser that simply lists the part
337 void list_this_part(char *name, char *filename, char *partnum, char *disp,
338 void *content, char *cbtype, size_t length) {
340 cprintf("part=%s|%s|%s|%s|%s|%d\n",
341 name, filename, partnum, disp, cbtype, length);
346 * Callback function for mime parser that wants to display text
348 void fixed_output(char *name, char *filename, char *partnum, char *disp,
349 void *content, char *cbtype, size_t length) {
351 if (!strcasecmp(cbtype, "text/plain")) {
352 client_write(content, length);
355 cprintf("Part %s: %s (%s) (%d bytes)\n",
356 partnum, filename, cbtype, length);
362 * Callback function for mime parser that opens a section for downloading
364 void mime_download(char *name, char *filename, char *partnum, char *disp,
365 void *content, char *cbtype, size_t length) {
367 /* Silently go away if there's already a download open... */
368 if (CC->download_fp != NULL) return;
370 /* ...or if this is not the desired section */
371 if (strcasecmp(desired_section, partnum)) return;
373 CC->download_fp = tmpfile();
374 if (CC->download_fp == NULL) return;
376 fwrite(content, length, 1, CC->download_fp);
377 fflush(CC->download_fp);
378 rewind(CC->download_fp);
380 OpenCmdResult(filename, cbtype);
386 * Get a message off disk. (return value is the message's timestamp)
389 time_t output_message(char *msgid, int mode, int headers_only) {
393 CIT_UBYTE format_type,anon_flag;
398 struct cdbdata *dmsgtext;
401 /* buffers needed for RFC822 translation */
410 msg_num = atol(msgid);
413 if ((!(CC->logged_in))&&(!(CC->internal_pgm))&&(mode!=MT_DATE)) {
414 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
418 /* We used to need to check in the current room's message list
419 * to determine where the message's disk position. We no longer need
420 * to do this, but we do it anyway as a security measure, in order to
421 * prevent rogue clients from reading messages not in the current room.
425 if (CC->num_msgs > 0) {
426 for (a=0; a<CC->num_msgs; ++a) {
427 if (MessageFromList(a) == msg_num) {
435 cprintf("%d Message %ld is not in this room.\n",
441 dmsgtext = cdb_fetch(CDB_MSGMAIN, &msg_num, sizeof(long));
443 if (dmsgtext == NULL) {
445 cprintf("%d Can't find message %ld\n",
446 ERROR+INTERNAL_ERROR);
450 msg_len = (long) dmsgtext->len;
451 mptr = dmsgtext->ptr;
453 /* this loop spews out the whole message if we're doing raw format */
454 if (mode == MT_RAW) {
455 cprintf("%d %ld\n", BINARY_FOLLOWS, msg_len);
456 client_write(dmsgtext->ptr, (int) msg_len);
461 /* Otherwise, we'll start parsing it field by field... */
464 cprintf("%d Illegal message format on disk\n",
465 ERROR+INTERNAL_ERROR);
471 format_type = *mptr++;
473 /* Are we downloading a MIME component? */
474 if (mode == MT_DOWNLOAD) {
475 if (format_type != 4) {
476 cprintf("%d This is not a MIME message.\n",
479 else if (CC->download_fp != NULL) {
480 cprintf("%d You already have a download open.\n",
484 /* Skip to the message body */
485 while(ch = *mptr++, (ch!='M' && ch!=0)) {
488 buf[strlen(buf)+1] = 0;
490 buf[strlen(buf)] = rch;
494 mime_parser(mptr, NULL, *mime_download);
495 /* If there's no file open by this time, the requested
496 * section wasn't found, so print an error
498 if (CC->download_fp == NULL) {
499 cprintf("%d Section %s not found.\n",
500 ERROR+FILE_NOT_FOUND,
508 /* Are we just looking for the message date? */
509 if (mode == MT_DATE) while(ch = *mptr++, (ch!='M' && ch!=0)) {
512 buf[strlen(buf)+1] = 0;
514 buf[strlen(buf)] = rch;
525 /* now for the user-mode message reading loops */
526 cprintf("%d Message %ld:\n",LISTING_FOLLOWS,msg_num);
528 if (mode == MT_CITADEL) cprintf("type=%d\n", format_type);
530 if ( (anon_flag == MES_ANON) && (mode == MT_CITADEL) ) {
531 cprintf("nhdr=yes\n");
534 /* begin header processing loop for Citadel message format */
536 if ((mode == MT_CITADEL)||(mode == MT_MIME))
537 while(ch = *mptr++, (ch!='M' && ch!=0)) {
540 buf[strlen(buf)+1] = 0;
542 buf[strlen(buf)] = rch;
546 PerformUserHooks(buf, (-1L), EVT_OUTPUTMSG);
547 if (anon_flag==MES_ANON) cprintf("from=****");
548 else if (anon_flag==MES_AN2) cprintf("from=anonymous");
549 else cprintf("from=%s",buf);
550 if ((is_room_aide()) && ((anon_flag == MES_ANON)
551 || (anon_flag == MES_AN2)))
552 cprintf(" [%s]",buf);
555 else if (ch=='P') cprintf("path=%s\n",buf);
556 else if (ch=='U') cprintf("subj=%s\n",buf);
557 else if (ch=='I') cprintf("msgn=%s\n",buf);
558 else if (ch=='H') cprintf("hnod=%s\n",buf);
559 else if (ch=='O') cprintf("room=%s\n",buf);
560 else if (ch=='N') cprintf("node=%s\n",buf);
561 else if (ch=='R') cprintf("rcpt=%s\n",buf);
562 else if (ch=='T') cprintf("time=%s\n",buf);
563 /* else cprintf("fld%c=%s\n",ch,buf); */
566 /* begin header processing loop for RFC822 transfer format */
570 strcpy(snode, NODENAME);
571 strcpy(lnode, HUMANNODE);
572 if (mode == MT_RFC822) while(ch = *mptr++, (ch!='M' && ch!=0)) {
575 buf[strlen(buf)+1] = 0;
577 buf[strlen(buf)] = rch;
580 if (ch=='A') strcpy(luser, buf);
582 cprintf("Path: %s\n",buf);
583 for (a=0; a<strlen(buf); ++a) {
585 strcpy(buf,&buf[a+1]);
591 else if (ch=='U') cprintf("Subject: %s\n",buf);
592 else if (ch=='I') strcpy(mid, buf);
593 else if (ch=='H') strcpy(lnode, buf);
594 else if (ch=='O') cprintf("X-Citadel-Room: %s\n",buf);
595 else if (ch=='N') strcpy(snode, buf);
596 else if (ch=='R') cprintf("To: %s\n",buf);
599 cprintf("Date: %s", asctime(localtime(&xtime)));
603 if (mode == MT_RFC822) {
604 if (!strcasecmp(snode, NODENAME)) {
607 cprintf("Message-ID: <%s@%s>\n", mid, snode);
608 PerformUserHooks(luser, (-1L), EVT_OUTPUTMSG);
609 cprintf("From: %s@%s (%s)\n",
610 suser, snode, luser);
611 cprintf("Organization: %s\n", lnode);
614 /* end header processing loop ... at this point, we're in the text */
617 cprintf("text\n*** ?Message truncated\n000\n");
622 /* do some sort of MIME output */
623 if (format_type == 4) {
624 if ((mode == MT_CITADEL)||(mode == MT_MIME)) {
625 mime_parser(mptr, NULL, *list_this_part);
627 if (mode == MT_MIME) { /* If MT_MIME then it's parts only */
635 /* give 'em a length */
637 while(ch = *mptr++, ch>0) {
640 cprintf("mlen=%ld\n", msg_len);
646 /* signify start of msg text */
647 if (mode == MT_CITADEL) cprintf("text\n");
648 if ( (mode == MT_RFC822) && (format_type != 4) ) cprintf("\n");
650 /* If the format type on disk is 1 (fixed-format), then we want
651 * everything to be output completely literally ... regardless of
652 * what message transfer format is in use.
654 if (format_type == 1) {
656 while(ch = *mptr++, ch>0) {
657 if (ch == 13) ch = 10;
658 if ( (ch == 10) || (strlen(buf)>250) ) {
659 cprintf("%s\n", buf);
663 buf[strlen(buf)+1] = 0;
664 buf[strlen(buf)] = ch;
667 if (strlen(buf)>0) cprintf("%s\n", buf);
669 /* If the message on disk is format 0 (Citadel vari-format), we
670 * output using the formatter at 80 columns. This is the final output
671 * form if the transfer format is RFC822, but if the transfer format
672 * is Citadel proprietary, it'll still work, because the indentation
673 * for new paragraphs is correct and the client will reformat the
674 * message to the reader's screen width.
676 if (format_type == 0) {
679 /* If the message on disk is format 4 (MIME), we've gotta hand it
680 * off to the MIME parser. The client has already been told that
681 * this message is format 1 (fixed format), so the callback function
682 * we use will display those parts as-is.
684 if (format_type == 4) {
685 mime_parser(mptr, NULL, *fixed_output);
696 * display a message (mode 0 - Citadel proprietary)
698 void cmd_msg0(char *cmdbuf)
701 int headers_only = 0;
703 extract(msgid,cmdbuf,0);
704 headers_only = extract_int(cmdbuf, 1);
706 output_message(msgid, MT_CITADEL, headers_only);
712 * display a message (mode 2 - RFC822)
714 void cmd_msg2(char *cmdbuf)
717 int headers_only = 0;
719 extract(msgid,cmdbuf,0);
720 headers_only = extract_int(cmdbuf,1);
722 output_message(msgid,MT_RFC822,headers_only);
726 * display a message (mode 3 - IGnet raw format - internal programs only)
728 void cmd_msg3(char *cmdbuf)
731 int headers_only = 0;
733 if (CC->internal_pgm == 0) {
734 cprintf("%d This command is for internal programs only.\n",
739 extract(msgid,cmdbuf,0);
740 headers_only = extract_int(cmdbuf,1);
742 output_message(msgid,MT_RAW,headers_only);
746 * display a message (mode 4 - MIME) (FIX ... still evolving, not complete)
748 void cmd_msg4(char *cmdbuf)
752 extract(msgid, cmdbuf, 0);
754 output_message(msgid, MT_MIME, 0);
760 * Open a component of a MIME message as a download file
762 void cmd_opna(char *cmdbuf)
766 CtdlAllocUserData(SYM_DESIRED_SECTION, 64);
768 extract(msgid, cmdbuf, 0);
769 extract(desired_section, cmdbuf, 1);
771 output_message(msgid, MT_DOWNLOAD, 0);
777 * Message base operation to send a message to the master file
778 * (returns new message number)
780 long send_message(char *message_in_memory, /* pointer to buffer */
781 size_t message_length, /* length of buffer */
782 int generate_id) { /* 1 to generate an I field */
785 char *actual_message;
786 size_t actual_length;
790 /* Get a new message number */
791 newmsgid = get_new_message_number();
794 sprintf(msgidbuf, "I%ld", newmsgid);
795 actual_length = message_length + strlen(msgidbuf) + 1;
796 actual_message = mallok(actual_length);
797 memcpy(actual_message, message_in_memory, 3);
798 memcpy(&actual_message[3], msgidbuf, (strlen(msgidbuf)+1) );
799 memcpy(&actual_message[strlen(msgidbuf)+4],
800 &message_in_memory[3], message_length - 3);
804 actual_message = message_in_memory;
805 actual_length = message_length;
808 /* Write our little bundle of joy into the message base */
809 begin_critical_section(S_MSGMAIN);
810 if ( cdb_store(CDB_MSGMAIN, &newmsgid, sizeof(long),
811 actual_message, actual_length) < 0 ) {
812 lprintf(2, "Can't store message\n");
818 end_critical_section(S_MSGMAIN);
821 phree(actual_message);
824 /* Finally, return the pointers */
831 * this is a simple file copy routine.
833 void copy_file(char *from, char *to)
839 if (ffp==NULL) return;
845 while (a=getc(ffp), a>=0) {
856 * message base operation to save a message and install its pointers
858 void save_message(char *mtmp, /* file containing proper message */
859 char *rec, /* Recipient (if mail) */
860 char *force, /* if non-zero length, force a room */
861 int mailtype, /* local or remote type, see citadel.h */
862 int generate_id) /* set to 1 to generate an 'I' field */
865 char hold_rm[ROOMNAMELEN];
866 char actual_rm[ROOMNAMELEN];
867 char force_room[ROOMNAMELEN];
870 char *message_in_memory;
874 struct usersupp userbuf;
876 static int seqnum = 0;
878 lprintf(9, "save_message(%s,%s,%s,%d,%d)\n",
879 mtmp, rec, force, mailtype, generate_id);
881 strcpy(force_room, force);
883 /* Strip non-printable characters out of the recipient name */
884 strcpy(recipient, rec);
885 for (a=0; a<strlen(recipient); ++a)
886 if (!isprint(recipient[a]))
887 strcpy(&recipient[a], &recipient[a+1]);
889 /* Measure the message */
890 stat(mtmp, &statbuf);
891 templen = statbuf.st_size;
893 /* Now read it into memory */
894 message_in_memory = (char *) mallok(templen);
895 if (message_in_memory == NULL) {
896 lprintf(2, "Can't allocate memory to save message!\n");
900 fp = fopen(mtmp, "rb");
901 fread(message_in_memory, templen, 1, fp);
904 newmsgid = send_message(message_in_memory, templen, generate_id);
905 phree(message_in_memory);
906 if (newmsgid <= 0L) return;
908 strcpy(actual_rm, CC->quickroom.QRname);
911 /* If the user is a twit, move to the twit room for posting... */
912 if (TWITDETECT) if (CC->usersupp.axlevel==2) {
913 strcpy(hold_rm, actual_rm);
914 strcpy(actual_rm, config.c_twitroom);
917 /* ...or if this is a private message, go to the target mailbox. */
918 lprintf(9, "mailbox aliasing loop\n");
919 if (strlen(recipient) > 0) {
920 /* mailtype = alias(recipient); */
921 if (mailtype == MES_LOCAL) {
922 if (getuser(&userbuf, recipient)!=0) {
923 /* User not found, goto Aide */
924 strcpy(force_room, AIDEROOM);
927 strcpy(hold_rm, actual_rm);
928 MailboxName(actual_rm, &userbuf, MAILROOM);
933 /* ...or if this message is destined for Aide> then go there. */
934 lprintf(9, "actual room forcing loop\n");
935 if (strlen(force_room) > 0) {
936 strcpy(hold_rm, actual_rm);
937 strcpy(actual_rm, force_room);
940 /* This call to usergoto() changes rooms if necessary. It also
941 * causes the latest message list to be read into memory.
943 usergoto(actual_rm, 0);
945 /* read in the quickroom record, obtaining a lock... */
946 lgetroom(&CC->quickroom, actual_rm);
948 /* Fix an obscure bug */
949 if (!strcasecmp(CC->quickroom.QRname, AIDEROOM)) {
950 CC->quickroom.QRflags = CC->quickroom.QRflags & ~QR_MAILBOX;
953 /* Add the message pointer to the room */
954 CC->quickroom.QRhighest = AddMessageToRoom(&CC->quickroom, newmsgid);
956 /* update quickroom */
957 lputroom(&CC->quickroom);
959 /* Network mail - send a copy to the network program. */
960 if ( (strlen(recipient)>0) && (mailtype != MES_LOCAL) ) {
961 sprintf(aaa,"./network/spoolin/netmail.%04lx.%04x.%04x",
962 (long)getpid(), CC->cs_pid, ++seqnum);
964 system("exec nohup ./netproc -i >/dev/null 2>&1 &");
967 /* Bump this user's messages posted counter. */
968 lgetuser(&CC->usersupp, CC->curr_user);
969 CC->usersupp.posted = CC->usersupp.posted + 1;
970 lputuser(&CC->usersupp, CC->curr_user);
972 /* If we've posted in a room other than the current room, then we
973 * have to now go back to the current room...
975 if (strlen(hold_rm) > 0) {
976 usergoto(hold_rm, 0);
978 unlink(mtmp); /* delete the temporary file */
983 * Generate an administrative message and post it in the Aide> room.
985 void aide_message(char *text)
989 fp=fopen(CC->temp,"wb");
990 fprintf(fp,"%c%c%c",255,MES_NORMAL,0);
991 fprintf(fp,"Psysop%c",0);
992 fprintf(fp,"T%ld%c", (long)time(NULL), 0);
993 fprintf(fp,"ACitadel%c",0);
994 fprintf(fp,"OAide%c",0);
995 fprintf(fp,"N%s%c",NODENAME,0);
996 fprintf(fp,"M%s\n%c",text,0);
998 save_message(CC->temp,"",AIDEROOM,MES_LOCAL,1);
999 syslog(LOG_NOTICE,text);
1005 * Build a binary message to be saved on disk.
1008 char *filename, /* temporary file name */
1009 struct usersupp *author, /* author's usersupp structure */
1010 char *recipient, /* NULL if it's not mail */
1011 char *room, /* room where it's going */
1012 int type, /* see MES_ types in header file */
1013 int net_type, /* see MES_ types in header file */
1014 int format_type, /* local or remote (see citadel.h) */
1015 char *fake_name) { /* who we're masquerading as */
1023 /* Don't confuse the poor folks if it's not routed mail. */
1024 strcpy(dest_node, "");
1027 /* If net_type is MES_BINARY, split out the destination node. */
1028 if (net_type == MES_BINARY) {
1029 strcpy(dest_node,NODENAME);
1030 for (a=0; a<strlen(recipient); ++a) {
1031 if (recipient[a]=='@') {
1033 strcpy(dest_node,&recipient[a+1]);
1038 /* if net_type is MES_INTERNET, set the dest node to 'internet' */
1039 if (net_type == MES_INTERNET) {
1040 strcpy(dest_node,"internet");
1043 while (isspace(recipient[strlen(recipient)-1]))
1044 recipient[strlen(recipient)-1] = 0;
1047 fp=fopen(filename,"w");
1049 putc(type,fp); /* Normal or anonymous, see MES_ flags */
1050 putc(format_type,fp); /* Formatted or unformatted */
1051 fprintf(fp,"Pcit%ld%c",author->usernum,0); /* path */
1052 fprintf(fp,"T%ld%c",(long)now,0); /* date/time */
1054 fprintf(fp,"A%s%c",fake_name,0);
1056 fprintf(fp,"A%s%c",author->fullname,0); /* author */
1058 if (CC->quickroom.QRflags & QR_MAILBOX) { /* room */
1059 fprintf(fp,"O%s%c", &CC->quickroom.QRname[11], 0);
1062 fprintf(fp,"O%s%c",CC->quickroom.QRname,0);
1065 fprintf(fp,"N%s%c",NODENAME,0); /* nodename */
1066 fprintf(fp,"H%s%c",HUMANNODE,0); /* human nodename */
1068 if (recipient[0]!=0) fprintf(fp, "R%s%c", recipient, 0);
1069 if (dest_node[0]!=0) fprintf(fp, "D%s%c", dest_node, 0);
1073 while (client_gets(buf), strcmp(buf,"000"))
1075 fprintf(fp,"%s\n",buf);
1077 syslog(LOG_INFO, "Closing message");
1087 * message entry - mode 0 (normal) <bc>
1089 void cmd_ent0(char *entargs)
1092 char recipient[256];
1094 int format_type = 0;
1095 char newusername[256]; /* <bc> */
1100 struct usersupp tempUS;
1103 post = extract_int(entargs,0);
1104 extract(recipient,entargs,1);
1105 anon_flag = extract_int(entargs,2);
1106 format_type = extract_int(entargs,3);
1108 /* first check to make sure the request is valid. */
1110 if (!(CC->logged_in)) {
1111 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1114 if ((CC->usersupp.axlevel<2)&&((CC->quickroom.QRflags&QR_MAILBOX)==0)) {
1115 cprintf("%d Need to be validated to enter ",
1116 ERROR+HIGHER_ACCESS_REQUIRED);
1117 cprintf("(except in %s> to sysop)\n", MAILROOM);
1120 if ((CC->usersupp.axlevel<4)&&(CC->quickroom.QRflags&QR_NETWORK)) {
1121 cprintf("%d Need net privileges to enter here.\n",
1122 ERROR+HIGHER_ACCESS_REQUIRED);
1125 if ((CC->usersupp.axlevel<6)&&(CC->quickroom.QRflags&QR_READONLY)) {
1126 cprintf("%d Sorry, this is a read-only room.\n",
1127 ERROR+HIGHER_ACCESS_REQUIRED);
1134 if (post==2) { /* <bc> */
1135 if (CC->usersupp.axlevel<6)
1137 cprintf("%d You don't have permission to do an aide post.\n",
1138 ERROR+HIGHER_ACCESS_REQUIRED);
1141 extract(newusername,entargs,4);
1142 memset(CC->fake_postname, 0, 32);
1143 strcpy(CC->fake_postname, newusername);
1144 cprintf("%d Ok\n",OK);
1148 CC->cs_flags |= CS_POSTING;
1151 if (CC->quickroom.QRflags & QR_MAILBOX) {
1152 if (CC->usersupp.axlevel>=2) {
1153 strcpy(buf,recipient);
1155 else strcpy(buf,"sysop");
1156 lprintf(9, "calling alias()\n");
1157 e=alias(buf); /* alias and mail type */
1158 lprintf(9, "alias() returned %d\n", e);
1159 if ((buf[0]==0) || (e==MES_ERROR)) {
1160 cprintf("%d Unknown address - cannot send message.\n",
1161 ERROR+NO_SUCH_USER);
1164 if ((e!=MES_LOCAL)&&(CC->usersupp.axlevel<4)) {
1165 cprintf("%d Net privileges required for network mail.\n",
1166 ERROR+HIGHER_ACCESS_REQUIRED);
1169 if ((RESTRICT_INTERNET==1)&&(e==MES_INTERNET)
1170 &&((CC->usersupp.flags&US_INTERNET)==0)
1171 &&(!CC->internal_pgm) ) {
1172 cprintf("%d You don't have access to Internet mail.\n",
1173 ERROR+HIGHER_ACCESS_REQUIRED);
1176 if (!strcasecmp(buf,"sysop")) {
1180 if (e!=MES_LOCAL) goto SKFALL; /* don't search local file */
1181 if (!strcasecmp(buf,CC->usersupp.fullname)) {
1182 cprintf("%d Can't send mail to yourself!\n",
1183 ERROR+NO_SUCH_USER);
1187 /* Check to make sure the user exists; also get the correct
1188 * upper/lower casing of the name.
1190 a = getuser(&tempUS, buf);
1192 cprintf("%d No such user.\n",ERROR+NO_SUCH_USER);
1195 strcpy(buf,tempUS.fullname);
1198 SKFALL: b=MES_NORMAL;
1199 if (CC->quickroom.QRflags&QR_ANONONLY) b=MES_ANON;
1200 if (CC->quickroom.QRflags&QR_ANONOPT) {
1201 if (anon_flag==1) b=MES_AN2;
1203 if ((CC->quickroom.QRflags & QR_MAILBOX) == 0) buf[0]=0;
1205 /* If we're only checking the validity of the request, return
1206 * success without creating the message.
1209 cprintf("%d %s\n",OK,buf);
1213 cprintf("%d send message\n",SEND_LISTING);
1214 if (CC->fake_postname[0])
1215 make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, CC->fake_postname);
1217 if (CC->fake_username[0])
1218 make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, CC->fake_username);
1220 make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, "");
1221 save_message(CC->temp,buf, (mtsflag ? AIDEROOM : ""), e,1);
1222 CC->fake_postname[0]='\0';
1229 * message entry - mode 3 (raw)
1231 void cmd_ent3(char *entargs)
1237 struct usersupp tempUS;
1242 if (CC->internal_pgm == 0) {
1243 cprintf("%d This command is for internal programs only.\n",
1248 /* See if there's a recipient, but make sure it's a real one */
1249 extract(recp, entargs, 1);
1250 for (a=0; a<strlen(recp); ++a)
1251 if (!isprint(recp[a]))
1252 strcpy(&recp[a], &recp[a+1]);
1253 while (isspace(recp[0])) strcpy(recp, &recp[1]);
1254 while (isspace(recp[strlen(recp)-1])) recp[strlen(recp)-1] = 0;
1256 /* If we're in Mail, check the recipient */
1257 if (strlen(recp) > 0) {
1258 e=alias(recp); /* alias and mail type */
1259 if ((recp[0]==0) || (e==MES_ERROR)) {
1260 cprintf("%d Unknown address - cannot send message.\n",
1261 ERROR+NO_SUCH_USER);
1264 if (e == MES_LOCAL) {
1265 a = getuser(&tempUS,recp);
1267 cprintf("%d No such user.\n",
1268 ERROR+NO_SUCH_USER);
1274 /* At this point, message has been approved. */
1275 if (extract_int(entargs, 0) == 0) {
1276 cprintf("%d OK to send\n", OK);
1280 /* open a temp file to hold the message */
1281 fp = fopen(CC->temp, "wb");
1283 cprintf("%d Cannot open %s: %s\n",
1284 ERROR + INTERNAL_ERROR,
1285 CC->temp, strerror(errno) );
1289 msglen = extract_long(entargs, 2);
1290 cprintf("%d %ld\n", SEND_BINARY, msglen);
1291 while(msglen > 0L) {
1292 bloklen = ((msglen >= 255L) ? 255 : msglen);
1293 client_read(buf, (int)bloklen );
1294 fwrite(buf, (int)bloklen, 1, fp);
1295 msglen = msglen - bloklen;
1299 save_message(CC->temp, recp, "", e, 0);
1304 * Delete message from current room
1306 void cmd_dele(char *delstr)
1311 getuser(&CC->usersupp,CC->curr_user);
1312 if ((CC->usersupp.axlevel < 6)
1313 && (CC->usersupp.usernum != CC->quickroom.QRroomaide)
1314 && ((CC->quickroom.QRflags & QR_MAILBOX) == 0)) {
1315 cprintf("%d Higher access required.\n",
1316 ERROR+HIGHER_ACCESS_REQUIRED);
1320 delnum = extract_long(delstr, 0);
1322 /* get room records, obtaining a lock... */
1323 lgetroom(&CC->quickroom,CC->quickroom.QRname);
1324 get_msglist(&CC->quickroom);
1327 if (CC->num_msgs > 0) for (a=0; a<(CC->num_msgs); ++a) {
1328 if (MessageFromList(a) == delnum) {
1329 SetMessageInList(a, 0L);
1334 CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
1335 CC->quickroom.QRhighest = MessageFromList(CC->num_msgs - 1);
1337 put_msglist(&CC->quickroom);
1338 lputroom(&CC->quickroom);
1340 cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));
1341 cprintf("%d Message deleted.\n",OK);
1343 else cprintf("%d No message %ld.\n",ERROR,delnum);
1348 * move a message to another room
1350 void cmd_move(char *args)
1355 struct quickroom qtemp;
1358 num = extract_long(args,0);
1359 extract(targ,args,1);
1361 getuser(&CC->usersupp,CC->curr_user);
1362 if ((CC->usersupp.axlevel < 6)
1363 && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
1364 cprintf("%d Higher access required.\n",
1365 ERROR+HIGHER_ACCESS_REQUIRED);
1369 if (getroom(&qtemp, targ) != 0) {
1370 cprintf("%d '%s' does not exist.\n",ERROR,targ);
1374 /* yank the message out of the current room... */
1375 lgetroom(&CC->quickroom, CC->quickroom.QRname);
1376 get_msglist(&CC->quickroom);
1379 for (a=0; a<(CC->num_msgs); ++a) {
1380 if (MessageFromList(a) == num) {
1382 SetMessageInList(a, 0L);
1386 CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
1387 put_msglist(&CC->quickroom);
1388 CC->quickroom.QRhighest = MessageFromList((CC->num_msgs)-1);
1390 lputroom(&CC->quickroom);
1392 cprintf("%d msg %ld does not exist.\n",ERROR,num);
1396 /* put the message into the target room */
1397 lgetroom(&qtemp, targ);
1398 qtemp.QRhighest = AddMessageToRoom(&qtemp, num);
1401 cprintf("%d Message moved.\n", OK);