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