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(@ISA $deftimeout);
31 goto &main::login; # save some writing, this was the default
36 my ($conn, $msg) = @_;
37 unless ($msg =~ /^[ABZ]/) {
38 if ($msg =~ /^E[-\w]+\|([01])/ && $conn->{csort} eq 'telnet') {
41 # $conn->send_raw("\xFF\xFC\x01");
43 # $conn->send_raw("\xFF\xFB\x01");
46 $msg =~ s/^[-\w]+\|//;
47 push (@{$conn->{outqueue}}, $msg . $conn->{lineend});
54 my ($conn, $msg) = @_;
55 my $sock = $conn->{sock};
56 return unless defined($sock);
57 push (@{$conn->{outqueue}}, $msg);
58 dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
59 Msg::set_event_handler ($sock, "write" => sub {$conn->_send(0)});
65 $conn->{echo} = shift;
73 if ($conn->{csort} eq 'ax25' && exists $conn->{msg}) {
74 $conn->{msg} =~ s/\cM/\cJ/g;
76 if ($conn->{state} eq 'WC') {
77 if (exists $conn->{cmd}) {
78 if (@{$conn->{cmd}}) {
79 dbg("connect $conn->{cnum}: $conn->{msg}") if isdbg('connect');
80 $conn->_docmd($conn->{msg});
83 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
84 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
86 } elsif ($conn->{msg} =~ /\cJ/) {
87 my @lines = $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
88 if ($conn->{msg} =~ /\cJ$/) {
91 $conn->{msg} =~ s/([^\cM\cJ]*)\cM?\cJ//g;
93 while (defined ($msg = shift @lines)) {
94 dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
96 $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
97 # $msg =~ s/[\x00-\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
99 if ($conn->{state} eq 'C') {
100 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
101 } elsif ($conn->{state} eq 'WL' ) {
103 if (is_callsign($msg) && $msg !~ m|/| ) {
104 my $sort = $conn->{csort};
105 $sort = 'local' if $conn->{peerhost} eq "127.0.0.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");
120 } elsif ($conn->{state} eq 'WP' ) {
121 my $uref = DXUser->get_current($conn->{call});
122 $msg =~ s/[\r\n]+$//;
123 if ($uref && $msg eq $uref->passwd) {
124 my $sort = $conn->{csort};
125 $conn->{echo} = $conn->{decho};
126 delete $conn->{decho};
127 $sort = 'local' if $conn->{peerhost} eq "127.0.0.1";
128 $conn->{usedpasswd} = 1;
129 $conn->to_connected($conn->{call}, 'A', $sort);
131 $conn->send_now("Sorry");
134 } elsif ($conn->{state} eq 'WC') {
135 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
137 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
138 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
148 my ($conn, $call, $dir, $sort) = @_;
149 $conn->{state} = 'C';
152 $conn->{timeout}->del if $conn->{timeout};
153 delete $conn->{timeout};
155 &{$conn->{rproc}}($conn, "$dir$call|$sort");
156 $conn->_send_file("$main::data/connected") unless $conn->{outgoing};
160 my $server_conn = shift;
161 my $sock = $server_conn->{sock}->accept();
163 my $conn = $server_conn->new($server_conn->{rproc});
164 $conn->{sock} = $sock;
166 Msg::blocking($sock, 0);
167 $conn->{blocking} = 0;
168 eval {$conn->{peerhost} = $sock->peerhost};
170 dbg($@) if isdbg('connll');
173 eval {$conn->{peerport} = $sock->peerport};
174 $conn->{peerport} = 0 if $@;
175 my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost}, $conn->{peerport});
176 dbg("accept $conn->{cnum} from $conn->{peerhost} $conn->{peerport}") if isdbg('connll');
178 $conn->{eproc} = $eproc;
179 Msg::set_event_handler ($sock, "error" => $eproc);
182 $conn->{rproc} = $rproc;
183 my $callback = sub {$conn->_rcv};
184 Msg::set_event_handler ($sock, "read" => $callback);
186 $conn->{state} = 'WL';
187 # $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
188 # $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
189 # $conn->send_raw("\xFF\xFC\x01");
190 $conn->_send_file("$main::data/issue");
191 $conn->send_raw("login: ");
192 $conn->_dotimeout(60);
195 &{$conn->{eproc}}() if $conn->{eproc};
200 dbg("ExtMsg: error on accept ($!)") if isdbg('err');
208 my $conn = ExtMsg->new(\&main::new_channel);
209 $conn->{outgoing} = 1;
212 my $f = new IO::File $fn;
213 push @{$conn->{cmd}}, <$f>;
215 $conn->{state} = 'WC';
216 $conn->_dotimeout($deftimeout);
226 while ($cmd = shift @{$conn->{cmd}}) {
228 next if $cmd =~ /^\s*\#/o;
229 next if $cmd =~ /^\s*$/o;
230 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
231 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
232 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
233 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
234 unless ($conn->_doconnect($1, $2)) {
236 @{$conn->{cmd}} = []; # empty any further commands
240 if ($cmd =~ /^\s*\'([^\']*)\'\s+\'([^\']*)\'/) {
241 $conn->_dochat($cmd, $msg, $1, $2);
244 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
245 $conn->_doclient($1);
248 last if $conn->{state} eq 'E';
254 my ($conn, $sort, $line) = @_;
258 dbg("CONNECT $conn->{cnum} sort: $sort command: $line") if isdbg('connect');
259 if ($sort eq 'telnet') {
260 # this is a straight network connect
261 my ($host, $port) = split /\s+/, $line;
262 $port = 23 if !$port;
263 $r = $conn->connect($host, $port);
265 dbg("Connected $conn->{cnum} to $host $port") if isdbg('connect');
267 dbg("***Connect $conn->{cnum} Failed to $host $port $!") if isdbg('connect');
269 } elsif ($sort eq 'agw') {
270 # turn it into an AGW object
271 bless $conn, 'AGWMsg';
272 $r = $conn->connect($line);
273 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
274 $r = $conn->start_program($line, $sort);
276 dbg("invalid type of connection ($sort)");
278 $conn->disconnect unless $r;
286 dbg("connect $conn->{cnum}: abort $string") if isdbg('connect');
287 $conn->{abort} = $string;
294 dbg("connect $conn->{cnum}: timeout set to $val") if isdbg('connect');
295 $conn->{timeout}->del if $conn->{timeout};
296 $conn->{timeval} = $val;
297 $conn->{timeout} = Timer->new($val, sub{ &_timedout($conn) });
304 dbg("connect $conn->{cnum}: lineend set to $val ") if isdbg('connect');
307 $conn->{lineend} = $val;
320 dbg("connect $conn->{cnum}: expecting: \"$expect\" received: \"$line\"") if isdbg('connect');
321 if ($conn->{abort} && $line =~ /\Q$conn->{abort}/i) {
322 dbg("connect $conn->{cnum}: aborted on /$conn->{abort}/") if isdbg('connect');
327 if ($line =~ /\Q$expect/i) {
329 dbg("connect $conn->{cnum}: got: \"$expect\" sending: \"$send\"") if isdbg('connect');
330 $conn->send_later("D$conn->{call}|$send");
332 delete $conn->{msg}; # get rid any input if a match
337 $conn->{state} = 'WC';
338 unshift @{$conn->{cmd}}, $cmd;
344 dbg("connect $conn->{cnum}: timed out after $conn->{timeval} seconds") if isdbg('connect');
348 # handle callsign and connection type firtling
353 my @f = split /\s+/, $line;
354 my $call = uc $f[0] if $f[0];
356 $conn->{csort} = $f[1] if $f[1];
357 $conn->{state} = 'C';
358 &{$conn->{rproc}}($conn, "O$call|$conn->{csort}");
360 $conn->{timeout}->del if $conn->{timeout};
369 my $f = new IO::File $fn;
374 dbg("connect $conn->{cnum}: $l") if isdbg('connll');
375 $conn->send_raw($l . $conn->{lineend});