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