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;
136 # cease communications
140 $conn->disconnect if $conn;
147 # terminate program from signal
153 # determine the colour of the line
157 foreach my $ref (@colors) {
158 if ($_[0] =~ m{$$ref[0]}) {
159 $top->attrset($$ref[1]);
167 # display the top screen
171 dbg("B: s:$spos h:" . scalar @sh) if isdbg('console');
174 $spos = 0 if $spos < 0;
177 $top->attrset(COLOR_PAIR(0)) if $has_colors;
179 for ($i = 0; $i < $pagel && $y < @sh; ++$y) {
183 dbg("C: s:$spos y:$i sh:" . scalar @sh . " l:" . length($line) . " '$line'") if isdbg('console');
186 $top->attrset(COLOR_PAIR(0)) if $has_colors;
193 } elsif ($spos < @sh || $spos < $pagel) {
194 # if we really are scrolling thru at the end of the history
195 while ($spos < @sh) {
196 my $line = $sh[$spos];
205 dbg("A: s:$spos sh:" . scalar @sh . " y:$y l:" . length($line) . " '$line'") if isdbg('console');
210 $top->attrset(COLOR_PAIR(0)) if $has_colors;
213 shift @sh while @sh > $maxshist;
219 my $size = $lines . 'x' . $cols . '-';
220 my $add = "-$spos-$shl";
221 my $time = ztime(time);
222 my $c = "$call\@$node";
223 my $str = "-" . $time . '-' . ($inscroll ? 'S':'-') . '-' x ($cols - (length($size) + length($c) + length($add) + length($time) + 3));
224 $scr->addstr($lines-4, 0, $str);
227 $scr->attrset($mycallcolor) if $has_colors;
229 $scr->attrset(COLOR_PAIR(0)) if $has_colors;
235 # add a line to the end of the top screen
240 my $l = length $inbuf;
243 if (length $inbuf > $cols) {
244 $Text::Wrap::columns = $cols;
245 push @sh, split /\n/, wrap('',' ' x 19, $inbuf);
253 # shift @sh while @sh > $maxshist;
257 # handle incoming messages
260 my ($con, $msg, $err) = @_;
261 if (defined $err && $err) {
265 my ($sort, $incall, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
266 if ($line =~ s/\x07+$//) {
269 $line =~ s/[\r\n]+//s;
271 # change my call if my node says "tonight Michael you are Jane" or something like that...
272 $call = $incall if $call ne $incall;
274 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
275 if ($sort && $sort eq 'D') {
276 $line = " " unless length($line);
278 } elsif ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
282 # ******************************************************
283 # ******************************************************
284 # any other sorts that might happen are silently ignored.
285 # ******************************************************
286 # ******************************************************
300 # $prbuf =~ s/\r/\\r/;
301 # $prbuf =~ s/\n/\\n/;
302 # print "sys: $r ($prbuf)\n";
307 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
310 $inbuf = " " unless length $inbuf;
312 # check for a pling and do a search back for a command
313 if ($inbuf =~ /^!/o) {
316 for ($i = $#kh; $i >= 0; $i--) {
317 if ($kh[$i] =~ /^$inbuf/) {
327 push @kh, $inbuf if length $inbuf;
328 shift @kh if @kh > $maxkhist;
332 $bot->addstr(substr($inbuf, 0, $cols));
334 if ($inscroll && $spos < @sh) {
335 $spos = @sh - $pagel;
340 # add it to the monitor window
343 # send it to the cluster
344 $conn->send_later("I$call|$inbuf");
347 } elsif ($r eq KEY_UP || $r eq "\020") {
350 $inbuf = $kh[$khistpos];
351 $pos = $lth = length $inbuf;
355 } elsif ($r eq KEY_DOWN || $r eq "\016") {
356 if ($khistpos < @kh - 1) {
358 $inbuf = $kh[$khistpos];
359 $pos = $lth = length $inbuf;
363 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
364 if ($spos > 0 && @sh > $pagel) {
365 $spos -= $pagel+int($pagel/2);
366 $spos = 0 if $spos < 0;
372 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
373 if ($inscroll && $spos < @sh) {
375 $spos += int($pagel/2);
376 if ($spos > @sh - $pagel) {
377 $spos = @sh - $pagel;
387 } elsif ($r eq KEY_LEFT || $r eq "\002") {
393 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
399 } elsif ($r eq KEY_HOME || $r eq "\001") {
401 } elsif ($r eq KEY_END || $r eq "\005") {
403 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
405 my $a = substr($inbuf, 0, $pos-1);
406 my $b = substr($inbuf, $pos) if $pos < $lth;
415 } elsif ($r eq KEY_DC || $r eq "\004") {
417 my $a = substr($inbuf, 0, $pos);
418 my $b = substr($inbuf, $pos+1) if $pos < $lth;
426 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
429 } elsif (defined $r && is_pctext($r)) {
430 # move the top screen back to the bottom if you type something
436 # $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
438 # insert the character into the keyboard buffer
440 my $a = substr($inbuf, 0, $pos);
441 my $b = substr($inbuf, $pos);
442 $inbuf = $a . $r . $b;
448 } elsif ($r eq "\014" || $r eq "\022") {
452 } elsif ($r eq "\013") {
453 $inbuf = substr($inbuf, 0, $pos);
454 $lth = length $inbuf;
460 $bot->addstr($inbuf);
471 $call = uc shift @ARGV if @ARGV;
472 $call = uc $myalias if !$call;
473 $node = uc $mycall unless $node;
475 my ($scall, $ssid) = split /-/, $call;
476 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
478 $ssid = 15 if $ssid > 15;
479 $call = "$scall-$ssid";
482 if ($call eq $mycall) {
483 print "You cannot connect as your cluster callsign ($mycall)\n";
489 $conn = IntMsg->connect("$clusteraddr", $clusterport, \&rec_socket);
491 if (-r "$data/offline") {
492 open IN, "$data/offline" or die;
498 print "Sorry, the cluster $mycall is currently off-line\n";
503 $conn->set_error(sub{cease(0)});
506 unless ($DB::VERSION) {
507 $SIG{'INT'} = \&sig_term;
508 $SIG{'TERM'} = \&sig_term;
511 $SIG{'HUP'} = \&sig_term;
516 $SIG{__DIE__} = \&sig_term;
518 $conn->send_later("A$call|$connsort width=$cols");
519 $conn->send_later("I$call|set/page $maxshist");
520 $conn->send_later("I$call|set/nobeep");
522 #Msg->set_event_handler(\*STDIN, "read" => \&rec_stdin);
524 $Text::Wrap::columns = $cols;
529 Msg->event_loop(1, 0.01);
531 if ($t > $lasttime) {
532 my ($min)= (gmtime($t))[1];
533 if ($min != $lastmin) {
539 my $ch = $bot->getch();
540 if (@time && tv_interval(\@time, [gettimeofday]) >= 1) {
548 $top->refresh() if $top->is_wintouched;