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);
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;
30 use vars qw(@ISA $deftimeout);
32 @ISA = qw(ExtMsg Msg);
37 my ($conn, $msg) = @_;
38 push (@{$conn->{outqueue}}, $msg . $conn->{lineend});
46 if ($conn->{csort} eq 'ax25' && exists $conn->{msg}) {
47 $conn->{msg} =~ s/\cM/\cJ/g;
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});
56 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
57 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
59 } elsif ($conn->{msg} =~ /\cJ/) {
60 my @lines = $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
61 if ($conn->{msg} =~ /\cJ$/) {
64 $conn->{msg} =~ s/([^\cM\cJ]*)\cM?\cJ//g;
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 'WA' ) {
71 my $uref = DXUser->get_current($conn->{call});
73 if ($uref && $msg eq $uref->passwd) {
74 my $sort = $conn->{csort};
75 $sort = 'local' if $conn->{peerhost} eq "127.0.0.1";
76 $conn->{usedpasswd} = 1;
77 $conn->to_connected($conn->{call}, 'A', $sort);
79 $conn->send_now("Sorry");
82 } elsif ($conn->{state} eq 'WC') {
83 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
85 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
86 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
96 my ($conn, $call, $dir, $sort) = @_;
100 $conn->{timeout}->del if $conn->{timeout};
101 delete $conn->{timeout};
103 &{$conn->{rproc}}($conn, "$dir$call|$sort");
108 return \&new_channel;
112 my $server_conn = shift;
113 my $sock = $server_conn->{sock}->accept();
115 my $conn = $server_conn->new($server_conn->{rproc});
116 $conn->{sock} = $sock;
118 Msg::blocking($sock, 0);
119 $conn->{blocking} = 0;
120 eval {$conn->{peerhost} = $sock->peerhost};
122 dbg($@) if isdbg('connll');
125 eval {$conn->{peerport} = $sock->peerport};
126 $conn->{peerport} = 0 if $@;
127 my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost}, $conn->{peerport});
128 dbg("accept $conn->{cnum} from $conn->{peerhost} $conn->{peerport}") if isdbg('connll');
130 $conn->{eproc} = $eproc;
131 Msg::set_event_handler ($sock, "error" => $eproc);
134 $conn->{rproc} = $rproc;
135 my $callback = sub {$conn->_rcv};
136 Msg::set_event_handler ($sock, "read" => $callback);
137 $conn->_dotimeout(60);
140 &{$conn->{eproc}}() if $conn->{eproc};
143 Log('Aranea', "Incoming connection from $conn->{peerhost}");
144 $conn->{outgoing} = 0;
145 $conn->{state} = 'WH'; # wait for return authorize
146 my $thing = $conn->{lastthing} = Thingy::Hello->new(origin=>$main::mycall, group=>'ROUTE');
147 $thing->send($conn, 'Aranea');
150 dbg("ExtMsg: error on accept ($!)") if isdbg('err');
158 my $conn = AMsg->new(\&new_channel);
159 $conn->{outgoing} = 1;
162 my $f = new IO::File $fn;
163 push @{$conn->{cmd}}, <$f>;
165 $conn->{state} = 'WC';
166 $conn->_dotimeout($deftimeout);
171 # happens next on receive
176 my ($conn, $msg) = @_;
177 my $thing = Aranea::input($msg);
178 return unless defined $thing;
180 my $call = $thing->{origin};
181 unless (is_callsign($call)) {
182 main::already_conn($conn, $call, DXM::msg($main::lang, "illcall", $call));
186 # set up the basic channel info
187 # is there one already connected to me - locally?
188 my $user = DXUser->get_current($call);
189 my $dxchan = DXChannel::get($call);
191 if ($main::bumpexisting) {
192 my $ip = $conn->{peerhost} || 'unknown';
193 $dxchan->send_now('D', DXM::msg($main::lang, 'conbump', $call, $ip));
194 Log('DXCommand', "$call bumped off by $ip, disconnected");
195 dbg("$call bumped off by $ip, disconnected");
198 main::already_conn($conn, $call, DXM::msg($main::lang, 'conother', $call, $main::mycall));
204 my $basecall = $call;
205 $basecall =~ s/-\d+$//;
206 my $baseuser = DXUser->get_current($basecall);
207 my $lock = $user->lockout if $user;
208 if ($baseuser && $baseuser->lockout || $lock) {
209 if (!$user || !defined $lock || $lock) {
210 my $host = $conn->{peerhost} || "unknown";
211 Log('DXCommand', "$call on $host is locked out, disconnected");
218 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
220 $user = DXUser->new($call);
224 $dxchan = Aranea->new($call, $conn, $user);
226 # check that the conn has a callsign
227 $conn->conns($call) if $conn->isa('IntMsg');
230 $conn->set_error(sub {main::error_handler($dxchan)});
231 $conn->set_rproc(sub {my ($conn,$msg) = @_; $dxchan->rec($msg)});
239 $conn->send_later($_);