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