]> dxcluster.net Git - spider.git/blob - perl/console.pl
14c945a58987edabecd084078caf58eedca9ed38
[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                         $top->touchwin();
256                         $bot->touchwin();
257                         $scr->touchwin();
258                         $scr->refresh();
259                         $top->refresh();
260                 } elsif ($r eq "\013") {
261                         $inbuf = substr($inbuf, 0, $pos);
262                         $lth = length $inbuf;
263                 } else {
264                         beep();
265                 }
266                 $bot->move(1, 0);
267                 $bot->clrtobot();
268                 $bot->addstr($inbuf);
269         } 
270         $bot->move(1, $pos);
271         $bot->refresh();
272 }
273
274
275 #
276 # deal with args
277 #
278
279 $call = uc shift @ARGV if @ARGV;
280 $call = uc $myalias if !$call;
281
282 if ($call eq $mycall) {
283         print "You cannot connect as your cluster callsign ($mycall)\n";
284         exit(0);
285 }
286
287 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
288 if (! $conn) {
289         if (-r "$data/offline") {
290                 open IN, "$data/offline" or die;
291                 while (<IN>) {
292                         print $_;
293                 }
294                 close IN;
295         } else {
296                 print "Sorry, the cluster $mycall is currently off-line\n";
297         }
298         exit(0);
299 }
300
301
302 $SIG{'INT'} = \&sig_term;
303 $SIG{'TERM'} = \&sig_term;
304 $SIG{'HUP'} = 'IGNORE';
305
306 $scr = new Curses;
307 raw();
308 noecho();
309 $has_colors = has_colors();
310
311 if ($has_colors) {
312         start_color();
313         init_pair(0, $foreground, $background);
314         init_pair(1, COLOR_RED, $background);
315         init_pair(2, COLOR_YELLOW, $background);
316         init_pair(3, COLOR_GREEN, $background);
317         init_pair(4, COLOR_CYAN, $background);
318         init_pair(5, COLOR_BLUE, $background);
319         init_pair(6, COLOR_MAGENTA, $background);
320 }
321
322 $top = $scr->subwin(LINES()-4, COLS, 0, 0);
323 $top->intrflush(0);
324 $top->scrollok(1);
325 $scr->addstr(LINES()-4, 0, '-' x COLS);
326 $bot = $scr->subwin(3, COLS, LINES()-3, 0);
327 $bot->intrflush(0);
328 $bot->scrollok(1);
329 $bot->keypad(1);
330 $bot->move(1,0);
331 $scr->refresh();
332
333 $SIG{__DIE__} = \&sig_term;
334
335 $pages = LINES()-4;
336
337 $conn->send_now("A$call|$connsort");
338 $conn->send_now("I$call|set/page $maxshist");
339 $conn->send_now("I$call|set/nobeep");
340
341 Msg->set_event_handler(\*STDIN, "read" => \&rec_stdin);
342
343 for (;;) {
344         my $t;
345         Msg->event_loop(1, 0.010);
346         $top->refresh() if $top->is_wintouched;
347         $bot->refresh();
348         $t = time;
349         if ($t > $lasttime) {
350                 $lasttime = $t;
351         }
352 }
353
354 exit(0);