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