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