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);
44 $call = ""; # the callsign being used
45 $conn = 0; # the connection object for the cluster
46 $lasttime = time; # lasttime something happened on the interface
52 $spos = $pos = $lth = 0;
56 $SIG{WINCH} = sub {@time = gettimeofday};
60 local *STDOUT = undef;
64 # do the screen initialisation
70 init_pair("0", $foreground, $background);
71 # init_pair(0, $background, $foreground);
72 init_pair(1, COLOR_RED, $background);
73 init_pair(2, COLOR_YELLOW, $background);
74 init_pair(3, COLOR_GREEN, $background);
75 init_pair(4, COLOR_CYAN, $background);
76 init_pair(5, COLOR_BLUE, $background);
77 init_pair(6, COLOR_MAGENTA, $background);
78 init_pair(7, COLOR_RED, COLOR_BLUE);
79 init_pair(8, COLOR_YELLOW, COLOR_BLUE);
80 init_pair(9, COLOR_GREEN, COLOR_BLUE);
81 init_pair(10, COLOR_CYAN, COLOR_BLUE);
82 init_pair(11, COLOR_BLUE, COLOR_RED);
83 init_pair(12, COLOR_MAGENTA, COLOR_BLUE);
84 init_pair(13, COLOR_YELLOW, COLOR_GREEN);
85 init_pair(14, COLOR_RED, COLOR_GREEN);
86 eval { assume_default_colors($foreground, $background) };
89 $top = $scr->subwin($lines-4, $cols, 0, 0);
94 # $scr->addstr($lines-4, 0, '-' x $cols);
95 $bot = $scr->subwin(3, $cols, $lines-3, 0);
106 $mycallcolor = COLOR_PAIR(1) unless $mycallcolor;
118 $has_colors = has_colors();
124 # cease communications
128 $conn->disconnect if $conn;
135 # terminate program from signal
141 # determine the colour of the line
145 foreach my $ref (@colors) {
146 if ($_[0] =~ m{$$ref[0]}) {
147 $top->attrset($$ref[1]);
154 # measure the no of screen lines a line will take
158 return 0 unless $line;
160 my $l = length $line;
161 my $lines = int ($l / $cols);
162 $lines++ if $l / $cols > $lines;
166 # display the top screen
169 if ($spos == @shistory - 1) {
171 # if we really are scrolling thru at the end of the history
172 my $line = $shistory[$spos];
173 $top->addstr("\n") if $spos > 0;
176 # $top->addstr("\n");
177 $top->attrset(COLOR_PAIR(0)) if $has_colors;
185 for ($i = 0; $i < $pagel && $p >= 0; ) {
186 $l = measure($shistory[$p]);
193 $top->attrset(COLOR_PAIR(0)) if $has_colors;
195 for ($i = 0; $i < $pagel && $p < @shistory; $p++) {
196 my $line = $shistory[$p];
197 my $lines = measure($line);
198 last if $i + $lines > $pagel;
199 $top->addstr("\n") if $i;
202 $top->attrset(COLOR_PAIR(0)) if $has_colors;
206 $spos = @shistory if $spos > @shistory;
209 my $size = $lines . 'x' . $cols . '-';
210 my $add = "-$spos-$shl";
211 my $time = ztime(time);
212 my $str = "-" . $time . '-' x ($cols - (length($size) + length($call) + length($add) + length($time) + 1));
213 $scr->addstr($lines-4, 0, $str);
216 $scr->attrset($mycallcolor) if $has_colors;
218 $scr->attrset(COLOR_PAIR(0)) if $has_colors;
224 # add a line to the end of the top screen
229 if ($inbuf =~ s/\x07+$//) {
232 push @shistory, $inbuf;
233 shift @shistory if @shistory > $maxshist;
238 # handle incoming messages
241 my ($con, $msg, $err) = @_;
242 if (defined $err && $err) {
246 my ($sort, $call, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
248 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
249 if ($sort && $sort eq 'D') {
250 $line = " " unless length($line);
252 } elsif ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
255 # ******************************************************
256 # ******************************************************
257 # any other sorts that might happen are silently ignored.
258 # ******************************************************
259 # ******************************************************
273 # $prbuf =~ s/\r/\\r/;
274 # $prbuf =~ s/\n/\\n/;
275 # print "sys: $r ($prbuf)\n";
280 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
283 $inbuf = " " unless length $inbuf;
285 # check for a pling and do a search back for a command
286 if ($inbuf =~ /^!/o) {
289 for ($i = $#khistory; $i >= 0; $i--) {
290 if ($khistory[$i] =~ /^$inbuf/) {
291 $inbuf = $khistory[$i];
300 push @khistory, $inbuf if length $inbuf;
301 shift @khistory if @khistory > $maxkhist;
302 $khistpos = @khistory;
305 $bot->addstr(substr($inbuf, 0, $cols));
307 # add it to the monitor window
308 unless ($spos == @shistory) {
314 # send it to the cluster
315 $conn->send_later("I$call|$inbuf");
318 } elsif ($r eq KEY_UP || $r eq "\020") {
321 $inbuf = $khistory[$khistpos];
322 $pos = $lth = length $inbuf;
326 } elsif ($r eq KEY_DOWN || $r eq "\016") {
327 if ($khistpos < @khistory - 1) {
329 $inbuf = $khistory[$khistpos];
330 $pos = $lth = length $inbuf;
334 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
337 for ($i = 0; $i <= $pagel-1 && $spos >= 0; ) {
338 $l = measure($shistory[$spos]);
340 $spos-- if $i <= $pagel;
342 $spos = 0 if $spos < 0;
347 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
348 if ($spos < @shistory - 1) {
350 for ($i = 0; $i <= $pagel && $spos <= @shistory; ) {
351 $l = measure($shistory[$spos]);
353 $spos++ if $i <= $pagel;
355 $spos = @shistory if $spos >= @shistory - 1;
360 } elsif ($r eq KEY_LEFT || $r eq "\002") {
366 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
372 } elsif ($r eq KEY_HOME || $r eq "\001") {
374 } elsif ($r eq KEY_END || $r eq "\005") {
376 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
378 my $a = substr($inbuf, 0, $pos-1);
379 my $b = substr($inbuf, $pos) if $pos < $lth;
388 } elsif ($r eq KEY_DC || $r eq "\004") {
390 my $a = substr($inbuf, 0, $pos);
391 my $b = substr($inbuf, $pos+1) if $pos < $lth;
399 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
402 } elsif (defined $r && is_pctext($r)) {
403 # move the top screen back to the bottom if you type something
404 if ($spos < @shistory) {
409 # $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
411 # insert the character into the keyboard buffer
413 my $a = substr($inbuf, 0, $pos);
414 my $b = substr($inbuf, $pos);
415 $inbuf = $a . $r . $b;
421 } elsif ($r eq "\014" || $r eq "\022") {
425 } elsif ($r eq "\013") {
426 $inbuf = substr($inbuf, 0, $pos);
427 $lth = length $inbuf;
433 $bot->addstr($inbuf);
444 $call = uc shift @ARGV if @ARGV;
445 $call = uc $myalias if !$call;
446 my ($scall, $ssid) = split /-/, $call;
447 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
449 $ssid = 15 if $ssid > 15;
450 $call = "$scall-$ssid";
453 if ($call eq $mycall) {
454 print "You cannot connect as your cluster callsign ($mycall)\n";
460 $conn = IntMsg->connect("$clusteraddr", $clusterport, \&rec_socket);
462 if (-r "$data/offline") {
463 open IN, "$data/offline" or die;
469 print "Sorry, the cluster $mycall is currently off-line\n";
474 $conn->set_error(sub{cease(0)});
477 unless ($DB::VERSION) {
478 $SIG{'INT'} = \&sig_term;
479 $SIG{'TERM'} = \&sig_term;
482 $SIG{'HUP'} = \&sig_term;
487 $SIG{__DIE__} = \&sig_term;
489 $conn->send_later("A$call|$connsort width=$cols");
490 $conn->send_later("I$call|set/page $maxshist");
491 #$conn->send_later("I$call|set/nobeep");
493 #Msg->set_event_handler(\*STDIN, "read" => \&rec_stdin);
498 Msg->event_loop(1, 0.01);
500 if ($t > $lasttime) {
501 my ($min)= (gmtime($t))[1];
502 if ($min != $lastmin) {
508 my $ch = $bot->getch();
509 if (@time && tv_interval(\@time, [gettimeofday]) >= 1) {
510 # mydbg("Got Resize");
519 $top->refresh() if $top->is_wintouched;