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 =~ m{^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 dbg((ref $conn) . " connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
58 $conn->SUPER::send_raw($msg);
64 $conn->{echo} = shift;
72 if ($conn->ax25 && exists $conn->{msg}) {
73 $conn->{msg} =~ s/\cM/\cJ/g;
75 if ($conn->{state} eq 'WC') {
76 if (exists $conn->{cmd}) {
77 if (@{$conn->{cmd}}) {
78 dbg("connect $conn->{cnum}: $conn->{msg}") if isdbg('connect');
79 $conn->_docmd($conn->{msg});
82 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
83 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
85 } elsif ($conn->{msg} =~ /\cJ/) {
86 my @lines = $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
87 if ($conn->{msg} =~ /\cJ$/) {
90 $conn->{msg} =~ s/([^\cM\cJ]*)\cM?\cJ//g;
92 while (defined ($msg = shift @lines)) {
93 dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
95 $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
96 # $msg =~ s/[\x00-\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
98 if ($conn->{state} eq 'C') {
99 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
100 } elsif ($conn->{state} eq 'WL' ) {
102 if (is_callsign($msg)) {
103 if ($main::allowslashcall || $msg !~ m|/|) {
104 my $sort = $conn->{csort};
105 $sort = 'local' if $conn->{peerhost} =~ /127\.\d+\.\d+\.\d+$/ || $conn->{peerhost} eq '::1';
107 if ($main::passwdreq || ($uref = DXUser::get_current($msg)) && $uref->passwd ) {
109 $conn->{state} = 'WP';
110 $conn->{decho} = $conn->{echo};
112 $conn->send_raw('password: ');
114 $conn->to_connected($msg, 'A', $sort);
117 $conn->send_now("Sorry $msg is an invalid callsign");
121 $conn->send_now("Sorry $msg is an invalid callsign");
124 } elsif ($conn->{state} eq 'WP' ) {
125 my $uref = DXUser::get_current($conn->{call});
126 $msg =~ s/[\r\n]+$//;
127 if ($uref && $msg eq $uref->passwd) {
128 my $sort = $conn->{csort};
129 $conn->{echo} = $conn->{decho};
130 delete $conn->{decho};
131 $sort = 'local' if $conn->{peerhost} =~ /127\.\d+\.\d+\.\d+$/ || $conn->{peerhost} eq '::1';
132 $conn->{usedpasswd} = 1;
133 $conn->to_connected($conn->{call}, 'A', $sort);
135 $conn->send_now("Sorry");
138 } elsif ($conn->{state} eq 'WC') {
139 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
141 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
142 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
152 my ($conn, $call, $dir, $sort) = @_;
153 $conn->{state} = 'C';
156 $conn->{timeout}->del if $conn->{timeout};
157 delete $conn->{timeout};
158 $conn->{csort} = $sort;
159 &{$conn->{rproc}}($conn, "$dir$call|$sort");
160 $conn->_send_file(localdata("connected")) unless $conn->{outgoing};
165 my $server_conn = shift;
167 my $conn = $server_conn->SUPER::new_client($client);
169 $conn->{state} = 'WL';
170 $conn->_send_file(localdata("issue"));
171 $conn->send_raw("login: ");
172 $conn->_dotimeout(60);
180 my $conn = ExtMsg->new(\&main::new_channel);
181 $conn->{outgoing} = 1;
184 my $f = new IO::File $fn;
185 push @{$conn->{cmd}}, <$f>;
187 $conn->{state} = 'WC';
188 $conn->_dotimeout($deftimeout);
198 while ($cmd = shift @{$conn->{cmd}}) {
200 next if $cmd =~ /^\s*\#/o;
201 next if $cmd =~ /^\s*$/o;
202 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
203 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
204 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
205 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
206 unless ($conn->_doconnect($1, $2)) {
208 @{$conn->{cmd}} = []; # empty any further commands
212 if ($cmd =~ /^\s*\'([^\']*)\'\s+\'([^\']*)\'/) {
213 $conn->_dochat($cmd, $msg, $1, $2);
216 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
217 $conn->_doclient($1);
220 last if $conn->{state} eq 'E';
226 my ($conn, $sort, $line) = @_;
230 dbg("CONNECT $conn->{cnum} sort: $sort command: $line") if isdbg('connect');
231 if ($sort eq 'telnet') {
232 # this is a straight network connect
233 my ($host, $port) = split /\s+/, $line;
234 $port = 23 if !$port;
235 $r = $conn->connect($host, $port);
237 dbg("Connected $conn->{cnum} to $host $port") if isdbg('connect');
239 dbg("***Connect $conn->{cnum} Failed to $host $port $!") if isdbg('connect');
241 } elsif ($sort eq 'agw') {
242 # turn it into an AGW object
243 bless $conn, 'AGWMsg';
244 $r = $conn->connect($line);
245 } elsif ($sort eq 'bpq') {
246 # turn it into an BPQ object
247 bless $conn, 'BPQMsg';
248 $r = $conn->connect($line);
249 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
250 $r = $conn->start_program($line, $sort);
252 dbg("invalid type of connection ($sort)");
254 $conn->disconnect unless $r;
262 dbg("connect $conn->{cnum}: abort $string") if isdbg('connect');
263 $conn->{abort} = $string;
270 dbg("connect $conn->{cnum}: timeout set to $val") if isdbg('connect');
271 $conn->{timeout}->del if $conn->{timeout};
272 $conn->{timeval} = $val;
273 $conn->{timeout} = Timer->new($val, sub{ &_timedout($conn) });
280 dbg("connect $conn->{cnum}: lineend set to $val ") if isdbg('connect');
283 $conn->{lineend} = $val;
296 dbg("connect $conn->{cnum}: expecting: \"$expect\" received: \"$line\"") if isdbg('connect');
297 if ($conn->{abort} && $line =~ /$conn->{abort}/i) {
298 dbg("connect $conn->{cnum}: aborted on /$conn->{abort}/") if isdbg('connect');
303 if ($line =~ /\Q$expect/i) {
305 dbg("connect $conn->{cnum}: got: \"$expect\" sending: \"$send\"") if isdbg('connect');
306 $conn->send_later("D$conn->{call}|$send");
308 delete $conn->{msg}; # get rid any input if a match
313 $conn->{state} = 'WC';
314 unshift @{$conn->{cmd}}, $cmd;
320 dbg("connect $conn->{cnum}: timed out after $conn->{timeval} seconds") if isdbg('connect');
324 # handle callsign and connection type firtling
329 my @f = split /\s+/, $line;
330 my $call = uc $f[0] if $f[0];
332 $conn->{csort} = $f[1] if $f[1];
333 $conn->{state} = 'C';
334 eval {$conn->{peerhost} = $conn->{sock}->handle->peerhost} unless $conn->ax25;
335 &{$conn->{rproc}}($conn, "O$call|$conn->{csort}");
337 $conn->{timeout}->del if $conn->{timeout};
346 my $f = new IO::File $fn;
351 dbg("connect $conn->{cnum}: $l") if isdbg('connll');
352 $conn->send_raw($l . $conn->{lineend});