do non blocking connects
[spider.git] / perl / ExtMsg.pm
1 #
2 # This class is the internal subclass that deals with the external port
3 # communications for Msg.pm
4 #
5 # This is where the cluster handles direct connections coming both in
6 # and out
7 #
8 # $Id$
9 #
10 # Copyright (c) 2001 - Dirk Koopman G1TLH
11 #
12
13 package ExtMsg;
14
15 use strict;
16 use Msg;
17 use DXVars;
18 use DXUtil;
19 use DXDebug;
20 use IO::File;
21 use IO::Socket;
22
23 use vars qw(@ISA $deftimeout);
24
25 @ISA = qw(Msg);
26 $deftimeout = 60;
27
28 sub enqueue
29 {
30         my ($conn, $msg) = @_;
31         unless ($msg =~ /^[ABZ]/) {
32                 if ($msg =~ /^E[-\w]+\|([01])/ && $conn->{csort} eq 'telnet') {
33                         $conn->{echo} = $1;
34                         if ($1) {
35 #                               $conn->send_raw("\xFF\xFC\x01");
36                         } else {
37 #                               $conn->send_raw("\xFF\xFB\x01");
38                         }
39                 } else {
40                         $msg =~ s/^[-\w]+\|//;
41                         push (@{$conn->{outqueue}}, $msg . $conn->{lineend});
42                 }
43         }
44 }
45
46 sub send_raw
47 {
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)});
54 }
55
56 sub dequeue
57 {
58         my $conn = shift;
59         my $msg;
60
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});
66                         } 
67                 }
68                 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
69                         $conn->{state} = 'C';
70                         &{$conn->{rproc}}($conn, "O$conn->{call}|telnet");
71                         delete $conn->{cmd};
72                         $conn->{timeout}->del if $conn->{timeout};
73                 }
74         } elsif ($conn->{msg} =~ /\n/) {
75                 my @lines = split /\r?\n/, $conn->{msg};
76                 if ($conn->{msg} =~ /\n$/) {
77                         delete $conn->{msg};
78                 } else {
79                         $conn->{msg} = pop @lines;
80                 }
81                 while (defined ($msg = shift @lines)) {
82                         dbg('connect', $msg) unless $conn->{state} eq 'C';
83                 
84                         $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
85                         $msg =~ s/[\x00-\x08\x0a-\x1f\x80-\x9f]/./g;         # immutable CSI sequence + control characters
86                         
87                         if ($conn->{state} eq 'C') {
88                                 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
89                         } elsif ($conn->{state} eq 'WL' ) {
90                                 $msg = uc $msg;
91                                 if (is_callsign($msg)) {
92                                         &{$conn->{rproc}}($conn, "A$msg|telnet");
93                                         _send_file($conn, "$main::data/connected");
94                                         $conn->{state} = 'C';
95                                 } else {
96                                         $conn->send_now("Sorry $msg is an invalid callsign");
97                                         $conn->disconnect;
98                                 }
99                         } elsif ($conn->{state} eq 'WC') {
100                                 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
101                                         $conn->_docmd($msg);
102                                         if ($conn->{state} eq 'WC' && exists $conn->{cmd} &&  @{$conn->{cmd}} == 0) {
103                                                 $conn->{state} = 'C';
104                                                 &{$conn->{rproc}}($conn, "O$conn->{call}|telnet");
105                                                 delete $conn->{cmd};
106                                                 $conn->{timeout}->del if $conn->{timeout};
107                                         }
108                                 }
109                         }
110                 }
111         }
112 }
113
114 sub new_client {
115         my $server_conn = shift;
116     my $sock = $server_conn->{sock}->accept();
117     my $conn = $server_conn->new($server_conn->{rproc});
118         $conn->{sock} = $sock;
119
120     my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost} = $sock->peerhost(), $conn->{peerport} = $sock->peerport());
121         if ($eproc) {
122                 $conn->{eproc} = $eproc;
123         set_event_handler ($sock, "error" => $eproc);
124         }
125     if ($rproc) {
126         $conn->{rproc} = $rproc;
127         my $callback = sub {$conn->_rcv};
128                 Msg::set_event_handler ($sock, "read" => $callback);
129                 # send login prompt
130                 $conn->{state} = 'WL';
131 #               $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
132 #               $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
133 #               $conn->send_raw("\xFF\xFC\x01");
134                 _send_file($conn, "$main::data/issue");
135                 $conn->send_raw("login: ");
136     } else { 
137         $conn->disconnect();
138     }
139 }
140
141 sub start_connect
142 {
143         my $call = shift;
144         my $fn = shift;
145         my $conn = ExtMsg->new(\&main::rec); 
146         $conn->conns($call);
147         
148         my $f = new IO::File $fn;
149         push @{$conn->{cmd}}, <$f>;
150         $f->close;
151         $conn->_dotimeout($deftimeout);
152         $conn->_docmd;
153 }
154
155 sub _docmd
156 {
157         my $conn = shift;
158         my $msg = shift;
159         my $cmd;
160
161         while ($cmd = shift @{$conn->{cmd}}) {
162                 chomp $cmd;
163                 next if $cmd =~ /^\s*\#/o;
164                 next if $cmd =~ /^\s*$/o;
165                 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
166                 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
167                 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
168                 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
169                         unless ($conn->_doconnect($1, $2)) {
170                                 $conn->disconnect;
171                                 @{$conn->{cmd}} = [];    # empty any further commands
172                                 last;
173                         }  
174                 }
175                 if ($cmd =~ /^\s*\'.*\'\s+\'.*\'/i) {
176                         $conn->_dochat($cmd, $msg);
177                         last;
178                 }
179                 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
180                         $conn->_doclient($1);
181                         last;
182                 }
183                 last if $conn->{state} eq 'E';
184         }
185 }
186
187 sub _doconnect
188 {
189         my ($conn, $sort, $line) = @_;
190         my $r;
191         
192         dbg('connect', "CONNECT sort: $sort command: $line");
193         if ($sort eq 'telnet') {
194                 # this is a straight network connect
195                 my ($host, $port) = split /\s+/, $line;
196                 $port = 23 if !$port;
197                 $r = $conn->connect($host, $port);
198                 if ($r) {
199                         dbg('connect', "Connected to $host $port");
200                 } else {
201                         dbg('connect', "***Connect Failed to $host $port $!");
202                 }
203         } elsif ($sort eq 'ax25' || $sort eq 'prog') {
204                 ;
205         } else {
206                 dbg('err', "invalid type of connection ($sort)");
207                 $conn->disconnect;
208         }
209         return $r;
210 }
211
212 sub _doabort
213 {
214         my $conn = shift;
215         my $string = shift;
216         dbg('connect', "abort $string");
217         $conn->{abort} = $string;
218 }
219
220 sub _dotimeout
221 {
222         my $conn = shift;
223         my $val = shift;
224         dbg('connect', "timeout set to $val");
225         my $old = $conn->{timeout}->del if $conn->{timeout};
226         $conn->{timeout} = Timer->new($val, sub{ &_timeout($conn) });
227         $conn->{timeval} = $val;
228 }
229
230 sub _dolineend
231 {
232         my $conn = shift;
233         my $val = shift;
234         dbg('connect', "lineend set to $val ");
235         $val =~ s/\\r/\r/g;
236         $val =~ s/\\n/\n/g;
237         $conn->{lineend} = $val;
238 }
239
240 sub _dochat
241 {
242         my $conn = shift;
243         my $cmd = shift;
244         my $line = shift;
245         
246         if ($line) {
247                 my ($expect, $send) = $cmd =~ /^\s*\'(.*)\'\s+\'(.*)\'/;
248                 if ($expect) {
249                         dbg('connect', "expecting: \"$expect\" received: \"$line\"");
250                         if ($conn->{abort} && $line =~ /$conn->{abort}/i) {
251                                 dbg('connect', "aborted on /$conn->{abort}/");
252                                 $conn->disconnect;
253                                 delete $conn->{cmd};
254                                 return;
255                         }
256                         if ($line =~ /$expect/i) {
257                                 dbg('connect', "got: \"$expect\" sending: \"$send\"");
258                                 $conn->send_later($send);
259                                 return;
260                         }
261                 }
262         }
263         $conn->{state} = 'WC';
264         unshift @{$conn->{cmd}}, $cmd;
265 }
266
267 sub _timeout
268 {
269         my $conn = shift;
270         dbg('connect', "timed out after $conn->{timeval} seconds");
271         $conn->disconnect;
272 }
273
274 # handle callsign and connection type firtling
275 sub _doclient
276 {
277         my $conn = shift;
278         my $line = shift;
279         my @f = split /\s+/, $line;
280         $conn->{call} = uc $f[0] if $f[0];
281         $conn->{csort} = $f[1] if $f[1];
282         $conn->{state} = 'C';
283         &{$conn->{rproc}}($conn, "O$conn->{call}|telnet");
284         delete $conn->{cmd};
285         $conn->{timeout}->del if $conn->{timeout};
286 }
287
288 sub _send_file
289 {
290         my $conn = shift;
291         my $fn = shift;
292         
293         if (-e $fn) {
294                 my $f = new IO::File $fn;
295                 if ($f) {
296                         while (<$f>) {
297                                 chomp;
298                                 $conn->send_raw($_ . $conn->{lineend});
299                         }
300                         $f->close;
301                 }
302         }
303 }