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;
75 #$SIG{WINCH} = sub {@time = gettimeofday};
79 local *STDOUT = undef;
83 # do the screen initialisation
87 if ($main::has_colors) {
89 init_pair("0", $main::foreground, $main::background);
90 # init_pair(0, $main::background, $main::foreground);
91 init_pair(1, COLOR_RED, $main::background);
92 init_pair(2, COLOR_YELLOW, $main::background);
93 init_pair(3, COLOR_GREEN, $main::background);
94 init_pair(4, COLOR_CYAN, $main::background);
95 init_pair(5, COLOR_BLUE, $main::background);
96 init_pair(6, COLOR_MAGENTA, $main::background);
97 init_pair(7, COLOR_RED, COLOR_BLUE);
98 init_pair(8, COLOR_YELLOW, COLOR_BLUE);
99 init_pair(9, COLOR_GREEN, COLOR_BLUE);
100 init_pair(10, COLOR_CYAN, COLOR_BLUE);
101 init_pair(11, COLOR_BLUE, COLOR_RED);
102 init_pair(12, COLOR_MAGENTA, COLOR_BLUE);
103 init_pair(13, COLOR_YELLOW, COLOR_GREEN);
104 init_pair(14, COLOR_RED, COLOR_GREEN);
105 eval { assume_default_colors($main::foreground, $main::background) } unless $main::is_win;
108 $top = $scr->subwin($lines-4, $cols, 0, 0);
113 # $scr->addstr($lines-4, 0, '-' x $cols);
114 $bot = $scr->subwin(3, $cols, $lines-3, 0);
125 $mycallcolor = COLOR_PAIR(1) unless $mycallcolor;
137 $main::has_colors = has_colors();
143 # cease communications
147 $conn->disconnect if $conn;
154 # terminate program from signal
160 # determine the colour of the line
163 if ($main::has_colors) {
164 foreach my $ref (@colors) {
165 if ($_[0] =~ m{$$ref[0]}) {
166 $top->attrset($$ref[1]);
173 # measure the no of screen lines a line will take
177 return 0 unless $line;
179 my $l = length $line;
180 my $lines = int ($l / $cols);
181 $lines++ if $l / $cols > $lines;
185 # display the top screen
188 if ($spos == @shistory - 1) {
190 # if we really are scrolling thru at the end of the history
191 my $line = $shistory[$spos];
192 $top->addstr("\n") if $spos > 0;
195 # $top->addstr("\n");
196 $top->attrset(COLOR_PAIR(0)) if $main::has_colors;
204 for ($i = 0; $i < $pagel && $p >= 0; ) {
205 $l = measure($shistory[$p]);
212 $top->attrset(COLOR_PAIR(0)) if $main::has_colors;
214 for ($i = 0; $i < $pagel && $p < @shistory; $p++) {
215 my $line = $shistory[$p];
216 my $lines = measure($line);
217 last if $i + $lines > $pagel;
218 $top->addstr("\n") if $i;
221 $top->attrset(COLOR_PAIR(0)) if $main::has_colors;
225 $spos = @shistory if $spos > @shistory;
228 my $size = $lines . 'x' . $cols . '-';
229 my $add = "-$spos-$shl";
230 my $time = ztime(time);
231 my $str = "-" . $time . '-' x ($cols - (length($size) + length($call) + length($add) + length($time) + 1));
232 $scr->addstr($lines-4, 0, $str);
235 $scr->attrset($mycallcolor) if $main::has_colors;
237 $scr->attrset(COLOR_PAIR(0)) if $main::has_colors;
243 # add a line to the end of the top screen
248 if ($inbuf =~ s/\x07+$//) {
251 if (length $inbuf >= $cols) {
252 $Text::Wrap::Columns = $cols;
253 push @shistory, wrap('',"\t", $inbuf);
255 push @shistory, $inbuf;
257 shift @shistory while @shistory > $maxshist;
262 # handle incoming messages
265 my ($con, $msg, $err) = @_;
266 if (defined $err && $err) {
270 my ($sort, $call, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
272 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
273 if ($sort && $sort eq 'D') {
274 $line = " " unless length($line);
276 } elsif ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
279 # ******************************************************
280 # ******************************************************
281 # any other sorts that might happen are silently ignored.
282 # ******************************************************
283 # ******************************************************
297 # $prbuf =~ s/\r/\\r/;
298 # $prbuf =~ s/\n/\\n/;
299 # print "sys: $r ($prbuf)\n";
304 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
307 $inbuf = " " unless length $inbuf;
309 # check for a pling and do a search back for a command
310 if ($inbuf =~ /^!/o) {
313 for ($i = $#khistory; $i >= 0; $i--) {
314 if ($khistory[$i] =~ /^$inbuf/) {
315 $inbuf = $khistory[$i];
324 push @khistory, $inbuf if length $inbuf;
325 shift @khistory if @khistory > $maxkhist;
326 $khistpos = @khistory;
329 $bot->addstr(substr($inbuf, 0, $cols));
331 # add it to the monitor window
332 unless ($spos == @shistory) {
338 # send it to the cluster
339 $conn->send_later("I$call|$inbuf");
342 } elsif ($r eq KEY_UP || $r eq "\020") {
345 $inbuf = $khistory[$khistpos];
346 $pos = $lth = length $inbuf;
350 } elsif ($r eq KEY_DOWN || $r eq "\016") {
351 if ($khistpos < @khistory - 1) {
353 $inbuf = $khistory[$khistpos];
354 $pos = $lth = length $inbuf;
358 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
361 for ($i = 0; $i < $pagel-1 && $spos >= 0; ) {
362 $l = measure($shistory[$spos]);
364 $spos-- if $i <= $pagel;
366 $spos = 0 if $spos < 0;
371 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
372 if ($spos < @shistory - 1) {
374 for ($i = 0; $i <= $pagel && $spos <= @shistory; ) {
375 $l = measure($shistory[$spos]);
377 $spos++ if $i <= $pagel;
379 $spos = @shistory if $spos >= @shistory - 1;
384 } elsif ($r eq KEY_LEFT || $r eq "\002") {
390 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
396 } elsif ($r eq KEY_HOME || $r eq "\001") {
398 } elsif ($r eq KEY_END || $r eq "\005") {
400 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
402 my $a = substr($inbuf, 0, $pos-1);
403 my $b = substr($inbuf, $pos) if $pos < $lth;
412 } elsif ($r eq KEY_DC || $r eq "\004") {
414 my $a = substr($inbuf, 0, $pos);
415 my $b = substr($inbuf, $pos+1) if $pos < $lth;
423 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
426 } elsif (defined $r && is_pctext($r)) {
427 # move the top screen back to the bottom if you type something
428 if ($spos < @shistory) {
433 # $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
435 # insert the character into the keyboard buffer
437 my $a = substr($inbuf, 0, $pos);
438 my $b = substr($inbuf, $pos);
439 $inbuf = $a . $r . $b;
445 } elsif ($r eq "\014" || $r eq "\022") {
449 } elsif ($r eq "\013") {
450 $inbuf = substr($inbuf, 0, $pos);
451 $lth = length $inbuf;
457 $bot->addstr($inbuf);
467 if ($t > $lasttime) {
468 my ($min)= (gmtime($t))[1];
469 if ($min != $lastmin) {
475 my $ch = $bot->getch();
476 if (@time && tv_interval(\@time, [gettimeofday]) >= 1) {
477 # mydbg("Got Resize");
486 $top->refresh() if $top->is_wintouched;
494 $call = uc shift @ARGV if @ARGV;
495 $call = uc $main::myalias if !$call;
496 my ($scall, $ssid) = split /-/, $call;
497 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
499 $ssid = 15 if $ssid > 15;
500 $call = "$scall-$ssid";
503 if ($call eq $main::mycall) {
504 print "You cannot connect as your cluster callsign ($main::mycall)\n";
510 $conn = IntMsg->connect("$main::clusteraddr", $main::clusterport, \&rec_socket);
512 if (-r "$main::root/data/offline") {
513 open IN, "$main::root/data/offline" or die;
519 print "Sorry, the cluster $main::mycall is currently off-line\n";
525 my $decease = AnyEvent->condvar;
527 $conn->set_error(sub{cease(0)});
529 unless ($DB::VERSION) {
530 $sigint = AnyEvent->signal(signal=>'INT', cb=> sub{$decease->send});
531 $sigterm = AnyEvent->signal(signal=>'TERM', cb=> sub{$decease->send});
534 $SIG{'HUP'} = \&sig_term;
539 $SIG{__DIE__} = \&sig_term;
541 $conn->send_later("A$call|$connsort width=$cols");
542 $conn->send_later("I$call|set/page $maxshist");
543 #$conn->send_later("I$call|set/nobeep");
545 #Msg->set_event_handler(\*STDIN, "read" => \&rec_stdin);
547 $Text::Wrap::Columns = $cols;
549 my $event_loop = AnyEvent->timer(after => 0, interval => 0.010, cb => sub{idle_loop()});