1. I believe I have fixed all the login/logout 'broken pipe' errors
[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)([^\|]+)\|(.*)$/;
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                 # ******************************************************
226                 # any other sorts that might happen are silently ignored.
227                 # ******************************************************
228                 # ******************************************************
229         } else {
230                 cease(0);
231         }
232         $top->refresh();
233         $lasttime = time; 
234 }
235
236 sub rec_stdin
237 {
238         my ($fh) = @_;
239
240         $r = $bot->getch();
241         
242         #  my $prbuf;
243         #  $prbuf = $buf;
244         #  $prbuf =~ s/\r/\\r/;
245         #  $prbuf =~ s/\n/\\n/;
246         #  print "sys: $r ($prbuf)\n";
247         if (defined $r) {
248                 
249                 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
250                         
251                         # save the lines
252                         if ($inbuf) {
253                                 # check for a pling and do a search back for a command
254                                 if ($inbuf =~ /^!/o) {
255                                         my $i;
256                                         $inbuf =~ s/^!//o;
257                                         for ($i = $#khistory; $i >= 0; $i--) {
258                                                 if ($khistory[$i] =~ /^$inbuf/) {
259                                                         $inbuf = $khistory[$i];
260                                                         last;
261                                                 }
262                                         }
263                                         if ($i < 0) {
264                                                 beep();
265                                                 return;
266                                         }
267                                 }
268                                 push @khistory, $inbuf if $inbuf;
269                                 shift @khistory if @khistory > $maxkhist;
270                                 $khistpos = @khistory;
271                                 $bot->move(0,0);
272                                 $bot->clrtoeol();
273                                 $bot->addstr(substr($inbuf, 0, COLS));
274                         }
275
276                         # add it to the monitor window
277                         unless ($spos == @shistory) {
278                                 $spos = @shistory;
279                                 show_screen();
280                         };
281                         addtotop($inbuf) if $inbuf;
282                 
283                         # send it to the cluster
284                         $inbuf = " " unless $inbuf;
285                         $conn->send_later("I$call|$inbuf");
286                         $inbuf = "";
287                         $pos = $lth = 0;
288                 } elsif ($r eq KEY_UP || $r eq "\020") {
289                         if ($khistpos > 0) {
290                                 --$khistpos;
291                                 $inbuf = $khistory[$khistpos];
292                                 $pos = $lth = length $inbuf;
293                         } else {
294                                 beep();
295                         }
296                 } elsif ($r eq KEY_DOWN || $r eq "\016") {
297                         if ($khistpos < @khistory - 1) {
298                                 ++$khistpos;
299                                 $inbuf = $khistory[$khistpos];
300                                 $pos = $lth = length $inbuf;
301                         } else {
302                                 beep();
303                         }
304                 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
305                         if ($spos > 0) {
306                                 my ($i, $l);
307                                 for ($i = 0; $i <= $pagel && $spos >= 0; ) {
308                                         $l = measure($shistory[$spos]);
309                                         $i += $l;
310                                         $spos-- if $i <= $pagel;
311                                 }
312                                 $spos = 0 if $spos < 0;
313                                 show_screen();
314                         } else {
315                                 beep();
316                         }
317                 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
318                         if ($spos < @shistory - 1) {
319                                 my ($i, $l);
320                                 for ($i = 0; $i <= $pagel && $spos <= @shistory; ) {
321                                         $l = measure($shistory[$spos]);
322                                         $i += $l;
323                                         $spos++ if $i <= $pagel;
324                                 }
325                                 $spos = @shistory if $spos >= @shistory - 1;
326                                 show_screen();
327                         } else {
328                                 beep();
329                         }
330                 } elsif ($r eq KEY_LEFT || $r eq "\002") {
331                         if ($pos > 0) {
332                                 --$pos;
333                         } else {
334                                 beep();
335                         }
336                 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
337                         if ($pos < $lth) {
338                                 ++$pos;
339                         } else {
340                                 beep();
341                         }
342                 } elsif ($r eq KEY_HOME || $r eq "\001") {
343                         $pos = 0;
344                 } elsif ($r eq KEY_END || $r eq "\005") {
345                         $pos = $lth;
346                 } elsif ($r eq KEY_BACKSPACE || $r eq "\010") {
347                         if ($pos > 0) {
348                                 my $a = substr($inbuf, 0, $pos-1);
349                                 my $b = substr($inbuf, $pos) if $pos < $lth;
350                                 $b = "" unless $b;
351                                 
352                                 $inbuf = $a . $b;
353                                 --$lth;
354                                 --$pos;
355                         } else {
356                                 beep();
357                         }
358                 } elsif ($r eq KEY_DC || $r eq "\004") {
359                         if ($pos < $lth) {
360                                 my $a = substr($inbuf, 0, $pos);
361                                 my $b = substr($inbuf, $pos+1) if $pos < $lth;
362                                 $b = "" unless $b;
363                                 
364                                 $inbuf = $a . $b;
365                                 --$lth;
366                         } else {
367                                 beep();
368                         }
369                 } elsif ($r ge ' ' && $r le '~') {
370                         # move the top screen back to the bottom if you type something
371                         if ($spos < @shistory) {
372                                 $spos = @shistory;
373                                 show_screen();
374                         }
375                 
376                         # insert the character into the keyboard buffer
377                         if ($pos < $lth) {
378                                 my $a = substr($inbuf, 0, $pos);
379                                 my $b = substr($inbuf, $pos);
380                                 $inbuf = $a . $r . $b;
381                         } else {
382                                 $inbuf .= $r;
383                         }
384                         $pos++;
385                         $lth++;
386                 } elsif ($r eq "\014" || $r eq "\022") {
387                         #do_resize();
388                         return;
389                 } elsif ($r eq "\013") {
390                         $inbuf = substr($inbuf, 0, $pos);
391                         $lth = length $inbuf;
392                 } else {
393                         beep();
394                 }
395                 $bot->move(1, 0);
396                 $bot->clrtobot();
397                 $bot->addstr($inbuf);
398         } 
399         $bot->move(1, $pos);
400         $bot->refresh();
401 }
402
403
404 #
405 # deal with args
406 #
407
408 $call = uc shift @ARGV if @ARGV;
409 $call = uc $myalias if !$call;
410 my ($scall, $ssid) = split /-/, $call;
411 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;  
412 if ($ssid) {
413         $ssid = 15 if $ssid > 15;
414         $call = "$scall-$ssid";
415 }
416
417 if ($call eq $mycall) {
418         print "You cannot connect as your cluster callsign ($mycall)\n";
419         exit(0);
420 }
421
422 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
423 if (! $conn) {
424         if (-r "$data/offline") {
425                 open IN, "$data/offline" or die;
426                 while (<IN>) {
427                         print $_;
428                 }
429                 close IN;
430         } else {
431                 print "Sorry, the cluster $mycall is currently off-line\n";
432         }
433         exit(0);
434 }
435
436
437 $SIG{'INT'} = \&sig_term;
438 $SIG{'TERM'} = \&sig_term;
439 #$SIG{'WINCH'} = \&do_resize;
440 $SIG{'HUP'} = \&sig_term;
441
442 do_initscr();
443
444 $SIG{__DIE__} = \&sig_term;
445
446 $conn->send_later("A$call|$connsort");
447 $conn->send_later("I$call|set/page $maxshist");
448 $conn->send_later("I$call|set/nobeep");
449
450 Msg->set_event_handler(\*STDIN, "read" => \&rec_stdin);
451
452 my $lastmin = 0;
453 for (;;) {
454         my $t;
455         Msg->event_loop(1, 1);
456         $t = time;
457         if ($t > $lasttime) {
458                 my ($min)= (gmtime($t))[1];
459                 if ($min != $lastmin) {
460                         show_screen();
461                         $lastmin = $min;
462                 }
463                 $lasttime = $t;
464         }
465         $top->refresh() if $top->is_wintouched;
466         $bot->refresh();
467 }
468
469 exit(0);