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) {
69 $conn->to_connected($conn->{call}, 'O', 'telnet');
71 } elsif ($conn->{msg} =~ /\cJ/) {
72 my @lines = $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
73 if ($conn->{msg} =~ /\cJ$/) {
76 $conn->{msg} = pop @lines;
78 while (defined ($msg = shift @lines)) {
79 dbg('connect', $msg) unless $conn->{state} eq 'C';
81 $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
82 $msg =~ s/[\x00-\x08\x0a-\x1f\x80-\x9f]/./g; # immutable CSI sequence + control characters
84 if ($conn->{state} eq 'C') {
85 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
86 } elsif ($conn->{state} eq 'WL' ) {
88 if (is_callsign($msg)) {
89 $conn->to_connected($msg, 'A', 'telnet');
91 $conn->send_now("Sorry $msg is an invalid callsign");
94 } elsif ($conn->{state} eq 'WC') {
95 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
97 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
98 $conn->to_connected($conn->{call}, 'O', 'telnet');
108 my ($conn, $call, $dir, $sort) = @_;
109 $conn->{state} = 'C';
112 $conn->{timeout}->del if $conn->{timeout};
113 delete $conn->{timeout};
114 $conn->_send_file("$main::data/connected");
115 &{$conn->{rproc}}($conn, "$dir$call|$sort");
119 my $server_conn = shift;
120 my $sock = $server_conn->{sock}->accept();
121 my $conn = $server_conn->new($server_conn->{rproc});
122 $conn->{sock} = $sock;
124 my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost} = $sock->peerhost(), $conn->{peerport} = $sock->peerport());
126 $conn->{eproc} = $eproc;
127 Msg::set_event_handler ($sock, "error" => $eproc);
130 $conn->{rproc} = $rproc;
131 my $callback = sub {$conn->_rcv};
132 Msg::set_event_handler ($sock, "read" => $callback);
134 $conn->{state} = 'WL';
135 # $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
136 # $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
137 # $conn->send_raw("\xFF\xFC\x01");
138 $conn->_send_file("$main::data/issue");
139 $conn->send_raw("login: ");
140 $conn->_dotimeout(60);
150 my $conn = ExtMsg->new(\&main::new_channel);
153 my $f = new IO::File $fn;
154 push @{$conn->{cmd}}, <$f>;
156 $conn->_dotimeout($deftimeout);
166 while ($cmd = shift @{$conn->{cmd}}) {
168 next if $cmd =~ /^\s*\#/o;
169 next if $cmd =~ /^\s*$/o;
170 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
171 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
172 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
173 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
174 unless ($conn->_doconnect($1, $2)) {
176 @{$conn->{cmd}} = []; # empty any further commands
180 if ($cmd =~ /^\s*\'.*\'\s+\'.*\'/i) {
181 $conn->_dochat($cmd, $msg);
184 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
185 $conn->_doclient($1);
188 last if $conn->{state} eq 'E';
194 my ($conn, $sort, $line) = @_;
197 dbg('connect', "CONNECT sort: $sort command: $line");
198 if ($sort eq 'telnet') {
199 # this is a straight network connect
200 my ($host, $port) = split /\s+/, $line;
201 $port = 23 if !$port;
202 $r = $conn->connect($host, $port);
204 dbg('connect', "Connected to $host $port");
206 dbg('connect', "***Connect Failed to $host $port $!");
208 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
211 dbg('err', "invalid type of connection ($sort)");
221 dbg('connect', "abort $string");
222 $conn->{abort} = $string;
229 dbg('connect', "timeout set to $val");
230 $conn->{timeout}->del if $conn->{timeout};
231 $conn->{timeval} = $val;
232 $conn->{timeout} = Timer->new($val, sub{ &_timedout($conn) });
239 dbg('connect', "lineend set to $val ");
242 $conn->{lineend} = $val;
252 my ($expect, $send) = $cmd =~ /^\s*\'(.*)\'\s+\'(.*)\'/;
254 dbg('connect', "expecting: \"$expect\" received: \"$line\"");
255 if ($conn->{abort} && $line =~ /$conn->{abort}/i) {
256 dbg('connect', "aborted on /$conn->{abort}/");
261 if ($line =~ /$expect/i) {
262 dbg('connect', "got: \"$expect\" sending: \"$send\"");
263 $conn->send_later($send);
264 delete $conn->{msg}; # get rid any input if a match
269 $conn->{state} = 'WC';
270 unshift @{$conn->{cmd}}, $cmd;
276 dbg('connect', "timed out after $conn->{timeval} seconds");
277 $conn->{timeout}->del;
278 delete $conn->{timeout};
282 # handle callsign and connection type firtling
287 my @f = split /\s+/, $line;
288 my $call = uc $f[0] if $f[0];
290 $conn->{csort} = $f[1] if $f[1];
291 $conn->{state} = 'C';
292 &{$conn->{rproc}}($conn, "O$call|telnet");
294 $conn->{timeout}->del if $conn->{timeout};
303 my $f = new IO::File $fn;
307 $conn->send_raw($_ . $conn->{lineend});