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 void really_do_keepalive(void) {
132 if (keepalives_enabled != KA_NO) {
134 if (keepalives_enabled == KA_YES) {
138 if (ok_to_interrupt == 1) {
139 printf("\r%64s\r", "");
141 printf("%s%c ", room_name,
142 room_prompt(room_flags));
150 /* threaded nonblocking keepalive stuff starts here. I'm going for a simple
151 encapsulated interface; in theory there should be no need to touch these
152 globals outside of the async_ka_* functions. */
154 #ifdef THREADED_CLIENT
155 static pthread_t ka_thr_handle;
156 static int ka_thr_active = 0;
157 static pthread_mutex_t ka_thr_mutex = PTHREAD_MUTEX_INITIALIZER;
158 static int async_ka_enabled = 0;
160 static void *ka_thread(void *arg)
162 really_do_keepalive();
163 pthread_detach(ka_thr_handle);
168 /* start up a thread to handle a keepalive in the background */
169 static void async_ka_exec(void)
174 pthread_mutex_lock(&ka_thr_mutex);
176 if (!ka_thr_active) {
178 if (pthread_create(&ka_thr_handle, NULL, ka_thread, NULL)) {
179 perror("pthread_create");
183 pthread_mutex_unlock(&ka_thr_mutex);
185 #endif /* THREADED_CLIENT */
187 static void do_keepalive(void)
189 static time_t idlet = 0;
193 if ((now - idlet) < ((long) S_KEEPALIVE))
197 /* Do a space-backspace to keep telnet sessions from idling out */
201 #ifdef THREADED_CLIENT
202 if (async_ka_enabled)
206 really_do_keepalive();
210 /* Now the actual async-keepalve API that we expose to higher levels:
211 async_ka_start() and async_ka_end(). These do nothing when we don't have
212 threading enabled, so we avoid sprinkling ifdef's throughout the code. */
214 /* wait for a background keepalive to complete. this must be done before
215 attempting any further server requests! */
216 void async_ka_end(void)
218 #ifdef THREADED_CLIENT
220 pthread_join(ka_thr_handle, NULL);
226 /* tell do_keepalive() that keepalives are asynchronous. */
227 void async_ka_start(void)
229 #ifdef THREADED_CLIENT
236 { /* get a character from the keyboard, with */
237 int a; /* the watchdog timer in effect if necessary */
240 time_t start_time, now;
248 /* This loop waits for keyboard input. If the keepalive
249 * timer expires, it sends a keepalive to the server if
250 * necessary and then waits again.
257 tv.tv_sec = S_KEEPALIVE;
261 if (((now - start_time) > SLEEPING)
262 && (SLEEPING != 0) && (getppid() == 1)) {
263 printf("Sleeping? Call again.\n");
266 select(1, &rfds, NULL, NULL, &tv);
267 } while (!FD_ISSET(0, &rfds));
272 /* At this point, there's input, so fetch it.
273 * (There's a hole in the bucket...)
283 if (((a != 4) && (a != 13) && (a != 8) && (a != NEXT_KEY) && (a != STOP_KEY))
284 && ((a < 32) || (a > 126)))
292 { /* Returns 1 for yes, 0 for no */
308 /* Returns 1 for yes, 0 for no, arg is default value */
331 /* Gets a line from the terminal */
332 /* string == Pointer to string buffer */
333 /* lim == Maximum length - if negative, no-show */
334 void getline(char *string, int lim)
348 if ((a == 8) && (strlen(string) == 0))
350 if ((a != 13) && (a != 8) && (strlen(string) == lim))
352 if ((a == 8) && (string[0] != 0)) {
353 string[strlen(string) - 1] = 0;
359 if ((a == 13) || (a == 10)) {
379 * strprompt() - prompt for a string, print the existing value and
380 * allow the user to press return to keep it...
382 void strprompt(char *prompt, char *str, int len)
387 printf("%s ", prompt);
390 color(BRIGHT_MAGENTA);
404 * boolprompt() - prompt for a yes/no, print the existing value and
405 * allow the user to press return to keep it...
407 int boolprompt(char *prompt, int prev_val)
412 printf("%s ", prompt);
415 color(BRIGHT_MAGENTA);
416 printf("%s", (prev_val ? "Yes" : "No"));
420 r = (yesno_d(prev_val));
426 * intprompt() - like strprompt(), except for an integer
427 * (note that it RETURNS the new value!)
429 int intprompt(char *prompt, int ival, int imin, int imax)
437 snprintf(buf, sizeof buf, "%d", i);
438 strprompt(prompt, buf, 15);
440 for (p=0; p<strlen(buf); ++p) {
441 if (!isdigit(buf[p])) i = imin - 1;
444 printf("*** Must be no less than %d.\n", imin);
446 printf("*** Must be no more than %d.\n", imax);
447 } while ((i < imin) || (i > imax));
452 * newprompt() - prompt for a string with no existing value
453 * (clears out string buffer first)
455 void newprompt(char *prompt, char *str, int len)
457 color(BRIGHT_MAGENTA);
458 printf("%s", prompt);
466 { /* returns a lower case value */
475 * parse the citadel.rc file
477 void load_command_set(void)
482 struct citcmd *lastcmd = NULL;
487 /* first, set up some defaults for non-required variables */
489 strcpy(editor_path, "");
490 strcpy(printcmd, "");
491 strcpy(rc_username, "");
492 strcpy(rc_password, "");
495 rc_allow_attachments = 0;
496 strcpy(rc_exp_cmd, "");
497 rc_display_message_numbers = 0;
498 rc_force_mail_prompts = 0;
500 strcpy(rc_url_cmd, "");
502 /* now try to open the citadel.rc file */
505 if (getenv("HOME") != NULL) {
506 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
507 ccfile = fopen(buf, "r");
509 if (ccfile == NULL) {
510 ccfile = fopen("/usr/local/lib/citadel.rc", "r");
512 if (ccfile == NULL) {
513 snprintf(buf, sizeof buf, "%s/citadel.rc", BBSDIR);
514 ccfile = fopen(buf, "r");
516 if (ccfile == NULL) {
517 ccfile = fopen("./citadel.rc", "r");
519 if (ccfile == NULL) {
520 perror("commands: cannot open citadel.rc");
523 while (fgets(buf, 256, ccfile) != NULL) {
524 while ((strlen(buf) > 0) ? (isspace(buf[strlen(buf) - 1])) : 0)
525 buf[strlen(buf) - 1] = 0;
527 if (!struncmp(buf, "editor=", 7))
528 strcpy(editor_path, &buf[7]);
530 if (!struncmp(buf, "printcmd=", 9))
531 strcpy(printcmd, &buf[9]);
533 if (!struncmp(buf, "expcmd=", 7))
534 strcpy(rc_exp_cmd, &buf[7]);
536 if (!struncmp(buf, "local_screen_dimensions=", 24))
537 have_xterm = (char) atoi(&buf[24]);
539 if (!struncmp(buf, "use_floors=", 11)) {
540 if (!strucmp(&buf[11], "yes"))
541 rc_floor_mode = RC_YES;
542 if (!strucmp(&buf[11], "no"))
543 rc_floor_mode = RC_NO;
544 if (!strucmp(&buf[11], "default"))
545 rc_floor_mode = RC_DEFAULT;
547 if (!struncmp(buf, "beep=", 5)) {
548 rc_exp_beep = atoi(&buf[5]);
550 if (!struncmp(buf, "allow_attachments=", 18)) {
551 rc_allow_attachments = atoi(&buf[18]);
553 if (!struncmp(buf, "display_message_numbers=", 24)) {
554 rc_display_message_numbers = atoi(&buf[24]);
556 if (!struncmp(buf, "force_mail_prompts=", 19)) {
557 rc_force_mail_prompts = atoi(&buf[19]);
559 if (!struncmp(buf, "ansi_color=", 11)) {
560 if (!struncmp(&buf[11], "on", 2))
562 if (!struncmp(&buf[11], "auto", 4))
563 rc_ansi_color = 2; /* autodetect */
564 if (!struncmp(&buf[11], "user", 4))
565 rc_ansi_color = 3; /* user config */
567 if (!struncmp(buf, "username=", 9))
568 strcpy(rc_username, &buf[9]);
570 if (!struncmp(buf, "password=", 9))
571 strcpy(rc_password, &buf[9]);
573 if (!struncmp(buf, "urlcmd=", 7))
574 strcpy(rc_url_cmd, &buf[7]);
576 if (!struncmp(buf, "cmd=", 4)) {
577 strcpy(buf, &buf[4]);
579 cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
581 cptr->c_cmdnum = atoi(buf);
582 for (d = strlen(buf); d >= 0; --d)
585 strcpy(buf, &buf[b + 1]);
587 cptr->c_axlevel = atoi(buf);
588 for (d = strlen(buf); d >= 0; --d)
591 strcpy(buf, &buf[b + 1]);
593 for (a = 0; a < 5; ++a)
594 cptr->c_keys[a][0] = 0;
598 buf[strlen(buf) + 1] = 0;
599 while (strlen(buf) > 0) {
601 for (d = strlen(buf); d >= 0; --d)
604 strncpy(cptr->c_keys[a], buf, b);
605 cptr->c_keys[a][b] = 0;
607 strcpy(buf, &buf[b + 1]);
617 lastcmd->next = cptr;
627 * return the key associated with a command
629 char keycmd(char *cmdstr)
633 for (a = 0; a < strlen(cmdstr); ++a)
634 if (cmdstr[a] == '&')
635 return (tolower(cmdstr[a + 1]));
641 * Output the string from a key command without the ampersand
642 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
644 char *cmd_expand(char *strbuf, int mode)
652 for (a = 0; a < strlen(exp); ++a) {
653 if (strbuf[a] == '&') {
656 strcpy(&exp[a], &exp[a + 1]);
660 strcpy(buf, &exp[a + 2]);
666 if (!strncmp(&exp[a], "^r", 2)) {
668 strcpy(&exp[a], room_name);
669 strcat(exp, &buf[a + 2]);
671 if (!strncmp(&exp[a], "^c", 2)) {
673 strcpy(&exp[a + 1], &exp[a + 2]);
683 * Comparison function to determine if entered commands match a
684 * command loaded from the config file.
686 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
697 for (a = 0; a < ncomp; ++a) {
698 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
699 || (cptr->c_axlevel > cmdax))
707 * This function returns 1 if a given command requires a string input
709 int requires_string(struct citcmd *cptr, int ncomp)
714 strcpy(buf, cptr->c_keys[ncomp - 1]);
715 for (a = 0; a < strlen(buf); ++a) {
724 * Input a command at the main prompt.
725 * This function returns an integer command number. If the command prompts
726 * for a string then it is placed in the supplied buffer.
728 int getcmd(char *argbuf)
739 /* Switch color support on or off if we're in user mode */
740 if (rc_ansi_color == 3) {
741 if (userflags & US_COLOR)
746 /* if we're running in idiot mode, display a cute little menu */
747 IFNEXPERT formout("mainmenu");
749 print_express(); /* print express messages if there are any */
752 for (a = 0; a < 5; ++a)
754 /* now the room prompt... */
757 printf("\n%s", room_name);
759 printf("%c ", room_prompt(room_flags));
766 /* Handle the backspace key, but only if there's something
767 * to backspace over...
769 if ((ch == 8) && (cmdpos > 0)) {
770 back(cmdspaces[cmdpos - 1] + 1);
774 /* Spacebar invokes "lazy traversal" commands */
775 if ((ch == 32) && (cmdpos == 0)) {
776 this_lazy_cmd = next_lazy_cmd;
777 if (this_lazy_cmd == 13)
779 if (this_lazy_cmd == 5)
781 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
782 if (cptr->c_cmdnum == this_lazy_cmd) {
783 for (a = 0; a < 5; ++a)
784 if (cptr->c_keys[a][0] != 0)
785 printf("%s ", cmd_expand(
786 cptr->c_keys[a], 0));
788 return (this_lazy_cmd);
792 return (this_lazy_cmd);
794 /* Otherwise, process the command */
795 cmdbuf[cmdpos] = tolower(ch);
797 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
798 if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
800 printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
801 cmdspaces[cmdpos] = strlen(
802 cmd_expand(cptr->c_keys[cmdpos], 0));
804 if ((cptr->c_keys[cmdpos + 1]) != 0)
810 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
811 if (cmdmatch(cmdbuf, cptr, 5)) {
812 /* We've found our command. */
813 if (requires_string(cptr, cmdpos)) {
819 /* If this command is one that changes rooms,
820 * then the next lazy-command (space bar)
821 * should be "read new" instead of "goto"
823 if ((cptr->c_cmdnum == 5)
824 || (cptr->c_cmdnum == 6)
825 || (cptr->c_cmdnum == 47)
826 || (cptr->c_cmdnum == 52)
827 || (cptr->c_cmdnum == 16)
828 || (cptr->c_cmdnum == 20))
831 return (cptr->c_cmdnum);
837 printf("\rOne of ... \n");
838 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
839 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
840 for (a = 0; a < 5; ++a) {
841 printf("%s ", cmd_expand(cptr->c_keys[a], 1));
847 printf("\n%s%c ", room_name, room_prompt(room_flags));
849 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
850 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
851 for (a = 0; a < cmdpos; ++a) {
853 cmd_expand(cptr->c_keys[a], 0));
868 * set tty modes. commands are:
870 * 0 - set to bbs mode, intr/quit disabled
871 * 1 - set to bbs mode, intr/quit enabled
872 * 2 - save current settings for later restoral
873 * 3 - restore saved settings
875 #ifdef HAVE_TERMIOS_H
876 void sttybbs(int cmd)
877 { /* SysV version of sttybbs() */
879 static struct termios saved_settings;
880 static int last_cmd = 0;
887 if ((cmd == 0) || (cmd == 1)) {
889 live.c_iflag = ISTRIP | IXON | IXANY;
890 live.c_oflag = OPOST | ONLCR;
891 live.c_lflag = ISIG | NOFLSH;
893 if (cmd == SB_YES_INTR) {
894 live.c_cc[VINTR] = NEXT_KEY;
895 live.c_cc[VQUIT] = STOP_KEY;
896 signal(SIGINT, *sighandler);
897 signal(SIGQUIT, *sighandler);
899 signal(SIGINT, SIG_IGN);
900 signal(SIGQUIT, SIG_IGN);
901 live.c_cc[VINTR] = (-1);
902 live.c_cc[VQUIT] = (-1);
905 /* do we even need this stuff anymore? */
907 live.c_cc[VERASE] = 8;
908 live.c_cc[VKILL] = 24;
910 live.c_cc[VEOL] = 255;
911 live.c_cc[VEOL2] = 0;
912 live.c_cc[VSTART] = 0;
913 tcsetattr(0, TCSADRAIN, &live);
916 tcgetattr(0, &saved_settings);
919 tcsetattr(0, TCSADRAIN, &saved_settings);
923 void sttybbs(int cmd)
924 { /* BSD version of sttybbs() */
926 static struct sgttyb saved_settings;
928 if ((cmd == 0) || (cmd == 1)) {
930 live.sg_flags |= CBREAK;
931 live.sg_flags |= CRMOD;
932 live.sg_flags |= NL1;
933 live.sg_flags &= ~ECHO;
935 live.sg_flags |= NOFLSH;
939 gtty(0, &saved_settings);
942 stty(0, &saved_settings);
949 * display_help() - help file viewer
951 void display_help(char *name)
958 * fmout() - Citadel text formatter and paginator
960 int fmout(int width, FILE * fp, char pagin, int height, int starting_lp, char subst)
961 /* screen width to use */
962 /* file to read from, or NULL to read from server */
963 /* nonzero if we should use the paginator */
964 /* screen height to use */
965 /* starting value for lines_printed, -1 for global */
966 /* nonzero if we should use hypertext mode */
974 num_urls = 0; /* Start with a clean slate of embedded URL's */
976 if (starting_lp >= 0) {
977 lines_printed = starting_lp;
982 c = 1; /* c is the current pos */
987 FMTA: while ((eof_flag == 0) && (strlen(buffer) < 126)) {
991 if (fp != NULL) { /* read from file */
996 buffer[strlen(buffer) + 1] = 0;
997 buffer[strlen(buffer)] = a;
999 } else { /* read from server */
1001 serv_gets(&buffer[d]);
1002 while ((!isspace(buffer[d])) && (isspace(buffer[strlen(buffer) - 1])))
1003 buffer[strlen(buffer) - 1] = 0;
1004 if (!strcmp(&buffer[d], "000")) {
1007 while (isspace(buffer[strlen(buffer) - 1]))
1008 buffer[strlen(buffer) - 1] = 0;
1016 if ( (!struncmp(buffer, "http://", 7))
1017 || (!struncmp(buffer, "ftp://", 6)) ) {
1018 safestrncpy(urls[num_urls], buffer, 255);
1019 for (a=0; a<strlen(urls[num_urls]); ++a) {
1020 b = urls[num_urls][a];
1021 if ( (b==' ') || (b==')') || (b=='>') || (b==10)
1022 || (b==13) || (b==9) || (b=='\"') )
1023 urls[num_urls][a] = 0;
1028 buffer[strlen(buffer) + 1] = 0;
1030 strcpy(buffer, &buffer[1]);
1037 if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
1039 if (((old == 13) || (old == 10)) && (isspace(real))) {
1042 lines_printed = checkpagin(lines_printed, pagin, height);
1049 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
1050 printf("\n%s", aaa);
1054 lines_printed = checkpagin(lines_printed, pagin, height);
1061 if ((strlen(aaa) + c) > (width - 5)) {
1065 lines_printed = checkpagin(lines_printed, pagin, height);
1069 c = c + strlen(aaa);
1073 if ((a == 13) || (a == 10)) {
1074 printf("%s\n", aaa);
1077 lines_printed = checkpagin(lines_printed, pagin, height);
1083 /* signal caught; drain the server */
1086 } while (strcmp(aaa, "000"));
1088 FMTEND:printf("\n");
1090 lines_printed = checkpagin(lines_printed, pagin, height);
1096 * support ANSI color if defined
1098 void color(int colornum)
1100 static int is_bold = 0;
1101 static int hold_color, current_color;
1103 if (colornum == COLOR_PUSH) {
1104 hold_color = current_color;
1108 if (colornum == COLOR_POP) {
1113 current_color = colornum;
1115 printf("\033[3%dm", (colornum % 8));
1116 if ((colornum >= 8) && (is_bold == 0)) {
1119 } else if ((colornum < 8) && (is_bold == 1)) {
1127 void cls(int colornum)
1130 printf("\033[4%dm\033[2J\033[H\033[0m", colornum);
1137 * Detect whether ANSI color is available (answerback)
1139 void send_ansi_detect(void)
1141 if (rc_ansi_color == 2) {
1148 void look_for_ansi(void)
1156 if (rc_ansi_color == 0) {
1158 } else if (rc_ansi_color == 1) {
1160 } else if (rc_ansi_color == 2) {
1162 /* otherwise, do the auto-detect */
1167 if ((now - AnsiDetect) < 2)
1176 select(1, &rfds, NULL, NULL, &tv);
1177 if (FD_ISSET(0, &rfds)) {
1178 abuf[strlen(abuf) + 1] = 0;
1179 read(0, &abuf[strlen(abuf)], 1);
1181 } while (FD_ISSET(0, &rfds));
1183 for (a = 0; a < strlen(abuf); ++a) {
1184 if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1185 && (abuf[a + 2] == '?')) {
1194 * Display key options (highlight hotkeys inside angle brackets)
1196 void keyopt(char *buf) {
1200 for (i=0; i<strlen(buf); ++i) {
1202 putc(buf[i], stdout);
1203 color(BRIGHT_MAGENTA);
1208 putc(buf[i], stdout);
1217 * Present a key-menu line choice type of thing
1219 char keymenu(char *menuprompt, char *menustring) {
1225 int display_prompt = 1;
1227 choices = num_tokens(menustring, '|');
1229 if (menuprompt != NULL) do_prompt = 1;
1230 if (menuprompt != NULL) if (strlen(menuprompt)==0) do_prompt = 0;
1233 if (display_prompt) {
1235 printf("%s ", menuprompt);
1238 for (i=0; i<choices; ++i) {
1239 extract(buf, menustring, i);
1249 if ( (do_prompt) && (ch=='?') ) {
1250 printf("\rOne of... ");
1252 for (i=0; i<choices; ++i) {
1253 extract(buf, menustring, i);
1262 for (i=0; i<choices; ++i) {
1263 extract(buf, menustring, i);
1264 for (c=1; c<strlen(buf); ++c) {
1265 if ( (ch == tolower(buf[c]))
1267 && (buf[c+1]=='>') ) {
1268 for (a=0; a<strlen(buf); ++a) {
1269 if ( (a!=(c-1)) && (a!=(c+1))) {
1270 putc(buf[a], stdout);