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