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