merge various things from master
[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 $lastmin = 0;
80 our $idle = 0;
81 our $inscroll = 0;
82
83 my $top;
84 my $bot;
85 my $lines;
86 my $scr;
87 my $cols;
88 my $pagel;
89 my $has_colors;
90 our $pos;
91 our $lth;
92 my $sh;
93
94 our $spos = $pos = $lth = 0;
95
96
97
98 #$SIG{WINCH} = sub {@time = gettimeofday};
99
100 $DXDebug::no_stdout = 1;
101
102 # do the screen initialisation
103 sub do_initscr
104 {
105         $scr = new Curses;
106         if ($has_colors) {
107                 start_color();
108                 init_pair(0, $foreground, $background);
109 #               init_pair(0, $background, $foreground);
110                 init_pair(1, COLOR_RED, $background);
111                 init_pair(2, COLOR_YELLOW, $background);
112                 init_pair(3, COLOR_GREEN, $background);
113                 init_pair(4, COLOR_CYAN, $background);
114                 init_pair(5, COLOR_BLUE, $background);
115                 init_pair(6, COLOR_MAGENTA, $background);
116                 init_pair(7, COLOR_RED, COLOR_BLUE);
117                 init_pair(8, COLOR_YELLOW, COLOR_BLUE);
118                 init_pair(9, COLOR_GREEN, COLOR_BLUE);
119                 init_pair(10, COLOR_CYAN, COLOR_BLUE);
120                 init_pair(11, COLOR_BLUE, COLOR_RED);
121                 init_pair(12, COLOR_MAGENTA, COLOR_BLUE);
122                 init_pair(13, COLOR_YELLOW, COLOR_GREEN);
123                 init_pair(14, COLOR_RED, COLOR_GREEN);
124                 eval { assume_default_colors($foreground, $background) } unless $is_win;
125         }
126
127         $top = $scr->subwin($lines-4, $cols, 0, 0);
128         $top->intrflush(0);
129         $top->scrollok(0);
130         $top->idlok(1);
131         $top->meta(1);
132         $top->leaveok(1);
133         $top->clrtobot();
134 #       $top->setscrreg(0, $lines-5);
135         
136         $bot = $scr->subwin(3, $cols, $lines-3, 0);
137         $bot->intrflush(0);
138         $bot->scrollok(1);
139         $bot->keypad(1);
140         $bot->move(1,0);
141         $bot->meta(1);
142         $bot->nodelay(1);
143         $bot->clrtobot();
144         $scr->refresh();
145         
146         
147         $pagel = $lines-4;
148         $mycallcolor = COLOR_PAIR(1) unless $mycallcolor;
149 }
150
151 sub doresize
152 {
153         endwin() if $scr;
154         initscr();
155         raw();
156         noecho();
157         nonl();
158         $lines = LINES;
159         $cols = COLS;
160         $has_colors = has_colors();
161         do_initscr();
162
163         $inscroll = 0;
164         $spos = @sh < $pagel ? 0 :  @sh - $pagel;
165         show_screen();
166         $conn->send_later("C$call|$cols") if $conn;
167 }
168
169 # cease communications
170 sub cease
171 {
172         my $sendz = shift;
173         $conn->disconnect if $conn;
174         endwin();
175         dbgclose();
176         print @_ if @_;
177         exit(0);        
178 }
179
180 # terminate program from signal
181 sub sig_term
182 {
183         cease(1, @_);
184 }
185
186 # determine the colour of the line
187 sub setattr
188 {
189         if ($has_colors) {
190                 foreach my $ref (@colors) {
191                         if ($_[0] =~ m{$$ref[0]}) {
192                                 $top->attrset($$ref[1]);
193                                 last;
194                         }
195                 }
196         }
197 }
198
199
200 # display the top screen
201 sub show_screen
202 {
203         if ($inscroll) {
204                 
205                 dbg("B: s:$spos h:" . scalar @sh) if isdbg('console');
206                 my ($i, $l);
207
208                 $spos = 0 if $spos < 0;
209                 my $y = $spos;
210                 $top->move(0, 0);
211                 $top->attrset(COLOR_PAIR(0)) if $has_colors;
212                 $top->clrtobot();
213                 for ($i = 0; $i < $pagel && $y < @sh; ++$y) {
214                         my $line = $sh[$y];
215                         my $lines = 1;
216                         $top->move($i, 0);
217                         dbg("C: s:$spos y:$i sh:" . scalar @sh . " l:" . length($line) . " '$line'") if isdbg('console');
218                         setattr($line);
219                         $top->addstr($line);
220                         $top->attrset(COLOR_PAIR(0)) if $has_colors;
221                         $i += $lines;
222                 }
223                 if ($y >= @sh) {
224                         $inscroll = 0;
225                         $spos = @sh;
226                 }
227         }       elsif ($spos < @sh || $spos < $pagel) {
228                 # if we really are scrolling thru at the end of the history
229                 while ($spos < @sh) {
230                         my $line = $sh[$spos];
231                         my $y = $spos;
232                         if ($y >= $pagel) {
233                                 $top->scrollok(1);
234                                 $top->scrl(1);
235                                 $top->scrollok(0);
236                                 $y = $pagel-1;
237                         }
238                         $top->move($y, 0);
239                         dbg("A: s:$spos sh:" . scalar @sh . " y:$y l:" . length($line) . " '$line'") if isdbg('console');
240                         $top->refresh;
241                         setattr($line);
242                         $line =~ s/\n//s;
243                         $top->addstr($line);
244                         $top->attrset(COLOR_PAIR(0)) if $has_colors;
245                         ++$spos;
246                 }
247                 shift @sh while @sh > $maxshist;
248                 $spos = @sh;
249         }
250
251         $top->refresh;
252     my $shl = @sh;
253         my $size = $lines . 'x' . $cols . '-'; 
254         my $add = "-$spos-$shl";
255     my $time = ztime(time);
256         my $c = "$call\@$node";
257         my $str =  "-" . $time . '-' . ($inscroll ? 'S':'-') . '-' x ($cols - (length($size) + length($c) + length($add) + length($time) + 3));
258         $scr->addstr($lines-4, 0, $str);
259         
260         $scr->addstr($size);
261         $scr->attrset($mycallcolor) if $has_colors;
262         $scr->addstr($c);
263         $scr->attrset(COLOR_PAIR(0)) if $has_colors;
264     $scr->addstr($add);
265         $scr->refresh();
266 #       $top->refresh();
267 }
268
269 sub rec_stdin
270 {
271         my $r = shift;
272         
273         dbg("KEY: " . unpack("H*", $r). " '$r'") if isdbg('console');
274
275         #  my $prbuf;
276         #  $prbuf = $buf;
277         #  $prbuf =~ s/\r/\\r/;
278         #  $prbuf =~ s/\n/\\n/;
279         #  print "sys: $r ($prbuf)\n";
280         if (defined $r) {
281
282                 $r = '0' if !$r;
283
284                 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
285                         
286                         # save the lines
287                         $inbuf = " " unless length $inbuf;
288
289                         # check for a pling and do a search back for a command
290                         if ($inbuf =~ /^!/o) {
291                                 my $i;
292                                 $inbuf =~ s/^!//o;
293                                 for ($i = $#kh; $i >= 0; $i--) {
294                                         if ($kh[$i] =~ /^$inbuf/) {
295                                                 $inbuf = $kh[$i];
296                                                 last;
297                                         }
298                                 }
299                                 if ($i < 0) {
300                                         beep();
301                                         return;
302                                 }
303                         }
304                         push @kh, $inbuf if length $inbuf;
305                         shift @kh if @kh > $maxkhist;
306                         $kpos = @kh;
307                         $bot->move(0,0);
308                         $bot->clrtoeol();
309                         $bot->addstr(substr($inbuf, 0, $cols));
310
311                         if ($inscroll && $spos < @sh) {
312                                 $spos = @sh - $pagel;
313                                 $inscroll = 0;
314                                 show_screen();
315                         }
316
317                         addtotop(' ', $inbuf);
318                 
319                         # send it to the cluster
320                         $conn->send_later("I$call|$inbuf");
321                         $inbuf = "";
322                         $pos = $lth = 0;
323                 } elsif ($r eq KEY_UP || $r eq "\020") {
324                         if ($kpos > 0) {
325                                 --$kpos;
326                                 $inbuf = $kh[$kpos];
327                                 $pos = $lth = length $inbuf;
328                         } else {
329                                 beep();
330                         }
331                 } elsif ($r eq KEY_DOWN || $r eq "\016") {
332                         if ($kpos < @kh - 1) {
333                                 ++$kpos;
334                                 $inbuf = $kh[$kpos];
335                                 $pos = $lth = length $inbuf;
336                         } else {
337                                 beep();
338                         }
339                 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
340                         if ($spos > 0 && @sh > $pagel) {
341                                 $spos -= $pagel+int($pagel/2); 
342                                 $spos = 0 if $spos < 0;
343                                 $inscroll = 1;
344                                 show_screen();
345                         } else {
346                                 beep();
347                         }
348                 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
349                         if ($inscroll && $spos < @sh) {
350
351                                 dbg("NPAGE sp:$spos $sh:". scalar @sh . " pl: $pagel") if isdbg('console');
352                                 $spos += int($pagel/2);
353                                 if ($spos > @sh - $pagel) {
354                                         $spos = @sh - $pagel;
355                                 } 
356                                 show_screen();
357                                 if ($spos >= @sh) {
358                                         $spos = @sh;
359                                         $inscroll = 0;
360                                 }
361                         } else {
362                                 beep();
363                         }
364                 } elsif ($r eq KEY_LEFT || $r eq "\002") {
365                         if ($pos > 0) {
366                                 --$pos;
367                         } else {
368                                 beep();
369                         }
370                 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
371                         if ($pos < $lth) {
372                                 ++$pos;
373                         } else {
374                                 beep();
375                         }
376                 } elsif ($r eq KEY_HOME || $r eq "\001") {
377                         $pos = 0;
378                 } elsif ($r eq KEY_END || $r eq "\005") {
379                         $pos = $lth;
380                 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
381                         if ($pos > 0) {
382                                 my $a = substr($inbuf, 0, $pos-1);
383                                 my $b = substr($inbuf, $pos) if $pos < $lth;
384                                 $b = "" unless $b;
385                                 
386                                 $inbuf = $a . $b;
387                                 --$lth;
388                                 --$pos;
389                         } else {
390                                 beep();
391                         }
392                 } elsif ($r eq KEY_DC || $r eq "\004") {
393                         if ($pos < $lth) {
394                                 my $a = substr($inbuf, 0, $pos);
395                                 my $b = substr($inbuf, $pos+1) if $pos < $lth;
396                                 $b = "" unless $b;
397                                 
398                                 $inbuf = $a . $b;
399                                 --$lth;
400                         } else {
401                                 beep();
402                         }
403                 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
404                         doresize();
405                         return;
406                 } elsif ($r eq "\x12" || $r eq "\x0c") {
407                         dbg("REDRAW called") if isdbg('console');
408                         doresize();
409                         return;
410                 } elsif ($r eq "\013") {
411                         $inbuf = substr($inbuf, 0, $pos);
412                         $lth = length $inbuf;
413                 } elsif (defined $r && is_pctext($r)) {
414                         # move the top screen back to the bottom if you type something
415                         
416                         if ($inscroll && $spos < @sh) {
417                                 $spos = @sh - $pagel;
418                                 $inscroll = 0;
419                                 show_screen();
420                         }
421
422                 #       $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
423                         
424                         # insert the character into the keyboard buffer
425                         if ($pos < $lth) {
426                                 my $a = substr($inbuf, 0, $pos);
427                                 my $b = substr($inbuf, $pos);
428                                 $inbuf = $a . $r . $b;
429                         } else {
430                                 $inbuf .= $r;
431                         }
432                         $pos++;
433                         $lth++;
434                 } else {
435                         beep();
436                 }
437
438                 $bot->move(1, 0);
439                 $bot->clrtobot();
440                 $bot->addstr($inbuf);
441         } 
442         $bot->move(1, $pos);
443         $bot->refresh();
444 }
445
446
447 # add a line to the end of the top screen
448 sub addtotop
449 {
450         my $sort = shift;
451         while (@_) {
452                 my $inbuf = shift;
453                 my $l = length $inbuf;
454                 if ($l > $cols) {
455                         $inbuf =~ s/\s+/ /g;
456                         if (length $inbuf > $cols) {
457                                 $Text::Wrap::columns = $cols;
458                                 my $token;
459                                 ($token) = $inbuf =~ m!^(.* de [-\w\d/\#]+:?\s+|\w{9}\@\d\d:\d\d:\d\d )!;
460                                 $token ||= ' ' x 19;
461                                 push @sh, split /\n/, wrap('', ' ' x length($token), $inbuf);
462                         } else {
463                                 push @sh, $inbuf;
464                         }
465                 } else {
466                         push @sh, $inbuf;
467                 }
468         }
469         
470         show_screen() unless $inscroll;
471 }
472
473 # handle incoming messages
474 sub rec_socket
475 {
476         my ($con, $msg, $err) = @_;
477         if (defined $err && $err) {
478                 cease(1);
479         }
480         if (defined $msg) {
481                 my ($sort, $incall, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
482                 dbg("msg: " . length($msg) . " '$msg'") if isdbg('console');
483                 if ($line =~ s/\x07+$//) {
484                         beep();
485                 }
486                 $line =~ s/[\r\n]+//s;
487
488                 # change my call if my node says "tonight Michael you are Jane" or something like that...
489                 $call = $incall if $call ne $incall;
490                 
491                 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g;         # immutable CSI sequence + control characters
492                 if ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
493                         cease(0);
494                 } else {
495                         $line = " " unless length($line);
496                         addtotop($sort, $line);
497                 }
498
499         } else {
500                 cease(0);
501         }
502         $top->refresh();
503         $lasttime = time; 
504 }
505
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 my $lastmin = 0;
593
594
595 $conn = IntMsg->connect($clusteraddr, $clusterport, rproc => \&rec_socket);
596 $conn->{on_connect} = \&on_connect;
597 $conn->{on_disconnect} = \&on_disconnect;
598
599 my $timer = Mojo::IOLoop->recurring(1, sub {DXLog::flushall()}) if $DXDebug::fp;
600
601 $idle = Mojo::IOLoop->recurring(0.100 => \&idle_loop);
602 Mojo::IOLoop->singleton->reactor->io(\*STDIN => sub {
603         my $ch = $bot->getch();
604         if (defined $ch) {
605                 if ($ch ne '-1') {
606                         rec_stdin($ch);
607                 }
608         }
609 })->watch(\*STDIN, 1, 0);
610
611
612 Mojo::IOLoop->start;
613
614
615 cease(0);