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