* support autoconf 2.53
[citadel.git] / citadel / citadel.c
1 /*
2  * $Id$
3  *
4  * Main source module for the client program.
5  */
6
7 #include "sysdep.h"
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <stdio.h>
12 #include <ctype.h>
13 #include <string.h>
14
15 #if TIME_WITH_SYS_TIME
16 # include <sys/time.h>
17 # include <time.h>
18 #else
19 # if HAVE_SYS_TIME_H
20 #  include <sys/time.h>
21 # else
22 #  include <time.h>
23 # endif
24 #endif
25
26 #include <limits.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <sys/stat.h>
30 #include <sys/ioctl.h>
31 #include <signal.h>
32 #include <pwd.h>
33 #include <setjmp.h>
34 #include <stdarg.h>
35
36 #include "citadel.h"
37 #include "axdefs.h"
38 #include "serv_info.h"
39 #include "routines.h"
40 #include "routines2.h"
41 #include "rooms.h"
42 #include "messages.h"
43 #include "commands.h"
44 #include "ipc.h"
45 #include "client_chat.h"
46 #include "client_passwords.h"
47 #include "citadel_decls.h"
48 #include "tools.h"
49 #include "acconfig.h"
50 #include "client_crypto.h"
51 #ifndef HAVE_SNPRINTF
52 #include "snprintf.h"
53 #endif
54 #include "screen.h"
55
56 #include "md5.h"
57
58 struct march {
59         struct march *next;
60         char march_name[ROOMNAMELEN];
61         char march_floor;
62         char march_order;
63         };
64
65 #define IFEXPERT if (userflags&US_EXPERT)
66 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
67 #define IFAIDE if (axlevel>=6)
68 #define IFNAIDE if (axlevel<6)
69
70 struct march *march = NULL;
71
72 /* globals associated with the client program */
73 char temp[PATH_MAX];            /* Name of general temp file */
74 char temp2[PATH_MAX];           /* Name of general temp file */
75 char tempdir[PATH_MAX];         /* Name of general temp dir */
76 char editor_path[SIZ];          /* path to external editor */
77 char printcmd[SIZ];             /* print command */
78 int editor_pid = (-1);
79 char fullname[32];
80 jmp_buf nextbuf;
81 struct CtdlServInfo serv_info;  /* Info on the server connected */
82 int screenwidth;
83 int screenheight;
84 unsigned room_flags;
85 char room_name[ROOMNAMELEN];
86 char *uglist[UGLISTLEN]; /* size of the ungoto list */
87 long uglistlsn[UGLISTLEN]; /* current read position for all the ungoto's. Not going to make any friends with this one. */
88 int uglistsize = 0;
89 char is_mail = 0;               /* nonzero when we're in a mail room */
90 char axlevel = 0;               /* access level */
91 char is_room_aide = 0;          /* boolean flag, 1 if room aide */
92 int timescalled;
93 int posted;
94 unsigned userflags;
95 long usernum = 0L;              /* user number */
96 time_t lastcall = 0L;           /* Date/time of previous login */
97 char newnow;
98 long highest_msg_read;          /* used for <A>bandon room cmd */
99 long maxmsgnum;                 /* used for <G>oto */
100 char sigcaught = 0;
101 char have_xterm = 0;            /* are we running on an xterm? */
102 char rc_username[32];
103 char rc_password[32];
104 char hostbuf[SIZ];
105 char portbuf[SIZ];
106 char rc_floor_mode;
107 char floor_mode;
108 char curr_floor = 0;            /* number of current floor */
109 char floorlist[128][SIZ];       /* names of floors */
110 char express_msgs = 0;          /* express messages waiting! */
111 int termn8 = 0;                 /* Set to nonzero to cause a logoff */
112 int secure;                     /* Set to nonzero when wire is encrypted */
113
114 extern int rc_ansi_color;       /* ansi color value from citadel.rc */
115
116 /*
117  * here is our 'clean up gracefully and exit' routine
118  */
119 void logoff(int code)
120 {
121     int lp;
122         if (editor_pid > 0) {   /* kill the editor if it's running */
123                 kill(editor_pid, SIGHUP);
124         }
125
126     /* Free the ungoto list */
127     for (lp = 0; lp < uglistsize; lp++)
128       free (uglist[lp]);
129
130 /* shut down the server... but not if the logoff code is 3, because
131  * that means we're exiting because we already lost the server
132  */
133         if (code != 3)
134                 serv_puts("QUIT");
135
136 /*
137  * now clean up various things
138  */
139
140         screen_delete();
141
142         unlink(temp);
143         unlink(temp2);
144         nukedir(tempdir);
145
146         /* Violently kill off any child processes if Citadel is
147          * the login shell. 
148          */
149         if (getppid() == 1) {
150                 kill(0 - getpgrp(), SIGTERM);
151                 sleep(1);
152                 kill(0 - getpgrp(), SIGKILL);
153         }
154         sttybbs(SB_RESTORE);    /* return the old terminal settings */
155         exit(code);             /* exit with the proper exit code */
156 }
157
158
159
160 /*
161  * signal catching function for hangups...
162  */
163 void dropcarr(int signum)
164 {
165         logoff(SIGHUP);
166 }
167
168
169
170 /*
171  * catch SIGCONT to reset terminal modes when were are put back into the
172  * foreground.
173  */
174 void catch_sigcont(int signum)
175 {
176         sttybbs(SB_LAST);
177         signal(SIGCONT, catch_sigcont);
178 }
179
180
181
182 /* general purpose routines */
183
184 void formout(char *name)
185 {                               /* display a file */
186         char cmd[SIZ];
187         snprintf(cmd, sizeof cmd, "MESG %s", name);
188         serv_puts(cmd);
189         serv_gets(cmd);
190         if (cmd[0] != '1') {
191                 scr_printf("%s\n", &cmd[4]);
192                 return;
193         }
194         fmout(screenwidth, NULL, NULL,
195               ((userflags & US_PAGINATOR) ? 1 : 0),
196               screenheight, 1, 1);
197 }
198
199
200 void userlist(char *patn)
201 {
202         char buf[SIZ];
203         char fl[SIZ];
204         struct tm *tmbuf;
205         time_t lc;
206
207         serv_puts("LIST");
208         serv_gets(buf);
209         if (buf[0] != '1') {
210                 pprintf("%s\n", &buf[4]);
211                 return;
212         }
213         pprintf("       User Name           Num  L  LastCall  Calls Posts\n");
214         pprintf("------------------------- ----- - ---------- ----- -----\n");
215         while (serv_gets(buf), strcmp(buf, "000")) {
216                 if (sigcaught == 0) {
217                     extract(fl, buf, 0);
218                     if (pattern(fl, patn) >= 0) {
219                         pprintf("%-25s ", fl);
220                         pprintf("%5ld %d ", extract_long(buf, 2),
221                                extract_int(buf, 1));
222                         lc = extract_long(buf, 3);
223                         tmbuf = (struct tm *) localtime(&lc);
224                         pprintf("%02d/%02d/%04d ",
225                                (tmbuf->tm_mon + 1),
226                                tmbuf->tm_mday,
227                                (tmbuf->tm_year + 1900));
228                         pprintf("%5ld %5ld\n", extract_long(buf, 4), extract_long(buf, 5));
229                     }
230
231                 }
232         }
233         pprintf("\n");
234 }
235
236
237 /*
238  * grab assorted info about the user...
239  */
240 void load_user_info(char *params)
241 {
242         extract(fullname, params, 0);
243         axlevel = extract_int(params, 1);
244         timescalled = extract_int(params, 2);
245         posted = extract_int(params, 3);
246         userflags = extract_int(params, 4);
247         usernum = extract_long(params, 5);
248         lastcall = extract_long(params, 6);
249 }
250
251
252 /*
253  * Remove a room from the march list.  'floornum' is ignored unless
254  * 'roomname' is set to _FLOOR_, in which case all rooms on the requested
255  * floor will be removed from the march list.
256  */
257 void remove_march(char *roomname, int floornum)
258 {
259         struct march *mptr, *mptr2;
260
261         if (march == NULL)
262                 return;
263
264         if ((!strcasecmp(march->march_name, roomname))
265             || ((!strcasecmp(roomname, "_FLOOR_")) && (march->march_floor == floornum))) {
266                 mptr = march->next;
267                 free(march);
268                 march = mptr;
269                 return;
270         }
271         mptr2 = march;
272         for (mptr = march; mptr != NULL; mptr = mptr->next) {
273
274                 if ((!strcasecmp(mptr->march_name, roomname))
275                     || ((!strcasecmp(roomname, "_FLOOR_"))
276                         && (mptr->march_floor == floornum))) {
277
278                         mptr2->next = mptr->next;
279                         free(mptr);
280                         mptr = mptr2;
281                 } else {
282                         mptr2 = mptr;
283                 }
284         }
285 }
286
287
288 /*
289  * Locate the room on the march list which we most want to go to.  Each room
290  * is measured given a "weight" of preference based on various factors.
291  */
292 char *pop_march(int desired_floor)
293 {
294         static char TheRoom[ROOMNAMELEN];
295         int TheFloor = 0;
296         int TheOrder = 32767;
297         int TheWeight = 0;
298         int weight;
299         struct march *mptr = NULL;
300
301         strcpy(TheRoom, "_BASEROOM_");
302         if (march == NULL)
303                 return (TheRoom);
304
305         for (mptr = march; mptr != NULL; mptr = mptr->next) {
306                 weight = 0;
307                 if ((strcasecmp(mptr->march_name, "_BASEROOM_")))
308                         weight = weight + 10000;
309                 if (mptr->march_floor == desired_floor)
310                         weight = weight + 5000;
311
312                 weight = weight + ((128 - (mptr->march_floor)) * 128);
313                 weight = weight + (128 - (mptr->march_order));
314
315                 if (weight > TheWeight) {
316                         TheWeight = weight;
317                         strcpy(TheRoom, mptr->march_name);
318                         TheFloor = mptr->march_floor;
319                         TheOrder = mptr->march_order;
320                 }
321         }
322         return (TheRoom);
323 }
324
325
326 /*
327  * jump directly to a room
328  */
329 void dotgoto(char *towhere, int display_name, int fromungoto)
330 {
331         char aaa[SIZ], bbb[SIZ], psearch[SIZ];
332         static long ls = 0L;
333         int newmailcount = 0;
334         static int oldmailcount = (-1);
335         int partial_match, best_match;
336         char from_floor;
337     int ugpos = uglistsize;
338
339         /* store ungoto information */
340     if (fromungoto == 0)
341       {
342         if (uglistsize >= (UGLISTLEN-1))
343           {  /* sloppy slide them all down, hey it's the client, who cares. :-) */
344             int lp;
345             free (uglist[0]);
346             for (lp = 0; lp < (UGLISTLEN-1); lp++)
347               {
348                 uglist[lp] = uglist[lp+1];
349                 uglistlsn[lp] = uglistlsn[lp+1];
350               }
351             ugpos--;
352           }
353         else
354           uglistsize++;
355         
356         uglist[ugpos] = malloc(strlen(room_name)+1);
357         strcpy(uglist[ugpos], room_name);
358         uglistlsn[ugpos] = ls;
359       }
360       
361         /* first try an exact match */
362         snprintf(aaa, sizeof aaa, "GOTO %s", towhere);
363         serv_puts(aaa);
364         serv_gets(aaa);
365         if (aaa[3] == '*')
366                 express_msgs = 1;
367         if (!strncmp(aaa, "54", 2)) {
368                 newprompt("Enter room password: ", bbb, 9);
369                 snprintf(aaa, sizeof aaa, "GOTO %s|%s", towhere, bbb);
370                 serv_puts(aaa);
371                 serv_gets(aaa);
372                 if (aaa[3] == '*')
373                         express_msgs = 1;
374         }
375         if (!strncmp(aaa, "54", 2)) {
376                 scr_printf("Wrong password.\n");
377                 return;
378         }
379         /*
380          * If a match is not found, try a partial match.
381          * Partial matches anywhere in the string carry a weight of 1,
382          * left-aligned matches carry a weight of 2.  Pick the room that
383          * has the highest-weighted match.
384          */
385         if (aaa[0] != '2') {
386                 best_match = 0;
387                 strcpy(bbb, "");
388                 serv_puts("LKRA");
389                 serv_gets(aaa);
390                 if (aaa[0] == '1')
391                         while (serv_gets(aaa), strcmp(aaa, "000")) {
392                                 extract(psearch, aaa, 0);
393                                 partial_match = 0;
394                                 if (pattern(psearch, towhere) >= 0) {
395                                         partial_match = 1;
396                                 }
397                                 if (!strncasecmp(towhere, psearch, strlen(towhere))) {
398                                         partial_match = 2;
399                                 }
400                                 if (partial_match > best_match) {
401                                         strcpy(bbb, psearch);
402                                         best_match = partial_match;
403                                 }
404                         }
405                 if (strlen(bbb) == 0) {
406                         scr_printf("No room '%s'.\n", towhere);
407                         return;
408                 }
409                 snprintf(aaa, sizeof aaa, "GOTO %s", bbb);
410                 serv_puts(aaa);
411                 serv_gets(aaa);
412                 if (aaa[3] == '*')
413                         express_msgs = 1;
414         }
415         if (aaa[0] != '2') {
416                 scr_printf("%s\n", aaa);
417                 return;
418         }
419         extract(room_name, &aaa[4], 0);
420         room_flags = extract_int(&aaa[4], 4);
421         from_floor = curr_floor;
422         curr_floor = extract_int(&aaa[4], 10);
423
424         remove_march(room_name, 0);
425         if (!strcasecmp(towhere, "_BASEROOM_"))
426                 remove_march(towhere, 0);
427         if ((from_floor != curr_floor) && (display_name > 0) && (floor_mode == 1)) {
428                 if (floorlist[(int) curr_floor][0] == 0)
429                         load_floorlist();
430                 scr_printf("(Entering floor: %s)\n", &floorlist[(int) curr_floor][0]);
431         }
432         if (display_name == 1) {
433                 color(BRIGHT_WHITE);
434                 scr_printf("%s ", room_name);
435                 color(DIM_WHITE);
436                 scr_printf("- ");
437         }
438         if (display_name != 2) {
439                 color(BRIGHT_YELLOW);
440                 scr_printf("%d ", extract_int(&aaa[4], 1));
441                 color(DIM_WHITE);
442                 scr_printf("new of ");
443                 color(BRIGHT_YELLOW);
444                 scr_printf("%d ", extract_int(&aaa[4], 2));
445                 color(DIM_WHITE);
446                 scr_printf("messages.\n");
447         }
448         highest_msg_read = extract_int(&aaa[4], 6);
449         maxmsgnum = extract_int(&aaa[4], 5);
450         is_mail = (char) extract_int(&aaa[4], 7);
451         is_room_aide = (char) extract_int(&aaa[4], 8);
452         ls = extract_long(&aaa[4], 6);
453
454         /* read info file if necessary */
455         if (extract_int(&aaa[4], 3) > 0)
456                 readinfo();
457
458         /* check for newly arrived mail if we can */
459         if (num_parms(&aaa[4]) >= 10) {
460                 newmailcount = extract_int(&aaa[4], 9);
461                 if ((oldmailcount >= 0) && (newmailcount > oldmailcount)) {
462                         color(BRIGHT_RED);
463                         scr_printf("*** You have new mail\n");
464                         color(DIM_WHITE);
465                 }
466                 oldmailcount = newmailcount;
467                 status_line(serv_info.serv_humannode, serv_info.serv_bbs_city,
468                                 room_name, secure, newmailcount);
469         } else {
470                 status_line(serv_info.serv_humannode, serv_info.serv_bbs_city,
471                                 room_name, secure, -1);
472         }
473 }
474
475 /* Goto next room having unread messages.
476  * We want to skip over rooms that the user has already been to, and take the
477  * user back to the lobby when done.  The room we end up in is placed in
478  * newroom - which is set to 0 (the lobby) initially.
479  */
480 void gotonext(void)
481 {
482         char buf[SIZ];
483         struct march *mptr, *mptr2;
484         char next_room[ROOMNAMELEN];
485
486         /* Check to see if the march-mode list is already allocated.
487          * If it is, pop the first room off the list and go there.
488          */
489         if (march == NULL) {
490                 serv_puts("LKRN");
491                 serv_gets(buf);
492                 if (buf[0] == '1')
493                         while (serv_gets(buf), strcmp(buf, "000")) {
494                                 mptr = (struct march *) malloc(sizeof(struct march));
495                                 mptr->next = NULL;
496                                 extract(mptr->march_name, buf, 0);
497                                 mptr->march_floor = (char) (extract_int(buf, 2) & 0x7F);
498                                 mptr->march_order = (char) (extract_int(buf, 3) & 0x7F);
499                                 if (march == NULL) {
500                                         march = mptr;
501                                 } else {
502                                         mptr2 = march;
503                                         while (mptr2->next != NULL)
504                                                 mptr2 = mptr2->next;
505                                         mptr2->next = mptr;
506                                 }
507                         }
508 /* add _BASEROOM_ to the end of the march list, so the user will end up
509  * in the system base room (usually the Lobby>) at the end of the loop
510  */
511                 mptr = (struct march *) malloc(sizeof(struct march));
512                 mptr->next = NULL;
513                 strcpy(mptr->march_name, "_BASEROOM_");
514                 if (march == NULL) {
515                         march = mptr;
516                 } else {
517                         mptr2 = march;
518                         while (mptr2->next != NULL)
519                                 mptr2 = mptr2->next;
520                         mptr2->next = mptr;
521                 }
522 /*
523  * ...and remove the room we're currently in, so a <G>oto doesn't make us
524  * walk around in circles
525  */
526                 remove_march(room_name, 0);
527         }
528         if (march != NULL) {
529                 strcpy(next_room, pop_march(curr_floor));
530         } else {
531                 strcpy(next_room, "_BASEROOM_");
532         }
533         remove_march(next_room, 0);
534         dotgoto(next_room, 1, 0);
535 }
536
537 /*
538  * forget all rooms on a given floor
539  */
540 void forget_all_rooms_on(int ffloor)
541 {
542         char buf[SIZ];
543         struct march *flist, *fptr;
544
545         scr_printf("Forgetting all rooms on %s...\r", &floorlist[ffloor][0]);
546         scr_flush();
547         snprintf(buf, sizeof buf, "LKRA %d", ffloor);
548         serv_puts(buf);
549         serv_gets(buf);
550         if (buf[0] != '1') {
551                 scr_printf("%-72s\n", &buf[4]);
552                 return;
553         }
554         flist = NULL;
555         while (serv_gets(buf), strcmp(buf, "000")) {
556                 fptr = (struct march *) malloc(sizeof(struct march));
557                 fptr->next = flist;
558                 flist = fptr;
559                 extract(fptr->march_name, buf, 0);
560         }
561         while (flist != NULL) {
562                 snprintf(buf, sizeof buf, "GOTO %s", flist->march_name);
563                 serv_puts(buf);
564                 serv_gets(buf);
565                 if (buf[0] == '2') {
566                         serv_puts("FORG");
567                         serv_gets(buf);
568                 }
569                 fptr = flist;
570                 flist = flist->next;
571                 free(fptr);
572         }
573         scr_printf("%-72s\r", "");
574 }
575
576
577 /*
578  * routine called by gotofloor() to move to a new room on a new floor
579  */
580 void gf_toroom(char *towhere, int mode)
581 {
582         int floor_being_left;
583
584         floor_being_left = curr_floor;
585
586         if (mode == GF_GOTO) {  /* <;G>oto mode */
587                 updatels();
588                 dotgoto(towhere, 1, 0);
589         }
590         if (mode == GF_SKIP) {  /* <;S>kip mode */
591                 dotgoto(towhere, 1, 0);
592                 remove_march("_FLOOR_", floor_being_left);
593         }
594         if (mode == GF_ZAP) {   /* <;Z>ap mode */
595                 dotgoto(towhere, 1, 0);
596                 remove_march("_FLOOR_", floor_being_left);
597                 forget_all_rooms_on(floor_being_left);
598         }
599 }
600
601
602 /*
603  * go to a new floor
604  */
605 void gotofloor(char *towhere, int mode)
606 {
607         int a, tofloor;
608         struct march *mptr;
609         char buf[SIZ], targ[SIZ];
610
611         if (floorlist[0][0] == 0)
612                 load_floorlist();
613         tofloor = (-1);
614         for (a = 0; a < 128; ++a)
615                 if (!strcasecmp(&floorlist[a][0], towhere))
616                         tofloor = a;
617
618         if (tofloor < 0) {
619                 for (a = 0; a < 128; ++a) {
620                         if (!strncasecmp(&floorlist[a][0], towhere, strlen(towhere))) {
621                                 tofloor = a;
622                         }
623                 }
624         }
625         if (tofloor < 0) {
626                 for (a = 0; a < 128; ++a)
627                         if (pattern(towhere, &floorlist[a][0]) > 0)
628                                 tofloor = a;
629         }
630         if (tofloor < 0) {
631                 scr_printf("No floor '%s'.\n", towhere);
632                 return;
633         }
634         for (mptr = march; mptr != NULL; mptr = mptr->next) {
635                 if ((mptr->march_floor) == tofloor)
636                         gf_toroom(mptr->march_name, mode);
637                 return;
638         }
639
640         strcpy(targ, "");
641         snprintf(buf, sizeof buf, "LKRA %d", tofloor);
642         serv_puts(buf);
643         serv_gets(buf);
644         if (buf[0] == '1')
645                 while (serv_gets(buf), strcmp(buf, "000")) {
646                         if ((extract_int(buf, 2) == tofloor) && (strlen(targ) == 0))
647                                 extract(targ, buf, 0);
648                 }
649         if (strlen(targ) > 0) {
650                 gf_toroom(targ, mode);
651         } else {
652                 scr_printf("There are no rooms on '%s'.\n", &floorlist[tofloor][0]);
653         }
654 }
655
656
657 /*
658  * forget all rooms on current floor
659  */
660 void forget_this_floor(void)
661 {
662
663         if (curr_floor == 0) {
664                 scr_printf("Can't forget this floor.\n");
665                 return;
666         }
667         if (floorlist[0][0] == 0)
668                 load_floorlist();
669         scr_printf("Are you sure you want to forget all rooms on %s? ",
670                &floorlist[(int) curr_floor][0]);
671         if (yesno() == 0)
672                 return;
673
674         gf_toroom("_BASEROOM_", GF_ZAP);
675 }
676
677
678 /* 
679  * Figure out the physical screen dimensions, if we can
680  * WARNING:  this is now called from a signal handler!
681  */
682 void check_screen_dims(void)
683 {
684 #ifdef TIOCGWINSZ
685         struct {
686                 unsigned short height;  /* rows */
687                 unsigned short width;   /* columns */
688                 unsigned short xpixels;
689                 unsigned short ypixels;         /* pixels */
690         } xwinsz;
691
692         if (have_xterm) {       /* dynamically size screen if on an xterm */
693                 if (ioctl(0, TIOCGWINSZ, &xwinsz) == 0) {
694                         if (xwinsz.height)
695                                 screenheight = is_curses_enabled() ? (int)xwinsz.height - 1 : (int) xwinsz.height;
696                         if (xwinsz.width)
697                                 screenwidth = (int) xwinsz.width;
698                 }
699         }
700 #endif
701 }
702
703
704 /*
705  * set floor mode depending on client, server, and user settings
706  */
707 void set_floor_mode(void)
708 {
709         if (serv_info.serv_ok_floors == 0) {
710                 floor_mode = 0; /* Don't use floors if the server */
711         }
712         /* doesn't support them!          */
713         else {
714                 if (rc_floor_mode == RC_NO) {   /* never use floors */
715                         floor_mode = 0;
716                 }
717                 if (rc_floor_mode == RC_YES) {  /* always use floors */
718                         floor_mode = 1;
719                 }
720                 if (rc_floor_mode == RC_DEFAULT) {      /* user choice */
721                         floor_mode = ((userflags & US_FLOORS) ? 1 : 0);
722                 }
723         }
724 }
725
726 /*
727  * Set or change the user's password
728  */
729 int set_password(void)
730 {
731         char pass1[20];
732         char pass2[20];
733         char buf[SIZ];
734
735         if (strlen(rc_password) > 0) {
736                 strcpy(pass1, rc_password);
737                 strcpy(pass2, rc_password);
738         } else {
739                 IFNEXPERT formout("changepw");
740                 newprompt("Enter a new password: ", pass1, -19);
741                 newprompt("Enter it again to confirm: ", pass2, -19);
742         }
743         strproc(pass1);
744         strproc(pass2);
745         if (!strcasecmp(pass1, pass2)) {
746                 snprintf(buf, sizeof buf, "SETP %s", pass1);
747                 serv_puts(buf);
748                 serv_gets(buf);
749                 scr_printf("%s\n", &buf[4]);
750                 offer_to_remember_password(hostbuf, portbuf, fullname, pass1);
751                 return (0);
752         } else {
753                 scr_printf("*** They don't match... try again.\n");
754                 return (1);
755         }
756 }
757
758
759
760 /*
761  * get info about the server we've connected to
762  */
763 void get_serv_info(char *supplied_hostname)
764 {
765         char buf[512];
766
767         CtdlInternalGetServInfo(&serv_info);
768
769         /* be nice and identify ourself to the server */
770         snprintf(buf, sizeof buf, "IDEN %d|%d|%d|%s|",
771                  SERVER_TYPE, 0, REV_LEVEL,
772                  (server_is_local ? "local" : CITADEL));
773
774         /* Append a hostname */
775         if (supplied_hostname != NULL) {
776                 strcat(buf, supplied_hostname);
777         }
778         else {
779                 locate_host(&buf[strlen(buf)]); /* append to the end */
780         }
781
782         serv_puts(buf);
783         serv_gets(buf);         /* we don't care about the result code */
784 }
785
786
787
788
789
790 /*
791  * Display list of users currently logged on to the server
792  */
793 void who_is_online(int longlist)
794 {
795         char buf[SIZ], username[SIZ], roomname[SIZ], fromhost[SIZ];
796         char flags[SIZ];
797         char actual_user[SIZ], actual_room[SIZ], actual_host[SIZ];
798         char tbuf[SIZ], clientsoft[SIZ];
799         time_t timenow = 0;
800         time_t idletime, idlehours, idlemins, idlesecs;
801         int last_session = (-1);
802     int skipidle = 0;
803     
804     if (longlist == 2)
805       {
806         longlist = 0;
807         skipidle = 1;
808       }
809
810         serv_puts("TIME");
811         serv_gets(tbuf);
812         if (tbuf[0] == '2') {
813                 timenow = extract_long(&tbuf[4], 0);
814         }
815         else {
816                 time(&timenow);
817         }
818
819         if (!longlist) {
820                 color(BRIGHT_WHITE);
821                 pprintf("FLG ###        User Name                 Room                 From host\n");
822                 color(DIM_WHITE);
823                 pprintf("--- --- ------------------------- -------------------- ------------------------\n");
824         }
825         serv_puts("RWHO");
826         serv_gets(buf);
827         if (buf[0] == '1') {
828                 while (serv_gets(buf), strcmp(buf, "000")) {
829                 int isidle = 0;
830                         extract(username, buf, 1);
831                         extract(roomname, buf, 2);
832                         extract(fromhost, buf, 3);
833                         extract(clientsoft, buf, 4);
834                         extract(flags, buf, 7);
835
836                         idletime = timenow - extract_long(buf, 5);
837                         idlehours = idletime / 3600;
838                         idlemins = (idletime - (idlehours * 3600)) / 60;
839                         idlesecs = (idletime - (idlehours * 3600) - (idlemins * 60));
840
841                         if (idletime > 900) {
842                                 while (strlen(roomname) < 20) {
843                                         strcat(roomname, " ");
844                                 }
845                                 strcpy(&roomname[14], "[idle]");
846                         if (skipidle)
847                   isidle = 1;
848                         }
849
850                         if (longlist) {
851
852                                 extract(actual_user, buf, 8);
853                                 extract(actual_room, buf, 9);
854                                 extract(actual_host, buf, 10);
855
856                                 pprintf("  Flags: %s\n", flags);
857                                 pprintf("Session: %d\n", extract_int(buf, 0));
858                                 pprintf("   Name: %s\n", username);
859                                 pprintf("In room: %s\n", roomname);
860                                 pprintf("   Host: %s\n", fromhost);
861                                 pprintf(" Client: %s\n", clientsoft);
862                                 pprintf("   Idle: %ld:%02ld:%02ld\n",
863                                         (long) idlehours,
864                                         (long) idlemins,
865                                         (long) idlesecs);
866
867                                 if ( (strlen(actual_user)+strlen(actual_room)+strlen(actual_host)) > 0) {
868                                         pprintf("(really ");
869                                         if (strlen(actual_user)>0) pprintf("<%s> ", actual_user);
870                                         if (strlen(actual_room)>0) pprintf("in <%s> ", actual_room);
871                                         if (strlen(actual_host)>0) pprintf("from <%s> ", actual_host);
872                                         pprintf(")\n");
873                                 }
874                                 pprintf("\n");
875
876                         } else {
877                     if (isidle == 0) {
878                                 if (extract_int(buf, 0) == last_session) {
879                                         pprintf("        ");
880                                 } else {
881                                         color(BRIGHT_MAGENTA);
882                                         pprintf("%-3s ", flags);
883                                         color(DIM_WHITE);
884                                         pprintf("%-3d ", extract_int(buf, 0));
885                                 }
886                                 last_session = extract_int(buf, 0);
887                                 color(BRIGHT_CYAN);
888                                 pprintf("%-25s ", username);
889                                 color(BRIGHT_MAGENTA);
890                                 roomname[20] = 0;
891                                 pprintf("%-20s ", roomname);
892                                 color(BRIGHT_CYAN);
893                                 fromhost[24] = '\0';
894                                 pprintf("%-24s\n", fromhost);
895                                 color(DIM_WHITE);
896                         }
897                         }
898                 }
899         }
900 }
901
902 void enternew(char *desc, char *buf, int maxlen)
903 {
904         char bbb[128];
905         snprintf(bbb, sizeof bbb, "Enter in your new %s: ", desc);
906         newprompt(bbb, buf, maxlen);
907 }
908
909
910
911 int shift(int argc, char **argv, int start, int count) {
912         int i;
913
914         for (i=start; i<(argc-count); ++i) {
915                 argv[i] = argv[i+count];
916         }
917         argc = argc - count;
918         return argc;
919 }
920
921 static void statusHook(char *s) {
922         sln_printf(s);
923         sln_flush();
924 }
925
926 /*
927  * main
928  */
929 int main(int argc, char **argv)
930 {
931         int a, b, mcmd;
932         char aaa[100], bbb[100];/* general purpose variables */
933         char argbuf[32];        /* command line buf */
934         char nonce[NONCE_SIZE];
935         char *telnet_client_host = NULL;
936         char *sptr, *sptr2;     /* USed to extract the nonce */
937         char hexstring[MD5_HEXSTRING_SIZE];
938         int stored_password = 0;
939         char password[SIZ];
940
941         setIPCDeathHook(screen_delete);
942         setIPCErrorPrintf(err_printf);
943         setCryptoStatusHook(statusHook);
944         
945         /* Permissions sanity check - don't run citadel setuid/setgid */
946         if (getuid() != geteuid()) {
947                 err_printf("Please do not run citadel setuid!\n");
948                 logoff(3);
949         } else if (getgid() != getegid()) {
950                 err_printf("Please do not run citadel setgid!\n");
951                 logoff(3);
952         }
953
954         sttybbs(SB_SAVE);       /* Store the old terminal parameters */
955         load_command_set();     /* parse the citadel.rc file */
956         sttybbs(SB_NO_INTR);    /* Install the new ones */
957         signal(SIGHUP, dropcarr);       /* Cleanup gracefully if carrier is dropped */
958         signal(SIGTERM, dropcarr);      /* Cleanup gracefully if terminated */
959         signal(SIGCONT, catch_sigcont); /* Catch SIGCONT so we can reset terminal */
960 #ifdef SIGWINCH
961         signal(SIGWINCH, scr_winch);    /* Window resize signal */
962 #endif
963
964 #ifdef HAVE_OPENSSL
965         arg_encrypt = RC_DEFAULT;
966 #endif
967 #ifdef HAVE_CURSES_H
968         arg_screen = RC_DEFAULT;
969 #endif
970
971         /* 
972          * Handle command line options as if we were called like /bin/login
973          * (i.e. from in.telnetd)
974          */
975         for (a=0; a<argc; ++a) {
976                 if ((argc > a+1) && (!strcmp(argv[a], "-h")) ) {
977                         telnet_client_host = argv[a+1];
978                         argc = shift(argc, argv, a, 2);
979                 }
980                 if (!strcmp(argv[a], "-x")) {
981 #ifdef HAVE_OPENSSL
982                         arg_encrypt = RC_NO;
983 #endif
984                         argc = shift(argc, argv, a, 1);
985                 }
986                 if (!strcmp(argv[a], "-X")) {
987 #ifdef HAVE_OPENSSL
988                         arg_encrypt = RC_YES;
989                         argc = shift(argc, argv, a, 1);
990 #else
991                         fprintf(stderr, "Not compiled with encryption support");
992                         return 1;
993 #endif
994                 }
995                 if (!strcmp(argv[a], "-s")) {
996 #ifdef HAVE_CURSES_H
997                         arg_screen = RC_NO;
998 #endif
999                         argc = shift(argc, argv, a, 1);
1000                 }
1001                 if (!strcmp(argv[a], "-S")) {
1002 #ifdef HAVE_CURSES_H
1003                         arg_screen = RC_YES;
1004 #endif
1005                         argc = shift(argc, argv, a, 1);
1006                 }
1007                 if (!strcmp(argv[a], "-p")) {
1008                         struct stat st;
1009                 
1010                         if (chdir(BBSDIR) < 0) {
1011                                 perror("can't change to " BBSDIR);
1012                                 logoff(3);
1013                         }
1014
1015                         /*
1016                          * Drop privileges if necessary. We stat
1017                          * citadel.config to get the uid/gid since it's
1018                          * guaranteed to have the uid/gid we want.
1019                          */
1020                         if (!getuid() || !getgid()) {
1021                                 if (stat(BBSDIR "/citadel.config", &st) < 0) {
1022                                         perror("couldn't stat citadel.config");
1023                                         logoff(3);
1024                                 }
1025                                 if (!getgid() && (setgid(st.st_gid) < 0)) {
1026                                         perror("couldn't change gid");
1027                                         logoff(3);
1028                                 }
1029                                 if (!getuid() && (setuid(st.st_uid) < 0)) {
1030                                         perror("couldn't change uid");
1031                                         logoff(3);
1032                                 }
1033                                 /*
1034                                 scr_printf("Privileges changed to uid %d gid %d\n",
1035                                                 getuid(), getgid());
1036                                 */
1037                         }
1038                         argc = shift(argc, argv, a, 1);
1039                 }
1040         }
1041
1042         screen_new();
1043
1044         sln_printf("Attaching to server... \r");
1045         sln_flush();
1046         attach_to_server(argc, argv, hostbuf, portbuf);
1047
1048         serv_gets(aaa);
1049         if (aaa[0] != '2') {
1050                 scr_printf("%s\n", &aaa[4]);
1051                 logoff(atoi(aaa));
1052         }
1053
1054 /* If there is a [nonce] at the end, put the nonce in <nonce>, else nonce
1055  * is zeroized.
1056  */
1057         
1058         if ((sptr = strchr(aaa, '<')) == NULL)
1059         {
1060            nonce[0] = '\0';
1061         }
1062         else
1063         {
1064            if ((sptr2 = strchr(sptr, '>')) == NULL)
1065            {
1066               nonce[0] = '\0';
1067            }
1068            else
1069            {
1070               sptr2++;
1071               *sptr2 = '\0';
1072               strncpy(nonce, sptr, NONCE_SIZE);
1073            }
1074         }
1075
1076         get_serv_info(telnet_client_host);
1077
1078         scr_printf("%-24s\n%s\n%s\n", serv_info.serv_software, serv_info.serv_humannode,
1079                 serv_info.serv_bbs_city);
1080         scr_flush();
1081
1082         secure = starttls();
1083         status_line(serv_info.serv_humannode, serv_info.serv_bbs_city, NULL,
1084                         secure, -1);
1085
1086         screenwidth = 80;       /* default screen dimensions */
1087         screenheight = 24;
1088         
1089         scr_printf(" pause    next    stop\n");
1090         scr_printf(" ctrl-s  ctrl-o  ctrl-c\n\n");
1091         formout("hello");       /* print the opening greeting */
1092         scr_printf("\n");
1093
1094 GSTA:   /* See if we have a username and password on disk */
1095         if (rc_remember_passwords) {
1096                 get_stored_password(hostbuf, portbuf, fullname, password);
1097                 if (strlen(fullname) > 0) {
1098                         snprintf(aaa, sizeof(aaa)-1, "USER %s", fullname);
1099                         serv_puts(aaa);
1100                         serv_gets(aaa);
1101                         if (nonce[0])
1102                         {
1103                                 snprintf(aaa, sizeof aaa, "PAS2 %s", make_apop_string(password, nonce, hexstring, sizeof hexstring));
1104                         }
1105                         else    /* Else no APOP */
1106                         {
1107                                 snprintf(aaa, sizeof(aaa)-1, "PASS %s", password);
1108                         }
1109                         
1110                         serv_puts(aaa);
1111                         serv_gets(aaa);
1112                         if (aaa[0] == '2') {
1113                                 load_user_info(&aaa[4]);
1114                                 stored_password = 1;
1115                                 goto PWOK;
1116                         }
1117                         else {
1118                                 set_stored_password(hostbuf, portbuf, "", "");
1119                         }
1120                 }
1121         }
1122
1123         termn8 = 0;
1124         newnow = 0;
1125         do {
1126                 if (strlen(rc_username) > 0) {
1127                         strcpy(fullname, rc_username);
1128                 } else {
1129                         newprompt("Enter your name: ", fullname, 29);
1130                 }
1131                 strproc(fullname);
1132                 if (!strcasecmp(fullname, "new")) {     /* just in case */
1133                         scr_printf("Please enter the name you wish to log in with.\n");
1134                 }
1135         } while (
1136                         (!strcasecmp(fullname, "bbs"))
1137                         || (!strcasecmp(fullname, "new"))
1138                         || (strlen(fullname) == 0));
1139
1140         if (!strcasecmp(fullname, "off")) {
1141                 mcmd = 29;
1142                 goto TERMN8;
1143         }
1144         /* sign on to the server */
1145         snprintf(aaa, sizeof aaa, "USER %s", fullname);
1146         serv_puts(aaa);
1147         serv_gets(aaa);
1148         if (aaa[0] != '3')
1149                 goto NEWUSR;
1150
1151         /* password authentication */
1152         if (strlen(rc_password) > 0) {
1153                 strcpy(password, rc_password);
1154         } else {
1155                 newprompt("\rPlease enter your password: ", password, -19);
1156         }
1157         strproc(password);
1158
1159         if (nonce[0])
1160         {
1161                 snprintf(aaa, sizeof aaa, "PAS2 %s", make_apop_string(password, nonce, hexstring, sizeof hexstring));
1162         }
1163         else    /* Else no APOP */
1164         {
1165                 snprintf(aaa, sizeof aaa, "PASS %s", password);
1166         }
1167         
1168         serv_puts(aaa);
1169         serv_gets(aaa);
1170         if (aaa[0] == '2') {
1171                 load_user_info(&aaa[4]);
1172                 offer_to_remember_password(hostbuf, portbuf,
1173                                         fullname, password);
1174                 goto PWOK;
1175         }
1176         scr_printf("<< wrong password >>\n");
1177         if (strlen(rc_password) > 0)
1178                 logoff(0);
1179         goto GSTA;
1180
1181 NEWUSR: if (strlen(rc_password) == 0) {
1182                 scr_printf("No record. Enter as new user? ");
1183                 if (yesno() == 0)
1184                         goto GSTA;
1185         }
1186         snprintf(aaa, sizeof aaa, "NEWU %s", fullname);
1187         serv_puts(aaa);
1188         serv_gets(aaa);
1189         if (aaa[0] != '2') {
1190                 scr_printf("%s\n", aaa);
1191                 goto GSTA;
1192         }
1193         load_user_info(&aaa[4]);
1194
1195         while (set_password() != 0);;
1196         newnow = 1;
1197
1198         enter_config(1);
1199
1200 PWOK:
1201         /* Switch color support on or off if we're in user mode */
1202         if (rc_ansi_color == 3) {
1203                 if (userflags & US_COLOR)
1204                         enable_color = 1;
1205                 else
1206                         enable_color = 0;
1207         }
1208
1209         scr_printf("%s\nAccess level: %d (%s)\n"
1210                 "User #%ld / Login #%d",
1211                 fullname, axlevel, axdefs[(int) axlevel],
1212                 usernum, timescalled);
1213         if (lastcall > 0L) {
1214                 scr_printf(" / Last login: %s\n",
1215                         asctime(localtime(&lastcall)) );
1216         }
1217         scr_printf("\n");
1218
1219         serv_puts("CHEK");
1220         serv_gets(aaa);
1221         if (aaa[0] == '2') {
1222                 b = extract_int(&aaa[4], 0);
1223                 if (b > 0) {
1224                         color(BRIGHT_RED);
1225                         if (b == 1)
1226                                 scr_printf("*** You have a new private message in Mail>\n");
1227                         if (b > 1)
1228                                 scr_printf("*** You have %d new private messages in Mail>\n", b);
1229                         color(DIM_WHITE);
1230                 }
1231                 if ((axlevel >= 6) && (extract_int(&aaa[4], 2) > 0)) {
1232                         scr_printf("*** Users need validation\n");
1233                 }
1234                 if (extract_int(&aaa[4], 1) > 0) {
1235                         scr_printf("*** Please register.\n");
1236                         formout("register");
1237                         entregis();
1238                 }
1239         }
1240         /* Make up some temporary filenames for use in various parts of the
1241          * program.  Don't mess with these once they've been set, because we
1242          * will be unlinking them later on in the program and we don't
1243          * want to delete something that we didn't create. */
1244         snprintf(temp, sizeof temp, tmpnam(NULL));
1245         snprintf(temp2, sizeof temp2, tmpnam(NULL));
1246         snprintf(tempdir, sizeof tempdir, tmpnam(NULL));
1247
1248         /* Get screen dimensions.  First we go to a default of 80x24.  Then
1249          * we try to get the user's actual screen dimensions off the server.
1250          * However, if we're running on an xterm, all this stuff is
1251          * irrelevant because we're going to dynamically size the screen
1252          * during the session.
1253          */
1254         screenwidth = 80;
1255         screenheight = 24;
1256         serv_puts("GETU");
1257         serv_gets(aaa);
1258         if (aaa[0] == '2') {
1259                 screenwidth = extract_int(&aaa[4], 0);
1260                 screenheight = extract_int(&aaa[4], 1);
1261         }
1262         if (getenv("TERM") != NULL)
1263                 if (!strcmp(getenv("TERM"), "xterm")) {
1264                         have_xterm = 1;
1265                 }
1266 #ifdef TIOCGWINSZ
1267         check_screen_dims();
1268 #endif
1269
1270         set_floor_mode();
1271
1272
1273         /* Enter the lobby */
1274         dotgoto("_BASEROOM_", 1, 0);
1275
1276 /* Main loop for the system... user is logged in. */
1277     uglistsize = 0;
1278
1279         if (newnow == 1)
1280                 readmsgs(3, 1, 5);
1281         else
1282                 readmsgs(1, 1, 0);
1283
1284         /* MAIN COMMAND LOOP */
1285         do {
1286                 mcmd = getcmd(argbuf);          /* Get keyboard command */
1287
1288 #ifdef TIOCGWINSZ
1289                 check_screen_dims();            /* if xterm, get screen size */
1290 #endif
1291
1292                 if (termn8 == 0)
1293                         switch (mcmd) {
1294                         case 1:
1295                                 formout("help");
1296                                 break;
1297                         case 4:
1298                                 entmsg(0, 0);
1299                                 break;
1300                         case 36:
1301                                 entmsg(0, 1);
1302                                 break;
1303                         case 46:
1304                                 entmsg(0, 2);
1305                                 break;
1306                         case 78:
1307                                 newprompt("What do you want your username to be? ", aaa, 32);
1308                                 snprintf(bbb, sizeof bbb, "ENT0 2|0|0|0|%s", aaa);
1309                                 serv_puts(bbb);
1310                                 serv_gets(aaa);
1311                                 if (strncmp("200", aaa, 3))
1312                                         scr_printf("\n%s\n", aaa);
1313                                 else
1314                                         entmsg(0, 0);
1315                                 break;
1316                         case 5:
1317                                 updatels();
1318                                 gotonext();
1319                                 break;
1320                         case 47:
1321                                 if (!rc_alt_semantics)
1322                                         updatelsa();
1323                                 gotonext();
1324                                 break;
1325                         case 90:
1326                                 if (!rc_alt_semantics)
1327                                         updatelsa();
1328                                 dotgoto(argbuf, 0, 0);
1329                                 break;
1330                         case 58:
1331                                 updatelsa();
1332                                 dotgoto("_MAIL_", 1, 0);
1333                                 break;
1334                         case 20:
1335                                 updatels();
1336                                 dotgoto(argbuf, 0, 0);
1337                                 break;
1338                         case 52:
1339                                 if (rc_alt_semantics)
1340                                         updatelsa();
1341                                 dotgoto(argbuf, 0, 0);
1342                                 break;
1343                         case 10:
1344                                 readmsgs(0, 1, 0);
1345                                 break;
1346                         case 9:
1347                                 readmsgs(3, 1, 5);
1348                                 break;
1349                         case 13:
1350                                 readmsgs(1, 1, 0);
1351                                 break;
1352                         case 11:
1353                                 readmsgs(0, (-1), 0);
1354                                 break;
1355                         case 12:
1356                                 readmsgs(2, (-1), 0);
1357                                 break;
1358                         case 71:
1359                                 readmsgs(3, 1, atoi(argbuf));
1360                                 break;
1361                         case 7:
1362                                 forget();
1363                                 break;
1364                         case 18:
1365                                 subshell();
1366                                 break;
1367                         case 38:
1368                                 updatels();
1369                                 entroom();
1370                                 break;
1371                         case 22:
1372                                 killroom();
1373                                 break;
1374                         case 32:
1375                                 userlist(argbuf);
1376                                 break;
1377                         case 27:
1378                                 invite();
1379                                 break;
1380                         case 28:
1381                                 kickout();
1382                                 break;
1383                         case 23:
1384                                 editthisroom();
1385                                 break;
1386                         case 14:
1387                                 roomdir();
1388                                 break;
1389                         case 33:
1390                                 download(0);
1391                                 break;
1392                         case 34:
1393                                 download(1);
1394                                 break;
1395                         case 31:
1396                                 download(2);
1397                                 break;
1398                         case 43:
1399                                 download(3);
1400                                 break;
1401                         case 45:
1402                                 download(4);
1403                                 break;
1404                         case 55:
1405                                 download(5);
1406                                 break;
1407                         case 39:
1408                                 upload(0);
1409                                 break;
1410                         case 40:
1411                                 upload(1);
1412                                 break;
1413                         case 42:
1414                                 upload(2);
1415                                 break;
1416                         case 44:
1417                                 upload(3);
1418                                 break;
1419                         case 57:
1420                                 cli_upload();
1421                                 break;
1422                         case 16:
1423                                 ungoto();
1424                                 break;
1425                         case 24:
1426                                 whoknows();
1427                                 break;
1428                         case 26:
1429                                 validate();
1430                                 break;
1431                         case 29:
1432                                 if (!rc_alt_semantics)
1433                                         updatels();
1434                                 termn8 = 1;
1435                                 break;
1436                         case 30:
1437                                 if (!rc_alt_semantics)
1438                                         updatels();
1439                                 termn8 = 1;
1440                                 break;
1441                         case 48:
1442                                 enterinfo();
1443                                 break;
1444                         case 49:
1445                                 readinfo();
1446                                 break;
1447                         case 72:
1448                                 cli_image_upload("_userpic_");
1449                                 break;
1450                         case 73:
1451                                 cli_image_upload("_roompic_");
1452                                 break;
1453
1454                         case 74:
1455                                 snprintf(aaa, sizeof aaa, "_floorpic_|%d", curr_floor);
1456                                 cli_image_upload(aaa);
1457                                 break;
1458
1459                         case 75:
1460                                 enternew("roomname", aaa, 20);
1461                                 snprintf(bbb, sizeof bbb, "RCHG %s", aaa);
1462                                 serv_puts(bbb);
1463                                 serv_gets(aaa);
1464                                 if (strncmp("200", aaa, 3))
1465                                         scr_printf("\n%s\n", aaa);
1466                                 break;
1467                         case 76:
1468                                 enternew("hostname", aaa, 25);
1469                                 snprintf(bbb, sizeof bbb, "HCHG %s", aaa);
1470                                 serv_puts(bbb);
1471                                 serv_gets(aaa);
1472                                 if (strncmp("200", aaa, 3))
1473                                         scr_printf("\n%s\n", aaa);
1474                                 break;
1475                         case 77:
1476                                 enternew("username", aaa, 32);
1477                                 snprintf(bbb, sizeof bbb, "UCHG %s", aaa);
1478                                 serv_puts(bbb);
1479                                 serv_gets(aaa);
1480                                 if (strncmp("200", aaa, 3))
1481                                         scr_printf("\n%s\n", aaa);
1482                                 break;
1483
1484                         case 35:
1485                                 set_password();
1486                                 break;
1487
1488                         case 21:
1489                                 if (argbuf[0] == 0)
1490                                         strcpy(aaa, "?");
1491                                 display_help(argbuf);
1492                                 break;
1493
1494                         case 41:
1495                                 formout("register");
1496                                 entregis();
1497                                 break;
1498
1499                         case 15:
1500                                 scr_printf("Are you sure (y/n)? ");
1501                                 if (yesno() == 1) {
1502                                         updatels();
1503                                         a = 0;
1504                                         termn8 = 1;
1505                                 }
1506                                 break;
1507
1508                         case 85:
1509                                 scr_printf("All users will be disconnected!  "
1510                                         "Really terminate the server? ");
1511                                 if (yesno() == 1) {
1512                                         serv_puts("DOWN");
1513                                         serv_gets(aaa);
1514                                         scr_printf("%s\n", &aaa[4]);
1515                                         if (aaa[0]=='2') {
1516                                                 updatels();
1517                                                 a = 0;
1518                                                 termn8 = 1;
1519                                         }
1520                                 }
1521                                 break;
1522
1523                         case 86:
1524                                 scr_printf("Do you really want to schedule a "
1525                                         "server shutdown? ");
1526                                 if (yesno() == 1) {
1527                                         serv_puts("SCDN 1");
1528                                         serv_gets(aaa);
1529                                         if (aaa[0]=='2') {
1530                                                 if (atoi(&aaa[4])) {
1531                                                         scr_printf(
1532 "The Citadel server will terminate when all users are logged off.\n"
1533                                                                 );
1534                                                 }
1535                                                 else {
1536                                                         scr_printf(
1537 "The Citadel server will not terminate.\n"
1538                                                                 );
1539                                                 }
1540                                         }
1541                                 }
1542                                 break;
1543
1544                         case 87:
1545                                 network_config_management("listrecp",
1546                                         "Mailing list recipients");
1547                                 break;
1548
1549                         case 89:
1550                                 network_config_management("ignet_push_share",
1551                                         "Nodes with which we share this room");
1552                                 break;
1553
1554                         case 88:
1555                                 do_ignet_configuration();
1556                                 break;
1557
1558                         case 92:
1559                                 do_filterlist_configuration();
1560                                 break;
1561
1562                         case 6:
1563                                 if (rc_alt_semantics)
1564                                         updatelsa();
1565                                 gotonext();
1566                                 break;
1567
1568                         case 3:
1569                                 chatmode();
1570                                 break;
1571
1572                         case 2:
1573                                 if (server_is_local) {
1574                                         screen_reset();
1575                                         sttybbs(SB_RESTORE);
1576                                         snprintf(aaa, sizeof aaa, "USERNAME=\042%s\042; export USERNAME;"
1577                                                  "exec ./subsystem %ld %d %d", fullname,
1578                                           usernum, screenwidth, axlevel);
1579                                         ka_system(aaa);
1580                                         sttybbs(SB_NO_INTR);
1581                                         screen_set();
1582                                 } else {
1583                                         scr_printf("*** Can't run doors when server is not local.\n");
1584                                 }
1585                                 break;
1586
1587                         case 17:
1588                                 who_is_online(0);
1589                                 break;
1590
1591                         case 79:
1592                                 who_is_online(1);
1593                                 break;
1594
1595             case 91:
1596                 who_is_online(2);
1597                 break;
1598                 
1599                         case 80:
1600                                 do_system_configuration();
1601                                 break;
1602
1603                         case 82:
1604                                 do_internet_configuration();
1605                                 break;
1606
1607                         case 83:
1608                                 check_message_base();
1609                                 break;
1610
1611                         case 84:
1612                                 quiet_mode();
1613                                 break;
1614
1615                         case 50:
1616                                 enter_config(2);
1617                                 break;
1618
1619                         case 37:
1620                                 enter_config(0);
1621                                 set_floor_mode();
1622                                 break;
1623
1624                         case 59:
1625                                 enter_config(3);
1626                                 set_floor_mode();
1627                                 break;
1628
1629                         case 60:
1630                                 gotofloor(argbuf, GF_GOTO);
1631                                 break;
1632
1633                         case 61:
1634                                 gotofloor(argbuf, GF_SKIP);
1635                                 break;
1636
1637                         case 62:
1638                                 forget_this_floor();
1639                                 break;
1640
1641                         case 63:
1642                                 create_floor();
1643                                 break;
1644
1645                         case 64:
1646                                 edit_floor();
1647                                 break;
1648
1649                         case 65:
1650                                 kill_floor();
1651                                 break;
1652
1653                         case 66:
1654                                 enter_bio();
1655                                 break;
1656
1657                         case 67:
1658                                 read_bio();
1659                                 break;
1660
1661                         case 25:
1662                                 edituser();
1663                                 break;
1664
1665                         case 8:
1666                                 knrooms(floor_mode);
1667                                 scr_printf("\n");
1668                                 break;
1669
1670                         case 68:
1671                                 knrooms(2);
1672                                 scr_printf("\n");
1673                                 break;
1674
1675                         case 69:
1676                                 misc_server_cmd(argbuf);
1677                                 break;
1678
1679                         case 70:
1680                                 edit_system_message(argbuf);
1681                                 break;
1682
1683                         case 19:
1684                                 listzrooms();
1685                                 scr_printf("\n");
1686                                 break;
1687
1688                         case 51:
1689                                 deletefile();
1690                                 break;
1691
1692                         case 53:
1693                                 netsendfile();
1694                                 break;
1695
1696                         case 54:
1697                                 movefile();
1698                                 break;
1699
1700                         case 56:
1701                                 page_user();
1702                                 break;
1703
1704                         }       /* end switch */
1705         } while (termn8 == 0);
1706
1707 TERMN8: scr_printf("%s logged out.\n", fullname);
1708         while (march != NULL) {
1709                 remove_march(march->march_name, 0);
1710         }
1711         if (mcmd == 30) {
1712                 sln_printf("\n\nType 'off' to disconnect, or next user...\n");
1713         }
1714         snprintf(aaa, sizeof aaa, "LOUT");
1715         serv_puts(aaa);
1716         serv_gets(aaa);
1717         screen_delete();
1718         sttybbs(SB_RESTORE);
1719         if ((mcmd == 29) || (mcmd == 15)) {
1720                 formout("goodbye");
1721                 logoff(0);
1722         }
1723         goto GSTA;
1724
1725 }       /* end main() */