]> code.citadel.org Git - citadel.git/blob - citadel/citadel.c
* Wrote the client side of the ICQ gateway. Now on to other projects. :)
[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         if (!strucmp(pass1, pass2)) {
716                 snprintf(buf, sizeof buf, "SETP %s", pass1);
717                 serv_puts(buf);
718                 serv_gets(buf);
719                 printf("%s\n", &buf[4]);
720                 strcpy(re_password, pass1);
721                 return (0);
722         } else {
723                 printf("*** They don't match... try again.\n");
724                 return (1);
725         }
726 }
727
728
729
730 /*
731  * get info about the server we've connected to
732  */
733 void get_serv_info(void)
734 {
735         char buf[512];
736
737         CtdlInternalGetServInfo(&serv_info);
738
739         /* be nice and identify ourself to the server */
740         snprintf(buf, sizeof buf, "IDEN %d|%d|%d|%s|",
741                  SERVER_TYPE, 0, REV_LEVEL,
742                  (server_is_local ? "local" : CITADEL));
743         locate_host(&buf[strlen(buf)]);         /* append to the end */
744         serv_puts(buf);
745         serv_gets(buf);         /* we don't care about the result code */
746 }
747
748
749
750
751
752 /*
753  * Display list of users currently logged on to the server
754  */
755 void who_is_online(int longlist)
756 {
757         char buf[128], username[128], roomname[128], fromhost[128],
758          flags[128];
759         char tbuf[128], clientsoft[128];
760         time_t timenow = 0;
761         time_t idletime, idlehours, idlemins, idlesecs;
762         int last_session = (-1);
763
764         if (longlist) {
765                 serv_puts("TIME");
766                 serv_gets(tbuf);
767                 if (tbuf[0] == '2') {
768                         timenow = extract_long(&tbuf[4], 0);
769                 } else {
770                         time(&timenow);
771                 }
772         } else {
773                 color(BRIGHT_WHITE);
774                 printf("FLG ###        User Name                 Room                 From host\n");
775                 color(DIM_WHITE);
776                 printf("--- --- ------------------------- -------------------- ------------------------\n");
777         }
778         serv_puts("RWHO");
779         serv_gets(buf);
780         if (buf[0] == '1') {
781                 while (serv_gets(buf), strcmp(buf, "000")) {
782                         extract(username, buf, 1);
783                         extract(roomname, buf, 2);
784                         extract(fromhost, buf, 3);
785                         extract(clientsoft, buf, 4);
786                         extract(flags, buf, 7);
787
788                         if (longlist) {
789                                 idletime = timenow - extract_long(buf, 5);
790                                 idlehours = idletime / 3600;
791                                 idlemins = (idletime - (idlehours * 3600)) / 60;
792                                 idlesecs = (idletime - (idlehours * 3600) - (idlemins * 60));
793                                 printf("\nFlags: %-3s  Sess# %-3d  Name: %-25s  Room: %s\n",
794                                        flags, extract_int(buf, 0), username, roomname);
795                                 printf("from <%s> using <%s>, idle %ld:%02ld:%02ld\n",
796                                        fromhost, clientsoft,
797                                        (long) idlehours, (long) idlemins, (long) idlesecs);
798
799                         } else {
800                                 if (extract_int(buf, 0) == last_session) {
801                                         printf("        ");
802                                 } else {
803                                         color(BRIGHT_MAGENTA);
804                                         printf("%-3s ", flags);
805                                         color(DIM_WHITE);
806                                         printf("%-3d ", extract_int(buf, 0));
807                                 }
808                                 last_session = extract_int(buf, 0);
809                                 color(BRIGHT_CYAN);
810                                 printf("%-25s ", username);
811                                 color(BRIGHT_MAGENTA);
812                                 printf("%-20s ", roomname);
813                                 color(BRIGHT_CYAN);
814                                 printf("%-24s\n", fromhost);
815                                 color(DIM_WHITE);
816                         }
817                 }
818         }
819 }
820
821 void enternew(char *desc, char *buf, int maxlen)
822 {
823         char bbb[128];
824         snprintf(bbb, sizeof bbb, "Enter in your new %s: ", desc);
825         newprompt(bbb, buf, maxlen);
826 }
827
828 /*
829  * main
830  */
831 int main(int argc, char **argv)
832 {
833         int a, b, mcmd;
834         char aaa[100], bbb[100], eee[100];      /* general purpose variables */
835         char argbuf[32];        /* command line buf */
836         volatile int termn8 = 0;
837
838
839         sttybbs(SB_SAVE);       /* Store the old terminal parameters */
840         load_command_set();     /* parse the citadel.rc file */
841         sttybbs(SB_NO_INTR);    /* Install the new ones */
842         signal(SIGINT, SIG_IGN);
843         signal(SIGQUIT, SIG_IGN);
844         signal(SIGHUP, dropcarr);       /* Cleanup gracefully if carrier is dropped */
845         signal(SIGTERM, dropcarr);      /* Cleanup gracefully if terminated */
846         signal(SIGCONT, catch_sigcont);         /* Catch SIGCONT so we can reset terminal */
847
848         printf("Attaching to server...\r");
849         fflush(stdout);
850         attach_to_server(argc, argv);
851
852         send_ansi_detect();
853
854         serv_gets(aaa);
855         if (aaa[0] != '2') {
856                 printf("%s\n", &aaa[4]);
857                 logoff(atoi(aaa));
858         }
859         get_serv_info();
860
861         look_for_ansi();
862         cls(0);
863         color(7);
864
865         printf("%-22s\n%s\n%s\n", serv_info.serv_software, serv_info.serv_humannode,
866                serv_info.serv_bbs_city);
867         screenwidth = 80;       /* default screen dimensions */
868         screenheight = 24;
869
870         printf(" pause    next    stop\n");
871         printf(" ctrl-s  ctrl-o  ctrl-c\n\n");
872         formout("hello");       /* print the opening greeting */
873         printf("\n");
874
875 GSTA:   termn8 = 0;
876         newnow = 0;
877         do {
878                 if (strlen(rc_username) > 0) {
879                         strcpy(fullname, rc_username);
880                 } else {
881                         newprompt("Enter your name: ", fullname, 29);
882                 }
883                 strproc(fullname);
884                 if (!strucmp(fullname, "new")) {        /* just in case */
885                         printf("Please enter the name you wish to log in with.\n");
886                 }
887         } while (
888                         (!strucmp(fullname, "bbs"))
889                         || (!strucmp(fullname, "new"))
890                         || (strlen(fullname) == 0));
891
892         if (!strucmp(fullname, "off")) {
893                 mcmd = 29;
894                 goto TERMN8;
895         }
896         /* sign on to the server */
897         strcpy(re_username, fullname);
898         snprintf(aaa, sizeof aaa, "USER %s", fullname);
899         serv_puts(aaa);
900         serv_gets(aaa);
901         if (aaa[0] != '3')
902                 goto NEWUSR;
903
904         /* password authentication */
905         if (strlen(rc_password) > 0) {
906                 strcpy(eee, rc_password);
907         } else {
908                 newprompt("\rPlease enter your password: ", eee, -19);
909         }
910         strproc(eee);
911         snprintf(aaa, sizeof aaa, "PASS %s", eee);
912         serv_puts(aaa);
913         serv_gets(aaa);
914         if (aaa[0] == '2') {
915                 strcpy(re_password, eee);
916                 load_user_info(&aaa[4]);
917                 goto PWOK;
918         }
919         printf("<< wrong password >>\n");
920         if (strlen(rc_password) > 0)
921                 logoff(0);
922         goto GSTA;
923
924       NEWUSR:if (strlen(rc_password) == 0) {
925                 printf("No record. Enter as new user? ");
926                 if (yesno() == 0)
927                         goto GSTA;
928         }
929         snprintf(aaa, sizeof aaa, "NEWU %s", fullname);
930         serv_puts(aaa);
931         serv_gets(aaa);
932         if (aaa[0] != '2') {
933                 printf("%s\n", aaa);
934                 goto GSTA;
935         }
936         load_user_info(&aaa[4]);
937
938         while (set_password() != 0);;
939         newnow = 1;
940
941         enter_config(1);
942
943
944       PWOK:printf("%s\nAccess level: %d (%s)\nUser #%ld / Call #%d\n",
945                fullname, axlevel, axdefs[(int) axlevel],
946                usernum, timescalled);
947
948         serv_puts("CHEK");
949         serv_gets(aaa);
950         if (aaa[0] == '2') {
951                 b = extract_int(&aaa[4], 0);
952                 if (b > 0) {
953                         color(BRIGHT_RED);
954                         if (b == 1)
955                                 printf("*** You have a new private message in Mail>\n");
956                         if (b > 1)
957                                 printf("*** You have %d new private messages in Mail>\n", b);
958                         color(DIM_WHITE);
959                 }
960                 if ((axlevel >= 6) && (extract_int(&aaa[4], 2) > 0)) {
961                         printf("*** Users need validation\n");
962                 }
963                 if (extract_int(&aaa[4], 1) > 0) {
964                         printf("*** Please register.\n");
965                         formout("register");
966                         entregis();
967                 }
968         }
969         /* Make up some temporary filenames for use in various parts of the
970          * program.  Don't mess with these once they've been set, because we
971          * will be unlinking them later on in the program and we don't
972          * want to delete something that we didn't create. */
973         snprintf(temp, sizeof temp, tmpnam(NULL));
974         snprintf(temp2, sizeof temp2, tmpnam(NULL));
975         snprintf(tempdir, sizeof tempdir, tmpnam(NULL));
976
977         /* Get screen dimensions.  First we go to a default of 80x24.  Then
978          * we try to get the user's actual screen dimensions off the server.
979          * However, if we're running on an xterm, all this stuff is
980          * irrelevant because we're going to dynamically size the screen
981          * during the session.
982          */
983         screenwidth = 80;
984         screenheight = 24;
985         serv_puts("GETU");
986         serv_gets(aaa);
987         if (aaa[0] == '2') {
988                 screenwidth = extract_int(&aaa[4], 0);
989                 screenheight = extract_int(&aaa[4], 1);
990         }
991         if (getenv("TERM") != NULL)
992                 if (!strcmp(getenv("TERM"), "xterm")) {
993                         have_xterm = 1;
994                 }
995 #ifdef TIOCGWINSZ
996         check_screen_dims();
997 #endif
998
999         set_floor_mode();
1000
1001
1002         /* Enter the lobby */
1003         dotgoto("_BASEROOM_", 1);
1004
1005 /* Main loop for the system... user is logged in. */
1006         strcpy(ugname, "");
1007         uglsn = 0L;
1008
1009         if (newnow == 1)
1010                 readmsgs(3, 1, 5);
1011         else
1012                 readmsgs(1, 1, 0);
1013
1014         do {                    /* MAIN LOOP OF PROGRAM */
1015
1016                 if (!is_connected()) {
1017                         for (a=15; a>=0; --a) {
1018                                 printf("\rServer connection broken; "
1019                                         "will reconnect in %d seconds\r", a);
1020                                 fflush(stdout);
1021                                 sleep(1);
1022                         }
1023                         printf( "                                "
1024                                 "                                \r");
1025                         fflush(stdout);
1026                         attach_to_server(argc, argv);
1027                         printf("                                         \r");
1028                         fflush(stdout);
1029                         serv_gets(aaa);
1030                         if (aaa[0] != '2') {
1031                                 printf("%s\n", &aaa[4]);
1032                                 exit(0);
1033                         }
1034                         get_serv_info();
1035                         sprintf(aaa, "USER %s", re_username);
1036                         serv_puts(aaa);
1037                         serv_gets(aaa);
1038                         if (aaa[0] != '3') {
1039                                 printf("%s\n", &aaa[4]);
1040                                 exit(0);
1041                         }
1042                         sprintf(aaa, "PASS %s", re_password);
1043                         serv_puts(aaa);
1044                         serv_gets(aaa);
1045                         if (aaa[0] != '2') {
1046                                 printf("%s\n", &aaa[4]);
1047                                 exit(0);
1048                         }
1049                         load_user_info(&aaa[4]);
1050                         sprintf(aaa, "GOTO %s", room_name);
1051                         serv_puts(aaa);
1052                         serv_gets(aaa);
1053                 }
1054
1055                 signal(SIGINT, SIG_IGN);
1056                 signal(SIGQUIT, SIG_IGN);
1057                 signal(SIGPIPE, SIG_IGN);
1058                 mcmd = getcmd(argbuf);
1059
1060 #ifdef TIOCGWINSZ
1061                 check_screen_dims();
1062 #endif
1063
1064                 if (termn8 == 0)
1065                         switch (mcmd) {
1066                         case 1:
1067                                 formout("help");
1068                                 break;
1069                         case 4:
1070                                 entmsg(0, 0);
1071                                 break;
1072                         case 36:
1073                                 entmsg(0, 1);
1074                                 break;
1075                         case 46:
1076                                 entmsg(0, 2);
1077                                 break;
1078                         case 78:
1079                                 newprompt("What do you want your username to be? ", aaa, 32);
1080                                 snprintf(bbb, sizeof bbb, "ENT0 2|0|0|0|%s", aaa);
1081                                 serv_puts(bbb);
1082                                 serv_gets(aaa);
1083                                 if (strncmp("200", aaa, 3))
1084                                         printf("\n%s\n", aaa);
1085                                 else
1086                                         entmsg(0, 0);
1087                                 break;
1088                         case 5:
1089                                 updatels();
1090                                 gotonext();
1091                                 break;
1092                         case 47:
1093                                 updatelsa();
1094                                 gotonext();
1095                                 break;
1096                         case 58:
1097                                 updatelsa();
1098                                 dotgoto("_MAIL_", 1);
1099                                 break;
1100                         case 20:
1101                                 updatels();
1102                         case 52:
1103                                 dotgoto(argbuf, 0);
1104                                 break;
1105                         case 10:
1106                                 readmsgs(0, 1, 0);
1107                                 break;
1108                         case 9:
1109                                 readmsgs(3, 1, 5);
1110                                 break;
1111                         case 13:
1112                                 readmsgs(1, 1, 0);
1113                                 break;
1114                         case 11:
1115                                 readmsgs(0, (-1), 0);
1116                                 break;
1117                         case 12:
1118                                 readmsgs(2, (-1), 0);
1119                                 break;
1120                         case 71:
1121                                 readmsgs(3, 1, atoi(argbuf));
1122                                 break;
1123                         case 7:
1124                                 forget();
1125                                 break;
1126                         case 18:
1127                                 subshell();
1128                                 break;
1129                         case 38:
1130                                 updatels();
1131                                 entroom();
1132                                 break;
1133                         case 22:
1134                                 killroom();
1135                                 break;
1136                         case 32:
1137                                 userlist();
1138                                 break;
1139                         case 27:
1140                                 invite();
1141                                 break;
1142                         case 28:
1143                                 kickout();
1144                                 break;
1145                         case 23:
1146                                 editthisroom();
1147                                 break;
1148                         case 14:
1149                                 roomdir();
1150                                 break;
1151                         case 33:
1152                                 download(0);
1153                                 break;
1154                         case 34:
1155                                 download(1);
1156                                 break;
1157                         case 31:
1158                                 download(2);
1159                                 break;
1160                         case 43:
1161                                 download(3);
1162                                 break;
1163                         case 45:
1164                                 download(4);
1165                                 break;
1166                         case 55:
1167                                 download(5);
1168                                 break;
1169                         case 39:
1170                                 upload(0);
1171                                 break;
1172                         case 40:
1173                                 upload(1);
1174                                 break;
1175                         case 42:
1176                                 upload(2);
1177                                 break;
1178                         case 44:
1179                                 upload(3);
1180                                 break;
1181                         case 57:
1182                                 cli_upload();
1183                                 break;
1184                         case 16:
1185                                 ungoto();
1186                                 break;
1187                         case 24:
1188                                 whoknows();
1189                                 break;
1190                         case 26:
1191                                 validate();
1192                                 break;
1193                         case 29:
1194                                 updatels();
1195                                 termn8 = 1;
1196                                 break;
1197                         case 30:
1198                                 updatels();
1199                                 termn8 = 1;
1200                                 break;
1201                         case 48:
1202                                 enterinfo();
1203                                 break;
1204                         case 49:
1205                                 readinfo();
1206                                 break;
1207                         case 72:
1208                                 cli_image_upload("_userpic_");
1209                                 break;
1210                         case 73:
1211                                 cli_image_upload("_roompic_");
1212                                 break;
1213
1214                         case 74:
1215                                 snprintf(aaa, sizeof aaa, "_floorpic_|%d", curr_floor);
1216                                 cli_image_upload(aaa);
1217                                 break;
1218
1219                         case 75:
1220                                 enternew("roomname", aaa, 20);
1221                                 snprintf(bbb, sizeof bbb, "RCHG %s", aaa);
1222                                 serv_puts(bbb);
1223                                 serv_gets(aaa);
1224                                 if (strncmp("200", aaa, 3))
1225                                         printf("\n%s\n", aaa);
1226                                 break;
1227                         case 76:
1228                                 enternew("hostname", aaa, 25);
1229                                 snprintf(bbb, sizeof bbb, "HCHG %s", aaa);
1230                                 serv_puts(bbb);
1231                                 serv_gets(aaa);
1232                                 if (strncmp("200", aaa, 3))
1233                                         printf("\n%s\n", aaa);
1234                                 break;
1235                         case 77:
1236                                 enternew("username", aaa, 32);
1237                                 snprintf(bbb, sizeof bbb, "UCHG %s", aaa);
1238                                 serv_puts(bbb);
1239                                 serv_gets(aaa);
1240                                 if (strncmp("200", aaa, 3))
1241                                         printf("\n%s\n", aaa);
1242                                 break;
1243
1244                         case 35:
1245                                 set_password();
1246                                 break;
1247
1248                         case 21:
1249                                 if (argbuf[0] == 0)
1250                                         strcpy(aaa, "?");
1251                                 display_help(argbuf);
1252                                 break;
1253
1254                         case 41:
1255                                 formout("register");
1256                                 entregis();
1257                                 break;
1258
1259                         case 15:
1260                                 printf("Are you sure (y/n)? ");
1261                                 if (yesno() == 1) {
1262                                         updatels();
1263                                         a = 0;
1264                                         termn8 = 1;
1265                                 }
1266                                 break;
1267
1268                         case 6:
1269                                 gotonext();
1270                                 break;
1271
1272                         case 3:
1273                                 chatmode();
1274                                 break;
1275
1276                         case 2:
1277                                 if (server_is_local) {
1278                                         sttybbs(SB_RESTORE);
1279                                         snprintf(aaa, sizeof aaa, "USERNAME=\042%s\042; export USERNAME;"
1280                                                  "exec ./subsystem %ld %d %d", fullname,
1281                                           usernum, screenwidth, axlevel);
1282                                         ka_system(aaa);
1283                                         sttybbs(SB_NO_INTR);
1284                                 } else {
1285                                         printf("*** Can't run doors when server is not local.\n");
1286                                 }
1287                                 break;
1288
1289                         case 17:
1290                                 who_is_online(0);
1291                                 break;
1292
1293                         case 79:
1294                                 who_is_online(1);
1295                                 break;
1296
1297                         case 80:
1298                                 do_system_configuration();
1299                                 break;
1300
1301                         case 50:
1302                                 enter_config(2);
1303                                 break;
1304
1305                         case 37:
1306                                 enter_config(0);
1307                                 set_floor_mode();
1308                                 break;
1309
1310                         case 59:
1311                                 enter_config(3);
1312                                 set_floor_mode();
1313                                 break;
1314
1315                         case 60:
1316                                 gotofloor(argbuf, GF_GOTO);
1317                                 break;
1318
1319                         case 61:
1320                                 gotofloor(argbuf, GF_SKIP);
1321                                 break;
1322
1323                         case 62:
1324                                 forget_this_floor();
1325                                 break;
1326
1327                         case 63:
1328                                 create_floor();
1329                                 break;
1330
1331                         case 64:
1332                                 edit_floor();
1333                                 break;
1334
1335                         case 65:
1336                                 kill_floor();
1337                                 break;
1338
1339                         case 66:
1340                                 enter_bio();
1341                                 break;
1342
1343                         case 67:
1344                                 read_bio();
1345                                 break;
1346
1347                         case 25:
1348                                 edituser();
1349                                 break;
1350
1351                         case 8:
1352                                 knrooms(floor_mode);
1353                                 printf("\n");
1354                                 break;
1355
1356                         case 68:
1357                                 knrooms(2);
1358                                 printf("\n");
1359                                 break;
1360
1361                         case 69:
1362                                 misc_server_cmd(argbuf);
1363                                 break;
1364
1365                         case 70:
1366                                 edit_system_message(argbuf);
1367                                 break;
1368
1369                         case 19:
1370                                 listzrooms();
1371                                 printf("\n");
1372                                 break;
1373
1374                         case 51:
1375                                 deletefile();
1376                                 break;
1377
1378                         case 53:
1379                                 netsendfile();
1380                                 break;
1381
1382                         case 54:
1383                                 movefile();
1384                                 break;
1385
1386                         case 56:
1387                                 page_user();
1388                                 break;
1389
1390                         case 81:
1391                                 setup_icq();
1392                                 break;
1393
1394                         }       /* end switch */
1395         } while (termn8 == 0);
1396
1397       TERMN8:printf("%s logged out.\n", fullname);
1398         while (march != NULL)
1399                 remove_march(march->march_name, 0);
1400         if (mcmd == 30)
1401                 printf("\n\nType 'off' to hang up, or next user...\n");
1402         snprintf(aaa, sizeof aaa, "LOUT");
1403         serv_puts(aaa);
1404         serv_gets(aaa);
1405         if ((mcmd == 29) || (mcmd == 15)) {
1406                 formout("goodbye");
1407                 logoff(0);
1408         }
1409         goto GSTA;
1410
1411 }                               /* end main() */