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 */
594 int format_type, int mode)
603 if (strlen(editor_path) == 0) {
605 ("*** No editor available, using built-in editor\n");
609 fmt_date(datestr, time(NULL));
611 if (room_flags & QR_ANONONLY) {
614 printf(" %s from %s", datestr, fullname);
615 if (strlen(recipient) > 0)
616 printf(" to %s", recipient);
623 printf("(Press ctrl-d when finished)\n");
625 fp = fopen(filename, "r");
627 fmout(screenwidth, fp, 0, screenheight, 0, 0);
631 fp = fopen(filename, "w");
639 fp = fopen(filename, "r+");
645 fp = fopen(filename, "w");
663 e_ex_code = 1; /* start with a failed exit code */
665 cksum = file_checksum(filename);
666 if (editor_pid == 0) {
667 chmod(filename, 0600);
669 execlp(editor_path, editor_path, filename, NULL);
675 b = ka_wait(&e_ex_code);
676 } while ((b != editor_pid) && (b >= 0));
682 MECR: if (mode == 2) {
683 if (file_checksum(filename) == cksum) {
684 printf("*** Aborted message.\n");
692 b = keymenu("Entry command (? for options)",
693 "<A>bort|<C>ontinue|<S>ave message|<P>rint formatted|"
694 "<R>eplace string|<H>old message");
703 printf(" %s from %s", datestr, fullname);
704 if (strlen(recipient) > 0)
705 printf(" to %s", recipient);
707 fp = fopen(filename, "r");
709 fmout(screenwidth, fp,
710 ((userflags & US_PAGINATOR) ? 1 : 0),
718 replace_string(filename, 0L);
727 MEABT: printf("Are you sure? ");
731 MEABT2: unlink(filename);
736 * Transmit message text to the server.
738 * This loop also implements a "tick" counter that displays the progress, if
739 * we're sending something that will take a long time to transmit.
741 void transmit_message(FILE *fp)
748 fseek(fp, 0L, SEEK_END);
751 lasttick = time(NULL);
753 while (ch = getc(fp), (ch >= 0)) {
755 if (!strcmp(buf, "000"))
763 if ((ch == 32) && (strlen(buf) > 200)) {
765 if (!strcmp(buf, "000"))
770 if (strlen(buf) > 250) {
771 if (!strcmp(buf, "000"))
778 if ((time(NULL) - lasttick) > 2L) {
779 printf(" %3ld%% completed\r",
780 ((ftell(fp) * 100L) / msglen));
782 lasttick = time(NULL);
794 * entmsg() - edit and create a message
795 * returns 0 if message was saved
797 int entmsg(int is_reply, /* nonzero if this was a <R>eply command */
814 * First, check to see if we have permission to enter a message in
815 * this room. The server will return an error code if we can't.
817 sprintf(cmd, "ENT0 0||0|%d", mode);
821 if ((strncmp(cmd, "570", 3)) && (strncmp(cmd, "200", 3))) {
822 printf("%s\n", &cmd[4]);
826 /* Error code 570 is special. It means that we CAN enter a message
827 * in this room, but a recipient needs to be specified.
830 if (!strncmp(cmd, "570", 3))
833 /* If the user is a dumbass, tell them how to type. */
834 if ((userflags & US_EXPERT) == 0)
837 /* Handle the selection of a recipient, if necessary. */
839 if (need_recp == 1) {
842 strcpy(buf, reply_to);
844 printf("Enter recipient: ");
846 if (strlen(buf) == 0)
850 strcpy(buf, "sysop");
854 if (room_flags & QR_ANONOPT) {
855 printf("Anonymous (Y/N)? ");
860 /* If it's mail, we've got to check the validity of the recipient... */
861 if (strlen(buf) > 0) {
862 sprintf(cmd, "ENT0 0|%s|%d|%d", buf, b, mode);
866 printf("%s\n", &cmd[4]);
871 /* Learn the number of the newest message in in the room, so we can
872 * tell upon saving whether someone else has posted too.
875 serv_puts("MSGS LAST|1");
878 printf("%s\n", &cmd[5]);
880 while (serv_gets(cmd), strcmp(cmd, "000")) {
881 msg_arr[num_msgs++] = atol(cmd);
885 /* Now compose the message... */
886 if (make_message(temp, buf, b, 0, c) != 0) {
890 /* ...and transmit it to the server. */
891 sprintf(cmd, "ENT0 1|%s|%d|%d||", buf, b, mode);
895 printf("%s\n", &cmd[4]);
898 fp = fopen(temp, "r");
900 transmit_message(fp);
906 highmsg = msg_arr[num_msgs - 1];
908 serv_puts("MSGS NEW");
911 printf("%s\n", &cmd[5]);
913 while (serv_gets(cmd), strcmp(cmd, "000")) {
914 msg_arr[num_msgs++] = atol(cmd);
918 /* get new highest message number in room to set lrp for goto... */
919 maxmsgnum = msg_arr[num_msgs - 1];
921 /* now see if anyone else has posted in here */
923 for (a = 0; a < num_msgs; ++a)
924 if (msg_arr[a] > highmsg)
927 /* in the Mail> room, this algorithm always counts one message
928 * higher than in public rooms, so we decrement it by one */
933 printf("*** 1 additional message has been entered "
934 "in this room by another user.\n");
937 printf("*** %d additional messages have been entered "
938 "in this room by other users.\n", b);
945 * Do editing on a quoted file
947 void process_quote(void)
951 int line, qstart, qend;
953 /* Unlink the second temp file as soon as it's opened, so it'll get
954 * deleted even if the program dies
956 qfile = fopen(temp2, "r");
959 /* Display the quotable text with line numbers added */
961 fgets(buf, 128, qfile);
962 while (fgets(buf, 128, qfile) != NULL) {
963 printf("%2d %s", ++line, buf);
965 printf("Begin quoting at [ 1] : ");
967 qstart = (buf[0] == 0) ? (1) : atoi(buf);
968 printf(" End quoting at [%d] : ", line);
970 qend = (buf[0] == 0) ? (line) : atoi(buf);
973 fgets(buf, 128, qfile);
974 tfile = fopen(temp, "w");
975 while (fgets(buf, 128, qfile) != NULL) {
976 if ((++line >= qstart) && (line <= qend))
977 fprintf(tfile, " >%s", buf);
979 fprintf(tfile, " \n");
988 * List the URL's which were embedded in the previous message
996 printf("There were no URL's in the previous message.\n\n");
1000 for (i = 0; i < num_urls; ++i) {
1001 printf("%3d %s\n", i + 1, urls[i]);
1004 if ((i = num_urls) != 1)
1005 i = intprompt("Display which one", 1, 1, num_urls);
1007 sprintf(cmd, rc_url_cmd, urls[i - 1]);
1013 * Read the messages in the current room
1016 int c, /* 0=Read all 1=Read new 2=Read old 3=Read last q */
1017 int rdir, /* 1=Forward (-1)=Reverse */
1018 int q /* Number of msgs to read (if c==3) */
1020 int a, b, e, f, g, start;
1026 char prtfile[PATH_MAX];
1029 char targ[ROOMNAMELEN];
1037 strcpy(prtfile, tmpnam(NULL));
1040 strcpy(cmd, "MSGS ");
1052 sprintf(&cmd[strlen(cmd)], "LAST|%d", q);
1057 if (cmd[0] != '1') {
1058 printf("%s\n", &cmd[5]);
1060 while (serv_gets(cmd), strcmp(cmd, "000")) {
1061 if (num_msgs == MAXMSGS) {
1062 memcpy(&msg_arr[0], &msg_arr[1],
1063 (sizeof(long) * (MAXMSGS - 1)));
1066 msg_arr[num_msgs++] = atol(cmd);
1072 /* this loop cycles through each message... */
1073 start = ((rdir == 1) ? 0 : (num_msgs - 1));
1074 for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1075 while (msg_arr[a] == 0L) {
1077 if ((a == MAXMSGS) || (a == (-1)))
1081 RAGAIN: pagin = ((arcflag == 0)
1083 && (userflags & US_PAGINATOR)) ? 1 : 0;
1085 /* If we're doing a quote, set the screenwidth to 72 */
1087 hold_sw = screenwidth;
1091 /* If printing or archiving, set the screenwidth to 80 */
1093 hold_sw = screenwidth;
1097 /* now read the message... */
1098 e = read_message(msg_arr[a], pagin);
1100 /* ...and set the screenwidth back if we have to */
1101 if ((quotflag) || (arcflag)) {
1102 screenwidth = hold_sw;
1104 RMSGREAD: fflush(stdout);
1105 highest_msg_read = msg_arr[a];
1107 freopen("/dev/tty", "r+", stdout);
1109 enable_color = hold_color;
1113 freopen("/dev/tty", "r+", stdout);
1115 enable_color = hold_color;
1118 freopen(prtfile, "r", stdin);
1119 sttybbs(SB_RESTORE);
1120 ka_system(printcmd);
1121 sttybbs(SB_NO_INTR);
1128 } while ((g != f) && (g >= 0));
1129 printf("Message printed.\n");
1133 if (((userflags & US_NOPROMPT) || (e == 2))
1134 && (((room_flags & QR_MAILBOX) == 0)
1135 || (rc_force_mail_prompts == 0))) {
1140 color(BRIGHT_WHITE);
1141 printf("%d", num_msgs - a - 1);
1146 ("<B>ack <A>gain <Q>uote <R>eply <N>ext <S>top ");
1147 if (rc_url_cmd[0] && num_urls)
1148 keyopt("<U>RL View ");
1149 keyopt("<?>Help/others -> ");
1153 e = (inkey() & 127);
1155 /* return key same as <N> */ if (e == 13)
1157 /* space key same as <N> */ if (e == 32)
1159 /* del/move for aides only */
1161 && ((room_flags & QR_MAILBOX) ==
1163 if ((e == 'd') || (e == 'm')
1167 /* print only if available */
1168 if ((e == 'p') && (strlen(printcmd) == 0))
1170 /* can't file if not allowed */
1172 && (rc_allow_attachments == 0))
1174 /* link only if browser avail*/
1176 && (strlen(rc_url_cmd) == 0))
1178 } while ((e != 'a') && (e != 'n') && (e != 's')
1179 && (e != 'd') && (e != 'm') && (e != 'p')
1180 && (e != 'q') && (e != 'b') && (e != 'h')
1181 && (e != 'r') && (e != 'f') && (e != '?')
1182 && (e != 'u') && (e != 'c'));
1224 printf("? <help>\r");
1227 if (userflags & US_DISAPPEAR)
1228 printf("\r%79s\r", "");
1235 printf("Options available here:\n");
1236 printf(" ? Help (prints this message)\n");
1237 printf(" S Stop reading immediately\n");
1238 printf(" A Again (repeats last message)\n");
1239 printf(" N Next (continue with next message)\n");
1240 printf(" B Back (go back to previous message)\n");
1242 || (room_flags & QR_MAILBOX)) {
1243 printf(" D Delete this message\n");
1245 (" M Move message to another room\n");
1247 (" C Copy message to another room\n");
1249 if (strlen(printcmd) > 0)
1250 printf(" P Print this message\n");
1252 (" Q Quote portions of this message for your next post\n");
1254 (" H Headers (display message headers only)\n");
1256 printf(" R Reply to this message\n");
1257 if (rc_allow_attachments)
1259 (" F (save attachments to a file)\n");
1260 if (strlen(rc_url_cmd) > 0)
1261 printf(" U (list URL's for display)\n");
1266 freopen(prtfile, "w", stdout);
1268 hold_color = enable_color;
1273 freopen(temp2, "w", stdout);
1275 hold_color = enable_color;
1287 newprompt("Enter target room: ",
1288 targ, ROOMNAMELEN - 1);
1289 if (strlen(targ) > 0) {
1290 sprintf(cmd, "MOVE %ld|%s|%d",
1292 (e == 'c' ? 1 : 0));
1295 printf("%s\n", &cmd[4]);
1305 newprompt("Which section? ", filename,
1306 ((sizeof filename) - 1));
1307 snprintf(cmd, sizeof cmd,
1308 "OPNA %ld|%s", msg_arr[a], filename);
1311 if (cmd[0] == '2') {
1312 extract(filename, &cmd[4], 2);
1313 download_to_local_disk(filename,
1314 extract_int(&cmd[4],
1317 printf("%s\n", &cmd[4]);
1321 printf("*** Delete this message? ");
1323 sprintf(cmd, "DELE %ld", msg_arr[a]);
1326 printf("%s\n", &cmd[4]);
1334 read_message(msg_arr[a], READ_HEADER);
1337 savedpos = num_msgs;
1338 entmsg(1, (DEFAULT_ENTRY == 46 ? 2 : 0));
1339 num_msgs = savedpos;
1345 } /* end for loop */
1346 } /* end read routine */
1352 * View and edit a system message
1354 void edit_system_message(char *which_message)
1360 sprintf(desc, "system message '%s'", which_message);
1361 sprintf(read_cmd, "MESG %s", which_message);
1362 sprintf(write_cmd, "EMSG %s", which_message);
1363 do_edit(desc, read_cmd, "NOOP", write_cmd);
1370 * Verify the message base
1372 void check_message_base(void)
1377 ("Please read the documentation before running this command.\n"
1378 "Having done so, do you still want to check the message base? ");
1384 if (buf[0] != '1') {
1385 printf("%s\n", &buf[4]);
1389 while (serv_gets(buf), strcmp(buf, "000")) {
1390 printf("%s\n", buf);