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