18 #include "sysdep_decls.h"
23 #include "dynloader.h"
25 #include "mime_parser.h"
34 extern struct config config;
38 * This function is self explanatory.
39 * (What can I say, I'm in a weird mood today...)
41 void remove_any_whitespace_to_the_left_or_right_of_at_symbol(char *name) {
44 for (i=0; i<strlen(name); ++i) if (name[i]=='@') {
45 if (i>0) if (isspace(name[i-1])) {
46 strcpy(&name[i-1], &name[i]);
49 while (isspace(name[i+1])) {
50 strcpy(&name[i+1], &name[i+2]);
57 * Aliasing for network mail.
58 * (Error messages have been commented out, because this is a server.)
60 int alias(char *name) /* process alias and routing info for mail */
64 char aaa[300],bbb[300];
66 lprintf(9, "alias() called for <%s>\n", name);
68 remove_any_whitespace_to_the_left_or_right_of_at_symbol(name);
70 fp=fopen("network/mail.aliases","r");
71 if (fp==NULL) fp=fopen("/dev/null","r");
72 if (fp==NULL) return(M_ERROR);
73 GNA: strcpy(aaa,""); strcpy(bbb,"");
96 if (strcasecmp(name,aaa)) goto GNA;
99 lprintf(7, "Mail is being forwarded to %s\n", name);
101 DETYPE: /* determine local or remote type, see citadel.h */
102 for (a=0; a<strlen(name); ++a) if (name[a]=='!') return(M_INTERNET);
103 for (a=0; a<strlen(name); ++a)
105 for (b=a; b<strlen(name); ++b)
106 if (name[b]=='.') return(M_INTERNET);
107 b=0; for (a=0; a<strlen(name); ++a) if (name[a]=='@') ++b;
109 lprintf(7, "Too many @'s in address\n");
113 for (a=0; a<strlen(name); ++a)
114 if (name[a]=='@') strcpy(bbb,&name[a+1]);
115 while (bbb[0]==32) strcpy(bbb,&bbb[1]);
116 fp = fopen("network/mail.sysinfo","r");
117 if (fp==NULL) return(M_ERROR);
120 } while ((a>=0)&&(strcasecmp(aaa,bbb)));
122 if (!strncmp(aaa,"use ",4)) {
128 if (!strncmp(aaa,"uum",3)) {
130 for (a=0; a<strlen(bbb); ++a) {
131 if (bbb[a]=='@') bbb[a]=0;
132 if (bbb[a]==' ') bbb[a]='_';
134 while(bbb[strlen(bbb)-1]=='_') bbb[strlen(bbb)-1]=0;
135 sprintf(name,&aaa[4],bbb);
138 if (!strncmp(aaa,"bin",3)) {
139 strcpy(aaa,name); strcpy(bbb,name);
140 while (aaa[strlen(aaa)-1]!='@') aaa[strlen(aaa)-1]=0;
141 aaa[strlen(aaa)-1]=0;
142 while (aaa[strlen(aaa)-1]==' ') aaa[strlen(aaa)-1]=0;
143 while (bbb[0]!='@') strcpy(bbb,&bbb[1]);
145 while (bbb[0]==' ') strcpy(bbb,&bbb[1]);
146 sprintf(name,"%s @%s",aaa,bbb);
158 fp=fopen("citadel.control","r");
159 fread((char *)&CitControl,sizeof(struct CitControl),1,fp);
164 * cmd_msgs() - get list of message #'s in this room
166 void cmd_msgs(char *cmdbuf)
175 extract(which,cmdbuf,0);
179 if (!strncasecmp(which,"OLD",3)) mode = MSGS_OLD;
180 if (!strncasecmp(which,"NEW",3)) mode = MSGS_NEW;
181 if (!strncasecmp(which,"FIRST",5)) {
183 cm_howmany = extract_int(cmdbuf,1);
185 if (!strncasecmp(which,"LAST",4)) {
187 cm_howmany = extract_int(cmdbuf,1);
189 if (!strncasecmp(which,"GT",2)) {
191 cm_gt = extract_long(cmdbuf,1);
194 if ((!(CC->logged_in))&&(!(CC->internal_pgm))) {
195 cprintf("%d not logged in\n",ERROR+NOT_LOGGED_IN);
199 get_msglist(&CC->quickroom);
200 getuser(&CC->usersupp,CC->curr_user);
201 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
203 cprintf("%d Message list...\n",LISTING_FOLLOWS);
204 if (CC->num_msgs != 0) {
205 for (a=0; a<(CC->num_msgs); ++a)
206 if ((MessageFromList(a) >=0)
210 || ((mode==MSGS_OLD) && (MessageFromList(a) <= vbuf.v_lastseen))
211 || ((mode==MSGS_NEW) && (MessageFromList(a) > vbuf.v_lastseen))
212 || ((mode==MSGS_NEW) && (MessageFromList(a) >= vbuf.v_lastseen)
213 && (CC->usersupp.flags & US_LASTOLD))
214 || ((mode==MSGS_LAST)&& (a>=(CC->num_msgs-cm_howmany)))
215 || ((mode==MSGS_FIRST)&&(a<cm_howmany))
216 || ((mode==MSGS_GT) && (MessageFromList(a) > cm_gt))
220 cprintf("%ld\n", MessageFromList(a));
229 * help_subst() - support routine for help file viewer
231 void help_subst(char *strbuf, char *source, char *dest)
236 while (p=pattern2(strbuf,source), (p>=0)) {
237 strcpy(workbuf,&strbuf[p+strlen(source)]);
238 strcpy(&strbuf[p],dest);
239 strcat(strbuf,workbuf);
244 void do_help_subst(char *buffer)
248 help_subst(buffer,"^nodename",config.c_nodename);
249 help_subst(buffer,"^humannode",config.c_humannode);
250 help_subst(buffer,"^fqdn",config.c_fqdn);
251 help_subst(buffer,"^username",CC->usersupp.fullname);
252 sprintf(buf2,"%ld",CC->usersupp.usernum);
253 help_subst(buffer,"^usernum",buf2);
254 help_subst(buffer,"^sysadm",config.c_sysadm);
255 help_subst(buffer,"^variantname",CITADEL);
256 sprintf(buf2,"%d",config.c_maxsessions);
257 help_subst(buffer,"^maxsessions",buf2);
263 * memfmout() - Citadel text formatter and paginator.
264 * Although the original purpose of this routine was to format
265 * text to the reader's screen width, all we're really using it
266 * for here is to format text out to 80 columns before sending it
267 * to the client. The client software may reformat it again.
269 void memfmout(int width, char *mptr, char subst)
270 /* screen width to use */
271 /* where are we going to get our text from? */
272 /* nonzero if we should use hypertext mode */
281 strcpy(aaa,""); old=255;
283 c=1; /* c is the current pos */
286 while (ch=*mptr, ((ch!=0) && (strlen(buffer)<126) )) {
288 buffer[strlen(buffer)+1] = 0;
289 buffer[strlen(buffer)] = ch;
292 if (buffer[0]=='^') do_help_subst(buffer);
294 buffer[strlen(buffer)+1] = 0;
296 strcpy(buffer,&buffer[1]);
303 if (ch<=0) goto FMTEND;
305 if ( ((ch==13)||(ch==10)) && (old!=13) && (old!=10) ) ch=32;
306 if ( ((old==13)||(old==10)) && (isspace(real)) ) {
310 if (ch>126) goto FMTA;
313 if ( ((strlen(aaa)+c)>(width-5)) && (strlen(aaa)>(width-5)) )
314 { cprintf("\n%s",aaa); c=strlen(aaa); aaa[0]=0;
316 b=strlen(aaa); aaa[b]=ch; aaa[b+1]=0; }
318 if ((strlen(aaa)+c)>(width-5)) {
322 cprintf("%s ",aaa); ++c; c=c+strlen(aaa);
326 if ((ch==13)||(ch==10)) {
334 FMTEND: cprintf("\n");
340 * Callback function for mime parser that simply lists the part
342 void list_this_part(char *name, char *filename, char *partnum, char *disp,
343 void *content, char *cbtype, size_t length) {
345 cprintf("part=%s|%s|%s|%s|%s|%d\n",
346 name, filename, partnum, disp, cbtype, length);
351 * Callback function for mime parser that wants to display text
353 void fixed_output(char *name, char *filename, char *partnum, char *disp,
354 void *content, char *cbtype, size_t length) {
356 if (!strcasecmp(cbtype, "text/plain")) {
357 client_write(content, length);
360 cprintf("Part %s: %s (%s) (%d bytes)\n",
361 partnum, filename, cbtype, length);
367 * Callback function for mime parser that opens a section for downloading
369 void mime_download(char *name, char *filename, char *partnum, char *disp,
370 void *content, char *cbtype, size_t length) {
372 char tmpname[PATH_MAX];
375 /* Silently go away if there's already a download open... */
376 if (CC->download_fp != NULL) return;
378 /* ...or if this is not the desired section */
379 if (strcasecmp(CC->desired_section, partnum)) return;
381 snprintf(tmpname, sizeof tmpname,
382 "/tmp/CitServer.download.%4x.%4x", getpid(), ++seq);
384 CC->download_fp = fopen(tmpname, "wb+");
385 if (CC->download_fp == NULL) return;
387 /* Unlink the file while it's open, to guarantee that the
388 * temp file will always be deleted.
392 fwrite(content, length, 1, CC->download_fp);
393 fflush(CC->download_fp);
394 rewind(CC->download_fp);
396 OpenCmdResult(filename, cbtype);
402 * Get a message off disk. (return value is the message's timestamp)
405 time_t output_message(char *msgid, int mode, int headers_only) {
409 CIT_UBYTE format_type,anon_flag;
414 struct cdbdata *dmsgtext;
417 /* buffers needed for RFC822 translation */
426 msg_num = atol(msgid);
429 if ((!(CC->logged_in))&&(!(CC->internal_pgm))&&(mode!=MT_DATE)) {
430 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
434 /* We used to need to check in the current room's message list
435 * to determine where the message's disk position. We no longer need
436 * to do this, but we do it anyway as a security measure, in order to
437 * prevent rogue clients from reading messages not in the current room.
441 if (CC->num_msgs > 0) {
442 for (a=0; a<CC->num_msgs; ++a) {
443 if (MessageFromList(a) == msg_num) {
451 cprintf("%d Message %ld is not in this room.\n",
457 dmsgtext = cdb_fetch(CDB_MSGMAIN, &msg_num, sizeof(long));
459 if (dmsgtext == NULL) {
461 cprintf("%d Can't find message %ld\n",
462 ERROR+INTERNAL_ERROR);
466 msg_len = (long) dmsgtext->len;
467 mptr = dmsgtext->ptr;
469 /* this loop spews out the whole message if we're doing raw format */
470 if (mode == MT_RAW) {
471 cprintf("%d %ld\n", BINARY_FOLLOWS, msg_len);
472 client_write(dmsgtext->ptr, (int) msg_len);
477 /* Otherwise, we'll start parsing it field by field... */
480 cprintf("%d Illegal message format on disk\n",
481 ERROR+INTERNAL_ERROR);
487 format_type = *mptr++;
489 /* Are we downloading a MIME component? */
490 if (mode == MT_DOWNLOAD) {
491 if (format_type != 4) {
492 cprintf("%d This is not a MIME message.\n",
495 else if (CC->download_fp != NULL) {
496 cprintf("%d You already have a download open.\n",
500 /* Skip to the message body */
501 while(ch = *mptr++, (ch!='M' && ch!=0)) {
504 buf[strlen(buf)+1] = 0;
506 buf[strlen(buf)] = rch;
510 mime_parser(mptr, NULL, *mime_download);
511 /* If there's no file open by this time, the requested
512 * section wasn't found, so print an error
514 if (CC->download_fp == NULL) {
515 cprintf("%d Section %s not found.\n",
516 ERROR+FILE_NOT_FOUND,
517 CC->desired_section);
524 /* Are we just looking for the message date? */
525 if (mode == MT_DATE) while(ch = *mptr++, (ch!='M' && ch!=0)) {
528 buf[strlen(buf)+1] = 0;
530 buf[strlen(buf)] = rch;
541 /* now for the user-mode message reading loops */
542 cprintf("%d Message %ld:\n",LISTING_FOLLOWS,msg_num);
544 if (mode == MT_CITADEL) cprintf("type=%d\n", format_type);
546 if ( (anon_flag == MES_ANON) && (mode == MT_CITADEL) ) {
547 cprintf("nhdr=yes\n");
550 /* begin header processing loop for Citadel message format */
552 if ((mode == MT_CITADEL)||(mode == MT_MIME))
553 while(ch = *mptr++, (ch!='M' && ch!=0)) {
556 buf[strlen(buf)+1] = 0;
558 buf[strlen(buf)] = rch;
562 PerformUserHooks(buf, (-1L), EVT_OUTPUTMSG);
563 if (anon_flag==MES_ANON) cprintf("from=****");
564 else if (anon_flag==MES_AN2) cprintf("from=anonymous");
565 else cprintf("from=%s",buf);
566 if ((is_room_aide()) && ((anon_flag == MES_ANON)
567 || (anon_flag == MES_AN2)))
568 cprintf(" [%s]",buf);
571 else if (ch=='P') cprintf("path=%s\n",buf);
572 else if (ch=='U') cprintf("subj=%s\n",buf);
573 else if (ch=='I') cprintf("msgn=%s\n",buf);
574 else if (ch=='H') cprintf("hnod=%s\n",buf);
575 else if (ch=='O') cprintf("room=%s\n",buf);
576 else if (ch=='N') cprintf("node=%s\n",buf);
577 else if (ch=='R') cprintf("rcpt=%s\n",buf);
578 else if (ch=='T') cprintf("time=%s\n",buf);
579 /* else cprintf("fld%c=%s\n",ch,buf); */
582 /* begin header processing loop for RFC822 transfer format */
586 strcpy(snode, NODENAME);
587 strcpy(lnode, HUMANNODE);
588 if (mode == MT_RFC822) while(ch = *mptr++, (ch!='M' && ch!=0)) {
591 buf[strlen(buf)+1] = 0;
593 buf[strlen(buf)] = rch;
596 if (ch=='A') strcpy(luser, buf);
598 cprintf("Path: %s\n",buf);
599 for (a=0; a<strlen(buf); ++a) {
601 strcpy(buf,&buf[a+1]);
607 else if (ch=='U') cprintf("Subject: %s\n",buf);
608 else if (ch=='I') strcpy(mid, buf);
609 else if (ch=='H') strcpy(lnode, buf);
610 else if (ch=='O') cprintf("X-Citadel-Room: %s\n",buf);
611 else if (ch=='N') strcpy(snode, buf);
612 else if (ch=='R') cprintf("To: %s\n",buf);
615 cprintf("Date: %s", asctime(localtime(&xtime)));
619 if (mode == MT_RFC822) {
620 if (!strcasecmp(snode, NODENAME)) {
623 cprintf("Message-ID: <%s@%s>\n", mid, snode);
624 PerformUserHooks(luser, (-1L), EVT_OUTPUTMSG);
625 cprintf("From: %s@%s (%s)\n",
626 suser, snode, luser);
627 cprintf("Organization: %s\n", lnode);
630 /* end header processing loop ... at this point, we're in the text */
633 cprintf("text\n*** ?Message truncated\n000\n");
638 /* do some sort of MIME output */
639 if ( (mode == MT_MIME) && (format_type == 4) ) {
640 mime_parser(mptr, NULL, *list_this_part);
647 /* give 'em a length */
649 while(ch = *mptr++, ch>0) {
652 cprintf("mlen=%ld\n", msg_len);
658 /* signify start of msg text */
659 if (mode == MT_CITADEL) cprintf("text\n");
660 if ( (mode == MT_RFC822) && (format_type != 4) ) cprintf("\n");
662 /* If the format type on disk is 1 (fixed-format), then we want
663 * everything to be output completely literally ... regardless of
664 * what message transfer format is in use.
666 if (format_type == 1) {
668 while(ch = *mptr++, ch>0) {
669 if (ch == 13) ch = 10;
670 if ( (ch == 10) || (strlen(buf)>250) ) {
671 cprintf("%s\n", buf);
675 buf[strlen(buf)+1] = 0;
676 buf[strlen(buf)] = ch;
679 if (strlen(buf)>0) cprintf("%s\n", buf);
681 /* If the message on disk is format 0 (Citadel vari-format), we
682 * output using the formatter at 80 columns. This is the final output
683 * form if the transfer format is RFC822, but if the transfer format
684 * is Citadel proprietary, it'll still work, because the indentation
685 * for new paragraphs is correct and the client will reformat the
686 * message to the reader's screen width.
688 if (format_type == 0) {
691 /* If the message on disk is format 4 (MIME), we've gotta hand it
692 * off to the MIME parser. The client has already been told that
693 * this message is format 1 (fixed format), so the callback function
694 * we use will display those parts as-is.
696 if (format_type == 4) {
697 mime_parser(mptr, NULL, *fixed_output);
708 * display a message (mode 0 - Citadel proprietary)
710 void cmd_msg0(char *cmdbuf)
713 int headers_only = 0;
715 extract(msgid,cmdbuf,0);
716 headers_only = extract_int(cmdbuf, 1);
718 output_message(msgid, MT_CITADEL, headers_only);
724 * display a message (mode 2 - RFC822)
726 void cmd_msg2(char *cmdbuf)
729 int headers_only = 0;
731 extract(msgid,cmdbuf,0);
732 headers_only = extract_int(cmdbuf,1);
734 output_message(msgid,MT_RFC822,headers_only);
738 * display a message (mode 3 - IGnet raw format - internal programs only)
740 void cmd_msg3(char *cmdbuf)
743 int headers_only = 0;
745 if (CC->internal_pgm == 0) {
746 cprintf("%d This command is for internal programs only.\n",
751 extract(msgid,cmdbuf,0);
752 headers_only = extract_int(cmdbuf,1);
754 output_message(msgid,MT_RAW,headers_only);
758 * display a message (mode 4 - MIME) (FIX ... still evolving, not complete)
760 void cmd_msg4(char *cmdbuf)
764 extract(msgid, cmdbuf, 0);
766 output_message(msgid, MT_MIME, 0);
772 * Open a component of a MIME message as a download file
774 void cmd_opna(char *cmdbuf)
778 extract(msgid, cmdbuf, 0);
779 extract(CC->desired_section, cmdbuf, 1);
781 output_message(msgid, MT_DOWNLOAD, 0);
787 * Message base operation to send a message to the master file
788 * (returns new message number)
790 long send_message(char *message_in_memory, /* pointer to buffer */
791 size_t message_length, /* length of buffer */
792 int generate_id) { /* 1 to generate an I field */
795 char *actual_message;
796 size_t actual_length;
800 /* Get a new message number */
801 newmsgid = get_new_message_number();
804 sprintf(msgidbuf, "I%ld", newmsgid);
805 actual_length = message_length + strlen(msgidbuf) + 1;
806 actual_message = mallok(actual_length);
807 memcpy(actual_message, message_in_memory, 3);
808 memcpy(&actual_message[3], msgidbuf, (strlen(msgidbuf)+1) );
809 memcpy(&actual_message[strlen(msgidbuf)+4],
810 &message_in_memory[3], message_length - 3);
814 actual_message = message_in_memory;
815 actual_length = message_length;
818 /* Write our little bundle of joy into the message base */
819 begin_critical_section(S_MSGMAIN);
820 if ( cdb_store(CDB_MSGMAIN, &newmsgid, sizeof(long),
821 actual_message, actual_length) < 0 ) {
822 lprintf(2, "Can't store message\n");
828 end_critical_section(S_MSGMAIN);
831 phree(actual_message);
834 /* Finally, return the pointers */
841 * this is a simple file copy routine.
843 void copy_file(char *from, char *to)
849 if (ffp==NULL) return;
855 while (a=getc(ffp), a>=0) {
866 * message base operation to save a message and install its pointers
868 void save_message(char *mtmp, /* file containing proper message */
869 char *rec, /* Recipient (if mail) */
870 char *force, /* if non-zero length, force a room */
871 int mailtype, /* local or remote type, see citadel.h */
872 int generate_id) /* set to 1 to generate an 'I' field */
875 char hold_rm[ROOMNAMELEN];
876 char actual_rm[ROOMNAMELEN];
877 char force_room[ROOMNAMELEN];
880 char *message_in_memory;
884 struct usersupp userbuf;
886 static int seqnum = 0;
888 lprintf(9, "save_message(%s,%s,%s,%d,%d)\n",
889 mtmp, rec, force_room, mailtype, generate_id);
891 strcpy(force_room, force);
893 /* Strip non-printable characters out of the recipient name */
894 strcpy(recipient, rec);
895 for (a=0; a<strlen(recipient); ++a)
896 if (!isprint(recipient[a]))
897 strcpy(&recipient[a], &recipient[a+1]);
899 /* Measure the message */
900 stat(mtmp, &statbuf);
901 templen = statbuf.st_size;
903 /* Now read it into memory */
904 message_in_memory = (char *) mallok(templen);
905 if (message_in_memory == NULL) {
906 lprintf(2, "Can't allocate memory to save message!\n");
910 fp = fopen(mtmp, "rb");
911 fread(message_in_memory, templen, 1, fp);
914 newmsgid = send_message(message_in_memory, templen, generate_id);
915 phree(message_in_memory);
916 if (newmsgid <= 0L) return;
918 strcpy(actual_rm, CC->quickroom.QRname);
921 /* If the user is a twit, move to the twit room for posting... */
922 if (TWITDETECT) if (CC->usersupp.axlevel==2) {
923 strcpy(hold_rm, actual_rm);
924 strcpy(actual_rm, config.c_twitroom);
927 /* ...or if this is a private message, go to the target mailbox. */
928 lprintf(9, "mailbox aliasing loop\n");
930 if (strlen(recipient) > 0) {
932 /* mailtype = alias(recipient); */
933 if (mailtype == M_LOCAL) {
935 if (getuser(&userbuf, recipient)!=0) {
936 /* User not found, goto Aide */
938 strcpy(force_room, AIDEROOM);
942 strcpy(hold_rm, actual_rm);
944 MailboxName(actual_rm, &userbuf, MAILROOM);
949 /* ...or if this message is destined for Aide> then go there. */
950 lprintf(9, "actual room forcing loop\n");
951 if (strlen(force_room) > 0) {
952 strcpy(hold_rm, actual_rm);
953 strcpy(actual_rm, force_room);
956 /* This call to usergoto() changes rooms if necessary. It also
957 * causes the latest message list to be read into memory.
959 usergoto(actual_rm, 0);
961 /* read in the quickroom record, obtaining a lock... */
962 lgetroom(&CC->quickroom, actual_rm);
964 /* Fix an obscure bug */
965 if (!strcasecmp(CC->quickroom.QRname, AIDEROOM)) {
966 CC->quickroom.QRflags = CC->quickroom.QRflags & ~QR_MAILBOX;
969 /* Add the message pointer to the room */
970 CC->quickroom.QRhighest = AddMessageToRoom(&CC->quickroom, newmsgid);
972 /* update quickroom */
973 lputroom(&CC->quickroom, actual_rm);
975 /* Network mail - send a copy to the network program. */
976 if ( (strlen(recipient)>0) && (mailtype != M_LOCAL) ) {
977 sprintf(aaa,"./network/spoolin/netmail.%04lx.%04x.%04x",
978 (long)getpid(), CC->cs_pid, ++seqnum);
980 system("exec nohup ./netproc -i >/dev/null 2>&1 &");
983 /* Bump this user's messages posted counter. */
984 lgetuser(&CC->usersupp, CC->curr_user);
985 CC->usersupp.posted = CC->usersupp.posted + 1;
986 lputuser(&CC->usersupp, CC->curr_user);
988 /* If we've posted in a room other than the current room, then we
989 * have to now go back to the current room...
991 if (strlen(hold_rm) > 0) {
992 usergoto(hold_rm, 0);
994 unlink(mtmp); /* delete the temporary file */
999 * Generate an administrative message and post it in the Aide> room.
1001 void aide_message(char *text)
1005 fp=fopen(CC->temp,"wb");
1006 fprintf(fp,"%c%c%c",255,MES_NORMAL,0);
1007 fprintf(fp,"Psysop%c",0);
1008 fprintf(fp,"T%ld%c", time(NULL), 0);
1009 fprintf(fp,"ACitadel%c",0);
1010 fprintf(fp,"OAide%c",0);
1011 fprintf(fp,"N%s%c",NODENAME,0);
1012 fprintf(fp,"M%s\n%c",text,0);
1014 save_message(CC->temp,"",AIDEROOM,M_LOCAL,1);
1015 syslog(LOG_NOTICE,text);
1021 * Build a binary message to be saved on disk.
1024 char *filename, /* temporary file name */
1025 struct usersupp *author, /* author's usersupp structure */
1026 char *recipient, /* NULL if it's not mail */
1027 char *room, /* room where it's going */
1028 int type, /* see MES_ types in header file */
1029 int net_type, /* see MES_ types in header file */
1030 int format_type, /* local or remote (see citadel.h) */
1031 char *fake_name) { /* who we're masquerading as */
1039 /* Don't confuse the poor folks if it's not routed mail. */
1040 strcpy(dest_node, "");
1043 /* If net_type is M_BINARY, split out the destination node. */
1044 if (net_type == M_BINARY) {
1045 strcpy(dest_node,NODENAME);
1046 for (a=0; a<strlen(recipient); ++a) {
1047 if (recipient[a]=='@') {
1049 strcpy(dest_node,&recipient[a+1]);
1054 /* if net_type is M_INTERNET, set the dest node to 'internet' */
1055 if (net_type == M_INTERNET) {
1056 strcpy(dest_node,"internet");
1059 while (isspace(recipient[strlen(recipient)-1]))
1060 recipient[strlen(recipient)-1] = 0;
1063 fp=fopen(filename,"w");
1065 putc(type,fp); /* Normal or anonymous, see MES_ flags */
1066 putc(format_type,fp); /* Formatted or unformatted */
1067 fprintf(fp,"Pcit%ld%c",author->usernum,0); /* path */
1068 fprintf(fp,"T%ld%c",now,0); /* date/time */
1070 fprintf(fp,"A%s%c",fake_name,0);
1072 fprintf(fp,"A%s%c",author->fullname,0); /* author */
1074 if (CC->quickroom.QRflags & QR_MAILBOX) { /* room */
1075 fprintf(fp,"O%s%c", &CC->quickroom.QRname[11], 0);
1078 fprintf(fp,"O%s%c",CC->quickroom.QRname,0);
1081 fprintf(fp,"N%s%c",NODENAME,0); /* nodename */
1082 fprintf(fp,"H%s%c",HUMANNODE,0); /* human nodename */
1084 if (recipient[0]!=0) fprintf(fp, "R%s%c", recipient, 0);
1085 if (dest_node[0]!=0) fprintf(fp, "D%s%c", dest_node, 0);
1089 while (client_gets(buf), strcmp(buf,"000"))
1091 fprintf(fp,"%s\n",buf);
1093 syslog(LOG_INFO, "Closing message");
1103 * message entry - mode 0 (normal) <bc>
1105 void cmd_ent0(char *entargs)
1108 char recipient[256];
1110 int format_type = 0;
1111 char newusername[256]; /* <bc> */
1116 struct usersupp tempUS;
1119 post = extract_int(entargs,0);
1120 extract(recipient,entargs,1);
1121 anon_flag = extract_int(entargs,2);
1122 format_type = extract_int(entargs,3);
1124 /* first check to make sure the request is valid. */
1126 if (!(CC->logged_in)) {
1127 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1130 if ((CC->usersupp.axlevel<2)&&((CC->quickroom.QRflags&QR_MAILBOX)==0)) {
1131 cprintf("%d Need to be validated to enter ",
1132 ERROR+HIGHER_ACCESS_REQUIRED);
1133 cprintf("(except in %s> to sysop)\n", MAILROOM);
1136 if ((CC->usersupp.axlevel<4)&&(CC->quickroom.QRflags&QR_NETWORK)) {
1137 cprintf("%d Need net privileges to enter here.\n",
1138 ERROR+HIGHER_ACCESS_REQUIRED);
1141 if ((CC->usersupp.axlevel<6)&&(CC->quickroom.QRflags&QR_READONLY)) {
1142 cprintf("%d Sorry, this is a read-only room.\n",
1143 ERROR+HIGHER_ACCESS_REQUIRED);
1150 if (post==2) { /* <bc> */
1151 if (CC->usersupp.axlevel<6)
1153 cprintf("%d You don't have permission to do an aide post.\n",
1154 ERROR+HIGHER_ACCESS_REQUIRED);
1157 extract(newusername,entargs,4);
1158 memset(CC->fake_postname, 0, 32);
1159 strcpy(CC->fake_postname, newusername);
1160 cprintf("%d Ok\n",OK);
1164 CC->cs_flags |= CS_POSTING;
1167 if (CC->quickroom.QRflags & QR_MAILBOX) {
1168 if (CC->usersupp.axlevel>=2) {
1169 strcpy(buf,recipient);
1171 else strcpy(buf,"sysop");
1172 e=alias(buf); /* alias and mail type */
1173 if ((buf[0]==0) || (e==M_ERROR)) {
1174 cprintf("%d Unknown address - cannot send message.\n",
1175 ERROR+NO_SUCH_USER);
1178 if ((e!=M_LOCAL)&&(CC->usersupp.axlevel<4)) {
1179 cprintf("%d Net privileges required for network mail.\n",
1180 ERROR+HIGHER_ACCESS_REQUIRED);
1183 if ((RESTRICT_INTERNET==1)&&(e==M_INTERNET)
1184 &&((CC->usersupp.flags&US_INTERNET)==0)
1185 &&(!CC->internal_pgm) ) {
1186 cprintf("%d You don't have access to Internet mail.\n",
1187 ERROR+HIGHER_ACCESS_REQUIRED);
1190 if (!strcasecmp(buf,"sysop")) {
1194 if (e!=M_LOCAL) goto SKFALL; /* don't search local file */
1195 if (!strcasecmp(buf,CC->usersupp.fullname)) {
1196 cprintf("%d Can't send mail to yourself!\n",
1197 ERROR+NO_SUCH_USER);
1201 /* Check to make sure the user exists; also get the correct
1202 * upper/lower casing of the name.
1204 a = getuser(&tempUS,buf);
1206 cprintf("%d No such user.\n",ERROR+NO_SUCH_USER);
1209 strcpy(buf,tempUS.fullname);
1212 SKFALL: b=MES_NORMAL;
1213 if (CC->quickroom.QRflags&QR_ANONONLY) b=MES_ANON;
1214 if (CC->quickroom.QRflags&QR_ANONOPT) {
1215 if (anon_flag==1) b=MES_AN2;
1217 if ((CC->quickroom.QRflags & QR_MAILBOX) == 0) buf[0]=0;
1219 /* If we're only checking the validity of the request, return
1220 * success without creating the message.
1223 cprintf("%d %s\n",OK,buf);
1227 cprintf("%d send message\n",SEND_LISTING);
1228 if (CC->fake_postname[0])
1229 make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, CC->fake_postname);
1231 if (CC->fake_username[0])
1232 make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, CC->fake_username);
1234 make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, "");
1235 save_message(CC->temp,buf, (mtsflag ? AIDEROOM : ""), e,1);
1236 CC->fake_postname[0]='\0';
1243 * message entry - mode 3 (raw)
1245 void cmd_ent3(char *entargs)
1251 struct usersupp tempUS;
1256 if (CC->internal_pgm == 0) {
1257 cprintf("%d This command is for internal programs only.\n",
1262 /* See if there's a recipient, but make sure it's a real one */
1263 extract(recp, entargs, 1);
1264 for (a=0; a<strlen(recp); ++a)
1265 if (!isprint(recp[a]))
1266 strcpy(&recp[a], &recp[a+1]);
1267 while (isspace(recp[0])) strcpy(recp, &recp[1]);
1268 while (isspace(recp[strlen(recp)-1])) recp[strlen(recp)-1] = 0;
1270 /* If we're in Mail, check the recipient */
1271 if (strlen(recp) > 0) {
1272 e=alias(recp); /* alias and mail type */
1273 if ((recp[0]==0) || (e==M_ERROR)) {
1274 cprintf("%d Unknown address - cannot send message.\n",
1275 ERROR+NO_SUCH_USER);
1279 a = getuser(&tempUS,recp);
1281 cprintf("%d No such user.\n",
1282 ERROR+NO_SUCH_USER);
1288 /* At this point, message has been approved. */
1289 if (extract_int(entargs, 0) == 0) {
1290 cprintf("%d OK to send\n", OK);
1294 /* open a temp file to hold the message */
1295 fp = fopen(CC->temp, "wb");
1297 cprintf("%d Cannot open %s: %s\n",
1298 ERROR + INTERNAL_ERROR,
1299 CC->temp, strerror(errno) );
1303 msglen = extract_long(entargs, 2);
1304 cprintf("%d %ld\n", SEND_BINARY, msglen);
1305 while(msglen > 0L) {
1306 bloklen = ((msglen >= 255L) ? 255 : msglen);
1307 client_read(buf, (int)bloklen );
1308 fwrite(buf, (int)bloklen, 1, fp);
1309 msglen = msglen - bloklen;
1313 save_message(CC->temp, recp, "", e, 0);
1318 * Delete message from current room
1320 void cmd_dele(char *delstr)
1325 getuser(&CC->usersupp,CC->curr_user);
1326 if ((CC->usersupp.axlevel < 6)
1327 && (CC->usersupp.usernum != CC->quickroom.QRroomaide)
1328 && ((CC->quickroom.QRflags & QR_MAILBOX) == 0)) {
1329 cprintf("%d Higher access required.\n",
1330 ERROR+HIGHER_ACCESS_REQUIRED);
1334 delnum = extract_long(delstr, 0);
1336 /* get room records, obtaining a lock... */
1337 lgetroom(&CC->quickroom,CC->quickroom.QRname);
1338 get_msglist(&CC->quickroom);
1341 if (CC->num_msgs > 0) for (a=0; a<(CC->num_msgs); ++a) {
1342 if (MessageFromList(a) == delnum) {
1343 SetMessageInList(a, 0L);
1348 CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
1349 CC->quickroom.QRhighest = MessageFromList(CC->num_msgs - 1);
1351 put_msglist(&CC->quickroom);
1352 lputroom(&CC->quickroom,CC->quickroom.QRname);
1354 cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));
1355 cprintf("%d Message deleted.\n",OK);
1357 else cprintf("%d No message %ld.\n",ERROR,delnum);
1362 * move a message to another room
1364 void cmd_move(char *args)
1369 struct quickroom qtemp;
1372 num = extract_long(args,0);
1373 extract(targ,args,1);
1375 getuser(&CC->usersupp,CC->curr_user);
1376 if ((CC->usersupp.axlevel < 6)
1377 && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
1378 cprintf("%d Higher access required.\n",
1379 ERROR+HIGHER_ACCESS_REQUIRED);
1383 if (getroom(&qtemp, targ) != 0) {
1384 cprintf("%d '%s' does not exist.\n",ERROR,targ);
1388 /* yank the message out of the current room... */
1389 lgetroom(&CC->quickroom, CC->quickroom.QRname);
1390 get_msglist(&CC->quickroom);
1393 for (a=0; a<(CC->num_msgs); ++a) {
1394 if (MessageFromList(a) == num) {
1396 SetMessageInList(a, 0L);
1400 CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
1401 put_msglist(&CC->quickroom);
1402 CC->quickroom.QRhighest = MessageFromList((CC->num_msgs)-1);
1404 lputroom(&CC->quickroom,CC->quickroom.QRname);
1406 cprintf("%d msg %ld does not exist.\n",ERROR,num);
1410 /* put the message into the target room */
1411 lgetroom(&qtemp, targ);
1412 qtemp.QRhighest = AddMessageToRoom(&qtemp, num);
1413 lputroom(&qtemp, targ);
1415 cprintf("%d Message moved.\n", OK);