4 * Citadel/UX message support routines
5 * see copyright.txt for copyright information
11 #include <sys/types.h>
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
27 # include <sys/time.h>
43 #define MAXWORDBUF SIZ
48 char text[MAXWORDBUF];
51 void sttybbs(int cmd);
52 int fmout(int width, FILE * fp, char pagin, int height, int starting_lp,
54 int haschar(char *st, int ch);
55 int checkpagin(int lp, int pagin, int height);
56 void getline(char *string, int lim);
57 void formout(char *name);
59 void newprompt(char *prompt, char *str, int len);
60 int file_checksum(char *filename);
61 void do_edit(char *desc, char *read_cmd, char *check_cmd, char *write_cmd);
64 long msg_arr[MAXMSGS];
66 extern char room_name[];
67 extern unsigned room_flags;
68 extern long highest_msg_read;
69 extern struct CtdlServInfo serv_info;
72 extern int screenwidth;
73 extern int screenheight;
74 extern long maxmsgnum;
77 extern char is_room_aide;
78 extern char fullname[];
80 extern unsigned userflags;
81 extern char sigcaught;
82 extern char editor_path[];
83 extern char printcmd[];
84 extern int rc_allow_attachments;
85 extern int rc_display_message_numbers;
86 extern int rc_force_mail_prompts;
88 extern int editor_pid;
90 void ka_sigcatch(int signum)
94 signal(SIGALRM, ka_sigcatch);
101 * server keep-alive version of wait() (needed for external editor)
103 pid_t ka_wait(int *kstatus)
108 signal(SIGALRM, ka_sigcatch);
112 } while (errno == EINTR);
113 signal(SIGALRM, SIG_IGN);
120 * version of system() that uses ka_wait()
122 int ka_system(char *shc)
131 perror("Cannot fork");
133 return ((pid_t) childpid);
137 execlp("/bin/sh", "sh", "-c", shc, NULL);
143 waitpid = ka_wait(&retcode);
144 } while (waitpid != childpid);
154 * add a newline to the buffer...
156 void add_newline(struct cittext *textlist)
161 while (ptr->next != NULL)
164 while (ptr->text[strlen(ptr->text) - 1] == 32)
165 ptr->text[strlen(ptr->text) - 1] = 0;
166 /* strcat(ptr->text,"\n"); */
168 ptr->next = (struct cittext *)
169 malloc(sizeof(struct cittext));
172 strcpy(ptr->text, "");
177 * add a word to the buffer...
179 void add_word(struct cittext *textlist, char *wordbuf)
184 while (ptr->next != NULL)
187 if (3 + strlen(ptr->text) + strlen(wordbuf) > screenwidth) {
188 ptr->next = (struct cittext *)
189 malloc(sizeof(struct cittext));
192 strcpy(ptr->text, "");
195 strcat(ptr->text, wordbuf);
196 strcat(ptr->text, " ");
201 * begin editing of an opened file pointed to by fp
203 void citedit(FILE * fp)
205 int a, prev, finished, b, last_space;
207 struct cittext *textlist = NULL;
209 char wordbuf[MAXWORDBUF];
211 /* first, load the text into the buffer */
213 textlist = (struct cittext *) malloc(sizeof(struct cittext));
214 textlist->next = NULL;
215 strcpy(textlist->text, "");
219 while (a = getc(fp), a >= 0) {
221 if ((a == 32) || (a == 9) || (a == 13) || (a == 10)) {
222 add_word(textlist, wordbuf);
224 if ((prev == 13) || (prev == 10)) {
225 add_word(textlist, "\n");
226 add_newline(textlist);
227 add_word(textlist, "");
230 wordbuf[strlen(wordbuf) + 1] = 0;
231 wordbuf[strlen(wordbuf)] = a;
233 if (strlen(wordbuf) + 3 > screenwidth) {
234 add_word(textlist, wordbuf);
242 prev = (appending ? 13 : (-1));
254 /******* new ***********/
255 if ((a > 32) && (a < 127) && (prev == 13)) {
256 add_word(textlist, "\n");
259 /***********************/
261 if ((a == 32) && (prev == 13)) {
262 add_word(textlist, "\n");
263 add_newline(textlist);
267 if (strlen(wordbuf) > 0) {
268 wordbuf[strlen(wordbuf) - 1] = 0;
273 } else if (a == 13) {
275 if (strlen(wordbuf) == 0)
278 for (b = 0; b < strlen(wordbuf); ++b)
279 if (wordbuf[b] == 32) {
287 add_word(textlist, wordbuf);
292 wordbuf[strlen(wordbuf) + 1] = 0;
293 wordbuf[strlen(wordbuf)] = a;
295 if ((strlen(wordbuf) + 3) > screenwidth) {
297 for (b = 0; b < strlen(wordbuf); ++b)
298 if (wordbuf[b] == 32)
300 if (last_space >= 0) {
301 for (b = 0; b < strlen(wordbuf); ++b)
302 if (wordbuf[b] == 32) {
310 for (b = 0; b < strlen(wordbuf); ++b) {
315 printf("\n%s", wordbuf);
317 add_word(textlist, wordbuf);
323 } while (finished == 0);
326 /* write the buffer back to disk */
328 for (ptr = textlist; ptr != NULL; ptr = ptr->next) {
329 fprintf(fp, "%s", ptr->text);
333 ftruncate(fileno(fp), ftell(fp));
335 /* and deallocate the memory we used */
336 while (textlist != NULL) {
337 ptr = textlist->next;
343 /* Read a message from the server
346 long num, /* message number */
347 char pagin) /* 0 = normal read, 1 = read with pagination, 2 = header */
351 char from[SIZ], node[SIZ], rfca[SIZ];
360 sprintf(buf, "MSG0 %ld|%d", num, (pagin == READ_HEADER ? 1 : 0));
364 printf("*** msg #%ld: %s\n", num, buf);
367 checkpagin(lines_printed, pagin, screenheight);
372 strcpy(m_subject, "");
373 strcpy(reply_to, "nobody ... xxxxx");
380 lines_printed = checkpagin(lines_printed, pagin, screenheight);
387 while (serv_gets(buf), strcmp(buf, "000")) {
392 checkpagin(lines_printed,
393 pagin, screenheight);
400 while (serv_gets(buf), strncasecmp(buf, "text", 4)) {
401 if (!strncasecmp(buf, "nhdr=yes", 8))
403 if (!strncasecmp(buf, "from=", 5)) {
404 strcpy(from, &buf[5]);
408 if (!strncasecmp(buf, "type=", 5))
409 format_type = atoi(&buf[5]);
410 if ((!strncasecmp(buf, "msgn=", 5))
411 && (rc_display_message_numbers)) {
415 printf("#%s", &buf[5]);
419 if (!strncasecmp(buf, "from=", 5)) {
423 printf("%s ", &buf[5]);
425 if (!strncasecmp(buf, "subj=", 5))
426 strcpy(m_subject, &buf[5]);
428 if (!strncasecmp(buf, "rfca=", 5)) {
429 safestrncpy(rfca, &buf[5], sizeof(rfca) - 5);
433 printf("%s", &buf[5]);
437 if ((!strncasecmp(buf, "hnod=", 5))
438 && (strcasecmp(&buf[5], serv_info.serv_humannode))
439 && (strlen(rfca) == 0)) {
443 printf("%s", &buf[5]);
447 if ((!strncasecmp(buf, "room=", 5))
448 && (strcasecmp(&buf[5], room_name))
449 && (strlen(rfca) == 0)) {
452 color(BRIGHT_MAGENTA);
453 printf("%s> ", &buf[5]);
456 if (!strncasecmp(buf, "node=", 5)) {
457 safestrncpy(node, &buf[5], sizeof(buf) - 5);
458 if ((room_flags & QR_NETWORK)
461 (&buf[5], serv_info.serv_nodename)
463 (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
465 if (strlen(rfca) == 0) {
468 color(BRIGHT_YELLOW);
469 printf("%s ", &buf[5]);
474 if (!strncasecmp(buf, "rcpt=", 5)) {
478 printf("%s ", &buf[5]);
480 if (!strncasecmp(buf, "time=", 5)) {
481 fmt_date(now, atol(&buf[5]));
495 if (strlen(rfca) > 0) {
496 strcpy(reply_to, rfca);
498 snprintf(reply_to, sizeof(reply_to), "%s @ %s", from,
505 lines_printed = checkpagin(lines_printed, pagin, screenheight);
507 if (strlen(m_subject) > 0) {
508 printf("Subject: %s\n", m_subject);
511 checkpagin(lines_printed, pagin, screenheight);
514 if (format_type == 0) {
515 fr = fmout(screenwidth, NULL,
516 ((pagin == 1) ? 1 : 0), screenheight, (-1), 1);
518 while (serv_gets(buf), strcmp(buf, "000")) {
519 if (sigcaught == 0) {
521 lines_printed = lines_printed + 1 +
522 (strlen(buf) / screenwidth);
524 checkpagin(lines_printed, pagin,
532 lines_printed = checkpagin(lines_printed, pagin, screenheight);
541 * replace string function for the built-in editor
543 void replace_string(char *filename, long int startpos)
552 int substitutions = 0;
555 printf("Enter text to be replaced:\n: ");
556 getline(srch_str, 128);
557 if (strlen(srch_str) == 0)
560 printf("Enter text to replace it with:\n: ");
561 getline(rplc_str, 128);
563 fp = fopen(filename, "r+");
568 fseek(fp, startpos, 0);
570 while (a = getc(fp), a > 0) {
572 buf[strlen(buf) + 1] = 0;
573 buf[strlen(buf)] = a;
574 if (strlen(buf) >= strlen(srch_str)) {
575 ptr = (&buf[strlen(buf) - strlen(srch_str)]);
576 if (!strncasecmp(ptr, srch_str, strlen(srch_str))) {
577 strcpy(ptr, rplc_str);
581 if (strlen(buf) > 384) {
584 fwrite((char *) buf, 128, 1, fp);
585 strcpy(buf, &buf[128]);
592 fwrite((char *) buf, strlen(buf), 1, fp);
595 truncate(filename, wpos);
596 printf("<R>eplace made %d substitution(s).\n\n", substitutions);
600 * Function to begin composing a new message
602 int make_message(char *filename, /* temporary file name */
603 char *recipient, /* NULL if it's not mail */
604 int anon_type, /* see MES_ types in header file */
615 if (strlen(editor_path) == 0) {
617 ("*** No editor available, using built-in editor\n");
621 fmt_date(datestr, time(NULL));
623 if (room_flags & QR_ANONONLY) {
627 printf(" %s from %s", datestr, fullname);
628 if (strlen(recipient) > 0) {
629 printf(" to %s", recipient);
637 printf("(Press ctrl-d when finished)\n");
641 fp = fopen(filename, "r");
643 fmout(screenwidth, fp, 0, screenheight, 0, 0);
647 fp = fopen(filename, "w");
649 printf("*** Error opening temp file!\n"
651 filename, strerror(errno));
661 fp = fopen(filename, "r+");
663 printf("*** Error opening temp file!\n"
665 filename, strerror(errno));
673 fp = fopen(filename, "a");
675 printf("*** Error opening temp file!\n"
677 filename, strerror(errno));
697 e_ex_code = 1; /* start with a failed exit code */
699 cksum = file_checksum(filename);
700 if (editor_pid == 0) {
701 chmod(filename, 0600);
703 execlp(editor_path, editor_path, filename, NULL);
709 b = ka_wait(&e_ex_code);
710 } while ((b != editor_pid) && (b >= 0));
716 MECR: if (mode == 2) {
717 if (file_checksum(filename) == cksum) {
718 printf("*** Aborted message.\n");
726 b = keymenu("Entry command (? for options)",
727 "<A>bort|<C>ontinue|<S>ave message|<P>rint formatted|"
728 "<R>eplace string|<H>old message");
737 printf(" %s from %s", datestr, fullname);
738 if (strlen(recipient) > 0)
739 printf(" to %s", recipient);
741 fp = fopen(filename, "r");
743 fmout(screenwidth, fp,
744 ((userflags & US_PAGINATOR) ? 1 : 0),
752 replace_string(filename, 0L);
761 MEABT: printf("Are you sure? ");
765 MEABT2: unlink(filename);
770 * Transmit message text to the server.
772 * This loop also implements a "tick" counter that displays the progress, if
773 * we're sending something that will take a long time to transmit.
775 void transmit_message(FILE *fp)
782 fseek(fp, 0L, SEEK_END);
785 lasttick = time(NULL);
787 while (ch = getc(fp), (ch >= 0)) {
789 if (!strcmp(buf, "000"))
797 if ((ch == 32) && (strlen(buf) > 200)) {
799 if (!strcmp(buf, "000"))
804 if (strlen(buf) > 250) {
805 if (!strcmp(buf, "000"))
812 if ((time(NULL) - lasttick) > 2L) {
813 printf(" %3ld%% completed\r",
814 ((ftell(fp) * 100L) / msglen));
816 lasttick = time(NULL);
828 * entmsg() - edit and create a message
829 * returns 0 if message was saved
831 int entmsg(int is_reply, /* nonzero if this was a <R>eply command */
848 * First, check to see if we have permission to enter a message in
849 * this room. The server will return an error code if we can't.
851 sprintf(cmd, "ENT0 0||0|%d", mode);
855 if ((strncmp(cmd, "570", 3)) && (strncmp(cmd, "200", 3))) {
856 printf("%s\n", &cmd[4]);
860 /* Error code 570 is special. It means that we CAN enter a message
861 * in this room, but a recipient needs to be specified.
864 if (!strncmp(cmd, "570", 3))
867 /* If the user is a dumbass, tell them how to type. */
868 if ((userflags & US_EXPERT) == 0)
871 /* Handle the selection of a recipient, if necessary. */
873 if (need_recp == 1) {
876 strcpy(buf, reply_to);
878 printf("Enter recipient: ");
880 if (strlen(buf) == 0)
884 strcpy(buf, "sysop");
888 if (room_flags & QR_ANONOPT) {
889 printf("Anonymous (Y/N)? ");
894 /* If it's mail, we've got to check the validity of the recipient... */
895 if (strlen(buf) > 0) {
896 sprintf(cmd, "ENT0 0|%s|%d|%d", buf, b, mode);
900 printf("%s\n", &cmd[4]);
905 /* Learn the number of the newest message in in the room, so we can
906 * tell upon saving whether someone else has posted too.
909 serv_puts("MSGS LAST|1");
912 printf("%s\n", &cmd[5]);
914 while (serv_gets(cmd), strcmp(cmd, "000")) {
915 msg_arr[num_msgs++] = atol(cmd);
919 /* Now compose the message... */
920 if (make_message(temp, buf, b, 0, c) != 0) {
924 /* Reopen the temp file that was created, so we can send it */
925 fp = fopen(temp, "r");
927 /* Yes, unlink it now, so it doesn't stick around if we crash */
931 printf("*** Internal error while trying to save message!\n"
933 temp, strerror(errno));
937 /* Transmit message to the server */
938 sprintf(cmd, "ENT0 1|%s|%d|%d||", buf, b, mode);
942 printf("%s\n", &cmd[4]);
946 transmit_message(fp);
951 highmsg = msg_arr[num_msgs - 1];
953 serv_puts("MSGS NEW");
956 printf("%s\n", &cmd[5]);
958 while (serv_gets(cmd), strcmp(cmd, "000")) {
959 msg_arr[num_msgs++] = atol(cmd);
963 /* get new highest message number in room to set lrp for goto... */
964 maxmsgnum = msg_arr[num_msgs - 1];
966 /* now see if anyone else has posted in here */
968 for (a = 0; a < num_msgs; ++a) {
969 if (msg_arr[a] > highmsg) {
974 /* In the Mail> room, this algorithm always counts one message
975 * higher than in public rooms, so we decrement it by one.
982 printf("*** 1 additional message has been entered "
983 "in this room by another user.\n");
986 printf("*** %d additional messages have been entered "
987 "in this room by other users.\n", b);
994 * Do editing on a quoted file
996 void process_quote(void)
1000 int line, qstart, qend;
1002 /* Unlink the second temp file as soon as it's opened, so it'll get
1003 * deleted even if the program dies
1005 qfile = fopen(temp2, "r");
1008 /* Display the quotable text with line numbers added */
1010 fgets(buf, 128, qfile);
1011 while (fgets(buf, 128, qfile) != NULL) {
1012 printf("%2d %s", ++line, buf);
1014 printf("Begin quoting at [ 1] : ");
1016 qstart = (buf[0] == 0) ? (1) : atoi(buf);
1017 printf(" End quoting at [%d] : ", line);
1019 qend = (buf[0] == 0) ? (line) : atoi(buf);
1022 fgets(buf, 128, qfile);
1023 tfile = fopen(temp, "w");
1024 while (fgets(buf, 128, qfile) != NULL) {
1025 if ((++line >= qstart) && (line <= qend))
1026 fprintf(tfile, " >%s", buf);
1028 fprintf(tfile, " \n");
1037 * List the URL's which were embedded in the previous message
1044 if (num_urls == 0) {
1045 printf("There were no URL's in the previous message.\n\n");
1049 for (i = 0; i < num_urls; ++i) {
1050 printf("%3d %s\n", i + 1, urls[i]);
1053 if ((i = num_urls) != 1)
1054 i = intprompt("Display which one", 1, 1, num_urls);
1056 sprintf(cmd, rc_url_cmd, urls[i - 1]);
1062 * Read the messages in the current room
1065 int c, /* 0=Read all 1=Read new 2=Read old 3=Read last q */
1066 int rdir, /* 1=Forward (-1)=Reverse */
1067 int q /* Number of msgs to read (if c==3) */
1069 int a, b, e, f, g, start;
1075 char prtfile[PATH_MAX];
1078 char targ[ROOMNAMELEN];
1086 strcpy(prtfile, tmpnam(NULL));
1089 strcpy(cmd, "MSGS ");
1101 sprintf(&cmd[strlen(cmd)], "LAST|%d", q);
1106 if (cmd[0] != '1') {
1107 printf("%s\n", &cmd[5]);
1109 while (serv_gets(cmd), strcmp(cmd, "000")) {
1110 if (num_msgs == MAXMSGS) {
1111 memcpy(&msg_arr[0], &msg_arr[1],
1112 (sizeof(long) * (MAXMSGS - 1)));
1115 msg_arr[num_msgs++] = atol(cmd);
1119 if (num_msgs == 0) {
1120 printf("*** There are no ");
1121 if (c == 1) printf("new ");
1122 if (c == 2) printf("old ");
1123 printf("messages in this room.\n");
1129 /* this loop cycles through each message... */
1130 start = ((rdir == 1) ? 0 : (num_msgs - 1));
1131 for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1132 while (msg_arr[a] == 0L) {
1134 if ((a == MAXMSGS) || (a == (-1)))
1138 RAGAIN: pagin = ((arcflag == 0)
1140 && (userflags & US_PAGINATOR)) ? 1 : 0;
1142 /* If we're doing a quote, set the screenwidth to 72 */
1144 hold_sw = screenwidth;
1148 /* If printing or archiving, set the screenwidth to 80 */
1150 hold_sw = screenwidth;
1154 /* now read the message... */
1155 e = read_message(msg_arr[a], pagin);
1157 /* ...and set the screenwidth back if we have to */
1158 if ((quotflag) || (arcflag)) {
1159 screenwidth = hold_sw;
1161 RMSGREAD: fflush(stdout);
1162 highest_msg_read = msg_arr[a];
1164 freopen("/dev/tty", "r+", stdout);
1166 enable_color = hold_color;
1170 freopen("/dev/tty", "r+", stdout);
1172 enable_color = hold_color;
1175 freopen(prtfile, "r", stdin);
1176 sttybbs(SB_RESTORE);
1177 ka_system(printcmd);
1178 sttybbs(SB_NO_INTR);
1185 } while ((g != f) && (g >= 0));
1186 printf("Message printed.\n");
1190 if (((userflags & US_NOPROMPT) || (e == 2))
1191 && (((room_flags & QR_MAILBOX) == 0)
1192 || (rc_force_mail_prompts == 0))) {
1197 color(BRIGHT_WHITE);
1198 printf("%d", num_msgs - a - 1);
1203 ("<B>ack <A>gain <Q>uote <R>eply <N>ext <S>top ");
1204 if (rc_url_cmd[0] && num_urls)
1205 keyopt("<U>RL View ");
1206 keyopt("<?>Help/others -> ");
1210 e = (inkey() & 127);
1212 /* return key same as <N> */ if (e == 13)
1214 /* space key same as <N> */ if (e == 32)
1216 /* del/move for aides only */
1218 && ((room_flags & QR_MAILBOX) ==
1220 if ((e == 'd') || (e == 'm')
1224 /* print only if available */
1225 if ((e == 'p') && (strlen(printcmd) == 0))
1227 /* can't file if not allowed */
1229 && (rc_allow_attachments == 0))
1231 /* link only if browser avail*/
1233 && (strlen(rc_url_cmd) == 0))
1235 } while ((e != 'a') && (e != 'n') && (e != 's')
1236 && (e != 'd') && (e != 'm') && (e != 'p')
1237 && (e != 'q') && (e != 'b') && (e != 'h')
1238 && (e != 'r') && (e != 'f') && (e != '?')
1239 && (e != 'u') && (e != 'c'));
1281 printf("? <help>\r");
1284 if (userflags & US_DISAPPEAR)
1285 printf("\r%79s\r", "");
1292 printf("Options available here:\n");
1293 printf(" ? Help (prints this message)\n");
1294 printf(" S Stop reading immediately\n");
1295 printf(" A Again (repeats last message)\n");
1296 printf(" N Next (continue with next message)\n");
1297 printf(" B Back (go back to previous message)\n");
1299 || (room_flags & QR_MAILBOX)) {
1300 printf(" D Delete this message\n");
1302 (" M Move message to another room\n");
1304 (" C Copy message to another room\n");
1306 if (strlen(printcmd) > 0)
1307 printf(" P Print this message\n");
1309 (" Q Quote portions of this message for your next post\n");
1311 (" H Headers (display message headers only)\n");
1313 printf(" R Reply to this message\n");
1314 if (rc_allow_attachments)
1316 (" F (save attachments to a file)\n");
1317 if (strlen(rc_url_cmd) > 0)
1318 printf(" U (list URL's for display)\n");
1323 freopen(prtfile, "w", stdout);
1325 hold_color = enable_color;
1330 freopen(temp2, "w", stdout);
1332 hold_color = enable_color;
1344 newprompt("Enter target room: ",
1345 targ, ROOMNAMELEN - 1);
1346 if (strlen(targ) > 0) {
1347 sprintf(cmd, "MOVE %ld|%s|%d",
1349 (e == 'c' ? 1 : 0));
1352 printf("%s\n", &cmd[4]);
1362 newprompt("Which section? ", filename,
1363 ((sizeof filename) - 1));
1364 snprintf(cmd, sizeof cmd,
1365 "OPNA %ld|%s", msg_arr[a], filename);
1368 if (cmd[0] == '2') {
1369 extract(filename, &cmd[4], 2);
1370 download_to_local_disk(filename,
1371 extract_int(&cmd[4],
1374 printf("%s\n", &cmd[4]);
1378 printf("*** Delete this message? ");
1380 sprintf(cmd, "DELE %ld", msg_arr[a]);
1383 printf("%s\n", &cmd[4]);
1391 read_message(msg_arr[a], READ_HEADER);
1394 savedpos = num_msgs;
1395 entmsg(1, (DEFAULT_ENTRY == 46 ? 2 : 0));
1396 num_msgs = savedpos;
1402 } /* end for loop */
1403 } /* end read routine */
1409 * View and edit a system message
1411 void edit_system_message(char *which_message)
1417 sprintf(desc, "system message '%s'", which_message);
1418 sprintf(read_cmd, "MESG %s", which_message);
1419 sprintf(write_cmd, "EMSG %s", which_message);
1420 do_edit(desc, read_cmd, "NOOP", write_cmd);
1427 * Verify the message base
1429 void check_message_base(void)
1434 ("Please read the documentation before running this command.\n"
1435 "Having done so, do you still want to check the message base? ");
1441 if (buf[0] != '1') {
1442 printf("%s\n", &buf[4]);
1446 while (serv_gets(buf), strcmp(buf, "000")) {
1447 printf("%s\n", buf);