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