fix simulanious connections
[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         while (@{$conn->{inqueue}}){
62                 $msg = shift @{$conn->{inqueue}};
63                 dbg('connect', $msg) unless $conn->{state} eq 'C';
64                 
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
67
68                 if ($conn->{state} eq 'C') {
69                         &{$conn->{rproc}}($conn, "I$conn->{call}|$msg", $!);
70                         $! = 0;
71                 } elsif ($conn->{state} eq 'WL' ) {
72                         $msg = uc $msg;
73                         if (is_callsign($msg)) {
74                                 &{$conn->{rproc}}($conn, "A$msg|telnet");
75                                 _send_file($conn, "$main::data/connected");
76                                 $conn->{state} = 'C';
77                         } else {
78                                 $conn->send_now("Sorry $msg is an invalid callsign");
79                                 $conn->disconnect;
80                         }
81                 } elsif ($conn->{state} eq 'WC') {
82                         if (exists $conn->{cmd} && @{$conn->{cmd}}) {
83                                 $conn->_docmd($msg);
84                                 if ($conn->{state} eq 'WC' && exists $conn->{cmd} &&  @{$conn->{cmd}} == 0) {
85                                         $conn->{state} = 'C';
86                                         &{$conn->{rproc}}($conn, "O$conn->{call}|telnet");
87                                         delete $conn->{cmd};
88                                         $conn->{timeout}->del_timer if $conn->{timeout};
89                                 }
90                         }
91                 }
92         }
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) {
97                         $conn->{state} = 'C';
98                         &{$conn->{rproc}}($conn, "O$conn->{call}|telnet");
99                         delete $conn->{cmd};
100                         $conn->{timeout}->del_timer if $conn->{timeout};
101                 }
102         }
103 }
104
105 sub new_client {
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;
110
111     my $rproc = &{$server_conn->{rproc}} ($conn, $sock->peerhost(), $sock->peerport());
112     if ($rproc) {
113         $conn->{rproc} = $rproc;
114         my $callback = sub {$conn->_rcv};
115                 Msg::set_event_handler ($sock, "read" => $callback);
116                 # send login prompt
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: ");
123     } else { 
124         $conn->disconnect();
125     }
126 }
127
128 sub start_connect
129 {
130         my $call = shift;
131         my $fn = shift;
132         my $conn = ExtMsg->new(\&main::rec); 
133         $conn->conns($call);
134         
135         my $f = new IO::File $fn;
136         push @{$conn->{cmd}}, <$f>;
137         $f->close;
138         $conn->_dotimeout($deftimeout);
139         $conn->_docmd;
140 }
141
142 sub _docmd
143 {
144         my $conn = shift;
145         my $msg = shift;
146         my $cmd;
147
148         while ($cmd = shift @{$conn->{cmd}}) {
149                 chomp $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)) {
157                                 $conn->disconnect;
158                                 @{$conn->{cmd}} = [];    # empty any further commands
159                                 last;
160                         }  
161                 }
162                 if ($cmd =~ /^\s*\'.*\'\s+\'.*\'/i) {
163                         $conn->_dochat($cmd, $msg);
164                         last;
165                 }
166                 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
167                         $conn->_doclient($1);
168                         last;
169                 }
170                 last if $conn->{state} eq 'E';
171         }
172 }
173
174 sub _doconnect
175 {
176         my ($conn, $sort, $line) = @_;
177         my $r;
178         
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);
185                 if ($r) {
186                         dbg('connect', "Connected to $host $port");
187                 } else {
188                         dbg('connect', "***Connect Failed to $host $port $!");
189                 }
190         } elsif ($sort eq 'ax25' || $sort eq 'prog') {
191                 ;
192         } else {
193                 dbg('err', "invalid type of connection ($sort)");
194                 $conn->disconnect;
195         }
196         return $r;
197 }
198
199 sub _doabort
200 {
201         my $conn = shift;
202         my $string = shift;
203         dbg('connect', "abort $string");
204         $conn->{abort} = $string;
205 }
206
207 sub _dotimeout
208 {
209         my $conn = shift;
210         my $val = shift;
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;
215 }
216
217 sub _dolineend
218 {
219         my $conn = shift;
220         my $val = shift;
221         dbg('connect', "lineend set to $val ");
222         $val =~ s/\\r/\r/g;
223         $val =~ s/\\n/\n/g;
224         $conn->{lineend} = $val;
225 }
226
227 sub _dochat
228 {
229         my $conn = shift;
230         my $cmd = shift;
231         my $line = shift;
232         
233         if ($line) {
234                 my ($expect, $send) = $cmd =~ /^\s*\'(.*)\'\s+\'(.*)\'/;
235                 if ($expect) {
236                         dbg('connect', "expecting: \"$expect\" received: \"$line\"");
237                         if ($conn->{abort} && $line =~ /$conn->{abort}/i) {
238                                 dbg('connect', "aborted on /$conn->{abort}/");
239                                 $conn->disconnect;
240                                 delete $conn->{cmd};
241                                 return;
242                         }
243                         if ($line =~ /$expect/i) {
244                                 dbg('connect', "got: \"$expect\" sending: \"$send\"");
245                                 $conn->send_later($send);
246                                 return;
247                         }
248                 }
249         }
250         $conn->{state} = 'WC';
251         unshift @{$conn->{cmd}}, $cmd;
252 }
253
254 sub _timeout
255 {
256         my $conn = shift;
257         dbg('connect', "timed out after $conn->{timeval} seconds");
258         $conn->disconnect;
259 }
260
261 # handle callsign and connection type firtling
262 sub _doclient
263 {
264         my $conn = shift;
265         my $line = shift;
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");
271         delete $conn->{cmd};
272         $conn->{timeout}->del_timer if $conn->{timeout};
273 }
274
275 sub _send_file
276 {
277         my $conn = shift;
278         my $fn = shift;
279         
280         if (-e $fn) {
281                 my $f = new IO::File $fn;
282                 if ($f) {
283                         while (<$f>) {
284                                 chomp;
285                                 $conn->send_raw($_ . $conn->{lineend});
286                         }
287                         $f->close;
288                 }
289         }
290         $! = undef;
291 }