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