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