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
31 # search local then perl directories
33 # root of directory tree for this system
35 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
37 unshift @INC, "$root/perl"; # this IS the right way round!
38 unshift @INC, "$root/local";
47 use Net::Telnet qw(TELOPT_ECHO);
50 # cease communications
54 if ($conn && $sendz) {
55 $conn->send_now("Z$call|bye...\n");
57 $stdout->flush if $stdout;
59 dbg('connect', "killing $pid");
66 # terminate program from signal
75 $SIG{CHLD} = \&sig_chld;
77 dbg('connect', "caught $pid");
91 # handle incoming messages
94 my ($con, $msg, $err) = @_;
95 if (defined $err && $err) {
99 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
104 $snl = "" if $mode == 0;
105 if ($mode == 2 && $line =~ />$/) {
109 $line =~ s/\n/\r/og if $mode == 1;
110 #my $p = qq($line$snl);
112 if (length $outqueue >= $client_buffer_lth) {
113 print $stdout $outqueue;
116 $outqueue .= "$savenl$line$snl";
119 print $stdout $savenl, $line, $snl;;
121 $savenl = $newsavenl;
122 } elsif ($sort eq 'M') {
123 $mode = $line; # set new mode from cluster
125 } elsif ($sort eq 'E') {
126 if ($sort eq 'telnet') {
127 $mode = $line; # set echo mode from cluster
128 my $term = POSIX::Termios->new;
129 $term->getattr(fileno($sock));
130 $term->setiflag( 0 );
131 $term->setoflag( 0 );
132 $term->setattr(fileno($sock), &POSIX::TCSANOW );
134 } elsif ($sort eq 'I') {
135 ; # ignore echoed I frames
136 } elsif ($sort eq 'B') {
137 if ($buffered && $outqueue) {
138 print $stdout $outqueue;
141 $buffered = $line; # set buffered or unbuffered
142 } elsif ($sort eq 'Z') { # end, disconnect, go, away .....
158 $r = sysread($fh, $buf, 1024);
161 # $prbuf =~ s/\r/\\r/;
162 # $prbuf =~ s/\n/\\n/;
163 # print "sys: $r ($prbuf)\n";
166 $buf =~ s/\r/\n/og if $mode == 1;
167 $buf =~ s/\r\n/\n/og if $mode == 2;
168 $dangle = !($buf =~ /\n$/);
172 @lines = split /\n/, $buf;
174 if ($dangle) { # pull off any dangly bits
179 $first = shift @lines;
180 unshift @lines, ($lastbit . $first) if ($first);
181 foreach $first (@lines) {
182 # print "send_now $call $first\n";
183 $conn->send_later("I$call|$first");
186 $savenl = ""; # reset savenl 'cos we will have done a newline on input
188 $conn->send_later("I$call|$buf");
202 my ($sort, $line) = @_;
203 dbg('connect', "CONNECT sort: $sort command: $line");
204 if ($sort eq 'telnet') {
205 # this is a straight network connect
206 my ($host, $port) = split /\s+/, $line;
207 $port = 23 if !$port;
210 $sock = new Net::Telnet (Timeout => $timeout, Port => $port);
211 $sock->option_callback(\&optioncb);
212 $sock->output_record_separator('');
213 $sock->option_log('option_log');
214 $sock->dump_log('dump');
215 $sock->option_accept(Wont => TELOPT_ECHO);
216 $sock->open($host) or die "Can't connect to $host port $port $!";
218 # $sock = IO::Socket::INET->new(PeerAddr => "$host:$port", Proto => 'tcp')
219 # or die "Can't connect to $host port $port $!";
221 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
222 my @args = split /\s+/, $line;
225 $pid = open2($rfh, $wfh, "$line") or die "can't do $line $!";
226 dbg('connect', "got pid $pid");
229 die "invalid type of connection ($sort)";
237 dbg('connect', "abort $string");
244 dbg('connect', "timeout set to $val");
250 my ($expect, $send) = @_;
251 dbg('connect', "CHAT \"$expect\" -> \"$send\"");
258 if ($csort eq 'telnet') {
259 $line = $sock->get();
260 $line =~ s/\r\n/\n/og;
262 } elsif ($csort eq 'ax25' || $csort eq 'prog') {
267 dbg('connect', "received \"$line\"");
268 if ($abort && $line =~ /$abort/i) {
269 dbg('connect', "aborted on /$abort/");
272 last if $line =~ /$expect/i;
276 if ($csort eq 'telnet') {
277 $sock->print("$send\n");
278 } elsif ($csort eq 'ax25') {
280 $wfh->print("$send");
282 dbg('connect', "sent \"$send\"");
288 dbg('connect', "timed out after $timeout seconds");
297 $mode = 2; # 1 - \n = \r as EOL, 2 - \n = \n, 0 - transparent
298 $call = ""; # the callsign being used
299 $conn = 0; # the connection object for the cluster
300 $lastbit = ""; # the last bit of an incomplete input line
301 $mynl = "\n"; # standard terminator
302 $lasttime = time; # lasttime something happened on the interface
303 $outqueue = ""; # the output queue
304 $client_buffer_lth = 200; # how many characters are buffered up on outqueue
305 $buffered = 1; # buffer output
306 $savenl = ""; # an NL that has been saved from last time
307 $timeout = 60; # default timeout for connects
308 $abort = ""; # the current abort string
309 $cpath = "$root/connect"; # the basic connect directory
311 $pid = 0; # the pid of the child program
312 $csort = ""; # the connection type
313 $sock = 0; # connection socket
326 $call = uc shift @ARGV;
327 $call = uc $myalias if !$call;
328 $connsort = lc shift @ARGV;
329 $connsort = 'local' if !$connsort;
331 $loginreq = $call eq 'LOGIN';
333 # we will do this again later 'cos things may have changed
334 $mode = ($connsort eq 'ax25') ? 1 : 2;
337 if ($call eq $mycall) {
338 print $stdout "You cannot connect as your cluster callsign ($mycall)", $nl;
342 $stdout->autoflush(1);
344 $SIG{'INT'} = \&sig_term;
345 $SIG{'TERM'} = \&sig_term;
346 $SIG{'HUP'} = 'IGNORE';
347 $SIG{'CHLD'} = \&sig_chld;
348 $SIG{'ALRM'} = \&timeout;
352 # do we need to do a login and password job?
357 if (-e "$data/issue") {
358 open(I, "$data/issue") or die;
362 $issue = s/\n/\r/og if $mode == 1;
364 $stdout->print($issue) if $issue;
370 DXUser->init($userfn);
372 # allow a login from an existing user. I could create a user but
373 # I want to check for valid callsigns and I don't have the
374 # necessary info / regular expression yet
375 for ($state = 0; $state < 2; ) {
379 $stdout->print('login: ');
382 $s = $stdin->getline();
385 $s =~ s/-\d+$//o; # no ssids!
386 cease(0) unless $s gt ' ';
388 $user = DXUser->get($call);
390 } elsif ($state == 1) {
391 $stdout->print('password: ');
394 $s = $stdin->getline();
397 if (!$user || ($user->passwd && $user->passwd ne $s)) {
398 $stdout->print("sorry...$nl");
405 # handle callsign and connection type firtling
409 my @f = split /\s+/, $line;
410 $call = uc $f[0] if $f[0];
411 $csort = $f[1] if $f[1];
414 # is this an out going connection?
415 if ($connsort eq "connect") {
416 my $mcall = lc $call;
418 open(IN, "$cpath/$mcall") or cease(2);
428 doconnect($1, $2) if /^\s*co\w*\s+(\w+)\s+(.*)$/io;
429 doabort($1) if /^\s*a\w*\s+(.*)/io;
430 dotimeout($1) if /^\s*t\w*\s+(\d+)/io;
431 dochat($1, $2) if /\s*\'(.*)\'\s+\'(.*)\'/io;
432 if (/\s*cl\w+\s+(.*)/io) {
438 dbg('connect', "Connected to $call ($csort), starting normal protocol");
441 # if we get here we are connected
442 if ($csort eq 'ax25' || $csort eq 'prog') {
443 # open(STDIN, "<&R");
444 # open(STDOUT, ">&W");
449 $csort = 'telnet' if $csort eq 'prog';
450 } elsif ($csort eq 'telnet') {
451 # open(STDIN, "<&$sock");
452 # open(STDOUT, ">&$sock");
460 $stdout->autoflush(1);
466 $mode = ($connsort eq 'ax25') ? 1 : 2;
469 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
471 if (-r "$data/offline") {
472 open IN, "$data/offline" or die;
474 s/\n/\r/og if $mode == 1;
479 print $stdout "Sorry, the cluster $mycall is currently off-line", $mynl;
484 $let = $outbound ? 'O' : 'A';
485 $conn->send_now("$let$call|$connsort");
486 Msg->set_event_handler($stdin, "read" => \&rec_stdin);
490 Msg->event_loop(1, 0.010);
492 if ($t > $lasttime) {
494 print $stdout $outqueue;