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