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 $conn = 0; # the connection object for the cluster
56 $lasttime = time; # lasttime something happened on the interface
62 $spos = $pos = $lth = 0;
68 #$SIG{WINCH} = sub {@time = gettimeofday};
70 $DXDebug::no_stdout = 1;
74 local *STDOUT = undef;
78 # do the screen initialisation
84 init_pair("0", $foreground, $background);
85 # init_pair(0, $background, $foreground);
86 init_pair(1, COLOR_RED, $background);
87 init_pair(2, COLOR_YELLOW, $background);
88 init_pair(3, COLOR_GREEN, $background);
89 init_pair(4, COLOR_CYAN, $background);
90 init_pair(5, COLOR_BLUE, $background);
91 init_pair(6, COLOR_MAGENTA, $background);
92 init_pair(7, COLOR_RED, COLOR_BLUE);
93 init_pair(8, COLOR_YELLOW, COLOR_BLUE);
94 init_pair(9, COLOR_GREEN, COLOR_BLUE);
95 init_pair(10, COLOR_CYAN, COLOR_BLUE);
96 init_pair(11, COLOR_BLUE, COLOR_RED);
97 init_pair(12, COLOR_MAGENTA, COLOR_BLUE);
98 init_pair(13, COLOR_YELLOW, COLOR_GREEN);
99 init_pair(14, COLOR_RED, COLOR_GREEN);
100 eval { assume_default_colors($foreground, $background) } unless $is_win;
103 $top = $scr->subwin($lines-4, $cols, 0, 0);
110 # $top->setscrreg(0, $lines-5);
112 $bot = $scr->subwin(3, $cols, $lines-3, 0);
124 $mycallcolor = COLOR_PAIR(1) unless $mycallcolor;
136 $has_colors = has_colors();
140 $spos = @sh < $pagel ? 0 : @sh - $pagel;
144 # cease communications
148 $conn->disconnect if $conn;
155 # terminate program from signal
161 # determine the colour of the line
165 foreach my $ref (@colors) {
166 if ($_[0] =~ m{$$ref[0]}) {
167 $top->attrset($$ref[1]);
174 # measure the no of screen lines a line will take
178 return 0 unless $line;
180 my $l = length $line;
181 my $lines = int ($l / $cols);
182 $lines++ if $l / $cols > $lines;
186 # display the top screen
191 dbg("B: s:$spos h:" . scalar @sh) if isdbg('console');
193 # for ($i = 0; $i < $pagel && $p >= 0; ) {
194 # $l = measure($sh[$p]);
196 # $p-- if $i < $pagel;
199 $spos = 0 if $spos < 0;
202 $top->attrset(COLOR_PAIR(0)) if $has_colors;
204 for ($i = 0; $i < $pagel && $y < @sh; ++$y) {
206 # my $lines = measure($line);
209 dbg("C: s:$spos y:$i sh:" . scalar @sh . " l:" . length($line) . " '$line'") if isdbg('console');
212 $top->attrset(COLOR_PAIR(0)) if $has_colors;
219 } elsif ($spos < @sh || $spos < $pagel) {
220 # if we really are scrolling thru at the end of the history
221 while ($spos < @sh) {
222 my $line = $sh[$spos];
231 dbg("A: s:$spos sh:" . scalar @sh . " y:$y l:" . length($line) . " '$line'") if isdbg('console');
236 $top->attrset(COLOR_PAIR(0)) if $has_colors;
239 shift @sh while @sh > $maxshist;
245 my $size = $lines . 'x' . $cols . '-';
246 my $add = "-$spos-$shl";
247 my $time = ztime(time);
248 my $str = "-" . $time . '-' . ($inscroll ? 'S':'-') . '-' x ($cols - (length($size) + length($call) + length($add) + length($time) + 3));
249 $scr->addstr($lines-4, 0, $str);
252 $scr->attrset($mycallcolor) if $has_colors;
254 $scr->attrset(COLOR_PAIR(0)) if $has_colors;
264 dbg("KEY: " . unpack("H*", $r). " '$r'") if isdbg('console');
268 # $prbuf =~ s/\r/\\r/;
269 # $prbuf =~ s/\n/\\n/;
270 # print "sys: $r ($prbuf)\n";
275 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
278 $inbuf = " " unless length $inbuf;
280 # check for a pling and do a search back for a command
281 if ($inbuf =~ /^!/o) {
284 for ($i = $#kh; $i >= 0; $i--) {
285 if ($kh[$i] =~ /^$inbuf/) {
295 push @kh, $inbuf if length $inbuf;
296 shift @kh if @kh > $maxkhist;
300 $bot->addstr(substr($inbuf, 0, $cols));
302 # add it to the monitor window
303 # unless ($spos == @sh) {
307 if ($inscroll && $spos < @sh) {
308 $spos = @sh - $pagel;
315 # send it to the cluster
316 $conn->send_later("I$call|$inbuf");
319 } elsif ($r eq KEY_UP || $r eq "\020") {
323 $pos = $lth = length $inbuf;
327 } elsif ($r eq KEY_DOWN || $r eq "\016") {
328 if ($kpos < @kh - 1) {
331 $pos = $lth = length $inbuf;
335 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
336 if ($spos > 0 && @sh > $pagel) {
338 # for ($i = 0; $i < $pagel-1 && $spos >= 0; ) {
339 # $l = measure($sh[$spos]);
341 # --$spos if $i <= $pagel;
343 $spos -= $pagel+int($pagel/2);
344 $spos = 0 if $spos < 0;
350 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
351 if ($inscroll && $spos < @sh) {
353 # for ($i = 0; $i <= $pagel && $spos < @sh; ) {
354 # $l = measure($sh[$spos]);
356 # ++$spos if $i <= $pagel && $spos < @sh;
359 dbg("NPAGE sp:$spos $sh:". scalar @sh . " pl: $pagel") if isdbg('console');
360 $spos += int($pagel/2);
361 if ($spos > @sh - $pagel) {
362 $spos = @sh - $pagel;
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 ($r eq "\x12" || $r eq "\x0c") {
415 dbg("REDRAW called") if isdbg('console');
418 } elsif ($r eq "\013") {
419 $inbuf = substr($inbuf, 0, $pos);
420 $lth = length $inbuf;
421 } elsif (defined $r && is_pctext($r)) {
422 # move the top screen back to the bottom if you type something
424 if ($inscroll && $spos < @sh) {
425 $spos = @sh - $pagel;
430 # $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
432 # insert the character into the keyboard buffer
434 my $a = substr($inbuf, 0, $pos);
435 my $b = substr($inbuf, $pos);
436 $inbuf = $a . $r . $b;
448 $bot->addstr($inbuf);
455 # add a line to the end of the top screen
460 my $l = length $inbuf;
462 # $Text::Wrap::Columns = $cols;
463 # push @sh, wrap('',"\t", $inbuf);
469 show_screen() unless $inscroll;
472 # handle incoming messages
475 my ($con, $msg, $err) = @_;
476 if (defined $err && $err) {
480 dbg("msg: " . length($msg) . " '$msg'") if isdbg('console');
481 my ($sort, $incall, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
482 if ($line =~ s/\x07+$//) {
485 $line =~ s/[\r\n]+//s;
487 # change my call if my node says "tonight Michael you are Jane" or something like that...
488 $call = $incall if $call ne $incall;
490 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
491 if ($sort && $sort eq 'D') {
492 $line = " " unless length($line);
494 } elsif ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
497 # ******************************************************
498 # ******************************************************
499 # any other sorts that might happen are silently ignored.
500 # ******************************************************
501 # ******************************************************
515 if ($t > $lasttime) {
516 my ($min)= (gmtime($t))[1];
517 if ($min != $lastmin) {
518 show_screen() unless $inscroll;
523 my $ch = $bot->getch(); # this is here just to catch RESIZE events
525 if ($ch == KEY_RESIZE) {
528 rec_stdin($ch) unless $ch == '-1';
531 $top->refresh() if $top->is_wintouched;
538 $conn->send_later("A$call|$connsort width=$cols");
539 $conn->send_later("I$call|set/page $maxshist");
540 #$conn->send_later("I$call|set/nobeep");
546 Mojo::IOLoop->remove($idle);
555 while (@ARGV && $ARGV[0] =~ /^-/) {
557 dbgadd('console'), $maxshist = 200 if $arg eq '-x';
560 $call = uc shift @ARGV if @ARGV;
561 $call = uc $myalias if !$call;
562 my ($scall, $ssid) = split /-/, $call;
563 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
565 $ssid = 15 if $ssid > 15;
566 $call = "$scall-$ssid";
569 if ($call eq $mycall) {
570 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()});
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);