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->setflag( &POSIX::ISIG );
131 $term->setattr(fileno($sock), &POSIX::TCSANOW );
133 } elsif ($sort eq 'I') {
134 ; # ignore echoed I frames
135 } elsif ($sort eq 'B') {
136 if ($buffered && $outqueue) {
137 print $stdout $outqueue;
140 $buffered = $line; # set buffered or unbuffered
141 } elsif ($sort eq 'Z') { # end, disconnect, go, away .....
157 $r = sysread($fh, $buf, 1024);
160 # $prbuf =~ s/\r/\\r/;
161 # $prbuf =~ s/\n/\\n/;
162 # print "sys: $r ($prbuf)\n";
165 $buf =~ s/\r/\n/og if $mode == 1;
166 $buf =~ s/\r\n/\n/og if $mode == 2;
167 $dangle = !($buf =~ /\n$/);
171 @lines = split /\n/, $buf;
173 if ($dangle) { # pull off any dangly bits
178 $first = shift @lines;
179 unshift @lines, ($lastbit . $first) if ($first);
180 foreach $first (@lines) {
181 # print "send_now $call $first\n";
182 $conn->send_later("I$call|$first");
185 $savenl = ""; # reset savenl 'cos we will have done a newline on input
187 $conn->send_later("I$call|$buf");
201 my ($sort, $line) = @_;
202 dbg('connect', "CONNECT sort: $sort command: $line");
203 if ($sort eq 'telnet') {
204 # this is a straight network connect
205 my ($host, $port) = split /\s+/, $line;
206 $port = 23 if !$port;
209 $sock = new Net::Telnet (Timeout => $timeout, Port => $port);
210 $sock->option_callback(\&optioncb);
211 $sock->output_record_separator('');
212 $sock->option_log('option_log');
213 $sock->dump_log('dump');
214 $sock->option_accept(Wont => TELOPT_ECHO);
215 $sock->open($host) or die "Can't connect to $host port $port $!";
217 # $sock = IO::Socket::INET->new(PeerAddr => "$host:$port", Proto => 'tcp')
218 # or die "Can't connect to $host port $port $!";
220 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
221 my @args = split /\s+/, $line;
224 $pid = open2($rfh, $wfh, "$line") or die "can't do $line $!";
225 dbg('connect', "got pid $pid");
228 die "invalid type of connection ($sort)";
236 dbg('connect', "abort $string");
243 dbg('connect', "timeout set to $val");
249 my ($expect, $send) = @_;
250 dbg('connect', "CHAT \"$expect\" -> \"$send\"");
257 if ($csort eq 'telnet') {
258 $line = $sock->get();
259 $line =~ s/\r\n/\n/og;
261 } elsif ($csort eq 'ax25' || $csort eq 'prog') {
266 dbg('connect', "received \"$line\"");
267 if ($abort && $line =~ /$abort/i) {
268 dbg('connect', "aborted on /$abort/");
271 last if $line =~ /$expect/i;
275 if ($csort eq 'telnet') {
276 $sock->print("$send\n");
277 } elsif ($csort eq 'ax25') {
279 $wfh->print("$send");
281 dbg('connect', "sent \"$send\"");
287 dbg('connect', "timed out after $timeout seconds");
296 $mode = 2; # 1 - \n = \r as EOL, 2 - \n = \n, 0 - transparent
297 $call = ""; # the callsign being used
298 $conn = 0; # the connection object for the cluster
299 $lastbit = ""; # the last bit of an incomplete input line
300 $mynl = "\n"; # standard terminator
301 $lasttime = time; # lasttime something happened on the interface
302 $outqueue = ""; # the output queue
303 $client_buffer_lth = 200; # how many characters are buffered up on outqueue
304 $buffered = 1; # buffer output
305 $savenl = ""; # an NL that has been saved from last time
306 $timeout = 60; # default timeout for connects
307 $abort = ""; # the current abort string
308 $cpath = "$root/connect"; # the basic connect directory
310 $pid = 0; # the pid of the child program
311 $csort = ""; # the connection type
312 $sock = 0; # connection socket
325 $call = uc shift @ARGV;
326 $call = uc $myalias if !$call;
327 $connsort = lc shift @ARGV;
328 $connsort = 'local' if !$connsort;
330 $loginreq = $call eq 'LOGIN';
332 # we will do this again later 'cos things may have changed
333 $mode = ($connsort eq 'ax25') ? 1 : 2;
336 if ($call eq $mycall) {
337 print $stdout "You cannot connect as your cluster callsign ($mycall)", $nl;
341 $stdout->autoflush(1);
343 $SIG{'INT'} = \&sig_term;
344 $SIG{'TERM'} = \&sig_term;
345 $SIG{'HUP'} = 'IGNORE';
346 $SIG{'CHLD'} = \&sig_chld;
347 $SIG{'ALRM'} = \&timeout;
351 # do we need to do a login and password job?
356 if (-e "$data/issue") {
357 open(I, "$data/issue") or die;
361 $issue = s/\n/\r/og if $mode == 1;
363 $stdout->print($issue) if $issue;
369 DXUser->init($userfn);
371 # allow a login from an existing user. I could create a user but
372 # I want to check for valid callsigns and I don't have the
373 # necessary info / regular expression yet
374 for ($state = 0; $state < 2; ) {
378 $stdout->print('login: ');
381 $s = $stdin->getline();
384 $s =~ s/-\d+$//o; # no ssids!
385 cease(0) unless $s gt ' ';
387 $user = DXUser->get($call);
389 } elsif ($state == 1) {
390 $stdout->print('password: ');
393 $s = $stdin->getline();
396 if (!$user || ($user->passwd && $user->passwd ne $s)) {
397 $stdout->print("sorry...$nl");
404 # handle callsign and connection type firtling
408 my @f = split /\s+/, $line;
409 $call = uc $f[0] if $f[0];
410 $csort = $f[1] if $f[1];
413 # is this an out going connection?
414 if ($connsort eq "connect") {
415 my $mcall = lc $call;
417 open(IN, "$cpath/$mcall") or cease(2);
427 doconnect($1, $2) if /^\s*co\w*\s+(\w+)\s+(.*)$/io;
428 doabort($1) if /^\s*a\w*\s+(.*)/io;
429 dotimeout($1) if /^\s*t\w*\s+(\d+)/io;
430 dochat($1, $2) if /\s*\'(.*)\'\s+\'(.*)\'/io;
431 if (/\s*cl\w+\s+(.*)/io) {
437 dbg('connect', "Connected to $call ($csort), starting normal protocol");
440 # if we get here we are connected
441 if ($csort eq 'ax25' || $csort eq 'prog') {
442 # open(STDIN, "<&R");
443 # open(STDOUT, ">&W");
448 $csort = 'telnet' if $csort eq 'prog';
449 } elsif ($csort eq 'telnet') {
450 # open(STDIN, "<&$sock");
451 # open(STDOUT, ">&$sock");
459 $stdout->autoflush(1);
465 $mode = ($connsort eq 'ax25') ? 1 : 2;
468 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
470 if (-r "$data/offline") {
471 open IN, "$data/offline" or die;
473 s/\n/\r/og if $mode == 1;
478 print $stdout "Sorry, the cluster $mycall is currently off-line", $mynl;
483 $let = $outbound ? 'O' : 'A';
484 $conn->send_now("$let$call|$connsort");
485 Msg->set_event_handler($stdin, "read" => \&rec_stdin);
489 Msg->event_loop(1, 0.010);
491 if ($t > $lasttime) {
493 print $stdout $outqueue;