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