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