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