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