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