added logging
[spider.git] / perl / client.pl
index f44120f2ef09631ce82a0395d0970c45899c257f..cff140b2eb41dba45ee142f7f08891f3066f2e44 100755 (executable)
@@ -5,21 +5,41 @@
 # This is a perl module/program that sits on the end of a dxcluster
 # 'protocol' connection and deals with anything that might come along.
 #
-# this program is called by ax25d and gets raw ax25 text on its input
+# this program is called by ax25d or inetd and gets raw ax25 text on its input
+# It can also be launched into the ether by the cluster program itself for outgoing
+# connections
+#
+# Calling syntax is:-
+#
+# client.pl [callsign] [telnet|ax25|local] [[connect] [program name and args ...]]
+#
+# if the callsign isn't given then the sysop callsign in DXVars.pm is assumed
+#
+# if there is no connection type then 'local' is assumed
+#
+# if there is a 'connect' keyword then it will try to launch the following program
+# and any arguments and connect the stdin & stdout of both the program and the 
+# client together.
 #
 # Copyright (c) 1998 Dirk Koopman G1TLH
 #
 # $Id$
 # 
 
+
 # search local then perl directories
 BEGIN {
-  unshift @INC, "/spider/perl";   # this IS the right way round!
-  unshift @INC, "/spider/local";
+  # root of directory tree for this system
+  $root = "/spider"; 
+  $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
+
+  unshift @INC, "$root/perl";   # this IS the right way round!
+  unshift @INC, "$root/local";
 }
 
 use Msg;
 use DXVars;
+use Carp;
 
 $mode = 2;                      # 1 - \n = \r as EOL, 2 - \n = \n, 0 - transparent
 $call = "";                     # the callsign being used
@@ -39,6 +59,8 @@ sub cease
   if (defined $conn && $sendz) {
     $conn->send_now("Z$call|bye...\n");
   }
+  STDOUT->flush;
+  sleep(2);
   exit(0);     
 }
 
@@ -48,6 +70,14 @@ sub sig_term
   cease(1);
 }
 
+# terminate a child
+sub sig_chld
+{
+  $SIG{CHLD} = \&sig_chld;
+  $waitedpid = wait;
+}
+
+
 sub setmode
 {
   if ($mode == 1) {
@@ -72,7 +102,7 @@ sub rec_socket
           my $snl = $mynl;
           my $newsavenl = "";
           $snl = "" if $mode == 0;
-          if ($mode && $line =~ />$/) {
+          if ($mode == 2 && $line =~ />$/) {
             $newsavenl = $snl;
                 $snl = ' ';
           }
@@ -120,7 +150,11 @@ sub rec_stdin
     if ($mode) {
          $buf =~ s/\r/\n/og if $mode == 1;
          $dangle = !($buf =~ /\n$/);
-         @lines = split /\n/, $buf;
+         if ($buf eq "\n") {
+           @lines = (" ");
+         } else {
+           @lines = split /\n/, $buf;
+         }
          if ($dangle) {                # pull off any dangly bits
            $buf = pop @lines;
          } else {
@@ -143,11 +177,22 @@ sub rec_stdin
 }
 
 $call = uc shift @ARGV;
-$call = uc $mycall if !$call; 
+$call = uc $myalias if !$call; 
 $connsort = lc shift @ARGV;
 $connsort = 'local' if !$connsort;
 $mode = ($connsort =~ /^ax/o) ? 1 : 2;
+
+# is this an out going connection?
+if ($ARGV[0] eq "connect") {
+  shift @ARGV;          # lose the keyword
+  
+}
+
 setmode();
+if ($call eq $mycall) {
+  print "You cannot connect as your cluster callsign ($mycall)", $nl;
+  cease(0);
+}
 
 #select STDOUT; $| = 1;
 STDOUT->autoflush(1);
@@ -155,8 +200,23 @@ STDOUT->autoflush(1);
 $SIG{'INT'} = \&sig_term;
 $SIG{'TERM'} = \&sig_term;
 $SIG{'HUP'} = \&sig_term;
+$SIG{'CHLD'} = \&sig_chld;
 
 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
+if (! $conn) {
+  if (-r "$data/offline") {
+    open IN, "$data/offline" or die;
+    while (<IN>) {
+         s/\n/\r/og if $mode == 1;
+         print;
+       }
+       close IN;
+  } else {
+    print "Sorry, the cluster $mycall is currently off-line", $mynl;
+  }
+  cease(0);
+}
+
 $conn->send_now("A$call|$connsort");
 Msg->set_event_handler(\*STDIN, "read" => \&rec_stdin);