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 (@{$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 _send_file($conn, "$main::data/connected");
76 &{$conn->{rproc}}($conn, "A$conn->{call}|telnet");
79 $conn->send_now("Sorry $msg is an invalid callsign");
82 } elsif ($conn->{state} eq 'WC') {
83 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
85 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
87 &{$conn->{rproc}}($conn, "O$conn->{call}|telnet");
89 $conn->{timeout}->del_timer if $conn->{timeout};
94 if ($conn->{msg} && $conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}}) {
95 dbg('connect', $conn->{msg});
96 $conn->_docmd($conn->{msg});
97 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
99 &{$conn->{rproc}}($conn, "O$conn->{call}|telnet");
101 $conn->{timeout}->del_timer if $conn->{timeout};
107 my $server_conn = shift;
108 my $sock = $server_conn->{sock}->accept();
109 my $conn = $server_conn->new($server_conn->{rproc});
110 $conn->{sock} = $sock;
112 my $rproc = &{$server_conn->{rproc}} ($conn, $sock->peerhost(), $sock->peerport());
114 $conn->{rproc} = $rproc;
115 my $callback = sub {$conn->_rcv};
116 Msg::set_event_handler ($sock, "read" => $callback);
118 $conn->{state} = 'WL';
119 # $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
120 # $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
121 $conn->send_raw("\xFF\xFC\x01");
122 _send_file($conn, "$main::data/issue");
123 $conn->send_raw("Login: ");
133 my $conn = ExtMsg->new(\&main::rec);
134 $conn->{call} = $call;
136 my $f = new IO::File $fn;
137 push @{$conn->{cmd}}, <$f>;
139 push @main::outstanding_connects, {call => $call, conn => $conn};
140 $conn->_dotimeout($deftimeout);
150 while ($cmd = shift @{$conn->{cmd}}) {
152 next if $cmd =~ /^\s*\#/o;
153 next if $cmd =~ /^\s*$/o;
154 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
155 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
156 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
157 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
158 unless ($conn->_doconnect($1, $2)) {
160 @{$conn->{cmd}} = []; # empty any further commands
164 if ($cmd =~ /^\s*\'.*\'\s+\'.*\'/i) {
165 $conn->_dochat($cmd, $msg);
168 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
169 $conn->_doclient($1);
172 last if $conn->{state} eq 'E';
174 unless (exists $conn->{cmd} && @{$conn->{cmd}}) {
175 @main::outstanding_connects = grep {$_->{call} ne $conn->{call}} @main::outstanding_connects;
181 my ($conn, $sort, $line) = @_;
184 dbg('connect', "CONNECT sort: $sort command: $line");
185 if ($sort eq 'telnet') {
186 # this is a straight network connect
187 my ($host, $port) = split /\s+/, $line;
188 $port = 23 if !$port;
189 $r = $conn->connect($host, $port);
191 dbg('connect', "Connected to $host $port");
193 dbg('connect', "***Connect Failed to $host $port $!");
195 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
198 dbg('err', "invalid type of connection ($sort)");
208 dbg('connect', "abort $string");
209 $conn->{abort} = $string;
216 dbg('connect', "timeout set to $val");
217 $conn->{timeout}->del_timer if $conn->{timeout};
218 $conn->{timeout} = ExtMsg->new_timer($val, sub{ _timeout($conn); });
219 $conn->{timeval} = $val;
226 dbg('connect', "lineend set to $val ");
229 $conn->{lineend} = $val;
239 my ($expect, $send) = $cmd =~ /^\s*\'(.*)\'\s+\'(.*)\'/;
241 dbg('connect', "expecting: \"$expect\" received: \"$line\"");
242 if ($conn->{abort} && $line =~ /$conn->{abort}/i) {
243 dbg('connect', "aborted on /$conn->{abort}/");
248 if ($line =~ /$expect/i) {
249 dbg('connect', "got: \"$expect\" sending: \"$send\"");
250 $conn->send_later($send);
255 $conn->{state} = 'WC';
256 unshift @{$conn->{cmd}}, $cmd;
262 dbg('connect', "timed out after $conn->{timeval} seconds");
264 @main::outstanding_connects = grep {$_->{call} ne $conn->{call}} @main::outstanding_connects;
267 # handle callsign and connection type firtling
272 my @f = split /\s+/, $line;
273 $conn->{call} = uc $f[0] if $f[0];
274 $conn->{csort} = $f[1] if $f[1];
275 $conn->{state} = 'C';
276 &{$conn->{rproc}}($conn, "O$conn->{call}|telnet");
278 $conn->{timeout}->del_timer if $conn->{timeout};
287 my $f = new IO::File $fn;
291 $conn->send_raw($_ . $conn->{lineend});