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
19 use vars qw($data $clusteraddr $clusterport);
21 $clusteraddr = '127.0.0.1'; # cluster tcp host address - used for things like console.pl
22 $clusterport = 27754; # cluster tcp port
24 # search local then perl directories
26 # root of directory tree for this system
28 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
30 unshift @INC, "$root/perl"; # this IS the right way round!
31 unshift @INC, "$root/local";
32 $is_win = ($^O =~ /^MS/ || $^O =~ /^OS-2/) ? 1 : 0; # is it Windows?
43 use Time::HiRes qw(gettimeofday tv_interval);
53 $call = ""; # the callsign being used
54 $conn = 0; # the connection object for the cluster
55 $lasttime = time; # lasttime something happened on the interface
61 $spos = $pos = $lth = 0;
65 #$SIG{WINCH} = sub {@time = gettimeofday};
69 local *STDOUT = undef;
73 # do the screen initialisation
79 init_pair("0", $foreground, $background);
80 # init_pair(0, $background, $foreground);
81 init_pair(1, COLOR_RED, $background);
82 init_pair(2, COLOR_YELLOW, $background);
83 init_pair(3, COLOR_GREEN, $background);
84 init_pair(4, COLOR_CYAN, $background);
85 init_pair(5, COLOR_BLUE, $background);
86 init_pair(6, COLOR_MAGENTA, $background);
87 init_pair(7, COLOR_RED, COLOR_BLUE);
88 init_pair(8, COLOR_YELLOW, COLOR_BLUE);
89 init_pair(9, COLOR_GREEN, COLOR_BLUE);
90 init_pair(10, COLOR_CYAN, COLOR_BLUE);
91 init_pair(11, COLOR_BLUE, COLOR_RED);
92 init_pair(12, COLOR_MAGENTA, COLOR_BLUE);
93 init_pair(13, COLOR_YELLOW, COLOR_GREEN);
94 init_pair(14, COLOR_RED, COLOR_GREEN);
95 eval { assume_default_colors($foreground, $background) } unless $is_win;
98 $top = $scr->subwin($lines-4, $cols, 0, 0);
103 # $scr->addstr($lines-4, 0, '-' x $cols);
104 $bot = $scr->subwin(3, $cols, $lines-3, 0);
115 $mycallcolor = COLOR_PAIR(1) unless $mycallcolor;
127 $has_colors = has_colors();
133 # cease communications
137 $conn->disconnect if $conn;
144 # terminate program from signal
150 # determine the colour of the line
154 foreach my $ref (@colors) {
155 if ($_[0] =~ m{$$ref[0]}) {
156 $top->attrset($$ref[1]);
163 # measure the no of screen lines a line will take
167 return 0 unless $line;
169 my $l = length $line;
170 my $lines = int ($l / $cols);
171 $lines++ if $l / $cols > $lines;
175 # display the top screen
178 if ($spos == @shistory - 1) {
180 # if we really are scrolling thru at the end of the history
181 my $line = $shistory[$spos];
182 $top->addstr("\n") if $spos > 0;
185 # $top->addstr("\n");
186 $top->attrset(COLOR_PAIR(0)) if $has_colors;
194 for ($i = 0; $i < $pagel && $p >= 0; ) {
195 $l = measure($shistory[$p]);
202 $top->attrset(COLOR_PAIR(0)) if $has_colors;
204 for ($i = 0; $i < $pagel && $p < @shistory; $p++) {
205 my $line = $shistory[$p];
206 my $lines = measure($line);
207 last if $i + $lines > $pagel;
208 $top->addstr("\n") if $i;
211 $top->attrset(COLOR_PAIR(0)) if $has_colors;
215 $spos = @shistory if $spos > @shistory;
218 my $size = $lines . 'x' . $cols . '-';
219 my $add = "-$spos-$shl";
220 my $time = ztime(time);
221 my $str = "-" . $time . '-' x ($cols - (length($size) + length($call) + length($add) + length($time) + 1));
222 $scr->addstr($lines-4, 0, $str);
225 $scr->attrset($mycallcolor) if $has_colors;
227 $scr->attrset(COLOR_PAIR(0)) if $has_colors;
233 # add a line to the end of the top screen
238 if ($inbuf =~ s/\x07+$//) {
241 if (length $inbuf >= $cols) {
242 $Text::Wrap::Columns = $cols;
243 push @shistory, wrap('',"\t", $inbuf);
245 push @shistory, $inbuf;
247 shift @shistory while @shistory > $maxshist;
252 # handle incoming messages
255 my ($con, $msg, $err) = @_;
256 if (defined $err && $err) {
260 my ($sort, $call, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
262 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
263 if ($sort && $sort eq 'D') {
264 $line = " " unless length($line);
266 } elsif ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
269 # ******************************************************
270 # ******************************************************
271 # any other sorts that might happen are silently ignored.
272 # ******************************************************
273 # ******************************************************
287 # $prbuf =~ s/\r/\\r/;
288 # $prbuf =~ s/\n/\\n/;
289 # print "sys: $r ($prbuf)\n";
294 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
297 $inbuf = " " unless length $inbuf;
299 # check for a pling and do a search back for a command
300 if ($inbuf =~ /^!/o) {
303 for ($i = $#khistory; $i >= 0; $i--) {
304 if ($khistory[$i] =~ /^$inbuf/) {
305 $inbuf = $khistory[$i];
314 push @khistory, $inbuf if length $inbuf;
315 shift @khistory if @khistory > $maxkhist;
316 $khistpos = @khistory;
319 $bot->addstr(substr($inbuf, 0, $cols));
321 # add it to the monitor window
322 unless ($spos == @shistory) {
328 # send it to the cluster
329 $conn->send_later("I$call|$inbuf");
332 } elsif ($r eq KEY_UP || $r eq "\020") {
335 $inbuf = $khistory[$khistpos];
336 $pos = $lth = length $inbuf;
340 } elsif ($r eq KEY_DOWN || $r eq "\016") {
341 if ($khistpos < @khistory - 1) {
343 $inbuf = $khistory[$khistpos];
344 $pos = $lth = length $inbuf;
348 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
351 for ($i = 0; $i < $pagel-1 && $spos >= 0; ) {
352 $l = measure($shistory[$spos]);
354 $spos-- if $i <= $pagel;
356 $spos = 0 if $spos < 0;
361 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
362 if ($spos < @shistory - 1) {
364 for ($i = 0; $i <= $pagel && $spos <= @shistory; ) {
365 $l = measure($shistory[$spos]);
367 $spos++ if $i <= $pagel;
369 $spos = @shistory if $spos >= @shistory - 1;
374 } elsif ($r eq KEY_LEFT || $r eq "\002") {
380 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
386 } elsif ($r eq KEY_HOME || $r eq "\001") {
388 } elsif ($r eq KEY_END || $r eq "\005") {
390 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
392 my $a = substr($inbuf, 0, $pos-1);
393 my $b = substr($inbuf, $pos) if $pos < $lth;
402 } elsif ($r eq KEY_DC || $r eq "\004") {
404 my $a = substr($inbuf, 0, $pos);
405 my $b = substr($inbuf, $pos+1) if $pos < $lth;
413 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
416 } elsif (defined $r && is_pctext($r)) {
417 # move the top screen back to the bottom if you type something
418 if ($spos < @shistory) {
423 # $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
425 # insert the character into the keyboard buffer
427 my $a = substr($inbuf, 0, $pos);
428 my $b = substr($inbuf, $pos);
429 $inbuf = $a . $r . $b;
435 } elsif ($r eq "\014" || $r eq "\022") {
439 } elsif ($r eq "\013") {
440 $inbuf = substr($inbuf, 0, $pos);
441 $lth = length $inbuf;
447 $bot->addstr($inbuf);
458 $call = uc shift @ARGV if @ARGV;
459 $call = uc $myalias if !$call;
460 my ($scall, $ssid) = split /-/, $call;
461 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
463 $ssid = 15 if $ssid > 15;
464 $call = "$scall-$ssid";
467 if ($call eq $mycall) {
468 print "You cannot connect as your cluster callsign ($mycall)\n";
474 $conn = IntMsg->connect("$clusteraddr", $clusterport, \&rec_socket);
476 if (-r "$data/offline") {
477 open IN, "$data/offline" or die;
483 print "Sorry, the cluster $mycall is currently off-line\n";
488 $conn->set_error(sub{cease(0)});
491 unless ($DB::VERSION) {
492 $SIG{'INT'} = \&sig_term;
493 $SIG{'TERM'} = \&sig_term;
496 $SIG{'HUP'} = \&sig_term;
501 $SIG{__DIE__} = \&sig_term;
503 $conn->send_later("A$call|$connsort width=$cols");
504 $conn->send_later("I$call|set/page $maxshist");
505 #$conn->send_later("I$call|set/nobeep");
507 #Msg->set_event_handler(\*STDIN, "read" => \&rec_stdin);
509 $Text::Wrap::Columns = $cols;
514 Msg->event_loop(1, 0.01);
516 if ($t > $lasttime) {
517 my ($min)= (gmtime($t))[1];
518 if ($min != $lastmin) {
524 my $ch = $bot->getch();
525 if (@time && tv_interval(\@time, [gettimeofday]) >= 1) {
526 # mydbg("Got Resize");
535 $top->refresh() if $top->is_wintouched;