started port of routing stuff
[spider.git] / perl / Aranea.pm
1 #
2 # The new protocol for real at last
3 #
4 # $Id$
5 #
6 # Copyright (c) 2005 Dirk Koopman G1TLH
7 #
8
9 package Aranea;
10
11 use strict;
12
13 use DXUtil;
14 use DXChannel;
15 use DXUser;
16 use DXM;
17 use DXLog;
18 use DXDebug;
19 use Filter;
20 use Time::HiRes qw(gettimeofday tv_interval);
21 use DXHash;
22 use Route;
23 use Route::Node;
24 use Script;
25 use Verify;
26 use DXDupe;
27 use Thingy;
28
29 use vars qw($VERSION $BRANCH);
30 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
31 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /^\d+\.\d+(?:\.(\d+)\.(\d+))?$/  || (0,0));
32 $main::build += $VERSION;
33 $main::branch += $BRANCH;
34
35 use vars qw(@ISA $ntpflag $dupeage);
36
37 @ISA = qw(DXChannel);
38
39 $ntpflag = 0;                                   # should be set in startup if NTP in use
40 $dupeage = 12*60*60;                    # duplicates stored half a day 
41
42 my $seqno = 0;
43 my $dayno = 0;
44 my $daystart = 0;
45
46 sub init
47 {
48
49 }
50
51 sub new
52 {
53         my $self = DXChannel::alloc(@_);
54
55         # add this node to the table, the values get filled in later
56         my $pkg = shift;
57         my $call = shift;
58         $main::routeroot->add($call, '5251', Route::here(1)) if $call ne $main::mycall;
59         $self->{'sort'} = 'W';
60         return $self;
61 }
62
63 sub start
64 {
65         my ($self, $line, $sort) = @_;
66         my $call = $self->{call};
67         my $user = $self->{user};
68
69         # log it
70         my $host = $self->{conn}->{peerhost} || "unknown";
71         Log('Aranea', "$call connected from $host");
72         
73         # remember type of connection
74         $self->{consort} = $line;
75         $self->{outbound} = $sort eq 'O';
76         my $priv = $user->priv;
77         $priv = $user->priv(1) unless $priv;
78         $self->{priv} = $priv;     # other clusters can always be 'normal' users
79         $self->{lang} = $user->lang || 'en';
80         $self->{consort} = $line;       # save the connection type
81         $self->{here} = 1;
82         $self->{width} = 80;
83
84         # sort out registration
85         $self->{registered} = 1;
86
87         # get the output filters
88         $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
89         $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
90         $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
91         $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
92         $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) unless $self->{isolate} ;
93
94
95         # get the INPUT filters (these only pertain to Clusters)
96         $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
97         $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
98         $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
99         $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
100         $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1) unless $self->{isolate};
101         
102         $self->conn->echo(0) if $self->conn->can('echo');
103         
104         # ping neighbour node stuff
105         my $ping = $user->pingint;
106         $ping = $DXProt::pingint unless defined $ping;
107         $self->{pingint} = $ping;
108         $self->{nopings} = $user->nopings || $DXProt::obscount;
109         $self->{pingtime} = [ ];
110         $self->{pingave} = 999;
111         $self->{metric} ||= 100;
112         $self->{lastping} = $main::systime;
113         
114         $self->state('normal');
115         $self->{pc50_t} = $main::systime;
116
117         # send info to all logged in thingies
118         $self->tell_login('loginn');
119
120         # run a script send the output to the debug file
121         my $script = new Script(lc $call) || new Script('node_default');
122         $script->run($self) if $script;
123 }
124
125 #
126 # This is the normal despatcher
127 #
128 sub normal
129 {
130         my ($self, $line) = @_;
131         my $thing = input($line);
132         $thing->queue($self) if $thing;
133 }
134
135 #
136 # periodic processing
137 #
138
139 sub process
140 {
141
142         # calc day number
143         my $d = (gmtime($main::systime))[3];
144         if ($d != $dayno) {
145                 $dayno = $d;
146                 $daystart = $main::systime - ($main::systime % 86400);
147         }
148 }
149
150 sub disconnect
151 {
152         my $self = shift;
153         my $call = $self->call;
154
155         return if $self->{disconnecting}++;
156         
157         # get rid of any PC16/17/19
158 #       eph_del_regex("^PC1[679]*$call");
159
160         # do routing stuff, remove me from routing table
161         my $node = Route::Node::get($call);
162         my @rout;
163         if ($node) {
164                 @rout = $node->del($main::routeroot);
165                 
166                 # and all my ephemera as well
167                 for (@rout) {
168                         my $c = $_->call;
169 #                       eph_del_regex("^PC1[679].*$c");
170                 }
171         }
172
173         RouteDB::delete_interface($call);
174         
175         # unbusy and stop and outgoing mail
176         my $mref = DXMsg::get_busy($call);
177         $mref->stop_msg($call) if $mref;
178         
179         # broadcast to all other nodes that all the nodes connected to via me are gone
180 #       $self->route_pc21($main::mycall, undef, @rout) if @rout;
181
182         # remove outstanding pings
183 #       delete $pings{$call};
184         
185         # I was the last node visited
186     $self->user->node($main::mycall);
187
188         # send info to all logged in thingies
189         $self->tell_login('logoutn');
190
191         Log('Aranea', $call . " Disconnected");
192
193         $self->SUPER::disconnect;
194 }
195
196
197 # generate new header (this is a general subroutine, not a method
198 # because it has to be used before a channel is fully initialised).
199 #
200
201 sub genheader
202 {
203         my $mycall = shift;
204         my $to = shift;
205         my $from = shift;
206         
207         my $date = ((($dayno << 1) | $ntpflag) << 18) |  ($main::systime % 86400);
208         my $r = "$mycall," . sprintf('%6X%04X,0', $date, $seqno);
209         $r .= ",$to" if $to;
210         $r .= ",$from" if $from;
211         $seqno++;
212         $seqno = 0 if $seqno > 0x0ffff;
213         return $r;
214 }
215
216 #
217 # decode the date time sequence group
218 #
219
220 sub decode_dts
221 {
222         my $dts = shift;
223         my ($dt, $seqno) = map {hex} unpack "H6H4", $dts;
224         my $secs = $dt & 0x3FFFF;
225         $dt >>= 18;
226         my $day = $dt >> 1;
227         my $ntp = $dt & 1;
228         my $t;
229         if ($dayno == $day) {
230                 $t = $daystart + $secs;
231         } elsif ($dayno < $day) {
232                 $t = $daystart + (($day-$dayno) * 86400) + $secs;
233         } else {
234                 $t = $daystart + (($dayno-$day) * 86400) + $secs;
235         }
236         return ($t, $seqno, $ntp);
237 }
238
239 # subroutines to encode and decode values in lists 
240 sub tencode
241 {
242         my $s = shift;
243         $s =~ s/([\%=|,\'\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg; 
244         $s = "'$s'" if $s =~ / /;
245         return $s;
246 }
247
248 sub tdecode
249 {
250         my $s = shift;
251         $s =~ s/^'(.*)'$/$1/;
252         $s =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
253         return $s;
254 }
255
256 sub genmsg
257 {
258         my $thing = shift;
259         my $name = shift;
260         my $head = genheader($thing->{origin}, 
261                                                  ($thing->{group} || $thing->{touser} || $thing->{tonode}),
262                                                  ($thing->{user} || $thing->{fromuser} || $thing->{fromnode})
263                                                 );
264         my $data = "$name,";
265         while (@_) {
266                 my $k = lc shift;
267                 my $v = tencode(shift);
268                 $data .= "$k=$v,";
269         }
270         chop $data;
271         return "$head|$data";
272 }
273
274
275 sub decode_input
276 {
277         my $self = shift;
278         my $line = shift;
279         return ('I', $self->{call}, $line);
280 }
281
282 sub input
283 {
284         my $line = shift;
285         my ($head, $data) = split /\|/, $line, 2;
286         return unless $head && $data;
287
288         my ($origin, $dts, $hop, $group, $user) = split /,/, $head;
289         return if DXDupe::check("Ara,$origin,$dts", $dupeage);
290         my $err;
291         $err .= "incomplete header," unless $origin && $dts && defined $hop;
292         my ($cmd, $rdata) = split /,/, $data, 2;
293
294         # validate it further
295         $err .= "missing cmd or data," unless $cmd && $data;
296         $err .= "invalid command ($cmd)," unless $cmd =~ /^[A-Z][A-Z0-9]*$/;
297         my ($gp, $tus) = split /:/, $group, 2;
298                 
299         $err .= "invalid group ($gp)," unless $gp =~ /^[A-Z0-9]{2,}$/;
300         $err .= "invalid tocall ($tus)," if $tus && !is_callsign($tus);
301         $err .= "invalid fromcall ($user)," if $user && !is_callsign($user);
302
303         my $class = 'Thingy::' . ucfirst(lc $cmd);
304         my $thing;
305         my ($t, $seqno, $ntp) = decode_dts($dts) unless $err;
306         $err .= "invalid date/seq," unless $t;
307         
308         if ($err) {
309                 chop $err;
310                 dbg("Aranea input: $err");
311         } elsif ($class->can('new')) {
312                 # create the appropriate Thingy
313                 $thing = $class->new();
314
315                 # reconstitute the header but wth hop increased by one
316                 $head = join(',', $origin, $group, $dts, ++$hop);
317                 $head .= ",$user" if $user;
318                 $thing->{Aranea} = "$head|$data";
319
320                 # store useful data
321                 $thing->{origin} = $origin;
322                 $thing->{time} = $t;
323                 $thing->{group} = $gp if $gp;
324                 $thing->{touser} = $tus if $tus;
325                 $thing->{user} = $user if $user;
326                 $thing->{hopsaway} = $hop; 
327                 
328                 for (split(/,/, $rdata)) {
329                         if (/=/) {
330                                 my ($k,$v) = split /=/, $_, 2;
331                                 $thing->{$k} = tdecode($v);
332                         } else {
333                                 $thing->{$_} = 1;
334                         }
335                 }
336
337                 # post process the thing, this generally adds on semantic meaning
338                 # does parameter checking etc. It also adds / prepares the thingy so
339                 # this is compatible with older protocol and arranges data so
340                 # that the filtering can still work.
341                 if ($thing->can('from_Aranea')) {
342
343                         # if a thing is ok then return that thing, otherwise return
344                         # nothing
345                         $thing = $thing->from_Aranea;
346                 }
347         }
348         return $thing;
349 }
350
351 # this is the DXChannel send
352 # note that this does NOT send out stuff in same way as other DXChannels
353 # it is just as it comes, no extra bits added (here)
354 sub send                                                # this is always later and always data
355 {
356         my $self = shift;
357         my $conn = $self->{conn};
358         return unless $conn;
359         my $call = $self->{call};
360
361         for (@_) {
362 #               chomp;
363         my @lines = split /\n/;
364                 for (@lines) {
365                         $conn->send_later($_);
366                         dbg("-> D $call $_") if isdbg('chan');
367                 }
368         }
369         $self->{t} = $main::systime;
370 }
371
372 #
373 # load of dummies for DXChannel broadcasts
374 # these will go away in time?
375 # These are all from PC protocol
376 #
377
378 sub dx_spot
379 {
380         my $self = shift;
381         my $line = shift;
382         my $isolate = shift;
383         my ($filter, $hops);
384
385         if ($self->{spotsfilter}) {
386                 ($filter, $hops) = $self->{spotsfilter}->it(@_);
387                 return unless $filter;
388         }
389 #       send_prot_line($self, $filter, $hops, $isolate, $line);
390 }
391
392 sub wwv
393 {
394         my $self = shift;
395         my $line = shift;
396         my $isolate = shift;
397         my ($filter, $hops);
398         
399         if ($self->{wwvfilter}) {
400                 ($filter, $hops) = $self->{wwvfilter}->it(@_);
401                 return unless $filter;
402         }
403 #       send_prot_line($self, $filter, $hops, $isolate, $line)
404 }
405
406 sub wcy
407 {
408         my $self = shift;
409         my $line = shift;
410         my $isolate = shift;
411         my ($filter, $hops);
412
413         if ($self->{wcyfilter}) {
414                 ($filter, $hops) = $self->{wcyfilter}->it(@_);
415                 return unless $filter;
416         }
417 #       send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
418 }
419
420 sub announce
421 {
422         my $self = shift;
423         my $line = shift;
424         my $isolate = shift;
425         my $to = shift;
426         my $target = shift;
427         my $text = shift;
428         my ($filter, $hops);
429
430         if ($self->{annfilter}) {
431                 ($filter, $hops) = $self->{annfilter}->it(@_);
432                 return unless $filter;
433         }
434 #       send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
435 }
436
437 sub chat
438 {
439         goto &announce;
440 }
441
442 1;