sorted abort
[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])/) {
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 ($msg = shift @{$conn->{inqueue}}){
62                 dbg('connect', $msg) unless $conn->{state} eq 'C';
63                 
64                 $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
65                 $msg =~ s/[\x00-\x08\x0a-\x1f\x80-\x9f]/./g;         # immutable CSI sequence + control characters
66
67                 if ($conn->{state} eq 'C') {
68                         &{$conn->{rproc}}($conn, "I$conn->{call}|$msg", $!);
69                         $! = 0;
70                 } elsif ($conn->{state} eq 'WL' ) {
71                         $msg = uc $msg;
72                         if (is_callsign($msg)) {
73                                 _send_file($conn, "$main::data/connected");
74                                 $conn->{call} = $msg;
75                                 &{$conn->{rproc}}($conn, "A$conn->{call}|telnet");
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                 $conn->_docmd($conn->{msg});
95                 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
96                         $conn->{state} = 'C';
97                         &{$conn->{rproc}}($conn, "O$conn->{call}|telnet");
98                         delete $conn->{cmd};
99                         $conn->{timeout}->del_timer if $conn->{timeout};
100                 }
101         }
102 }
103
104 sub new_client {
105         my $server_conn = shift;
106     my $sock = $server_conn->{sock}->accept();
107     my $conn = $server_conn->new($server_conn->{rproc});
108         $conn->{sock} = $sock;
109
110     my $rproc = &{$server_conn->{rproc}} ($conn, $sock->peerhost(), $sock->peerport());
111     if ($rproc) {
112         $conn->{rproc} = $rproc;
113         my $callback = sub {$conn->_rcv};
114                 Msg::set_event_handler ($sock, "read" => $callback);
115                 # send login prompt
116                 $conn->{state} = 'WL';
117 #               $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
118 #               $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
119                 $conn->send_raw("\xFF\xFC\x01");
120                 _send_file($conn, "$main::data/issue");
121                 $conn->send_raw("Login: ");
122     } else { 
123         $conn->disconnect();
124     }
125 }
126
127 sub start_connect
128 {
129         my $call = shift;
130         my $fn = shift;
131         my $conn = ExtMsg->new(\&main::rec); 
132         $conn->{call} = $call;
133         
134         my $f = new IO::File $fn;
135         push @{$conn->{cmd}}, <$f>;
136         $f->close;
137         push @main::outstanding_connects, {call => $call, conn => $conn};
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         unless (exists $conn->{cmd} && @{$conn->{cmd}}) {
173                 @main::outstanding_connects = grep {$_->{call} ne $conn->{call}} @main::outstanding_connects;
174         }
175 }
176
177 sub _doconnect
178 {
179         my ($conn, $sort, $line) = @_;
180         my $r;
181         
182         dbg('connect', "CONNECT sort: $sort command: $line");
183         if ($sort eq 'telnet') {
184                 # this is a straight network connect
185                 my ($host, $port) = split /\s+/, $line;
186                 $port = 23 if !$port;
187                 $r = $conn->connect($host, $port);
188                 if ($r) {
189                         dbg('connect', "Connected to $host $port");
190                 } else {
191                         dbg('connect', "***Connect Failed to $host $port $!");
192                 }
193         } elsif ($sort eq 'ax25' || $sort eq 'prog') {
194                 ;
195         } else {
196                 dbg('err', "invalid type of connection ($sort)");
197                 $conn->disconnect;
198         }
199         return $r;
200 }
201
202 sub _doabort
203 {
204         my $conn = shift;
205         my $string = shift;
206         dbg('connect', "abort $string");
207         $conn->{abort} = $string;
208 }
209
210 sub _dotimeout
211 {
212         my $conn = shift;
213         my $val = shift;
214         dbg('connect', "timeout set to $val");
215         $conn->{timeout}->del_timer if $conn->{timeout};
216         $conn->{timeout} = ExtMsg->new_timer($val, sub{ _timeout($conn); });
217         $conn->{timeval} = $val;
218 }
219
220 sub _dolineend
221 {
222         my $conn = shift;
223         my $val = shift;
224         dbg('connect', "lineend set to $val ");
225         $val =~ s/\\r/\r/g;
226         $val =~ s/\\n/\n/g;
227         $conn->{lineend} = $val;
228 }
229
230 sub _dochat
231 {
232         my $conn = shift;
233         my $cmd = shift;
234         my $line = shift;
235         
236         if ($line) {
237                 my ($expect, $send) = $cmd =~ /^\s*\'(.*)\'\s+\'(.*)\'/;
238                 if ($expect) {
239                         dbg('connect', "expecting: \"$expect\" received: \"$line\"");
240                         if ($conn->{abort} && $line =~ /$conn->{abort}/i) {
241                                 dbg('connect', "aborted on /$conn->{abort}/");
242                                 $conn->disconnect;
243                                 delete $conn->{cmd};
244                                 return;
245                         }
246                         if ($line =~ /$expect/i) {
247                                 dbg('connect', "got: \"$expect\" sending: \"$send\"");
248                                 $conn->send_later($send);
249                                 return;
250                         }
251                 }
252         }
253         $conn->{state} = 'WC';
254         unshift @{$conn->{cmd}}, $cmd;
255 }
256
257 sub _timeout
258 {
259         my $conn = shift;
260         dbg('connect', "timed out after $conn->{timeval} seconds");
261         $conn->disconnect;
262         @main::outstanding_connects = grep {$_->{call} ne $conn->{call}} @main::outstanding_connects;
263 }
264
265 # handle callsign and connection type firtling
266 sub _doclient
267 {
268         my $conn = shift;
269         my $line = shift;
270         my @f = split /\s+/, $line;
271         $conn->{call} = uc $f[0] if $f[0];
272         $conn->{csort} = $f[1] if $f[1];
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_later($_);
286                         }
287                         $f->close;
288                 }
289         }
290         $! = undef;
291 }