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