Allow synonyms for localhost
[spider.git] / perl / console.pl
1 #!/usr/bin/env perl
2 #
3 # this is the operators console.
4 #
5 # Calling syntax is:-
6 #
7 # console.pl [callsign] 
8 #
9 # if the callsign isn't given then the sysop callsign in DXVars.pm is assumed
10 #
11 # Copyright (c) 1999 Dirk Koopman G1TLH
12 #
13 #
14
15
16 require 5.16.1;
17 use strict;
18 use warnings;
19
20 our $root;
21 our $is_win;
22 our $myalias;
23 our $mycall;
24 our $clusteraddr;
25 our $clusterport;
26 our $maxshist;
27 our $maxkhist;
28 our $foreground;
29 our $background;
30 our $mycallcolor;
31 our @colors;
32
33 # search local then perl directories
34 BEGIN {
35         # root of directory tree for this system
36         $root = "/spider"; 
37         $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
38         
39         unshift @INC, "$root/perl";     # this IS the right way round!
40         unshift @INC, "$root/local";
41         $is_win = ($^O =~ /^MS/ || $^O =~ /^OS-2/) ? 1 : 0; # is it Windows?
42 }
43
44
45 use Mojo::IOLoop;
46
47 use DXVars;
48 use SysVar;
49
50 use Msg;
51 use IntMsg;
52 use DXDebug;
53 use DXUtil;
54 use DXDebug;
55 use IO::File;
56 use Time::HiRes qw(gettimeofday tv_interval);
57 use Curses 1.06;
58 use Text::Wrap qw(wrap);
59
60 use Console;
61
62 #
63 # initialisation
64 #
65
66 $clusteraddr //= '127.0.0.1';
67 $clusterport //= 27754;
68
69 our $call = "";                     # the callsign being used
70 our $node = "";                     # the node callsign being used
71 our $conn = 0;                      # the connection object for the cluster
72 our $lasttime = time;               # lasttime something happened on the interface
73
74 our $connsort = "local";
75 our @kh = ();
76 our @sh = ();
77 our $kpos = 0;
78 our $inbuf = "";
79 our $idle = 0;
80 our $inscroll = 0;
81
82 my $top;
83 my $bot;
84 my $lines;
85 my $scr;
86 my $cols;
87 my $pagel;
88 my $has_colors;
89 our $pos;
90 our $lth;
91 my $sh;
92
93 our $spos = $pos = $lth = 0;
94
95
96
97 #$SIG{WINCH} = sub {@time = gettimeofday};
98
99 $DXDebug::no_stdout = 1;
100
101 # do the screen initialisation
102 sub do_initscr
103 {
104         $scr = new Curses;
105         if ($has_colors) {
106                 start_color();
107                 init_pair(0, $foreground, $background);
108 #               init_pair(0, $background, $foreground);
109                 init_pair(1, COLOR_RED, $background);
110                 init_pair(2, COLOR_YELLOW, $background);
111                 init_pair(3, COLOR_GREEN, $background);
112                 init_pair(4, COLOR_CYAN, $background);
113                 init_pair(5, COLOR_BLUE, $background);
114                 init_pair(6, COLOR_MAGENTA, $background);
115                 init_pair(7, COLOR_RED, COLOR_BLUE);
116                 init_pair(8, COLOR_YELLOW, COLOR_BLUE);
117                 init_pair(9, COLOR_GREEN, COLOR_BLUE);
118                 init_pair(10, COLOR_CYAN, COLOR_BLUE);
119                 init_pair(11, COLOR_BLUE, COLOR_RED);
120                 init_pair(12, COLOR_MAGENTA, COLOR_BLUE);
121                 init_pair(13, COLOR_YELLOW, COLOR_GREEN);
122                 init_pair(14, COLOR_RED, COLOR_GREEN);
123                 eval { assume_default_colors($foreground, $background) } unless $is_win;
124         }
125
126         $top = $scr->subwin($lines-4, $cols, 0, 0);
127         $top->intrflush(0);
128         $top->scrollok(0);
129         $top->idlok(1);
130         $top->meta(1);
131         $top->leaveok(1);
132         $top->clrtobot();
133 #       $top->setscrreg(0, $lines-5);
134         
135         $bot = $scr->subwin(3, $cols, $lines-3, 0);
136         $bot->intrflush(0);
137         $bot->scrollok(1);
138         $bot->keypad(1);
139         $bot->move(1,0);
140         $bot->meta(1);
141         $bot->nodelay(1);
142         $bot->clrtobot();
143         $scr->refresh();
144         
145         
146         $pagel = $lines-4;
147         $mycallcolor = COLOR_PAIR(1) unless $mycallcolor;
148 }
149
150 sub doresize
151 {
152         endwin() if $scr;
153         initscr();
154         raw();
155         noecho();
156         nonl();
157         $lines = LINES;
158         $cols = COLS;
159         $has_colors = has_colors();
160         do_initscr();
161
162         $inscroll = 0;
163         $spos = @sh < $pagel ? 0 :  @sh - $pagel;
164         show_screen();
165         $conn->send_later("C$call|$cols") if $conn;
166 }
167
168 # cease communications
169 sub cease
170 {
171         my $sendz = shift;
172         $conn->disconnect if $conn;
173         endwin();
174         dbgclose();
175         print @_ if @_;
176         exit(0);        
177 }
178
179 # terminate program from signal
180 sub sig_term
181 {
182         cease(1, @_);
183 }
184
185 # determine the colour of the line
186 sub setattr
187 {
188         if ($has_colors) {
189                 foreach my $ref (@colors) {
190                         if ($_[0] =~ m{$$ref[0]}) {
191                                 $top->attrset($$ref[1]);
192                                 last;
193                         }
194                 }
195         }
196 }
197
198
199 # display the top screen
200 sub show_screen
201 {
202         if ($inscroll) {
203                 
204                 dbg("B: s:$spos h:" . scalar @sh) if isdbg('console');
205                 my ($i, $l);
206
207                 $spos = 0 if $spos < 0;
208                 my $y = $spos;
209                 $top->move(0, 0);
210                 $top->attrset(COLOR_PAIR(0)) if $has_colors;
211                 $top->clrtobot();
212                 for ($i = 0; $i < $pagel && $y < @sh; ++$y) {
213                         my $line = $sh[$y];
214                         my $lines = 1;
215                         $top->move($i, 0);
216                         dbg("C: s:$spos y:$i sh:" . scalar @sh . " l:" . length($line) . " '$line'") if isdbg('console');
217                         setattr($line);
218                         $top->addstr($line);
219                         $top->attrset(COLOR_PAIR(0)) if $has_colors;
220                         $i += $lines;
221                 }
222                 if ($y >= @sh) {
223                         $inscroll = 0;
224                         $spos = @sh;
225                 }
226         }       elsif ($spos < @sh || $spos < $pagel) {
227                 # if we really are scrolling thru at the end of the history
228                 while ($spos < @sh) {
229                         my $line = $sh[$spos];
230                         my $y = $spos;
231                         if ($y >= $pagel) {
232                                 $top->scrollok(1);
233                                 $top->scrl(1);
234                                 $top->scrollok(0);
235                                 $y = $pagel-1;
236                         }
237                         $top->move($y, 0);
238                         dbg("A: s:$spos sh:" . scalar @sh . " y:$y l:" . length($line) . " '$line'") if isdbg('console');
239                         $top->refresh;
240                         setattr($line);
241                         $line =~ s/\n//s;
242                         $top->addstr($line);
243                         $top->attrset(COLOR_PAIR(0)) if $has_colors;
244                         ++$spos;
245                 }
246                 shift @sh while @sh > $maxshist;
247                 $spos = @sh;
248         }
249
250         $top->refresh;
251     my $shl = @sh;
252         my $size = $lines . 'x' . $cols . '-'; 
253         my $add = "-$spos-$shl";
254     my $time = ztime(time);
255         my $c = "$call\@$node";
256         my $str =  "-" . $time . '-' . ($inscroll ? 'S':'-') . '-' x ($cols - (length($size) + length($c) + length($add) + length($time) + 3));
257         $scr->addstr($lines-4, 0, $str);
258         
259         $scr->addstr($size);
260         $scr->attrset($mycallcolor) if $has_colors;
261         $scr->addstr($c);
262         $scr->attrset(COLOR_PAIR(0)) if $has_colors;
263     $scr->addstr($add);
264         $scr->refresh();
265 #       $top->refresh();
266 }
267
268 sub rec_stdin
269 {
270         my $r = shift;
271         
272         dbg("KEY: " . unpack("H*", $r). " '$r'") if isdbg('console');
273
274         #  my $prbuf;
275         #  $prbuf = $buf;
276         #  $prbuf =~ s/\r/\\r/;
277         #  $prbuf =~ s/\n/\\n/;
278         #  print "sys: $r ($prbuf)\n";
279         if (defined $r) {
280
281                 $r = '0' if !$r;
282
283                 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
284                         
285                         # save the lines
286                         $inbuf = " " unless length $inbuf;
287
288                         # check for a pling and do a search back for a command
289                         if ($inbuf =~ /^!/o) {
290                                 my $i;
291                                 $inbuf =~ s/^!//o;
292                                 for ($i = $#kh; $i >= 0; $i--) {
293                                         if ($kh[$i] =~ /^$inbuf/) {
294                                                 $inbuf = $kh[$i];
295                                                 last;
296                                         }
297                                 }
298                                 if ($i < 0) {
299                                         beep();
300                                         return;
301                                 }
302                         }
303                         push @kh, $inbuf if length $inbuf;
304                         shift @kh if @kh > $maxkhist;
305                         $kpos = @kh;
306                         $bot->move(0,0);
307                         $bot->clrtoeol();
308                         $bot->addstr(substr($inbuf, 0, $cols));
309
310                         if ($inscroll && $spos < @sh) {
311                                 $spos = @sh - $pagel;
312                                 $inscroll = 0;
313                                 show_screen();
314                         }
315
316                         addtotop(' ', $inbuf);
317                 
318                         # send it to the cluster
319                         $conn->send_later("I$call|$inbuf");
320                         $inbuf = "";
321                         $pos = $lth = 0;
322                 } elsif ($r eq KEY_UP || $r eq "\020") {
323                         if ($kpos > 0) {
324                                 --$kpos;
325                                 $inbuf = $kh[$kpos];
326                                 $pos = $lth = length $inbuf;
327                         } else {
328                                 beep();
329                         }
330                 } elsif ($r eq KEY_DOWN || $r eq "\016") {
331                         if ($kpos < @kh - 1) {
332                                 ++$kpos;
333                                 $inbuf = $kh[$kpos];
334                                 $pos = $lth = length $inbuf;
335                         } else {
336                                 beep();
337                         }
338                 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
339                         if ($spos > 0 && @sh > $pagel) {
340                                 $spos -= $pagel+int($pagel/2); 
341                                 $spos = 0 if $spos < 0;
342                                 $inscroll = 1;
343                                 show_screen();
344                         } else {
345                                 beep();
346                         }
347                 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
348                         if ($inscroll && $spos < @sh) {
349
350                                 dbg("NPAGE sp:$spos $sh:". scalar @sh . " pl: $pagel") if isdbg('console');
351                                 $spos += int($pagel/2);
352                                 if ($spos > @sh - $pagel) {
353                                         $spos = @sh - $pagel;
354                                 } 
355                                 show_screen();
356                                 if ($spos >= @sh) {
357                                         $spos = @sh;
358                                         $inscroll = 0;
359                                 }
360                         } else {
361                                 beep();
362                         }
363                 } elsif ($r eq KEY_LEFT || $r eq "\002") {
364                         if ($pos > 0) {
365                                 --$pos;
366                         } else {
367                                 beep();
368                         }
369                 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
370                         if ($pos < $lth) {
371                                 ++$pos;
372                         } else {
373                                 beep();
374                         }
375                 } elsif ($r eq KEY_HOME || $r eq "\001") {
376                         $pos = 0;
377                 } elsif ($r eq KEY_END || $r eq "\005") {
378                         $pos = $lth;
379                 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
380                         if ($pos > 0) {
381                                 my $a = substr($inbuf, 0, $pos-1);
382                                 my $b = substr($inbuf, $pos) if $pos < $lth;
383                                 $b = "" unless $b;
384                                 
385                                 $inbuf = $a . $b;
386                                 --$lth;
387                                 --$pos;
388                         } else {
389                                 beep();
390                         }
391                 } elsif ($r eq KEY_DC || $r eq "\004") {
392                         if ($pos < $lth) {
393                                 my $a = substr($inbuf, 0, $pos);
394                                 my $b = substr($inbuf, $pos+1) if $pos < $lth;
395                                 $b = "" unless $b;
396                                 
397                                 $inbuf = $a . $b;
398                                 --$lth;
399                         } else {
400                                 beep();
401                         }
402                 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
403                         doresize();
404                         return;
405                 } elsif ($r eq "\x12" || $r eq "\x0c") {
406                         dbg("REDRAW called") if isdbg('console');
407                         doresize();
408                         return;
409                 } elsif ($r eq "\013") {
410                         $inbuf = substr($inbuf, 0, $pos);
411                         $lth = length $inbuf;
412                 } elsif (defined $r && is_pctext($r)) {
413                         # move the top screen back to the bottom if you type something
414                         
415                         if ($inscroll && $spos < @sh) {
416                                 $spos = @sh - $pagel;
417                                 $inscroll = 0;
418                                 show_screen();
419                         }
420
421                 #       $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
422                         
423                         # insert the character into the keyboard buffer
424                         if ($pos < $lth) {
425                                 my $a = substr($inbuf, 0, $pos);
426                                 my $b = substr($inbuf, $pos);
427                                 $inbuf = $a . $r . $b;
428                         } else {
429                                 $inbuf .= $r;
430                         }
431                         $pos++;
432                         $lth++;
433                 } else {
434                         beep();
435                 }
436
437                 $bot->move(1, 0);
438                 $bot->clrtobot();
439                 $bot->addstr($inbuf);
440         } 
441         $bot->move(1, $pos);
442         $bot->refresh();
443 }
444
445
446 # add a line to the end of the top screen
447 sub addtotop
448 {
449         my $sort = shift;
450         while (@_) {
451                 my $inbuf = shift;
452                 my $l = length $inbuf;
453                 if ($l > $cols) {
454                         $inbuf =~ s/\s+/ /g;
455                         if (length $inbuf > $cols) {
456                                 $Text::Wrap::columns = $cols;
457                                 my $token;
458                                 ($token) = $inbuf =~ m!^(.* de [-\w\d/\#]+:?\s+|\w{9}\@\d\d:\d\d:\d\d )!;
459                                 $token ||= ' ' x 19;
460                                 push @sh, split /\n/, wrap('', ' ' x length($token), $inbuf);
461                         } else {
462                                 push @sh, $inbuf;
463                         }
464                 } else {
465                         push @sh, $inbuf;
466                 }
467         }
468         
469         show_screen() unless $inscroll;
470 }
471
472 # handle incoming messages
473 sub rec_socket
474 {
475         my ($con, $msg, $err) = @_;
476         if (defined $err && $err) {
477                 cease(1);
478         }
479         if (defined $msg) {
480                 my ($sort, $incall, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
481                 dbg("msg: " . length($msg) . " '$msg'") if isdbg('console');
482                 if ($line =~ s/\x07+$//) {
483                         beep();
484                 }
485                 $line =~ s/[\r\n]+//s;
486
487                 # change my call if my node says "tonight Michael you are Jane" or something like that...
488                 $call = $incall if $call ne $incall;
489                 
490                 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g;         # immutable CSI sequence + control characters
491                 if ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
492                         cease(0);
493                 } else {
494                         $line = " " unless length($line);
495                         addtotop($sort, $line);
496                 }
497
498         } else {
499                 cease(0);
500         }
501         $top->refresh();
502         $lasttime = time; 
503 }
504
505 my $lastmin = 0;
506
507 sub idle_loop
508 {
509         my $t;
510         
511         $t = time;
512         if ($t > $lasttime) {
513                 my ($min)= (gmtime($t))[1];
514                 if ($min != $lastmin) {
515                         show_screen() unless $inscroll;
516                         $lastmin = $min;
517                 }
518                 $lasttime = $t;
519         }
520         my $ch = $bot->getch();         # this is here just to catch RESIZE events
521         if (defined $ch) {
522                 if ($ch eq KEY_RESIZE) {
523                         doresize();
524                 } else {
525                         rec_stdin($ch) unless $ch eq '-1';
526                 }
527         }
528         $top->refresh() if $top->is_wintouched;
529         $bot->refresh();
530 }
531
532 sub on_connect
533 {
534         my $conn = shift;
535         $conn->send_later("A$call|$connsort width=$cols enhanced");
536         $conn->send_later("I$call|set/page " . ($maxshist-5));
537         $conn->send_later("I$call|set/nobeep");
538 }
539
540 sub on_disconnect
541 {
542         $conn = shift;
543         Mojo::IOLoop->remove($idle);
544         Mojo::IOLoop->stop;
545 }
546
547 #
548 # deal with args
549 #
550
551
552 while (@ARGV && $ARGV[0] =~ /^-/) {
553         my $arg = shift;
554         if ($arg eq '-x') {
555                 dbginit('console');
556                 dbgadd('console');
557                 $maxshist = 200;
558         }
559 }
560
561 $call = uc shift @ARGV if @ARGV;
562 $call = uc $myalias unless $call;
563 $node = uc $mycall unless $node;
564
565 $call = normalise_call($call);
566 my ($scall, $ssid) = split /-/, $call;
567 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;  
568 if ($ssid) {
569         $ssid = 99 if $ssid > 99;
570         $call = "$scall-$ssid";
571 }
572
573 if ($call eq $mycall) {
574         print "You cannot connect as your cluster callsign ($mycall)\n";
575         exit(0);
576 }
577
578 unless ($DB::VERSION) {
579         $SIG{'INT'} = \&sig_term;
580         $SIG{'TERM'} = \&sig_term;
581 }
582
583 $SIG{'HUP'} = \&sig_term;
584
585
586 # start upb
587 $Text::Wrap::columns = $cols;
588 doresize();
589
590 $SIG{__DIE__} = \&sig_term;
591
592 $conn = IntMsg->connect($clusteraddr, $clusterport, rproc => \&rec_socket);
593 $conn->{on_connect} = \&on_connect;
594 $conn->{on_disconnect} = \&on_disconnect;
595
596 my $timer = Mojo::IOLoop->recurring(1, sub {DXLog::flushall()}) if $DXDebug::fp;
597
598 $idle = Mojo::IOLoop->recurring(0.100 => \&idle_loop);
599 Mojo::IOLoop->singleton->reactor->io(\*STDIN => sub {
600         my $ch = $bot->getch();
601         if (defined $ch) {
602                 if ($ch ne '-1') {
603                         rec_stdin($ch);
604                 }
605         }
606 })->watch(\*STDIN, 1, 0);
607
608
609 Mojo::IOLoop->start;
610
611
612 cease(0);