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?
36 use Time::HiRes qw(gettimeofday tv_interval);
46 $call = ""; # the callsign being used
47 $conn = 0; # the connection object for the cluster
48 $lasttime = time; # lasttime something happened on the interface
54 $spos = $pos = $lth = 0;
58 #$SIG{WINCH} = sub {@time = gettimeofday};
62 local *STDOUT = undef;
66 # do the screen initialisation
72 init_pair("0", $foreground, $background);
73 # init_pair(0, $background, $foreground);
74 init_pair(1, COLOR_RED, $background);
75 init_pair(2, COLOR_YELLOW, $background);
76 init_pair(3, COLOR_GREEN, $background);
77 init_pair(4, COLOR_CYAN, $background);
78 init_pair(5, COLOR_BLUE, $background);
79 init_pair(6, COLOR_MAGENTA, $background);
80 init_pair(7, COLOR_RED, COLOR_BLUE);
81 init_pair(8, COLOR_YELLOW, COLOR_BLUE);
82 init_pair(9, COLOR_GREEN, COLOR_BLUE);
83 init_pair(10, COLOR_CYAN, COLOR_BLUE);
84 init_pair(11, COLOR_BLUE, COLOR_RED);
85 init_pair(12, COLOR_MAGENTA, COLOR_BLUE);
86 init_pair(13, COLOR_YELLOW, COLOR_GREEN);
87 init_pair(14, COLOR_RED, COLOR_GREEN);
88 eval { assume_default_colors($foreground, $background) } unless $is_win;
91 $top = $scr->subwin($lines-4, $cols, 0, 0);
96 # $scr->addstr($lines-4, 0, '-' x $cols);
97 $bot = $scr->subwin(3, $cols, $lines-3, 0);
108 $mycallcolor = COLOR_PAIR(1) unless $mycallcolor;
120 $has_colors = has_colors();
126 # cease communications
130 $conn->disconnect if $conn;
137 # terminate program from signal
143 # determine the colour of the line
147 foreach my $ref (@colors) {
148 if ($_[0] =~ m{$$ref[0]}) {
149 $top->attrset($$ref[1]);
156 # measure the no of screen lines a line will take
160 return 0 unless $line;
162 my $l = length $line;
163 my $lines = int ($l / $cols);
164 $lines++ if $l / $cols > $lines;
168 # display the top screen
171 if ($spos == @shistory - 1) {
173 # if we really are scrolling thru at the end of the history
174 my $line = $shistory[$spos];
175 $top->addstr("\n") if $spos > 0;
178 # $top->addstr("\n");
179 $top->attrset(COLOR_PAIR(0)) if $has_colors;
187 for ($i = 0; $i < $pagel && $p >= 0; ) {
188 $l = measure($shistory[$p]);
195 $top->attrset(COLOR_PAIR(0)) if $has_colors;
197 for ($i = 0; $i < $pagel && $p < @shistory; $p++) {
198 my $line = $shistory[$p];
199 my $lines = measure($line);
200 last if $i + $lines > $pagel;
201 $top->addstr("\n") if $i;
204 $top->attrset(COLOR_PAIR(0)) if $has_colors;
208 $spos = @shistory if $spos > @shistory;
211 my $size = $lines . 'x' . $cols . '-';
212 my $add = "-$spos-$shl";
213 my $time = ztime(time);
214 my $str = "-" . $time . '-' x ($cols - (length($size) + length($call) + length($add) + length($time) + 1));
215 $scr->addstr($lines-4, 0, $str);
218 $scr->attrset($mycallcolor) if $has_colors;
220 $scr->attrset(COLOR_PAIR(0)) if $has_colors;
226 # add a line to the end of the top screen
231 if ($inbuf =~ s/\x07+$//) {
234 if (length $inbuf >= $cols) {
235 $Text::Wrap::Columns = $cols;
236 push @shistory, wrap('',"\t", $inbuf);
238 push @shistory, $inbuf;
240 shift @shistory while @shistory > $maxshist;
245 # handle incoming messages
248 my ($con, $msg, $err) = @_;
249 if (defined $err && $err) {
253 my ($sort, $call, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
255 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
256 if ($sort && $sort eq 'D') {
257 $line = " " unless length($line);
259 } elsif ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
262 # ******************************************************
263 # ******************************************************
264 # any other sorts that might happen are silently ignored.
265 # ******************************************************
266 # ******************************************************
280 # $prbuf =~ s/\r/\\r/;
281 # $prbuf =~ s/\n/\\n/;
282 # print "sys: $r ($prbuf)\n";
287 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
290 $inbuf = " " unless length $inbuf;
292 # check for a pling and do a search back for a command
293 if ($inbuf =~ /^!/o) {
296 for ($i = $#khistory; $i >= 0; $i--) {
297 if ($khistory[$i] =~ /^$inbuf/) {
298 $inbuf = $khistory[$i];
307 push @khistory, $inbuf if length $inbuf;
308 shift @khistory if @khistory > $maxkhist;
309 $khistpos = @khistory;
312 $bot->addstr(substr($inbuf, 0, $cols));
314 # add it to the monitor window
315 unless ($spos == @shistory) {
321 # send it to the cluster
322 $conn->send_later("I$call|$inbuf");
325 } elsif ($r eq KEY_UP || $r eq "\020") {
328 $inbuf = $khistory[$khistpos];
329 $pos = $lth = length $inbuf;
333 } elsif ($r eq KEY_DOWN || $r eq "\016") {
334 if ($khistpos < @khistory - 1) {
336 $inbuf = $khistory[$khistpos];
337 $pos = $lth = length $inbuf;
341 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
344 for ($i = 0; $i < $pagel-1 && $spos >= 0; ) {
345 $l = measure($shistory[$spos]);
347 $spos-- if $i <= $pagel;
349 $spos = 0 if $spos < 0;
354 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
355 if ($spos < @shistory - 1) {
357 for ($i = 0; $i <= $pagel && $spos <= @shistory; ) {
358 $l = measure($shistory[$spos]);
360 $spos++ if $i <= $pagel;
362 $spos = @shistory if $spos >= @shistory - 1;
367 } elsif ($r eq KEY_LEFT || $r eq "\002") {
373 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
379 } elsif ($r eq KEY_HOME || $r eq "\001") {
381 } elsif ($r eq KEY_END || $r eq "\005") {
383 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
385 my $a = substr($inbuf, 0, $pos-1);
386 my $b = substr($inbuf, $pos) if $pos < $lth;
395 } elsif ($r eq KEY_DC || $r eq "\004") {
397 my $a = substr($inbuf, 0, $pos);
398 my $b = substr($inbuf, $pos+1) if $pos < $lth;
406 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
409 } elsif (defined $r && is_pctext($r)) {
410 # move the top screen back to the bottom if you type something
411 if ($spos < @shistory) {
416 # $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
418 # insert the character into the keyboard buffer
420 my $a = substr($inbuf, 0, $pos);
421 my $b = substr($inbuf, $pos);
422 $inbuf = $a . $r . $b;
428 } elsif ($r eq "\014" || $r eq "\022") {
432 } elsif ($r eq "\013") {
433 $inbuf = substr($inbuf, 0, $pos);
434 $lth = length $inbuf;
440 $bot->addstr($inbuf);
451 $call = uc shift @ARGV if @ARGV;
452 $call = uc $myalias if !$call;
453 my ($scall, $ssid) = split /-/, $call;
454 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
456 $ssid = 15 if $ssid > 15;
457 $call = "$scall-$ssid";
460 if ($call eq $mycall) {
461 print "You cannot connect as your cluster callsign ($mycall)\n";
467 $conn = IntMsg->connect("$clusteraddr", $clusterport, \&rec_socket);
469 if (-r "$data/offline") {
470 open IN, "$data/offline" or die;
476 print "Sorry, the cluster $mycall is currently off-line\n";
481 $conn->set_error(sub{cease(0)});
484 unless ($DB::VERSION) {
485 $SIG{'INT'} = \&sig_term;
486 $SIG{'TERM'} = \&sig_term;
489 $SIG{'HUP'} = \&sig_term;
494 $SIG{__DIE__} = \&sig_term;
496 $conn->send_later("A$call|$connsort width=$cols");
497 $conn->send_later("I$call|set/page $maxshist");
498 #$conn->send_later("I$call|set/nobeep");
500 #Msg->set_event_handler(\*STDIN, "read" => \&rec_stdin);
502 $Text::Wrap::Columns = $cols;
507 Msg->event_loop(1, 0.01);
509 if ($t > $lasttime) {
510 my ($min)= (gmtime($t))[1];
511 if ($min != $lastmin) {
517 my $ch = $bot->getch();
518 if (@time && tv_interval(\@time, [gettimeofday]) >= 1) {
519 # mydbg("Got Resize");
528 $top->refresh() if $top->is_wintouched;