d431a0d12430a982516919eea23f9f23ac9ec10b
[spider.git] / perl / client.pl
1 #!/usr/bin/perl
2 #
3 # A thing that implements dxcluster 'protocol'
4 #
5 # This is a perl module/program that sits on the end of a dxcluster
6 # 'protocol' connection and deals with anything that might come along.
7 #
8 # this program is called by ax25d or inetd and gets raw ax25 text on its input
9 # It can also be launched into the ether by the cluster program itself for outgoing
10 # connections
11 #
12 # Calling syntax is:-
13 #
14 # client.pl [callsign] [telnet|ax25|local] [[connect] [program name and args ...]]
15 #
16 # if the callsign isn't given then the sysop callsign in DXVars.pm is assumed
17 #
18 # if there is no connection type then 'local' is assumed
19 #
20 # if there is a 'connect' keyword then it will try to launch the following program
21 # and any arguments and connect the stdin & stdout of both the program and the 
22 # client together.
23 #
24 # Copyright (c) 1998 Dirk Koopman G1TLH
25 #
26 # $Id$
27
28
29
30 # search local then perl directories
31 BEGIN {
32         # root of directory tree for this system
33         $root = "/spider"; 
34         $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
35         
36         unshift @INC, "$root/perl";     # this IS the right way round!
37         unshift @INC, "$root/local";
38 }
39
40 use Msg;
41 use DXVars;
42 use DXDebug;
43 use IO::File;
44 use IO::Socket;
45 use IPC::Open2;
46 use Net::Telnet qw(TELOPT_ECHO);
47 use Carp;
48
49 # cease communications
50 sub cease
51 {
52         my $sendz = shift;
53         if ($conn && $sendz) {
54                 $conn->send_now("Z$call|bye...\n");
55         }
56         $stdout->flush if $stdout;
57         kill(15, $pid) if $pid;
58         sleep(1);
59         exit(0);        
60 }
61
62 # terminate program from signal
63 sub sig_term
64 {
65         cease(1);
66 }
67
68 # terminate a child
69 sub sig_chld
70 {
71         $SIG{CHLD} = \&sig_chld;
72         $waitedpid = wait;
73 }
74
75
76 sub setmode
77 {
78         if ($mode == 1) {
79                 $mynl = "\r";
80         } else {
81                 $mynl = "\n";
82         }
83         $/ = $mynl;
84 }
85
86 # handle incoming messages
87 sub rec_socket
88 {
89         my ($con, $msg, $err) = @_;
90         if (defined $err && $err) {
91                 cease(1);
92         }
93         if (defined $msg) {
94                 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
95                 
96                 if ($sort eq 'D') {
97                         my $snl = $mynl;
98                         my $newsavenl = "";
99                         $snl = "" if $mode == 0;
100                         if ($mode == 2 && $line =~ />$/) {
101                                 $newsavenl = $snl;
102                                 $snl = ' ';
103                         }
104                         $line =~ s/\n/\r/og if $mode == 1;
105                         #my $p = qq($line$snl);
106                         if ($buffered) {
107                                 if (length $outqueue >= 128) {
108                                         print $stdout $outqueue;
109                                         $outqueue = "";
110                                 }
111                                 $outqueue .= "$savenl$line$snl";
112                                 $lasttime = time;
113                         } else {
114                                 print $stdout $savenl, $line, $snl;;
115                         }
116                         $savenl = $newsavenl;
117                 } elsif ($sort eq 'M') {
118                         $mode = $line;          # set new mode from cluster
119                         setmode();
120                 } elsif ($sort eq 'B') {
121                         if ($buffered && $outqueue) {
122                                 print $stdout $outqueue;
123                                 $outqueue = "";
124                         }
125                         $buffered = $line;      # set buffered or unbuffered
126                 } elsif ($sort eq 'Z') { # end, disconnect, go, away .....
127                         cease(0);
128                 }         
129         }
130         $lasttime = time; 
131 }
132
133 sub rec_stdin
134 {
135         my ($fh) = @_;
136         my $buf;
137         my @lines;
138         my $r;
139         my $first;
140         my $dangle = 0;
141         
142         $r = sysread($fh, $buf, 1024);
143         #  my $prbuf;
144         #  $prbuf = $buf;
145         #  $prbuf =~ s/\r/\\r/;
146         #  $prbuf =~ s/\n/\\n/;
147         #  print "sys: $r ($prbuf)\n";
148         if ($r > 0) {
149                 if ($mode) {
150                         $buf =~ s/\r/\n/og if $mode == 1;
151                         $dangle = !($buf =~ /\n$/);
152                         if ($buf eq "\n") {
153                                 @lines = (" ");
154                         } else {
155                                 @lines = split /\n/, $buf;
156                         }
157                         if ($dangle) {          # pull off any dangly bits
158                                 $buf = pop @lines;
159                         } else {
160                                 $buf = "";
161                         }
162                         $first = shift @lines;
163                         unshift @lines, ($lastbit . $first) if ($first);
164                         foreach $first (@lines) {
165                                 #                 print "send_now $call $first\n";
166                                 $conn->send_now("D$call|$first");
167                         }
168                         $lastbit = $buf;
169                         $savenl = "";           # reset savenl 'cos we will have done a newline on input
170                 } else {
171                         $conn->send_now("D$call|$buf");
172                 }
173         } elsif ($r == 0) {
174                 cease(1);
175         }
176         $lasttime = time;
177 }
178
179 sub doconnect
180 {
181         my ($sort, $line) = @_;
182         dbg('connect', "CONNECT sort: $sort command: $line");
183         if ($sort eq 'telnet') {
184                 # this is a straight network connect
185                 my ($host, $port) = split /\s+/, $line;
186                 $port = 23 if !$port;
187                 
188                 if ($port == 23) {
189                         $sock = new Net::Telnet (Timeout => $timeout, BinMode => 1);
190                         $sock->option_accept(Dont => TELOPT_ECHO, Wont => TELOPT_ECHO);
191                         #$sock->option_log('option_log');
192                         $sock->dump_log('dump');
193                         $sock->open($host) or die "Can't connect to $host port $port $!";
194                 } else {
195                         $sock = IO::Socket::INET->new(PeerAddr => "$host:$port", Proto => 'tcp')
196                                 or die "Can't connect to $host port $port $!";
197                         
198                 }
199         } elsif ($sort eq 'ax25') {
200                 my @args = split /\s+/, $line;
201                 $rfh = new IO::File;
202                 $wfh = new IO::File;
203                 $pid = open2($rfh, $wfh, "$line") or die "can't do $line $!";
204                 dbg('connect', "got pid $pid");
205                 $wfh->autoflush(1);
206         } else {
207                 die "invalid type of connection ($sort)";
208         }
209         $csort = $sort;
210 }
211
212 sub doabort
213 {
214         my $string = shift;
215         dbg('connect', "abort $string");
216         $abort = $string;
217 }
218
219 sub dotimeout
220 {
221         my $val = shift;
222         dbg('connect', "timeout set to $val");
223         $timeout = $val;
224 }
225
226 sub dochat
227 {
228         my ($expect, $send) = @_;
229         dbg('connect', "CHAT \"$expect\" -> \"$send\"");
230     my $line;
231         
232         alarm($timeout);
233         
234     if ($expect) {
235                 for (;;) {
236                         if ($csort eq 'telnet') {
237                                 $line = $sock->get();
238                                 chomp;
239                         } elsif ($csort eq 'ax25') {
240                                 local $/ = "\r";
241                                 $line = <$rfh>;
242                                 $line =~ s/\r//og;
243                         }
244                         dbg('connect', "received \"$line\"");
245                         if ($abort && $line =~ /$abort/i) {
246                                 dbg('connect', "aborted on /$abort/");
247                                 cease(11);
248                         }
249                         last if $line =~ /$expect/i;
250                 }
251         }
252         if ($send) {
253                 if ($csort eq 'telnet') {
254                         $sock->print("$send\n");
255                 } elsif ($csort eq 'ax25') {
256                         local $\ = "\r";
257                         $wfh->print("$send");
258                 }
259                 dbg('connect', "sent \"$send\"");
260         }
261 }
262
263 sub timeout
264 {
265         dbg('connect', "timed out after $timeout seconds");
266         cease(10);
267 }
268
269
270 #
271 # initialisation
272 #
273
274 $mode = 2;                      # 1 - \n = \r as EOL, 2 - \n = \n, 0 - transparent
275 $call = "";                     # the callsign being used
276 @stdoutq = ();                  # the queue of stuff to send out to the user
277 $conn = 0;                      # the connection object for the cluster
278 $lastbit = "";                  # the last bit of an incomplete input line
279 $mynl = "\n";                   # standard terminator
280 $lasttime = time;               # lasttime something happened on the interface
281 $outqueue = "";                 # the output queue length
282 $buffered = 1;                  # buffer output
283 $savenl = "";                   # an NL that has been saved from last time
284 $timeout = 60;                  # default timeout for connects
285 $abort = "";                    # the current abort string
286 $cpath = "$root/connect";               # the basic connect directory
287
288 $pid = 0;                       # the pid of the child program
289 $csort = "";                    # the connection type
290 $sock = 0;                      # connection socket
291
292 $stdin = *STDIN;
293 $stdout = *STDOUT;
294 $rfh = 0;
295 $wfh = 0;
296
297
298 #
299 # deal with args
300 #
301
302 $call = uc shift @ARGV;
303 $call = uc $myalias if !$call; 
304 $connsort = lc shift @ARGV;
305 $connsort = 'local' if !$connsort;
306
307 #
308 # strip off any SSID if it is a telnet connection 
309 #
310 # SSID's are a problem, basically we don't allow them EXCEPT for the special case
311 # of local users. i.e. you can have a cluster call with an SSID and a usercall with
312 # an SSID and they are different to the system to those without SSIDs
313 #
314
315 $call =~ s/-\d+$//o if $mode eq 'telnet';
316 $mode = ($connsort eq 'ax25') ? 1 : 2;
317 setmode();
318
319 if ($call eq $mycall) {
320         print $stdout "You cannot connect as your cluster callsign ($mycall)", $nl;
321         cease(0);
322 }
323
324 $stdout->autoflush(1);
325
326 $SIG{'INT'} = \&sig_term;
327 $SIG{'TERM'} = \&sig_term;
328 $SIG{'HUP'} = 'IGNORE';
329 $SIG{'CHLD'} = \&sig_chld;
330
331 dbgadd('connect');
332
333 # is this an out going connection?
334 if ($connsort eq "connect") {
335         my $mcall = lc $call;
336         
337         open(IN, "$cpath/$mcall") or cease(2);
338         @in = <IN>;
339         close IN;
340         
341         #       alarm($timeout);
342         
343         for (@in) {
344                 chomp;
345                 next if /^\s*\#/o;
346                 next if /^\s*$/o;
347                 doconnect($1, $2) if /^\s*co\w*\s+(\w+)\s+(.*)$/io;
348                 doabort($1) if /^\s*a\w*\s+(.*)/io;
349                 dotimeout($1) if /^\s*t\w*\s+(\d+)/io;
350                 dochat($1, $2) if /\s*\'(.*)\'\s+\'(.*)\'/io;          
351         }
352         
353     dbg('connect', "Connected to $call, starting normal protocol");
354         dbgsub('connect');
355         
356         # if we get here we are connected
357         if ($csort eq 'ax25') {
358                 #               open(STDIN, "<&R"); 
359                 #               open(STDOUT, ">&W"); 
360                 #               close R;
361                 #               close W;
362         $stdin = $rfh;
363                 $stdout = $wfh;
364         } elsif ($csort eq 'telnet') {
365                 #               open(STDIN, "<&$sock"); 
366                 #               open(STDOUT, ">&$sock"); 
367                 #               close $sock;
368                 $stdin = $sock;
369                 $stdout = $sock;
370         }
371     alarm(0);
372     $outbound = 1;
373         $connsort = $csort;
374         $stdout->autoflush(1);
375         close STDIN;
376         close STDOUT;
377         close STDERR;
378         
379         
380         $mode = ($connsort =~ /^ax/o) ? 1 : 2;
381         setmode();
382 }
383
384 setmode();
385
386 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
387 if (! $conn) {
388         if (-r "$data/offline") {
389                 open IN, "$data/offline" or die;
390                 while (<IN>) {
391                         s/\n/\r/og if $mode == 1;
392                         print $stdout;
393                 }
394                 close IN;
395         } else {
396                 print $stdout "Sorry, the cluster $mycall is currently off-line", $mynl;
397         }
398         cease(0);
399 }
400
401 $let = $outbound ? 'O' : 'A';
402 $conn->send_now("$let$call|$connsort");
403 Msg->set_event_handler($stdin, "read" => \&rec_stdin);
404
405 for (;;) {
406         my $t;
407         Msg->event_loop(1, 0.010);
408         $t = time;
409         if ($t > $lasttime) {
410                 if ($outqueue) {
411                         print $stdout $outqueue;
412                         $outqueue = "";
413                 }
414                 $lasttime = $t;
415         }
416 }
417
418 exit(0);