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