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
21 # search local then perl directories
23 # root of directory tree for this system
25 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
27 unshift @INC, "$root/perl"; # this IS the right way round!
28 unshift @INC, "$root/local";
29 $is_win = ($^O =~ /^MS/ || $^O =~ /^OS-2/) ? 1 : 0; # is it Windows?
40 use Time::HiRes qw(gettimeofday tv_interval);
50 $call = ""; # the callsign being used
51 $conn = 0; # the connection object for the cluster
52 $lasttime = time; # lasttime something happened on the interface
58 $spos = $pos = $lth = 0;
62 #$SIG{WINCH} = sub {@time = gettimeofday};
66 local *STDOUT = undef;
70 # do the screen initialisation
76 init_pair("0", $foreground, $background);
77 # init_pair(0, $background, $foreground);
78 init_pair(1, COLOR_RED, $background);
79 init_pair(2, COLOR_YELLOW, $background);
80 init_pair(3, COLOR_GREEN, $background);
81 init_pair(4, COLOR_CYAN, $background);
82 init_pair(5, COLOR_BLUE, $background);
83 init_pair(6, COLOR_MAGENTA, $background);
84 init_pair(7, COLOR_RED, COLOR_BLUE);
85 init_pair(8, COLOR_YELLOW, COLOR_BLUE);
86 init_pair(9, COLOR_GREEN, COLOR_BLUE);
87 init_pair(10, COLOR_CYAN, COLOR_BLUE);
88 init_pair(11, COLOR_BLUE, COLOR_RED);
89 init_pair(12, COLOR_MAGENTA, COLOR_BLUE);
90 init_pair(13, COLOR_YELLOW, COLOR_GREEN);
91 init_pair(14, COLOR_RED, COLOR_GREEN);
92 eval { assume_default_colors($foreground, $background) } unless $is_win;
95 $top = $scr->subwin($lines-4, $cols, 0, 0);
100 # $scr->addstr($lines-4, 0, '-' x $cols);
101 $bot = $scr->subwin(3, $cols, $lines-3, 0);
112 $mycallcolor = COLOR_PAIR(1) unless $mycallcolor;
124 $has_colors = has_colors();
130 # cease communications
134 $conn->disconnect if $conn;
141 # terminate program from signal
147 # determine the colour of the line
151 foreach my $ref (@colors) {
152 if ($_[0] =~ m{$$ref[0]}) {
153 $top->attrset($$ref[1]);
160 # measure the no of screen lines a line will take
164 return 0 unless $line;
166 my $l = length $line;
167 my $lines = int ($l / $cols);
168 $lines++ if $l / $cols > $lines;
172 # display the top screen
175 if ($spos == @shistory - 1) {
177 # if we really are scrolling thru at the end of the history
178 my $line = $shistory[$spos];
179 $top->addstr("\n") if $spos > 0;
182 # $top->addstr("\n");
183 $top->attrset(COLOR_PAIR(0)) if $has_colors;
191 for ($i = 0; $i < $pagel && $p >= 0; ) {
192 $l = measure($shistory[$p]);
199 $top->attrset(COLOR_PAIR(0)) if $has_colors;
201 for ($i = 0; $i < $pagel && $p < @shistory; $p++) {
202 my $line = $shistory[$p];
203 my $lines = measure($line);
204 last if $i + $lines > $pagel;
205 $top->addstr("\n") if $i;
208 $top->attrset(COLOR_PAIR(0)) if $has_colors;
212 $spos = @shistory if $spos > @shistory;
215 my $size = $lines . 'x' . $cols . '-';
216 my $add = "-$spos-$shl";
217 my $time = ztime(time);
218 my $str = "-" . $time . '-' x ($cols - (length($size) + length($call) + length($add) + length($time) + 1));
219 $scr->addstr($lines-4, 0, $str);
222 $scr->attrset($mycallcolor) if $has_colors;
224 $scr->attrset(COLOR_PAIR(0)) if $has_colors;
230 # add a line to the end of the top screen
235 if ($inbuf =~ s/\x07+$//) {
238 if (length $inbuf >= $cols) {
239 $Text::Wrap::Columns = $cols;
240 push @shistory, wrap('',"\t", $inbuf);
242 push @shistory, $inbuf;
244 shift @shistory while @shistory > $maxshist;
249 # handle incoming messages
252 my ($con, $msg, $err) = @_;
253 if (defined $err && $err) {
257 my ($sort, $call, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
259 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
260 if ($sort && $sort eq 'D') {
261 $line = " " unless length($line);
263 } elsif ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
266 # ******************************************************
267 # ******************************************************
268 # any other sorts that might happen are silently ignored.
269 # ******************************************************
270 # ******************************************************
284 # $prbuf =~ s/\r/\\r/;
285 # $prbuf =~ s/\n/\\n/;
286 # print "sys: $r ($prbuf)\n";
291 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
294 $inbuf = " " unless length $inbuf;
296 # check for a pling and do a search back for a command
297 if ($inbuf =~ /^!/o) {
300 for ($i = $#khistory; $i >= 0; $i--) {
301 if ($khistory[$i] =~ /^$inbuf/) {
302 $inbuf = $khistory[$i];
311 push @khistory, $inbuf if length $inbuf;
312 shift @khistory if @khistory > $maxkhist;
313 $khistpos = @khistory;
316 $bot->addstr(substr($inbuf, 0, $cols));
318 # add it to the monitor window
319 unless ($spos == @shistory) {
325 # send it to the cluster
326 $conn->send_later("I$call|$inbuf");
329 } elsif ($r eq KEY_UP || $r eq "\020") {
332 $inbuf = $khistory[$khistpos];
333 $pos = $lth = length $inbuf;
337 } elsif ($r eq KEY_DOWN || $r eq "\016") {
338 if ($khistpos < @khistory - 1) {
340 $inbuf = $khistory[$khistpos];
341 $pos = $lth = length $inbuf;
345 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
348 for ($i = 0; $i < $pagel-1 && $spos >= 0; ) {
349 $l = measure($shistory[$spos]);
351 $spos-- if $i <= $pagel;
353 $spos = 0 if $spos < 0;
358 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
359 if ($spos < @shistory - 1) {
361 for ($i = 0; $i <= $pagel && $spos <= @shistory; ) {
362 $l = measure($shistory[$spos]);
364 $spos++ if $i <= $pagel;
366 $spos = @shistory if $spos >= @shistory - 1;
371 } elsif ($r eq KEY_LEFT || $r eq "\002") {
377 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
383 } elsif ($r eq KEY_HOME || $r eq "\001") {
385 } elsif ($r eq KEY_END || $r eq "\005") {
387 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
389 my $a = substr($inbuf, 0, $pos-1);
390 my $b = substr($inbuf, $pos) if $pos < $lth;
399 } elsif ($r eq KEY_DC || $r eq "\004") {
401 my $a = substr($inbuf, 0, $pos);
402 my $b = substr($inbuf, $pos+1) if $pos < $lth;
410 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
413 } elsif (defined $r && is_pctext($r)) {
414 # move the top screen back to the bottom if you type something
415 if ($spos < @shistory) {
420 # $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
422 # insert the character into the keyboard buffer
424 my $a = substr($inbuf, 0, $pos);
425 my $b = substr($inbuf, $pos);
426 $inbuf = $a . $r . $b;
432 } elsif ($r eq "\014" || $r eq "\022") {
436 } elsif ($r eq "\013") {
437 $inbuf = substr($inbuf, 0, $pos);
438 $lth = length $inbuf;
444 $bot->addstr($inbuf);
455 $call = uc shift @ARGV if @ARGV;
456 $call = uc $myalias if !$call;
457 my ($scall, $ssid) = split /-/, $call;
458 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
460 $ssid = 15 if $ssid > 15;
461 $call = "$scall-$ssid";
464 if ($call eq $mycall) {
465 print "You cannot connect as your cluster callsign ($mycall)\n";
471 $conn = IntMsg->connect("$clusteraddr", $clusterport, \&rec_socket);
473 if (-r "$data/offline") {
474 open IN, "$data/offline" or die;
480 print "Sorry, the cluster $mycall is currently off-line\n";
485 $conn->set_error(sub{cease(0)});
488 unless ($DB::VERSION) {
489 $SIG{'INT'} = \&sig_term;
490 $SIG{'TERM'} = \&sig_term;
493 $SIG{'HUP'} = \&sig_term;
498 $SIG{__DIE__} = \&sig_term;
500 $conn->send_later("A$call|$connsort width=$cols");
501 $conn->send_later("I$call|set/page $maxshist");
502 #$conn->send_later("I$call|set/nobeep");
504 #Msg->set_event_handler(\*STDIN, "read" => \&rec_stdin);
506 $Text::Wrap::Columns = $cols;
511 Msg->event_loop(1, 0.01);
513 if ($t > $lasttime) {
514 my ($min)= (gmtime($t))[1];
515 if ($min != $lastmin) {
521 my $ch = $bot->getch();
522 if (@time && tv_interval(\@time, [gettimeofday]) >= 1) {
523 # mydbg("Got Resize");
532 $top->refresh() if $top->is_wintouched;