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
35 # search local then perl directories
37 # root of directory tree for this system
39 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
40 $local_data = "$root/local_data";
43 unshift @INC, "$root/perl"; # this IS the right way round!
44 unshift @INC, "$root/local";
45 $is_win = ($^O =~ /^MS/ || $^O =~ /^OS-2/) ? 1 : 0; # is it Windows?
60 use Time::HiRes qw(gettimeofday tv_interval);
62 use Text::Wrap qw(wrap);
70 $clusteraddr //= '127.0.0.1';
71 $clusterport //= 27754;
73 our $call = ""; # the callsign being used
74 our $node = ""; # the node callsign being used
75 our $conn = 0; # the connection object for the cluster
76 our $lasttime = time; # lasttime something happened on the interface
78 our $connsort = "local";
97 our $spos = $pos = $lth = 0;
101 #$SIG{WINCH} = sub {@time = gettimeofday};
103 $DXDebug::no_stdout = 1;
105 # do the screen initialisation
111 init_pair(0, $foreground, $background);
112 # init_pair(0, $background, $foreground);
113 init_pair(1, COLOR_RED, $background);
114 init_pair(2, COLOR_YELLOW, $background);
115 init_pair(3, COLOR_GREEN, $background);
116 init_pair(4, COLOR_CYAN, $background);
117 init_pair(5, COLOR_BLUE, $background);
118 init_pair(6, COLOR_MAGENTA, $background);
119 init_pair(7, COLOR_RED, COLOR_BLUE);
120 init_pair(8, COLOR_YELLOW, COLOR_BLUE);
121 init_pair(9, COLOR_GREEN, COLOR_BLUE);
122 init_pair(10, COLOR_CYAN, COLOR_BLUE);
123 init_pair(11, COLOR_BLUE, COLOR_RED);
124 init_pair(12, COLOR_MAGENTA, COLOR_BLUE);
125 init_pair(13, COLOR_YELLOW, COLOR_GREEN);
126 init_pair(14, COLOR_RED, COLOR_GREEN);
127 eval { assume_default_colors($foreground, $background) } unless $is_win;
130 $top = $scr->subwin($lines-4, $cols, 0, 0);
137 # $top->setscrreg(0, $lines-5);
139 $bot = $scr->subwin(3, $cols, $lines-3, 0);
151 $mycallcolor = COLOR_PAIR(1) unless $mycallcolor;
163 $has_colors = has_colors();
167 $spos = @sh < $pagel ? 0 : @sh - $pagel;
169 $conn->send_later("C$call|$cols") if $conn;
172 # cease communications
176 $conn->disconnect if $conn;
183 # terminate program from signal
189 # determine the colour of the line
193 foreach my $ref (@colors) {
194 if ($_[0] =~ m{$$ref[0]}) {
195 $top->attrset($$ref[1]);
203 # display the top screen
208 dbg("B: s:$spos h:" . scalar @sh) if isdbg('console');
211 $spos = 0 if $spos < 0;
214 $top->attrset(COLOR_PAIR(0)) if $has_colors;
216 for ($i = 0; $i < $pagel && $y < @sh; ++$y) {
220 dbg("C: s:$spos y:$i sh:" . scalar @sh . " l:" . length($line) . " '$line'") if isdbg('console');
223 $top->attrset(COLOR_PAIR(0)) if $has_colors;
230 } elsif ($spos < @sh || $spos < $pagel) {
231 # if we really are scrolling thru at the end of the history
232 while ($spos < @sh) {
233 my $line = $sh[$spos];
242 dbg("A: s:$spos sh:" . scalar @sh . " y:$y l:" . length($line) . " '$line'") if isdbg('console');
247 $top->attrset(COLOR_PAIR(0)) if $has_colors;
250 shift @sh while @sh > $maxshist;
256 my $size = $lines . 'x' . $cols . '-';
257 my $add = "-$spos-$shl";
258 my $time = ztime(time);
259 my $c = "$call\@$node";
260 my $str = "-" . $time . '-' . ($inscroll ? 'S':'-') . '-' x ($cols - (length($size) + length($c) + length($add) + length($time) + 3));
261 $scr->addstr($lines-4, 0, $str);
264 $scr->attrset($mycallcolor) if $has_colors;
266 $scr->attrset(COLOR_PAIR(0)) if $has_colors;
276 dbg("KEY: " . unpack("H*", $r). " '$r'") if isdbg('console');
280 # $prbuf =~ s/\r/\\r/;
281 # $prbuf =~ s/\n/\\n/;
282 # print "sys: $r ($prbuf)\n";
287 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
290 $inbuf = " " unless length $inbuf;
292 # check for a pling and do a search back for a command
293 if ($inbuf =~ /^!/o) {
296 for ($i = $#kh; $i >= 0; $i--) {
297 if ($kh[$i] =~ /^$inbuf/) {
307 push @kh, $inbuf if length $inbuf;
308 shift @kh if @kh > $maxkhist;
312 $bot->addstr(substr($inbuf, 0, $cols));
314 if ($inscroll && $spos < @sh) {
315 $spos = @sh - $pagel;
320 addtotop(' ', $inbuf);
322 # send it to the cluster
323 $conn->send_later("I$call|$inbuf");
326 } elsif ($r eq KEY_UP || $r eq "\020") {
330 $pos = $lth = length $inbuf;
334 } elsif ($r eq KEY_DOWN || $r eq "\016") {
335 if ($kpos < @kh - 1) {
338 $pos = $lth = length $inbuf;
342 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
343 if ($spos > 0 && @sh > $pagel) {
344 $spos -= $pagel+int($pagel/2);
345 $spos = 0 if $spos < 0;
351 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
352 if ($inscroll && $spos < @sh) {
354 dbg("NPAGE sp:$spos $sh:". scalar @sh . " pl: $pagel") if isdbg('console');
355 $spos += int($pagel/2);
356 if ($spos > @sh - $pagel) {
357 $spos = @sh - $pagel;
367 } elsif ($r eq KEY_LEFT || $r eq "\002") {
373 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
379 } elsif ($r eq KEY_HOME || $r eq "\001") {
381 } elsif ($r eq KEY_END || $r eq "\005") {
383 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
385 my $a = substr($inbuf, 0, $pos-1);
386 my $b = substr($inbuf, $pos) if $pos < $lth;
395 } elsif ($r eq KEY_DC || $r eq "\004") {
397 my $a = substr($inbuf, 0, $pos);
398 my $b = substr($inbuf, $pos+1) if $pos < $lth;
406 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
409 } elsif ($r eq "\x12" || $r eq "\x0c") {
410 dbg("REDRAW called") if isdbg('console');
413 } elsif ($r eq "\013") {
414 $inbuf = substr($inbuf, 0, $pos);
415 $lth = length $inbuf;
416 } elsif (defined $r && is_pctext($r)) {
417 # move the top screen back to the bottom if you type something
419 if ($inscroll && $spos < @sh) {
420 $spos = @sh - $pagel;
425 # $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
427 # insert the character into the keyboard buffer
429 my $a = substr($inbuf, 0, $pos);
430 my $b = substr($inbuf, $pos);
431 $inbuf = $a . $r . $b;
443 $bot->addstr($inbuf);
450 # add a line to the end of the top screen
456 my $l = length $inbuf;
459 if (length $inbuf > $cols) {
460 $Text::Wrap::columns = $cols;
462 ($token) = $inbuf =~ m!^(.* de [-\w\d/\#]+:?\s+|\w{9}\@\d\d:\d\d:\d\d )!;
464 push @sh, split /\n/, wrap('', ' ' x length($token), $inbuf);
473 show_screen() unless $inscroll;
476 # handle incoming messages
479 my ($con, $msg, $err) = @_;
480 if (defined $err && $err) {
484 my ($sort, $incall, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
485 dbg("msg: " . length($msg) . " '$msg'") if isdbg('console');
486 if ($line =~ s/\x07+$//) {
489 $line =~ s/[\r\n]+//s;
491 # change my call if my node says "tonight Michael you are Jane" or something like that...
492 $call = $incall if $call ne $incall;
494 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
495 if ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
498 $line = " " unless length($line);
499 addtotop($sort, $line);
516 if ($t > $lasttime) {
517 my ($min)= (gmtime($t))[1];
518 if ($min != $lastmin) {
519 show_screen() unless $inscroll;
524 my $ch = $bot->getch(); # this is here just to catch RESIZE events
526 if ($ch eq KEY_RESIZE) {
529 rec_stdin($ch) unless $ch eq '-1';
532 $top->refresh() if $top->is_wintouched;
539 $conn->send_later("A$call|$connsort width=$cols enhanced");
540 $conn->send_later("I$call|set/page " . ($maxshist-5));
541 $conn->send_later("I$call|set/nobeep");
547 Mojo::IOLoop->remove($idle);
556 while (@ARGV && $ARGV[0] =~ /^-/) {
565 $call = uc shift @ARGV if @ARGV;
566 $call = uc $myalias unless $call;
567 $node = uc $mycall unless $node;
569 $call = normalise_call($call);
570 my ($scall, $ssid) = split /-/, $call;
571 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
573 $ssid = 99 if $ssid > 99;
574 $call = "$scall-$ssid";
577 if ($call eq $mycall) {
578 print "You cannot connect as your cluster callsign ($mycall)\n";
582 unless ($DB::VERSION) {
583 $SIG{'INT'} = \&sig_term;
584 $SIG{'TERM'} = \&sig_term;
587 $SIG{'HUP'} = \&sig_term;
591 $Text::Wrap::columns = $cols;
594 $SIG{__DIE__} = \&sig_term;
596 $conn = IntMsg->connect($clusteraddr, $clusterport, rproc => \&rec_socket);
597 $conn->{on_connect} = \&on_connect;
598 $conn->{on_disconnect} = \&on_disconnect;
600 my $timer = Mojo::IOLoop->recurring(1, sub {DXLog::flushall()}) if $DXDebug::fp;
602 $idle = Mojo::IOLoop->recurring(0.100 => \&idle_loop);
603 Mojo::IOLoop->singleton->reactor->io(\*STDIN => sub {
604 my $ch = $bot->getch();
610 })->watch(\*STDIN, 1, 0);