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
23 use vars qw(@ISA $deftimeout);
30 my ($conn, $msg) = @_;
31 unless ($msg =~ /^[ABZ]/) {
32 if ($msg =~ /^E[-\w]+\|([01])/ && $conn->{csort} eq 'telnet') {
35 # $conn->send_raw("\xFF\xFC\x01");
37 # $conn->send_raw("\xFF\xFB\x01");
40 $msg =~ s/^[-\w]+\|//;
41 push (@{$conn->{outqueue}}, $msg . $conn->{lineend});
48 my ($conn, $msg) = @_;
49 my $sock = $conn->{sock};
50 return unless defined($sock);
51 push (@{$conn->{outqueue}}, $msg);
52 dbg('connect', $msg) unless $conn->{state} eq 'C';
53 Msg::set_event_handler ($sock, "write" => sub {$conn->_send(0)});
61 if ($conn->{csort} eq 'ax25' && exists $conn->{msg}) {
62 $conn->{msg} =~ s/\cM/\cJ/g;
64 if ($conn->{state} eq 'WC') {
65 if (exists $conn->{cmd}) {
66 if (@{$conn->{cmd}}) {
67 dbg('connect', $conn->{msg});
68 $conn->_docmd($conn->{msg});
71 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
72 $conn->to_connected($conn->{call}, 'O', 'telnet');
74 } elsif ($conn->{msg} =~ /\cJ/) {
75 my @lines = $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
76 if ($conn->{msg} =~ /\cJ$/) {
79 $conn->{msg} = pop @lines;
81 while (defined ($msg = shift @lines)) {
82 dbg('connect', $msg) unless $conn->{state} eq 'C';
84 $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
85 $msg =~ s/[\x00-\x08\x0a-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
87 if ($conn->{state} eq 'C') {
88 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
89 } elsif ($conn->{state} eq 'WL' ) {
91 if (is_callsign($msg)) {
92 $conn->to_connected($msg, 'A', 'telnet');
94 $conn->send_now("Sorry $msg is an invalid callsign");
97 } elsif ($conn->{state} eq 'WC') {
98 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
100 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
101 $conn->to_connected($conn->{call}, 'O', 'telnet');
111 my ($conn, $call, $dir, $sort) = @_;
112 $conn->{state} = 'C';
115 $conn->{timeout}->del if $conn->{timeout};
116 delete $conn->{timeout};
117 $conn->_send_file("$main::data/connected");
118 &{$conn->{rproc}}($conn, "$dir$call|$sort");
122 my $server_conn = shift;
123 my $sock = $server_conn->{sock}->accept();
124 my $conn = $server_conn->new($server_conn->{rproc});
125 $conn->{sock} = $sock;
127 my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost} = $sock->peerhost(), $conn->{peerport} = $sock->peerport());
129 $conn->{eproc} = $eproc;
130 Msg::set_event_handler ($sock, "error" => $eproc);
133 $conn->{rproc} = $rproc;
134 my $callback = sub {$conn->_rcv};
135 Msg::set_event_handler ($sock, "read" => $callback);
137 $conn->{state} = 'WL';
138 # $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
139 # $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
140 # $conn->send_raw("\xFF\xFC\x01");
141 $conn->_send_file("$main::data/issue");
142 $conn->send_raw("login: ");
143 $conn->_dotimeout(60);
153 my $conn = ExtMsg->new(\&main::new_channel);
156 my $f = new IO::File $fn;
157 push @{$conn->{cmd}}, <$f>;
159 $conn->_dotimeout($deftimeout);
169 while ($cmd = shift @{$conn->{cmd}}) {
171 next if $cmd =~ /^\s*\#/o;
172 next if $cmd =~ /^\s*$/o;
173 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
174 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
175 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
176 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
177 unless ($conn->_doconnect($1, $2)) {
179 @{$conn->{cmd}} = []; # empty any further commands
183 if ($cmd =~ /^\s*\'.*\'\s+\'.*\'/i) {
184 $conn->_dochat($cmd, $msg);
187 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
188 $conn->_doclient($1);
191 last if $conn->{state} eq 'E';
197 my ($conn, $sort, $line) = @_;
201 dbg('connect', "CONNECT sort: $sort command: $line");
202 if ($sort eq 'telnet') {
203 # this is a straight network connect
204 my ($host, $port) = split /\s+/, $line;
205 $port = 23 if !$port;
206 $r = $conn->connect($host, $port);
208 dbg('connect', "Connected to $host $port");
210 dbg('connect', "***Connect Failed to $host $port $!");
212 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
216 # in child, unset warnings, disable debugging and general clean up from us
218 eval "{ package DB; sub DB {} }";
219 DXChannel::closeall();
220 for (@main::listeners) {
223 unless ($^O =~ /^MS/) {
224 $SIG{HUP} = 'IGNORE';
225 $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = $SIG{__WARN__} = 'DEFAULT';
228 exec "$line" or dbg('cron', "exec '$line' failed $!");
230 dbg('connect', "program $sort $line started");
232 $conn->{csort} = $sort;
233 $conn->{lineend} = "\cM" if $sort eq 'ax25';
235 dbg('connect', "can't $sort fork for $line $!");
238 dbg('err', "invalid type of connection ($sort)");
248 dbg('connect', "abort $string");
249 $conn->{abort} = $string;
256 dbg('connect', "timeout set to $val");
257 $conn->{timeout}->del if $conn->{timeout};
258 $conn->{timeval} = $val;
259 $conn->{timeout} = Timer->new($val, sub{ &_timedout($conn) });
266 dbg('connect', "lineend set to $val ");
269 $conn->{lineend} = $val;
279 my ($expect, $send) = $cmd =~ /^\s*\'(.*)\'\s+\'(.*)\'/;
281 dbg('connect', "expecting: \"$expect\" received: \"$line\"");
282 if ($conn->{abort} && $line =~ /$conn->{abort}/i) {
283 dbg('connect', "aborted on /$conn->{abort}/");
288 if ($line =~ /$expect/i) {
289 dbg('connect', "got: \"$expect\" sending: \"$send\"");
290 $conn->send_later($send);
291 delete $conn->{msg}; # get rid any input if a match
296 $conn->{state} = 'WC';
297 unshift @{$conn->{cmd}}, $cmd;
303 dbg('connect', "timed out after $conn->{timeval} seconds");
304 $conn->{timeout}->del;
305 delete $conn->{timeout};
309 # handle callsign and connection type firtling
314 my @f = split /\s+/, $line;
315 my $call = uc $f[0] if $f[0];
317 $conn->{csort} = $f[1] if $f[1];
318 $conn->{state} = 'C';
319 &{$conn->{rproc}}($conn, "O$call|telnet");
321 $conn->{timeout}->del if $conn->{timeout};
330 my $f = new IO::File $fn;
334 $conn->send_raw($_ . $conn->{lineend});