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