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