3 # A thing that implements dxcluster 'protocol'
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.
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
14 # client.pl [callsign] [telnet|ax25|local] [[connect] [program name and args ...]]
16 # if the callsign isn't given then the sysop callsign in DXVars.pm is assumed
18 # if there is no connection type then 'local' is assumed
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
24 # Copyright (c) 1998 Dirk Koopman G1TLH
30 # search local then perl directories
32 # root of directory tree for this system
34 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
36 unshift @INC, "$root/perl"; # this IS the right way round!
37 unshift @INC, "$root/local";
46 use Net::Telnet qw(TELOPT_ECHO);
49 # cease communications
53 if ($conn && $sendz) {
54 $conn->send_now("Z$call|bye...\n");
56 $stdout->flush if $stdout;
57 kill(15, $pid) if $pid;
62 # terminate program from signal
71 $SIG{CHLD} = \&sig_chld;
86 # handle incoming messages
89 my ($con, $msg, $err) = @_;
90 if (defined $err && $err) {
94 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
99 $snl = "" if $mode == 0;
100 if ($mode == 2 && $line =~ />$/) {
104 $line =~ s/\n/\r/og if $mode == 1;
105 #my $p = qq($line$snl);
107 if (length $outqueue >= 128) {
108 print $stdout $outqueue;
111 $outqueue .= "$savenl$line$snl";
114 print $stdout $savenl, $line, $snl;;
116 $savenl = $newsavenl;
117 } elsif ($sort eq 'M') {
118 $mode = $line; # set new mode from cluster
120 } elsif ($sort eq 'B') {
121 if ($buffered && $outqueue) {
122 print $stdout $outqueue;
125 $buffered = $line; # set buffered or unbuffered
126 } elsif ($sort eq 'Z') { # end, disconnect, go, away .....
142 $r = sysread($fh, $buf, 1024);
145 # $prbuf =~ s/\r/\\r/;
146 # $prbuf =~ s/\n/\\n/;
147 # print "sys: $r ($prbuf)\n";
150 $buf =~ s/\r/\n/og if $mode == 1;
151 $dangle = !($buf =~ /\n$/);
155 @lines = split /\n/, $buf;
157 if ($dangle) { # pull off any dangly bits
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");
169 $savenl = ""; # reset savenl 'cos we will have done a newline on input
171 $conn->send_now("D$call|$buf");
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;
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 $!";
195 $sock = IO::Socket::INET->new(PeerAddr => "$host:$port", Proto => 'tcp')
196 or die "Can't connect to $host port $port $!";
199 } elsif ($sort eq 'ax25') {
200 my @args = split /\s+/, $line;
203 $pid = open2($rfh, $wfh, "$line") or die "can't do $line $!";
204 dbg('connect', "got pid $pid");
207 die "invalid type of connection ($sort)";
215 dbg('connect', "abort $string");
222 dbg('connect', "timeout set to $val");
228 my ($expect, $send) = @_;
229 dbg('connect', "CHAT \"$expect\" -> \"$send\"");
236 if ($csort eq 'telnet') {
237 $line = $sock->get();
239 } elsif ($csort eq 'ax25') {
244 dbg('connect', "received \"$line\"");
245 if ($abort && $line =~ /$abort/i) {
246 dbg('connect', "aborted on /$abort/");
249 last if $line =~ /$expect/i;
253 if ($csort eq 'telnet') {
254 $sock->print("$send\n");
255 } elsif ($csort eq 'ax25') {
257 $wfh->print("$send");
259 dbg('connect', "sent \"$send\"");
265 dbg('connect', "timed out after $timeout seconds");
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
288 $pid = 0; # the pid of the child program
289 $csort = ""; # the connection type
290 $sock = 0; # connection socket
302 $call = uc shift @ARGV;
303 $call = uc $myalias if !$call;
304 $connsort = lc shift @ARGV;
305 $connsort = 'local' if !$connsort;
308 # strip off any SSID if it is a telnet connection
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
315 $call =~ s/-\d+$//o if $mode eq 'telnet';
316 $mode = ($connsort eq 'ax25') ? 1 : 2;
319 if ($call eq $mycall) {
320 print $stdout "You cannot connect as your cluster callsign ($mycall)", $nl;
324 $stdout->autoflush(1);
326 $SIG{'INT'} = \&sig_term;
327 $SIG{'TERM'} = \&sig_term;
328 $SIG{'HUP'} = 'IGNORE';
329 $SIG{'CHLD'} = \&sig_chld;
333 # is this an out going connection?
334 if ($connsort eq "connect") {
335 my $mcall = lc $call;
337 open(IN, "$cpath/$mcall") or cease(2);
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;
353 dbg('connect', "Connected to $call, starting normal protocol");
356 # if we get here we are connected
357 if ($csort eq 'ax25') {
358 # open(STDIN, "<&R");
359 # open(STDOUT, ">&W");
364 } elsif ($csort eq 'telnet') {
365 # open(STDIN, "<&$sock");
366 # open(STDOUT, ">&$sock");
374 $stdout->autoflush(1);
380 $mode = ($connsort =~ /^ax/o) ? 1 : 2;
386 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
388 if (-r "$data/offline") {
389 open IN, "$data/offline" or die;
391 s/\n/\r/og if $mode == 1;
396 print $stdout "Sorry, the cluster $mycall is currently off-line", $mynl;
401 $let = $outbound ? 'O' : 'A';
402 $conn->send_now("$let$call|$connsort");
403 Msg->set_event_handler($stdin, "read" => \&rec_stdin);
407 Msg->event_loop(1, 0.010);
409 if ($t > $lasttime) {
411 print $stdout $outqueue;