]> scm.dxcluster.org Git - spider.git/blob - perl/console.pl
fixed ^K handling
[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 "\032") {
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 "\026") {
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_END || $r eq "\005") {
220                         $pos = $lth;
221                 } elsif ($r eq KEY_BACKSPACE || $r eq "\010") {
222                         if ($pos > 0) {
223                                 my $a = substr($inbuf, 0, $pos-1);
224                                 my $b = substr($inbuf, $pos) if $pos < $lth;
225                                 $b = "" unless $b;
226                                 
227                                 $inbuf = $a . $b;
228                                 --$lth;
229                                 --$pos;
230                         } else {
231                                 beep();
232                         }
233                 } elsif ($r eq KEY_DC || $r eq "\004") {
234                         if ($pos < $lth) {
235                                 my $a = substr($inbuf, 0, $pos);
236                                 my $b = substr($inbuf, $pos+1) if $pos < $lth;
237                                 $b = "" unless $b;
238                                 
239                                 $inbuf = $a . $b;
240                                 --$lth;
241                         } else {
242                                 beep();
243                         }
244                 } elsif ($r ge ' ' && $r le '~') {
245                         if ($pos < $lth) {
246                                 my $a = substr($inbuf, 0, $pos);
247                                 my $b = substr($inbuf, $pos);
248                                 $inbuf = $a . $r . $b;
249                         } else {
250                                 $inbuf .= $r;
251                         }
252                         $pos++;
253                         $lth++;
254                 } elsif ($r eq "\014" || $r eq "\022") {
255                         $scr->touchwin();
256                         $scr->refresh();
257                 } elsif ($r eq "\013") {
258                         $inbuf = substr($inbuf, 0, $pos);
259                         $lth = length $inbuf;
260                 } else {
261                         beep();
262                 }
263                 $bot->move(1, 0);
264                 $bot->clrtobot();
265                 $bot->addstr($inbuf);
266         } 
267         $bot->move(1, $pos);
268         $bot->refresh();
269 }
270
271
272 #
273 # deal with args
274 #
275
276 $call = uc shift @ARGV if @ARGV;
277 $call = uc $myalias if !$call;
278
279 if ($call eq $mycall) {
280         print "You cannot connect as your cluster callsign ($mycall)\n";
281         exit(0);
282 }
283
284 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
285 if (! $conn) {
286         if (-r "$data/offline") {
287                 open IN, "$data/offline" or die;
288                 while (<IN>) {
289                         print $_;
290                 }
291                 close IN;
292         } else {
293                 print "Sorry, the cluster $mycall is currently off-line\n";
294         }
295         exit(0);
296 }
297
298
299 $SIG{'INT'} = \&sig_term;
300 $SIG{'TERM'} = \&sig_term;
301 $SIG{'HUP'} = 'IGNORE';
302
303 $scr = new Curses;
304 raw();
305 noecho();
306 $has_colors = has_colors();
307
308 if ($has_colors) {
309         start_color();
310         init_pair(0, $foreground, $background);
311         init_pair(1, COLOR_RED, $background);
312         init_pair(2, COLOR_YELLOW, $background);
313         init_pair(3, COLOR_GREEN, $background);
314         init_pair(4, COLOR_CYAN, $background);
315         init_pair(5, COLOR_BLUE, $background);
316         init_pair(6, COLOR_MAGENTA, $background);
317 }
318
319 $top = $scr->subwin(LINES()-4, COLS, 0, 0);
320 $top->intrflush(0);
321 $top->scrollok(1);
322 $scr->addstr(LINES()-4, 0, '-' x COLS);
323 $bot = $scr->subwin(3, COLS, LINES()-3, 0);
324 $bot->intrflush(0);
325 $bot->scrollok(1);
326 $bot->keypad(1);
327 $bot->move(1,0);
328 $scr->refresh();
329
330 $SIG{__DIE__} = \&sig_term;
331
332 $pages = LINES()-4;
333
334 $conn->send_now("A$call|$connsort");
335 $conn->send_now("I$call|set/page $maxshist");
336 $conn->send_now("I$call|set/nobeep");
337
338 Msg->set_event_handler(\*STDIN, "read" => \&rec_stdin);
339
340 for (;;) {
341         my $t;
342         Msg->event_loop(1, 0.010);
343         $top->refresh() if $top->is_wintouched;
344         $bot->refresh();
345         $t = time;
346         if ($t > $lasttime) {
347                 $lasttime = $t;
348         }
349 }
350
351 exit(0);