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
9 # Copyright (c) 2001 - Dirk Koopman G1TLH
11 # Modified Jan 2006 by John Wiseman G8BPQ to support connections to BPQ32 node,
12 # and fix pattern matching on 'chat' abort handling
26 use vars qw(@ISA $deftimeout);
33 goto &main::login; # save some writing, this was the default
38 my ($conn, $msg) = @_;
39 unless ($msg =~ /^[ABZ]/) {
40 if ($msg =~ /^E[-\w]+\|([01])/ && $conn->{csort} eq 'telnet') {
43 # $conn->send_raw("\xFF\xFC\x01");
45 # $conn->send_raw("\xFF\xFB\x01");
48 $msg =~ s/^[-\w]+\|//;
49 push (@{$conn->{outqueue}}, $msg . $conn->{lineend});
56 my ($conn, $msg) = @_;
57 my $sock = $conn->{sock};
58 return unless defined($sock);
59 push (@{$conn->{outqueue}}, $msg);
60 dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
61 Msg::set_event_handler ($sock, "write" => sub {$conn->_send(0)});
67 $conn->{echo} = shift;
75 if ($conn->{csort} eq 'ax25' && exists $conn->{msg}) {
76 $conn->{msg} =~ s/\cM/\cJ/g;
78 if ($conn->{state} eq 'WC') {
79 if (exists $conn->{cmd}) {
80 if (@{$conn->{cmd}}) {
81 dbg("connect $conn->{cnum}: $conn->{msg}") if isdbg('connect');
82 $conn->_docmd($conn->{msg});
85 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
86 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
88 } elsif ($conn->{msg} =~ /\cJ/) {
89 my @lines = $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
90 if ($conn->{msg} =~ /\cJ$/) {
93 $conn->{msg} =~ s/([^\cM\cJ]*)\cM?\cJ//g;
95 while (defined ($msg = shift @lines)) {
96 dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
98 $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
99 # $msg =~ s/[\x00-\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
101 if ($conn->{state} eq 'C') {
102 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
103 } elsif ($conn->{state} eq 'WL' ) {
105 if (is_callsign($msg) && $msg !~ m|/| ) {
106 my $sort = $conn->{csort};
107 $sort = 'local' if $conn->{peerhost} =~ /127\.\d+\.\d+\.\d+$/ || $conn->{peerhost} eq '::1';
109 if ($main::passwdreq || ($uref = DXUser::get_current($msg)) && $uref->passwd ) {
111 $conn->{state} = 'WP';
112 $conn->{decho} = $conn->{echo};
114 $conn->send_raw('password: ');
116 $conn->to_connected($msg, 'A', $sort);
119 $conn->send_now("Sorry $msg is an invalid callsign");
122 } elsif ($conn->{state} eq 'WP' ) {
123 my $uref = DXUser::get_current($conn->{call});
124 $msg =~ s/[\r\n]+$//;
125 if ($uref && $msg eq $uref->passwd) {
126 my $sort = $conn->{csort};
127 $conn->{echo} = $conn->{decho};
128 delete $conn->{decho};
129 $sort = 'local' if $conn->{peerhost} =~ /127\.\d+\.\d+\.\d+$/ || $conn->{peerhost} eq '::1';
130 $conn->{usedpasswd} = 1;
131 $conn->to_connected($conn->{call}, 'A', $sort);
133 $conn->send_now("Sorry");
136 } elsif ($conn->{state} eq 'WC') {
137 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
139 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
140 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
150 my ($conn, $call, $dir, $sort) = @_;
151 $conn->{state} = 'C';
154 $conn->{timeout}->del if $conn->{timeout};
155 delete $conn->{timeout};
156 $conn->nolinger unless $conn->isa('AGWMsg') || $conn->isa('BPQMsg');
157 &{$conn->{rproc}}($conn, "$dir$call|$sort");
158 $conn->_send_file("$main::data/connected") unless $conn->{outgoing};
162 my $server_conn = shift;
163 my $sock = $server_conn->{sock}->accept();
165 my $conn = $server_conn->new($server_conn->{rproc});
166 $conn->{sock} = $sock;
168 Msg::blocking($sock, 0);
169 $conn->{blocking} = 0;
170 eval {$conn->{peerhost} = $sock->peerhost};
172 dbg($@) if isdbg('connll');
175 eval {$conn->{peerport} = $sock->peerport};
176 $conn->{peerport} = 0 if $@;
177 my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost}, $conn->{peerport});
178 dbg("accept $conn->{cnum} from $conn->{peerhost} $conn->{peerport}") if isdbg('connll');
180 $conn->{eproc} = $eproc;
181 Msg::set_event_handler ($sock, "error" => $eproc);
184 $conn->{rproc} = $rproc;
185 my $callback = sub {$conn->_rcv};
186 Msg::set_event_handler ($sock, "read" => $callback);
188 $conn->{state} = 'WL';
189 # $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
190 # $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
191 # $conn->send_raw("\xFF\xFC\x01");
192 $conn->_send_file("$main::data/issue");
193 $conn->send_raw("login: ");
194 $conn->_dotimeout(60);
197 &{$conn->{eproc}}() if $conn->{eproc};
202 dbg("ExtMsg: error on accept ($!)") if isdbg('err');
210 my $conn = ExtMsg->new(\&main::new_channel);
211 $conn->{outgoing} = 1;
214 my $f = new IO::File $fn;
215 push @{$conn->{cmd}}, <$f>;
217 $conn->{state} = 'WC';
218 $conn->_dotimeout($deftimeout);
228 while ($cmd = shift @{$conn->{cmd}}) {
230 next if $cmd =~ /^\s*\#/o;
231 next if $cmd =~ /^\s*$/o;
232 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
233 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
234 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
235 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
236 unless ($conn->_doconnect($1, $2)) {
238 @{$conn->{cmd}} = []; # empty any further commands
242 if ($cmd =~ /^\s*\'([^\']*)\'\s+\'([^\']*)\'/) {
243 $conn->_dochat($cmd, $msg, $1, $2);
246 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
247 $conn->_doclient($1);
250 last if $conn->{state} eq 'E';
256 my ($conn, $sort, $line) = @_;
260 dbg("CONNECT $conn->{cnum} sort: $sort command: $line") if isdbg('connect');
261 if ($sort eq 'telnet') {
262 # this is a straight network connect
263 my ($host, $port) = split /\s+/, $line;
264 $port = 23 if !$port;
265 $r = $conn->connect($host, $port);
267 dbg("Connected $conn->{cnum} to $host $port") if isdbg('connect');
269 dbg("***Connect $conn->{cnum} Failed to $host $port $!") if isdbg('connect');
271 } elsif ($sort eq 'agw') {
272 # turn it into an AGW object
273 bless $conn, 'AGWMsg';
274 $r = $conn->connect($line);
275 } elsif ($sort eq 'bpq') {
276 # turn it into an BPQ object
277 bless $conn, 'BPQMsg';
278 $r = $conn->connect($line);
279 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
280 $r = $conn->start_program($line, $sort);
282 dbg("invalid type of connection ($sort)");
284 $conn->disconnect unless $r;
292 dbg("connect $conn->{cnum}: abort $string") if isdbg('connect');
293 $conn->{abort} = $string;
300 dbg("connect $conn->{cnum}: timeout set to $val") if isdbg('connect');
301 $conn->{timeout}->del if $conn->{timeout};
302 $conn->{timeval} = $val;
303 $conn->{timeout} = Timer->new($val, sub{ &_timedout($conn) });
310 dbg("connect $conn->{cnum}: lineend set to $val ") if isdbg('connect');
313 $conn->{lineend} = $val;
326 dbg("connect $conn->{cnum}: expecting: \"$expect\" received: \"$line\"") if isdbg('connect');
327 if ($conn->{abort} && $line =~ /$conn->{abort}/i) {
328 dbg("connect $conn->{cnum}: aborted on /$conn->{abort}/") if isdbg('connect');
333 if ($line =~ /\Q$expect/i) {
335 dbg("connect $conn->{cnum}: got: \"$expect\" sending: \"$send\"") if isdbg('connect');
336 $conn->send_later("D$conn->{call}|$send");
338 delete $conn->{msg}; # get rid any input if a match
343 $conn->{state} = 'WC';
344 unshift @{$conn->{cmd}}, $cmd;
350 dbg("connect $conn->{cnum}: timed out after $conn->{timeval} seconds") if isdbg('connect');
354 # handle callsign and connection type firtling
359 my @f = split /\s+/, $line;
360 my $call = uc $f[0] if $f[0];
362 $conn->{csort} = $f[1] if $f[1];
363 $conn->{state} = 'C';
364 &{$conn->{rproc}}($conn, "O$call|$conn->{csort}");
366 $conn->{timeout}->del if $conn->{timeout};
375 my $f = new IO::File $fn;
380 dbg("connect $conn->{cnum}: $l") if isdbg('connll');
381 $conn->send_raw($l . $conn->{lineend});