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