made all protocol except PC29 reject %xx characters
[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 # add a line to the end of the top screen
122 sub addtotop
123 {
124         my $inbuf = shift;
125         push @shistory, $inbuf;
126         shift @shistory if @shistory > $maxshist;
127         show_screen();
128 }
129
130 # handle incoming messages
131 sub rec_socket
132 {
133         my ($con, $msg, $err) = @_;
134         if (defined $err && $err) {
135                 cease(1);
136         }
137         if (defined $msg) {
138                 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
139                 
140                 if ($sort eq 'D') {
141                         addtotop($line);
142                 } elsif ($sort eq 'Z') { # end, disconnect, go, away .....
143                         cease(0);
144                 }         
145         }
146         $top->refresh();
147         $lasttime = time; 
148 }
149
150 sub rec_stdin
151 {
152         my ($fh) = @_;
153
154         $r = $bot->getch();
155         
156         #  my $prbuf;
157         #  $prbuf = $buf;
158         #  $prbuf =~ s/\r/\\r/;
159         #  $prbuf =~ s/\n/\\n/;
160         #  print "sys: $r ($prbuf)\n";
161         if (defined $r) {
162                 
163                 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
164                         
165                         # save the lines
166                         if ($inbuf) {
167                                 # check for a pling and do a search back for a command
168                                 if ($inbuf =~ /^!/o) {
169                                         my $i;
170                                         $inbuf =~ s/^!//o;
171                                         for ($i = $#khistory; $i >= 0; $i--) {
172                                                 if ($khistory[$i] =~ /^$inbuf/) {
173                                                         $inbuf = $khistory[$i];
174                                                         last;
175                                                 }
176                                         }
177                                         if ($i < 0) {
178                                                 beep();
179                                                 return;
180                                         }
181                                 }
182                                 push @khistory, $inbuf if $inbuf;
183                                 shift @khistory if @khistory > $maxkhist;
184                                 $khistpos = @khistory;
185                                 $bot->move(0,0);
186                                 $bot->clrtoeol();
187                                 $bot->addstr(substr($inbuf, 0, COLS));
188                         }
189
190                         # add it to the monitor window
191                         addtotop($inbuf) if $inbuf;
192                 
193                         # send it to the cluster
194                         $inbuf = " " unless $inbuf;
195                         $conn->send_later("I$call|$inbuf");
196                         $inbuf = "";
197                         $pos = $lth = 0;
198                 } elsif ($r eq KEY_UP || $r eq "\020") {
199                         if ($khistpos > 0) {
200                                 --$khistpos;
201                                 $inbuf = $khistory[$khistpos];
202                                 $pos = $lth = length $inbuf;
203                         } else {
204                                 beep();
205                         }
206                 } elsif ($r eq KEY_DOWN || $r eq "\016") {
207                         if ($khistpos < @khistory - 1) {
208                                 ++$khistpos;
209                                 $inbuf = $khistory[$khistpos];
210                                 $pos = $lth = length $inbuf;
211                         } else {
212                                 beep();
213                         }
214                 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
215                         if ($spos > 0) {
216                                 $spos -= $pages;
217                                 $spos = 0 if $spos < 0;
218                                 show_screen();
219                         } else {
220                                 beep();
221                         }
222                 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
223                         if ($spos < @shistory - 1) {
224                                 $spos += $pages;
225                                 $spos = @shistory if $spos > @shistory;
226                                 show_screen();
227                         } else {
228                                 beep();
229                         }
230                 } elsif ($r eq KEY_LEFT || $r eq "\002") {
231                         if ($pos > 0) {
232                                 --$pos;
233                         } else {
234                                 beep();
235                         }
236                 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
237                         if ($pos < $lth) {
238                                 ++$pos;
239                         } else {
240                                 beep();
241                         }
242                 } elsif ($r eq KEY_HOME || $r eq "\001") {
243                         $pos = 0;
244                 } elsif ($r eq KEY_END || $r eq "\005") {
245                         $pos = $lth;
246                 } elsif ($r eq KEY_BACKSPACE || $r eq "\010") {
247                         if ($pos > 0) {
248                                 my $a = substr($inbuf, 0, $pos-1);
249                                 my $b = substr($inbuf, $pos) if $pos < $lth;
250                                 $b = "" unless $b;
251                                 
252                                 $inbuf = $a . $b;
253                                 --$lth;
254                                 --$pos;
255                         } else {
256                                 beep();
257                         }
258                 } elsif ($r eq KEY_DC || $r eq "\004") {
259                         if ($pos < $lth) {
260                                 my $a = substr($inbuf, 0, $pos);
261                                 my $b = substr($inbuf, $pos+1) if $pos < $lth;
262                                 $b = "" unless $b;
263                                 
264                                 $inbuf = $a . $b;
265                                 --$lth;
266                         } else {
267                                 beep();
268                         }
269                 } elsif ($r ge ' ' && $r le '~') {
270                         # move the top screen back to the bottom if you type something
271                         if ($spos < @shistory - 1) {
272                                 $spos = @shistory;
273                                 show_screen();
274                         }
275                 
276                         # insert the character into the keyboard buffer
277                         if ($pos < $lth) {
278                                 my $a = substr($inbuf, 0, $pos);
279                                 my $b = substr($inbuf, $pos);
280                                 $inbuf = $a . $r . $b;
281                         } else {
282                                 $inbuf .= $r;
283                         }
284                         $pos++;
285                         $lth++;
286                 } elsif ($r eq "\014" || $r eq "\022") {
287 #                       curscr()->refresh();
288                         return;
289                 } elsif ($r eq "\013") {
290                         $inbuf = substr($inbuf, 0, $pos);
291                         $lth = length $inbuf;
292                 } else {
293                         beep();
294                 }
295                 $bot->move(1, 0);
296                 $bot->clrtobot();
297                 $bot->addstr($inbuf);
298         } 
299         $bot->move(1, $pos);
300         $bot->refresh();
301 }
302
303
304 #
305 # deal with args
306 #
307
308 $call = uc shift @ARGV if @ARGV;
309 $call = uc $myalias if !$call;
310
311 if ($call eq $mycall) {
312         print "You cannot connect as your cluster callsign ($mycall)\n";
313         exit(0);
314 }
315
316 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
317 if (! $conn) {
318         if (-r "$data/offline") {
319                 open IN, "$data/offline" or die;
320                 while (<IN>) {
321                         print $_;
322                 }
323                 close IN;
324         } else {
325                 print "Sorry, the cluster $mycall is currently off-line\n";
326         }
327         exit(0);
328 }
329
330
331 $SIG{'INT'} = \&sig_term;
332 $SIG{'TERM'} = \&sig_term;
333 $SIG{'HUP'} = 'IGNORE';
334
335 $scr = new Curses;
336 raw();
337 noecho();
338 $has_colors = has_colors();
339
340 if ($has_colors) {
341         start_color();
342         init_pair(0, $foreground, $background);
343         init_pair(1, COLOR_RED, $background);
344         init_pair(2, COLOR_YELLOW, $background);
345         init_pair(3, COLOR_GREEN, $background);
346         init_pair(4, COLOR_CYAN, $background);
347         init_pair(5, COLOR_BLUE, $background);
348         init_pair(6, COLOR_MAGENTA, $background);
349 }
350
351 $top = $scr->subwin(LINES()-4, COLS, 0, 0);
352 $top->intrflush(0);
353 $top->scrollok(1);
354 $scr->addstr(LINES()-4, 0, '-' x COLS);
355 $bot = $scr->subwin(3, COLS, LINES()-3, 0);
356 $bot->intrflush(0);
357 $bot->scrollok(1);
358 $bot->keypad(1);
359 $bot->move(1,0);
360 $scr->refresh();
361
362 $SIG{__DIE__} = \&sig_term;
363
364 $pages = LINES()-4;
365
366 $conn->send_now("A$call|$connsort");
367 $conn->send_now("I$call|set/page $maxshist");
368 $conn->send_now("I$call|set/nobeep");
369
370 Msg->set_event_handler(\*STDIN, "read" => \&rec_stdin);
371
372 for (;;) {
373         my $t;
374         Msg->event_loop(1, 0.010);
375         $top->refresh() if $top->is_wintouched;
376         $bot->refresh();
377         $t = time;
378         if ($t > $lasttime) {
379                 $lasttime = $t;
380         }
381 }
382
383 exit(0);