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