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