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