fix all the <time.h> vs. <sys/time.h> issues, hopefully
[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 <setjmp.h>
34 #include <stdarg.h>
35
36 #include "citadel.h"
37 #include "axdefs.h"
38 #include "serv_info.h"
39 #include "routines.h"
40 #include "routines2.h"
41 #include "rooms.h"
42 #include "messages.h"
43 #include "commands.h"
44 #include "ipc.h"
45 #include "client_chat.h"
46 #include "client_passwords.h"
47 #include "citadel_decls.h"
48 #include "tools.h"
49 #include "acconfig.h"
50 #ifndef HAVE_SNPRINTF
51 #include "snprintf.h"
52 #endif
53
54 #include "md5.h"
55
56 struct march {
57         struct march *next;
58         char march_name[ROOMNAMELEN];
59         char march_floor;
60         char march_order;
61         };
62
63 #define IFEXPERT if (userflags&US_EXPERT)
64 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
65 #define IFAIDE if (axlevel>=6)
66 #define IFNAIDE if (axlevel<6)
67
68 struct march *march = NULL;
69
70 /* globals associated with the client program */
71 char temp[PATH_MAX];            /* Name of general temp file */
72 char temp2[PATH_MAX];           /* Name of general temp file */
73 char tempdir[PATH_MAX];         /* Name of general temp dir */
74 char editor_path[SIZ];          /* path to external editor */
75 char printcmd[SIZ];             /* print command */
76 int editor_pid = (-1);
77 char fullname[32];
78 jmp_buf nextbuf;
79 struct CtdlServInfo serv_info;  /* Info on the server connected */
80 int screenwidth;
81 int screenheight;
82 unsigned room_flags;
83 char room_name[ROOMNAMELEN];
84 char ugname[ROOMNAMELEN];
85 long uglsn;                     /* holds <u>ngoto info */
86 char is_mail = 0;               /* nonzero when we're in a mail room */
87 char axlevel = 0;               /* access level */
88 char is_room_aide = 0;          /* boolean flag, 1 if room aide */
89 int timescalled;
90 int posted;
91 unsigned userflags;
92 long usernum = 0L;              /* user number */
93 char newnow;
94 long highest_msg_read;          /* used for <A>bandon room cmd */
95 long maxmsgnum;                 /* used for <G>oto */
96 char sigcaught = 0;
97 char have_xterm = 0;            /* are we running on an xterm? */
98 char rc_username[32];
99 char rc_password[32];
100 char hostbuf[SIZ];
101 char portbuf[SIZ];
102 char rc_floor_mode;
103 char floor_mode;
104 char curr_floor = 0;            /* number of current floor */
105 char floorlist[128][SIZ];       /* names of floors */
106 char express_msgs = 0;          /* express messages waiting! */
107 int termn8 = 0;                 /* Set to nonzero to cause a logoff */
108
109 extern int rc_ansi_color;       /* ansi color value from citadel.rc */
110
111 /*
112  * here is our 'clean up gracefully and exit' routine
113  */
114 void logoff(int code)
115 {
116         if (editor_pid > 0) {   /* kill the editor if it's running */
117                 kill(editor_pid, SIGHUP);
118         }
119 /* shut down the server... but not if the logoff code is 3, because
120  * that means we're exiting because we already lost the server
121  */
122         if (code != 3)
123                 serv_puts("QUIT");
124
125 /*
126  * now clean up various things
127  */
128
129         unlink(temp);
130         unlink(temp2);
131         nukedir(tempdir);
132
133         /* Violently kill off any child processes if Citadel is
134          * the login shell. 
135          */
136         if (getppid() == 1) {
137                 kill(0 - getpgrp(), SIGTERM);
138                 sleep(1);
139                 kill(0 - getpgrp(), SIGKILL);
140         }
141         sttybbs(SB_RESTORE);    /* return the old terminal settings */
142         exit(code);             /* exit with the proper exit code */
143 }
144
145
146
147 /*
148  * signal catching function for hangups...
149  */
150 void dropcarr(int signum)
151 {
152         logoff(SIGHUP);
153 }
154
155
156
157 /*
158  * catch SIGCONT to reset terminal modes when were are put back into the
159  * foreground.
160  */
161 void catch_sigcont(int signum)
162 {
163         sttybbs(SB_LAST);
164         signal(SIGCONT, catch_sigcont);
165 }
166
167
168
169 /* general purpose routines */
170
171 void formout(char *name)
172 {                               /* display a file */
173         char cmd[SIZ];
174         snprintf(cmd, sizeof cmd, "MESG %s", name);
175         serv_puts(cmd);
176         serv_gets(cmd);
177         if (cmd[0] != '1') {
178                 printf("%s\n", &cmd[4]);
179                 return;
180         }
181         fmout(screenwidth, NULL,
182               ((userflags & US_PAGINATOR) ? 1 : 0),
183               screenheight, 1, 1);
184 }
185
186
187 void userlist(char *patn)
188 {
189         char buf[SIZ];
190         char fl[SIZ];
191         struct tm *tmbuf;
192         time_t lc;
193
194         serv_puts("LIST");
195         serv_gets(buf);
196         if (buf[0] != '1') {
197                 pprintf("%s\n", &buf[4]);
198                 return;
199         }
200         pprintf("       User Name           Num  L  LastCall  Calls Posts\n");
201         pprintf("------------------------- ----- - ---------- ----- -----\n");
202         while (serv_gets(buf), strcmp(buf, "000")) {
203                 if (sigcaught == 0) {
204                     extract(fl, buf, 0);
205                     if (pattern(fl, patn) >= 0) {
206                         pprintf("%-25s ", fl);
207                         pprintf("%5ld %d ", extract_long(buf, 2),
208                                extract_int(buf, 1));
209                         lc = extract_long(buf, 3);
210                         tmbuf = (struct tm *) localtime(&lc);
211                         pprintf("%02d/%02d/%04d ",
212                                (tmbuf->tm_mon + 1),
213                                tmbuf->tm_mday,
214                                (tmbuf->tm_year + 1900));
215                         pprintf("%5ld %5ld\n", extract_long(buf, 4), extract_long(buf, 5));
216                     }
217
218                 }
219         }
220         pprintf("\n");
221 }
222
223
224 /*
225  * grab assorted info about the user...
226  */
227 void load_user_info(char *params)
228 {
229         extract(fullname, params, 0);
230         axlevel = extract_int(params, 1);
231         timescalled = extract_int(params, 2);
232         posted = extract_int(params, 3);
233         userflags = extract_int(params, 4);
234         usernum = extract_long(params, 5);
235 }
236
237
238 /*
239  * Remove a room from the march list.  'floornum' is ignored unless
240  * 'roomname' is set to _FLOOR_, in which case all rooms on the requested
241  * floor will be removed from the march list.
242  */
243 void remove_march(char *roomname, int floornum)
244 {
245         struct march *mptr, *mptr2;
246
247         if (march == NULL)
248                 return;
249
250         if ((!strcasecmp(march->march_name, roomname))
251             || ((!strcasecmp(roomname, "_FLOOR_")) && (march->march_floor == floornum))) {
252                 mptr = march->next;
253                 free(march);
254                 march = mptr;
255                 return;
256         }
257         mptr2 = march;
258         for (mptr = march; mptr != NULL; mptr = mptr->next) {
259
260                 if ((!strcasecmp(mptr->march_name, roomname))
261                     || ((!strcasecmp(roomname, "_FLOOR_"))
262                         && (mptr->march_floor == floornum))) {
263
264                         mptr2->next = mptr->next;
265                         free(mptr);
266                         mptr = mptr2;
267                 } else {
268                         mptr2 = mptr;
269                 }
270         }
271 }
272
273
274 /*
275  * Locate the room on the march list which we most want to go to.  Each room
276  * is measured given a "weight" of preference based on various factors.
277  */
278 char *pop_march(int desired_floor)
279 {
280         static char TheRoom[ROOMNAMELEN];
281         int TheFloor = 0;
282         int TheOrder = 32767;
283         int TheWeight = 0;
284         int weight;
285         struct march *mptr = NULL;
286
287         strcpy(TheRoom, "_BASEROOM_");
288         if (march == NULL)
289                 return (TheRoom);
290
291         for (mptr = march; mptr != NULL; mptr = mptr->next) {
292                 weight = 0;
293                 if ((strcasecmp(mptr->march_name, "_BASEROOM_")))
294                         weight = weight + 10000;
295                 if (mptr->march_floor == desired_floor)
296                         weight = weight + 5000;
297
298                 weight = weight + ((128 - (mptr->march_floor)) * 128);
299                 weight = weight + (128 - (mptr->march_order));
300
301                 if (weight > TheWeight) {
302                         TheWeight = weight;
303                         strcpy(TheRoom, mptr->march_name);
304                         TheFloor = mptr->march_floor;
305                         TheOrder = mptr->march_order;
306                 }
307         }
308         return (TheRoom);
309 }
310
311
312 /*
313  * jump directly to a room
314  */
315 void dotgoto(char *towhere, int display_name)
316 {
317         char aaa[SIZ], bbb[SIZ], psearch[SIZ];
318         static long ls = 0L;
319         int newmailcount;
320         static int oldmailcount = (-1);
321         int partial_match, best_match;
322         char from_floor;
323
324         /* store ungoto information */
325         strcpy(ugname, room_name);
326         uglsn = ls;
327
328         /* first try an exact match */
329         snprintf(aaa, sizeof aaa, "GOTO %s", towhere);
330         serv_puts(aaa);
331         serv_gets(aaa);
332         if (aaa[3] == '*')
333                 express_msgs = 1;
334         if (!strncmp(aaa, "54", 2)) {
335                 newprompt("Enter room password: ", bbb, 9);
336                 snprintf(aaa, sizeof aaa, "GOTO %s|%s", towhere, bbb);
337                 serv_puts(aaa);
338                 serv_gets(aaa);
339                 if (aaa[3] == '*')
340                         express_msgs = 1;
341         }
342         if (!strncmp(aaa, "54", 2)) {
343                 printf("Wrong password.\n");
344                 return;
345         }
346         /*
347          * If a match is not found, try a partial match.
348          * Partial matches anywhere in the string carry a weight of 1,
349          * left-aligned matches carry a weight of 2.  Pick the room that
350          * has the highest-weighted match.
351          */
352         if (aaa[0] != '2') {
353                 best_match = 0;
354                 strcpy(bbb, "");
355                 serv_puts("LKRA");
356                 serv_gets(aaa);
357                 if (aaa[0] == '1')
358                         while (serv_gets(aaa), strcmp(aaa, "000")) {
359                                 extract(psearch, aaa, 0);
360                                 partial_match = 0;
361                                 if (pattern(psearch, towhere) >= 0) {
362                                         partial_match = 1;
363                                 }
364                                 if (!strncasecmp(towhere, psearch, strlen(towhere))) {
365                                         partial_match = 2;
366                                 }
367                                 if (partial_match > best_match) {
368                                         strcpy(bbb, psearch);
369                                         best_match = partial_match;
370                                 }
371                         }
372                 if (strlen(bbb) == 0) {
373                         printf("No room '%s'.\n", towhere);
374                         return;
375                 }
376                 snprintf(aaa, sizeof aaa, "GOTO %s", bbb);
377                 serv_puts(aaa);
378                 serv_gets(aaa);
379                 if (aaa[3] == '*')
380                         express_msgs = 1;
381         }
382         if (aaa[0] != '2') {
383                 printf("%s\n", aaa);
384                 return;
385         }
386         extract(room_name, &aaa[4], 0);
387         room_flags = extract_int(&aaa[4], 4);
388         from_floor = curr_floor;
389         curr_floor = extract_int(&aaa[4], 10);
390
391         remove_march(room_name, 0);
392         if (!strcasecmp(towhere, "_BASEROOM_"))
393                 remove_march(towhere, 0);
394         if ((from_floor != curr_floor) && (display_name > 0) && (floor_mode == 1)) {
395                 if (floorlist[(int) curr_floor][0] == 0)
396                         load_floorlist();
397                 printf("(Entering floor: %s)\n", &floorlist[(int) curr_floor][0]);
398         }
399         if (display_name == 1) {
400                 color(BRIGHT_WHITE);
401                 printf("%s ", room_name);
402                 color(DIM_WHITE);
403                 printf("- ");
404         }
405         if (display_name != 2) {
406                 color(BRIGHT_YELLOW);
407                 printf("%d ", extract_int(&aaa[4], 1));
408                 color(DIM_WHITE);
409                 printf("new of ");
410                 color(BRIGHT_YELLOW);
411                 printf("%d ", extract_int(&aaa[4], 2));
412                 color(DIM_WHITE);
413                 printf("messages.\n");
414         }
415         highest_msg_read = extract_int(&aaa[4], 6);
416         maxmsgnum = extract_int(&aaa[4], 5);
417         is_mail = (char) extract_int(&aaa[4], 7);
418         is_room_aide = (char) extract_int(&aaa[4], 8);
419         ls = extract_long(&aaa[4], 6);
420
421         /* read info file if necessary */
422         if (extract_int(&aaa[4], 3) > 0)
423                 readinfo();
424
425         /* check for newly arrived mail if we can */
426         if (num_parms(&aaa[4]) >= 10) {
427                 newmailcount = extract_int(&aaa[4], 9);
428                 if ((oldmailcount >= 0) && (newmailcount > oldmailcount)) {
429                         color(BRIGHT_RED);
430                         printf("*** You have new mail\n");
431                         color(DIM_WHITE);
432                 }
433                 oldmailcount = newmailcount;
434         }
435 }
436
437 /* Goto next room having unread messages.
438  * We want to skip over rooms that the user has already been to, and take the
439  * user back to the lobby when done.  The room we end up in is placed in
440  * newroom - which is set to 0 (the lobby) initially.
441  */
442 void gotonext(void)
443 {
444         char buf[SIZ];
445         struct march *mptr, *mptr2;
446         char next_room[ROOMNAMELEN];
447
448         /* Check to see if the march-mode list is already allocated.
449          * If it is, pop the first room off the list and go there.
450          */
451         if (march == NULL) {
452                 serv_puts("LKRN");
453                 serv_gets(buf);
454                 if (buf[0] == '1')
455                         while (serv_gets(buf), strcmp(buf, "000")) {
456                                 mptr = (struct march *) malloc(sizeof(struct march));
457                                 mptr->next = NULL;
458                                 extract(mptr->march_name, buf, 0);
459                                 mptr->march_floor = (char) (extract_int(buf, 2) & 0x7F);
460                                 mptr->march_order = (char) (extract_int(buf, 3) & 0x7F);
461                                 if (march == NULL) {
462                                         march = mptr;
463                                 } else {
464                                         mptr2 = march;
465                                         while (mptr2->next != NULL)
466                                                 mptr2 = mptr2->next;
467                                         mptr2->next = mptr;
468                                 }
469                         }
470 /* add _BASEROOM_ to the end of the march list, so the user will end up
471  * in the system base room (usually the Lobby>) at the end of the loop
472  */
473                 mptr = (struct march *) malloc(sizeof(struct march));
474                 mptr->next = NULL;
475                 strcpy(mptr->march_name, "_BASEROOM_");
476                 if (march == NULL) {
477                         march = mptr;
478                 } else {
479                         mptr2 = march;
480                         while (mptr2->next != NULL)
481                                 mptr2 = mptr2->next;
482                         mptr2->next = mptr;
483                 }
484 /*
485  * ...and remove the room we're currently in, so a <G>oto doesn't make us
486  * walk around in circles
487  */
488                 remove_march(room_name, 0);
489         }
490         if (march != NULL) {
491                 strcpy(next_room, pop_march(curr_floor));
492         } else {
493                 strcpy(next_room, "_BASEROOM_");
494         }
495         remove_march(next_room, 0);
496         dotgoto(next_room, 1);
497 }
498
499 /*
500  * forget all rooms on a given floor
501  */
502 void forget_all_rooms_on(int ffloor)
503 {
504         char buf[SIZ];
505         struct march *flist, *fptr;
506
507         printf("Forgetting all rooms on %s...\r", &floorlist[ffloor][0]);
508         fflush(stdout);
509         snprintf(buf, sizeof buf, "LKRA %d", ffloor);
510         serv_puts(buf);
511         serv_gets(buf);
512         if (buf[0] != '1') {
513                 printf("%-72s\n", &buf[4]);
514                 return;
515         }
516         flist = NULL;
517         while (serv_gets(buf), strcmp(buf, "000")) {
518                 fptr = (struct march *) malloc(sizeof(struct march));
519                 fptr->next = flist;
520                 flist = fptr;
521                 extract(fptr->march_name, buf, 0);
522         }
523         while (flist != NULL) {
524                 snprintf(buf, sizeof buf, "GOTO %s", flist->march_name);
525                 serv_puts(buf);
526                 serv_gets(buf);
527                 if (buf[0] == '2') {
528                         serv_puts("FORG");
529                         serv_gets(buf);
530                 }
531                 fptr = flist;
532                 flist = flist->next;
533                 free(fptr);
534         }
535         printf("%-72s\r", "");
536 }
537
538
539 /*
540  * routine called by gotofloor() to move to a new room on a new floor
541  */
542 void gf_toroom(char *towhere, int mode)
543 {
544         int floor_being_left;
545
546         floor_being_left = curr_floor;
547
548         if (mode == GF_GOTO) {  /* <;G>oto mode */
549                 updatels();
550                 dotgoto(towhere, 1);
551         }
552         if (mode == GF_SKIP) {  /* <;S>kip mode */
553                 dotgoto(towhere, 1);
554                 remove_march("_FLOOR_", floor_being_left);
555         }
556         if (mode == GF_ZAP) {   /* <;Z>ap mode */
557                 dotgoto(towhere, 1);
558                 remove_march("_FLOOR_", floor_being_left);
559                 forget_all_rooms_on(floor_being_left);
560         }
561 }
562
563
564 /*
565  * go to a new floor
566  */
567 void gotofloor(char *towhere, int mode)
568 {
569         int a, tofloor;
570         struct march *mptr;
571         char buf[SIZ], targ[SIZ];
572
573         if (floorlist[0][0] == 0)
574                 load_floorlist();
575         tofloor = (-1);
576         for (a = 0; a < 128; ++a)
577                 if (!strcasecmp(&floorlist[a][0], towhere))
578                         tofloor = a;
579
580         if (tofloor < 0) {
581                 for (a = 0; a < 128; ++a) {
582                         if (!strncasecmp(&floorlist[a][0], towhere, strlen(towhere))) {
583                                 tofloor = a;
584                         }
585                 }
586         }
587         if (tofloor < 0) {
588                 for (a = 0; a < 128; ++a)
589                         if (pattern(towhere, &floorlist[a][0]) > 0)
590                                 tofloor = a;
591         }
592         if (tofloor < 0) {
593                 printf("No floor '%s'.\n", towhere);
594                 return;
595         }
596         for (mptr = march; mptr != NULL; mptr = mptr->next) {
597                 if ((mptr->march_floor) == tofloor)
598                         gf_toroom(mptr->march_name, mode);
599                 return;
600         }
601
602         strcpy(targ, "");
603         snprintf(buf, sizeof buf, "LKRA %d", tofloor);
604         serv_puts(buf);
605         serv_gets(buf);
606         if (buf[0] == '1')
607                 while (serv_gets(buf), strcmp(buf, "000")) {
608                         if ((extract_int(buf, 2) == tofloor) && (strlen(targ) == 0))
609                                 extract(targ, buf, 0);
610                 }
611         if (strlen(targ) > 0) {
612                 gf_toroom(targ, mode);
613         } else {
614                 printf("There are no rooms on '%s'.\n", &floorlist[tofloor][0]);
615         }
616 }
617
618
619 /*
620  * forget all rooms on current floor
621  */
622 void forget_this_floor(void)
623 {
624
625         if (curr_floor == 0) {
626                 printf("Can't forget this floor.\n");
627                 return;
628         }
629         if (floorlist[0][0] == 0)
630                 load_floorlist();
631         printf("Are you sure you want to forget all rooms on %s? ",
632                &floorlist[(int) curr_floor][0]);
633         if (yesno() == 0)
634                 return;
635
636         gf_toroom("_BASEROOM_", GF_ZAP);
637 }
638
639
640 /* 
641  * Figure out the physical screen dimensions, if we can
642  */
643 void check_screen_dims(void)
644 {
645 #ifdef TIOCGWINSZ
646         struct {
647                 unsigned short height;  /* rows */
648                 unsigned short width;   /* columns */
649                 unsigned short xpixels;
650                 unsigned short ypixels;         /* pixels */
651         } xwinsz;
652
653         if (have_xterm) {       /* dynamically size screen if on an xterm */
654                 if (ioctl(0, TIOCGWINSZ, &xwinsz) == 0) {
655                         if (xwinsz.height)
656                                 screenheight = (int) xwinsz.height;
657                         if (xwinsz.width)
658                                 screenwidth = (int) xwinsz.width;
659                 }
660         }
661 #endif
662 }
663
664
665 /*
666  * set floor mode depending on client, server, and user settings
667  */
668 void set_floor_mode(void)
669 {
670         if (serv_info.serv_ok_floors == 0) {
671                 floor_mode = 0; /* Don't use floors if the server */
672         }
673         /* doesn't support them!          */
674         else {
675                 if (rc_floor_mode == RC_NO) {   /* never use floors */
676                         floor_mode = 0;
677                 }
678                 if (rc_floor_mode == RC_YES) {  /* always use floors */
679                         floor_mode = 1;
680                 }
681                 if (rc_floor_mode == RC_DEFAULT) {      /* user choice */
682                         floor_mode = ((userflags & US_FLOORS) ? 1 : 0);
683                 }
684         }
685 }
686
687 /*
688  * Set or change the user's password
689  */
690 int set_password(void)
691 {
692         char pass1[20];
693         char pass2[20];
694         char buf[SIZ];
695
696         if (strlen(rc_password) > 0) {
697                 strcpy(pass1, rc_password);
698                 strcpy(pass2, rc_password);
699         } else {
700                 IFNEXPERT formout("changepw");
701                 newprompt("Enter a new password: ", pass1, -19);
702                 newprompt("Enter it again to confirm: ", pass2, -19);
703         }
704         strproc(pass1);
705         strproc(pass2);
706         if (!strcasecmp(pass1, pass2)) {
707                 snprintf(buf, sizeof buf, "SETP %s", pass1);
708                 serv_puts(buf);
709                 serv_gets(buf);
710                 printf("%s\n", &buf[4]);
711                 offer_to_remember_password(hostbuf, portbuf, fullname, pass1);
712                 return (0);
713         } else {
714                 printf("*** They don't match... try again.\n");
715                 return (1);
716         }
717 }
718
719
720
721 /*
722  * get info about the server we've connected to
723  */
724 void get_serv_info(char *supplied_hostname)
725 {
726         char buf[512];
727
728         CtdlInternalGetServInfo(&serv_info);
729
730         /* be nice and identify ourself to the server */
731         snprintf(buf, sizeof buf, "IDEN %d|%d|%d|%s|",
732                  SERVER_TYPE, 0, REV_LEVEL,
733                  (server_is_local ? "local" : CITADEL));
734
735         /* Append a hostname */
736         if (supplied_hostname != NULL) {
737                 strcat(buf, supplied_hostname);
738         }
739         else {
740                 locate_host(&buf[strlen(buf)]); /* append to the end */
741         }
742
743         serv_puts(buf);
744         serv_gets(buf);         /* we don't care about the result code */
745 }
746
747
748
749
750
751 /*
752  * Display list of users currently logged on to the server
753  */
754 void who_is_online(int longlist)
755 {
756         char buf[SIZ], username[SIZ], roomname[SIZ], fromhost[SIZ];
757         char flags[SIZ];
758         char actual_user[SIZ], actual_room[SIZ], actual_host[SIZ];
759         char tbuf[SIZ], clientsoft[SIZ];
760         time_t timenow = 0;
761         time_t idletime, idlehours, idlemins, idlesecs;
762         int last_session = (-1);
763
764         serv_puts("TIME");
765         serv_gets(tbuf);
766         if (tbuf[0] == '2') {
767                 timenow = extract_long(&tbuf[4], 0);
768         }
769         else {
770                 time(&timenow);
771         }
772
773         if (!longlist) {
774                 color(BRIGHT_WHITE);
775                 pprintf("FLG ###        User Name                 Room                 From host\n");
776                 color(DIM_WHITE);
777                 pprintf("--- --- ------------------------- -------------------- ------------------------\n");
778         }
779         serv_puts("RWHO");
780         serv_gets(buf);
781         if (buf[0] == '1') {
782                 while (serv_gets(buf), strcmp(buf, "000")) {
783                         extract(username, buf, 1);
784                         extract(roomname, buf, 2);
785                         extract(fromhost, buf, 3);
786                         extract(clientsoft, buf, 4);
787                         extract(flags, buf, 7);
788
789                         idletime = timenow - extract_long(buf, 5);
790                         idlehours = idletime / 3600;
791                         idlemins = (idletime - (idlehours * 3600)) / 60;
792                         idlesecs = (idletime - (idlehours * 3600) - (idlemins * 60));
793
794                         if (idletime > 900) {
795                                 while (strlen(roomname) < 20) {
796                                         strcat(roomname, " ");
797                                 }
798                                 strcpy(&roomname[14], "[idle]");
799                         }
800
801                         if (longlist) {
802
803                                 extract(actual_user, buf, 8);
804                                 extract(actual_room, buf, 9);
805                                 extract(actual_host, buf, 10);
806
807                                 pprintf("  Flags: %s\n", flags);
808                                 pprintf("Session: %d\n", extract_int(buf, 0));
809                                 pprintf("   Name: %s\n", username);
810                                 pprintf("In room: %s\n", roomname);
811                                 pprintf("   Host: %s\n", fromhost);
812                                 pprintf(" Client: %s\n", clientsoft);
813                                 pprintf("   Idle: %ld:%02ld:%02ld\n",
814                                         (long) idlehours,
815                                         (long) idlemins,
816                                         (long) idlesecs);
817
818                                 if ( (strlen(actual_user)+strlen(actual_room)+strlen(actual_host)) > 0) {
819                                         pprintf("(really ");
820                                         if (strlen(actual_user)>0) pprintf("<%s> ", actual_user);
821                                         if (strlen(actual_room)>0) pprintf("in <%s> ", actual_room);
822                                         if (strlen(actual_host)>0) pprintf("from <%s> ", actual_host);
823                                         pprintf(")\n");
824                                 }
825                                 pprintf("\n");
826
827                         } else {
828                                 if (extract_int(buf, 0) == last_session) {
829                                         pprintf("        ");
830                                 } else {
831                                         color(BRIGHT_MAGENTA);
832                                         pprintf("%-3s ", flags);
833                                         color(DIM_WHITE);
834                                         pprintf("%-3d ", extract_int(buf, 0));
835                                 }
836                                 last_session = extract_int(buf, 0);
837                                 color(BRIGHT_CYAN);
838                                 pprintf("%-25s ", username);
839                                 color(BRIGHT_MAGENTA);
840                                 roomname[20] = 0;
841                                 pprintf("%-20s ", roomname);
842                                 color(BRIGHT_CYAN);
843                                 pprintf("%-24s\n", fromhost);
844                                 color(DIM_WHITE);
845                         }
846                 }
847         }
848 }
849
850 void enternew(char *desc, char *buf, int maxlen)
851 {
852         char bbb[128];
853         snprintf(bbb, sizeof bbb, "Enter in your new %s: ", desc);
854         newprompt(bbb, buf, maxlen);
855 }
856
857
858
859 int shift(int argc, char **argv, int start, int count) {
860         int i;
861
862         for (i=start; i<(argc-count); ++i) {
863                 argv[i] = argv[i+count];
864         }
865         argc = argc - count;
866         return argc;
867 }
868
869 /*
870  * main
871  */
872 int main(int argc, char **argv)
873 {
874         int a, b, mcmd;
875         char aaa[100], bbb[100];/* general purpose variables */
876         char argbuf[32];        /* command line buf */
877         char nonce[NONCE_SIZE];
878         char *telnet_client_host = NULL;
879         char *sptr, *sptr2;     /* USed to extract the nonce */
880         char hexstring[MD5_HEXSTRING_SIZE];
881         int stored_password = 0;
882         char password[SIZ];
883
884         sttybbs(SB_SAVE);       /* Store the old terminal parameters */
885         load_command_set();     /* parse the citadel.rc file */
886         sttybbs(SB_NO_INTR);    /* Install the new ones */
887         signal(SIGHUP, dropcarr);       /* Cleanup gracefully if carrier is dropped */
888         signal(SIGTERM, dropcarr);      /* Cleanup gracefully if terminated */
889         signal(SIGCONT, catch_sigcont);         /* Catch SIGCONT so we can reset terminal */
890
891         /* 
892          * Handle command line options as if we were called like /bin/login
893          * (i.e. from in.telnetd)
894          */
895         for (a=0; a<argc; ++a) {
896                 if ((argc > a+1) && (!strcmp(argv[a], "-h")) ) {
897                         telnet_client_host = argv[a+1];
898                         argc = shift(argc, argv, a, 2);
899                 }
900                 if (!strcmp(argv[a], "-p")) {
901                         argc = shift(argc, argv, a, 1);
902                 }
903         }
904
905         printf("Attaching to server... \r");
906         fflush(stdout);
907         attach_to_server(argc, argv, hostbuf, portbuf);
908
909         send_ansi_detect();
910
911         serv_gets(aaa);
912         if (aaa[0] != '2') {
913                 printf("%s\n", &aaa[4]);
914                 logoff(atoi(aaa));
915         }
916
917 /* If there is a [nonce] at the end, put the nonce in <nonce>, else nonce
918  * is zeroized.
919  */
920         
921         if ((sptr = strchr(aaa, '<')) == NULL)
922         {
923            nonce[0] = '\0';
924         }
925         else
926         {
927            if ((sptr2 = strchr(sptr, '>')) == NULL)
928            {
929               nonce[0] = '\0';
930            }
931            else
932            {
933               sptr2++;
934               *sptr2 = '\0';
935               strncpy(nonce, sptr, NONCE_SIZE);
936            }
937         }
938         
939         get_serv_info(telnet_client_host);
940
941         look_for_ansi();
942         cls(0);
943         color(7);
944
945         printf("%-23s\n%s\n%s\n", serv_info.serv_software, serv_info.serv_humannode,
946                serv_info.serv_bbs_city);
947         screenwidth = 80;       /* default screen dimensions */
948         screenheight = 24;
949
950         printf(" pause    next    stop\n");
951         printf(" ctrl-s  ctrl-o  ctrl-c\n\n");
952         formout("hello");       /* print the opening greeting */
953         printf("\n");
954
955 GSTA:   /* See if we have a username and password on disk */
956         if (rc_remember_passwords) {
957                 get_stored_password(hostbuf, portbuf, fullname, password);
958                 if (strlen(fullname) > 0) {
959                         snprintf(aaa, sizeof(aaa)-1, "USER %s", fullname);
960                         serv_puts(aaa);
961                         serv_gets(aaa);
962                         if (nonce[0])
963                         {
964                                 sprintf(aaa, "PAS2 %s", make_apop_string(password, nonce, hexstring));
965                         }
966                         else    /* Else no APOP */
967                         {
968                                 snprintf(aaa, sizeof(aaa)-1, "PASS %s", password);
969                         }
970                         
971                         serv_puts(aaa);
972                         serv_gets(aaa);
973                         if (aaa[0] == '2') {
974                                 load_user_info(&aaa[4]);
975                                 stored_password = 1;
976                                 goto PWOK;
977                         }
978                         else {
979                                 set_stored_password(hostbuf, portbuf, "", "");
980                         }
981                 }
982         }
983
984         termn8 = 0;
985         newnow = 0;
986         do {
987                 if (strlen(rc_username) > 0) {
988                         strcpy(fullname, rc_username);
989                 } else {
990                         newprompt("Enter your name: ", fullname, 29);
991                 }
992                 strproc(fullname);
993                 if (!strcasecmp(fullname, "new")) {     /* just in case */
994                         printf("Please enter the name you wish to log in with.\n");
995                 }
996         } while (
997                         (!strcasecmp(fullname, "bbs"))
998                         || (!strcasecmp(fullname, "new"))
999                         || (strlen(fullname) == 0));
1000
1001         if (!strcasecmp(fullname, "off")) {
1002                 mcmd = 29;
1003                 goto TERMN8;
1004         }
1005         /* sign on to the server */
1006         snprintf(aaa, sizeof aaa, "USER %s", fullname);
1007         serv_puts(aaa);
1008         serv_gets(aaa);
1009         if (aaa[0] != '3')
1010                 goto NEWUSR;
1011
1012         /* password authentication */
1013         if (strlen(rc_password) > 0) {
1014                 strcpy(password, rc_password);
1015         } else {
1016                 newprompt("\rPlease enter your password: ", password, -19);
1017         }
1018         strproc(password);
1019
1020         if (nonce[0])
1021         {
1022                 sprintf(aaa, "PAS2 %s", make_apop_string(password, nonce, hexstring));
1023         }
1024         else    /* Else no APOP */
1025         {
1026                         snprintf(aaa, sizeof(aaa)-1, "PASS %s", password);
1027         }
1028         
1029         serv_puts(aaa);
1030         serv_gets(aaa);
1031         if (aaa[0] == '2') {
1032                 load_user_info(&aaa[4]);
1033                 offer_to_remember_password(hostbuf, portbuf,
1034                                         fullname, password);
1035                 goto PWOK;
1036         }
1037         printf("<< wrong password >>\n");
1038         if (strlen(rc_password) > 0)
1039                 logoff(0);
1040         goto GSTA;
1041
1042 NEWUSR: if (strlen(rc_password) == 0) {
1043                 printf("No record. Enter as new user? ");
1044                 if (yesno() == 0)
1045                         goto GSTA;
1046         }
1047         snprintf(aaa, sizeof aaa, "NEWU %s", fullname);
1048         serv_puts(aaa);
1049         serv_gets(aaa);
1050         if (aaa[0] != '2') {
1051                 printf("%s\n", aaa);
1052                 goto GSTA;
1053         }
1054         load_user_info(&aaa[4]);
1055
1056         while (set_password() != 0);;
1057         newnow = 1;
1058
1059         enter_config(1);
1060
1061 PWOK:
1062         /* Switch color support on or off if we're in user mode */
1063         if (rc_ansi_color == 3) {
1064                 if (userflags & US_COLOR)
1065                         enable_color = 1;
1066                 else
1067                         enable_color = 0;
1068         }
1069
1070         printf("%s\nAccess level: %d (%s)\nUser #%ld / Call #%d\n",
1071                fullname, axlevel, axdefs[(int) axlevel],
1072                usernum, timescalled);
1073
1074         serv_puts("CHEK");
1075         serv_gets(aaa);
1076         if (aaa[0] == '2') {
1077                 b = extract_int(&aaa[4], 0);
1078                 if (b > 0) {
1079                         color(BRIGHT_RED);
1080                         if (b == 1)
1081                                 printf("*** You have a new private message in Mail>\n");
1082                         if (b > 1)
1083                                 printf("*** You have %d new private messages in Mail>\n", b);
1084                         color(DIM_WHITE);
1085                 }
1086                 if ((axlevel >= 6) && (extract_int(&aaa[4], 2) > 0)) {
1087                         printf("*** Users need validation\n");
1088                 }
1089                 if (extract_int(&aaa[4], 1) > 0) {
1090                         printf("*** Please register.\n");
1091                         formout("register");
1092                         entregis();
1093                 }
1094         }
1095         /* Make up some temporary filenames for use in various parts of the
1096          * program.  Don't mess with these once they've been set, because we
1097          * will be unlinking them later on in the program and we don't
1098          * want to delete something that we didn't create. */
1099         snprintf(temp, sizeof temp, tmpnam(NULL));
1100         snprintf(temp2, sizeof temp2, tmpnam(NULL));
1101         snprintf(tempdir, sizeof tempdir, tmpnam(NULL));
1102
1103         /* Get screen dimensions.  First we go to a default of 80x24.  Then
1104          * we try to get the user's actual screen dimensions off the server.
1105          * However, if we're running on an xterm, all this stuff is
1106          * irrelevant because we're going to dynamically size the screen
1107          * during the session.
1108          */
1109         screenwidth = 80;
1110         screenheight = 24;
1111         serv_puts("GETU");
1112         serv_gets(aaa);
1113         if (aaa[0] == '2') {
1114                 screenwidth = extract_int(&aaa[4], 0);
1115                 screenheight = extract_int(&aaa[4], 1);
1116         }
1117         if (getenv("TERM") != NULL)
1118                 if (!strcmp(getenv("TERM"), "xterm")) {
1119                         have_xterm = 1;
1120                 }
1121 #ifdef TIOCGWINSZ
1122         check_screen_dims();
1123 #endif
1124
1125         set_floor_mode();
1126
1127
1128         /* Enter the lobby */
1129         dotgoto("_BASEROOM_", 1);
1130
1131 /* Main loop for the system... user is logged in. */
1132         strcpy(ugname, "");
1133         uglsn = 0L;
1134
1135         if (newnow == 1)
1136                 readmsgs(3, 1, 5);
1137         else
1138                 readmsgs(1, 1, 0);
1139
1140         /* MAIN COMMAND LOOP */
1141         do {
1142                 mcmd = getcmd(argbuf);          /* Get keyboard command */
1143
1144 #ifdef TIOCGWINSZ
1145                 check_screen_dims();            /* if xterm, get screen size */
1146 #endif
1147
1148                 if (termn8 == 0)
1149                         switch (mcmd) {
1150                         case 1:
1151                                 formout("help");
1152                                 break;
1153                         case 4:
1154                                 entmsg(0, 0);
1155                                 break;
1156                         case 36:
1157                                 entmsg(0, 1);
1158                                 break;
1159                         case 46:
1160                                 entmsg(0, 2);
1161                                 break;
1162                         case 78:
1163                                 newprompt("What do you want your username to be? ", aaa, 32);
1164                                 snprintf(bbb, sizeof bbb, "ENT0 2|0|0|0|%s", aaa);
1165                                 serv_puts(bbb);
1166                                 serv_gets(aaa);
1167                                 if (strncmp("200", aaa, 3))
1168                                         printf("\n%s\n", aaa);
1169                                 else
1170                                         entmsg(0, 0);
1171                                 break;
1172                         case 5:
1173                                 updatels();
1174                                 gotonext();
1175                                 break;
1176                         case 47:
1177                                 updatelsa();
1178                                 gotonext();
1179                                 break;
1180                         case 58:
1181                                 updatelsa();
1182                                 dotgoto("_MAIL_", 1);
1183                                 break;
1184                         case 20:
1185                                 updatels();
1186                         case 52:
1187                                 dotgoto(argbuf, 0);
1188                                 break;
1189                         case 10:
1190                                 readmsgs(0, 1, 0);
1191                                 break;
1192                         case 9:
1193                                 readmsgs(3, 1, 5);
1194                                 break;
1195                         case 13:
1196                                 readmsgs(1, 1, 0);
1197                                 break;
1198                         case 11:
1199                                 readmsgs(0, (-1), 0);
1200                                 break;
1201                         case 12:
1202                                 readmsgs(2, (-1), 0);
1203                                 break;
1204                         case 71:
1205                                 readmsgs(3, 1, atoi(argbuf));
1206                                 break;
1207                         case 7:
1208                                 forget();
1209                                 break;
1210                         case 18:
1211                                 subshell();
1212                                 break;
1213                         case 38:
1214                                 updatels();
1215                                 entroom();
1216                                 break;
1217                         case 22:
1218                                 killroom();
1219                                 break;
1220                         case 32:
1221                                 userlist(argbuf);
1222                                 break;
1223                         case 27:
1224                                 invite();
1225                                 break;
1226                         case 28:
1227                                 kickout();
1228                                 break;
1229                         case 23:
1230                                 editthisroom();
1231                                 break;
1232                         case 14:
1233                                 roomdir();
1234                                 break;
1235                         case 33:
1236                                 download(0);
1237                                 break;
1238                         case 34:
1239                                 download(1);
1240                                 break;
1241                         case 31:
1242                                 download(2);
1243                                 break;
1244                         case 43:
1245                                 download(3);
1246                                 break;
1247                         case 45:
1248                                 download(4);
1249                                 break;
1250                         case 55:
1251                                 download(5);
1252                                 break;
1253                         case 39:
1254                                 upload(0);
1255                                 break;
1256                         case 40:
1257                                 upload(1);
1258                                 break;
1259                         case 42:
1260                                 upload(2);
1261                                 break;
1262                         case 44:
1263                                 upload(3);
1264                                 break;
1265                         case 57:
1266                                 cli_upload();
1267                                 break;
1268                         case 16:
1269                                 ungoto();
1270                                 break;
1271                         case 24:
1272                                 whoknows();
1273                                 break;
1274                         case 26:
1275                                 validate();
1276                                 break;
1277                         case 29:
1278                                 updatels();
1279                                 termn8 = 1;
1280                                 break;
1281                         case 30:
1282                                 updatels();
1283                                 termn8 = 1;
1284                                 break;
1285                         case 48:
1286                                 enterinfo();
1287                                 break;
1288                         case 49:
1289                                 readinfo();
1290                                 break;
1291                         case 72:
1292                                 cli_image_upload("_userpic_");
1293                                 break;
1294                         case 73:
1295                                 cli_image_upload("_roompic_");
1296                                 break;
1297
1298                         case 74:
1299                                 snprintf(aaa, sizeof aaa, "_floorpic_|%d", curr_floor);
1300                                 cli_image_upload(aaa);
1301                                 break;
1302
1303                         case 75:
1304                                 enternew("roomname", aaa, 20);
1305                                 snprintf(bbb, sizeof bbb, "RCHG %s", aaa);
1306                                 serv_puts(bbb);
1307                                 serv_gets(aaa);
1308                                 if (strncmp("200", aaa, 3))
1309                                         printf("\n%s\n", aaa);
1310                                 break;
1311                         case 76:
1312                                 enternew("hostname", aaa, 25);
1313                                 snprintf(bbb, sizeof bbb, "HCHG %s", aaa);
1314                                 serv_puts(bbb);
1315                                 serv_gets(aaa);
1316                                 if (strncmp("200", aaa, 3))
1317                                         printf("\n%s\n", aaa);
1318                                 break;
1319                         case 77:
1320                                 enternew("username", aaa, 32);
1321                                 snprintf(bbb, sizeof bbb, "UCHG %s", aaa);
1322                                 serv_puts(bbb);
1323                                 serv_gets(aaa);
1324                                 if (strncmp("200", aaa, 3))
1325                                         printf("\n%s\n", aaa);
1326                                 break;
1327
1328                         case 35:
1329                                 set_password();
1330                                 break;
1331
1332                         case 21:
1333                                 if (argbuf[0] == 0)
1334                                         strcpy(aaa, "?");
1335                                 display_help(argbuf);
1336                                 break;
1337
1338                         case 41:
1339                                 formout("register");
1340                                 entregis();
1341                                 break;
1342
1343                         case 15:
1344                                 printf("Are you sure (y/n)? ");
1345                                 if (yesno() == 1) {
1346                                         updatels();
1347                                         a = 0;
1348                                         termn8 = 1;
1349                                 }
1350                                 break;
1351
1352                         case 85:
1353                                 printf("All users will be disconnected!  "
1354                                         "Really terminate the server? ");
1355                                 if (yesno() == 1) {
1356                                         serv_puts("DOWN");
1357                                         serv_gets(aaa);
1358                                         printf("%s\n", &aaa[4]);
1359                                         if (aaa[0]=='2') {
1360                                                 updatels();
1361                                                 a = 0;
1362                                                 termn8 = 1;
1363                                         }
1364                                 }
1365
1366                         case 86:
1367                                 printf("Do you really want to schedule a "
1368                                         "server shutdown? ");
1369                                 if (yesno() == 1) {
1370                                         serv_puts("SCDN 1");
1371                                         serv_gets(aaa);
1372                                         if (aaa[0]=='2') {
1373                                                 if (atoi(&aaa[4])) {
1374                                                         printf(
1375 "The Citadel server will terminate when all users are logged off.\n"
1376                                                                 );
1377                                                 }
1378                                                 else {
1379                                                         printf(
1380 "The Citadel server will not terminate.\n"
1381                                                                 );
1382                                                 }
1383                                         }
1384                                 }
1385
1386                         case 6:
1387                                 gotonext();
1388                                 break;
1389
1390                         case 3:
1391                                 chatmode();
1392                                 break;
1393
1394                         case 2:
1395                                 if (server_is_local) {
1396                                         sttybbs(SB_RESTORE);
1397                                         snprintf(aaa, sizeof aaa, "USERNAME=\042%s\042; export USERNAME;"
1398                                                  "exec ./subsystem %ld %d %d", fullname,
1399                                           usernum, screenwidth, axlevel);
1400                                         ka_system(aaa);
1401                                         sttybbs(SB_NO_INTR);
1402                                 } else {
1403                                         printf("*** Can't run doors when server is not local.\n");
1404                                 }
1405                                 break;
1406
1407                         case 17:
1408                                 who_is_online(0);
1409                                 break;
1410
1411                         case 79:
1412                                 who_is_online(1);
1413                                 break;
1414
1415                         case 80:
1416                                 do_system_configuration();
1417                                 break;
1418
1419                         case 82:
1420                                 do_internet_configuration();
1421                                 break;
1422
1423                         case 83:
1424                                 check_message_base();
1425                                 break;
1426
1427                         case 84:
1428                                 quiet_mode();
1429                                 break;
1430
1431                         case 50:
1432                                 enter_config(2);
1433                                 break;
1434
1435                         case 37:
1436                                 enter_config(0);
1437                                 set_floor_mode();
1438                                 break;
1439
1440                         case 59:
1441                                 enter_config(3);
1442                                 set_floor_mode();
1443                                 break;
1444
1445                         case 60:
1446                                 gotofloor(argbuf, GF_GOTO);
1447                                 break;
1448
1449                         case 61:
1450                                 gotofloor(argbuf, GF_SKIP);
1451                                 break;
1452
1453                         case 62:
1454                                 forget_this_floor();
1455                                 break;
1456
1457                         case 63:
1458                                 create_floor();
1459                                 break;
1460
1461                         case 64:
1462                                 edit_floor();
1463                                 break;
1464
1465                         case 65:
1466                                 kill_floor();
1467                                 break;
1468
1469                         case 66:
1470                                 enter_bio();
1471                                 break;
1472
1473                         case 67:
1474                                 read_bio();
1475                                 break;
1476
1477                         case 25:
1478                                 edituser();
1479                                 break;
1480
1481                         case 8:
1482                                 knrooms(floor_mode);
1483                                 printf("\n");
1484                                 break;
1485
1486                         case 68:
1487                                 knrooms(2);
1488                                 printf("\n");
1489                                 break;
1490
1491                         case 69:
1492                                 misc_server_cmd(argbuf);
1493                                 break;
1494
1495                         case 70:
1496                                 edit_system_message(argbuf);
1497                                 break;
1498
1499                         case 19:
1500                                 listzrooms();
1501                                 printf("\n");
1502                                 break;
1503
1504                         case 51:
1505                                 deletefile();
1506                                 break;
1507
1508                         case 53:
1509                                 netsendfile();
1510                                 break;
1511
1512                         case 54:
1513                                 movefile();
1514                                 break;
1515
1516                         case 56:
1517                                 page_user();
1518                                 break;
1519
1520                         }       /* end switch */
1521         } while (termn8 == 0);
1522
1523 TERMN8: printf("%s logged out.\n", fullname);
1524         while (march != NULL) {
1525                 remove_march(march->march_name, 0);
1526         }
1527         if (mcmd == 30) {
1528                 printf("\n\nType 'off' to hang up, or next user...\n");
1529         }
1530         snprintf(aaa, sizeof aaa, "LOUT");
1531         serv_puts(aaa);
1532         serv_gets(aaa);
1533         if ((mcmd == 29) || (mcmd == 15)) {
1534                 formout("goodbye");
1535                 logoff(0);
1536         }
1537         goto GSTA;
1538
1539 }       /* end main() */