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