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);
45 use Text::Wrap qw(wrap);
53 $clusteraddr //= '127.0.0.1';
54 $clusterport //= 27754;
56 $call = ""; # the callsign being used
57 $node = ""; # the node callsign being used
58 $conn = 0; # the connection object for the cluster
59 $lasttime = time; # lasttime something happened on the interface
65 $spos = $pos = $lth = 0;
70 #$SIG{WINCH} = sub {@time = gettimeofday};
72 # do the screen initialisation
78 init_pair("0", $foreground, $background);
79 # init_pair(0, $background, $foreground);
80 init_pair(1, COLOR_RED, $background);
81 init_pair(2, COLOR_YELLOW, $background);
82 init_pair(3, COLOR_GREEN, $background);
83 init_pair(4, COLOR_CYAN, $background);
84 init_pair(5, COLOR_BLUE, $background);
85 init_pair(6, COLOR_MAGENTA, $background);
86 init_pair(7, COLOR_RED, COLOR_BLUE);
87 init_pair(8, COLOR_YELLOW, COLOR_BLUE);
88 init_pair(9, COLOR_GREEN, COLOR_BLUE);
89 init_pair(10, COLOR_CYAN, COLOR_BLUE);
90 init_pair(11, COLOR_BLUE, COLOR_RED);
91 init_pair(12, COLOR_MAGENTA, COLOR_BLUE);
92 init_pair(13, COLOR_YELLOW, COLOR_GREEN);
93 init_pair(14, COLOR_RED, COLOR_GREEN);
94 eval { assume_default_colors($foreground, $background) } unless $is_win;
97 $top = $scr->subwin($lines-4, $cols, 0, 0);
104 $bot = $scr->subwin(3, $cols, $lines-3, 0);
116 $mycallcolor = COLOR_PAIR(1) unless $mycallcolor;
128 $has_colors = has_colors();
132 $spos = @sh < $pagel ? 0 : @sh - $pagel;
134 $conn->send_later("C$call|$cols") if $conn;
137 # cease communications
141 $conn->disconnect if $conn;
148 # terminate program from signal
154 # determine the colour of the line
158 foreach my $ref (@colors) {
159 if ($_[0] =~ m{$$ref[0]}) {
160 $top->attrset($$ref[1]);
168 # 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;
241 dbg("KEY: " . unpack("H*", $r). " '$r'") if isdbg('console');
245 # $prbuf =~ s/\r/\\r/;
246 # $prbuf =~ s/\n/\\n/;
247 # print "sys: $r ($prbuf)\n";
252 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
255 $inbuf = " " unless length $inbuf;
257 # check for a pling and do a search back for a command
258 if ($inbuf =~ /^!/o) {
261 for ($i = $#kh; $i >= 0; $i--) {
262 if ($kh[$i] =~ /^$inbuf/) {
272 push @kh, $inbuf if length $inbuf;
273 shift @kh if @kh > $maxkhist;
277 $bot->addstr(substr($inbuf, 0, $cols));
279 if ($inscroll && $spos < @sh) {
280 $spos = @sh - $pagel;
285 addtotop(' ', $inbuf);
287 # send it to the cluster
288 $conn->send_later("I$call|$inbuf");
291 } elsif ($r eq KEY_UP || $r eq "\020") {
295 $pos = $lth = length $inbuf;
299 } elsif ($r eq KEY_DOWN || $r eq "\016") {
300 if ($kpos < @kh - 1) {
303 $pos = $lth = length $inbuf;
307 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
308 if ($spos > 0 && @sh > $pagel) {
309 $spos -= $pagel+int($pagel/2);
310 $spos = 0 if $spos < 0;
316 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
317 if ($inscroll && $spos < @sh) {
319 dbg("NPAGE sp:$spos $sh:". scalar @sh . " pl: $pagel") if isdbg('console');
320 $spos += int($pagel/2);
321 if ($spos > @sh - $pagel) {
322 $spos = @sh - $pagel;
332 } elsif ($r eq KEY_LEFT || $r eq "\002") {
338 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
344 } elsif ($r eq KEY_HOME || $r eq "\001") {
346 } elsif ($r eq KEY_END || $r eq "\005") {
348 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
350 my $a = substr($inbuf, 0, $pos-1);
351 my $b = substr($inbuf, $pos) if $pos < $lth;
360 } elsif ($r eq KEY_DC || $r eq "\004") {
362 my $a = substr($inbuf, 0, $pos);
363 my $b = substr($inbuf, $pos+1) if $pos < $lth;
371 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
374 } elsif ($r eq "\x12" || $r eq "\x0c") {
375 dbg("REDRAW called") if isdbg('console');
378 } elsif ($r eq "\013") {
379 $inbuf = substr($inbuf, 0, $pos);
380 $lth = length $inbuf;
381 } elsif (defined $r && is_pctext($r)) {
382 # move the top screen back to the bottom if you type something
384 if ($inscroll && $spos < @sh) {
385 $spos = @sh - $pagel;
390 # $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
392 # insert the character into the keyboard buffer
394 my $a = substr($inbuf, 0, $pos);
395 my $b = substr($inbuf, $pos);
396 $inbuf = $a . $r . $b;
408 $bot->addstr($inbuf);
415 # add a line to the end of the top screen
421 my $l = length $inbuf;
424 if (length $inbuf > $cols) {
425 $Text::Wrap::columns = $cols;
427 ($token) = $inbuf =~ m!^(.* de [-\w\d/\#]+:?\s+|\w{9}\@\d\d:\d\d:\d\d )!;
429 push @sh, split /\n/, wrap('', ' ' x length($token), $inbuf);
438 show_screen() unless $inscroll;
441 # handle incoming messages
444 my ($con, $msg, $err) = @_;
445 if (defined $err && $err) {
449 my ($sort, $incall, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
450 dbg("msg: " . length($msg) . " '$msg'") if isdbg('console');
451 if ($line =~ s/\x07+$//) {
454 $line =~ s/[\r\n]+//s;
456 # change my call if my node says "tonight Michael you are Jane" or something like that...
457 $call = $incall if $call ne $incall;
459 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
460 if ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
463 $line = " " unless length($line);
464 addtotop($sort, $line);
478 while (@ARGV && $ARGV[0] =~ /^-/) {
487 $call = uc shift @ARGV if @ARGV;
488 $call = uc $myalias unless $call;
489 $node = uc $mycall unless $node;
491 $call = normalise_call($call);
492 my ($scall, $ssid) = split /-/, $call;
493 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
495 $ssid = 99 if $ssid > 99;
496 $call = "$scall-$ssid";
499 if ($call eq $mycall) {
500 print "You cannot connect as your cluster callsign ($mycall)\n";
505 $conn = IntMsg->connect("$clusteraddr", $clusterport, \&rec_socket);
507 if (-r "$data/offline") {
508 open IN, "$data/offline" or die;
514 print "Sorry, the cluster $mycall is currently off-line\n";
519 $conn->set_error(sub{cease(0)});
522 unless ($DB::VERSION) {
523 $SIG{'INT'} = \&sig_term;
524 $SIG{'TERM'} = \&sig_term;
527 $SIG{'HUP'} = \&sig_term;
532 $SIG{__DIE__} = \&sig_term;
534 $Text::Wrap::columns = $cols;
535 $conn->send_later("A$call|$connsort width=$cols enhanced");
536 $conn->send_later("I$call|set/page $maxshist");
537 $conn->send_later("I$call|set/nobeep");
539 #Msg->set_event_handler(\*STDIN, "read" => \&rec_stdin);
541 $Text::Wrap::columns = $cols;
546 Msg->event_loop(1, 0.01);
548 if ($t > $lasttime) {
549 my ($min)= (gmtime($t))[1];
550 if ($min != $lastmin) {
556 my $ch = $bot->getch();
557 if (@time && tv_interval(\@time, [gettimeofday]) >= 1) {
565 $top->refresh() if $top->is_wintouched;