]> dxcluster.org Git - spider.git/blob - perl/console.pl
moan about and then delete empty message files
[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 1.05;
34
35 use Console;
36
37 #
38 # initialisation
39 #
40
41 $call = "";                     # the callsign being used
42 $conn = 0;                      # the connection object for the cluster
43 $lasttime = time;               # lasttime something happened on the interface
44
45 $connsort = "local";
46 @khistory = ();
47 @shistory = ();
48 $khistpos = 0;
49 $spos = $pos = $lth = 0;
50 $inbuf = "";
51
52 # do the screen initialisation
53 sub do_initscr
54 {
55         $scr = new Curses;
56         raw();
57         noecho();
58         $has_colors = has_colors();
59         
60         if ($has_colors) {
61                 start_color();
62                 init_pair("0", $foreground, $background);
63 #               init_pair(0, $background, $foreground);
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                 init_pair(7, COLOR_RED, COLOR_BLUE);
71                 init_pair(8, COLOR_YELLOW, COLOR_BLUE);
72                 init_pair(9, COLOR_GREEN, COLOR_BLUE);
73                 init_pair(10, COLOR_CYAN, COLOR_BLUE);
74                 init_pair(11, COLOR_BLUE, COLOR_RED);
75                 init_pair(12, COLOR_MAGENTA, COLOR_BLUE);
76                 init_pair(13, COLOR_YELLOW, COLOR_GREEN);
77                 init_pair(14, COLOR_RED, COLOR_GREEN);
78                 $scr->attrset(COLOR_PAIR(0));
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)([^\|]+)\|(.*)$/;
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                 # ******************************************************
228                 # any other sorts that might happen are silently ignored.
229                 # ******************************************************
230                 # ******************************************************
231         } else {
232                 cease(0);
233         }
234         $top->refresh();
235         $lasttime = time; 
236 }
237
238 sub rec_stdin
239 {
240         my ($fh) = @_;
241
242         $r = $bot->getch();
243         
244         #  my $prbuf;
245         #  $prbuf = $buf;
246         #  $prbuf =~ s/\r/\\r/;
247         #  $prbuf =~ s/\n/\\n/;
248         #  print "sys: $r ($prbuf)\n";
249         if (defined $r) {
250                 
251                 if ($r eq KEY_ENTER || $r eq "\n" || $r eq "\r") {
252                         
253                         # save the lines
254                         if ($inbuf) {
255                                 # check for a pling and do a search back for a command
256                                 if ($inbuf =~ /^!/o) {
257                                         my $i;
258                                         $inbuf =~ s/^!//o;
259                                         for ($i = $#khistory; $i >= 0; $i--) {
260                                                 if ($khistory[$i] =~ /^$inbuf/) {
261                                                         $inbuf = $khistory[$i];
262                                                         last;
263                                                 }
264                                         }
265                                         if ($i < 0) {
266                                                 beep();
267                                                 return;
268                                         }
269                                 }
270                                 push @khistory, $inbuf if $inbuf;
271                                 shift @khistory if @khistory > $maxkhist;
272                                 $khistpos = @khistory;
273                                 $bot->move(0,0);
274                                 $bot->clrtoeol();
275                                 $bot->addstr(substr($inbuf, 0, COLS));
276                         }
277
278                         # add it to the monitor window
279                         unless ($spos == @shistory) {
280                                 $spos = @shistory;
281                                 show_screen();
282                         };
283                         addtotop($inbuf) if $inbuf;
284                 
285                         # send it to the cluster
286                         $inbuf = " " unless $inbuf;
287                         $conn->send_later("I$call|$inbuf");
288                         $inbuf = "";
289                         $pos = $lth = 0;
290                 } elsif ($r eq KEY_UP || $r eq "\020") {
291                         if ($khistpos > 0) {
292                                 --$khistpos;
293                                 $inbuf = $khistory[$khistpos];
294                                 $pos = $lth = length $inbuf;
295                         } else {
296                                 beep();
297                         }
298                 } elsif ($r eq KEY_DOWN || $r eq "\016") {
299                         if ($khistpos < @khistory - 1) {
300                                 ++$khistpos;
301                                 $inbuf = $khistory[$khistpos];
302                                 $pos = $lth = length $inbuf;
303                         } else {
304                                 beep();
305                         }
306                 } elsif ($r eq KEY_PPAGE || $r eq "\032") {
307                         if ($spos > 0) {
308                                 my ($i, $l);
309                                 for ($i = 0; $i <= $pagel && $spos >= 0; ) {
310                                         $l = measure($shistory[$spos]);
311                                         $i += $l;
312                                         $spos-- if $i <= $pagel;
313                                 }
314                                 $spos = 0 if $spos < 0;
315                                 show_screen();
316                         } else {
317                                 beep();
318                         }
319                 } elsif ($r eq KEY_NPAGE || $r eq "\026") {
320                         if ($spos < @shistory - 1) {
321                                 my ($i, $l);
322                                 for ($i = 0; $i <= $pagel && $spos <= @shistory; ) {
323                                         $l = measure($shistory[$spos]);
324                                         $i += $l;
325                                         $spos++ if $i <= $pagel;
326                                 }
327                                 $spos = @shistory if $spos >= @shistory - 1;
328                                 show_screen();
329                         } else {
330                                 beep();
331                         }
332                 } elsif ($r eq KEY_LEFT || $r eq "\002") {
333                         if ($pos > 0) {
334                                 --$pos;
335                         } else {
336                                 beep();
337                         }
338                 } elsif ($r eq KEY_RIGHT || $r eq "\006") {
339                         if ($pos < $lth) {
340                                 ++$pos;
341                         } else {
342                                 beep();
343                         }
344                 } elsif ($r eq KEY_HOME || $r eq "\001") {
345                         $pos = 0;
346                 } elsif ($r eq KEY_END || $r eq "\005") {
347                         $pos = $lth;
348                 } elsif ($r eq KEY_BACKSPACE || $r eq "\010") {
349                         if ($pos > 0) {
350                                 my $a = substr($inbuf, 0, $pos-1);
351                                 my $b = substr($inbuf, $pos) if $pos < $lth;
352                                 $b = "" unless $b;
353                                 
354                                 $inbuf = $a . $b;
355                                 --$lth;
356                                 --$pos;
357                         } else {
358                                 beep();
359                         }
360                 } elsif ($r eq KEY_DC || $r eq "\004") {
361                         if ($pos < $lth) {
362                                 my $a = substr($inbuf, 0, $pos);
363                                 my $b = substr($inbuf, $pos+1) if $pos < $lth;
364                                 $b = "" unless $b;
365                                 
366                                 $inbuf = $a . $b;
367                                 --$lth;
368                         } else {
369                                 beep();
370                         }
371                 } elsif (is_pctext($r)) {
372                         # move the top screen back to the bottom if you type something
373                         if ($spos < @shistory) {
374                                 $spos = @shistory;
375                                 show_screen();
376                         }
377                 
378                         # insert the character into the keyboard buffer
379                         if ($pos < $lth) {
380                                 my $a = substr($inbuf, 0, $pos);
381                                 my $b = substr($inbuf, $pos);
382                                 $inbuf = $a . $r . $b;
383                         } else {
384                                 $inbuf .= $r;
385                         }
386                         $pos++;
387                         $lth++;
388                 } elsif ($r eq "\014" || $r eq "\022") {
389                         touchwin($curscr, 1);
390                         refresh($curscr);
391                         return;
392                 } elsif ($r eq "\013") {
393                         $inbuf = substr($inbuf, 0, $pos);
394                         $lth = length $inbuf;
395                 } else {
396                         beep();
397                 }
398                 $bot->move(1, 0);
399                 $bot->clrtobot();
400                 $bot->addstr($inbuf);
401         } 
402         $bot->move(1, $pos);
403         $bot->refresh();
404 }
405
406
407 #
408 # deal with args
409 #
410
411 $call = uc shift @ARGV if @ARGV;
412 $call = uc $myalias if !$call;
413 my ($scall, $ssid) = split /-/, $call;
414 $ssid = undef unless $ssid && $ssid =~ /^\d+$/;  
415 if ($ssid) {
416         $ssid = 15 if $ssid > 15;
417         $call = "$scall-$ssid";
418 }
419
420 if ($call eq $mycall) {
421         print "You cannot connect as your cluster callsign ($mycall)\n";
422         exit(0);
423 }
424
425 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
426 if (! $conn) {
427         if (-r "$data/offline") {
428                 open IN, "$data/offline" or die;
429                 while (<IN>) {
430                         print $_;
431                 }
432                 close IN;
433         } else {
434                 print "Sorry, the cluster $mycall is currently off-line\n";
435         }
436         exit(0);
437 }
438
439
440 $SIG{'INT'} = \&sig_term;
441 $SIG{'TERM'} = \&sig_term;
442 #$SIG{'WINCH'} = \&do_resize;
443 $SIG{'HUP'} = \&sig_term;
444
445 do_initscr();
446
447 $SIG{__DIE__} = \&sig_term;
448
449 $conn->send_later("A$call|$connsort");
450 $conn->send_later("I$call|set/page $maxshist");
451 $conn->send_later("I$call|set/nobeep");
452
453 Msg->set_event_handler(\*STDIN, "read" => \&rec_stdin);
454
455 my $lastmin = 0;
456 for (;;) {
457         my $t;
458         Msg->event_loop(1, 1);
459         $t = time;
460         if ($t > $lasttime) {
461                 my ($min)= (gmtime($t))[1];
462                 if ($min != $lastmin) {
463                         show_screen();
464                         $lastmin = $min;
465                 }
466                 $lasttime = $t;
467         }
468         $top->refresh() if $top->is_wintouched;
469         $bot->refresh();
470 }
471
472 exit(0);