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 while (@{$conn->{inqueue}}){
62 $msg = shift @{$conn->{inqueue}};
63 dbg('connect', $msg) unless $conn->{state} eq 'C';
65 $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
66 $msg =~ s/[\x00-\x08\x0a-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
68 if ($conn->{state} eq 'C') {
69 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg", $!);
71 } elsif ($conn->{state} eq 'WL' ) {
73 if (is_callsign($msg)) {
74 &{$conn->{rproc}}($conn, "A$msg|telnet");
75 _send_file($conn, "$main::data/connected");
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 dbg('connect', $conn->{msg});
95 $conn->_docmd($conn->{msg});
96 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
98 &{$conn->{rproc}}($conn, "O$conn->{call}|telnet");
100 $conn->{timeout}->del_timer if $conn->{timeout};
106 my $server_conn = shift;
107 my $sock = $server_conn->{sock}->accept();
108 my $conn = $server_conn->new($server_conn->{rproc});
109 $conn->{sock} = $sock;
111 my $rproc = &{$server_conn->{rproc}} ($conn, $sock->peerhost(), $sock->peerport());
113 $conn->{rproc} = $rproc;
114 my $callback = sub {$conn->_rcv};
115 Msg::set_event_handler ($sock, "read" => $callback);
117 $conn->{state} = 'WL';
118 # $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
119 # $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
120 # $conn->send_raw("\xFF\xFC\x01");
121 _send_file($conn, "$main::data/issue");
122 $conn->send_raw("login: ");
132 my $conn = ExtMsg->new(\&main::rec);
135 my $f = new IO::File $fn;
136 push @{$conn->{cmd}}, <$f>;
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';
176 my ($conn, $sort, $line) = @_;
179 dbg('connect', "CONNECT sort: $sort command: $line");
180 if ($sort eq 'telnet') {
181 # this is a straight network connect
182 my ($host, $port) = split /\s+/, $line;
183 $port = 23 if !$port;
184 $r = $conn->connect($host, $port);
186 dbg('connect', "Connected to $host $port");
188 dbg('connect', "***Connect Failed to $host $port $!");
190 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
193 dbg('err', "invalid type of connection ($sort)");
203 dbg('connect', "abort $string");
204 $conn->{abort} = $string;
211 dbg('connect', "timeout set to $val");
212 $conn->{timeout}->del_timer if $conn->{timeout};
213 $conn->{timeout} = ExtMsg->new_timer($val, sub{ _timeout($conn); });
214 $conn->{timeval} = $val;
221 dbg('connect', "lineend set to $val ");
224 $conn->{lineend} = $val;
234 my ($expect, $send) = $cmd =~ /^\s*\'(.*)\'\s+\'(.*)\'/;
236 dbg('connect', "expecting: \"$expect\" received: \"$line\"");
237 if ($conn->{abort} && $line =~ /$conn->{abort}/i) {
238 dbg('connect', "aborted on /$conn->{abort}/");
243 if ($line =~ /$expect/i) {
244 dbg('connect', "got: \"$expect\" sending: \"$send\"");
245 $conn->send_later($send);
250 $conn->{state} = 'WC';
251 unshift @{$conn->{cmd}}, $cmd;
257 dbg('connect', "timed out after $conn->{timeval} seconds");
261 # handle callsign and connection type firtling
266 my @f = split /\s+/, $line;
267 $conn->{call} = uc $f[0] if $f[0];
268 $conn->{csort} = $f[1] if $f[1];
269 $conn->{state} = 'C';
270 &{$conn->{rproc}}($conn, "O$conn->{call}|telnet");
272 $conn->{timeout}->del_timer if $conn->{timeout};
281 my $f = new IO::File $fn;
285 $conn->send_raw($_ . $conn->{lineend});