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