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