c374d9761b10c07778653d8af46a1f0286cfd43e
[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 <stdarg.h>
34 #include <errno.h>
35
36 #include "citadel.h"
37 #include "citadel_ipc.h"
38 #include "axdefs.h"
39 #include "routines.h"
40 #include "routines2.h"
41 #include "rooms.h"
42 #include "messages.h"
43 #include "commands.h"
44 #include "client_chat.h"
45 #include "client_passwords.h"
46 #include "citadel_decls.h"
47 #include "tools.h"
48 #include "sysdep.h"
49 #ifndef HAVE_SNPRINTF
50 #include "snprintf.h"
51 #endif
52 #include "screen.h"
53 #include "citadel_dirs.h"
54
55 #include "md5.h"
56
57 #define IFEXPERT if (userflags&US_EXPERT)
58 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
59 #define IFAIDE if (axlevel>=6)
60 #define IFNAIDE if (axlevel<6)
61
62 int rordercmp(struct ctdlroomlisting *r1, struct ctdlroomlisting *r2);
63
64 struct march *march = NULL;
65
66 /* globals associated with the client program */
67 char temp[PATH_MAX];            /* Name of general-purpose temp file */
68 char temp2[PATH_MAX];           /* Name of general-purpose temp file */
69 char tempdir[PATH_MAX];         /* Name of general-purpose temp directory */
70 char editor_paths[MAX_EDITORS][SIZ];    /* paths to external editors */
71 char printcmd[SIZ];             /* print command */
72 int editor_pid = (-1);
73 char fullname[USERNAME_SIZE];
74 int screenwidth;
75 int screenheight;
76 unsigned room_flags;
77 unsigned room_flags2;
78 char room_name[ROOMNAMELEN];
79 char *uglist[UGLISTLEN]; /* size of the ungoto list */
80 long uglistlsn[UGLISTLEN]; /* current read position for all the ungoto's. Not going to make any friends with this one. */
81 int uglistsize = 0;
82 char is_mail = 0;               /* nonzero when we're in a mail room */
83 char axlevel = 0;               /* access level */
84 char is_room_aide = 0;          /* boolean flag, 1 if room aide */
85 int timescalled;
86 int posted;
87 unsigned userflags;
88 long usernum = 0L;              /* user number */
89 time_t lastcall = 0L;           /* Date/time of previous login */
90 char newnow;
91 long highest_msg_read;          /* used for <A>bandon room cmd */
92 long maxmsgnum;                 /* used for <G>oto */
93 char sigcaught = 0;
94 char have_xterm = 0;            /* are we running on an xterm? */
95 char rc_username[USERNAME_SIZE];
96 char rc_password[32];
97 char hostbuf[SIZ];
98 char portbuf[SIZ];
99 char rc_floor_mode;
100 char floor_mode;
101 char curr_floor = 0;            /* number of current floor */
102 char floorlist[128][SIZ];       /* names of floors */
103 int termn8 = 0;                 /* Set to nonzero to cause a logoff */
104 int secure;                     /* Set to nonzero when wire is encrypted */
105 int can_do_msg4 = 0;            /* Set to nonzero if the server can handle MSG4 commands */
106
107 extern char instant_msgs;       /* instant messages waiting! */
108 extern int rc_ansi_color;       /* ansi color value from citadel.rc */
109 extern int next_lazy_cmd;
110
111 CtdlIPC *ipc_for_signal_handlers;       /* KLUDGE cover your eyes */
112
113 /*
114  * here is our 'clean up gracefully and exit' routine
115  */
116 void ctdl_logoff(char *file, int line, CtdlIPC *ipc, int code)
117 {
118         int lp;
119
120         if (editor_pid > 0) {   /* kill the editor if it's running */
121                 kill(editor_pid, SIGHUP);
122         }
123
124         /* Free the ungoto list */
125         for (lp = 0; lp < uglistsize; lp++) {
126                 free(uglist[lp]);
127         }
128
129 /* Shut down the server connection ... but not if the logoff code is 3,
130  * because that means we're exiting because we already lost the server.
131  */
132         if (code != 3) {
133                 CtdlIPCQuit(ipc);
134         }
135
136 /*
137  * now clean up various things
138  */
139         screen_delete();
140
141         unlink(temp);
142         unlink(temp2);
143         nukedir(tempdir);
144
145         /* Violently kill off any child processes if Citadel is
146          * the login shell. 
147          */
148         if (getppid() == 1) {
149                 kill(0 - getpgrp(), SIGTERM);
150                 sleep(1);
151                 kill(0 - getpgrp(), SIGKILL);
152         }
153         color(ORIGINAL_PAIR);   /* Restore the old color settings */
154         stty_ctdl(SB_RESTORE);  /* return the old terminal settings */
155         /* 
156          * uncomment the following if you need to know why Citadel exited
157         printf("*** Exit code %d at %s:%d\n", code, file, line);
158         sleep(2);
159          */
160         exit(code);             /* exit with the proper exit code */
161 }
162
163
164
165 /*
166  * signal catching function for hangups...
167  */
168 void dropcarr(int signum)
169 {
170         logoff(NULL, 3);        /* No IPC when server's already gone! */
171 }
172
173
174
175 /*
176  * catch SIGCONT to reset terminal modes when were are put back into the
177  * foreground.
178  */
179 void catch_sigcont(int signum)
180 {
181         stty_ctdl(SB_LAST);
182         signal(SIGCONT, catch_sigcont);
183 }
184
185
186 /* general purpose routines */
187
188 /* display a file */
189 void formout(CtdlIPC *ipc, char *name)
190 {
191         int r;                  /* IPC return code */
192         char buf[SIZ];
193         char *text = NULL;
194
195         r = CtdlIPCSystemMessage(ipc, name, &text, buf);
196         if (r / 100 != 1) {
197                 scr_printf("%s\n", buf);
198                 return;
199         }
200         if (text) {
201                 fmout(screenwidth, NULL, text, NULL,
202                       ((userflags & US_PAGINATOR) ? 1 : 0),
203                       screenheight, 1, 1);
204                 free(text);
205         }
206 }
207
208
209 void userlist(CtdlIPC *ipc, char *patn)
210 {
211         char buf[SIZ];
212         char fl[SIZ];
213         struct tm tmbuf;
214         time_t lc;
215         int r;                          /* IPC response code */
216         char *listing = NULL;
217
218         r = CtdlIPCUserListing(ipc, patn, &listing, buf);
219         if (r / 100 != 1) {
220                 pprintf("%s\n", buf);
221                 return;
222         }
223
224         pprintf("       User Name           Num  L  LastCall  Calls Posts\n");
225         pprintf("------------------------- ----- - ---------- ----- -----\n");
226         if (listing != NULL) while (strlen(listing) > 0) {
227                 extract_token(buf, listing, 0, '\n', sizeof buf);
228                 remove_token(listing, 0, '\n');
229
230                 if (sigcaught == 0) {
231                     extract_token(fl, buf, 0, '|', sizeof fl);
232                     if (pattern(fl, patn) >= 0) {
233                         pprintf("%-25s ", fl);
234                         pprintf("%5ld %d ", extract_long(buf, 2),
235                                extract_int(buf, 1));
236                         lc = extract_long(buf, 3);
237                         localtime_r(&lc, &tmbuf);
238                         pprintf("%02d/%02d/%04d ",
239                                (tmbuf.tm_mon + 1),
240                                tmbuf.tm_mday,
241                                (tmbuf.tm_year + 1900));
242                         pprintf("%5ld %5ld\n", extract_long(buf, 4), extract_long(buf, 5));
243                     }
244
245                 }
246         }
247         free(listing);
248         pprintf("\n");
249 }
250
251
252 /*
253  * grab assorted info about the user...
254  */
255 void load_user_info(char *params)
256 {
257         extract_token(fullname, params, 0, '|', sizeof fullname);
258         axlevel = extract_int(params, 1);
259         timescalled = extract_int(params, 2);
260         posted = extract_int(params, 3);
261         userflags = extract_int(params, 4);
262         usernum = extract_long(params, 5);
263         lastcall = extract_long(params, 6);
264 }
265
266
267 /*
268  * Remove a room from the march list.  'floornum' is ignored unless
269  * 'roomname' is set to _FLOOR_, in which case all rooms on the requested
270  * floor will be removed from the march list.
271  */
272 void remove_march(char *roomname, int floornum)
273 {
274         struct march *mptr, *mptr2;
275
276         if (march == NULL)
277                 return;
278
279         if ((!strcasecmp(march->march_name, roomname))
280             || ((!strcasecmp(roomname, "_FLOOR_")) && (march->march_floor == floornum))) {
281                 mptr = march->next;
282                 free(march);
283                 march = mptr;
284                 return;
285         }
286         mptr2 = march;
287         for (mptr = march; mptr != NULL; mptr = mptr->next) {
288
289                 if ((!strcasecmp(mptr->march_name, roomname))
290                     || ((!strcasecmp(roomname, "_FLOOR_"))
291                         && (mptr->march_floor == floornum))) {
292
293                         mptr2->next = mptr->next;
294                         free(mptr);
295                         mptr = mptr2;
296                 } else {
297                         mptr2 = mptr;
298                 }
299         }
300 }
301
302
303 /*
304  * Locate the room on the march list which we most want to go to.  Each room
305  * is measured given a "weight" of preference based on various factors.
306  */
307 char *pop_march(int desired_floor, struct march *_march)
308 {
309         static char TheRoom[ROOMNAMELEN];
310         int TheFloor = 0;
311         int TheOrder = 32767;
312         int TheWeight = 0;
313         int weight;
314         struct march *mptr = NULL;
315
316         strcpy(TheRoom, "_BASEROOM_");
317         if (_march == NULL)
318                 return (TheRoom);
319
320         for (mptr = _march; mptr != NULL; mptr = mptr->next) {
321                 weight = 0;
322                 if ((strcasecmp(mptr->march_name, "_BASEROOM_")))
323                         weight = weight + 10000;
324                 if (mptr->march_floor == desired_floor)
325                         weight = weight + 5000;
326
327                 weight = weight + ((128 - (mptr->march_floor)) * 128);
328                 weight = weight + (128 - (mptr->march_order));
329
330                 if (weight > TheWeight) {
331                         TheWeight = weight;
332                         strcpy(TheRoom, mptr->march_name);
333                         TheFloor = mptr->march_floor;
334                         TheOrder = mptr->march_order;
335                 }
336         }
337         return (TheRoom);
338 }
339
340
341 /*
342  * jump directly to a room
343  */
344 void dotgoto(CtdlIPC *ipc, char *towhere, int display_name, int fromungoto)
345 {
346         char aaa[SIZ], bbb[SIZ];
347         static long ls = 0L;
348         int newmailcount = 0;
349         int partial_match, best_match;
350         char from_floor;
351         int ugpos = uglistsize;
352         int r;                          /* IPC result code */
353         struct ctdlipcroom *room = NULL;
354
355         /* store ungoto information */
356         if (fromungoto == 0) {
357                 /* sloppy slide them all down, hey it's the client, who cares. :-) */
358                 if (uglistsize >= (UGLISTLEN-1)) {
359                         int lp;
360                         free (uglist[0]);
361                         for (lp = 0; lp < (UGLISTLEN-1); lp++) {
362                                 uglist[lp] = uglist[lp+1];
363                                 uglistlsn[lp] = uglistlsn[lp+1];
364                         }
365                         ugpos--;
366                 } else {
367                         uglistsize++;
368                 }
369         
370                 uglist[ugpos] = malloc(strlen(room_name)+1);
371                 strcpy(uglist[ugpos], room_name);
372                 uglistlsn[ugpos] = ls;
373         }
374       
375         /* first try an exact match */
376         r = CtdlIPCGotoRoom(ipc, towhere, "", &room, aaa);
377         if (r / 10 == 54) {
378                 newprompt("Enter room password: ", bbb, 9);
379                 r = CtdlIPCGotoRoom(ipc, towhere, bbb, &room, aaa);
380                 if (r / 10 == 54) {
381                         scr_printf("Wrong password.\n");
382                         return;
383                 }
384         }       
385
386         /*
387          * If a match is not found, try a partial match.
388          * Partial matches anywhere in the string carry a weight of 1,
389          * left-aligned matches carry a weight of 2.  Pick the room that
390          * has the highest-weighted match.  Do not match on forgotten
391          * rooms.
392          */
393         if (r / 100 != 2) {
394                 struct march *march = NULL;
395
396                 best_match = 0;
397                 strcpy(bbb, "");
398
399                 r = CtdlIPCKnownRooms(ipc, SubscribedRooms, AllFloors, &march, aaa);
400                 if (r / 100 == 1) {
401                         /* Run the roomlist; free the data as we go */
402                         struct march *mp = march;       /* Current */
403
404                         while (mp) {
405                                 partial_match = 0;
406                                 if (pattern(mp->march_name, towhere) >= 0) {
407                                         partial_match = 1;
408                                 }
409                                 if (!strncasecmp(mp->march_name, towhere, strlen(towhere))) {
410                                         partial_match = 2;
411                                 }
412                                 if (partial_match > best_match) {
413                                         strcpy(bbb, mp->march_name);
414                                         best_match = partial_match;
415                                 }
416                                 /* Both pointers are NULL at end of list */
417                                 march = mp->next;
418                                 free(mp);
419                                 mp = march;
420                         }
421                 }
422
423                 if (strlen(bbb) == 0) {
424                         scr_printf("No room '%s'.\n", towhere);
425                         return;
426                 }
427                 r = CtdlIPCGotoRoom(ipc, bbb, "", &room, aaa);
428         }
429         if (r / 100 != 1 && r / 100 != 2) {
430                 scr_printf("%s\n", aaa);
431                 return;
432         }
433         safestrncpy(room_name, room->RRname, ROOMNAMELEN);
434         room_flags = room->RRflags;
435         room_flags2 = room->RRflags2;
436         from_floor = curr_floor;
437         curr_floor = room->RRfloor;
438
439         remove_march(room_name, 0);
440         if (!strcasecmp(towhere, "_BASEROOM_"))
441                 remove_march(towhere, 0);
442         if (!room->RRunread)
443                 next_lazy_cmd = 5;      /* Don't read new if no new msgs */
444         if ((from_floor != curr_floor) && (display_name > 0) && (floor_mode == 1)) {
445                 if (floorlist[(int) curr_floor][0] == 0)
446                         load_floorlist(ipc);
447                 scr_printf("(Entering floor: %s)\n", &floorlist[(int) curr_floor][0]);
448         }
449         if (display_name == 1) {
450                 color(BRIGHT_WHITE);
451                 scr_printf("%s ", room_name);
452                 color(DIM_WHITE);
453                 scr_printf("- ");
454         }
455         if (display_name != 2) {
456                 color(BRIGHT_YELLOW);
457                 scr_printf("%d ", room->RRunread);
458                 color(DIM_WHITE);
459                 scr_printf("new of ");
460                 color(BRIGHT_YELLOW);
461                 scr_printf("%d ", room->RRtotal);
462                 color(DIM_WHITE);
463                 scr_printf("messages.\n");
464         }
465         highest_msg_read = room->RRlastread;
466         maxmsgnum = room->RRhighest;
467         is_mail = room->RRismailbox;
468         is_room_aide = room->RRaide;
469         ls = room->RRlastread;
470
471         /* read info file if necessary */
472         if (room->RRinfoupdated > 0)
473                 readinfo(ipc);
474
475         /* check for newly arrived mail if we can */
476         newmailcount = room->RRnewmail;
477         if (newmailcount > 0) {
478                 color(BRIGHT_RED);
479                 if (newmailcount == 1) {
480                         scr_printf("*** A new mail message has arrived.\n");
481                 }
482                 else {
483                         scr_printf("*** %d new mail messages have arrived.\n",
484                                         newmailcount);
485                 }
486                 color(DIM_WHITE);
487                 if (strlen(rc_gotmail_cmd) > 0) {
488                         system(rc_gotmail_cmd);
489                 }
490         }
491         status_line(ipc->ServInfo.humannode, ipc->ServInfo.site_location,
492                         room_name, secure, newmailcount);
493         free(room);
494 }
495
496 /* Goto next room having unread messages.
497  * We want to skip over rooms that the user has already been to, and take the
498  * user back to the lobby when done.  The room we end up in is placed in
499  * newroom - which is set to 0 (the lobby) initially.
500  */
501 void gotonext(CtdlIPC *ipc)
502 {
503         char buf[SIZ];
504         struct march *mptr, *mptr2;
505         char next_room[ROOMNAMELEN];
506         int r;                          /* IPC response code */
507
508         /* Check to see if the march-mode list is already allocated.
509          * If it is, pop the first room off the list and go there.
510          */
511         if (march == NULL) {
512                 r = CtdlIPCKnownRooms(ipc, SubscribedRoomsWithNewMessages,
513                                         AllFloors, &march, buf);
514
515 /* add _BASEROOM_ to the end of the march list, so the user will end up
516  * in the system base room (usually the Lobby>) at the end of the loop
517  */
518                 mptr = (struct march *) malloc(sizeof(struct march));
519                 mptr->next = NULL;
520                 strcpy(mptr->march_name, "_BASEROOM_");
521                 if (march == NULL) {
522                         march = mptr;
523                 } else {
524                         mptr2 = march;
525                         while (mptr2->next != NULL)
526                                 mptr2 = mptr2->next;
527                         mptr2->next = mptr;
528                 }
529 /*
530  * ...and remove the room we're currently in, so a <G>oto doesn't make us
531  * walk around in circles
532  */
533                 remove_march(room_name, 0);
534         }
535         if (march != NULL) {
536                 strcpy(next_room, pop_march(curr_floor, march));
537         } else {
538                 strcpy(next_room, "_BASEROOM_");
539         }
540         remove_march(next_room, 0);
541         dotgoto(ipc, next_room, 1, 0);
542 }
543
544 /*
545  * forget all rooms on a given floor
546  */
547 void forget_all_rooms_on(CtdlIPC *ipc, int ffloor)
548 {
549         char buf[SIZ];
550         struct march *flist = NULL;
551         struct march *fptr = NULL;
552         struct ctdlipcroom *room = NULL;
553         int r;                          /* IPC response code */
554
555         scr_printf("Forgetting all rooms on %s...\n", &floorlist[ffloor][0]);
556         scr_flush();
557         remove_march("_FLOOR_", ffloor);
558         r = CtdlIPCKnownRooms(ipc, AllAccessibleRooms, ffloor, &flist, buf);
559         if (r / 100 != 1) {
560                 scr_printf("Error %d: %s\n", r, buf);
561                 return;
562         }
563         while (flist) {
564                 r = CtdlIPCGotoRoom(ipc, flist->march_name, "", &room, buf);
565                 if (r / 100 == 2) {
566                         r = CtdlIPCForgetRoom(ipc, buf);
567                         if (r / 100 != 2) {
568                                 scr_printf("Error %d: %s\n", r, buf);
569                         }
570
571                 }
572                 fptr = flist;
573                 flist = flist->next;
574                 free(fptr);
575         }
576         if (room) free(room);
577 }
578
579
580 /*
581  * routine called by gotofloor() to move to a new room on a new floor
582  */
583 void gf_toroom(CtdlIPC *ipc, char *towhere, int mode)
584 {
585         int floor_being_left;
586
587         floor_being_left = curr_floor;
588
589         if (mode == GF_GOTO) {          /* <;G>oto mode */
590                 updatels(ipc);
591                 dotgoto(ipc, towhere, 1, 0);
592         }
593         else if (mode == GF_SKIP) {     /* <;S>kip mode */
594                 dotgoto(ipc, towhere, 1, 0);
595                 remove_march("_FLOOR_", floor_being_left);
596         }
597         else if (mode == GF_ZAP) {      /* <;Z>ap mode */
598                 dotgoto(ipc, towhere, 1, 0);
599                 remove_march("_FLOOR_", floor_being_left);
600                 forget_all_rooms_on(ipc, floor_being_left);
601         }
602 }
603
604
605 /*
606  * go to a new floor
607  */
608 void gotofloor(CtdlIPC *ipc, char *towhere, int mode)
609 {
610         int a, tofloor;
611         int r;          /* IPC response code */
612         struct march *mptr;
613         char buf[SIZ], targ[SIZ];
614
615         if (floorlist[0][0] == 0)
616                 load_floorlist(ipc);
617         tofloor = (-1);
618         for (a = 0; a < 128; ++a)
619                 if (!strcasecmp(&floorlist[a][0], towhere))
620                         tofloor = a;
621
622         if (tofloor < 0) {
623                 for (a = 0; a < 128; ++a) {
624                         if (!strncasecmp(&floorlist[a][0], towhere, strlen(towhere))) {
625                                 tofloor = a;
626                         }
627                 }
628         }
629         if (tofloor < 0) {
630                 for (a = 0; a < 128; ++a)
631                         if (pattern(towhere, &floorlist[a][0]) > 0)
632                                 tofloor = a;
633         }
634         if (tofloor < 0) {
635                 scr_printf("No floor '%s'.\n", towhere);
636                 return;
637         }
638         for (mptr = march; mptr != NULL; mptr = mptr->next) {
639                 if ((mptr->march_floor) == tofloor) {
640                         gf_toroom(ipc, mptr->march_name, mode);
641                         return;
642                 }
643         }
644
645         /* Find first known room on the floor */
646
647         strcpy(targ, "");
648         mptr = NULL;
649         r = CtdlIPCKnownRooms(ipc, SubscribedRooms, tofloor, &mptr, buf);
650         if (r / 100 == 1) {
651                 struct march *tmp = mptr;
652
653                 /*. . . according to room order */
654                 if (mptr)
655             strcpy(targ, pop_march(tofloor, mptr));
656                 while (mptr) {
657                         tmp = mptr->next;
658                         free(mptr);
659                         mptr = tmp;
660                 }
661         }
662         if (strlen(targ) > 0) {
663                 gf_toroom(ipc, targ, mode);
664                 return;
665         }
666
667         /* No known rooms on the floor; unzap the first one then */
668
669         strcpy(targ, "");
670         mptr = NULL;
671         r = CtdlIPCKnownRooms(ipc, AllAccessibleRooms, tofloor, &mptr, buf);
672         if (r / 100 == 1) {
673                 struct march *tmp = mptr;
674                 
675         /*. . . according to room order */
676                 if (mptr)
677                         strcpy(targ, pop_march(tofloor, mptr));
678                 while (mptr) {
679                         tmp = mptr->next;
680                         free(mptr);
681                         mptr = tmp;
682                 }
683         }
684         if (strlen(targ) > 0) {
685                 gf_toroom(ipc, targ, mode);
686         } else {
687                 scr_printf("There are no rooms on '%s'.\n", &floorlist[tofloor][0]);
688         }
689 }
690
691 /*
692  * Indexing mechanism for a room list, called by gotoroomstep()
693  */
694 void room_tree_list_query(struct ctdlroomlisting *rp, char *findrmname, int findrmslot, char *rmname, int *rmslot, int *rmtotal)
695 {
696         char roomname[ROOMNAMELEN];
697         static int cur_rmslot = 0;
698
699         if (rp == NULL) {
700                 cur_rmslot = 0;
701                 return;
702         }
703
704         if (rp->lnext != NULL) {
705                 room_tree_list_query(rp->lnext, findrmname, findrmslot, rmname, rmslot, rmtotal);
706         }
707
708         if (sigcaught == 0) {
709                 strcpy(roomname, rp->rlname);
710
711                 if (rmname != NULL) {
712                         if (cur_rmslot == findrmslot) {
713                                 strcpy(rmname, roomname);
714                         }
715                 }
716                 if (rmslot != NULL) {
717                         if (!strcmp(roomname, findrmname)) {
718                                 *rmslot = cur_rmslot;
719                         }
720                 }
721                 cur_rmslot++;
722         }
723
724         if (rp->rnext != NULL) {
725                 room_tree_list_query(rp->rnext, findrmname, findrmslot, rmname, rmslot, rmtotal);
726         }
727
728         if ((rmname == NULL) && (rmslot == NULL))
729                 free(rp);
730
731         if (rmtotal != NULL) {
732                 *rmtotal = cur_rmslot;
733         }
734 }
735
736 /*
737  * step through rooms on current floor
738  */
739 void  gotoroomstep(CtdlIPC *ipc, int direction, int mode)
740 {
741         struct march *listing = NULL;
742         struct march *mptr;
743         int r;          /* IPC response code */
744         char buf[SIZ];
745         struct ctdlroomlisting *rl = NULL;
746         struct ctdlroomlisting *rp;
747         struct ctdlroomlisting *rs;
748         int list_it;
749         char rmname[ROOMNAMELEN];
750         int rmslot;
751         int rmtotal;
752
753         /* Ask the server for a room list */
754         r = CtdlIPCKnownRooms(ipc, SubscribedRooms, (-1), &listing, buf);
755         if (r / 100 != 1) {
756                 listing = NULL;
757         }
758
759         load_floorlist(ipc);
760
761         for (mptr = listing; mptr != NULL; mptr = mptr->next) {
762                 list_it = 1;
763
764                 if ( floor_mode 
765                          && (mptr->march_floor != curr_floor))
766                         list_it = 0;
767
768                 if (list_it) {
769                         rp = malloc(sizeof(struct ctdlroomlisting));
770                         strncpy(rp->rlname, mptr->march_name, ROOMNAMELEN);
771                         rp->rlflags = mptr->march_flags;
772                         rp->rlfloor = mptr->march_floor;
773                         rp->rlorder = mptr->march_order;
774                         rp->lnext = NULL;
775                         rp->rnext = NULL;
776
777                         rs = rl;
778                         if (rl == NULL) {
779                                 rl = rp;
780                         } else {
781                                 while (rp != NULL) {
782                                         if (rordercmp(rp, rs) < 0) {
783                                                 if (rs->lnext == NULL) {
784                                                         rs->lnext = rp;
785                                                         rp = NULL;
786                                                 } else {
787                                                         rs = rs->lnext;
788                                                 }
789                                         } else {
790                                                 if (rs->rnext == NULL) {
791                                                         rs->rnext = rp;
792                                                         rp = NULL;
793                                                 } else {
794                                                         rs = rs->rnext;
795                                                 }
796                                         }
797                                 }
798                         }
799                 }
800         }
801
802         /* Find position of current room */
803         room_tree_list_query(NULL, NULL, 0, NULL, NULL, NULL);
804         room_tree_list_query(rl,  room_name, 0, NULL, &rmslot, &rmtotal);
805
806         if (direction == 0) { /* Previous room */
807                 /* If we're at the first room, wrap to the last room */
808                 if (rmslot == 0) {
809                         rmslot = rmtotal - 1;
810                 } else {
811                         rmslot--;
812                 }
813         } else {                 /* Next room */
814                 /* If we're at the last room, wrap to the first room */
815                 if (rmslot == rmtotal - 1) {
816                         rmslot = 0; 
817                 } else {
818                         rmslot++;
819                 }
820         }
821
822         /* Get name of next/previous room */
823         room_tree_list_query(NULL, NULL, 0, NULL, NULL, NULL);
824         room_tree_list_query(rl,  NULL, rmslot, rmname, NULL, NULL);
825
826         /* Free the tree */
827         room_tree_list_query(rl, NULL, 0, NULL, NULL, NULL);
828
829         if (mode == 0) { /* not skipping */
830             updatels(ipc);
831         } else {
832                 if (rc_alt_semantics) {
833                 updatelsa(ipc);
834                 }
835         }
836
837         /* Free the room list */
838         while (listing) {
839                 mptr = listing->next;
840                 free(listing);
841                 listing = mptr;
842         };
843
844         dotgoto(ipc, rmname, 1, 0);
845 }
846
847
848 /*
849  * step through floors on system
850  */
851 void  gotofloorstep(CtdlIPC *ipc, int direction, int mode)
852 {
853         int  tofloor;
854
855         if (floorlist[0][0] == 0)
856                 load_floorlist(ipc);
857
858         empty_keep_going:
859
860         if (direction == 0) { /* Previous floor */
861                 if (curr_floor) tofloor = curr_floor - 1;
862                 else tofloor = 127;
863
864                 while (!floorlist[tofloor][0]) tofloor--;
865         } else {                   /* Next floor */
866                 if (curr_floor < 127) tofloor = curr_floor + 1;
867                 else tofloor = 0;
868
869                 while (!floorlist[tofloor][0] && tofloor < 127) tofloor++;
870                 if (!floorlist[tofloor][0])     tofloor = 0;
871         }
872         /* ;g works when not in floor mode so . . . */
873         if (!floor_mode) {
874                 scr_printf("(%s)\n", floorlist[tofloor] );
875         }
876
877         gotofloor(ipc, floorlist[tofloor], mode);
878         if (curr_floor != tofloor) { /* gotofloor failed */
879              curr_floor = tofloor;
880              goto empty_keep_going;
881         }            
882 }
883
884 /* 
885  * Display user 'preferences'.
886  */
887 extern int rc_prompt_control;
888 void read_config(CtdlIPC *ipc)
889 {
890         char buf[SIZ];
891         char *resp = NULL;
892         int r;                  /* IPC response code */
893     char _fullname[USERNAME_SIZE];
894         long _usernum;
895         int _axlevel, _timescalled, _posted;
896         time_t _lastcall;
897         struct ctdluser *user = NULL;
898
899         /* get misc user info */   
900         r = CtdlIPCGetBio(ipc, fullname, &resp, buf);
901         if (r / 100 != 1) {
902                 pprintf("%s\n", buf);
903                 return;
904         }
905         extract_token(_fullname, buf, 1, '|', sizeof fullname);
906         _usernum = extract_long(buf, 2);
907         _axlevel = extract_int(buf, 3);
908         _lastcall = extract_long(buf, 4);
909     _timescalled = extract_int(buf, 5);
910         _posted = extract_int(buf, 6);
911         free(resp);
912         resp = NULL;
913    
914         /* get preferences */
915         r = CtdlIPCGetConfig(ipc, &user, buf);
916         if (r / 100 != 2) {
917                 scr_printf("%s\n", buf);
918                 free(user);
919                 return;
920         }
921
922         /* show misc user info */
923         scr_printf("%s\nAccess level: %d (%s)\n"
924                    "User #%ld / %d Calls / %d Posts",
925                    _fullname, _axlevel, axdefs[(int) _axlevel],
926                    _usernum, _timescalled, _posted);
927         if (_lastcall > 0L) {
928                 scr_printf(" / Curr login: %s",
929                            asctime(localtime(&_lastcall)));
930         }
931         scr_printf("\n");
932
933         /* show preferences */
934         scr_printf("Your screen width: ");                                     color(BRIGHT_CYAN); scr_printf("%d",   /*user->USscreenwidth*/ screenwidth);          color(DIM_WHITE); 
935         scr_printf(", height: ");                                              color(BRIGHT_CYAN); scr_printf("%d\n", /*user->USscreenheight*/ screenheight);        color(DIM_WHITE);  
936         scr_printf("Are you an experienced Citadel user: ");                   color(BRIGHT_CYAN); scr_printf("%s\n", (user->flags & US_EXPERT) ? "Yes" : "No");     color(DIM_WHITE);
937         scr_printf("Print last old message on New message request: ");         color(BRIGHT_CYAN); scr_printf("%s\n", (user->flags & US_LASTOLD)? "Yes" : "No");     color(DIM_WHITE);
938         scr_printf("Prompt after each message: ");                             color(BRIGHT_CYAN); scr_printf("%s\n", (!(user->flags & US_NOPROMPT))? "Yes" : "No"); color(DIM_WHITE);
939         if ((user->flags & US_NOPROMPT) == 0) {
940         scr_printf("Use 'disappearing' prompts: ");                        color(BRIGHT_CYAN); scr_printf("%s\n", (user->flags & US_DISAPPEAR)? "Yes" : "No");   color(DIM_WHITE);
941         }
942         scr_printf("Pause after each screenful of text: ");                    color(BRIGHT_CYAN); scr_printf("%s\n", (user->flags & US_PAGINATOR)? "Yes" : "No");   color(DIM_WHITE);
943     if (rc_prompt_control == 3 && (user->flags & US_PAGINATOR)) {
944         scr_printf("<N>ext and <S>top work at paginator prompt: ");        color(BRIGHT_CYAN); scr_printf("%s\n", (user->flags & US_PROMPTCTL)? "Yes" : "No");   color(DIM_WHITE);
945         }
946     if (rc_floor_mode == RC_DEFAULT) {
947         scr_printf("View rooms by floor: ");                               color(BRIGHT_CYAN); scr_printf("%s\n", (user->flags & US_FLOORS)? "Yes" : "No");          color(DIM_WHITE);
948         }
949         if (rc_ansi_color == 3) {
950             scr_printf("Enable color support: ");                              color(BRIGHT_CYAN); scr_printf("%s\n", (user->flags & US_COLOR)? "Yes" : "No");       color(DIM_WHITE);
951         }
952         scr_printf("Be unlisted in userlog: ");                                color(BRIGHT_CYAN); scr_printf("%s\n", (user->flags & US_UNLISTED)? "Yes" : "No");    color(DIM_WHITE);
953         if (strlen(editor_paths[0]) > 0) {
954         scr_printf("Always enter messages with the full-screen editor: "); color(BRIGHT_CYAN); scr_printf("%s\n", (user->flags & US_EXTEDIT)? "Yes" : "No");     color(DIM_WHITE);
955         }
956         free(user);
957 }
958
959 /*
960  * Display system statistics.
961  */
962 void system_info(CtdlIPC *ipc)
963 {
964         char buf[SIZ];
965         char *resp = NULL;
966         size_t bytes;
967         int mrtg_users, mrtg_active_users; 
968         char mrtg_server_uptime[40];
969         long mrtg_himessage;
970         int ret;                        /* IPC response code */
971
972         /* get #users, #active & server uptime */
973         ret = CtdlIPCGenericCommand(ipc, "MRTG|users", NULL, 0, &resp, &bytes, buf);
974         mrtg_users = extract_int(resp, 0);
975         remove_token(resp, 0, '\n');
976         mrtg_active_users = extract_int(resp, 0);
977         remove_token(resp, 0, '\n');
978         extract_token(mrtg_server_uptime, resp, 0, '\n', sizeof mrtg_server_uptime);
979     free(resp);
980         resp = NULL;
981
982         /* get high message# */
983         ret = CtdlIPCGenericCommand(ipc, "MRTG|messages", NULL, 0, &resp, &bytes, buf);
984         mrtg_himessage = extract_long(resp, 0);
985         free(resp);
986         resp = NULL;
987
988         /* refresh server info just in case */
989         CtdlIPCServerInfo(ipc, buf);
990
991         scr_printf("You are connected to %s (%s) @%s\n", ipc->ServInfo.nodename, ipc->ServInfo.humannode, ipc->ServInfo.fqdn);
992         scr_printf("running %s with text client v%.2f,\n", ipc->ServInfo.software, (float)REV_LEVEL/100);
993     scr_printf("and located in %s.\n", ipc->ServInfo.site_location);
994     scr_printf("Connected users %d / Active users %d / Highest message #%ld\n", mrtg_users, mrtg_active_users, mrtg_himessage);
995     scr_printf("Server uptime: %s\n", mrtg_server_uptime);
996     scr_printf("Your system administrator is %s.\n", ipc->ServInfo.sysadm);
997     scr_printf("Copyright (C)1987-2007 by the Citadel development team\n");
998 }
999
1000 /*
1001  * forget all rooms on current floor
1002  */
1003 void forget_this_floor(CtdlIPC *ipc)
1004 {
1005         if (curr_floor == 0) {
1006                 scr_printf("Can't forget this floor.\n");
1007                 return;
1008         }
1009         if (floorlist[0][0] == 0) {
1010                 load_floorlist(ipc);
1011         }
1012         scr_printf("Are you sure you want to forget all rooms on %s? ",
1013                &floorlist[(int) curr_floor][0]);
1014         if (yesno() == 0) {
1015                 return;
1016         }
1017
1018         gf_toroom(ipc, "_BASEROOM_", GF_ZAP);
1019 }
1020
1021
1022 /* 
1023  * Figure out the physical screen dimensions, if we can
1024  * WARNING:  this is now called from a signal handler!
1025  */
1026 void check_screen_dims(void)
1027 {
1028 #ifdef TIOCGWINSZ
1029         struct {
1030                 unsigned short height;  /* rows */
1031                 unsigned short width;   /* columns */
1032                 unsigned short xpixels;
1033                 unsigned short ypixels;         /* pixels */
1034         } xwinsz;
1035
1036         if (have_xterm) {       /* dynamically size screen if on an xterm */
1037                 if (ioctl(0, TIOCGWINSZ, &xwinsz) == 0) {
1038                         if (xwinsz.height)
1039                                 screenheight = is_curses_enabled() ? (int)xwinsz.height - 1 : (int) xwinsz.height;
1040                         if (xwinsz.width)
1041                                 screenwidth = (int) xwinsz.width;
1042                 }
1043         }
1044 #endif
1045 }
1046
1047
1048 /*
1049  * set floor mode depending on client, server, and user settings
1050  */
1051 void set_floor_mode(CtdlIPC* ipc)
1052 {
1053         if (ipc->ServInfo.ok_floors == 0) {
1054                 floor_mode = 0; /* Don't use floors if the server */
1055         }
1056         /* doesn't support them!          */
1057         else {
1058                 if (rc_floor_mode == RC_NO) {   /* never use floors */
1059                         floor_mode = 0;
1060                 }
1061                 if (rc_floor_mode == RC_YES) {  /* always use floors */
1062                         floor_mode = 1;
1063                 }
1064                 if (rc_floor_mode == RC_DEFAULT) {      /* user choice */
1065                         floor_mode = ((userflags & US_FLOORS) ? 1 : 0);
1066                 }
1067         }
1068 }
1069
1070 /*
1071  * Set or change the user's password
1072  */
1073 int set_password(CtdlIPC *ipc)
1074 {
1075         char pass1[20];
1076         char pass2[20];
1077         char buf[SIZ];
1078
1079         if (strlen(rc_password) > 0) {
1080                 strcpy(pass1, rc_password);
1081                 strcpy(pass2, rc_password);
1082         } else {
1083                 IFNEXPERT formout(ipc, "changepw");
1084                 newprompt("Enter a new password: ", pass1, -19);
1085                 newprompt("Enter it again to confirm: ", pass2, -19);
1086         }
1087         strproc(pass1);
1088         strproc(pass2);
1089         if (!strcasecmp(pass1, pass2)) {
1090                 CtdlIPCChangePassword(ipc, pass1, buf);
1091                 scr_printf("%s\n", buf);
1092                 offer_to_remember_password(ipc, hostbuf, portbuf, fullname, pass1);
1093                 return (0);
1094         } else {
1095                 scr_printf("*** They don't match... try again.\n");
1096                 return (1);
1097         }
1098 }
1099
1100
1101
1102 /*
1103  * get info about the server we've connected to
1104  */
1105 void get_serv_info(CtdlIPC *ipc, char *supplied_hostname)
1106 {
1107         char buf[SIZ];
1108
1109         CtdlIPCServerInfo(ipc, buf);
1110
1111         /* be nice and identify ourself to the server */
1112         CtdlIPCIdentifySoftware(ipc, SERVER_TYPE, 0, REV_LEVEL,
1113                  (ipc->isLocal ? "local" : CITADEL),
1114                  (supplied_hostname) ? supplied_hostname : 
1115                  /* Look up the , in the bible if you're confused */
1116                  (locate_host(ipc, buf), buf), buf);
1117
1118         /*
1119          * Tell the server what our preferred content formats are.
1120          *
1121          * Originally we preferred HTML over plain text because we can format
1122          * it to the reader's screen width, but since our HTML-to-text parser
1123          * isn't really all that great, it's probably better to just go with
1124          * the plain text when we have it available.
1125          */
1126         if ((CtdlIPCSpecifyPreferredFormats(ipc, buf, "text/plain|text/html") / 100 )== 2) {
1127                 can_do_msg4 = 1;
1128         }
1129 }
1130
1131
1132
1133 /*
1134  * Record compare function for SortOnlineUsers()
1135  */
1136 int idlecmp(const void *rec1, const void *rec2) {
1137         time_t i1, i2;
1138
1139         i1 = extract_long(rec1, 5);
1140         i2 = extract_long(rec2, 5);
1141
1142         if (i1 < i2) return(1);
1143         if (i1 > i2) return(-1);
1144         return(0);
1145 }
1146
1147
1148 /*
1149  * Sort the list of online users by idle time.
1150  * This function frees the supplied buffer, and returns a new one
1151  * to the caller.  The caller is responsible for freeing the returned buffer.
1152  */
1153 char *SortOnlineUsers(char *listing) {
1154         int rows;
1155         char *sortbuf;
1156         char *retbuf;
1157         char buf[SIZ];
1158         int i;
1159
1160         rows = num_tokens(listing, '\n');
1161         sortbuf = malloc(rows * SIZ);
1162         if (sortbuf == NULL) return(listing);
1163         retbuf = malloc(rows * SIZ);
1164         if (retbuf == NULL) {
1165                 free(sortbuf);
1166                 return(listing);
1167         }
1168
1169         /* Copy the list into a fixed-record-size array for sorting */
1170         for (i=0; i<rows; ++i) {
1171                 memset(buf, 0, SIZ);
1172                 extract_token(buf, listing, i, '\n', sizeof buf);
1173                 memcpy(&sortbuf[i*SIZ], buf, (size_t)SIZ);
1174         }
1175
1176         /* Do the sort */
1177         qsort(sortbuf, rows, SIZ, idlecmp);
1178
1179         /* Copy back to a \n delimited list */
1180         strcpy(retbuf, "");
1181         for (i=0; i<rows; ++i) {
1182                 strcat(retbuf, &sortbuf[i*SIZ]);
1183                 if (i<(rows-1)) strcat(retbuf, "\n");
1184         }
1185     free(listing);
1186     free(sortbuf);
1187         return(retbuf);
1188 }
1189
1190
1191
1192 /*
1193  * Display list of users currently logged on to the server
1194  */
1195 void who_is_online(CtdlIPC *ipc, int longlist)
1196 {
1197         char buf[SIZ], username[SIZ], roomname[SIZ], fromhost[SIZ];
1198         char flags[SIZ];
1199         char actual_user[SIZ], actual_room[SIZ], actual_host[SIZ];
1200         char clientsoft[SIZ];
1201         time_t timenow = 0;
1202         time_t idletime, idlehours, idlemins, idlesecs;
1203         int last_session = (-1);
1204         int skipidle = 0;
1205         char *listing = NULL;
1206         int r;                          /* IPC response code */
1207     
1208         if (longlist == 2) {
1209                 longlist = 0;
1210                 skipidle = 1;
1211         }
1212
1213         if (!longlist) {
1214                 color(BRIGHT_WHITE);
1215                 pprintf("           User Name               Room          ");
1216                 if (screenwidth >= 80) pprintf(" Idle        From host");
1217                 pprintf("\n");
1218                 color(DIM_WHITE);
1219                 pprintf("   ------------------------- --------------------");
1220                 if (screenwidth >= 80) pprintf(" ---- ------------------------");
1221                 pprintf("\n");
1222         }
1223         r = CtdlIPCOnlineUsers(ipc, &listing, &timenow, buf);
1224         listing = SortOnlineUsers(listing);
1225         if (r / 100 == 1) {
1226                 while (strlen(listing) > 0) {
1227                         int isidle = 0;
1228                         
1229                         /* Get another line */
1230                         extract_token(buf, listing, 0, '\n', sizeof buf);
1231                         remove_token(listing, 0, '\n');
1232
1233                         extract_token(username, buf, 1, '|', sizeof username);
1234                         extract_token(roomname, buf, 2, '|', sizeof roomname);
1235                         extract_token(fromhost, buf, 3, '|', sizeof fromhost);
1236                         extract_token(clientsoft, buf, 4, '|', sizeof clientsoft);
1237                         extract_token(flags, buf, 7, '|', sizeof flags);
1238
1239                         idletime = timenow - extract_long(buf, 5);
1240                         idlehours = idletime / 3600;
1241                         idlemins = (idletime - (idlehours * 3600)) / 60;
1242                         idlesecs = (idletime - (idlehours * 3600) - (idlemins * 60));
1243
1244                         if (idletime > rc_idle_threshold) {
1245                                 if (skipidle) {
1246                                         isidle = 1;
1247                                 }
1248                         }
1249
1250                         if (longlist) {
1251                                 extract_token(actual_user, buf, 8, '|', sizeof actual_user);
1252                                 extract_token(actual_room, buf, 9, '|', sizeof actual_room);
1253                                 extract_token(actual_host, buf, 10, '|', sizeof actual_host);
1254
1255                                 pprintf("  Flags: %s\n", flags);
1256                                 pprintf("Session: %d\n", extract_int(buf, 0));
1257                                 pprintf("   Name: %s\n", username);
1258                                 pprintf("In room: %s\n", roomname);
1259                                 pprintf("   Host: %s\n", fromhost);
1260                                 pprintf(" Client: %s\n", clientsoft);
1261                                 pprintf("   Idle: %ld:%02ld:%02ld\n",
1262                                         (long) idlehours,
1263                                         (long) idlemins,
1264                                         (long) idlesecs);
1265
1266                                 if ( (strlen(actual_user)+strlen(actual_room)+strlen(actual_host)) > 0) {
1267                                         pprintf("(really ");
1268                                         if (strlen(actual_user)>0) pprintf("<%s> ", actual_user);
1269                                         if (strlen(actual_room)>0) pprintf("in <%s> ", actual_room);
1270                                         if (strlen(actual_host)>0) pprintf("from <%s> ", actual_host);
1271                                         pprintf(")\n");
1272                                 }
1273                                 pprintf("\n");
1274
1275                         } else {
1276                                 if (isidle == 0) {
1277                                         if (extract_int(buf, 0) == last_session) {
1278                                                 pprintf("        ");
1279                                         }
1280                                         else {
1281                                                 color(BRIGHT_MAGENTA);
1282                                                 pprintf("%-3s", flags);
1283                                         }
1284                                         last_session = extract_int(buf, 0);
1285                                         color(BRIGHT_CYAN);
1286                                         pprintf("%-25s ", username);
1287                                         color(BRIGHT_MAGENTA);
1288                                         roomname[20] = 0;
1289                                         pprintf("%-20s", roomname);
1290
1291                                         if (screenwidth >= 80) {
1292                                                 pprintf(" ");
1293                                                 if (idletime > rc_idle_threshold) {
1294                                                         /* over 1000d, must be gone fishing */
1295                                                         if (idlehours > 23999) {
1296                                                                 pprintf("fish");
1297                                                         /* over 10 days */
1298                                                         } else if (idlehours > 239) {
1299                                                                 pprintf("%3ldd", idlehours / 24);
1300                                                         /* over 10 hours */
1301                                                         } else if (idlehours > 9) {
1302                                                                 pprintf("%1ldd%02ld",
1303                                                                         idlehours / 24,
1304                                                                         idlehours % 24);
1305                                                         /* less than 10 hours */
1306                                                         }
1307                                                         else {
1308                                                                 pprintf("%1ld:%02ld", idlehours, idlemins);
1309                                                         }
1310                                                 }
1311                                                 else {
1312                                                         pprintf("    ");
1313                                                 }
1314                                                 pprintf(" ");
1315                                                 color(BRIGHT_CYAN);
1316                                                 fromhost[24] = '\0';
1317                                                 pprintf("%-24s", fromhost);
1318                                         }
1319                                         pprintf("\n");
1320                                         color(DIM_WHITE);
1321                                 }
1322                         }
1323                 }
1324         }
1325         free(listing);
1326 }
1327
1328 void enternew(CtdlIPC *ipc, char *desc, char *buf, int maxlen)
1329 {
1330         char bbb[128];
1331         snprintf(bbb, sizeof bbb, "Enter in your new %s: ", desc);
1332         newprompt(bbb, buf, maxlen);
1333 }
1334
1335
1336
1337 int shift(int argc, char **argv, int start, int count) {
1338         int i;
1339
1340         for (i=start; i<(argc-count); ++i) {
1341                 argv[i] = argv[i+count];
1342         }
1343         argc = argc - count;
1344         return argc;
1345 }
1346
1347 static void statusHook(char *s) {
1348         sln_printf(s);
1349         sln_flush();
1350 }
1351
1352 /*
1353  * main
1354  */
1355 int main(int argc, char **argv)
1356 {
1357         int a, b, mcmd;
1358         char aaa[100], bbb[100];/* general purpose variables */
1359         char argbuf[64];        /* command line buf */
1360         char nonce[NONCE_SIZE];
1361         char *telnet_client_host = NULL;
1362         char *sptr, *sptr2;     /* USed to extract the nonce */
1363         char hexstring[MD5_HEXSTRING_SIZE];
1364         int stored_password = 0;
1365         char password[SIZ];
1366         struct ctdlipcmisc chek;
1367         struct ctdluser *myself = NULL;
1368         CtdlIPC* ipc;                   /* Our server connection */
1369         int r;                          /* IPC result code */
1370
1371         int relh=0;
1372         int home=0;
1373         char relhome[PATH_MAX]="";
1374         char ctdldir[PATH_MAX]=CTDLDIR;
1375     int lp; 
1376
1377
1378         calc_dirs_n_files(relh, home, relhome, ctdldir);
1379         
1380         setIPCDeathHook(screen_delete);
1381         setIPCErrorPrintf(err_printf);
1382         setCryptoStatusHook(statusHook);
1383         
1384         /* Permissions sanity check - don't run citadel setuid/setgid */
1385         if (getuid() != geteuid()) {
1386                 err_printf("Please do not run citadel setuid!\n");
1387                 logoff(NULL, 3);
1388         } else if (getgid() != getegid()) {
1389                 err_printf("Please do not run citadel setgid!\n");
1390                 logoff(NULL, 3);
1391         }
1392
1393         stty_ctdl(SB_SAVE);     /* Store the old terminal parameters */
1394         load_command_set();     /* parse the citadel.rc file */
1395         stty_ctdl(SB_NO_INTR);  /* Install the new ones */
1396         /* signal(SIGHUP, dropcarr);FIXME */    /* Cleanup gracefully if carrier is dropped */
1397         signal(SIGPIPE, dropcarr);      /* Cleanup gracefully if local conn. dropped */
1398         signal(SIGTERM, dropcarr);      /* Cleanup gracefully if terminated */
1399         signal(SIGCONT, catch_sigcont); /* Catch SIGCONT so we can reset terminal */
1400 #ifdef SIGWINCH
1401         signal(SIGWINCH, scr_winch);    /* Window resize signal */
1402 #endif
1403
1404 #ifdef HAVE_OPENSSL
1405         arg_encrypt = RC_DEFAULT;
1406 #endif
1407 #if defined(HAVE_CURSES_H) && !defined(DISABLE_CURSES)
1408         arg_screen = RC_DEFAULT;
1409 #endif
1410
1411         /* 
1412          * Handle command line options as if we were called like /bin/login
1413          * (i.e. from in.telnetd)
1414          */
1415         for (a=0; a<argc; ++a) {
1416                 if ((argc > a+1) && (!strcmp(argv[a], "-h")) ) {
1417                         telnet_client_host = argv[a+1];
1418                         argc = shift(argc, argv, a, 2);
1419                 }
1420                 if (!strcmp(argv[a], "-x")) {
1421 #ifdef HAVE_OPENSSL
1422                         arg_encrypt = RC_NO;
1423 #endif
1424                         argc = shift(argc, argv, a, 1);
1425                 }
1426                 if (!strcmp(argv[a], "-X")) {
1427 #ifdef HAVE_OPENSSL
1428                         arg_encrypt = RC_YES;
1429                         argc = shift(argc, argv, a, 1);
1430 #else
1431                         fprintf(stderr, "Not compiled with encryption support");
1432                         return 1;
1433 #endif
1434                 }
1435                 if (!strcmp(argv[a], "-s")) {
1436 #if defined(HAVE_CURSES_H) && !defined(DISABLE_CURSES)
1437                         arg_screen = RC_NO;
1438 #endif
1439                         argc = shift(argc, argv, a, 1);
1440                 }
1441                 if (!strcmp(argv[a], "-S")) {
1442 #if defined(HAVE_CURSES_H) && !defined(DISABLE_CURSES)
1443                         arg_screen = RC_YES;
1444 #endif
1445                         argc = shift(argc, argv, a, 1);
1446                 }
1447                 if (!strcmp(argv[a], "-p")) {
1448                         struct stat st;
1449                 
1450                         if (chdir(CTDLDIR) < 0) {
1451                                 perror("can't change to " CTDLDIR);
1452                                 logoff(NULL, 3);
1453                         }
1454
1455                         /*
1456                          * Drop privileges if necessary. We stat
1457                          * citadel.config to get the uid/gid since it's
1458                          * guaranteed to have the uid/gid we want.
1459                          */
1460                         if (!getuid() || !getgid()) {
1461                                 if (stat(file_citadel_config, &st) < 0) {
1462                                         perror("couldn't stat citadel.config");
1463                                         logoff(NULL, 3);
1464                                 }
1465                                 if (!getgid() && (setgid(st.st_gid) < 0)) {
1466                                         perror("couldn't change gid");
1467                                         logoff(NULL, 3);
1468                                 }
1469                                 if (!getuid() && (setuid(st.st_uid) < 0)) {
1470                                         perror("couldn't change uid");
1471                                         logoff(NULL, 3);
1472                                 }
1473                                 /*
1474                                   scr_printf("Privileges changed to uid %d gid %d\n",
1475                                   getuid(), getgid());
1476                                 */
1477                         }
1478                         argc = shift(argc, argv, a, 1);
1479                 }
1480         }
1481         
1482
1483         screen_new();
1484
1485 #ifdef __CYGWIN__
1486         newprompt("Connect to (return for local server): ", hostbuf, 64);
1487 #endif
1488
1489         sln_printf("Attaching to server... \r");
1490         sln_flush();
1491         ipc = CtdlIPC_new(argc, argv, hostbuf, portbuf);
1492         if (!ipc) {
1493                 screen_delete();
1494                 error_printf("Can't connect: %s\n", strerror(errno));
1495                 logoff(NULL, 3);
1496         }
1497 #if defined(HAVE_CURSES_H) && !defined(DISABLE_CURSES)
1498         CtdlIPC_SetNetworkStatusCallback(ipc, wait_indicator);
1499 #endif
1500         ipc_for_signal_handlers = ipc;  /* KLUDGE cover your eyes */
1501
1502         CtdlIPC_chat_recv(ipc, aaa);
1503         if (aaa[0] != '2') {
1504                 scr_printf("%s\n", &aaa[4]);
1505                 logoff(ipc, atoi(aaa));
1506         }
1507
1508         /* If there is a [nonce] at the end, put the nonce in <nonce>, else nonce
1509          * is zeroized.
1510          */
1511         
1512         if ((sptr = strchr(aaa, '<')) == NULL)
1513                 {
1514                         nonce[0] = '\0';
1515                 }
1516         else
1517                 {
1518                         if ((sptr2 = strchr(sptr, '>')) == NULL)
1519                                 {
1520                                         nonce[0] = '\0';
1521                                 }
1522                         else
1523                                 {
1524                                         sptr2++;
1525                                         *sptr2 = '\0';
1526                                         strncpy(nonce, sptr, (size_t)NONCE_SIZE);
1527                                 }
1528                 }
1529
1530 #ifdef HAVE_OPENSSL
1531         /* Evaluate encryption preferences */
1532         if (arg_encrypt != RC_NO && rc_encrypt != RC_NO) {
1533                 if (!ipc->isLocal || arg_encrypt == RC_YES || rc_encrypt == RC_YES) {
1534                         secure = (CtdlIPCStartEncryption(ipc, aaa) / 100 == 2) ? 1 : 0;
1535                         if (!secure)
1536                                 error_printf("Can't encrypt: %s\n", aaa);
1537                 }
1538         }
1539 #endif
1540
1541         get_serv_info(ipc, telnet_client_host);
1542         scr_printf("%-24s\n%s\n%s\n", ipc->ServInfo.software, ipc->ServInfo.humannode,
1543                    ipc->ServInfo.site_location);
1544         scr_flush();
1545
1546         status_line(ipc->ServInfo.humannode, ipc->ServInfo.site_location, NULL,
1547                     secure, -1);
1548
1549         screenwidth = 80;       /* default screen dimensions */
1550         screenheight = 24;
1551         
1552         scr_printf(" pause    next    stop\n");
1553         scr_printf(" ctrl-s  ctrl-o  ctrl-c\n\n");
1554         formout(ipc, "hello");  /* print the opening greeting */
1555         scr_printf("\n");
1556
1557  GSTA:  /* See if we have a username and password on disk */
1558         if (rc_remember_passwords) {
1559                 get_stored_password(hostbuf, portbuf, fullname, password);
1560                 if (strlen(fullname) > 0) {
1561                         r = CtdlIPCTryLogin(ipc, fullname, aaa);
1562                         if (r / 100 == 3) {
1563                                 if (*nonce) {
1564                                         r = CtdlIPCTryApopPassword(ipc, make_apop_string(password, nonce, hexstring, sizeof hexstring), aaa);
1565                                 } else {
1566                                         r = CtdlIPCTryPassword(ipc, password, aaa);
1567                                 }
1568                         }
1569
1570                         if (r / 100 == 2) {
1571                                 load_user_info(aaa);
1572                                 stored_password = 1;
1573                                 goto PWOK;
1574                         } else {
1575                                 set_stored_password(hostbuf, portbuf, "", "");
1576                         }
1577                 }
1578         }
1579
1580         termn8 = 0;
1581         newnow = 0;
1582         do {
1583                 if (strlen(rc_username) > 0) {
1584                         strcpy(fullname, rc_username);
1585                 } else {
1586                         newprompt("Enter your name: ", fullname, 29);
1587                 }
1588                 strproc(fullname);
1589                 if (!strcasecmp(fullname, "new")) {     /* just in case */
1590                         scr_printf("Please enter the name you wish to log in with.\n");
1591                 }
1592         } while (
1593                  (!strcasecmp(fullname, "bbs"))
1594                  || (!strcasecmp(fullname, "new"))
1595                  || (strlen(fullname) == 0));
1596
1597         if (!strcasecmp(fullname, "off")) {
1598                 mcmd = 29;
1599                 goto TERMN8;
1600         }
1601         /* sign on to the server */
1602         r = CtdlIPCTryLogin(ipc, fullname, aaa);
1603         if (r / 100 != 3)
1604                 goto NEWUSR;
1605
1606         /* password authentication */
1607         if (strlen(rc_password) > 0) {
1608                 strcpy(password, rc_password);
1609         } else {
1610                 newprompt("\rPlease enter your password: ", password, -19);
1611         }
1612         strproc(password);
1613
1614         if (*nonce) {
1615                 r = CtdlIPCTryApopPassword(ipc, make_apop_string(password, nonce, hexstring, sizeof hexstring), aaa);
1616         } else {
1617                 r = CtdlIPCTryPassword(ipc, password, aaa);
1618         }
1619         
1620         if (r / 100 == 2) {
1621                 load_user_info(aaa);
1622                 offer_to_remember_password(ipc, hostbuf, portbuf,
1623                                            fullname, password);
1624                 goto PWOK;
1625         }
1626         scr_printf("<< wrong password >>\n");
1627         if (strlen(rc_password) > 0)
1628                 logoff(ipc, 2);
1629         goto GSTA;
1630
1631 NEWUSR: if (strlen(rc_password) == 0) {
1632                 scr_printf("'%s' not found.\n", fullname);
1633                 scr_printf("Type 'off' if you would like to exit.\n");
1634                 if (ipc->ServInfo.newuser_disabled == 1) {
1635                         goto GSTA;
1636                 }
1637                 scr_printf("Do you want to create a new user account called '%s'? ",
1638                         fullname);
1639                 if (yesno() == 0) {
1640                         goto GSTA;
1641                 }
1642         }
1643
1644         r = CtdlIPCCreateUser(ipc, fullname, 1, aaa);
1645         if (r / 100 != 2) {
1646                 scr_printf("%s\n", aaa);
1647                 goto GSTA;
1648         }
1649         load_user_info(aaa);
1650
1651         while (set_password(ipc) != 0);
1652         newnow = 1;
1653
1654         enter_config(ipc, 1);
1655
1656  PWOK:
1657         /* Switch color support on or off if we're in user mode */
1658         if (rc_ansi_color == 3) {
1659                 if (userflags & US_COLOR)
1660                         enable_color = 1;
1661                 else
1662                         enable_color = 0;
1663         }
1664
1665         color(BRIGHT_WHITE);
1666         scr_printf("\n%s\nAccess level: %d (%s)\n"
1667                    "User #%ld / Login #%d",
1668                    fullname, axlevel, axdefs[(int) axlevel],
1669                    usernum, timescalled);
1670         if (lastcall > 0L) {
1671                 scr_printf(" / Last login: %s",
1672                            asctime(localtime(&lastcall)));
1673         }
1674         scr_printf("\n");
1675
1676         r = CtdlIPCMiscCheck(ipc, &chek, aaa);
1677         if (r / 100 == 2) {
1678                 b = chek.newmail;
1679                 if (b > 0) {
1680                         color(BRIGHT_RED);
1681                         if (b == 1)
1682                                 scr_printf("*** You have a new private message in Mail>\n");
1683                         if (b > 1)
1684                                 scr_printf("*** You have %d new private messages in Mail>\n", b);
1685                         color(DIM_WHITE);
1686                         if (strlen(rc_gotmail_cmd) > 0) {
1687                                 system(rc_gotmail_cmd);
1688                         }
1689                 }
1690                 if ((axlevel >= 6) && (chek.needvalid > 0)) {
1691                         scr_printf("*** Users need validation\n");
1692                 }
1693                 if (chek.needregis > 0) {
1694                         scr_printf("*** Please register.\n");
1695                         formout(ipc, "register");
1696                         entregis(ipc);
1697                 }
1698         }
1699         /* Make up some temporary filenames for use in various parts of the
1700          * program.  Don't mess with these once they've been set, because we
1701          * will be unlinking them later on in the program and we don't
1702          * want to delete something that we didn't create. */
1703         CtdlMakeTempFileName(temp, sizeof temp);
1704         CtdlMakeTempFileName(temp2, sizeof temp2);
1705         CtdlMakeTempFileName(tempdir, sizeof tempdir);
1706
1707         /* Get screen dimensions.  First we go to a default of 80x24.  Then
1708          * we try to get the user's actual screen dimensions off the server.
1709          * However, if we're running on an xterm, all this stuff is
1710          * irrelevant because we're going to dynamically size the screen
1711          * during the session.
1712          */
1713         screenwidth = 80;
1714         screenheight = 24;
1715         r = CtdlIPCGetConfig(ipc, &myself, aaa);
1716         if (r == 2) {
1717                 screenwidth = myself->USscreenwidth;
1718                 screenheight = myself->USscreenheight;
1719         }
1720         if (getenv("TERM") != NULL)
1721                 if (!strcmp(getenv("TERM"), "xterm")) {
1722                         have_xterm = 1;
1723                 }
1724 #ifdef TIOCGWINSZ
1725         check_screen_dims();
1726 #endif
1727
1728         set_floor_mode(ipc);
1729
1730         /* Enter the lobby */
1731         dotgoto(ipc, "_BASEROOM_", 1, 0);
1732
1733         /* Main loop for the system... user is logged in. */
1734     free(uglist[0]);
1735         uglistsize = 0;
1736
1737         if (newnow == 1)
1738                 readmsgs(ipc, LastMessages, ReadForward, 5);
1739         else
1740                 readmsgs(ipc, NewMessages, ReadForward, 0);
1741
1742         /* MAIN COMMAND LOOP */
1743         do {
1744                 mcmd = getcmd(ipc, argbuf);     /* Get keyboard command */
1745
1746 #ifdef TIOCGWINSZ
1747                 check_screen_dims();            /* if xterm, get screen size */
1748 #endif
1749
1750                 if (termn8 == 0)
1751                         switch (mcmd) {
1752                         case 1:
1753                                 formout(ipc, "help");
1754                                 break;
1755                         case 4:
1756                                 entmsg(ipc, 0, ((userflags & US_EXTEDIT) ? 2 : 0), 0);
1757                                 break;
1758                         case 36:
1759                                 entmsg(ipc, 0, 1, 0);
1760                                 break;
1761                         case 46:
1762                                 entmsg(ipc, 0, 2, 0);
1763                                 break;
1764                         case 78:
1765                                 entmsg(ipc, 0, ((userflags & US_EXTEDIT) ? 2 : 0), 1);
1766                                 break;
1767                         case 5:                         /* <G>oto */
1768                                 updatels(ipc);
1769                                 gotonext(ipc);
1770                                 break;
1771                         case 47:                        /* <A>bandon */
1772                                 if (!rc_alt_semantics) {
1773                                         updatelsa(ipc);
1774                                 }
1775                                 gotonext(ipc);
1776                                 break;
1777                         case 90:                        /* <.A>bandon */
1778                                 if (!rc_alt_semantics)
1779                                         updatelsa(ipc);
1780                                 dotgoto(ipc, argbuf, 0, 0);
1781                                 break;
1782                         case 58:                        /* <M>ail */
1783                                 updatelsa(ipc);
1784                                 dotgoto(ipc, "_MAIL_", 1, 0);
1785                                 break;
1786                         case 20:
1787                                 if (strlen(argbuf) > 0) {
1788                                         updatels(ipc);
1789                                         dotgoto(ipc, argbuf, 0, 0);
1790                                 }
1791                                 break;
1792                         case 52:
1793                                 if (strlen(argbuf) > 0) {
1794                                         if (rc_alt_semantics) {
1795                                                 updatelsa(ipc);
1796                                         }
1797                                         dotgoto(ipc, argbuf, 0, 0);
1798                                 }
1799                                 break;
1800                         case 95: /* what exactly is the numbering scheme supposed to be anyway? --Ford, there isn't one. -IO */
1801                                 dotungoto(ipc, argbuf);
1802                                 break;
1803                         case 10:
1804                                 readmsgs(ipc, AllMessages, ReadForward, 0);
1805                                 break;
1806                         case 9:
1807                                 readmsgs(ipc, LastMessages, ReadForward, 5);
1808                                 break;
1809                         case 13:
1810                                 readmsgs(ipc, NewMessages, ReadForward, 0);
1811                                 break;
1812                         case 11:
1813                                 readmsgs(ipc, AllMessages, ReadReverse, 0);
1814                                 break;
1815                         case 12:
1816                                 readmsgs(ipc, OldMessages, ReadReverse, 0);
1817                                 break;
1818                         case 71:
1819                                 readmsgs(ipc, LastMessages, ReadForward,
1820                                                 atoi(argbuf));
1821                                 break;
1822                         case 7:
1823                                 forget(ipc);
1824                                 break;
1825                         case 18:
1826                                 subshell();
1827                                 break;
1828                         case 38:
1829                                 updatels(ipc);
1830                                 entroom(ipc);
1831                                 break;
1832                         case 22:
1833                                 killroom(ipc);
1834                                 break;
1835                         case 32:
1836                                 userlist(ipc, argbuf);
1837                                 break;
1838                         case 27:
1839                                 invite(ipc);
1840                                 break;
1841                         case 28:
1842                                 kickout(ipc);
1843                                 break;
1844                         case 23:
1845                                 editthisroom(ipc);
1846                                 break;
1847                         case 14:
1848                                 roomdir(ipc);
1849                                 break;
1850                         case 33:
1851                                 download(ipc, 0);
1852                                 break;
1853                         case 34:
1854                                 download(ipc, 1);
1855                                 break;
1856                         case 31:
1857                                 download(ipc, 2);
1858                                 break;
1859                         case 43:
1860                                 download(ipc, 3);
1861                                 break;
1862                         case 45:
1863                                 download(ipc, 4);
1864                                 break;
1865                         case 55:
1866                                 download(ipc, 5);
1867                                 break;
1868                         case 39:
1869                                 upload(ipc, 0);
1870                                 break;
1871                         case 40:
1872                                 upload(ipc, 1);
1873                                 break;
1874                         case 42:
1875                                 upload(ipc, 2);
1876                                 break;
1877                         case 44:
1878                                 upload(ipc, 3);
1879                                 break;
1880                         case 57:
1881                                 cli_upload(ipc);
1882                                 break;
1883                         case 16:
1884                                 ungoto(ipc);
1885                                 break;
1886                         case 24:
1887                                 whoknows(ipc);
1888                                 break;
1889                         case 26:
1890                                 validate(ipc);
1891                                 break;
1892                         case 29:
1893                         case 30:
1894                                 if (!rc_alt_semantics) {
1895                                         updatels(ipc);
1896                                 }
1897                                 termn8 = 1;
1898                                 break;
1899                         case 48:
1900                                 enterinfo(ipc);
1901                                 break;
1902                         case 49:
1903                                 readinfo(ipc);
1904                                 break;
1905                         case 72:
1906                                 cli_image_upload(ipc, "_userpic_");
1907                                 break;
1908                         case 73:
1909                                 cli_image_upload(ipc, "_roompic_");
1910                                 break;
1911
1912                         case 74:
1913                                 snprintf(aaa, sizeof aaa, "_floorpic_|%d", curr_floor);
1914                                 cli_image_upload(ipc, aaa);
1915                                 break;
1916
1917                         case 75:
1918                                 enternew(ipc, "roomname", aaa, 20);
1919                                 r = CtdlIPCChangeRoomname(ipc, aaa, bbb);
1920                                 if (r / 100 != 2)
1921                                         scr_printf("\n%s\n", bbb);
1922                                 break;
1923                         case 76:
1924                                 enternew(ipc, "hostname", aaa, 25);
1925                                 r = CtdlIPCChangeHostname(ipc, aaa, bbb);
1926                                 if (r / 100 != 2)
1927                                         scr_printf("\n%s\n", bbb);
1928                                 break;
1929                         case 77:
1930                                 enternew(ipc, "username", aaa, 32);
1931                                 r = CtdlIPCChangeUsername(ipc, aaa, bbb);
1932                                 if (r / 100 != 2)
1933                                         scr_printf("\n%s\n", bbb);
1934                                 break;
1935
1936                         case 35:
1937                                 set_password(ipc);
1938                                 break;
1939
1940                         case 21:
1941                                 if (argbuf[0] == 0)
1942                                         strcpy(aaa, "?");
1943                                 display_help(ipc, argbuf);
1944                                 break;
1945
1946                         case 41:
1947                                 formout(ipc, "register");
1948                                 entregis(ipc);
1949                                 break;
1950
1951                         case 15:
1952                                 scr_printf("Are you sure (y/n)? ");
1953                                 if (yesno() == 1) {
1954                                         if (!rc_alt_semantics)
1955                                                 updatels(ipc);
1956                                         a = 0;
1957                                         termn8 = 1;
1958                                 }
1959                                 break;
1960
1961                         case 85:
1962                                 scr_printf("All users will be disconnected!  "
1963                                            "Really terminate the server? ");
1964                                 if (yesno() == 1) {
1965                                         if (!rc_alt_semantics)
1966                                                 updatels(ipc);
1967                                         r = CtdlIPCTerminateServerNow(ipc, aaa);
1968                                         scr_printf("%s\n", aaa);
1969                                         if (r / 100 == 2) {
1970                                                 a = 0;
1971                                                 termn8 = 1;
1972                                         }
1973                                 }
1974                                 break;
1975
1976                         case 86:
1977                                 scr_printf("Do you really want to schedule a "
1978                                            "server shutdown? ");
1979                                 if (yesno() == 1) {
1980                                         r = CtdlIPCTerminateServerScheduled(ipc, 1, aaa);
1981                                         if (r / 100 == 2) {
1982                                                 if (atoi(aaa)) {
1983                                                         scr_printf(
1984                                                                    "The Citadel server will terminate when all users are logged off.\n"
1985                                                                    );
1986                                                 } else {
1987                                                         scr_printf(
1988                                                                    "The Citadel server will not terminate.\n"
1989                                                                    );
1990                                                 }
1991                                         }
1992                                 }
1993                                 break;
1994
1995                         case 87:
1996                                 network_config_management(ipc, "listrecp",
1997                                                           "Message-by-message mailing list recipients");
1998                                 break;
1999
2000                         case 94:
2001                                 network_config_management(ipc, "digestrecp",
2002                                                           "Digest mailing list recipients");
2003                                 break;
2004
2005                         case 89:
2006                                 network_config_management(ipc, "ignet_push_share",
2007                                                           "Nodes with which we share this room");
2008                                 break;
2009
2010                         case 88:
2011                                 do_ignet_configuration(ipc);
2012                                 break;
2013
2014                         case 92:
2015                                 do_filterlist_configuration(ipc);
2016                                 break;
2017
2018                         case 6:
2019                                 if (rc_alt_semantics) {
2020                                         updatelsa(ipc);
2021                                 }
2022                                 gotonext(ipc);
2023                                 break;
2024
2025                         case 3:
2026                                 chatmode(ipc);
2027                                 break;
2028
2029                         case 2:
2030                                 if (ipc->isLocal) {
2031                                         screen_reset();
2032                                         stty_ctdl(SB_RESTORE);
2033                                         snprintf(aaa, sizeof aaa, "USERNAME=\042%s\042; export USERNAME;"
2034                                                  "exec ./subsystem %ld %d %d", fullname,
2035                                                  usernum, screenwidth, axlevel);
2036                                         ka_system(aaa);
2037                                         stty_ctdl(SB_NO_INTR);
2038                                         screen_set();
2039                                 } else {
2040                                         scr_printf("*** Can't run doors when server is not local.\n");
2041                                 }
2042                                 break;
2043
2044                         case 17:
2045                                 who_is_online(ipc, 0);
2046                                 break;
2047
2048                         case 79:
2049                                 who_is_online(ipc, 1);
2050                                 break;
2051
2052                         case 91:
2053                                 who_is_online(ipc, 2);
2054                                 break;
2055                 
2056                         case 80:
2057                                 do_system_configuration(ipc);
2058                                 break;
2059
2060                         case 82:
2061                                 do_internet_configuration(ipc);
2062                                 break;
2063
2064                         case 83:
2065                                 check_message_base(ipc);
2066                                 break;
2067
2068                         case 84:
2069                                 quiet_mode(ipc);
2070                                 break;
2071
2072                         case 93:
2073                                 stealth_mode(ipc);
2074                                 break;
2075
2076                         case 50:
2077                                 enter_config(ipc, 2);
2078                                 break;
2079
2080                         case 37:
2081                                 enter_config(ipc, 0);
2082                                 set_floor_mode(ipc);
2083                                 break;
2084
2085                         case 59:
2086                                 enter_config(ipc, 3);
2087                                 set_floor_mode(ipc);
2088                                 break;
2089
2090                         case 60:
2091                                 gotofloor(ipc, argbuf, GF_GOTO);
2092                                 break;
2093
2094                         case 61:
2095                                 gotofloor(ipc, argbuf, GF_SKIP);
2096                                 break;
2097
2098                         case 62:
2099                                 forget_this_floor(ipc);
2100                                 break;
2101
2102                         case 63:
2103                                 create_floor(ipc);
2104                                 break;
2105
2106                         case 64:
2107                                 edit_floor(ipc);
2108                                 break;
2109
2110                         case 65:
2111                                 kill_floor(ipc);
2112                                 break;
2113
2114                         case 66:
2115                                 enter_bio(ipc);
2116                                 break;
2117
2118                         case 67:
2119                                 read_bio(ipc);
2120                                 break;
2121
2122                         case 25:
2123                                 edituser(ipc, 25);
2124                                 break;
2125
2126                         case 96:
2127                                 edituser(ipc, 96);
2128                                 break;
2129
2130                         case 8:
2131                                 knrooms(ipc, floor_mode);
2132                                 scr_printf("\n");
2133                                 break;
2134
2135                         case 68:
2136                                 knrooms(ipc, 2);
2137                                 scr_printf("\n");
2138                                 break;
2139
2140                         case 69:
2141                                 misc_server_cmd(ipc, argbuf);
2142                                 break;
2143
2144                         case 70:
2145                                 edit_system_message(ipc, argbuf);
2146                                 break;
2147
2148                         case 19:
2149                                 listzrooms(ipc);
2150                                 scr_printf("\n");
2151                                 break;
2152
2153                         case 51:
2154                                 deletefile(ipc);
2155                                 break;
2156
2157                         case 53:
2158                                 netsendfile(ipc);
2159                                 break;
2160
2161                         case 54:
2162                                 movefile(ipc);
2163                                 break;
2164
2165                         case 56:
2166                                 page_user(ipc);
2167                                 break;
2168
2169             case 110:           /* <+> Next room */
2170                                  gotoroomstep(ipc, 1, 0);
2171                              break;
2172
2173             case 111:           /* <-> Previous room */
2174                  gotoroomstep(ipc, 0, 0);
2175                              break;
2176
2177                         case 112:           /* <>> Next floor */
2178                  gotofloorstep(ipc, 1, GF_GOTO);
2179                              break;
2180
2181                         case 113:           /* <<> Previous floor */
2182                  gotofloorstep(ipc, 0, GF_GOTO);
2183                                  break;
2184
2185             case 116:           /* <.> skip to <+> Next room */
2186                  gotoroomstep(ipc, 1, 1);
2187                              break;
2188
2189             case 117:           /* <.> skip to <-> Previous room */
2190                  gotoroomstep(ipc, 0, 1);
2191                              break;
2192
2193                         case 118:           /* <.> skip to <>> Next floor */
2194                  gotofloorstep(ipc, 1, GF_SKIP);
2195                              break;
2196
2197                         case 119:           /* <.> skip to <<> Previous floor */
2198                  gotofloorstep(ipc, 0, GF_SKIP);
2199                                  break;
2200
2201                         case 114:           
2202                  read_config(ipc);
2203                                  break;
2204
2205                         case 115:           
2206                  system_info(ipc);
2207                                  break;
2208
2209                         case 120:           /* .KAnonymous */
2210                          dotknown(ipc, 0, NULL);
2211                                  break;
2212
2213                         case 121:           /* .KDirectory */
2214                          dotknown(ipc, 1, NULL);
2215                                  break;
2216
2217                         case 122:           /* .KMatch */
2218                          dotknown(ipc, 2, argbuf);
2219                                  break;
2220
2221                         case 123:           /* .KpreferredOnly */
2222                          dotknown(ipc, 3, NULL);
2223                                  break;
2224
2225                         case 124:           /* .KPrivate */
2226                          dotknown(ipc, 4, NULL);
2227                                  break;
2228
2229                         case 125:           /* .KRead only */
2230                          dotknown(ipc, 5, NULL);
2231                                  break;
2232
2233                         case 126:           /* .KShared */
2234                          dotknown(ipc, 6, NULL);
2235                                  break;
2236
2237                         default: /* allow some math on the command */
2238                                 /* commands 100... to 100+MAX_EDITORS-1 will
2239                                    call the appropriate editor... in other
2240                                    words, command numbers 100+ will match
2241                                    the citadel.rc keys editor0, editor1, etc.*/
2242                                 if (mcmd >= 100 && mcmd < (100+MAX_EDITORS))
2243                                 {
2244                                         /* entmsg mode >=2 select editor */
2245                                         entmsg(ipc, 0, mcmd - 100 + 2, 0);
2246                                         break;
2247                                 }
2248                         }       /* end switch */
2249         } while (termn8 == 0);
2250
2251 TERMN8: scr_printf("%s logged out.", fullname);
2252         termn8 = 0;
2253         color(ORIGINAL_PAIR);
2254         scr_printf("\n");
2255         while (march != NULL) {
2256                 remove_march(march->march_name, 0);
2257         }
2258         if (mcmd == 30) {
2259                 sln_printf("\n\nType 'off' to disconnect, or next user...\n");
2260         }
2261         CtdlIPCLogout(ipc);
2262         if ((mcmd == 29) || (mcmd == 15)) {
2263                 screen_delete();
2264                 stty_ctdl(SB_RESTORE);
2265                 formout(ipc, "goodbye");
2266                 logoff(ipc, 0);
2267         }
2268         /* Free the ungoto list */
2269         for (lp = 0; lp < uglistsize; lp++) {
2270                 free(uglist[lp]);
2271         }
2272     uglistsize = 0;
2273         goto GSTA;
2274
2275 }       /* end main() */