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