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
23 use vars qw(@ISA $deftimeout);
30 my ($conn, $msg) = @_;
31 unless ($msg =~ /^[ABZ]/) {
32 if ($msg =~ /^E[-\w]+\|([01])/ && $conn->{csort} eq 'telnet') {
35 # $conn->send_raw("\xFF\xFC\x01");
37 # $conn->send_raw("\xFF\xFB\x01");
40 $msg =~ s/^[-\w]+\|//;
41 push (@{$conn->{outqueue}}, $msg . $conn->{lineend});
48 my ($conn, $msg) = @_;
49 my $sock = $conn->{sock};
50 return unless defined($sock);
51 push (@{$conn->{outqueue}}, $msg);
52 dbg('connect', $msg) unless $conn->{state} eq 'C';
53 Msg::set_event_handler ($sock, "write" => sub {$conn->_send(0)});
61 if ($conn->{state} eq 'WC') {
62 if (exists $conn->{cmd}) {
63 if (@{$conn->{cmd}}) {
64 dbg('connect', $conn->{msg});
65 $conn->_docmd($conn->{msg});
68 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
70 &{$conn->{rproc}}($conn, "O$conn->{call}|telnet");
72 $conn->{timeout}->del if $conn->{timeout};
74 } elsif ($conn->{msg} =~ /\n/) {
75 my @lines = split /\r?\n/, $conn->{msg};
76 if ($conn->{msg} =~ /\n$/) {
79 $conn->{msg} = pop @lines;
81 while (defined ($msg = shift @lines)) {
82 dbg('connect', $msg) unless $conn->{state} eq 'C';
84 $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
85 $msg =~ s/[\x00-\x08\x0a-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
87 if ($conn->{state} eq 'C') {
88 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
89 } elsif ($conn->{state} eq 'WL' ) {
91 if (is_callsign($msg)) {
92 &{$conn->{rproc}}($conn, "A$msg|telnet");
93 _send_file($conn, "$main::data/connected");
96 $conn->send_now("Sorry $msg is an invalid callsign");
99 } elsif ($conn->{state} eq 'WC') {
100 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
102 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
103 $conn->{state} = 'C';
104 &{$conn->{rproc}}($conn, "O$conn->{call}|telnet");
106 $conn->{timeout}->del if $conn->{timeout};
115 my $server_conn = shift;
116 my $sock = $server_conn->{sock}->accept();
117 my $conn = $server_conn->new($server_conn->{rproc});
118 $conn->{sock} = $sock;
120 my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost} = $sock->peerhost(), $conn->{peerport} = $sock->peerport());
122 $conn->{eproc} = $eproc;
123 set_event_handler ($sock, "error" => $eproc);
126 $conn->{rproc} = $rproc;
127 my $callback = sub {$conn->_rcv};
128 Msg::set_event_handler ($sock, "read" => $callback);
130 $conn->{state} = 'WL';
131 # $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
132 # $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
133 # $conn->send_raw("\xFF\xFC\x01");
134 _send_file($conn, "$main::data/issue");
135 $conn->send_raw("login: ");
145 my $conn = ExtMsg->new(\&main::rec);
148 my $f = new IO::File $fn;
149 push @{$conn->{cmd}}, <$f>;
151 $conn->_dotimeout($deftimeout);
161 while ($cmd = shift @{$conn->{cmd}}) {
163 next if $cmd =~ /^\s*\#/o;
164 next if $cmd =~ /^\s*$/o;
165 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
166 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
167 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
168 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
169 unless ($conn->_doconnect($1, $2)) {
171 @{$conn->{cmd}} = []; # empty any further commands
175 if ($cmd =~ /^\s*\'.*\'\s+\'.*\'/i) {
176 $conn->_dochat($cmd, $msg);
179 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
180 $conn->_doclient($1);
183 last if $conn->{state} eq 'E';
189 my ($conn, $sort, $line) = @_;
192 dbg('connect', "CONNECT sort: $sort command: $line");
193 if ($sort eq 'telnet') {
194 # this is a straight network connect
195 my ($host, $port) = split /\s+/, $line;
196 $port = 23 if !$port;
197 $r = $conn->connect($host, $port);
199 dbg('connect', "Connected to $host $port");
201 dbg('connect', "***Connect Failed to $host $port $!");
203 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
206 dbg('err', "invalid type of connection ($sort)");
216 dbg('connect', "abort $string");
217 $conn->{abort} = $string;
224 dbg('connect', "timeout set to $val");
225 my $old = $conn->{timeout}->del if $conn->{timeout};
226 $conn->{timeout} = Timer->new($val, sub{ &_timeout($conn) });
227 $conn->{timeval} = $val;
234 dbg('connect', "lineend set to $val ");
237 $conn->{lineend} = $val;
247 my ($expect, $send) = $cmd =~ /^\s*\'(.*)\'\s+\'(.*)\'/;
249 dbg('connect', "expecting: \"$expect\" received: \"$line\"");
250 if ($conn->{abort} && $line =~ /$conn->{abort}/i) {
251 dbg('connect', "aborted on /$conn->{abort}/");
256 if ($line =~ /$expect/i) {
257 dbg('connect', "got: \"$expect\" sending: \"$send\"");
258 $conn->send_later($send);
263 $conn->{state} = 'WC';
264 unshift @{$conn->{cmd}}, $cmd;
270 dbg('connect', "timed out after $conn->{timeval} seconds");
274 # handle callsign and connection type firtling
279 my @f = split /\s+/, $line;
280 $conn->{call} = uc $f[0] if $f[0];
281 $conn->{csort} = $f[1] if $f[1];
282 $conn->{state} = 'C';
283 &{$conn->{rproc}}($conn, "O$conn->{call}|telnet");
285 $conn->{timeout}->del if $conn->{timeout};
294 my $f = new IO::File $fn;
298 $conn->send_raw($_ . $conn->{lineend});