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 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) && $msg !~ m|/| ) {
103 my $sort = $conn->{csort};
104 $sort = 'local' if $conn->{peerhost} =~ /127\.\d+\.\d+\.\d+$/ || $conn->{peerhost} eq '::1';
106 if ($main::passwdreq || ($uref = DXUser::get_current($msg)) && $uref->passwd ) {
108 $conn->{state} = 'WP';
109 $conn->{decho} = $conn->{echo};
111 $conn->send_raw('password: ');
113 $conn->to_connected($msg, 'A', $sort);
116 $conn->send_now("Sorry $msg is an invalid callsign");
119 } elsif ($conn->{state} eq 'WP' ) {
120 my $uref = DXUser::get_current($conn->{call});
121 $msg =~ s/[\r\n]+$//;
122 if ($uref && $msg eq $uref->passwd) {
123 my $sort = $conn->{csort};
124 $conn->{echo} = $conn->{decho};
125 delete $conn->{decho};
126 $sort = 'local' if $conn->{peerhost} =~ /127\.\d+\.\d+\.\d+$/ || $conn->{peerhost} eq '::1';
127 $conn->{usedpasswd} = 1;
128 $conn->to_connected($conn->{call}, 'A', $sort);
130 $conn->send_now("Sorry");
133 } elsif ($conn->{state} eq 'WC') {
134 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
136 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
137 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
147 my ($conn, $call, $dir, $sort) = @_;
148 $conn->{state} = 'C';
151 $conn->{timeout}->del if $conn->{timeout};
152 delete $conn->{timeout};
153 $conn->{csort} = $sort;
154 &{$conn->{rproc}}($conn, "$dir$call|$sort");
155 $conn->_send_file(localdata("connected")) unless $conn->{outgoing};
160 my $server_conn = shift;
162 my $conn = $server_conn->SUPER::new_client($client);
164 $conn->{state} = 'WL';
165 $conn->_send_file(localdata("issue"));
166 $conn->send_raw("login: ");
167 $conn->_dotimeout(60);
175 my $conn = ExtMsg->new(\&main::new_channel);
176 $conn->{outgoing} = 1;
179 my $f = new IO::File $fn;
180 push @{$conn->{cmd}}, <$f>;
182 $conn->{state} = 'WC';
183 $conn->_dotimeout($deftimeout);
193 while ($cmd = shift @{$conn->{cmd}}) {
195 next if $cmd =~ /^\s*\#/o;
196 next if $cmd =~ /^\s*$/o;
197 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
198 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
199 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
200 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
201 unless ($conn->_doconnect($1, $2)) {
203 @{$conn->{cmd}} = []; # empty any further commands
207 if ($cmd =~ /^\s*\'([^\']*)\'\s+\'([^\']*)\'/) {
208 $conn->_dochat($cmd, $msg, $1, $2);
211 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
212 $conn->_doclient($1);
215 last if $conn->{state} eq 'E';
221 my ($conn, $sort, $line) = @_;
225 dbg("CONNECT $conn->{cnum} sort: $sort command: $line") if isdbg('connect');
226 if ($sort eq 'telnet') {
227 # this is a straight network connect
228 my ($host, $port) = split /\s+/, $line;
229 $port = 23 if !$port;
230 $r = $conn->connect($host, $port);
232 dbg("Connected $conn->{cnum} to $host $port") if isdbg('connect');
234 dbg("***Connect $conn->{cnum} Failed to $host $port $!") if isdbg('connect');
236 } elsif ($sort eq 'agw') {
237 # turn it into an AGW object
238 bless $conn, 'AGWMsg';
239 $r = $conn->connect($line);
240 } elsif ($sort eq 'bpq') {
241 # turn it into an BPQ object
242 bless $conn, 'BPQMsg';
243 $r = $conn->connect($line);
244 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
245 $r = $conn->start_program($line, $sort);
247 dbg("invalid type of connection ($sort)");
249 $conn->disconnect unless $r;
257 dbg("connect $conn->{cnum}: abort $string") if isdbg('connect');
258 $conn->{abort} = $string;
265 dbg("connect $conn->{cnum}: timeout set to $val") if isdbg('connect');
266 $conn->{timeout}->del if $conn->{timeout};
267 $conn->{timeval} = $val;
268 $conn->{timeout} = Timer->new($val, sub{ &_timedout($conn) });
275 dbg("connect $conn->{cnum}: lineend set to $val ") if isdbg('connect');
278 $conn->{lineend} = $val;
291 dbg("connect $conn->{cnum}: expecting: \"$expect\" received: \"$line\"") if isdbg('connect');
292 if ($conn->{abort} && $line =~ /$conn->{abort}/i) {
293 dbg("connect $conn->{cnum}: aborted on /$conn->{abort}/") if isdbg('connect');
298 if ($line =~ /\Q$expect/i) {
300 dbg("connect $conn->{cnum}: got: \"$expect\" sending: \"$send\"") if isdbg('connect');
301 $conn->send_later("D$conn->{call}|$send");
303 delete $conn->{msg}; # get rid any input if a match
308 $conn->{state} = 'WC';
309 unshift @{$conn->{cmd}}, $cmd;
315 dbg("connect $conn->{cnum}: timed out after $conn->{timeval} seconds") if isdbg('connect');
319 # handle callsign and connection type firtling
324 my @f = split /\s+/, $line;
325 my $call = uc $f[0] if $f[0];
327 $conn->{csort} = $f[1] if $f[1];
328 $conn->{state} = 'C';
329 eval {$conn->{peerhost} = $conn->{sock}->handle->peerhost} unless $conn->ax25;
330 &{$conn->{rproc}}($conn, "O$call|$conn->{csort}");
332 $conn->{timeout}->del if $conn->{timeout};
341 my $f = new IO::File $fn;
346 dbg("connect $conn->{cnum}: $l") if isdbg('connll');
347 $conn->send_raw($l . $conn->{lineend});