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