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($VERSION $BRANCH);
25 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
26 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
27 $main::build += $VERSION;
28 $main::branch += $BRANCH;
30 use vars qw(@ISA $deftimeout);
37 goto &main::login; # save some writing, this was the default
42 my ($conn, $msg) = @_;
43 unless ($msg =~ /^[ABZ]/) {
44 if ($msg =~ /^E[-\w]+\|([01])/ && $conn->{csort} eq 'telnet') {
47 # $conn->send_raw("\xFF\xFC\x01");
49 # $conn->send_raw("\xFF\xFB\x01");
52 $msg =~ s/^[-\w]+\|//;
53 push (@{$conn->{outqueue}}, $msg . $conn->{lineend});
60 my ($conn, $msg) = @_;
61 my $sock = $conn->{sock};
62 return unless defined($sock);
63 push (@{$conn->{outqueue}}, $msg);
64 dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
65 Msg::set_event_handler ($sock, "write" => sub {$conn->_send(0)});
71 $conn->{echo} = shift;
79 if ($conn->{csort} eq 'ax25' && exists $conn->{msg}) {
80 $conn->{msg} =~ s/\cM/\cJ/g;
82 if ($conn->{state} eq 'WC') {
83 if (exists $conn->{cmd}) {
84 if (@{$conn->{cmd}}) {
85 dbg("connect $conn->{cnum}: $conn->{msg}") if isdbg('connect');
86 $conn->_docmd($conn->{msg});
89 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
90 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
92 } elsif ($conn->{msg} =~ /\cJ/) {
93 my @lines = $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
94 if ($conn->{msg} =~ /\cJ$/) {
97 $conn->{msg} =~ s/([^\cM\cJ]*)\cM?\cJ//g;
99 while (defined ($msg = shift @lines)) {
100 dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
102 $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
103 # $msg =~ s/[\x00-\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
105 if ($conn->{state} eq 'C') {
106 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
107 } elsif ($conn->{state} eq 'WL' ) {
109 if (is_callsign($msg) && $msg !~ m|/| ) {
110 my $sort = $conn->{csort};
111 $sort = 'local' if $conn->{peerhost} eq "127.0.0.1";
113 if ($main::passwdreq || ($uref = DXUser->get_current($msg)) && $uref->passwd ) {
115 $conn->{state} = 'WP';
116 $conn->{decho} = $conn->{echo};
118 $conn->send_raw('password: ');
120 $conn->to_connected($msg, 'A', $sort);
123 $conn->send_now("Sorry $msg is an invalid callsign");
126 } elsif ($conn->{state} eq 'WP' ) {
127 my $uref = DXUser->get_current($conn->{call});
128 $msg =~ s/[\r\n]+$//;
129 if ($uref && $msg eq $uref->passwd) {
130 my $sort = $conn->{csort};
131 $conn->{echo} = $conn->{decho};
132 delete $conn->{decho};
133 $sort = 'local' if $conn->{peerhost} eq "127.0.0.1";
134 $conn->{usedpasswd} = 1;
135 $conn->to_connected($conn->{call}, 'A', $sort);
137 $conn->send_now("Sorry");
140 } elsif ($conn->{state} eq 'WC') {
141 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
143 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
144 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
154 my ($conn, $call, $dir, $sort) = @_;
155 $conn->{state} = 'C';
158 $conn->{timeout}->del if $conn->{timeout};
159 delete $conn->{timeout};
161 &{$conn->{rproc}}($conn, "$dir$call|$sort");
162 $conn->_send_file("$main::data/connected") unless $conn->{outgoing};
166 my $server_conn = shift;
167 my $sock = $server_conn->{sock}->accept();
169 my $conn = $server_conn->new($server_conn->{rproc});
170 $conn->{sock} = $sock;
172 Msg::blocking($sock, 0);
173 $conn->{blocking} = 0;
174 eval {$conn->{peerhost} = $sock->peerhost};
176 dbg($@) if isdbg('connll');
179 eval {$conn->{peerport} = $sock->peerport};
180 $conn->{peerport} = 0 if $@;
181 my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost}, $conn->{peerport});
182 dbg("accept $conn->{cnum} from $conn->{peerhost} $conn->{peerport}") if isdbg('connll');
184 $conn->{eproc} = $eproc;
185 Msg::set_event_handler ($sock, "error" => $eproc);
188 $conn->{rproc} = $rproc;
189 my $callback = sub {$conn->_rcv};
190 Msg::set_event_handler ($sock, "read" => $callback);
192 $conn->{state} = 'WL';
193 # $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
194 # $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
195 # $conn->send_raw("\xFF\xFC\x01");
196 $conn->_send_file("$main::data/issue");
197 $conn->send_raw("login: ");
198 $conn->_dotimeout(60);
201 &{$conn->{eproc}}() if $conn->{eproc};
206 dbg("ExtMsg: error on accept ($!)") if isdbg('err');
214 my $conn = ExtMsg->new(\&main::new_channel);
215 $conn->{outgoing} = 1;
218 my $f = new IO::File $fn;
219 push @{$conn->{cmd}}, <$f>;
221 $conn->{state} = 'WC';
222 $conn->_dotimeout($deftimeout);
232 while ($cmd = shift @{$conn->{cmd}}) {
234 next if $cmd =~ /^\s*\#/o;
235 next if $cmd =~ /^\s*$/o;
236 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
237 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
238 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
239 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
240 unless ($conn->_doconnect($1, $2)) {
242 @{$conn->{cmd}} = []; # empty any further commands
246 if ($cmd =~ /^\s*\'([^\']*)\'\s+\'([^\']*)\'/) {
247 $conn->_dochat($cmd, $msg, $1, $2);
250 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
251 $conn->_doclient($1);
254 last if $conn->{state} eq 'E';
260 my ($conn, $sort, $line) = @_;
264 dbg("CONNECT $conn->{cnum} sort: $sort command: $line") if isdbg('connect');
265 if ($sort eq 'telnet') {
266 # this is a straight network connect
267 my ($host, $port) = split /\s+/, $line;
268 $port = 23 if !$port;
269 $r = $conn->connect($host, $port);
271 dbg("Connected $conn->{cnum} to $host $port") if isdbg('connect');
273 dbg("***Connect $conn->{cnum} Failed to $host $port $!") if isdbg('connect');
275 } elsif ($sort eq 'agw') {
276 # turn it into an AGW object
277 bless $conn, 'AGWMsg';
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 =~ /\Q$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});