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
19 # search local then perl directories
21 # root of directory tree for this system
23 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
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?
42 use Time::HiRes qw(gettimeofday tv_interval);
44 use Text::Wrap qw(wrap);
52 $clusteraddr //= '127.0.0.1';
53 $clusterport //= 27754;
55 $call = ""; # the callsign being used
56 $node = ""; # the node callsign being used
57 $conn = 0; # the connection object for the cluster
58 $lasttime = time; # lasttime something happened on the interface
64 $spos = $pos = $lth = 0;
70 #$SIG{WINCH} = sub {@time = gettimeofday};
72 $DXDebug::no_stdout = 1;
74 # do the screen initialisation
80 init_pair(0, $foreground, $background);
81 # init_pair(0, $background, $foreground);
82 init_pair(1, COLOR_RED, $background);
83 init_pair(2, COLOR_YELLOW, $background);
84 init_pair(3, COLOR_GREEN, $background);
85 init_pair(4, COLOR_CYAN, $background);
86 init_pair(5, COLOR_BLUE, $background);
87 init_pair(6, COLOR_MAGENTA, $background);
88 init_pair(7, COLOR_RED, COLOR_BLUE);
89 init_pair(8, COLOR_YELLOW, COLOR_BLUE);
90 init_pair(9, COLOR_GREEN, COLOR_BLUE);
91 init_pair(10, COLOR_CYAN, COLOR_BLUE);
92 init_pair(11, COLOR_BLUE, COLOR_RED);
93 init_pair(12, COLOR_MAGENTA, COLOR_BLUE);
94 init_pair(13, COLOR_YELLOW, COLOR_GREEN);
95 init_pair(14, COLOR_RED, COLOR_GREEN);
96 eval { assume_default_colors($foreground, $background) } unless $is_win;
99 $top = $scr->subwin($lines-4, $cols, 0, 0);
106 # $top->setscrreg(0, $lines-5);
108 $bot = $scr->subwin(3, $cols, $lines-3, 0);
120 $mycallcolor = COLOR_PAIR(1) unless $mycallcolor;
132 $has_colors = has_colors();
136 $spos = @sh < $pagel ? 0 : @sh - $pagel;
138 $conn->send_later("C$call|$cols") if $conn;
141 # cease communications
145 $conn->disconnect if $conn;
152 # terminate program from signal
158 # determine the colour of the line
162 foreach my $ref (@colors) {
163 if ($_[0] =~ m{$$ref[0]}) {
164 $top->attrset($$ref[1]);
172 # display the top screen
177 dbg("B: s:$spos h:" . scalar @sh) if isdbg('console');
180 $spos = 0 if $spos < 0;
183 $top->attrset(COLOR_PAIR(0)) if $has_colors;
185 for ($i = 0; $i < $pagel && $y < @sh; ++$y) {
189 dbg("C: s:$spos y:$i sh:" . scalar @sh . " l:" . length($line) . " '$line'") if isdbg('console');
192 $top->attrset(COLOR_PAIR(0)) if $has_colors;
199 } elsif ($spos < @sh || $spos < $pagel) {
200 # if we really are scrolling thru at the end of the history
201 while ($spos < @sh) {
202 my $line = $sh[$spos];
211 dbg("A: s:$spos sh:" . scalar @sh . " y:$y l:" . length($line) . " '$line'") if isdbg('console');
216 $top->attrset(COLOR_PAIR(0)) if $has_colors;
219 shift @sh while @sh > $maxshist;
225 my $size = $lines . 'x' . $cols . '-';
226 my $add = "-$spos-$shl";
227 my $time = ztime(time);
228 my $c = "$call\@$node";
229 my $str = "-" . $time . '-' . ($inscroll ? 'S':'-') . '-' x ($cols - (length($size) + length($c) + length($add) + length($time) + 3));
230 $scr->addstr($lines-4, 0, $str);
233 $scr->attrset($mycallcolor) if $has_colors;
235 $scr->attrset(COLOR_PAIR(0)) if $has_colors;
245 dbg("KEY: " . unpack("H*", $r). " '$r'") if isdbg('console');
249 # $prbuf =~ s/\r/\\r/;
250 # $prbuf =~ s/\n/\\n/;
251 # print "sys: $r ($prbuf)\n";
256 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
259 $inbuf = " " unless length $inbuf;
261 # check for a pling and do a search back for a command
262 if ($inbuf =~ /^!/o) {
265 for ($i = $#kh; $i >= 0; $i--) {
266 if ($kh[$i] =~ /^$inbuf/) {
276 push @kh, $inbuf if length $inbuf;
277 shift @kh if @kh > $maxkhist;
281 $bot->addstr(substr($inbuf, 0, $cols));
283 if ($inscroll && $spos < @sh) {
284 $spos = @sh - $pagel;
289 addtotop(' ', $inbuf);
291 # send it to the cluster
292 $conn->send_later("I$call|$inbuf");
295 } elsif ($r eq KEY_UP || $r eq "\020") {
299 $pos = $lth = length $inbuf;
303 } elsif ($r eq KEY_DOWN || $r eq "\016") {
304 if ($kpos < @kh - 1) {
307 $pos = $lth = length $inbuf;
311 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
312 if ($spos > 0 && @sh > $pagel) {
313 $spos -= $pagel+int($pagel/2);
314 $spos = 0 if $spos < 0;
320 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
321 if ($inscroll && $spos < @sh) {
323 dbg("NPAGE sp:$spos $sh:". scalar @sh . " pl: $pagel") if isdbg('console');
324 $spos += int($pagel/2);
325 if ($spos > @sh - $pagel) {
326 $spos = @sh - $pagel;
336 } elsif ($r eq KEY_LEFT || $r eq "\002") {
342 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
348 } elsif ($r eq KEY_HOME || $r eq "\001") {
350 } elsif ($r eq KEY_END || $r eq "\005") {
352 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
354 my $a = substr($inbuf, 0, $pos-1);
355 my $b = substr($inbuf, $pos) if $pos < $lth;
364 } elsif ($r eq KEY_DC || $r eq "\004") {
366 my $a = substr($inbuf, 0, $pos);
367 my $b = substr($inbuf, $pos+1) if $pos < $lth;
375 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
378 } elsif ($r eq "\x12" || $r eq "\x0c") {
379 dbg("REDRAW called") if isdbg('console');
382 } elsif ($r eq "\013") {
383 $inbuf = substr($inbuf, 0, $pos);
384 $lth = length $inbuf;
385 } elsif (defined $r && is_pctext($r)) {
386 # move the top screen back to the bottom if you type something
388 if ($inscroll && $spos < @sh) {
389 $spos = @sh - $pagel;
394 # $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
396 # insert the character into the keyboard buffer
398 my $a = substr($inbuf, 0, $pos);
399 my $b = substr($inbuf, $pos);
400 $inbuf = $a . $r . $b;
412 $bot->addstr($inbuf);
419 # add a line to the end of the top screen
425 my $l = length $inbuf;
428 if (length $inbuf > $cols) {
429 $Text::Wrap::columns = $cols;
431 ($token) = $inbuf =~ m!^(.* de [-\w\d/\#]+:?\s+|\w{9}\@\d\d:\d\d:\d\d )!;
433 push @sh, split /\n/, wrap('', ' ' x length($token), $inbuf);
442 show_screen() unless $inscroll;
445 # handle incoming messages
448 my ($con, $msg, $err) = @_;
449 if (defined $err && $err) {
453 my ($sort, $incall, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
454 dbg("msg: " . length($msg) . " '$msg'") if isdbg('console');
455 if ($line =~ s/\x07+$//) {
458 $line =~ s/[\r\n]+//s;
460 # change my call if my node says "tonight Michael you are Jane" or something like that...
461 $call = $incall if $call ne $incall;
463 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
464 if ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
467 $line = " " unless length($line);
468 addtotop($sort, $line);
484 if ($t > $lasttime) {
485 my ($min)= (gmtime($t))[1];
486 if ($min != $lastmin) {
487 show_screen() unless $inscroll;
492 my $ch = $bot->getch(); # this is here just to catch RESIZE events
494 if ($ch eq KEY_RESIZE) {
497 rec_stdin($ch) unless $ch eq '-1';
500 $top->refresh() if $top->is_wintouched;
507 $conn->send_later("A$call|$connsort width=$cols enhanced");
508 $conn->send_later("I$call|set/page $maxshist");
509 #$conn->send_later("I$call|set/nobeep");
515 Mojo::IOLoop->remove($idle);
524 while (@ARGV && $ARGV[0] =~ /^-/) {
533 $call = uc shift @ARGV if @ARGV;
534 $call = uc $myalias unless $call;
535 $node = uc $mycall unless $node;
537 $call = normalise_call($call);
538 my ($scall, $ssid) = split /-/, $call;
539 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
541 $ssid = 99 if $ssid > 99;
542 $call = "$scall-$ssid";
545 if ($call eq $mycall) {
546 print "You cannot connect as your cluster callsign ($mycall)\n";
550 unless ($DB::VERSION) {
551 $SIG{'INT'} = \&sig_term;
552 $SIG{'TERM'} = \&sig_term;
555 $SIG{'HUP'} = \&sig_term;
559 $Text::Wrap::columns = $cols;
562 $SIG{__DIE__} = \&sig_term;
567 $conn = IntMsg->connect($clusteraddr, $clusterport, rproc => \&rec_socket);
568 $conn->{on_connect} = \&on_connect;
569 $conn->{on_disconnect} = \&on_disconnect;
571 my $timer = Mojo::IOLoop->recurring(1, sub {DXLog::flushall()}) if $DXDebug::fp;
573 $idle = Mojo::IOLoop->recurring(0.100 => \&idle_loop);
574 Mojo::IOLoop->singleton->reactor->io(\*STDIN => sub {
575 my $ch = $bot->getch();
581 })->watch(\*STDIN, 1, 0);