22 #include "sysdep_decls.h"
27 #include "dynloader.h"
29 #include "mime_parser.h"
38 extern struct config config;
42 * This function is self explanatory.
43 * (What can I say, I'm in a weird mood today...)
45 void remove_any_whitespace_to_the_left_or_right_of_at_symbol(char *name) {
48 for (i=0; i<strlen(name); ++i) if (name[i]=='@') {
49 if (i>0) if (isspace(name[i-1])) {
50 strcpy(&name[i-1], &name[i]);
53 while (isspace(name[i+1])) {
54 strcpy(&name[i+1], &name[i+2]);
61 * Aliasing for network mail.
62 * (Error messages have been commented out, because this is a server.)
64 int alias(char *name) /* process alias and routing info for mail */
68 char aaa[300],bbb[300];
70 lprintf(9, "alias() called for <%s>\n", name);
72 remove_any_whitespace_to_the_left_or_right_of_at_symbol(name);
74 fp=fopen("network/mail.aliases","r");
75 if (fp==NULL) fp=fopen("/dev/null","r");
76 if (fp==NULL) return(M_ERROR);
77 GNA: strcpy(aaa,""); strcpy(bbb,"");
100 if (strcasecmp(name,aaa)) goto GNA;
103 lprintf(7, "Mail is being forwarded to %s\n", name);
105 DETYPE: /* determine local or remote type, see citadel.h */
106 for (a=0; a<strlen(name); ++a) if (name[a]=='!') return(M_INTERNET);
107 for (a=0; a<strlen(name); ++a)
109 for (b=a; b<strlen(name); ++b)
110 if (name[b]=='.') return(M_INTERNET);
111 b=0; for (a=0; a<strlen(name); ++a) if (name[a]=='@') ++b;
113 lprintf(7, "Too many @'s in address\n");
117 for (a=0; a<strlen(name); ++a)
118 if (name[a]=='@') strcpy(bbb,&name[a+1]);
119 while (bbb[0]==32) strcpy(bbb,&bbb[1]);
120 fp = fopen("network/mail.sysinfo","r");
121 if (fp==NULL) return(M_ERROR);
124 } while ((a>=0)&&(strcasecmp(aaa,bbb)));
126 if (!strncmp(aaa,"use ",4)) {
132 if (!strncmp(aaa,"uum",3)) {
134 for (a=0; a<strlen(bbb); ++a) {
135 if (bbb[a]=='@') bbb[a]=0;
136 if (bbb[a]==' ') bbb[a]='_';
138 while(bbb[strlen(bbb)-1]=='_') bbb[strlen(bbb)-1]=0;
139 sprintf(name,&aaa[4],bbb);
142 if (!strncmp(aaa,"bin",3)) {
143 strcpy(aaa,name); strcpy(bbb,name);
144 while (aaa[strlen(aaa)-1]!='@') aaa[strlen(aaa)-1]=0;
145 aaa[strlen(aaa)-1]=0;
146 while (aaa[strlen(aaa)-1]==' ') aaa[strlen(aaa)-1]=0;
147 while (bbb[0]!='@') strcpy(bbb,&bbb[1]);
149 while (bbb[0]==' ') strcpy(bbb,&bbb[1]);
150 sprintf(name,"%s @%s",aaa,bbb);
162 fp=fopen("citadel.control","r");
163 fread((char *)&CitControl,sizeof(struct CitControl),1,fp);
168 * cmd_msgs() - get list of message #'s in this room
170 void cmd_msgs(char *cmdbuf)
179 extract(which,cmdbuf,0);
183 if (!strncasecmp(which,"OLD",3)) mode = MSGS_OLD;
184 if (!strncasecmp(which,"NEW",3)) mode = MSGS_NEW;
185 if (!strncasecmp(which,"FIRST",5)) {
187 cm_howmany = extract_int(cmdbuf,1);
189 if (!strncasecmp(which,"LAST",4)) {
191 cm_howmany = extract_int(cmdbuf,1);
193 if (!strncasecmp(which,"GT",2)) {
195 cm_gt = extract_long(cmdbuf,1);
198 if ((!(CC->logged_in))&&(!(CC->internal_pgm))) {
199 cprintf("%d not logged in\n",ERROR+NOT_LOGGED_IN);
203 get_msglist(&CC->quickroom);
204 getuser(&CC->usersupp,CC->curr_user);
205 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
207 cprintf("%d Message list...\n",LISTING_FOLLOWS);
208 if (CC->num_msgs != 0) {
209 for (a=0; a<(CC->num_msgs); ++a)
210 if ((MessageFromList(a) >=0)
214 || ((mode==MSGS_OLD) && (MessageFromList(a) <= vbuf.v_lastseen))
215 || ((mode==MSGS_NEW) && (MessageFromList(a) > vbuf.v_lastseen))
216 || ((mode==MSGS_NEW) && (MessageFromList(a) >= vbuf.v_lastseen)
217 && (CC->usersupp.flags & US_LASTOLD))
218 || ((mode==MSGS_LAST)&& (a>=(CC->num_msgs-cm_howmany)))
219 || ((mode==MSGS_FIRST)&&(a<cm_howmany))
220 || ((mode==MSGS_GT) && (MessageFromList(a) > cm_gt))
224 cprintf("%ld\n", MessageFromList(a));
233 * help_subst() - support routine for help file viewer
235 void help_subst(char *strbuf, char *source, char *dest)
240 while (p=pattern2(strbuf,source), (p>=0)) {
241 strcpy(workbuf,&strbuf[p+strlen(source)]);
242 strcpy(&strbuf[p],dest);
243 strcat(strbuf,workbuf);
248 void do_help_subst(char *buffer)
252 help_subst(buffer,"^nodename",config.c_nodename);
253 help_subst(buffer,"^humannode",config.c_humannode);
254 help_subst(buffer,"^fqdn",config.c_fqdn);
255 help_subst(buffer,"^username",CC->usersupp.fullname);
256 sprintf(buf2,"%ld",CC->usersupp.usernum);
257 help_subst(buffer,"^usernum",buf2);
258 help_subst(buffer,"^sysadm",config.c_sysadm);
259 help_subst(buffer,"^variantname",CITADEL);
260 sprintf(buf2,"%d",config.c_maxsessions);
261 help_subst(buffer,"^maxsessions",buf2);
267 * memfmout() - Citadel text formatter and paginator.
268 * Although the original purpose of this routine was to format
269 * text to the reader's screen width, all we're really using it
270 * for here is to format text out to 80 columns before sending it
271 * to the client. The client software may reformat it again.
273 void memfmout(int width, char *mptr, char subst)
274 /* screen width to use */
275 /* where are we going to get our text from? */
276 /* nonzero if we should use hypertext mode */
285 strcpy(aaa,""); old=255;
287 c=1; /* c is the current pos */
290 while (ch=*mptr, ((ch!=0) && (strlen(buffer)<126) )) {
292 buffer[strlen(buffer)+1] = 0;
293 buffer[strlen(buffer)] = ch;
296 if (buffer[0]=='^') do_help_subst(buffer);
298 buffer[strlen(buffer)+1] = 0;
300 strcpy(buffer,&buffer[1]);
307 if (ch<=0) goto FMTEND;
309 if ( ((ch==13)||(ch==10)) && (old!=13) && (old!=10) ) ch=32;
310 if ( ((old==13)||(old==10)) && (isspace(real)) ) {
314 if (ch>126) goto FMTA;
317 if ( ((strlen(aaa)+c)>(width-5)) && (strlen(aaa)>(width-5)) )
318 { cprintf("\n%s",aaa); c=strlen(aaa); aaa[0]=0;
320 b=strlen(aaa); aaa[b]=ch; aaa[b+1]=0; }
322 if ((strlen(aaa)+c)>(width-5)) {
326 cprintf("%s ",aaa); ++c; c=c+strlen(aaa);
330 if ((ch==13)||(ch==10)) {
338 FMTEND: cprintf("\n");
344 * Callback function for mime parser that simply lists the part
346 void list_this_part(char *name, char *filename, char *partnum, char *disp,
347 void *content, char *cbtype, size_t length) {
349 cprintf("part=%s|%s|%s|%s|%s|%d\n",
350 name, filename, partnum, disp, cbtype, length);
355 * Callback function for mime parser that wants to display text
357 void fixed_output(char *name, char *filename, char *partnum, char *disp,
358 void *content, char *cbtype, size_t length) {
360 if (!strcasecmp(cbtype, "text/plain")) {
361 client_write(content, length);
364 cprintf("Part %s: %s (%s) (%d bytes)\n",
365 partnum, filename, cbtype, length);
371 * Callback function for mime parser that opens a section for downloading
373 void mime_download(char *name, char *filename, char *partnum, char *disp,
374 void *content, char *cbtype, size_t length) {
376 char tmpname[PATH_MAX];
379 /* Silently go away if there's already a download open... */
380 if (CC->download_fp != NULL) return;
382 /* ...or if this is not the desired section */
383 if (strcasecmp(CC->desired_section, partnum)) return;
385 snprintf(tmpname, sizeof tmpname,
386 "/tmp/CitServer.download.%4x.%4x", getpid(), ++seq);
388 CC->download_fp = fopen(tmpname, "wb+");
389 if (CC->download_fp == NULL) return;
391 /* Unlink the file while it's open, to guarantee that the
392 * temp file will always be deleted.
396 fwrite(content, length, 1, CC->download_fp);
397 fflush(CC->download_fp);
398 rewind(CC->download_fp);
400 OpenCmdResult(filename, cbtype);
406 * Get a message off disk. (return value is the message's timestamp)
409 time_t output_message(char *msgid, int mode, int headers_only) {
413 CIT_UBYTE format_type,anon_flag;
418 struct cdbdata *dmsgtext;
421 /* buffers needed for RFC822 translation */
430 msg_num = atol(msgid);
433 if ((!(CC->logged_in))&&(!(CC->internal_pgm))&&(mode!=MT_DATE)) {
434 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
438 /* We used to need to check in the current room's message list
439 * to determine where the message's disk position. We no longer need
440 * to do this, but we do it anyway as a security measure, in order to
441 * prevent rogue clients from reading messages not in the current room.
445 if (CC->num_msgs > 0) {
446 for (a=0; a<CC->num_msgs; ++a) {
447 if (MessageFromList(a) == msg_num) {
455 cprintf("%d Message %ld is not in this room.\n",
461 dmsgtext = cdb_fetch(CDB_MSGMAIN, &msg_num, sizeof(long));
463 if (dmsgtext == NULL) {
465 cprintf("%d Can't find message %ld\n",
466 ERROR+INTERNAL_ERROR);
470 msg_len = (long) dmsgtext->len;
471 mptr = dmsgtext->ptr;
473 /* this loop spews out the whole message if we're doing raw format */
474 if (mode == MT_RAW) {
475 cprintf("%d %ld\n", BINARY_FOLLOWS, msg_len);
476 client_write(dmsgtext->ptr, (int) msg_len);
481 /* Otherwise, we'll start parsing it field by field... */
484 cprintf("%d Illegal message format on disk\n",
485 ERROR+INTERNAL_ERROR);
491 format_type = *mptr++;
493 /* Are we downloading a MIME component? */
494 if (mode == MT_DOWNLOAD) {
495 if (format_type != 4) {
496 cprintf("%d This is not a MIME message.\n",
499 else if (CC->download_fp != NULL) {
500 cprintf("%d You already have a download open.\n",
504 /* Skip to the message body */
505 while(ch = *mptr++, (ch!='M' && ch!=0)) {
508 buf[strlen(buf)+1] = 0;
510 buf[strlen(buf)] = rch;
514 mime_parser(mptr, NULL, *mime_download);
515 /* If there's no file open by this time, the requested
516 * section wasn't found, so print an error
518 if (CC->download_fp == NULL) {
519 cprintf("%d Section %s not found.\n",
520 ERROR+FILE_NOT_FOUND,
521 CC->desired_section);
528 /* Are we just looking for the message date? */
529 if (mode == MT_DATE) while(ch = *mptr++, (ch!='M' && ch!=0)) {
532 buf[strlen(buf)+1] = 0;
534 buf[strlen(buf)] = rch;
545 /* now for the user-mode message reading loops */
546 cprintf("%d Message %ld:\n",LISTING_FOLLOWS,msg_num);
548 if (mode == MT_CITADEL) cprintf("type=%d\n", format_type);
550 if ( (anon_flag == MES_ANON) && (mode == MT_CITADEL) ) {
551 cprintf("nhdr=yes\n");
554 /* begin header processing loop for Citadel message format */
556 if ((mode == MT_CITADEL)||(mode == MT_MIME))
557 while(ch = *mptr++, (ch!='M' && ch!=0)) {
560 buf[strlen(buf)+1] = 0;
562 buf[strlen(buf)] = rch;
566 PerformUserHooks(buf, (-1L), EVT_OUTPUTMSG);
567 if (anon_flag==MES_ANON) cprintf("from=****");
568 else if (anon_flag==MES_AN2) cprintf("from=anonymous");
569 else cprintf("from=%s",buf);
570 if ((is_room_aide()) && ((anon_flag == MES_ANON)
571 || (anon_flag == MES_AN2)))
572 cprintf(" [%s]",buf);
575 else if (ch=='P') cprintf("path=%s\n",buf);
576 else if (ch=='U') cprintf("subj=%s\n",buf);
577 else if (ch=='I') cprintf("msgn=%s\n",buf);
578 else if (ch=='H') cprintf("hnod=%s\n",buf);
579 else if (ch=='O') cprintf("room=%s\n",buf);
580 else if (ch=='N') cprintf("node=%s\n",buf);
581 else if (ch=='R') cprintf("rcpt=%s\n",buf);
582 else if (ch=='T') cprintf("time=%s\n",buf);
583 /* else cprintf("fld%c=%s\n",ch,buf); */
586 /* begin header processing loop for RFC822 transfer format */
590 strcpy(snode, NODENAME);
591 strcpy(lnode, HUMANNODE);
592 if (mode == MT_RFC822) while(ch = *mptr++, (ch!='M' && ch!=0)) {
595 buf[strlen(buf)+1] = 0;
597 buf[strlen(buf)] = rch;
600 if (ch=='A') strcpy(luser, buf);
602 cprintf("Path: %s\n",buf);
603 for (a=0; a<strlen(buf); ++a) {
605 strcpy(buf,&buf[a+1]);
611 else if (ch=='U') cprintf("Subject: %s\n",buf);
612 else if (ch=='I') strcpy(mid, buf);
613 else if (ch=='H') strcpy(lnode, buf);
614 else if (ch=='O') cprintf("X-Citadel-Room: %s\n",buf);
615 else if (ch=='N') strcpy(snode, buf);
616 else if (ch=='R') cprintf("To: %s\n",buf);
619 cprintf("Date: %s", asctime(localtime(&xtime)));
623 if (mode == MT_RFC822) {
624 if (!strcasecmp(snode, NODENAME)) {
627 cprintf("Message-ID: <%s@%s>\n", mid, snode);
628 PerformUserHooks(luser, (-1L), EVT_OUTPUTMSG);
629 cprintf("From: %s@%s (%s)\n",
630 suser, snode, luser);
631 cprintf("Organization: %s\n", lnode);
634 /* end header processing loop ... at this point, we're in the text */
637 cprintf("text\n*** ?Message truncated\n000\n");
642 /* do some sort of MIME output */
643 if (format_type == 4) {
644 if ((mode == MT_CITADEL)||(mode == MT_MIME)) {
645 mime_parser(mptr, NULL, *list_this_part);
647 if (mode == MT_MIME) { /* If MT_MIME then it's parts only */
655 /* give 'em a length */
657 while(ch = *mptr++, ch>0) {
660 cprintf("mlen=%ld\n", msg_len);
666 /* signify start of msg text */
667 if (mode == MT_CITADEL) cprintf("text\n");
668 if ( (mode == MT_RFC822) && (format_type != 4) ) cprintf("\n");
670 /* If the format type on disk is 1 (fixed-format), then we want
671 * everything to be output completely literally ... regardless of
672 * what message transfer format is in use.
674 if (format_type == 1) {
676 while(ch = *mptr++, ch>0) {
677 if (ch == 13) ch = 10;
678 if ( (ch == 10) || (strlen(buf)>250) ) {
679 cprintf("%s\n", buf);
683 buf[strlen(buf)+1] = 0;
684 buf[strlen(buf)] = ch;
687 if (strlen(buf)>0) cprintf("%s\n", buf);
689 /* If the message on disk is format 0 (Citadel vari-format), we
690 * output using the formatter at 80 columns. This is the final output
691 * form if the transfer format is RFC822, but if the transfer format
692 * is Citadel proprietary, it'll still work, because the indentation
693 * for new paragraphs is correct and the client will reformat the
694 * message to the reader's screen width.
696 if (format_type == 0) {
699 /* If the message on disk is format 4 (MIME), we've gotta hand it
700 * off to the MIME parser. The client has already been told that
701 * this message is format 1 (fixed format), so the callback function
702 * we use will display those parts as-is.
704 if (format_type == 4) {
705 mime_parser(mptr, NULL, *fixed_output);
716 * display a message (mode 0 - Citadel proprietary)
718 void cmd_msg0(char *cmdbuf)
721 int headers_only = 0;
723 extract(msgid,cmdbuf,0);
724 headers_only = extract_int(cmdbuf, 1);
726 output_message(msgid, MT_CITADEL, headers_only);
732 * display a message (mode 2 - RFC822)
734 void cmd_msg2(char *cmdbuf)
737 int headers_only = 0;
739 extract(msgid,cmdbuf,0);
740 headers_only = extract_int(cmdbuf,1);
742 output_message(msgid,MT_RFC822,headers_only);
746 * display a message (mode 3 - IGnet raw format - internal programs only)
748 void cmd_msg3(char *cmdbuf)
751 int headers_only = 0;
753 if (CC->internal_pgm == 0) {
754 cprintf("%d This command is for internal programs only.\n",
759 extract(msgid,cmdbuf,0);
760 headers_only = extract_int(cmdbuf,1);
762 output_message(msgid,MT_RAW,headers_only);
766 * display a message (mode 4 - MIME) (FIX ... still evolving, not complete)
768 void cmd_msg4(char *cmdbuf)
772 extract(msgid, cmdbuf, 0);
774 output_message(msgid, MT_MIME, 0);
780 * Open a component of a MIME message as a download file
782 void cmd_opna(char *cmdbuf)
786 extract(msgid, cmdbuf, 0);
787 extract(CC->desired_section, cmdbuf, 1);
789 output_message(msgid, MT_DOWNLOAD, 0);
795 * Message base operation to send a message to the master file
796 * (returns new message number)
798 long send_message(char *message_in_memory, /* pointer to buffer */
799 size_t message_length, /* length of buffer */
800 int generate_id) { /* 1 to generate an I field */
803 char *actual_message;
804 size_t actual_length;
808 /* Get a new message number */
809 newmsgid = get_new_message_number();
812 sprintf(msgidbuf, "I%ld", newmsgid);
813 actual_length = message_length + strlen(msgidbuf) + 1;
814 actual_message = mallok(actual_length);
815 memcpy(actual_message, message_in_memory, 3);
816 memcpy(&actual_message[3], msgidbuf, (strlen(msgidbuf)+1) );
817 memcpy(&actual_message[strlen(msgidbuf)+4],
818 &message_in_memory[3], message_length - 3);
822 actual_message = message_in_memory;
823 actual_length = message_length;
826 /* Write our little bundle of joy into the message base */
827 begin_critical_section(S_MSGMAIN);
828 if ( cdb_store(CDB_MSGMAIN, &newmsgid, sizeof(long),
829 actual_message, actual_length) < 0 ) {
830 lprintf(2, "Can't store message\n");
836 end_critical_section(S_MSGMAIN);
839 phree(actual_message);
842 /* Finally, return the pointers */
849 * this is a simple file copy routine.
851 void copy_file(char *from, char *to)
857 if (ffp==NULL) return;
863 while (a=getc(ffp), a>=0) {
874 * message base operation to save a message and install its pointers
876 void save_message(char *mtmp, /* file containing proper message */
877 char *rec, /* Recipient (if mail) */
878 char *force, /* if non-zero length, force a room */
879 int mailtype, /* local or remote type, see citadel.h */
880 int generate_id) /* set to 1 to generate an 'I' field */
883 char hold_rm[ROOMNAMELEN];
884 char actual_rm[ROOMNAMELEN];
885 char force_room[ROOMNAMELEN];
888 char *message_in_memory;
892 struct usersupp userbuf;
894 static int seqnum = 0;
896 lprintf(9, "save_message(%s,%s,%s,%d,%d)\n",
897 mtmp, rec, force_room, mailtype, generate_id);
899 strcpy(force_room, force);
901 /* Strip non-printable characters out of the recipient name */
902 strcpy(recipient, rec);
903 for (a=0; a<strlen(recipient); ++a)
904 if (!isprint(recipient[a]))
905 strcpy(&recipient[a], &recipient[a+1]);
907 /* Measure the message */
908 stat(mtmp, &statbuf);
909 templen = statbuf.st_size;
911 /* Now read it into memory */
912 message_in_memory = (char *) mallok(templen);
913 if (message_in_memory == NULL) {
914 lprintf(2, "Can't allocate memory to save message!\n");
918 fp = fopen(mtmp, "rb");
919 fread(message_in_memory, templen, 1, fp);
922 newmsgid = send_message(message_in_memory, templen, generate_id);
923 phree(message_in_memory);
924 if (newmsgid <= 0L) return;
926 strcpy(actual_rm, CC->quickroom.QRname);
929 /* If the user is a twit, move to the twit room for posting... */
930 if (TWITDETECT) if (CC->usersupp.axlevel==2) {
931 strcpy(hold_rm, actual_rm);
932 strcpy(actual_rm, config.c_twitroom);
935 /* ...or if this is a private message, go to the target mailbox. */
936 lprintf(9, "mailbox aliasing loop\n");
938 if (strlen(recipient) > 0) {
940 /* mailtype = alias(recipient); */
941 if (mailtype == M_LOCAL) {
943 if (getuser(&userbuf, recipient)!=0) {
944 /* User not found, goto Aide */
946 strcpy(force_room, AIDEROOM);
950 strcpy(hold_rm, actual_rm);
952 MailboxName(actual_rm, &userbuf, MAILROOM);
957 /* ...or if this message is destined for Aide> then go there. */
958 lprintf(9, "actual room forcing loop\n");
959 if (strlen(force_room) > 0) {
960 strcpy(hold_rm, actual_rm);
961 strcpy(actual_rm, force_room);
964 /* This call to usergoto() changes rooms if necessary. It also
965 * causes the latest message list to be read into memory.
967 usergoto(actual_rm, 0);
969 /* read in the quickroom record, obtaining a lock... */
970 lgetroom(&CC->quickroom, actual_rm);
972 /* Fix an obscure bug */
973 if (!strcasecmp(CC->quickroom.QRname, AIDEROOM)) {
974 CC->quickroom.QRflags = CC->quickroom.QRflags & ~QR_MAILBOX;
977 /* Add the message pointer to the room */
978 CC->quickroom.QRhighest = AddMessageToRoom(&CC->quickroom, newmsgid);
980 /* update quickroom */
981 lputroom(&CC->quickroom, actual_rm);
983 /* Network mail - send a copy to the network program. */
984 if ( (strlen(recipient)>0) && (mailtype != M_LOCAL) ) {
985 sprintf(aaa,"./network/spoolin/netmail.%04lx.%04x.%04x",
986 (long)getpid(), CC->cs_pid, ++seqnum);
988 system("exec nohup ./netproc -i >/dev/null 2>&1 &");
991 /* Bump this user's messages posted counter. */
992 lgetuser(&CC->usersupp, CC->curr_user);
993 CC->usersupp.posted = CC->usersupp.posted + 1;
994 lputuser(&CC->usersupp, CC->curr_user);
996 /* If we've posted in a room other than the current room, then we
997 * have to now go back to the current room...
999 if (strlen(hold_rm) > 0) {
1000 usergoto(hold_rm, 0);
1002 unlink(mtmp); /* delete the temporary file */
1007 * Generate an administrative message and post it in the Aide> room.
1009 void aide_message(char *text)
1013 fp=fopen(CC->temp,"wb");
1014 fprintf(fp,"%c%c%c",255,MES_NORMAL,0);
1015 fprintf(fp,"Psysop%c",0);
1016 fprintf(fp,"T%ld%c", time(NULL), 0);
1017 fprintf(fp,"ACitadel%c",0);
1018 fprintf(fp,"OAide%c",0);
1019 fprintf(fp,"N%s%c",NODENAME,0);
1020 fprintf(fp,"M%s\n%c",text,0);
1022 save_message(CC->temp,"",AIDEROOM,M_LOCAL,1);
1023 syslog(LOG_NOTICE,text);
1029 * Build a binary message to be saved on disk.
1032 char *filename, /* temporary file name */
1033 struct usersupp *author, /* author's usersupp structure */
1034 char *recipient, /* NULL if it's not mail */
1035 char *room, /* room where it's going */
1036 int type, /* see MES_ types in header file */
1037 int net_type, /* see MES_ types in header file */
1038 int format_type, /* local or remote (see citadel.h) */
1039 char *fake_name) { /* who we're masquerading as */
1047 /* Don't confuse the poor folks if it's not routed mail. */
1048 strcpy(dest_node, "");
1051 /* If net_type is M_BINARY, split out the destination node. */
1052 if (net_type == M_BINARY) {
1053 strcpy(dest_node,NODENAME);
1054 for (a=0; a<strlen(recipient); ++a) {
1055 if (recipient[a]=='@') {
1057 strcpy(dest_node,&recipient[a+1]);
1062 /* if net_type is M_INTERNET, set the dest node to 'internet' */
1063 if (net_type == M_INTERNET) {
1064 strcpy(dest_node,"internet");
1067 while (isspace(recipient[strlen(recipient)-1]))
1068 recipient[strlen(recipient)-1] = 0;
1071 fp=fopen(filename,"w");
1073 putc(type,fp); /* Normal or anonymous, see MES_ flags */
1074 putc(format_type,fp); /* Formatted or unformatted */
1075 fprintf(fp,"Pcit%ld%c",author->usernum,0); /* path */
1076 fprintf(fp,"T%ld%c",now,0); /* date/time */
1078 fprintf(fp,"A%s%c",fake_name,0);
1080 fprintf(fp,"A%s%c",author->fullname,0); /* author */
1082 if (CC->quickroom.QRflags & QR_MAILBOX) { /* room */
1083 fprintf(fp,"O%s%c", &CC->quickroom.QRname[11], 0);
1086 fprintf(fp,"O%s%c",CC->quickroom.QRname,0);
1089 fprintf(fp,"N%s%c",NODENAME,0); /* nodename */
1090 fprintf(fp,"H%s%c",HUMANNODE,0); /* human nodename */
1092 if (recipient[0]!=0) fprintf(fp, "R%s%c", recipient, 0);
1093 if (dest_node[0]!=0) fprintf(fp, "D%s%c", dest_node, 0);
1097 while (client_gets(buf), strcmp(buf,"000"))
1099 fprintf(fp,"%s\n",buf);
1101 syslog(LOG_INFO, "Closing message");
1111 * message entry - mode 0 (normal) <bc>
1113 void cmd_ent0(char *entargs)
1116 char recipient[256];
1118 int format_type = 0;
1119 char newusername[256]; /* <bc> */
1124 struct usersupp tempUS;
1127 post = extract_int(entargs,0);
1128 extract(recipient,entargs,1);
1129 anon_flag = extract_int(entargs,2);
1130 format_type = extract_int(entargs,3);
1132 /* first check to make sure the request is valid. */
1134 if (!(CC->logged_in)) {
1135 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1138 if ((CC->usersupp.axlevel<2)&&((CC->quickroom.QRflags&QR_MAILBOX)==0)) {
1139 cprintf("%d Need to be validated to enter ",
1140 ERROR+HIGHER_ACCESS_REQUIRED);
1141 cprintf("(except in %s> to sysop)\n", MAILROOM);
1144 if ((CC->usersupp.axlevel<4)&&(CC->quickroom.QRflags&QR_NETWORK)) {
1145 cprintf("%d Need net privileges to enter here.\n",
1146 ERROR+HIGHER_ACCESS_REQUIRED);
1149 if ((CC->usersupp.axlevel<6)&&(CC->quickroom.QRflags&QR_READONLY)) {
1150 cprintf("%d Sorry, this is a read-only room.\n",
1151 ERROR+HIGHER_ACCESS_REQUIRED);
1158 if (post==2) { /* <bc> */
1159 if (CC->usersupp.axlevel<6)
1161 cprintf("%d You don't have permission to do an aide post.\n",
1162 ERROR+HIGHER_ACCESS_REQUIRED);
1165 extract(newusername,entargs,4);
1166 memset(CC->fake_postname, 0, 32);
1167 strcpy(CC->fake_postname, newusername);
1168 cprintf("%d Ok\n",OK);
1172 CC->cs_flags |= CS_POSTING;
1175 if (CC->quickroom.QRflags & QR_MAILBOX) {
1176 if (CC->usersupp.axlevel>=2) {
1177 strcpy(buf,recipient);
1179 else strcpy(buf,"sysop");
1180 e=alias(buf); /* alias and mail type */
1181 if ((buf[0]==0) || (e==M_ERROR)) {
1182 cprintf("%d Unknown address - cannot send message.\n",
1183 ERROR+NO_SUCH_USER);
1186 if ((e!=M_LOCAL)&&(CC->usersupp.axlevel<4)) {
1187 cprintf("%d Net privileges required for network mail.\n",
1188 ERROR+HIGHER_ACCESS_REQUIRED);
1191 if ((RESTRICT_INTERNET==1)&&(e==M_INTERNET)
1192 &&((CC->usersupp.flags&US_INTERNET)==0)
1193 &&(!CC->internal_pgm) ) {
1194 cprintf("%d You don't have access to Internet mail.\n",
1195 ERROR+HIGHER_ACCESS_REQUIRED);
1198 if (!strcasecmp(buf,"sysop")) {
1202 if (e!=M_LOCAL) goto SKFALL; /* don't search local file */
1203 if (!strcasecmp(buf,CC->usersupp.fullname)) {
1204 cprintf("%d Can't send mail to yourself!\n",
1205 ERROR+NO_SUCH_USER);
1209 /* Check to make sure the user exists; also get the correct
1210 * upper/lower casing of the name.
1212 a = getuser(&tempUS,buf);
1214 cprintf("%d No such user.\n",ERROR+NO_SUCH_USER);
1217 strcpy(buf,tempUS.fullname);
1220 SKFALL: b=MES_NORMAL;
1221 if (CC->quickroom.QRflags&QR_ANONONLY) b=MES_ANON;
1222 if (CC->quickroom.QRflags&QR_ANONOPT) {
1223 if (anon_flag==1) b=MES_AN2;
1225 if ((CC->quickroom.QRflags & QR_MAILBOX) == 0) buf[0]=0;
1227 /* If we're only checking the validity of the request, return
1228 * success without creating the message.
1231 cprintf("%d %s\n",OK,buf);
1235 cprintf("%d send message\n",SEND_LISTING);
1236 if (CC->fake_postname[0])
1237 make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, CC->fake_postname);
1239 if (CC->fake_username[0])
1240 make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, CC->fake_username);
1242 make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, "");
1243 save_message(CC->temp,buf, (mtsflag ? AIDEROOM : ""), e,1);
1244 CC->fake_postname[0]='\0';
1251 * message entry - mode 3 (raw)
1253 void cmd_ent3(char *entargs)
1259 struct usersupp tempUS;
1264 if (CC->internal_pgm == 0) {
1265 cprintf("%d This command is for internal programs only.\n",
1270 /* See if there's a recipient, but make sure it's a real one */
1271 extract(recp, entargs, 1);
1272 for (a=0; a<strlen(recp); ++a)
1273 if (!isprint(recp[a]))
1274 strcpy(&recp[a], &recp[a+1]);
1275 while (isspace(recp[0])) strcpy(recp, &recp[1]);
1276 while (isspace(recp[strlen(recp)-1])) recp[strlen(recp)-1] = 0;
1278 /* If we're in Mail, check the recipient */
1279 if (strlen(recp) > 0) {
1280 e=alias(recp); /* alias and mail type */
1281 if ((recp[0]==0) || (e==M_ERROR)) {
1282 cprintf("%d Unknown address - cannot send message.\n",
1283 ERROR+NO_SUCH_USER);
1287 a = getuser(&tempUS,recp);
1289 cprintf("%d No such user.\n",
1290 ERROR+NO_SUCH_USER);
1296 /* At this point, message has been approved. */
1297 if (extract_int(entargs, 0) == 0) {
1298 cprintf("%d OK to send\n", OK);
1302 /* open a temp file to hold the message */
1303 fp = fopen(CC->temp, "wb");
1305 cprintf("%d Cannot open %s: %s\n",
1306 ERROR + INTERNAL_ERROR,
1307 CC->temp, strerror(errno) );
1311 msglen = extract_long(entargs, 2);
1312 cprintf("%d %ld\n", SEND_BINARY, msglen);
1313 while(msglen > 0L) {
1314 bloklen = ((msglen >= 255L) ? 255 : msglen);
1315 client_read(buf, (int)bloklen );
1316 fwrite(buf, (int)bloklen, 1, fp);
1317 msglen = msglen - bloklen;
1321 save_message(CC->temp, recp, "", e, 0);
1326 * Delete message from current room
1328 void cmd_dele(char *delstr)
1333 getuser(&CC->usersupp,CC->curr_user);
1334 if ((CC->usersupp.axlevel < 6)
1335 && (CC->usersupp.usernum != CC->quickroom.QRroomaide)
1336 && ((CC->quickroom.QRflags & QR_MAILBOX) == 0)) {
1337 cprintf("%d Higher access required.\n",
1338 ERROR+HIGHER_ACCESS_REQUIRED);
1342 delnum = extract_long(delstr, 0);
1344 /* get room records, obtaining a lock... */
1345 lgetroom(&CC->quickroom,CC->quickroom.QRname);
1346 get_msglist(&CC->quickroom);
1349 if (CC->num_msgs > 0) for (a=0; a<(CC->num_msgs); ++a) {
1350 if (MessageFromList(a) == delnum) {
1351 SetMessageInList(a, 0L);
1356 CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
1357 CC->quickroom.QRhighest = MessageFromList(CC->num_msgs - 1);
1359 put_msglist(&CC->quickroom);
1360 lputroom(&CC->quickroom,CC->quickroom.QRname);
1362 cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));
1363 cprintf("%d Message deleted.\n",OK);
1365 else cprintf("%d No message %ld.\n",ERROR,delnum);
1370 * move a message to another room
1372 void cmd_move(char *args)
1377 struct quickroom qtemp;
1380 num = extract_long(args,0);
1381 extract(targ,args,1);
1383 getuser(&CC->usersupp,CC->curr_user);
1384 if ((CC->usersupp.axlevel < 6)
1385 && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
1386 cprintf("%d Higher access required.\n",
1387 ERROR+HIGHER_ACCESS_REQUIRED);
1391 if (getroom(&qtemp, targ) != 0) {
1392 cprintf("%d '%s' does not exist.\n",ERROR,targ);
1396 /* yank the message out of the current room... */
1397 lgetroom(&CC->quickroom, CC->quickroom.QRname);
1398 get_msglist(&CC->quickroom);
1401 for (a=0; a<(CC->num_msgs); ++a) {
1402 if (MessageFromList(a) == num) {
1404 SetMessageInList(a, 0L);
1408 CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
1409 put_msglist(&CC->quickroom);
1410 CC->quickroom.QRhighest = MessageFromList((CC->num_msgs)-1);
1412 lputroom(&CC->quickroom,CC->quickroom.QRname);
1414 cprintf("%d msg %ld does not exist.\n",ERROR,num);
1418 /* put the message into the target room */
1419 lgetroom(&qtemp, targ);
1420 qtemp.QRhighest = AddMessageToRoom(&qtemp, num);
1421 lputroom(&qtemp, targ);
1423 cprintf("%d Message moved.\n", OK);