calc home dirs
[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
1046         
1047         setIPCDeathHook(screen_delete);
1048         setIPCErrorPrintf(err_printf);
1049         setCryptoStatusHook(statusHook);
1050         
1051         /* Permissions sanity check - don't run citadel setuid/setgid */
1052         if (getuid() != geteuid()) {
1053                 err_printf("Please do not run citadel setuid!\n");
1054                 logoff(NULL, 3);
1055         } else if (getgid() != getegid()) {
1056                 err_printf("Please do not run citadel setgid!\n");
1057                 logoff(NULL, 3);
1058         }
1059
1060         stty_ctdl(SB_SAVE);     /* Store the old terminal parameters */
1061         load_command_set();     /* parse the citadel.rc file */
1062         stty_ctdl(SB_NO_INTR);  /* Install the new ones */
1063         /* signal(SIGHUP, dropcarr);FIXME */    /* Cleanup gracefully if carrier is dropped */
1064         signal(SIGPIPE, dropcarr);      /* Cleanup gracefully if local conn. dropped */
1065         signal(SIGTERM, dropcarr);      /* Cleanup gracefully if terminated */
1066         signal(SIGCONT, catch_sigcont); /* Catch SIGCONT so we can reset terminal */
1067 #ifdef SIGWINCH
1068         signal(SIGWINCH, scr_winch);    /* Window resize signal */
1069 #endif
1070
1071 #ifdef HAVE_OPENSSL
1072         arg_encrypt = RC_DEFAULT;
1073 #endif
1074 #if defined(HAVE_CURSES_H) && !defined(DISABLE_CURSES)
1075         arg_screen = RC_DEFAULT;
1076 #endif
1077
1078         /* 
1079          * Handle command line options as if we were called like /bin/login
1080          * (i.e. from in.telnetd)
1081          */
1082         for (a=0; a<argc; ++a) {
1083                 if ((argc > a+1) && (!strcmp(argv[a], "-h")) ) {
1084                         telnet_client_host = argv[a+1];
1085                         argc = shift(argc, argv, a, 2);
1086                 }
1087                 if (!strcmp(argv[a], "-x")) {
1088 #ifdef HAVE_OPENSSL
1089                         arg_encrypt = RC_NO;
1090 #endif
1091                         argc = shift(argc, argv, a, 1);
1092                 }
1093                 if (!strcmp(argv[a], "-X")) {
1094 #ifdef HAVE_OPENSSL
1095                         arg_encrypt = RC_YES;
1096                         argc = shift(argc, argv, a, 1);
1097 #else
1098                         fprintf(stderr, "Not compiled with encryption support");
1099                         return 1;
1100 #endif
1101                 }
1102                 if (!strcmp(argv[a], "-s")) {
1103 #if defined(HAVE_CURSES_H) && !defined(DISABLE_CURSES)
1104                         arg_screen = RC_NO;
1105 #endif
1106                         argc = shift(argc, argv, a, 1);
1107                 }
1108                 if (!strcmp(argv[a], "-S")) {
1109 #if defined(HAVE_CURSES_H) && !defined(DISABLE_CURSES)
1110                         arg_screen = RC_YES;
1111 #endif
1112                         argc = shift(argc, argv, a, 1);
1113                 }
1114                 if (!strcmp(argv[a], "-p")) {
1115                         struct stat st;
1116                 
1117                         if (chdir(CTDLDIR) < 0) {
1118                                 perror("can't change to " CTDLDIR);
1119                                 logoff(NULL, 3);
1120                         }
1121
1122                         calc_dirs_n_files(relh, home, relhome, ctdldir);
1123
1124                         /*
1125                          * Drop privileges if necessary. We stat
1126                          * citadel.config to get the uid/gid since it's
1127                          * guaranteed to have the uid/gid we want.
1128                          */
1129                         if (!getuid() || !getgid()) {
1130                                 if (stat(file_citadel_config, &st) < 0) {
1131                                         perror("couldn't stat citadel.config");
1132                                         logoff(NULL, 3);
1133                                 }
1134                                 if (!getgid() && (setgid(st.st_gid) < 0)) {
1135                                         perror("couldn't change gid");
1136                                         logoff(NULL, 3);
1137                                 }
1138                                 if (!getuid() && (setuid(st.st_uid) < 0)) {
1139                                         perror("couldn't change uid");
1140                                         logoff(NULL, 3);
1141                                 }
1142                                 /*
1143                                   scr_printf("Privileges changed to uid %d gid %d\n",
1144                                   getuid(), getgid());
1145                                 */
1146                         }
1147                         argc = shift(argc, argv, a, 1);
1148                 }
1149         }
1150
1151         screen_new();
1152
1153 #ifdef __CYGWIN__
1154         newprompt("Connect to (return for local server): ", hostbuf, 64);
1155 #endif
1156
1157         sln_printf("Attaching to server... \r");
1158         sln_flush();
1159         ipc = CtdlIPC_new(argc, argv, hostbuf, portbuf);
1160         if (!ipc) {
1161                 screen_delete();
1162                 error_printf("Can't connect: %s\n", strerror(errno));
1163                 logoff(NULL, 3);
1164         }
1165 #if defined(HAVE_CURSES_H) && !defined(DISABLE_CURSES)
1166         CtdlIPC_SetNetworkStatusCallback(ipc, wait_indicator);
1167 #endif
1168         ipc_for_signal_handlers = ipc;  /* KLUDGE cover your eyes */
1169
1170         CtdlIPC_chat_recv(ipc, aaa);
1171         if (aaa[0] != '2') {
1172                 scr_printf("%s\n", &aaa[4]);
1173                 logoff(ipc, atoi(aaa));
1174         }
1175
1176         /* If there is a [nonce] at the end, put the nonce in <nonce>, else nonce
1177          * is zeroized.
1178          */
1179         
1180         if ((sptr = strchr(aaa, '<')) == NULL)
1181                 {
1182                         nonce[0] = '\0';
1183                 }
1184         else
1185                 {
1186                         if ((sptr2 = strchr(sptr, '>')) == NULL)
1187                                 {
1188                                         nonce[0] = '\0';
1189                                 }
1190                         else
1191                                 {
1192                                         sptr2++;
1193                                         *sptr2 = '\0';
1194                                         strncpy(nonce, sptr, (size_t)NONCE_SIZE);
1195                                 }
1196                 }
1197
1198 #ifdef HAVE_OPENSSL
1199         /* Evaluate encryption preferences */
1200         if (arg_encrypt != RC_NO && rc_encrypt != RC_NO) {
1201                 if (!ipc->isLocal || arg_encrypt == RC_YES || rc_encrypt == RC_YES) {
1202                         secure = (CtdlIPCStartEncryption(ipc, aaa) / 100 == 2) ? 1 : 0;
1203                         if (!secure)
1204                                 error_printf("Can't encrypt: %s\n", aaa);
1205                 }
1206         }
1207 #endif
1208
1209         get_serv_info(ipc, telnet_client_host);
1210         scr_printf("%-24s\n%s\n%s\n", ipc->ServInfo.software, ipc->ServInfo.humannode,
1211                    ipc->ServInfo.site_location);
1212         scr_flush();
1213
1214         status_line(ipc->ServInfo.humannode, ipc->ServInfo.site_location, NULL,
1215                     secure, -1);
1216
1217         screenwidth = 80;       /* default screen dimensions */
1218         screenheight = 24;
1219         
1220         scr_printf(" pause    next    stop\n");
1221         scr_printf(" ctrl-s  ctrl-o  ctrl-c\n\n");
1222         formout(ipc, "hello");  /* print the opening greeting */
1223         scr_printf("\n");
1224
1225  GSTA:  /* See if we have a username and password on disk */
1226         if (rc_remember_passwords) {
1227                 get_stored_password(hostbuf, portbuf, fullname, password);
1228                 if (strlen(fullname) > 0) {
1229                         r = CtdlIPCTryLogin(ipc, fullname, aaa);
1230                         if (r / 100 == 3) {
1231                                 if (*nonce) {
1232                                         r = CtdlIPCTryApopPassword(ipc, make_apop_string(password, nonce, hexstring, sizeof hexstring), aaa);
1233                                 } else {
1234                                         r = CtdlIPCTryPassword(ipc, password, aaa);
1235                                 }
1236                         }
1237
1238                         if (r / 100 == 2) {
1239                                 load_user_info(aaa);
1240                                 stored_password = 1;
1241                                 goto PWOK;
1242                         } else {
1243                                 set_stored_password(hostbuf, portbuf, "", "");
1244                         }
1245                 }
1246         }
1247
1248         termn8 = 0;
1249         newnow = 0;
1250         do {
1251                 if (strlen(rc_username) > 0) {
1252                         strcpy(fullname, rc_username);
1253                 } else {
1254                         newprompt("Enter your name: ", fullname, 29);
1255                 }
1256                 strproc(fullname);
1257                 if (!strcasecmp(fullname, "new")) {     /* just in case */
1258                         scr_printf("Please enter the name you wish to log in with.\n");
1259                 }
1260         } while (
1261                  (!strcasecmp(fullname, "bbs"))
1262                  || (!strcasecmp(fullname, "new"))
1263                  || (strlen(fullname) == 0));
1264
1265         if (!strcasecmp(fullname, "off")) {
1266                 mcmd = 29;
1267                 goto TERMN8;
1268         }
1269         /* sign on to the server */
1270         r = CtdlIPCTryLogin(ipc, fullname, aaa);
1271         if (r / 100 != 3)
1272                 goto NEWUSR;
1273
1274         /* password authentication */
1275         if (strlen(rc_password) > 0) {
1276                 strcpy(password, rc_password);
1277         } else {
1278                 newprompt("\rPlease enter your password: ", password, -19);
1279         }
1280         strproc(password);
1281
1282         if (*nonce) {
1283                 r = CtdlIPCTryApopPassword(ipc, make_apop_string(password, nonce, hexstring, sizeof hexstring), aaa);
1284         } else {
1285                 r = CtdlIPCTryPassword(ipc, password, aaa);
1286         }
1287         
1288         if (r / 100 == 2) {
1289                 load_user_info(aaa);
1290                 offer_to_remember_password(ipc, hostbuf, portbuf,
1291                                            fullname, password);
1292                 goto PWOK;
1293         }
1294         scr_printf("<< wrong password >>\n");
1295         if (strlen(rc_password) > 0)
1296                 logoff(ipc, 2);
1297         goto GSTA;
1298
1299 NEWUSR: if (strlen(rc_password) == 0) {
1300                 scr_printf("'%s' not found.\n"
1301                         "Do you want to create a new account with this name? ",
1302                         fullname);
1303                 if (yesno() == 0)
1304                         goto GSTA;
1305         }
1306
1307         r = CtdlIPCCreateUser(ipc, fullname, 1, aaa);
1308         if (r / 100 != 2) {
1309                 scr_printf("%s\n", aaa);
1310                 goto GSTA;
1311         }
1312         load_user_info(aaa);
1313
1314         while (set_password(ipc) != 0);
1315         newnow = 1;
1316
1317         enter_config(ipc, 1);
1318
1319  PWOK:
1320         /* Switch color support on or off if we're in user mode */
1321         if (rc_ansi_color == 3) {
1322                 if (userflags & US_COLOR)
1323                         enable_color = 1;
1324                 else
1325                         enable_color = 0;
1326         }
1327
1328         color(BRIGHT_WHITE);
1329         scr_printf("\n%s\nAccess level: %d (%s)\n"
1330                    "User #%ld / Login #%d",
1331                    fullname, axlevel, axdefs[(int) axlevel],
1332                    usernum, timescalled);
1333         if (lastcall > 0L) {
1334                 scr_printf(" / Last login: %s",
1335                            asctime(localtime(&lastcall)));
1336         }
1337         scr_printf("\n");
1338
1339         r = CtdlIPCMiscCheck(ipc, &chek, aaa);
1340         if (r / 100 == 2) {
1341                 b = chek.newmail;
1342                 if (b > 0) {
1343                         color(BRIGHT_RED);
1344                         if (b == 1)
1345                                 scr_printf("*** You have a new private message in Mail>\n");
1346                         if (b > 1)
1347                                 scr_printf("*** You have %d new private messages in Mail>\n", b);
1348                         color(DIM_WHITE);
1349                         if (strlen(rc_gotmail_cmd) > 0) {
1350                                 system(rc_gotmail_cmd);
1351                         }
1352                 }
1353                 if ((axlevel >= 6) && (chek.needvalid > 0)) {
1354                         scr_printf("*** Users need validation\n");
1355                 }
1356                 if (chek.needregis > 0) {
1357                         scr_printf("*** Please register.\n");
1358                         formout(ipc, "register");
1359                         entregis(ipc);
1360                 }
1361         }
1362         /* Make up some temporary filenames for use in various parts of the
1363          * program.  Don't mess with these once they've been set, because we
1364          * will be unlinking them later on in the program and we don't
1365          * want to delete something that we didn't create. */
1366         CtdlMakeTempFileName(temp, sizeof temp);
1367         CtdlMakeTempFileName(temp2, sizeof temp2);
1368         CtdlMakeTempFileName(tempdir, sizeof tempdir);
1369
1370         /* Get screen dimensions.  First we go to a default of 80x24.  Then
1371          * we try to get the user's actual screen dimensions off the server.
1372          * However, if we're running on an xterm, all this stuff is
1373          * irrelevant because we're going to dynamically size the screen
1374          * during the session.
1375          */
1376         screenwidth = 80;
1377         screenheight = 24;
1378         r = CtdlIPCGetConfig(ipc, &myself, aaa);
1379         if (r == 2) {
1380                 screenwidth = myself->USscreenwidth;
1381                 screenheight = myself->USscreenheight;
1382         }
1383         if (getenv("TERM") != NULL)
1384                 if (!strcmp(getenv("TERM"), "xterm")) {
1385                         have_xterm = 1;
1386                 }
1387 #ifdef TIOCGWINSZ
1388         check_screen_dims();
1389 #endif
1390
1391         set_floor_mode(ipc);
1392
1393         /* Enter the lobby */
1394         dotgoto(ipc, "_BASEROOM_", 1, 0);
1395
1396         /* Main loop for the system... user is logged in. */
1397         uglistsize = 0;
1398
1399         if (newnow == 1)
1400                 readmsgs(ipc, LastMessages, ReadForward, 5);
1401         else
1402                 readmsgs(ipc, NewMessages, ReadForward, 0);
1403
1404         /* MAIN COMMAND LOOP */
1405         do {
1406                 mcmd = getcmd(ipc, argbuf);     /* Get keyboard command */
1407
1408 #ifdef TIOCGWINSZ
1409                 check_screen_dims();            /* if xterm, get screen size */
1410 #endif
1411
1412                 if (termn8 == 0)
1413                         switch (mcmd) {
1414                         case 1:
1415                                 formout(ipc, "help");
1416                                 break;
1417                         case 4:
1418                                 entmsg(ipc, 0, ((userflags & US_EXTEDIT) ? 2 : 0));
1419                                 break;
1420                         case 36:
1421                                 entmsg(ipc, 0, 1);
1422                                 break;
1423                         case 46:
1424                                 entmsg(ipc, 0, 2);
1425                                 break;
1426                         case 78:
1427                                 {
1428                                         /* Only m.author is used */
1429                                         struct ctdlipcmessage m;
1430                                         newprompt("What do you want your username to be? ",
1431                                                 m.author, USERNAME_SIZE - 1);
1432                                         m.text = "";
1433                                         r = CtdlIPCPostMessage(ipc, 2, &m, aaa);
1434                                         if (r / 100 != 2)
1435                                                 scr_printf("%s\n", aaa);
1436                                         else
1437                                                 entmsg(ipc, 0, 0);
1438                                 }
1439                                 break;
1440                         case 5:                         /* <G>oto */
1441                                 updatels(ipc);
1442                                 gotonext(ipc);
1443                                 break;
1444                         case 47:                        /* <A>bandon */
1445                                 if (!rc_alt_semantics) {
1446                                         updatelsa(ipc);
1447                                 }
1448                                 gotonext(ipc);
1449                                 break;
1450                         case 90:                        /* <.A>bandon */
1451                                 if (!rc_alt_semantics)
1452                                         updatelsa(ipc);
1453                                 dotgoto(ipc, argbuf, 0, 0);
1454                                 break;
1455                         case 58:                        /* <M>ail */
1456                                 updatelsa(ipc);
1457                                 dotgoto(ipc, "_MAIL_", 1, 0);
1458                                 break;
1459                         case 20:
1460                                 if (strlen(argbuf) > 0) {
1461                                         updatels(ipc);
1462                                         dotgoto(ipc, argbuf, 0, 0);
1463                                 }
1464                                 break;
1465                         case 52:
1466                                 if (strlen(argbuf) > 0) {
1467                                         if (rc_alt_semantics) {
1468                                                 updatelsa(ipc);
1469                                         }
1470                                         dotgoto(ipc, argbuf, 0, 0);
1471                                 }
1472                                 break;
1473                         case 95: /* what exactly is the numbering scheme supposed to be anyway? --Ford, there isn't one. -IO */
1474                                 dotungoto(ipc, argbuf);
1475                                 break;
1476                         case 10:
1477                                 readmsgs(ipc, AllMessages, ReadForward, 0);
1478                                 break;
1479                         case 9:
1480                                 readmsgs(ipc, LastMessages, ReadForward, 5);
1481                                 break;
1482                         case 13:
1483                                 readmsgs(ipc, NewMessages, ReadForward, 0);
1484                                 break;
1485                         case 11:
1486                                 readmsgs(ipc, AllMessages, ReadReverse, 0);
1487                                 break;
1488                         case 12:
1489                                 readmsgs(ipc, OldMessages, ReadReverse, 0);
1490                                 break;
1491                         case 71:
1492                                 readmsgs(ipc, LastMessages, ReadForward,
1493                                                 atoi(argbuf));
1494                                 break;
1495                         case 7:
1496                                 forget(ipc);
1497                                 break;
1498                         case 18:
1499                                 subshell();
1500                                 break;
1501                         case 38:
1502                                 updatels(ipc);
1503                                 entroom(ipc);
1504                                 break;
1505                         case 22:
1506                                 killroom(ipc);
1507                                 break;
1508                         case 32:
1509                                 userlist(ipc, argbuf);
1510                                 break;
1511                         case 27:
1512                                 invite(ipc);
1513                                 break;
1514                         case 28:
1515                                 kickout(ipc);
1516                                 break;
1517                         case 23:
1518                                 editthisroom(ipc);
1519                                 break;
1520                         case 14:
1521                                 roomdir(ipc);
1522                                 break;
1523                         case 33:
1524                                 download(ipc, 0);
1525                                 break;
1526                         case 34:
1527                                 download(ipc, 1);
1528                                 break;
1529                         case 31:
1530                                 download(ipc, 2);
1531                                 break;
1532                         case 43:
1533                                 download(ipc, 3);
1534                                 break;
1535                         case 45:
1536                                 download(ipc, 4);
1537                                 break;
1538                         case 55:
1539                                 download(ipc, 5);
1540                                 break;
1541                         case 39:
1542                                 upload(ipc, 0);
1543                                 break;
1544                         case 40:
1545                                 upload(ipc, 1);
1546                                 break;
1547                         case 42:
1548                                 upload(ipc, 2);
1549                                 break;
1550                         case 44:
1551                                 upload(ipc, 3);
1552                                 break;
1553                         case 57:
1554                                 cli_upload(ipc);
1555                                 break;
1556                         case 16:
1557                                 ungoto(ipc);
1558                                 break;
1559                         case 24:
1560                                 whoknows(ipc);
1561                                 break;
1562                         case 26:
1563                                 validate(ipc);
1564                                 break;
1565                         case 29:
1566                         case 30:
1567                                 if (!rc_alt_semantics) {
1568                                         updatels(ipc);
1569                                 }
1570                                 termn8 = 1;
1571                                 break;
1572                         case 48:
1573                                 enterinfo(ipc);
1574                                 break;
1575                         case 49:
1576                                 readinfo(ipc);
1577                                 break;
1578                         case 72:
1579                                 cli_image_upload(ipc, "_userpic_");
1580                                 break;
1581                         case 73:
1582                                 cli_image_upload(ipc, "_roompic_");
1583                                 break;
1584
1585                         case 74:
1586                                 snprintf(aaa, sizeof aaa, "_floorpic_|%d", curr_floor);
1587                                 cli_image_upload(ipc, aaa);
1588                                 break;
1589
1590                         case 75:
1591                                 enternew(ipc, "roomname", aaa, 20);
1592                                 r = CtdlIPCChangeRoomname(ipc, aaa, bbb);
1593                                 if (r / 100 != 2)
1594                                         scr_printf("\n%s\n", bbb);
1595                                 break;
1596                         case 76:
1597                                 enternew(ipc, "hostname", aaa, 25);
1598                                 r = CtdlIPCChangeHostname(ipc, aaa, bbb);
1599                                 if (r / 100 != 2)
1600                                         scr_printf("\n%s\n", bbb);
1601                                 break;
1602                         case 77:
1603                                 enternew(ipc, "username", aaa, 32);
1604                                 r = CtdlIPCChangeUsername(ipc, aaa, bbb);
1605                                 if (r / 100 != 2)
1606                                         scr_printf("\n%s\n", bbb);
1607                                 break;
1608
1609                         case 35:
1610                                 set_password(ipc);
1611                                 break;
1612
1613                         case 21:
1614                                 if (argbuf[0] == 0)
1615                                         strcpy(aaa, "?");
1616                                 display_help(ipc, argbuf);
1617                                 break;
1618
1619                         case 41:
1620                                 formout(ipc, "register");
1621                                 entregis(ipc);
1622                                 break;
1623
1624                         case 15:
1625                                 scr_printf("Are you sure (y/n)? ");
1626                                 if (yesno() == 1) {
1627                                         if (!rc_alt_semantics)
1628                                                 updatels(ipc);
1629                                         a = 0;
1630                                         termn8 = 1;
1631                                 }
1632                                 break;
1633
1634                         case 85:
1635                                 scr_printf("All users will be disconnected!  "
1636                                            "Really terminate the server? ");
1637                                 if (yesno() == 1) {
1638                                         if (!rc_alt_semantics)
1639                                                 updatels(ipc);
1640                                         r = CtdlIPCTerminateServerNow(ipc, aaa);
1641                                         scr_printf("%s\n", aaa);
1642                                         if (r / 100 == 2) {
1643                                                 a = 0;
1644                                                 termn8 = 1;
1645                                         }
1646                                 }
1647                                 break;
1648
1649                         case 86:
1650                                 scr_printf("Do you really want to schedule a "
1651                                            "server shutdown? ");
1652                                 if (yesno() == 1) {
1653                                         r = CtdlIPCTerminateServerScheduled(ipc, 1, aaa);
1654                                         if (r / 100 == 2) {
1655                                                 if (atoi(aaa)) {
1656                                                         scr_printf(
1657                                                                    "The Citadel server will terminate when all users are logged off.\n"
1658                                                                    );
1659                                                 } else {
1660                                                         scr_printf(
1661                                                                    "The Citadel server will not terminate.\n"
1662                                                                    );
1663                                                 }
1664                                         }
1665                                 }
1666                                 break;
1667
1668                         case 87:
1669                                 network_config_management(ipc, "listrecp",
1670                                                           "Message-by-message mailing list recipients");
1671                                 break;
1672
1673                         case 94:
1674                                 network_config_management(ipc, "digestrecp",
1675                                                           "Digest mailing list recipients");
1676                                 break;
1677
1678                         case 89:
1679                                 network_config_management(ipc, "ignet_push_share",
1680                                                           "Nodes with which we share this room");
1681                                 break;
1682
1683                         case 88:
1684                                 do_ignet_configuration(ipc);
1685                                 break;
1686
1687                         case 92:
1688                                 do_filterlist_configuration(ipc);
1689                                 break;
1690
1691                         case 6:
1692                                 if (rc_alt_semantics) {
1693                                         updatelsa(ipc);
1694                                 }
1695                                 gotonext(ipc);
1696                                 break;
1697
1698                         case 3:
1699                                 chatmode(ipc);
1700                                 break;
1701
1702                         case 2:
1703                                 if (ipc->isLocal) {
1704                                         screen_reset();
1705                                         stty_ctdl(SB_RESTORE);
1706                                         snprintf(aaa, sizeof aaa, "USERNAME=\042%s\042; export USERNAME;"
1707                                                  "exec ./subsystem %ld %d %d", fullname,
1708                                                  usernum, screenwidth, axlevel);
1709                                         ka_system(aaa);
1710                                         stty_ctdl(SB_NO_INTR);
1711                                         screen_set();
1712                                 } else {
1713                                         scr_printf("*** Can't run doors when server is not local.\n");
1714                                 }
1715                                 break;
1716
1717                         case 17:
1718                                 who_is_online(ipc, 0);
1719                                 break;
1720
1721                         case 79:
1722                                 who_is_online(ipc, 1);
1723                                 break;
1724
1725                         case 91:
1726                                 who_is_online(ipc, 2);
1727                                 break;
1728                 
1729                         case 80:
1730                                 do_system_configuration(ipc);
1731                                 break;
1732
1733                         case 82:
1734                                 do_internet_configuration(ipc);
1735                                 break;
1736
1737                         case 83:
1738                                 check_message_base(ipc);
1739                                 break;
1740
1741                         case 84:
1742                                 quiet_mode(ipc);
1743                                 break;
1744
1745                         case 93:
1746                                 stealth_mode(ipc);
1747                                 break;
1748
1749                         case 50:
1750                                 enter_config(ipc, 2);
1751                                 break;
1752
1753                         case 37:
1754                                 enter_config(ipc, 0);
1755                                 set_floor_mode(ipc);
1756                                 break;
1757
1758                         case 59:
1759                                 enter_config(ipc, 3);
1760                                 set_floor_mode(ipc);
1761                                 break;
1762
1763                         case 60:
1764                                 gotofloor(ipc, argbuf, GF_GOTO);
1765                                 break;
1766
1767                         case 61:
1768                                 gotofloor(ipc, argbuf, GF_SKIP);
1769                                 break;
1770
1771                         case 62:
1772                                 forget_this_floor(ipc);
1773                                 break;
1774
1775                         case 63:
1776                                 create_floor(ipc);
1777                                 break;
1778
1779                         case 64:
1780                                 edit_floor(ipc);
1781                                 break;
1782
1783                         case 65:
1784                                 kill_floor(ipc);
1785                                 break;
1786
1787                         case 66:
1788                                 enter_bio(ipc);
1789                                 break;
1790
1791                         case 67:
1792                                 read_bio(ipc);
1793                                 break;
1794
1795                         case 25:
1796                                 edituser(ipc, 25);
1797                                 break;
1798
1799                         case 96:
1800                                 edituser(ipc, 96);
1801                                 break;
1802
1803                         case 8:
1804                                 knrooms(ipc, floor_mode);
1805                                 scr_printf("\n");
1806                                 break;
1807
1808                         case 68:
1809                                 knrooms(ipc, 2);
1810                                 scr_printf("\n");
1811                                 break;
1812
1813                         case 69:
1814                                 misc_server_cmd(ipc, argbuf);
1815                                 break;
1816
1817                         case 70:
1818                                 edit_system_message(ipc, argbuf);
1819                                 break;
1820
1821                         case 19:
1822                                 listzrooms(ipc);
1823                                 scr_printf("\n");
1824                                 break;
1825
1826                         case 51:
1827                                 deletefile(ipc);
1828                                 break;
1829
1830                         case 53:
1831                                 netsendfile(ipc);
1832                                 break;
1833
1834                         case 54:
1835                                 movefile(ipc);
1836                                 break;
1837
1838                         case 56:
1839                                 page_user(ipc);
1840                                 break;
1841
1842                         default: /* allow some math on the command */
1843                                 /* commands 100... to 100+MAX_EDITORS-1 will
1844                                    call the appropriate editor... in other
1845                                    words, command numbers 100+ will match
1846                                    the citadel.rc keys editor0, editor1, etc.*/
1847                                 if (mcmd >= 100 && mcmd < (100+MAX_EDITORS))
1848                                 {
1849                                         /* entmsg mode >=2 select editor */
1850                                         entmsg(ipc, 0, mcmd - 100 + 2);
1851                                         break;
1852                                 }
1853                         }       /* end switch */
1854         } while (termn8 == 0);
1855
1856 TERMN8: scr_printf("%s logged out.", fullname);
1857         termn8 = 0;
1858         color(ORIGINAL_PAIR);
1859         scr_printf("\n");
1860         while (march != NULL) {
1861                 remove_march(march->march_name, 0);
1862         }
1863         if (mcmd == 30) {
1864                 sln_printf("\n\nType 'off' to disconnect, or next user...\n");
1865         }
1866         CtdlIPCLogout(ipc);
1867         if ((mcmd == 29) || (mcmd == 15)) {
1868                 screen_delete();
1869                 stty_ctdl(SB_RESTORE);
1870                 formout(ipc, "goodbye");
1871                 logoff(ipc, 0);
1872         }
1873         goto GSTA;
1874
1875 }       /* end main() */