started to flesh some of it out.
[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 and gets raw ax25 text on its input
9 #
10 # Copyright (c) 1998 Dirk Koopman G1TLH
11 #
12 # $Id$
13
14
15 BEGIN {
16   unshift @INC, "/spider/local";
17   unshift @INC, "/spider/perl";
18 }
19
20 use Msg;
21 use DXVars;
22
23 $mode = 1;                      # 1 - \n = \r as EOL, 2 - \n = \n, 0 - transparent
24 $call = "";                     # the callsign being used
25 @stdoutq = ();                  # the queue of stuff to send out to the user
26 $conn = 0;                      # the connection object for the cluster
27 $lastbit = "";                  # the last bit of an incomplete input line
28 $nl = "\r";
29
30 # cease communications
31 sub cease
32 {
33   my $sendz = shift;
34   if (defined $conn && $sendz) {
35     $conn->send_now("Z$call|bye...\n");
36   }
37   exit(0);      
38 }
39
40 # terminate program from signal
41 sub sig_term
42 {
43   cease(1);
44 }
45
46 # handle incoming messages
47 sub rec_socket
48 {
49   my ($con, $msg, $err) = @_;
50   if (defined $err && $err) {
51     cease(1);
52   }
53   if (defined $msg) {
54     my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)|(.*)$/;
55         
56         if ($sort eq 'D') {
57            $nl = "" if $mode == 0;
58            $line =~ s/\n/\r/o if $mode == 1;
59            print $line, $nl;
60         } elsif ($sort eq 'M') {
61           $mode = $line;               # set new mode from cluster
62         } elsif ($sort eq 'Z') {       # end, disconnect, go, away .....
63           cease(0);
64     }     
65   } 
66 }
67
68 sub rec_stdin
69 {
70   my ($fh) = @_;
71   my $buf;
72   my @lines;
73   my $r;
74   my $first;
75   my $dangle = 0;
76   
77   $r = sysread($fh, $buf, 1024);
78 #  print "sys: $r $buf";
79   if ($r > 0) {
80     if ($mode) {
81           $buf =~ s/\r/\n/o if $mode == 1;
82           $dangle = !($buf =~ /\n$/);
83           @lines = split /\n/, $buf;
84           if ($dangle) {                # pull off any dangly bits
85             $buf = pop @lines;
86           } else {
87             $buf = "";
88           }
89           $first = shift @lines;
90           unshift @lines, ($lastbit . $first) if ($first);
91           foreach $first (@lines) {
92             $conn->send_now("D$call|$first");
93           }
94           $lastbit = $buf;  
95         } else {
96           $conn->send_now("D$call|$buf");
97         }
98   } elsif ($r == 0) {
99     cease(1);
100   }
101 }
102
103 $call = uc $ARGV[0];
104 die "client.pl <call> [<mode>]\r\n" if (!$call);
105 $mode = $ARGV[1] if (@ARGV > 1);
106
107 if ($mode != 1) {
108   $nl = "\n";
109   $\ = $nl;
110 }
111
112 select STDOUT; $| = 1;
113
114 $SIG{'INT'} = \&sig_term;
115 $SIG{'TERM'} = \&sig_term;
116 #$SIG{'HUP'} = \&sig_term;
117
118 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
119 $conn->send_now("A$call|start");
120 Msg->set_event_handler(\*STDIN, "read" => \&rec_stdin);
121 Msg->event_loop();
122