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