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";
35 use Time::HiRes qw(gettimeofday tv_interval);
45 $call = ""; # the callsign being used
46 $conn = 0; # the connection object for the cluster
47 $lasttime = time; # lasttime something happened on the interface
53 $spos = $pos = $lth = 0;
57 $SIG{WINCH} = sub {@time = gettimeofday};
61 local *STDOUT = undef;
65 # do the screen initialisation
71 init_pair("0", $foreground, $background);
72 # init_pair(0, $background, $foreground);
73 init_pair(1, COLOR_RED, $background);
74 init_pair(2, COLOR_YELLOW, $background);
75 init_pair(3, COLOR_GREEN, $background);
76 init_pair(4, COLOR_CYAN, $background);
77 init_pair(5, COLOR_BLUE, $background);
78 init_pair(6, COLOR_MAGENTA, $background);
79 init_pair(7, COLOR_RED, COLOR_BLUE);
80 init_pair(8, COLOR_YELLOW, COLOR_BLUE);
81 init_pair(9, COLOR_GREEN, COLOR_BLUE);
82 init_pair(10, COLOR_CYAN, COLOR_BLUE);
83 init_pair(11, COLOR_BLUE, COLOR_RED);
84 init_pair(12, COLOR_MAGENTA, COLOR_BLUE);
85 init_pair(13, COLOR_YELLOW, COLOR_GREEN);
86 init_pair(14, COLOR_RED, COLOR_GREEN);
87 eval { assume_default_colors($foreground, $background) };
90 $top = $scr->subwin($lines-4, $cols, 0, 0);
95 # $scr->addstr($lines-4, 0, '-' x $cols);
96 $bot = $scr->subwin(3, $cols, $lines-3, 0);
107 $mycallcolor = COLOR_PAIR(1) unless $mycallcolor;
119 $has_colors = has_colors();
125 # cease communications
129 $conn->disconnect if $conn;
136 # terminate program from signal
142 # determine the colour of the line
146 foreach my $ref (@colors) {
147 if ($_[0] =~ m{$$ref[0]}) {
148 $top->attrset($$ref[1]);
155 # measure the no of screen lines a line will take
159 return 0 unless $line;
161 my $l = length $line;
162 my $lines = int ($l / $cols);
163 $lines++ if $l / $cols > $lines;
167 # display the top screen
170 if ($spos == @shistory - 1) {
172 # if we really are scrolling thru at the end of the history
173 my $line = $shistory[$spos];
174 $top->addstr("\n") if $spos > 0;
177 # $top->addstr("\n");
178 $top->attrset(COLOR_PAIR(0)) if $has_colors;
186 for ($i = 0; $i < $pagel && $p >= 0; ) {
187 $l = measure($shistory[$p]);
194 $top->attrset(COLOR_PAIR(0)) if $has_colors;
196 for ($i = 0; $i < $pagel && $p < @shistory; $p++) {
197 my $line = $shistory[$p];
198 my $lines = measure($line);
199 last if $i + $lines > $pagel;
200 $top->addstr("\n") if $i;
203 $top->attrset(COLOR_PAIR(0)) if $has_colors;
207 $spos = @shistory if $spos > @shistory;
210 my $size = $lines . 'x' . $cols . '-';
211 my $add = "-$spos-$shl";
212 my $time = ztime(time);
213 my $str = "-" . $time . '-' x ($cols - (length($size) + length($call) + length($add) + length($time) + 1));
214 $scr->addstr($lines-4, 0, $str);
217 $scr->attrset($mycallcolor) if $has_colors;
219 $scr->attrset(COLOR_PAIR(0)) if $has_colors;
225 # add a line to the end of the top screen
230 if ($inbuf =~ s/\x07+$//) {
233 if (length $inbuf > $cols) {
234 $Text::Wrap::Columns = $cols;
235 push @shistory, wrap('',"\t", $inbuf);
237 push @shistory, $inbuf;
239 shift @shistory while @shistory > $maxshist;
244 # handle incoming messages
247 my ($con, $msg, $err) = @_;
248 if (defined $err && $err) {
252 my ($sort, $call, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
254 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
255 if ($sort && $sort eq 'D') {
256 $line = " " unless length($line);
258 } elsif ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
261 # ******************************************************
262 # ******************************************************
263 # any other sorts that might happen are silently ignored.
264 # ******************************************************
265 # ******************************************************
279 # $prbuf =~ s/\r/\\r/;
280 # $prbuf =~ s/\n/\\n/;
281 # print "sys: $r ($prbuf)\n";
286 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
289 $inbuf = " " unless length $inbuf;
291 # check for a pling and do a search back for a command
292 if ($inbuf =~ /^!/o) {
295 for ($i = $#khistory; $i >= 0; $i--) {
296 if ($khistory[$i] =~ /^$inbuf/) {
297 $inbuf = $khistory[$i];
306 push @khistory, $inbuf if length $inbuf;
307 shift @khistory if @khistory > $maxkhist;
308 $khistpos = @khistory;
311 $bot->addstr(substr($inbuf, 0, $cols));
313 # add it to the monitor window
314 unless ($spos == @shistory) {
320 # send it to the cluster
321 $conn->send_later("I$call|$inbuf");
324 } elsif ($r eq KEY_UP || $r eq "\020") {
327 $inbuf = $khistory[$khistpos];
328 $pos = $lth = length $inbuf;
332 } elsif ($r eq KEY_DOWN || $r eq "\016") {
333 if ($khistpos < @khistory - 1) {
335 $inbuf = $khistory[$khistpos];
336 $pos = $lth = length $inbuf;
340 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
343 for ($i = 0; $i < $pagel-1 && $spos >= 0; ) {
344 $l = measure($shistory[$spos]);
346 $spos-- if $i <= $pagel;
348 $spos = 0 if $spos < 0;
353 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
354 if ($spos < @shistory - 1) {
356 for ($i = 0; $i <= $pagel && $spos <= @shistory; ) {
357 $l = measure($shistory[$spos]);
359 $spos++ if $i <= $pagel;
361 $spos = @shistory if $spos >= @shistory - 1;
366 } elsif ($r eq KEY_LEFT || $r eq "\002") {
372 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
378 } elsif ($r eq KEY_HOME || $r eq "\001") {
380 } elsif ($r eq KEY_END || $r eq "\005") {
382 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
384 my $a = substr($inbuf, 0, $pos-1);
385 my $b = substr($inbuf, $pos) if $pos < $lth;
394 } elsif ($r eq KEY_DC || $r eq "\004") {
396 my $a = substr($inbuf, 0, $pos);
397 my $b = substr($inbuf, $pos+1) if $pos < $lth;
405 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
408 } elsif (defined $r && is_pctext($r)) {
409 # move the top screen back to the bottom if you type something
410 if ($spos < @shistory) {
415 # $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
417 # insert the character into the keyboard buffer
419 my $a = substr($inbuf, 0, $pos);
420 my $b = substr($inbuf, $pos);
421 $inbuf = $a . $r . $b;
427 } elsif ($r eq "\014" || $r eq "\022") {
431 } elsif ($r eq "\013") {
432 $inbuf = substr($inbuf, 0, $pos);
433 $lth = length $inbuf;
439 $bot->addstr($inbuf);
450 $call = uc shift @ARGV if @ARGV;
451 $call = uc $myalias if !$call;
452 my ($scall, $ssid) = split /-/, $call;
453 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
455 $ssid = 15 if $ssid > 15;
456 $call = "$scall-$ssid";
459 if ($call eq $mycall) {
460 print "You cannot connect as your cluster callsign ($mycall)\n";
466 $conn = IntMsg->connect("$clusteraddr", $clusterport, \&rec_socket);
468 if (-r "$data/offline") {
469 open IN, "$data/offline" or die;
475 print "Sorry, the cluster $mycall is currently off-line\n";
480 $conn->set_error(sub{cease(0)});
483 unless ($DB::VERSION) {
484 $SIG{'INT'} = \&sig_term;
485 $SIG{'TERM'} = \&sig_term;
488 $SIG{'HUP'} = \&sig_term;
493 $SIG{__DIE__} = \&sig_term;
495 $conn->send_later("A$call|$connsort width=$cols");
496 $conn->send_later("I$call|set/page $maxshist");
497 #$conn->send_later("I$call|set/nobeep");
499 #Msg->set_event_handler(\*STDIN, "read" => \&rec_stdin);
501 $Text::Wrap::Columns = $cols;
506 Msg->event_loop(1, 0.01);
508 if ($t > $lasttime) {
509 my ($min)= (gmtime($t))[1];
510 if ($min != $lastmin) {
516 my $ch = $bot->getch();
517 if (@time && tv_interval(\@time, [gettimeofday]) >= 1) {
518 # mydbg("Got Resize");
527 $top->refresh() if $top->is_wintouched;