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";
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;
58 kill(15, $pid) if $pid;
63 # terminate program from signal
72 $SIG{CHLD} = \&sig_chld;
87 # handle incoming messages
90 my ($con, $msg, $err) = @_;
91 if (defined $err && $err) {
95 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
100 $snl = "" if $mode == 0;
101 if ($mode == 2 && $line =~ />$/) {
105 $line =~ s/\n/\r/og if $mode == 1;
106 #my $p = qq($line$snl);
108 if (length $outqueue >= 128) {
109 print $stdout $outqueue;
112 $outqueue .= "$savenl$line$snl";
115 print $stdout $savenl, $line, $snl;;
117 $savenl = $newsavenl;
118 } elsif ($sort eq 'M') {
119 $mode = $line; # set new mode from cluster
121 } elsif ($sort eq 'E') {
122 if ($sort eq 'telnet') {
123 $mode = $line; # set echo mode from cluster
124 my $term = POSIX::Termios->new;
125 $term->getattr(fileno($sock));
126 $term->setflag( &POSIX::ISIG );
127 $term->setattr(fileno($sock), &POSIX::TCSANOW );
129 } elsif ($sort eq 'I') {
130 ; # ignore echoed I frames
131 } elsif ($sort eq 'B') {
132 if ($buffered && $outqueue) {
133 print $stdout $outqueue;
136 $buffered = $line; # set buffered or unbuffered
137 } elsif ($sort eq 'Z') { # end, disconnect, go, away .....
153 $r = sysread($fh, $buf, 1024);
156 # $prbuf =~ s/\r/\\r/;
157 # $prbuf =~ s/\n/\\n/;
158 # print "sys: $r ($prbuf)\n";
161 $buf =~ s/\r/\n/og if $mode == 1;
162 $buf =~ s/\r\n/\n/og if $mode == 2;
163 $dangle = !($buf =~ /\n$/);
167 @lines = split /\n/, $buf;
169 if ($dangle) { # pull off any dangly bits
174 $first = shift @lines;
175 unshift @lines, ($lastbit . $first) if ($first);
176 foreach $first (@lines) {
177 # print "send_now $call $first\n";
178 $conn->send_later("I$call|$first");
181 $savenl = ""; # reset savenl 'cos we will have done a newline on input
183 $conn->send_later("I$call|$buf");
197 my ($sort, $line) = @_;
198 dbg('connect', "CONNECT sort: $sort command: $line");
199 if ($sort eq 'telnet') {
200 # this is a straight network connect
201 my ($host, $port) = split /\s+/, $line;
202 $port = 23 if !$port;
205 $sock = new Net::Telnet (Timeout => $timeout);
206 $sock->option_callback(\&optioncb);
207 $sock->output_record_separator('');
208 $sock->option_log('option_log');
209 $sock->dump_log('dump');
210 $sock->option_accept(Wont => TELOPT_ECHO);
211 $sock->open($host) or die "Can't connect to $host port $port $!";
213 $sock = IO::Socket::INET->new(PeerAddr => "$host:$port", Proto => 'tcp')
214 or die "Can't connect to $host port $port $!";
217 } elsif ($sort eq 'ax25' || $sort eq 'prog') {
218 my @args = split /\s+/, $line;
221 $pid = open2($rfh, $wfh, "$line") or die "can't do $line $!";
222 dbg('connect', "got pid $pid");
225 die "invalid type of connection ($sort)";
233 dbg('connect', "abort $string");
240 dbg('connect', "timeout set to $val");
246 my ($expect, $send) = @_;
247 dbg('connect', "CHAT \"$expect\" -> \"$send\"");
254 if ($csort eq 'telnet') {
255 $line = $sock->get();
256 $line =~ s/\r\n/\n/og;
258 } elsif ($csort eq 'ax25' || $csort eq 'prog') {
263 dbg('connect', "received \"$line\"");
264 if ($abort && $line =~ /$abort/i) {
265 dbg('connect', "aborted on /$abort/");
268 last if $line =~ /$expect/i;
272 if ($csort eq 'telnet') {
273 $sock->print("$send\n");
274 } elsif ($csort eq 'ax25') {
276 $wfh->print("$send");
278 dbg('connect', "sent \"$send\"");
284 dbg('connect', "timed out after $timeout seconds");
293 $mode = 2; # 1 - \n = \r as EOL, 2 - \n = \n, 0 - transparent
294 $call = ""; # the callsign being used
295 @stdoutq = (); # the queue of stuff to send out to the user
296 $conn = 0; # the connection object for the cluster
297 $lastbit = ""; # the last bit of an incomplete input line
298 $mynl = "\n"; # standard terminator
299 $lasttime = time; # lasttime something happened on the interface
300 $outqueue = ""; # the output queue length
301 $buffered = 1; # buffer output
302 $savenl = ""; # an NL that has been saved from last time
303 $timeout = 60; # default timeout for connects
304 $abort = ""; # the current abort string
305 $cpath = "$root/connect"; # the basic connect directory
307 $pid = 0; # the pid of the child program
308 $csort = ""; # the connection type
309 $sock = 0; # connection socket
321 $call = uc shift @ARGV;
322 $call = uc $myalias if !$call;
323 $connsort = lc shift @ARGV;
324 $connsort = 'local' if !$connsort;
326 $loginreq = $call eq 'LOGIN';
328 # we will do this again later 'cos things may have changed
329 $mode = ($connsort eq 'ax25') ? 1 : 2;
332 if ($call eq $mycall) {
333 print $stdout "You cannot connect as your cluster callsign ($mycall)", $nl;
337 $stdout->autoflush(1);
339 $SIG{'INT'} = \&sig_term;
340 $SIG{'TERM'} = \&sig_term;
341 $SIG{'HUP'} = 'IGNORE';
342 $SIG{'CHLD'} = \&sig_chld;
346 # do we need to do a login and password job?
351 if (-e "$data/issue") {
352 open(I, "$data/issue") or die;
356 $issue = s/\n/\r/og if $mode == 1;
358 $stdout->print($issue) if issue;
362 DXUser->init($userfn);
364 # allow a login from an existing user. I could create a user but
365 # I want to check for valid callsigns and I don't have the
366 # necessary info / regular expression yet
367 for ($state = 0; $state < 2; ) {
371 $stdout->print('login: ');
374 $s = $stdin->getline();
377 $s =~ s/-\d+$//o; # no ssids!
378 cease(0) unless $s gt ' ';
380 $user = DXUser->get($call);
382 } elsif ($state == 1) {
383 $stdout->print('password: ');
386 $s = $stdin->getline();
389 if (!$user || ($user->passwd && $user->passwd ne $s)) {
390 $stdout->print("sorry...$nl");
397 # handle callsign and connection type firtling
401 my @f = split /\s+/, $line;
402 $call = uc $f[0] if $f[0];
403 $csort = $f[1] if $f[1];
406 # is this an out going connection?
407 if ($connsort eq "connect") {
408 my $mcall = lc $call;
410 open(IN, "$cpath/$mcall") or cease(2);
420 doconnect($1, $2) if /^\s*co\w*\s+(\w+)\s+(.*)$/io;
421 doabort($1) if /^\s*a\w*\s+(.*)/io;
422 dotimeout($1) if /^\s*t\w*\s+(\d+)/io;
423 dochat($1, $2) if /\s*\'(.*)\'\s+\'(.*)\'/io;
424 if (/\s*cl\w+\s+(.*)/io) {
430 dbg('connect', "Connected to $call ($csort), starting normal protocol");
433 # if we get here we are connected
434 if ($csort eq 'ax25' || $csort eq 'prog') {
435 # open(STDIN, "<&R");
436 # open(STDOUT, ">&W");
441 $csort = 'telnet' if $sort eq 'prog';
442 } elsif ($csort eq 'telnet') {
443 # open(STDIN, "<&$sock");
444 # open(STDOUT, ">&$sock");
452 $stdout->autoflush(1);
458 $mode = ($connsort eq 'ax25') ? 1 : 2;
461 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
463 if (-r "$data/offline") {
464 open IN, "$data/offline" or die;
466 s/\n/\r/og if $mode == 1;
471 print $stdout "Sorry, the cluster $mycall is currently off-line", $mynl;
476 $let = $outbound ? 'O' : 'A';
477 $conn->send_now("$let$call|$connsort");
478 Msg->set_event_handler($stdin, "read" => \&rec_stdin);
482 Msg->event_loop(1, 0.010);
484 if ($t > $lasttime) {
486 print $stdout $outqueue;