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