4 * This file contains functions which implement parts of the
5 * text-mode user interface.
16 #include <sys/types.h>
25 #ifdef HAVE_SYS_SELECT_H
26 #include <sys/select.h>
29 #ifdef THREADED_CLIENT
39 #include "citadel_decls.h"
41 #include "routines2.h"
54 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
59 int rc_allow_attachments;
60 int rc_display_message_numbers;
61 int rc_force_mail_prompts;
64 char urls[MAXURLS][256];
68 int next_lazy_cmd = 5;
70 struct citcmd *cmdlist = NULL;
73 /* these variables are local to this module */
74 char keepalives_enabled = KA_YES; /* send NOOPs to server when idle */
75 int ok_to_interrupt = 0; /* print express msgs asynchronously */
76 time_t AnsiDetect; /* when did we send the detect code? */
77 int enable_color = 0; /* nonzero for ANSI color */
81 * print_express() - print express messages if there are any
83 void print_express(void)
88 if (express_msgs == 0)
96 if (strlen(rc_exp_cmd) > 0) {
97 outpipe = popen(rc_exp_cmd, "w");
98 if (outpipe != NULL) {
99 while (serv_gets(buf), strcmp(buf, "000")) {
100 fprintf(outpipe, "%s\n", buf);
106 /* fall back to built-in express message display */
112 while (serv_gets(buf), strcmp(buf, "000")) {
120 void set_keepalives(int s)
122 keepalives_enabled = (char) s;
126 * This loop handles the "keepalive" messages sent to the server when idling.
129 static time_t idlet = 0;
130 static void really_do_keepalive(void) {
134 if (keepalives_enabled != KA_NO) {
136 if (keepalives_enabled == KA_YES) {
140 if (ok_to_interrupt == 1) {
141 printf("\r%64s\r", "");
143 printf("%s%c ", room_name,
144 room_prompt(room_flags));
152 /* threaded nonblocking keepalive stuff starts here. I'm going for a simple
153 encapsulated interface; in theory there should be no need to touch these
154 globals outside of the async_ka_* functions. */
156 #ifdef THREADED_CLIENT
157 static pthread_t ka_thr_handle;
158 static int ka_thr_active = 0;
159 static int async_ka_enabled = 0;
161 static void *ka_thread(void *arg)
163 really_do_keepalive();
164 pthread_detach(ka_thr_handle);
169 /* start up a thread to handle a keepalive in the background */
170 static void async_ka_exec(void)
172 if (!ka_thr_active) {
174 if (pthread_create(&ka_thr_handle, NULL, ka_thread, NULL)) {
175 perror("pthread_create");
180 #endif /* THREADED_CLIENT */
182 static void do_keepalive(void)
187 if ((now - idlet) < ((long) S_KEEPALIVE))
190 /* Do a space-backspace to keep telnet sessions from idling out */
194 #ifdef THREADED_CLIENT
195 if (async_ka_enabled)
199 really_do_keepalive();
203 /* Now the actual async-keepalve API that we expose to higher levels:
204 async_ka_start() and async_ka_end(). These do nothing when we don't have
205 threading enabled, so we avoid sprinkling ifdef's throughout the code. */
207 /* wait for a background keepalive to complete. this must be done before
208 attempting any further server requests! */
209 void async_ka_end(void)
211 #ifdef THREADED_CLIENT
213 pthread_join(ka_thr_handle, NULL);
219 /* tell do_keepalive() that keepalives are asynchronous. */
220 void async_ka_start(void)
222 #ifdef THREADED_CLIENT
229 { /* get a character from the keyboard, with */
230 int a; /* the watchdog timer in effect if necessary */
233 time_t start_time, now;
241 /* This loop waits for keyboard input. If the keepalive
242 * timer expires, it sends a keepalive to the server if
243 * necessary and then waits again.
250 tv.tv_sec = S_KEEPALIVE;
254 if (((now - start_time) > SLEEPING)
255 && (SLEEPING != 0) && (getppid() == 1)) {
256 printf("Sleeping? Call again.\n");
259 select(1, &rfds, NULL, NULL, &tv);
260 } while (!FD_ISSET(0, &rfds));
265 /* At this point, there's input, so fetch it.
266 * (There's a hole in the bucket...)
276 if (((a != 4) && (a != 13) && (a != 8) && (a != NEXT_KEY) && (a != STOP_KEY))
277 && ((a < 32) || (a > 126)))
285 { /* Returns 1 for yes, 0 for no */
301 /* Returns 1 for yes, 0 for no, arg is default value */
324 /* Gets a line from the terminal */
325 /* string == Pointer to string buffer */
326 /* lim == Maximum length - if negative, no-show */
327 void getline(char *string, int lim)
341 if ((a == 8) && (strlen(string) == 0))
343 if ((a != 13) && (a != 8) && (strlen(string) == lim))
345 if ((a == 8) && (string[0] != 0)) {
346 string[strlen(string) - 1] = 0;
352 if ((a == 13) || (a == 10)) {
372 * strprompt() - prompt for a string, print the existing value and
373 * allow the user to press return to keep it...
375 void strprompt(char *prompt, char *str, int len)
380 printf("%s ", prompt);
383 color(BRIGHT_MAGENTA);
397 * boolprompt() - prompt for a yes/no, print the existing value and
398 * allow the user to press return to keep it...
400 int boolprompt(char *prompt, int prev_val)
405 printf("%s ", prompt);
408 color(BRIGHT_MAGENTA);
409 printf("%s", (prev_val ? "Yes" : "No"));
413 r = (yesno_d(prev_val));
419 * intprompt() - like strprompt(), except for an integer
420 * (note that it RETURNS the new value!)
422 int intprompt(char *prompt, int ival, int imin, int imax)
430 snprintf(buf, sizeof buf, "%d", i);
431 strprompt(prompt, buf, 15);
433 for (p=0; p<strlen(buf); ++p) {
434 if (!isdigit(buf[p])) i = imin - 1;
437 printf("*** Must be no less than %d.\n", imin);
439 printf("*** Must be no more than %d.\n", imax);
440 } while ((i < imin) || (i > imax));
445 * newprompt() - prompt for a string with no existing value
446 * (clears out string buffer first)
448 void newprompt(char *prompt, char *str, int len)
450 color(BRIGHT_MAGENTA);
451 printf("%s", prompt);
459 { /* returns a lower case value */
468 * parse the citadel.rc file
470 void load_command_set(void)
475 struct citcmd *lastcmd = NULL;
480 /* first, set up some defaults for non-required variables */
482 strcpy(editor_path, "");
483 strcpy(printcmd, "");
484 strcpy(rc_username, "");
485 strcpy(rc_password, "");
488 rc_allow_attachments = 0;
489 strcpy(rc_exp_cmd, "");
490 rc_display_message_numbers = 0;
491 rc_force_mail_prompts = 0;
493 strcpy(rc_url_cmd, "");
495 /* now try to open the citadel.rc file */
498 if (getenv("HOME") != NULL) {
499 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
500 ccfile = fopen(buf, "r");
502 if (ccfile == NULL) {
503 ccfile = fopen("/usr/local/lib/citadel.rc", "r");
505 if (ccfile == NULL) {
506 snprintf(buf, sizeof buf, "%s/citadel.rc", BBSDIR);
507 ccfile = fopen(buf, "r");
509 if (ccfile == NULL) {
510 ccfile = fopen("./citadel.rc", "r");
512 if (ccfile == NULL) {
513 perror("commands: cannot open citadel.rc");
516 while (fgets(buf, 256, ccfile) != NULL) {
517 while ((strlen(buf) > 0) ? (isspace(buf[strlen(buf) - 1])) : 0)
518 buf[strlen(buf) - 1] = 0;
520 if (!struncmp(buf, "editor=", 7))
521 strcpy(editor_path, &buf[7]);
523 if (!struncmp(buf, "printcmd=", 9))
524 strcpy(printcmd, &buf[9]);
526 if (!struncmp(buf, "expcmd=", 7))
527 strcpy(rc_exp_cmd, &buf[7]);
529 if (!struncmp(buf, "local_screen_dimensions=", 24))
530 have_xterm = (char) atoi(&buf[24]);
532 if (!struncmp(buf, "use_floors=", 11)) {
533 if (!strucmp(&buf[11], "yes"))
534 rc_floor_mode = RC_YES;
535 if (!strucmp(&buf[11], "no"))
536 rc_floor_mode = RC_NO;
537 if (!strucmp(&buf[11], "default"))
538 rc_floor_mode = RC_DEFAULT;
540 if (!struncmp(buf, "beep=", 5)) {
541 rc_exp_beep = atoi(&buf[5]);
543 if (!struncmp(buf, "allow_attachments=", 18)) {
544 rc_allow_attachments = atoi(&buf[18]);
546 if (!struncmp(buf, "display_message_numbers=", 24)) {
547 rc_display_message_numbers = atoi(&buf[24]);
549 if (!struncmp(buf, "force_mail_prompts=", 19)) {
550 rc_force_mail_prompts = atoi(&buf[19]);
552 if (!struncmp(buf, "ansi_color=", 11)) {
553 if (!struncmp(&buf[11], "on", 2))
555 if (!struncmp(&buf[11], "auto", 4))
556 rc_ansi_color = 2; /* autodetect */
557 if (!struncmp(&buf[11], "user", 4))
558 rc_ansi_color = 3; /* user config */
560 if (!struncmp(buf, "username=", 9))
561 strcpy(rc_username, &buf[9]);
563 if (!struncmp(buf, "password=", 9))
564 strcpy(rc_password, &buf[9]);
566 if (!struncmp(buf, "urlcmd=", 7))
567 strcpy(rc_url_cmd, &buf[7]);
569 if (!struncmp(buf, "cmd=", 4)) {
570 strcpy(buf, &buf[4]);
572 cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
574 cptr->c_cmdnum = atoi(buf);
575 for (d = strlen(buf); d >= 0; --d)
578 strcpy(buf, &buf[b + 1]);
580 cptr->c_axlevel = atoi(buf);
581 for (d = strlen(buf); d >= 0; --d)
584 strcpy(buf, &buf[b + 1]);
586 for (a = 0; a < 5; ++a)
587 cptr->c_keys[a][0] = 0;
591 buf[strlen(buf) + 1] = 0;
592 while (strlen(buf) > 0) {
594 for (d = strlen(buf); d >= 0; --d)
597 strncpy(cptr->c_keys[a], buf, b);
598 cptr->c_keys[a][b] = 0;
600 strcpy(buf, &buf[b + 1]);
610 lastcmd->next = cptr;
620 * return the key associated with a command
622 char keycmd(char *cmdstr)
626 for (a = 0; a < strlen(cmdstr); ++a)
627 if (cmdstr[a] == '&')
628 return (tolower(cmdstr[a + 1]));
634 * Output the string from a key command without the ampersand
635 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
637 char *cmd_expand(char *strbuf, int mode)
645 for (a = 0; a < strlen(exp); ++a) {
646 if (strbuf[a] == '&') {
649 strcpy(&exp[a], &exp[a + 1]);
653 strcpy(buf, &exp[a + 2]);
659 if (!strncmp(&exp[a], "^r", 2)) {
661 strcpy(&exp[a], room_name);
662 strcat(exp, &buf[a + 2]);
664 if (!strncmp(&exp[a], "^c", 2)) {
666 strcpy(&exp[a + 1], &exp[a + 2]);
676 * Comparison function to determine if entered commands match a
677 * command loaded from the config file.
679 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
690 for (a = 0; a < ncomp; ++a) {
691 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
692 || (cptr->c_axlevel > cmdax))
700 * This function returns 1 if a given command requires a string input
702 int requires_string(struct citcmd *cptr, int ncomp)
707 strcpy(buf, cptr->c_keys[ncomp - 1]);
708 for (a = 0; a < strlen(buf); ++a) {
717 * Input a command at the main prompt.
718 * This function returns an integer command number. If the command prompts
719 * for a string then it is placed in the supplied buffer.
721 int getcmd(char *argbuf)
732 /* Switch color support on or off if we're in user mode */
733 if (rc_ansi_color == 3) {
734 if (userflags & US_COLOR)
739 /* if we're running in idiot mode, display a cute little menu */
740 IFNEXPERT formout("mainmenu");
742 print_express(); /* print express messages if there are any */
745 for (a = 0; a < 5; ++a)
747 /* now the room prompt... */
750 printf("\n%s", room_name);
752 printf("%c ", room_prompt(room_flags));
759 /* Handle the backspace key, but only if there's something
760 * to backspace over...
762 if ((ch == 8) && (cmdpos > 0)) {
763 back(cmdspaces[cmdpos - 1] + 1);
767 /* Spacebar invokes "lazy traversal" commands */
768 if ((ch == 32) && (cmdpos == 0)) {
769 this_lazy_cmd = next_lazy_cmd;
770 if (this_lazy_cmd == 13)
772 if (this_lazy_cmd == 5)
774 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
775 if (cptr->c_cmdnum == this_lazy_cmd) {
776 for (a = 0; a < 5; ++a)
777 if (cptr->c_keys[a][0] != 0)
778 printf("%s ", cmd_expand(
779 cptr->c_keys[a], 0));
781 return (this_lazy_cmd);
785 return (this_lazy_cmd);
787 /* Otherwise, process the command */
788 cmdbuf[cmdpos] = tolower(ch);
790 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
791 if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
793 printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
794 cmdspaces[cmdpos] = strlen(
795 cmd_expand(cptr->c_keys[cmdpos], 0));
797 if ((cptr->c_keys[cmdpos + 1]) != 0)
803 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
804 if (cmdmatch(cmdbuf, cptr, 5)) {
805 /* We've found our command. */
806 if (requires_string(cptr, cmdpos)) {
812 /* If this command is one that changes rooms,
813 * then the next lazy-command (space bar)
814 * should be "read new" instead of "goto"
816 if ((cptr->c_cmdnum == 5)
817 || (cptr->c_cmdnum == 6)
818 || (cptr->c_cmdnum == 47)
819 || (cptr->c_cmdnum == 52)
820 || (cptr->c_cmdnum == 16)
821 || (cptr->c_cmdnum == 20))
824 return (cptr->c_cmdnum);
830 printf("\rOne of ... \n");
831 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
832 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
833 for (a = 0; a < 5; ++a) {
834 printf("%s ", cmd_expand(cptr->c_keys[a], 1));
840 printf("\n%s%c ", room_name, room_prompt(room_flags));
842 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
843 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
844 for (a = 0; a < cmdpos; ++a) {
846 cmd_expand(cptr->c_keys[a], 0));
861 * set tty modes. commands are:
863 * 0 - set to bbs mode, intr/quit disabled
864 * 1 - set to bbs mode, intr/quit enabled
865 * 2 - save current settings for later restoral
866 * 3 - restore saved settings
868 #ifdef HAVE_TERMIOS_H
869 void sttybbs(int cmd)
870 { /* SysV version of sttybbs() */
872 static struct termios saved_settings;
873 static int last_cmd = 0;
880 if ((cmd == 0) || (cmd == 1)) {
882 live.c_iflag = ISTRIP | IXON | IXANY;
883 live.c_oflag = OPOST | ONLCR;
884 live.c_lflag = ISIG | NOFLSH;
886 if (cmd == SB_YES_INTR) {
887 live.c_cc[VINTR] = NEXT_KEY;
888 live.c_cc[VQUIT] = STOP_KEY;
889 signal(SIGINT, *sighandler);
890 signal(SIGQUIT, *sighandler);
892 signal(SIGINT, SIG_IGN);
893 signal(SIGQUIT, SIG_IGN);
894 live.c_cc[VINTR] = (-1);
895 live.c_cc[VQUIT] = (-1);
898 /* do we even need this stuff anymore? */
900 live.c_cc[VERASE] = 8;
901 live.c_cc[VKILL] = 24;
903 live.c_cc[VEOL] = 255;
904 live.c_cc[VEOL2] = 0;
905 live.c_cc[VSTART] = 0;
906 tcsetattr(0, TCSADRAIN, &live);
909 tcgetattr(0, &saved_settings);
912 tcsetattr(0, TCSADRAIN, &saved_settings);
916 void sttybbs(int cmd)
917 { /* BSD version of sttybbs() */
919 static struct sgttyb saved_settings;
921 if ((cmd == 0) || (cmd == 1)) {
923 live.sg_flags |= CBREAK;
924 live.sg_flags |= CRMOD;
925 live.sg_flags |= NL1;
926 live.sg_flags &= ~ECHO;
928 live.sg_flags |= NOFLSH;
932 gtty(0, &saved_settings);
935 stty(0, &saved_settings);
942 * display_help() - help file viewer
944 void display_help(char *name)
951 * fmout() - Citadel text formatter and paginator
953 int fmout(int width, FILE * fp, char pagin, int height, int starting_lp, char subst)
954 /* screen width to use */
955 /* file to read from, or NULL to read from server */
956 /* nonzero if we should use the paginator */
957 /* screen height to use */
958 /* starting value for lines_printed, -1 for global */
959 /* nonzero if we should use hypertext mode */
967 num_urls = 0; /* Start with a clean slate of embedded URL's */
969 if (starting_lp >= 0) {
970 lines_printed = starting_lp;
975 c = 1; /* c is the current pos */
980 FMTA: while ((eof_flag == 0) && (strlen(buffer) < 126)) {
984 if (fp != NULL) { /* read from file */
989 buffer[strlen(buffer) + 1] = 0;
990 buffer[strlen(buffer)] = a;
992 } else { /* read from server */
994 serv_gets(&buffer[d]);
995 while ((!isspace(buffer[d])) && (isspace(buffer[strlen(buffer) - 1])))
996 buffer[strlen(buffer) - 1] = 0;
997 if (!strcmp(&buffer[d], "000")) {
1000 while (isspace(buffer[strlen(buffer) - 1]))
1001 buffer[strlen(buffer) - 1] = 0;
1009 if ( (!struncmp(buffer, "http://", 7))
1010 || (!struncmp(buffer, "ftp://", 6)) ) {
1011 safestrncpy(urls[num_urls], buffer, 255);
1012 for (a=0; a<strlen(urls[num_urls]); ++a) {
1013 b = urls[num_urls][a];
1014 if ( (b==' ') || (b==')') || (b=='>') || (b==10)
1015 || (b==13) || (b==9) || (b=='\"') )
1016 urls[num_urls][a] = 0;
1021 buffer[strlen(buffer) + 1] = 0;
1023 strcpy(buffer, &buffer[1]);
1030 if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
1032 if (((old == 13) || (old == 10)) && (isspace(real))) {
1035 lines_printed = checkpagin(lines_printed, pagin, height);
1042 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
1043 printf("\n%s", aaa);
1047 lines_printed = checkpagin(lines_printed, pagin, height);
1054 if ((strlen(aaa) + c) > (width - 5)) {
1058 lines_printed = checkpagin(lines_printed, pagin, height);
1062 c = c + strlen(aaa);
1066 if ((a == 13) || (a == 10)) {
1067 printf("%s\n", aaa);
1070 lines_printed = checkpagin(lines_printed, pagin, height);
1076 /* signal caught; drain the server */
1079 } while (strcmp(aaa, "000"));
1081 FMTEND:printf("\n");
1083 lines_printed = checkpagin(lines_printed, pagin, height);
1089 * support ANSI color if defined
1091 void color(int colornum)
1093 static int is_bold = 0;
1094 static int hold_color, current_color;
1096 if (colornum == COLOR_PUSH) {
1097 hold_color = current_color;
1101 if (colornum == COLOR_POP) {
1106 current_color = colornum;
1108 /* Don't switch to black or white explicitly as this confuses
1109 * black-on-white terminals. Instead, output the "original
1112 if ((colornum & 7) == DIM_WHITE || (colornum & 7) == DIM_BLACK)
1113 printf("\033[39;49m");
1115 printf("\033[3%dm", (colornum & 7));
1117 if ((colornum >= 8) && (is_bold == 0)) {
1120 } else if ((colornum < 8) && (is_bold == 1)) {
1129 void cls(int colornum)
1132 printf("\033[4%dm\033[2J\033[H\033[0m", colornum);
1140 * Detect whether ANSI color is available (answerback)
1142 void send_ansi_detect(void)
1144 if (rc_ansi_color == 2) {
1151 void look_for_ansi(void)
1159 if (rc_ansi_color == 0) {
1161 } else if (rc_ansi_color == 1) {
1163 } else if (rc_ansi_color == 2) {
1165 /* otherwise, do the auto-detect */
1170 if ((now - AnsiDetect) < 2)
1179 select(1, &rfds, NULL, NULL, &tv);
1180 if (FD_ISSET(0, &rfds)) {
1181 abuf[strlen(abuf) + 1] = 0;
1182 read(0, &abuf[strlen(abuf)], 1);
1184 } while (FD_ISSET(0, &rfds));
1186 for (a = 0; a < strlen(abuf); ++a) {
1187 if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1188 && (abuf[a + 2] == '?')) {
1197 * Display key options (highlight hotkeys inside angle brackets)
1199 void keyopt(char *buf) {
1203 for (i=0; i<strlen(buf); ++i) {
1205 putc(buf[i], stdout);
1206 color(BRIGHT_MAGENTA);
1211 putc(buf[i], stdout);
1220 * Present a key-menu line choice type of thing
1222 char keymenu(char *menuprompt, char *menustring) {
1228 int display_prompt = 1;
1230 choices = num_tokens(menustring, '|');
1232 if (menuprompt != NULL) do_prompt = 1;
1233 if (menuprompt != NULL) if (strlen(menuprompt)==0) do_prompt = 0;
1236 if (display_prompt) {
1238 printf("%s ", menuprompt);
1241 for (i=0; i<choices; ++i) {
1242 extract(buf, menustring, i);
1252 if ( (do_prompt) && (ch=='?') ) {
1253 printf("\rOne of... ");
1255 for (i=0; i<choices; ++i) {
1256 extract(buf, menustring, i);
1265 for (i=0; i<choices; ++i) {
1266 extract(buf, menustring, i);
1267 for (c=1; c<strlen(buf); ++c) {
1268 if ( (ch == tolower(buf[c]))
1270 && (buf[c+1]=='>') ) {
1271 for (a=0; a<strlen(buf); ++a) {
1272 if ( (a!=(c-1)) && (a!=(c+1))) {
1273 putc(buf[a], stdout);