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 $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 while ($msg = shift @{$conn->{inqueue}}){
62 dbg('connect', $msg) unless $conn->{state} eq 'C';
64 $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
65 $msg =~ s/[\x00-\x08\x0a-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
67 if ($conn->{state} eq 'C') {
68 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg", $!);
70 } elsif ($conn->{state} eq 'WL' ) {
72 if (is_callsign($msg)) {
73 _send_file($conn, "$main::data/connected");
75 &{$conn->{rproc}}($conn, "A$conn->{call}|telnet");
78 $conn->send_now("Sorry $msg is an invalid callsign");
81 } elsif ($conn->{state} eq 'WC') {
82 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
84 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
86 &{$conn->{rproc}}($conn, "O$conn->{call}|telnet");
88 $conn->{timeout}->del_timer if $conn->{timeout};
93 if ($conn->{msg} && $conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}}) {
94 $conn->_docmd($conn->{msg});
95 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
97 &{$conn->{rproc}}($conn, "O$conn->{call}|telnet");
99 $conn->{timeout}->del_timer if $conn->{timeout};
105 my $server_conn = shift;
106 my $sock = $server_conn->{sock}->accept();
107 my $conn = $server_conn->new($server_conn->{rproc});
108 $conn->{sock} = $sock;
110 my $rproc = &{$server_conn->{rproc}} ($conn, $sock->peerhost(), $sock->peerport());
112 $conn->{rproc} = $rproc;
113 my $callback = sub {$conn->_rcv};
114 Msg::set_event_handler ($sock, "read" => $callback);
116 $conn->{state} = 'WL';
117 # $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
118 # $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
119 $conn->send_raw("\xFF\xFC\x01");
120 _send_file($conn, "$main::data/issue");
121 $conn->send_raw("Login: ");
131 my $conn = ExtMsg->new(\&main::rec);
132 $conn->{call} = $call;
134 my $f = new IO::File $fn;
135 push @{$conn->{cmd}}, <$f>;
137 push @main::outstanding_connects, {call => $call, conn => $conn};
138 $conn->_dotimeout($deftimeout);
148 while ($cmd = shift @{$conn->{cmd}}) {
150 next if $cmd =~ /^\s*\#/o;
151 next if $cmd =~ /^\s*$/o;
152 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
153 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
154 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
155 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
156 unless ($conn->_doconnect($1, $2)) {
158 @{$conn->{cmd}} = []; # empty any further commands
162 if ($cmd =~ /^\s*\'.*\'\s+\'.*\'/i) {
163 $conn->_dochat($cmd, $msg);
166 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
167 $conn->_doclient($1);
170 last if $conn->{state} eq 'E';
172 unless (exists $conn->{cmd} && @{$conn->{cmd}}) {
173 @main::outstanding_connects = grep {$_->{call} ne $conn->{call}} @main::outstanding_connects;
179 my ($conn, $sort, $line) = @_;
182 dbg('connect', "CONNECT sort: $sort command: $line");
183 if ($sort eq 'telnet') {
184 # this is a straight network connect
185 my ($host, $port) = split /\s+/, $line;
186 $port = 23 if !$port;
187 $r = $conn->connect($host, $port);
189 dbg('connect', "Connected to $host $port");
191 dbg('connect', "***Connect Failed to $host $port $!");
193 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
196 dbg('err', "invalid type of connection ($sort)");
206 dbg('connect', "abort $string");
207 $conn->{abort} = $string;
214 dbg('connect', "timeout set to $val");
215 $conn->{timeout}->del_timer if $conn->{timeout};
216 $conn->{timeout} = ExtMsg->new_timer($val, sub{ _timeout($conn); });
217 $conn->{timeval} = $val;
224 dbg('connect', "lineend set to $val ");
227 $conn->{lineend} = $val;
237 my ($expect, $send) = $cmd =~ /^\s*\'(.*)\'\s+\'(.*)\'/;
239 dbg('connect', "expecting: \"$expect\" received: \"$line\"");
240 if ($conn->{abort} && $line =~ /$conn->{abort}/i) {
241 dbg('connect', "aborted on /$conn->{abort}/");
246 if ($line =~ /$expect/i) {
247 dbg('connect', "got: \"$expect\" sending: \"$send\"");
248 $conn->send_later($send);
253 $conn->{state} = 'WC';
254 unshift @{$conn->{cmd}}, $cmd;
260 dbg('connect', "timed out after $conn->{timeval} seconds");
262 @main::outstanding_connects = grep {$_->{call} ne $conn->{call}} @main::outstanding_connects;
265 # handle callsign and connection type firtling
270 my @f = split /\s+/, $line;
271 $conn->{call} = uc $f[0] if $f[0];
272 $conn->{csort} = $f[1] if $f[1];
273 $conn->{state} = 'C';
274 &{$conn->{rproc}}($conn, "O$conn->{call}|telnet");
276 $conn->{timeout}->del_timer if $conn->{timeout};
285 my $f = new IO::File $fn;
289 $conn->send_later($_);