fix uninit line in console
[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 # do the screen initialisation
54 sub do_initscr
55 {
56         $scr = new Curses;
57         raw();
58         noecho();
59         $has_colors = has_colors();
60         
61         if ($has_colors) {
62                 start_color();
63                 init_pair(0, $foreground, $background);
64                 init_pair(1, COLOR_RED, $background);
65                 init_pair(2, COLOR_YELLOW, $background);
66                 init_pair(3, COLOR_GREEN, $background);
67                 init_pair(4, COLOR_CYAN, $background);
68                 init_pair(5, COLOR_BLUE, $background);
69                 init_pair(6, COLOR_MAGENTA, $background);
70         }
71         
72         $top = $scr->subwin(LINES()-4, COLS, 0, 0);
73         $top->intrflush(0);
74         $top->scrollok(1);
75         $scr->addstr(LINES()-4, 0, '-' x COLS);
76         $bot = $scr->subwin(3, COLS, LINES()-3, 0);
77         $bot->intrflush(0);
78         $bot->scrollok(1);
79         $bot->keypad(1);
80         $bot->move(1,0);
81         $scr->refresh();
82         
83         $pagel = LINES()-4;
84         $mycallcolor = COLOR_PAIR(1) unless $mycallcolor;
85 }
86
87 sub do_resize
88 {
89         undef $scr;
90         do_initscr();
91 }
92
93 # cease communications
94 sub cease
95 {
96         my $sendz = shift;
97         if ($conn && $sendz) {
98                 $conn->send_now("Z$call|bye...\n");
99         }
100         endwin();
101         dbgclose();
102         print @_ if @_;
103         exit(0);        
104 }
105
106 # terminate program from signal
107 sub sig_term
108 {
109         cease(1, @_);
110 }
111
112 # determine the colour of the line
113 sub setattr
114 {
115         if ($has_colors) {
116                 foreach my $ref (@colors) {
117                         if ($_[0] =~ m{$$ref[0]}) {
118                                 $top->attrset($$ref[1]);
119                                 last;
120                         }
121                 }
122         }
123 }
124
125 # measure the no of screen lines a line will take
126 sub measure
127 {
128         my $line = shift;
129         return 0 unless $line;
130
131         my $l = length $line;
132         my $lines = int ($l / COLS());
133         $lines++ if $l / COLS() > $lines;
134         return $lines;
135 }
136
137 # display the top screen
138 sub show_screen
139 {
140         if ($spos >= @shistory - 1) {
141
142                 # if we really are scrolling thru at the end of the history
143                 $spos = @shistory - 1 if $spos >= @shistory;
144                 my $line = $shistory[$spos];
145                 $top->addstr("\n") if $spos > 0;
146                 setattr($line);
147                 $top->addstr($line);
148                 $top->attrset(COLOR_PAIR(0)) if $has_colors;
149                 $spos = @shistory;
150                 
151         } else {
152                 
153                 # anywhere else
154                 my ($i, $l);
155                 my $p = $spos-1;
156                 for ($i = 0; $i < $pagel && $p >= 0; ) {
157                         $l = measure($shistory[$p]);
158                         $i += $l;
159                         $p-- if $i < $pagel;
160                 }
161                 $p = 0 if $p < 0;
162                 
163                 $top->move(0, 0);
164                 $top->attrset(COLOR_PAIR(0)) if $has_colors;
165                 $top->clrtobot();
166                 for ($i = 0; $i < $pagel && $p < @shistory; $p++) {
167                         my $line = $shistory[$p];
168                         my $lines = measure($line);
169                         last if $i + $lines > $pagel;
170                         setattr($line);
171                         $top->addstr($i, 0, $line);
172                         $top->attrset(COLOR_PAIR(0)) if $has_colors;
173                         $i += $lines;
174                 }
175                 $spos = $p;
176                 $spos = @shistory if $spos > @shistory;
177         }
178     my $shl = @shistory;
179         my $add = "-$spos-$shl";
180     $scr->addstr(LINES()-4, 0, '-' x (COLS() - (length($call) + length($add))));
181         $scr->attrset($mycallcolor) if $has_colors;
182         $scr->addstr("$call");
183         $scr->attrset(COLOR_PAIR(0)) if $has_colors;
184     $scr->addstr($add);
185         $scr->refresh();
186 #       $top->refresh();
187 }
188
189 # add a line to the end of the top screen
190 sub addtotop
191 {
192         my $inbuf = shift;
193         push @shistory, $inbuf;
194         shift @shistory if @shistory > $maxshist;
195         show_screen();
196 }
197
198 # handle incoming messages
199 sub rec_socket
200 {
201         my ($con, $msg, $err) = @_;
202         if (defined $err && $err) {
203                 cease(1);
204         }
205         if (defined $msg) {
206                 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
207                 
208                 if ($sort && $sort eq 'D') {
209                         addtotop($line);
210                 } elsif ($sort && $sort eq 'Z') { # end, disconnect, go, away .....
211                         cease(0);
212                 }         
213         }
214         $top->refresh();
215         $lasttime = time; 
216 }
217
218 sub rec_stdin
219 {
220         my ($fh) = @_;
221
222         $r = $bot->getch();
223         
224         #  my $prbuf;
225         #  $prbuf = $buf;
226         #  $prbuf =~ s/\r/\\r/;
227         #  $prbuf =~ s/\n/\\n/;
228         #  print "sys: $r ($prbuf)\n";
229         if (defined $r) {
230                 
231                 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
232                         
233                         # save the lines
234                         if ($inbuf) {
235                                 # check for a pling and do a search back for a command
236                                 if ($inbuf =~ /^!/o) {
237                                         my $i;
238                                         $inbuf =~ s/^!//o;
239                                         for ($i = $#khistory; $i >= 0; $i--) {
240                                                 if ($khistory[$i] =~ /^$inbuf/) {
241                                                         $inbuf = $khistory[$i];
242                                                         last;
243                                                 }
244                                         }
245                                         if ($i < 0) {
246                                                 beep();
247                                                 return;
248                                         }
249                                 }
250                                 push @khistory, $inbuf if $inbuf;
251                                 shift @khistory if @khistory > $maxkhist;
252                                 $khistpos = @khistory;
253                                 $bot->move(0,0);
254                                 $bot->clrtoeol();
255                                 $bot->addstr(substr($inbuf, 0, COLS));
256                         }
257
258                         # add it to the monitor window
259                         unless ($spos == @shistory) {
260                                 $spos = @shistory;
261                                 show_screen();
262                         };
263                         addtotop($inbuf) if $inbuf;
264                 
265                         # send it to the cluster
266                         $inbuf = " " unless $inbuf;
267                         $conn->send_later("I$call|$inbuf");
268                         $inbuf = "";
269                         $pos = $lth = 0;
270                 } elsif ($r eq KEY_UP || $r eq "\020") {
271                         if ($khistpos > 0) {
272                                 --$khistpos;
273                                 $inbuf = $khistory[$khistpos];
274                                 $pos = $lth = length $inbuf;
275                         } else {
276                                 beep();
277                         }
278                 } elsif ($r eq KEY_DOWN || $r eq "\016") {
279                         if ($khistpos < @khistory - 1) {
280                                 ++$khistpos;
281                                 $inbuf = $khistory[$khistpos];
282                                 $pos = $lth = length $inbuf;
283                         } else {
284                                 beep();
285                         }
286                 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
287                         if ($spos > 0) {
288                                 my ($i, $l);
289                                 for ($i = 0; $i <= $pagel && $spos >= 0; ) {
290                                         $l = measure($shistory[$spos]);
291                                         $i += $l;
292                                         $spos-- if $i <= $pagel;
293                                 }
294                                 $spos = 0 if $spos < 0;
295                                 show_screen();
296                         } else {
297                                 beep();
298                         }
299                 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
300                         if ($spos < @shistory - 1) {
301                                 my ($i, $l);
302                                 for ($i = 0; $i <= $pagel && $spos <= @shistory; ) {
303                                         $l = measure($shistory[$spos]);
304                                         $i += $l;
305                                         $spos++ if $i <= $pagel;
306                                 }
307                                 $spos = @shistory if $spos >= @shistory - 1;
308                                 show_screen();
309                         } else {
310                                 beep();
311                         }
312                 } elsif ($r eq KEY_LEFT || $r eq "\002") {
313                         if ($pos > 0) {
314                                 --$pos;
315                         } else {
316                                 beep();
317                         }
318                 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
319                         if ($pos < $lth) {
320                                 ++$pos;
321                         } else {
322                                 beep();
323                         }
324                 } elsif ($r eq KEY_HOME || $r eq "\001") {
325                         $pos = 0;
326                 } elsif ($r eq KEY_END || $r eq "\005") {
327                         $pos = $lth;
328                 } elsif ($r eq KEY_BACKSPACE || $r eq "\010") {
329                         if ($pos > 0) {
330                                 my $a = substr($inbuf, 0, $pos-1);
331                                 my $b = substr($inbuf, $pos) if $pos < $lth;
332                                 $b = "" unless $b;
333                                 
334                                 $inbuf = $a . $b;
335                                 --$lth;
336                                 --$pos;
337                         } else {
338                                 beep();
339                         }
340                 } elsif ($r eq KEY_DC || $r eq "\004") {
341                         if ($pos < $lth) {
342                                 my $a = substr($inbuf, 0, $pos);
343                                 my $b = substr($inbuf, $pos+1) if $pos < $lth;
344                                 $b = "" unless $b;
345                                 
346                                 $inbuf = $a . $b;
347                                 --$lth;
348                         } else {
349                                 beep();
350                         }
351                 } elsif ($r ge ' ' && $r le '~') {
352                         # move the top screen back to the bottom if you type something
353                         if ($spos < @shistory) {
354                                 $spos = @shistory;
355                                 show_screen();
356                         }
357                 
358                         # insert the character into the keyboard buffer
359                         if ($pos < $lth) {
360                                 my $a = substr($inbuf, 0, $pos);
361                                 my $b = substr($inbuf, $pos);
362                                 $inbuf = $a . $r . $b;
363                         } else {
364                                 $inbuf .= $r;
365                         }
366                         $pos++;
367                         $lth++;
368                 } elsif ($r eq "\014" || $r eq "\022") {
369                         #do_resize();
370                         return;
371                 } elsif ($r eq "\013") {
372                         $inbuf = substr($inbuf, 0, $pos);
373                         $lth = length $inbuf;
374                 } else {
375                         beep();
376                 }
377                 $bot->move(1, 0);
378                 $bot->clrtobot();
379                 $bot->addstr($inbuf);
380         } 
381         $bot->move(1, $pos);
382         $bot->refresh();
383 }
384
385
386 #
387 # deal with args
388 #
389
390 $call = uc shift @ARGV if @ARGV;
391 $call = uc $myalias if !$call;
392
393 if ($call eq $mycall) {
394         print "You cannot connect as your cluster callsign ($mycall)\n";
395         exit(0);
396 }
397
398 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
399 if (! $conn) {
400         if (-r "$data/offline") {
401                 open IN, "$data/offline" or die;
402                 while (<IN>) {
403                         print $_;
404                 }
405                 close IN;
406         } else {
407                 print "Sorry, the cluster $mycall is currently off-line\n";
408         }
409         exit(0);
410 }
411
412
413 $SIG{'INT'} = \&sig_term;
414 $SIG{'TERM'} = \&sig_term;
415 #$SIG{'WINCH'} = \&do_resize;
416 $SIG{'HUP'} = 'IGNORE';
417
418 do_initscr();
419
420 $SIG{__DIE__} = \&sig_term;
421
422 $conn->send_now("A$call|$connsort");
423 $conn->send_now("I$call|set/page $maxshist");
424 $conn->send_now("I$call|set/nobeep");
425
426 Msg->set_event_handler(\*STDIN, "read" => \&rec_stdin);
427
428 for (;;) {
429         my $t;
430         Msg->event_loop(1, 1);
431         $top->refresh() if $top->is_wintouched;
432         $bot->refresh();
433         $t = time;
434         if ($t > $lasttime) {
435                 $lasttime = $t;
436         }
437 }
438
439 exit(0);