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
18 # search local then perl directories
20 # root of directory tree for this system
22 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
24 unshift @INC, "$root/perl"; # this IS the right way round!
25 unshift @INC, "$root/local";
26 $is_win = ($^O =~ /^MS/ || $^O =~ /^OS-2/) ? 1 : 0; # is it Windows?
39 use Time::HiRes qw(gettimeofday tv_interval);
45 use vars qw($maxkhist $maxshist $foreground $background $mycallcolor @colors );
51 my $call = ""; # the callsign being used
52 my $conn = 0; # the connection object for the cluster
53 my $lasttime = time; # lasttime something happened on the interface
55 my $connsort = "local";
67 my $spos = $pos = $lth = 0;
76 #$SIG{WINCH} = sub {@time = gettimeofday};
80 local *STDOUT = undef;
84 # do the screen initialisation
88 if ($main::has_colors) {
90 init_pair("0", $main::foreground, $main::background);
91 # init_pair(0, $main::background, $main::foreground);
92 init_pair(1, COLOR_RED, $main::background);
93 init_pair(2, COLOR_YELLOW, $main::background);
94 init_pair(3, COLOR_GREEN, $main::background);
95 init_pair(4, COLOR_CYAN, $main::background);
96 init_pair(5, COLOR_BLUE, $main::background);
97 init_pair(6, COLOR_MAGENTA, $main::background);
98 init_pair(7, COLOR_RED, COLOR_BLUE);
99 init_pair(8, COLOR_YELLOW, COLOR_BLUE);
100 init_pair(9, COLOR_GREEN, COLOR_BLUE);
101 init_pair(10, COLOR_CYAN, COLOR_BLUE);
102 init_pair(11, COLOR_BLUE, COLOR_RED);
103 init_pair(12, COLOR_MAGENTA, COLOR_BLUE);
104 init_pair(13, COLOR_YELLOW, COLOR_GREEN);
105 init_pair(14, COLOR_RED, COLOR_GREEN);
106 eval { assume_default_colors($main::foreground, $main::background) } unless $main::is_win;
109 $top = $scr->subwin($lines-4, $cols, 0, 0);
114 # $scr->addstr($lines-4, 0, '-' x $cols);
115 $bot = $scr->subwin(3, $cols, $lines-3, 0);
126 $mycallcolor = COLOR_PAIR(1) unless $mycallcolor;
138 $main::has_colors = has_colors();
146 # cease communications
151 print "ceasing ($ceasing)\r\n";
156 $conn->disconnect if $conn;
165 # terminate program from signal
171 # determine the colour of the line
174 if ($main::has_colors) {
175 foreach my $ref (@colors) {
176 if ($_[0] =~ m{$$ref[0]}) {
177 $top->attrset($$ref[1]);
184 # measure the no of screen lines a line will take
188 return 0 unless $line;
190 my $l = length $line;
191 my $lines = int ($l / $cols);
192 $lines++ if $l / $cols > $lines;
196 # display the top screen
199 if ($spos == @shistory - 1) {
201 # if we really are scrolling thru at the end of the history
202 my $line = $shistory[$spos];
203 $top->addstr("\n") if $spos > 0;
206 # $top->addstr("\n");
207 $top->attrset(COLOR_PAIR(0)) if $main::has_colors;
215 for ($i = 0; $i < $pagel && $p >= 0; ) {
216 $l = measure($shistory[$p]);
223 $top->attrset(COLOR_PAIR(0)) if $main::has_colors;
225 for ($i = 0; $i < $pagel && $p < @shistory; $p++) {
226 my $line = $shistory[$p];
227 my $lines = measure($line);
228 last if $i + $lines > $pagel;
229 $top->addstr("\n") if $i;
232 $top->attrset(COLOR_PAIR(0)) if $main::has_colors;
236 $spos = @shistory if $spos > @shistory;
239 my $size = $lines . 'x' . $cols . '-';
240 my $add = "-$spos-$shl";
241 my $time = ztime(time);
242 my $str = "-" . $time . '-' x ($cols - (length($size) + length($call) + length($add) + length($time) + 1));
243 $scr->addstr($lines-4, 0, $str);
246 $scr->attrset($mycallcolor) if $main::has_colors;
248 $scr->attrset(COLOR_PAIR(0)) if $main::has_colors;
254 # add a line to the end of the top screen
259 if ($inbuf =~ s/\x07+$//) {
262 if (length $inbuf >= $cols) {
263 $Text::Wrap::Columns = $cols;
264 push @shistory, wrap('',"\t", $inbuf);
266 push @shistory, $inbuf;
268 shift @shistory while @shistory > $maxshist;
273 # handle incoming messages
276 my ($con, $msg, $err) = @_;
277 if (defined $err && $err) {
281 my ($sort, $call, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
283 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
284 if ($sort && $sort eq 'D') {
285 $line = " " unless length($line);
287 } elsif ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
290 # ******************************************************
291 # ******************************************************
292 # any other sorts that might happen are silently ignored.
293 # ******************************************************
294 # ******************************************************
308 # $prbuf =~ s/\r/\\r/;
309 # $prbuf =~ s/\n/\\n/;
310 # print "sys: $r ($prbuf)\n";
315 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
318 $inbuf = " " unless length $inbuf;
320 # check for a pling and do a search back for a command
321 if ($inbuf =~ /^!/o) {
324 for ($i = $#khistory; $i >= 0; $i--) {
325 if ($khistory[$i] =~ /^$inbuf/) {
326 $inbuf = $khistory[$i];
335 push @khistory, $inbuf if length $inbuf;
336 shift @khistory if @khistory > $maxkhist;
337 $khistpos = @khistory;
340 $bot->addstr(substr($inbuf, 0, $cols));
342 # add it to the monitor window
343 unless ($spos == @shistory) {
349 # send it to the cluster
350 $conn->send_later("I$call|$inbuf");
353 } elsif ($r eq KEY_UP || $r eq "\020") {
356 $inbuf = $khistory[$khistpos];
357 $pos = $lth = length $inbuf;
361 } elsif ($r eq KEY_DOWN || $r eq "\016") {
362 if ($khistpos < @khistory - 1) {
364 $inbuf = $khistory[$khistpos];
365 $pos = $lth = length $inbuf;
369 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
372 for ($i = 0; $i < $pagel-1 && $spos >= 0; ) {
373 $l = measure($shistory[$spos]);
375 $spos-- if $i <= $pagel;
377 $spos = 0 if $spos < 0;
382 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
383 if ($spos < @shistory - 1) {
385 for ($i = 0; $i <= $pagel && $spos <= @shistory; ) {
386 $l = measure($shistory[$spos]);
388 $spos++ if $i <= $pagel;
390 $spos = @shistory if $spos >= @shistory - 1;
395 } elsif ($r eq KEY_LEFT || $r eq "\002") {
401 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
407 } elsif ($r eq KEY_HOME || $r eq "\001") {
409 } elsif ($r eq KEY_END || $r eq "\005") {
411 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
413 my $a = substr($inbuf, 0, $pos-1);
414 my $b = substr($inbuf, $pos) if $pos < $lth;
423 } elsif ($r eq KEY_DC || $r eq "\004") {
425 my $a = substr($inbuf, 0, $pos);
426 my $b = substr($inbuf, $pos+1) if $pos < $lth;
434 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
437 } elsif (defined $r && is_pctext($r)) {
438 # move the top screen back to the bottom if you type something
439 if ($spos < @shistory) {
444 # $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
446 # insert the character into the keyboard buffer
448 my $a = substr($inbuf, 0, $pos);
449 my $b = substr($inbuf, $pos);
450 $inbuf = $a . $r . $b;
456 } elsif ($r eq "\014" || $r eq "\022") {
460 } elsif ($r eq "\013") {
461 $inbuf = substr($inbuf, 0, $pos);
462 $lth = length $inbuf;
468 $bot->addstr($inbuf);
478 if ($t > $lasttime) {
479 my ($min)= (gmtime($t))[1];
480 if ($min != $lastmin) {
486 my $ch = $bot->getch();
487 if (@time && tv_interval(\@time, [gettimeofday]) >= 1) {
488 # mydbg("Got Resize");
497 $top->refresh() if $top->is_wintouched;
505 $call = uc shift @ARGV if @ARGV;
506 $call = uc $main::myalias if !$call;
507 my ($scall, $ssid) = split /-/, $call;
508 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
510 $ssid = 15 if $ssid > 15;
511 $call = "$scall-$ssid";
514 if ($call eq $main::mycall) {
515 print "You cannot connect as your cluster callsign ($main::mycall)\n";
520 $decease = AnyEvent->condvar;
524 $conn = IntMsg->connect("$main::clusteraddr", $main::clusterport, \&rec_socket);
526 if (-r "$main::root/data/offline") {
527 open IN, "$main::root/data/offline" or die;
533 print "Sorry, the cluster $main::mycall is currently off-line\n";
539 unless ($DB::VERSION) {
540 $sigint = AnyEvent->signal(signal=>'INT', cb=> sub{$decease->send});
541 $sigterm = AnyEvent->signal(signal=>'TERM', cb=> sub{$decease->send});
544 #$SIG{'HUP'} = \&sig_term;
545 my $sighup = AnyEvent->signal(signal=>'HUP', cb=> sub{$decease->send});
546 $conn->{sock}->on_eof(sub{$decease->send});
547 $conn->{sock}->on_error(sub{$decease->send});
552 #$SIG{__DIE__} = \&sig_term;
553 #my $sigdie = AnyEvent->signal(signal=>'__DIE__', cb=> sub{$decease->send});
555 $conn->send_later("A$call|$connsort width=$cols");
556 $conn->send_later("I$call|set/page $maxshist");
557 #$conn->send_later("I$call|set/nobeep");
559 #Msg->set_event_handler(\*STDIN, "read" => \&rec_stdin);
561 $Text::Wrap::Columns = $cols;
563 my $event_loop = AnyEvent->timer(after => 0, interval => 0.010, cb => sub{idle_loop()});