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