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 'E') {
121 if ($sort eq 'telnet') {
122 $mode = $line; # set echo mode from cluster
123 my $term = POSIX::Termios->new;
124 $term->getattr(fileno($sock));
125 $term->setflag( &POSIX::ISIG );
126 $term->setattr(fileno($sock), &POSIX::TCSANOW );
128 } elsif ($sort eq 'I') {
129 ; # ignore echoed I frames
130 } elsif ($sort eq 'B') {
131 if ($buffered && $outqueue) {
132 print $stdout $outqueue;
135 $buffered = $line; # set buffered or unbuffered
136 } elsif ($sort eq 'Z') { # end, disconnect, go, away .....
152 $r = sysread($fh, $buf, 1024);
155 # $prbuf =~ s/\r/\\r/;
156 # $prbuf =~ s/\n/\\n/;
157 # print "sys: $r ($prbuf)\n";
160 $buf =~ s/\r/\n/og if $mode == 1;
161 $dangle = !($buf =~ /\n$/);
165 @lines = split /\n/, $buf;
167 if ($dangle) { # pull off any dangly bits
172 $first = shift @lines;
173 unshift @lines, ($lastbit . $first) if ($first);
174 foreach $first (@lines) {
175 # print "send_now $call $first\n";
176 $conn->send_now("I$call|$first");
179 $savenl = ""; # reset savenl 'cos we will have done a newline on input
181 $conn->send_now("I$call|$buf");
191 my ($sort, $line) = @_;
192 dbg('connect', "CONNECT sort: $sort command: $line");
193 if ($sort eq 'telnet') {
194 # this is a straight network connect
195 my ($host, $port) = split /\s+/, $line;
196 $port = 23 if !$port;
199 $sock = new Net::Telnet (Timeout => $timeout, BinMode => 1);
200 $sock->option_accept(Dont => TELOPT_ECHO, Wont => TELOPT_ECHO);
201 $sock->option_log('option_log');
202 $sock->dump_log('dump');
203 $sock->open($host) or die "Can't connect to $host port $port $!";
205 $sock = IO::Socket::INET->new(PeerAddr => "$host:$port", Proto => 'tcp')
206 or die "Can't connect to $host port $port $!";
209 } elsif ($sort eq 'ax25') {
210 my @args = split /\s+/, $line;
213 $pid = open2($rfh, $wfh, "$line") or die "can't do $line $!";
214 dbg('connect', "got pid $pid");
217 die "invalid type of connection ($sort)";
225 dbg('connect', "abort $string");
232 dbg('connect', "timeout set to $val");
238 my ($expect, $send) = @_;
239 dbg('connect', "CHAT \"$expect\" -> \"$send\"");
246 if ($csort eq 'telnet') {
247 $line = $sock->get();
249 } elsif ($csort eq 'ax25') {
254 dbg('connect', "received \"$line\"");
255 if ($abort && $line =~ /$abort/i) {
256 dbg('connect', "aborted on /$abort/");
259 last if $line =~ /$expect/i;
263 if ($csort eq 'telnet') {
264 $sock->print("$send\n");
265 } elsif ($csort eq 'ax25') {
267 $wfh->print("$send");
269 dbg('connect', "sent \"$send\"");
275 dbg('connect', "timed out after $timeout seconds");
284 $mode = 2; # 1 - \n = \r as EOL, 2 - \n = \n, 0 - transparent
285 $call = ""; # the callsign being used
286 @stdoutq = (); # the queue of stuff to send out to the user
287 $conn = 0; # the connection object for the cluster
288 $lastbit = ""; # the last bit of an incomplete input line
289 $mynl = "\n"; # standard terminator
290 $lasttime = time; # lasttime something happened on the interface
291 $outqueue = ""; # the output queue length
292 $buffered = 1; # buffer output
293 $savenl = ""; # an NL that has been saved from last time
294 $timeout = 60; # default timeout for connects
295 $abort = ""; # the current abort string
296 $cpath = "$root/connect"; # the basic connect directory
298 $pid = 0; # the pid of the child program
299 $csort = ""; # the connection type
300 $sock = 0; # connection socket
312 $call = uc shift @ARGV;
313 $call = uc $myalias if !$call;
314 $connsort = lc shift @ARGV;
315 $connsort = 'local' if !$connsort;
318 # strip off any SSID if it is a telnet connection
320 # SSID's are a problem, basically we don't allow them EXCEPT for the special case
321 # of local users. i.e. you can have a cluster call with an SSID and a usercall with
322 # an SSID and they are different to the system to those without SSIDs
325 $call =~ s/-\d+$//o if $mode eq 'telnet';
326 $mode = ($connsort eq 'ax25') ? 1 : 2;
329 if ($call eq $mycall) {
330 print $stdout "You cannot connect as your cluster callsign ($mycall)", $nl;
334 $stdout->autoflush(1);
336 $SIG{'INT'} = \&sig_term;
337 $SIG{'TERM'} = \&sig_term;
338 $SIG{'HUP'} = 'IGNORE';
339 $SIG{'CHLD'} = \&sig_chld;
343 # is this an out going connection?
344 if ($connsort eq "connect") {
345 my $mcall = lc $call;
347 open(IN, "$cpath/$mcall") or cease(2);
357 doconnect($1, $2) if /^\s*co\w*\s+(\w+)\s+(.*)$/io;
358 doabort($1) if /^\s*a\w*\s+(.*)/io;
359 dotimeout($1) if /^\s*t\w*\s+(\d+)/io;
360 dochat($1, $2) if /\s*\'(.*)\'\s+\'(.*)\'/io;
363 dbg('connect', "Connected to $call, starting normal protocol");
366 # if we get here we are connected
367 if ($csort eq 'ax25') {
368 # open(STDIN, "<&R");
369 # open(STDOUT, ">&W");
374 } elsif ($csort eq 'telnet') {
375 # open(STDIN, "<&$sock");
376 # open(STDOUT, ">&$sock");
384 $stdout->autoflush(1);
390 $mode = ($connsort =~ /^ax/o) ? 1 : 2;
396 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
398 if (-r "$data/offline") {
399 open IN, "$data/offline" or die;
401 s/\n/\r/og if $mode == 1;
406 print $stdout "Sorry, the cluster $mycall is currently off-line", $mynl;
411 $let = $outbound ? 'O' : 'A';
412 $conn->send_now("$let$call|$connsort");
413 Msg->set_event_handler($stdin, "read" => \&rec_stdin);
417 Msg->event_loop(1, 0.010);
419 if ($t > $lasttime) {
421 print $stdout $outqueue;