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