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