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