* Added the ability to handle embedded URL's from the text client
[citadel.git] / citadel / commands.c
1 /*
2  * Citadel/UX
3  *
4  * commands.c - front end for Citadel
5  *
6  * This version is the traditional command parser for room prompts.
7  *
8  * $Id$
9  *
10  */
11
12 #include "sysdep.h"
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <fcntl.h>
17 #include <ctype.h>
18 #include <string.h>
19 #include <sys/types.h>
20 #include <sys/time.h>
21
22 #ifdef HAVE_TERMIOS_H
23 #include <termios.h>
24 #else
25 #include <sgtty.h>
26 #endif
27
28 #ifdef HAVE_SYS_SELECT_H
29 #include <sys/select.h>
30 #endif
31
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("---\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 void do_keepalive(void)
129 {
130         char buf[256];
131         static time_t idlet = 0;
132         time_t now;
133
134         time(&now);
135         if ((now - idlet) < ((long) S_KEEPALIVE))
136                 return;
137         time(&idlet);
138
139         /* Do a space-backspace to keep socksified telnet sessions open */
140         printf(" %c", 8);
141         fflush(stdout);
142
143         if (keepalives_enabled != KA_NO) {
144                 serv_puts("NOOP");
145                 if (keepalives_enabled == KA_YES) {
146                         serv_gets(buf);
147                         if (buf[3] == '*') {
148                                 express_msgs = 1;
149                                 if (ok_to_interrupt == 1) {
150                                         printf("\r%64s\r", "");
151                                         print_express();
152                                         printf("%s%c ", room_name,
153                                                room_prompt(room_flags));
154                                         fflush(stdout);
155                                 }
156                         }
157                 }
158         }
159 }
160
161
162 int inkey(void)
163 {                               /* get a character from the keyboard, with   */
164         int a;                  /* the watchdog timer in effect if necessary */
165         fd_set rfds;
166         struct timeval tv;
167         time_t start_time, now;
168         char inbuf[2];
169
170         fflush(stdout);
171         time(&start_time);
172
173         do {
174
175                 /* This loop waits for keyboard input.  If the keepalive
176                  * timer expires, it sends a keepalive to the server if
177                  * necessary and then waits again.
178                  */
179                 do {
180                         do_keepalive();
181                         FD_ZERO(&rfds);
182                         FD_SET(0, &rfds);
183                         tv.tv_sec = S_KEEPALIVE;
184                         tv.tv_usec = 0;
185
186                         time(&now);
187                         if (((now - start_time) > SLEEPING)
188                             && (SLEEPING != 0) && (getppid() == 1)) {
189                                 printf("Sleeping? Call again.\n");
190                                 logoff(SIGALRM);
191                         }
192                         select(1, &rfds, NULL, NULL, &tv);
193                 } while (!FD_ISSET(0, &rfds));
194
195
196
197
198                 /* At this point, there's input, so fetch it.
199                  * (There's a hole in the bucket...)
200                  */
201                 read(0, inbuf, 1);
202                 a = inbuf[0];
203                 if (a == 127)
204                         a = 8;
205                 if (a > 126)
206                         a = 0;
207                 if (a == 10)
208                         a = 13;
209                 if (((a != 4) && (a != 13) && (a != 8) && (a != NEXT_KEY) && (a != STOP_KEY))
210                     && ((a < 32) || (a > 126)))
211                         a = 0;
212         } while (a == 0);
213         return (a);
214 }
215
216
217 int yesno(void)
218 {                               /* Returns 1 for yes, 0 for no */
219         int a;
220         while (1) {
221                 a = inkey();
222                 a = tolower(a);
223                 if (a == 'y') {
224                         printf("Yes\n");
225                         return (1);
226                 }
227                 if (a == 'n') {
228                         printf("No\n");
229                         return (0);
230                 }
231         }
232 }
233
234 /* Returns 1 for yes, 0 for no, arg is default value */
235 int yesno_d(int d)
236 {
237         int a;
238         while (1) {
239                 a = inkey();
240                 a = tolower(a);
241                 if (a == 13)
242                         a = (d ? 'y' : 'n');
243                 if (a == 'y') {
244                         printf("Yes\n");
245                         return (1);
246                 }
247                 if (a == 'n') {
248                         printf("No\n");
249                         return (0);
250                 }
251         }
252 }
253
254
255
256
257 /* Gets a line from the terminal */
258 /* string == Pointer to string buffer */
259 /* lim == Maximum length - if negative, no-show */
260 void getline(char *string, int lim) 
261 {
262         int a, b;
263         char flag = 0;
264
265         if (lim < 0) {
266                 lim = (0 - lim);
267                 flag = 1;
268         }
269         strcpy(string, "");
270         gl_string = string;
271       GLA:a = inkey();
272         a = (a & 127);
273         if ((a == 8) && (strlen(string) == 0))
274                 goto GLA;
275         if ((a != 13) && (a != 8) && (strlen(string) == lim))
276                 goto GLA;
277         if ((a == 8) && (string[0] != 0)) {
278                 string[strlen(string) - 1] = 0;
279                 putc(8, stdout);
280                 putc(32, stdout);
281                 putc(8, stdout);
282                 goto GLA;
283         }
284         if ((a == 13) || (a == 10)) {
285                 putc(13, stdout);
286                 putc(10, stdout);
287                 return;
288         }
289         if (a < 32)
290                 a = '.';
291         b = strlen(string);
292         string[b] = a;
293         string[b + 1] = 0;
294         if (flag == 0)
295                 putc(a, stdout);
296         if (flag == 1)
297                 putc('*', stdout);
298         goto GLA;
299 }
300
301
302 /*
303  * strprompt()  -  prompt for a string, print the existing value and
304  *                 allow the user to press return to keep it...
305  */
306 void strprompt(char *prompt, char *str, int len)
307 {
308         char buf[128];
309         print_express();
310         color(DIM_WHITE);
311         printf("%s ", prompt);
312         color(DIM_MAGENTA);
313         printf("[");
314         color(BRIGHT_MAGENTA);
315         printf("%s", str);
316         color(DIM_MAGENTA);
317         printf("]");
318         color(DIM_WHITE);
319         printf(": ");
320         color(BRIGHT_CYAN);
321         getline(buf, len);
322         if (buf[0] != 0)
323                 strcpy(str, buf);
324         color(DIM_WHITE);
325 }
326
327 /*
328  * boolprompt()  -  prompt for a yes/no, print the existing value and
329  *                  allow the user to press return to keep it...
330  */
331 int boolprompt(char *prompt, int prev_val)
332 {
333         int r;
334
335         color(DIM_WHITE);
336         printf("%s ", prompt);
337         color(DIM_MAGENTA);
338         printf(" [");
339         color(BRIGHT_MAGENTA);
340         printf("%s", (prev_val ? "Yes" : "No"));
341         color(DIM_MAGENTA);
342         printf("]: ");
343         color(BRIGHT_CYAN);
344         r = (yesno_d(prev_val));
345         color(DIM_WHITE);
346         return r;
347 }
348
349 /* 
350  * intprompt()  -  like strprompt(), except for an integer
351  *                 (note that it RETURNS the new value!)
352  */
353 int intprompt(char *prompt, int ival, int imin, int imax)
354 {
355         char buf[16];
356         int i;
357         i = ival;
358         do {
359                 snprintf(buf, sizeof buf, "%d", i);
360                 strprompt(prompt, buf, 15);
361                 i = atoi(buf);
362                 if (i < imin)
363                         printf("*** Must be no less than %d.\n", imin);
364                 if (i > imax)
365                         printf("*** Must be no more than %d.\n", imax);
366         } while ((i < imin) || (i > imax));
367         return (i);
368 }
369
370 /* 
371  * newprompt()  -  prompt for a string with no existing value
372  *                 (clears out string buffer first)
373  */
374 void newprompt(char *prompt, char *str, int len)
375 {
376         color(BRIGHT_MAGENTA);
377         printf("%s", prompt);
378         color(DIM_MAGENTA);
379         getline(str, len);
380         color(DIM_WHITE);
381 }
382
383
384 int lkey(void)
385 {                               /* returns a lower case value */
386         int a;
387         a = inkey();
388         if (isupper(a))
389                 a = tolower(a);
390         return (a);
391 }
392
393 /*
394  * parse the citadel.rc file
395  */
396 void load_command_set(void)
397 {
398         FILE *ccfile;
399         char buf[256];
400         struct citcmd *cptr;
401         struct citcmd *lastcmd = NULL;
402         int a, d;
403         int b = 0;
404
405
406         /* first, set up some defaults for non-required variables */
407
408         strcpy(editor_path, "");
409         strcpy(printcmd, "");
410         strcpy(rc_username, "");
411         strcpy(rc_password, "");
412         rc_floor_mode = 0;
413         rc_exp_beep = 1;
414         rc_allow_attachments = 0;
415         strcpy(rc_exp_cmd, "");
416         rc_display_message_numbers = 0;
417         rc_force_mail_prompts = 0;
418         rc_ansi_color = 0;
419         strcpy(rc_url_cmd, "");
420
421         /* now try to open the citadel.rc file */
422
423         ccfile = NULL;
424         if (getenv("HOME") != NULL) {
425                 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
426                 ccfile = fopen(buf, "r");
427         }
428         if (ccfile == NULL) {
429                 ccfile = fopen("/usr/local/lib/citadel.rc", "r");
430         }
431         if (ccfile == NULL) {
432                 snprintf(buf, sizeof buf, "%s/citadel.rc", BBSDIR);
433                 ccfile = fopen(buf, "r");
434         }
435         if (ccfile == NULL) {
436                 ccfile = fopen("./citadel.rc", "r");
437         }
438         if (ccfile == NULL) {
439                 perror("commands: cannot open citadel.rc");
440                 logoff(errno);
441         }
442         while (fgets(buf, 256, ccfile) != NULL) {
443                 while ((strlen(buf) > 0) ? (isspace(buf[strlen(buf) - 1])) : 0)
444                         buf[strlen(buf) - 1] = 0;
445
446                 if (!struncmp(buf, "editor=", 7))
447                         strcpy(editor_path, &buf[7]);
448
449                 if (!struncmp(buf, "printcmd=", 9))
450                         strcpy(printcmd, &buf[9]);
451
452                 if (!struncmp(buf, "expcmd=", 7))
453                         strcpy(rc_exp_cmd, &buf[7]);
454
455                 if (!struncmp(buf, "local_screen_dimensions=", 24))
456                         have_xterm = (char) atoi(&buf[24]);
457
458                 if (!struncmp(buf, "use_floors=", 11)) {
459                         if (!strucmp(&buf[11], "yes"))
460                                 rc_floor_mode = RC_YES;
461                         if (!strucmp(&buf[11], "no"))
462                                 rc_floor_mode = RC_NO;
463                         if (!strucmp(&buf[11], "default"))
464                                 rc_floor_mode = RC_DEFAULT;
465                 }
466                 if (!struncmp(buf, "beep=", 5)) {
467                         rc_exp_beep = atoi(&buf[5]);
468                 }
469                 if (!struncmp(buf, "allow_attachments=", 18)) {
470                         rc_allow_attachments = atoi(&buf[18]);
471                 }
472                 if (!struncmp(buf, "display_message_numbers=", 24)) {
473                         rc_display_message_numbers = atoi(&buf[24]);
474                 }
475                 if (!struncmp(buf, "force_mail_prompts=", 19)) {
476                         rc_force_mail_prompts = atoi(&buf[19]);
477                 }
478                 if (!struncmp(buf, "ansi_color=", 11)) {
479                         if (!struncmp(&buf[11], "on", 2))
480                                 rc_ansi_color = 1;
481                         if (!struncmp(&buf[11], "auto", 4))
482                                 rc_ansi_color = 2;      /* autodetect */
483                         if (!struncmp(&buf[11], "user", 4))
484                                 rc_ansi_color = 3;      /* user config */
485                 }
486                 if (!struncmp(buf, "username=", 9))
487                         strcpy(rc_username, &buf[9]);
488
489                 if (!struncmp(buf, "password=", 9))
490                         strcpy(rc_password, &buf[9]);
491
492                 if (!struncmp(buf, "urlcmd=", 7))
493                         strcpy(rc_url_cmd, &buf[7]);
494
495                 if (!struncmp(buf, "cmd=", 4)) {
496                         strcpy(buf, &buf[4]);
497
498                         cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
499
500                         cptr->c_cmdnum = atoi(buf);
501                         for (d = strlen(buf); d >= 0; --d)
502                                 if (buf[d] == ',')
503                                         b = d;
504                         strcpy(buf, &buf[b + 1]);
505
506                         cptr->c_axlevel = atoi(buf);
507                         for (d = strlen(buf); d >= 0; --d)
508                                 if (buf[d] == ',')
509                                         b = d;
510                         strcpy(buf, &buf[b + 1]);
511
512                         for (a = 0; a < 5; ++a)
513                                 cptr->c_keys[a][0] = 0;
514
515                         a = 0;
516                         b = 0;
517                         buf[strlen(buf) + 1] = 0;
518                         while (strlen(buf) > 0) {
519                                 b = strlen(buf);
520                                 for (d = strlen(buf); d >= 0; --d)
521                                         if (buf[d] == ',')
522                                                 b = d;
523                                 strncpy(cptr->c_keys[a], buf, b);
524                                 cptr->c_keys[a][b] = 0;
525                                 if (buf[b] == ',')
526                                         strcpy(buf, &buf[b + 1]);
527                                 else
528                                         strcpy(buf, "");
529                                 ++a;
530                         }
531
532                         cptr->next = NULL;
533                         if (cmdlist == NULL)
534                                 cmdlist = cptr;
535                         else
536                                 lastcmd->next = cptr;
537                         lastcmd = cptr;
538                 }
539         }
540         fclose(ccfile);
541 }
542
543
544
545 /*
546  * return the key associated with a command
547  */
548 char keycmd(char *cmdstr)
549 {
550         int a;
551
552         for (a = 0; a < strlen(cmdstr); ++a)
553                 if (cmdstr[a] == '&')
554                         return (tolower(cmdstr[a + 1]));
555         return (0);
556 }
557
558
559 /*
560  * Output the string from a key command without the ampersand
561  * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
562  */
563 char *cmd_expand(char *strbuf, int mode)
564 {
565         int a;
566         static char exp[64];
567         char buf[256];
568
569         strcpy(exp, strbuf);
570
571         for (a = 0; a < strlen(exp); ++a) {
572                 if (strbuf[a] == '&') {
573
574                         if (mode == 0) {
575                                 strcpy(&exp[a], &exp[a + 1]);
576                         }
577                         if (mode == 1) {
578                                 exp[a] = '<';
579                                 strcpy(buf, &exp[a + 2]);
580                                 exp[a + 2] = '>';
581                                 exp[a + 3] = 0;
582                                 strcat(exp, buf);
583                         }
584                 }
585                 if (!strncmp(&exp[a], "^r", 2)) {
586                         strcpy(buf, exp);
587                         strcpy(&exp[a], room_name);
588                         strcat(exp, &buf[a + 2]);
589                 }
590                 if (!strncmp(&exp[a], "^c", 2)) {
591                         exp[a] = ',';
592                         strcpy(&exp[a + 1], &exp[a + 2]);
593                 }
594         }
595
596         return (exp);
597 }
598
599
600
601 /*
602  * Comparison function to determine if entered commands match a
603  * command loaded from the config file.
604  */
605 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
606 {
607         int a;
608         int cmdax;
609
610         cmdax = 0;
611         if (is_room_aide)
612                 cmdax = 1;
613         if (axlevel >= 6)
614                 cmdax = 2;
615
616         for (a = 0; a < ncomp; ++a) {
617                 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
618                     || (cptr->c_axlevel > cmdax))
619                         return (0);
620         }
621         return (1);
622 }
623
624
625 /*
626  * This function returns 1 if a given command requires a string input
627  */
628 int requires_string(struct citcmd *cptr, int ncomp)
629 {
630         int a;
631         char buf[64];
632
633         strcpy(buf, cptr->c_keys[ncomp - 1]);
634         for (a = 0; a < strlen(buf); ++a) {
635                 if (buf[a] == ':')
636                         return (1);
637         }
638         return (0);
639 }
640
641
642 /*
643  * Input a command at the main prompt.
644  * This function returns an integer command number.  If the command prompts
645  * for a string then it is placed in the supplied buffer.
646  */
647 int getcmd(char *argbuf)
648 {
649         char cmdbuf[5];
650         int cmdspaces[5];
651         int cmdpos;
652         int ch;
653         int a;
654         int got;
655         int this_lazy_cmd;
656         struct citcmd *cptr;
657
658         /* Switch color support on or off if we're in user mode */
659         if (rc_ansi_color == 3) {
660                 if (userflags & US_COLOR)
661                         enable_color = 1;
662                 else
663                         enable_color = 0;
664         }
665         /* if we're running in idiot mode, display a cute little menu */
666         IFNEXPERT formout("mainmenu");
667
668         print_express();        /* print express messages if there are any */
669         strcpy(argbuf, "");
670         cmdpos = 0;
671         for (a = 0; a < 5; ++a)
672                 cmdbuf[a] = 0;
673         /* now the room prompt... */
674         ok_to_interrupt = 1;
675         color(BRIGHT_WHITE);
676         printf("\n%s", room_name);
677         color(DIM_WHITE);
678         printf("%c ", room_prompt(room_flags));
679         fflush(stdout);
680
681         while (1) {
682                 ch = inkey();
683                 ok_to_interrupt = 0;
684
685                 /* Handle the backspace key, but only if there's something
686                  * to backspace over...
687                  */
688                 if ((ch == 8) && (cmdpos > 0)) {
689                         back(cmdspaces[cmdpos - 1] + 1);
690                         cmdbuf[cmdpos] = 0;
691                         --cmdpos;
692                 }
693                 /* Spacebar invokes "lazy traversal" commands */
694                 if ((ch == 32) && (cmdpos == 0)) {
695                         this_lazy_cmd = next_lazy_cmd;
696                         if (this_lazy_cmd == 13)
697                                 next_lazy_cmd = 5;
698                         if (this_lazy_cmd == 5)
699                                 next_lazy_cmd = 13;
700                         for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
701                                 if (cptr->c_cmdnum == this_lazy_cmd) {
702                                         for (a = 0; a < 5; ++a)
703                                                 if (cptr->c_keys[a][0] != 0)
704                                                         printf("%s ", cmd_expand(
705                                                                                         cptr->c_keys[a], 0));
706                                         printf("\n");
707                                         return (this_lazy_cmd);
708                                 }
709                         }
710                         printf("\n");
711                         return (this_lazy_cmd);
712                 }
713                 /* Otherwise, process the command */
714                 cmdbuf[cmdpos] = tolower(ch);
715
716                 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
717                         if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
718
719                                 printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
720                                 cmdspaces[cmdpos] = strlen(
721                                     cmd_expand(cptr->c_keys[cmdpos], 0));
722                                 if (cmdpos < 4)
723                                         if ((cptr->c_keys[cmdpos + 1]) != 0)
724                                                 putc(' ', stdout);
725                                 ++cmdpos;
726                         }
727                 }
728
729                 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
730                         if (cmdmatch(cmdbuf, cptr, 5)) {
731                                 /* We've found our command. */
732                                 if (requires_string(cptr, cmdpos)) {
733                                         getline(argbuf, 32);
734                                 } else {
735                                         printf("\n");
736                                 }
737
738                                 /* If this command is one that changes rooms,
739                                  * then the next lazy-command (space bar)
740                                  * should be "read new" instead of "goto"
741                                  */
742                                 if ((cptr->c_cmdnum == 5)
743                                     || (cptr->c_cmdnum == 6)
744                                     || (cptr->c_cmdnum == 47)
745                                     || (cptr->c_cmdnum == 52)
746                                     || (cptr->c_cmdnum == 16)
747                                     || (cptr->c_cmdnum == 20))
748                                         next_lazy_cmd = 13;
749
750                                 return (cptr->c_cmdnum);
751
752                         }
753                 }
754
755                 if (ch == '?') {
756                         printf("\rOne of ...                         \n");
757                         for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
758                                 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
759                                         for (a = 0; a < 5; ++a) {
760                                                 printf("%s ", cmd_expand(cptr->c_keys[a], 1));
761                                         }
762                                         printf("\n");
763                                 }
764                         }
765
766                         printf("\n%s%c ", room_name, room_prompt(room_flags));
767                         got = 0;
768                         for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
769                                 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
770                                         for (a = 0; a < cmdpos; ++a) {
771                                                 printf("%s ",
772                                                        cmd_expand(cptr->c_keys[a], 0));
773                                         }
774                                         got = 1;
775                                 }
776                         }
777                 }
778         }
779
780 }
781
782
783
784
785
786 /*
787  * set tty modes.  commands are:
788  * 
789  * 0 - set to bbs mode, intr/quit disabled
790  * 1 - set to bbs mode, intr/quit enabled
791  * 2 - save current settings for later restoral
792  * 3 - restore saved settings
793  */
794 #ifdef HAVE_TERMIOS_H
795 void sttybbs(int cmd)
796 {                               /* SysV version of sttybbs() */
797         struct termios live;
798         static struct termios saved_settings;
799         static int last_cmd = 0;
800
801         if (cmd == SB_LAST)
802                 cmd = last_cmd;
803         else
804                 last_cmd = cmd;
805
806         if ((cmd == 0) || (cmd == 1)) {
807                 tcgetattr(0, &live);
808                 live.c_iflag = ISTRIP | IXON | IXANY;
809                 live.c_oflag = OPOST | ONLCR;
810                 live.c_lflag = ISIG | NOFLSH;
811
812                 if (cmd == SB_YES_INTR) {
813                         live.c_cc[VINTR] = NEXT_KEY;
814                         live.c_cc[VQUIT] = STOP_KEY;
815                         signal(SIGINT, *sighandler);
816                         signal(SIGQUIT, *sighandler);
817                 } else {
818                         signal(SIGINT, SIG_IGN);
819                         signal(SIGQUIT, SIG_IGN);
820                         live.c_cc[VINTR] = (-1);
821                         live.c_cc[VQUIT] = (-1);
822                 }
823
824                 /* do we even need this stuff anymore? */
825                 /* live.c_line=0; */
826                 live.c_cc[VERASE] = 8;
827                 live.c_cc[VKILL] = 24;
828                 live.c_cc[VEOF] = 1;
829                 live.c_cc[VEOL] = 255;
830                 live.c_cc[VEOL2] = 0;
831                 live.c_cc[VSTART] = 0;
832                 tcsetattr(0, TCSADRAIN, &live);
833         }
834         if (cmd == 2) {
835                 tcgetattr(0, &saved_settings);
836         }
837         if (cmd == 3) {
838                 tcsetattr(0, TCSADRAIN, &saved_settings);
839         }
840 }
841 #else
842 void sttybbs(int cmd)
843 {                               /* BSD version of sttybbs() */
844         struct sgttyb live;
845         static struct sgttyb saved_settings;
846
847         if ((cmd == 0) || (cmd == 1)) {
848                 gtty(0, &live);
849                 live.sg_flags |= CBREAK;
850                 live.sg_flags |= CRMOD;
851                 live.sg_flags |= NL1;
852                 live.sg_flags &= ~ECHO;
853                 if (cmd == 1)
854                         live.sg_flags |= NOFLSH;
855                 stty(0, &live);
856         }
857         if (cmd == 2) {
858                 gtty(0, &saved_settings);
859         }
860         if (cmd == 3) {
861                 stty(0, &saved_settings);
862         }
863 }
864 #endif
865
866
867 /*
868  * display_help()  -  help file viewer
869  */
870 void display_help(char *name)
871 {
872         formout(name);
873 }
874
875
876 /*
877  * fmout()  -  Citadel text formatter and paginator
878  */
879 int fmout(int width, FILE * fp, char pagin, int height, int starting_lp, char subst)
880                         /* screen width to use */
881                         /* file to read from, or NULL to read from server */
882                         /* nonzero if we should use the paginator */
883                         /* screen height to use */
884                         /* starting value for lines_printed, -1 for global */
885                         /* nonzero if we should use hypertext mode */
886 {
887         int a, b, c, d, old;
888         int real = (-1);
889         char aaa[140];
890         char buffer[512];
891         int eof_flag = 0;
892
893         num_urls = 0;   /* Start with a clean slate of embedded URL's */
894
895         if (starting_lp >= 0) {
896                 lines_printed = starting_lp;
897         }
898         strcpy(aaa, "");
899         old = 255;
900         strcpy(buffer, "");
901         c = 1;                  /* c is the current pos */
902
903         sigcaught = 0;
904         sttybbs(1);
905
906 FMTA:   while ((eof_flag == 0) && (strlen(buffer) < 126)) {
907         
908                 if (sigcaught)
909                         goto OOPS;
910                 if (fp != NULL) {       /* read from file */
911                         if (feof(fp))
912                                 eof_flag = 1;
913                         if (eof_flag == 0) {
914                                 a = getc(fp);
915                                 buffer[strlen(buffer) + 1] = 0;
916                                 buffer[strlen(buffer)] = a;
917                         }
918                 } else {        /* read from server */
919                         d = strlen(buffer);
920                         serv_gets(&buffer[d]);
921                         while ((!isspace(buffer[d])) && (isspace(buffer[strlen(buffer) - 1])))
922                                 buffer[strlen(buffer) - 1] = 0;
923                         if (!strcmp(&buffer[d], "000")) {
924                                 buffer[d] = 0;
925                                 eof_flag = 1;
926                                 while (isspace(buffer[strlen(buffer) - 1]))
927                                         buffer[strlen(buffer) - 1] = 0;
928                         }
929                         d = strlen(buffer);
930                         buffer[d] = 10;
931                         buffer[d + 1] = 0;
932                 }
933         }
934
935         if ( (!struncmp(buffer, "http://", 7))
936            || (!struncmp(buffer, "ftp://", 6)) ) {
937                 safestrncpy(urls[num_urls], buffer, 255);
938                 for (a=0; a<strlen(urls[num_urls]); ++a) {
939                         b = urls[num_urls][a];
940                         if ( (b==' ') || (b==')') || (b=='>') || (b==10)
941                            || (b==13) || (b==9) )
942                                 urls[num_urls][a] = 0;
943                 }
944                 ++num_urls;
945         }
946
947         buffer[strlen(buffer) + 1] = 0;
948         a = buffer[0];
949         strcpy(buffer, &buffer[1]);
950
951         old = real;
952         real = a;
953         if (a <= 0)
954                 goto FMTEND;
955
956         if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
957                 a = 32;
958         if (((old == 13) || (old == 10)) && (isspace(real))) {
959                 printf("\n");
960                 ++lines_printed;
961                 lines_printed = checkpagin(lines_printed, pagin, height);
962                 c = 1;
963         }
964         if (a > 126)
965                 goto FMTA;
966
967         if (a > 32) {
968                 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
969                         printf("\n%s", aaa);
970                         c = strlen(aaa);
971                         aaa[0] = 0;
972                         ++lines_printed;
973                         lines_printed = checkpagin(lines_printed, pagin, height);
974                 }
975                 b = strlen(aaa);
976                 aaa[b] = a;
977                 aaa[b + 1] = 0;
978         }
979         if (a == 32) {
980                 if ((strlen(aaa) + c) > (width - 5)) {
981                         c = 1;
982                         printf("\n");
983                         ++lines_printed;
984                         lines_printed = checkpagin(lines_printed, pagin, height);
985                 }
986                 printf("%s ", aaa);
987                 ++c;
988                 c = c + strlen(aaa);
989                 strcpy(aaa, "");
990                 goto FMTA;
991         }
992         if ((a == 13) || (a == 10)) {
993                 printf("%s\n", aaa);
994                 c = 1;
995                 ++lines_printed;
996                 lines_printed = checkpagin(lines_printed, pagin, height);
997                 strcpy(aaa, "");
998                 goto FMTA;
999         }
1000         goto FMTA;
1001
1002         /* signal caught; drain the server */
1003       OOPS:do {
1004                 serv_gets(aaa);
1005         } while (strcmp(aaa, "000"));
1006
1007       FMTEND:printf("\n");
1008         ++lines_printed;
1009         lines_printed = checkpagin(lines_printed, pagin, height);
1010         return (sigcaught);
1011 }
1012
1013
1014 /*
1015  * support ANSI color if defined
1016  */
1017 void color(int colornum)
1018 {
1019         static int is_bold = 0;
1020         static int hold_color, current_color;
1021
1022         if (colornum == COLOR_PUSH) {
1023                 hold_color = current_color;
1024                 return;
1025         }
1026
1027         if (colornum == COLOR_POP) {
1028                 color(hold_color);
1029                 return;
1030         }
1031
1032         current_color = colornum;
1033         if (enable_color) {
1034                 printf("\033[3%dm", (colornum % 8));
1035                 if ((colornum >= 8) && (is_bold == 0)) {
1036                         printf("\033[1m");
1037                         is_bold = 1;
1038                 } else if ((colornum < 8) && (is_bold == 1)) {
1039                         printf("\033[0m");
1040                         is_bold = 0;
1041                 }
1042                 fflush(stdout);
1043         }
1044 }
1045
1046 void cls(int colornum)
1047 {
1048         if (enable_color) {
1049                 printf("\033[4%dm\033[2J\033[H\033[0m", colornum);
1050                 fflush(stdout);
1051         }
1052 }
1053
1054
1055 /*
1056  * Detect whether ANSI color is available (answerback)
1057  */
1058 void send_ansi_detect(void)
1059 {
1060         if (rc_ansi_color == 2) {
1061                 printf("\033[c");
1062                 fflush(stdout);
1063                 time(&AnsiDetect);
1064         }
1065 }
1066
1067 void look_for_ansi(void)
1068 {
1069         fd_set rfds;
1070         struct timeval tv;
1071         char abuf[512];
1072         time_t now;
1073         int a;
1074
1075         if (rc_ansi_color == 0) {
1076                 enable_color = 0;
1077         } else if (rc_ansi_color == 1) {
1078                 enable_color = 1;
1079         } else if (rc_ansi_color == 2) {
1080
1081                 /* otherwise, do the auto-detect */
1082
1083                 strcpy(abuf, "");
1084
1085                 time(&now);
1086                 if ((now - AnsiDetect) < 2)
1087                         sleep(1);
1088
1089                 do {
1090                         FD_ZERO(&rfds);
1091                         FD_SET(0, &rfds);
1092                         tv.tv_sec = 0;
1093                         tv.tv_usec = 1;
1094
1095                         select(1, &rfds, NULL, NULL, &tv);
1096                         if (FD_ISSET(0, &rfds)) {
1097                                 abuf[strlen(abuf) + 1] = 0;
1098                                 read(0, &abuf[strlen(abuf)], 1);
1099                         }
1100                 } while (FD_ISSET(0, &rfds));
1101
1102                 for (a = 0; a < strlen(abuf); ++a) {
1103                         if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1104                             && (abuf[a + 2] == '?')) {
1105                                 enable_color = 1;
1106                         }
1107                 }
1108         }
1109 }
1110
1111
1112 /*
1113  * Display key options (highlight hotkeys inside angle brackets)
1114  */
1115 void keyopt(char *buf) {
1116         int i;
1117
1118         color(DIM_WHITE);
1119         for (i=0; i<strlen(buf); ++i) {
1120                 if (buf[i]=='<') {
1121                         putc(buf[i], stdout);
1122                         color(BRIGHT_MAGENTA);
1123                 } else {
1124                         if (buf[i]=='>') {
1125                                 color(DIM_WHITE);
1126                         }
1127                         putc(buf[i], stdout);
1128                 }
1129         }
1130         color(DIM_WHITE);
1131 }