3fbc46fe64ab432169d125e1281c007b75a0b76a
[spider.git] / perl / console.pl
1 #!/usr/bin/perl -w
2 #
3 # this is the operators console.
4 #
5 # Calling syntax is:-
6 #
7 # console.pl [callsign] 
8 #
9 # if the callsign isn't given then the sysop callsign in DXVars.pm is assumed
10 #
11 # Copyright (c) 1999 Dirk Koopman G1TLH
12 #
13 #
14
15
16 require 5.004;
17 package main;
18
19 use vars qw($data $clusteraddr $clusterport);
20
21 $clusteraddr = '127.0.0.1';     # cluster tcp host address - used for things like console.pl
22 $clusterport = 27754;           # cluster tcp port
23
24 # search local then perl directories
25 BEGIN {
26         # root of directory tree for this system
27         $root = "/spider"; 
28         $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
29         
30         unshift @INC, "$root/perl";     # this IS the right way round!
31         unshift @INC, "$root/local";
32         $is_win = ($^O =~ /^MS/ || $^O =~ /^OS-2/) ? 1 : 0; # is it Windows?
33         $data = "$root/data";
34 }
35
36 use Msg;
37 use IntMsg;
38 use DXVars;
39 use DXDebug;
40 use DXUtil;
41 use DXDebug;
42 use IO::File;
43 use Time::HiRes qw(gettimeofday tv_interval);
44 use Curses 1.06;
45 use Text::Wrap;
46
47 use Console;
48
49 #
50 # initialisation
51 #
52
53 $call = "";                     # the callsign being used
54 $node = "";                     # the node callsign being used
55
56 $conn = 0;                      # the connection object for the cluster
57 $lasttime = time;               # lasttime something happened on the interface
58
59 $connsort = "local";
60 @kh = ();
61 @sh = ();
62 $khistpos = 0;
63 $spos = $pos = $lth = 0;
64 $inbuf = "";
65 @time = ();
66
67 #$SIG{WINCH} = sub {@time = gettimeofday};
68
69 sub mydbg
70 {
71         local *STDOUT = undef;
72         dbg(@_);
73 }
74
75 # do the screen initialisation
76 sub do_initscr
77 {
78         $scr = new Curses;
79         if ($has_colors) {
80                 start_color();
81                 init_pair("0", $foreground, $background);
82 #               init_pair(0, $background, $foreground);
83                 init_pair(1, COLOR_RED, $background);
84                 init_pair(2, COLOR_YELLOW, $background);
85                 init_pair(3, COLOR_GREEN, $background);
86                 init_pair(4, COLOR_CYAN, $background);
87                 init_pair(5, COLOR_BLUE, $background);
88                 init_pair(6, COLOR_MAGENTA, $background);
89                 init_pair(7, COLOR_RED, COLOR_BLUE);
90                 init_pair(8, COLOR_YELLOW, COLOR_BLUE);
91                 init_pair(9, COLOR_GREEN, COLOR_BLUE);
92                 init_pair(10, COLOR_CYAN, COLOR_BLUE);
93                 init_pair(11, COLOR_BLUE, COLOR_RED);
94                 init_pair(12, COLOR_MAGENTA, COLOR_BLUE);
95                 init_pair(13, COLOR_YELLOW, COLOR_GREEN);
96                 init_pair(14, COLOR_RED, COLOR_GREEN);
97                 eval { assume_default_colors($foreground, $background) } unless $is_win;
98         }
99
100         $top = $scr->subwin($lines-4, $cols, 0, 0);
101         $top->intrflush(0);
102         $top->scrollok(1);
103         $top->idlok(1);
104         $top->meta(1);
105 #       $scr->addstr($lines-4, 0, '-' x $cols);
106         $bot = $scr->subwin(3, $cols, $lines-3, 0);
107         $bot->intrflush(0);
108         $bot->scrollok(1);
109         $top->idlok(1);
110         $bot->keypad(1);
111         $bot->move(1,0);
112         $bot->meta(1);
113         $bot->nodelay(1);
114         $scr->refresh();
115         
116         $pagel = $lines-4;
117         $mycallcolor = COLOR_PAIR(1) unless $mycallcolor;
118 }
119
120 sub do_resize
121 {
122         endwin() if $scr;
123         initscr();
124         raw();
125         noecho();
126         nonl();
127         $lines = LINES;
128         $cols = COLS;
129         $has_colors = has_colors();
130         do_initscr();
131         $inscroll = 0;
132         $spos = @sh < $pagel ? 0 :  @sh - $pagel;
133         show_screen();
134         $conn->send_later("C$call|$cols") if $conn;
135         
136 }
137
138 # cease communications
139 sub cease
140 {
141         my $sendz = shift;
142         $conn->disconnect if $conn;
143         endwin();
144         dbgclose();
145         print @_ if @_;
146         exit(0);        
147 }
148
149 # terminate program from signal
150 sub sig_term
151 {
152         cease(1, @_);
153 }
154
155 # determine the colour of the line
156 sub setattr
157 {
158         if ($has_colors) {
159                 foreach my $ref (@colors) {
160                         if ($_[0] =~ m{$$ref[0]}) {
161                                 $top->attrset($$ref[1]);
162                                 last;
163                         }
164                 }
165         }
166 }
167
168
169 # display the top screen
170 sub show_screen
171 {       if ($inscroll) {
172                 
173                 dbg("B: s:$spos h:" . scalar @sh) if isdbg('console');
174                 my ($i, $l);
175
176                 $spos = 0 if $spos < 0;
177                 my $y = $spos;
178                 $top->move(0, 0);
179                 $top->attrset(COLOR_PAIR(0)) if $has_colors;
180                 $top->clrtobot();
181                 for ($i = 0; $i < $pagel && $y < @sh; ++$y) {
182                         my $line = $sh[$y];
183                         my $lines = 1;
184                         $top->move($i, 0);
185                         dbg("C: s:$spos y:$i sh:" . scalar @sh . " l:" . length($line) . " '$line'") if isdbg('console');
186                         setattr($line);
187                         $top->addstr($line);
188                         $top->attrset(COLOR_PAIR(0)) if $has_colors;
189                         $i += $lines;
190                 }
191                 if ($y >= @sh) {
192                         $inscroll = 0;
193                         $spos = @sh;
194                 }
195         }       elsif ($spos < @sh || $spos < $pagel) {
196                 # if we really are scrolling thru at the end of the history
197                 while ($spos < @sh) {
198                         my $line = $sh[$spos];
199                         my $y = $spos;
200                         if ($y >= $pagel) {
201                                 $top->scrollok(1);
202                                 $top->scrl(1);
203                                 $top->scrollok(0);
204                                 $y = $pagel-1;
205                         }
206                         $top->move($y, 0);
207                         dbg("A: s:$spos sh:" . scalar @sh . " y:$y l:" . length($line) . " '$line'") if isdbg('console');
208                         $top->refresh;
209                         setattr($line);
210                         $line =~ s/\n//s;
211                         $top->addstr($line);
212                         $top->attrset(COLOR_PAIR(0)) if $has_colors;
213                         ++$spos;
214                 }
215                 shift @sh while @sh > $maxshist;
216                 $spos = @sh;
217         }
218
219         $top->refresh;
220     my $shl = @sh;
221         my $size = $lines . 'x' . $cols . '-'; 
222         my $add = "-$spos-$shl";
223     my $time = ztime(time);
224         my $c = "$call\@$node";
225         my $str =  "-" . $time . '-' . ($inscroll ? 'S':'-') . '-' x ($cols - (length($size) + length($c) + length($add) + length($time) + 3));
226         $scr->addstr($lines-4, 0, $str);
227         
228         $scr->addstr($size);
229         $scr->attrset($mycallcolor) if $has_colors;
230         $scr->addstr($c);
231         $scr->attrset(COLOR_PAIR(0)) if $has_colors;
232     $scr->addstr($add);
233         $scr->refresh();
234 #       $top->refresh();
235 }
236
237 # add a line to the end of the top screen
238 sub addtotop
239 {
240         while (@_) {
241                 my $inbuf = shift;
242                 my $l = length $inbuf;
243                 if ($l > $cols) {
244                         $inbuf =~ s/\s+/ /g;
245                         if (length $inbuf > $cols) {
246                                 $Text::Wrap::columns = $cols;
247                                 my $token;
248                                 ($token) = $inbuf =~ m!^(.* de [-\w\d/\#]+:?\s+|\w{9}\@\d\d:\d\d:\d\d )!;
249                                 $token ||= ' ' x 19;
250                                 push @sh, split /\n/, wrap('', ' ' x length($token), $inbuf);
251                         } else {
252                                 push @sh, $inbuf;
253                         }
254                 } else {
255                         push @sh, $inbuf;
256                 }
257         }
258 #       shift @sh while @sh > $maxshist;
259         show_screen();
260 }
261
262 # handle incoming messages
263 sub rec_socket
264 {
265         my ($con, $msg, $err) = @_;
266         if (defined $err && $err) {
267                 cease(1);
268         }
269         if (defined $msg) {
270                 my ($sort, $incall, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
271                 if ($line =~ s/\x07+$//) {
272                         beep();
273                 }
274                 $line =~ s/[\r\n]+//s;
275
276                 # change my call if my node says "tonight Michael you are Jane" or something like that...
277                 $call = $incall if $call ne $incall;
278                 
279                 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g;         # immutable CSI sequence + control characters
280                 if ($sort && $sort eq 'D') {
281                         $line = " " unless length($line);
282                         addtotop($line);
283                 } elsif ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
284                         cease(0);
285                 }         
286                 
287                 # ******************************************************
288                 # ******************************************************
289                 # any other sorts that might happen are silently ignored.
290                 # ******************************************************
291                 # ******************************************************
292         } else {
293                 cease(0);
294         }
295         $top->refresh();
296         $lasttime = time; 
297 }
298
299 sub rec_stdin
300 {
301         my $r = shift;;
302         
303         #  my $prbuf;
304         #  $prbuf = $buf;
305         #  $prbuf =~ s/\r/\\r/;
306         #  $prbuf =~ s/\n/\\n/;
307         #  print "sys: $r ($prbuf)\n";
308         if (defined $r) {
309
310                 $r = '0' if !$r;
311                 
312                 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
313                         
314                         # save the lines
315                         $inbuf = " " unless length $inbuf;
316
317                         # check for a pling and do a search back for a command
318                         if ($inbuf =~ /^!/o) {
319                                 my $i;
320                                 $inbuf =~ s/^!//o;
321                                 for ($i = $#kh; $i >= 0; $i--) {
322                                         if ($kh[$i] =~ /^$inbuf/) {
323                                                 $inbuf = $kh[$i];
324                                                 last;
325                                         }
326                                 }
327                                 if ($i < 0) {
328                                         beep();
329                                         return;
330                                 }
331                         }
332                         push @kh, $inbuf if length $inbuf;
333                         shift @kh if @kh > $maxkhist;
334                         $khistpos = @kh;
335                         $bot->move(0,0);
336                         $bot->clrtoeol();
337                         $bot->addstr(substr($inbuf, 0, $cols));
338
339                         if ($inscroll && $spos < @sh) {
340                                 $spos = @sh - $pagel;
341                                 $inscroll = 0;
342                                 show_screen();
343                         }
344
345                         # add it to the monitor window
346                         addtotop($inbuf);
347                 
348                         # send it to the cluster
349                         $conn->send_later("I$call|$inbuf");
350                         $inbuf = "";
351                         $pos = $lth = 0;
352                 } elsif ($r eq KEY_UP || $r eq "\020") {
353                         if ($khistpos > 0) {
354                                 --$khistpos;
355                                 $inbuf = $kh[$khistpos];
356                                 $pos = $lth = length $inbuf;
357                         } else {
358                                 beep();
359                         }
360                 } elsif ($r eq KEY_DOWN || $r eq "\016") {
361                         if ($khistpos < @kh - 1) {
362                                 ++$khistpos;
363                                 $inbuf = $kh[$khistpos];
364                                 $pos = $lth = length $inbuf;
365                         } else {
366                                 beep();
367                         }
368                 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
369                         if ($spos > 0 && @sh > $pagel) {
370                                 $spos -= $pagel+int($pagel/2); 
371                                 $spos = 0 if $spos < 0;
372                                 $inscroll = 1;
373                                 show_screen();
374                         } else {
375                                 beep();
376                         }
377                 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
378                         if ($inscroll && $spos < @sh) {
379
380                                 $spos += int($pagel/2);
381                                 if ($spos > @sh - $pagel) {
382                                         $spos = @sh - $pagel;
383                                 } 
384                                 show_screen();
385                                 if ($spos >= @sh) {
386                                         $spos = @sh;
387                                         $inscroll = 0;
388                                 }
389                         } else {
390                                 beep();
391                         }
392                 } elsif ($r eq KEY_LEFT || $r eq "\002") {
393                         if ($pos > 0) {
394                                 --$pos;
395                         } else {
396                                 beep();
397                         }
398                 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
399                         if ($pos < $lth) {
400                                 ++$pos;
401                         } else {
402                                 beep();
403                         }
404                 } elsif ($r eq KEY_HOME || $r eq "\001") {
405                         $pos = 0;
406                 } elsif ($r eq KEY_END || $r eq "\005") {
407                         $pos = $lth;
408                 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
409                         if ($pos > 0) {
410                                 my $a = substr($inbuf, 0, $pos-1);
411                                 my $b = substr($inbuf, $pos) if $pos < $lth;
412                                 $b = "" unless $b;
413                                 
414                                 $inbuf = $a . $b;
415                                 --$lth;
416                                 --$pos;
417                         } else {
418                                 beep();
419                         }
420                 } elsif ($r eq KEY_DC || $r eq "\004") {
421                         if ($pos < $lth) {
422                                 my $a = substr($inbuf, 0, $pos);
423                                 my $b = substr($inbuf, $pos+1) if $pos < $lth;
424                                 $b = "" unless $b;
425                                 
426                                 $inbuf = $a . $b;
427                                 --$lth;
428                         } else {
429                                 beep();
430                         }
431                 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
432                         do_resize();
433                         return;
434                 } elsif (defined $r && is_pctext($r)) {
435                         # move the top screen back to the bottom if you type something
436                         if ($spos < @sh) {
437                                 $spos = @sh;
438                                 show_screen();
439                         }
440
441                 #       $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
442                         
443                         # insert the character into the keyboard buffer
444                         if ($pos < $lth) {
445                                 my $a = substr($inbuf, 0, $pos);
446                                 my $b = substr($inbuf, $pos);
447                                 $inbuf = $a . $r . $b;
448                         } else {
449                                 $inbuf .= $r;
450                         }
451                         $pos++;
452                         $lth++;
453                 } elsif ($r eq "\014" || $r eq "\022") {
454                         touchwin(curscr, 1);
455                         refresh(curscr);
456                         return;
457                 } elsif ($r eq "\013") {
458                         $inbuf = substr($inbuf, 0, $pos);
459                         $lth = length $inbuf;
460                 } else {
461                         beep();
462                 }
463                 $bot->move(1, 0);
464                 $bot->clrtobot();
465                 $bot->addstr($inbuf);
466         } 
467         $bot->move(1, $pos);
468         $bot->refresh();
469 }
470
471
472 #
473 # deal with args
474 #
475
476 $call = uc shift @ARGV if @ARGV;
477 $call = uc $myalias if !$call;
478 $node = uc $mycall unless $node;
479
480 my ($scall, $ssid) = split /-/, $call;
481 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;  
482 if ($ssid) {
483         $ssid = 15 if $ssid > 15;
484         $call = "$scall-$ssid";
485 }
486
487 if ($call eq $mycall) {
488         print "You cannot connect as your cluster callsign ($mycall)\n";
489         exit(0);
490 }
491
492 dbginit();
493
494 $conn = IntMsg->connect("$clusteraddr", $clusterport, \&rec_socket);
495 if (! $conn) {
496         if (-r "$data/offline") {
497                 open IN, "$data/offline" or die;
498                 while (<IN>) {
499                         print $_;
500                 }
501                 close IN;
502         } else {
503                 print "Sorry, the cluster $mycall is currently off-line\n";
504         }
505         exit(0);
506 }
507
508 $conn->set_error(sub{cease(0)});
509
510
511 unless ($DB::VERSION) {
512         $SIG{'INT'} = \&sig_term;
513         $SIG{'TERM'} = \&sig_term;
514 }
515
516 $SIG{'HUP'} = \&sig_term;
517
518 # start up
519 do_resize();
520
521 $SIG{__DIE__} = \&sig_term;
522
523 $conn->send_later("A$call|$connsort width=$cols");
524 $conn->send_later("I$call|set/page $maxshist");
525 $conn->send_later("I$call|set/nobeep");
526
527 #Msg->set_event_handler(\*STDIN, "read" => \&rec_stdin);
528
529 $Text::Wrap::columns = $cols;
530
531 my $lastmin = 0;
532 for (;;) {
533         my $t;
534         Msg->event_loop(1, 0.01);
535         $t = time;
536         if ($t > $lasttime) {
537                 my ($min)= (gmtime($t))[1];
538                 if ($min != $lastmin) {
539                         show_screen();
540                         $lastmin = $min;
541                 }
542                 $lasttime = $t;
543         }
544         my $ch = $bot->getch();
545         if (@time && tv_interval(\@time, [gettimeofday]) >= 1) {
546                 next;
547         }
548         if (defined $ch) {
549                 if ($ch ne '-1') {
550                         rec_stdin($ch);
551                 }
552         }
553         $top->refresh() if $top->is_wintouched;
554         $bot->refresh();
555 }
556
557 exit(0);