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