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