]> www.dxcluster.org Git - spider.git/blob - perl/ExtMsg.pm
remove 5.10.1 isms (//= etc)
[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 #
9 # Copyright (c) 2001 - Dirk Koopman G1TLH
10 #
11 #       Modified Jan 2006 by John Wiseman G8BPQ to support connections to BPQ32 node,
12 #               and fix pattern matching on 'chat' abort handling
13 #
14
15 package ExtMsg;
16
17 use strict;
18 use Msg;
19 use DXVars;
20 use DXUtil;
21 use DXDebug;
22 use IO::File;
23 use IO::Socket;
24 use IPC::Open3;
25
26 use vars qw(@ISA $deftimeout);
27
28 @ISA = qw(Msg);
29 $deftimeout = 60;
30
31 sub login
32 {
33         goto &main::login;        # save some writing, this was the default
34 }
35
36 sub enqueue
37 {
38         my ($conn, $msg) = @_;
39         unless ($msg =~ /^[ABZ]/) {
40                 if ($msg =~ /^E[-\w]+\|([01])/ && $conn->{csort} eq 'telnet') {
41                         $conn->{echo} = $1;
42                         if ($1) {
43 #                               $conn->send_raw("\xFF\xFC\x01");
44                         } else {
45 #                               $conn->send_raw("\xFF\xFB\x01");
46                         }
47                 } else {
48                         $msg =~ s/^[-\w]+\|//;
49                         push (@{$conn->{outqueue}}, $msg . $conn->{lineend});
50                 }
51         }
52 }
53
54 sub send_raw
55 {
56         my ($conn, $msg) = @_;
57     my $sock = $conn->{sock};
58     return unless defined($sock);
59         push (@{$conn->{outqueue}}, $msg);
60         dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
61     Msg::set_event_handler ($sock, "write" => sub {$conn->_send(0)});
62 }
63
64 sub echo
65 {
66         my $conn = shift;
67         $conn->{echo} = shift;
68 }
69
70 sub dequeue
71 {
72         my $conn = shift;
73         my $msg;
74
75         if ($conn->ax25 && exists $conn->{msg}) {
76                 $conn->{msg} =~ s/\cM/\cJ/g;
77         }
78         if ($conn->{state} eq 'WC') {
79                 if (exists $conn->{cmd}) {
80                         if (@{$conn->{cmd}}) {
81                                 dbg("connect $conn->{cnum}: $conn->{msg}") if isdbg('connect');
82                                 $conn->_docmd($conn->{msg});
83                         } 
84                 }
85                 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
86                         $conn->to_connected($conn->{call}, 'O', $conn->{csort});
87                 }
88         } elsif ($conn->{msg} =~ /\cJ/) {
89                 my @lines =  $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
90                 if ($conn->{msg} =~ /\cJ$/) {
91                         delete $conn->{msg};
92                 } else {
93                         $conn->{msg} =~ s/([^\cM\cJ]*)\cM?\cJ//g;
94                 }
95                 while (defined ($msg = shift @lines)) {
96                         dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
97                 
98                         $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
99 #                       $msg =~ s/[\x00-\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g;         # immutable CSI sequence + control characters
100                         
101                         if ($conn->{state} eq 'C') {
102                                 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
103                         } elsif ($conn->{state} eq 'WL' ) {
104                                 $msg = uc $msg;
105                                 if (is_callsign($msg) && $msg !~ m|/| ) {
106                                         my $sort = $conn->{csort};
107                                         $sort = 'local' if $conn->{peerhost} =~ /127\.\d+\.\d+\.\d+$/ || $conn->{peerhost} eq '::1';
108                                         my $uref;
109                                         if ($main::passwdreq || ($uref = DXUser::get_current($msg)) && $uref->passwd ) {
110                                                 $conn->conns($msg);
111                                                 $conn->{state} = 'WP';
112                                                 $conn->{decho} = $conn->{echo};
113                                                 $conn->{echo} = 0;
114                                                 $conn->send_raw('password: ');
115                                         } else {
116                                                 $conn->to_connected($msg, 'A', $sort);
117                                         }
118                                 } else {
119                                         $conn->send_now("Sorry $msg is an invalid callsign");
120                                         $conn->disconnect;
121                                 }
122                         } elsif ($conn->{state} eq 'WP' ) {
123                                 my $uref = DXUser::get_current($conn->{call});
124                                 $msg =~ s/[\r\n]+$//;
125                                 if ($uref && $msg eq $uref->passwd) {
126                                         my $sort = $conn->{csort};
127                                         $conn->{echo} = $conn->{decho};
128                                         delete $conn->{decho};
129                                         $sort = 'local' if $conn->{peerhost} =~ /127\.\d+\.\d+\.\d+$/ || $conn->{peerhost} eq '::1';
130                                         $conn->{usedpasswd} = 1;
131                                         $conn->to_connected($conn->{call}, 'A', $sort);
132                                 } else {
133                                         $conn->send_now("Sorry");
134                                         $conn->disconnect;
135                                 }
136                         } elsif ($conn->{state} eq 'WC') {
137                                 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
138                                         $conn->_docmd($msg);
139                                         if ($conn->{state} eq 'WC' && exists $conn->{cmd} &&  @{$conn->{cmd}} == 0) {
140                                                 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
141                                         }
142                                 }
143                         }
144                 }
145         }
146 }
147
148 sub to_connected
149 {
150         my ($conn, $call, $dir, $sort) = @_;
151         $conn->{state} = 'C';
152         $conn->conns($call);
153         delete $conn->{cmd};
154         $conn->{timeout}->del if $conn->{timeout};
155         delete $conn->{timeout};
156         $conn->{csort} = $sort;
157         unless ($conn->ax25) {
158                 eval {$conn->{peerhost} = $conn->{sock}->peerhost};
159                 $conn->nolinger;
160         }
161         &{$conn->{rproc}}($conn, "$dir$call|$sort");
162         $conn->_send_file("$main::data/connected") unless $conn->{outgoing};
163 }
164
165 sub new_client {
166         my $server_conn = shift;
167     my $sock = $server_conn->{sock}->accept();
168         if ($sock) {
169                 my $conn = $server_conn->new($server_conn->{rproc});
170                 $conn->{sock} = $sock;
171                 $conn->nolinger;
172                 Msg::blocking($sock, 0);
173                 $conn->{blocking} = 0;
174                 eval {$conn->{peerhost} = $sock->peerhost};
175                 if ($@) {
176                         dbg($@) if isdbg('connll');
177                         $conn->disconnect;
178                 } else {
179                         eval {$conn->{peerport} = $sock->peerport};
180                         $conn->{peerport} = 0 if $@;
181                         my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost}, $conn->{peerport});
182                         dbg("accept $conn->{cnum} from $conn->{peerhost} $conn->{peerport}") if isdbg('connll');
183                         if ($eproc) {
184                                 $conn->{eproc} = $eproc;
185                                 Msg::set_event_handler ($sock, "error" => $eproc);
186                         }
187                         if ($rproc) {
188                                 $conn->{rproc} = $rproc;
189                                 my $callback = sub {$conn->_rcv};
190                                 Msg::set_event_handler ($sock, "read" => $callback);
191                                 # send login prompt
192                                 $conn->{state} = 'WL';
193                                 #               $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
194                                 #               $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
195                                 #               $conn->send_raw("\xFF\xFC\x01");
196                                 $conn->_send_file("$main::data/issue");
197                                 $conn->send_raw("login: ");
198                                 $conn->_dotimeout(60);
199                                 $conn->{echo} = 1;
200                         } else { 
201                                 &{$conn->{eproc}}() if $conn->{eproc};
202                                 $conn->disconnect();
203                         }
204                 }
205         } else {
206                 dbg("ExtMsg: error on accept ($!)") if isdbg('err');
207         }
208 }
209
210 sub start_connect
211 {
212         my $call = shift;
213         my $fn = shift;
214         my $conn = ExtMsg->new(\&main::new_channel); 
215         $conn->{outgoing} = 1;
216         $conn->conns($call);
217         
218         my $f = new IO::File $fn;
219         push @{$conn->{cmd}}, <$f>;
220         $f->close;
221         $conn->{state} = 'WC';
222         $conn->_dotimeout($deftimeout);
223         $conn->_docmd;
224 }
225
226 sub _docmd
227 {
228         my $conn = shift;
229         my $msg = shift;
230         my $cmd;
231
232         while ($cmd = shift @{$conn->{cmd}}) {
233                 chomp $cmd;
234                 next if $cmd =~ /^\s*\#/o;
235                 next if $cmd =~ /^\s*$/o;
236                 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
237                 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
238                 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
239                 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
240                         unless ($conn->_doconnect($1, $2)) {
241                                 $conn->disconnect;
242                                 @{$conn->{cmd}} = [];    # empty any further commands
243                                 last;
244                         }  
245                 }
246                 if ($cmd =~ /^\s*\'([^\']*)\'\s+\'([^\']*)\'/) {
247                         $conn->_dochat($cmd, $msg, $1, $2);
248                         last;
249                 }
250                 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
251                         $conn->_doclient($1);
252                         last;
253                 }
254                 last if $conn->{state} eq 'E';
255         }
256 }
257
258 sub _doconnect
259 {
260         my ($conn, $sort, $line) = @_;
261         my $r;
262
263         $sort = lc $sort;
264         dbg("CONNECT $conn->{cnum} sort: $sort command: $line") if isdbg('connect');
265         if ($sort eq 'telnet') {
266                 # this is a straight network connect
267                 my ($host, $port) = split /\s+/, $line;
268                 $port = 23 if !$port;
269                 $r = $conn->connect($host, $port);
270                 if ($r) {
271                         dbg("Connected $conn->{cnum} to $host $port") if isdbg('connect');
272                 } else {
273                         dbg("***Connect $conn->{cnum} Failed to $host $port $!") if isdbg('connect');
274                 }
275         } elsif ($sort eq 'agw') {
276                 # turn it into an AGW object
277                 bless $conn, 'AGWMsg';
278                 $r = $conn->connect($line);
279         } elsif ($sort eq 'bpq') {
280                 # turn it into an BPQ object
281                 bless $conn, 'BPQMsg';
282                 $r = $conn->connect($line);
283         } elsif ($sort eq 'ax25' || $sort eq 'prog') {
284                 $r = $conn->start_program($line, $sort);
285         } else {
286                 dbg("invalid type of connection ($sort)");
287         }
288         $conn->disconnect unless $r;
289         return $r;
290 }
291
292 sub _doabort
293 {
294         my $conn = shift;
295         my $string = shift;
296         dbg("connect $conn->{cnum}: abort $string") if isdbg('connect');
297         $conn->{abort} = $string;
298 }
299
300 sub _dotimeout
301 {
302         my $conn = shift;
303         my $val = shift;
304         dbg("connect $conn->{cnum}: timeout set to $val") if isdbg('connect');
305         $conn->{timeout}->del if $conn->{timeout};
306         $conn->{timeval} = $val;
307         $conn->{timeout} = Timer->new($val, sub{ &_timedout($conn) });
308 }
309
310 sub _dolineend
311 {
312         my $conn = shift;
313         my $val = shift;
314         dbg("connect $conn->{cnum}: lineend set to $val ") if isdbg('connect');
315         $val =~ s/\\r/\r/g;
316         $val =~ s/\\n/\n/g;
317         $conn->{lineend} = $val;
318 }
319
320 sub _dochat
321 {
322         my $conn = shift;
323         my $cmd = shift;
324         my $line = shift;
325         my $expect = shift;
326         my $send = shift;
327                 
328         if ($line) {
329                 if ($expect) {
330                         dbg("connect $conn->{cnum}: expecting: \"$expect\" received: \"$line\"") if isdbg('connect');
331                         if ($conn->{abort} && $line =~ /$conn->{abort}/i) {
332                                 dbg("connect $conn->{cnum}: aborted on /$conn->{abort}/") if isdbg('connect');
333                                 $conn->disconnect;
334                                 delete $conn->{cmd};
335                                 return;
336                         }
337                         if ($line =~ /\Q$expect/i) {
338                                 if (length $send) {
339                                         dbg("connect $conn->{cnum}: got: \"$expect\" sending: \"$send\"") if isdbg('connect');
340                                         $conn->send_later("D$conn->{call}|$send");
341                                 }
342                                 delete $conn->{msg}; # get rid any input if a match
343                                 return;
344                         }
345                 }
346         }
347         $conn->{state} = 'WC';
348         unshift @{$conn->{cmd}}, $cmd;
349 }
350
351 sub _timedout
352 {
353         my $conn = shift;
354         dbg("connect $conn->{cnum}: timed out after $conn->{timeval} seconds") if isdbg('connect');
355         $conn->disconnect;
356 }
357
358 # handle callsign and connection type firtling
359 sub _doclient
360 {
361         my $conn = shift;
362         my $line = shift;
363         my @f = split /\s+/, $line;
364         my $call = uc $f[0] if $f[0];
365         $conn->conns($call);
366         $conn->{csort} = $f[1] if $f[1];
367         $conn->{state} = 'C';
368         eval {$conn->{peerhost} = $conn->{sock}->peerhost} unless $conn->ax25;
369         &{$conn->{rproc}}($conn, "O$call|$conn->{csort}");
370         delete $conn->{cmd};
371         $conn->{timeout}->del if $conn->{timeout};
372 }
373
374 sub _send_file
375 {
376         my $conn = shift;
377         my $fn = shift;
378         
379         if (-e $fn) {
380                 my $f = new IO::File $fn;
381                 if ($f) {
382                         while (<$f>) {
383                                 chomp;
384                                 my $l = $_;
385                                 dbg("connect $conn->{cnum}: $l") if isdbg('connll');
386                                 $conn->send_raw($l . $conn->{lineend});
387                         }
388                         $f->close;
389                 }
390         }
391 }