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);
46 use Text::Wrap qw(wrap);
54 $call = ""; # the callsign being used
55 $node = ""; # the node callsign being used
56 $conn = 0; # the connection object for the cluster
57 $lasttime = time; # lasttime something happened on the interface
63 $spos = $pos = $lth = 0;
69 #$SIG{WINCH} = sub {@time = gettimeofday};
71 $DXDebug::no_stdout = 1;
73 # do the screen initialisation
79 init_pair(0, $foreground, $background);
80 # init_pair(0, $background, $foreground);
81 init_pair(1, COLOR_RED, $background);
82 init_pair(2, COLOR_YELLOW, $background);
83 init_pair(3, COLOR_GREEN, $background);
84 init_pair(4, COLOR_CYAN, $background);
85 init_pair(5, COLOR_BLUE, $background);
86 init_pair(6, COLOR_MAGENTA, $background);
87 init_pair(7, COLOR_RED, COLOR_BLUE);
88 init_pair(8, COLOR_YELLOW, COLOR_BLUE);
89 init_pair(9, COLOR_GREEN, COLOR_BLUE);
90 init_pair(10, COLOR_CYAN, COLOR_BLUE);
91 init_pair(11, COLOR_BLUE, COLOR_RED);
92 init_pair(12, COLOR_MAGENTA, COLOR_BLUE);
93 init_pair(13, COLOR_YELLOW, COLOR_GREEN);
94 init_pair(14, COLOR_RED, COLOR_GREEN);
95 eval { assume_default_colors($foreground, $background) } unless $is_win;
98 $top = $scr->subwin($lines-4, $cols, 0, 0);
105 # $top->setscrreg(0, $lines-5);
107 $bot = $scr->subwin(3, $cols, $lines-3, 0);
119 $mycallcolor = COLOR_PAIR(1) unless $mycallcolor;
131 $has_colors = has_colors();
135 $spos = @sh < $pagel ? 0 : @sh - $pagel;
137 $conn->send_later("C$call|$cols") if $conn;
140 # cease communications
144 $conn->disconnect if $conn;
151 # terminate program from signal
157 # determine the colour of the line
161 foreach my $ref (@colors) {
162 if ($_[0] =~ m{$$ref[0]}) {
163 $top->attrset($$ref[1]);
171 # display the top screen
176 dbg("B: s:$spos h:" . scalar @sh) if isdbg('console');
179 $spos = 0 if $spos < 0;
182 $top->attrset(COLOR_PAIR(0)) if $has_colors;
184 for ($i = 0; $i < $pagel && $y < @sh; ++$y) {
188 dbg("C: s:$spos y:$i sh:" . scalar @sh . " l:" . length($line) . " '$line'") if isdbg('console');
191 $top->attrset(COLOR_PAIR(0)) if $has_colors;
198 } elsif ($spos < @sh || $spos < $pagel) {
199 # if we really are scrolling thru at the end of the history
200 while ($spos < @sh) {
201 my $line = $sh[$spos];
210 dbg("A: s:$spos sh:" . scalar @sh . " y:$y l:" . length($line) . " '$line'") if isdbg('console');
215 $top->attrset(COLOR_PAIR(0)) if $has_colors;
218 shift @sh while @sh > $maxshist;
224 my $size = $lines . 'x' . $cols . '-';
225 my $add = "-$spos-$shl";
226 my $time = ztime(time);
227 my $c = "$call\@$node";
228 my $str = "-" . $time . '-' . ($inscroll ? 'S':'-') . '-' x ($cols - (length($size) + length($c) + length($add) + length($time) + 3));
229 $scr->addstr($lines-4, 0, $str);
232 $scr->attrset($mycallcolor) if $has_colors;
234 $scr->attrset(COLOR_PAIR(0)) if $has_colors;
244 dbg("KEY: " . unpack("H*", $r). " '$r'") if isdbg('console');
248 # $prbuf =~ s/\r/\\r/;
249 # $prbuf =~ s/\n/\\n/;
250 # print "sys: $r ($prbuf)\n";
255 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
258 $inbuf = " " unless length $inbuf;
260 # check for a pling and do a search back for a command
261 if ($inbuf =~ /^!/o) {
264 for ($i = $#kh; $i >= 0; $i--) {
265 if ($kh[$i] =~ /^$inbuf/) {
275 push @kh, $inbuf if length $inbuf;
276 shift @kh if @kh > $maxkhist;
280 $bot->addstr(substr($inbuf, 0, $cols));
282 if ($inscroll && $spos < @sh) {
283 $spos = @sh - $pagel;
290 # send it to the cluster
291 $conn->send_later("I$call|$inbuf");
294 } elsif ($r eq KEY_UP || $r eq "\020") {
298 $pos = $lth = length $inbuf;
302 } elsif ($r eq KEY_DOWN || $r eq "\016") {
303 if ($kpos < @kh - 1) {
306 $pos = $lth = length $inbuf;
310 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
311 if ($spos > 0 && @sh > $pagel) {
312 $spos -= $pagel+int($pagel/2);
313 $spos = 0 if $spos < 0;
319 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
320 if ($inscroll && $spos < @sh) {
322 dbg("NPAGE sp:$spos $sh:". scalar @sh . " pl: $pagel") if isdbg('console');
323 $spos += int($pagel/2);
324 if ($spos > @sh - $pagel) {
325 $spos = @sh - $pagel;
335 } elsif ($r eq KEY_LEFT || $r eq "\002") {
341 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
347 } elsif ($r eq KEY_HOME || $r eq "\001") {
349 } elsif ($r eq KEY_END || $r eq "\005") {
351 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
353 my $a = substr($inbuf, 0, $pos-1);
354 my $b = substr($inbuf, $pos) if $pos < $lth;
363 } elsif ($r eq KEY_DC || $r eq "\004") {
365 my $a = substr($inbuf, 0, $pos);
366 my $b = substr($inbuf, $pos+1) if $pos < $lth;
374 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
377 } elsif ($r eq "\x12" || $r eq "\x0c") {
378 dbg("REDRAW called") if isdbg('console');
381 } elsif ($r eq "\013") {
382 $inbuf = substr($inbuf, 0, $pos);
383 $lth = length $inbuf;
384 } elsif (defined $r && is_pctext($r)) {
385 # move the top screen back to the bottom if you type something
387 if ($inscroll && $spos < @sh) {
388 $spos = @sh - $pagel;
393 # $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
395 # insert the character into the keyboard buffer
397 my $a = substr($inbuf, 0, $pos);
398 my $b = substr($inbuf, $pos);
399 $inbuf = $a . $r . $b;
411 $bot->addstr($inbuf);
418 # add a line to the end of the top screen
423 my $l = length $inbuf;
424 dbg("addtotop: $l $inbuf");
427 if (length $inbuf > $cols) {
428 $Text::Wrap::columns = $cols;
429 push @sh, split /\n/, wrap('',' ' x 19, $inbuf);
437 show_screen() unless $inscroll;
440 # handle incoming messages
443 my ($con, $msg, $err) = @_;
444 if (defined $err && $err) {
448 dbg("msg: " . length($msg) . " '$msg'") if isdbg('console');
449 my ($sort, $incall, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
450 if ($line =~ s/\x07+$//) {
453 $line =~ s/[\r\n]+//s;
455 # change my call if my node says "tonight Michael you are Jane" or something like that...
456 $call = $incall if $call ne $incall;
458 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
459 if ($sort && $sort eq 'D') {
460 $line = " " unless length($line);
462 } elsif ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
465 # ******************************************************
466 # ******************************************************
467 # any other sorts that might happen are silently ignored.
468 # ******************************************************
469 # ******************************************************
483 if ($t > $lasttime) {
484 my ($min)= (gmtime($t))[1];
485 if ($min != $lastmin) {
486 show_screen() unless $inscroll;
491 my $ch = $bot->getch(); # this is here just to catch RESIZE events
493 if ($ch eq KEY_RESIZE) {
496 rec_stdin($ch) unless $ch eq '-1';
499 $top->refresh() if $top->is_wintouched;
506 $conn->send_later("A$call|$connsort width=$cols");
507 $conn->send_later("I$call|set/page $maxshist");
508 #$conn->send_later("I$call|set/nobeep");
514 Mojo::IOLoop->remove($idle);
523 while (@ARGV && $ARGV[0] =~ /^-/) {
532 $call = uc shift @ARGV if @ARGV;
533 $call = uc $myalias unless $call;
534 $node = uc $mycall unless $node;
536 my ($scall, $ssid) = split /-/, $call;
537 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
539 $ssid = 15 if $ssid > 15;
540 $call = "$scall-$ssid";
543 if ($call eq $mycall) {
544 print "You cannot connect as your cluster callsign ($mycall)\n";
548 unless ($DB::VERSION) {
549 $SIG{'INT'} = \&sig_term;
550 $SIG{'TERM'} = \&sig_term;
553 $SIG{'HUP'} = \&sig_term;
557 $Text::Wrap::columns = $cols;
560 $SIG{__DIE__} = \&sig_term;
565 $conn = IntMsg->connect($clusteraddr, $clusterport, rproc => \&rec_socket);
566 $conn->{on_connect} = \&on_connect;
567 $conn->{on_disconnect} = \&on_disconnect;
569 my $timer = Mojo::IOLoop->recurring(1, sub {DXLog::flushall()}) if $DXDebug::fp;
571 $idle = Mojo::IOLoop->recurring(0.100 => \&idle_loop);
572 Mojo::IOLoop->singleton->reactor->io(\*STDIN => sub {
573 my $ch = $bot->getch();
579 })->watch(\*STDIN, 1, 0);