8097f6cd96c24621d6112b93e6594105bb8d269a
[spider.git] / perl / cluster.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 use Msg;
16 use DXVars;
17 use DXUtil;
18 use DXChannel;
19 use DXUser;
20 use DXM;
21
22 package main;
23
24 @inqueue = ();                # the main input queue, an array of hashes 
25
26 # handle disconnections
27 sub disconnect
28 {
29   my $dxchan = shift;
30   return if !defined $dxchan;
31   my $user = $dxchan->{user};
32   my $conn = $dxchan->{conn};
33   $user->close() if defined $user;
34   $conn->disconnect() if defined $conn;
35   $dxchan->del();
36 }
37
38 # handle incoming messages
39 sub rec
40 {
41   my ($conn, $msg, $err) = @_;
42   my $dxchan = DXChannel->get_by_cnum($conn);      # get the dxconnnect object for this message
43   
44   if (defined $err && $err) {
45     disconnect($dxchan) if defined $dxchan;
46         return;
47   }
48   
49   # set up the basic channel info - this needs a bit more thought - there is duplication here
50   if (!defined $dxchan) {
51      my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
52      my $user = DXUser->get($call);
53          $user = DXUser->new($call) if !defined $user;
54      $dxchan = DXChannel->new($call, $conn, $user);  
55   }
56   
57   # queue the message and the channel object for later processing
58   if (defined $msg) {
59     my $self = bless {}, "inqueue";
60     $self->{dxchan} = $dxchan;
61     $self->{data} = $msg;
62         push @inqueue, $self;
63   }
64 }
65
66 sub login
67 {
68   return \&rec;
69 }
70
71 # cease running this program, close down all the connections nicely
72 sub cease
73 {
74   my $dxchan;
75   foreach $dxchan (DXChannel->get_all()) {
76     disconnect($dxchan);
77   }
78   exit(0);
79 }
80
81 # this is where the input queue is dealt with and things are dispatched off to other parts of
82 # the cluster
83 sub process_inqueue
84 {
85   my $self = shift @inqueue;
86   return if !$self;
87   
88   my $data = $self->{data};
89   my $dxchan = $self->{dxchan};
90   my ($sort, $call, $line) = $data =~ /^(\w)(\S+)\|(.*)$/;
91   
92   # do the really sexy console interface bit! (Who is going to do the TK interface then?)
93   print DEBUG atime, " < $sort $call $line\n" if defined DEBUG;
94   print "< $sort $call $line\n";
95   
96   # handle A records
97   if ($sort eq 'A') {
98     my $user = $dxchan->{user};
99         $user->{sort} = 'U' if !defined $user->{sort};
100     if ($user->{sort} eq 'U') {
101           $dxchan->send_now('D', msg('l2', $call, $mycall, $myqth));
102           $dxchan->send_file($motd) if (-e $motd);
103         }
104   } elsif (sort eq 'D') {
105     ;
106   } elsif ($sort eq 'Z') {
107     disconnect($dxchan);
108   }
109 }
110
111 #############################################################
112 #
113 # The start of the main line of code 
114 #
115 #############################################################
116
117 # open the debug file, set various FHs to be unbuffered
118 open(DEBUG, ">>$debugfn") or die "can't open $debugfn($!)";
119 select DEBUG; $| = 1;
120 select STDOUT; $| = 1;
121
122 # initialise User file system
123 DXUser->init($userfn);
124
125 # start listening for incoming messages/connects
126 Msg->new_server("$clusteraddr", $clusterport, \&login);
127
128 # prime some signals
129 $SIG{'INT'} = \&cease;
130 $SIG{'TERM'} = \&cease;
131 $SIG{'HUP'} = 'IGNORE';
132
133 # this, such as it is, is the main loop!
134 for (;;) {
135   Msg->event_loop(1, 0.001);
136   process_inqueue();
137 }
138