]> dxcluster.net Git - spider.git/blob - perl/AMsg.pm
15596a98196958e3e2185e0d76cb09ca397f26da
[spider.git] / perl / AMsg.pm
1 #
2 # This class implements the new style comms for Aranea
3 # communications for Msg.pm
4 #
5 # $Id$
6 #
7 # Copyright (c) 2005 - Dirk Koopman G1TLH
8 #
9
10 use strict;
11
12 package AMsg;
13
14 use Msg;
15 use DXVars;
16 use DXUtil;
17 use DXDebug;
18 use Aranea;
19 use Verify;
20 use DXLog;
21 use Thingy;
22 use Thingy::Hello;
23
24 use vars qw($VERSION $BRANCH);
25
26 main::mkver($VERSION = q$Revision$);
27
28 use vars qw(@ISA $deftimeout);
29
30 @ISA = qw(ExtMsg Msg);
31 $deftimeout = 60;
32
33 sub enqueue
34 {
35         my ($conn, $msg) = @_;
36         push (@{$conn->{outqueue}}, $msg . $conn->{lineend});
37 }
38
39 sub dequeue
40 {
41         my $conn = shift;
42         my $msg;
43
44         if ($conn->{csort} eq 'ax25' && exists $conn->{msg}) {
45                 $conn->{msg} =~ s/\cM/\cJ/g;
46         }
47         if ($conn->{state} eq 'WC' ) {
48                 if (exists $conn->{cmd}) {
49                         if (@{$conn->{cmd}}) {
50                                 dbg("connect $conn->{cnum}: $conn->{msg}") if isdbg('connect');
51                                 $conn->_docmd($conn->{msg});
52                         } 
53                 }
54                 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
55                         $conn->{state} = 'WH';
56                 }
57         } elsif ($conn->{msg} =~ /\cJ/) {
58                 my @lines =  $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
59                 if ($conn->{msg} =~ /\cJ$/) {
60                         delete $conn->{msg};
61                 } else {
62                         $conn->{msg} =~ s/([^\cM\cJ]*)\cM?\cJ//g;
63                 }
64                 while (defined ($msg = shift @lines)) {
65                         dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
66                         if ($conn->{state} eq 'C') {
67                                 &{$conn->{rproc}}($conn, $msg);
68                         } elsif ($conn->{state} eq 'WH' ) {
69                                 # this is the first stage that we have a callsign
70                                 # do we have a hello?
71                                 $msg =~ s/[\r\n]+$//;
72                                 if ($msg =~ m{|HELLO,}) {
73                                         # a possibly valid HELLO line, process it
74                                         $conn->new_channel($msg);
75                                 }
76                         } elsif ($conn->{state} eq 'WC') {
77                                 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
78                                         $conn->_docmd($msg);
79                                         if ($conn->{state} eq 'WC' && exists $conn->{cmd} &&  @{$conn->{cmd}} == 0) {
80                                                 $conn->{state} = 'WH';
81                                         }
82                                 }
83                         }
84                 }
85         } 
86 }
87
88 sub login
89 {
90         return \&new_channel;
91 }
92
93 sub new_client {
94         my $server_conn = shift;
95     my $sock = $server_conn->{sock}->accept();
96         if ($sock) {
97                 my $conn = $server_conn->new($server_conn->{rproc});
98                 $conn->{sock} = $sock;
99                 $conn->nolinger;
100                 Msg::blocking($sock, 0);
101                 $conn->{blocking} = 0;
102                 eval {$conn->{peerhost} = $sock->peerhost};
103                 if ($@) {
104                         dbg($@) if isdbg('connll');
105                         $conn->disconnect;
106                 } else {
107                         eval {$conn->{peerport} = $sock->peerport};
108                         $conn->{peerport} = 0 if $@;
109                         my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost}, $conn->{peerport});
110                         dbg("accept $conn->{cnum} from $conn->{peerhost} $conn->{peerport}") if isdbg('connll');
111                         if ($eproc) {
112                                 $conn->{eproc} = $eproc;
113                                 Msg::set_event_handler ($sock, "error" => $eproc);
114                         }
115                         if ($rproc) {
116                                 $conn->{rproc} = $rproc;
117                                 my $callback = sub {$conn->_rcv};
118                                 Msg::set_event_handler ($sock, "read" => $callback);
119                                 $conn->_dotimeout(60);
120                                 $conn->{echo} = 0;
121                         } else { 
122                                 &{$conn->{eproc}}() if $conn->{eproc};
123                                 $conn->disconnect();
124                         }
125                         Log('Aranea', "Incoming connection from $conn->{peerhost}");
126                         $conn->{outbound} = 0;
127                         $conn->{state} = 'WH';          # wait for return authorize
128                         my $thing = $conn->{lastthing} = Thingy::Hello->new();
129
130                         $thing->send($conn, 'Aranea');
131                         dbg("-> D $conn->{peerhost} $thing->{Aranea}") if isdbg('chan');
132                 }
133         } else {
134                 dbg("ExtMsg: error on accept ($!)") if isdbg('err');
135         }
136 }
137
138 sub set_newchannel_rproc
139 {
140         my $conn = shift;
141         $conn->{rproc} = \&new_channel;
142         $conn->{state} = 'WH';
143 }
144
145
146 # happens next on receive 
147 #
148
149 sub new_channel
150 {
151         my ($conn, $msg) = @_;
152         my $call = $conn->{call} || $conn->{peerhost};
153
154         dbg("<- I $call $msg") if isdbg('chan');
155
156         my $thing = Aranea::input($msg);
157         unless ($thing) {
158                 dbg("Invalid thingy: $msg from $conn->{peerhost}");
159                 $conn->send_now("Sorry");
160                 $conn->disconnect;
161                 return;
162         }
163
164         $call = $thing->{origin};
165         unless (is_callsign($call)) {
166                 main::already_conn($conn, $call, DXM::msg($main::lang, "illcall", $call));
167                 return;
168         }
169
170         # set up the basic channel info
171         # is there one already connected to me - locally? 
172         my $user = DXUser->get_current($call);
173         my $dxchan = DXChannel->get($call);
174         if ($dxchan) {
175                 if ($main::bumpexisting && $call ne $main::mycall) {
176                         my $ip = $conn->{peerhost} || 'unknown';
177                         $dxchan->send_now('D', DXM::msg($main::lang, 'conbump', $call, $ip));
178                         Log('DXCommand', "$call bumped off by $ip, disconnected");
179                         dbg("$call bumped off by $ip, disconnected");
180                         $dxchan->disconnect;
181                 } else {
182                         main::already_conn($conn, $call, DXM::msg($main::lang, 'conother', $call, $main::mycall));
183                         return;
184                 }
185         }
186
187         # is he locked out ?
188         my $basecall = $call;
189         $basecall =~ s/-\d+$//;
190         my $baseuser = DXUser->get_current($basecall);
191         my $lock = $user->lockout if $user;
192         if ($baseuser && $baseuser->lockout || $lock) {
193                 if (!$user || !defined $lock || $lock) {
194                         my $host = $conn->{peerhost} || "unknown";
195                         Log('DXCommand', "$call on $host is locked out, disconnected");
196                         $conn->disconnect;
197                         return;
198                 }
199         }
200         
201         if ($user) {
202                 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
203         } else {
204                 $user = DXUser->new($call);
205         }
206         
207         # create the channel
208         $dxchan = Aranea->new($call, $conn, $user);
209
210         # check that the conn has a callsign
211         $conn->conns($call);
212
213         # set callbacks
214         $conn->set_error(sub {main::error_handler($dxchan)});
215         $conn->set_rproc(sub {my ($conn,$msg) = @_; $dxchan->rec($msg)});
216         $conn->{state} = 'C';
217         delete $conn->{cmd};
218         $conn->{timeout}->del if $conn->{timeout};
219         delete $conn->{timeout};
220         $conn->nolinger;
221         $thing->handle($dxchan);
222 }
223
224 sub send
225 {
226         my $conn = shift;
227         for (@_) {
228                 $conn->send_later($_);
229         }
230 }