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]);
170 # measure the no of screen lines a line will take
174 return 0 unless $line;
176 my $l = length $line;
177 my $lines = int ($l / $cols);
178 $lines++ if $l / $cols > $lines;
182 # display the top screen
187 dbg("B: s:$spos h:" . scalar @sh) if isdbg('console');
189 # for ($i = 0; $i < $pagel && $p >= 0; ) {
190 # $l = measure($sh[$p]);
192 # $p-- if $i < $pagel;
195 $spos = 0 if $spos < 0;
198 $top->attrset(COLOR_PAIR(0)) if $has_colors;
200 for ($i = 0; $i < $pagel && $y < @sh; ++$y) {
202 # my $lines = measure($line);
205 dbg("C: s:$spos y:$i sh:" . scalar @sh . " l:" . length($line) . " '$line'") if isdbg('console');
208 $top->attrset(COLOR_PAIR(0)) if $has_colors;
215 } elsif ($spos < @sh || $spos < $pagel) {
216 # if we really are scrolling thru at the end of the history
217 while ($spos < @sh) {
218 my $line = $sh[$spos];
227 dbg("A: s:$spos sh:" . scalar @sh . " y:$y l:" . length($line) . " '$line'") if isdbg('console');
232 $top->attrset(COLOR_PAIR(0)) if $has_colors;
235 shift @sh while @sh > $maxshist;
241 my $size = $lines . 'x' . $cols . '-';
242 my $add = "-$spos-$shl";
243 my $time = ztime(time);
244 my $c = "$call\@$node";
245 my $str = "-" . $time . '-' . ($inscroll ? 'S':'-') . '-' x ($cols - (length($size) + length($c) + length($add) + length($time) + 3));
246 $scr->addstr($lines-4, 0, $str);
249 $scr->attrset($mycallcolor) if $has_colors;
251 $scr->attrset(COLOR_PAIR(0)) if $has_colors;
261 dbg("KEY: " . unpack("H*", $r). " '$r'") if isdbg('console');
265 # $prbuf =~ s/\r/\\r/;
266 # $prbuf =~ s/\n/\\n/;
267 # print "sys: $r ($prbuf)\n";
272 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
275 $inbuf = " " unless length $inbuf;
277 # check for a pling and do a search back for a command
278 if ($inbuf =~ /^!/o) {
281 for ($i = $#kh; $i >= 0; $i--) {
282 if ($kh[$i] =~ /^$inbuf/) {
292 push @kh, $inbuf if length $inbuf;
293 shift @kh if @kh > $maxkhist;
297 $bot->addstr(substr($inbuf, 0, $cols));
299 # add it to the monitor window
300 # unless ($spos == @sh) {
304 if ($inscroll && $spos < @sh) {
305 $spos = @sh - $pagel;
312 # send it to the cluster
313 $conn->send_later("I$call|$inbuf");
316 } elsif ($r eq KEY_UP || $r eq "\020") {
320 $pos = $lth = length $inbuf;
324 } elsif ($r eq KEY_DOWN || $r eq "\016") {
325 if ($kpos < @kh - 1) {
328 $pos = $lth = length $inbuf;
332 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
333 if ($spos > 0 && @sh > $pagel) {
335 # for ($i = 0; $i < $pagel-1 && $spos >= 0; ) {
336 # $l = measure($sh[$spos]);
338 # --$spos if $i <= $pagel;
340 $spos -= $pagel+int($pagel/2);
341 $spos = 0 if $spos < 0;
347 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
348 if ($inscroll && $spos < @sh) {
350 # for ($i = 0; $i <= $pagel && $spos < @sh; ) {
351 # $l = measure($sh[$spos]);
353 # ++$spos if $i <= $pagel && $spos < @sh;
356 dbg("NPAGE sp:$spos $sh:". scalar @sh . " pl: $pagel") if isdbg('console');
357 $spos += int($pagel/2);
358 if ($spos > @sh - $pagel) {
359 $spos = @sh - $pagel;
369 } elsif ($r eq KEY_LEFT || $r eq "\002") {
375 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
381 } elsif ($r eq KEY_HOME || $r eq "\001") {
383 } elsif ($r eq KEY_END || $r eq "\005") {
385 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
387 my $a = substr($inbuf, 0, $pos-1);
388 my $b = substr($inbuf, $pos) if $pos < $lth;
397 } elsif ($r eq KEY_DC || $r eq "\004") {
399 my $a = substr($inbuf, 0, $pos);
400 my $b = substr($inbuf, $pos+1) if $pos < $lth;
408 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
411 } elsif ($r eq "\x12" || $r eq "\x0c") {
412 dbg("REDRAW called") if isdbg('console');
415 } elsif ($r eq "\013") {
416 $inbuf = substr($inbuf, 0, $pos);
417 $lth = length $inbuf;
418 } elsif (defined $r && is_pctext($r)) {
419 # move the top screen back to the bottom if you type something
421 if ($inscroll && $spos < @sh) {
422 $spos = @sh - $pagel;
427 # $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
429 # insert the character into the keyboard buffer
431 my $a = substr($inbuf, 0, $pos);
432 my $b = substr($inbuf, $pos);
433 $inbuf = $a . $r . $b;
445 $bot->addstr($inbuf);
452 # add a line to the end of the top screen
457 my $l = length $inbuf;
459 # $Text::Wrap::Columns = $cols;
460 # push @sh, wrap('',"\t", $inbuf);
466 show_screen() unless $inscroll;
469 # handle incoming messages
472 my ($con, $msg, $err) = @_;
473 if (defined $err && $err) {
477 dbg("msg: " . length($msg) . " '$msg'") if isdbg('console');
478 my ($sort, $incall, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
479 if ($line =~ s/\x07+$//) {
482 $line =~ s/[\r\n]+//s;
484 # change my call if my node says "tonight Michael you are Jane" or something like that...
485 $call = $incall if $call ne $incall;
487 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
488 if ($sort && $sort eq 'D') {
489 $line = " " unless length($line);
491 } elsif ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
494 # ******************************************************
495 # ******************************************************
496 # any other sorts that might happen are silently ignored.
497 # ******************************************************
498 # ******************************************************
512 if ($t > $lasttime) {
513 my ($min)= (gmtime($t))[1];
514 if ($min != $lastmin) {
515 show_screen() unless $inscroll;
520 my $ch = $bot->getch(); # this is here just to catch RESIZE events
522 if ($ch eq KEY_RESIZE) {
525 rec_stdin($ch) unless $ch eq '-1';
528 $top->refresh() if $top->is_wintouched;
535 $conn->send_later("A$call|$connsort width=$cols");
536 $conn->send_later("I$call|set/page $maxshist");
537 #$conn->send_later("I$call|set/nobeep");
543 Mojo::IOLoop->remove($idle);
552 while (@ARGV && $ARGV[0] =~ /^-/) {
561 $call = uc shift @ARGV if @ARGV;
562 $call = uc $myalias unless $call;
563 $node = uc $mycall unless $node;
565 my ($scall, $ssid) = split /-/, $call;
566 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
568 $ssid = 15 if $ssid > 15;
569 $call = "$scall-$ssid";
572 if ($call eq $mycall) {
573 print "You cannot connect as your cluster callsign ($mycall)\n";
577 unless ($DB::VERSION) {
578 $SIG{'INT'} = \&sig_term;
579 $SIG{'TERM'} = \&sig_term;
582 $SIG{'HUP'} = \&sig_term;
587 $SIG{__DIE__} = \&sig_term;
589 #$Text::Wrap::Columns = $cols;
594 $conn = IntMsg->connect($clusteraddr, $clusterport, rproc => \&rec_socket);
595 $conn->{on_connect} = \&on_connect;
596 $conn->{on_disconnect} = \&on_disconnect;
598 my $timer = Mojo::IOLoop->recurring(1, sub {DXLog::flushall()}) if $DXDebug::fp;
600 $idle = Mojo::IOLoop->recurring(0.100 => \&idle_loop);
601 Mojo::IOLoop->singleton->reactor->io(\*STDIN => sub {
602 my $ch = $bot->getch();
608 })->watch(\*STDIN, 1, 0);