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 dbg("resize: l=$lines c=$cols");
136 dbg("resize: sh=". scalar @sh );
139 # while (defined ($t = shift @sh)) {
140 # dbg("t: $t(" , length $t . ')');
143 # push(@tsh, pop(@tsh) . $t)
147 # dbg("tsh: " . scalar @tsh);
149 # dbg("resize: tsh=". scalar @tsh );
150 # $spos = @tsh < $pagel ? 0 : @tsh - $pagel;
152 $spos = @sh < $pagel ? 0 : @sh - $pagel;
154 $conn->send_later("C$call|$cols") if $conn;
157 # cease communications
161 $conn->disconnect if $conn;
168 # terminate program from signal
174 # determine the colour of the line
178 foreach my $ref (@colors) {
179 if ($_[0] =~ m{$$ref[0]}) {
180 $top->attrset($$ref[1]);
187 # measure the no of screen lines a line will take
191 return 0 unless $line;
193 my $l = length $line;
194 my $lines = int ($l / $cols);
195 $lines++ if $l / $cols > $lines;
199 # display the top screen
204 dbg("B: s:$spos h:" . scalar @sh) if isdbg('console');
206 # for ($i = 0; $i < $pagel && $p >= 0; ) {
207 # $l = measure($sh[$p]);
209 # $p-- if $i < $pagel;
212 $spos = 0 if $spos < 0;
215 $top->attrset(COLOR_PAIR(0)) if $has_colors;
217 for ($i = 0; $i < $pagel && $y < @sh; ++$y) {
219 # my $lines = measure($line);
222 dbg("C: s:$spos y:$i sh:" . scalar @sh . " l:" . length($line) . " '$line'") if isdbg('console');
225 $top->attrset(COLOR_PAIR(0)) if $has_colors;
232 } elsif ($spos < @sh || $spos < $pagel) {
233 # if we really are scrolling thru at the end of the history
234 while ($spos < @sh) {
235 my $line = $sh[$spos];
244 dbg("A: s:$spos sh:" . scalar @sh . " y:$y l:" . length($line) . " '$line'") if isdbg('console');
249 $top->attrset(COLOR_PAIR(0)) if $has_colors;
252 shift @sh while @sh > $maxshist;
258 my $size = $lines . 'x' . $cols . '-';
259 my $add = "-$spos-$shl";
260 my $time = ztime(time);
261 my $c = "$call\@$node";
262 my $str = "-" . $time . '-' . ($inscroll ? 'S':'-') . '-' x ($cols - (length($size) + length($c) + length($add) + length($time) + 3));
263 $scr->addstr($lines-4, 0, $str);
266 $scr->attrset($mycallcolor) if $has_colors;
268 $scr->attrset(COLOR_PAIR(0)) if $has_colors;
278 dbg("KEY: " . unpack("H*", $r). " '$r'") if isdbg('console');
282 # $prbuf =~ s/\r/\\r/;
283 # $prbuf =~ s/\n/\\n/;
284 # print "sys: $r ($prbuf)\n";
289 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
292 $inbuf = " " unless length $inbuf;
294 # check for a pling and do a search back for a command
295 if ($inbuf =~ /^!/o) {
298 for ($i = $#kh; $i >= 0; $i--) {
299 if ($kh[$i] =~ /^$inbuf/) {
309 push @kh, $inbuf if length $inbuf;
310 shift @kh if @kh > $maxkhist;
314 $bot->addstr(substr($inbuf, 0, $cols));
316 # add it to the monitor window
317 # unless ($spos == @sh) {
321 if ($inscroll && $spos < @sh) {
322 $spos = @sh - $pagel;
329 # send it to the cluster
330 $conn->send_later("I$call|$inbuf");
333 } elsif ($r eq KEY_UP || $r eq "\020") {
337 $pos = $lth = length $inbuf;
341 } elsif ($r eq KEY_DOWN || $r eq "\016") {
342 if ($kpos < @kh - 1) {
345 $pos = $lth = length $inbuf;
349 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
350 if ($spos > 0 && @sh > $pagel) {
352 # for ($i = 0; $i < $pagel-1 && $spos >= 0; ) {
353 # $l = measure($sh[$spos]);
355 # --$spos if $i <= $pagel;
357 $spos -= $pagel+int($pagel/2);
358 $spos = 0 if $spos < 0;
364 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
365 if ($inscroll && $spos < @sh) {
367 # for ($i = 0; $i <= $pagel && $spos < @sh; ) {
368 # $l = measure($sh[$spos]);
370 # ++$spos if $i <= $pagel && $spos < @sh;
373 dbg("NPAGE sp:$spos $sh:". scalar @sh . " pl: $pagel") if isdbg('console');
374 $spos += int($pagel/2);
375 if ($spos > @sh - $pagel) {
376 $spos = @sh - $pagel;
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 ($r eq "\x12" || $r eq "\x0c") {
429 dbg("REDRAW called") if isdbg('console');
432 } elsif ($r eq "\013") {
433 $inbuf = substr($inbuf, 0, $pos);
434 $lth = length $inbuf;
435 } elsif (defined $r && is_pctext($r)) {
436 # move the top screen back to the bottom if you type something
438 if ($inscroll && $spos < @sh) {
439 $spos = @sh - $pagel;
444 # $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
446 # insert the character into the keyboard buffer
448 my $a = substr($inbuf, 0, $pos);
449 my $b = substr($inbuf, $pos);
450 $inbuf = $a . $r . $b;
462 $bot->addstr($inbuf);
469 # add a line to the end of the top screen
472 $Text::Wrap::Columns = $cols;
475 my $l = length $inbuf;
476 dbg("addtotop: $l $inbuf");
479 if (length $inbuf > $cols) {
480 push @sh, split /\n/, wrap('',' ' x 19, $inbuf);
488 show_screen() unless $inscroll;
491 # handle incoming messages
494 my ($con, $msg, $err) = @_;
495 if (defined $err && $err) {
499 dbg("msg: " . length($msg) . " '$msg'") if isdbg('console');
500 my ($sort, $incall, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
501 if ($line =~ s/\x07+$//) {
504 $line =~ s/[\r\n]+//s;
506 # change my call if my node says "tonight Michael you are Jane" or something like that...
507 $call = $incall if $call ne $incall;
509 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
510 if ($sort && $sort eq 'D') {
511 $line = " " unless length($line);
513 } elsif ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
516 # ******************************************************
517 # ******************************************************
518 # any other sorts that might happen are silently ignored.
519 # ******************************************************
520 # ******************************************************
534 if ($t > $lasttime) {
535 my ($min)= (gmtime($t))[1];
536 if ($min != $lastmin) {
537 show_screen() unless $inscroll;
542 my $ch = $bot->getch(); # this is here just to catch RESIZE events
544 if ($ch eq KEY_RESIZE) {
547 rec_stdin($ch) unless $ch eq '-1';
550 $top->refresh() if $top->is_wintouched;
557 $conn->send_later("A$call|$connsort width=$cols");
558 $conn->send_later("I$call|set/page $maxshist");
559 #$conn->send_later("I$call|set/nobeep");
565 Mojo::IOLoop->remove($idle);
574 while (@ARGV && $ARGV[0] =~ /^-/) {
583 $call = uc shift @ARGV if @ARGV;
584 $call = uc $myalias unless $call;
585 $node = uc $mycall unless $node;
587 my ($scall, $ssid) = split /-/, $call;
588 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
590 $ssid = 15 if $ssid > 15;
591 $call = "$scall-$ssid";
594 if ($call eq $mycall) {
595 print "You cannot connect as your cluster callsign ($mycall)\n";
599 unless ($DB::VERSION) {
600 $SIG{'INT'} = \&sig_term;
601 $SIG{'TERM'} = \&sig_term;
604 $SIG{'HUP'} = \&sig_term;
608 $Text::Wrap::Columns = $cols;
611 $SIG{__DIE__} = \&sig_term;
613 #$Text::Wrap::Columns = $cols;
618 $conn = IntMsg->connect($clusteraddr, $clusterport, rproc => \&rec_socket);
619 $conn->{on_connect} = \&on_connect;
620 $conn->{on_disconnect} = \&on_disconnect;
622 my $timer = Mojo::IOLoop->recurring(1, sub {DXLog::flushall()}) if $DXDebug::fp;
624 $idle = Mojo::IOLoop->recurring(0.100 => \&idle_loop);
625 Mojo::IOLoop->singleton->reactor->io(\*STDIN => sub {
626 my $ch = $bot->getch();
632 })->watch(\*STDIN, 1, 0);