]> gb7djk.dxcluster.net Git - spider.git/blob - perl/ExtMsg.pm
f2d132645356c42e56e2778ae5feb15b9d15f1c6
[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
23 use vars qw(@ISA $deftimeout);
24
25 @ISA = qw(Msg);
26 $deftimeout = 60;
27
28 sub enqueue
29 {
30         my ($conn, $msg) = @_;
31         unless ($msg =~ /^[ABZ]/) {
32                 if ($msg =~ /^E[-\w]+\|([01])/) {
33                         $conn->{echo} = $1;
34                 } else {
35                         $msg =~ s/^[-\w]+\|//;
36                         push (@{$conn->{outqueue}}, $msg . $conn->{lineend});
37                 }
38         }
39 }
40
41 sub send_raw
42 {
43         my ($conn, $msg) = @_;
44     my $sock = $conn->{sock};
45     return unless defined($sock);
46         push (@{$conn->{outqueue}}, $msg);
47         dbg('connect', $msg) unless $conn->{state} eq 'C';
48     Msg::set_event_handler ($sock, "write" => sub {$conn->_send(0)});
49 }
50
51 sub dequeue
52 {
53         my $conn = shift;
54         my $msg;
55         
56         while ($msg = shift @{$conn->{inqueue}}){
57                 dbg('connect', $msg) unless $conn->{state} eq 'C';
58                 
59                 $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
60                 $msg =~ s/[\x00-\x08\x0a-\x1f\x80-\x9f]/./g;         # immutable CSI sequence + control characters
61
62                 if ($conn->{state} eq 'C') {
63                         &{$conn->{rproc}}($conn, "I$conn->{call}|$msg", $!);
64                         $! = 0;
65                 } elsif ($conn->{state} eq 'WL' ) {
66                         $msg = uc $msg;
67                         if (is_callsign($msg)) {
68                                 _send_file($conn, "$main::data/connected");
69                                 $conn->{call} = $msg;
70                                 &{$conn->{rproc}}($conn, "A$conn->{call}|telnet");
71                                 $conn->{state} = 'C';
72                         } else {
73                                 $conn->send_now("Sorry $msg is an invalid callsign");
74                                 $conn->disconnect;
75                         }
76                 } elsif ($conn->{state} eq 'WC') {
77                         if (exists $conn->{cmd} && @{$conn->{cmd}}) {
78                                 $conn->_docmd($msg);
79                                 unless (exists $conn->{cmd} && @{$conn->{cmd}}) {
80                                         $conn->{state} = 'C';
81                                         &{$conn->{rproc}}($conn, "O$conn->{call}|telnet");
82                                         delete $conn->{cmd};
83                                         $conn->{timeout}->del_timer if $conn->{timeout};
84                                 }
85                         }
86                 }
87         }
88         if ($conn->{msg} && $conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}}) {
89                 $conn->_docmd($conn->{msg});
90                 unless (@{$conn->{cmd}}) {
91                         $conn->{state} = 'C';
92                         &{$conn->{rproc}}($conn, "O$conn->{call}|telnet");
93                         delete $conn->{cmd};
94                         $conn->{timeout}->del_timer if $conn->{timeout};
95                 }
96         }
97 }
98
99 sub new_client {
100         my $server_conn = shift;
101     my $sock = $server_conn->{sock}->accept();
102     my $conn = $server_conn->new($server_conn->{rproc});
103         $conn->{sock} = $sock;
104
105     my $rproc = &{$server_conn->{rproc}} ($conn, $sock->peerhost(), $sock->peerport());
106     if ($rproc) {
107         $conn->{rproc} = $rproc;
108         my $callback = sub {$conn->_rcv};
109                 Msg::set_event_handler ($sock, "read" => $callback);
110                 # send login prompt
111                 $conn->{state} = 'WL';
112 #               $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
113 #               $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
114                 _send_file($conn, "$main::data/issue");
115                 $conn->send_raw("Login: ");
116     } else { 
117         $conn->disconnect();
118     }
119 }
120
121 sub start_connect
122 {
123         my $call = shift;
124         my $fn = shift;
125         my $conn = ExtMsg->new(\&main::rec); 
126         $conn->{call} = $call;
127         
128         my $f = new IO::File $fn;
129         push @{$conn->{cmd}}, <$f>;
130         $f->close;
131         push @main::outstanding_connects, {call => $call, conn => $conn};
132         $conn->_dotimeout($deftimeout);
133         $conn->_docmd;
134 }
135
136 sub _docmd
137 {
138         my $conn = shift;
139         my $msg = shift;
140         my $cmd;
141
142         while ($cmd = shift @{$conn->{cmd}}) {
143                 chomp $cmd;
144                 next if $cmd =~ /^\s*\#/o;
145                 next if $cmd =~ /^\s*$/o;
146                 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
147                 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
148                 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
149                 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
150                         unless ($conn->_doconnect($1, $2)) {
151                                 $conn->disconnect;
152                                 @{$conn->{cmd}} = [];    # empty any further commands
153                                 last;
154                         }  
155                 }
156                 if ($cmd =~ /^\s*\'.*\'\s+\'.*\'/i) {
157                         $conn->_dochat($cmd, $msg);
158                         last;
159                 }
160                 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
161                         $conn->_doclient($1);
162                         last;
163                 }
164                 last if $conn->{state} eq 'E';
165         }
166         unless (exists $conn->{cmd} && @{$conn->{cmd}}) {
167                 @main::outstanding_connects = grep {$_->{call} ne $conn->{call}} @main::outstanding_connects;
168         }
169 }
170
171 sub _doconnect
172 {
173         my ($conn, $sort, $line) = @_;
174         my $r;
175         
176         dbg('connect', "CONNECT sort: $sort command: $line");
177         if ($sort eq 'telnet') {
178                 # this is a straight network connect
179                 my ($host, $port) = split /\s+/, $line;
180                 $port = 23 if !$port;
181                 $r = $conn->connect($host, $port);
182                 if ($r) {
183                         dbg('connect', "Connected to $host $port");
184                 } else {
185                         dbg('connect', "***Connect Failed to $host $port $!");
186                 }
187         } elsif ($sort eq 'ax25' || $sort eq 'prog') {
188                 ;
189         } else {
190                 dbg('err', "invalid type of connection ($sort)");
191                 $conn->disconnect;
192         }
193         return $r;
194 }
195
196 sub _doabort
197 {
198         my $conn = shift;
199         my $string = shift;
200         dbg('connect', "abort $string");
201         $conn->{abort} = $string;
202 }
203
204 sub _dotimeout
205 {
206         my $conn = shift;
207         my $val = shift;
208         dbg('connect', "timeout set to $val");
209         $conn->{timeout}->del_timer if $conn->{timeout};
210         $conn->{timeout} = ExtMsg->new_timer($val, sub{ _timeout($conn); });
211         $conn->{timeval} = $val;
212 }
213
214 sub _dolineend
215 {
216         my $conn = shift;
217         my $val = shift;
218         dbg('connect', "lineend set to $val ");
219         $val =~ s/\\r/\r/g;
220         $val =~ s/\\n/\n/g;
221         $conn->{lineend} = $val;
222 }
223
224 sub _dochat
225 {
226         my $conn = shift;
227         my $cmd = shift;
228         my $line = shift;
229         
230         if ($line) {
231                 my ($expect, $send) = $cmd =~ /^\s*\'(.*)\'\s+\'(.*)\'/;
232                 if ($expect) {
233                         dbg('connect', "expecting: \"$expect\" received: \"$line\"");
234                         if ($conn->{abort} && $line =~ /$conn->{abort}/i) {
235                                 dbg('connect', "aborted on /$conn->{abort}/");
236                                 $conn->disconnect;
237                                 return;
238                         }
239                         if ($line =~ /$expect/i) {
240                                 dbg('connect', "got: \"$expect\" sending: \"$send\"");
241                                 $conn->send_later($send);
242                                 return;
243                         }
244                 }
245         }
246         $conn->{state} = 'WC';
247         unshift @{$conn->{cmd}}, $cmd;
248 }
249
250 sub _timeout
251 {
252         my $conn = shift;
253         dbg('connect', "timed out after $conn->{timeval} seconds");
254         $conn->disconnect;
255         @main::outstanding_connects = grep {$_->{call} ne $conn->{call}} @main::outstanding_connects;
256 }
257
258 # handle callsign and connection type firtling
259 sub _doclient
260 {
261         my $conn = shift;
262         my $line = shift;
263         my @f = split /\s+/, $line;
264         $conn->{call} = uc $f[0] if $f[0];
265         $conn->{csort} = $f[1] if $f[1];
266 }
267
268 sub _send_file
269 {
270         my $conn = shift;
271         my $fn = shift;
272         
273         if (-e $fn) {
274                 my $f = new IO::File $fn;
275                 if ($f) {
276                         while (<$f>) {
277                                 chomp;
278                                 $conn->send_later($_);
279                         }
280                         $f->close;
281                 }
282         }
283         $! = undef;
284 }