]> code.citadel.org Git - citadel.git/blob - citadel/citadel.c
07a9eb6152d134d1d2fdaba7412c7b031e90458a
[citadel.git] / citadel / citadel.c
1 /*
2  * $Id$
3  *
4  * Main source module for the client program.
5  */
6
7 #include "sysdep.h"
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <stdio.h>
12 #include <ctype.h>
13 #include <string.h>
14
15 #if TIME_WITH_SYS_TIME
16 # include <sys/time.h>
17 # include <time.h>
18 #else
19 # if HAVE_SYS_TIME_H
20 #  include <sys/time.h>
21 # else
22 #  include <time.h>
23 # endif
24 #endif
25
26 #include <limits.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <sys/stat.h>
30 #include <sys/ioctl.h>
31 #include <signal.h>
32 #include <pwd.h>
33 #include <stdarg.h>
34 #include <errno.h>
35
36 #include "citadel.h"
37 #include "citadel_ipc.h"
38 #include "axdefs.h"
39 #include "routines.h"
40 #include "routines2.h"
41 #include "rooms.h"
42 #include "messages.h"
43 #include "commands.h"
44 #include "client_chat.h"
45 #include "client_passwords.h"
46 #include "citadel_decls.h"
47 #include "tools.h"
48 #include "acconfig.h"
49 #ifndef HAVE_SNPRINTF
50 #include "snprintf.h"
51 #endif
52 #include "screen.h"
53
54 #include "md5.h"
55
56 #define IFEXPERT if (userflags&US_EXPERT)
57 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
58 #define IFAIDE if (axlevel>=6)
59 #define IFNAIDE if (axlevel<6)
60
61 struct march *march = NULL;
62
63 /* globals associated with the client program */
64 char temp[PATH_MAX];            /* Name of general-purpose temp file */
65 char temp2[PATH_MAX];           /* Name of general-purpose temp file */
66 char tempdir[PATH_MAX];         /* Name of general-purpose temp directory */
67 char editor_paths[MAX_EDITORS][SIZ];    /* paths to external editors */
68 char printcmd[SIZ];             /* print command */
69 int editor_pid = (-1);
70 char fullname[USERNAME_SIZE];
71 struct CtdlServInfo serv_info;  /* Info on the server connected */
72 int screenwidth;
73 int screenheight;
74 unsigned room_flags;
75 char room_name[ROOMNAMELEN];
76 char *uglist[UGLISTLEN]; /* size of the ungoto list */
77 long uglistlsn[UGLISTLEN]; /* current read position for all the ungoto's. Not going to make any friends with this one. */
78 int uglistsize = 0;
79 char is_mail = 0;               /* nonzero when we're in a mail room */
80 char axlevel = 0;               /* access level */
81 char is_room_aide = 0;          /* boolean flag, 1 if room aide */
82 int timescalled;
83 int posted;
84 unsigned userflags;
85 long usernum = 0L;              /* user number */
86 time_t lastcall = 0L;           /* Date/time of previous login */
87 char newnow;
88 long highest_msg_read;          /* used for <A>bandon room cmd */
89 long maxmsgnum;                 /* used for <G>oto */
90 char sigcaught = 0;
91 char have_xterm = 0;            /* are we running on an xterm? */
92 char rc_username[USERNAME_SIZE];
93 char rc_password[32];
94 char hostbuf[SIZ];
95 char portbuf[SIZ];
96 char rc_floor_mode;
97 char floor_mode;
98 char curr_floor = 0;            /* number of current floor */
99 char floorlist[128][SIZ];       /* names of floors */
100 int termn8 = 0;                 /* Set to nonzero to cause a logoff */
101 int secure;                     /* Set to nonzero when wire is encrypted */
102 int can_do_msg4 = 0;            /* Set to nonzero if the server can handle MSG4 commands */
103
104 extern char express_msgs;       /* express messages waiting! */
105 extern int rc_ansi_color;       /* ansi color value from citadel.rc */
106 extern int next_lazy_cmd;
107
108 CtdlIPC *ipc_for_signal_handlers;       /* KLUDGE cover your eyes */
109
110 /*
111  * here is our 'clean up gracefully and exit' routine
112  */
113 void logoff(CtdlIPC *ipc, int code)
114 {
115         int lp;
116
117         if (editor_pid > 0) {   /* kill the editor if it's running */
118                 kill(editor_pid, SIGHUP);
119         }
120
121         /* Free the ungoto list */
122         for (lp = 0; lp < uglistsize; lp++) {
123                 free(uglist[lp]);
124         }
125
126 /* Shut down the server connection ... but not if the logoff code is 3,
127  * because that means we're exiting because we already lost the server.
128  */
129         if (code != 3) {
130                 CtdlIPCQuit(ipc);
131         }
132
133 /*
134  * now clean up various things
135  */
136         screen_delete();
137
138         unlink(temp);
139         unlink(temp2);
140         nukedir(tempdir);
141
142         /* Violently kill off any child processes if Citadel is
143          * the login shell. 
144          */
145         if (getppid() == 1) {
146                 kill(0 - getpgrp(), SIGTERM);
147                 sleep(1);
148                 kill(0 - getpgrp(), SIGKILL);
149         }
150         color(ORIGINAL_PAIR);   /* Restore the old color settings */
151         sttybbs(SB_RESTORE);    /* return the old terminal settings */
152         exit(code);             /* exit with the proper exit code */
153 }
154
155
156
157 /*
158  * signal catching function for hangups...
159  */
160 void dropcarr(int signum)
161 {
162         logoff(NULL, 3);        /* No IPC when server's already gone! */
163 }
164
165
166
167 /*
168  * catch SIGCONT to reset terminal modes when were are put back into the
169  * foreground.
170  */
171 void catch_sigcont(int signum)
172 {
173         sttybbs(SB_LAST);
174         signal(SIGCONT, catch_sigcont);
175 }
176
177
178 /* general purpose routines */
179
180 /* display a file */
181 void formout(CtdlIPC *ipc, char *name)
182 {
183         int r;                  /* IPC return code */
184         char buf[SIZ];
185         char *text = NULL;
186
187         r = CtdlIPCSystemMessage(ipc, name, &text, buf);
188         if (r / 100 != 1) {
189                 scr_printf("%s\n", buf);
190                 return;
191         }
192         if (text) {
193                 fmout(screenwidth, NULL, text, NULL,
194                       ((userflags & US_PAGINATOR) ? 1 : 0),
195                       screenheight, 1, 1);
196                 free(text);
197         }
198 }
199
200
201 void userlist(CtdlIPC *ipc, char *patn)
202 {
203         char buf[SIZ];
204         char fl[SIZ];
205         struct tm *tmbuf;
206         time_t lc;
207         int r;                          /* IPC response code */
208         char *listing = NULL;
209
210         r = CtdlIPCUserListing(ipc, &listing, buf);
211         if (r / 100 != 1) {
212                 pprintf("%s\n", buf);
213                 return;
214         }
215
216         pprintf("       User Name           Num  L  LastCall  Calls Posts\n");
217         pprintf("------------------------- ----- - ---------- ----- -----\n");
218         while (strlen(listing) > 0) {
219                 extract_token(buf, listing, 0, '\n');
220                 remove_token(listing, 0, '\n');
221
222                 if (sigcaught == 0) {
223                     extract(fl, buf, 0);
224                     if (pattern(fl, patn) >= 0) {
225                         pprintf("%-25s ", fl);
226                         pprintf("%5ld %d ", extract_long(buf, 2),
227                                extract_int(buf, 1));
228                         lc = extract_long(buf, 3);
229                         tmbuf = (struct tm *) localtime(&lc);
230                         pprintf("%02d/%02d/%04d ",
231                                (tmbuf->tm_mon + 1),
232                                tmbuf->tm_mday,
233                                (tmbuf->tm_year + 1900));
234                         pprintf("%5ld %5ld\n", extract_long(buf, 4), extract_long(buf, 5));
235                     }
236
237                 }
238         }
239         free(listing);
240         pprintf("\n");
241 }
242
243
244 /*
245  * grab assorted info about the user...
246  */
247 void load_user_info(char *params)
248 {
249         extract(fullname, params, 0);
250         axlevel = extract_int(params, 1);
251         timescalled = extract_int(params, 2);
252         posted = extract_int(params, 3);
253         userflags = extract_int(params, 4);
254         usernum = extract_long(params, 5);
255         lastcall = extract_long(params, 6);
256 }
257
258
259 /*
260  * Remove a room from the march list.  'floornum' is ignored unless
261  * 'roomname' is set to _FLOOR_, in which case all rooms on the requested
262  * floor will be removed from the march list.
263  */
264 void remove_march(char *roomname, int floornum)
265 {
266         struct march *mptr, *mptr2;
267
268         if (march == NULL)
269                 return;
270
271         if ((!strcasecmp(march->march_name, roomname))
272             || ((!strcasecmp(roomname, "_FLOOR_")) && (march->march_floor == floornum))) {
273                 mptr = march->next;
274                 free(march);
275                 march = mptr;
276                 return;
277         }
278         mptr2 = march;
279         for (mptr = march; mptr != NULL; mptr = mptr->next) {
280
281                 if ((!strcasecmp(mptr->march_name, roomname))
282                     || ((!strcasecmp(roomname, "_FLOOR_"))
283                         && (mptr->march_floor == floornum))) {
284
285                         mptr2->next = mptr->next;
286                         free(mptr);
287                         mptr = mptr2;
288                 } else {
289                         mptr2 = mptr;
290                 }
291         }
292 }
293
294
295 /*
296  * Locate the room on the march list which we most want to go to.  Each room
297  * is measured given a "weight" of preference based on various factors.
298  */
299 char *pop_march(int desired_floor)
300 {
301         static char TheRoom[ROOMNAMELEN];
302         int TheFloor = 0;
303         int TheOrder = 32767;
304         int TheWeight = 0;
305         int weight;
306         struct march *mptr = NULL;
307
308         strcpy(TheRoom, "_BASEROOM_");
309         if (march == NULL)
310                 return (TheRoom);
311
312         for (mptr = march; mptr != NULL; mptr = mptr->next) {
313                 weight = 0;
314                 if ((strcasecmp(mptr->march_name, "_BASEROOM_")))
315                         weight = weight + 10000;
316                 if (mptr->march_floor == desired_floor)
317                         weight = weight + 5000;
318
319                 weight = weight + ((128 - (mptr->march_floor)) * 128);
320                 weight = weight + (128 - (mptr->march_order));
321
322                 if (weight > TheWeight) {
323                         TheWeight = weight;
324                         strcpy(TheRoom, mptr->march_name);
325                         TheFloor = mptr->march_floor;
326                         TheOrder = mptr->march_order;
327                 }
328         }
329         return (TheRoom);
330 }
331
332
333 /*
334  * jump directly to a room
335  */
336 void dotgoto(CtdlIPC *ipc, char *towhere, int display_name, int fromungoto)
337 {
338         char aaa[SIZ], bbb[SIZ];
339         static long ls = 0L;
340         int newmailcount = 0;
341         int partial_match, best_match;
342         char from_floor;
343         int ugpos = uglistsize;
344         int r;                          /* IPC result code */
345         struct ctdlipcroom *room = NULL;
346
347         /* store ungoto information */
348         if (fromungoto == 0) {
349                 /* sloppy slide them all down, hey it's the client, who cares. :-) */
350                 if (uglistsize >= (UGLISTLEN-1)) {
351                         int lp;
352                         free (uglist[0]);
353                         for (lp = 0; lp < (UGLISTLEN-1); lp++) {
354                                 uglist[lp] = uglist[lp+1];
355                                 uglistlsn[lp] = uglistlsn[lp+1];
356                         }
357                         ugpos--;
358                 } else {
359                         uglistsize++;
360                 }
361         
362                 uglist[ugpos] = malloc(strlen(room_name)+1);
363                 strcpy(uglist[ugpos], room_name);
364                 uglistlsn[ugpos] = ls;
365         }
366       
367         /* first try an exact match */
368         r = CtdlIPCGotoRoom(ipc, towhere, "", &room, aaa);
369         if (r / 10 == 54) {
370                 newprompt("Enter room password: ", bbb, 9);
371                 r = CtdlIPCGotoRoom(ipc, towhere, bbb, &room, aaa);
372                 if (r / 10 == 54) {
373                         scr_printf("Wrong password.\n");
374                         return;
375                 }
376         }       
377
378         /*
379          * If a match is not found, try a partial match.
380          * Partial matches anywhere in the string carry a weight of 1,
381          * left-aligned matches carry a weight of 2.  Pick the room that
382          * has the highest-weighted match.
383          */
384         if (r / 100 != 2) {
385                 struct march *march = NULL;
386
387                 best_match = 0;
388                 strcpy(bbb, "");
389
390                 r = CtdlIPCKnownRooms(ipc, AllAccessibleRooms, 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(serv_info.serv_humannode, serv_info.serv_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(void)
703 {
704         if (serv_info.serv_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, &serv_info, 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(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(SIGTERM, dropcarr);      /* Cleanup gracefully if terminated */
1027         signal(SIGCONT, catch_sigcont); /* Catch SIGCONT so we can reset terminal */
1028 #ifdef SIGWINCH
1029         signal(SIGWINCH, scr_winch);    /* Window resize signal */
1030 #endif
1031
1032 #ifdef HAVE_OPENSSL
1033         arg_encrypt = RC_DEFAULT;
1034 #endif
1035 #if defined(HAVE_CURSES_H) && !defined(DISABLE_CURSES)
1036         arg_screen = RC_DEFAULT;
1037 #endif
1038
1039         /* 
1040          * Handle command line options as if we were called like /bin/login
1041          * (i.e. from in.telnetd)
1042          */
1043         for (a=0; a<argc; ++a) {
1044                 if ((argc > a+1) && (!strcmp(argv[a], "-h")) ) {
1045                         telnet_client_host = argv[a+1];
1046                         argc = shift(argc, argv, a, 2);
1047                 }
1048                 if (!strcmp(argv[a], "-x")) {
1049 #ifdef HAVE_OPENSSL
1050                         arg_encrypt = RC_NO;
1051 #endif
1052                         argc = shift(argc, argv, a, 1);
1053                 }
1054                 if (!strcmp(argv[a], "-X")) {
1055 #ifdef HAVE_OPENSSL
1056                         arg_encrypt = RC_YES;
1057                         argc = shift(argc, argv, a, 1);
1058 #else
1059                         fprintf(stderr, "Not compiled with encryption support");
1060                         return 1;
1061 #endif
1062                 }
1063                 if (!strcmp(argv[a], "-s")) {
1064 #if defined(HAVE_CURSES_H) && !defined(DISABLE_CURSES)
1065                         arg_screen = RC_NO;
1066 #endif
1067                         argc = shift(argc, argv, a, 1);
1068                 }
1069                 if (!strcmp(argv[a], "-S")) {
1070 #if defined(HAVE_CURSES_H) && !defined(DISABLE_CURSES)
1071                         arg_screen = RC_YES;
1072 #endif
1073                         argc = shift(argc, argv, a, 1);
1074                 }
1075                 if (!strcmp(argv[a], "-p")) {
1076                         struct stat st;
1077                 
1078                         if (chdir(BBSDIR) < 0) {
1079                                 perror("can't change to " BBSDIR);
1080                                 logoff(NULL, 3);
1081                         }
1082
1083                         /*
1084                          * Drop privileges if necessary. We stat
1085                          * citadel.config to get the uid/gid since it's
1086                          * guaranteed to have the uid/gid we want.
1087                          */
1088                         if (!getuid() || !getgid()) {
1089                                 if (stat(BBSDIR "/citadel.config", &st) < 0) {
1090                                         perror("couldn't stat citadel.config");
1091                                         logoff(NULL, 3);
1092                                 }
1093                                 if (!getgid() && (setgid(st.st_gid) < 0)) {
1094                                         perror("couldn't change gid");
1095                                         logoff(NULL, 3);
1096                                 }
1097                                 if (!getuid() && (setuid(st.st_uid) < 0)) {
1098                                         perror("couldn't change uid");
1099                                         logoff(NULL, 3);
1100                                 }
1101                                 /*
1102                                   scr_printf("Privileges changed to uid %d gid %d\n",
1103                                   getuid(), getgid());
1104                                 */
1105                         }
1106                         argc = shift(argc, argv, a, 1);
1107                 }
1108         }
1109
1110         screen_new();
1111
1112 #ifdef __CYGWIN__
1113         newprompt("Connect to (return for local server): ", hostbuf, 64);
1114 #endif
1115
1116         sln_printf("Attaching to server... \r");
1117         sln_flush();
1118         ipc = CtdlIPC_new(argc, argv, hostbuf, portbuf);
1119         if (!ipc) {
1120                 screen_delete();
1121                 error_printf("Can't connect: %s\n", strerror(errno));
1122                 logoff(NULL, 3);
1123         }
1124 #if defined(HAVE_CURSES_H) && !defined(DISABLE_CURSES)
1125         CtdlIPC_SetNetworkStatusCallback(ipc, wait_indicator);
1126 #endif
1127         ipc_for_signal_handlers = ipc;  /* KLUDGE cover your eyes */
1128
1129         CtdlIPC_getline(ipc, aaa);
1130         if (aaa[0] != '2') {
1131                 scr_printf("%s\n", &aaa[4]);
1132                 logoff(ipc, atoi(aaa));
1133         }
1134
1135         /* If there is a [nonce] at the end, put the nonce in <nonce>, else nonce
1136          * is zeroized.
1137          */
1138         
1139         if ((sptr = strchr(aaa, '<')) == NULL)
1140                 {
1141                         nonce[0] = '\0';
1142                 }
1143         else
1144                 {
1145                         if ((sptr2 = strchr(sptr, '>')) == NULL)
1146                                 {
1147                                         nonce[0] = '\0';
1148                                 }
1149                         else
1150                                 {
1151                                         sptr2++;
1152                                         *sptr2 = '\0';
1153                                         strncpy(nonce, sptr, NONCE_SIZE);
1154                                 }
1155                 }
1156
1157 #ifdef HAVE_OPENSSL
1158         /* Evaluate encryption preferences */
1159         if (arg_encrypt != RC_NO && rc_encrypt != RC_NO) {
1160                 if (!ipc->isLocal || arg_encrypt == RC_YES || rc_encrypt == RC_YES) {
1161                         secure = (CtdlIPCStartEncryption(ipc, aaa) / 100 == 2) ? 1 : 0;
1162                         if (!secure)
1163                                 error_printf("Can't encrypt: %s\n", aaa);
1164                 }
1165         }
1166 #endif
1167
1168         get_serv_info(ipc, telnet_client_host);
1169         scr_printf("%-24s\n%s\n%s\n", serv_info.serv_software, serv_info.serv_humannode,
1170                    serv_info.serv_bbs_city);
1171         scr_flush();
1172
1173         status_line(serv_info.serv_humannode, serv_info.serv_bbs_city, NULL,
1174                     secure, -1);
1175
1176         screenwidth = 80;       /* default screen dimensions */
1177         screenheight = 24;
1178         
1179         scr_printf(" pause    next    stop\n");
1180         scr_printf(" ctrl-s  ctrl-o  ctrl-c\n\n");
1181         formout(ipc, "hello");  /* print the opening greeting */
1182         scr_printf("\n");
1183
1184  GSTA:  /* See if we have a username and password on disk */
1185         if (rc_remember_passwords) {
1186                 get_stored_password(hostbuf, portbuf, fullname, password);
1187                 if (strlen(fullname) > 0) {
1188                         r = CtdlIPCTryLogin(ipc, fullname, aaa);
1189                         if (r / 100 == 3) {
1190                                 if (*nonce) {
1191                                         r = CtdlIPCTryApopPassword(ipc, make_apop_string(password, nonce, hexstring, sizeof hexstring), aaa);
1192                                 } else {
1193                                         r = CtdlIPCTryPassword(ipc, password, aaa);
1194                                 }
1195                         }
1196
1197                         if (r / 100 == 2) {
1198                                 load_user_info(aaa);
1199                                 stored_password = 1;
1200                                 goto PWOK;
1201                         } else {
1202                                 set_stored_password(hostbuf, portbuf, "", "");
1203                         }
1204                 }
1205         }
1206
1207         termn8 = 0;
1208         newnow = 0;
1209         do {
1210                 if (strlen(rc_username) > 0) {
1211                         strcpy(fullname, rc_username);
1212                 } else {
1213                         newprompt("Enter your name: ", fullname, 29);
1214                 }
1215                 strproc(fullname);
1216                 if (!strcasecmp(fullname, "new")) {     /* just in case */
1217                         scr_printf("Please enter the name you wish to log in with.\n");
1218                 }
1219         } while (
1220                  (!strcasecmp(fullname, "bbs"))
1221                  || (!strcasecmp(fullname, "new"))
1222                  || (strlen(fullname) == 0));
1223
1224         if (!strcasecmp(fullname, "off")) {
1225                 mcmd = 29;
1226                 goto TERMN8;
1227         }
1228         /* sign on to the server */
1229         r = CtdlIPCTryLogin(ipc, fullname, aaa);
1230         if (r / 100 != 3)
1231                 goto NEWUSR;
1232
1233         /* password authentication */
1234         if (strlen(rc_password) > 0) {
1235                 strcpy(password, rc_password);
1236         } else {
1237                 newprompt("\rPlease enter your password: ", password, -19);
1238         }
1239         strproc(password);
1240
1241         if (*nonce) {
1242                 r = CtdlIPCTryApopPassword(ipc, make_apop_string(password, nonce, hexstring, sizeof hexstring), aaa);
1243         } else {
1244                 r = CtdlIPCTryPassword(ipc, password, aaa);
1245         }
1246         
1247         if (r / 100 == 2) {
1248                 load_user_info(aaa);
1249                 offer_to_remember_password(ipc, hostbuf, portbuf,
1250                                            fullname, password);
1251                 goto PWOK;
1252         }
1253         scr_printf("<< wrong password >>\n");
1254         if (strlen(rc_password) > 0)
1255                 logoff(ipc, 2);
1256         goto GSTA;
1257
1258 NEWUSR: if (strlen(rc_password) == 0) {
1259                 scr_printf("'%s' not found.\n"
1260                         "Do you want to create a new account with this name? ");
1261                 if (yesno() == 0)
1262                         goto GSTA;
1263         }
1264
1265         r = CtdlIPCCreateUser(ipc, fullname, 1, aaa);
1266         if (r / 100 != 2) {
1267                 scr_printf("%s\n", aaa);
1268                 goto GSTA;
1269         }
1270         load_user_info(aaa);
1271
1272         while (set_password(ipc) != 0);
1273         newnow = 1;
1274
1275         enter_config(ipc, 1);
1276
1277  PWOK:
1278         /* Switch color support on or off if we're in user mode */
1279         if (rc_ansi_color == 3) {
1280                 if (userflags & US_COLOR)
1281                         enable_color = 1;
1282                 else
1283                         enable_color = 0;
1284         }
1285
1286         color(BRIGHT_WHITE);
1287         scr_printf("\n%s\nAccess level: %d (%s)\n"
1288                    "User #%ld / Login #%d",
1289                    fullname, axlevel, axdefs[(int) axlevel],
1290                    usernum, timescalled);
1291         if (lastcall > 0L) {
1292                 scr_printf(" / Last login: %s",
1293                            asctime(localtime(&lastcall)));
1294         }
1295         scr_printf("\n");
1296
1297         r = CtdlIPCMiscCheck(ipc, &chek, aaa);
1298         if (r / 100 == 2) {
1299                 b = chek.newmail;
1300                 if (b > 0) {
1301                         color(BRIGHT_RED);
1302                         if (b == 1)
1303                                 scr_printf("*** You have a new private message in Mail>\n");
1304                         if (b > 1)
1305                                 scr_printf("*** You have %d new private messages in Mail>\n", b);
1306                         color(DIM_WHITE);
1307                         if (strlen(rc_gotmail_cmd) > 0) {
1308                                 system(rc_gotmail_cmd);
1309                         }
1310                 }
1311                 if ((axlevel >= 6) && (chek.needvalid > 0)) {
1312                         scr_printf("*** Users need validation\n");
1313                 }
1314                 if (chek.needregis > 0) {
1315                         scr_printf("*** Please register.\n");
1316                         formout(ipc, "register");
1317                         entregis(ipc);
1318                 }
1319         }
1320         /* Make up some temporary filenames for use in various parts of the
1321          * program.  Don't mess with these once they've been set, because we
1322          * will be unlinking them later on in the program and we don't
1323          * want to delete something that we didn't create. */
1324         snprintf(temp, sizeof temp, tmpnam(NULL));
1325         snprintf(temp2, sizeof temp2, tmpnam(NULL));
1326         snprintf(tempdir, sizeof tempdir, tmpnam(NULL));
1327
1328         /* Get screen dimensions.  First we go to a default of 80x24.  Then
1329          * we try to get the user's actual screen dimensions off the server.
1330          * However, if we're running on an xterm, all this stuff is
1331          * irrelevant because we're going to dynamically size the screen
1332          * during the session.
1333          */
1334         screenwidth = 80;
1335         screenheight = 24;
1336         r = CtdlIPCGetConfig(ipc, &myself, aaa);
1337         if (r == 2) {
1338                 screenwidth = myself->USscreenwidth;
1339                 screenheight = myself->USscreenheight;
1340         }
1341         if (getenv("TERM") != NULL)
1342                 if (!strcmp(getenv("TERM"), "xterm")) {
1343                         have_xterm = 1;
1344                 }
1345 #ifdef TIOCGWINSZ
1346         check_screen_dims();
1347 #endif
1348
1349         set_floor_mode();
1350
1351         /* Enter the lobby */
1352         dotgoto(ipc, "_BASEROOM_", 1, 0);
1353
1354         /* Main loop for the system... user is logged in. */
1355         uglistsize = 0;
1356
1357         if (newnow == 1)
1358                 readmsgs(ipc, LastMessages, ReadForward, 5);
1359         else
1360                 readmsgs(ipc, NewMessages, ReadForward, 0);
1361
1362         /* MAIN COMMAND LOOP */
1363         do {
1364                 mcmd = getcmd(ipc, argbuf);     /* Get keyboard command */
1365
1366 #ifdef TIOCGWINSZ
1367                 check_screen_dims();            /* if xterm, get screen size */
1368 #endif
1369
1370                 if (termn8 == 0)
1371                         switch (mcmd) {
1372                         case 1:
1373                                 formout(ipc, "help");
1374                                 break;
1375                         case 4:
1376                                 entmsg(ipc, 0, 0);
1377                                 break;
1378                         case 36:
1379                                 entmsg(ipc, 0, 1);
1380                                 break;
1381                         case 46:
1382                                 entmsg(ipc, 0, 2);
1383                                 break;
1384                         case 78:
1385                                 newprompt("What do you want your username to be? ", aaa, 32);
1386                                 snprintf(bbb, sizeof bbb, "ENT0 2|0|0|0|%s", aaa);
1387                                 CtdlIPC_putline(ipc, bbb);
1388                                 CtdlIPC_getline(ipc, aaa);
1389                                 if (strncmp("200", aaa, 3))
1390                                         scr_printf("\n%s\n", aaa);
1391                                 else
1392                                         entmsg(ipc, 0, 0);
1393                                 break;
1394                         case 5:                         /* <G>oto */
1395                                 updatels(ipc);
1396                                 gotonext(ipc);
1397                                 break;
1398                         case 47:                        /* <A>bandon */
1399                                 if (!rc_alt_semantics) {
1400                                         updatelsa(ipc);
1401                                 }
1402                                 gotonext(ipc);
1403                                 break;
1404                         case 90:                        /* <.A>bandon */
1405                                 if (!rc_alt_semantics)
1406                                         updatelsa(ipc);
1407                                 dotgoto(ipc, argbuf, 0, 0);
1408                                 break;
1409                         case 58:                        /* <M>ail */
1410                                 updatelsa(ipc);
1411                                 dotgoto(ipc, "_MAIL_", 1, 0);
1412                                 break;
1413                         case 20:
1414                                 if (strlen(argbuf) > 0) {
1415                                         updatels(ipc);
1416                                         dotgoto(ipc, argbuf, 0, 0);
1417                                 }
1418                                 break;
1419                         case 52:
1420                                 if (strlen(argbuf) > 0) {
1421                                         if (rc_alt_semantics) {
1422                                                 updatelsa(ipc);
1423                                         }
1424                                         dotgoto(ipc, argbuf, 0, 0);
1425                                 }
1426                                 break;
1427                         case 95: /* what exactly is the numbering scheme supposed to be anyway? */
1428                                 dotungoto(ipc, argbuf);
1429                                 break;
1430                         case 10:
1431                                 readmsgs(ipc, AllMessages, ReadForward, 0);
1432                                 break;
1433                         case 9:
1434                                 readmsgs(ipc, LastMessages, ReadForward, 5);
1435                                 break;
1436                         case 13:
1437                                 readmsgs(ipc, NewMessages, ReadForward, 0);
1438                                 break;
1439                         case 11:
1440                                 readmsgs(ipc, AllMessages, ReadReverse, 0);
1441                                 break;
1442                         case 12:
1443                                 readmsgs(ipc, OldMessages, ReadReverse, 0);
1444                                 break;
1445                         case 71:
1446                                 readmsgs(ipc, LastMessages, ReadForward,
1447                                                 atoi(argbuf));
1448                                 break;
1449                         case 7:
1450                                 forget(ipc);
1451                                 break;
1452                         case 18:
1453                                 subshell();
1454                                 break;
1455                         case 38:
1456                                 updatels(ipc);
1457                                 entroom(ipc);
1458                                 break;
1459                         case 22:
1460                                 killroom(ipc);
1461                                 break;
1462                         case 32:
1463                                 userlist(ipc, argbuf);
1464                                 break;
1465                         case 27:
1466                                 invite(ipc);
1467                                 break;
1468                         case 28:
1469                                 kickout(ipc);
1470                                 break;
1471                         case 23:
1472                                 editthisroom(ipc);
1473                                 break;
1474                         case 14:
1475                                 roomdir(ipc);
1476                                 break;
1477                         case 33:
1478                                 download(ipc, 0);
1479                                 break;
1480                         case 34:
1481                                 download(ipc, 1);
1482                                 break;
1483                         case 31:
1484                                 download(ipc, 2);
1485                                 break;
1486                         case 43:
1487                                 download(ipc, 3);
1488                                 break;
1489                         case 45:
1490                                 download(ipc, 4);
1491                                 break;
1492                         case 55:
1493                                 download(ipc, 5);
1494                                 break;
1495                         case 39:
1496                                 upload(ipc, 0);
1497                                 break;
1498                         case 40:
1499                                 upload(ipc, 1);
1500                                 break;
1501                         case 42:
1502                                 upload(ipc, 2);
1503                                 break;
1504                         case 44:
1505                                 upload(ipc, 3);
1506                                 break;
1507                         case 57:
1508                                 cli_upload(ipc);
1509                                 break;
1510                         case 16:
1511                                 ungoto(ipc);
1512                                 break;
1513                         case 24:
1514                                 whoknows(ipc);
1515                                 break;
1516                         case 26:
1517                                 validate(ipc);
1518                                 break;
1519                         case 29:
1520                         case 30:
1521                                 if (!rc_alt_semantics) {
1522                                         updatels(ipc);
1523                                 }
1524                                 termn8 = 1;
1525                                 break;
1526                         case 48:
1527                                 enterinfo(ipc);
1528                                 break;
1529                         case 49:
1530                                 readinfo(ipc);
1531                                 break;
1532                         case 72:
1533                                 cli_image_upload(ipc, "_userpic_");
1534                                 break;
1535                         case 73:
1536                                 cli_image_upload(ipc, "_roompic_");
1537                                 break;
1538
1539                         case 74:
1540                                 snprintf(aaa, sizeof aaa, "_floorpic_|%d", curr_floor);
1541                                 cli_image_upload(ipc, aaa);
1542                                 break;
1543
1544                         case 75:
1545                                 enternew(ipc, "roomname", aaa, 20);
1546                                 r = CtdlIPCChangeRoomname(ipc, aaa, bbb);
1547                                 if (r / 100 != 2)
1548                                         scr_printf("\n%s\n", aaa);
1549                                 break;
1550                         case 76:
1551                                 enternew(ipc, "hostname", aaa, 25);
1552                                 r = CtdlIPCChangeHostname(ipc, aaa, bbb);
1553                                 if (r / 100 != 2)
1554                                         scr_printf("\n%s\n", aaa);
1555                                 break;
1556                         case 77:
1557                                 enternew(ipc, "username", aaa, 32);
1558                                 r = CtdlIPCChangeUsername(ipc, aaa, bbb);
1559                                 if (r / 100 != 2)
1560                                         scr_printf("\n%s\n", aaa);
1561                                 break;
1562
1563                         case 35:
1564                                 set_password(ipc);
1565                                 break;
1566
1567                         case 21:
1568                                 if (argbuf[0] == 0)
1569                                         strcpy(aaa, "?");
1570                                 display_help(ipc, argbuf);
1571                                 break;
1572
1573                         case 41:
1574                                 formout(ipc, "register");
1575                                 entregis(ipc);
1576                                 break;
1577
1578                         case 15:
1579                                 scr_printf("Are you sure (y/n)? ");
1580                                 if (yesno() == 1) {
1581                                         if (!rc_alt_semantics)
1582                                                 updatels(ipc);
1583                                         a = 0;
1584                                         termn8 = 1;
1585                                 }
1586                                 break;
1587
1588                         case 85:
1589                                 scr_printf("All users will be disconnected!  "
1590                                            "Really terminate the server? ");
1591                                 if (yesno() == 1) {
1592                                         if (!rc_alt_semantics)
1593                                                 updatels(ipc);
1594                                         r = CtdlIPCTerminateServerNow(ipc, aaa);
1595                                         scr_printf("%s\n", aaa);
1596                                         if (r / 100 == 2) {
1597                                                 a = 0;
1598                                                 termn8 = 1;
1599                                         }
1600                                 }
1601                                 break;
1602
1603                         case 86:
1604                                 scr_printf("Do you really want to schedule a "
1605                                            "server shutdown? ");
1606                                 if (yesno() == 1) {
1607                                         r = CtdlIPCTerminateServerScheduled(ipc, 1, aaa);
1608                                         if (r / 100 == 2) {
1609                                                 if (atoi(aaa)) {
1610                                                         scr_printf(
1611                                                                    "The Citadel server will terminate when all users are logged off.\n"
1612                                                                    );
1613                                                 } else {
1614                                                         scr_printf(
1615                                                                    "The Citadel server will not terminate.\n"
1616                                                                    );
1617                                                 }
1618                                         }
1619                                 }
1620                                 break;
1621
1622                         case 87:
1623                                 network_config_management(ipc, "listrecp",
1624                                                           "Message-by-message mailing list recipients");
1625                                 break;
1626
1627                         case 94:
1628                                 network_config_management(ipc, "digestrecp",
1629                                                           "Digest mailing list recipients");
1630                                 break;
1631
1632                         case 89:
1633                                 network_config_management(ipc, "ignet_push_share",
1634                                                           "Nodes with which we share this room");
1635                                 break;
1636
1637                         case 88:
1638                                 do_ignet_configuration(ipc);
1639                                 break;
1640
1641                         case 92:
1642                                 do_filterlist_configuration(ipc);
1643                                 break;
1644
1645                         case 6:
1646                                 if (rc_alt_semantics) {
1647                                         updatelsa(ipc);
1648                                 }
1649                                 gotonext(ipc);
1650                                 break;
1651
1652                         case 3:
1653                                 chatmode(ipc);
1654                                 break;
1655
1656                         case 2:
1657                                 if (ipc->isLocal) {
1658                                         screen_reset();
1659                                         sttybbs(SB_RESTORE);
1660                                         snprintf(aaa, sizeof aaa, "USERNAME=\042%s\042; export USERNAME;"
1661                                                  "exec ./subsystem %ld %d %d", fullname,
1662                                                  usernum, screenwidth, axlevel);
1663                                         ka_system(aaa);
1664                                         sttybbs(SB_NO_INTR);
1665                                         screen_set();
1666                                 } else {
1667                                         scr_printf("*** Can't run doors when server is not local.\n");
1668                                 }
1669                                 break;
1670
1671                         case 17:
1672                                 who_is_online(ipc, 0);
1673                                 break;
1674
1675                         case 79:
1676                                 who_is_online(ipc, 1);
1677                                 break;
1678
1679                         case 91:
1680                                 who_is_online(ipc, 2);
1681                                 break;
1682                 
1683                         case 80:
1684                                 do_system_configuration(ipc);
1685                                 break;
1686
1687                         case 82:
1688                                 do_internet_configuration(ipc);
1689                                 break;
1690
1691                         case 83:
1692                                 check_message_base(ipc);
1693                                 break;
1694
1695                         case 84:
1696                                 quiet_mode(ipc);
1697                                 break;
1698
1699                         case 93:
1700                                 stealth_mode(ipc);
1701                                 break;
1702
1703                         case 50:
1704                                 enter_config(ipc, 2);
1705                                 break;
1706
1707                         case 37:
1708                                 enter_config(ipc, 0);
1709                                 set_floor_mode();
1710                                 break;
1711
1712                         case 59:
1713                                 enter_config(ipc, 3);
1714                                 set_floor_mode();
1715                                 break;
1716
1717                         case 60:
1718                                 gotofloor(ipc, argbuf, GF_GOTO);
1719                                 break;
1720
1721                         case 61:
1722                                 gotofloor(ipc, argbuf, GF_SKIP);
1723                                 break;
1724
1725                         case 62:
1726                                 forget_this_floor(ipc);
1727                                 break;
1728
1729                         case 63:
1730                                 create_floor(ipc);
1731                                 break;
1732
1733                         case 64:
1734                                 edit_floor(ipc);
1735                                 break;
1736
1737                         case 65:
1738                                 kill_floor(ipc);
1739                                 break;
1740
1741                         case 66:
1742                                 enter_bio(ipc);
1743                                 break;
1744
1745                         case 67:
1746                                 read_bio(ipc);
1747                                 break;
1748
1749                         case 25:
1750                                 edituser(ipc);
1751                                 break;
1752
1753                         case 8:
1754                                 knrooms(ipc, floor_mode);
1755                                 scr_printf("\n");
1756                                 break;
1757
1758                         case 68:
1759                                 knrooms(ipc, 2);
1760                                 scr_printf("\n");
1761                                 break;
1762
1763                         case 69:
1764                                 misc_server_cmd(ipc, argbuf);
1765                                 break;
1766
1767                         case 70:
1768                                 edit_system_message(ipc, argbuf);
1769                                 break;
1770
1771                         case 19:
1772                                 listzrooms(ipc);
1773                                 scr_printf("\n");
1774                                 break;
1775
1776                         case 51:
1777                                 deletefile(ipc);
1778                                 break;
1779
1780                         case 53:
1781                                 netsendfile(ipc);
1782                                 break;
1783
1784                         case 54:
1785                                 movefile(ipc);
1786                                 break;
1787
1788                         case 56:
1789                                 page_user(ipc);
1790                                 break;
1791
1792                         default: /* allow some math on the command */
1793                                 /* commands 100... to 100+MAX_EDITORS-1 will
1794                                    call the appropriate editor... in other
1795                                    words, command numbers 100+ will match
1796                                    the citadel.rc keys editor0, editor1, etc.*/
1797                                 if (mcmd >= 100 && mcmd < (100+MAX_EDITORS))
1798                                 {
1799                                         /* entmsg mode >=2 select editor */
1800                                         entmsg(ipc, 0, mcmd - 100 + 2);
1801                                         break;
1802                                 }
1803                         }       /* end switch */
1804         } while (termn8 == 0);
1805
1806 TERMN8: scr_printf("%s logged out.", fullname);
1807         termn8 = 0;
1808         color(ORIGINAL_PAIR);
1809         scr_printf("\n");
1810         while (march != NULL) {
1811                 remove_march(march->march_name, 0);
1812         }
1813         if (mcmd == 30) {
1814                 sln_printf("\n\nType 'off' to disconnect, or next user...\n");
1815         }
1816         CtdlIPCLogout(ipc);
1817         if ((mcmd == 29) || (mcmd == 15)) {
1818                 screen_delete();
1819                 sttybbs(SB_RESTORE);
1820                 formout(ipc, "goodbye");
1821                 logoff(ipc, 0);
1822         }
1823         goto GSTA;
1824
1825 }       /* end main() */