]> gb7djk.dxcluster.net Git - spider.git/blob - perl/console.pl
wwv & wcy progress, random lockouts
[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, $call, $line) = $msg =~ /^(\w)([^\|]+)\|(.*)$/;
265                 
266                 $line =~ s/[\x00-\x06\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g;         # immutable CSI sequence + control characters
267                 if ($sort && $sort eq 'D') {
268                         $line = " " unless length($line);
269                         addtotop($line);
270                 } elsif ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
271                         cease(0);
272                 }         
273                 # ******************************************************
274                 # ******************************************************
275                 # any other sorts that might happen are silently ignored.
276                 # ******************************************************
277                 # ******************************************************
278         } else {
279                 cease(0);
280         }
281         $top->refresh();
282         $lasttime = time; 
283 }
284
285 sub rec_stdin
286 {
287         my $r = shift;;
288         
289         #  my $prbuf;
290         #  $prbuf = $buf;
291         #  $prbuf =~ s/\r/\\r/;
292         #  $prbuf =~ s/\n/\\n/;
293         #  print "sys: $r ($prbuf)\n";
294         if (defined $r) {
295
296                 $r = '0' if !$r;
297                 
298                 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
299                         
300                         # save the lines
301                         $inbuf = " " unless length $inbuf;
302
303                         # check for a pling and do a search back for a command
304                         if ($inbuf =~ /^!/o) {
305                                 my $i;
306                                 $inbuf =~ s/^!//o;
307                                 for ($i = $#khistory; $i >= 0; $i--) {
308                                         if ($khistory[$i] =~ /^$inbuf/) {
309                                                 $inbuf = $khistory[$i];
310                                                 last;
311                                         }
312                                 }
313                                 if ($i < 0) {
314                                         beep();
315                                         return;
316                                 }
317                         }
318                         push @khistory, $inbuf if length $inbuf;
319                         shift @khistory if @khistory > $maxkhist;
320                         $khistpos = @khistory;
321                         $bot->move(0,0);
322                         $bot->clrtoeol();
323                         $bot->addstr(substr($inbuf, 0, $cols));
324
325                         # add it to the monitor window
326                         unless ($spos == @shistory) {
327                                 $spos = @shistory;
328                                 show_screen();
329                         };
330                         addtotop($inbuf);
331                 
332                         # send it to the cluster
333                         $conn->send_later("I$call|$inbuf");
334                         $inbuf = "";
335                         $pos = $lth = 0;
336                 } elsif ($r eq KEY_UP || $r eq "\020") {
337                         if ($khistpos > 0) {
338                                 --$khistpos;
339                                 $inbuf = $khistory[$khistpos];
340                                 $pos = $lth = length $inbuf;
341                         } else {
342                                 beep();
343                         }
344                 } elsif ($r eq KEY_DOWN || $r eq "\016") {
345                         if ($khistpos < @khistory - 1) {
346                                 ++$khistpos;
347                                 $inbuf = $khistory[$khistpos];
348                                 $pos = $lth = length $inbuf;
349                         } else {
350                                 beep();
351                         }
352                 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
353                         if ($spos > 0) {
354                                 my ($i, $l);
355                                 for ($i = 0; $i < $pagel-1 && $spos >= 0; ) {
356                                         $l = measure($shistory[$spos]);
357                                         $i += $l;
358                                         $spos-- if $i <= $pagel;
359                                 }
360                                 $spos = 0 if $spos < 0;
361                                 show_screen();
362                         } else {
363                                 beep();
364                         }
365                 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
366                         if ($spos < @shistory - 1) {
367                                 my ($i, $l);
368                                 for ($i = 0; $i <= $pagel && $spos <= @shistory; ) {
369                                         $l = measure($shistory[$spos]);
370                                         $i += $l;
371                                         $spos++ if $i <= $pagel;
372                                 }
373                                 $spos = @shistory if $spos >= @shistory - 1;
374                                 show_screen();
375                         } else {
376                                 beep();
377                         }
378                 } elsif ($r eq KEY_LEFT || $r eq "\002") {
379                         if ($pos > 0) {
380                                 --$pos;
381                         } else {
382                                 beep();
383                         }
384                 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
385                         if ($pos < $lth) {
386                                 ++$pos;
387                         } else {
388                                 beep();
389                         }
390                 } elsif ($r eq KEY_HOME || $r eq "\001") {
391                         $pos = 0;
392                 } elsif ($r eq KEY_END || $r eq "\005") {
393                         $pos = $lth;
394                 } elsif ($r eq KEY_BACKSPACE || $r eq "\010" || $r eq "\x7f") {
395                         if ($pos > 0) {
396                                 my $a = substr($inbuf, 0, $pos-1);
397                                 my $b = substr($inbuf, $pos) if $pos < $lth;
398                                 $b = "" unless $b;
399                                 
400                                 $inbuf = $a . $b;
401                                 --$lth;
402                                 --$pos;
403                         } else {
404                                 beep();
405                         }
406                 } elsif ($r eq KEY_DC || $r eq "\004") {
407                         if ($pos < $lth) {
408                                 my $a = substr($inbuf, 0, $pos);
409                                 my $b = substr($inbuf, $pos+1) if $pos < $lth;
410                                 $b = "" unless $b;
411                                 
412                                 $inbuf = $a . $b;
413                                 --$lth;
414                         } else {
415                                 beep();
416                         }
417                 } elsif ($r eq KEY_RESIZE || $r eq "\0632") {
418                         do_resize();
419                         return;
420                 } elsif (defined $r && is_pctext($r)) {
421                         # move the top screen back to the bottom if you type something
422                         if ($spos < @shistory) {
423                                 $spos = @shistory;
424                                 show_screen();
425                         }
426
427                 #       $r = ($r lt ' ' || $r gt "\x7e") ? sprintf("'%x", ord $r) : $r;
428                         
429                         # insert the character into the keyboard buffer
430                         if ($pos < $lth) {
431                                 my $a = substr($inbuf, 0, $pos);
432                                 my $b = substr($inbuf, $pos);
433                                 $inbuf = $a . $r . $b;
434                         } else {
435                                 $inbuf .= $r;
436                         }
437                         $pos++;
438                         $lth++;
439                 } elsif ($r eq "\014" || $r eq "\022") {
440                         touchwin(curscr, 1);
441                         refresh(curscr);
442                         return;
443                 } elsif ($r eq "\013") {
444                         $inbuf = substr($inbuf, 0, $pos);
445                         $lth = length $inbuf;
446                 } else {
447                         beep();
448                 }
449                 $bot->move(1, 0);
450                 $bot->clrtobot();
451                 $bot->addstr($inbuf);
452         } 
453         $bot->move(1, $pos);
454         $bot->refresh();
455 }
456
457 sub idle_loop
458 {
459         my $t;
460         
461         $t = time;
462         if ($t > $lasttime) {
463                 my ($min)= (gmtime($t))[1];
464                 if ($min != $lastmin) {
465                         show_screen();
466                         $lastmin = $min;
467                 }
468                 $lasttime = $t;
469         }
470         my $ch = $bot->getch();
471         if (@time && tv_interval(\@time, [gettimeofday]) >= 1) {
472                 next;
473         }
474         if (defined $ch) {
475                 if ($ch ne '-1') {
476                         rec_stdin($ch);
477                 }
478         }
479         $top->refresh() if $top->is_wintouched;
480         $bot->refresh();
481 }
482
483 sub on_connect
484 {
485         my $conn = shift;
486         $conn->send_later("A$call|$connsort width=$cols");
487         $conn->send_later("I$call|set/page $maxshist");
488         #$conn->send_later("I$call|set/nobeep");
489 }
490
491 sub on_disconnect
492 {
493         $conn = shift;
494         Mojo::IOLoop->remove($idle);
495         Mojo::IOLoop->stop;
496 }
497
498 #
499 # deal with args
500 #
501
502 $call = uc shift @ARGV if @ARGV;
503 $call = uc $myalias if !$call;
504 my ($scall, $ssid) = split /-/, $call;
505 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;  
506 if ($ssid) {
507         $ssid = 15 if $ssid > 15;
508         $call = "$scall-$ssid";
509 }
510
511 if ($call eq $mycall) {
512         print "You cannot connect as your cluster callsign ($mycall)\n";
513         exit(0);
514 }
515
516 dbginit();
517
518 unless ($DB::VERSION) {
519         $SIG{'INT'} = \&sig_term;
520         $SIG{'TERM'} = \&sig_term;
521 }
522
523 $SIG{'HUP'} = \&sig_term;
524
525 # start up
526 do_resize();
527
528 $SIG{__DIE__} = \&sig_term;
529
530 $Text::Wrap::Columns = $cols;
531
532 my $lastmin = 0;
533
534
535 $conn = IntMsg->connect($clusteraddr, $clusterport, rproc => \&rec_socket);
536 $conn->{on_connect} = \&on_connect;
537 $conn->{on_disconnect} = \&on_disconnect;
538
539 $idle = Mojo::IOLoop->recurring(0.100 => \&idle_loop);
540 Mojo::IOLoop->start;
541
542
543 cease(0);