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)
52 for (i = 0; i < strlen(name); ++i)
55 if (isspace(name[i - 1])) {
56 strcpy(&name[i - 1], &name[i]);
59 while (isspace(name[i + 1])) {
60 strcpy(&name[i + 1], &name[i + 2]);
67 * Aliasing for network mail.
68 * (Error messages have been commented out, because this is a server.)
71 { /* process alias and routing info for mail */
74 char aaa[300], bbb[300];
76 lprintf(9, "alias() called for <%s>\n", name);
78 remove_any_whitespace_to_the_left_or_right_of_at_symbol(name);
80 fp = fopen("network/mail.aliases", "r");
82 fp = fopen("/dev/null", "r");
87 while (fgets(aaa, sizeof aaa, fp) != NULL) {
88 while (isspace(name[0]))
89 strcpy(name, &name[1]);
90 aaa[strlen(aaa) - 1] = 0;
92 for (a = 0; a < strlen(aaa); ++a) {
94 strcpy(bbb, &aaa[a + 1]);
98 if (!strcasecmp(name, aaa))
102 lprintf(7, "Mail is being forwarded to %s\n", name);
104 /* determine local or remote type, see citadel.h */
105 for (a = 0; a < strlen(name); ++a)
107 return (MES_INTERNET);
108 for (a = 0; a < strlen(name); ++a)
110 for (b = a; b < strlen(name); ++b)
112 return (MES_INTERNET);
114 for (a = 0; a < strlen(name); ++a)
118 lprintf(7, "Too many @'s in address\n");
122 for (a = 0; a < strlen(name); ++a)
124 strcpy(bbb, &name[a + 1]);
126 strcpy(bbb, &bbb[1]);
127 fp = fopen("network/mail.sysinfo", "r");
131 a = getstring(fp, aaa);
132 } while ((a >= 0) && (strcasecmp(aaa, bbb)));
133 a = getstring(fp, aaa);
134 if (!strncmp(aaa, "use ", 4)) {
135 strcpy(bbb, &aaa[4]);
140 if (!strncmp(aaa, "uum", 3)) {
142 for (a = 0; a < strlen(bbb); ++a) {
148 while (bbb[strlen(bbb) - 1] == '_')
149 bbb[strlen(bbb) - 1] = 0;
150 sprintf(name, &aaa[4], bbb);
151 return (MES_INTERNET);
153 if (!strncmp(aaa, "bin", 3)) {
156 while (aaa[strlen(aaa) - 1] != '@')
157 aaa[strlen(aaa) - 1] = 0;
158 aaa[strlen(aaa) - 1] = 0;
159 while (aaa[strlen(aaa) - 1] == ' ')
160 aaa[strlen(aaa) - 1] = 0;
161 while (bbb[0] != '@')
162 strcpy(bbb, &bbb[1]);
163 strcpy(bbb, &bbb[1]);
164 while (bbb[0] == ' ')
165 strcpy(bbb, &bbb[1]);
166 sprintf(name, "%s @%s", aaa, bbb);
179 fp = fopen("citadel.control", "r");
180 fread((char *) &CitControl, sizeof(struct CitControl), 1, fp);
185 * cmd_msgs() - get list of message #'s in this room
187 void cmd_msgs(char *cmdbuf)
196 extract(which, cmdbuf, 0);
200 if (!strncasecmp(which, "OLD", 3))
202 if (!strncasecmp(which, "NEW", 3))
204 if (!strncasecmp(which, "FIRST", 5)) {
206 cm_howmany = extract_int(cmdbuf, 1);
208 if (!strncasecmp(which, "LAST", 4)) {
210 cm_howmany = extract_int(cmdbuf, 1);
212 if (!strncasecmp(which, "GT", 2)) {
214 cm_gt = extract_long(cmdbuf, 1);
216 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
217 cprintf("%d not logged in\n", ERROR + NOT_LOGGED_IN);
221 get_msglist(&CC->quickroom);
222 getuser(&CC->usersupp, CC->curr_user);
223 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
225 cprintf("%d Message list...\n", LISTING_FOLLOWS);
226 if (CC->num_msgs != 0) {
227 for (a = 0; a < (CC->num_msgs); ++a)
228 if ((MessageFromList(a) >= 0)
232 || ((mode == MSGS_OLD) && (MessageFromList(a) <= vbuf.v_lastseen))
233 || ((mode == MSGS_NEW) && (MessageFromList(a) > vbuf.v_lastseen))
234 || ((mode == MSGS_NEW) && (MessageFromList(a) >= vbuf.v_lastseen)
235 && (CC->usersupp.flags & US_LASTOLD))
236 || ((mode == MSGS_LAST) && (a >= (CC->num_msgs - cm_howmany)))
237 || ((mode == MSGS_FIRST) && (a < cm_howmany))
238 || ((mode == MSGS_GT) && (MessageFromList(a) > cm_gt))
241 cprintf("%ld\n", MessageFromList(a));
250 * help_subst() - support routine for help file viewer
252 void help_subst(char *strbuf, char *source, char *dest)
257 while (p = pattern2(strbuf, source), (p >= 0)) {
258 strcpy(workbuf, &strbuf[p + strlen(source)]);
259 strcpy(&strbuf[p], dest);
260 strcat(strbuf, workbuf);
265 void do_help_subst(char *buffer)
269 help_subst(buffer, "^nodename", config.c_nodename);
270 help_subst(buffer, "^humannode", config.c_humannode);
271 help_subst(buffer, "^fqdn", config.c_fqdn);
272 help_subst(buffer, "^username", CC->usersupp.fullname);
273 sprintf(buf2, "%ld", CC->usersupp.usernum);
274 help_subst(buffer, "^usernum", buf2);
275 help_subst(buffer, "^sysadm", config.c_sysadm);
276 help_subst(buffer, "^variantname", CITADEL);
277 sprintf(buf2, "%d", config.c_maxsessions);
278 help_subst(buffer, "^maxsessions", buf2);
284 * memfmout() - Citadel text formatter and paginator.
285 * Although the original purpose of this routine was to format
286 * text to the reader's screen width, all we're really using it
287 * for here is to format text out to 80 columns before sending it
288 * to the client. The client software may reformat it again.
290 void memfmout(int width, char *mptr, char subst)
291 /* screen width to use */
292 /* where are we going to get our text from? */
293 /* nonzero if we should use hypertext mode */
305 c = 1; /* c is the current pos */
308 while (ch = *mptr, ((ch != 0) && (strlen(buffer) < 126))) {
310 buffer[strlen(buffer) + 1] = 0;
311 buffer[strlen(buffer)] = ch;
314 if (buffer[0] == '^')
315 do_help_subst(buffer);
317 buffer[strlen(buffer) + 1] = 0;
319 strcpy(buffer, &buffer[1]);
328 if (((ch == 13) || (ch == 10)) && (old != 13) && (old != 10))
330 if (((old == 13) || (old == 10)) && (isspace(real))) {
338 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
339 cprintf("\n%s", aaa);
348 if ((strlen(aaa) + c) > (width - 5)) {
358 if ((ch == 13) || (ch == 10)) {
359 cprintf("%s\n", aaa);
366 FMTEND: cprintf("%s\n", aaa);
372 * Callback function for mime parser that simply lists the part
374 void list_this_part(char *name, char *filename, char *partnum, char *disp,
375 void *content, char *cbtype, size_t length)
378 cprintf("part=%s|%s|%s|%s|%s|%d\n",
379 name, filename, partnum, disp, cbtype, length);
384 * Callback function for mime parser that wants to display text
386 void fixed_output(char *name, char *filename, char *partnum, char *disp,
387 void *content, char *cbtype, size_t length)
390 if (!strcasecmp(cbtype, "text/plain")) {
391 client_write(content, length);
393 cprintf("Part %s: %s (%s) (%d bytes)\n",
394 partnum, filename, cbtype, length);
400 * Callback function for mime parser that opens a section for downloading
402 void mime_download(char *name, char *filename, char *partnum, char *disp,
403 void *content, char *cbtype, size_t length)
406 /* Silently go away if there's already a download open... */
407 if (CC->download_fp != NULL)
410 /* ...or if this is not the desired section */
411 if (strcasecmp(desired_section, partnum))
414 CC->download_fp = tmpfile();
415 if (CC->download_fp == NULL)
418 fwrite(content, length, 1, CC->download_fp);
419 fflush(CC->download_fp);
420 rewind(CC->download_fp);
422 OpenCmdResult(filename, cbtype);
428 * Get a message off disk. (return value is the message's timestamp)
431 time_t output_message(char *msgid, int mode, int headers_only)
436 CIT_UBYTE format_type, anon_flag;
441 struct cdbdata *dmsgtext;
444 /* buffers needed for RFC822 translation */
453 msg_num = atol(msgid);
456 if ((!(CC->logged_in)) && (!(CC->internal_pgm)) && (mode != MT_DATE)) {
457 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
460 /* We used to need to check in the current room's message list
461 * to determine where the message's disk position. We no longer need
462 * to do this, but we do it anyway as a security measure, in order to
463 * prevent rogue clients from reading messages not in the current room.
467 if (CC->num_msgs > 0) {
468 for (a = 0; a < CC->num_msgs; ++a) {
469 if (MessageFromList(a) == msg_num) {
476 cprintf("%d Message %ld is not in this room.\n",
480 dmsgtext = cdb_fetch(CDB_MSGMAIN, &msg_num, sizeof(long));
482 if (dmsgtext == NULL) {
484 cprintf("%d Can't find message %ld\n",
485 ERROR + INTERNAL_ERROR);
488 msg_len = (long) dmsgtext->len;
489 mptr = dmsgtext->ptr;
491 /* this loop spews out the whole message if we're doing raw format */
492 if (mode == MT_RAW) {
493 cprintf("%d %ld\n", BINARY_FOLLOWS, msg_len);
494 client_write(dmsgtext->ptr, (int) msg_len);
498 /* Otherwise, we'll start parsing it field by field... */
501 cprintf("%d Illegal message format on disk\n",
502 ERROR + INTERNAL_ERROR);
507 format_type = *mptr++;
509 /* Are we downloading a MIME component? */
510 if (mode == MT_DOWNLOAD) {
511 if (format_type != 4) {
512 cprintf("%d This is not a MIME message.\n",
514 } else if (CC->download_fp != NULL) {
515 cprintf("%d You already have a download open.\n",
518 /* Skip to the message body */
519 while (ch = *mptr++, (ch != 'M' && ch != 0)) {
522 buf[strlen(buf) + 1] = 0;
524 buf[strlen(buf)] = rch;
528 mime_parser(mptr, NULL, *mime_download);
529 /* If there's no file open by this time, the requested
530 * section wasn't found, so print an error
532 if (CC->download_fp == NULL) {
533 cprintf("%d Section %s not found.\n",
534 ERROR + FILE_NOT_FOUND,
541 /* Are we just looking for the message date? */
543 while (ch = *mptr++, (ch != 'M' && ch != 0)) {
546 buf[strlen(buf) + 1] = 0;
548 buf[strlen(buf)] = rch;
557 /* now for the user-mode message reading loops */
558 cprintf("%d Message %ld:\n", LISTING_FOLLOWS, msg_num);
560 if (mode == MT_CITADEL)
561 cprintf("type=%d\n", format_type);
563 if ((anon_flag == MES_ANON) && (mode == MT_CITADEL)) {
564 cprintf("nhdr=yes\n");
566 /* begin header processing loop for Citadel message format */
568 if ((mode == MT_CITADEL) || (mode == MT_MIME))
569 while (ch = *mptr++, (ch != 'M' && ch != 0)) {
572 buf[strlen(buf) + 1] = 0;
574 buf[strlen(buf)] = rch;
578 PerformUserHooks(buf, (-1L), EVT_OUTPUTMSG);
579 if (anon_flag == MES_ANON)
580 cprintf("from=****");
581 else if (anon_flag == MES_AN2)
582 cprintf("from=anonymous");
584 cprintf("from=%s", buf);
585 if ((is_room_aide()) && ((anon_flag == MES_ANON)
586 || (anon_flag == MES_AN2)))
587 cprintf(" [%s]", buf);
589 } else if (ch == 'P')
590 cprintf("path=%s\n", buf);
592 cprintf("subj=%s\n", buf);
594 cprintf("msgn=%s\n", buf);
596 cprintf("hnod=%s\n", buf);
598 cprintf("room=%s\n", buf);
600 cprintf("node=%s\n", buf);
602 cprintf("rcpt=%s\n", buf);
604 cprintf("time=%s\n", buf);
605 /* else cprintf("fld%c=%s\n",ch,buf); */
607 /* begin header processing loop for RFC822 transfer format */
611 strcpy(snode, NODENAME);
612 strcpy(lnode, HUMANNODE);
613 if (mode == MT_RFC822)
614 while (ch = *mptr++, (ch != 'M' && ch != 0)) {
617 buf[strlen(buf) + 1] = 0;
619 buf[strlen(buf)] = rch;
624 else if (ch == 'P') {
625 cprintf("Path: %s\n", buf);
626 for (a = 0; a < strlen(buf); ++a) {
628 strcpy(buf, &buf[a + 1]);
633 } else if (ch == 'U')
634 cprintf("Subject: %s\n", buf);
640 cprintf("X-Citadel-Room: %s\n", buf);
644 cprintf("To: %s\n", buf);
645 else if (ch == 'T') {
647 cprintf("Date: %s", asctime(localtime(&xtime)));
650 if (mode == MT_RFC822) {
651 if (!strcasecmp(snode, NODENAME)) {
654 cprintf("Message-ID: <%s@%s>\n", mid, snode);
655 PerformUserHooks(luser, (-1L), EVT_OUTPUTMSG);
656 cprintf("From: %s@%s (%s)\n",
657 suser, snode, luser);
658 cprintf("Organization: %s\n", lnode);
660 /* end header processing loop ... at this point, we're in the text */
663 cprintf("text\n*** ?Message truncated\n000\n");
667 /* do some sort of MIME output */
668 if (format_type == 4) {
669 if ((mode == MT_CITADEL) || (mode == MT_MIME)) {
670 mime_parser(mptr, NULL, *list_this_part);
672 if (mode == MT_MIME) { /* If MT_MIME then it's parts only */
679 /* give 'em a length */
681 while (ch = *mptr++, ch > 0) {
684 cprintf("mlen=%ld\n", msg_len);
689 /* signify start of msg text */
690 if (mode == MT_CITADEL)
692 if ((mode == MT_RFC822) && (format_type != 4))
695 /* If the format type on disk is 1 (fixed-format), then we want
696 * everything to be output completely literally ... regardless of
697 * what message transfer format is in use.
699 if (format_type == 1) {
701 while (ch = *mptr++, ch > 0) {
704 if ((ch == 10) || (strlen(buf) > 250)) {
705 cprintf("%s\n", buf);
708 buf[strlen(buf) + 1] = 0;
709 buf[strlen(buf)] = ch;
713 cprintf("%s\n", buf);
715 /* If the message on disk is format 0 (Citadel vari-format), we
716 * output using the formatter at 80 columns. This is the final output
717 * form if the transfer format is RFC822, but if the transfer format
718 * is Citadel proprietary, it'll still work, because the indentation
719 * for new paragraphs is correct and the client will reformat the
720 * message to the reader's screen width.
722 if (format_type == 0) {
723 memfmout(80, mptr, 0);
725 /* If the message on disk is format 4 (MIME), we've gotta hand it
726 * off to the MIME parser. The client has already been told that
727 * this message is format 1 (fixed format), so the callback function
728 * we use will display those parts as-is.
730 if (format_type == 4) {
731 mime_parser(mptr, NULL, *fixed_output);
741 * display a message (mode 0 - Citadel proprietary)
743 void cmd_msg0(char *cmdbuf)
746 int headers_only = 0;
748 extract(msgid, cmdbuf, 0);
749 headers_only = extract_int(cmdbuf, 1);
751 output_message(msgid, MT_CITADEL, headers_only);
757 * display a message (mode 2 - RFC822)
759 void cmd_msg2(char *cmdbuf)
762 int headers_only = 0;
764 extract(msgid, cmdbuf, 0);
765 headers_only = extract_int(cmdbuf, 1);
767 output_message(msgid, MT_RFC822, headers_only);
771 * display a message (mode 3 - IGnet raw format - internal programs only)
773 void cmd_msg3(char *cmdbuf)
776 int headers_only = 0;
778 if (CC->internal_pgm == 0) {
779 cprintf("%d This command is for internal programs only.\n",
783 extract(msgid, cmdbuf, 0);
784 headers_only = extract_int(cmdbuf, 1);
786 output_message(msgid, MT_RAW, headers_only);
790 * display a message (mode 4 - MIME) (FIX ... still evolving, not complete)
792 void cmd_msg4(char *cmdbuf)
796 extract(msgid, cmdbuf, 0);
798 output_message(msgid, MT_MIME, 0);
804 * Open a component of a MIME message as a download file
806 void cmd_opna(char *cmdbuf)
810 CtdlAllocUserData(SYM_DESIRED_SECTION, 64);
812 extract(msgid, cmdbuf, 0);
813 extract(desired_section, cmdbuf, 1);
815 output_message(msgid, MT_DOWNLOAD, 0);
821 * Message base operation to send a message to the master file
822 * (returns new message number)
824 long send_message(char *message_in_memory, /* pointer to buffer */
825 size_t message_length, /* length of buffer */
827 { /* 1 to generate an I field */
830 char *actual_message;
831 size_t actual_length;
835 /* Get a new message number */
836 newmsgid = get_new_message_number();
839 sprintf(msgidbuf, "I%ld", newmsgid);
840 actual_length = message_length + strlen(msgidbuf) + 1;
841 actual_message = mallok(actual_length);
842 memcpy(actual_message, message_in_memory, 3);
843 memcpy(&actual_message[3], msgidbuf, (strlen(msgidbuf) + 1));
844 memcpy(&actual_message[strlen(msgidbuf) + 4],
845 &message_in_memory[3], message_length - 3);
847 actual_message = message_in_memory;
848 actual_length = message_length;
851 /* Write our little bundle of joy into the message base */
852 begin_critical_section(S_MSGMAIN);
853 if (cdb_store(CDB_MSGMAIN, &newmsgid, sizeof(long),
854 actual_message, actual_length) < 0) {
855 lprintf(2, "Can't store message\n");
860 end_critical_section(S_MSGMAIN);
863 phree(actual_message);
865 /* Finally, return the pointers */
872 * this is a simple file copy routine.
874 void copy_file(char *from, char *to)
879 ffp = fopen(from, "r");
882 tfp = fopen(to, "w");
887 while (a = getc(ffp), a >= 0) {
898 * message base operation to save a message and install its pointers
900 void save_message(char *mtmp, /* file containing proper message */
901 char *rec, /* Recipient (if mail) */
902 char *force, /* if non-zero length, force a room */
903 int mailtype, /* local or remote type, see citadel.h */
905 { /* set to 1 to generate an 'I' field */
907 char hold_rm[ROOMNAMELEN];
908 char actual_rm[ROOMNAMELEN];
909 char force_room[ROOMNAMELEN];
912 char *message_in_memory;
916 struct usersupp userbuf;
918 static int seqnum = 0;
919 int successful_local_recipients;
920 struct quickroom qtemp;
922 lprintf(9, "save_message(%s,%s,%s,%d,%d)\n",
923 mtmp, rec, force, mailtype, generate_id);
925 strcpy(force_room, force);
927 /* Strip non-printable characters out of the recipient name */
928 strcpy(recipient, rec);
929 for (a = 0; a < strlen(recipient); ++a)
930 if (!isprint(recipient[a]))
931 strcpy(&recipient[a], &recipient[a + 1]);
933 /* Measure the message */
934 stat(mtmp, &statbuf);
935 templen = statbuf.st_size;
937 /* Now read it into memory */
938 message_in_memory = (char *) mallok(templen);
939 if (message_in_memory == NULL) {
940 lprintf(2, "Can't allocate memory to save message!\n");
943 fp = fopen(mtmp, "rb");
944 fread(message_in_memory, templen, 1, fp);
947 newmsgid = send_message(message_in_memory, templen, generate_id);
948 phree(message_in_memory);
952 strcpy(actual_rm, CC->quickroom.QRname);
955 /* If the user is a twit, move to the twit room for posting... */
957 if (CC->usersupp.axlevel == 2) {
958 strcpy(hold_rm, actual_rm);
959 strcpy(actual_rm, config.c_twitroom);
961 /* ...or if this message is destined for Aide> then go there. */
962 lprintf(9, "actual room forcing loop\n");
963 if (strlen(force_room) > 0) {
964 strcpy(hold_rm, actual_rm);
965 strcpy(actual_rm, force_room);
967 /* This call to usergoto() changes rooms if necessary. It also
968 * causes the latest message list to be read into memory.
970 usergoto(actual_rm, 0);
972 /* read in the quickroom record, obtaining a lock... */
973 lgetroom(&CC->quickroom, actual_rm);
975 /* Fix an obscure bug */
976 if (!strcasecmp(CC->quickroom.QRname, AIDEROOM)) {
977 CC->quickroom.QRflags = CC->quickroom.QRflags & ~QR_MAILBOX;
979 /* Add the message pointer to the room */
980 CC->quickroom.QRhighest = AddMessageToRoom(&CC->quickroom, newmsgid);
982 /* update quickroom */
983 lputroom(&CC->quickroom);
985 /* Network mail - send a copy to the network program. */
986 if ((strlen(recipient) > 0) && (mailtype != MES_LOCAL)) {
987 sprintf(aaa, "./network/spoolin/netmail.%04lx.%04x.%04x",
988 (long) getpid(), CC->cs_pid, ++seqnum);
989 copy_file(mtmp, aaa);
990 system("exec nohup ./netproc -i >/dev/null 2>&1 &");
992 /* Bump this user's messages posted counter. */
993 lgetuser(&CC->usersupp, CC->curr_user);
994 CC->usersupp.posted = CC->usersupp.posted + 1;
995 lputuser(&CC->usersupp);
997 /* If this is private, local mail, make a copy in the
998 * recipient's mailbox and bump the reference count.
1000 successful_local_recipients = 0;
1001 if ((strlen(recipient) > 0) && (mailtype == MES_LOCAL)) {
1002 if (getuser(&userbuf, recipient) == 0) {
1003 MailboxName(actual_rm, &userbuf, MAILROOM);
1004 lprintf(9, "Targeting mailbox: <%s>\n", actual_rm);
1005 if (lgetroom(&qtemp, actual_rm) == 0) {
1007 AddMessageToRoom(&qtemp, newmsgid);
1009 ++successful_local_recipients;
1013 /* If we've posted in a room other than the current room, then we
1014 * have to now go back to the current room...
1016 if (strlen(hold_rm) > 0) {
1017 usergoto(hold_rm, 0);
1019 unlink(mtmp); /* delete the temporary file */
1021 /* If the message was delivered to more than one location locally,
1022 * we have to bump the reference count accordingly.
1024 if (successful_local_recipients != 0) {
1025 AdjRefCount(newmsgid, successful_local_recipients);
1031 * Generate an administrative message and post it in the Aide> room.
1033 void aide_message(char *text)
1037 fp = fopen(CC->temp, "wb");
1038 fprintf(fp, "%c%c%c", 255, MES_NORMAL, 0);
1039 fprintf(fp, "Psysop%c", 0);
1040 fprintf(fp, "T%ld%c", (long) time(NULL), 0);
1041 fprintf(fp, "ACitadel%c", 0);
1042 fprintf(fp, "OAide%c", 0);
1043 fprintf(fp, "N%s%c", NODENAME, 0);
1044 fprintf(fp, "M%s\n%c", text, 0);
1046 save_message(CC->temp, "", AIDEROOM, MES_LOCAL, 1);
1047 syslog(LOG_NOTICE, text);
1053 * Build a binary message to be saved on disk.
1056 char *filename, /* temporary file name */
1057 struct usersupp *author, /* author's usersupp structure */
1058 char *recipient, /* NULL if it's not mail */
1059 char *room, /* room where it's going */
1060 int type, /* see MES_ types in header file */
1061 int net_type, /* see MES_ types in header file */
1062 int format_type, /* local or remote (see citadel.h) */
1064 { /* who we're masquerading as */
1072 /* Don't confuse the poor folks if it's not routed mail. */
1073 strcpy(dest_node, "");
1076 /* If net_type is MES_BINARY, split out the destination node. */
1077 if (net_type == MES_BINARY) {
1078 strcpy(dest_node, NODENAME);
1079 for (a = 0; a < strlen(recipient); ++a) {
1080 if (recipient[a] == '@') {
1082 strcpy(dest_node, &recipient[a + 1]);
1086 /* if net_type is MES_INTERNET, set the dest node to 'internet' */
1087 if (net_type == MES_INTERNET) {
1088 strcpy(dest_node, "internet");
1090 while (isspace(recipient[strlen(recipient) - 1]))
1091 recipient[strlen(recipient) - 1] = 0;
1094 fp = fopen(filename, "w");
1096 putc(type, fp); /* Normal or anonymous, see MES_ flags */
1097 putc(format_type, fp); /* Formatted or unformatted */
1098 fprintf(fp, "Pcit%ld%c", author->usernum, 0); /* path */
1099 fprintf(fp, "T%ld%c", (long) now, 0); /* date/time */
1101 fprintf(fp, "A%s%c", fake_name, 0);
1103 fprintf(fp, "A%s%c", author->fullname, 0); /* author */
1105 if (CC->quickroom.QRflags & QR_MAILBOX) { /* room */
1106 fprintf(fp, "O%s%c", &CC->quickroom.QRname[11], 0);
1108 fprintf(fp, "O%s%c", CC->quickroom.QRname, 0);
1111 fprintf(fp, "N%s%c", NODENAME, 0); /* nodename */
1112 fprintf(fp, "H%s%c", HUMANNODE, 0); /* human nodename */
1114 if (recipient[0] != 0)
1115 fprintf(fp, "R%s%c", recipient, 0);
1116 if (dest_node[0] != 0)
1117 fprintf(fp, "D%s%c", dest_node, 0);
1121 while (client_gets(buf), strcmp(buf, "000")) {
1122 fprintf(fp, "%s\n", buf);
1133 * message entry - mode 0 (normal) <bc>
1135 void cmd_ent0(char *entargs)
1138 char recipient[256];
1140 int format_type = 0;
1141 char newusername[256]; /* <bc> */
1146 struct usersupp tempUS;
1149 post = extract_int(entargs, 0);
1150 extract(recipient, entargs, 1);
1151 anon_flag = extract_int(entargs, 2);
1152 format_type = extract_int(entargs, 3);
1154 /* first check to make sure the request is valid. */
1156 if (!(CC->logged_in)) {
1157 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1160 if ((CC->usersupp.axlevel < 2) && ((CC->quickroom.QRflags & QR_MAILBOX) == 0)) {
1161 cprintf("%d Need to be validated to enter ",
1162 ERROR + HIGHER_ACCESS_REQUIRED);
1163 cprintf("(except in %s> to sysop)\n", MAILROOM);
1166 if ((CC->usersupp.axlevel < 4) && (CC->quickroom.QRflags & QR_NETWORK)) {
1167 cprintf("%d Need net privileges to enter here.\n",
1168 ERROR + HIGHER_ACCESS_REQUIRED);
1171 if ((CC->usersupp.axlevel < 6) && (CC->quickroom.QRflags & QR_READONLY)) {
1172 cprintf("%d Sorry, this is a read-only room.\n",
1173 ERROR + HIGHER_ACCESS_REQUIRED);
1179 if (post == 2) { /* <bc> */
1180 if (CC->usersupp.axlevel < 6) {
1181 cprintf("%d You don't have permission to do an aide post.\n",
1182 ERROR + HIGHER_ACCESS_REQUIRED);
1185 extract(newusername, entargs, 4);
1186 memset(CC->fake_postname, 0, 32);
1187 strcpy(CC->fake_postname, newusername);
1188 cprintf("%d Ok\n", OK);
1191 CC->cs_flags |= CS_POSTING;
1194 if (CC->quickroom.QRflags & QR_MAILBOX) {
1195 if (CC->usersupp.axlevel >= 2) {
1196 strcpy(buf, recipient);
1198 strcpy(buf, "sysop");
1199 lprintf(9, "calling alias()\n");
1200 e = alias(buf); /* alias and mail type */
1201 lprintf(9, "alias() returned %d\n", e);
1202 if ((buf[0] == 0) || (e == MES_ERROR)) {
1203 cprintf("%d Unknown address - cannot send message.\n",
1204 ERROR + NO_SUCH_USER);
1207 if ((e != MES_LOCAL) && (CC->usersupp.axlevel < 4)) {
1208 cprintf("%d Net privileges required for network mail.\n",
1209 ERROR + HIGHER_ACCESS_REQUIRED);
1212 if ((RESTRICT_INTERNET == 1) && (e == MES_INTERNET)
1213 && ((CC->usersupp.flags & US_INTERNET) == 0)
1214 && (!CC->internal_pgm)) {
1215 cprintf("%d You don't have access to Internet mail.\n",
1216 ERROR + HIGHER_ACCESS_REQUIRED);
1219 if (!strcasecmp(buf, "sysop")) {
1224 goto SKFALL; /* don't search local file */
1225 if (!strcasecmp(buf, CC->usersupp.fullname)) {
1226 cprintf("%d Can't send mail to yourself!\n",
1227 ERROR + NO_SUCH_USER);
1230 /* Check to make sure the user exists; also get the correct
1231 * upper/lower casing of the name.
1233 a = getuser(&tempUS, buf);
1235 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1238 strcpy(buf, tempUS.fullname);
1240 SKFALL:b = MES_NORMAL;
1241 if (CC->quickroom.QRflags & QR_ANONONLY)
1243 if (CC->quickroom.QRflags & QR_ANONOPT) {
1247 if ((CC->quickroom.QRflags & QR_MAILBOX) == 0)
1250 /* If we're only checking the validity of the request, return
1251 * success without creating the message.
1254 cprintf("%d %s\n", OK, buf);
1257 cprintf("%d send message\n", SEND_LISTING);
1258 if (CC->fake_postname[0])
1259 make_message(CC->temp, &CC->usersupp, buf, CC->quickroom.QRname, b, e, format_type, CC->fake_postname);
1260 else if (CC->fake_username[0])
1261 make_message(CC->temp, &CC->usersupp, buf, CC->quickroom.QRname, b, e, format_type, CC->fake_username);
1263 make_message(CC->temp, &CC->usersupp, buf, CC->quickroom.QRname, b, e, format_type, "");
1264 save_message(CC->temp, buf, (mtsflag ? AIDEROOM : ""), e, 1);
1265 CC->fake_postname[0] = '\0';
1272 * message entry - mode 3 (raw)
1274 void cmd_ent3(char *entargs)
1280 struct usersupp tempUS;
1285 if (CC->internal_pgm == 0) {
1286 cprintf("%d This command is for internal programs only.\n",
1290 /* See if there's a recipient, but make sure it's a real one */
1291 extract(recp, entargs, 1);
1292 for (a = 0; a < strlen(recp); ++a)
1293 if (!isprint(recp[a]))
1294 strcpy(&recp[a], &recp[a + 1]);
1295 while (isspace(recp[0]))
1296 strcpy(recp, &recp[1]);
1297 while (isspace(recp[strlen(recp) - 1]))
1298 recp[strlen(recp) - 1] = 0;
1300 /* If we're in Mail, check the recipient */
1301 if (strlen(recp) > 0) {
1302 e = alias(recp); /* alias and mail type */
1303 if ((recp[0] == 0) || (e == MES_ERROR)) {
1304 cprintf("%d Unknown address - cannot send message.\n",
1305 ERROR + NO_SUCH_USER);
1308 if (e == MES_LOCAL) {
1309 a = getuser(&tempUS, recp);
1311 cprintf("%d No such user.\n",
1312 ERROR + NO_SUCH_USER);
1317 /* At this point, message has been approved. */
1318 if (extract_int(entargs, 0) == 0) {
1319 cprintf("%d OK to send\n", OK);
1322 /* open a temp file to hold the message */
1323 fp = fopen(CC->temp, "wb");
1325 cprintf("%d Cannot open %s: %s\n",
1326 ERROR + INTERNAL_ERROR,
1327 CC->temp, strerror(errno));
1330 msglen = extract_long(entargs, 2);
1331 cprintf("%d %ld\n", SEND_BINARY, msglen);
1332 while (msglen > 0L) {
1333 bloklen = ((msglen >= 255L) ? 255 : msglen);
1334 client_read(buf, (int) bloklen);
1335 fwrite(buf, (int) bloklen, 1, fp);
1336 msglen = msglen - bloklen;
1340 save_message(CC->temp, recp, "", e, 0);
1345 * Delete message from current room
1347 void cmd_dele(char *delstr)
1352 getuser(&CC->usersupp, CC->curr_user);
1353 if ((CC->usersupp.axlevel < 6)
1354 && (CC->usersupp.usernum != CC->quickroom.QRroomaide)
1355 && ((CC->quickroom.QRflags & QR_MAILBOX) == 0)) {
1356 cprintf("%d Higher access required.\n",
1357 ERROR + HIGHER_ACCESS_REQUIRED);
1360 delnum = extract_long(delstr, 0);
1362 /* get room records, obtaining a lock... */
1363 lgetroom(&CC->quickroom, CC->quickroom.QRname);
1364 get_msglist(&CC->quickroom);
1367 if (CC->num_msgs > 0)
1368 for (a = 0; a < (CC->num_msgs); ++a) {
1369 if (MessageFromList(a) == delnum) {
1370 SetMessageInList(a, 0L);
1374 CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
1375 CC->quickroom.QRhighest = MessageFromList(CC->num_msgs - 1);
1377 put_msglist(&CC->quickroom);
1378 lputroom(&CC->quickroom);
1380 AdjRefCount(delnum, -1);
1381 cprintf("%d Message deleted.\n", OK);
1383 cprintf("%d No message %ld.\n", ERROR, delnum);
1388 * move a message to another room
1390 void cmd_move(char *args)
1395 struct quickroom qtemp;
1398 num = extract_long(args, 0);
1399 extract(targ, args, 1);
1401 getuser(&CC->usersupp, CC->curr_user);
1402 if ((CC->usersupp.axlevel < 6)
1403 && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
1404 cprintf("%d Higher access required.\n",
1405 ERROR + HIGHER_ACCESS_REQUIRED);
1408 if (getroom(&qtemp, targ) != 0) {
1409 cprintf("%d '%s' does not exist.\n", ERROR, targ);
1412 /* yank the message out of the current room... */
1413 lgetroom(&CC->quickroom, CC->quickroom.QRname);
1414 get_msglist(&CC->quickroom);
1417 for (a = 0; a < (CC->num_msgs); ++a) {
1418 if (MessageFromList(a) == num) {
1420 SetMessageInList(a, 0L);
1424 CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
1425 put_msglist(&CC->quickroom);
1426 CC->quickroom.QRhighest = MessageFromList((CC->num_msgs) - 1);
1428 lputroom(&CC->quickroom);
1430 cprintf("%d msg %ld does not exist.\n", ERROR, num);
1433 /* put the message into the target room */
1434 lgetroom(&qtemp, targ);
1435 qtemp.QRhighest = AddMessageToRoom(&qtemp, num);
1438 cprintf("%d Message moved.\n", OK);
1444 * GetSuppMsgInfo() - Get the supplementary record for a message
1446 void GetSuppMsgInfo(struct SuppMsgInfo *smibuf, long msgnum)
1449 struct cdbdata *cdbsmi;
1452 memset(smibuf, 0, sizeof(struct SuppMsgInfo));
1453 smibuf->smi_msgnum = msgnum;
1454 smibuf->smi_refcount = 1; /* Default reference count is 1 */
1456 /* Use the negative of the message number for its supp record index */
1457 TheIndex = (0L - msgnum);
1459 cdbsmi = cdb_fetch(CDB_MSGMAIN, &TheIndex, sizeof(long));
1460 if (cdbsmi == NULL) {
1461 return; /* record not found; go with defaults */
1463 memcpy(smibuf, cdbsmi->ptr,
1464 ((cdbsmi->len > sizeof(struct SuppMsgInfo)) ?
1465 sizeof(struct SuppMsgInfo) : cdbsmi->len));
1472 * PutSuppMsgInfo() - (re)write supplementary record for a message
1474 void PutSuppMsgInfo(struct SuppMsgInfo *smibuf)
1478 /* Use the negative of the message number for its supp record index */
1479 TheIndex = (0L - smibuf->smi_msgnum);
1481 cdb_store(CDB_MSGMAIN,
1482 &TheIndex, sizeof(long),
1483 smibuf, sizeof(struct SuppMsgInfo));
1490 * AdjRefCount - change the reference count for a message;
1491 * delete the message if it reaches zero
1493 void AdjRefCount(long msgnum, int incr)
1496 struct SuppMsgInfo smi;
1499 lprintf(9, "Adjust msg <%ld> ref count by <%d>\n",
1502 /* This is a *tight* critical section; please keep it that way, as
1503 * it may get called while nested in other critical sections.
1504 * Complicating this any further will surely cause deadlock!
1506 begin_critical_section(S_SUPPMSGMAIN);
1507 GetSuppMsgInfo(&smi, msgnum);
1508 smi.smi_refcount += incr;
1509 PutSuppMsgInfo(&smi);
1510 end_critical_section(S_SUPPMSGMAIN);
1512 lprintf(9, "Ref count for message <%ld> after write is <%d>\n",
1513 msgnum, smi.smi_refcount);
1515 /* If the reference count is now zero, delete the message
1516 * (and its supplementary record as well).
1518 if (smi.smi_refcount == 0) {
1519 lprintf(9, "Deleting message <%ld>\n", msgnum);
1521 cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));
1522 delnum = (0L - msgnum);
1523 cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));