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