4 * Citadel/UX message support routines
5 * see copyright.txt for copyright information
11 #include <sys/types.h>
32 #define MAXWORDBUF 256
37 char text[MAXWORDBUF];
40 void sttybbs(int cmd);
41 int fmout(int width, FILE * fp, char pagin, int height, int starting_lp,
43 int haschar(char *st, int ch);
44 int checkpagin(int lp, int pagin, int height);
45 void getline(char *string, int lim);
46 void formout(char *name);
48 void newprompt(char *prompt, char *str, int len);
49 int file_checksum(char *filename);
50 void do_edit(char *desc, char *read_cmd, char *check_cmd, char *write_cmd);
53 long msg_arr[MAXMSGS];
55 extern char room_name[];
56 extern unsigned room_flags;
57 extern long highest_msg_read;
58 extern struct CtdlServInfo serv_info;
61 extern int screenwidth;
62 extern int screenheight;
63 extern long maxmsgnum;
66 extern char is_room_aide;
67 extern char fullname[];
69 extern unsigned userflags;
70 extern char sigcaught;
71 extern char editor_path[];
72 extern char printcmd[];
73 extern int rc_allow_attachments;
74 extern int rc_display_message_numbers;
75 extern int rc_force_mail_prompts;
77 extern int editor_pid;
79 void ka_sigcatch(int signum)
83 signal(SIGALRM, ka_sigcatch);
90 * server keep-alive version of wait() (needed for external editor)
92 pid_t ka_wait(int *kstatus)
97 signal(SIGALRM, ka_sigcatch);
101 } while (errno == EINTR);
102 signal(SIGALRM, SIG_IGN);
109 * version of system() that uses ka_wait()
111 int ka_system(char *shc)
120 perror("Cannot fork");
122 return ((pid_t) childpid);
126 execlp("/bin/sh", "sh", "-c", shc, NULL);
132 waitpid = ka_wait(&retcode);
133 } while (waitpid != childpid);
143 * add a newline to the buffer...
145 void add_newline(struct cittext *textlist)
150 while (ptr->next != NULL)
153 while (ptr->text[strlen(ptr->text) - 1] == 32)
154 ptr->text[strlen(ptr->text) - 1] = 0;
155 /* strcat(ptr->text,"\n"); */
157 ptr->next = (struct cittext *)
158 malloc(sizeof(struct cittext));
161 strcpy(ptr->text, "");
166 * add a word to the buffer...
168 void add_word(struct cittext *textlist, char *wordbuf)
173 while (ptr->next != NULL)
176 if (3 + strlen(ptr->text) + strlen(wordbuf) > screenwidth) {
177 ptr->next = (struct cittext *)
178 malloc(sizeof(struct cittext));
181 strcpy(ptr->text, "");
184 strcat(ptr->text, wordbuf);
185 strcat(ptr->text, " ");
190 * begin editing of an opened file pointed to by fp
192 void citedit(FILE * fp)
194 int a, prev, finished, b, last_space;
196 struct cittext *textlist = NULL;
198 char wordbuf[MAXWORDBUF];
200 /* first, load the text into the buffer */
202 textlist = (struct cittext *) malloc(sizeof(struct cittext));
203 textlist->next = NULL;
204 strcpy(textlist->text, "");
208 while (a = getc(fp), a >= 0) {
210 if ((a == 32) || (a == 9) || (a == 13) || (a == 10)) {
211 add_word(textlist, wordbuf);
213 if ((prev == 13) || (prev == 10)) {
214 add_word(textlist, "\n");
215 add_newline(textlist);
216 add_word(textlist, "");
219 wordbuf[strlen(wordbuf) + 1] = 0;
220 wordbuf[strlen(wordbuf)] = a;
222 if (strlen(wordbuf) + 3 > screenwidth) {
223 add_word(textlist, wordbuf);
231 prev = (appending ? 13 : (-1));
243 /******* new ***********/
244 if ((a > 32) && (a < 127) && (prev == 13)) {
245 add_word(textlist, "\n");
248 /***********************/
250 if ((a == 32) && (prev == 13)) {
251 add_word(textlist, "\n");
252 add_newline(textlist);
256 if (strlen(wordbuf) > 0) {
257 wordbuf[strlen(wordbuf) - 1] = 0;
262 } else if (a == 13) {
264 if (strlen(wordbuf) == 0)
267 for (b = 0; b < strlen(wordbuf); ++b)
268 if (wordbuf[b] == 32) {
276 add_word(textlist, wordbuf);
281 wordbuf[strlen(wordbuf) + 1] = 0;
282 wordbuf[strlen(wordbuf)] = a;
284 if ((strlen(wordbuf) + 3) > screenwidth) {
286 for (b = 0; b < strlen(wordbuf); ++b)
287 if (wordbuf[b] == 32)
289 if (last_space >= 0) {
290 for (b = 0; b < strlen(wordbuf); ++b)
291 if (wordbuf[b] == 32) {
299 for (b = 0; b < strlen(wordbuf); ++b) {
304 printf("\n%s", wordbuf);
306 add_word(textlist, wordbuf);
312 } while (finished == 0);
315 /* write the buffer back to disk */
317 for (ptr = textlist; ptr != NULL; ptr = ptr->next) {
318 fprintf(fp, "%s", ptr->text);
322 ftruncate(fileno(fp), ftell(fp));
324 /* and deallocate the memory we used */
325 while (textlist != NULL) {
326 ptr = textlist->next;
332 /* Read a message from the server
335 long num, /* message number */
336 char pagin) /* 0 = normal read, 1 = read with pagination, 2 = header */
340 char from[256], node[256], rfca[256];
349 sprintf(buf, "MSG0 %ld|%d", num, (pagin == READ_HEADER ? 1 : 0));
353 printf("*** msg #%ld: %s\n", num, buf);
356 checkpagin(lines_printed, pagin, screenheight);
361 strcpy(m_subject, "");
362 strcpy(reply_to, "nobody ... xxxxx");
369 lines_printed = checkpagin(lines_printed, pagin, screenheight);
376 while (serv_gets(buf), strcmp(buf, "000")) {
381 checkpagin(lines_printed,
382 pagin, screenheight);
389 while (serv_gets(buf), strncasecmp(buf, "text", 4)) {
390 if (!strncasecmp(buf, "nhdr=yes", 8))
392 if (!strncasecmp(buf, "from=", 5)) {
393 strcpy(from, &buf[5]);
397 if (!strncasecmp(buf, "type=", 5))
398 format_type = atoi(&buf[5]);
399 if ((!strncasecmp(buf, "msgn=", 5))
400 && (rc_display_message_numbers)) {
404 printf("#%s", &buf[5]);
408 if (!strncasecmp(buf, "from=", 5)) {
412 printf("%s ", &buf[5]);
414 if (!strncasecmp(buf, "subj=", 5))
415 strcpy(m_subject, &buf[5]);
417 if (!strncasecmp(buf, "rfca=", 5)) {
418 safestrncpy(rfca, &buf[5], sizeof(rfca) - 5);
422 printf("%s", &buf[5]);
426 if ((!strncasecmp(buf, "hnod=", 5))
427 && (strcasecmp(&buf[5], serv_info.serv_humannode))
428 && (strlen(rfca) == 0)) {
432 printf("%s", &buf[5]);
436 if ((!strncasecmp(buf, "room=", 5))
437 && (strcasecmp(&buf[5], room_name))
438 && (strlen(rfca) == 0)) {
441 color(BRIGHT_MAGENTA);
442 printf("%s> ", &buf[5]);
445 if (!strncasecmp(buf, "node=", 5)) {
446 safestrncpy(node, &buf[5], sizeof(buf) - 5);
447 if ((room_flags & QR_NETWORK)
450 (&buf[5], serv_info.serv_nodename)
452 (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
454 if (strlen(rfca) == 0) {
457 color(BRIGHT_YELLOW);
458 printf("%s ", &buf[5]);
463 if (!strncasecmp(buf, "rcpt=", 5)) {
467 printf("%s ", &buf[5]);
469 if (!strncasecmp(buf, "time=", 5)) {
470 fmt_date(now, atol(&buf[5]));
484 if (strlen(rfca) > 0) {
485 strcpy(reply_to, rfca);
487 snprintf(reply_to, sizeof(reply_to), "%s @ %s", from,
494 lines_printed = checkpagin(lines_printed, pagin, screenheight);
496 if (strlen(m_subject) > 0) {
497 printf("Subject: %s\n", m_subject);
500 checkpagin(lines_printed, pagin, screenheight);
503 if (format_type == 0) {
504 fr = fmout(screenwidth, NULL,
505 ((pagin == 1) ? 1 : 0), screenheight, (-1), 1);
507 while (serv_gets(buf), strcmp(buf, "000")) {
508 if (sigcaught == 0) {
510 lines_printed = lines_printed + 1 +
511 (strlen(buf) / screenwidth);
513 checkpagin(lines_printed, pagin,
521 lines_printed = checkpagin(lines_printed, pagin, screenheight);
530 * replace string function for the built-in editor
532 void replace_string(char *filename, long int startpos)
541 int substitutions = 0;
544 printf("Enter text to be replaced:\n: ");
545 getline(srch_str, 128);
546 if (strlen(srch_str) == 0)
549 printf("Enter text to replace it with:\n: ");
550 getline(rplc_str, 128);
552 fp = fopen(filename, "r+");
557 fseek(fp, startpos, 0);
559 while (a = getc(fp), a > 0) {
561 buf[strlen(buf) + 1] = 0;
562 buf[strlen(buf)] = a;
563 if (strlen(buf) >= strlen(srch_str)) {
564 ptr = (&buf[strlen(buf) - strlen(srch_str)]);
565 if (!strncasecmp(ptr, srch_str, strlen(srch_str))) {
566 strcpy(ptr, rplc_str);
570 if (strlen(buf) > 384) {
573 fwrite((char *) buf, 128, 1, fp);
574 strcpy(buf, &buf[128]);
581 fwrite((char *) buf, strlen(buf), 1, fp);
584 truncate(filename, wpos);
585 printf("<R>eplace made %d substitution(s).\n\n", substitutions);
589 * Function to begin composing a new message
591 int make_message(char *filename, /* temporary file name */
592 char *recipient, /* NULL if it's not mail */
593 int anon_type, /* see MES_ types in header file */
604 if (strlen(editor_path) == 0) {
606 ("*** No editor available, using built-in editor\n");
610 fmt_date(datestr, time(NULL));
612 if (room_flags & QR_ANONONLY) {
616 printf(" %s from %s", datestr, fullname);
617 if (strlen(recipient) > 0) {
618 printf(" to %s", recipient);
626 printf("(Press ctrl-d when finished)\n");
629 fp = fopen(filename, "r");
631 fmout(screenwidth, fp, 0, screenheight, 0, 0);
635 fp = fopen(filename, "w");
637 printf("*** Error opening temp file!\n"
639 filename, strerror(errno));
649 fp = fopen(filename, "r+");
651 printf("*** Error opening temp file!\n"
653 filename, strerror(errno));
661 fp = fopen(filename, "w");
663 printf("*** Error opening temp file!\n"
665 filename, strerror(errno));
685 e_ex_code = 1; /* start with a failed exit code */
687 cksum = file_checksum(filename);
688 if (editor_pid == 0) {
689 chmod(filename, 0600);
691 execlp(editor_path, editor_path, filename, NULL);
697 b = ka_wait(&e_ex_code);
698 } while ((b != editor_pid) && (b >= 0));
704 MECR: if (mode == 2) {
705 if (file_checksum(filename) == cksum) {
706 printf("*** Aborted message.\n");
714 b = keymenu("Entry command (? for options)",
715 "<A>bort|<C>ontinue|<S>ave message|<P>rint formatted|"
716 "<R>eplace string|<H>old message");
725 printf(" %s from %s", datestr, fullname);
726 if (strlen(recipient) > 0)
727 printf(" to %s", recipient);
729 fp = fopen(filename, "r");
731 fmout(screenwidth, fp,
732 ((userflags & US_PAGINATOR) ? 1 : 0),
740 replace_string(filename, 0L);
749 MEABT: printf("Are you sure? ");
753 MEABT2: unlink(filename);
758 * Transmit message text to the server.
760 * This loop also implements a "tick" counter that displays the progress, if
761 * we're sending something that will take a long time to transmit.
763 void transmit_message(FILE *fp)
770 fseek(fp, 0L, SEEK_END);
773 lasttick = time(NULL);
775 while (ch = getc(fp), (ch >= 0)) {
777 if (!strcmp(buf, "000"))
785 if ((ch == 32) && (strlen(buf) > 200)) {
787 if (!strcmp(buf, "000"))
792 if (strlen(buf) > 250) {
793 if (!strcmp(buf, "000"))
800 if ((time(NULL) - lasttick) > 2L) {
801 printf(" %3ld%% completed\r",
802 ((ftell(fp) * 100L) / msglen));
804 lasttick = time(NULL);
816 * entmsg() - edit and create a message
817 * returns 0 if message was saved
819 int entmsg(int is_reply, /* nonzero if this was a <R>eply command */
836 * First, check to see if we have permission to enter a message in
837 * this room. The server will return an error code if we can't.
839 sprintf(cmd, "ENT0 0||0|%d", mode);
843 if ((strncmp(cmd, "570", 3)) && (strncmp(cmd, "200", 3))) {
844 printf("%s\n", &cmd[4]);
848 /* Error code 570 is special. It means that we CAN enter a message
849 * in this room, but a recipient needs to be specified.
852 if (!strncmp(cmd, "570", 3))
855 /* If the user is a dumbass, tell them how to type. */
856 if ((userflags & US_EXPERT) == 0)
859 /* Handle the selection of a recipient, if necessary. */
861 if (need_recp == 1) {
864 strcpy(buf, reply_to);
866 printf("Enter recipient: ");
868 if (strlen(buf) == 0)
872 strcpy(buf, "sysop");
876 if (room_flags & QR_ANONOPT) {
877 printf("Anonymous (Y/N)? ");
882 /* If it's mail, we've got to check the validity of the recipient... */
883 if (strlen(buf) > 0) {
884 sprintf(cmd, "ENT0 0|%s|%d|%d", buf, b, mode);
888 printf("%s\n", &cmd[4]);
893 /* Learn the number of the newest message in in the room, so we can
894 * tell upon saving whether someone else has posted too.
897 serv_puts("MSGS LAST|1");
900 printf("%s\n", &cmd[5]);
902 while (serv_gets(cmd), strcmp(cmd, "000")) {
903 msg_arr[num_msgs++] = atol(cmd);
907 /* Now compose the message... */
908 if (make_message(temp, buf, b, 0, c) != 0) {
912 /* Reopen the temp file that was created, so we can send it */
913 fp = fopen(temp, "r");
915 /* Yes, unlink it now, so it doesn't stick around if we crash */
919 printf("*** Internal error while trying to save message!\n"
921 temp, strerror(errno));
925 /* Transmit message to the server */
926 sprintf(cmd, "ENT0 1|%s|%d|%d||", buf, b, mode);
930 printf("%s\n", &cmd[4]);
934 transmit_message(fp);
939 highmsg = msg_arr[num_msgs - 1];
941 serv_puts("MSGS NEW");
944 printf("%s\n", &cmd[5]);
946 while (serv_gets(cmd), strcmp(cmd, "000")) {
947 msg_arr[num_msgs++] = atol(cmd);
951 /* get new highest message number in room to set lrp for goto... */
952 maxmsgnum = msg_arr[num_msgs - 1];
954 /* now see if anyone else has posted in here */
956 for (a = 0; a < num_msgs; ++a) {
957 if (msg_arr[a] > highmsg) {
962 /* In the Mail> room, this algorithm always counts one message
963 * higher than in public rooms, so we decrement it by one.
970 printf("*** 1 additional message has been entered "
971 "in this room by another user.\n");
974 printf("*** %d additional messages have been entered "
975 "in this room by other users.\n", b);
982 * Do editing on a quoted file
984 void process_quote(void)
988 int line, qstart, qend;
990 /* Unlink the second temp file as soon as it's opened, so it'll get
991 * deleted even if the program dies
993 qfile = fopen(temp2, "r");
996 /* Display the quotable text with line numbers added */
998 fgets(buf, 128, qfile);
999 while (fgets(buf, 128, qfile) != NULL) {
1000 printf("%2d %s", ++line, buf);
1002 printf("Begin quoting at [ 1] : ");
1004 qstart = (buf[0] == 0) ? (1) : atoi(buf);
1005 printf(" End quoting at [%d] : ", line);
1007 qend = (buf[0] == 0) ? (line) : atoi(buf);
1010 fgets(buf, 128, qfile);
1011 tfile = fopen(temp, "w");
1012 while (fgets(buf, 128, qfile) != NULL) {
1013 if ((++line >= qstart) && (line <= qend))
1014 fprintf(tfile, " >%s", buf);
1016 fprintf(tfile, " \n");
1025 * List the URL's which were embedded in the previous message
1032 if (num_urls == 0) {
1033 printf("There were no URL's in the previous message.\n\n");
1037 for (i = 0; i < num_urls; ++i) {
1038 printf("%3d %s\n", i + 1, urls[i]);
1041 if ((i = num_urls) != 1)
1042 i = intprompt("Display which one", 1, 1, num_urls);
1044 sprintf(cmd, rc_url_cmd, urls[i - 1]);
1050 * Read the messages in the current room
1053 int c, /* 0=Read all 1=Read new 2=Read old 3=Read last q */
1054 int rdir, /* 1=Forward (-1)=Reverse */
1055 int q /* Number of msgs to read (if c==3) */
1057 int a, b, e, f, g, start;
1063 char prtfile[PATH_MAX];
1066 char targ[ROOMNAMELEN];
1074 strcpy(prtfile, tmpnam(NULL));
1077 strcpy(cmd, "MSGS ");
1089 sprintf(&cmd[strlen(cmd)], "LAST|%d", q);
1094 if (cmd[0] != '1') {
1095 printf("%s\n", &cmd[5]);
1097 while (serv_gets(cmd), strcmp(cmd, "000")) {
1098 if (num_msgs == MAXMSGS) {
1099 memcpy(&msg_arr[0], &msg_arr[1],
1100 (sizeof(long) * (MAXMSGS - 1)));
1103 msg_arr[num_msgs++] = atol(cmd);
1109 /* this loop cycles through each message... */
1110 start = ((rdir == 1) ? 0 : (num_msgs - 1));
1111 for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1112 while (msg_arr[a] == 0L) {
1114 if ((a == MAXMSGS) || (a == (-1)))
1118 RAGAIN: pagin = ((arcflag == 0)
1120 && (userflags & US_PAGINATOR)) ? 1 : 0;
1122 /* If we're doing a quote, set the screenwidth to 72 */
1124 hold_sw = screenwidth;
1128 /* If printing or archiving, set the screenwidth to 80 */
1130 hold_sw = screenwidth;
1134 /* now read the message... */
1135 e = read_message(msg_arr[a], pagin);
1137 /* ...and set the screenwidth back if we have to */
1138 if ((quotflag) || (arcflag)) {
1139 screenwidth = hold_sw;
1141 RMSGREAD: fflush(stdout);
1142 highest_msg_read = msg_arr[a];
1144 freopen("/dev/tty", "r+", stdout);
1146 enable_color = hold_color;
1150 freopen("/dev/tty", "r+", stdout);
1152 enable_color = hold_color;
1155 freopen(prtfile, "r", stdin);
1156 sttybbs(SB_RESTORE);
1157 ka_system(printcmd);
1158 sttybbs(SB_NO_INTR);
1165 } while ((g != f) && (g >= 0));
1166 printf("Message printed.\n");
1170 if (((userflags & US_NOPROMPT) || (e == 2))
1171 && (((room_flags & QR_MAILBOX) == 0)
1172 || (rc_force_mail_prompts == 0))) {
1177 color(BRIGHT_WHITE);
1178 printf("%d", num_msgs - a - 1);
1183 ("<B>ack <A>gain <Q>uote <R>eply <N>ext <S>top ");
1184 if (rc_url_cmd[0] && num_urls)
1185 keyopt("<U>RL View ");
1186 keyopt("<?>Help/others -> ");
1190 e = (inkey() & 127);
1192 /* return key same as <N> */ if (e == 13)
1194 /* space key same as <N> */ if (e == 32)
1196 /* del/move for aides only */
1198 && ((room_flags & QR_MAILBOX) ==
1200 if ((e == 'd') || (e == 'm')
1204 /* print only if available */
1205 if ((e == 'p') && (strlen(printcmd) == 0))
1207 /* can't file if not allowed */
1209 && (rc_allow_attachments == 0))
1211 /* link only if browser avail*/
1213 && (strlen(rc_url_cmd) == 0))
1215 } while ((e != 'a') && (e != 'n') && (e != 's')
1216 && (e != 'd') && (e != 'm') && (e != 'p')
1217 && (e != 'q') && (e != 'b') && (e != 'h')
1218 && (e != 'r') && (e != 'f') && (e != '?')
1219 && (e != 'u') && (e != 'c'));
1261 printf("? <help>\r");
1264 if (userflags & US_DISAPPEAR)
1265 printf("\r%79s\r", "");
1272 printf("Options available here:\n");
1273 printf(" ? Help (prints this message)\n");
1274 printf(" S Stop reading immediately\n");
1275 printf(" A Again (repeats last message)\n");
1276 printf(" N Next (continue with next message)\n");
1277 printf(" B Back (go back to previous message)\n");
1279 || (room_flags & QR_MAILBOX)) {
1280 printf(" D Delete this message\n");
1282 (" M Move message to another room\n");
1284 (" C Copy message to another room\n");
1286 if (strlen(printcmd) > 0)
1287 printf(" P Print this message\n");
1289 (" Q Quote portions of this message for your next post\n");
1291 (" H Headers (display message headers only)\n");
1293 printf(" R Reply to this message\n");
1294 if (rc_allow_attachments)
1296 (" F (save attachments to a file)\n");
1297 if (strlen(rc_url_cmd) > 0)
1298 printf(" U (list URL's for display)\n");
1303 freopen(prtfile, "w", stdout);
1305 hold_color = enable_color;
1310 freopen(temp2, "w", stdout);
1312 hold_color = enable_color;
1324 newprompt("Enter target room: ",
1325 targ, ROOMNAMELEN - 1);
1326 if (strlen(targ) > 0) {
1327 sprintf(cmd, "MOVE %ld|%s|%d",
1329 (e == 'c' ? 1 : 0));
1332 printf("%s\n", &cmd[4]);
1342 newprompt("Which section? ", filename,
1343 ((sizeof filename) - 1));
1344 snprintf(cmd, sizeof cmd,
1345 "OPNA %ld|%s", msg_arr[a], filename);
1348 if (cmd[0] == '2') {
1349 extract(filename, &cmd[4], 2);
1350 download_to_local_disk(filename,
1351 extract_int(&cmd[4],
1354 printf("%s\n", &cmd[4]);
1358 printf("*** Delete this message? ");
1360 sprintf(cmd, "DELE %ld", msg_arr[a]);
1363 printf("%s\n", &cmd[4]);
1371 read_message(msg_arr[a], READ_HEADER);
1374 savedpos = num_msgs;
1375 entmsg(1, (DEFAULT_ENTRY == 46 ? 2 : 0));
1376 num_msgs = savedpos;
1382 } /* end for loop */
1383 } /* end read routine */
1389 * View and edit a system message
1391 void edit_system_message(char *which_message)
1397 sprintf(desc, "system message '%s'", which_message);
1398 sprintf(read_cmd, "MESG %s", which_message);
1399 sprintf(write_cmd, "EMSG %s", which_message);
1400 do_edit(desc, read_cmd, "NOOP", write_cmd);
1407 * Verify the message base
1409 void check_message_base(void)
1414 ("Please read the documentation before running this command.\n"
1415 "Having done so, do you still want to check the message base? ");
1421 if (buf[0] != '1') {
1422 printf("%s\n", &buf[4]);
1426 while (serv_gets(buf), strcmp(buf, "000")) {
1427 printf("%s\n", buf);