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