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