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