a0cb567175ec7f55b4380626e333311e1216ec6b
[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         dbg("resize: l=$lines c=$cols");
136         dbg("resize: sh=". scalar @sh );
137 #       my @tsh;
138 #       my $t;
139 #       while (defined ($t = shift @sh)) {
140 #               dbg("t: $t(" , length $t . ')'); 
141 #               if ($t =~ /^\t/) {
142 #                       $t =~ s/^\t/ /;
143 #                       push(@tsh, pop(@tsh) . $t)
144 #               } else {
145 #                       push(@tsh, $t);
146 #               }
147 #               dbg("tsh: " . scalar @tsh);
148 #       }
149 #       dbg("resize: tsh=". scalar @tsh );
150 #       $spos = @tsh < $pagel ? 0 :  @tsh - $pagel;
151         #       addtotop(@tsh);
152         $spos = @sh < $pagel ? 0 :  @sh - $pagel;
153         show_screen();
154         $conn->send_later("C$call|$cols") if $conn;
155 }
156
157 # cease communications
158 sub cease
159 {
160         my $sendz = shift;
161         $conn->disconnect if $conn;
162         endwin();
163         dbgclose();
164         print @_ if @_;
165         exit(0);        
166 }
167
168 # terminate program from signal
169 sub sig_term
170 {
171         cease(1, @_);
172 }
173
174 # determine the colour of the line
175 sub setattr
176 {
177         if ($has_colors) {
178                 foreach my $ref (@colors) {
179                         if ($_[0] =~ m{$$ref[0]}) {
180                                 $top->attrset($$ref[1]);
181                                 last;
182                         }
183                 }
184         }
185 }
186
187 # measure the no of screen lines a line will take
188 sub measure
189 {
190         my $line = shift;
191         return 0 unless $line;
192
193         my $l = length $line;
194         my $lines = int ($l / $cols);
195         $lines++ if $l / $cols > $lines;
196         return $lines;
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 #               for ($i = 0; $i < $pagel && $p >= 0; ) {
207 #                       $l = measure($sh[$p]);
208 #                       $i += $l;
209 #                       $p-- if $i < $pagel;
210                 #               }
211
212                 $spos = 0 if $spos < 0;
213                 my $y = $spos;
214                 $top->move(0, 0);
215                 $top->attrset(COLOR_PAIR(0)) if $has_colors;
216                 $top->clrtobot();
217                 for ($i = 0; $i < $pagel && $y < @sh; ++$y) {
218                         my $line = $sh[$y];
219 #                       my $lines = measure($line);
220                         my $lines = 1;
221                         $top->move($i, 0);
222                         dbg("C: s:$spos y:$i sh:" . scalar @sh . " l:" . length($line) . " '$line'") if isdbg('console');
223                         setattr($line);
224                         $top->addstr($line);
225                         $top->attrset(COLOR_PAIR(0)) if $has_colors;
226                         $i += $lines;
227                 }
228                 if ($y >= @sh) {
229                         $inscroll = 0;
230                         $spos = @sh;
231                 }
232         }       elsif ($spos < @sh || $spos < $pagel) {
233                 # if we really are scrolling thru at the end of the history
234                 while ($spos < @sh) {
235                         my $line = $sh[$spos];
236                         my $y = $spos;
237                         if ($y >= $pagel) {
238                                 $top->scrollok(1);
239                                 $top->scrl(1);
240                                 $top->scrollok(0);
241                                 $y = $pagel-1;
242                         }
243                         $top->move($y, 0);
244                         dbg("A: s:$spos sh:" . scalar @sh . " y:$y l:" . length($line) . " '$line'") if isdbg('console');
245                         $top->refresh;
246                         setattr($line);
247                         $line =~ s/\n//s;
248                         $top->addstr($line);
249                         $top->attrset(COLOR_PAIR(0)) if $has_colors;
250                         ++$spos;
251                 }
252                 shift @sh while @sh > $maxshist;
253                 $spos = @sh;
254         }
255
256         $top->refresh;
257     my $shl = @sh;
258         my $size = $lines . 'x' . $cols . '-'; 
259         my $add = "-$spos-$shl";
260     my $time = ztime(time);
261         my $c = "$call\@$node";
262         my $str =  "-" . $time . '-' . ($inscroll ? 'S':'-') . '-' x ($cols - (length($size) + length($c) + length($add) + length($time) + 3));
263         $scr->addstr($lines-4, 0, $str);
264         
265         $scr->addstr($size);
266         $scr->attrset($mycallcolor) if $has_colors;
267         $scr->addstr($c);
268         $scr->attrset(COLOR_PAIR(0)) if $has_colors;
269     $scr->addstr($add);
270         $scr->refresh();
271 #       $top->refresh();
272 }
273
274 sub rec_stdin
275 {
276         my $r = shift;
277         
278         dbg("KEY: " . unpack("H*", $r). " '$r'") if isdbg('console');
279
280         #  my $prbuf;
281         #  $prbuf = $buf;
282         #  $prbuf =~ s/\r/\\r/;
283         #  $prbuf =~ s/\n/\\n/;
284         #  print "sys: $r ($prbuf)\n";
285         if (defined $r) {
286
287                 $r = '0' if !$r;
288
289                 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
290                         
291                         # save the lines
292                         $inbuf = " " unless length $inbuf;
293
294                         # check for a pling and do a search back for a command
295                         if ($inbuf =~ /^!/o) {
296                                 my $i;
297                                 $inbuf =~ s/^!//o;
298                                 for ($i = $#kh; $i >= 0; $i--) {
299                                         if ($kh[$i] =~ /^$inbuf/) {
300                                                 $inbuf = $kh[$i];
301                                                 last;
302                                         }
303                                 }
304                                 if ($i < 0) {
305                                         beep();
306                                         return;
307                                 }
308                         }
309                         push @kh, $inbuf if length $inbuf;
310                         shift @kh if @kh > $maxkhist;
311                         $kpos = @kh;
312                         $bot->move(0,0);
313                         $bot->clrtoeol();
314                         $bot->addstr(substr($inbuf, 0, $cols));
315
316                         # add it to the monitor window
317 #                       unless ($spos == @sh) {
318 #                               $spos = @sh;
319 #                               show_screen();
320 #                       }
321                         if ($inscroll && $spos < @sh) {
322                                 $spos = @sh - $pagel;
323                                 $inscroll = 0;
324                                 show_screen();
325                         }
326
327                         addtotop($inbuf);
328                 
329                         # send it to the cluster
330                         $conn->send_later("I$call|$inbuf");
331                         $inbuf = "";
332                         $pos = $lth = 0;
333                 } elsif ($r eq KEY_UP || $r eq "\020") {
334                         if ($kpos > 0) {
335                                 --$kpos;
336                                 $inbuf = $kh[$kpos];
337                                 $pos = $lth = length $inbuf;
338                         } else {
339                                 beep();
340                         }
341                 } elsif ($r eq KEY_DOWN || $r eq "\016") {
342                         if ($kpos < @kh - 1) {
343                                 ++$kpos;
344                                 $inbuf = $kh[$kpos];
345                                 $pos = $lth = length $inbuf;
346                         } else {
347                                 beep();
348                         }
349                 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
350                         if ($spos > 0 && @sh > $pagel) {
351 #                               my ($i, $l);
352 #                               for ($i = 0; $i < $pagel-1 && $spos >= 0; ) {
353 #                                       $l = measure($sh[$spos]);
354 #                                       $i += $l;
355 #                                       --$spos if $i <= $pagel;
356 #                               }
357                                 $spos -= $pagel+int($pagel/2); 
358                                 $spos = 0 if $spos < 0;
359                                 $inscroll = 1;
360                                 show_screen();
361                         } else {
362                                 beep();
363                         }
364                 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
365                         if ($inscroll && $spos < @sh) {
366 #                               my ($i, $l);
367 #                               for ($i = 0; $i <= $pagel && $spos < @sh; ) {
368 #                                       $l = measure($sh[$spos]);
369 #                                       $i += $l;
370 #                                       ++$spos if $i <= $pagel && $spos < @sh;
371 #                               }
372
373                                 dbg("NPAGE sp:$spos $sh:". scalar @sh . " pl: $pagel") if isdbg('console');
374                                 $spos += int($pagel/2);
375                                 if ($spos > @sh - $pagel) {
376                                         $spos = @sh - $pagel;
377                                 } 
378                                 show_screen();
379                                 if ($spos >= @sh) {
380                                         $spos = @sh;
381                                         $inscroll = 0;
382                                 }
383                         } else {
384                                 beep();
385                         }
386                 } elsif ($r eq KEY_LEFT || $r eq "\002") {
387                         if ($pos > 0) {
388                                 --$pos;
389                         } else {
390                                 beep();
391                         }
392                 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
393                         if ($pos < $lth) {
394                                 ++$pos;
395                         } else {
396                                 beep();
397                         }
398                 } elsif ($r eq KEY_HOME || $r eq "\001") {
399                         $pos = 0;
400                 } elsif ($r eq KEY_END || $r eq "\005") {
401                         $pos = $lth;
402                 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
403                         if ($pos > 0) {
404                                 my $a = substr($inbuf, 0, $pos-1);
405                                 my $b = substr($inbuf, $pos) if $pos < $lth;
406                                 $b = "" unless $b;
407                                 
408                                 $inbuf = $a . $b;
409                                 --$lth;
410                                 --$pos;
411                         } else {
412                                 beep();
413                         }
414                 } elsif ($r eq KEY_DC || $r eq "\004") {
415                         if ($pos < $lth) {
416                                 my $a = substr($inbuf, 0, $pos);
417                                 my $b = substr($inbuf, $pos+1) if $pos < $lth;
418                                 $b = "" unless $b;
419                                 
420                                 $inbuf = $a . $b;
421                                 --$lth;
422                         } else {
423                                 beep();
424                         }
425                 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
426                         doresize();
427                         return;
428                 } elsif ($r eq "\x12" || $r eq "\x0c") {
429                         dbg("REDRAW called") if isdbg('console');
430                         doresize();
431                         return;
432                 } elsif ($r eq "\013") {
433                         $inbuf = substr($inbuf, 0, $pos);
434                         $lth = length $inbuf;
435                 } elsif (defined $r && is_pctext($r)) {
436                         # move the top screen back to the bottom if you type something
437                         
438                         if ($inscroll && $spos < @sh) {
439                                 $spos = @sh - $pagel;
440                                 $inscroll = 0;
441                                 show_screen();
442                         }
443
444                 #       $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
445                         
446                         # insert the character into the keyboard buffer
447                         if ($pos < $lth) {
448                                 my $a = substr($inbuf, 0, $pos);
449                                 my $b = substr($inbuf, $pos);
450                                 $inbuf = $a . $r . $b;
451                         } else {
452                                 $inbuf .= $r;
453                         }
454                         $pos++;
455                         $lth++;
456                 } else {
457                         beep();
458                 }
459
460                 $bot->move(1, 0);
461                 $bot->clrtobot();
462                 $bot->addstr($inbuf);
463         } 
464         $bot->move(1, $pos);
465         $bot->refresh();
466 }
467
468
469 # add a line to the end of the top screen
470 sub addtotop
471 {
472         $Text::Wrap::Columns = $cols;
473         while (@_) {
474                 my $inbuf = shift;
475                 my $l = length $inbuf;
476                 dbg("addtotop: $l $inbuf");
477                 if ($l > $cols) {
478                         $inbuf =~ s/\s+/ /g;
479                         if (length $inbuf > $cols) {
480                                 push @sh, split /\n/, wrap('',' ' x 19, $inbuf);
481                         } else {
482                                 push @sh, $inbuf;
483                         }
484                 } else {
485                         push @sh, $inbuf;
486                 }
487         }
488         show_screen() unless $inscroll;
489 }
490
491 # handle incoming messages
492 sub rec_socket
493 {
494         my ($con, $msg, $err) = @_;
495         if (defined $err && $err) {
496                 cease(1);
497         }
498         if (defined $msg) {
499                 dbg("msg: " . length($msg) . " '$msg'") if isdbg('console');
500                 my ($sort, $incall, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
501                 if ($line =~ s/\x07+$//) {
502                         beep();
503                 }
504                 $line =~ s/[\r\n]+//s;
505
506                 # change my call if my node says "tonight Michael you are Jane" or something like that...
507                 $call = $incall if $call ne $incall;
508                 
509                 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g;         # immutable CSI sequence + control characters
510                 if ($sort && $sort eq 'D') {
511                         $line = " " unless length($line);
512                         addtotop($line);
513                 } elsif ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
514                         cease(0);
515                 }         
516                 # ******************************************************
517                 # ******************************************************
518                 # any other sorts that might happen are silently ignored.
519                 # ******************************************************
520                 # ******************************************************
521         } else {
522                 cease(0);
523         }
524         $top->refresh();
525         $lasttime = time; 
526 }
527
528
529 sub idle_loop
530 {
531         my $t;
532         
533         $t = time;
534         if ($t > $lasttime) {
535                 my ($min)= (gmtime($t))[1];
536                 if ($min != $lastmin) {
537                         show_screen() unless $inscroll;
538                         $lastmin = $min;
539                 }
540                 $lasttime = $t;
541         }
542         my $ch = $bot->getch();         # this is here just to catch RESIZE events
543         if (defined $ch) {
544                 if ($ch eq KEY_RESIZE) {
545                         doresize();
546                 } else {
547                         rec_stdin($ch) unless $ch eq '-1';
548                 }
549         }
550         $top->refresh() if $top->is_wintouched;
551         $bot->refresh();
552 }
553
554 sub on_connect
555 {
556         my $conn = shift;
557         $conn->send_later("A$call|$connsort width=$cols");
558         $conn->send_later("I$call|set/page $maxshist");
559         #$conn->send_later("I$call|set/nobeep");
560 }
561
562 sub on_disconnect
563 {
564         $conn = shift;
565         Mojo::IOLoop->remove($idle);
566         Mojo::IOLoop->stop;
567 }
568
569 #
570 # deal with args
571 #
572
573
574 while (@ARGV && $ARGV[0] =~ /^-/) {
575         my $arg = shift;
576         if ($arg eq '-x') {
577                 dbginit('console');
578                 dbgadd('console');
579                 $maxshist = 200;
580         }
581 }
582
583 $call = uc shift @ARGV if @ARGV;
584 $call = uc $myalias unless $call;
585 $node = uc $mycall unless $node;
586
587 my ($scall, $ssid) = split /-/, $call;
588 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;  
589 if ($ssid) {
590         $ssid = 15 if $ssid > 15;
591         $call = "$scall-$ssid";
592 }
593
594 if ($call eq $mycall) {
595         print "You cannot connect as your cluster callsign ($mycall)\n";
596         exit(0);
597 }
598
599 unless ($DB::VERSION) {
600         $SIG{'INT'} = \&sig_term;
601         $SIG{'TERM'} = \&sig_term;
602 }
603
604 $SIG{'HUP'} = \&sig_term;
605
606
607 # start upb
608 $Text::Wrap::Columns = $cols;
609 doresize();
610
611 $SIG{__DIE__} = \&sig_term;
612
613 #$Text::Wrap::Columns = $cols;
614
615 my $lastmin = 0;
616
617
618 $conn = IntMsg->connect($clusteraddr, $clusterport, rproc => \&rec_socket);
619 $conn->{on_connect} = \&on_connect;
620 $conn->{on_disconnect} = \&on_disconnect;
621
622 my $timer = Mojo::IOLoop->recurring(1, sub {DXLog::flushall()}) if $DXDebug::fp;
623
624 $idle = Mojo::IOLoop->recurring(0.100 => \&idle_loop);
625 Mojo::IOLoop->singleton->reactor->io(\*STDIN => sub {
626         my $ch = $bot->getch();
627         if (defined $ch) {
628                 if ($ch ne '-1') {
629                         rec_stdin($ch);
630                 }
631         }
632 })->watch(\*STDIN, 1, 0);
633
634
635 Mojo::IOLoop->start;
636
637
638 cease(0);