]> dxcluster.net Git - spider.git/blob - perl/AMsg.pm
regenerate prefix_data from new cty.dat
[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                         $thing->send($conn, 'Aranea');
130                         dbg("-> D $conn->{peerhost} $thing->{Aranea}") if isdbg('chan');
131                 }
132         } else {
133                 dbg("ExtMsg: error on accept ($!)") if isdbg('err');
134         }
135 }
136
137 sub set_newchannel_rproc
138 {
139         my $conn = shift;
140         $conn->{rproc} = \&new_channel;
141         $conn->{state} = 'WH';
142 }
143
144
145 # happens next on receive 
146 #
147
148 sub new_channel
149 {
150         my ($conn, $msg) = @_;
151         my $call = $conn->{call} || $conn->{peerhost};
152
153         dbg("<- I $call $msg") if isdbg('chan');
154
155         my $thing = Aranea::input($msg);
156         unless ($thing) {
157                 dbg("Invalid thingy: $msg from $conn->{peerhost}");
158                 $conn->send_now("Sorry");
159                 $conn->disconnect;
160                 return;
161         }
162
163         $call = $thing->{origin};
164         unless (is_callsign($call)) {
165                 main::already_conn($conn, $call, DXM::msg($main::lang, "illcall", $call));
166                 return;
167         }
168
169         # set up the basic channel info
170         # is there one already connected to me - locally? 
171         my $user = DXUser->get_current($call);
172         my $dxchan = DXChannel::get($call);
173         if ($dxchan) {
174                 if ($main::bumpexisting && $call ne $main::mycall) {
175                         my $ip = $conn->{peerhost} || 'unknown';
176                         $dxchan->send_now('D', DXM::msg($main::lang, 'conbump', $call, $ip));
177                         Log('DXCommand', "$call bumped off by $ip, disconnected");
178                         dbg("$call bumped off by $ip, disconnected");
179                         $dxchan->disconnect;
180                 } else {
181                         main::already_conn($conn, $call, DXM::msg($main::lang, 'conother', $call, $main::mycall));
182                         return;
183                 }
184         }
185
186         # is he locked out ?
187         my $basecall = $call;
188         $basecall =~ s/-\d+$//;
189         my $baseuser = DXUser->get_current($basecall);
190         my $lock = $user->lockout if $user;
191         if ($baseuser && $baseuser->lockout || $lock) {
192                 if (!$user || !defined $lock || $lock) {
193                         my $host = $conn->{peerhost} || "unknown";
194                         Log('DXCommand', "$call on $host is locked out, disconnected");
195                         $conn->disconnect;
196                         return;
197                 }
198         }
199         
200         if ($user) {
201                 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
202         } else {
203                 $user = DXUser->new($call);
204         }
205         
206         # create the channel
207         $dxchan = Aranea->new($call, $conn, $user);
208
209         # check that the conn has a callsign
210         $conn->conns($call);
211
212         # set callbacks
213         $conn->set_error(sub {main::error_handler($dxchan)});
214         $conn->set_rproc(sub {my ($conn,$msg) = @_; $dxchan->rec($msg)});
215         $conn->{state} = 'C';
216         delete $conn->{cmd};
217         $conn->{timeout}->del if $conn->{timeout};
218         delete $conn->{timeout};
219         $conn->nolinger;
220         $thing->handle($dxchan);
221 }
222
223 sub send
224 {
225         my $conn = shift;
226         for (@_) {
227                 $conn->send_later($_);
228         }
229 }