2 # This class is the internal subclass that deals with the external port
3 # communications for Msg.pm
5 # This is where the cluster handles direct connections coming both in
10 # Copyright (c) 2001 - Dirk Koopman G1TLH
24 use vars qw(@ISA $deftimeout);
31 my ($conn, $msg) = @_;
32 unless ($msg =~ /^[ABZ]/) {
33 if ($msg =~ /^E[-\w]+\|([01])/ && $conn->{csort} eq 'telnet') {
36 # $conn->send_raw("\xFF\xFC\x01");
38 # $conn->send_raw("\xFF\xFB\x01");
41 $msg =~ s/^[-\w]+\|//;
42 push (@{$conn->{outqueue}}, $msg . $conn->{lineend});
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)});
62 if ($conn->{csort} eq 'ax25' && exists $conn->{msg}) {
63 $conn->{msg} =~ s/\cM/\cJ/g;
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});
72 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
73 $conn->to_connected($conn->{call}, 'O', $conn->{sort});
75 } elsif ($conn->{msg} =~ /\cJ/) {
76 my @lines = $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
77 if ($conn->{msg} =~ /\cJ$/) {
80 $conn->{msg} = pop @lines;
82 while (defined ($msg = shift @lines)) {
83 dbg('connect', $msg) unless $conn->{state} eq 'C';
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
88 if ($conn->{state} eq 'C') {
89 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
90 } elsif ($conn->{state} eq 'WL' ) {
92 if (is_callsign($msg)) {
93 $conn->to_connected($msg, 'A', 'telnet');
95 $conn->send_now("Sorry $msg is an invalid callsign");
98 } elsif ($conn->{state} eq 'WC') {
99 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
101 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
102 $conn->to_connected($conn->{call}, 'O', $conn->{sort});
112 my ($conn, $call, $dir, $sort) = @_;
113 $conn->{state} = 'C';
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");
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;
128 my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost} = $sock->peerhost(), $conn->{peerport} = $sock->peerport());
130 $conn->{eproc} = $eproc;
131 Msg::set_event_handler ($sock, "error" => $eproc);
134 $conn->{rproc} = $rproc;
135 my $callback = sub {$conn->_rcv};
136 Msg::set_event_handler ($sock, "read" => $callback);
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);
154 my $conn = ExtMsg->new(\&main::new_channel);
157 my $f = new IO::File $fn;
158 push @{$conn->{cmd}}, <$f>;
160 $conn->{state} = 'WC';
161 $conn->_dotimeout($deftimeout);
171 while ($cmd = shift @{$conn->{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)) {
181 @{$conn->{cmd}} = []; # empty any further commands
185 if ($cmd =~ /^\s*\'.*\'\s+\'.*\'/i) {
186 $conn->_dochat($cmd, $msg);
189 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
190 $conn->_doclient($1);
193 last if $conn->{state} eq 'E';
199 my ($conn, $sort, $line) = @_;
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);
210 dbg('connect', "Connected to $host $port");
212 dbg('connect', "***Connect Failed to $host $port $!");
214 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
215 local $^F = 10000; # make sure it ain't closed on exec
216 my ($a, $b) = IO::Socket->socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC);
226 $conn->{csort} = $sort;
227 $conn->{lineend} = "\cM" if $sort eq 'ax25';
229 if ($conn->{rproc}) {
230 my $callback = sub {$conn->_rcv};
231 Msg::set_event_handler ($a, read => $callback);
233 dbg('connect', "started pid: $conn->{pid} as $line");
240 *STDIN = IO::File->new_from_fd($b, 'r') or die;
241 *STDOUT = IO::File->new_from_fd($b, 'w') or die;
242 *STDERR = IO::File->new_from_fd($b, 'w') or die;
244 unless ($^O =~ /^MS/) {
245 # $SIG{HUP} = 'IGNORE';
246 $SIG{HUP} = $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = 'DEFAULT';
249 exec "$line" or dbg('err', "exec '$line' failed $!");
252 dbg('err', "cannot fork");
256 dbg('err', "no socket pair $!");
259 dbg('err', "invalid type of connection ($sort)");
261 $conn->disconnect unless $r;
269 dbg('connect', "abort $string");
270 $conn->{abort} = $string;
277 dbg('connect', "timeout set to $val");
278 $conn->{timeout}->del if $conn->{timeout};
279 $conn->{timeval} = $val;
280 $conn->{timeout} = Timer->new($val, sub{ &_timedout($conn) });
287 dbg('connect', "lineend set to $val ");
290 $conn->{lineend} = $val;
300 my ($expect, $send) = $cmd =~ /^\s*\'(.*)\'\s+\'(.*)\'/;
302 dbg('connect', "expecting: \"$expect\" received: \"$line\"");
303 if ($conn->{abort} && $line =~ /\Q$conn->{abort}/i) {
304 dbg('connect', "aborted on /$conn->{abort}/");
309 if ($line =~ /\Q$expect/i) {
310 dbg('connect', "got: \"$expect\" sending: \"$send\"");
311 $conn->send_later($send);
312 delete $conn->{msg}; # get rid any input if a match
317 $conn->{state} = 'WC';
318 unshift @{$conn->{cmd}}, $cmd;
324 dbg('connect', "timed out after $conn->{timeval} seconds");
325 $conn->{timeout}->del;
326 delete $conn->{timeout};
330 # handle callsign and connection type firtling
335 my @f = split /\s+/, $line;
336 my $call = uc $f[0] if $f[0];
338 $conn->{csort} = $f[1] if $f[1];
339 $conn->{state} = 'C';
340 &{$conn->{rproc}}($conn, "O$call|telnet");
342 $conn->{timeout}->del if $conn->{timeout};
351 my $f = new IO::File $fn;
355 $conn->send_raw($_ . $conn->{lineend});