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])/) {
35 $msg =~ s/^[-\w]+\|//;
36 push (@{$conn->{outqueue}}, $msg . $conn->{lineend});
43 my ($conn, $msg) = @_;
44 my $sock = $conn->{sock};
45 return unless defined($sock);
46 push (@{$conn->{outqueue}}, $msg);
47 dbg('connect', $msg) unless $conn->{state} eq 'C';
48 Msg::set_event_handler ($sock, "write" => sub {$conn->_send(0)});
56 while ($msg = shift @{$conn->{inqueue}}){
57 dbg('connect', $msg) unless $conn->{state} eq 'C';
59 $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
60 $msg =~ s/[\x00-\x08\x0a-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
62 if ($conn->{state} eq 'C') {
63 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg", $!);
65 } elsif ($conn->{state} eq 'WL' ) {
67 if (is_callsign($msg)) {
68 _send_file($conn, "$main::data/connected");
70 &{$conn->{rproc}}($conn, "A$conn->{call}|telnet");
73 $conn->send_now("Sorry $msg is an invalid callsign");
76 } elsif ($conn->{state} eq 'WC') {
77 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
79 unless (exists $conn->{cmd} && @{$conn->{cmd}}) {
81 &{$conn->{rproc}}($conn, "O$conn->{call}|telnet");
83 $conn->{timeout}->del_timer if $conn->{timeout};
88 if ($conn->{msg} && $conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}}) {
89 $conn->_docmd($conn->{msg});
90 unless (@{$conn->{cmd}}) {
92 &{$conn->{rproc}}($conn, "O$conn->{call}|telnet");
94 $conn->{timeout}->del_timer if $conn->{timeout};
100 my $server_conn = shift;
101 my $sock = $server_conn->{sock}->accept();
102 my $conn = $server_conn->new($server_conn->{rproc});
103 $conn->{sock} = $sock;
105 my $rproc = &{$server_conn->{rproc}} ($conn, $sock->peerhost(), $sock->peerport());
107 $conn->{rproc} = $rproc;
108 my $callback = sub {$conn->_rcv};
109 Msg::set_event_handler ($sock, "read" => $callback);
111 $conn->{state} = 'WL';
112 # $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
113 # $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
114 _send_file($conn, "$main::data/issue");
115 $conn->send_raw("Login: ");
125 my $conn = ExtMsg->new(\&main::rec);
126 $conn->{call} = $call;
128 my $f = new IO::File $fn;
129 push @{$conn->{cmd}}, <$f>;
131 push @main::outstanding_connects, {call => $call, conn => $conn};
132 $conn->_dotimeout($deftimeout);
142 while ($cmd = shift @{$conn->{cmd}}) {
144 next if $cmd =~ /^\s*\#/o;
145 next if $cmd =~ /^\s*$/o;
146 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
147 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
148 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
149 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
150 unless ($conn->_doconnect($1, $2)) {
152 @{$conn->{cmd}} = []; # empty any further commands
156 if ($cmd =~ /^\s*\'.*\'\s+\'.*\'/i) {
157 $conn->_dochat($cmd, $msg);
160 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
161 $conn->_doclient($1);
164 last if $conn->{state} eq 'E';
166 unless (exists $conn->{cmd} && @{$conn->{cmd}}) {
167 @main::outstanding_connects = grep {$_->{call} ne $conn->{call}} @main::outstanding_connects;
173 my ($conn, $sort, $line) = @_;
176 dbg('connect', "CONNECT sort: $sort command: $line");
177 if ($sort eq 'telnet') {
178 # this is a straight network connect
179 my ($host, $port) = split /\s+/, $line;
180 $port = 23 if !$port;
181 $r = $conn->connect($host, $port);
183 dbg('connect', "Connected to $host $port");
185 dbg('connect', "***Connect Failed to $host $port $!");
187 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
190 dbg('err', "invalid type of connection ($sort)");
200 dbg('connect', "abort $string");
201 $conn->{abort} = $string;
208 dbg('connect', "timeout set to $val");
209 $conn->{timeout}->del_timer if $conn->{timeout};
210 $conn->{timeout} = ExtMsg->new_timer($val, sub{ _timeout($conn); });
211 $conn->{timeval} = $val;
218 dbg('connect', "lineend set to $val ");
221 $conn->{lineend} = $val;
231 my ($expect, $send) = $cmd =~ /^\s*\'(.*)\'\s+\'(.*)\'/;
233 dbg('connect', "expecting: \"$expect\" received: \"$line\"");
234 if ($conn->{abort} && $line =~ /$conn->{abort}/i) {
235 dbg('connect', "aborted on /$conn->{abort}/");
239 if ($line =~ /$expect/i) {
240 dbg('connect', "got: \"$expect\" sending: \"$send\"");
241 $conn->send_later($send);
246 $conn->{state} = 'WC';
247 unshift @{$conn->{cmd}}, $cmd;
253 dbg('connect', "timed out after $conn->{timeval} seconds");
255 @main::outstanding_connects = grep {$_->{call} ne $conn->{call}} @main::outstanding_connects;
258 # handle callsign and connection type firtling
263 my @f = split /\s+/, $line;
264 $conn->{call} = uc $f[0] if $f[0];
265 $conn->{csort} = $f[1] if $f[1];
274 my $f = new IO::File $fn;
278 $conn->send_later($_);