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