]> gb7djk.dxcluster.net Git - spider.git/blob - perl/AMsg.pm
clean up various things and add the DXXml.pm module
[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->to_connected($conn->{call}, 'O', $conn->{csort});
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 'WA' ) {
71                                 my $uref = DXUser->get_current($conn->{call});
72                                 $msg =~ s/[\r\n]+$//;
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);
78                                 } else {
79                                         $conn->send_now("Sorry");
80                                         $conn->disconnect;
81                                 }
82                         } elsif ($conn->{state} eq 'WC') {
83                                 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
84                                         $conn->_docmd($msg);
85                                         if ($conn->{state} eq 'WC' && exists $conn->{cmd} &&  @{$conn->{cmd}} == 0) {
86                                                 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
87                                         }
88                                 }
89                         }
90                 }
91         } 
92 }
93
94 sub to_connected
95 {
96         my ($conn, $call, $dir, $sort) = @_;
97         $conn->{state} = 'C';
98         $conn->conns($call);
99         delete $conn->{cmd};
100         $conn->{timeout}->del if $conn->{timeout};
101         delete $conn->{timeout};
102         $conn->nolinger;
103         &{$conn->{rproc}}($conn, "$dir$call|$sort");
104 }
105
106 sub login
107 {
108         return \&new_channel;
109 }
110
111 sub new_client {
112         my $server_conn = shift;
113     my $sock = $server_conn->{sock}->accept();
114         if ($sock) {
115                 my $conn = $server_conn->new($server_conn->{rproc});
116                 $conn->{sock} = $sock;
117                 $conn->nolinger;
118                 Msg::blocking($sock, 0);
119                 $conn->{blocking} = 0;
120                 eval {$conn->{peerhost} = $sock->peerhost};
121                 if ($@) {
122                         dbg($@) if isdbg('connll');
123                         $conn->disconnect;
124                 } else {
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');
129                         if ($eproc) {
130                                 $conn->{eproc} = $eproc;
131                                 Msg::set_event_handler ($sock, "error" => $eproc);
132                         }
133                         if ($rproc) {
134                                 $conn->{rproc} = $rproc;
135                                 my $callback = sub {$conn->_rcv};
136                                 Msg::set_event_handler ($sock, "read" => $callback);
137                                 $conn->_dotimeout(60);
138                                 $conn->{echo} = 0;
139                         } else { 
140                                 &{$conn->{eproc}}() if $conn->{eproc};
141                                 $conn->disconnect();
142                         }
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');
148                 }
149         } else {
150                 dbg("ExtMsg: error on accept ($!)") if isdbg('err');
151         }
152 }
153
154 sub start_connect
155 {
156         my $call = shift;
157         my $fn = shift;
158         my $conn = AMsg->new(\&new_channel); 
159         $conn->{outgoing} = 1;
160         $conn->conns($call);
161         
162         my $f = new IO::File $fn;
163         push @{$conn->{cmd}}, <$f>;
164         $f->close;
165         $conn->{state} = 'WC';
166         $conn->_dotimeout($deftimeout);
167         $conn->_docmd;
168 }
169
170
171 # happens next on receive 
172 #
173
174 sub new_channel
175 {
176         my ($conn, $msg) = @_;
177         my $thing = Aranea::input($msg);
178         return unless defined $thing;
179
180         my $call = $thing->{origin};
181         unless (is_callsign($call)) {
182                 main::already_conn($conn, $call, DXM::msg($main::lang, "illcall", $call));
183                 return;
184         }
185
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);
190         if ($dxchan) {
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");
196                         $dxchan->disconnect;
197                 } else {
198                         main::already_conn($conn, $call, DXM::msg($main::lang, 'conother', $call, $main::mycall));
199                         return;
200                 }
201         }
202
203         # is he locked out ?
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");
212                         $conn->disconnect;
213                         return;
214                 }
215         }
216         
217         if ($user) {
218                 $user->{lang} = $main::lang if !$user->{lang}; # to autoupdate old systems
219         } else {
220                 $user = DXUser->new($call);
221         }
222         
223         # create the channel
224         $dxchan = Aranea->new($call, $conn, $user);
225
226         # check that the conn has a callsign
227         $conn->conns($call) if $conn->isa('IntMsg');
228
229         # set callbacks
230         $conn->set_error(sub {main::error_handler($dxchan)});
231         $conn->set_rproc(sub {my ($conn,$msg) = @_; $dxchan->rec($msg)});
232         $dxchan->rec($msg);
233 }
234
235 sub send
236 {
237         my $conn = shift;
238         for (@_) {
239                 $conn->send_later($_);
240         }
241 }