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