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