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