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