3 # this is the operators console.
7 # console.pl [callsign]
9 # if the callsign isn't given then the sysop callsign in DXVars.pm is assumed
11 # Copyright (c) 1999 Dirk Koopman G1TLH
33 # search local then perl directories
35 # root of directory tree for this system
37 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
39 unshift @INC, "$root/perl"; # this IS the right way round!
40 unshift @INC, "$root/local";
41 $is_win = ($^O =~ /^MS/ || $^O =~ /^OS-2/) ? 1 : 0; # is it Windows?
56 use Time::HiRes qw(gettimeofday tv_interval);
58 use Text::Wrap qw(wrap);
66 $clusteraddr //= '127.0.0.1';
67 $clusterport //= 27754;
69 our $call = ""; # the callsign being used
70 our $node = ""; # the node callsign being used
71 our $conn = 0; # the connection object for the cluster
72 our $lasttime = time; # lasttime something happened on the interface
74 our $connsort = "local";
93 our $spos = $pos = $lth = 0;
97 #$SIG{WINCH} = sub {@time = gettimeofday};
99 $DXDebug::no_stdout = 1;
101 # do the screen initialisation
107 init_pair(0, $foreground, $background);
108 # init_pair(0, $background, $foreground);
109 init_pair(1, COLOR_RED, $background);
110 init_pair(2, COLOR_YELLOW, $background);
111 init_pair(3, COLOR_GREEN, $background);
112 init_pair(4, COLOR_CYAN, $background);
113 init_pair(5, COLOR_BLUE, $background);
114 init_pair(6, COLOR_MAGENTA, $background);
115 init_pair(7, COLOR_RED, COLOR_BLUE);
116 init_pair(8, COLOR_YELLOW, COLOR_BLUE);
117 init_pair(9, COLOR_GREEN, COLOR_BLUE);
118 init_pair(10, COLOR_CYAN, COLOR_BLUE);
119 init_pair(11, COLOR_BLUE, COLOR_RED);
120 init_pair(12, COLOR_MAGENTA, COLOR_BLUE);
121 init_pair(13, COLOR_YELLOW, COLOR_GREEN);
122 init_pair(14, COLOR_RED, COLOR_GREEN);
123 eval { assume_default_colors($foreground, $background) } unless $is_win;
126 $top = $scr->subwin($lines-4, $cols, 0, 0);
133 # $top->setscrreg(0, $lines-5);
135 $bot = $scr->subwin(3, $cols, $lines-3, 0);
147 $mycallcolor = COLOR_PAIR(1) unless $mycallcolor;
159 $has_colors = has_colors();
163 $spos = @sh < $pagel ? 0 : @sh - $pagel;
165 $conn->send_later("C$call|$cols") if $conn;
168 # cease communications
172 $conn->disconnect if $conn;
179 # terminate program from signal
185 # determine the colour of the line
189 foreach my $ref (@colors) {
190 if ($_[0] =~ m{$$ref[0]}) {
191 $top->attrset($$ref[1]);
199 # display the top screen
204 dbg("B: s:$spos h:" . scalar @sh) if isdbg('console');
207 $spos = 0 if $spos < 0;
210 $top->attrset(COLOR_PAIR(0)) if $has_colors;
212 for ($i = 0; $i < $pagel && $y < @sh; ++$y) {
216 dbg("C: s:$spos y:$i sh:" . scalar @sh . " l:" . length($line) . " '$line'") if isdbg('console');
219 $top->attrset(COLOR_PAIR(0)) if $has_colors;
226 } elsif ($spos < @sh || $spos < $pagel) {
227 # if we really are scrolling thru at the end of the history
228 while ($spos < @sh) {
229 my $line = $sh[$spos];
238 dbg("A: s:$spos sh:" . scalar @sh . " y:$y l:" . length($line) . " '$line'") if isdbg('console');
243 $top->attrset(COLOR_PAIR(0)) if $has_colors;
246 shift @sh while @sh > $maxshist;
252 my $size = $lines . 'x' . $cols . '-';
253 my $add = "-$spos-$shl";
254 my $time = ztime(time);
255 my $c = "$call\@$node";
256 my $str = "-" . $time . '-' . ($inscroll ? 'S':'-') . '-' x ($cols - (length($size) + length($c) + length($add) + length($time) + 3));
257 $scr->addstr($lines-4, 0, $str);
260 $scr->attrset($mycallcolor) if $has_colors;
262 $scr->attrset(COLOR_PAIR(0)) if $has_colors;
272 dbg("KEY: " . unpack("H*", $r). " '$r'") if isdbg('console');
276 # $prbuf =~ s/\r/\\r/;
277 # $prbuf =~ s/\n/\\n/;
278 # print "sys: $r ($prbuf)\n";
283 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
286 $inbuf = " " unless length $inbuf;
288 # check for a pling and do a search back for a command
289 if ($inbuf =~ /^!/o) {
292 for ($i = $#kh; $i >= 0; $i--) {
293 if ($kh[$i] =~ /^$inbuf/) {
303 push @kh, $inbuf if length $inbuf;
304 shift @kh if @kh > $maxkhist;
308 $bot->addstr(substr($inbuf, 0, $cols));
310 if ($inscroll && $spos < @sh) {
311 $spos = @sh - $pagel;
316 addtotop(' ', $inbuf);
318 # send it to the cluster
319 $conn->send_later("I$call|$inbuf");
322 } elsif ($r eq KEY_UP || $r eq "\020") {
326 $pos = $lth = length $inbuf;
330 } elsif ($r eq KEY_DOWN || $r eq "\016") {
331 if ($kpos < @kh - 1) {
334 $pos = $lth = length $inbuf;
338 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
339 if ($spos > 0 && @sh > $pagel) {
340 $spos -= $pagel+int($pagel/2);
341 $spos = 0 if $spos < 0;
347 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
348 if ($inscroll && $spos < @sh) {
350 dbg("NPAGE sp:$spos $sh:". scalar @sh . " pl: $pagel") if isdbg('console');
351 $spos += int($pagel/2);
352 if ($spos > @sh - $pagel) {
353 $spos = @sh - $pagel;
363 } elsif ($r eq KEY_LEFT || $r eq "\002") {
369 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
375 } elsif ($r eq KEY_HOME || $r eq "\001") {
377 } elsif ($r eq KEY_END || $r eq "\005") {
379 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
381 my $a = substr($inbuf, 0, $pos-1);
382 my $b = substr($inbuf, $pos) if $pos < $lth;
391 } elsif ($r eq KEY_DC || $r eq "\004") {
393 my $a = substr($inbuf, 0, $pos);
394 my $b = substr($inbuf, $pos+1) if $pos < $lth;
402 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
405 } elsif ($r eq "\x12" || $r eq "\x0c") {
406 dbg("REDRAW called") if isdbg('console');
409 } elsif ($r eq "\013") {
410 $inbuf = substr($inbuf, 0, $pos);
411 $lth = length $inbuf;
412 } elsif (defined $r && is_pctext($r)) {
413 # move the top screen back to the bottom if you type something
415 if ($inscroll && $spos < @sh) {
416 $spos = @sh - $pagel;
421 # $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
423 # insert the character into the keyboard buffer
425 my $a = substr($inbuf, 0, $pos);
426 my $b = substr($inbuf, $pos);
427 $inbuf = $a . $r . $b;
439 $bot->addstr($inbuf);
446 # add a line to the end of the top screen
452 my $l = length $inbuf;
455 if (length $inbuf > $cols) {
456 $Text::Wrap::columns = $cols;
458 ($token) = $inbuf =~ m!^(.* de [-\w\d/\#]+:?\s+|\w{9}\@\d\d:\d\d:\d\d )!;
460 push @sh, split /\n/, wrap('', ' ' x length($token), $inbuf);
469 show_screen() unless $inscroll;
472 # handle incoming messages
475 my ($con, $msg, $err) = @_;
476 if (defined $err && $err) {
480 my ($sort, $incall, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
481 dbg("msg: " . length($msg) . " '$msg'") if isdbg('console');
482 if ($line =~ s/\x07+$//) {
485 $line =~ s/[\r\n]+//s;
487 # change my call if my node says "tonight Michael you are Jane" or something like that...
488 $call = $incall if $call ne $incall;
490 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
491 if ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
494 $line = " " unless length($line);
495 addtotop($sort, $line);
512 if ($t > $lasttime) {
513 my ($min)= (gmtime($t))[1];
514 if ($min != $lastmin) {
515 show_screen() unless $inscroll;
520 my $ch = $bot->getch(); # this is here just to catch RESIZE events
522 if ($ch eq KEY_RESIZE) {
525 rec_stdin($ch) unless $ch eq '-1';
528 $top->refresh() if $top->is_wintouched;
535 $conn->send_later("A$call|$connsort width=$cols enhanced");
536 $conn->send_later("I$call|set/page " . ($maxshist-5));
537 $conn->send_later("I$call|set/nobeep");
543 Mojo::IOLoop->remove($idle);
552 while (@ARGV && $ARGV[0] =~ /^-/) {
561 $call = uc shift @ARGV if @ARGV;
562 $call = uc $myalias unless $call;
563 $node = uc $mycall unless $node;
565 $call = normalise_call($call);
566 my ($scall, $ssid) = split /-/, $call;
567 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
569 $ssid = 99 if $ssid > 99;
570 $call = "$scall-$ssid";
573 if ($call eq $mycall) {
574 print "You cannot connect as your cluster callsign ($mycall)\n";
578 unless ($DB::VERSION) {
579 $SIG{'INT'} = \&sig_term;
580 $SIG{'TERM'} = \&sig_term;
583 $SIG{'HUP'} = \&sig_term;
587 $Text::Wrap::columns = $cols;
590 $SIG{__DIE__} = \&sig_term;
592 $conn = IntMsg->connect($clusteraddr, $clusterport, rproc => \&rec_socket);
593 $conn->{on_connect} = \&on_connect;
594 $conn->{on_disconnect} = \&on_disconnect;
596 my $timer = Mojo::IOLoop->recurring(1, sub {DXLog::flushall()}) if $DXDebug::fp;
598 $idle = Mojo::IOLoop->recurring(0.100 => \&idle_loop);
599 Mojo::IOLoop->singleton->reactor->io(\*STDIN => sub {
600 my $ch = $bot->getch();
606 })->watch(\*STDIN, 1, 0);