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