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