]> code.citadel.org Git - citadel.git/blob - citadel/citadel.c
fix systems with SYSV-style signal handling (e.g. libc5)
[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(3);
753         printf("FLG ###        User Name                 Room                 From host\n");
754         color(6);
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(1); printf("%-3s ", flags);
787                                         color(2); printf("%-3d ", extract_int(buf,0));
788                                         }
789                                 last_session=extract_int(buf,0);
790                                 color(3); printf("%-25s ", username);
791                                 color(4); printf("%-20s ", roomname);
792                                 color(5); printf("%-24s\n", fromhost);
793                                 color(7);
794                                 }
795                         }
796                 }
797         }
798
799 void enternew(char *desc, char *buf, int maxlen)
800 {
801    char bbb[128];
802    snprintf(bbb, sizeof bbb, "Enter in your new %s: ", desc);
803    newprompt(bbb, buf, maxlen);
804 }
805
806 /*
807  * main
808  */
809 int main(int argc, char **argv)
810 {
811 int a,b,mcmd;
812 char aaa[100],bbb[100],eee[100];                /* general purpose variables */
813 char argbuf[32];                                /* command line buf */
814 volatile int termn8 = 0;
815
816
817 sttybbs(SB_SAVE);               /* Store the old terminal parameters */
818 load_command_set();             /* parse the citadel.rc file */
819 sttybbs(SB_NO_INTR);            /* Install the new ones */
820 signal(SIGINT,SIG_IGN);
821 signal(SIGQUIT,SIG_IGN);
822 signal(SIGHUP,dropcarr);        /* Cleanup gracefully if carrier is dropped */
823 signal(SIGTERM,dropcarr);       /* Cleanup gracefully if terminated */
824 signal(SIGCONT,catch_sigcont);  /* Catch SIGCONT so we can reset terminal */
825
826 printf("Attaching to server...\r");
827 fflush(stdout);
828 attach_to_server(argc,argv);
829
830 send_ansi_detect();
831
832 serv_gets(aaa);
833 if (aaa[0]!='2') {
834         printf("%s\n",&aaa[4]);
835         logoff(atoi(aaa));
836         }
837 get_serv_info();
838
839 look_for_ansi();
840 cls(0);
841 color(7);
842
843 printf("%-22s\n%s\n%s\n",serv_info.serv_software,serv_info.serv_humannode,
844         serv_info.serv_bbs_city);
845 screenwidth = 80;               /* default screen dimensions */
846 screenheight = 24;
847
848 printf(" pause    next    stop\n");
849 printf(" ctrl-s  ctrl-o  ctrl-c\n\n");
850 formout("hello");               /* print the opening greeting */
851 printf("\n");
852
853 GSTA:   termn8=0; newnow=0;
854         do {
855                 if (strlen(rc_username) > 0) {
856                         strcpy(fullname,rc_username);
857                         }
858                 else {
859                         newprompt("Enter your name: ",fullname,29);
860                         }
861                 strproc(fullname); 
862                 if (!strucmp(fullname,"new")) {         /* just in case */
863                    printf("Please enter the name you wish to log in with.\n");
864                    }
865                 } while( 
866                         (!strucmp(fullname,"bbs"))
867                         || (!strucmp(fullname,"new"))
868                         || (strlen(fullname)==0) );
869
870         if (!strucmp(fullname,"off")) {
871                 mcmd=29;
872                 goto TERMN8;
873                 }
874
875         /* sign on to the server */
876         strcpy(re_username, fullname);
877         snprintf(aaa,sizeof aaa,"USER %s",fullname);
878         serv_puts(aaa);
879         serv_gets(aaa);
880         if (aaa[0]!='3') goto NEWUSR;
881
882         /* password authentication */
883         if (strlen(rc_password)>0) {
884                 strcpy(eee,rc_password);
885                 }
886         else {
887                 newprompt("\rPlease enter your password: ",eee,-19);
888                 }
889         strproc(eee);
890         snprintf(aaa,sizeof aaa,"PASS %s",eee);
891         serv_puts(aaa);
892         serv_gets(aaa);
893         if (aaa[0]=='2') {
894                 strcpy(re_password, eee);
895                 load_user_info(&aaa[4]);
896                 goto PWOK;
897                 }
898         
899         printf("<< wrong password >>\n");
900         if (strlen(rc_password)>0) logoff(0);
901         goto GSTA;
902
903 NEWUSR: if (strlen(rc_password)==0) {
904                 printf("No record. Enter as new user? ");
905                 if (yesno()==0) goto GSTA;
906                 }
907
908         snprintf(aaa,sizeof aaa,"NEWU %s",fullname);
909         serv_puts(aaa);
910         serv_gets(aaa);
911         if (aaa[0]!='2') {
912                 printf("%s\n",aaa);
913                 goto GSTA;
914                 }
915         load_user_info(&aaa[4]);
916
917         while (set_password() != 0) ;;
918         newnow=1;
919
920         enter_config(1);
921         
922
923 PWOK:   printf("%s\nAccess level: %d (%s)\nUser #%ld / Call #%d\n",
924                 fullname,axlevel,axdefs[(int)axlevel],
925                 usernum,timescalled);
926
927         serv_puts("CHEK");
928         serv_gets(aaa);
929         if (aaa[0]=='2') {
930                 b = extract_int(&aaa[4],0);
931                 if (b==1) printf("*** You have a new private message in Mail>\n");
932                 if (b>1)  printf("*** You have %d new private messages in Mail>\n",b);
933
934                 if ((axlevel>=6) && (extract_int(&aaa[4],2)>0)) {
935                         printf("*** Users need validation\n");
936                         }
937
938                 if (extract_int(&aaa[4],1)>0) {
939                         printf("*** Please register.\n");
940                         formout("register");
941                         entregis();
942                         }
943                 }
944
945         /* Make up some temporary filenames for use in various parts of the
946          * program.  Don't mess with these once they've been set, because we
947          * will be unlinking them later on in the program and we don't
948          * want to delete something that we didn't create. */
949         snprintf(temp,sizeof temp,"/tmp/citA%d",getpid());
950         snprintf(temp2,sizeof temp2,"/tmp/citB%d",getpid());
951         snprintf(tempdir,sizeof tempdir,"/tmp/citC%d",getpid());
952
953         /* Get screen dimensions.  First we go to a default of 80x24.  Then
954          * we try to get the user's actual screen dimensions off the server.
955          * However, if we're running on an xterm, all this stuff is
956          * irrelevant because we're going to dynamically size the screen
957          * during the session.
958          */
959         screenwidth = 80;
960         screenheight = 24;
961         serv_puts("GETU");
962         serv_gets(aaa);
963         if (aaa[0]=='2') {
964                 screenwidth = extract_int(&aaa[4],0);
965                 screenheight = extract_int(&aaa[4],1);
966                 }
967         if (getenv("TERM")!=NULL) if (!strcmp(getenv("TERM"),"xterm")) {
968                 have_xterm = 1;
969                 }
970 #ifdef TIOCGWINSZ
971         check_screen_dims();
972 #endif
973
974         set_floor_mode();
975
976
977         /* Enter the lobby */
978         dotgoto("_BASEROOM_",1);
979
980 /* Main loop for the system... user is logged in. */
981         strcpy(ugname,"");
982         uglsn = 0L;
983
984         if (newnow==1)  readmsgs(3,1,5);
985                 else    readmsgs(1,1,0);
986
987 do {    /* MAIN LOOP OF PROGRAM */
988
989         /* Reconnect to the server if the connection was broken */
990         if (setjmp(jmp_reconnect)) {
991                 printf("\rServer connection broken; reconnecting...\r");
992                 fflush(stdout);
993                 attach_to_server(argc,argv);
994                 printf("                                         \r");
995                 fflush(stdout);
996                 serv_gets(aaa);
997                 if (aaa[0]!='2') { printf("%s\n", &aaa[4]); exit(0); }
998                 get_serv_info();
999                 sprintf(aaa, "USER %s", re_username);
1000                 serv_puts(aaa);
1001                 serv_gets(aaa);
1002                 if (aaa[0]!='3') { printf("%s\n", &aaa[4]); exit(0); }
1003                 sprintf(aaa, "PASS %s", re_password);
1004                 serv_puts(aaa);
1005                 serv_gets(aaa);
1006                 if (aaa[0]!='2') { printf("%s\n", &aaa[4]); exit(0); }
1007                 load_user_info(&aaa[4]);
1008                 sprintf(aaa, "GOTO %s", room_name);
1009                 serv_puts(aaa);
1010                 serv_gets(aaa);
1011                 }
1012         signal(SIGPIPE, sigpipehandler);
1013
1014         signal(SIGINT,SIG_IGN);
1015         signal(SIGQUIT,SIG_IGN);
1016         mcmd=getcmd(argbuf);
1017
1018 #ifdef TIOCGWINSZ
1019         check_screen_dims();
1020 #endif
1021
1022         if (termn8==0) switch(mcmd) {
1023            case 1:      formout("help");
1024                         break;
1025            case 4:      entmsg(0,0);
1026                         break;
1027            case 36:     entmsg(0,1);
1028                         break;
1029            case 46:     entmsg(0,2);
1030                         break;
1031            case 78:     newprompt("What do you want your username to be? ", aaa, 32);
1032                         snprintf(bbb, sizeof bbb, "ENT0 2|0|0|0|%s", aaa);
1033                         serv_puts(bbb);
1034                         serv_gets(aaa);
1035                         if (strncmp("200", aaa, 3))
1036                            printf("\n%s\n", aaa);
1037                         else
1038                            entmsg(0, 0);
1039                         break;
1040            case 5:      updatels();
1041                         gotonext();
1042                         break;
1043            case 47:     updatelsa();
1044                         gotonext();
1045                         break;
1046            case 58:     updatelsa();
1047                         dotgoto("_MAIL_",1);
1048                         break;
1049            case 20:     updatels();
1050            case 52:     dotgoto(argbuf,0);
1051                         break;
1052            case 10:     readmsgs(0,1,0);
1053                         break;
1054            case 9:      readmsgs(3,1,5);
1055                         break;
1056            case 13:     readmsgs(1,1,0);
1057                         break;
1058            case 11:     readmsgs(0,(-1),0);
1059                         break;
1060            case 12:     readmsgs(2,(-1),0);
1061                         break;
1062            case 71:     readmsgs(3, 1, atoi(argbuf));
1063                         break;
1064            case 7:      forget();       break;
1065            case 18:     subshell();     break;
1066            case 38:     updatels();
1067                         entroom();      break;
1068            case 22:     killroom();     break;
1069            case 32:     userlist();     break;
1070            case 27:     invite();       break;
1071            case 28:     kickout();      break;
1072            case 23:     editthisroom(); break;
1073            case 14:     roomdir();      break;
1074            case 33:     download(0);    break;
1075            case 34:     download(1);    break;
1076            case 31:     download(2);    break;
1077            case 43:     download(3);    break;
1078            case 45:     download(4);    break;
1079            case 55:     download(5);    break;
1080            case 39:     upload(0);      break;
1081            case 40:     upload(1);      break;
1082            case 42:     upload(2);      break;
1083            case 44:     upload(3);      break;
1084            case 57:     cli_upload();   break;
1085            case 16:     ungoto();       break;
1086            case 24:     whoknows();     break;
1087            case 26:     validate();     break;
1088            case 29:     updatels();
1089                         termn8=1;
1090                         break;
1091            case 30:     updatels();
1092                         termn8=1;
1093                         break;
1094            case 48:     enterinfo();
1095                         break;
1096            case 49:     readinfo();
1097                         break;
1098            case 72:     cli_image_upload("_userpic_");
1099                         break;
1100            case 73:     cli_image_upload("_roompic_");
1101                         break;
1102
1103 case 74:
1104         snprintf(aaa, sizeof aaa, "_floorpic_|%d", curr_floor);
1105         cli_image_upload(aaa);
1106         break;
1107         
1108 case 75:
1109         enternew("roomname", aaa, 20);
1110         snprintf(bbb, sizeof bbb, "RCHG %s", aaa);
1111         serv_puts(bbb);
1112         serv_gets(aaa);
1113         if (strncmp("200",aaa, 3))
1114            printf("\n%s\n", aaa);
1115         break;
1116 case 76:
1117         enternew("hostname", aaa, 25);
1118         snprintf(bbb, sizeof bbb, "HCHG %s", aaa);
1119         serv_puts(bbb);
1120         serv_gets(aaa);
1121         if (strncmp("200",aaa, 3))
1122            printf("\n%s\n", aaa);
1123         break;
1124 case 77:
1125         enternew("username", aaa, 32);
1126         snprintf(bbb, sizeof bbb, "UCHG %s", aaa);
1127         serv_puts(bbb);
1128         serv_gets(aaa);
1129         if (strncmp("200",aaa, 3))
1130            printf("\n%s\n", aaa);
1131         break;
1132
1133 case 35:
1134         set_password();
1135         break;
1136
1137 case 21:
1138         if (argbuf[0]==0) strcpy(aaa,"?");
1139         display_help(argbuf);
1140         break;
1141
1142 case 41:
1143         formout("register");
1144         entregis();
1145         break;
1146
1147 case 15:
1148         printf("Are you sure (y/n)? ");
1149         if (yesno()==1) {
1150                 updatels();
1151                 a=0;
1152                 termn8=1;
1153                 }
1154         break;
1155
1156 case 6:
1157         gotonext();
1158         break;
1159
1160 case 3: chatmode();
1161         break;
1162
1163 case 2: if (server_is_local) {
1164                 sttybbs(SB_RESTORE);
1165                 snprintf(aaa,sizeof aaa,"USERNAME=\042%s\042; export USERNAME;"
1166                         "exec ./subsystem %ld %d %d", fullname,
1167                         usernum,screenwidth,axlevel);
1168                 ka_system(aaa);
1169                 sttybbs(SB_NO_INTR);
1170                 }
1171         else {
1172                 printf("*** Can't run doors when server is not local.\n");
1173                 }
1174         break;
1175
1176 case 17:
1177         who_is_online(0);
1178         break;
1179
1180 case 79:
1181         who_is_online(1);
1182         break;
1183
1184 case 80:
1185         do_system_configuration();
1186         break;
1187
1188 case 50:
1189         enter_config(2);
1190         break;
1191
1192 case 37:
1193         enter_config(0);
1194         set_floor_mode();
1195         break;
1196
1197 case 59:
1198         enter_config(3);
1199         set_floor_mode();
1200         break;
1201
1202 case 60:
1203         gotofloor(argbuf,GF_GOTO);
1204         break;
1205         
1206 case 61:
1207         gotofloor(argbuf,GF_SKIP);
1208         break;
1209         
1210 case 62:
1211         forget_this_floor();
1212         break;
1213
1214 case 63:
1215         create_floor();
1216         break;
1217
1218 case 64:
1219         edit_floor();
1220         break;
1221
1222 case 65:
1223         kill_floor();
1224         break;
1225
1226 case 66:
1227         enter_bio();
1228         break;
1229
1230 case 67:
1231         read_bio();
1232         break;
1233
1234 case 25:
1235         edituser();
1236         break;
1237
1238 case 8:
1239         knrooms(floor_mode);
1240         printf("\n");
1241         break;
1242
1243 case 68:
1244         knrooms(2);
1245         printf("\n");
1246         break;
1247
1248 case 69:
1249         misc_server_cmd(argbuf);
1250         break;
1251
1252 case 70:
1253         edit_system_message(argbuf);
1254         break;
1255
1256 case 19:
1257         listzrooms();
1258         printf("\n");
1259         break;
1260
1261 case 51:
1262         deletefile();
1263         break;
1264
1265 case 53:
1266         netsendfile();
1267         break;
1268
1269 case 54:
1270         movefile();
1271         break;
1272
1273 case 56:
1274         page_user();
1275         break;
1276
1277         } /* end switch */
1278     } while(termn8==0);
1279
1280 TERMN8: printf("%s logged out.\n",fullname);
1281         while (march!=NULL) remove_march(march->march_name,0);
1282         if (mcmd==30)
1283                 printf("\n\nType 'off' to hang up, or next user...\n");
1284         snprintf(aaa,sizeof aaa,"LOUT");
1285         serv_puts(aaa);
1286         serv_gets(aaa);
1287         if ((mcmd==29)||(mcmd==15)) {
1288                 formout("goodbye");
1289                 logoff(0);
1290                 }
1291         goto GSTA;
1292
1293 } /* end main() */
1294