21 #include "sysdep_decls.h"
26 #include "dynloader.h"
28 #include "mime_parser.h"
37 extern struct config config;
41 * This function is self explanatory.
42 * (What can I say, I'm in a weird mood today...)
44 void remove_any_whitespace_to_the_left_or_right_of_at_symbol(char *name) {
47 for (i=0; i<strlen(name); ++i) if (name[i]=='@') {
48 if (i>0) if (isspace(name[i-1])) {
49 strcpy(&name[i-1], &name[i]);
52 while (isspace(name[i+1])) {
53 strcpy(&name[i+1], &name[i+2]);
60 * Aliasing for network mail.
61 * (Error messages have been commented out, because this is a server.)
63 int alias(char *name) /* process alias and routing info for mail */
67 char aaa[300],bbb[300];
69 lprintf(9, "alias() called for <%s>\n", name);
71 remove_any_whitespace_to_the_left_or_right_of_at_symbol(name);
73 fp=fopen("network/mail.aliases","r");
74 if (fp==NULL) fp=fopen("/dev/null","r");
75 if (fp==NULL) return(M_ERROR);
76 GNA: strcpy(aaa,""); strcpy(bbb,"");
99 if (strcasecmp(name,aaa)) goto GNA;
102 lprintf(7, "Mail is being forwarded to %s\n", name);
104 DETYPE: /* determine local or remote type, see citadel.h */
105 for (a=0; a<strlen(name); ++a) if (name[a]=='!') return(M_INTERNET);
106 for (a=0; a<strlen(name); ++a)
108 for (b=a; b<strlen(name); ++b)
109 if (name[b]=='.') return(M_INTERNET);
110 b=0; for (a=0; a<strlen(name); ++a) if (name[a]=='@') ++b;
112 lprintf(7, "Too many @'s in address\n");
116 for (a=0; a<strlen(name); ++a)
117 if (name[a]=='@') strcpy(bbb,&name[a+1]);
118 while (bbb[0]==32) strcpy(bbb,&bbb[1]);
119 fp = fopen("network/mail.sysinfo","r");
120 if (fp==NULL) return(M_ERROR);
123 } while ((a>=0)&&(strcasecmp(aaa,bbb)));
125 if (!strncmp(aaa,"use ",4)) {
131 if (!strncmp(aaa,"uum",3)) {
133 for (a=0; a<strlen(bbb); ++a) {
134 if (bbb[a]=='@') bbb[a]=0;
135 if (bbb[a]==' ') bbb[a]='_';
137 while(bbb[strlen(bbb)-1]=='_') bbb[strlen(bbb)-1]=0;
138 sprintf(name,&aaa[4],bbb);
141 if (!strncmp(aaa,"bin",3)) {
142 strcpy(aaa,name); strcpy(bbb,name);
143 while (aaa[strlen(aaa)-1]!='@') aaa[strlen(aaa)-1]=0;
144 aaa[strlen(aaa)-1]=0;
145 while (aaa[strlen(aaa)-1]==' ') aaa[strlen(aaa)-1]=0;
146 while (bbb[0]!='@') strcpy(bbb,&bbb[1]);
148 while (bbb[0]==' ') strcpy(bbb,&bbb[1]);
149 sprintf(name,"%s @%s",aaa,bbb);
161 fp=fopen("citadel.control","r");
162 fread((char *)&CitControl,sizeof(struct CitControl),1,fp);
167 * cmd_msgs() - get list of message #'s in this room
169 void cmd_msgs(char *cmdbuf)
178 extract(which,cmdbuf,0);
182 if (!strncasecmp(which,"OLD",3)) mode = MSGS_OLD;
183 if (!strncasecmp(which,"NEW",3)) mode = MSGS_NEW;
184 if (!strncasecmp(which,"FIRST",5)) {
186 cm_howmany = extract_int(cmdbuf,1);
188 if (!strncasecmp(which,"LAST",4)) {
190 cm_howmany = extract_int(cmdbuf,1);
192 if (!strncasecmp(which,"GT",2)) {
194 cm_gt = extract_long(cmdbuf,1);
197 if ((!(CC->logged_in))&&(!(CC->internal_pgm))) {
198 cprintf("%d not logged in\n",ERROR+NOT_LOGGED_IN);
202 get_msglist(&CC->quickroom);
203 getuser(&CC->usersupp,CC->curr_user);
204 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
206 cprintf("%d Message list...\n",LISTING_FOLLOWS);
207 if (CC->num_msgs != 0) {
208 for (a=0; a<(CC->num_msgs); ++a)
209 if ((MessageFromList(a) >=0)
213 || ((mode==MSGS_OLD) && (MessageFromList(a) <= vbuf.v_lastseen))
214 || ((mode==MSGS_NEW) && (MessageFromList(a) > vbuf.v_lastseen))
215 || ((mode==MSGS_NEW) && (MessageFromList(a) >= vbuf.v_lastseen)
216 && (CC->usersupp.flags & US_LASTOLD))
217 || ((mode==MSGS_LAST)&& (a>=(CC->num_msgs-cm_howmany)))
218 || ((mode==MSGS_FIRST)&&(a<cm_howmany))
219 || ((mode==MSGS_GT) && (MessageFromList(a) > cm_gt))
223 cprintf("%ld\n", MessageFromList(a));
232 * help_subst() - support routine for help file viewer
234 void help_subst(char *strbuf, char *source, char *dest)
239 while (p=pattern2(strbuf,source), (p>=0)) {
240 strcpy(workbuf,&strbuf[p+strlen(source)]);
241 strcpy(&strbuf[p],dest);
242 strcat(strbuf,workbuf);
247 void do_help_subst(char *buffer)
251 help_subst(buffer,"^nodename",config.c_nodename);
252 help_subst(buffer,"^humannode",config.c_humannode);
253 help_subst(buffer,"^fqdn",config.c_fqdn);
254 help_subst(buffer,"^username",CC->usersupp.fullname);
255 sprintf(buf2,"%ld",CC->usersupp.usernum);
256 help_subst(buffer,"^usernum",buf2);
257 help_subst(buffer,"^sysadm",config.c_sysadm);
258 help_subst(buffer,"^variantname",CITADEL);
259 sprintf(buf2,"%d",config.c_maxsessions);
260 help_subst(buffer,"^maxsessions",buf2);
266 * memfmout() - Citadel text formatter and paginator.
267 * Although the original purpose of this routine was to format
268 * text to the reader's screen width, all we're really using it
269 * for here is to format text out to 80 columns before sending it
270 * to the client. The client software may reformat it again.
272 void memfmout(int width, char *mptr, char subst)
273 /* screen width to use */
274 /* where are we going to get our text from? */
275 /* nonzero if we should use hypertext mode */
284 strcpy(aaa,""); old=255;
286 c=1; /* c is the current pos */
289 while (ch=*mptr, ((ch!=0) && (strlen(buffer)<126) )) {
291 buffer[strlen(buffer)+1] = 0;
292 buffer[strlen(buffer)] = ch;
295 if (buffer[0]=='^') do_help_subst(buffer);
297 buffer[strlen(buffer)+1] = 0;
299 strcpy(buffer,&buffer[1]);
306 if (ch<=0) goto FMTEND;
308 if ( ((ch==13)||(ch==10)) && (old!=13) && (old!=10) ) ch=32;
309 if ( ((old==13)||(old==10)) && (isspace(real)) ) {
313 if (ch>126) goto FMTA;
316 if ( ((strlen(aaa)+c)>(width-5)) && (strlen(aaa)>(width-5)) )
317 { cprintf("\n%s",aaa); c=strlen(aaa); aaa[0]=0;
319 b=strlen(aaa); aaa[b]=ch; aaa[b+1]=0; }
321 if ((strlen(aaa)+c)>(width-5)) {
325 cprintf("%s ",aaa); ++c; c=c+strlen(aaa);
329 if ((ch==13)||(ch==10)) {
337 FMTEND: cprintf("\n");
343 * Callback function for mime parser that simply lists the part
345 void list_this_part(char *name, char *filename, char *partnum, char *disp,
346 void *content, char *cbtype, size_t length) {
348 cprintf("part=%s|%s|%s|%s|%s|%d\n",
349 name, filename, partnum, disp, cbtype, length);
354 * Callback function for mime parser that wants to display text
356 void fixed_output(char *name, char *filename, char *partnum, char *disp,
357 void *content, char *cbtype, size_t length) {
359 if (!strcasecmp(cbtype, "text/plain")) {
360 client_write(content, length);
363 cprintf("Part %s: %s (%s) (%d bytes)\n",
364 partnum, filename, cbtype, length);
370 * Callback function for mime parser that opens a section for downloading
372 void mime_download(char *name, char *filename, char *partnum, char *disp,
373 void *content, char *cbtype, size_t length) {
375 char tmpname[PATH_MAX];
378 /* Silently go away if there's already a download open... */
379 if (CC->download_fp != NULL) return;
381 /* ...or if this is not the desired section */
382 if (strcasecmp(CC->desired_section, partnum)) return;
384 snprintf(tmpname, sizeof tmpname,
385 "/tmp/CitServer.download.%4x.%4x", getpid(), ++seq);
387 CC->download_fp = fopen(tmpname, "wb+");
388 if (CC->download_fp == NULL) return;
390 /* Unlink the file while it's open, to guarantee that the
391 * temp file will always be deleted.
395 fwrite(content, length, 1, CC->download_fp);
396 fflush(CC->download_fp);
397 rewind(CC->download_fp);
399 OpenCmdResult(filename, cbtype);
405 * Get a message off disk. (return value is the message's timestamp)
408 time_t output_message(char *msgid, int mode, int headers_only) {
412 CIT_UBYTE format_type,anon_flag;
417 struct cdbdata *dmsgtext;
420 /* buffers needed for RFC822 translation */
429 msg_num = atol(msgid);
432 if ((!(CC->logged_in))&&(!(CC->internal_pgm))&&(mode!=MT_DATE)) {
433 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
437 /* We used to need to check in the current room's message list
438 * to determine where the message's disk position. We no longer need
439 * to do this, but we do it anyway as a security measure, in order to
440 * prevent rogue clients from reading messages not in the current room.
444 if (CC->num_msgs > 0) {
445 for (a=0; a<CC->num_msgs; ++a) {
446 if (MessageFromList(a) == msg_num) {
454 cprintf("%d Message %ld is not in this room.\n",
460 dmsgtext = cdb_fetch(CDB_MSGMAIN, &msg_num, sizeof(long));
462 if (dmsgtext == NULL) {
464 cprintf("%d Can't find message %ld\n",
465 ERROR+INTERNAL_ERROR);
469 msg_len = (long) dmsgtext->len;
470 mptr = dmsgtext->ptr;
472 /* this loop spews out the whole message if we're doing raw format */
473 if (mode == MT_RAW) {
474 cprintf("%d %ld\n", BINARY_FOLLOWS, msg_len);
475 client_write(dmsgtext->ptr, (int) msg_len);
480 /* Otherwise, we'll start parsing it field by field... */
483 cprintf("%d Illegal message format on disk\n",
484 ERROR+INTERNAL_ERROR);
490 format_type = *mptr++;
492 /* Are we downloading a MIME component? */
493 if (mode == MT_DOWNLOAD) {
494 if (format_type != 4) {
495 cprintf("%d This is not a MIME message.\n",
498 else if (CC->download_fp != NULL) {
499 cprintf("%d You already have a download open.\n",
503 /* Skip to the message body */
504 while(ch = *mptr++, (ch!='M' && ch!=0)) {
507 buf[strlen(buf)+1] = 0;
509 buf[strlen(buf)] = rch;
513 mime_parser(mptr, NULL, *mime_download);
514 /* If there's no file open by this time, the requested
515 * section wasn't found, so print an error
517 if (CC->download_fp == NULL) {
518 cprintf("%d Section %s not found.\n",
519 ERROR+FILE_NOT_FOUND,
520 CC->desired_section);
527 /* Are we just looking for the message date? */
528 if (mode == MT_DATE) while(ch = *mptr++, (ch!='M' && ch!=0)) {
531 buf[strlen(buf)+1] = 0;
533 buf[strlen(buf)] = rch;
544 /* now for the user-mode message reading loops */
545 cprintf("%d Message %ld:\n",LISTING_FOLLOWS,msg_num);
547 if (mode == MT_CITADEL) cprintf("type=%d\n", format_type);
549 if ( (anon_flag == MES_ANON) && (mode == MT_CITADEL) ) {
550 cprintf("nhdr=yes\n");
553 /* begin header processing loop for Citadel message format */
555 if ((mode == MT_CITADEL)||(mode == MT_MIME))
556 while(ch = *mptr++, (ch!='M' && ch!=0)) {
559 buf[strlen(buf)+1] = 0;
561 buf[strlen(buf)] = rch;
565 PerformUserHooks(buf, (-1L), EVT_OUTPUTMSG);
566 if (anon_flag==MES_ANON) cprintf("from=****");
567 else if (anon_flag==MES_AN2) cprintf("from=anonymous");
568 else cprintf("from=%s",buf);
569 if ((is_room_aide()) && ((anon_flag == MES_ANON)
570 || (anon_flag == MES_AN2)))
571 cprintf(" [%s]",buf);
574 else if (ch=='P') cprintf("path=%s\n",buf);
575 else if (ch=='U') cprintf("subj=%s\n",buf);
576 else if (ch=='I') cprintf("msgn=%s\n",buf);
577 else if (ch=='H') cprintf("hnod=%s\n",buf);
578 else if (ch=='O') cprintf("room=%s\n",buf);
579 else if (ch=='N') cprintf("node=%s\n",buf);
580 else if (ch=='R') cprintf("rcpt=%s\n",buf);
581 else if (ch=='T') cprintf("time=%s\n",buf);
582 /* else cprintf("fld%c=%s\n",ch,buf); */
585 /* begin header processing loop for RFC822 transfer format */
589 strcpy(snode, NODENAME);
590 strcpy(lnode, HUMANNODE);
591 if (mode == MT_RFC822) while(ch = *mptr++, (ch!='M' && ch!=0)) {
594 buf[strlen(buf)+1] = 0;
596 buf[strlen(buf)] = rch;
599 if (ch=='A') strcpy(luser, buf);
601 cprintf("Path: %s\n",buf);
602 for (a=0; a<strlen(buf); ++a) {
604 strcpy(buf,&buf[a+1]);
610 else if (ch=='U') cprintf("Subject: %s\n",buf);
611 else if (ch=='I') strcpy(mid, buf);
612 else if (ch=='H') strcpy(lnode, buf);
613 else if (ch=='O') cprintf("X-Citadel-Room: %s\n",buf);
614 else if (ch=='N') strcpy(snode, buf);
615 else if (ch=='R') cprintf("To: %s\n",buf);
618 cprintf("Date: %s", asctime(localtime(&xtime)));
622 if (mode == MT_RFC822) {
623 if (!strcasecmp(snode, NODENAME)) {
626 cprintf("Message-ID: <%s@%s>\n", mid, snode);
627 PerformUserHooks(luser, (-1L), EVT_OUTPUTMSG);
628 cprintf("From: %s@%s (%s)\n",
629 suser, snode, luser);
630 cprintf("Organization: %s\n", lnode);
633 /* end header processing loop ... at this point, we're in the text */
636 cprintf("text\n*** ?Message truncated\n000\n");
641 /* do some sort of MIME output */
642 if (format_type == 4) {
643 if ((mode == MT_CITADEL)||(mode == MT_MIME)) {
644 mime_parser(mptr, NULL, *list_this_part);
646 if (mode == MT_MIME) { /* If MT_MIME then it's parts only */
654 /* give 'em a length */
656 while(ch = *mptr++, ch>0) {
659 cprintf("mlen=%ld\n", msg_len);
665 /* signify start of msg text */
666 if (mode == MT_CITADEL) cprintf("text\n");
667 if ( (mode == MT_RFC822) && (format_type != 4) ) cprintf("\n");
669 /* If the format type on disk is 1 (fixed-format), then we want
670 * everything to be output completely literally ... regardless of
671 * what message transfer format is in use.
673 if (format_type == 1) {
675 while(ch = *mptr++, ch>0) {
676 if (ch == 13) ch = 10;
677 if ( (ch == 10) || (strlen(buf)>250) ) {
678 cprintf("%s\n", buf);
682 buf[strlen(buf)+1] = 0;
683 buf[strlen(buf)] = ch;
686 if (strlen(buf)>0) cprintf("%s\n", buf);
688 /* If the message on disk is format 0 (Citadel vari-format), we
689 * output using the formatter at 80 columns. This is the final output
690 * form if the transfer format is RFC822, but if the transfer format
691 * is Citadel proprietary, it'll still work, because the indentation
692 * for new paragraphs is correct and the client will reformat the
693 * message to the reader's screen width.
695 if (format_type == 0) {
698 /* If the message on disk is format 4 (MIME), we've gotta hand it
699 * off to the MIME parser. The client has already been told that
700 * this message is format 1 (fixed format), so the callback function
701 * we use will display those parts as-is.
703 if (format_type == 4) {
704 mime_parser(mptr, NULL, *fixed_output);
715 * display a message (mode 0 - Citadel proprietary)
717 void cmd_msg0(char *cmdbuf)
720 int headers_only = 0;
722 extract(msgid,cmdbuf,0);
723 headers_only = extract_int(cmdbuf, 1);
725 output_message(msgid, MT_CITADEL, headers_only);
731 * display a message (mode 2 - RFC822)
733 void cmd_msg2(char *cmdbuf)
736 int headers_only = 0;
738 extract(msgid,cmdbuf,0);
739 headers_only = extract_int(cmdbuf,1);
741 output_message(msgid,MT_RFC822,headers_only);
745 * display a message (mode 3 - IGnet raw format - internal programs only)
747 void cmd_msg3(char *cmdbuf)
750 int headers_only = 0;
752 if (CC->internal_pgm == 0) {
753 cprintf("%d This command is for internal programs only.\n",
758 extract(msgid,cmdbuf,0);
759 headers_only = extract_int(cmdbuf,1);
761 output_message(msgid,MT_RAW,headers_only);
765 * display a message (mode 4 - MIME) (FIX ... still evolving, not complete)
767 void cmd_msg4(char *cmdbuf)
771 extract(msgid, cmdbuf, 0);
773 output_message(msgid, MT_MIME, 0);
779 * Open a component of a MIME message as a download file
781 void cmd_opna(char *cmdbuf)
785 extract(msgid, cmdbuf, 0);
786 extract(CC->desired_section, cmdbuf, 1);
788 output_message(msgid, MT_DOWNLOAD, 0);
794 * Message base operation to send a message to the master file
795 * (returns new message number)
797 long send_message(char *message_in_memory, /* pointer to buffer */
798 size_t message_length, /* length of buffer */
799 int generate_id) { /* 1 to generate an I field */
802 char *actual_message;
803 size_t actual_length;
807 /* Get a new message number */
808 newmsgid = get_new_message_number();
811 sprintf(msgidbuf, "I%ld", newmsgid);
812 actual_length = message_length + strlen(msgidbuf) + 1;
813 actual_message = mallok(actual_length);
814 memcpy(actual_message, message_in_memory, 3);
815 memcpy(&actual_message[3], msgidbuf, (strlen(msgidbuf)+1) );
816 memcpy(&actual_message[strlen(msgidbuf)+4],
817 &message_in_memory[3], message_length - 3);
821 actual_message = message_in_memory;
822 actual_length = message_length;
825 /* Write our little bundle of joy into the message base */
826 begin_critical_section(S_MSGMAIN);
827 if ( cdb_store(CDB_MSGMAIN, &newmsgid, sizeof(long),
828 actual_message, actual_length) < 0 ) {
829 lprintf(2, "Can't store message\n");
835 end_critical_section(S_MSGMAIN);
838 phree(actual_message);
841 /* Finally, return the pointers */
848 * this is a simple file copy routine.
850 void copy_file(char *from, char *to)
856 if (ffp==NULL) return;
862 while (a=getc(ffp), a>=0) {
873 * message base operation to save a message and install its pointers
875 void save_message(char *mtmp, /* file containing proper message */
876 char *rec, /* Recipient (if mail) */
877 char *force, /* if non-zero length, force a room */
878 int mailtype, /* local or remote type, see citadel.h */
879 int generate_id) /* set to 1 to generate an 'I' field */
882 char hold_rm[ROOMNAMELEN];
883 char actual_rm[ROOMNAMELEN];
884 char force_room[ROOMNAMELEN];
887 char *message_in_memory;
891 struct usersupp userbuf;
893 static int seqnum = 0;
895 lprintf(9, "save_message(%s,%s,%s,%d,%d)\n",
896 mtmp, rec, force_room, mailtype, generate_id);
898 strcpy(force_room, force);
900 /* Strip non-printable characters out of the recipient name */
901 strcpy(recipient, rec);
902 for (a=0; a<strlen(recipient); ++a)
903 if (!isprint(recipient[a]))
904 strcpy(&recipient[a], &recipient[a+1]);
906 /* Measure the message */
907 stat(mtmp, &statbuf);
908 templen = statbuf.st_size;
910 /* Now read it into memory */
911 message_in_memory = (char *) mallok(templen);
912 if (message_in_memory == NULL) {
913 lprintf(2, "Can't allocate memory to save message!\n");
917 fp = fopen(mtmp, "rb");
918 fread(message_in_memory, templen, 1, fp);
921 newmsgid = send_message(message_in_memory, templen, generate_id);
922 phree(message_in_memory);
923 if (newmsgid <= 0L) return;
925 strcpy(actual_rm, CC->quickroom.QRname);
928 /* If the user is a twit, move to the twit room for posting... */
929 if (TWITDETECT) if (CC->usersupp.axlevel==2) {
930 strcpy(hold_rm, actual_rm);
931 strcpy(actual_rm, config.c_twitroom);
934 /* ...or if this is a private message, go to the target mailbox. */
935 lprintf(9, "mailbox aliasing loop\n");
937 if (strlen(recipient) > 0) {
939 /* mailtype = alias(recipient); */
940 if (mailtype == M_LOCAL) {
942 if (getuser(&userbuf, recipient)!=0) {
943 /* User not found, goto Aide */
945 strcpy(force_room, AIDEROOM);
949 strcpy(hold_rm, actual_rm);
951 MailboxName(actual_rm, &userbuf, MAILROOM);
956 /* ...or if this message is destined for Aide> then go there. */
957 lprintf(9, "actual room forcing loop\n");
958 if (strlen(force_room) > 0) {
959 strcpy(hold_rm, actual_rm);
960 strcpy(actual_rm, force_room);
963 /* This call to usergoto() changes rooms if necessary. It also
964 * causes the latest message list to be read into memory.
966 usergoto(actual_rm, 0);
968 /* read in the quickroom record, obtaining a lock... */
969 lgetroom(&CC->quickroom, actual_rm);
971 /* Fix an obscure bug */
972 if (!strcasecmp(CC->quickroom.QRname, AIDEROOM)) {
973 CC->quickroom.QRflags = CC->quickroom.QRflags & ~QR_MAILBOX;
976 /* Add the message pointer to the room */
977 CC->quickroom.QRhighest = AddMessageToRoom(&CC->quickroom, newmsgid);
979 /* update quickroom */
980 lputroom(&CC->quickroom, actual_rm);
982 /* Network mail - send a copy to the network program. */
983 if ( (strlen(recipient)>0) && (mailtype != M_LOCAL) ) {
984 sprintf(aaa,"./network/spoolin/netmail.%04lx.%04x.%04x",
985 (long)getpid(), CC->cs_pid, ++seqnum);
987 system("exec nohup ./netproc -i >/dev/null 2>&1 &");
990 /* Bump this user's messages posted counter. */
991 lgetuser(&CC->usersupp, CC->curr_user);
992 CC->usersupp.posted = CC->usersupp.posted + 1;
993 lputuser(&CC->usersupp, CC->curr_user);
995 /* If we've posted in a room other than the current room, then we
996 * have to now go back to the current room...
998 if (strlen(hold_rm) > 0) {
999 usergoto(hold_rm, 0);
1001 unlink(mtmp); /* delete the temporary file */
1006 * Generate an administrative message and post it in the Aide> room.
1008 void aide_message(char *text)
1012 fp=fopen(CC->temp,"wb");
1013 fprintf(fp,"%c%c%c",255,MES_NORMAL,0);
1014 fprintf(fp,"Psysop%c",0);
1015 fprintf(fp,"T%ld%c", time(NULL), 0);
1016 fprintf(fp,"ACitadel%c",0);
1017 fprintf(fp,"OAide%c",0);
1018 fprintf(fp,"N%s%c",NODENAME,0);
1019 fprintf(fp,"M%s\n%c",text,0);
1021 save_message(CC->temp,"",AIDEROOM,M_LOCAL,1);
1022 syslog(LOG_NOTICE,text);
1028 * Build a binary message to be saved on disk.
1031 char *filename, /* temporary file name */
1032 struct usersupp *author, /* author's usersupp structure */
1033 char *recipient, /* NULL if it's not mail */
1034 char *room, /* room where it's going */
1035 int type, /* see MES_ types in header file */
1036 int net_type, /* see MES_ types in header file */
1037 int format_type, /* local or remote (see citadel.h) */
1038 char *fake_name) { /* who we're masquerading as */
1046 /* Don't confuse the poor folks if it's not routed mail. */
1047 strcpy(dest_node, "");
1050 /* If net_type is M_BINARY, split out the destination node. */
1051 if (net_type == M_BINARY) {
1052 strcpy(dest_node,NODENAME);
1053 for (a=0; a<strlen(recipient); ++a) {
1054 if (recipient[a]=='@') {
1056 strcpy(dest_node,&recipient[a+1]);
1061 /* if net_type is M_INTERNET, set the dest node to 'internet' */
1062 if (net_type == M_INTERNET) {
1063 strcpy(dest_node,"internet");
1066 while (isspace(recipient[strlen(recipient)-1]))
1067 recipient[strlen(recipient)-1] = 0;
1070 fp=fopen(filename,"w");
1072 putc(type,fp); /* Normal or anonymous, see MES_ flags */
1073 putc(format_type,fp); /* Formatted or unformatted */
1074 fprintf(fp,"Pcit%ld%c",author->usernum,0); /* path */
1075 fprintf(fp,"T%ld%c",now,0); /* date/time */
1077 fprintf(fp,"A%s%c",fake_name,0);
1079 fprintf(fp,"A%s%c",author->fullname,0); /* author */
1081 if (CC->quickroom.QRflags & QR_MAILBOX) { /* room */
1082 fprintf(fp,"O%s%c", &CC->quickroom.QRname[11], 0);
1085 fprintf(fp,"O%s%c",CC->quickroom.QRname,0);
1088 fprintf(fp,"N%s%c",NODENAME,0); /* nodename */
1089 fprintf(fp,"H%s%c",HUMANNODE,0); /* human nodename */
1091 if (recipient[0]!=0) fprintf(fp, "R%s%c", recipient, 0);
1092 if (dest_node[0]!=0) fprintf(fp, "D%s%c", dest_node, 0);
1096 while (client_gets(buf), strcmp(buf,"000"))
1098 fprintf(fp,"%s\n",buf);
1100 syslog(LOG_INFO, "Closing message");
1110 * message entry - mode 0 (normal) <bc>
1112 void cmd_ent0(char *entargs)
1115 char recipient[256];
1117 int format_type = 0;
1118 char newusername[256]; /* <bc> */
1123 struct usersupp tempUS;
1126 post = extract_int(entargs,0);
1127 extract(recipient,entargs,1);
1128 anon_flag = extract_int(entargs,2);
1129 format_type = extract_int(entargs,3);
1131 /* first check to make sure the request is valid. */
1133 if (!(CC->logged_in)) {
1134 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1137 if ((CC->usersupp.axlevel<2)&&((CC->quickroom.QRflags&QR_MAILBOX)==0)) {
1138 cprintf("%d Need to be validated to enter ",
1139 ERROR+HIGHER_ACCESS_REQUIRED);
1140 cprintf("(except in %s> to sysop)\n", MAILROOM);
1143 if ((CC->usersupp.axlevel<4)&&(CC->quickroom.QRflags&QR_NETWORK)) {
1144 cprintf("%d Need net privileges to enter here.\n",
1145 ERROR+HIGHER_ACCESS_REQUIRED);
1148 if ((CC->usersupp.axlevel<6)&&(CC->quickroom.QRflags&QR_READONLY)) {
1149 cprintf("%d Sorry, this is a read-only room.\n",
1150 ERROR+HIGHER_ACCESS_REQUIRED);
1157 if (post==2) { /* <bc> */
1158 if (CC->usersupp.axlevel<6)
1160 cprintf("%d You don't have permission to do an aide post.\n",
1161 ERROR+HIGHER_ACCESS_REQUIRED);
1164 extract(newusername,entargs,4);
1165 memset(CC->fake_postname, 0, 32);
1166 strcpy(CC->fake_postname, newusername);
1167 cprintf("%d Ok\n",OK);
1171 CC->cs_flags |= CS_POSTING;
1174 if (CC->quickroom.QRflags & QR_MAILBOX) {
1175 if (CC->usersupp.axlevel>=2) {
1176 strcpy(buf,recipient);
1178 else strcpy(buf,"sysop");
1179 e=alias(buf); /* alias and mail type */
1180 if ((buf[0]==0) || (e==M_ERROR)) {
1181 cprintf("%d Unknown address - cannot send message.\n",
1182 ERROR+NO_SUCH_USER);
1185 if ((e!=M_LOCAL)&&(CC->usersupp.axlevel<4)) {
1186 cprintf("%d Net privileges required for network mail.\n",
1187 ERROR+HIGHER_ACCESS_REQUIRED);
1190 if ((RESTRICT_INTERNET==1)&&(e==M_INTERNET)
1191 &&((CC->usersupp.flags&US_INTERNET)==0)
1192 &&(!CC->internal_pgm) ) {
1193 cprintf("%d You don't have access to Internet mail.\n",
1194 ERROR+HIGHER_ACCESS_REQUIRED);
1197 if (!strcasecmp(buf,"sysop")) {
1201 if (e!=M_LOCAL) goto SKFALL; /* don't search local file */
1202 if (!strcasecmp(buf,CC->usersupp.fullname)) {
1203 cprintf("%d Can't send mail to yourself!\n",
1204 ERROR+NO_SUCH_USER);
1208 /* Check to make sure the user exists; also get the correct
1209 * upper/lower casing of the name.
1211 a = getuser(&tempUS,buf);
1213 cprintf("%d No such user.\n",ERROR+NO_SUCH_USER);
1216 strcpy(buf,tempUS.fullname);
1219 SKFALL: b=MES_NORMAL;
1220 if (CC->quickroom.QRflags&QR_ANONONLY) b=MES_ANON;
1221 if (CC->quickroom.QRflags&QR_ANONOPT) {
1222 if (anon_flag==1) b=MES_AN2;
1224 if ((CC->quickroom.QRflags & QR_MAILBOX) == 0) buf[0]=0;
1226 /* If we're only checking the validity of the request, return
1227 * success without creating the message.
1230 cprintf("%d %s\n",OK,buf);
1234 cprintf("%d send message\n",SEND_LISTING);
1235 if (CC->fake_postname[0])
1236 make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, CC->fake_postname);
1238 if (CC->fake_username[0])
1239 make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, CC->fake_username);
1241 make_message(CC->temp,&CC->usersupp,buf,CC->quickroom.QRname,b,e,format_type, "");
1242 save_message(CC->temp,buf, (mtsflag ? AIDEROOM : ""), e,1);
1243 CC->fake_postname[0]='\0';
1250 * message entry - mode 3 (raw)
1252 void cmd_ent3(char *entargs)
1258 struct usersupp tempUS;
1263 if (CC->internal_pgm == 0) {
1264 cprintf("%d This command is for internal programs only.\n",
1269 /* See if there's a recipient, but make sure it's a real one */
1270 extract(recp, entargs, 1);
1271 for (a=0; a<strlen(recp); ++a)
1272 if (!isprint(recp[a]))
1273 strcpy(&recp[a], &recp[a+1]);
1274 while (isspace(recp[0])) strcpy(recp, &recp[1]);
1275 while (isspace(recp[strlen(recp)-1])) recp[strlen(recp)-1] = 0;
1277 /* If we're in Mail, check the recipient */
1278 if (strlen(recp) > 0) {
1279 e=alias(recp); /* alias and mail type */
1280 if ((recp[0]==0) || (e==M_ERROR)) {
1281 cprintf("%d Unknown address - cannot send message.\n",
1282 ERROR+NO_SUCH_USER);
1286 a = getuser(&tempUS,recp);
1288 cprintf("%d No such user.\n",
1289 ERROR+NO_SUCH_USER);
1295 /* At this point, message has been approved. */
1296 if (extract_int(entargs, 0) == 0) {
1297 cprintf("%d OK to send\n", OK);
1301 /* open a temp file to hold the message */
1302 fp = fopen(CC->temp, "wb");
1304 cprintf("%d Cannot open %s: %s\n",
1305 ERROR + INTERNAL_ERROR,
1306 CC->temp, strerror(errno) );
1310 msglen = extract_long(entargs, 2);
1311 cprintf("%d %ld\n", SEND_BINARY, msglen);
1312 while(msglen > 0L) {
1313 bloklen = ((msglen >= 255L) ? 255 : msglen);
1314 client_read(buf, (int)bloklen );
1315 fwrite(buf, (int)bloklen, 1, fp);
1316 msglen = msglen - bloklen;
1320 save_message(CC->temp, recp, "", e, 0);
1325 * Delete message from current room
1327 void cmd_dele(char *delstr)
1332 getuser(&CC->usersupp,CC->curr_user);
1333 if ((CC->usersupp.axlevel < 6)
1334 && (CC->usersupp.usernum != CC->quickroom.QRroomaide)
1335 && ((CC->quickroom.QRflags & QR_MAILBOX) == 0)) {
1336 cprintf("%d Higher access required.\n",
1337 ERROR+HIGHER_ACCESS_REQUIRED);
1341 delnum = extract_long(delstr, 0);
1343 /* get room records, obtaining a lock... */
1344 lgetroom(&CC->quickroom,CC->quickroom.QRname);
1345 get_msglist(&CC->quickroom);
1348 if (CC->num_msgs > 0) for (a=0; a<(CC->num_msgs); ++a) {
1349 if (MessageFromList(a) == delnum) {
1350 SetMessageInList(a, 0L);
1355 CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
1356 CC->quickroom.QRhighest = MessageFromList(CC->num_msgs - 1);
1358 put_msglist(&CC->quickroom);
1359 lputroom(&CC->quickroom,CC->quickroom.QRname);
1361 cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));
1362 cprintf("%d Message deleted.\n",OK);
1364 else cprintf("%d No message %ld.\n",ERROR,delnum);
1369 * move a message to another room
1371 void cmd_move(char *args)
1376 struct quickroom qtemp;
1379 num = extract_long(args,0);
1380 extract(targ,args,1);
1382 getuser(&CC->usersupp,CC->curr_user);
1383 if ((CC->usersupp.axlevel < 6)
1384 && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
1385 cprintf("%d Higher access required.\n",
1386 ERROR+HIGHER_ACCESS_REQUIRED);
1390 if (getroom(&qtemp, targ) != 0) {
1391 cprintf("%d '%s' does not exist.\n",ERROR,targ);
1395 /* yank the message out of the current room... */
1396 lgetroom(&CC->quickroom, CC->quickroom.QRname);
1397 get_msglist(&CC->quickroom);
1400 for (a=0; a<(CC->num_msgs); ++a) {
1401 if (MessageFromList(a) == num) {
1403 SetMessageInList(a, 0L);
1407 CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
1408 put_msglist(&CC->quickroom);
1409 CC->quickroom.QRhighest = MessageFromList((CC->num_msgs)-1);
1411 lputroom(&CC->quickroom,CC->quickroom.QRname);
1413 cprintf("%d msg %ld does not exist.\n",ERROR,num);
1417 /* put the message into the target room */
1418 lgetroom(&qtemp, targ);
1419 qtemp.QRhighest = AddMessageToRoom(&qtemp, num);
1420 lputroom(&qtemp, targ);
1422 cprintf("%d Message moved.\n", OK);