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