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