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