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