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?
38 use Time::HiRes qw(gettimeofday tv_interval);
48 $call = ""; # the callsign being used
49 $conn = 0; # the connection object for the cluster
50 $lasttime = time; # lasttime something happened on the interface
56 $spos = $pos = $lth = 0;
63 #$SIG{WINCH} = sub {@time = gettimeofday};
67 local *STDOUT = undef;
71 # do the screen initialisation
77 init_pair("0", $foreground, $background);
78 # init_pair(0, $background, $foreground);
79 init_pair(1, COLOR_RED, $background);
80 init_pair(2, COLOR_YELLOW, $background);
81 init_pair(3, COLOR_GREEN, $background);
82 init_pair(4, COLOR_CYAN, $background);
83 init_pair(5, COLOR_BLUE, $background);
84 init_pair(6, COLOR_MAGENTA, $background);
85 init_pair(7, COLOR_RED, COLOR_BLUE);
86 init_pair(8, COLOR_YELLOW, COLOR_BLUE);
87 init_pair(9, COLOR_GREEN, COLOR_BLUE);
88 init_pair(10, COLOR_CYAN, COLOR_BLUE);
89 init_pair(11, COLOR_BLUE, COLOR_RED);
90 init_pair(12, COLOR_MAGENTA, COLOR_BLUE);
91 init_pair(13, COLOR_YELLOW, COLOR_GREEN);
92 init_pair(14, COLOR_RED, COLOR_GREEN);
93 eval { assume_default_colors($foreground, $background) } unless $is_win;
96 $top = $scr->subwin($lines-4, $cols, 0, 0);
101 # $scr->addstr($lines-4, 0, '-' x $cols);
102 $bot = $scr->subwin(3, $cols, $lines-3, 0);
113 $mycallcolor = COLOR_PAIR(1) unless $mycallcolor;
125 $has_colors = has_colors();
131 # cease communications
135 $conn->disconnect if $conn;
142 # terminate program from signal
148 # determine the colour of the line
152 foreach my $ref (@colors) {
153 if ($_[0] =~ m{$$ref[0]}) {
154 $top->attrset($$ref[1]);
161 # measure the no of screen lines a line will take
165 return 0 unless $line;
167 my $l = length $line;
168 my $lines = int ($l / $cols);
169 $lines++ if $l / $cols > $lines;
173 # display the top screen
176 if ($spos == @shistory - 1) {
178 # if we really are scrolling thru at the end of the history
179 my $line = $shistory[$spos];
180 $top->addstr("\n") if $spos > 0;
183 # $top->addstr("\n");
184 $top->attrset(COLOR_PAIR(0)) if $has_colors;
192 for ($i = 0; $i < $pagel && $p >= 0; ) {
193 $l = measure($shistory[$p]);
200 $top->attrset(COLOR_PAIR(0)) if $has_colors;
202 for ($i = 0; $i < $pagel && $p < @shistory; $p++) {
203 my $line = $shistory[$p];
204 my $lines = measure($line);
205 last if $i + $lines > $pagel;
206 $top->addstr("\n") if $i;
209 $top->attrset(COLOR_PAIR(0)) if $has_colors;
213 $spos = @shistory if $spos > @shistory;
216 my $size = $lines . 'x' . $cols . '-';
217 my $add = "-$spos-$shl";
218 my $time = ztime(time);
219 my $str = "-" . $time . '-' x ($cols - (length($size) + length($call) + length($add) + length($time) + 1));
220 $scr->addstr($lines-4, 0, $str);
223 $scr->attrset($mycallcolor) if $has_colors;
225 $scr->attrset(COLOR_PAIR(0)) if $has_colors;
231 # add a line to the end of the top screen
236 if ($inbuf =~ s/\x07+$//) {
239 if (length $inbuf >= $cols) {
240 $Text::Wrap::Columns = $cols;
241 push @shistory, wrap('',"\t", $inbuf);
243 push @shistory, $inbuf;
245 shift @shistory while @shistory > $maxshist;
250 # handle incoming messages
253 my ($con, $msg, $err) = @_;
254 if (defined $err && $err) {
258 my ($sort, $call, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
260 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
261 if ($sort && $sort eq 'D') {
262 $line = " " unless length($line);
264 } elsif ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
267 # ******************************************************
268 # ******************************************************
269 # any other sorts that might happen are silently ignored.
270 # ******************************************************
271 # ******************************************************
285 # $prbuf =~ s/\r/\\r/;
286 # $prbuf =~ s/\n/\\n/;
287 # print "sys: $r ($prbuf)\n";
292 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
295 $inbuf = " " unless length $inbuf;
297 # check for a pling and do a search back for a command
298 if ($inbuf =~ /^!/o) {
301 for ($i = $#khistory; $i >= 0; $i--) {
302 if ($khistory[$i] =~ /^$inbuf/) {
303 $inbuf = $khistory[$i];
312 push @khistory, $inbuf if length $inbuf;
313 shift @khistory if @khistory > $maxkhist;
314 $khistpos = @khistory;
317 $bot->addstr(substr($inbuf, 0, $cols));
319 # add it to the monitor window
320 unless ($spos == @shistory) {
326 # send it to the cluster
327 $conn->send_later("I$call|$inbuf");
330 } elsif ($r eq KEY_UP || $r eq "\020") {
333 $inbuf = $khistory[$khistpos];
334 $pos = $lth = length $inbuf;
338 } elsif ($r eq KEY_DOWN || $r eq "\016") {
339 if ($khistpos < @khistory - 1) {
341 $inbuf = $khistory[$khistpos];
342 $pos = $lth = length $inbuf;
346 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
349 for ($i = 0; $i < $pagel-1 && $spos >= 0; ) {
350 $l = measure($shistory[$spos]);
352 $spos-- if $i <= $pagel;
354 $spos = 0 if $spos < 0;
359 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
360 if ($spos < @shistory - 1) {
362 for ($i = 0; $i <= $pagel && $spos <= @shistory; ) {
363 $l = measure($shistory[$spos]);
365 $spos++ if $i <= $pagel;
367 $spos = @shistory if $spos >= @shistory - 1;
372 } elsif ($r eq KEY_LEFT || $r eq "\002") {
378 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
384 } elsif ($r eq KEY_HOME || $r eq "\001") {
386 } elsif ($r eq KEY_END || $r eq "\005") {
388 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
390 my $a = substr($inbuf, 0, $pos-1);
391 my $b = substr($inbuf, $pos) if $pos < $lth;
400 } elsif ($r eq KEY_DC || $r eq "\004") {
402 my $a = substr($inbuf, 0, $pos);
403 my $b = substr($inbuf, $pos+1) if $pos < $lth;
411 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
414 } elsif (defined $r && is_pctext($r)) {
415 # move the top screen back to the bottom if you type something
416 if ($spos < @shistory) {
421 # $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
423 # insert the character into the keyboard buffer
425 my $a = substr($inbuf, 0, $pos);
426 my $b = substr($inbuf, $pos);
427 $inbuf = $a . $r . $b;
433 } elsif ($r eq "\014" || $r eq "\022") {
437 } elsif ($r eq "\013") {
438 $inbuf = substr($inbuf, 0, $pos);
439 $lth = length $inbuf;
445 $bot->addstr($inbuf);
456 if ($t > $lasttime) {
457 my ($min)= (gmtime($t))[1];
458 if ($min != $lastmin) {
464 my $ch = $bot->getch();
465 if (@time && tv_interval(\@time, [gettimeofday]) >= 1) {
473 $top->refresh() if $top->is_wintouched;
480 $conn->send_later("A$call|$connsort width=$cols");
481 $conn->send_later("I$call|set/page $maxshist");
482 #$conn->send_later("I$call|set/nobeep");
488 Mojo::IOLoop->remove($idle);
496 $call = uc shift @ARGV if @ARGV;
497 $call = uc $myalias if !$call;
498 my ($scall, $ssid) = split /-/, $call;
499 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
501 $ssid = 15 if $ssid > 15;
502 $call = "$scall-$ssid";
505 if ($call eq $mycall) {
506 print "You cannot connect as your cluster callsign ($mycall)\n";
512 unless ($DB::VERSION) {
513 $SIG{'INT'} = \&sig_term;
514 $SIG{'TERM'} = \&sig_term;
517 $SIG{'HUP'} = \&sig_term;
522 $SIG{__DIE__} = \&sig_term;
524 $Text::Wrap::Columns = $cols;
529 $conn = IntMsg->connect($clusteraddr, $clusterport, rproc => \&rec_socket);
530 $conn->{on_connect} = \&on_connect;
531 $conn->{on_disconnect} = \&on_disconnect;
533 $idle = Mojo::IOLoop->recurring(0.100 => \&idle_loop);