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