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?
41 use Time::HiRes qw(gettimeofday tv_interval);
51 $call = ""; # the callsign being used
52 $conn = 0; # the connection object for the cluster
53 $lasttime = time; # lasttime something happened on the interface
59 $spos = $pos = $lth = 0;
66 #$SIG{WINCH} = sub {@time = gettimeofday};
70 local *STDOUT = undef;
74 # do the screen initialisation
80 init_pair("0", $foreground, $background);
81 # init_pair(0, $background, $foreground);
82 init_pair(1, COLOR_RED, $background);
83 init_pair(2, COLOR_YELLOW, $background);
84 init_pair(3, COLOR_GREEN, $background);
85 init_pair(4, COLOR_CYAN, $background);
86 init_pair(5, COLOR_BLUE, $background);
87 init_pair(6, COLOR_MAGENTA, $background);
88 init_pair(7, COLOR_RED, COLOR_BLUE);
89 init_pair(8, COLOR_YELLOW, COLOR_BLUE);
90 init_pair(9, COLOR_GREEN, COLOR_BLUE);
91 init_pair(10, COLOR_CYAN, COLOR_BLUE);
92 init_pair(11, COLOR_BLUE, COLOR_RED);
93 init_pair(12, COLOR_MAGENTA, COLOR_BLUE);
94 init_pair(13, COLOR_YELLOW, COLOR_GREEN);
95 init_pair(14, COLOR_RED, COLOR_GREEN);
96 eval { assume_default_colors($foreground, $background) } unless $is_win;
99 $top = $scr->subwin($lines-4, $cols, 0, 0);
104 # $scr->addstr($lines-4, 0, '-' x $cols);
105 $bot = $scr->subwin(3, $cols, $lines-3, 0);
116 $mycallcolor = COLOR_PAIR(1) unless $mycallcolor;
128 $has_colors = has_colors();
134 # cease communications
138 $conn->disconnect if $conn;
145 # terminate program from signal
151 # determine the colour of the line
155 foreach my $ref (@colors) {
156 if ($_[0] =~ m{$$ref[0]}) {
157 $top->attrset($$ref[1]);
164 # measure the no of screen lines a line will take
168 return 0 unless $line;
170 my $l = length $line;
171 my $lines = int ($l / $cols);
172 $lines++ if $l / $cols > $lines;
176 # display the top screen
179 if ($spos == @shistory - 1) {
181 # if we really are scrolling thru at the end of the history
182 my $line = $shistory[$spos];
183 $top->addstr("\n") if $spos > 0;
186 # $top->addstr("\n");
187 $top->attrset(COLOR_PAIR(0)) if $has_colors;
195 for ($i = 0; $i < $pagel && $p >= 0; ) {
196 $l = measure($shistory[$p]);
203 $top->attrset(COLOR_PAIR(0)) if $has_colors;
205 for ($i = 0; $i < $pagel && $p < @shistory; $p++) {
206 my $line = $shistory[$p];
207 my $lines = measure($line);
208 last if $i + $lines > $pagel;
209 $top->addstr("\n") if $i;
212 $top->attrset(COLOR_PAIR(0)) if $has_colors;
216 $spos = @shistory if $spos > @shistory;
219 my $size = $lines . 'x' . $cols . '-';
220 my $add = "-$spos-$shl";
221 my $time = ztime(time);
222 my $str = "-" . $time . '-' x ($cols - (length($size) + length($call) + length($add) + length($time) + 1));
223 $scr->addstr($lines-4, 0, $str);
226 $scr->attrset($mycallcolor) if $has_colors;
228 $scr->attrset(COLOR_PAIR(0)) if $has_colors;
234 # add a line to the end of the top screen
239 if ($inbuf =~ s/\x07+$//) {
242 if (length $inbuf >= $cols) {
243 $Text::Wrap::Columns = $cols;
244 push @shistory, wrap('',"\t", $inbuf);
246 push @shistory, $inbuf;
248 shift @shistory while @shistory > $maxshist;
253 # handle incoming messages
256 my ($con, $msg, $err) = @_;
257 if (defined $err && $err) {
261 my ($sort, $call, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
263 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
264 if ($sort && $sort eq 'D') {
265 $line = " " unless length($line);
267 } elsif ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
270 # ******************************************************
271 # ******************************************************
272 # any other sorts that might happen are silently ignored.
273 # ******************************************************
274 # ******************************************************
288 # $prbuf =~ s/\r/\\r/;
289 # $prbuf =~ s/\n/\\n/;
290 # print "sys: $r ($prbuf)\n";
295 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
298 $inbuf = " " unless length $inbuf;
300 # check for a pling and do a search back for a command
301 if ($inbuf =~ /^!/o) {
304 for ($i = $#khistory; $i >= 0; $i--) {
305 if ($khistory[$i] =~ /^$inbuf/) {
306 $inbuf = $khistory[$i];
315 push @khistory, $inbuf if length $inbuf;
316 shift @khistory if @khistory > $maxkhist;
317 $khistpos = @khistory;
320 $bot->addstr(substr($inbuf, 0, $cols));
322 # add it to the monitor window
323 unless ($spos == @shistory) {
329 # send it to the cluster
330 $conn->send_later("I$call|$inbuf");
333 } elsif ($r eq KEY_UP || $r eq "\020") {
336 $inbuf = $khistory[$khistpos];
337 $pos = $lth = length $inbuf;
341 } elsif ($r eq KEY_DOWN || $r eq "\016") {
342 if ($khistpos < @khistory - 1) {
344 $inbuf = $khistory[$khistpos];
345 $pos = $lth = length $inbuf;
349 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
352 for ($i = 0; $i < $pagel-1 && $spos >= 0; ) {
353 $l = measure($shistory[$spos]);
355 $spos-- if $i <= $pagel;
357 $spos = 0 if $spos < 0;
362 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
363 if ($spos < @shistory - 1) {
365 for ($i = 0; $i <= $pagel && $spos <= @shistory; ) {
366 $l = measure($shistory[$spos]);
368 $spos++ if $i <= $pagel;
370 $spos = @shistory if $spos >= @shistory - 1;
375 } elsif ($r eq KEY_LEFT || $r eq "\002") {
381 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
387 } elsif ($r eq KEY_HOME || $r eq "\001") {
389 } elsif ($r eq KEY_END || $r eq "\005") {
391 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
393 my $a = substr($inbuf, 0, $pos-1);
394 my $b = substr($inbuf, $pos) if $pos < $lth;
403 } elsif ($r eq KEY_DC || $r eq "\004") {
405 my $a = substr($inbuf, 0, $pos);
406 my $b = substr($inbuf, $pos+1) if $pos < $lth;
414 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
417 } elsif (defined $r && is_pctext($r)) {
418 # move the top screen back to the bottom if you type something
419 if ($spos < @shistory) {
424 # $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
426 # insert the character into the keyboard buffer
428 my $a = substr($inbuf, 0, $pos);
429 my $b = substr($inbuf, $pos);
430 $inbuf = $a . $r . $b;
436 } elsif ($r eq "\014" || $r eq "\022") {
440 } elsif ($r eq "\013") {
441 $inbuf = substr($inbuf, 0, $pos);
442 $lth = length $inbuf;
448 $bot->addstr($inbuf);
459 if ($t > $lasttime) {
460 my ($min)= (gmtime($t))[1];
461 if ($min != $lastmin) {
467 my $ch = $bot->getch();
468 if (@time && tv_interval(\@time, [gettimeofday]) >= 1) {
476 $top->refresh() if $top->is_wintouched;
483 $conn->send_later("A$call|$connsort width=$cols");
484 $conn->send_later("I$call|set/page $maxshist");
485 #$conn->send_later("I$call|set/nobeep");
491 Mojo::IOLoop->remove($idle);
499 $call = uc shift @ARGV if @ARGV;
500 $call = uc $myalias if !$call;
501 my ($scall, $ssid) = split /-/, $call;
502 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
504 $ssid = 15 if $ssid > 15;
505 $call = "$scall-$ssid";
508 if ($call eq $mycall) {
509 print "You cannot connect as your cluster callsign ($mycall)\n";
515 unless ($DB::VERSION) {
516 $SIG{'INT'} = \&sig_term;
517 $SIG{'TERM'} = \&sig_term;
520 $SIG{'HUP'} = \&sig_term;
525 $SIG{__DIE__} = \&sig_term;
527 $Text::Wrap::Columns = $cols;
532 $conn = IntMsg->connect($clusteraddr, $clusterport, rproc => \&rec_socket);
533 $conn->{on_connect} = \&on_connect;
534 $conn->{on_disconnect} = \&on_disconnect;
536 $idle = Mojo::IOLoop->recurring(0.100 => \&idle_loop);