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