]> scm.dxcluster.org Git - spider.git/blob - perl/ExtMsg.pm
954703b335cdb0ac134b953ad6ebdd6b7c953caa
[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->{csort} eq 'ax25' && exists $conn->{msg}) {
62                 $conn->{msg} =~ s/\cM/\cJ/g;
63         }
64         if ($conn->{state} eq 'WC') {
65                 if (exists $conn->{cmd}) {
66                         if (@{$conn->{cmd}}) {
67                                 dbg('connect', $conn->{msg});
68                                 $conn->_docmd($conn->{msg});
69                         } 
70                 }
71                 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
72                         $conn->to_connected($conn->{call}, 'O', 'telnet');
73                 }
74         } elsif ($conn->{msg} =~ /\cJ/) {
75                 my @lines =  $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
76                 if ($conn->{msg} =~ /\cJ$/) {
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->to_connected($msg, 'A', 'telnet');
93                                 } else {
94                                         $conn->send_now("Sorry $msg is an invalid callsign");
95                                         $conn->disconnect;
96                                 }
97                         } elsif ($conn->{state} eq 'WC') {
98                                 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
99                                         $conn->_docmd($msg);
100                                         if ($conn->{state} eq 'WC' && exists $conn->{cmd} &&  @{$conn->{cmd}} == 0) {
101                                                 $conn->to_connected($conn->{call}, 'O', 'telnet');
102                                         }
103                                 }
104                         }
105                 }
106         }
107 }
108
109 sub to_connected
110 {
111         my ($conn, $call, $dir, $sort) = @_;
112         $conn->{state} = 'C';
113         $conn->conns($call);
114         delete $conn->{cmd};
115         $conn->{timeout}->del if $conn->{timeout};
116         delete $conn->{timeout};
117         $conn->_send_file("$main::data/connected");
118         &{$conn->{rproc}}($conn, "$dir$call|$sort");
119 }
120
121 sub new_client {
122         my $server_conn = shift;
123     my $sock = $server_conn->{sock}->accept();
124     my $conn = $server_conn->new($server_conn->{rproc});
125         $conn->{sock} = $sock;
126
127     my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost} = $sock->peerhost(), $conn->{peerport} = $sock->peerport());
128         if ($eproc) {
129                 $conn->{eproc} = $eproc;
130         Msg::set_event_handler ($sock, "error" => $eproc);
131         }
132     if ($rproc) {
133         $conn->{rproc} = $rproc;
134         my $callback = sub {$conn->_rcv};
135                 Msg::set_event_handler ($sock, "read" => $callback);
136                 # send login prompt
137                 $conn->{state} = 'WL';
138 #               $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
139 #               $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
140 #               $conn->send_raw("\xFF\xFC\x01");
141                 $conn->_send_file("$main::data/issue");
142                 $conn->send_raw("login: ");
143                 $conn->_dotimeout(60);
144     } else { 
145         $conn->disconnect();
146     }
147 }
148
149 sub start_connect
150 {
151         my $call = shift;
152         my $fn = shift;
153         my $conn = ExtMsg->new(\&main::new_channel); 
154         $conn->conns($call);
155         
156         my $f = new IO::File $fn;
157         push @{$conn->{cmd}}, <$f>;
158         $f->close;
159         $conn->_dotimeout($deftimeout);
160         $conn->_docmd;
161 }
162
163 sub _docmd
164 {
165         my $conn = shift;
166         my $msg = shift;
167         my $cmd;
168
169         while ($cmd = shift @{$conn->{cmd}}) {
170                 chomp $cmd;
171                 next if $cmd =~ /^\s*\#/o;
172                 next if $cmd =~ /^\s*$/o;
173                 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
174                 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
175                 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
176                 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
177                         unless ($conn->_doconnect($1, $2)) {
178                                 $conn->disconnect;
179                                 @{$conn->{cmd}} = [];    # empty any further commands
180                                 last;
181                         }  
182                 }
183                 if ($cmd =~ /^\s*\'.*\'\s+\'.*\'/i) {
184                         $conn->_dochat($cmd, $msg);
185                         last;
186                 }
187                 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
188                         $conn->_doclient($1);
189                         last;
190                 }
191                 last if $conn->{state} eq 'E';
192         }
193 }
194
195 sub _doconnect
196 {
197         my ($conn, $sort, $line) = @_;
198         my $r;
199
200         $sort = lc $sort;
201         dbg('connect', "CONNECT sort: $sort command: $line");
202         if ($sort eq 'telnet') {
203                 # this is a straight network connect
204                 my ($host, $port) = split /\s+/, $line;
205                 $port = 23 if !$port;
206                 $r = $conn->connect($host, $port);
207                 if ($r) {
208                         dbg('connect', "Connected to $host $port");
209                 } else {
210                         dbg('connect', "***Connect Failed to $host $port $!");
211                 }
212         } elsif ($sort eq 'ax25' || $sort eq 'prog') {
213                 my $pid = fork();
214                 if (defined $pid) {
215                         if (!$pid) {
216                                 # in child, unset warnings, disable debugging and general clean up from us
217                                 $^W = 0;
218                                 eval "{ package DB; sub DB {} }";
219                                 DXChannel::closeall();
220                                 for (@main::listeners) {
221                                         $_->close_server;
222                                 }
223                                 unless ($^O =~ /^MS/) {
224                                         $SIG{HUP} = 'IGNORE';
225                                         $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = $SIG{__WARN__} = 'DEFAULT';
226                                         alarm(0);
227                                 }
228                                 exec "$line" or dbg('cron', "exec '$line' failed $!");
229                         }
230                         dbg('connect', "program $sort $line started");
231                         $conn->{pid} = $pid;
232                         $conn->{csort} = $sort;
233                         $conn->{lineend} = "\cM" if $sort eq 'ax25';
234                 } else {
235                         dbg('connect', "can't $sort fork for $line $!");
236                 }
237         } else {
238                 dbg('err', "invalid type of connection ($sort)");
239                 $conn->disconnect;
240         }
241         return $r;
242 }
243
244 sub _doabort
245 {
246         my $conn = shift;
247         my $string = shift;
248         dbg('connect', "abort $string");
249         $conn->{abort} = $string;
250 }
251
252 sub _dotimeout
253 {
254         my $conn = shift;
255         my $val = shift;
256         dbg('connect', "timeout set to $val");
257         $conn->{timeout}->del if $conn->{timeout};
258         $conn->{timeval} = $val;
259         $conn->{timeout} = Timer->new($val, sub{ &_timedout($conn) });
260 }
261
262 sub _dolineend
263 {
264         my $conn = shift;
265         my $val = shift;
266         dbg('connect', "lineend set to $val ");
267         $val =~ s/\\r/\r/g;
268         $val =~ s/\\n/\n/g;
269         $conn->{lineend} = $val;
270 }
271
272 sub _dochat
273 {
274         my $conn = shift;
275         my $cmd = shift;
276         my $line = shift;
277                 
278         if ($line) {
279                 my ($expect, $send) = $cmd =~ /^\s*\'(.*)\'\s+\'(.*)\'/;
280                 if ($expect) {
281                         dbg('connect', "expecting: \"$expect\" received: \"$line\"");
282                         if ($conn->{abort} && $line =~ /$conn->{abort}/i) {
283                                 dbg('connect', "aborted on /$conn->{abort}/");
284                                 $conn->disconnect;
285                                 delete $conn->{cmd};
286                                 return;
287                         }
288                         if ($line =~ /$expect/i) {
289                                 dbg('connect', "got: \"$expect\" sending: \"$send\"");
290                                 $conn->send_later($send);
291                                 delete $conn->{msg}; # get rid any input if a match
292                                 return;
293                         }
294                 }
295         }
296         $conn->{state} = 'WC';
297         unshift @{$conn->{cmd}}, $cmd;
298 }
299
300 sub _timedout
301 {
302         my $conn = shift;
303         dbg('connect', "timed out after $conn->{timeval} seconds");
304         $conn->{timeout}->del;
305         delete $conn->{timeout};
306         $conn->disconnect;
307 }
308
309 # handle callsign and connection type firtling
310 sub _doclient
311 {
312         my $conn = shift;
313         my $line = shift;
314         my @f = split /\s+/, $line;
315         my $call = uc $f[0] if $f[0];
316         $conn->conns($call);
317         $conn->{csort} = $f[1] if $f[1];
318         $conn->{state} = 'C';
319         &{$conn->{rproc}}($conn, "O$call|telnet");
320         delete $conn->{cmd};
321         $conn->{timeout}->del if $conn->{timeout};
322 }
323
324 sub _send_file
325 {
326         my $conn = shift;
327         my $fn = shift;
328         
329         if (-e $fn) {
330                 my $f = new IO::File $fn;
331                 if ($f) {
332                         while (<$f>) {
333                                 chomp;
334                                 $conn->send_raw($_ . $conn->{lineend});
335                         }
336                         $f->close;
337                 }
338         }
339 }