add DXSql module and condiational processing
[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 login
36 {
37         goto &main::login;        # save some writing, this was the default
38 }
39
40 sub enqueue
41 {
42         my ($conn, $msg) = @_;
43         unless ($msg =~ /^[ABZ]/) {
44                 if ($msg =~ /^E[-\w]+\|([01])/ && $conn->{csort} eq 'telnet') {
45                         $conn->{echo} = $1;
46                         if ($1) {
47 #                               $conn->send_raw("\xFF\xFC\x01");
48                         } else {
49 #                               $conn->send_raw("\xFF\xFB\x01");
50                         }
51                 } else {
52                         $msg =~ s/^[-\w]+\|//;
53                         push (@{$conn->{outqueue}}, $msg . $conn->{lineend});
54                 }
55         }
56 }
57
58 sub send_raw
59 {
60         my ($conn, $msg) = @_;
61     my $sock = $conn->{sock};
62     return unless defined($sock);
63         push (@{$conn->{outqueue}}, $msg);
64         dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
65     Msg::set_event_handler ($sock, "write" => sub {$conn->_send(0)});
66 }
67
68 sub echo
69 {
70         my $conn = shift;
71         $conn->{echo} = shift;
72 }
73
74 sub dequeue
75 {
76         my $conn = shift;
77         my $msg;
78
79         if ($conn->{csort} eq 'ax25' && exists $conn->{msg}) {
80                 $conn->{msg} =~ s/\cM/\cJ/g;
81         }
82         if ($conn->{state} eq 'WC') {
83                 if (exists $conn->{cmd}) {
84                         if (@{$conn->{cmd}}) {
85                                 dbg("connect $conn->{cnum}: $conn->{msg}") if isdbg('connect');
86                                 $conn->_docmd($conn->{msg});
87                         } 
88                 }
89                 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
90                         $conn->to_connected($conn->{call}, 'O', $conn->{csort});
91                 }
92         } elsif ($conn->{msg} =~ /\cJ/) {
93                 my @lines =  $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
94                 if ($conn->{msg} =~ /\cJ$/) {
95                         delete $conn->{msg};
96                 } else {
97                         $conn->{msg} =~ s/([^\cM\cJ]*)\cM?\cJ//g;
98                 }
99                 while (defined ($msg = shift @lines)) {
100                         dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
101                 
102                         $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
103 #                       $msg =~ s/[\x00-\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g;         # immutable CSI sequence + control characters
104                         
105                         if ($conn->{state} eq 'C') {
106                                 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
107                         } elsif ($conn->{state} eq 'WL' ) {
108                                 $msg = uc $msg;
109                                 if (is_callsign($msg) && $msg !~ m|/| ) {
110                                         my $sort = $conn->{csort};
111                                         $sort = 'local' if $conn->{peerhost} eq "127.0.0.1";
112                                         my $uref;
113                                         if ($main::passwdreq || ($uref = DXUser->get_current($msg)) && $uref->passwd ) {
114                                                 $conn->conns($msg);
115                                                 $conn->{state} = 'WP';
116                                                 $conn->{decho} = $conn->{echo};
117                                                 $conn->{echo} = 0;
118                                                 $conn->send_raw('password: ');
119                                         } else {
120                                                 $conn->to_connected($msg, 'A', $sort);
121                                         }
122                                 } else {
123                                         $conn->send_now("Sorry $msg is an invalid callsign");
124                                         $conn->disconnect;
125                                 }
126                         } elsif ($conn->{state} eq 'WP' ) {
127                                 my $uref = DXUser->get_current($conn->{call});
128                                 $msg =~ s/[\r\n]+$//;
129                                 if ($uref && $msg eq $uref->passwd) {
130                                         my $sort = $conn->{csort};
131                                         $conn->{echo} = $conn->{decho};
132                                         delete $conn->{decho};
133                                         $sort = 'local' if $conn->{peerhost} eq "127.0.0.1";
134                                         $conn->{usedpasswd} = 1;
135                                         $conn->to_connected($conn->{call}, 'A', $sort);
136                                 } else {
137                                         $conn->send_now("Sorry");
138                                         $conn->disconnect;
139                                 }
140                         } elsif ($conn->{state} eq 'WC') {
141                                 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
142                                         $conn->_docmd($msg);
143                                         if ($conn->{state} eq 'WC' && exists $conn->{cmd} &&  @{$conn->{cmd}} == 0) {
144                                                 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
145                                         }
146                                 }
147                         }
148                 }
149         }
150 }
151
152 sub to_connected
153 {
154         my ($conn, $call, $dir, $sort) = @_;
155         $conn->{state} = 'C';
156         $conn->conns($call);
157         delete $conn->{cmd};
158         $conn->{timeout}->del if $conn->{timeout};
159         delete $conn->{timeout};
160         $conn->nolinger;
161         &{$conn->{rproc}}($conn, "$dir$call|$sort");
162         $conn->_send_file("$main::data/connected") unless $conn->{outbound};
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->{outbound} = 1;
216         $conn->conns($call);
217         push @{$conn->{cmd}}, @_;
218         $conn->{state} = 'WC';
219         $conn->_dotimeout($deftimeout);
220         $conn->_docmd;
221 }
222
223 sub _docmd
224 {
225         my $conn = shift;
226         my $msg = shift;
227         my $cmd;
228
229         while ($cmd = shift @{$conn->{cmd}}) {
230                 chomp $cmd;
231                 next if $cmd =~ /^\s*\#/o;
232                 next if $cmd =~ /^\s*$/o;
233                 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
234                 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
235                 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
236                 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
237                         unless ($conn->_doconnect($1, $2)) {
238                                 $conn->disconnect;
239                                 @{$conn->{cmd}} = [];    # empty any further commands
240                                 last;
241                         }  
242                 }
243                 if ($cmd =~ /^\s*\'([^\']*)\'\s+\'([^\']*)\'/) {
244                         $conn->_dochat($cmd, $msg, $1, $2);
245                         last;
246                 }
247                 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
248                         $conn->_doclient($1);
249                         last;
250                 }
251                 last if $conn->{state} eq 'E';
252         }
253 }
254
255 sub _doconnect
256 {
257         my ($conn, $sort, $line) = @_;
258         my $r;
259
260         $sort = lc $sort;
261         dbg("CONNECT $conn->{cnum} sort: $sort command: $line") if isdbg('connect');
262         if ($sort eq 'telnet') {
263                 # this is a straight network connect
264                 my ($host, $port, $type) = split /\s+/, $line;
265                 if ($type && ref($conn) ne $type) {
266                         bless $conn, $type;
267                         $conn->set_newchannel_rproc;
268                         dbg("$conn->{cnum} to $host $port reblessed as $type") if isdbg('connect');
269                 }
270                 $port = 23 if !$port;
271                 $r = $conn->connect($host, $port);
272                 if ($r) {
273                         dbg("Connected $conn->{cnum} to $host $port") if isdbg('connect');
274
275                 } else {
276                         dbg("***Connect $conn->{cnum} Failed to $host $port $!") if isdbg('connect');
277                 }
278         } elsif ($sort eq 'agw') {
279                 # turn it into an AGW object
280                 bless $conn, 'AGWMsg';
281                 $r = $conn->connect($line);
282         } elsif ($sort eq 'ax25' || $sort eq 'prog') {
283                 $r = $conn->start_program($line, $sort);
284         } else {
285                 dbg("invalid type of connection ($sort)");
286         }
287         $conn->disconnect unless $r;
288         return $r;
289 }
290
291 sub _doabort
292 {
293         my $conn = shift;
294         my $string = shift;
295         dbg("connect $conn->{cnum}: abort $string") if isdbg('connect');
296         $conn->{abort} = $string;
297 }
298
299 sub _dotimeout
300 {
301         my $conn = shift;
302         my $val = shift;
303         dbg("connect $conn->{cnum}: timeout set to $val") if isdbg('connect');
304         $conn->{timeout}->del if $conn->{timeout};
305         $conn->{timeval} = $val;
306         $conn->{timeout} = Timer->new($val, sub{ &_timedout($conn) });
307 }
308
309 sub _dolineend
310 {
311         my $conn = shift;
312         my $val = shift;
313         dbg("connect $conn->{cnum}: lineend set to $val ") if isdbg('connect');
314         $val =~ s/\\r/\r/g;
315         $val =~ s/\\n/\n/g;
316         $conn->{lineend} = $val;
317 }
318
319 sub _dochat
320 {
321         my $conn = shift;
322         my $cmd = shift;
323         my $line = shift;
324         my $expect = shift;
325         my $send = shift;
326                 
327         if ($line) {
328                 if ($expect) {
329                         dbg("connect $conn->{cnum}: expecting: \"$expect\" received: \"$line\"") if isdbg('connect');
330                         if ($conn->{abort} && $line =~ /\Q$conn->{abort}/i) {
331                                 dbg("connect $conn->{cnum}: aborted on /$conn->{abort}/") if isdbg('connect');
332                                 $conn->disconnect;
333                                 delete $conn->{cmd};
334                                 return;
335                         }
336                         if ($line =~ /\Q$expect/i) {
337                                 if (length $send) {
338                                         dbg("connect $conn->{cnum}: got: \"$expect\" sending: \"$send\"") if isdbg('connect');
339                                         $conn->send_later("D$conn->{call}|$send");
340                                 }
341                                 delete $conn->{msg}; # get rid any input if a match
342                                 return;
343                         }
344                 }
345         }
346         $conn->{state} = 'WC';
347         unshift @{$conn->{cmd}}, $cmd;
348 }
349
350 sub _timedout
351 {
352         my $conn = shift;
353         dbg("connect $conn->{cnum}: timed out after $conn->{timeval} seconds") if isdbg('connect');
354         $conn->disconnect;
355 }
356
357 # handle callsign and connection type firtling
358 sub _doclient
359 {
360         my $conn = shift;
361         my $line = shift;
362         my @f = split /\s+/, $line;
363         my $call = uc $f[0] if $f[0];
364         $conn->conns($call);
365         $conn->{csort} = $f[1] if $f[1];
366         $conn->{state} = 'C';
367         &{$conn->{rproc}}($conn, "O$call|$conn->{csort}");
368         delete $conn->{cmd};
369         $conn->{timeout}->del if $conn->{timeout};
370 }
371
372 sub _send_file
373 {
374         my $conn = shift;
375         my $fn = shift;
376         
377         if (-e $fn) {
378                 my $f = new IO::File $fn;
379                 if ($f) {
380                         while (<$f>) {
381                                 chomp;
382                                 my $l = $_;
383                                 dbg("connect $conn->{cnum}: $l") if isdbg('connll');
384                                 $conn->send_raw($l . $conn->{lineend});
385                         }
386                         $f->close;
387                 }
388         }
389 }