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