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 use vars qw($data $clusteraddr $clusterport);
21 $clusteraddr = '127.0.0.1'; # cluster tcp host address - used for things like console.pl
22 $clusterport = 27754; # cluster tcp port
24 # search local then perl directories
26 # root of directory tree for this system
28 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
30 unshift @INC, "$root/perl"; # this IS the right way round!
31 unshift @INC, "$root/local";
32 $is_win = ($^O =~ /^MS/ || $^O =~ /^OS-2/) ? 1 : 0; # is it Windows?
43 use Time::HiRes qw(gettimeofday tv_interval);
53 $call = ""; # the callsign being used
54 $node = ""; # the node callsign being used
56 $conn = 0; # the connection object for the cluster
57 $lasttime = time; # lasttime something happened on the interface
63 $spos = $pos = $lth = 0;
67 #$SIG{WINCH} = sub {@time = gettimeofday};
71 local *STDOUT = undef;
75 # do the screen initialisation
81 init_pair("0", $foreground, $background);
82 # init_pair(0, $background, $foreground);
83 init_pair(1, COLOR_RED, $background);
84 init_pair(2, COLOR_YELLOW, $background);
85 init_pair(3, COLOR_GREEN, $background);
86 init_pair(4, COLOR_CYAN, $background);
87 init_pair(5, COLOR_BLUE, $background);
88 init_pair(6, COLOR_MAGENTA, $background);
89 init_pair(7, COLOR_RED, COLOR_BLUE);
90 init_pair(8, COLOR_YELLOW, COLOR_BLUE);
91 init_pair(9, COLOR_GREEN, COLOR_BLUE);
92 init_pair(10, COLOR_CYAN, COLOR_BLUE);
93 init_pair(11, COLOR_BLUE, COLOR_RED);
94 init_pair(12, COLOR_MAGENTA, COLOR_BLUE);
95 init_pair(13, COLOR_YELLOW, COLOR_GREEN);
96 init_pair(14, COLOR_RED, COLOR_GREEN);
97 eval { assume_default_colors($foreground, $background) } unless $is_win;
100 $top = $scr->subwin($lines-4, $cols, 0, 0);
105 # $scr->addstr($lines-4, 0, '-' x $cols);
106 $bot = $scr->subwin(3, $cols, $lines-3, 0);
117 $mycallcolor = COLOR_PAIR(1) unless $mycallcolor;
129 $has_colors = has_colors();
132 $spos = @sh < $pagel ? 0 : @sh - $pagel;
134 $conn->send_later("C$call|$cols") if $conn;
138 # cease communications
142 $conn->disconnect if $conn;
149 # terminate program from signal
155 # determine the colour of the line
159 foreach my $ref (@colors) {
160 if ($_[0] =~ m{$$ref[0]}) {
161 $top->attrset($$ref[1]);
169 # display the top screen
173 dbg("B: s:$spos h:" . scalar @sh) if isdbg('console');
176 $spos = 0 if $spos < 0;
179 $top->attrset(COLOR_PAIR(0)) if $has_colors;
181 for ($i = 0; $i < $pagel && $y < @sh; ++$y) {
185 dbg("C: s:$spos y:$i sh:" . scalar @sh . " l:" . length($line) . " '$line'") if isdbg('console');
188 $top->attrset(COLOR_PAIR(0)) if $has_colors;
195 } elsif ($spos < @sh || $spos < $pagel) {
196 # if we really are scrolling thru at the end of the history
197 while ($spos < @sh) {
198 my $line = $sh[$spos];
207 dbg("A: s:$spos sh:" . scalar @sh . " y:$y l:" . length($line) . " '$line'") if isdbg('console');
212 $top->attrset(COLOR_PAIR(0)) if $has_colors;
215 shift @sh while @sh > $maxshist;
221 my $size = $lines . 'x' . $cols . '-';
222 my $add = "-$spos-$shl";
223 my $time = ztime(time);
224 my $c = "$call\@$node";
225 my $str = "-" . $time . '-' . ($inscroll ? 'S':'-') . '-' x ($cols - (length($size) + length($c) + length($add) + length($time) + 3));
226 $scr->addstr($lines-4, 0, $str);
229 $scr->attrset($mycallcolor) if $has_colors;
231 $scr->attrset(COLOR_PAIR(0)) if $has_colors;
237 # add a line to the end of the top screen
242 my $l = length $inbuf;
245 if (length $inbuf > $cols) {
246 $Text::Wrap::columns = $cols;
248 ($token) = $inbuf =~ m!^(.* de [-\w\d/\#]+:?\s+|\w{9}\@\d\d:\d\d:\d\d )!;
250 push @sh, split /\n/, wrap('', ' ' x length($token), $inbuf);
258 # shift @sh while @sh > $maxshist;
262 # handle incoming messages
265 my ($con, $msg, $err) = @_;
266 if (defined $err && $err) {
270 my ($sort, $incall, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
271 if ($line =~ s/\x07+$//) {
274 $line =~ s/[\r\n]+//s;
276 # change my call if my node says "tonight Michael you are Jane" or something like that...
277 $call = $incall if $call ne $incall;
279 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
280 if ($sort && $sort eq 'D') {
281 $line = " " unless length($line);
283 } elsif ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
287 # ******************************************************
288 # ******************************************************
289 # any other sorts that might happen are silently ignored.
290 # ******************************************************
291 # ******************************************************
305 # $prbuf =~ s/\r/\\r/;
306 # $prbuf =~ s/\n/\\n/;
307 # print "sys: $r ($prbuf)\n";
312 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
315 $inbuf = " " unless length $inbuf;
317 # check for a pling and do a search back for a command
318 if ($inbuf =~ /^!/o) {
321 for ($i = $#kh; $i >= 0; $i--) {
322 if ($kh[$i] =~ /^$inbuf/) {
332 push @kh, $inbuf if length $inbuf;
333 shift @kh if @kh > $maxkhist;
337 $bot->addstr(substr($inbuf, 0, $cols));
339 if ($inscroll && $spos < @sh) {
340 $spos = @sh - $pagel;
345 # add it to the monitor window
348 # send it to the cluster
349 $conn->send_later("I$call|$inbuf");
352 } elsif ($r eq KEY_UP || $r eq "\020") {
355 $inbuf = $kh[$khistpos];
356 $pos = $lth = length $inbuf;
360 } elsif ($r eq KEY_DOWN || $r eq "\016") {
361 if ($khistpos < @kh - 1) {
363 $inbuf = $kh[$khistpos];
364 $pos = $lth = length $inbuf;
368 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
369 if ($spos > 0 && @sh > $pagel) {
370 $spos -= $pagel+int($pagel/2);
371 $spos = 0 if $spos < 0;
377 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
378 if ($inscroll && $spos < @sh) {
380 $spos += int($pagel/2);
381 if ($spos > @sh - $pagel) {
382 $spos = @sh - $pagel;
392 } elsif ($r eq KEY_LEFT || $r eq "\002") {
398 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
404 } elsif ($r eq KEY_HOME || $r eq "\001") {
406 } elsif ($r eq KEY_END || $r eq "\005") {
408 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
410 my $a = substr($inbuf, 0, $pos-1);
411 my $b = substr($inbuf, $pos) if $pos < $lth;
420 } elsif ($r eq KEY_DC || $r eq "\004") {
422 my $a = substr($inbuf, 0, $pos);
423 my $b = substr($inbuf, $pos+1) if $pos < $lth;
431 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
434 } elsif (defined $r && is_pctext($r)) {
435 # move the top screen back to the bottom if you type something
441 # $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
443 # insert the character into the keyboard buffer
445 my $a = substr($inbuf, 0, $pos);
446 my $b = substr($inbuf, $pos);
447 $inbuf = $a . $r . $b;
453 } elsif ($r eq "\014" || $r eq "\022") {
457 } elsif ($r eq "\013") {
458 $inbuf = substr($inbuf, 0, $pos);
459 $lth = length $inbuf;
465 $bot->addstr($inbuf);
476 $call = uc shift @ARGV if @ARGV;
477 $call = uc $myalias if !$call;
478 $node = uc $mycall unless $node;
480 my ($scall, $ssid) = split /-/, $call;
481 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
483 $ssid = 15 if $ssid > 15;
484 $call = "$scall-$ssid";
487 if ($call eq $mycall) {
488 print "You cannot connect as your cluster callsign ($mycall)\n";
494 $conn = IntMsg->connect("$clusteraddr", $clusterport, \&rec_socket);
496 if (-r "$data/offline") {
497 open IN, "$data/offline" or die;
503 print "Sorry, the cluster $mycall is currently off-line\n";
508 $conn->set_error(sub{cease(0)});
511 unless ($DB::VERSION) {
512 $SIG{'INT'} = \&sig_term;
513 $SIG{'TERM'} = \&sig_term;
516 $SIG{'HUP'} = \&sig_term;
521 $SIG{__DIE__} = \&sig_term;
523 $conn->send_later("A$call|$connsort width=$cols");
524 $conn->send_later("I$call|set/page $maxshist");
525 $conn->send_later("I$call|set/nobeep");
527 #Msg->set_event_handler(\*STDIN, "read" => \&rec_stdin);
529 $Text::Wrap::columns = $cols;
534 Msg->event_loop(1, 0.01);
536 if ($t > $lasttime) {
537 my ($min)= (gmtime($t))[1];
538 if ($min != $lastmin) {
544 my $ch = $bot->getch();
545 if (@time && tv_interval(\@time, [gettimeofday]) >= 1) {
553 $top->refresh() if $top->is_wintouched;