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