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?
30 $clusteraddr = '127.0.0.1';
44 use Time::HiRes qw(gettimeofday tv_interval);
54 $call = ""; # the callsign being used
55 $conn = 0; # the connection object for the cluster
56 $lasttime = time; # lasttime something happened on the interface
62 $spos = $pos = $lth = 0;
69 #$SIG{WINCH} = sub {@time = gettimeofday};
73 local *STDOUT = undef;
77 # do the screen initialisation
83 init_pair("0", $foreground, $background);
84 # init_pair(0, $background, $foreground);
85 init_pair(1, COLOR_RED, $background);
86 init_pair(2, COLOR_YELLOW, $background);
87 init_pair(3, COLOR_GREEN, $background);
88 init_pair(4, COLOR_CYAN, $background);
89 init_pair(5, COLOR_BLUE, $background);
90 init_pair(6, COLOR_MAGENTA, $background);
91 init_pair(7, COLOR_RED, COLOR_BLUE);
92 init_pair(8, COLOR_YELLOW, COLOR_BLUE);
93 init_pair(9, COLOR_GREEN, COLOR_BLUE);
94 init_pair(10, COLOR_CYAN, COLOR_BLUE);
95 init_pair(11, COLOR_BLUE, COLOR_RED);
96 init_pair(12, COLOR_MAGENTA, COLOR_BLUE);
97 init_pair(13, COLOR_YELLOW, COLOR_GREEN);
98 init_pair(14, COLOR_RED, COLOR_GREEN);
99 eval { assume_default_colors($foreground, $background) } unless $is_win;
102 $top = $scr->subwin($lines-4, $cols, 0, 0);
107 # $scr->addstr($lines-4, 0, '-' x $cols);
108 $bot = $scr->subwin(3, $cols, $lines-3, 0);
119 $mycallcolor = COLOR_PAIR(1) unless $mycallcolor;
131 $has_colors = has_colors();
137 # cease communications
141 $conn->disconnect if $conn;
148 # terminate program from signal
154 # determine the colour of the line
158 foreach my $ref (@colors) {
159 if ($_[0] =~ m{$$ref[0]}) {
160 $top->attrset($$ref[1]);
167 # measure the no of screen lines a line will take
171 return 0 unless $line;
173 my $l = length $line;
174 my $lines = int ($l / $cols);
175 $lines++ if $l / $cols > $lines;
179 # display the top screen
182 if ($spos == @shistory - 1) {
184 # if we really are scrolling thru at the end of the history
185 my $line = $shistory[$spos];
186 $top->addstr("\n") if $spos > 0;
189 # $top->addstr("\n");
190 $top->attrset(COLOR_PAIR(0)) if $has_colors;
198 for ($i = 0; $i < $pagel && $p >= 0; ) {
199 $l = measure($shistory[$p]);
206 $top->attrset(COLOR_PAIR(0)) if $has_colors;
208 for ($i = 0; $i < $pagel && $p < @shistory; $p++) {
209 my $line = $shistory[$p];
210 my $lines = measure($line);
211 last if $i + $lines > $pagel;
212 $top->addstr("\n") if $i;
215 $top->attrset(COLOR_PAIR(0)) if $has_colors;
219 $spos = @shistory if $spos > @shistory;
222 my $size = $lines . 'x' . $cols . '-';
223 my $add = "-$spos-$shl";
224 my $time = ztime(time);
225 my $str = "-" . $time . '-' x ($cols - (length($size) + length($call) + length($add) + length($time) + 1));
226 $scr->addstr($lines-4, 0, $str);
229 $scr->attrset($mycallcolor) if $has_colors;
231 $scr->attrset(COLOR_PAIR(0)) if $has_colors;
237 # add a line to the end of the top screen
242 if ($inbuf =~ s/\x07+$//) {
245 if (length $inbuf >= $cols) {
246 $Text::Wrap::Columns = $cols;
247 push @shistory, wrap('',"\t", $inbuf);
249 push @shistory, $inbuf;
251 shift @shistory while @shistory > $maxshist;
256 # handle incoming messages
259 my ($con, $msg, $err) = @_;
260 if (defined $err && $err) {
264 my ($sort, $call, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
266 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
267 if ($sort && $sort eq 'D') {
268 $line = " " unless length($line);
270 } elsif ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
273 # ******************************************************
274 # ******************************************************
275 # any other sorts that might happen are silently ignored.
276 # ******************************************************
277 # ******************************************************
291 # $prbuf =~ s/\r/\\r/;
292 # $prbuf =~ s/\n/\\n/;
293 # print "sys: $r ($prbuf)\n";
298 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
301 $inbuf = " " unless length $inbuf;
303 # check for a pling and do a search back for a command
304 if ($inbuf =~ /^!/o) {
307 for ($i = $#khistory; $i >= 0; $i--) {
308 if ($khistory[$i] =~ /^$inbuf/) {
309 $inbuf = $khistory[$i];
318 push @khistory, $inbuf if length $inbuf;
319 shift @khistory if @khistory > $maxkhist;
320 $khistpos = @khistory;
323 $bot->addstr(substr($inbuf, 0, $cols));
325 # add it to the monitor window
326 unless ($spos == @shistory) {
332 # send it to the cluster
333 $conn->send_later("I$call|$inbuf");
336 } elsif ($r eq KEY_UP || $r eq "\020") {
339 $inbuf = $khistory[$khistpos];
340 $pos = $lth = length $inbuf;
344 } elsif ($r eq KEY_DOWN || $r eq "\016") {
345 if ($khistpos < @khistory - 1) {
347 $inbuf = $khistory[$khistpos];
348 $pos = $lth = length $inbuf;
352 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
355 for ($i = 0; $i < $pagel-1 && $spos >= 0; ) {
356 $l = measure($shistory[$spos]);
358 $spos-- if $i <= $pagel;
360 $spos = 0 if $spos < 0;
365 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
366 if ($spos < @shistory - 1) {
368 for ($i = 0; $i <= $pagel && $spos <= @shistory; ) {
369 $l = measure($shistory[$spos]);
371 $spos++ if $i <= $pagel;
373 $spos = @shistory if $spos >= @shistory - 1;
378 } elsif ($r eq KEY_LEFT || $r eq "\002") {
384 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
390 } elsif ($r eq KEY_HOME || $r eq "\001") {
392 } elsif ($r eq KEY_END || $r eq "\005") {
394 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
396 my $a = substr($inbuf, 0, $pos-1);
397 my $b = substr($inbuf, $pos) if $pos < $lth;
406 } elsif ($r eq KEY_DC || $r eq "\004") {
408 my $a = substr($inbuf, 0, $pos);
409 my $b = substr($inbuf, $pos+1) if $pos < $lth;
417 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
420 } elsif (defined $r && is_pctext($r)) {
421 # move the top screen back to the bottom if you type something
422 if ($spos < @shistory) {
427 # $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
429 # insert the character into the keyboard buffer
431 my $a = substr($inbuf, 0, $pos);
432 my $b = substr($inbuf, $pos);
433 $inbuf = $a . $r . $b;
439 } elsif ($r eq "\014" || $r eq "\022") {
443 } elsif ($r eq "\013") {
444 $inbuf = substr($inbuf, 0, $pos);
445 $lth = length $inbuf;
451 $bot->addstr($inbuf);
462 if ($t > $lasttime) {
463 my ($min)= (gmtime($t))[1];
464 if ($min != $lastmin) {
470 my $ch = $bot->getch();
471 if (@time && tv_interval(\@time, [gettimeofday]) >= 1) {
479 $top->refresh() if $top->is_wintouched;
486 $conn->send_later("A$call|$connsort width=$cols");
487 $conn->send_later("I$call|set/page $maxshist");
488 #$conn->send_later("I$call|set/nobeep");
494 Mojo::IOLoop->remove($idle);
502 $call = uc shift @ARGV if @ARGV;
503 $call = uc $myalias if !$call;
504 my ($scall, $ssid) = split /-/, $call;
505 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
507 $ssid = 15 if $ssid > 15;
508 $call = "$scall-$ssid";
511 if ($call eq $mycall) {
512 print "You cannot connect as your cluster callsign ($mycall)\n";
518 unless ($DB::VERSION) {
519 $SIG{'INT'} = \&sig_term;
520 $SIG{'TERM'} = \&sig_term;
523 $SIG{'HUP'} = \&sig_term;
528 $SIG{__DIE__} = \&sig_term;
530 $Text::Wrap::Columns = $cols;
535 $conn = IntMsg->connect($clusteraddr, $clusterport, rproc => \&rec_socket);
536 $conn->{on_connect} = \&on_connect;
537 $conn->{on_disconnect} = \&on_disconnect;
539 $idle = Mojo::IOLoop->recurring(0.100 => \&idle_loop);