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 my ($conn, $msg) = @_;
38 unless ($msg =~ /^[ABZ]/) {
39 if ($msg =~ /^E[-\w]+\|([01])/ && $conn->{csort} eq 'telnet') {
42 # $conn->send_raw("\xFF\xFC\x01");
44 # $conn->send_raw("\xFF\xFB\x01");
47 $msg =~ s/^[-\w]+\|//;
48 push (@{$conn->{outqueue}}, $msg . $conn->{lineend});
55 my ($conn, $msg) = @_;
56 my $sock = $conn->{sock};
57 return unless defined($sock);
58 push (@{$conn->{outqueue}}, $msg);
59 dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
60 Msg::set_event_handler ($sock, "write" => sub {$conn->_send(0)});
66 $conn->{echo} = shift;
74 if ($conn->{csort} eq 'ax25' && exists $conn->{msg}) {
75 $conn->{msg} =~ s/\cM/\cJ/g;
77 if ($conn->{state} eq 'WC') {
78 if (exists $conn->{cmd}) {
79 if (@{$conn->{cmd}}) {
80 dbg("connect $conn->{cnum}: $conn->{msg}") if isdbg('connect');
81 $conn->_docmd($conn->{msg});
84 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
85 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
87 } elsif ($conn->{msg} =~ /\cJ/) {
88 my @lines = $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
89 if ($conn->{msg} =~ /\cJ$/) {
92 $conn->{msg} =~ s/([^\cM\cJ]*)\cM?\cJ//g;
94 while (defined ($msg = shift @lines)) {
95 dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
97 $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
98 # $msg =~ s/[\x00-\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
100 if ($conn->{state} eq 'C') {
101 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
102 } elsif ($conn->{state} eq 'WL' ) {
104 if (is_callsign($msg) && $msg !~ m|/| ) {
105 my $sort = $conn->{csort};
106 $sort = 'local' if $conn->{peerhost} eq "127.0.0.1";
108 if ($main::passwdreq || ($uref = DXUser->get_current($msg)) && $uref->passwd ) {
110 $conn->{state} = 'WP';
111 $conn->{decho} = $conn->{echo};
113 $conn->send_raw('password: ');
115 $conn->to_connected($msg, 'A', $sort);
118 $conn->send_now("Sorry $msg is an invalid callsign");
121 } elsif ($conn->{state} eq 'WP' ) {
122 my $uref = DXUser->get_current($conn->{call});
123 $msg =~ s/[\r\n]+$//;
124 if ($uref && $msg eq $uref->passwd) {
125 my $sort = $conn->{csort};
126 $conn->{echo} = $conn->{decho};
127 delete $conn->{decho};
128 $sort = 'local' if $conn->{peerhost} eq "127.0.0.1";
129 $conn->{usedpasswd} = 1;
130 $conn->to_connected($conn->{call}, 'A', $sort);
132 $conn->send_now("Sorry");
135 } elsif ($conn->{state} eq 'WC') {
136 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
138 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
139 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
149 my ($conn, $call, $dir, $sort) = @_;
150 $conn->{state} = 'C';
153 $conn->{timeout}->del if $conn->{timeout};
154 delete $conn->{timeout};
156 &{$conn->{rproc}}($conn, "$dir$call|$sort");
157 $conn->_send_file("$main::data/connected") unless $conn->{outgoing};
161 my $server_conn = shift;
162 my $sock = $server_conn->{sock}->accept();
164 my $conn = $server_conn->new($server_conn->{rproc});
165 $conn->{sock} = $sock;
167 Msg::blocking($sock, 0);
168 $conn->{blocking} = 0;
169 eval {$conn->{peerhost} = $sock->peerhost};
171 dbg($@) if isdbg('connll');
174 eval {$conn->{peerport} = $sock->peerport};
175 $conn->{peerport} = 0 if $@;
176 my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost}, $conn->{peerport});
177 dbg("accept $conn->{cnum} from $conn->{peerhost} $conn->{peerport}") if isdbg('connll');
179 $conn->{eproc} = $eproc;
180 Msg::set_event_handler ($sock, "error" => $eproc);
183 $conn->{rproc} = $rproc;
184 my $callback = sub {$conn->_rcv};
185 Msg::set_event_handler ($sock, "read" => $callback);
187 $conn->{state} = 'WL';
188 # $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
189 # $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
190 # $conn->send_raw("\xFF\xFC\x01");
191 $conn->_send_file("$main::data/issue");
192 $conn->send_raw("login: ");
193 $conn->_dotimeout(60);
196 &{$conn->{eproc}}() if $conn->{eproc};
201 dbg("ExtMsg: error on accept ($!)") if isdbg('err');
209 my $conn = ExtMsg->new(\&main::new_channel);
210 $conn->{outgoing} = 1;
213 my $f = new IO::File $fn;
214 push @{$conn->{cmd}}, <$f>;
216 $conn->{state} = 'WC';
217 $conn->_dotimeout($deftimeout);
227 while ($cmd = shift @{$conn->{cmd}}) {
229 next if $cmd =~ /^\s*\#/o;
230 next if $cmd =~ /^\s*$/o;
231 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
232 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
233 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
234 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
235 unless ($conn->_doconnect($1, $2)) {
237 @{$conn->{cmd}} = []; # empty any further commands
241 if ($cmd =~ /^\s*\'([^\']*)\'\s+\'([^\']*)\'/) {
242 $conn->_dochat($cmd, $msg, $1, $2);
245 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
246 $conn->_doclient($1);
249 last if $conn->{state} eq 'E';
255 my ($conn, $sort, $line) = @_;
259 dbg("CONNECT $conn->{cnum} sort: $sort command: $line") if isdbg('connect');
260 if ($sort eq 'telnet') {
261 # this is a straight network connect
262 my ($host, $port) = split /\s+/, $line;
263 $port = 23 if !$port;
264 $r = $conn->connect($host, $port);
266 dbg("Connected $conn->{cnum} to $host $port") if isdbg('connect');
268 dbg("***Connect $conn->{cnum} Failed to $host $port $!") if isdbg('connect');
270 } elsif ($sort eq 'agw') {
271 # turn it into an AGW object
272 bless $conn, 'AGWMsg';
273 $r = $conn->connect($line);
274 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
275 $r = $conn->start_program($line, $sort);
277 dbg("invalid type of connection ($sort)");
279 $conn->disconnect unless $r;
287 dbg("connect $conn->{cnum}: abort $string") if isdbg('connect');
288 $conn->{abort} = $string;
295 dbg("connect $conn->{cnum}: timeout set to $val") if isdbg('connect');
296 $conn->{timeout}->del if $conn->{timeout};
297 $conn->{timeval} = $val;
298 $conn->{timeout} = Timer->new($val, sub{ &_timedout($conn) });
305 dbg("connect $conn->{cnum}: lineend set to $val ") if isdbg('connect');
308 $conn->{lineend} = $val;
321 dbg("connect $conn->{cnum}: expecting: \"$expect\" received: \"$line\"") if isdbg('connect');
322 if ($conn->{abort} && $line =~ /\Q$conn->{abort}/i) {
323 dbg("connect $conn->{cnum}: aborted on /$conn->{abort}/") if isdbg('connect');
328 if ($line =~ /\Q$expect/i) {
330 dbg("connect $conn->{cnum}: got: \"$expect\" sending: \"$send\"") if isdbg('connect');
331 $conn->send_later("D$conn->{call}|$send");
333 delete $conn->{msg}; # get rid any input if a match
338 $conn->{state} = 'WC';
339 unshift @{$conn->{cmd}}, $cmd;
345 dbg("connect $conn->{cnum}: timed out after $conn->{timeval} seconds") if isdbg('connect');
349 # handle callsign and connection type firtling
354 my @f = split /\s+/, $line;
355 my $call = uc $f[0] if $f[0];
357 $conn->{csort} = $f[1] if $f[1];
358 $conn->{state} = 'C';
359 &{$conn->{rproc}}($conn, "O$call|$conn->{csort}");
361 $conn->{timeout}->del if $conn->{timeout};
370 my $f = new IO::File $fn;
375 dbg("connect $conn->{cnum}: $l") if isdbg('connll');
376 $conn->send_raw($l . $conn->{lineend});