]> scm.dxcluster.org Git - spider.git/blob - perl/ExtMsg.pm
01a30924393e088535a9d2f98f97cff11004b5b7
[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->to_connected($conn->{call}, 'O', 'telnet');
70                 }
71         } elsif ($conn->{msg} =~ /\cJ/) {
72                 my @lines =  $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
73                 if ($conn->{msg} =~ /\cJ$/) {
74                         delete $conn->{msg};
75                 } else {
76                         $conn->{msg} = pop @lines;
77                 }
78                 while (defined ($msg = shift @lines)) {
79                         dbg('connect', $msg) unless $conn->{state} eq 'C';
80                 
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
83                         
84                         if ($conn->{state} eq 'C') {
85                                 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
86                         } elsif ($conn->{state} eq 'WL' ) {
87                                 $msg = uc $msg;
88                                 if (is_callsign($msg)) {
89                                         $conn->to_connected($msg, 'A', 'telnet');
90                                 } else {
91                                         $conn->send_now("Sorry $msg is an invalid callsign");
92                                         $conn->disconnect;
93                                 }
94                         } elsif ($conn->{state} eq 'WC') {
95                                 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
96                                         $conn->_docmd($msg);
97                                         if ($conn->{state} eq 'WC' && exists $conn->{cmd} &&  @{$conn->{cmd}} == 0) {
98                                                 $conn->to_connected($conn->{call}, 'O', 'telnet');
99                                         }
100                                 }
101                         }
102                 }
103         }
104 }
105
106 sub to_connected
107 {
108         my ($conn, $call, $dir, $sort) = @_;
109         $conn->{state} = 'C';
110         $conn->conns($call);
111         delete $conn->{cmd};
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");
116 }
117
118 sub new_client {
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;
123
124     my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost} = $sock->peerhost(), $conn->{peerport} = $sock->peerport());
125         if ($eproc) {
126                 $conn->{eproc} = $eproc;
127         Msg::set_event_handler ($sock, "error" => $eproc);
128         }
129     if ($rproc) {
130         $conn->{rproc} = $rproc;
131         my $callback = sub {$conn->_rcv};
132                 Msg::set_event_handler ($sock, "read" => $callback);
133                 # send login prompt
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);
141     } else { 
142         $conn->disconnect();
143     }
144 }
145
146 sub start_connect
147 {
148         my $call = shift;
149         my $fn = shift;
150         my $conn = ExtMsg->new(\&main::new_channel); 
151         $conn->conns($call);
152         
153         my $f = new IO::File $fn;
154         push @{$conn->{cmd}}, <$f>;
155         $f->close;
156         $conn->_dotimeout($deftimeout);
157         $conn->_docmd;
158 }
159
160 sub _docmd
161 {
162         my $conn = shift;
163         my $msg = shift;
164         my $cmd;
165
166         while ($cmd = shift @{$conn->{cmd}}) {
167                 chomp $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)) {
175                                 $conn->disconnect;
176                                 @{$conn->{cmd}} = [];    # empty any further commands
177                                 last;
178                         }  
179                 }
180                 if ($cmd =~ /^\s*\'.*\'\s+\'.*\'/i) {
181                         $conn->_dochat($cmd, $msg);
182                         last;
183                 }
184                 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
185                         $conn->_doclient($1);
186                         last;
187                 }
188                 last if $conn->{state} eq 'E';
189         }
190 }
191
192 sub _doconnect
193 {
194         my ($conn, $sort, $line) = @_;
195         my $r;
196         
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);
203                 if ($r) {
204                         dbg('connect', "Connected to $host $port");
205                 } else {
206                         dbg('connect', "***Connect Failed to $host $port $!");
207                 }
208         } elsif ($sort eq 'ax25' || $sort eq 'prog') {
209                 ;
210         } else {
211                 dbg('err', "invalid type of connection ($sort)");
212                 $conn->disconnect;
213         }
214         return $r;
215 }
216
217 sub _doabort
218 {
219         my $conn = shift;
220         my $string = shift;
221         dbg('connect', "abort $string");
222         $conn->{abort} = $string;
223 }
224
225 sub _dotimeout
226 {
227         my $conn = shift;
228         my $val = shift;
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) });
233 }
234
235 sub _dolineend
236 {
237         my $conn = shift;
238         my $val = shift;
239         dbg('connect', "lineend set to $val ");
240         $val =~ s/\\r/\r/g;
241         $val =~ s/\\n/\n/g;
242         $conn->{lineend} = $val;
243 }
244
245 sub _dochat
246 {
247         my $conn = shift;
248         my $cmd = shift;
249         my $line = shift;
250                 
251         if ($line) {
252                 my ($expect, $send) = $cmd =~ /^\s*\'(.*)\'\s+\'(.*)\'/;
253                 if ($expect) {
254                         dbg('connect', "expecting: \"$expect\" received: \"$line\"");
255                         if ($conn->{abort} && $line =~ /$conn->{abort}/i) {
256                                 dbg('connect', "aborted on /$conn->{abort}/");
257                                 $conn->disconnect;
258                                 delete $conn->{cmd};
259                                 return;
260                         }
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
265                                 return;
266                         }
267                 }
268         }
269         $conn->{state} = 'WC';
270         unshift @{$conn->{cmd}}, $cmd;
271 }
272
273 sub _timedout
274 {
275         my $conn = shift;
276         dbg('connect', "timed out after $conn->{timeval} seconds");
277         $conn->{timeout}->del;
278         delete $conn->{timeout};
279         $conn->disconnect;
280 }
281
282 # handle callsign and connection type firtling
283 sub _doclient
284 {
285         my $conn = shift;
286         my $line = shift;
287         my @f = split /\s+/, $line;
288         my $call = uc $f[0] if $f[0];
289         $conn->conns($call);
290         $conn->{csort} = $f[1] if $f[1];
291         $conn->{state} = 'C';
292         &{$conn->{rproc}}($conn, "O$call|telnet");
293         delete $conn->{cmd};
294         $conn->{timeout}->del if $conn->{timeout};
295 }
296
297 sub _send_file
298 {
299         my $conn = shift;
300         my $fn = shift;
301         
302         if (-e $fn) {
303                 my $f = new IO::File $fn;
304                 if ($f) {
305                         while (<$f>) {
306                                 chomp;
307                                 $conn->send_raw($_ . $conn->{lineend});
308                         }
309                         $f->close;
310                 }
311         }
312 }