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