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