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) {
183 # if we really are scrolling thru at the end of the history
184 my $line = $shistory[$spos];
186 if ($spos >= $pagel) {
189 # $top->addstr("\r");
195 $top->attrset(COLOR_PAIR(0)) if $has_colors;
203 for ($i = 0; $i < $pagel && $p >= 0; ) {
204 $l = measure($shistory[$p]);
211 $top->attrset(COLOR_PAIR(0)) if $has_colors;
213 for ($i = 0; $i < $pagel && $p < @shistory; $p++) {
214 my $line = $shistory[$p];
215 my $lines = measure($line);
219 $top->attrset(COLOR_PAIR(0)) if $has_colors;
223 $spos = @shistory if $spos > @shistory;
226 my $size = $lines . 'x' . $cols . '-';
227 my $add = "-$spos-$shl";
228 my $time = ztime(time);
229 my $str = "-" . $time . '-' x ($cols - (length($size) + length($call) + length($add) + length($time) + 1));
230 $scr->addstr($lines-4, 0, $str);
233 $scr->attrset($mycallcolor) if $has_colors;
235 $scr->attrset(COLOR_PAIR(0)) if $has_colors;
241 # add a line to the end of the top screen
246 if ($inbuf =~ s/\x07+$//) {
250 if (length $inbuf > $cols) {
251 $Text::Wrap::Columns = $cols;
252 push @shistory, wrap('',"\t", $inbuf);
254 push @shistory, $inbuf;
256 shift @shistory while @shistory > $maxshist;
261 # handle incoming messages
264 my ($con, $msg, $err) = @_;
265 if (defined $err && $err) {
269 my ($sort, $incall, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
271 # change my call if my node says "tonight Michael you are Jane" or something like that...
272 $call = $incall if $call ne $incall;
274 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
275 if ($sort && $sort eq 'D') {
276 $line = " " unless length($line);
278 } elsif ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
281 # ******************************************************
282 # ******************************************************
283 # any other sorts that might happen are silently ignored.
284 # ******************************************************
285 # ******************************************************
299 # $prbuf =~ s/\r/\\r/;
300 # $prbuf =~ s/\n/\\n/;
301 # print "sys: $r ($prbuf)\n";
306 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
309 $inbuf = " " unless length $inbuf;
311 # check for a pling and do a search back for a command
312 if ($inbuf =~ /^!/o) {
315 for ($i = $#khistory; $i >= 0; $i--) {
316 if ($khistory[$i] =~ /^$inbuf/) {
317 $inbuf = $khistory[$i];
326 push @khistory, $inbuf if length $inbuf;
327 shift @khistory if @khistory > $maxkhist;
328 $khistpos = @khistory;
331 $bot->addstr(substr($inbuf, 0, $cols));
333 # add it to the monitor window
334 unless ($spos == @shistory) {
340 # send it to the cluster
341 $conn->send_later("I$call|$inbuf");
344 } elsif ($r eq KEY_UP || $r eq "\020") {
347 $inbuf = $khistory[$khistpos];
348 $pos = $lth = length $inbuf;
352 } elsif ($r eq KEY_DOWN || $r eq "\016") {
353 if ($khistpos < @khistory - 1) {
355 $inbuf = $khistory[$khistpos];
356 $pos = $lth = length $inbuf;
360 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
363 for ($i = 0; $i < $pagel-1 && $spos >= 0; ) {
364 $l = measure($shistory[$spos]);
366 $spos-- if $i <= $pagel;
368 $spos = 0 if $spos < 0;
373 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
374 if ($spos < @shistory - 1) {
376 for ($i = 0; $i <= $pagel && $spos <= @shistory; ) {
377 $l = measure($shistory[$spos]);
379 $spos++ if $i <= $pagel;
381 $spos = @shistory if $spos >= @shistory - 1;
386 } elsif ($r eq KEY_LEFT || $r eq "\002") {
392 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
398 } elsif ($r eq KEY_HOME || $r eq "\001") {
400 } elsif ($r eq KEY_END || $r eq "\005") {
402 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
404 my $a = substr($inbuf, 0, $pos-1);
405 my $b = substr($inbuf, $pos) if $pos < $lth;
414 } elsif ($r eq KEY_DC || $r eq "\004") {
416 my $a = substr($inbuf, 0, $pos);
417 my $b = substr($inbuf, $pos+1) if $pos < $lth;
425 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
428 } elsif (defined $r && is_pctext($r)) {
429 # move the top screen back to the bottom if you type something
430 if ($spos < @shistory) {
435 # $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
437 # insert the character into the keyboard buffer
439 my $a = substr($inbuf, 0, $pos);
440 my $b = substr($inbuf, $pos);
441 $inbuf = $a . $r . $b;
447 } elsif ($r eq "\014" || $r eq "\022") {
451 } elsif ($r eq "\013") {
452 $inbuf = substr($inbuf, 0, $pos);
453 $lth = length $inbuf;
459 $bot->addstr($inbuf);
470 if ($t > $lasttime) {
471 my ($min)= (gmtime($t))[1];
472 if ($min != $lastmin) {
478 my $ch = $bot->getch();
479 if (@time && tv_interval(\@time, [gettimeofday]) >= 1) {
487 $top->refresh() if $top->is_wintouched;
494 $conn->send_later("A$call|$connsort width=$cols");
495 $conn->send_later("I$call|set/page $maxshist");
496 #$conn->send_later("I$call|set/nobeep");
502 Mojo::IOLoop->remove($idle);
510 $call = uc shift @ARGV if @ARGV;
511 $call = uc $myalias if !$call;
512 my ($scall, $ssid) = split /-/, $call;
513 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
515 $ssid = 15 if $ssid > 15;
516 $call = "$scall-$ssid";
519 if ($call eq $mycall) {
520 print "You cannot connect as your cluster callsign ($mycall)\n";
526 unless ($DB::VERSION) {
527 $SIG{'INT'} = \&sig_term;
528 $SIG{'TERM'} = \&sig_term;
531 $SIG{'HUP'} = \&sig_term;
536 $SIG{__DIE__} = \&sig_term;
538 $Text::Wrap::Columns = $cols;
543 $conn = IntMsg->connect($clusteraddr, $clusterport, rproc => \&rec_socket);
544 $conn->{on_connect} = \&on_connect;
545 $conn->{on_disconnect} = \&on_disconnect;
547 $idle = Mojo::IOLoop->recurring(0.100 => \&idle_loop);