changed ping regime to be a rolling list of up to ten
[spider.git] / perl / DXProt.pm
1 #!/usr/bin/perl
2 #
3 # This module impliments the protocal mode for a dx cluster
4 #
5 # Copyright (c) 1998 Dirk Koopman G1TLH
6 #
7 # $Id$
8
9
10 package DXProt;
11
12 @ISA = qw(DXChannel);
13
14 use DXUtil;
15 use DXChannel;
16 use DXUser;
17 use DXM;
18 use DXCluster;
19 use DXProtVars;
20 use DXCommandmode;
21 use DXLog;
22 use Spot;
23 use DXProtout;
24 use DXDebug;
25 use Filter;
26 use Local;
27 use DXDb;
28 use Time::HiRes qw(gettimeofday tv_interval);
29
30 use Carp;
31
32 use strict;
33 use vars qw($me $pc11_max_age $pc23_max_age $pc11_dup_age $pc23_dup_age
34                         %spotdup %wwvdup $last_hour %pings %rcmds
35                         %nodehops @baddx $baddxfn $pc12_dup_age
36                         %anndup $allowzero $pc12_dup_lth $decode_dk0wcy);
37
38 $me = undef;                                    # the channel id for this cluster
39 $decode_dk0wcy = undef;                 # if set use this callsign to decode announces from the EU WWV data beacon
40 $pc11_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc11
41 $pc23_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc23
42 $pc11_dup_age = 3*3600;                 # the maximum time to keep the spot dup list for
43 $pc23_dup_age = 3*3600;                 # the maximum time to keep the wwv dup list for
44 $pc12_dup_age = 24*3600;                # the maximum time to keep the ann dup list for
45 $pc12_dup_lth = 60;                             # the length of ANN text to save for deduping 
46 %spotdup = ();                              # the pc11 and 26 dup hash 
47 %wwvdup = ();                               # the pc23 and 27 dup hash
48 %anndup = ();                               # the PC12 dup hash
49 $last_hour = time;                              # last time I did an hourly periodic update
50 %pings = ();                    # outstanding ping requests outbound
51 %rcmds = ();                    # outstanding rcmd requests outbound
52 %nodehops = ();                 # node specific hop control
53 @baddx = ();                    # list of illegal spotted callsigns
54
55
56 $baddxfn = "$main::data/baddx.pl";
57
58 sub init
59 {
60         my $user = DXUser->get($main::mycall);
61         $DXProt::myprot_version += $main::version*100;
62         $me = DXProt->new($main::mycall, 0, $user); 
63         $me->{here} = 1;
64         $me->{state} = "indifferent";
65         do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
66         confess $@ if $@;
67         #  $me->{sort} = 'M';    # M for me
68
69         # now prime the spot duplicates file with today's and yesterday's data
70     my @today = Julian::unixtoj(time);
71         my @spots = Spot::readfile(@today);
72         @today = Julian::sub(@today, 1);
73         push @spots, Spot::readfile(@today);
74         for (@spots) {
75                 my $dupkey = "$_->[0]$_->[1]$_->[2]$_->[3]$_->[4]";
76                 $spotdup{$dupkey} = $_->[2];
77         }
78
79         # now prime the wwv duplicates file with just this month's data
80         my @wwv = Geomag::readfile(time);
81         for (@wwv) {
82                 my $dupkey = "$_->[1].$_->[2]$_->[3]$_->[4]";
83                 $wwvdup{$dupkey} = $_->[1];
84         }
85
86         # load the baddx file
87         do "$baddxfn" if -e "$baddxfn";
88         print "$@\n" if $@;
89 }
90
91 #
92 # obtain a new connection this is derived from dxchannel
93 #
94
95 sub new 
96 {
97         my $self = DXChannel::alloc(@_);
98         $self->{'sort'} = 'A';          # in absence of how to find out what sort of an object I am
99         return $self;
100 }
101
102 # this is how a pc connection starts (for an incoming connection)
103 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
104 # all the crap that comes between).
105 sub start
106 {
107         my ($self, $line, $sort) = @_;
108         my $call = $self->{call};
109         my $user = $self->{user};
110         
111         # remember type of connection
112         $self->{consort} = $line;
113         $self->{outbound} = $sort eq 'O';
114         $self->{priv} = $user->priv;
115         $self->{lang} = $user->lang;
116         $self->{isolate} = $user->{isolate};
117         $self->{consort} = $line;       # save the connection type
118         $self->{here} = 1;
119
120         # get the INPUT filters (these only pertain to Clusters)
121         $self->{inspotfilter} = Filter::read_in('spots', $call, 1);
122         $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1);
123         $self->{inannfilter} = Filter::read_in('ann', $call, 1);
124         
125         # set unbuffered and no echo
126         $self->send_now('B',"0");
127         $self->send_now('E',"0");
128         
129         # send initialisation string
130         if (!$self->{outbound}) {
131                 $self->send(pc38()) if DXNode->get_all();
132                 $self->send(pc18());
133         }
134         $self->state('init');
135         $self->pc50_t(time);
136         $self->pingint($user->pingint || 3*60);
137         $self->nopings(3);
138         $self->lastping($main::systime);
139         $self->pingtime([ ]);
140
141         Log('DXProt', "$call connected");
142 }
143
144 #
145 # This is the normal pcxx despatcher
146 #
147 sub normal
148 {
149         my ($self, $line) = @_;
150         my @field = split /\^/, $line;
151         pop @field if $field[-1] eq '~';
152         
153 #       print join(',', @field), "\n";
154                                                 
155         # ignore any lines that don't start with PC
156         return if !$field[0] =~ /^PC/;
157         
158         # process PC frames
159         my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
160         return unless $pcno;
161         return if $pcno < 10 || $pcno > 51;
162
163         # dump bad protocol messages unless it is a PC29
164         if ($line =~ /\%[0-9A-F][0-9A-F]/o && $pcno != 29) {
165                 dbg('chan', "CORRUPT protocol message - dumped");
166                 return;
167         }
168
169         # local processing 1
170         my $pcr;
171         eval {
172                 $pcr = Local::pcprot($self, $pcno, @field);
173         };
174 #       dbg('local', "Local::pcprot error $@") if $@;
175         return if $pcr;
176         
177  SWITCH: {
178                 if ($pcno == 10) {              # incoming talk
179                         
180                         # is it for me or one of mine?
181                         my $call = ($field[5] gt ' ') ? $field[5] : $field[2];
182                         if ($call eq $main::mycall || grep $_ eq $call, get_all_user_calls()) {
183                                 
184                                 # yes, it is
185                                 my $text = unpad($field[3]);
186                                 Log('talk', $call, $field[1], $field[6], $text);
187                                 $call = $main::myalias if $call eq $main::mycall;
188                                 my $ref = DXChannel->get($call);
189                                 $ref->send("$call de $field[1]: $text") if $ref && $ref->{talk};
190                         } else {
191                                 $self->route($field[2], $line); # relay it on its way
192                         }
193                         return;
194                 }
195                 
196                 if ($pcno == 11 || $pcno == 26) { # dx spot
197
198                         # route 'foreign' pc26s 
199                         if ($pcno == 26) {
200                                 if ($field[7] ne $main::mycall) {
201                                         $self->route($field[7], $line);
202                                         return;
203                                 }
204                         }
205                         
206                         # if this is a 'nodx' node then ignore it
207                         if (grep $field[7] =~ /^$_/,  @DXProt::nodx_node) {
208                                 dbg('chan', "Bad DXNode, dropped");
209                                 return;
210                         }
211                         
212                         # convert the date to a unix date
213                         my $d = cltounix($field[3], $field[4]);
214                         # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
215                         if (!$d || ($pcno == 11 && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
216                                 dbg('chan', "Spot ignored, invalid date or out of range ($field[3] $field[4])\n");
217                                 return;
218                         }
219
220                         # strip off the leading & trailing spaces from the comment
221                         my $text = unpad($field[5]);
222                         
223                         # store it away
224                         my $spotter = $field[6];
225                         $spotter =~ s/-[\@\d]+$//o;     # strip off the ssid from the spotter
226                         
227                         # do some de-duping
228                         my $freq = $field[1] - 0;
229                         my $dupkey = "$freq$field[2]$d$text$spotter";
230                         if ($spotdup{$dupkey}) {
231                                 dbg('chan', "Duplicate Spot ignored\n");
232                                 return;
233                         }
234                         
235                         $spotdup{$dupkey} = $d;
236
237                         # is it 'baddx'
238                         if (grep $field[2] eq $_, @baddx) {
239                                 dbg('chan', "Bad DX spot, ignored");
240                                 return;
241                         }
242
243                         # are any of the crucial fields invalid?
244             if ($field[2] =~ /[a-z]/ || $field[6] =~ /[a-z]/ || $field[7] =~ /[a-z]/) {
245                                 dbg('chan', "Spot contains lower case callsigns, rejected");
246                                 return;
247                         }
248                         
249                         my @spot = Spot::add($freq, $field[2], $d, $text, $spotter, $field[7]);
250
251             #
252                         # @spot at this point contains:-
253             # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
254                         # then  spotted itu, spotted cq, spotters itu, spotters cq
255                         # you should be able to route on any of these
256             #
257                         
258                         # local processing 
259                         my $r;
260                         eval {
261                                 $r = Local::spot($self, @spot);
262                         };
263 #                       dbg('local', "Local::spot1 error $@") if $@;
264                         return if $r;
265
266                         # DON'T be silly and send on PC26s!
267                         return if $pcno == 26;
268
269                         # send out the filtered spots
270                         send_dx_spot($self, $line, @spot) if @spot;
271                         return;
272                 }
273                 
274                 if ($pcno == 12) {              # announces
275                         # announce duplicate checking
276                         my $text = substr(uc unpad($field[3]), 0, $pc12_dup_lth);
277                         my $dupkey = $field[1].$field[2].$text;
278                         if ($anndup{$dupkey}) {
279                                 dbg('chan', "Duplicate Announce ignored\n");
280                                 return;
281                         }
282                         $anndup{$dupkey} = $main::systime;
283                         
284                         if ($field[2] eq '*' || $field[2] eq $main::mycall) {
285                                 
286                                 # global ann filtering on INPUT
287                                 if ($self->{inannfilter}) {
288                                         my ($filter, $hops) = Filter::it($self->{inannfilter}, @field[1..6], $self->{call} );
289                                         unless ($filter) {
290                                                 dbg('chan', "Rejected by filter");
291                                                 return;
292                                         }
293                                 }
294
295                                 # send it
296                                 $self->send_announce($line, @field[1..6]);
297                                 
298                                 if ($decode_dk0wcy && $field[1] eq $decode_dk0wcy) {
299                                         my ($hour, $k, $next, $a, $r, $sfi, $alarm) = $field[3] =~ /^Aurora Beacon\s+(\d+)UTC,\s+Kiel\s+K=(\d+),.*ed\s+K=(\d+),\s+A=(\d+),\s+R=(\d+),\s+SFI=(\d+),.*larm:\s+(\w+)/;
300                                         $alarm = ($alarm =~ /^Y/i) ? ', Aurora in DE' : ''; 
301                                         my $wwv = Geomag::update($main::systime, $hour, $sfi, $a, $k, "R=$r, Next K=$next$alarm", $decode_dk0wcy, $field[5], $r) if $sfi && $r;
302                                 }
303                                 
304                         } else {
305                                 $self->route($field[2], $line);
306                         }
307                         
308                         return;
309                 }
310                 
311                 if ($pcno == 13) {
312                         last SWITCH;
313                 }
314                 if ($pcno == 14) {
315                         last SWITCH;
316                 }
317                 if ($pcno == 15) {
318                         last SWITCH;
319                 }
320                 
321                 if ($pcno == 16) {              # add a user
322                         my $node = DXCluster->get_exact($field[1]); 
323                         my $dxchan;
324                         if (!$node && ($dxchan = DXChannel->get($field[1]))) {
325                                 # add it to the node table if it isn't present and it's
326                                 # connected locally
327                                 $node = DXNode->new($dxchan, $field[1], 0, 1, 5400);
328                                 broadcast_ak1a(pc19($dxchan, $node), $dxchan, $self) unless $dxchan->{isolate};
329                                 
330                         }
331                         return unless $node; # ignore if havn't seen a PC19 for this one yet
332                         return unless $node->isa('DXNode');
333                         if ($node->dxchan != $self) {
334                                 dbg('chan', "LOOP: $field[1] came in on wrong channel");
335                                 return;
336                         }
337                         if (($dxchan = DXChannel->get($field[1])) && $dxchan != $self) {
338                                 dbg('chan', "LOOP: $field[1] connected locally");
339                                 return;
340                         }
341                         my $i;
342                                                 
343                         for ($i = 2; $i < $#field; $i++) {
344                                 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (\S) (\d)/o;
345                                 next if !$call || length $call < 3 || length $call > 8;
346                                 next if !$confmode;
347                                 $call = uc $call;
348                                 next if DXCluster->get_exact($call); # we already have this (loop?)
349                                 
350                                 $confmode = $confmode eq '*';
351                                 DXNodeuser->new($self, $node, $call, $confmode, $here);
352                                 
353                                 # add this station to the user database, if required
354                                 $call =~ s/-\d+$//o;        # remove ssid for users
355                                 my $user = DXUser->get_current($call);
356                                 $user = DXUser->new($call) if !$user;
357                                 $user->homenode($node->call) if !$user->homenode;
358                                 $user->node($node->call);
359                                 $user->lastin($main::systime) unless DXChannel->get($call);
360                                 $user->put;
361                         }
362                         
363                         # queue up any messages (look for privates only)
364                         DXMsg::queue_msg(1) if $self->state eq 'normal';     
365                         last SWITCH;
366                 }
367                 
368                 if ($pcno == 17) {              # remove a user
369                         my $node = DXCluster->get_exact($field[2]);
370                         my $dxchan;
371                         if (!$node && ($dxchan = DXChannel->get($field[2]))) {
372                                 # add it to the node table if it isn't present and it's
373                                 # connected locally
374                                 $node = DXNode->new($dxchan, $field[2], 0, 1, 5400);
375                                 broadcast_ak1a(pc19($dxchan, $node), $dxchan, $self) unless $dxchan->{isolate};
376                                 return;
377                         }
378                         return unless $node;
379                         return unless $node->isa('DXNode');
380                         if ($node->dxchan != $self) {
381                                 dbg('chan', "LOOP: $field[2] came in on wrong channel");
382                                 return;
383                         }
384                         if (($dxchan = DXChannel->get($field[2])) && $dxchan != $self) {
385                                 dbg('chan', "LOOP: $field[2] connected locally");
386                                 return;
387                         }
388                         my $ref = DXCluster->get_exact($field[1]);
389                         $ref->del() if $ref;
390                         last SWITCH;
391                 }
392                 
393                 if ($pcno == 18) {              # link request
394                         $self->state('init');   
395
396                         # first clear out any nodes on this dxchannel
397                         my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
398                         foreach my $node (@gonenodes) {
399                                 next if $node->dxchan == $DXProt::me;
400                                 broadcast_ak1a(pc21($node->call, 'Gone, re-init') , $self) unless $self->{isolate}; 
401                                 $node->del();
402                         }
403                         $self->send_local_config();
404                         $self->send(pc20());
405                         return;             # we don't pass these on
406                 }
407                 
408                 if ($pcno == 19) {              # incoming cluster list
409                         my $i;
410                         my $newline = "PC19^";
411                         for ($i = 1; $i < $#field-1; $i += 4) {
412                                 my $here = $field[$i];
413                                 my $call = uc $field[$i+1];
414                                 my $confmode = $field[$i+2];
415                                 my $ver = $field[$i+3];
416
417                                 $ver = 5400 if !$ver && $allowzero;
418                                 
419                                 # now check the call over
420                                 my $node = DXCluster->get_exact($call);
421                                 if ($node) {
422                                         my $dxchan;
423                                         if (($dxchan = DXChannel->get($call)) && $dxchan != $self) {
424                                                 dbg('chan', "LOOP: $call connected locally");
425                                         }
426                                     if ($node->dxchan != $self) {
427                                                 dbg('chan', "LOOP: $call come in on wrong channel");
428                                                 next;
429                                         }
430                                         dbg('chan', "already have $call");
431                                         next;
432                                 }
433                                 
434                                 # check for sane parameters
435                                 next if $ver < 5000; # only works with version 5 software
436                                 next if length $call < 3; # min 3 letter callsigns
437
438                                 # add it to the nodes table and outgoing line
439                                 $newline .= "$here^$call^$confmode^$ver^";
440                                 DXNode->new($self, $call, $confmode, $here, $ver);
441                                 
442                                 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
443                                 my $mref = DXMsg::get_busy($call);
444                                 $mref->stop_msg($call) if $mref;
445                                 
446                                 # add this station to the user database, if required (don't remove SSID from nodes)
447                                 my $user = DXUser->get_current($call);
448                                 if (!$user) {
449                                         $user = DXUser->new($call);
450                                         $user->sort('A');
451                                         $user->priv(1);                   # I have relented and defaulted nodes
452                                         $self->{priv} = 1;                # to user RCMDs allowed
453                                         $user->homenode($call);
454                                         $user->node($call);
455                                 }
456                                 $user->lastin($main::systime) unless DXChannel->get($call);
457                                 $user->put;
458                         }
459                         
460                         return if $newline eq "PC19^";
461
462                         # add hop count 
463                         $newline .=  get_hops(19) . "^";
464                         $line = $newline;
465                         last SWITCH;
466                 }
467                 
468                 if ($pcno == 20) {              # send local configuration
469                         $self->send_local_config();
470                         $self->send(pc22());
471                         $self->state('normal');
472                         return;
473                 }
474                 
475                 if ($pcno == 21) {              # delete a cluster from the list
476                         my $call = uc $field[1];
477                         if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
478                                 my $node = DXCluster->get_exact($call);
479                                 if ($node) {
480                                         if ($node->dxchan != $self) {
481                                                 dbg('chan', "LOOP: $call come in on wrong channel");
482                                                 return;
483                                         }
484                                         my $dxchan;
485                                         if (($dxchan = DXChannel->get($call)) && $dxchan != $self) {
486                                                 dbg('chan', "LOOP: $call connected locally");
487                                                 return;
488                                         }
489                                         $node->del();
490                                 } else {
491                                         dbg('chan', "$call not in table, dropped");
492                                         return;
493                                 }
494                         }
495                         last SWITCH;
496                 }
497                 
498                 if ($pcno == 22) {
499                         $self->state('normal');
500                         return;
501                 }
502                                 
503                 if ($pcno == 23 || $pcno == 27) { # WWV info
504                         
505                         # route 'foreign' pc27s 
506                         if ($pcno == 27) {
507                                 if ($field[8] ne $main::mycall) {
508                                         $self->route($field[8], $line);
509                                         return;
510                                 }
511                         }
512
513                         # do some de-duping
514                         my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
515                         my $sfi = unpad($field[3]);
516                         my $k = unpad($field[4]);
517                         my $i = unpad($field[5]);
518                         my ($r) = $field[6] =~ /R=(\d+)/;
519                         $r = 0 unless $r;
520                         my $dupkey = "$d.$sfi$k$i";
521                         if ($wwvdup{$dupkey}) {
522                                 dbg('chan', "Dup WWV Spot ignored\n");
523                                 return;
524                         }
525                         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
526                                 dbg('chan', "WWV Date ($field[1] $field[2]) out of range");
527                                 return;
528                         }
529                         $wwvdup{$dupkey} = $d;
530                         $field[6] =~ s/-\d+$//o;            # remove spotter's ssid
531                 
532                         my $wwv = Geomag::update($d, $field[2], $sfi, $k, $i, @field[6..8], $r);
533
534                         my $rep;
535                         eval {
536                                 $rep = Local::wwv($self, $field[1], $field[2], $sfi, $k, $i, @field[6..8], $r);
537                         };
538 #                       dbg('local', "Local::wwv2 error $@") if $@;
539                         return if $rep;
540
541                         # DON'T be silly and send on PC27s!
542                         return if $pcno == 27;
543
544                         # broadcast to the eager world
545                         send_wwv_spot($self, $line, $d, $field[2], $sfi, $k, $i, @field[6..8]);
546                         return;
547                 }
548                 
549                 if ($pcno == 24) {              # set here status
550                         my $call = uc $field[1];
551                         my $ref = DXCluster->get_exact($call);
552                         $ref->here($field[2]) if $ref;
553                         last SWITCH;
554                 }
555                 
556                 if ($pcno == 25) {      # merge request
557                         if ($field[1] ne $main::mycall) {
558                                 $self->route($field[1], $line);
559                                 return;
560                         }
561                         if ($field[2] eq $main::mycall) {
562                                 dbg('chan', "Trying to merge to myself, ignored");
563                                 return;
564                         }
565
566                         Log('DXProt', "Merge request for $field[3] spots and $field[4] WWV from $field[1]");
567                         
568                         # spots
569                         if ($field[3] > 0) {
570                                 my @in = reverse Spot::search(1, undef, undef, 0, $field[3]);
571                                 my $in;
572                                 foreach $in (@in) {
573                                         $self->send(pc26(@{$in}[0..4], $field[2]));
574                                 }
575                         }
576
577                         # wwv
578                         if ($field[4] > 0) {
579                                 my @in = reverse Geomag::search(0, $field[4], time, 1);
580                                 my $in;
581                                 foreach $in (@in) {
582                                         $self->send(pc27(@{$in}[0..5], $field[2]));
583                                 }
584                         }
585                         return;
586                 }
587
588                 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
589                         if ($pcno == 49 || $field[1] eq $main::mycall) {
590                                 DXMsg::process($self, $line);
591                         } else {
592                                 $self->route($field[1], $line);
593                         }
594                         return;
595                 }
596                 
597                 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
598                         if ($field[1] eq $main::mycall) {
599                                 my $ref = DXUser->get_current($field[2]);
600                                 my $cref = DXCluster->get($field[2]);
601                                 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
602                                 unless ($field[3] =~ /rcmd/i || !$cref || !$ref || $cref->mynode->call ne $ref->homenode) {    # not allowed to relay RCMDS!
603                                         if ($ref->{priv}) {     # you have to have SOME privilege, the commands have further filtering
604                                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
605                                                 my $oldpriv = $self->{priv};
606                                                 $self->{priv} = $ref->{priv};     # assume the user's privilege level
607                                                 my @in = (DXCommandmode::run_cmd($self, $field[3]));
608                                                 $self->{priv} = $oldpriv;
609                                                 for (@in) {
610                                                         s/\s*$//og;
611                                                         $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
612                                                         Log('rcmd', 'out', $field[2], $_);
613                                                 }
614                                                 delete $self->{remotecmd};
615                                         } else {
616                                                 $self->send(pc35($main::mycall, $field[2], "$main::mycall:sorry...!"));
617                                         }
618                                 } else {
619                                         $self->send(pc35($main::mycall, $field[2], "$main::mycall:your attempt is logged, Tut tut tut...!"));
620                                 }
621                         } else {
622                                 $self->route($field[1], $line);
623                         }
624                         return;
625                 }
626                 
627                 if ($pcno == 35) {              # remote command replies
628                         if ($field[1] eq $main::mycall) {
629                                 my $s = $rcmds{$field[2]};
630                                 if ($s) {
631                                         my $dxchan = DXChannel->get($s->{call});
632                                         $dxchan->send($field[3]) if $dxchan;
633                                         delete $rcmds{$field[2]} if !$dxchan;
634                                 }
635                         } else {
636                                 $self->route($field[1], $line);
637                         }
638                         return;
639                 }
640                 
641                 # for pc 37 see 44 onwards
642
643                 if ($pcno == 38) {              # node connected list from neighbour
644                         return;
645                 }
646                 
647                 if ($pcno == 39) {              # incoming disconnect
648                         $self->disconnect();
649                         return;
650                 }
651                 
652                 if ($pcno == 41) {              # user info
653                         # add this station to the user database, if required
654                         my $user = DXUser->get_current($field[1]);
655                         if (!$user) {
656                                 # then try without an SSID
657                                 $field[1] =~ s/-\d+$//o;
658                                 $user = DXUser->get_current($field[1]);
659                         }
660                         $user = DXUser->new($field[1]) if !$user;
661                         
662                         if ($field[2] == 1) {
663                                 $user->name($field[3]);
664                         } elsif ($field[2] == 2) {
665                                 $user->qth($field[3]);
666                         } elsif ($field[2] == 3) {
667                                 my ($lat, $long) = DXBearing::stoll($field[3]);
668                                 $user->lat($lat);
669                                 $user->long($long);
670                         } elsif ($field[2] == 4) {
671                                 $user->homenode($field[3]);
672                         }
673                         $user->put;
674                         last SWITCH;
675                 }
676                 if ($pcno == 43) {
677                         last SWITCH;
678                 }
679                 if ($pcno == 37 || $pcno == 44 || $pcno == 45 || $pcno == 46 || $pcno == 47) {
680                         DXDb::process($self, $line);
681                         return;
682                 }
683                 
684                 if ($pcno == 50) {              # keep alive/user list
685                         my $node = DXCluster->get_exact($field[1]);
686                         if ($node) {
687                                 return unless $node->isa('DXNode');
688                                 return unless $node->dxchan == $self;
689                                 $node->update_users($field[2]);
690                         }
691                         last SWITCH;
692                 }
693                 
694                 if ($pcno == 51) {              # incoming ping requests/answers
695                         
696                         # is it for us?
697                         if ($field[1] eq $main::mycall) {
698                                 my $flag = $field[3];
699                                 if ($flag == 1) {
700                                         $self->send(pc51($field[2], $field[1], '0'));
701                                 } else {
702                                         # it's a reply, look in the ping list for this one
703                                         my $ref = $pings{$field[2]};
704                                         if ($ref) {
705                                                 my $tochan =  DXChannel->get($field[2]);
706                                                 while (@$ref) {
707                                                         my $r = shift @$ref;
708                                                         my $dxchan = DXChannel->get($r->{call});
709                                                         next unless $dxchan;
710                                                         my $t = tv_interval($r->{t}, [ gettimeofday ]);
711                                                         if ($dxchan->is_user) {
712                                                                 my $s = sprintf "%.2f", $t; 
713                                                                 my $ave = sprintf "%.2f", $tochan ? ($tochan->pingave || $t) : $t;
714                                                                 $dxchan->send($dxchan->msg('pingi', $field[2], $s, $ave))
715                                                         } elsif ($dxchan->is_ak1a) {
716                                                                 if ($tochan) {
717                                                                         $tochan->nopings(3); # pump up the timer
718                                                                         push @{$tochan->pingtime}, $t;
719                                                                         shift @{$tochan->pingtime} if @{$tochan->pingtime} > 10;
720                                                                         my $st;
721                                                                         for (@{$tochan->pingtime}) {
722                                                                                 $st += $_;
723                                                                         }
724                                                                         $tochan->{pingave} = $st / @{$tochan->pingtime};
725                                                                 }
726                                                         } 
727                                                 }
728                                         }
729                                 }
730                         } else {
731                                 # route down an appropriate thingy
732                                 $self->route($field[1], $line);
733                         }
734                         return;
735                 }
736         }
737          
738          # if get here then rebroadcast the thing with its Hop count decremented (if
739          # there is one). If it has a hop count and it decrements to zero then don't
740          # rebroadcast it.
741          #
742          # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
743          #        REBROADCAST!!!!
744          #
745          
746         unless ($self->{isolate}) {
747                 broadcast_ak1a($line, $self); # send it to everyone but me
748         }
749 }
750
751 #
752 # This is called from inside the main cluster processing loop and is used
753 # for despatching commands that are doing some long processing job
754 #
755 sub process
756 {
757         my $t = time;
758         my @dxchan = DXChannel->get_all();
759         my $dxchan;
760         
761         foreach $dxchan (@dxchan) {
762                 next unless $dxchan->is_ak1a();
763                 next if $dxchan == $me;
764                 
765                 # send a pc50 out on this channel
766                 if ($t >= $dxchan->pc50_t + $DXProt::pc50_interval) {
767                         $dxchan->send(pc50());
768                         $dxchan->pc50_t($t);
769                 } 
770
771                 # send a ping out on this channel
772                 if ($t >= $dxchan->pingint + $dxchan->lastping) {
773                         if ($dxchan->nopings <= 0) {
774                                 $dxchan->disconnect;
775                         } else {
776                                 addping($main::mycall, $dxchan->call);
777                                 $dxchan->nopings($dxchan->nopings - 1);
778                                 $dxchan->lastping($t);
779                         }
780                 }
781         }
782         
783         my $key;
784         my $val;
785         my $cutoff;
786         if ($main::systime - 3600 > $last_hour) {
787                 $cutoff  = $main::systime - $pc11_dup_age;
788                 while (($key, $val) = each %spotdup) {
789                         delete $spotdup{$key} if $val < $cutoff;
790                 }
791                 $cutoff = $main::systime - $pc23_dup_age;
792                 while (($key, $val) = each %wwvdup) {
793                         delete $wwvdup{$key} if $val < $cutoff;
794                 }
795                 $cutoff = $main::systime - $pc12_dup_age;
796                 while (($key, $val) = each %anndup) {
797                         delete $anndup{$key} if $val < $cutoff;
798                 }
799                 $last_hour = $main::systime;
800         }
801 }
802
803 #
804 # finish up a pc context
805 #
806 sub finish
807 {
808         my $self = shift;
809         my $call = $self->call;
810         my $ref = DXCluster->get_exact($call);
811         
812         $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
813
814         # unbusy and stop and outgoing mail
815         my $mref = DXMsg::get_busy($call);
816         $mref->stop_msg($call) if $mref;
817         
818         # broadcast to all other nodes that all the nodes connected to via me are gone
819         my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
820         my $node;
821         
822         foreach $node (@gonenodes) {
823                 next if $node->call eq $call;
824                 broadcast_ak1a(pc21($node->call, 'Gone') , $self) unless $self->{isolate}; 
825                 $node->del();
826         }
827
828         # remove outstanding pings
829         delete $pings{$call};
830         
831         # now broadcast to all other ak1a nodes that I have gone
832         broadcast_ak1a(pc21($call, 'Gone.'), $self) unless $self->{isolate};
833         
834         Log('DXProt', $call . " Disconnected");
835         $ref->del() if $ref;
836 }
837
838 #
839 # some active measures
840 #
841 sub send_dx_spot
842 {
843         my $self = shift;
844         my $line = shift;
845         my @dxchan = DXChannel->get_all();
846         my $dxchan;
847         
848         # send it if it isn't the except list and isn't isolated and still has a hop count
849         # taking into account filtering and so on
850         foreach $dxchan (@dxchan) {
851                 my $routeit;
852                 my ($filter, $hops);
853
854                 if ($dxchan->{spotfilter}) {
855                     ($filter, $hops) = Filter::it($dxchan->{spotfilter}, @_, $self->{call} );
856                         next unless $filter;
857                 }
858                 
859                 if ($dxchan->is_ak1a) {
860                         next if $dxchan == $self;
861                         if ($hops) {
862                                 $routeit = $line;
863                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
864                         } else {
865                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
866                                 next unless $routeit;
867                         }
868                         if ($filter) {
869                                 $dxchan->send($routeit) if $routeit;
870                         } else {
871                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
872                         }
873                 } elsif ($dxchan->is_user && $dxchan->{dx}) {
874                         my $buf = Spot::formatb($_[0], $_[1], $_[2], $_[3], $_[4]);
875                         $buf .= "\a\a" if $dxchan->{beep};
876                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
877                                 $dxchan->send($buf);
878                         } else {
879                                 $dxchan->delay($buf);
880                         }
881                 }                                       
882         }
883 }
884
885 sub send_wwv_spot
886 {
887         my $self = shift;
888         my $line = shift;
889         my @dxchan = DXChannel->get_all();
890         my $dxchan;
891         
892         # send it if it isn't the except list and isn't isolated and still has a hop count
893         # taking into account filtering and so on
894         foreach $dxchan (@dxchan) {
895                 my $routeit;
896                 my ($filter, $hops);
897
898                 if ($dxchan->{spotfilter}) {
899                          ($filter, $hops) = Filter::it($dxchan->{wwvfilter}, @_, $self->{call} );
900                          next unless $filter;
901                 }
902                 if ($dxchan->is_ak1a) {
903                         next if $dxchan == $self;
904                         if ($hops) {
905                                 $routeit = $line;
906                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
907                         } else {
908                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
909                                 next unless $routeit;
910                         }
911                         if ($filter) {
912                                 $dxchan->send($routeit) if $routeit;
913                         } else {
914                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
915                                 
916                         }
917                 } elsif ($dxchan->is_user && $dxchan->{wwv}) {
918                         my $buf = "WWV de $_[6] <$_[1]>:   SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
919                         $buf .= "\a\a" if $dxchan->{beep};
920                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
921                                 $dxchan->send($buf);
922                         } else {
923                                 $dxchan->delay($buf);
924                         }
925                 }                                       
926         }
927 }
928
929 # send an announce
930 sub send_announce
931 {
932         my $self = shift;
933         my $line = shift;
934         my @dxchan = DXChannel->get_all();
935         my $dxchan;
936         my $text = unpad($_[2]);
937         my $target;
938         my $to = 'To ';
939                                 
940         if ($_[3] eq '*') {     # sysops
941                 $target = "SYSOP";
942         } elsif ($_[3] gt ' ') { # speciality list handling
943                 my ($name) = split /\./, $_[3]; 
944                 $target = "$name"; # put the rest in later (if bothered) 
945         } 
946         
947         if ($_[5] eq '1') {
948                 $target = "WX"; 
949                 $to = '';
950         }
951         $target = "All" if !$target;
952         
953         Log('ann', $target, $_[0], $text);
954
955         # send it if it isn't the except list and isn't isolated and still has a hop count
956         # taking into account filtering and so on
957         foreach $dxchan (@dxchan) {
958                 my $routeit;
959                 my ($filter, $hops);
960
961                 if ($dxchan->{annfilter}) {
962                         ($filter, $hops) = Filter::it($dxchan->{annfilter}, @_, $self->{call} );
963                         next unless $filter;
964                 } 
965                 if ($dxchan->is_ak1a && $_[1] ne $main::mycall) {  # i.e not specifically routed to me
966                         next if $dxchan == $self;
967                         if ($hops) {
968                                 $routeit = $line;
969                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
970                         } else {
971                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
972                                 next unless $routeit;
973                         }
974                         if ($filter) {
975                                 $dxchan->send($routeit) if $routeit;
976                         } else {
977                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
978                                 
979                         }
980                 } elsif ($dxchan->is_user && $dxchan->{ann}) {
981                         next if $target eq 'SYSOP' && $dxchan->{priv} < 5;
982                         my $buf = "$to$target de $_[0]: $text";
983                         $buf .= "\a\a" if $dxchan->{beep};
984                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
985                                 $dxchan->send($buf);
986                         } else {
987                                 $dxchan->delay($buf);
988                         }
989                 }                                       
990         }
991 }
992
993 sub send_local_config
994 {
995         my $self = shift;
996         my $n;
997         my @nodes;
998         my @localnodes;
999         my @remotenodes;
1000                 
1001         # send our nodes
1002         if ($self->{isolate}) {
1003                 @localnodes = (DXCluster->get_exact($main::mycall));
1004         } else {
1005                 # create a list of all the nodes that are not connected to this connection
1006                 # and are not themselves isolated, this to make sure that isolated nodes
1007         # don't appear outside of this node
1008                 @nodes = DXNode::get_all();
1009                 @nodes = grep { $_->{call} ne $main::mycall } @nodes;
1010                 @nodes = grep { $_->dxchan != $self } @nodes if @nodes;
1011                 @nodes = grep { !$_->dxchan->{isolate} } @nodes if @nodes;
1012                 @localnodes = grep { $_->dxchan->{call} eq $_->{call} } @nodes if @nodes;
1013                 unshift @localnodes, DXCluster->get_exact($main::mycall);
1014                 @remotenodes = grep { $_->dxchan->{call} ne $_->{call} } @nodes if @nodes;
1015         }
1016
1017         my @s = $me->pc19(@localnodes, @remotenodes);
1018         for (@s) {
1019                 my $routeit = adjust_hops($self, $_);
1020                 $self->send($routeit) if $routeit;
1021         }
1022         
1023         # get all the users connected on the above nodes and send them out
1024         foreach $n (@localnodes, @remotenodes) {
1025                 my @users = values %{$n->list};
1026                 my @s = pc16($n, @users);
1027                 for (@s) {
1028                         my $routeit = adjust_hops($self, $_);
1029                         $self->send($routeit) if $routeit;
1030                 }
1031         }
1032 }
1033
1034 #
1035 # route a message down an appropriate interface for a callsign
1036 #
1037 # is called route(to, pcline);
1038 #
1039 sub route
1040 {
1041         my ($self, $call, $line) = @_;
1042         my $cl = DXCluster->get_exact($call);
1043         if ($cl) {       # don't route it back down itself
1044                 if (ref $self && $call eq $self->{call}) {
1045                         dbg('chan', "Trying to route back to source, dropped");
1046                         return;
1047                 }
1048                 my $hops;
1049                 my $dxchan = $cl->{dxchan};
1050                 if ($dxchan) {
1051                         my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
1052                         if ($routeit) {
1053                                 $dxchan->send($routeit) if $dxchan;
1054                         }
1055                 }
1056         }
1057 }
1058
1059 # broadcast a message to all clusters taking into account isolation
1060 # [except those mentioned after buffer]
1061 sub broadcast_ak1a
1062 {
1063         my $s = shift;                          # the line to be rebroadcast
1064         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1065         my @dxchan = get_all_ak1a();
1066         my $dxchan;
1067         
1068         # send it if it isn't the except list and isn't isolated and still has a hop count
1069         foreach $dxchan (@dxchan) {
1070                 next if grep $dxchan == $_, @except;
1071                 my $routeit = adjust_hops($dxchan, $s);      # adjust its hop count by node name
1072                 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
1073         }
1074 }
1075
1076 # broadcast a message to all clusters ignoring isolation
1077 # [except those mentioned after buffer]
1078 sub broadcast_all_ak1a
1079 {
1080         my $s = shift;                          # the line to be rebroadcast
1081         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1082         my @dxchan = get_all_ak1a();
1083         my $dxchan;
1084         
1085         # send it if it isn't the except list and isn't isolated and still has a hop count
1086         foreach $dxchan (@dxchan) {
1087                 next if grep $dxchan == $_, @except;
1088                 my $routeit = adjust_hops($dxchan, $s);      # adjust its hop count by node name
1089                 $dxchan->send($routeit);
1090         }
1091 }
1092
1093 # broadcast to all users
1094 # storing the spot or whatever until it is in a state to receive it
1095 sub broadcast_users
1096 {
1097         my $s = shift;                          # the line to be rebroadcast
1098         my $sort = shift;           # the type of transmission
1099         my $fref = shift;           # a reference to an object to filter on
1100         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1101         my @dxchan = get_all_users();
1102         my $dxchan;
1103         my @out;
1104         
1105         foreach $dxchan (@dxchan) {
1106                 next if grep $dxchan == $_, @except;
1107                 push @out, $dxchan;
1108         }
1109         broadcast_list($s, $sort, $fref, @out);
1110 }
1111
1112 # broadcast to a list of users
1113 sub broadcast_list
1114 {
1115         my $s = shift;
1116         my $sort = shift;
1117         my $fref = shift;
1118         my $dxchan;
1119         
1120         foreach $dxchan (@_) {
1121                 my $filter = 1;
1122                 
1123                 if ($sort eq 'dx') {
1124                     next unless $dxchan->{dx};
1125                         ($filter) = Filter::it($dxchan->{spotfilter}, @{$fref}) if ref $fref;
1126                         next unless $filter;
1127                 }
1128                 next if $sort eq 'ann' && !$dxchan->{ann};
1129                 next if $sort eq 'wwv' && !$dxchan->{wwv};
1130                 next if $sort eq 'wx' && !$dxchan->{wx};
1131
1132                 $s =~ s/\a//og unless $dxchan->{beep};
1133
1134                 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
1135                         $dxchan->send($s);      
1136                 } else {
1137                         $dxchan->delay($s);
1138                 }
1139         }
1140 }
1141
1142 #
1143 # gimme all the ak1a nodes
1144 #
1145 sub get_all_ak1a
1146 {
1147         my @list = DXChannel->get_all();
1148         my $ref;
1149         my @out;
1150         foreach $ref (@list) {
1151                 push @out, $ref if $ref->is_ak1a;
1152         }
1153         return @out;
1154 }
1155
1156 # return a list of all users
1157 sub get_all_users
1158 {
1159         my @list = DXChannel->get_all();
1160         my $ref;
1161         my @out;
1162         foreach $ref (@list) {
1163                 push @out, $ref if $ref->is_user;
1164         }
1165         return @out;
1166 }
1167
1168 # return a list of all user callsigns
1169 sub get_all_user_calls
1170 {
1171         my @list = DXChannel->get_all();
1172         my $ref;
1173         my @out;
1174         foreach $ref (@list) {
1175                 push @out, $ref->call if $ref->is_user;
1176         }
1177         return @out;
1178 }
1179
1180 #
1181 # obtain the hops from the list for this callsign and pc no 
1182 #
1183
1184 sub get_hops
1185 {
1186         my $pcno = shift;
1187         my $hops = $DXProt::hopcount{$pcno};
1188         $hops = $DXProt::def_hopcount if !$hops;
1189         return "H$hops";       
1190 }
1191
1192
1193 # adjust the hop count on a per node basis using the user loadable 
1194 # hop table if available or else decrement an existing one
1195 #
1196
1197 sub adjust_hops
1198 {
1199         my $self = shift;
1200         my $s = shift;
1201         my $call = $self->{call};
1202         my $hops;
1203         
1204         if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
1205                 my ($pcno) = $s =~ /^PC(\d\d)/o;
1206                 confess "$call called adjust_hops with '$s'" unless $pcno;
1207                 my $ref = $nodehops{$call} if %nodehops;
1208                 if ($ref) {
1209                         my $newhops = $ref->{$pcno};
1210                         return "" if defined $newhops && $newhops == 0;
1211                         $newhops = $ref->{default} unless $newhops;
1212                         return "" if defined $newhops && $newhops == 0;
1213                         $newhops = $hops if !$newhops;
1214                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
1215                 } else {
1216                         # simply decrement it
1217                         $hops--;
1218                         return "" if !$hops;
1219                         $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
1220                 }
1221         }
1222         return $s;
1223 }
1224
1225
1226 # load hop tables
1227 #
1228 sub load_hops
1229 {
1230         my $self = shift;
1231         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1232         do "$main::data/hop_table.pl";
1233         return $@ if $@;
1234         return 0;
1235 }
1236
1237 # remove leading and trailing spaces from an input string
1238 sub unpad
1239 {
1240         my $s = shift;
1241         $s =~ s/^\s+|\s+$//;
1242         return $s;
1243 }
1244
1245 # add a ping request to the ping queues
1246 sub addping
1247 {
1248         my ($from, $to) = @_;
1249         my $ref = $pings{$to} || [];
1250         my $r = {};
1251         $r->{call} = $from;
1252         $r->{t} = [ gettimeofday ];
1253         route(undef, $to, pc51($to, $main::mycall, 1));
1254         push @$ref, $r;
1255         $pings{$to} = $ref;
1256 }
1257
1258 # add a rcmd request to the rcmd queues
1259 sub addrcmd
1260 {
1261         my ($from, $to, $cmd) = @_;
1262         my $r = {};
1263         $r->{call} = $from;
1264         $r->{t} = $main::systime;
1265         $r->{cmd} = $cmd;
1266         route(undef, $to, pc34($main::mycall, $to, $cmd));
1267         $rcmds{$to} = $r;
1268 }
1269 1;
1270 __END__