44846c53312f12a370b2babd6cd5acd888605c4d
[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 =~ m{^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         dbg((ref $conn) . " connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
58         $conn->SUPER::send_raw($msg);
59 }
60
61 sub echo
62 {
63         my $conn = shift;
64         $conn->{echo} = shift;
65 }
66
67 sub dequeue
68 {
69         my $conn = shift;
70         my $msg;
71
72         if ($conn->ax25 && exists $conn->{msg}) {
73                 $conn->{msg} =~ s/\cM/\cJ/g;
74         }
75         if ($conn->{state} eq 'WC') {
76                 if (exists $conn->{cmd}) {
77                         if (@{$conn->{cmd}}) {
78                                 dbg("connect $conn->{cnum}: $conn->{msg}") if isdbg('connect');
79                                 $conn->_docmd($conn->{msg});
80                         } 
81                 }
82                 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
83                         $conn->to_connected($conn->{call}, 'O', $conn->{csort});
84                 }
85         } elsif ($conn->{msg} =~ /\cJ/) {
86                 my @lines =  $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
87                 if ($conn->{msg} =~ /\cJ$/) {
88                         delete $conn->{msg};
89                 } else {
90                         $conn->{msg} =~ s/([^\cM\cJ]*)\cM?\cJ//g;
91                 }
92                 while (defined ($msg = shift @lines)) {
93                         dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
94                 
95                         $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
96 #                       $msg =~ s/[\x00-\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g;         # immutable CSI sequence + control characters
97                         
98                         if ($conn->{state} eq 'C') {
99                                 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
100                         } elsif ($conn->{state} eq 'WL' ) {
101                                 $msg = uc $msg;
102                                 if (is_callsign($msg)) {
103                                         if ($main::allowslashcall || $msg !~ m|/|) {
104                                                 my $sort = $conn->{csort};
105                                                 $sort = 'local' if $conn->{peerhost} =~ /127\.\d+\.\d+\.\d+$/ || $conn->{peerhost} eq '::1';
106                                                 my $uref;
107                                                 if ($main::passwdreq || ($uref = DXUser::get_current($msg)) && $uref->passwd ) {
108                                                         $conn->conns($msg);
109                                                         $conn->{state} = 'WP';
110                                                         $conn->{decho} = $conn->{echo};
111                                                         $conn->{echo} = 0;
112                                                         $conn->send_raw('password: ');
113                                                 } else {
114                                                         $conn->to_connected($msg, 'A', $sort);
115                                                 }
116                                         } else {
117                                                 $conn->send_now("Sorry $msg is an invalid callsign");
118                                                 $conn->disconnect;
119                                         }
120                                 } else {
121                                         $conn->send_now("Sorry $msg is an invalid callsign");
122                                         $conn->disconnect;
123                                 }
124                         } elsif ($conn->{state} eq 'WP' ) {
125                                 my $uref = DXUser::get_current($conn->{call});
126                                 $msg =~ s/[\r\n]+$//;
127                                 if ($uref && $msg eq $uref->passwd) {
128                                         my $sort = $conn->{csort};
129                                         $conn->{echo} = $conn->{decho};
130                                         delete $conn->{decho};
131                                         $sort = 'local' if $conn->{peerhost} =~ /127\.\d+\.\d+\.\d+$/ || $conn->{peerhost} eq '::1';
132                                         $conn->{usedpasswd} = 1;
133                                         $conn->to_connected($conn->{call}, 'A', $sort);
134                                 } else {
135                                         $conn->send_now("Sorry");
136                                         $conn->disconnect;
137                                 }
138                         } elsif ($conn->{state} eq 'WC') {
139                                 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
140                                         $conn->_docmd($msg);
141                                         if ($conn->{state} eq 'WC' && exists $conn->{cmd} &&  @{$conn->{cmd}} == 0) {
142                                                 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
143                                         }
144                                 }
145                         }
146                 }
147         }
148 }
149
150 sub to_connected
151 {
152         my ($conn, $call, $dir, $sort) = @_;
153         $conn->{state} = 'C';
154         $conn->conns($call);
155         delete $conn->{cmd};
156         $conn->{timeout}->del if $conn->{timeout};
157         delete $conn->{timeout};
158         $conn->{csort} = $sort;
159         &{$conn->{rproc}}($conn, "$dir$call|$sort");
160         $conn->_send_file(localdata("connected")) unless $conn->{outgoing};
161 }
162
163 sub new_client {
164         
165         my $server_conn = shift;
166         my $client = shift;
167         my $conn = $server_conn->SUPER::new_client($client);
168         # send login prompt
169         $conn->{state} = 'WL';
170         $conn->_send_file(localdata("issue"));
171         $conn->send_raw("login: ");
172         $conn->_dotimeout(60);
173         $conn->{echo} = 1;
174 }
175
176 sub start_connect
177 {
178         my $call = shift;
179         my $fn = shift;
180         my $conn = ExtMsg->new(\&main::new_channel); 
181         $conn->{outgoing} = 1;
182         $conn->conns($call);
183         
184         my $f = new IO::File $fn;
185         push @{$conn->{cmd}}, <$f>;
186         $f->close;
187         $conn->{state} = 'WC';
188         $conn->_dotimeout($deftimeout);
189         $conn->_docmd;
190 }
191
192 sub _docmd
193 {
194         my $conn = shift;
195         my $msg = shift;
196         my $cmd;
197
198         while ($cmd = shift @{$conn->{cmd}}) {
199                 chomp $cmd;
200                 next if $cmd =~ /^\s*\#/o;
201                 next if $cmd =~ /^\s*$/o;
202                 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
203                 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
204                 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
205                 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
206                         unless ($conn->_doconnect($1, $2)) {
207                                 $conn->disconnect;
208                                 @{$conn->{cmd}} = [];    # empty any further commands
209                                 last;
210                         }  
211                 }
212                 if ($cmd =~ /^\s*\'([^\']*)\'\s+\'([^\']*)\'/) {
213                         $conn->_dochat($cmd, $msg, $1, $2);
214                         last;
215                 }
216                 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
217                         $conn->_doclient($1);
218                         last;
219                 }
220                 last if $conn->{state} eq 'E';
221         }
222 }
223
224 sub _doconnect
225 {
226         my ($conn, $sort, $line) = @_;
227         my $r;
228
229         $sort = lc $sort;
230         dbg("CONNECT $conn->{cnum} sort: $sort command: $line") if isdbg('connect');
231         if ($sort eq 'telnet') {
232                 # this is a straight network connect
233                 my ($host, $port) = split /\s+/, $line;
234                 $port = 23 if !$port;
235                 $r = $conn->connect($host, $port);
236                 if ($r) {
237                         dbg("Connected $conn->{cnum} to $host $port") if isdbg('connect');
238                 } else {
239                         dbg("***Connect $conn->{cnum} Failed to $host $port $!") if isdbg('connect');
240                 }
241         } elsif ($sort eq 'agw') {
242                 # turn it into an AGW object
243                 bless $conn, 'AGWMsg';
244                 $r = $conn->connect($line);
245         } elsif ($sort eq 'bpq') {
246                 # turn it into an BPQ object
247                 bless $conn, 'BPQMsg';
248                 $r = $conn->connect($line);
249         } elsif ($sort eq 'ax25' || $sort eq 'prog') {
250                 $r = $conn->start_program($line, $sort);
251         } else {
252                 dbg("invalid type of connection ($sort)");
253         }
254         $conn->disconnect unless $r;
255         return $r;
256 }
257
258 sub _doabort
259 {
260         my $conn = shift;
261         my $string = shift;
262         dbg("connect $conn->{cnum}: abort $string") if isdbg('connect');
263         $conn->{abort} = $string;
264 }
265
266 sub _dotimeout
267 {
268         my $conn = shift;
269         my $val = shift;
270         dbg("connect $conn->{cnum}: timeout set to $val") if isdbg('connect');
271         $conn->{timeout}->del if $conn->{timeout};
272         $conn->{timeval} = $val;
273         $conn->{timeout} = Timer->new($val, sub{ &_timedout($conn) });
274 }
275
276 sub _dolineend
277 {
278         my $conn = shift;
279         my $val = shift;
280         dbg("connect $conn->{cnum}: lineend set to $val ") if isdbg('connect');
281         $val =~ s/\\r/\r/g;
282         $val =~ s/\\n/\n/g;
283         $conn->{lineend} = $val;
284 }
285
286 sub _dochat
287 {
288         my $conn = shift;
289         my $cmd = shift;
290         my $line = shift;
291         my $expect = shift;
292         my $send = shift;
293                 
294         if ($line) {
295                 if ($expect) {
296                         dbg("connect $conn->{cnum}: expecting: \"$expect\" received: \"$line\"") if isdbg('connect');
297                         if ($conn->{abort} && $line =~ /$conn->{abort}/i) {
298                                 dbg("connect $conn->{cnum}: aborted on /$conn->{abort}/") if isdbg('connect');
299                                 $conn->disconnect;
300                                 delete $conn->{cmd};
301                                 return;
302                         }
303                         if ($line =~ /\Q$expect/i) {
304                                 if (length $send) {
305                                         dbg("connect $conn->{cnum}: got: \"$expect\" sending: \"$send\"") if isdbg('connect');
306                                         $conn->send_later("D$conn->{call}|$send");
307                                 }
308                                 delete $conn->{msg}; # get rid any input if a match
309                                 return;
310                         }
311                 }
312         }
313         $conn->{state} = 'WC';
314         unshift @{$conn->{cmd}}, $cmd;
315 }
316
317 sub _timedout
318 {
319         my $conn = shift;
320         dbg("connect $conn->{cnum}: timed out after $conn->{timeval} seconds") if isdbg('connect');
321         $conn->disconnect;
322 }
323
324 # handle callsign and connection type firtling
325 sub _doclient
326 {
327         my $conn = shift;
328         my $line = shift;
329         my @f = split /\s+/, $line;
330         my $call = uc $f[0] if $f[0];
331         $conn->conns($call);
332         $conn->{csort} = $f[1] if $f[1];
333         $conn->{state} = 'C';
334         eval {$conn->{peerhost} = $conn->{sock}->handle->peerhost} unless $conn->ax25;
335         &{$conn->{rproc}}($conn, "O$call|$conn->{csort}");
336         delete $conn->{cmd};
337         $conn->{timeout}->del if $conn->{timeout};
338 }
339
340 sub _send_file
341 {
342         my $conn = shift;
343         my $fn = shift;
344         
345         if (-e $fn) {
346                 my $f = new IO::File $fn;
347                 if ($f) {
348                         while (<$f>) {
349                                 chomp;
350                                 my $l = $_;
351                                 dbg("connect $conn->{cnum}: $l") if isdbg('connll');
352                                 $conn->send_raw($l . $conn->{lineend});
353                         }
354                         $f->close;
355                 }
356         }
357 }