backport mojo console.pl width/scolling changes
[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 }
135
136 # cease communications
137 sub cease
138 {
139         my $sendz = shift;
140         $conn->disconnect if $conn;
141         endwin();
142         dbgclose();
143         print @_ if @_;
144         exit(0);        
145 }
146
147 # terminate program from signal
148 sub sig_term
149 {
150         cease(1, @_);
151 }
152
153 # determine the colour of the line
154 sub setattr
155 {
156         if ($has_colors) {
157                 foreach my $ref (@colors) {
158                         if ($_[0] =~ m{$$ref[0]}) {
159                                 $top->attrset($$ref[1]);
160                                 last;
161                         }
162                 }
163         }
164 }
165
166
167 # display the top screen
168 sub show_screen
169 {       if ($inscroll) {
170                 
171                 dbg("B: s:$spos h:" . scalar @sh) if isdbg('console');
172                 my ($i, $l);
173
174                 $spos = 0 if $spos < 0;
175                 my $y = $spos;
176                 $top->move(0, 0);
177                 $top->attrset(COLOR_PAIR(0)) if $has_colors;
178                 $top->clrtobot();
179                 for ($i = 0; $i < $pagel && $y < @sh; ++$y) {
180                         my $line = $sh[$y];
181                         my $lines = 1;
182                         $top->move($i, 0);
183                         dbg("C: s:$spos y:$i sh:" . scalar @sh . " l:" . length($line) . " '$line'") if isdbg('console');
184                         setattr($line);
185                         $top->addstr($line);
186                         $top->attrset(COLOR_PAIR(0)) if $has_colors;
187                         $i += $lines;
188                 }
189                 if ($y >= @sh) {
190                         $inscroll = 0;
191                         $spos = @sh;
192                 }
193         }       elsif ($spos < @sh || $spos < $pagel) {
194                 # if we really are scrolling thru at the end of the history
195                 while ($spos < @sh) {
196                         my $line = $sh[$spos];
197                         my $y = $spos;
198                         if ($y >= $pagel) {
199                                 $top->scrollok(1);
200                                 $top->scrl(1);
201                                 $top->scrollok(0);
202                                 $y = $pagel-1;
203                         }
204                         $top->move($y, 0);
205                         dbg("A: s:$spos sh:" . scalar @sh . " y:$y l:" . length($line) . " '$line'") if isdbg('console');
206                         $top->refresh;
207                         setattr($line);
208                         $line =~ s/\n//s;
209                         $top->addstr($line);
210                         $top->attrset(COLOR_PAIR(0)) if $has_colors;
211                         ++$spos;
212                 }
213                 shift @sh while @sh > $maxshist;
214                 $spos = @sh;
215         }
216
217         $top->refresh;
218     my $shl = @sh;
219         my $size = $lines . 'x' . $cols . '-'; 
220         my $add = "-$spos-$shl";
221     my $time = ztime(time);
222         my $c = "$call\@$node";
223         my $str =  "-" . $time . '-' . ($inscroll ? 'S':'-') . '-' x ($cols - (length($size) + length($c) + length($add) + length($time) + 3));
224         $scr->addstr($lines-4, 0, $str);
225         
226         $scr->addstr($size);
227         $scr->attrset($mycallcolor) if $has_colors;
228         $scr->addstr($c);
229         $scr->attrset(COLOR_PAIR(0)) if $has_colors;
230     $scr->addstr($add);
231         $scr->refresh();
232 #       $top->refresh();
233 }
234
235 # add a line to the end of the top screen
236 sub addtotop
237 {
238         while (@_) {
239                 my $inbuf = shift;
240                 my $l = length $inbuf;
241                 if ($l > $cols) {
242                         $inbuf =~ s/\s+/ /g;
243                         if (length $inbuf > $cols) {
244                                 $Text::Wrap::columns = $cols;
245                                 push @sh, split /\n/, wrap('',' ' x 19, $inbuf);
246                         } else {
247                                 push @sh, $inbuf;
248                         }
249                 } else {
250                         push @sh, $inbuf;
251                 }
252         }
253 #       shift @sh while @sh > $maxshist;
254         show_screen();
255 }
256
257 # handle incoming messages
258 sub rec_socket
259 {
260         my ($con, $msg, $err) = @_;
261         if (defined $err && $err) {
262                 cease(1);
263         }
264         if (defined $msg) {
265                 my ($sort, $incall, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
266                 if ($line =~ s/\x07+$//) {
267                         beep();
268                 }
269                 $line =~ s/[\r\n]+//s;
270
271                 # change my call if my node says "tonight Michael you are Jane" or something like that...
272                 $call = $incall if $call ne $incall;
273                 
274                 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g;         # immutable CSI sequence + control characters
275                 if ($sort && $sort eq 'D') {
276                         $line = " " unless length($line);
277                         addtotop($line);
278                 } elsif ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
279                         cease(0);
280                 }         
281                 
282                 # ******************************************************
283                 # ******************************************************
284                 # any other sorts that might happen are silently ignored.
285                 # ******************************************************
286                 # ******************************************************
287         } else {
288                 cease(0);
289         }
290         $top->refresh();
291         $lasttime = time; 
292 }
293
294 sub rec_stdin
295 {
296         my $r = shift;;
297         
298         #  my $prbuf;
299         #  $prbuf = $buf;
300         #  $prbuf =~ s/\r/\\r/;
301         #  $prbuf =~ s/\n/\\n/;
302         #  print "sys: $r ($prbuf)\n";
303         if (defined $r) {
304
305                 $r = '0' if !$r;
306                 
307                 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
308                         
309                         # save the lines
310                         $inbuf = " " unless length $inbuf;
311
312                         # check for a pling and do a search back for a command
313                         if ($inbuf =~ /^!/o) {
314                                 my $i;
315                                 $inbuf =~ s/^!//o;
316                                 for ($i = $#kh; $i >= 0; $i--) {
317                                         if ($kh[$i] =~ /^$inbuf/) {
318                                                 $inbuf = $kh[$i];
319                                                 last;
320                                         }
321                                 }
322                                 if ($i < 0) {
323                                         beep();
324                                         return;
325                                 }
326                         }
327                         push @kh, $inbuf if length $inbuf;
328                         shift @kh if @kh > $maxkhist;
329                         $khistpos = @kh;
330                         $bot->move(0,0);
331                         $bot->clrtoeol();
332                         $bot->addstr(substr($inbuf, 0, $cols));
333
334                         if ($inscroll && $spos < @sh) {
335                                 $spos = @sh - $pagel;
336                                 $inscroll = 0;
337                                 show_screen();
338                         }
339
340                         # add it to the monitor window
341                         addtotop($inbuf);
342                 
343                         # send it to the cluster
344                         $conn->send_later("I$call|$inbuf");
345                         $inbuf = "";
346                         $pos = $lth = 0;
347                 } elsif ($r eq KEY_UP || $r eq "\020") {
348                         if ($khistpos > 0) {
349                                 --$khistpos;
350                                 $inbuf = $kh[$khistpos];
351                                 $pos = $lth = length $inbuf;
352                         } else {
353                                 beep();
354                         }
355                 } elsif ($r eq KEY_DOWN || $r eq "\016") {
356                         if ($khistpos < @kh - 1) {
357                                 ++$khistpos;
358                                 $inbuf = $kh[$khistpos];
359                                 $pos = $lth = length $inbuf;
360                         } else {
361                                 beep();
362                         }
363                 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
364                         if ($spos > 0 && @sh > $pagel) {
365                                 $spos -= $pagel+int($pagel/2); 
366                                 $spos = 0 if $spos < 0;
367                                 $inscroll = 1;
368                                 show_screen();
369                         } else {
370                                 beep();
371                         }
372                 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
373                         if ($inscroll && $spos < @sh) {
374
375                                 $spos += int($pagel/2);
376                                 if ($spos > @sh - $pagel) {
377                                         $spos = @sh - $pagel;
378                                 } 
379                                 show_screen();
380                                 if ($spos >= @sh) {
381                                         $spos = @sh;
382                                         $inscroll = 0;
383                                 }
384                         } else {
385                                 beep();
386                         }
387                 } elsif ($r eq KEY_LEFT || $r eq "\002") {
388                         if ($pos > 0) {
389                                 --$pos;
390                         } else {
391                                 beep();
392                         }
393                 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
394                         if ($pos < $lth) {
395                                 ++$pos;
396                         } else {
397                                 beep();
398                         }
399                 } elsif ($r eq KEY_HOME || $r eq "\001") {
400                         $pos = 0;
401                 } elsif ($r eq KEY_END || $r eq "\005") {
402                         $pos = $lth;
403                 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
404                         if ($pos > 0) {
405                                 my $a = substr($inbuf, 0, $pos-1);
406                                 my $b = substr($inbuf, $pos) if $pos < $lth;
407                                 $b = "" unless $b;
408                                 
409                                 $inbuf = $a . $b;
410                                 --$lth;
411                                 --$pos;
412                         } else {
413                                 beep();
414                         }
415                 } elsif ($r eq KEY_DC || $r eq "\004") {
416                         if ($pos < $lth) {
417                                 my $a = substr($inbuf, 0, $pos);
418                                 my $b = substr($inbuf, $pos+1) if $pos < $lth;
419                                 $b = "" unless $b;
420                                 
421                                 $inbuf = $a . $b;
422                                 --$lth;
423                         } else {
424                                 beep();
425                         }
426                 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
427                         do_resize();
428                         return;
429                 } elsif (defined $r && is_pctext($r)) {
430                         # move the top screen back to the bottom if you type something
431                         if ($spos < @sh) {
432                                 $spos = @sh;
433                                 show_screen();
434                         }
435
436                 #       $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
437                         
438                         # insert the character into the keyboard buffer
439                         if ($pos < $lth) {
440                                 my $a = substr($inbuf, 0, $pos);
441                                 my $b = substr($inbuf, $pos);
442                                 $inbuf = $a . $r . $b;
443                         } else {
444                                 $inbuf .= $r;
445                         }
446                         $pos++;
447                         $lth++;
448                 } elsif ($r eq "\014" || $r eq "\022") {
449                         touchwin(curscr, 1);
450                         refresh(curscr);
451                         return;
452                 } elsif ($r eq "\013") {
453                         $inbuf = substr($inbuf, 0, $pos);
454                         $lth = length $inbuf;
455                 } else {
456                         beep();
457                 }
458                 $bot->move(1, 0);
459                 $bot->clrtobot();
460                 $bot->addstr($inbuf);
461         } 
462         $bot->move(1, $pos);
463         $bot->refresh();
464 }
465
466
467 #
468 # deal with args
469 #
470
471 $call = uc shift @ARGV if @ARGV;
472 $call = uc $myalias if !$call;
473 $node = uc $mycall unless $node;
474
475 my ($scall, $ssid) = split /-/, $call;
476 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;  
477 if ($ssid) {
478         $ssid = 15 if $ssid > 15;
479         $call = "$scall-$ssid";
480 }
481
482 if ($call eq $mycall) {
483         print "You cannot connect as your cluster callsign ($mycall)\n";
484         exit(0);
485 }
486
487 dbginit();
488
489 $conn = IntMsg->connect("$clusteraddr", $clusterport, \&rec_socket);
490 if (! $conn) {
491         if (-r "$data/offline") {
492                 open IN, "$data/offline" or die;
493                 while (<IN>) {
494                         print $_;
495                 }
496                 close IN;
497         } else {
498                 print "Sorry, the cluster $mycall is currently off-line\n";
499         }
500         exit(0);
501 }
502
503 $conn->set_error(sub{cease(0)});
504
505
506 unless ($DB::VERSION) {
507         $SIG{'INT'} = \&sig_term;
508         $SIG{'TERM'} = \&sig_term;
509 }
510
511 $SIG{'HUP'} = \&sig_term;
512
513 # start up
514 do_resize();
515
516 $SIG{__DIE__} = \&sig_term;
517
518 $conn->send_later("A$call|$connsort width=$cols");
519 $conn->send_later("I$call|set/page $maxshist");
520 $conn->send_later("I$call|set/nobeep");
521
522 #Msg->set_event_handler(\*STDIN, "read" => \&rec_stdin);
523
524 $Text::Wrap::columns = $cols;
525
526 my $lastmin = 0;
527 for (;;) {
528         my $t;
529         Msg->event_loop(1, 0.01);
530         $t = time;
531         if ($t > $lasttime) {
532                 my ($min)= (gmtime($t))[1];
533                 if ($min != $lastmin) {
534                         show_screen();
535                         $lastmin = $min;
536                 }
537                 $lasttime = $t;
538         }
539         my $ch = $bot->getch();
540         if (@time && tv_interval(\@time, [gettimeofday]) >= 1) {
541                 next;
542         }
543         if (defined $ch) {
544                 if ($ch ne '-1') {
545                         rec_stdin($ch);
546                 }
547         }
548         $top->refresh() if $top->is_wintouched;
549         $bot->refresh();
550 }
551
552 exit(0);