fixed problem caused by moving the command execution into a separate
[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::Socket;
44 use IPC::Open2;
45 use FileHandle;
46 use Carp;
47
48 # cease communications
49 sub cease
50 {
51         my $sendz = shift;
52         if ($conn && $sendz) {
53                 $conn->send_now("Z$call|bye...\n");
54         }
55         $stdout->flush if $stdout;
56         kill(15, $pid) if $pid;
57         sleep(1);
58         exit(0);        
59 }
60
61 # terminate program from signal
62 sub sig_term
63 {
64         cease(1);
65 }
66
67 # terminate a child
68 sub sig_chld
69 {
70         $SIG{CHLD} = \&sig_chld;
71         $waitedpid = wait;
72 }
73
74
75 sub setmode
76 {
77         if ($mode == 1) {
78                 $mynl = "\r";
79         } else {
80                 $mynl = "\n";
81         }
82         $/ = $mynl;
83 }
84
85 # handle incoming messages
86 sub rec_socket
87 {
88         my ($con, $msg, $err) = @_;
89         if (defined $err && $err) {
90                 cease(1);
91         }
92         if (defined $msg) {
93                 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
94                 
95                 if ($sort eq 'D') {
96                         my $snl = $mynl;
97                         my $newsavenl = "";
98                         $snl = "" if $mode == 0;
99                         if ($mode == 2 && $line =~ />$/) {
100                                 $newsavenl = $snl;
101                                 $snl = ' ';
102                         }
103                         $line =~ s/\n/\r/og if $mode == 1;
104                         #my $p = qq($line$snl);
105                         if ($buffered) {
106                                 if (length $outqueue >= 128) {
107                                         print $stdout $outqueue;
108                                         $outqueue = "";
109                                 }
110                                 $outqueue .= "$savenl$line$snl";
111                                 $lasttime = time;
112                         } else {
113                                 print $stdout $savenl, $line, $snl;;
114                         }
115                         $savenl = $newsavenl;
116                 } elsif ($sort eq 'M') {
117                         $mode = $line;          # set new mode from cluster
118                         setmode();
119                 } elsif ($sort eq 'B') {
120                         if ($buffered && $outqueue) {
121                                 print $stdout $outqueue;
122                                 $outqueue = "";
123                         }
124                         $buffered = $line;      # set buffered or unbuffered
125                 } elsif ($sort eq 'Z') { # end, disconnect, go, away .....
126                         cease(0);
127                 }         
128         }
129         $lasttime = time; 
130 }
131
132 sub rec_stdin
133 {
134         my ($fh) = @_;
135         my $buf;
136         my @lines;
137         my $r;
138         my $first;
139         my $dangle = 0;
140         
141         $r = sysread($fh, $buf, 1024);
142         #  my $prbuf;
143         #  $prbuf = $buf;
144         #  $prbuf =~ s/\r/\\r/;
145         #  $prbuf =~ s/\n/\\n/;
146         #  print "sys: $r ($prbuf)\n";
147         if ($r > 0) {
148                 if ($mode) {
149                         $buf =~ s/\r/\n/og if $mode == 1;
150                         $dangle = !($buf =~ /\n$/);
151                         if ($buf eq "\n") {
152                                 @lines = (" ");
153                         } else {
154                                 @lines = split /\n/, $buf;
155                         }
156                         if ($dangle) {          # pull off any dangly bits
157                                 $buf = pop @lines;
158                         } else {
159                                 $buf = "";
160                         }
161                         $first = shift @lines;
162                         unshift @lines, ($lastbit . $first) if ($first);
163                         foreach $first (@lines) {
164                                 #                 print "send_now $call $first\n";
165                                 $conn->send_now("D$call|$first");
166                         }
167                         $lastbit = $buf;
168                         $savenl = "";           # reset savenl 'cos we will have done a newline on input
169                 } else {
170                         $conn->send_now("D$call|$buf");
171                 }
172         } elsif ($r == 0) {
173                 cease(1);
174         }
175         $lasttime = time;
176 }
177
178 sub doconnect
179 {
180         my ($sort, $line) = @_;
181         dbg('connect', "CONNECT sort: $sort command: $line");
182         if ($sort eq 'telnet') {
183                 # this is a straight network connect
184                 my ($host) = $line =~ /host\s+(\w+)/o;
185                 my ($port) = $line =~ /port\s+(\d+)/o;
186                 $port = 23 if !$port;
187                 
188                 $sock = IO::Socket::INET->new(PeerAddr => "$host", PeerPort => "$port", Proto => 'tcp')
189                         or die "Can't connect to $host port $port $!";
190                 
191         } elsif ($sort eq 'ax25') {
192                 my @args = split /\s+/, $line;
193                 $rfh = new FileHandle;
194                 $wfh = new FileHandle;
195                 $pid = open2($rfh, $wfh, "$line") or die "can't do $line $!";
196                 dbg('connect', "got pid $pid");
197                 $wfh->autoflush(1);
198         } else {
199                 die "invalid type of connection ($sort)";
200         }
201         $csort = $sort;
202 }
203
204 sub doabort
205 {
206         my $string = shift;
207         dbg('connect', "abort $string");
208         $abort = $string;
209 }
210
211 sub dotimeout
212 {
213         my $val = shift;
214         dbg('connect', "timeout set to $val");
215         $timeout = $val;
216 }
217
218 sub dochat
219 {
220         my ($expect, $send) = @_;
221         dbg('connect', "CHAT \"$expect\" -> \"$send\"");
222     my $line;
223         
224         #       alarm($timeout);
225         
226     if ($expect) {
227                 if ($csort eq 'telnet') {
228                         $line = <$sock>;
229                         chomp;
230                 } elsif ($csort eq 'ax25') {
231                         local $/ = "\r";
232                         $line = <$rfh>;
233                         $line =~ s/\r//og;
234                 }
235                 dbg('connect', "received \"$line\"");
236                 if ($abort && $line =~ /$abort/i) {
237                         dbg('connect', "aborted on /$abort/");
238                         cease(11);
239                 }
240         }
241         if ($send && (!$expect || $line =~ /$expect/i)) {
242                 if ($csort eq 'telnet') {
243                         $sock->print("$send\n");
244                 } elsif ($csort eq 'ax25') {
245                         local $\ = "\r";
246                         $wfh->print("$send\r");
247                 }
248                 dbg('connect', "sent \"$send\"");
249         }
250 }
251
252 sub timeout
253 {
254         dbg('connect', "timed out after $timeout seconds");
255         cease(10);
256 }
257
258
259 #
260 # initialisation
261 #
262
263 $mode = 2;                      # 1 - \n = \r as EOL, 2 - \n = \n, 0 - transparent
264 $call = "";                     # the callsign being used
265 @stdoutq = ();                  # the queue of stuff to send out to the user
266 $conn = 0;                      # the connection object for the cluster
267 $lastbit = "";                  # the last bit of an incomplete input line
268 $mynl = "\n";                   # standard terminator
269 $lasttime = time;               # lasttime something happened on the interface
270 $outqueue = "";                 # the output queue length
271 $buffered = 1;                  # buffer output
272 $savenl = "";                   # an NL that has been saved from last time
273 $timeout = 30;                  # default timeout for connects
274 $abort = "";                    # the current abort string
275 $cpath = "$root/connect";               # the basic connect directory
276
277 $pid = 0;                       # the pid of the child program
278 $csort = "";                    # the connection type
279 $sock = 0;                      # connection socket
280
281 $stdin = *STDIN;
282 $stdout = *STDOUT;
283 $rfh = 0;
284 $wfh = 0;
285
286
287 #
288 # deal with args
289 #
290
291 $call = uc shift @ARGV;
292 $call = uc $myalias if !$call; 
293 $connsort = lc shift @ARGV;
294 $connsort = 'local' if !$connsort;
295
296 $mode = ($connsort =~ /^ax/o) ? 1 : 2;
297 setmode();
298
299 if ($call eq $mycall) {
300         print $stdout "You cannot connect as your cluster callsign ($mycall)", $nl;
301         cease(0);
302 }
303
304 $stdout->autoflush(1);
305
306 $SIG{'INT'} = \&sig_term;
307 $SIG{'TERM'} = \&sig_term;
308 $SIG{'HUP'} = 'IGNORE';
309 $SIG{'CHLD'} = \&sig_chld;
310
311 dbgadd('connect');
312
313 # is this an out going connection?
314 if ($connsort eq "connect") {
315         my $mcall = lc $call;
316         
317         open(IN, "$cpath/$mcall") or cease(2);
318         @in = <IN>;
319         close IN;
320         
321         #       alarm($timeout);
322         
323         for (@in) {
324                 chomp;
325                 next if /^\s*\#/o;
326                 next if /^\s*$/o;
327                 doconnect($1, $2) if /^\s*co\w*\s+(\w+)\s+(.*)$/io;
328                 doabort($1) if /^\s*a\w*\s+(.*)/io;
329                 dotimeout($1) if /^\s*t\w*\s+(\d+)/io;
330                 dochat($1, $2) if /\s*\'(.*)\'\s+\'(.*)\'/io;          
331         }
332         
333     dbg('connect', "Connected to $call, starting normal protocol");
334         dbgsub('connect');
335         
336         # if we get here we are connected
337         if ($csort eq 'ax25') {
338                 #               open(STDIN, "<&R"); 
339                 #               open(STDOUT, ">&W"); 
340                 #               close R;
341                 #               close W;
342         $stdin = $rfh;
343                 $stdout = $wfh;
344         } elsif ($csort eq 'telnet') {
345                 #               open(STDIN, "<&$sock"); 
346                 #               open(STDOUT, ">&$sock"); 
347                 #               close $sock;
348                 $stdin = $sock;
349                 $stdout = $sock;
350         }
351     alarm(0);
352     $outbound = 1;
353         $connsort = $csort;
354         $stdout->autoflush(1);
355         close STDIN;
356         close STDOUT;
357         close STDERR;
358         
359         
360         $mode = ($connsort =~ /^ax/o) ? 1 : 2;
361         setmode();
362 }
363
364 setmode();
365
366 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
367 if (! $conn) {
368         if (-r "$data/offline") {
369                 open IN, "$data/offline" or die;
370                 while (<IN>) {
371                         s/\n/\r/og if $mode == 1;
372                         print $stdout;
373                 }
374                 close IN;
375         } else {
376                 print $stdout "Sorry, the cluster $mycall is currently off-line", $mynl;
377         }
378         cease(0);
379 }
380
381 $let = $outbound ? 'O' : 'A';
382 $conn->send_now("$let$call|$connsort");
383 Msg->set_event_handler($stdin, "read" => \&rec_stdin);
384
385 for (;;) {
386         my $t;
387         Msg->event_loop(1, 0.010);
388         $t = time;
389         if ($t > $lasttime) {
390                 if ($outqueue) {
391                         print $stdout $outqueue;
392                         $outqueue = "";
393                 }
394                 $lasttime = $t;
395         }
396 }
397
398 exit(0);