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);
186 void simple_listing(long msgnum) {
187 cprintf("%ld\n", msgnum);
192 * API function to perform an operation for each qualifying message in the
195 void CtdlForEachMessage(int mode, long ref,
196 void (*CallBack) (long msgnum) ) {
202 get_msglist(&CC->quickroom);
203 getuser(&CC->usersupp, CC->curr_user);
204 CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
206 if (CC->num_msgs != 0) for (a = 0; a < (CC->num_msgs); ++a) {
207 if ((MessageFromList(a) >= 0)
211 || ((mode == MSGS_OLD) && (MessageFromList(a) <= vbuf.v_lastseen))
212 || ((mode == MSGS_NEW) && (MessageFromList(a) > vbuf.v_lastseen))
213 || ((mode == MSGS_NEW) && (MessageFromList(a) >= vbuf.v_lastseen)
214 && (CC->usersupp.flags & US_LASTOLD))
215 || ((mode == MSGS_LAST) && (a >= (CC->num_msgs - ref)))
216 || ((mode == MSGS_FIRST) && (a < ref))
217 || ((mode == MSGS_GT) && (MessageFromList(a) > ref))
220 CallBack(MessageFromList(a));
228 * cmd_msgs() - get list of message #'s in this room
229 * implements the MSGS server command using CtdlForEachMessage()
231 void cmd_msgs(char *cmdbuf)
237 extract(which, cmdbuf, 0);
238 cm_ref = extract_int(cmdbuf, 1);
242 if (!strncasecmp(which, "OLD", 3))
244 else if (!strncasecmp(which, "NEW", 3))
246 else if (!strncasecmp(which, "FIRST", 5))
248 else if (!strncasecmp(which, "LAST", 4))
250 else if (!strncasecmp(which, "GT", 2))
253 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
254 cprintf("%d not logged in\n", ERROR + NOT_LOGGED_IN);
258 cprintf("%d Message list...\n", LISTING_FOLLOWS);
259 CtdlForEachMessage(mode, cm_ref, simple_listing);
267 * help_subst() - support routine for help file viewer
269 void help_subst(char *strbuf, char *source, char *dest)
274 while (p = pattern2(strbuf, source), (p >= 0)) {
275 strcpy(workbuf, &strbuf[p + strlen(source)]);
276 strcpy(&strbuf[p], dest);
277 strcat(strbuf, workbuf);
282 void do_help_subst(char *buffer)
286 help_subst(buffer, "^nodename", config.c_nodename);
287 help_subst(buffer, "^humannode", config.c_humannode);
288 help_subst(buffer, "^fqdn", config.c_fqdn);
289 help_subst(buffer, "^username", CC->usersupp.fullname);
290 sprintf(buf2, "%ld", CC->usersupp.usernum);
291 help_subst(buffer, "^usernum", buf2);
292 help_subst(buffer, "^sysadm", config.c_sysadm);
293 help_subst(buffer, "^variantname", CITADEL);
294 sprintf(buf2, "%d", config.c_maxsessions);
295 help_subst(buffer, "^maxsessions", buf2);
301 * memfmout() - Citadel text formatter and paginator.
302 * Although the original purpose of this routine was to format
303 * text to the reader's screen width, all we're really using it
304 * for here is to format text out to 80 columns before sending it
305 * to the client. The client software may reformat it again.
307 void memfmout(int width, char *mptr, char subst)
308 /* screen width to use */
309 /* where are we going to get our text from? */
310 /* nonzero if we should use hypertext mode */
322 c = 1; /* c is the current pos */
325 while (ch = *mptr, ((ch != 0) && (strlen(buffer) < 126))) {
327 buffer[strlen(buffer) + 1] = 0;
328 buffer[strlen(buffer)] = ch;
331 if (buffer[0] == '^')
332 do_help_subst(buffer);
334 buffer[strlen(buffer) + 1] = 0;
336 strcpy(buffer, &buffer[1]);
345 if (((ch == 13) || (ch == 10)) && (old != 13) && (old != 10))
347 if (((old == 13) || (old == 10)) && (isspace(real))) {
355 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
356 cprintf("\n%s", aaa);
365 if ((strlen(aaa) + c) > (width - 5)) {
375 if ((ch == 13) || (ch == 10)) {
376 cprintf("%s\n", aaa);
383 FMTEND: cprintf("%s\n", aaa);
389 * Callback function for mime parser that simply lists the part
391 void list_this_part(char *name, char *filename, char *partnum, char *disp,
392 void *content, char *cbtype, size_t length)
395 cprintf("part=%s|%s|%s|%s|%s|%d\n",
396 name, filename, partnum, disp, cbtype, length);
401 * Callback function for mime parser that wants to display text
403 void fixed_output(char *name, char *filename, char *partnum, char *disp,
404 void *content, char *cbtype, size_t length)
407 if (!strcasecmp(cbtype, "text/plain")) {
408 client_write(content, length);
410 cprintf("Part %s: %s (%s) (%d bytes)\n",
411 partnum, filename, cbtype, length);
417 * Callback function for mime parser that opens a section for downloading
419 void mime_download(char *name, char *filename, char *partnum, char *disp,
420 void *content, char *cbtype, size_t length)
423 /* Silently go away if there's already a download open... */
424 if (CC->download_fp != NULL)
427 /* ...or if this is not the desired section */
428 if (strcasecmp(desired_section, partnum))
431 CC->download_fp = tmpfile();
432 if (CC->download_fp == NULL)
435 fwrite(content, length, 1, CC->download_fp);
436 fflush(CC->download_fp);
437 rewind(CC->download_fp);
439 OpenCmdResult(filename, cbtype);
445 * Get a message off disk. (return value is the message's timestamp)
448 time_t output_message(char *msgid, int mode, int headers_only)
453 CIT_UBYTE format_type, anon_flag;
458 struct cdbdata *dmsgtext;
461 /* buffers needed for RFC822 translation */
470 msg_num = atol(msgid);
473 if ((!(CC->logged_in)) && (!(CC->internal_pgm)) && (mode != MT_DATE)) {
474 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
477 /* We used to need to check in the current room's message list
478 * to determine where the message's disk position. We no longer need
479 * to do this, but we do it anyway as a security measure, in order to
480 * prevent rogue clients from reading messages not in the current room.
484 if (CC->num_msgs > 0) {
485 for (a = 0; a < CC->num_msgs; ++a) {
486 if (MessageFromList(a) == msg_num) {
493 cprintf("%d Message %ld is not in this room.\n",
497 dmsgtext = cdb_fetch(CDB_MSGMAIN, &msg_num, sizeof(long));
499 if (dmsgtext == NULL) {
501 cprintf("%d Can't find message %ld\n",
502 ERROR + INTERNAL_ERROR);
505 msg_len = (long) dmsgtext->len;
506 mptr = dmsgtext->ptr;
508 /* this loop spews out the whole message if we're doing raw format */
509 if (mode == MT_RAW) {
510 cprintf("%d %ld\n", BINARY_FOLLOWS, msg_len);
511 client_write(dmsgtext->ptr, (int) msg_len);
515 /* Otherwise, we'll start parsing it field by field... */
518 cprintf("%d Illegal message format on disk\n",
519 ERROR + INTERNAL_ERROR);
524 format_type = *mptr++;
526 /* Are we downloading a MIME component? */
527 if (mode == MT_DOWNLOAD) {
528 if (format_type != 4) {
529 cprintf("%d This is not a MIME message.\n",
531 } else if (CC->download_fp != NULL) {
532 cprintf("%d You already have a download open.\n",
535 /* Skip to the message body */
536 while (ch = *mptr++, (ch != 'M' && ch != 0)) {
539 buf[strlen(buf) + 1] = 0;
541 buf[strlen(buf)] = rch;
545 mime_parser(mptr, NULL, *mime_download);
546 /* If there's no file open by this time, the requested
547 * section wasn't found, so print an error
549 if (CC->download_fp == NULL) {
550 cprintf("%d Section %s not found.\n",
551 ERROR + FILE_NOT_FOUND,
558 /* Are we just looking for the message date? */
560 while (ch = *mptr++, (ch != 'M' && ch != 0)) {
563 buf[strlen(buf) + 1] = 0;
565 buf[strlen(buf)] = rch;
574 /* now for the user-mode message reading loops */
575 cprintf("%d Message %ld:\n", LISTING_FOLLOWS, msg_num);
577 if (mode == MT_CITADEL)
578 cprintf("type=%d\n", format_type);
580 if ((anon_flag == MES_ANON) && (mode == MT_CITADEL)) {
581 cprintf("nhdr=yes\n");
583 /* begin header processing loop for Citadel message format */
585 if ((mode == MT_CITADEL) || (mode == MT_MIME))
586 while (ch = *mptr++, (ch != 'M' && ch != 0)) {
589 buf[strlen(buf) + 1] = 0;
591 buf[strlen(buf)] = rch;
595 PerformUserHooks(buf, (-1L), EVT_OUTPUTMSG);
596 if (anon_flag == MES_ANON)
597 cprintf("from=****");
598 else if (anon_flag == MES_AN2)
599 cprintf("from=anonymous");
601 cprintf("from=%s", buf);
602 if ((is_room_aide()) && ((anon_flag == MES_ANON)
603 || (anon_flag == MES_AN2)))
604 cprintf(" [%s]", buf);
606 } else if (ch == 'P')
607 cprintf("path=%s\n", buf);
609 cprintf("subj=%s\n", buf);
611 cprintf("msgn=%s\n", buf);
613 cprintf("hnod=%s\n", buf);
615 cprintf("room=%s\n", buf);
617 cprintf("node=%s\n", buf);
619 cprintf("rcpt=%s\n", buf);
621 cprintf("time=%s\n", buf);
622 /* else cprintf("fld%c=%s\n",ch,buf); */
624 /* begin header processing loop for RFC822 transfer format */
628 strcpy(snode, NODENAME);
629 strcpy(lnode, HUMANNODE);
630 if (mode == MT_RFC822)
631 while (ch = *mptr++, (ch != 'M' && ch != 0)) {
634 buf[strlen(buf) + 1] = 0;
636 buf[strlen(buf)] = rch;
641 else if (ch == 'P') {
642 cprintf("Path: %s\n", buf);
643 for (a = 0; a < strlen(buf); ++a) {
645 strcpy(buf, &buf[a + 1]);
650 } else if (ch == 'U')
651 cprintf("Subject: %s\n", buf);
657 cprintf("X-Citadel-Room: %s\n", buf);
661 cprintf("To: %s\n", buf);
662 else if (ch == 'T') {
664 cprintf("Date: %s", asctime(localtime(&xtime)));
667 if (mode == MT_RFC822) {
668 if (!strcasecmp(snode, NODENAME)) {
671 cprintf("Message-ID: <%s@%s>\n", mid, snode);
672 PerformUserHooks(luser, (-1L), EVT_OUTPUTMSG);
673 cprintf("From: %s@%s (%s)\n",
674 suser, snode, luser);
675 cprintf("Organization: %s\n", lnode);
677 /* end header processing loop ... at this point, we're in the text */
680 cprintf("text\n*** ?Message truncated\n000\n");
684 /* do some sort of MIME output */
685 if (format_type == 4) {
686 if ((mode == MT_CITADEL) || (mode == MT_MIME)) {
687 mime_parser(mptr, NULL, *list_this_part);
689 if (mode == MT_MIME) { /* If MT_MIME then it's parts only */
696 /* give 'em a length */
698 while (ch = *mptr++, ch > 0) {
701 cprintf("mlen=%ld\n", msg_len);
706 /* signify start of msg text */
707 if (mode == MT_CITADEL)
709 if ((mode == MT_RFC822) && (format_type != 4))
712 /* If the format type on disk is 1 (fixed-format), then we want
713 * everything to be output completely literally ... regardless of
714 * what message transfer format is in use.
716 if (format_type == 1) {
718 while (ch = *mptr++, ch > 0) {
721 if ((ch == 10) || (strlen(buf) > 250)) {
722 cprintf("%s\n", buf);
725 buf[strlen(buf) + 1] = 0;
726 buf[strlen(buf)] = ch;
730 cprintf("%s\n", buf);
732 /* If the message on disk is format 0 (Citadel vari-format), we
733 * output using the formatter at 80 columns. This is the final output
734 * form if the transfer format is RFC822, but if the transfer format
735 * is Citadel proprietary, it'll still work, because the indentation
736 * for new paragraphs is correct and the client will reformat the
737 * message to the reader's screen width.
739 if (format_type == 0) {
740 memfmout(80, mptr, 0);
742 /* If the message on disk is format 4 (MIME), we've gotta hand it
743 * off to the MIME parser. The client has already been told that
744 * this message is format 1 (fixed format), so the callback function
745 * we use will display those parts as-is.
747 if (format_type == 4) {
748 mime_parser(mptr, NULL, *fixed_output);
758 * display a message (mode 0 - Citadel proprietary)
760 void cmd_msg0(char *cmdbuf)
763 int headers_only = 0;
765 extract(msgid, cmdbuf, 0);
766 headers_only = extract_int(cmdbuf, 1);
768 output_message(msgid, MT_CITADEL, headers_only);
774 * display a message (mode 2 - RFC822)
776 void cmd_msg2(char *cmdbuf)
779 int headers_only = 0;
781 extract(msgid, cmdbuf, 0);
782 headers_only = extract_int(cmdbuf, 1);
784 output_message(msgid, MT_RFC822, headers_only);
788 * display a message (mode 3 - IGnet raw format - internal programs only)
790 void cmd_msg3(char *cmdbuf)
793 int headers_only = 0;
795 if (CC->internal_pgm == 0) {
796 cprintf("%d This command is for internal programs only.\n",
800 extract(msgid, cmdbuf, 0);
801 headers_only = extract_int(cmdbuf, 1);
803 output_message(msgid, MT_RAW, headers_only);
807 * display a message (mode 4 - MIME) (FIX ... still evolving, not complete)
809 void cmd_msg4(char *cmdbuf)
813 extract(msgid, cmdbuf, 0);
815 output_message(msgid, MT_MIME, 0);
821 * Open a component of a MIME message as a download file
823 void cmd_opna(char *cmdbuf)
827 CtdlAllocUserData(SYM_DESIRED_SECTION, 64);
829 extract(msgid, cmdbuf, 0);
830 extract(desired_section, cmdbuf, 1);
832 output_message(msgid, MT_DOWNLOAD, 0);
838 * Message base operation to send a message to the master file
839 * (returns new message number)
841 long send_message(char *message_in_memory, /* pointer to buffer */
842 size_t message_length, /* length of buffer */
844 { /* 1 to generate an I field */
847 char *actual_message;
848 size_t actual_length;
852 /* Get a new message number */
853 newmsgid = get_new_message_number();
856 sprintf(msgidbuf, "I%ld", newmsgid);
857 actual_length = message_length + strlen(msgidbuf) + 1;
858 actual_message = mallok(actual_length);
859 memcpy(actual_message, message_in_memory, 3);
860 memcpy(&actual_message[3], msgidbuf, (strlen(msgidbuf) + 1));
861 memcpy(&actual_message[strlen(msgidbuf) + 4],
862 &message_in_memory[3], message_length - 3);
864 actual_message = message_in_memory;
865 actual_length = message_length;
868 /* Write our little bundle of joy into the message base */
869 begin_critical_section(S_MSGMAIN);
870 if (cdb_store(CDB_MSGMAIN, &newmsgid, sizeof(long),
871 actual_message, actual_length) < 0) {
872 lprintf(2, "Can't store message\n");
877 end_critical_section(S_MSGMAIN);
880 phree(actual_message);
882 /* Finally, return the pointers */
889 * this is a simple file copy routine.
891 void copy_file(char *from, char *to)
896 ffp = fopen(from, "r");
899 tfp = fopen(to, "w");
904 while (a = getc(ffp), a >= 0) {
915 * message base operation to save a message and install its pointers
917 void save_message(char *mtmp, /* file containing proper message */
918 char *rec, /* Recipient (if mail) */
919 char *force, /* if non-zero length, force a room */
920 int mailtype, /* local or remote type, see citadel.h */
922 { /* set to 1 to generate an 'I' field */
924 char hold_rm[ROOMNAMELEN];
925 char actual_rm[ROOMNAMELEN];
926 char force_room[ROOMNAMELEN];
927 char content_type[256]; /* We have to learn this */
931 char *message_in_memory;
936 struct usersupp userbuf;
938 static int seqnum = 0;
939 int successful_local_recipients = 0;
940 struct quickroom qtemp;
941 struct SuppMsgInfo smi;
943 lprintf(9, "save_message(%s,%s,%s,%d,%d)\n",
944 mtmp, rec, force, mailtype, generate_id);
946 strcpy(force_room, force);
948 /* Strip non-printable characters out of the recipient name */
949 strcpy(recipient, rec);
950 for (a = 0; a < strlen(recipient); ++a)
951 if (!isprint(recipient[a]))
952 strcpy(&recipient[a], &recipient[a + 1]);
954 /* Measure the message */
955 stat(mtmp, &statbuf);
956 templen = statbuf.st_size;
958 /* Now read it into memory */
959 message_in_memory = (char *) mallok(templen);
960 if (message_in_memory == NULL) {
961 lprintf(2, "Can't allocate memory to save message!\n");
964 fp = fopen(mtmp, "rb");
965 fread(message_in_memory, templen, 1, fp);
968 /* Learn about what's inside, because it's what's inside that counts */
969 mptr = message_in_memory;
970 ++mptr; /* advance past 0xFF header */
971 ++mptr; /* advance past anon flag */
975 strcpy(content_type, "text/x-citadel-variformat");
978 strcpy(content_type, "text/plain");
981 strcpy(content_type, "text/plain");
982 /* advance past header fields */
983 while (ch = *mptr++, (ch != 'M' && ch != 0)) {
990 if (!strncasecmp(mptr, "Content-type: ", 14)) {
991 lprintf(9, "%s\n", content_type);
992 strcpy(content_type, &content_type[14]);
993 for (a=0; a<strlen(content_type); ++a)
994 if ( (content_type[a]==';')
995 || (content_type[a]==' ')
996 || (content_type[a]==13)
997 || (content_type[a]==10) )
1004 lprintf(9, "Content type is <%s>\n", content_type);
1006 /* Save it to disk */
1007 newmsgid = send_message(message_in_memory, templen, generate_id);
1008 phree(message_in_memory);
1012 strcpy(actual_rm, CC->quickroom.QRname);
1013 strcpy(hold_rm, "");
1015 /* If this is being done by the networker delivering a private
1016 * message, we want to BYPASS saving the sender's copy (because there
1017 * is no local sender; it would otherwise go to the Trashcan).
1019 if (!CC->internal_pgm) {
1020 /* If the user is a twit, move to the twit room for posting */
1022 if (CC->usersupp.axlevel == 2) {
1023 strcpy(hold_rm, actual_rm);
1024 strcpy(actual_rm, config.c_twitroom);
1026 /* ...or if this message is destined for Aide> then go there. */
1027 lprintf(9, "actual room forcing loop\n");
1028 if (strlen(force_room) > 0) {
1029 strcpy(hold_rm, actual_rm);
1030 strcpy(actual_rm, force_room);
1032 /* This call to usergoto() changes rooms if necessary. It also
1033 * causes the latest message list to be read into memory.
1035 usergoto(actual_rm, 0);
1037 /* read in the quickroom record, obtaining a lock... */
1038 lgetroom(&CC->quickroom, actual_rm);
1040 /* Fix an obscure bug */
1041 if (!strcasecmp(CC->quickroom.QRname, AIDEROOM)) {
1042 CC->quickroom.QRflags =
1043 CC->quickroom.QRflags & ~QR_MAILBOX;
1046 /* Add the message pointer to the room */
1047 CC->quickroom.QRhighest =
1048 AddMessageToRoom(&CC->quickroom, newmsgid);
1050 /* update quickroom */
1051 lputroom(&CC->quickroom);
1052 ++successful_local_recipients;
1055 /* Network mail - send a copy to the network program. */
1056 if ((strlen(recipient) > 0) && (mailtype != MES_LOCAL)) {
1057 sprintf(aaa, "./network/spoolin/netmail.%04lx.%04x.%04x",
1058 (long) getpid(), CC->cs_pid, ++seqnum);
1059 copy_file(mtmp, aaa);
1060 system("exec nohup ./netproc -i >/dev/null 2>&1 &");
1062 /* Bump this user's messages posted counter. */
1063 lgetuser(&CC->usersupp, CC->curr_user);
1064 CC->usersupp.posted = CC->usersupp.posted + 1;
1065 lputuser(&CC->usersupp);
1067 /* If this is private, local mail, make a copy in the
1068 * recipient's mailbox and bump the reference count.
1070 if ((strlen(recipient) > 0) && (mailtype == MES_LOCAL)) {
1071 if (getuser(&userbuf, recipient) == 0) {
1072 MailboxName(actual_rm, &userbuf, MAILROOM);
1073 lprintf(9, "Targeting mailbox: <%s>\n", actual_rm);
1074 if (lgetroom(&qtemp, actual_rm) == 0) {
1076 AddMessageToRoom(&qtemp, newmsgid);
1078 ++successful_local_recipients;
1082 /* If we've posted in a room other than the current room, then we
1083 * have to now go back to the current room...
1085 if (strlen(hold_rm) > 0) {
1086 usergoto(hold_rm, 0);
1088 unlink(mtmp); /* delete the temporary file */
1090 /* Write a supplemental message info record. This doesn't have to
1091 * be a critical section because nobody else knows about this message
1094 memset(&smi, 0, sizeof(struct SuppMsgInfo));
1095 smi.smi_msgnum = newmsgid;
1096 smi.smi_refcount = successful_local_recipients;
1097 safestrncpy(smi.smi_content_type, content_type, 64);
1098 PutSuppMsgInfo(&smi);
1103 * Generate an administrative message and post it in the Aide> room.
1105 void aide_message(char *text)
1109 fp = fopen(CC->temp, "wb");
1110 fprintf(fp, "%c%c%c", 255, MES_NORMAL, 0);
1111 fprintf(fp, "Psysop%c", 0);
1112 fprintf(fp, "T%ld%c", (long) time(NULL), 0);
1113 fprintf(fp, "ACitadel%c", 0);
1114 fprintf(fp, "OAide%c", 0);
1115 fprintf(fp, "N%s%c", NODENAME, 0);
1116 fprintf(fp, "M%s\n%c", text, 0);
1118 save_message(CC->temp, "", AIDEROOM, MES_LOCAL, 1);
1119 syslog(LOG_NOTICE, text);
1125 * Build a binary message to be saved on disk.
1128 char *filename, /* temporary file name */
1129 struct usersupp *author, /* author's usersupp structure */
1130 char *recipient, /* NULL if it's not mail */
1131 char *room, /* room where it's going */
1132 int type, /* see MES_ types in header file */
1133 int net_type, /* see MES_ types in header file */
1134 int format_type, /* local or remote (see citadel.h) */
1136 { /* who we're masquerading as */
1144 /* Don't confuse the poor folks if it's not routed mail. */
1145 strcpy(dest_node, "");
1148 /* If net_type is MES_BINARY, split out the destination node. */
1149 if (net_type == MES_BINARY) {
1150 strcpy(dest_node, NODENAME);
1151 for (a = 0; a < strlen(recipient); ++a) {
1152 if (recipient[a] == '@') {
1154 strcpy(dest_node, &recipient[a + 1]);
1158 /* if net_type is MES_INTERNET, set the dest node to 'internet' */
1159 if (net_type == MES_INTERNET) {
1160 strcpy(dest_node, "internet");
1162 while (isspace(recipient[strlen(recipient) - 1]))
1163 recipient[strlen(recipient) - 1] = 0;
1166 fp = fopen(filename, "w");
1168 putc(type, fp); /* Normal or anonymous, see MES_ flags */
1169 putc(format_type, fp); /* Formatted or unformatted */
1170 fprintf(fp, "Pcit%ld%c", author->usernum, 0); /* path */
1171 fprintf(fp, "T%ld%c", (long) now, 0); /* date/time */
1173 fprintf(fp, "A%s%c", fake_name, 0);
1175 fprintf(fp, "A%s%c", author->fullname, 0); /* author */
1177 if (CC->quickroom.QRflags & QR_MAILBOX) { /* room */
1178 fprintf(fp, "O%s%c", &CC->quickroom.QRname[11], 0);
1180 fprintf(fp, "O%s%c", CC->quickroom.QRname, 0);
1183 fprintf(fp, "N%s%c", NODENAME, 0); /* nodename */
1184 fprintf(fp, "H%s%c", HUMANNODE, 0); /* human nodename */
1186 if (recipient[0] != 0)
1187 fprintf(fp, "R%s%c", recipient, 0);
1188 if (dest_node[0] != 0)
1189 fprintf(fp, "D%s%c", dest_node, 0);
1193 while (client_gets(buf), strcmp(buf, "000")) {
1194 fprintf(fp, "%s\n", buf);
1205 * message entry - mode 0 (normal) <bc>
1207 void cmd_ent0(char *entargs)
1210 char recipient[256];
1212 int format_type = 0;
1213 char newusername[256]; /* <bc> */
1218 struct usersupp tempUS;
1221 post = extract_int(entargs, 0);
1222 extract(recipient, entargs, 1);
1223 anon_flag = extract_int(entargs, 2);
1224 format_type = extract_int(entargs, 3);
1226 /* first check to make sure the request is valid. */
1228 if (!(CC->logged_in)) {
1229 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
1232 if ((CC->usersupp.axlevel < 2) && ((CC->quickroom.QRflags & QR_MAILBOX) == 0)) {
1233 cprintf("%d Need to be validated to enter ",
1234 ERROR + HIGHER_ACCESS_REQUIRED);
1235 cprintf("(except in %s> to sysop)\n", MAILROOM);
1238 if ((CC->usersupp.axlevel < 4) && (CC->quickroom.QRflags & QR_NETWORK)) {
1239 cprintf("%d Need net privileges to enter here.\n",
1240 ERROR + HIGHER_ACCESS_REQUIRED);
1243 if ((CC->usersupp.axlevel < 6) && (CC->quickroom.QRflags & QR_READONLY)) {
1244 cprintf("%d Sorry, this is a read-only room.\n",
1245 ERROR + HIGHER_ACCESS_REQUIRED);
1251 if (post == 2) { /* <bc> */
1252 if (CC->usersupp.axlevel < 6) {
1253 cprintf("%d You don't have permission to do an aide post.\n",
1254 ERROR + HIGHER_ACCESS_REQUIRED);
1257 extract(newusername, entargs, 4);
1258 memset(CC->fake_postname, 0, 32);
1259 strcpy(CC->fake_postname, newusername);
1260 cprintf("%d Ok\n", OK);
1263 CC->cs_flags |= CS_POSTING;
1266 if (CC->quickroom.QRflags & QR_MAILBOX) {
1267 if (CC->usersupp.axlevel >= 2) {
1268 strcpy(buf, recipient);
1270 strcpy(buf, "sysop");
1271 lprintf(9, "calling alias()\n");
1272 e = alias(buf); /* alias and mail type */
1273 lprintf(9, "alias() returned %d\n", e);
1274 if ((buf[0] == 0) || (e == MES_ERROR)) {
1275 cprintf("%d Unknown address - cannot send message.\n",
1276 ERROR + NO_SUCH_USER);
1279 if ((e != MES_LOCAL) && (CC->usersupp.axlevel < 4)) {
1280 cprintf("%d Net privileges required for network mail.\n",
1281 ERROR + HIGHER_ACCESS_REQUIRED);
1284 if ((RESTRICT_INTERNET == 1) && (e == MES_INTERNET)
1285 && ((CC->usersupp.flags & US_INTERNET) == 0)
1286 && (!CC->internal_pgm)) {
1287 cprintf("%d You don't have access to Internet mail.\n",
1288 ERROR + HIGHER_ACCESS_REQUIRED);
1291 if (!strcasecmp(buf, "sysop")) {
1296 goto SKFALL; /* don't search local file */
1297 if (!strcasecmp(buf, CC->usersupp.fullname)) {
1298 cprintf("%d Can't send mail to yourself!\n",
1299 ERROR + NO_SUCH_USER);
1302 /* Check to make sure the user exists; also get the correct
1303 * upper/lower casing of the name.
1305 a = getuser(&tempUS, buf);
1307 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1310 strcpy(buf, tempUS.fullname);
1312 SKFALL:b = MES_NORMAL;
1313 if (CC->quickroom.QRflags & QR_ANONONLY)
1315 if (CC->quickroom.QRflags & QR_ANONOPT) {
1319 if ((CC->quickroom.QRflags & QR_MAILBOX) == 0)
1322 /* If we're only checking the validity of the request, return
1323 * success without creating the message.
1326 cprintf("%d %s\n", OK, buf);
1329 cprintf("%d send message\n", SEND_LISTING);
1330 if (CC->fake_postname[0])
1331 make_message(CC->temp, &CC->usersupp, buf, CC->quickroom.QRname, b, e, format_type, CC->fake_postname);
1332 else if (CC->fake_username[0])
1333 make_message(CC->temp, &CC->usersupp, buf, CC->quickroom.QRname, b, e, format_type, CC->fake_username);
1335 make_message(CC->temp, &CC->usersupp, buf, CC->quickroom.QRname, b, e, format_type, "");
1336 save_message(CC->temp, buf, (mtsflag ? AIDEROOM : ""), e, 1);
1337 CC->fake_postname[0] = '\0';
1344 * message entry - mode 3 (raw)
1346 void cmd_ent3(char *entargs)
1352 struct usersupp tempUS;
1357 if (CC->internal_pgm == 0) {
1358 cprintf("%d This command is for internal programs only.\n",
1362 /* See if there's a recipient, but make sure it's a real one */
1363 extract(recp, entargs, 1);
1364 for (a = 0; a < strlen(recp); ++a)
1365 if (!isprint(recp[a]))
1366 strcpy(&recp[a], &recp[a + 1]);
1367 while (isspace(recp[0]))
1368 strcpy(recp, &recp[1]);
1369 while (isspace(recp[strlen(recp) - 1]))
1370 recp[strlen(recp) - 1] = 0;
1372 /* If we're in Mail, check the recipient */
1373 if (strlen(recp) > 0) {
1374 e = alias(recp); /* alias and mail type */
1375 if ((recp[0] == 0) || (e == MES_ERROR)) {
1376 cprintf("%d Unknown address - cannot send message.\n",
1377 ERROR + NO_SUCH_USER);
1380 if (e == MES_LOCAL) {
1381 a = getuser(&tempUS, recp);
1383 cprintf("%d No such user.\n",
1384 ERROR + NO_SUCH_USER);
1389 /* At this point, message has been approved. */
1390 if (extract_int(entargs, 0) == 0) {
1391 cprintf("%d OK to send\n", OK);
1394 /* open a temp file to hold the message */
1395 fp = fopen(CC->temp, "wb");
1397 cprintf("%d Cannot open %s: %s\n",
1398 ERROR + INTERNAL_ERROR,
1399 CC->temp, strerror(errno));
1402 msglen = extract_long(entargs, 2);
1403 cprintf("%d %ld\n", SEND_BINARY, msglen);
1404 while (msglen > 0L) {
1405 bloklen = ((msglen >= 255L) ? 255 : msglen);
1406 client_read(buf, (int) bloklen);
1407 fwrite(buf, (int) bloklen, 1, fp);
1408 msglen = msglen - bloklen;
1412 save_message(CC->temp, recp, "", e, 0);
1417 * API function to delete messages which match a set of criteria
1418 * (returns the actual number of messages deleted)
1419 * FIX ... still need to implement delete by content type
1421 int CtdlDeleteMessages( char *room_name, /* which room */
1422 long dmsgnum, /* or "0" for any */
1423 char *content_type /* or NULL for any */
1426 struct quickroom qrbuf;
1427 struct cdbdata *cdbfr;
1428 long *msglist = NULL;
1431 int num_deleted = 0;
1433 struct SuppMsgInfo smi;
1435 /* get room record, obtaining a lock... */
1436 if (lgetroom(&qrbuf, room_name) != 0) {
1437 lprintf(7, "CtdlDeleteMessages(): Room <%s> not found\n",
1439 return(0); /* room not found */
1442 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
1444 if (cdbfr != NULL) {
1445 msglist = mallok(cdbfr->len);
1446 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1447 num_msgs = cdbfr->len / sizeof(long);
1451 if (num_msgs > 0) for (i=0; i<num_msgs; ++i) {
1454 /* Set/clear a bit for each criterion */
1456 if ( (dmsgnum == 0L) || (msglist[i]==dmsgnum) ) {
1457 delete_this |= 0x01;
1460 if (content_type == NULL) {
1461 delete_this |= 0x02;
1463 GetSuppMsgInfo(&smi, msglist[i]);
1464 if (!strcasecmp(smi.smi_content_type, content_type)) {
1465 delete_this |= 0x02;
1469 /* Delete message only if all bits are set */
1470 if (delete_this == 0x03) {
1471 AdjRefCount(msglist[i], -1);
1477 num_msgs = sort_msglist(msglist, num_msgs);
1478 cdb_store(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long),
1479 msglist, (num_msgs * sizeof(long)) );
1481 qrbuf.QRhighest = msglist[num_msgs - 1];
1483 lprintf(9, "%d message(s) deleted.\n", num_deleted);
1484 return(num_deleted);
1490 * Delete message from current room
1492 void cmd_dele(char *delstr)
1497 getuser(&CC->usersupp, CC->curr_user);
1498 if ((CC->usersupp.axlevel < 6)
1499 && (CC->usersupp.usernum != CC->quickroom.QRroomaide)
1500 && ((CC->quickroom.QRflags & QR_MAILBOX) == 0)) {
1501 cprintf("%d Higher access required.\n",
1502 ERROR + HIGHER_ACCESS_REQUIRED);
1505 delnum = extract_long(delstr, 0);
1507 num_deleted = CtdlDeleteMessages(CC->quickroom.QRname, delnum, NULL);
1510 cprintf("%d %d message%s deleted.\n", OK,
1511 num_deleted, ((num_deleted!=1) ? "s" : "") );
1513 cprintf("%d Message %ld not found.\n", ERROR, delnum);
1519 * move a message to another room
1521 void cmd_move(char *args)
1526 struct quickroom qtemp;
1529 num = extract_long(args, 0);
1530 extract(targ, args, 1);
1532 getuser(&CC->usersupp, CC->curr_user);
1533 if ((CC->usersupp.axlevel < 6)
1534 && (CC->usersupp.usernum != CC->quickroom.QRroomaide)) {
1535 cprintf("%d Higher access required.\n",
1536 ERROR + HIGHER_ACCESS_REQUIRED);
1539 if (getroom(&qtemp, targ) != 0) {
1540 cprintf("%d '%s' does not exist.\n", ERROR, targ);
1543 /* yank the message out of the current room... */
1544 lgetroom(&CC->quickroom, CC->quickroom.QRname);
1545 get_msglist(&CC->quickroom);
1548 for (a = 0; a < (CC->num_msgs); ++a) {
1549 if (MessageFromList(a) == num) {
1551 SetMessageInList(a, 0L);
1555 CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
1556 put_msglist(&CC->quickroom);
1557 CC->quickroom.QRhighest = MessageFromList((CC->num_msgs) - 1);
1559 lputroom(&CC->quickroom);
1561 cprintf("%d msg %ld does not exist.\n", ERROR, num);
1564 /* put the message into the target room */
1565 lgetroom(&qtemp, targ);
1566 qtemp.QRhighest = AddMessageToRoom(&qtemp, num);
1569 cprintf("%d Message moved.\n", OK);
1575 * GetSuppMsgInfo() - Get the supplementary record for a message
1577 void GetSuppMsgInfo(struct SuppMsgInfo *smibuf, long msgnum)
1580 struct cdbdata *cdbsmi;
1583 memset(smibuf, 0, sizeof(struct SuppMsgInfo));
1584 smibuf->smi_msgnum = msgnum;
1585 smibuf->smi_refcount = 1; /* Default reference count is 1 */
1587 /* Use the negative of the message number for its supp record index */
1588 TheIndex = (0L - msgnum);
1590 cdbsmi = cdb_fetch(CDB_MSGMAIN, &TheIndex, sizeof(long));
1591 if (cdbsmi == NULL) {
1592 return; /* record not found; go with defaults */
1594 memcpy(smibuf, cdbsmi->ptr,
1595 ((cdbsmi->len > sizeof(struct SuppMsgInfo)) ?
1596 sizeof(struct SuppMsgInfo) : cdbsmi->len));
1603 * PutSuppMsgInfo() - (re)write supplementary record for a message
1605 void PutSuppMsgInfo(struct SuppMsgInfo *smibuf)
1609 /* Use the negative of the message number for its supp record index */
1610 TheIndex = (0L - smibuf->smi_msgnum);
1612 lprintf(9, "PuttSuppMsgInfo(%ld) - ref count is %d\n",
1613 smibuf->smi_msgnum, smibuf->smi_refcount);
1615 cdb_store(CDB_MSGMAIN,
1616 &TheIndex, sizeof(long),
1617 smibuf, sizeof(struct SuppMsgInfo));
1624 * AdjRefCount - change the reference count for a message;
1625 * delete the message if it reaches zero
1627 void AdjRefCount(long msgnum, int incr)
1630 struct SuppMsgInfo smi;
1633 /* This is a *tight* critical section; please keep it that way, as
1634 * it may get called while nested in other critical sections.
1635 * Complicating this any further will surely cause deadlock!
1637 begin_critical_section(S_SUPPMSGMAIN);
1638 GetSuppMsgInfo(&smi, msgnum);
1639 smi.smi_refcount += incr;
1640 PutSuppMsgInfo(&smi);
1641 end_critical_section(S_SUPPMSGMAIN);
1643 lprintf(9, "Ref count for message <%ld> after write is <%d>\n",
1644 msgnum, smi.smi_refcount);
1646 /* If the reference count is now zero, delete the message
1647 * (and its supplementary record as well).
1649 if (smi.smi_refcount == 0) {
1650 lprintf(9, "Deleting message <%ld>\n", msgnum);
1652 cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));
1653 delnum = (0L - msgnum);
1654 cdb_delete(CDB_MSGMAIN, &delnum, sizeof(long));