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 # search local then perl directories
21 # root of directory tree for this system
23 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
25 unshift @INC, "$root/perl"; # this IS the right way round!
26 unshift @INC, "$root/local";
27 $is_win = ($^O =~ /^MS/ || $^O =~ /^OS-2/) ? 1 : 0; # is it Windows?
39 use Time::HiRes qw(gettimeofday tv_interval);
49 $call = ""; # the callsign being used
50 $conn = 0; # the connection object for the cluster
51 $lasttime = time; # lasttime something happened on the interface
57 $spos = $pos = $lth = 0;
64 #$SIG{WINCH} = sub {@time = gettimeofday};
68 local *STDOUT = undef;
72 # do the screen initialisation
78 init_pair("0", $foreground, $background);
79 # init_pair(0, $background, $foreground);
80 init_pair(1, COLOR_RED, $background);
81 init_pair(2, COLOR_YELLOW, $background);
82 init_pair(3, COLOR_GREEN, $background);
83 init_pair(4, COLOR_CYAN, $background);
84 init_pair(5, COLOR_BLUE, $background);
85 init_pair(6, COLOR_MAGENTA, $background);
86 init_pair(7, COLOR_RED, COLOR_BLUE);
87 init_pair(8, COLOR_YELLOW, COLOR_BLUE);
88 init_pair(9, COLOR_GREEN, COLOR_BLUE);
89 init_pair(10, COLOR_CYAN, COLOR_BLUE);
90 init_pair(11, COLOR_BLUE, COLOR_RED);
91 init_pair(12, COLOR_MAGENTA, COLOR_BLUE);
92 init_pair(13, COLOR_YELLOW, COLOR_GREEN);
93 init_pair(14, COLOR_RED, COLOR_GREEN);
94 eval { assume_default_colors($foreground, $background) } unless $is_win;
97 $top = $scr->subwin($lines-4, $cols, 0, 0);
102 # $scr->addstr($lines-4, 0, '-' x $cols);
103 $bot = $scr->subwin(3, $cols, $lines-3, 0);
114 $mycallcolor = COLOR_PAIR(1) unless $mycallcolor;
126 $has_colors = has_colors();
132 # cease communications
136 $conn->disconnect if $conn;
143 # terminate program from signal
149 # determine the colour of the line
153 foreach my $ref (@colors) {
154 if ($_[0] =~ m{$$ref[0]}) {
155 $top->attrset($$ref[1]);
162 # measure the no of screen lines a line will take
166 return 0 unless $line;
168 my $l = length $line;
169 my $lines = int ($l / $cols);
170 $lines++ if $l / $cols > $lines;
174 # display the top screen
177 if ($spos == @shistory - 1) {
179 # if we really are scrolling thru at the end of the history
180 my $line = $shistory[$spos];
181 $top->addstr("\n") if $spos > 0;
184 # $top->addstr("\n");
185 $top->attrset(COLOR_PAIR(0)) if $has_colors;
193 for ($i = 0; $i < $pagel && $p >= 0; ) {
194 $l = measure($shistory[$p]);
201 $top->attrset(COLOR_PAIR(0)) if $has_colors;
203 for ($i = 0; $i < $pagel && $p < @shistory; $p++) {
204 my $line = $shistory[$p];
205 my $lines = measure($line);
206 last if $i + $lines > $pagel;
207 $top->addstr("\n") if $i;
210 $top->attrset(COLOR_PAIR(0)) if $has_colors;
214 $spos = @shistory if $spos > @shistory;
217 my $size = $lines . 'x' . $cols . '-';
218 my $add = "-$spos-$shl";
219 my $time = ztime(time);
220 my $str = "-" . $time . '-' x ($cols - (length($size) + length($call) + length($add) + length($time) + 1));
221 $scr->addstr($lines-4, 0, $str);
224 $scr->attrset($mycallcolor) if $has_colors;
226 $scr->attrset(COLOR_PAIR(0)) if $has_colors;
232 # add a line to the end of the top screen
237 if ($inbuf =~ s/\x07+$//) {
240 if (length $inbuf >= $cols) {
241 $Text::Wrap::Columns = $cols;
242 push @shistory, wrap('',"\t", $inbuf);
244 push @shistory, $inbuf;
246 shift @shistory while @shistory > $maxshist;
251 # handle incoming messages
254 my ($con, $msg, $err) = @_;
255 if (defined $err && $err) {
259 my ($sort, $call, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
261 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
262 if ($sort && $sort eq 'D') {
263 $line = " " unless length($line);
265 } elsif ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
268 # ******************************************************
269 # ******************************************************
270 # any other sorts that might happen are silently ignored.
271 # ******************************************************
272 # ******************************************************
286 # $prbuf =~ s/\r/\\r/;
287 # $prbuf =~ s/\n/\\n/;
288 # print "sys: $r ($prbuf)\n";
293 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
296 $inbuf = " " unless length $inbuf;
298 # check for a pling and do a search back for a command
299 if ($inbuf =~ /^!/o) {
302 for ($i = $#khistory; $i >= 0; $i--) {
303 if ($khistory[$i] =~ /^$inbuf/) {
304 $inbuf = $khistory[$i];
313 push @khistory, $inbuf if length $inbuf;
314 shift @khistory if @khistory > $maxkhist;
315 $khistpos = @khistory;
318 $bot->addstr(substr($inbuf, 0, $cols));
320 # add it to the monitor window
321 unless ($spos == @shistory) {
327 # send it to the cluster
328 $conn->send_later("I$call|$inbuf");
331 } elsif ($r eq KEY_UP || $r eq "\020") {
334 $inbuf = $khistory[$khistpos];
335 $pos = $lth = length $inbuf;
339 } elsif ($r eq KEY_DOWN || $r eq "\016") {
340 if ($khistpos < @khistory - 1) {
342 $inbuf = $khistory[$khistpos];
343 $pos = $lth = length $inbuf;
347 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
350 for ($i = 0; $i < $pagel-1 && $spos >= 0; ) {
351 $l = measure($shistory[$spos]);
353 $spos-- if $i <= $pagel;
355 $spos = 0 if $spos < 0;
360 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
361 if ($spos < @shistory - 1) {
363 for ($i = 0; $i <= $pagel && $spos <= @shistory; ) {
364 $l = measure($shistory[$spos]);
366 $spos++ if $i <= $pagel;
368 $spos = @shistory if $spos >= @shistory - 1;
373 } elsif ($r eq KEY_LEFT || $r eq "\002") {
379 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
385 } elsif ($r eq KEY_HOME || $r eq "\001") {
387 } elsif ($r eq KEY_END || $r eq "\005") {
389 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
391 my $a = substr($inbuf, 0, $pos-1);
392 my $b = substr($inbuf, $pos) if $pos < $lth;
401 } elsif ($r eq KEY_DC || $r eq "\004") {
403 my $a = substr($inbuf, 0, $pos);
404 my $b = substr($inbuf, $pos+1) if $pos < $lth;
412 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
415 } elsif (defined $r && is_pctext($r)) {
416 # move the top screen back to the bottom if you type something
417 if ($spos < @shistory) {
422 # $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
424 # insert the character into the keyboard buffer
426 my $a = substr($inbuf, 0, $pos);
427 my $b = substr($inbuf, $pos);
428 $inbuf = $a . $r . $b;
434 } elsif ($r eq "\014" || $r eq "\022") {
438 } elsif ($r eq "\013") {
439 $inbuf = substr($inbuf, 0, $pos);
440 $lth = length $inbuf;
446 $bot->addstr($inbuf);
457 if ($t > $lasttime) {
458 my ($min)= (gmtime($t))[1];
459 if ($min != $lastmin) {
465 my $ch = $bot->getch();
466 if (@time && tv_interval(\@time, [gettimeofday]) >= 1) {
474 $top->refresh() if $top->is_wintouched;
481 $conn->send_later("A$call|$connsort width=$cols");
482 $conn->send_later("I$call|set/page $maxshist");
483 #$conn->send_later("I$call|set/nobeep");
489 Mojo::IOLoop->remove($idle);
497 $call = uc shift @ARGV if @ARGV;
498 $call = uc $myalias if !$call;
499 my ($scall, $ssid) = split /-/, $call;
500 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
502 $ssid = 15 if $ssid > 15;
503 $call = "$scall-$ssid";
506 if ($call eq $mycall) {
507 print "You cannot connect as your cluster callsign ($mycall)\n";
513 unless ($DB::VERSION) {
514 $SIG{'INT'} = \&sig_term;
515 $SIG{'TERM'} = \&sig_term;
518 $SIG{'HUP'} = \&sig_term;
523 $SIG{__DIE__} = \&sig_term;
525 $Text::Wrap::Columns = $cols;
530 $conn = IntMsg->connect($clusteraddr, $clusterport, rproc => \&rec_socket);
531 $conn->{on_connect} = \&on_connect;
532 $conn->{on_disconnect} = \&on_disconnect;
534 $idle = Mojo::IOLoop->recurring(0.100 => \&idle_loop);