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);
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;
69 #$SIG{WINCH} = sub {@time = gettimeofday};
73 local *STDOUT = undef;
77 # do the screen initialisation
83 init_pair("0", $foreground, $background);
84 # init_pair(0, $background, $foreground);
85 init_pair(1, COLOR_RED, $background);
86 init_pair(2, COLOR_YELLOW, $background);
87 init_pair(3, COLOR_GREEN, $background);
88 init_pair(4, COLOR_CYAN, $background);
89 init_pair(5, COLOR_BLUE, $background);
90 init_pair(6, COLOR_MAGENTA, $background);
91 init_pair(7, COLOR_RED, COLOR_BLUE);
92 init_pair(8, COLOR_YELLOW, COLOR_BLUE);
93 init_pair(9, COLOR_GREEN, COLOR_BLUE);
94 init_pair(10, COLOR_CYAN, COLOR_BLUE);
95 init_pair(11, COLOR_BLUE, COLOR_RED);
96 init_pair(12, COLOR_MAGENTA, COLOR_BLUE);
97 init_pair(13, COLOR_YELLOW, COLOR_GREEN);
98 init_pair(14, COLOR_RED, COLOR_GREEN);
99 eval { assume_default_colors($foreground, $background) } unless $is_win;
102 $top = $scr->subwin($lines-4, $cols, 0, 0);
107 # $scr->addstr($lines-4, 0, '-' x $cols);
108 $bot = $scr->subwin(3, $cols, $lines-3, 0);
119 $mycallcolor = COLOR_PAIR(1) unless $mycallcolor;
131 $has_colors = has_colors();
137 # cease communications
141 $conn->disconnect if $conn;
148 # terminate program from signal
154 # determine the colour of the line
158 foreach my $ref (@colors) {
159 if ($_[0] =~ m{$$ref[0]}) {
160 $top->attrset($$ref[1]);
167 # measure the no of screen lines a line will take
171 return 0 unless $line;
173 my $l = length $line;
174 my $lines = int ($l / $cols);
175 $lines++ if $l / $cols > $lines;
179 # display the top screen
182 if ($spos == @shistory - 1) {
184 # if we really are scrolling thru at the end of the history
185 my $line = $shistory[$spos];
186 $top->addstr("\n") if $spos > 0;
189 # $top->addstr("\n");
190 $top->attrset(COLOR_PAIR(0)) if $has_colors;
198 for ($i = 0; $i < $pagel && $p >= 0; ) {
199 $l = measure($shistory[$p]);
206 $top->attrset(COLOR_PAIR(0)) if $has_colors;
208 for ($i = 0; $i < $pagel && $p < @shistory; $p++) {
209 my $line = $shistory[$p];
210 my $lines = measure($line);
211 last if $i + $lines > $pagel;
212 $top->addstr("\n") if $i;
215 $top->attrset(COLOR_PAIR(0)) if $has_colors;
219 $spos = @shistory if $spos > @shistory;
222 my $size = $lines . 'x' . $cols . '-';
223 my $add = "-$spos-$shl";
224 my $time = ztime(time);
225 my $str = "-" . $time . '-' x ($cols - (length($size) + length($call) + length($add) + length($time) + 1));
226 $scr->addstr($lines-4, 0, $str);
229 $scr->attrset($mycallcolor) if $has_colors;
231 $scr->attrset(COLOR_PAIR(0)) if $has_colors;
237 # add a line to the end of the top screen
242 if ($inbuf =~ s/\x07+$//) {
245 if (length $inbuf >= $cols) {
246 $Text::Wrap::Columns = $cols;
247 push @shistory, wrap('',"\t", $inbuf);
249 push @shistory, $inbuf;
251 shift @shistory while @shistory > $maxshist;
256 # handle incoming messages
259 my ($con, $msg, $err) = @_;
260 if (defined $err && $err) {
264 my ($sort, $incall, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
266 # change my call if my node says "tonight Michael you are Jane" or something like that...
267 $call = $incall if $call ne $incall;
269 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
270 if ($sort && $sort eq 'D') {
271 $line = " " unless length($line);
273 } elsif ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
276 # ******************************************************
277 # ******************************************************
278 # any other sorts that might happen are silently ignored.
279 # ******************************************************
280 # ******************************************************
294 # $prbuf =~ s/\r/\\r/;
295 # $prbuf =~ s/\n/\\n/;
296 # print "sys: $r ($prbuf)\n";
301 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
304 $inbuf = " " unless length $inbuf;
306 # check for a pling and do a search back for a command
307 if ($inbuf =~ /^!/o) {
310 for ($i = $#khistory; $i >= 0; $i--) {
311 if ($khistory[$i] =~ /^$inbuf/) {
312 $inbuf = $khistory[$i];
321 push @khistory, $inbuf if length $inbuf;
322 shift @khistory if @khistory > $maxkhist;
323 $khistpos = @khistory;
326 $bot->addstr(substr($inbuf, 0, $cols));
328 # add it to the monitor window
329 unless ($spos == @shistory) {
335 # send it to the cluster
336 $conn->send_later("I$call|$inbuf");
339 } elsif ($r eq KEY_UP || $r eq "\020") {
342 $inbuf = $khistory[$khistpos];
343 $pos = $lth = length $inbuf;
347 } elsif ($r eq KEY_DOWN || $r eq "\016") {
348 if ($khistpos < @khistory - 1) {
350 $inbuf = $khistory[$khistpos];
351 $pos = $lth = length $inbuf;
355 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
358 for ($i = 0; $i < $pagel-1 && $spos >= 0; ) {
359 $l = measure($shistory[$spos]);
361 $spos-- if $i <= $pagel;
363 $spos = 0 if $spos < 0;
368 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
369 if ($spos < @shistory - 1) {
371 for ($i = 0; $i <= $pagel && $spos <= @shistory; ) {
372 $l = measure($shistory[$spos]);
374 $spos++ if $i <= $pagel;
376 $spos = @shistory if $spos >= @shistory - 1;
381 } elsif ($r eq KEY_LEFT || $r eq "\002") {
387 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
393 } elsif ($r eq KEY_HOME || $r eq "\001") {
395 } elsif ($r eq KEY_END || $r eq "\005") {
397 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
399 my $a = substr($inbuf, 0, $pos-1);
400 my $b = substr($inbuf, $pos) if $pos < $lth;
409 } elsif ($r eq KEY_DC || $r eq "\004") {
411 my $a = substr($inbuf, 0, $pos);
412 my $b = substr($inbuf, $pos+1) if $pos < $lth;
420 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
423 } elsif (defined $r && is_pctext($r)) {
424 # move the top screen back to the bottom if you type something
425 if ($spos < @shistory) {
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;
442 } elsif ($r eq "\014" || $r eq "\022") {
446 } elsif ($r eq "\013") {
447 $inbuf = substr($inbuf, 0, $pos);
448 $lth = length $inbuf;
454 $bot->addstr($inbuf);
465 if ($t > $lasttime) {
466 my ($min)= (gmtime($t))[1];
467 if ($min != $lastmin) {
473 my $ch = $bot->getch();
474 if (@time && tv_interval(\@time, [gettimeofday]) >= 1) {
482 $top->refresh() if $top->is_wintouched;
489 $conn->send_later("A$call|$connsort width=$cols");
490 $conn->send_later("I$call|set/page $maxshist");
491 #$conn->send_later("I$call|set/nobeep");
497 Mojo::IOLoop->remove($idle);
505 $call = uc shift @ARGV if @ARGV;
506 $call = uc $myalias if !$call;
507 my ($scall, $ssid) = split /-/, $call;
508 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;
510 $ssid = 15 if $ssid > 15;
511 $call = "$scall-$ssid";
514 if ($call eq $mycall) {
515 print "You cannot connect as your cluster callsign ($mycall)\n";
521 unless ($DB::VERSION) {
522 $SIG{'INT'} = \&sig_term;
523 $SIG{'TERM'} = \&sig_term;
526 $SIG{'HUP'} = \&sig_term;
531 $SIG{__DIE__} = \&sig_term;
533 $Text::Wrap::Columns = $cols;
538 $conn = IntMsg->connect($clusteraddr, $clusterport, rproc => \&rec_socket);
539 $conn->{on_connect} = \&on_connect;
540 $conn->{on_disconnect} = \&on_disconnect;
542 $idle = Mojo::IOLoop->recurring(0.100 => \&idle_loop);