fix always 'telnet' in a connect script with a client line
[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(@ISA $deftimeout);
25
26 @ISA = qw(Msg);
27 $deftimeout = 60;
28
29 sub enqueue
30 {
31         my ($conn, $msg) = @_;
32         unless ($msg =~ /^[ABZ]/) {
33                 if ($msg =~ /^E[-\w]+\|([01])/ && $conn->{csort} eq 'telnet') {
34                         $conn->{echo} = $1;
35                         if ($1) {
36 #                               $conn->send_raw("\xFF\xFC\x01");
37                         } else {
38 #                               $conn->send_raw("\xFF\xFB\x01");
39                         }
40                 } else {
41                         $msg =~ s/^[-\w]+\|//;
42                         push (@{$conn->{outqueue}}, $msg . $conn->{lineend});
43                 }
44         }
45 }
46
47 sub send_raw
48 {
49         my ($conn, $msg) = @_;
50     my $sock = $conn->{sock};
51     return unless defined($sock);
52         push (@{$conn->{outqueue}}, $msg);
53         dbg('connect', $msg) unless $conn->{state} eq 'C';
54     Msg::set_event_handler ($sock, "write" => sub {$conn->_send(0)});
55 }
56
57 sub dequeue
58 {
59         my $conn = shift;
60         my $msg;
61
62         if ($conn->{csort} eq 'ax25' && exists $conn->{msg}) {
63                 $conn->{msg} =~ s/\cM/\cJ/g;
64         }
65         if ($conn->{state} eq 'WC') {
66                 if (exists $conn->{cmd}) {
67                         if (@{$conn->{cmd}}) {
68                                 dbg('connect', $conn->{msg});
69                                 $conn->_docmd($conn->{msg});
70                         } 
71                 }
72                 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
73                         $conn->to_connected($conn->{call}, 'O', $conn->{csort});
74                 }
75         } elsif ($conn->{msg} =~ /\cJ/) {
76                 my @lines =  $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
77                 if ($conn->{msg} =~ /\cJ$/) {
78                         delete $conn->{msg};
79                 } else {
80                         $conn->{msg} = pop @lines;
81                 }
82                 while (defined ($msg = shift @lines)) {
83                         dbg('connect', $msg) unless $conn->{state} eq 'C';
84                 
85                         $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
86                         $msg =~ s/[\x00-\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g;         # immutable CSI sequence + control characters
87                         
88                         if ($conn->{state} eq 'C') {
89                                 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
90                         } elsif ($conn->{state} eq 'WL' ) {
91                                 $msg = uc $msg;
92                                 if (is_callsign($msg)) {
93                                         $conn->to_connected($msg, 'A', $conn->{csort});
94                                 } else {
95                                         $conn->send_now("Sorry $msg is an invalid callsign");
96                                         $conn->disconnect;
97                                 }
98                         } elsif ($conn->{state} eq 'WC') {
99                                 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
100                                         $conn->_docmd($msg);
101                                         if ($conn->{state} eq 'WC' && exists $conn->{cmd} &&  @{$conn->{cmd}} == 0) {
102                                                 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
103                                         }
104                                 }
105                         }
106                 }
107         }
108 }
109
110 sub to_connected
111 {
112         my ($conn, $call, $dir, $sort) = @_;
113         $conn->{state} = 'C';
114         $conn->conns($call);
115         delete $conn->{cmd};
116         $conn->{timeout}->del if $conn->{timeout};
117         delete $conn->{timeout};
118         $conn->_send_file("$main::data/connected");
119         &{$conn->{rproc}}($conn, "$dir$call|$sort");
120 }
121
122 sub new_client {
123         my $server_conn = shift;
124     my $sock = $server_conn->{sock}->accept();
125     my $conn = $server_conn->new($server_conn->{rproc});
126         $conn->{sock} = $sock;
127
128     my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost} = $sock->peerhost(), $conn->{peerport} = $sock->peerport());
129         if ($eproc) {
130                 $conn->{eproc} = $eproc;
131         Msg::set_event_handler ($sock, "error" => $eproc);
132         }
133     if ($rproc) {
134         $conn->{rproc} = $rproc;
135         my $callback = sub {$conn->_rcv};
136                 Msg::set_event_handler ($sock, "read" => $callback);
137                 # send login prompt
138                 $conn->{state} = 'WL';
139 #               $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
140 #               $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
141 #               $conn->send_raw("\xFF\xFC\x01");
142                 $conn->_send_file("$main::data/issue");
143                 $conn->send_raw("login: ");
144                 $conn->_dotimeout(60);
145     } else { 
146         $conn->disconnect();
147     }
148 }
149
150 sub start_connect
151 {
152         my $call = shift;
153         my $fn = shift;
154         my $conn = ExtMsg->new(\&main::new_channel); 
155         $conn->conns($call);
156         
157         my $f = new IO::File $fn;
158         push @{$conn->{cmd}}, <$f>;
159         $f->close;
160         $conn->{state} = 'WC';
161         $conn->_dotimeout($deftimeout);
162         $conn->_docmd;
163 }
164
165 sub _docmd
166 {
167         my $conn = shift;
168         my $msg = shift;
169         my $cmd;
170
171         while ($cmd = shift @{$conn->{cmd}}) {
172                 chomp $cmd;
173                 next if $cmd =~ /^\s*\#/o;
174                 next if $cmd =~ /^\s*$/o;
175                 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
176                 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
177                 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
178                 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
179                         unless ($conn->_doconnect($1, $2)) {
180                                 $conn->disconnect;
181                                 @{$conn->{cmd}} = [];    # empty any further commands
182                                 last;
183                         }  
184                 }
185                 if ($cmd =~ /^\s*\'.*\'\s+\'.*\'/i) {
186                         $conn->_dochat($cmd, $msg);
187                         last;
188                 }
189                 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
190                         $conn->_doclient($1);
191                         last;
192                 }
193                 last if $conn->{state} eq 'E';
194         }
195 }
196
197 sub _doconnect
198 {
199         my ($conn, $sort, $line) = @_;
200         my $r;
201
202         $sort = lc $sort;
203         dbg('connect', "CONNECT sort: $sort command: $line");
204         if ($sort eq 'telnet') {
205                 # this is a straight network connect
206                 my ($host, $port) = split /\s+/, $line;
207                 $port = 23 if !$port;
208                 $r = $conn->connect($host, $port);
209                 if ($r) {
210                         dbg('connect', "Connected to $host $port");
211                 } else {
212                         dbg('connect', "***Connect Failed to $host $port $!");
213                 }
214         } elsif ($sort eq 'agw') {
215                 # turn it into an AGW object
216                 bless $conn, 'AGWMsg';
217                 $r = $conn->connect($line);
218         } elsif ($sort eq 'ax25' || $sort eq 'prog') {
219                 local $^F = 10000;              # make sure it ain't closed on exec
220                 my ($a, $b) = IO::Socket->socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC);
221                 if ($a && $b) {
222                         $r = 1;
223                         $a->autoflush(1);
224                         $b->autoflush(1);
225                         my $pid = fork;
226                         if (defined $pid) {
227                                 if ($pid) {
228                                         close $b;
229                                         $conn->{sock} = $a;
230                                         $conn->{csort} = $sort;
231                                         $conn->{lineend} = "\cM" if $sort eq 'ax25';
232                                         $conn->{pid} = $pid;
233                                         if ($conn->{rproc}) {
234                                                 my $callback = sub {$conn->_rcv};
235                                                 Msg::set_event_handler ($a, read => $callback);
236                                         }
237                                         dbg('connect', "started pid: $conn->{pid} as $line");
238                                 } else {
239                                         $^W = 0;
240                                         dbgclose();
241                                         STDIN->close;
242                                         STDOUT->close;
243                                         STDOUT->close;
244                                         *STDIN = IO::File->new_from_fd($b, 'r') or die;
245                                         *STDOUT = IO::File->new_from_fd($b, 'w') or die;
246                                         *STDERR = IO::File->new_from_fd($b, 'w') or die;
247                                         close $a;
248                                         unless ($^O =~ /^MS/) {
249 #                                               $SIG{HUP} = 'IGNORE';
250                                                 $SIG{HUP} = $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = 'DEFAULT';
251                                                 alarm(0);
252                                         }
253                                         exec "$line" or dbg('err', "exec '$line' failed $!");
254                                 } 
255                         } else {
256                                 dbg('err', "cannot fork");      
257                                 $r = undef;
258                         }
259                 } else {
260                         dbg('err', "no socket pair $!");
261                 }
262         } else {
263                 dbg('err', "invalid type of connection ($sort)");
264         }
265         $conn->disconnect unless $r;
266         return $r;
267 }
268
269 sub _doabort
270 {
271         my $conn = shift;
272         my $string = shift;
273         dbg('connect', "abort $string");
274         $conn->{abort} = $string;
275 }
276
277 sub _dotimeout
278 {
279         my $conn = shift;
280         my $val = shift;
281         dbg('connect', "timeout set to $val");
282         $conn->{timeout}->del if $conn->{timeout};
283         $conn->{timeval} = $val;
284         $conn->{timeout} = Timer->new($val, sub{ &_timedout($conn) });
285 }
286
287 sub _dolineend
288 {
289         my $conn = shift;
290         my $val = shift;
291         dbg('connect', "lineend set to $val ");
292         $val =~ s/\\r/\r/g;
293         $val =~ s/\\n/\n/g;
294         $conn->{lineend} = $val;
295 }
296
297 sub _dochat
298 {
299         my $conn = shift;
300         my $cmd = shift;
301         my $line = shift;
302                 
303         if ($line) {
304                 my ($expect, $send) = $cmd =~ /^\s*\'(.*)\'\s+\'(.*)\'/;
305                 if ($expect) {
306                         dbg('connect', "expecting: \"$expect\" received: \"$line\"");
307                         if ($conn->{abort} && $line =~ /\Q$conn->{abort}/i) {
308                                 dbg('connect', "aborted on /$conn->{abort}/");
309                                 $conn->disconnect;
310                                 delete $conn->{cmd};
311                                 return;
312                         }
313                         if ($line =~ /\Q$expect/i) {
314                                 dbg('connect', "got: \"$expect\" sending: \"$send\"");
315                                 $conn->send_later("D$conn->{call}|$send");
316                                 delete $conn->{msg}; # get rid any input if a match
317                                 return;
318                         }
319                 }
320         }
321         $conn->{state} = 'WC';
322         unshift @{$conn->{cmd}}, $cmd;
323 }
324
325 sub _timedout
326 {
327         my $conn = shift;
328         dbg('connect', "timed out after $conn->{timeval} seconds");
329         $conn->{timeout}->del;
330         delete $conn->{timeout};
331         $conn->disconnect;
332 }
333
334 # handle callsign and connection type firtling
335 sub _doclient
336 {
337         my $conn = shift;
338         my $line = shift;
339         my @f = split /\s+/, $line;
340         my $call = uc $f[0] if $f[0];
341         $conn->conns($call);
342         $conn->{csort} = $f[1] if $f[1];
343         $conn->{state} = 'C';
344         &{$conn->{rproc}}($conn, "O$call|$conn->{csort}");
345         delete $conn->{cmd};
346         $conn->{timeout}->del if $conn->{timeout};
347 }
348
349 sub _send_file
350 {
351         my $conn = shift;
352         my $fn = shift;
353         
354         if (-e $fn) {
355                 my $f = new IO::File $fn;
356                 if ($f) {
357                         while (<$f>) {
358                                 chomp;
359                                 $conn->send_raw($_ . $conn->{lineend});
360                         }
361                         $f->close;
362                 }
363         }
364 }