]> code.citadel.org Git - citadel.git/blob - citadel/citadel.c
8932d9bbedf84f9d2b7658ee09245305b2a7af23
[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 *roomrec = 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, "", &roomrec, aaa);
369         if (r / 10 == 54) {
370                 newprompt("Enter room password: ", bbb, 9);
371                 r = CtdlIPCGotoRoom(ipc, towhere, bbb, &roomrec, 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                 roomrec = NULL;
419                 r = CtdlIPCGotoRoom(ipc, bbb, "", &roomrec, aaa);
420         }
421         if (r / 100 != 1 && r / 100 != 2) {
422                 scr_printf("%s\n", aaa);
423                 return;
424         }
425         safestrncpy(room_name, roomrec->RRname, ROOMNAMELEN);
426         room_flags = roomrec->RRflags;
427         from_floor = curr_floor;
428         curr_floor = roomrec->RRfloor;
429
430         remove_march(room_name, 0);
431         if (!strcasecmp(towhere, "_BASEROOM_"))
432                 remove_march(towhere, 0);
433         if (!roomrec->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 ", roomrec->RRunread);
449                 color(DIM_WHITE);
450                 scr_printf("new of ");
451                 color(BRIGHT_YELLOW);
452                 scr_printf("%d ", roomrec->RRtotal);
453                 color(DIM_WHITE);
454                 scr_printf("messages.\n");
455         }
456         highest_msg_read = roomrec->RRlastread;
457         maxmsgnum = roomrec->RRhighest;
458         is_mail = roomrec->RRismailbox;
459         is_room_aide = roomrec->RRaide;
460         ls = roomrec->RRlastread;
461
462         /* read info file if necessary */
463         if (roomrec->RRinfoupdated > 0)
464                 readinfo(ipc);
465
466         /* check for newly arrived mail if we can */
467         newmailcount = roomrec->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 *roomrec;    /* Ignored */
542         int r;                          /* IPC response code */
543
544         scr_printf("Forgetting all rooms on %s...\r", &floorlist[ffloor][0]);
545         scr_flush();
546         r = CtdlIPCKnownRooms(ipc, AllAccessibleRooms, ffloor, &flist, buf);
547         if (r / 100 != 1) {
548                 scr_printf("%-72s\n", buf);
549                 return;
550         }
551         while (flist) {
552                 r = CtdlIPCGotoRoom(ipc, flist->march_name, "", &roomrec, buf);
553                 if (r / 100 == 2) {
554                         r = CtdlIPCForgetRoom(ipc, buf);
555                 }
556                 fptr = flist;
557                 flist = flist->next;
558                 free(fptr);
559         }
560         scr_printf("%-72s\r", "");
561 }
562
563
564 /*
565  * routine called by gotofloor() to move to a new room on a new floor
566  */
567 void gf_toroom(CtdlIPC *ipc, char *towhere, int mode)
568 {
569         int floor_being_left;
570
571         floor_being_left = curr_floor;
572
573         if (mode == GF_GOTO) {  /* <;G>oto mode */
574                 updatels(ipc);
575                 dotgoto(ipc, towhere, 1, 0);
576         }
577         if (mode == GF_SKIP) {  /* <;S>kip mode */
578                 dotgoto(ipc, towhere, 1, 0);
579                 remove_march("_FLOOR_", floor_being_left);
580         }
581         if (mode == GF_ZAP) {   /* <;Z>ap mode */
582                 dotgoto(ipc, towhere, 1, 0);
583                 remove_march("_FLOOR_", floor_being_left);
584                 forget_all_rooms_on(ipc, floor_being_left);
585         }
586 }
587
588
589 /*
590  * go to a new floor
591  */
592 void gotofloor(CtdlIPC *ipc, char *towhere, int mode)
593 {
594         int a, tofloor;
595         int r;          /* IPC response code */
596         struct march *mptr;
597         char buf[SIZ], targ[SIZ];
598
599         if (floorlist[0][0] == 0)
600                 load_floorlist(ipc);
601         tofloor = (-1);
602         for (a = 0; a < 128; ++a)
603                 if (!strcasecmp(&floorlist[a][0], towhere))
604                         tofloor = a;
605
606         if (tofloor < 0) {
607                 for (a = 0; a < 128; ++a) {
608                         if (!strncasecmp(&floorlist[a][0], towhere, strlen(towhere))) {
609                                 tofloor = a;
610                         }
611                 }
612         }
613         if (tofloor < 0) {
614                 for (a = 0; a < 128; ++a)
615                         if (pattern(towhere, &floorlist[a][0]) > 0)
616                                 tofloor = a;
617         }
618         if (tofloor < 0) {
619                 scr_printf("No floor '%s'.\n", towhere);
620                 return;
621         }
622         for (mptr = march; mptr != NULL; mptr = mptr->next) {
623                 if ((mptr->march_floor) == tofloor)
624                         gf_toroom(ipc, mptr->march_name, mode);
625                 return;
626         }
627
628         strcpy(targ, "");
629         mptr = NULL;
630         r = CtdlIPCKnownRooms(ipc, AllAccessibleRooms, tofloor, &mptr, buf);
631         if (r / 100 == 1) {
632                 struct march *tmp = mptr;
633
634                 /* TODO: room order is being ignored? */
635                 if (mptr)
636                         strncpy(targ, mptr->march_name, ROOMNAMELEN);
637                 while (mptr) {
638                         tmp = mptr->next;
639                         free(mptr);
640                         mptr = tmp;
641                 }
642         }
643         if (strlen(targ) > 0) {
644                 gf_toroom(ipc, targ, mode);
645         } else {
646                 scr_printf("There are no rooms on '%s'.\n", &floorlist[tofloor][0]);
647         }
648 }
649
650
651 /*
652  * forget all rooms on current floor
653  */
654 void forget_this_floor(CtdlIPC *ipc)
655 {
656
657         if (curr_floor == 0) {
658                 scr_printf("Can't forget this floor.\n");
659                 return;
660         }
661         if (floorlist[0][0] == 0)
662                 load_floorlist(ipc);
663         scr_printf("Are you sure you want to forget all rooms on %s? ",
664                &floorlist[(int) curr_floor][0]);
665         if (yesno() == 0)
666                 return;
667
668         gf_toroom(ipc, "_BASEROOM_", GF_ZAP);
669 }
670
671
672 /* 
673  * Figure out the physical screen dimensions, if we can
674  * WARNING:  this is now called from a signal handler!
675  */
676 void check_screen_dims(void)
677 {
678 #ifdef TIOCGWINSZ
679         struct {
680                 unsigned short height;  /* rows */
681                 unsigned short width;   /* columns */
682                 unsigned short xpixels;
683                 unsigned short ypixels;         /* pixels */
684         } xwinsz;
685
686         if (have_xterm) {       /* dynamically size screen if on an xterm */
687                 if (ioctl(0, TIOCGWINSZ, &xwinsz) == 0) {
688                         if (xwinsz.height)
689                                 screenheight = is_curses_enabled() ? (int)xwinsz.height - 1 : (int) xwinsz.height;
690                         if (xwinsz.width)
691                                 screenwidth = (int) xwinsz.width;
692                 }
693         }
694 #endif
695 }
696
697
698 /*
699  * set floor mode depending on client, server, and user settings
700  */
701 void set_floor_mode(void)
702 {
703         if (serv_info.serv_ok_floors == 0) {
704                 floor_mode = 0; /* Don't use floors if the server */
705         }
706         /* doesn't support them!          */
707         else {
708                 if (rc_floor_mode == RC_NO) {   /* never use floors */
709                         floor_mode = 0;
710                 }
711                 if (rc_floor_mode == RC_YES) {  /* always use floors */
712                         floor_mode = 1;
713                 }
714                 if (rc_floor_mode == RC_DEFAULT) {      /* user choice */
715                         floor_mode = ((userflags & US_FLOORS) ? 1 : 0);
716                 }
717         }
718 }
719
720 /*
721  * Set or change the user's password
722  */
723 int set_password(CtdlIPC *ipc)
724 {
725         char pass1[20];
726         char pass2[20];
727         char buf[SIZ];
728
729         if (strlen(rc_password) > 0) {
730                 strcpy(pass1, rc_password);
731                 strcpy(pass2, rc_password);
732         } else {
733                 IFNEXPERT formout(ipc, "changepw");
734                 newprompt("Enter a new password: ", pass1, -19);
735                 newprompt("Enter it again to confirm: ", pass2, -19);
736         }
737         strproc(pass1);
738         strproc(pass2);
739         if (!strcasecmp(pass1, pass2)) {
740                 CtdlIPCChangePassword(ipc, pass1, buf);
741                 scr_printf("%s\n", buf);
742                 offer_to_remember_password(ipc, hostbuf, portbuf, fullname, pass1);
743                 return (0);
744         } else {
745                 scr_printf("*** They don't match... try again.\n");
746                 return (1);
747         }
748 }
749
750
751
752 /*
753  * get info about the server we've connected to
754  */
755 void get_serv_info(CtdlIPC *ipc, char *supplied_hostname)
756 {
757         char buf[SIZ];
758
759         CtdlIPCServerInfo(ipc, &serv_info, buf);
760
761         /* be nice and identify ourself to the server */
762         CtdlIPCIdentifySoftware(ipc, SERVER_TYPE, 0, REV_LEVEL,
763                  (ipc->isLocal ? "local" : CITADEL),
764                  (supplied_hostname) ? supplied_hostname : 
765                  /* Look up the , in the bible if you're confused */
766                  (locate_host(buf), buf), buf);
767
768         /* Tell the server what our preferred content formats are */
769         if ((CtdlIPCSpecifyPreferredFormats(ipc, buf, "text/html|text/plain") / 100 )== 2) {
770                 can_do_msg4 = 1;
771         }
772 }
773
774
775
776 /*
777  * Record compare function for SortOnlineUsers()
778  */
779 int idlecmp(const void *rec1, const void *rec2) {
780         time_t i1, i2;
781
782         i1 = extract_long(rec1, 5);
783         i2 = extract_long(rec2, 5);
784
785         if (i1 < i2) return(1);
786         if (i1 > i2) return(-1);
787         return(0);
788 }
789
790
791 /*
792  * Sort the list of online users by idle time.
793  * This function frees the supplied buffer, and returns a new one
794  * to the caller.  The caller is responsible for freeing the returned buffer.
795  */
796 char *SortOnlineUsers(char *listing) {
797         int rows;
798         char *sortbuf;
799         char *retbuf;
800         char buf[SIZ];
801         int i;
802
803         rows = num_tokens(listing, '\n');
804         sortbuf = malloc(rows * SIZ);
805         if (sortbuf == NULL) return(listing);
806         retbuf = malloc(rows * SIZ);
807         if (retbuf == NULL) {
808                 free(sortbuf);
809                 return(listing);
810         }
811
812         /* Copy the list into a fixed-record-size array for sorting */
813         for (i=0; i<rows; ++i) {
814                 memset(buf, 0, SIZ);
815                 extract_token(buf, listing, i, '\n');
816                 memcpy(&sortbuf[i*SIZ], buf, SIZ);
817         }
818
819         /* Do the sort */
820         qsort(sortbuf, rows, SIZ, idlecmp);
821
822         /* Copy back to a \n delimited list */
823         strcpy(retbuf, "");
824         for (i=0; i<rows; ++i) {
825                 strcat(retbuf, &sortbuf[i*SIZ]);
826                 if (i<(rows-1)) strcat(retbuf, "\n");
827         }
828         return(retbuf);
829 }
830
831
832
833 /*
834  * Display list of users currently logged on to the server
835  */
836 void who_is_online(CtdlIPC *ipc, int longlist)
837 {
838         char buf[SIZ], username[SIZ], roomname[SIZ], fromhost[SIZ];
839         char flags[SIZ];
840         char actual_user[SIZ], actual_room[SIZ], actual_host[SIZ];
841         char clientsoft[SIZ];
842         time_t timenow = 0;
843         time_t idletime, idlehours, idlemins, idlesecs;
844         int last_session = (-1);
845         int skipidle = 0;
846         char *listing = NULL;
847         int r;                          /* IPC response code */
848     
849         if (longlist == 2) {
850                 longlist = 0;
851                 skipidle = 1;
852         }
853
854         if (!longlist) {
855                 color(BRIGHT_WHITE);
856                 pprintf("           User Name               Room           Idle        From host\n");
857                 color(DIM_WHITE);
858                 pprintf("   ------------------------- -------------------- ---- ------------------------\n");
859         }
860         r = CtdlIPCOnlineUsers(ipc, &listing, &timenow, buf);
861         listing = SortOnlineUsers(listing);
862         if (r / 100 == 1) {
863                 while (strlen(listing) > 0) {
864                         int isidle = 0;
865                         
866                         /* Get another line */
867                         extract_token(buf, listing, 0, '\n');
868                         remove_token(listing, 0, '\n');
869
870                         extract(username, buf, 1);
871                         extract(roomname, buf, 2);
872                         extract(fromhost, buf, 3);
873                         extract(clientsoft, buf, 4);
874                         extract(flags, buf, 7);
875
876                         idletime = timenow - extract_long(buf, 5);
877                         idlehours = idletime / 3600;
878                         idlemins = (idletime - (idlehours * 3600)) / 60;
879                         idlesecs = (idletime - (idlehours * 3600) - (idlemins * 60));
880
881                         if (idletime > rc_idle_threshold) {
882                                 /*
883                                 while (strlen(roomname) < 20) {
884                                         strcat(roomname, " ");
885                                 }
886                                 strcpy(&roomname[14], "[idle]");
887                                 */
888                                 if (skipidle)
889                                         isidle = 1;
890                         }
891
892                         if (longlist) {
893                                 extract(actual_user, buf, 8);
894                                 extract(actual_room, buf, 9);
895                                 extract(actual_host, buf, 10);
896
897                                 pprintf("  Flags: %s\n", flags);
898                                 pprintf("Session: %d\n", extract_int(buf, 0));
899                                 pprintf("   Name: %s\n", username);
900                                 pprintf("In room: %s\n", roomname);
901                                 pprintf("   Host: %s\n", fromhost);
902                                 pprintf(" Client: %s\n", clientsoft);
903                                 pprintf("   Idle: %ld:%02ld:%02ld\n",
904                                         (long) idlehours,
905                                         (long) idlemins,
906                                         (long) idlesecs);
907
908                                 if ( (strlen(actual_user)+strlen(actual_room)+strlen(actual_host)) > 0) {
909                                         pprintf("(really ");
910                                         if (strlen(actual_user)>0) pprintf("<%s> ", actual_user);
911                                         if (strlen(actual_room)>0) pprintf("in <%s> ", actual_room);
912                                         if (strlen(actual_host)>0) pprintf("from <%s> ", actual_host);
913                                         pprintf(")\n");
914                                 }
915                                 pprintf("\n");
916
917                         } else {
918                     if (isidle == 0) {
919                                 if (extract_int(buf, 0) == last_session) {
920                                         pprintf("        ");
921                                 } else {
922                                         color(BRIGHT_MAGENTA);
923                                         pprintf("%-3s", flags);
924                                 }
925                                 last_session = extract_int(buf, 0);
926                                 color(BRIGHT_CYAN);
927                                 pprintf("%-25s ", username);
928                                 color(BRIGHT_MAGENTA);
929                                 roomname[20] = 0;
930                                 pprintf("%-20s ", roomname);
931                                 if (idletime > rc_idle_threshold) {
932                                         /* over 10 days, must be gone fishing */
933                                         if (idlehours > 239) {
934                                                 pprintf("fish");
935                                         /* over 10 hours */
936                                         } else if (idlehours > 9) {
937                                                 pprintf("%1ldd%02ld",
938                                                         idlehours / 24,
939                                                         idlehours % 24);
940                                         /* less than 10 hours */
941                                         } else {
942                                                 pprintf("%1ld:%02ld",
943                                                         idlehours, idlemins);
944                                         }
945                                 }
946                                 else {
947                                         pprintf("    ");
948                                 }
949                                 pprintf(" ");
950                                 color(BRIGHT_CYAN);
951                                 fromhost[24] = '\0';
952                                 pprintf("%-24s\n", fromhost);
953                                 color(DIM_WHITE);
954                         }
955                         }
956                 }
957         }
958         free(listing);
959 }
960
961 void enternew(CtdlIPC *ipc, char *desc, char *buf, int maxlen)
962 {
963         char bbb[128];
964         snprintf(bbb, sizeof bbb, "Enter in your new %s: ", desc);
965         newprompt(bbb, buf, maxlen);
966 }
967
968
969
970 int shift(int argc, char **argv, int start, int count) {
971         int i;
972
973         for (i=start; i<(argc-count); ++i) {
974                 argv[i] = argv[i+count];
975         }
976         argc = argc - count;
977         return argc;
978 }
979
980 static void statusHook(char *s) {
981         sln_printf(s);
982         sln_flush();
983 }
984
985 /*
986  * main
987  */
988 int main(int argc, char **argv)
989 {
990         int a, b, mcmd;
991         char aaa[100], bbb[100];/* general purpose variables */
992         char argbuf[32];        /* command line buf */
993         char nonce[NONCE_SIZE];
994         char *telnet_client_host = NULL;
995         char *sptr, *sptr2;     /* USed to extract the nonce */
996         char hexstring[MD5_HEXSTRING_SIZE];
997         int stored_password = 0;
998         char password[SIZ];
999         struct ctdlipcmisc chek;
1000         struct usersupp *myself = NULL;
1001         CtdlIPC* ipc;                   /* Our server connection */
1002         int r;                          /* IPC result code */
1003
1004         setIPCDeathHook(screen_delete);
1005         setIPCErrorPrintf(err_printf);
1006         setCryptoStatusHook(statusHook);
1007         
1008         /* Permissions sanity check - don't run citadel setuid/setgid */
1009         if (getuid() != geteuid()) {
1010                 err_printf("Please do not run citadel setuid!\n");
1011                 logoff(NULL, 3);
1012         } else if (getgid() != getegid()) {
1013                 err_printf("Please do not run citadel setgid!\n");
1014                 logoff(NULL, 3);
1015         }
1016
1017         sttybbs(SB_SAVE);       /* Store the old terminal parameters */
1018         load_command_set();     /* parse the citadel.rc file */
1019         sttybbs(SB_NO_INTR);    /* Install the new ones */
1020         signal(SIGHUP, dropcarr);       /* Cleanup gracefully if carrier is dropped */
1021         signal(SIGTERM, dropcarr);      /* Cleanup gracefully if terminated */
1022         signal(SIGCONT, catch_sigcont); /* Catch SIGCONT so we can reset terminal */
1023 #ifdef SIGWINCH
1024         signal(SIGWINCH, scr_winch);    /* Window resize signal */
1025 #endif
1026
1027 #ifdef HAVE_OPENSSL
1028         arg_encrypt = RC_DEFAULT;
1029 #endif
1030 #if defined(HAVE_CURSES_H) && !defined(DISABLE_CURSES)
1031         arg_screen = RC_DEFAULT;
1032 #endif
1033
1034         /* 
1035          * Handle command line options as if we were called like /bin/login
1036          * (i.e. from in.telnetd)
1037          */
1038         for (a=0; a<argc; ++a) {
1039                 if ((argc > a+1) && (!strcmp(argv[a], "-h")) ) {
1040                         telnet_client_host = argv[a+1];
1041                         argc = shift(argc, argv, a, 2);
1042                 }
1043                 if (!strcmp(argv[a], "-x")) {
1044 #ifdef HAVE_OPENSSL
1045                         arg_encrypt = RC_NO;
1046 #endif
1047                         argc = shift(argc, argv, a, 1);
1048                 }
1049                 if (!strcmp(argv[a], "-X")) {
1050 #ifdef HAVE_OPENSSL
1051                         arg_encrypt = RC_YES;
1052                         argc = shift(argc, argv, a, 1);
1053 #else
1054                         fprintf(stderr, "Not compiled with encryption support");
1055                         return 1;
1056 #endif
1057                 }
1058                 if (!strcmp(argv[a], "-s")) {
1059 #if defined(HAVE_CURSES_H) && !defined(DISABLE_CURSES)
1060                         arg_screen = RC_NO;
1061 #endif
1062                         argc = shift(argc, argv, a, 1);
1063                 }
1064                 if (!strcmp(argv[a], "-S")) {
1065 #if defined(HAVE_CURSES_H) && !defined(DISABLE_CURSES)
1066                         arg_screen = RC_YES;
1067 #endif
1068                         argc = shift(argc, argv, a, 1);
1069                 }
1070                 if (!strcmp(argv[a], "-p")) {
1071                         struct stat st;
1072                 
1073                         if (chdir(BBSDIR) < 0) {
1074                                 perror("can't change to " BBSDIR);
1075                                 logoff(NULL, 3);
1076                         }
1077
1078                         /*
1079                          * Drop privileges if necessary. We stat
1080                          * citadel.config to get the uid/gid since it's
1081                          * guaranteed to have the uid/gid we want.
1082                          */
1083                         if (!getuid() || !getgid()) {
1084                                 if (stat(BBSDIR "/citadel.config", &st) < 0) {
1085                                         perror("couldn't stat citadel.config");
1086                                         logoff(NULL, 3);
1087                                 }
1088                                 if (!getgid() && (setgid(st.st_gid) < 0)) {
1089                                         perror("couldn't change gid");
1090                                         logoff(NULL, 3);
1091                                 }
1092                                 if (!getuid() && (setuid(st.st_uid) < 0)) {
1093                                         perror("couldn't change uid");
1094                                         logoff(NULL, 3);
1095                                 }
1096                                 /*
1097                                   scr_printf("Privileges changed to uid %d gid %d\n",
1098                                   getuid(), getgid());
1099                                 */
1100                         }
1101                         argc = shift(argc, argv, a, 1);
1102                 }
1103         }
1104
1105         screen_new();
1106
1107 #ifdef __CYGWIN__
1108         newprompt("Connect to (return for local server): ", hostbuf, 64);
1109 #endif
1110
1111         sln_printf("Attaching to server... \r");
1112         sln_flush();
1113         ipc = CtdlIPC_new(argc, argv, hostbuf, portbuf);
1114         if (!ipc) {
1115                 screen_delete();
1116                 error_printf("Can't connect: %s\n", strerror(errno));
1117                 logoff(NULL, 3);
1118         }
1119 #if defined(HAVE_CURSES_H) && !defined(DISABLE_CURSES)
1120         CtdlIPC_SetNetworkStatusCallback(ipc, wait_indicator);
1121 #endif
1122         ipc_for_signal_handlers = ipc;  /* KLUDGE cover your eyes */
1123
1124         CtdlIPC_getline(ipc, aaa);
1125         if (aaa[0] != '2') {
1126                 scr_printf("%s\n", &aaa[4]);
1127                 logoff(ipc, atoi(aaa));
1128         }
1129
1130         /* If there is a [nonce] at the end, put the nonce in <nonce>, else nonce
1131          * is zeroized.
1132          */
1133         
1134         if ((sptr = strchr(aaa, '<')) == NULL)
1135                 {
1136                         nonce[0] = '\0';
1137                 }
1138         else
1139                 {
1140                         if ((sptr2 = strchr(sptr, '>')) == NULL)
1141                                 {
1142                                         nonce[0] = '\0';
1143                                 }
1144                         else
1145                                 {
1146                                         sptr2++;
1147                                         *sptr2 = '\0';
1148                                         strncpy(nonce, sptr, NONCE_SIZE);
1149                                 }
1150                 }
1151
1152 #ifdef HAVE_OPENSSL
1153         /* Evaluate encryption preferences */
1154         if (arg_encrypt != RC_NO && rc_encrypt != RC_NO) {
1155                 if (!ipc->isLocal || arg_encrypt == RC_YES || rc_encrypt == RC_YES) {
1156                         secure = (CtdlIPCStartEncryption(ipc, aaa) / 100 == 2) ? 1 : 0;
1157                         if (!secure)
1158                                 error_printf("Can't encrypt: %s\n", aaa);
1159                 }
1160         }
1161 #endif
1162
1163         get_serv_info(ipc, telnet_client_host);
1164         scr_printf("%-24s\n%s\n%s\n", serv_info.serv_software, serv_info.serv_humannode,
1165                    serv_info.serv_bbs_city);
1166         scr_flush();
1167
1168         status_line(serv_info.serv_humannode, serv_info.serv_bbs_city, NULL,
1169                     secure, -1);
1170
1171         screenwidth = 80;       /* default screen dimensions */
1172         screenheight = 24;
1173         
1174         scr_printf(" pause    next    stop\n");
1175         scr_printf(" ctrl-s  ctrl-o  ctrl-c\n\n");
1176         formout(ipc, "hello");  /* print the opening greeting */
1177         scr_printf("\n");
1178
1179  GSTA:  /* See if we have a username and password on disk */
1180         if (rc_remember_passwords) {
1181                 get_stored_password(hostbuf, portbuf, fullname, password);
1182                 if (strlen(fullname) > 0) {
1183                         r = CtdlIPCTryLogin(ipc, fullname, aaa);
1184                         if (r / 100 == 3) {
1185                                 if (*nonce) {
1186                                         r = CtdlIPCTryApopPassword(ipc, make_apop_string(password, nonce, hexstring, sizeof hexstring), aaa);
1187                                 } else {
1188                                         r = CtdlIPCTryPassword(ipc, password, aaa);
1189                                 }
1190                         }
1191
1192                         if (r / 100 == 2) {
1193                                 load_user_info(aaa);
1194                                 stored_password = 1;
1195                                 goto PWOK;
1196                         } else {
1197                                 set_stored_password(hostbuf, portbuf, "", "");
1198                         }
1199                 }
1200         }
1201
1202         termn8 = 0;
1203         newnow = 0;
1204         do {
1205                 if (strlen(rc_username) > 0) {
1206                         strcpy(fullname, rc_username);
1207                 } else {
1208                         newprompt("Enter your name: ", fullname, 29);
1209                 }
1210                 strproc(fullname);
1211                 if (!strcasecmp(fullname, "new")) {     /* just in case */
1212                         scr_printf("Please enter the name you wish to log in with.\n");
1213                 }
1214         } while (
1215                  (!strcasecmp(fullname, "bbs"))
1216                  || (!strcasecmp(fullname, "new"))
1217                  || (strlen(fullname) == 0));
1218
1219         if (!strcasecmp(fullname, "off")) {
1220                 mcmd = 29;
1221                 goto TERMN8;
1222         }
1223         /* sign on to the server */
1224         r = CtdlIPCTryLogin(ipc, fullname, aaa);
1225         if (r / 100 != 3)
1226                 goto NEWUSR;
1227
1228         /* password authentication */
1229         if (strlen(rc_password) > 0) {
1230                 strcpy(password, rc_password);
1231         } else {
1232                 newprompt("\rPlease enter your password: ", password, -19);
1233         }
1234         strproc(password);
1235
1236         if (*nonce) {
1237                 r = CtdlIPCTryApopPassword(ipc, make_apop_string(password, nonce, hexstring, sizeof hexstring), aaa);
1238         } else {
1239                 r = CtdlIPCTryPassword(ipc, password, aaa);
1240         }
1241         
1242         if (r / 100 == 2) {
1243                 load_user_info(aaa);
1244                 offer_to_remember_password(ipc, hostbuf, portbuf,
1245                                            fullname, password);
1246                 goto PWOK;
1247         }
1248         scr_printf("<< wrong password >>\n");
1249         if (strlen(rc_password) > 0)
1250                 logoff(ipc, 2);
1251         goto GSTA;
1252
1253 NEWUSR: if (strlen(rc_password) == 0) {
1254                 scr_printf("No record. Enter as new user? ");
1255                 if (yesno() == 0)
1256                         goto GSTA;
1257         }
1258
1259         r = CtdlIPCCreateUser(ipc, fullname, 1, aaa);
1260         if (r / 100 != 2) {
1261                 scr_printf("%s\n", aaa);
1262                 goto GSTA;
1263         }
1264         load_user_info(aaa);
1265
1266         while (set_password(ipc) != 0);
1267         newnow = 1;
1268
1269         enter_config(ipc, 1);
1270
1271  PWOK:
1272         /* Switch color support on or off if we're in user mode */
1273         if (rc_ansi_color == 3) {
1274                 if (userflags & US_COLOR)
1275                         enable_color = 1;
1276                 else
1277                         enable_color = 0;
1278         }
1279
1280         color(BRIGHT_WHITE);
1281         scr_printf("\n%s\nAccess level: %d (%s)\n"
1282                    "User #%ld / Login #%d",
1283                    fullname, axlevel, axdefs[(int) axlevel],
1284                    usernum, timescalled);
1285         if (lastcall > 0L) {
1286                 scr_printf(" / Last login: %s",
1287                            asctime(localtime(&lastcall)));
1288         }
1289         scr_printf("\n");
1290
1291         r = CtdlIPCMiscCheck(ipc, &chek, aaa);
1292         if (r / 100 == 2) {
1293                 b = chek.newmail;
1294                 if (b > 0) {
1295                         color(BRIGHT_RED);
1296                         if (b == 1)
1297                                 scr_printf("*** You have a new private message in Mail>\n");
1298                         if (b > 1)
1299                                 scr_printf("*** You have %d new private messages in Mail>\n", b);
1300                         color(DIM_WHITE);
1301                         if (strlen(rc_gotmail_cmd) > 0) {
1302                                 system(rc_gotmail_cmd);
1303                         }
1304                 }
1305                 if ((axlevel >= 6) && (chek.needvalid > 0)) {
1306                         scr_printf("*** Users need validation\n");
1307                 }
1308                 if (chek.needregis > 0) {
1309                         scr_printf("*** Please register.\n");
1310                         formout(ipc, "register");
1311                         entregis(ipc);
1312                 }
1313         }
1314         /* Make up some temporary filenames for use in various parts of the
1315          * program.  Don't mess with these once they've been set, because we
1316          * will be unlinking them later on in the program and we don't
1317          * want to delete something that we didn't create. */
1318         snprintf(temp, sizeof temp, tmpnam(NULL));
1319         snprintf(temp2, sizeof temp2, tmpnam(NULL));
1320         snprintf(tempdir, sizeof tempdir, tmpnam(NULL));
1321
1322         /* Get screen dimensions.  First we go to a default of 80x24.  Then
1323          * we try to get the user's actual screen dimensions off the server.
1324          * However, if we're running on an xterm, all this stuff is
1325          * irrelevant because we're going to dynamically size the screen
1326          * during the session.
1327          */
1328         screenwidth = 80;
1329         screenheight = 24;
1330         r = CtdlIPCGetConfig(ipc, &myself, aaa);
1331         if (r == 2) {
1332                 screenwidth = myself->USscreenwidth;
1333                 screenheight = myself->USscreenheight;
1334         }
1335         if (getenv("TERM") != NULL)
1336                 if (!strcmp(getenv("TERM"), "xterm")) {
1337                         have_xterm = 1;
1338                 }
1339 #ifdef TIOCGWINSZ
1340         check_screen_dims();
1341 #endif
1342
1343         set_floor_mode();
1344
1345         /* Enter the lobby */
1346         dotgoto(ipc, "_BASEROOM_", 1, 0);
1347
1348         /* Main loop for the system... user is logged in. */
1349         uglistsize = 0;
1350
1351         if (newnow == 1)
1352                 readmsgs(ipc, LastMessages, ReadForward, 5);
1353         else
1354                 readmsgs(ipc, NewMessages, ReadForward, 0);
1355
1356         /* MAIN COMMAND LOOP */
1357         do {
1358                 mcmd = getcmd(ipc, argbuf);     /* Get keyboard command */
1359
1360 #ifdef TIOCGWINSZ
1361                 check_screen_dims();            /* if xterm, get screen size */
1362 #endif
1363
1364                 if (termn8 == 0)
1365                         switch (mcmd) {
1366                         case 1:
1367                                 formout(ipc, "help");
1368                                 break;
1369                         case 4:
1370                                 entmsg(ipc, 0, 0);
1371                                 break;
1372                         case 36:
1373                                 entmsg(ipc, 0, 1);
1374                                 break;
1375                         case 46:
1376                                 entmsg(ipc, 0, 2);
1377                                 break;
1378                         case 78:
1379                                 newprompt("What do you want your username to be? ", aaa, 32);
1380                                 snprintf(bbb, sizeof bbb, "ENT0 2|0|0|0|%s", aaa);
1381                                 CtdlIPC_putline(ipc, bbb);
1382                                 CtdlIPC_getline(ipc, aaa);
1383                                 if (strncmp("200", aaa, 3))
1384                                         scr_printf("\n%s\n", aaa);
1385                                 else
1386                                         entmsg(ipc, 0, 0);
1387                                 break;
1388                         case 5:                         /* <G>oto */
1389                                 updatels(ipc);
1390                                 gotonext(ipc);
1391                                 break;
1392                         case 47:                        /* <A>bandon */
1393                                 if (!rc_alt_semantics) {
1394                                         updatelsa(ipc);
1395                                 }
1396                                 gotonext(ipc);
1397                                 break;
1398                         case 90:                        /* <.A>bandon */
1399                                 if (!rc_alt_semantics)
1400                                         updatelsa(ipc);
1401                                 dotgoto(ipc, argbuf, 0, 0);
1402                                 break;
1403                         case 58:                        /* <M>ail */
1404                                 updatelsa(ipc);
1405                                 dotgoto(ipc, "_MAIL_", 1, 0);
1406                                 break;
1407                         case 20:
1408                                 if (strlen(argbuf) > 0) {
1409                                         updatels(ipc);
1410                                         dotgoto(ipc, argbuf, 0, 0);
1411                                 }
1412                                 break;
1413                         case 52:
1414                                 if (strlen(argbuf) > 0) {
1415                                         if (rc_alt_semantics) {
1416                                                 updatelsa(ipc);
1417                                         }
1418                                         dotgoto(ipc, argbuf, 0, 0);
1419                                 }
1420                                 break;
1421                         case 95: /* what exactly is the numbering scheme supposed to be anyway? */
1422                                 dotungoto(ipc, argbuf);
1423                                 break;
1424                         case 10:
1425                                 readmsgs(ipc, AllMessages, ReadForward, 0);
1426                                 break;
1427                         case 9:
1428                                 readmsgs(ipc, LastMessages, ReadForward, 5);
1429                                 break;
1430                         case 13:
1431                                 readmsgs(ipc, NewMessages, ReadForward, 0);
1432                                 break;
1433                         case 11:
1434                                 readmsgs(ipc, AllMessages, ReadReverse, 0);
1435                                 break;
1436                         case 12:
1437                                 readmsgs(ipc, OldMessages, ReadReverse, 0);
1438                                 break;
1439                         case 71:
1440                                 readmsgs(ipc, LastMessages, ReadForward,
1441                                                 atoi(argbuf));
1442                                 break;
1443                         case 7:
1444                                 forget(ipc);
1445                                 break;
1446                         case 18:
1447                                 subshell();
1448                                 break;
1449                         case 38:
1450                                 updatels(ipc);
1451                                 entroom(ipc);
1452                                 break;
1453                         case 22:
1454                                 killroom(ipc);
1455                                 break;
1456                         case 32:
1457                                 userlist(ipc, argbuf);
1458                                 break;
1459                         case 27:
1460                                 invite(ipc);
1461                                 break;
1462                         case 28:
1463                                 kickout(ipc);
1464                                 break;
1465                         case 23:
1466                                 editthisroom(ipc);
1467                                 break;
1468                         case 14:
1469                                 roomdir(ipc);
1470                                 break;
1471                         case 33:
1472                                 download(ipc, 0);
1473                                 break;
1474                         case 34:
1475                                 download(ipc, 1);
1476                                 break;
1477                         case 31:
1478                                 download(ipc, 2);
1479                                 break;
1480                         case 43:
1481                                 download(ipc, 3);
1482                                 break;
1483                         case 45:
1484                                 download(ipc, 4);
1485                                 break;
1486                         case 55:
1487                                 download(ipc, 5);
1488                                 break;
1489                         case 39:
1490                                 upload(ipc, 0);
1491                                 break;
1492                         case 40:
1493                                 upload(ipc, 1);
1494                                 break;
1495                         case 42:
1496                                 upload(ipc, 2);
1497                                 break;
1498                         case 44:
1499                                 upload(ipc, 3);
1500                                 break;
1501                         case 57:
1502                                 cli_upload(ipc);
1503                                 break;
1504                         case 16:
1505                                 ungoto(ipc);
1506                                 break;
1507                         case 24:
1508                                 whoknows(ipc);
1509                                 break;
1510                         case 26:
1511                                 validate(ipc);
1512                                 break;
1513                         case 29:
1514                         case 30:
1515                                 if (!rc_alt_semantics) {
1516                                         updatels(ipc);
1517                                 }
1518                                 termn8 = 1;
1519                                 break;
1520                         case 48:
1521                                 enterinfo(ipc);
1522                                 break;
1523                         case 49:
1524                                 readinfo(ipc);
1525                                 break;
1526                         case 72:
1527                                 cli_image_upload(ipc, "_userpic_");
1528                                 break;
1529                         case 73:
1530                                 cli_image_upload(ipc, "_roompic_");
1531                                 break;
1532
1533                         case 74:
1534                                 snprintf(aaa, sizeof aaa, "_floorpic_|%d", curr_floor);
1535                                 cli_image_upload(ipc, aaa);
1536                                 break;
1537
1538                         case 75:
1539                                 enternew(ipc, "roomname", aaa, 20);
1540                                 r = CtdlIPCChangeRoomname(ipc, aaa, bbb);
1541                                 if (r / 100 != 2)
1542                                         scr_printf("\n%s\n", aaa);
1543                                 break;
1544                         case 76:
1545                                 enternew(ipc, "hostname", aaa, 25);
1546                                 r = CtdlIPCChangeHostname(ipc, aaa, bbb);
1547                                 if (r / 100 != 2)
1548                                         scr_printf("\n%s\n", aaa);
1549                                 break;
1550                         case 77:
1551                                 enternew(ipc, "username", aaa, 32);
1552                                 r = CtdlIPCChangeUsername(ipc, aaa, bbb);
1553                                 if (r / 100 != 2)
1554                                         scr_printf("\n%s\n", aaa);
1555                                 break;
1556
1557                         case 35:
1558                                 set_password(ipc);
1559                                 break;
1560
1561                         case 21:
1562                                 if (argbuf[0] == 0)
1563                                         strcpy(aaa, "?");
1564                                 display_help(ipc, argbuf);
1565                                 break;
1566
1567                         case 41:
1568                                 formout(ipc, "register");
1569                                 entregis(ipc);
1570                                 break;
1571
1572                         case 15:
1573                                 scr_printf("Are you sure (y/n)? ");
1574                                 if (yesno() == 1) {
1575                                         if (!rc_alt_semantics)
1576                                                 updatels(ipc);
1577                                         a = 0;
1578                                         termn8 = 1;
1579                                 }
1580                                 break;
1581
1582                         case 85:
1583                                 scr_printf("All users will be disconnected!  "
1584                                            "Really terminate the server? ");
1585                                 if (yesno() == 1) {
1586                                         if (!rc_alt_semantics)
1587                                                 updatels(ipc);
1588                                         r = CtdlIPCTerminateServerNow(ipc, aaa);
1589                                         scr_printf("%s\n", aaa);
1590                                         if (r / 100 == 2) {
1591                                                 a = 0;
1592                                                 termn8 = 1;
1593                                         }
1594                                 }
1595                                 break;
1596
1597                         case 86:
1598                                 scr_printf("Do you really want to schedule a "
1599                                            "server shutdown? ");
1600                                 if (yesno() == 1) {
1601                                         r = CtdlIPCTerminateServerScheduled(ipc, 1, aaa);
1602                                         if (r / 100 == 2) {
1603                                                 if (atoi(aaa)) {
1604                                                         scr_printf(
1605                                                                    "The Citadel server will terminate when all users are logged off.\n"
1606                                                                    );
1607                                                 } else {
1608                                                         scr_printf(
1609                                                                    "The Citadel server will not terminate.\n"
1610                                                                    );
1611                                                 }
1612                                         }
1613                                 }
1614                                 break;
1615
1616                         case 87:
1617                                 network_config_management(ipc, "listrecp",
1618                                                           "Message-by-message mailing list recipients");
1619                                 break;
1620
1621                         case 94:
1622                                 network_config_management(ipc, "digestrecp",
1623                                                           "Digest mailing list recipients");
1624                                 break;
1625
1626                         case 89:
1627                                 network_config_management(ipc, "ignet_push_share",
1628                                                           "Nodes with which we share this room");
1629                                 break;
1630
1631                         case 88:
1632                                 do_ignet_configuration(ipc);
1633                                 break;
1634
1635                         case 92:
1636                                 do_filterlist_configuration(ipc);
1637                                 break;
1638
1639                         case 6:
1640                                 if (rc_alt_semantics) {
1641                                         updatelsa(ipc);
1642                                 }
1643                                 gotonext(ipc);
1644                                 break;
1645
1646                         case 3:
1647                                 chatmode(ipc);
1648                                 break;
1649
1650                         case 2:
1651                                 if (ipc->isLocal) {
1652                                         screen_reset();
1653                                         sttybbs(SB_RESTORE);
1654                                         snprintf(aaa, sizeof aaa, "USERNAME=\042%s\042; export USERNAME;"
1655                                                  "exec ./subsystem %ld %d %d", fullname,
1656                                                  usernum, screenwidth, axlevel);
1657                                         ka_system(aaa);
1658                                         sttybbs(SB_NO_INTR);
1659                                         screen_set();
1660                                 } else {
1661                                         scr_printf("*** Can't run doors when server is not local.\n");
1662                                 }
1663                                 break;
1664
1665                         case 17:
1666                                 who_is_online(ipc, 0);
1667                                 break;
1668
1669                         case 79:
1670                                 who_is_online(ipc, 1);
1671                                 break;
1672
1673                         case 91:
1674                                 who_is_online(ipc, 2);
1675                                 break;
1676                 
1677                         case 80:
1678                                 do_system_configuration(ipc);
1679                                 break;
1680
1681                         case 82:
1682                                 do_internet_configuration(ipc);
1683                                 break;
1684
1685                         case 83:
1686                                 check_message_base(ipc);
1687                                 break;
1688
1689                         case 84:
1690                                 quiet_mode(ipc);
1691                                 break;
1692
1693                         case 93:
1694                                 stealth_mode(ipc);
1695                                 break;
1696
1697                         case 50:
1698                                 enter_config(ipc, 2);
1699                                 break;
1700
1701                         case 37:
1702                                 enter_config(ipc, 0);
1703                                 set_floor_mode();
1704                                 break;
1705
1706                         case 59:
1707                                 enter_config(ipc, 3);
1708                                 set_floor_mode();
1709                                 break;
1710
1711                         case 60:
1712                                 gotofloor(ipc, argbuf, GF_GOTO);
1713                                 break;
1714
1715                         case 61:
1716                                 gotofloor(ipc, argbuf, GF_SKIP);
1717                                 break;
1718
1719                         case 62:
1720                                 forget_this_floor(ipc);
1721                                 break;
1722
1723                         case 63:
1724                                 create_floor(ipc);
1725                                 break;
1726
1727                         case 64:
1728                                 edit_floor(ipc);
1729                                 break;
1730
1731                         case 65:
1732                                 kill_floor(ipc);
1733                                 break;
1734
1735                         case 66:
1736                                 enter_bio(ipc);
1737                                 break;
1738
1739                         case 67:
1740                                 read_bio(ipc);
1741                                 break;
1742
1743                         case 25:
1744                                 edituser(ipc);
1745                                 break;
1746
1747                         case 8:
1748                                 knrooms(ipc, floor_mode);
1749                                 scr_printf("\n");
1750                                 break;
1751
1752                         case 68:
1753                                 knrooms(ipc, 2);
1754                                 scr_printf("\n");
1755                                 break;
1756
1757                         case 69:
1758                                 misc_server_cmd(ipc, argbuf);
1759                                 break;
1760
1761                         case 70:
1762                                 edit_system_message(ipc, argbuf);
1763                                 break;
1764
1765                         case 19:
1766                                 listzrooms(ipc);
1767                                 scr_printf("\n");
1768                                 break;
1769
1770                         case 51:
1771                                 deletefile(ipc);
1772                                 break;
1773
1774                         case 53:
1775                                 netsendfile(ipc);
1776                                 break;
1777
1778                         case 54:
1779                                 movefile(ipc);
1780                                 break;
1781
1782                         case 56:
1783                                 page_user(ipc);
1784                                 break;
1785
1786                         default: /* allow some math on the command */
1787                                 /* commands 100... to 100+MAX_EDITORS-1 will
1788                                    call the appropriate editor... in other
1789                                    words, command numbers 100+ will match
1790                                    the citadel.rc keys editor0, editor1, etc.*/
1791                                 if (mcmd >= 100 && mcmd < (100+MAX_EDITORS))
1792                                 {
1793                                         /* entmsg mode >=2 select editor */
1794                                         entmsg(ipc, 0, mcmd - 100 + 2);
1795                                         break;
1796                                 }
1797                         }       /* end switch */
1798         } while (termn8 == 0);
1799
1800 TERMN8: scr_printf("%s logged out.", fullname);
1801         termn8 = 0;
1802         color(ORIGINAL_PAIR);
1803         scr_printf("\n");
1804         while (march != NULL) {
1805                 remove_march(march->march_name, 0);
1806         }
1807         if (mcmd == 30) {
1808                 sln_printf("\n\nType 'off' to disconnect, or next user...\n");
1809         }
1810         CtdlIPCLogout(ipc);
1811         if ((mcmd == 29) || (mcmd == 15)) {
1812                 screen_delete();
1813                 sttybbs(SB_RESTORE);
1814                 formout(ipc, "goodbye");
1815                 logoff(ipc, 0);
1816         }
1817         goto GSTA;
1818
1819 }       /* end main() */