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