834aaf37ce2e33a019987e1ad2b4fe0d8fc1c653
[spider.git] / perl / client.pl
1 #!/usr/bin/perl
2 #
3 # A thing that implements dxcluster 'protocol'
4 #
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.
7 #
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
10 # connections
11 #
12 # Calling syntax is:-
13 #
14 # client.pl [callsign] [telnet|ax25|local] [[connect] [program name and args ...]]
15 #
16 # if the callsign isn't given then the sysop callsign in DXVars.pm is assumed
17 #
18 # if there is no connection type then 'local' is assumed
19 #
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 
22 # client together.
23 #
24 # Copyright (c) 1998 Dirk Koopman G1TLH
25 #
26 # $Id$
27
28
29
30 # search local then perl directories
31 BEGIN {
32   # root of directory tree for this system
33   $root = "/spider"; 
34   $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
35
36   unshift @INC, "$root/perl";   # this IS the right way round!
37   unshift @INC, "$root/local";
38 }
39
40 use Msg;
41 use DXVars;
42 use Carp;
43
44 $mode = 2;                      # 1 - \n = \r as EOL, 2 - \n = \n, 0 - transparent
45 $call = "";                     # the callsign being used
46 @stdoutq = ();                  # the queue of stuff to send out to the user
47 $conn = 0;                      # the connection object for the cluster
48 $lastbit = "";                  # the last bit of an incomplete input line
49 $mynl = "\n";                   # standard terminator
50 $lasttime = time;               # lasttime something happened on the interface
51 $outqueue = "";                 # the output queue length
52 $buffered = 1;                  # buffer output
53 $savenl = "";                   # an NL that has been saved from last time
54
55 # cease communications
56 sub cease
57 {
58   my $sendz = shift;
59   if (defined $conn && $sendz) {
60     $conn->send_now("Z$call|bye...\n");
61   }
62   exit(0);      
63 }
64
65 # terminate program from signal
66 sub sig_term
67 {
68   cease(1);
69 }
70
71 # terminate a child
72 sub sig_chld
73 {
74   $SIG{CHLD} = \&sig_chld;
75   $waitedpid = wait;
76 }
77
78
79 sub setmode
80 {
81   if ($mode == 1) {
82     $mynl = "\r";
83   } else {
84         $mynl = "\n";
85   }
86   $/ = $mynl;
87 }
88
89 # handle incoming messages
90 sub rec_socket
91 {
92   my ($con, $msg, $err) = @_;
93   if (defined $err && $err) {
94     cease(1);
95   }
96   if (defined $msg) {
97     my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
98         
99         if ($sort eq 'D') {
100            my $snl = $mynl;
101            my $newsavenl = "";
102            $snl = "" if $mode == 0;
103            if ($mode && $line =~ />$/) {
104              $newsavenl = $snl;
105                  $snl = ' ';
106            }
107            $line =~ s/\n/\r/og if $mode == 1;
108            #my $p = qq($line$snl);
109            if ($buffered) {
110              if (length $outqueue >= 128) {
111                print $outqueue;
112                    $outqueue = "";
113              }
114              $outqueue .= "$savenl$line$snl";
115                  $lasttime = time;
116            } else {
117              print $savenl, $line, $snl;;
118            }
119            $savenl = $newsavenl;
120         } elsif ($sort eq 'M') {
121           $mode = $line;               # set new mode from cluster
122       setmode();
123         } elsif ($sort eq 'B') {
124           if ($buffered && $outqueue) {
125             print $outqueue;
126                 $outqueue = "";
127           }
128           $buffered = $line;           # set buffered or unbuffered
129     } elsif ($sort eq 'Z') {       # end, disconnect, go, away .....
130           cease(0);
131     }     
132   }
133   $lasttime = time; 
134 }
135
136 sub rec_stdin
137 {
138   my ($fh) = @_;
139   my $buf;
140   my @lines;
141   my $r;
142   my $first;
143   my $dangle = 0;
144   
145   $r = sysread($fh, $buf, 1024);
146 #  print "sys: $r $buf";
147   if ($r > 0) {
148     if ($mode) {
149           $buf =~ s/\r/\n/og if $mode == 1;
150           $dangle = !($buf =~ /\n$/);
151           if ($buf eq "\n") {
152             @lines = (" ");
153           } else {
154             @lines = split /\n/, $buf;
155           }
156           if ($dangle) {                # pull off any dangly bits
157             $buf = pop @lines;
158           } else {
159             $buf = "";
160           }
161           $first = shift @lines;
162           unshift @lines, ($lastbit . $first) if ($first);
163           foreach $first (@lines) {
164             $conn->send_now("D$call|$first");
165           }
166           $lastbit = $buf;
167           $savenl = "";     # reset savenl 'cos we will have done a newline on input
168         } else {
169           $conn->send_now("D$call|$buf");
170         }
171   } elsif ($r == 0) {
172     cease(1);
173   }
174   $lasttime = time;
175 }
176
177 $call = uc shift @ARGV;
178 $call = uc $myalias if !$call; 
179 $connsort = lc shift @ARGV;
180 $connsort = 'local' if !$connsort;
181 $mode = ($connsort =~ /^ax/o) ? 1 : 2;
182
183 # is this an out going connection?
184 if ($ARGV[0] eq "connect") {
185   shift @ARGV;          # lose the keyword
186   
187 }
188
189 setmode();
190 if ($call eq $mycall) {
191   print "You cannot connect as your cluster callsign ($mycall)", $nl;
192   cease(0);
193 }
194
195 #select STDOUT; $| = 1;
196 STDOUT->autoflush(1);
197
198 $SIG{'INT'} = \&sig_term;
199 $SIG{'TERM'} = \&sig_term;
200 $SIG{'HUP'} = \&sig_term;
201 $SIG{'CHLD'} = \&sig_chld;
202
203 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
204 $conn->send_now("A$call|$connsort");
205 Msg->set_event_handler(\*STDIN, "read" => \&rec_stdin);
206
207 for (;;) {
208   my $t;
209   Msg->event_loop(1, 0.010);
210   $t = time;
211   if ($t > $lasttime) {
212     if ($outqueue) {
213           print $outqueue;
214           $outqueue = "";
215         }
216         $lasttime = $t;
217   }
218 }
219