]> scm.dxcluster.org Git - spider.git/blob - perl/ExtMsg.pm
remove shadow $i
[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', "connect $conn->{cnum}: $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', "connect $conn->{cnum}: $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', "connect $conn->{cnum}: $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                                         my $sort = $conn->{csort};
94                                         $sort = 'local' if $conn->{peerhost} eq "127.0.0.1";
95                                         $conn->to_connected($msg, 'A', $sort);
96                                 } else {
97                                         $conn->send_now("Sorry $msg is an invalid callsign");
98                                         $conn->disconnect;
99                                 }
100                         } elsif ($conn->{state} eq 'WC') {
101                                 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
102                                         $conn->_docmd($msg);
103                                         if ($conn->{state} eq 'WC' && exists $conn->{cmd} &&  @{$conn->{cmd}} == 0) {
104                                                 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
105                                         }
106                                 }
107                         }
108                 }
109         }
110 }
111
112 sub to_connected
113 {
114         my ($conn, $call, $dir, $sort) = @_;
115         $conn->{state} = 'C';
116         $conn->conns($call);
117         delete $conn->{cmd};
118         $conn->{timeout}->del if $conn->{timeout};
119         delete $conn->{timeout};
120         &{$conn->{rproc}}($conn, "$dir$call|$sort");
121         $conn->_send_file("$main::data/connected");
122 }
123
124 sub new_client {
125         my $server_conn = shift;
126     my $sock = $server_conn->{sock}->accept();
127         if ($sock) {
128                 my $conn = $server_conn->new($server_conn->{rproc});
129                 $conn->{sock} = $sock;
130                 Msg::blocking($sock, 0);
131                 $conn->{blocking} = 0;
132                 
133                 my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost} = $sock->peerhost(), $conn->{peerport} = $sock->peerport());
134                 dbg('connll', "accept $conn->{cnum} from $conn->{peerhost} $conn->{peerport}");
135                 if ($eproc) {
136                         $conn->{eproc} = $eproc;
137                         Msg::set_event_handler ($sock, "error" => $eproc);
138                 }
139                 if ($rproc) {
140                         $conn->{rproc} = $rproc;
141                         my $callback = sub {$conn->_rcv};
142                         Msg::set_event_handler ($sock, "read" => $callback);
143                         # send login prompt
144                         $conn->{state} = 'WL';
145                         #               $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
146                         #               $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
147                         #               $conn->send_raw("\xFF\xFC\x01");
148                         $conn->_send_file("$main::data/issue");
149                         $conn->send_raw("login: ");
150                         $conn->_dotimeout(60);
151                 } else { 
152                         &{$conn->{eproc}}() if $conn->{eproc};
153                         $conn->disconnect();
154                 }
155         } else {
156                 dbg('err', "ExtMsg: error on accept ($!)");
157         }
158 }
159
160 sub start_connect
161 {
162         my $call = shift;
163         my $fn = shift;
164         my $conn = ExtMsg->new(\&main::new_channel); 
165         $conn->conns($call);
166         
167         my $f = new IO::File $fn;
168         push @{$conn->{cmd}}, <$f>;
169         $f->close;
170         $conn->{state} = 'WC';
171         $conn->_dotimeout($deftimeout);
172         $conn->_docmd;
173 }
174
175 sub _docmd
176 {
177         my $conn = shift;
178         my $msg = shift;
179         my $cmd;
180
181         while ($cmd = shift @{$conn->{cmd}}) {
182                 chomp $cmd;
183                 next if $cmd =~ /^\s*\#/o;
184                 next if $cmd =~ /^\s*$/o;
185                 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
186                 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
187                 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
188                 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
189                         unless ($conn->_doconnect($1, $2)) {
190                                 $conn->disconnect;
191                                 @{$conn->{cmd}} = [];    # empty any further commands
192                                 last;
193                         }  
194                 }
195                 if ($cmd =~ /^\s*\'.*\'\s+\'.*\'/i) {
196                         $conn->_dochat($cmd, $msg);
197                         last;
198                 }
199                 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
200                         $conn->_doclient($1);
201                         last;
202                 }
203                 last if $conn->{state} eq 'E';
204         }
205 }
206
207 sub _doconnect
208 {
209         my ($conn, $sort, $line) = @_;
210         my $r;
211
212         $sort = lc $sort;
213         dbg('connect', "CONNECT $conn->{cnum} sort: $sort command: $line");
214         if ($sort eq 'telnet') {
215                 # this is a straight network connect
216                 my ($host, $port) = split /\s+/, $line;
217                 $port = 23 if !$port;
218                 $r = $conn->connect($host, $port);
219                 if ($r) {
220                         dbg('connect', "Connected $conn->{cnum} to $host $port");
221                 } else {
222                         dbg('connect', "***Connect $conn->{cnum} Failed to $host $port $!");
223                 }
224         } elsif ($sort eq 'agw') {
225                 # turn it into an AGW object
226                 bless $conn, 'AGWMsg';
227                 $r = $conn->connect($line);
228         } elsif ($sort eq 'ax25' || $sort eq 'prog') {
229                 local $^F = 10000;              # make sure it ain't closed on exec
230                 my ($a, $b) = IO::Socket->socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC);
231                 if ($a && $b) {
232                         $r = 1;
233                         $a->autoflush(1);
234                         $b->autoflush(1);
235                         my $pid = fork;
236                         if (defined $pid) {
237                                 if ($pid) {
238                                         close $b;
239                                         $conn->{sock} = $a;
240                                         $conn->{csort} = $sort;
241                                         $conn->{lineend} = "\cM" if $sort eq 'ax25';
242                                         $conn->{pid} = $pid;
243                                         if ($conn->{rproc}) {
244                                                 my $callback = sub {$conn->_rcv};
245                                                 Msg::set_event_handler ($a, read => $callback);
246                                         }
247                                         dbg('connect', "connect $conn->{cnum}: started pid: $conn->{pid} as $line");
248                                 } else {
249                                         $^W = 0;
250                                         dbgclose();
251                                         STDIN->close;
252                                         STDOUT->close;
253                                         STDOUT->close;
254                                         *STDIN = IO::File->new_from_fd($b, 'r') or die;
255                                         *STDOUT = IO::File->new_from_fd($b, 'w') or die;
256                                         *STDERR = IO::File->new_from_fd($b, 'w') or die;
257                                         close $a;
258                                         unless ($main::is_win) {
259 #                                               $SIG{HUP} = 'IGNORE';
260                                                 $SIG{HUP} = $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = 'DEFAULT';
261                                                 alarm(0);
262                                         }
263                                         exec "$line" or dbg('err', "exec '$line' failed $!");
264                                 } 
265                         } else {
266                                 dbg('err', "cannot fork");      
267                                 $r = undef;
268                         }
269                 } else {
270                         dbg('err', "no socket pair $!");
271                 }
272         } else {
273                 dbg('err', "invalid type of connection ($sort)");
274         }
275         $conn->disconnect unless $r;
276         return $r;
277 }
278
279 sub _doabort
280 {
281         my $conn = shift;
282         my $string = shift;
283         dbg('connect', "connect $conn->{cnum}: abort $string");
284         $conn->{abort} = $string;
285 }
286
287 sub _dotimeout
288 {
289         my $conn = shift;
290         my $val = shift;
291         dbg('connect', "connect $conn->{cnum}: timeout set to $val");
292         $conn->{timeout}->del if $conn->{timeout};
293         $conn->{timeval} = $val;
294         $conn->{timeout} = Timer->new($val, sub{ &_timedout($conn) });
295 }
296
297 sub _dolineend
298 {
299         my $conn = shift;
300         my $val = shift;
301         dbg('connect', "connect $conn->{cnum}: lineend set to $val ");
302         $val =~ s/\\r/\r/g;
303         $val =~ s/\\n/\n/g;
304         $conn->{lineend} = $val;
305 }
306
307 sub _dochat
308 {
309         my $conn = shift;
310         my $cmd = shift;
311         my $line = shift;
312                 
313         if ($line) {
314                 my ($expect, $send) = $cmd =~ /^\s*\'(.*)\'\s+\'(.*)\'/;
315                 if ($expect) {
316                         dbg('connect', "connect $conn->{cnum}: expecting: \"$expect\" received: \"$line\"");
317                         if ($conn->{abort} && $line =~ /\Q$conn->{abort}/i) {
318                                 dbg('connect', "connect $conn->{cnum}: aborted on /$conn->{abort}/");
319                                 $conn->disconnect;
320                                 delete $conn->{cmd};
321                                 return;
322                         }
323                         if ($line =~ /\Q$expect/i) {
324                                 if (length $send) {
325                                         dbg('connect', "connect $conn->{cnum}: got: \"$expect\" sending: \"$send\"");
326                                         $conn->send_later("D$conn->{call}|$send");
327                                 }
328                                 delete $conn->{msg}; # get rid any input if a match
329                                 return;
330                         }
331                 }
332         }
333         $conn->{state} = 'WC';
334         unshift @{$conn->{cmd}}, $cmd;
335 }
336
337 sub _timedout
338 {
339         my $conn = shift;
340         dbg('connect', "connect $conn->{cnum}: timed out after $conn->{timeval} seconds");
341         $conn->disconnect;
342 }
343
344 # handle callsign and connection type firtling
345 sub _doclient
346 {
347         my $conn = shift;
348         my $line = shift;
349         my @f = split /\s+/, $line;
350         my $call = uc $f[0] if $f[0];
351         $conn->conns($call);
352         $conn->{csort} = $f[1] if $f[1];
353         $conn->{state} = 'C';
354         &{$conn->{rproc}}($conn, "O$call|$conn->{csort}");
355         delete $conn->{cmd};
356         $conn->{timeout}->del if $conn->{timeout};
357 }
358
359 sub _send_file
360 {
361         my $conn = shift;
362         my $fn = shift;
363         
364         if (-e $fn) {
365                 my $f = new IO::File $fn;
366                 if ($f) {
367                         while (<$f>) {
368                                 chomp;
369                                 my $l = $_;
370                                 dbg('connll', "connect $conn->{cnum}: $l");
371                                 $conn->send_raw($l . $conn->{lineend});
372                         }
373                         $f->close;
374                 }
375         }
376 }