* citadel.c, commands.c: comment out cls() as this wasn't called if
[citadel.git] / citadel / commands.c
1 /*
2  * $Id$
3  *
4  * This file contains functions which implement parts of the
5  * text-mode user interface.
6  *
7  */
8
9 #include "sysdep.h"
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <sys/types.h>
17 #include <sys/time.h>
18
19 #ifdef HAVE_TERMIOS_H
20 #include <termios.h>
21 #else
22 #include <sgtty.h>
23 #endif
24
25 #ifdef HAVE_SYS_SELECT_H
26 #include <sys/select.h>
27 #endif
28
29 #ifdef THREADED_CLIENT
30 #include <pthread.h>
31 #endif
32
33 #include <signal.h>
34 #include <errno.h>
35 #include <stdarg.h>
36 #include "citadel.h"
37 #include "commands.h"
38 #include "messages.h"
39 #include "citadel_decls.h"
40 #include "routines.h"
41 #include "routines2.h"
42 #include "tools.h"
43 #ifndef HAVE_SNPRINTF
44 #include "snprintf.h"
45 #endif
46
47 struct citcmd {
48         struct citcmd *next;
49         int c_cmdnum;
50         int c_axlevel;
51         char c_keys[5][64];
52 };
53
54 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
55
56
57 int rc_exp_beep;
58 char rc_exp_cmd[256];
59 int rc_allow_attachments;
60 int rc_display_message_numbers;
61 int rc_force_mail_prompts;
62 int rc_ansi_color;
63 int num_urls = 0;
64 char urls[MAXURLS][256];
65 char rc_url_cmd[256];
66
67 char *gl_string;
68 int next_lazy_cmd = 5;
69
70 struct citcmd *cmdlist = NULL;
71
72
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 */
78
79
80 /*
81  * print_express()  -  print express messages if there are any
82  */
83 void print_express(void)
84 {
85         char buf[256];
86         FILE *outpipe;
87
88         if (express_msgs == 0)
89                 return;
90         express_msgs = 0;
91         serv_puts("PEXP");
92         serv_gets(buf);
93         if (buf[0] != '1')
94                 return;
95
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);
101                         }
102                         pclose(outpipe);
103                         return;
104                 }
105         }
106         /* fall back to built-in express message display */
107         if (rc_exp_beep) {
108                 putc(7, stdout);
109         }
110         color(BRIGHT_RED);
111         printf("\r---\n");
112         while (serv_gets(buf), strcmp(buf, "000")) {
113                 printf("%s\n", buf);
114         }
115         printf("---\n");
116         color(BRIGHT_WHITE);
117 }
118
119
120 void set_keepalives(int s)
121 {
122         keepalives_enabled = (char) s;
123 }
124
125 /* 
126  * This loop handles the "keepalive" messages sent to the server when idling.
127  */
128
129 static time_t idlet = 0;
130 static void really_do_keepalive(void) {
131         char buf[256];
132
133         time(&idlet);
134         if (keepalives_enabled != KA_NO) {
135                 serv_puts("NOOP");
136                 if (keepalives_enabled == KA_YES) {
137                         serv_gets(buf);
138                         if (buf[3] == '*') {
139                                 express_msgs = 1;
140                                 if (ok_to_interrupt == 1) {
141                                         printf("\r%64s\r", "");
142                                         print_express();
143                                         printf("%s%c ", room_name,
144                                                room_prompt(room_flags));
145                                         fflush(stdout);
146                                 }
147                         }
148                 }
149         }
150 }
151
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. */
155
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;
160
161 static void *ka_thread(void *arg)
162 {
163         really_do_keepalive();
164         pthread_detach(ka_thr_handle);
165         ka_thr_active = 0;
166         return NULL;
167 }
168
169 /* start up a thread to handle a keepalive in the background */
170 static void async_ka_exec(void)
171 {
172         if (!ka_thr_active) {
173                 ka_thr_active = 1;
174                 if (pthread_create(&ka_thr_handle, NULL, ka_thread, NULL)) {
175                         perror("pthread_create");
176                         exit(1);
177                 }
178         }
179 }
180 #endif /* THREADED_CLIENT */
181
182 static void do_keepalive(void)
183 {
184         time_t now;
185
186         time(&now);
187         if ((now - idlet) < ((long) S_KEEPALIVE))
188                 return;
189
190         /* Do a space-backspace to keep telnet sessions from idling out */
191         printf(" %c", 8);
192         fflush(stdout);
193
194 #ifdef THREADED_CLIENT
195         if (async_ka_enabled)
196                 async_ka_exec();
197         else
198 #endif
199                 really_do_keepalive();
200 }
201
202
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. */
206
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)
210 {
211 #ifdef THREADED_CLIENT
212         if (ka_thr_active)
213                 pthread_join(ka_thr_handle, NULL);
214
215         async_ka_enabled--;
216 #endif
217 }
218
219 /* tell do_keepalive() that keepalives are asynchronous. */
220 void async_ka_start(void)
221 {
222 #ifdef THREADED_CLIENT
223         async_ka_enabled++;
224 #endif
225 }
226
227
228 int inkey(void)
229 {                               /* get a character from the keyboard, with   */
230         int a;                  /* the watchdog timer in effect if necessary */
231         fd_set rfds;
232         struct timeval tv;
233         time_t start_time, now;
234         char inbuf[2];
235
236         fflush(stdout);
237         time(&start_time);
238
239         do {
240
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.
244                  */
245                 do {
246                         do_keepalive();
247
248                         FD_ZERO(&rfds);
249                         FD_SET(0, &rfds);
250                         tv.tv_sec = S_KEEPALIVE;
251                         tv.tv_usec = 0;
252
253                         time(&now);
254                         if (((now - start_time) > SLEEPING)
255                             && (SLEEPING != 0) && (getppid() == 1)) {
256                                 printf("Sleeping? Call again.\n");
257                                 logoff(SIGALRM);
258                         }
259                         select(1, &rfds, NULL, NULL, &tv);
260                 } while (!FD_ISSET(0, &rfds));
261
262
263
264
265                 /* At this point, there's input, so fetch it.
266                  * (There's a hole in the bucket...)
267                  */
268                 read(0, inbuf, 1);
269                 a = inbuf[0];
270                 if (a == 127)
271                         a = 8;
272                 if (a > 126)
273                         a = 0;
274                 if (a == 10)
275                         a = 13;
276                 if (((a != 4) && (a != 13) && (a != 8) && (a != NEXT_KEY) && (a != STOP_KEY))
277                     && ((a < 32) || (a > 126)))
278                         a = 0;
279         } while (a == 0);
280         return (a);
281 }
282
283
284 int yesno(void)
285 {                               /* Returns 1 for yes, 0 for no */
286         int a;
287         while (1) {
288                 a = inkey();
289                 a = tolower(a);
290                 if (a == 'y') {
291                         printf("Yes\n");
292                         return (1);
293                 }
294                 if (a == 'n') {
295                         printf("No\n");
296                         return (0);
297                 }
298         }
299 }
300
301 /* Returns 1 for yes, 0 for no, arg is default value */
302 int yesno_d(int d)
303 {
304         int a;
305         while (1) {
306                 a = inkey();
307                 a = tolower(a);
308                 if (a == 13)
309                         a = (d ? 'y' : 'n');
310                 if (a == 'y') {
311                         printf("Yes\n");
312                         return (1);
313                 }
314                 if (a == 'n') {
315                         printf("No\n");
316                         return (0);
317                 }
318         }
319 }
320
321
322
323
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) 
328 {
329         int a, b;
330         char flag = 0;
331
332         if (lim < 0) {
333                 lim = (0 - lim);
334                 flag = 1;
335         }
336         strcpy(string, "");
337         gl_string = string;
338         async_ka_start();
339       GLA:a = inkey();
340         a = (a & 127);
341         if ((a == 8) && (strlen(string) == 0))
342                 goto GLA;
343         if ((a != 13) && (a != 8) && (strlen(string) == lim))
344                 goto GLA;
345         if ((a == 8) && (string[0] != 0)) {
346                 string[strlen(string) - 1] = 0;
347                 putc(8, stdout);
348                 putc(32, stdout);
349                 putc(8, stdout);
350                 goto GLA;
351         }
352         if ((a == 13) || (a == 10)) {
353                 putc(13, stdout);
354                 putc(10, stdout);
355                 async_ka_end();
356                 return;
357         }
358         if (a < 32)
359                 a = '.';
360         b = strlen(string);
361         string[b] = a;
362         string[b + 1] = 0;
363         if (flag == 0)
364                 putc(a, stdout);
365         if (flag == 1)
366                 putc('*', stdout);
367         goto GLA;
368 }
369
370
371 /*
372  * strprompt()  -  prompt for a string, print the existing value and
373  *                 allow the user to press return to keep it...
374  */
375 void strprompt(char *prompt, char *str, int len)
376 {
377         char buf[128];
378         print_express();
379         color(DIM_WHITE);
380         printf("%s ", prompt);
381         color(DIM_MAGENTA);
382         printf("[");
383         color(BRIGHT_MAGENTA);
384         printf("%s", str);
385         color(DIM_MAGENTA);
386         printf("]");
387         color(DIM_WHITE);
388         printf(": ");
389         color(BRIGHT_CYAN);
390         getline(buf, len);
391         if (buf[0] != 0)
392                 strcpy(str, buf);
393         color(DIM_WHITE);
394 }
395
396 /*
397  * boolprompt()  -  prompt for a yes/no, print the existing value and
398  *                  allow the user to press return to keep it...
399  */
400 int boolprompt(char *prompt, int prev_val)
401 {
402         int r;
403
404         color(DIM_WHITE);
405         printf("%s ", prompt);
406         color(DIM_MAGENTA);
407         printf(" [");
408         color(BRIGHT_MAGENTA);
409         printf("%s", (prev_val ? "Yes" : "No"));
410         color(DIM_MAGENTA);
411         printf("]: ");
412         color(BRIGHT_CYAN);
413         r = (yesno_d(prev_val));
414         color(DIM_WHITE);
415         return r;
416 }
417
418 /* 
419  * intprompt()  -  like strprompt(), except for an integer
420  *                 (note that it RETURNS the new value!)
421  */
422 int intprompt(char *prompt, int ival, int imin, int imax)
423 {
424         char buf[16];
425         int i;
426         int p;
427
428         do {
429                 i = ival;
430                 snprintf(buf, sizeof buf, "%d", i);
431                 strprompt(prompt, buf, 15);
432                 i = atoi(buf);
433                 for (p=0; p<strlen(buf); ++p) {
434                         if (!isdigit(buf[p])) i = imin - 1;
435                 }
436                 if (i < imin)
437                         printf("*** Must be no less than %d.\n", imin);
438                 if (i > imax)
439                         printf("*** Must be no more than %d.\n", imax);
440         } while ((i < imin) || (i > imax));
441         return (i);
442 }
443
444 /* 
445  * newprompt()  -  prompt for a string with no existing value
446  *                 (clears out string buffer first)
447  */
448 void newprompt(char *prompt, char *str, int len)
449 {
450         color(BRIGHT_MAGENTA);
451         printf("%s", prompt);
452         color(DIM_MAGENTA);
453         getline(str, len);
454         color(DIM_WHITE);
455 }
456
457
458 int lkey(void)
459 {                               /* returns a lower case value */
460         int a;
461         a = inkey();
462         if (isupper(a))
463                 a = tolower(a);
464         return (a);
465 }
466
467 /*
468  * parse the citadel.rc file
469  */
470 void load_command_set(void)
471 {
472         FILE *ccfile;
473         char buf[256];
474         struct citcmd *cptr;
475         struct citcmd *lastcmd = NULL;
476         int a, d;
477         int b = 0;
478
479
480         /* first, set up some defaults for non-required variables */
481
482         strcpy(editor_path, "");
483         strcpy(printcmd, "");
484         strcpy(rc_username, "");
485         strcpy(rc_password, "");
486         rc_floor_mode = 0;
487         rc_exp_beep = 1;
488         rc_allow_attachments = 0;
489         strcpy(rc_exp_cmd, "");
490         rc_display_message_numbers = 0;
491         rc_force_mail_prompts = 0;
492         rc_ansi_color = 0;
493         strcpy(rc_url_cmd, "");
494
495         /* now try to open the citadel.rc file */
496
497         ccfile = NULL;
498         if (getenv("HOME") != NULL) {
499                 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
500                 ccfile = fopen(buf, "r");
501         }
502         if (ccfile == NULL) {
503                 ccfile = fopen("/usr/local/lib/citadel.rc", "r");
504         }
505         if (ccfile == NULL) {
506                 snprintf(buf, sizeof buf, "%s/citadel.rc", BBSDIR);
507                 ccfile = fopen(buf, "r");
508         }
509         if (ccfile == NULL) {
510                 ccfile = fopen("./citadel.rc", "r");
511         }
512         if (ccfile == NULL) {
513                 perror("commands: cannot open citadel.rc");
514                 logoff(errno);
515         }
516         while (fgets(buf, 256, ccfile) != NULL) {
517                 while ((strlen(buf) > 0) ? (isspace(buf[strlen(buf) - 1])) : 0)
518                         buf[strlen(buf) - 1] = 0;
519
520                 if (!struncmp(buf, "editor=", 7))
521                         strcpy(editor_path, &buf[7]);
522
523                 if (!struncmp(buf, "printcmd=", 9))
524                         strcpy(printcmd, &buf[9]);
525
526                 if (!struncmp(buf, "expcmd=", 7))
527                         strcpy(rc_exp_cmd, &buf[7]);
528
529                 if (!struncmp(buf, "local_screen_dimensions=", 24))
530                         have_xterm = (char) atoi(&buf[24]);
531
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;
539                 }
540                 if (!struncmp(buf, "beep=", 5)) {
541                         rc_exp_beep = atoi(&buf[5]);
542                 }
543                 if (!struncmp(buf, "allow_attachments=", 18)) {
544                         rc_allow_attachments = atoi(&buf[18]);
545                 }
546                 if (!struncmp(buf, "display_message_numbers=", 24)) {
547                         rc_display_message_numbers = atoi(&buf[24]);
548                 }
549                 if (!struncmp(buf, "force_mail_prompts=", 19)) {
550                         rc_force_mail_prompts = atoi(&buf[19]);
551                 }
552                 if (!struncmp(buf, "ansi_color=", 11)) {
553                         if (!struncmp(&buf[11], "on", 2))
554                                 rc_ansi_color = 1;
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 */
559                 }
560                 if (!struncmp(buf, "username=", 9))
561                         strcpy(rc_username, &buf[9]);
562
563                 if (!struncmp(buf, "password=", 9))
564                         strcpy(rc_password, &buf[9]);
565
566                 if (!struncmp(buf, "urlcmd=", 7))
567                         strcpy(rc_url_cmd, &buf[7]);
568
569                 if (!struncmp(buf, "cmd=", 4)) {
570                         strcpy(buf, &buf[4]);
571
572                         cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
573
574                         cptr->c_cmdnum = atoi(buf);
575                         for (d = strlen(buf); d >= 0; --d)
576                                 if (buf[d] == ',')
577                                         b = d;
578                         strcpy(buf, &buf[b + 1]);
579
580                         cptr->c_axlevel = atoi(buf);
581                         for (d = strlen(buf); d >= 0; --d)
582                                 if (buf[d] == ',')
583                                         b = d;
584                         strcpy(buf, &buf[b + 1]);
585
586                         for (a = 0; a < 5; ++a)
587                                 cptr->c_keys[a][0] = 0;
588
589                         a = 0;
590                         b = 0;
591                         buf[strlen(buf) + 1] = 0;
592                         while (strlen(buf) > 0) {
593                                 b = strlen(buf);
594                                 for (d = strlen(buf); d >= 0; --d)
595                                         if (buf[d] == ',')
596                                                 b = d;
597                                 strncpy(cptr->c_keys[a], buf, b);
598                                 cptr->c_keys[a][b] = 0;
599                                 if (buf[b] == ',')
600                                         strcpy(buf, &buf[b + 1]);
601                                 else
602                                         strcpy(buf, "");
603                                 ++a;
604                         }
605
606                         cptr->next = NULL;
607                         if (cmdlist == NULL)
608                                 cmdlist = cptr;
609                         else
610                                 lastcmd->next = cptr;
611                         lastcmd = cptr;
612                 }
613         }
614         fclose(ccfile);
615 }
616
617
618
619 /*
620  * return the key associated with a command
621  */
622 char keycmd(char *cmdstr)
623 {
624         int a;
625
626         for (a = 0; a < strlen(cmdstr); ++a)
627                 if (cmdstr[a] == '&')
628                         return (tolower(cmdstr[a + 1]));
629         return (0);
630 }
631
632
633 /*
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
636  */
637 char *cmd_expand(char *strbuf, int mode)
638 {
639         int a;
640         static char exp[64];
641         char buf[256];
642
643         strcpy(exp, strbuf);
644
645         for (a = 0; a < strlen(exp); ++a) {
646                 if (strbuf[a] == '&') {
647
648                         if (mode == 0) {
649                                 strcpy(&exp[a], &exp[a + 1]);
650                         }
651                         if (mode == 1) {
652                                 exp[a] = '<';
653                                 strcpy(buf, &exp[a + 2]);
654                                 exp[a + 2] = '>';
655                                 exp[a + 3] = 0;
656                                 strcat(exp, buf);
657                         }
658                 }
659                 if (!strncmp(&exp[a], "^r", 2)) {
660                         strcpy(buf, exp);
661                         strcpy(&exp[a], room_name);
662                         strcat(exp, &buf[a + 2]);
663                 }
664                 if (!strncmp(&exp[a], "^c", 2)) {
665                         exp[a] = ',';
666                         strcpy(&exp[a + 1], &exp[a + 2]);
667                 }
668         }
669
670         return (exp);
671 }
672
673
674
675 /*
676  * Comparison function to determine if entered commands match a
677  * command loaded from the config file.
678  */
679 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
680 {
681         int a;
682         int cmdax;
683
684         cmdax = 0;
685         if (is_room_aide)
686                 cmdax = 1;
687         if (axlevel >= 6)
688                 cmdax = 2;
689
690         for (a = 0; a < ncomp; ++a) {
691                 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
692                     || (cptr->c_axlevel > cmdax))
693                         return (0);
694         }
695         return (1);
696 }
697
698
699 /*
700  * This function returns 1 if a given command requires a string input
701  */
702 int requires_string(struct citcmd *cptr, int ncomp)
703 {
704         int a;
705         char buf[64];
706
707         strcpy(buf, cptr->c_keys[ncomp - 1]);
708         for (a = 0; a < strlen(buf); ++a) {
709                 if (buf[a] == ':')
710                         return (1);
711         }
712         return (0);
713 }
714
715
716 /*
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.
720  */
721 int getcmd(char *argbuf)
722 {
723         char cmdbuf[5];
724         int cmdspaces[5];
725         int cmdpos;
726         int ch;
727         int a;
728         int got;
729         int this_lazy_cmd;
730         struct citcmd *cptr;
731
732         /* Switch color support on or off if we're in user mode */
733         if (rc_ansi_color == 3) {
734                 if (userflags & US_COLOR)
735                         enable_color = 1;
736                 else
737                         enable_color = 0;
738         }
739         /* if we're running in idiot mode, display a cute little menu */
740         IFNEXPERT formout("mainmenu");
741
742         print_express();        /* print express messages if there are any */
743         strcpy(argbuf, "");
744         cmdpos = 0;
745         for (a = 0; a < 5; ++a)
746                 cmdbuf[a] = 0;
747         /* now the room prompt... */
748         ok_to_interrupt = 1;
749         color(BRIGHT_WHITE);
750         printf("\n%s", room_name);
751         color(DIM_WHITE);
752         printf("%c ", room_prompt(room_flags));
753         fflush(stdout);
754
755         while (1) {
756                 ch = inkey();
757                 ok_to_interrupt = 0;
758
759                 /* Handle the backspace key, but only if there's something
760                  * to backspace over...
761                  */
762                 if ((ch == 8) && (cmdpos > 0)) {
763                         back(cmdspaces[cmdpos - 1] + 1);
764                         cmdbuf[cmdpos] = 0;
765                         --cmdpos;
766                 }
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)
771                                 next_lazy_cmd = 5;
772                         if (this_lazy_cmd == 5)
773                                 next_lazy_cmd = 13;
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));
780                                         printf("\n");
781                                         return (this_lazy_cmd);
782                                 }
783                         }
784                         printf("\n");
785                         return (this_lazy_cmd);
786                 }
787                 /* Otherwise, process the command */
788                 cmdbuf[cmdpos] = tolower(ch);
789
790                 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
791                         if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
792
793                                 printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
794                                 cmdspaces[cmdpos] = strlen(
795                                     cmd_expand(cptr->c_keys[cmdpos], 0));
796                                 if (cmdpos < 4)
797                                         if ((cptr->c_keys[cmdpos + 1]) != 0)
798                                                 putc(' ', stdout);
799                                 ++cmdpos;
800                         }
801                 }
802
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)) {
807                                         getline(argbuf, 32);
808                                 } else {
809                                         printf("\n");
810                                 }
811
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"
815                                  */
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))
822                                         next_lazy_cmd = 13;
823
824                                 return (cptr->c_cmdnum);
825
826                         }
827                 }
828
829                 if (ch == '?') {
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));
835                                         }
836                                         printf("\n");
837                                 }
838                         }
839
840                         printf("\n%s%c ", room_name, room_prompt(room_flags));
841                         got = 0;
842                         for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
843                                 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
844                                         for (a = 0; a < cmdpos; ++a) {
845                                                 printf("%s ",
846                                                        cmd_expand(cptr->c_keys[a], 0));
847                                         }
848                                         got = 1;
849                                 }
850                         }
851                 }
852         }
853
854 }
855
856
857
858
859
860 /*
861  * set tty modes.  commands are:
862  * 
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
867  */
868 #ifdef HAVE_TERMIOS_H
869 void sttybbs(int cmd)
870 {                               /* SysV version of sttybbs() */
871         struct termios live;
872         static struct termios saved_settings;
873         static int last_cmd = 0;
874
875         if (cmd == SB_LAST)
876                 cmd = last_cmd;
877         else
878                 last_cmd = cmd;
879
880         if ((cmd == 0) || (cmd == 1)) {
881                 tcgetattr(0, &live);
882                 live.c_iflag = ISTRIP | IXON | IXANY;
883                 live.c_oflag = OPOST | ONLCR;
884                 live.c_lflag = ISIG | NOFLSH;
885
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);
891                 } else {
892                         signal(SIGINT, SIG_IGN);
893                         signal(SIGQUIT, SIG_IGN);
894                         live.c_cc[VINTR] = (-1);
895                         live.c_cc[VQUIT] = (-1);
896                 }
897
898                 /* do we even need this stuff anymore? */
899                 /* live.c_line=0; */
900                 live.c_cc[VERASE] = 8;
901                 live.c_cc[VKILL] = 24;
902                 live.c_cc[VEOF] = 1;
903                 live.c_cc[VEOL] = 255;
904                 live.c_cc[VEOL2] = 0;
905                 live.c_cc[VSTART] = 0;
906                 tcsetattr(0, TCSADRAIN, &live);
907         }
908         if (cmd == 2) {
909                 tcgetattr(0, &saved_settings);
910         }
911         if (cmd == 3) {
912                 tcsetattr(0, TCSADRAIN, &saved_settings);
913         }
914 }
915 #else
916 void sttybbs(int cmd)
917 {                               /* BSD version of sttybbs() */
918         struct sgttyb live;
919         static struct sgttyb saved_settings;
920
921         if ((cmd == 0) || (cmd == 1)) {
922                 gtty(0, &live);
923                 live.sg_flags |= CBREAK;
924                 live.sg_flags |= CRMOD;
925                 live.sg_flags |= NL1;
926                 live.sg_flags &= ~ECHO;
927                 if (cmd == 1)
928                         live.sg_flags |= NOFLSH;
929                 stty(0, &live);
930         }
931         if (cmd == 2) {
932                 gtty(0, &saved_settings);
933         }
934         if (cmd == 3) {
935                 stty(0, &saved_settings);
936         }
937 }
938 #endif
939
940
941 /*
942  * display_help()  -  help file viewer
943  */
944 void display_help(char *name)
945 {
946         formout(name);
947 }
948
949
950 /*
951  * fmout()  -  Citadel text formatter and paginator
952  */
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 */
960 {
961         int a, b, c, d, old;
962         int real = (-1);
963         char aaa[140];
964         char buffer[512];
965         int eof_flag = 0;
966
967         num_urls = 0;   /* Start with a clean slate of embedded URL's */
968
969         if (starting_lp >= 0) {
970                 lines_printed = starting_lp;
971         }
972         strcpy(aaa, "");
973         old = 255;
974         strcpy(buffer, "");
975         c = 1;                  /* c is the current pos */
976
977         sigcaught = 0;
978         sttybbs(1);
979
980 FMTA:   while ((eof_flag == 0) && (strlen(buffer) < 126)) {
981         
982                 if (sigcaught)
983                         goto OOPS;
984                 if (fp != NULL) {       /* read from file */
985                         if (feof(fp))
986                                 eof_flag = 1;
987                         if (eof_flag == 0) {
988                                 a = getc(fp);
989                                 buffer[strlen(buffer) + 1] = 0;
990                                 buffer[strlen(buffer)] = a;
991                         }
992                 } else {        /* read from server */
993                         d = strlen(buffer);
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")) {
998                                 buffer[d] = 0;
999                                 eof_flag = 1;
1000                                 while (isspace(buffer[strlen(buffer) - 1]))
1001                                         buffer[strlen(buffer) - 1] = 0;
1002                         }
1003                         d = strlen(buffer);
1004                         buffer[d] = 10;
1005                         buffer[d + 1] = 0;
1006                 }
1007         }
1008
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;
1017                 }
1018                 ++num_urls;
1019         }
1020
1021         buffer[strlen(buffer) + 1] = 0;
1022         a = buffer[0];
1023         strcpy(buffer, &buffer[1]);
1024
1025         old = real;
1026         real = a;
1027         if (a <= 0)
1028                 goto FMTEND;
1029
1030         if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
1031                 a = 32;
1032         if (((old == 13) || (old == 10)) && (isspace(real))) {
1033                 printf("\n");
1034                 ++lines_printed;
1035                 lines_printed = checkpagin(lines_printed, pagin, height);
1036                 c = 1;
1037         }
1038         if (a > 126)
1039                 goto FMTA;
1040
1041         if (a > 32) {
1042                 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
1043                         printf("\n%s", aaa);
1044                         c = strlen(aaa);
1045                         aaa[0] = 0;
1046                         ++lines_printed;
1047                         lines_printed = checkpagin(lines_printed, pagin, height);
1048                 }
1049                 b = strlen(aaa);
1050                 aaa[b] = a;
1051                 aaa[b + 1] = 0;
1052         }
1053         if (a == 32) {
1054                 if ((strlen(aaa) + c) > (width - 5)) {
1055                         c = 1;
1056                         printf("\n");
1057                         ++lines_printed;
1058                         lines_printed = checkpagin(lines_printed, pagin, height);
1059                 }
1060                 printf("%s ", aaa);
1061                 ++c;
1062                 c = c + strlen(aaa);
1063                 strcpy(aaa, "");
1064                 goto FMTA;
1065         }
1066         if ((a == 13) || (a == 10)) {
1067                 printf("%s\n", aaa);
1068                 c = 1;
1069                 ++lines_printed;
1070                 lines_printed = checkpagin(lines_printed, pagin, height);
1071                 strcpy(aaa, "");
1072                 goto FMTA;
1073         }
1074         goto FMTA;
1075
1076         /* signal caught; drain the server */
1077       OOPS:do {
1078                 serv_gets(aaa);
1079         } while (strcmp(aaa, "000"));
1080
1081       FMTEND:printf("\n");
1082         ++lines_printed;
1083         lines_printed = checkpagin(lines_printed, pagin, height);
1084         return (sigcaught);
1085 }
1086
1087
1088 /*
1089  * support ANSI color if defined
1090  */
1091 void color(int colornum)
1092 {
1093         static int is_bold = 0;
1094         static int hold_color, current_color;
1095
1096         if (colornum == COLOR_PUSH) {
1097                 hold_color = current_color;
1098                 return;
1099         }
1100
1101         if (colornum == COLOR_POP) {
1102                 color(hold_color);
1103                 return;
1104         }
1105
1106         current_color = colornum;
1107         if (enable_color) {
1108                 /* Don't switch to black or white explicitly as this confuses
1109                  * black-on-white terminals. Instead, output the "original
1110                  * pair" sequence.
1111                  */
1112                 if ((colornum & 7) == DIM_WHITE || (colornum & 7) == DIM_BLACK)
1113                         printf("\033[39;49m");
1114                 else
1115                         printf("\033[3%dm", (colornum & 7));
1116
1117                 if ((colornum >= 8) && (is_bold == 0)) {
1118                         printf("\033[1m");
1119                         is_bold = 1;
1120                 } else if ((colornum < 8) && (is_bold == 1)) {
1121                         printf("\033[0m");
1122                         is_bold = 0;
1123                 }
1124                 fflush(stdout);
1125         }
1126 }
1127
1128 #if 0
1129 void cls(int colornum)
1130 {
1131         if (enable_color) {
1132                 printf("\033[4%dm\033[2J\033[H\033[0m", colornum);
1133                 fflush(stdout);
1134         }
1135 }
1136 #endif
1137
1138
1139 /*
1140  * Detect whether ANSI color is available (answerback)
1141  */
1142 void send_ansi_detect(void)
1143 {
1144         if (rc_ansi_color == 2) {
1145                 printf("\033[c");
1146                 fflush(stdout);
1147                 time(&AnsiDetect);
1148         }
1149 }
1150
1151 void look_for_ansi(void)
1152 {
1153         fd_set rfds;
1154         struct timeval tv;
1155         char abuf[512];
1156         time_t now;
1157         int a;
1158
1159         if (rc_ansi_color == 0) {
1160                 enable_color = 0;
1161         } else if (rc_ansi_color == 1) {
1162                 enable_color = 1;
1163         } else if (rc_ansi_color == 2) {
1164
1165                 /* otherwise, do the auto-detect */
1166
1167                 strcpy(abuf, "");
1168
1169                 time(&now);
1170                 if ((now - AnsiDetect) < 2)
1171                         sleep(1);
1172
1173                 do {
1174                         FD_ZERO(&rfds);
1175                         FD_SET(0, &rfds);
1176                         tv.tv_sec = 0;
1177                         tv.tv_usec = 1;
1178
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);
1183                         }
1184                 } while (FD_ISSET(0, &rfds));
1185
1186                 for (a = 0; a < strlen(abuf); ++a) {
1187                         if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1188                             && (abuf[a + 2] == '?')) {
1189                                 enable_color = 1;
1190                         }
1191                 }
1192         }
1193 }
1194
1195
1196 /*
1197  * Display key options (highlight hotkeys inside angle brackets)
1198  */
1199 void keyopt(char *buf) {
1200         int i;
1201
1202         color(DIM_WHITE);
1203         for (i=0; i<strlen(buf); ++i) {
1204                 if (buf[i]=='<') {
1205                         putc(buf[i], stdout);
1206                         color(BRIGHT_MAGENTA);
1207                 } else {
1208                         if (buf[i]=='>') {
1209                                 color(DIM_WHITE);
1210                         }
1211                         putc(buf[i], stdout);
1212                 }
1213         }
1214         color(DIM_WHITE);
1215 }
1216
1217
1218
1219 /*
1220  * Present a key-menu line choice type of thing
1221  */
1222 char keymenu(char *menuprompt, char *menustring) {
1223         int i, c, a;
1224         int choices;
1225         int do_prompt = 0;
1226         char buf[256];
1227         int ch;
1228         int display_prompt = 1;
1229
1230         choices = num_tokens(menustring, '|');
1231
1232         if (menuprompt != NULL) do_prompt = 1;
1233         if (menuprompt != NULL) if (strlen(menuprompt)==0) do_prompt = 0;
1234
1235         while (1) {
1236                 if (display_prompt) {
1237                         if (do_prompt) {
1238                                 printf("%s ", menuprompt);
1239                         } 
1240                         else {
1241                                 for (i=0; i<choices; ++i) {
1242                                         extract(buf, menustring, i);
1243                                         keyopt(buf);
1244                                         printf(" ");
1245                                 }
1246                         }
1247                         printf(" -> ");
1248                         display_prompt = 0;
1249                 }
1250                 ch = lkey();
1251         
1252                 if ( (do_prompt) && (ch=='?') ) {
1253                         printf("\rOne of...                               ");
1254                         printf("                                      \n");
1255                         for (i=0; i<choices; ++i) {
1256                                 extract(buf, menustring, i);
1257                                 printf("   ");
1258                                 keyopt(buf);
1259                                 printf("\n");
1260                         }
1261                         printf("\n");
1262                         display_prompt = 1;
1263                 }
1264
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]))
1269                                    && (buf[c-1]=='<')
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);
1274                                                 }
1275                                         }
1276                                         printf("\n\n");
1277                                         return ch;
1278                                 }
1279                         }
1280                 }
1281         }
1282 }