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