2 # This class implements the new style comms for Aranea
3 # communications for Msg.pm
7 # Copyright (c) 2005 - Dirk Koopman G1TLH
24 use vars qw($VERSION $BRANCH);
26 main::mkver($VERSION = q$Revision$);
28 use vars qw(@ISA $deftimeout);
30 @ISA = qw(ExtMsg Msg);
35 my ($conn, $msg) = @_;
36 push (@{$conn->{outqueue}}, $msg . $conn->{lineend});
44 if ($conn->{csort} eq 'ax25' && exists $conn->{msg}) {
45 $conn->{msg} =~ s/\cM/\cJ/g;
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});
54 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
55 $conn->{state} = 'WH';
57 } elsif ($conn->{msg} =~ /\cJ/) {
58 my @lines = $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
59 if ($conn->{msg} =~ /\cJ$/) {
62 $conn->{msg} =~ s/([^\cM\cJ]*)\cM?\cJ//g;
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
72 if ($msg =~ m{|HELLO,}) {
73 # a possibly valid HELLO line, process it
74 $conn->new_channel($msg);
76 } elsif ($conn->{state} eq 'WC') {
77 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
79 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
80 $conn->{state} = 'WH';
94 my $server_conn = shift;
95 my $sock = $server_conn->{sock}->accept();
97 my $conn = $server_conn->new($server_conn->{rproc});
98 $conn->{sock} = $sock;
100 Msg::blocking($sock, 0);
101 $conn->{blocking} = 0;
102 eval {$conn->{peerhost} = $sock->peerhost};
104 dbg($@) if isdbg('connll');
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');
112 $conn->{eproc} = $eproc;
113 Msg::set_event_handler ($sock, "error" => $eproc);
116 $conn->{rproc} = $rproc;
117 my $callback = sub {$conn->_rcv};
118 Msg::set_event_handler ($sock, "read" => $callback);
119 $conn->_dotimeout(60);
122 &{$conn->{eproc}}() if $conn->{eproc};
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');
133 dbg("ExtMsg: error on accept ($!)") if isdbg('err');
137 sub set_newchannel_rproc
140 $conn->{rproc} = \&new_channel;
141 $conn->{state} = 'WH';
145 # happens next on receive
150 my ($conn, $msg) = @_;
151 my $call = $conn->{call} || $conn->{peerhost};
153 dbg("<- I $call $msg") if isdbg('chan');
155 my $thing = Aranea::input($msg);
157 dbg("Invalid thingy: $msg from $conn->{peerhost}");
158 $conn->send_now("Sorry");
163 $call = $thing->{origin};
164 unless (is_callsign($call)) {
165 main::already_conn($conn, $call, DXM::msg($main::lang, "illcall", $call));
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);
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");
181 main::already_conn($conn, $call, DXM::msg($main::lang, 'conother', $call, $main::mycall));
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");
201 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
203 $user = DXUser->new($call);
207 $dxchan = Aranea->new($call, $conn, $user);
209 # check that the conn has a callsign
213 $conn->set_error(sub {main::error_handler($dxchan)});
214 $conn->set_rproc(sub {my ($conn,$msg) = @_; $dxchan->rec($msg)});
215 $conn->{state} = 'C';
217 $conn->{timeout}->del if $conn->{timeout};
218 delete $conn->{timeout};
220 $thing->handle($dxchan);
227 $conn->send_later($_);