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;
139 # cease communications
143 $conn->disconnect if $conn;
150 # terminate program from signal
156 # determine the colour of the line
160 foreach my $ref (@colors) {
161 if ($_[0] =~ m{$$ref[0]}) {
162 $top->attrset($$ref[1]);
169 # measure the no of screen lines a line will take
173 return 0 unless $line;
175 my $l = length $line;
176 my $lines = int ($l / $cols);
177 $lines++ if $l / $cols > $lines;
181 # display the top screen
186 dbg("B: s:$spos h:" . scalar @sh) if isdbg('console');
188 # for ($i = 0; $i < $pagel && $p >= 0; ) {
189 # $l = measure($sh[$p]);
191 # $p-- if $i < $pagel;
194 $spos = 0 if $spos < 0;
197 $top->attrset(COLOR_PAIR(0)) if $has_colors;
199 for ($i = 0; $i < $pagel && $y < @sh; ++$y) {
201 # my $lines = measure($line);
204 dbg("C: s:$spos y:$i sh:" . scalar @sh . " l:" . length($line) . " '$line'") if isdbg('console');
207 $top->attrset(COLOR_PAIR(0)) if $has_colors;
214 } elsif ($spos < @sh || $spos < $pagel) {
215 # if we really are scrolling thru at the end of the history
216 while ($spos < @sh) {
217 my $line = $sh[$spos];
226 dbg("A: s:$spos sh:" . scalar @sh . " y:$y l:" . length($line) . " '$line'") if isdbg('console');
231 $top->attrset(COLOR_PAIR(0)) if $has_colors;
234 shift @sh while @sh > $maxshist;
240 my $size = $lines . 'x' . $cols . '-';
241 my $add = "-$spos-$shl";
242 my $time = ztime(time);
243 my $c = "$call\@$node";
244 my $str = "-" . $time . '-' . ($inscroll ? 'S':'-') . '-' x ($cols - (length($size) + length($c) + length($add) + length($time) + 3));
245 $scr->addstr($lines-4, 0, $str);
248 $scr->attrset($mycallcolor) if $has_colors;
250 $scr->attrset(COLOR_PAIR(0)) if $has_colors;
260 dbg("KEY: " . unpack("H*", $r). " '$r'") if isdbg('console');
264 # $prbuf =~ s/\r/\\r/;
265 # $prbuf =~ s/\n/\\n/;
266 # print "sys: $r ($prbuf)\n";
271 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
274 $inbuf = " " unless length $inbuf;
276 # check for a pling and do a search back for a command
277 if ($inbuf =~ /^!/o) {
280 for ($i = $#kh; $i >= 0; $i--) {
281 if ($kh[$i] =~ /^$inbuf/) {
291 push @kh, $inbuf if length $inbuf;
292 shift @kh if @kh > $maxkhist;
296 $bot->addstr(substr($inbuf, 0, $cols));
298 # add it to the monitor window
299 # unless ($spos == @sh) {
303 if ($inscroll && $spos < @sh) {
304 $spos = @sh - $pagel;
311 # send it to the cluster
312 $conn->send_later("I$call|$inbuf");
315 } elsif ($r eq KEY_UP || $r eq "\020") {
319 $pos = $lth = length $inbuf;
323 } elsif ($r eq KEY_DOWN || $r eq "\016") {
324 if ($kpos < @kh - 1) {
327 $pos = $lth = length $inbuf;
331 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
332 if ($spos > 0 && @sh > $pagel) {
334 # for ($i = 0; $i < $pagel-1 && $spos >= 0; ) {
335 # $l = measure($sh[$spos]);
337 # --$spos if $i <= $pagel;
339 $spos -= $pagel+int($pagel/2);
340 $spos = 0 if $spos < 0;
346 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
347 if ($inscroll && $spos < @sh) {
349 # for ($i = 0; $i <= $pagel && $spos < @sh; ) {
350 # $l = measure($sh[$spos]);
352 # ++$spos if $i <= $pagel && $spos < @sh;
355 dbg("NPAGE sp:$spos $sh:". scalar @sh . " pl: $pagel") if isdbg('console');
356 $spos += int($pagel/2);
357 if ($spos > @sh - $pagel) {
358 $spos = @sh - $pagel;
368 } elsif ($r eq KEY_LEFT || $r eq "\002") {
374 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
380 } elsif ($r eq KEY_HOME || $r eq "\001") {
382 } elsif ($r eq KEY_END || $r eq "\005") {
384 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
386 my $a = substr($inbuf, 0, $pos-1);
387 my $b = substr($inbuf, $pos) if $pos < $lth;
396 } elsif ($r eq KEY_DC || $r eq "\004") {
398 my $a = substr($inbuf, 0, $pos);
399 my $b = substr($inbuf, $pos+1) if $pos < $lth;
407 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
410 } elsif ($r eq "\x12" || $r eq "\x0c") {
411 dbg("REDRAW called") if isdbg('console');
414 } elsif ($r eq "\013") {
415 $inbuf = substr($inbuf, 0, $pos);
416 $lth = length $inbuf;
417 } elsif (defined $r && is_pctext($r)) {
418 # move the top screen back to the bottom if you type something
420 if ($inscroll && $spos < @sh) {
421 $spos = @sh - $pagel;
426 # $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
428 # insert the character into the keyboard buffer
430 my $a = substr($inbuf, 0, $pos);
431 my $b = substr($inbuf, $pos);
432 $inbuf = $a . $r . $b;
444 $bot->addstr($inbuf);
451 # add a line to the end of the top screen
456 my $l = length $inbuf;
458 # $Text::Wrap::Columns = $cols;
459 # push @sh, wrap('',"\t", $inbuf);
465 show_screen() unless $inscroll;
468 # handle incoming messages
471 my ($con, $msg, $err) = @_;
472 if (defined $err && $err) {
476 dbg("msg: " . length($msg) . " '$msg'") if isdbg('console');
477 my ($sort, $incall, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
478 if ($line =~ s/\x07+$//) {
481 $line =~ s/[\r\n]+//s;
483 # change my call if my node says "tonight Michael you are Jane" or something like that...
484 $call = $incall if $call ne $incall;
486 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
487 if ($sort && $sort eq 'D') {
488 $line = " " unless length($line);
490 } elsif ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
493 # ******************************************************
494 # ******************************************************
495 # any other sorts that might happen are silently ignored.
496 # ******************************************************
497 # ******************************************************
511 if ($t > $lasttime) {
512 my ($min)= (gmtime($t))[1];
513 if ($min != $lastmin) {
514 show_screen() unless $inscroll;
519 my $ch = $bot->getch(); # this is here just to catch RESIZE events
521 if ($ch =~ /^\d+$/ && $ch == KEY_RESIZE) {
524 rec_stdin($ch) unless $ch == '-1';
527 $top->refresh() if $top->is_wintouched;
534 $conn->send_later("A$call|$connsort width=$cols");
535 $conn->send_later("I$call|set/page $maxshist");
536 #$conn->send_later("I$call|set/nobeep");
542 Mojo::IOLoop->remove($idle);
551 while (@ARGV && $ARGV[0] =~ /^-/) {
560 $call = uc shift @ARGV if @ARGV;
561 $call = uc $myalias unless $call;
562 $node = uc $mycall unless $node;
564 my ($scall, $ssid) = split /-/, $call;
565 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
567 $ssid = 15 if $ssid > 15;
568 $call = "$scall-$ssid";
571 if ($call eq $mycall) {
572 print "You cannot connect as your cluster callsign ($mycall)\n";
576 unless ($DB::VERSION) {
577 $SIG{'INT'} = \&sig_term;
578 $SIG{'TERM'} = \&sig_term;
581 $SIG{'HUP'} = \&sig_term;
586 $SIG{__DIE__} = \&sig_term;
588 #$Text::Wrap::Columns = $cols;
593 $conn = IntMsg->connect($clusteraddr, $clusterport, rproc => \&rec_socket);
594 $conn->{on_connect} = \&on_connect;
595 $conn->{on_disconnect} = \&on_disconnect;
597 my $timer = Mojo::IOLoop->recurring(1, sub {DXLog::flushall()}) if $DXDebug::fp;
599 $idle = Mojo::IOLoop->recurring(0.100 => \&idle_loop);
600 Mojo::IOLoop->singleton->reactor->io(\*STDIN => sub {
601 my $ch = $bot->getch();
607 })->watch(\*STDIN, 1, 0);