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