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