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