remove warnings from $BRANCH lines for 5.8.0
[spider.git] / perl / ExtMsg.pm
1 #
2 # This class is the internal subclass that deals with the external port
3 # communications for Msg.pm
4 #
5 # This is where the cluster handles direct connections coming both in
6 # and out
7 #
8 # $Id$
9 #
10 # Copyright (c) 2001 - Dirk Koopman G1TLH
11 #
12
13 package ExtMsg;
14
15 use strict;
16 use Msg;
17 use DXVars;
18 use DXUtil;
19 use DXDebug;
20 use IO::File;
21 use IO::Socket;
22 use IPC::Open3;
23
24 use vars qw($VERSION $BRANCH);
25 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
26 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/  || (0,0));
27 $main::build += $VERSION;
28 $main::branch += $BRANCH;
29
30 use vars qw(@ISA $deftimeout);
31
32 @ISA = qw(Msg);
33 $deftimeout = 60;
34
35 sub enqueue
36 {
37         my ($conn, $msg) = @_;
38         unless ($msg =~ /^[ABZ]/) {
39                 if ($msg =~ /^E[-\w]+\|([01])/ && $conn->{csort} eq 'telnet') {
40                         $conn->{echo} = $1;
41                         if ($1) {
42 #                               $conn->send_raw("\xFF\xFC\x01");
43                         } else {
44 #                               $conn->send_raw("\xFF\xFB\x01");
45                         }
46                 } else {
47                         $msg =~ s/^[-\w]+\|//;
48                         push (@{$conn->{outqueue}}, $msg . $conn->{lineend});
49                 }
50         }
51 }
52
53 sub send_raw
54 {
55         my ($conn, $msg) = @_;
56     my $sock = $conn->{sock};
57     return unless defined($sock);
58         push (@{$conn->{outqueue}}, $msg);
59         dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
60     Msg::set_event_handler ($sock, "write" => sub {$conn->_send(0)});
61 }
62
63 sub echo
64 {
65         my $conn = shift;
66         $conn->{echo} = shift;
67 }
68
69 sub dequeue
70 {
71         my $conn = shift;
72         my $msg;
73
74         if ($conn->{csort} eq 'ax25' && exists $conn->{msg}) {
75                 $conn->{msg} =~ s/\cM/\cJ/g;
76         }
77         if ($conn->{state} eq 'WC') {
78                 if (exists $conn->{cmd}) {
79                         if (@{$conn->{cmd}}) {
80                                 dbg("connect $conn->{cnum}: $conn->{msg}") if isdbg('connect');
81                                 $conn->_docmd($conn->{msg});
82                         } 
83                 }
84                 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
85                         $conn->to_connected($conn->{call}, 'O', $conn->{csort});
86                 }
87         } elsif ($conn->{msg} =~ /\cJ/) {
88                 my @lines =  $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
89                 if ($conn->{msg} =~ /\cJ$/) {
90                         delete $conn->{msg};
91                 } else {
92                         $conn->{msg} =~ s/([^\cM\cJ]*)\cM?\cJ//g;
93                 }
94                 while (defined ($msg = shift @lines)) {
95                         dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
96                 
97                         $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
98 #                       $msg =~ s/[\x00-\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g;         # immutable CSI sequence + control characters
99                         
100                         if ($conn->{state} eq 'C') {
101                                 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
102                         } elsif ($conn->{state} eq 'WL' ) {
103                                 $msg = uc $msg;
104                                 if (is_callsign($msg) && $msg !~ m|/| ) {
105                                         my $sort = $conn->{csort};
106                                         $sort = 'local' if $conn->{peerhost} eq "127.0.0.1";
107                                         my $uref;
108                                         if ($main::passwdreq || ($uref = DXUser->get_current($msg)) && $uref->passwd ) {
109                                                 $conn->conns($msg);
110                                                 $conn->{state} = 'WP';
111                                                 $conn->{decho} = $conn->{echo};
112                                                 $conn->{echo} = 0;
113                                                 $conn->send_raw('password: ');
114                                         } else {
115                                                 $conn->to_connected($msg, 'A', $sort);
116                                         }
117                                 } else {
118                                         $conn->send_now("Sorry $msg is an invalid callsign");
119                                         $conn->disconnect;
120                                 }
121                         } elsif ($conn->{state} eq 'WP' ) {
122                                 my $uref = DXUser->get_current($conn->{call});
123                                 $msg =~ s/[\r\n]+$//;
124                                 if ($uref && $msg eq $uref->passwd) {
125                                         my $sort = $conn->{csort};
126                                         $conn->{echo} = $conn->{decho};
127                                         delete $conn->{decho};
128                                         $sort = 'local' if $conn->{peerhost} eq "127.0.0.1";
129                                         $conn->{usedpasswd} = 1;
130                                         $conn->to_connected($conn->{call}, 'A', $sort);
131                                 } else {
132                                         $conn->send_now("Sorry");
133                                         $conn->disconnect;
134                                 }
135                         } elsif ($conn->{state} eq 'WC') {
136                                 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
137                                         $conn->_docmd($msg);
138                                         if ($conn->{state} eq 'WC' && exists $conn->{cmd} &&  @{$conn->{cmd}} == 0) {
139                                                 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
140                                         }
141                                 }
142                         }
143                 }
144         }
145 }
146
147 sub to_connected
148 {
149         my ($conn, $call, $dir, $sort) = @_;
150         $conn->{state} = 'C';
151         $conn->conns($call);
152         delete $conn->{cmd};
153         $conn->{timeout}->del if $conn->{timeout};
154         delete $conn->{timeout};
155         $conn->nolinger;
156         &{$conn->{rproc}}($conn, "$dir$call|$sort");
157         $conn->_send_file("$main::data/connected") unless $conn->{outgoing};
158 }
159
160 sub new_client {
161         my $server_conn = shift;
162     my $sock = $server_conn->{sock}->accept();
163         if ($sock) {
164                 my $conn = $server_conn->new($server_conn->{rproc});
165                 $conn->{sock} = $sock;
166                 $conn->nolinger;
167                 Msg::blocking($sock, 0);
168                 $conn->{blocking} = 0;
169                 eval {$conn->{peerhost} = $sock->peerhost};
170                 if ($@) {
171                         dbg($@) if isdbg('connll');
172                         $conn->disconnect;
173                 } else {
174                         eval {$conn->{peerport} = $sock->peerport};
175                         $conn->{peerport} = 0 if $@;
176                         my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost}, $conn->{peerport});
177                         dbg("accept $conn->{cnum} from $conn->{peerhost} $conn->{peerport}") if isdbg('connll');
178                         if ($eproc) {
179                                 $conn->{eproc} = $eproc;
180                                 Msg::set_event_handler ($sock, "error" => $eproc);
181                         }
182                         if ($rproc) {
183                                 $conn->{rproc} = $rproc;
184                                 my $callback = sub {$conn->_rcv};
185                                 Msg::set_event_handler ($sock, "read" => $callback);
186                                 # send login prompt
187                                 $conn->{state} = 'WL';
188                                 #               $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
189                                 #               $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
190                                 #               $conn->send_raw("\xFF\xFC\x01");
191                                 $conn->_send_file("$main::data/issue");
192                                 $conn->send_raw("login: ");
193                                 $conn->_dotimeout(60);
194                                 $conn->{echo} = 1;
195                         } else { 
196                                 &{$conn->{eproc}}() if $conn->{eproc};
197                                 $conn->disconnect();
198                         }
199                 }
200         } else {
201                 dbg("ExtMsg: error on accept ($!)") if isdbg('err');
202         }
203 }
204
205 sub start_connect
206 {
207         my $call = shift;
208         my $fn = shift;
209         my $conn = ExtMsg->new(\&main::new_channel); 
210         $conn->{outgoing} = 1;
211         $conn->conns($call);
212         
213         my $f = new IO::File $fn;
214         push @{$conn->{cmd}}, <$f>;
215         $f->close;
216         $conn->{state} = 'WC';
217         $conn->_dotimeout($deftimeout);
218         $conn->_docmd;
219 }
220
221 sub _docmd
222 {
223         my $conn = shift;
224         my $msg = shift;
225         my $cmd;
226
227         while ($cmd = shift @{$conn->{cmd}}) {
228                 chomp $cmd;
229                 next if $cmd =~ /^\s*\#/o;
230                 next if $cmd =~ /^\s*$/o;
231                 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
232                 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
233                 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
234                 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
235                         unless ($conn->_doconnect($1, $2)) {
236                                 $conn->disconnect;
237                                 @{$conn->{cmd}} = [];    # empty any further commands
238                                 last;
239                         }  
240                 }
241                 if ($cmd =~ /^\s*\'([^\']*)\'\s+\'([^\']*)\'/) {
242                         $conn->_dochat($cmd, $msg, $1, $2);
243                         last;
244                 }
245                 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
246                         $conn->_doclient($1);
247                         last;
248                 }
249                 last if $conn->{state} eq 'E';
250         }
251 }
252
253 sub _doconnect
254 {
255         my ($conn, $sort, $line) = @_;
256         my $r;
257
258         $sort = lc $sort;
259         dbg("CONNECT $conn->{cnum} sort: $sort command: $line") if isdbg('connect');
260         if ($sort eq 'telnet') {
261                 # this is a straight network connect
262                 my ($host, $port) = split /\s+/, $line;
263                 $port = 23 if !$port;
264                 $r = $conn->connect($host, $port);
265                 if ($r) {
266                         dbg("Connected $conn->{cnum} to $host $port") if isdbg('connect');
267                 } else {
268                         dbg("***Connect $conn->{cnum} Failed to $host $port $!") if isdbg('connect');
269                 }
270         } elsif ($sort eq 'agw') {
271                 # turn it into an AGW object
272                 bless $conn, 'AGWMsg';
273                 $r = $conn->connect($line);
274         } elsif ($sort eq 'ax25' || $sort eq 'prog') {
275                 local $^F = 10000;              # make sure it ain't closed on exec
276                 my ($a, $b) = IO::Socket->socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC);
277                 if ($a && $b) {
278                         $r = 1;
279                         $a->autoflush(1);
280                         $b->autoflush(1);
281                         my $pid = fork;
282                         if (defined $pid) {
283                                 if ($pid) {
284                                         close $b;
285                                         $conn->{sock} = $a;
286                                         $conn->{csort} = $sort;
287                                         $conn->{lineend} = "\cM" if $sort eq 'ax25';
288                                         $conn->{pid} = $pid;
289                                         if ($conn->{rproc}) {
290                                                 my $callback = sub {$conn->_rcv};
291                                                 Msg::set_event_handler ($a, read => $callback);
292                                         }
293                                         dbg("connect $conn->{cnum}: started pid: $conn->{pid} as $line") if isdbg('connect');
294                                 } else {
295                                         $^W = 0;
296                                         dbgclose();
297                                         STDIN->close;
298                                         STDOUT->close;
299                                         STDOUT->close;
300                                         *STDIN = IO::File->new_from_fd($b, 'r') or die;
301                                         *STDOUT = IO::File->new_from_fd($b, 'w') or die;
302                                         *STDERR = IO::File->new_from_fd($b, 'w') or die;
303                                         close $a;
304                                         unless ($main::is_win) {
305 #                                               $SIG{HUP} = 'IGNORE';
306                                                 $SIG{HUP} = $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = 'DEFAULT';
307                                                 alarm(0);
308                                         }
309                                         exec "$line" or dbg("exec '$line' failed $!");
310                                 } 
311                         } else {
312                                 dbg("cannot fork");
313                                 $r = undef;
314                         }
315                 } else {
316                         dbg("no socket pair $!");
317                 }
318         } else {
319                 dbg("invalid type of connection ($sort)");
320         }
321         $conn->disconnect unless $r;
322         return $r;
323 }
324
325 sub _doabort
326 {
327         my $conn = shift;
328         my $string = shift;
329         dbg("connect $conn->{cnum}: abort $string") if isdbg('connect');
330         $conn->{abort} = $string;
331 }
332
333 sub _dotimeout
334 {
335         my $conn = shift;
336         my $val = shift;
337         dbg("connect $conn->{cnum}: timeout set to $val") if isdbg('connect');
338         $conn->{timeout}->del if $conn->{timeout};
339         $conn->{timeval} = $val;
340         $conn->{timeout} = Timer->new($val, sub{ &_timedout($conn) });
341 }
342
343 sub _dolineend
344 {
345         my $conn = shift;
346         my $val = shift;
347         dbg("connect $conn->{cnum}: lineend set to $val ") if isdbg('connect');
348         $val =~ s/\\r/\r/g;
349         $val =~ s/\\n/\n/g;
350         $conn->{lineend} = $val;
351 }
352
353 sub _dochat
354 {
355         my $conn = shift;
356         my $cmd = shift;
357         my $line = shift;
358         my $expect = shift;
359         my $send = shift;
360                 
361         if ($line) {
362                 if ($expect) {
363                         dbg("connect $conn->{cnum}: expecting: \"$expect\" received: \"$line\"") if isdbg('connect');
364                         if ($conn->{abort} && $line =~ /\Q$conn->{abort}/i) {
365                                 dbg("connect $conn->{cnum}: aborted on /$conn->{abort}/") if isdbg('connect');
366                                 $conn->disconnect;
367                                 delete $conn->{cmd};
368                                 return;
369                         }
370                         if ($line =~ /\Q$expect/i) {
371                                 if (length $send) {
372                                         dbg("connect $conn->{cnum}: got: \"$expect\" sending: \"$send\"") if isdbg('connect');
373                                         $conn->send_later("D$conn->{call}|$send");
374                                 }
375                                 delete $conn->{msg}; # get rid any input if a match
376                                 return;
377                         }
378                 }
379         }
380         $conn->{state} = 'WC';
381         unshift @{$conn->{cmd}}, $cmd;
382 }
383
384 sub _timedout
385 {
386         my $conn = shift;
387         dbg("connect $conn->{cnum}: timed out after $conn->{timeval} seconds") if isdbg('connect');
388         $conn->disconnect;
389 }
390
391 # handle callsign and connection type firtling
392 sub _doclient
393 {
394         my $conn = shift;
395         my $line = shift;
396         my @f = split /\s+/, $line;
397         my $call = uc $f[0] if $f[0];
398         $conn->conns($call);
399         $conn->{csort} = $f[1] if $f[1];
400         $conn->{state} = 'C';
401         &{$conn->{rproc}}($conn, "O$call|$conn->{csort}");
402         delete $conn->{cmd};
403         $conn->{timeout}->del if $conn->{timeout};
404 }
405
406 sub _send_file
407 {
408         my $conn = shift;
409         my $fn = shift;
410         
411         if (-e $fn) {
412                 my $f = new IO::File $fn;
413                 if ($f) {
414                         while (<$f>) {
415                                 chomp;
416                                 my $l = $_;
417                                 dbg("connect $conn->{cnum}: $l") if isdbg('connll');
418                                 $conn->send_raw($l . $conn->{lineend});
419                         }
420                         $f->close;
421                 }
422         }
423 }