some detail changes and speed ups
[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 IO::File;
32 use Curses;
33
34 use Carp qw{cluck};
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 # cease communications
54 sub cease
55 {
56         my $sendz = shift;
57         if ($conn && $sendz) {
58                 $conn->send_now("Z$call|bye...\n");
59         }
60         endwin();
61         dbgclose();
62         print @_ if @_;
63         exit(0);        
64 }
65
66 # terminate program from signal
67 sub sig_term
68 {
69         cease(1, @_);
70 }
71
72 # determine the colour of the line
73 sub setattr
74 {
75         if ($has_colors) {
76                 foreach my $ref (@colors) {
77                         if ($_[0] =~ m{$$ref[0]}) {
78                                 $top->attrset($$ref[1]);
79                                 last;
80                         }
81                 }
82         }
83 }
84
85 # display the top screen
86 sub show_screen
87 {
88         if ($spos == @shistory - 1) {
89
90                 # if we really are scrolling thru at the end of the history
91                 my $line = $shistory[-1];
92                 $top->addstr("\n") if $spos > 0;
93                 setattr($line);
94                 $top->addstr($line);
95                 $top->attrset(COLOR_PAIR(0)) if $has_colors;
96                 $spos = @shistory;
97                 
98         } else {
99                 
100                 # anywhere else
101                 my $p = $spos - $pages;
102                 my $i;
103                 $p = 0 if $p < 0;
104                 
105                 $top->move(0, 0);
106                 $top->attrset(COLOR_PAIR(0)) if $has_colors;
107                 $top->clrtobot();
108                 for ($i = 0; $i < $pages && $p < @shistory; $i++, $p++) {
109                         my $line = $shistory[$p];
110                         $line = substr($line, 0, COLS()) if length $line > COLS();
111                         $top->move($i, 0);
112                         setattr($line);
113                         $top->addstr($line);
114                         $top->attrset(COLOR_PAIR(0)) if $has_colors;
115                 }
116                 $spos = $p;
117         }
118         $top->refresh();
119 }
120
121 # handle incoming messages
122 sub rec_socket
123 {
124         my ($con, $msg, $err) = @_;
125         if (defined $err && $err) {
126                 cease(1);
127         }
128         if (defined $msg) {
129                 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
130                 
131                 if ($sort eq 'D') {
132                         push @shistory, $line;
133                         shift @shistory if @shistory > $maxshist;
134 #                       $spos = @shistory if $spos >= @shistory - 1;
135                         show_screen();
136                 } elsif ($sort eq 'Z') { # end, disconnect, go, away .....
137                         cease(0);
138                 }         
139         }
140         $top->refresh();
141         $lasttime = time; 
142 }
143
144 sub rec_stdin
145 {
146         my ($fh) = @_;
147
148         $r = $bot->getch();
149         
150         #  my $prbuf;
151         #  $prbuf = $buf;
152         #  $prbuf =~ s/\r/\\r/;
153         #  $prbuf =~ s/\n/\\n/;
154         #  print "sys: $r ($prbuf)\n";
155         if (defined $r) {
156                 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
157                         
158                         # save the lines
159                         if ($inbuf) {
160                                 push @khistory, $inbuf if $inbuf;
161                                 shift @khistory if @khistory > $maxkhist;
162                                 $khistpos = @khistory;
163                                 $bot->move(0,0);
164                                 $bot->clrtoeol();
165                                 $bot->addstr(substr($inbuf, 0, COLS));
166                         }
167                 
168                         # send it to the cluster
169                         $inbuf = " " unless $inbuf;
170                         $conn->send_later("I$call|$inbuf");
171                         $inbuf = "";
172                         $pos = $lth = 0;
173                 } elsif ($r eq KEY_UP || $r eq "\020") {
174                         if ($khistpos > 0) {
175                                 --$khistpos;
176                                 $inbuf = $khistory[$khistpos];
177                                 $pos = $lth = length $inbuf;
178                         } else {
179                                 beep();
180                         }
181                 } elsif ($r eq KEY_DOWN || $r eq "\016") {
182                         if ($khistpos < @khistory - 1) {
183                                 ++$khistpos;
184                                 $inbuf = $khistory[$khistpos];
185                                 $pos = $lth = length $inbuf;
186                         } else {
187                                 beep();
188                         }
189                 } elsif ($r eq KEY_PPAGE || $r eq "\026") {
190                         if ($spos > 0) {
191                                 $spos -= $pages;
192                                 $spos = 0 if $spos < 0;
193                                 show_screen();
194                         } else {
195                                 beep();
196                         }
197                 } elsif ($r eq KEY_NPAGE || $r eq "\032") {
198                         if ($spos < @shistory - 1) {
199                                 $spos += $pages;
200                                 $spos = @shistory if $spos > @shistory;
201                                 show_screen();
202                         } else {
203                                 beep();
204                         }
205                 } elsif ($r eq KEY_LEFT || $r eq "\002") {
206                         if ($pos > 0) {
207                                 --$pos;
208                         } else {
209                                 beep();
210                         }
211                 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
212                         if ($pos < $lth) {
213                                 ++$pos;
214                         } else {
215                                 beep();
216                         }
217                 } elsif ($r eq KEY_HOME || $r eq "\001") {
218                         $pos = 0;
219                 } elsif ($r eq KEY_BACKSPACE || $r eq "\010") {
220                         if ($pos > 0) {
221                                 my $a = substr($inbuf, 0, $pos-1);
222                                 my $b = substr($inbuf, $pos) if $pos < $lth;
223                                 $b = "" unless $b;
224                                 
225                                 $inbuf = $a . $b;
226                                 --$lth;
227                                 --$pos;
228                         } else {
229                                 beep();
230                         }
231                 } elsif ($r eq KEY_DC || $r eq "\004") {
232                         if ($pos < $lth) {
233                                 my $a = substr($inbuf, 0, $pos);
234                                 my $b = substr($inbuf, $pos+1) if $pos < $lth;
235                                 $b = "" unless $b;
236                                 
237                                 $inbuf = $a . $b;
238                                 --$lth;
239                         } else {
240                                 beep();
241                         }
242                 } elsif ($r ge ' ' && $r le '~') {
243                         if ($pos < $lth) {
244                                 my $a = substr($inbuf, 0, $pos);
245                                 my $b = substr($inbuf, $pos);
246                                 $inbuf = $a . $r . $b;
247                         } else {
248                                 $inbuf .= $r;
249                         }
250                         $pos++;
251                         $lth++;
252                 } elsif ($r eq "\014" || $r eq "\022") {
253                         $scr->touchwin();
254                         $scr->refresh();
255                 } elsif ($r eq "\013") {
256                         $inbuf = "";
257                         $pos = $lth = 0;
258                 } else {
259                         beep();
260                 }
261                 $bot->move(1, 0);
262                 $bot->clrtobot();
263                 $bot->addstr($inbuf);
264         } 
265         $bot->move(1, $pos);
266         $bot->refresh();
267 }
268
269
270 #
271 # deal with args
272 #
273
274 $call = uc shift @ARGV if @ARGV;
275 $call = uc $myalias if !$call;
276
277 if ($call eq $mycall) {
278         print "You cannot connect as your cluster callsign ($mycall)\n";
279         exit(0);
280 }
281
282 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
283 if (! $conn) {
284         if (-r "$data/offline") {
285                 open IN, "$data/offline" or die;
286                 while (<IN>) {
287                         print $_;
288                 }
289                 close IN;
290         } else {
291                 print "Sorry, the cluster $mycall is currently off-line\n";
292         }
293         exit(0);
294 }
295
296
297 $SIG{'INT'} = \&sig_term;
298 $SIG{'TERM'} = \&sig_term;
299 $SIG{'HUP'} = 'IGNORE';
300
301 $scr = new Curses;
302 raw();
303 noecho();
304 $has_colors = has_colors();
305
306 if ($has_colors) {
307         start_color();
308         init_pair(0, $foreground, $background);
309         init_pair(1, COLOR_RED, $background);
310         init_pair(2, COLOR_YELLOW, $background);
311         init_pair(3, COLOR_GREEN, $background);
312         init_pair(4, COLOR_CYAN, $background);
313         init_pair(5, COLOR_BLUE, $background);
314         init_pair(6, COLOR_MAGENTA, $background);
315 }
316
317 $top = $scr->subwin(LINES()-4, COLS, 0, 0);
318 $top->intrflush(0);
319 $top->scrollok(1);
320 $scr->addstr(LINES()-4, 0, '-' x COLS);
321 $bot = $scr->subwin(3, COLS, LINES()-3, 0);
322 $bot->intrflush(0);
323 $bot->scrollok(1);
324 $bot->keypad(1);
325 $bot->move(1,0);
326 $scr->refresh();
327
328 $SIG{__DIE__} = \&sig_term;
329
330 $pages = LINES()-4;
331
332 $conn->send_now("A$call|$connsort");
333 $conn->send_now("I$call|set/page $maxshist");
334 $conn->send_now("I$call|set/nobeep");
335
336 Msg->set_event_handler(\*STDIN, "read" => \&rec_stdin);
337
338 for (;;) {
339         my $t;
340         Msg->event_loop(1, 0.010);
341         $top->refresh() if $top->is_wintouched;
342         $bot->refresh();
343         $t = time;
344         if ($t > $lasttime) {
345                 $lasttime = $t;
346         }
347 }
348
349 exit(0);