removed unwanted pc21 for isolated nodes
[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);
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: come in on wrong channel");
320                                 return;
321                         }
322                         my $i;
323                         
324                         
325                         for ($i = 2; $i < $#field; $i++) {
326                                 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (\S) (\d)/o;
327                                 next if !$call || length $call < 3 || length $call > 8;
328                                 next if !$confmode;
329                                 $call = uc $call;
330                                 next if DXCluster->get_exact($call); # we already have this (loop?)
331                                 
332                                 $confmode = $confmode eq '*';
333                                 DXNodeuser->new($self, $node, $call, $confmode, $here);
334                                 
335                                 # add this station to the user database, if required
336                                 $call =~ s/-\d+$//o;        # remove ssid for users
337                                 my $user = DXUser->get_current($call);
338                                 $user = DXUser->new($call) if !$user;
339                                 $user->homenode($node->call) if !$user->homenode;
340                                 $user->node($node->call);
341                                 $user->lastin($main::systime) unless DXChannel->get($call);
342                                 $user->put;
343                         }
344                         
345                         # queue up any messages (look for privates only)
346                         DXMsg::queue_msg(1) if $self->state eq 'normal';     
347                         last SWITCH;
348                 }
349                 
350                 if ($pcno == 17) {              # remove a user
351                         my $node = DXCluster->get_exact($field[2]);
352                         return unless $node;
353                         return unless $node->isa('DXNode');
354                         if ($node->dxchan != $self) {
355                                 dbg('chan', "LOOP: come in on wrong channel");
356                                 return;
357                         }
358                         my $ref = DXCluster->get_exact($field[1]);
359                         $ref->del() if $ref;
360                         last SWITCH;
361                 }
362                 
363                 if ($pcno == 18) {              # link request
364                         $self->send_local_config();
365                         $self->send(pc20());
366                         $self->state('init');   
367                         return;             # we don't pass these on
368                 }
369                 
370                 if ($pcno == 19) {              # incoming cluster list
371                         my $i;
372                         for ($i = 1; $i < $#field-1; $i += 4) {
373                                 my $here = $field[$i];
374                                 my $call = uc $field[$i+1];
375                                 my $confmode = $field[$i+2] eq '*';
376                                 my $ver = $field[$i+3];
377                                 
378                                 # now check the call over
379                                 my $node = DXCluster->get_exact($call);
380                                 if ($node && $node->dxchan != $self) {
381                                         dbg('chan', "LOOP: come in on wrong channel");
382                                         return;
383                                 }
384                                 next if $node; # we already have this
385                                 
386                                 # check for sane parameters
387                                 next if $ver < 5000; # only works with version 5 software
388                                 next if length $call < 3; # min 3 letter callsigns
389                                 DXNode->new($self, $call, $confmode, $here, $ver);
390                                 
391                                 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
392                                 my $mref = DXMsg::get_busy($call);
393                                 $mref->stop_msg($self) if $mref;
394                                 
395                                 # add this station to the user database, if required (don't remove SSID from nodes)
396                                 my $user = DXUser->get_current($call);
397                                 if (!$user) {
398                                         $user = DXUser->new($call);
399                                         $user->sort('A');
400                                         $user->priv(1);                   # I have relented and defaulted nodes
401                                         $self->{priv} = 1;                # to user RCMDs allowed
402                                         $user->homenode($call);
403                                         $user->node($call);
404                                 }
405                                 $user->lastin($main::systime) unless DXChannel->get($call);
406                                 $user->put;
407                         }
408                         
409                         # queue up any messages
410                         DXMsg::queue_msg(0) if $self->state eq 'normal';
411                         last SWITCH;
412                 }
413                 
414                 if ($pcno == 20) {              # send local configuration
415                         $self->send_local_config();
416                         $self->send(pc22());
417                         $self->state('normal');
418                         
419                         # queue mail
420                         DXMsg::queue_msg(0);
421
422                         return;
423                 }
424                 
425                 if ($pcno == 21) {              # delete a cluster from the list
426                         my $call = uc $field[1];
427                         if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
428                                 my $node = DXCluster->get_exact($call);
429                                 if ($node) {
430                                         if ($node->dxchan != $self) {
431                                                 dbg('chan', "LOOP: come in on wrong channel");
432                                                 return;
433                                         }
434                                         $node->del();
435                                 } else {
436                                         dbg('chan', "$call not in table, dropped");
437                                         return;
438                                 }
439                         }
440                         last SWITCH;
441                 }
442                 
443                 if ($pcno == 22) {
444                         $self->state('normal');
445                         
446                         # queue mail
447                         DXMsg::queue_msg(0);
448                         return;
449                 }
450                 
451                 if ($pcno == 23 || $pcno == 27) { # WWV info
452                         
453                         # route 'foreign' pc27s 
454                         if ($pcno == 27) {
455                                 if ($field[8] ne $main::mycall) {
456                                         route($field[8], $line);
457                                         return;
458                                 }
459                         }
460
461                         # do some de-duping
462                         my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
463                         my $sfi = unpad($field[3]);
464                         my $k = unpad($field[4]);
465                         my $i = unpad($field[5]);
466                         my $dupkey = "$d.$sfi$k$i";
467                         if ($wwvdup{$dupkey}) {
468                                 dbg('chan', "Dup WWV Spot ignored\n");
469                                 return;
470                         }
471                         if ($d > $main::systime + 900 || $field[2] < 0 || $field[2] > 23) {
472                                 dbg('chan', "WWV Date ($field[1] $field[2]) out of range");
473                                 return;
474                         }
475                         $wwvdup{$dupkey} = $d;
476                         $field[6] =~ s/-\d+$//o;            # remove spotter's ssid
477                 
478                         my $wwv = Geomag::update($d, $field[2], $sfi, $k, $i, @field[6..$#field]);
479
480                         my $r;
481                         eval {
482                                 $r = Local::wwv($self, $field[1], $field[2], $sfi, $k, $i, @field[6..$#field]);
483                         };
484 #                       dbg('local', "Local::wwv2 error $@") if $@;
485                         return if $r;
486
487                         # DON'T be silly and send on PC27s!
488                         return if $pcno == 27;
489
490                         # broadcast to the eager users
491                         broadcast_users("WWV de $field[7] <$field[2]>:   SFI=$sfi, A=$k, K=$i, $field[6]", 'wwv', $wwv );
492                         last SWITCH;
493                 }
494                 
495                 if ($pcno == 24) {              # set here status
496                         my $call = uc $field[1];
497                         my $ref = DXCluster->get_exact($call);
498                         $ref->here($field[2]) if $ref;
499                         last SWITCH;
500                 }
501                 
502                 if ($pcno == 25) {      # merge request
503                         if ($field[1] ne $main::mycall) {
504                                 route($field[1], $line);
505                                 return;
506                         }
507
508                         Log('DXProt', "Merge request for $field[3] spots and $field[4] WWV from $field[1]");
509                         
510                         # spots
511                         if ($field[3] > 0) {
512                                 my @in = reverse Spot::search(1, undef, undef, 0, $field[3]-1);
513                                 my $in;
514                                 foreach $in (@in) {
515                                         $self->send(pc26(@{$in}[0..4], $field[2]));
516                                 }
517                         }
518
519                         # wwv
520                         if ($field[4] > 0) {
521                                 my @in = reverse Geomag::search(0, $field[4], time, 1);
522                                 my $in;
523                                 foreach $in (@in) {
524                                         $self->send(pc27(@{$in}[0..5], $field[2]));
525                                 }
526                         }
527                         return;
528                 }
529
530                 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
531                         if ($field[1] eq $main::mycall) {
532                                 DXMsg::process($self, $line);
533                         } else {
534                                 route($field[1], $line);
535                         }
536                         return;
537                 }
538                 
539                 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
540                         if ($field[1] eq $main::mycall) {
541                                 my $ref = DXUser->get_current($field[2]);
542                                 my $cref = DXCluster->get($field[2]);
543                                 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
544                                 unless ($field[3] =~ /rcmd/i || !$cref || !$ref || $cref->mynode->call ne $ref->homenode) {    # not allowed to relay RCMDS!
545                                         if ($ref->{priv}) {     # you have to have SOME privilege, the commands have further filtering
546                                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
547                                                 my $oldpriv = $self->{priv};
548                                                 $self->{priv} = $ref->{priv};     # assume the user's privilege level
549                                                 my @in = (DXCommandmode::run_cmd($self, $field[3]));
550                                                 $self->{priv} = $oldpriv;
551                                                 for (@in) {
552                                                         s/\s*$//og;
553                                                         $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
554                                                         Log('rcmd', 'out', $field[2], $_);
555                                                 }
556                                                 delete $self->{remotecmd};
557                                         } else {
558                                                 $self->send(pc35($main::mycall, $field[2], "$main::mycall:sorry...!"));
559                                         }
560                                 } else {
561                                         $self->send(pc35($main::mycall, $field[2], "$main::mycall:your attempt is logged, Tut tut tut...!"));
562                                 }
563                         } else {
564                                 route($field[1], $line);
565                         }
566                         return;
567                 }
568                 
569                 if ($pcno == 35) {              # remote command replies
570                         if ($field[1] eq $main::mycall) {
571                                 my $s = $rcmds{$field[2]};
572                                 if ($s) {
573                                         my $dxchan = DXChannel->get($s->{call});
574                                         $dxchan->send($field[3]) if $dxchan;
575                                         delete $rcmds{$field[2]} if !$dxchan;
576                                 }
577                         } else {
578                                 route($field[1], $line);
579                         }
580                         return;
581                 }
582                 
583                 # for pc 37 see 44 onwards
584
585                 if ($pcno == 38) {              # node connected list from neighbour
586                         return;
587                 }
588                 
589                 if ($pcno == 39) {              # incoming disconnect
590                         $self->disconnect();
591                         return;
592                 }
593                 
594                 if ($pcno == 41) {              # user info
595                         # add this station to the user database, if required
596                         my $user = DXUser->get_current($field[1]);
597                         if (!$user) {
598                                 # then try without an SSID
599                                 $field[1] =~ s/-\d+$//o;
600                                 $user = DXUser->get_current($field[1]);
601                         }
602                         $user = DXUser->new($field[1]) if !$user;
603                         
604                         if ($field[2] == 1) {
605                                 $user->name($field[3]);
606                         } elsif ($field[2] == 2) {
607                                 $user->qth($field[3]);
608                         } elsif ($field[2] == 3) {
609                                 my ($lat, $long) = DXBearing::stoll($field[3]);
610                                 $user->lat($lat);
611                                 $user->long($long);
612                         } elsif ($field[2] == 4) {
613                                 $user->homenode($field[3]);
614                         }
615                         $user->put;
616                         last SWITCH;
617                 }
618                 if ($pcno == 43) {
619                         last SWITCH;
620                 }
621                 if ($pcno == 37 || $pcno == 44 || $pcno == 45 || $pcno == 46 || $pcno == 47 || $pcno == 49) {
622                         if ($field[1] eq $main::mycall) {
623                                 ;
624                         } else {
625                                 route($field[1], $line);
626                         }
627                         return;
628                 }
629                 
630                 if ($pcno == 50) {              # keep alive/user list
631                         my $node = DXCluster->get_exact($field[1]);
632                         if ($node) {
633                                 return unless $node->dxchan == $self;
634                                 $node->update_users($field[2]);
635                         }
636                         last SWITCH;
637                 }
638                 
639                 if ($pcno == 51) {              # incoming ping requests/answers
640                         
641                         # is it for us?
642                         if ($field[1] eq $main::mycall) {
643                                 my $flag = $field[3];
644                                 if ($flag == 1) {
645                                         $self->send(pc51($field[2], $field[1], '0'));
646                                 } else {
647                                         # it's a reply, look in the ping list for this one
648                                         my $ref = $pings{$field[2]};
649                                         if ($ref) {
650                                                 my $r = shift @$ref;
651                                                 my $dxchan = DXChannel->get($r->{call});
652                                                 $dxchan->send($dxchan->msg('pingi', $field[2], atime($main::systime), $main::systime - $r->{t})) if $dxchan;
653                                         }
654                                 }
655                                 
656                         } else {
657                                 # route down an appropriate thingy
658                                 route($field[1], $line);
659                         }
660                         return;
661                 }
662         }
663          
664          # if get here then rebroadcast the thing with its Hop count decremented (if
665          # there is one). If it has a hop count and it decrements to zero then don't
666          # rebroadcast it.
667          #
668          # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
669          #        REBROADCAST!!!!
670          #
671          
672         if (!$self->{isolate}) {
673                 broadcast_ak1a($line, $self); # send it to everyone but me
674         }
675 }
676
677 #
678 # This is called from inside the main cluster processing loop and is used
679 # for despatching commands that are doing some long processing job
680 #
681 sub process
682 {
683         my $t = time;
684         my @dxchan = DXChannel->get_all();
685         my $dxchan;
686         
687         foreach $dxchan (@dxchan) {
688                 next unless $dxchan->is_ak1a();
689                 next if $dxchan == $me;
690                 
691                 # send a pc50 out on this channel
692                 if ($t >= $dxchan->pc50_t + $DXProt::pc50_interval) {
693                         $dxchan->send(pc50());
694                         $dxchan->pc50_t($t);
695                 }
696         }
697         
698         my $key;
699         my $val;
700         my $cutoff;
701         if ($main::systime - 3600 > $last_hour) {
702                 $cutoff  = $main::systime - $pc11_dup_age;
703                 while (($key, $val) = each %spotdup) {
704                         delete $spotdup{$key} if $val < $cutoff;
705                 }
706                 $cutoff = $main::systime - $pc23_dup_age;
707                 while (($key, $val) = each %wwvdup) {
708                         delete $wwvdup{$key} if $val < $cutoff;
709                 }
710                 $cutoff = $main::systime - $pc12_dup_age;
711                 while (($key, $val) = each %anndup) {
712                         delete $anndup{$key} if $val < $cutoff;
713                 }
714                 $last_hour = $main::systime;
715         }
716 }
717
718 #
719 # finish up a pc context
720 #
721 sub finish
722 {
723         my $self = shift;
724         my $call = $self->call;
725         my $ref = DXCluster->get_exact($call);
726         
727         # unbusy and stop and outgoing mail
728         my $mref = DXMsg::get_busy($call);
729         $mref->stop_msg($self) if $mref;
730         
731         # broadcast to all other nodes that all the nodes connected to via me are gone
732         my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
733         my $node;
734         
735         foreach $node (@gonenodes) {
736                 next if $node->call eq $call;
737                 broadcast_ak1a(pc21($node->call, 'Gone') , $self) unless $self->{isolate}; 
738                 $node->del();
739         }
740
741         # remove outstanding pings
742         delete $pings{$call};
743         
744         # now broadcast to all other ak1a nodes that I have gone
745         broadcast_ak1a(pc21($call, 'Gone.'), $self) unless $self->{isolate};
746         
747         Log('DXProt', $call . " Disconnected");
748         $ref->del() if $ref;
749 }
750
751 #
752 # some active measures
753 #
754 sub send_dx_spot
755 {
756         my $self = shift;
757         my $line = shift;
758         my @dxchan = DXChannel->get_all();
759         my $dxchan;
760         
761         # send it if it isn't the except list and isn't isolated and still has a hop count
762         # taking into account filtering and so on
763         foreach $dxchan (@dxchan) {
764                 my $routeit;
765                 my ($filter, $hops) = Filter::it($dxchan->{spotfilter}, @_, $self->{call} ) if $dxchan->{spotfilter};
766                 if ($dxchan->is_ak1a) {
767                         next if $dxchan == $self;
768                         if ($hops) {
769                                 $routeit = $line;
770                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
771                         } else {
772                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
773                                 next unless $routeit;
774                         }
775                         if ($filter) {
776                                 $dxchan->send($routeit) if $routeit;
777                         } else {
778                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
779                                 
780                         }
781                 } elsif ($dxchan->is_user && $dxchan->{dx}) {
782                         my $buf = Spot::formatb($_[0], $_[1], $_[2], $_[3], $_[4]);
783                         $buf .= "\a\a" if $dxchan->{beep};
784                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
785                                 $dxchan->send($buf) if !$hops || ($hops && $filter);
786                         } else {
787                                 $dxchan->delay($buf) if !$hops || ($hops && $filter);
788                         }
789                 }                                       
790         }
791 }
792
793 sub send_local_config
794 {
795         my $self = shift;
796         my $n;
797         my @nodes;
798         my @localnodes;
799         my @remotenodes;
800                 
801         # send our nodes
802         if ($self->{isolate}) {
803                 @localnodes = (DXCluster->get_exact($main::mycall));
804         } else {
805                 # create a list of all the nodes that are not connected to this connection
806                 # and are not themselves isolated, this to make sure that isolated nodes
807         # don't appear outside of this node
808                 @nodes = DXNode::get_all();
809                 @nodes = grep { $_->{call} ne $main::mycall } @nodes;
810                 @nodes = grep { $_->dxchan != $self } @nodes if @nodes;
811                 @nodes = grep { !$_->dxchan->{isolate} } @nodes if @nodes;
812                 @localnodes = grep { $_->dxchan->{call} eq $_->{call} } @nodes if @nodes;
813                 unshift @localnodes, DXCluster->get_exact($main::mycall);
814                 @remotenodes = grep { $_->dxchan->{call} ne $_->{call} } @nodes if @nodes;
815         }
816
817         my @s = $me->pc19(@localnodes, @remotenodes);
818         for (@s) {
819                 my $routeit = adjust_hops($self, $_);
820                 $self->send($routeit) if $routeit;
821         }
822         
823         # get all the users connected on the above nodes and send them out
824         foreach $n (@localnodes, @remotenodes) {
825                 my @users = values %{$n->list};
826                 my @s = pc16($n, @users);
827                 for (@s) {
828                         my $routeit = adjust_hops($self, $_);
829                         $self->send($routeit) if $routeit;
830                 }
831         }
832 }
833
834 #
835 # route a message down an appropriate interface for a callsign
836 #
837 # is called route(to, pcline);
838 #
839 sub route
840 {
841         my ($call, $line) = @_;
842         my $cl = DXCluster->get_exact($call);
843         if ($cl) {
844                 my $hops;
845                 my $dxchan = $cl->{dxchan};
846                 if ($dxchan) {
847                         my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
848                         if ($routeit) {
849                                 $dxchan->send($routeit) if $dxchan;
850                         }
851                 }
852         }
853 }
854
855 # broadcast a message to all clusters taking into account isolation
856 # [except those mentioned after buffer]
857 sub broadcast_ak1a
858 {
859         my $s = shift;                          # the line to be rebroadcast
860         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
861         my @dxchan = get_all_ak1a();
862         my $dxchan;
863         
864         # send it if it isn't the except list and isn't isolated and still has a hop count
865         foreach $dxchan (@dxchan) {
866                 next if grep $dxchan == $_, @except;
867                 my $routeit = adjust_hops($dxchan, $s);      # adjust its hop count by node name
868                 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
869         }
870 }
871
872 # broadcast a message to all clusters ignoring isolation
873 # [except those mentioned after buffer]
874 sub broadcast_all_ak1a
875 {
876         my $s = shift;                          # the line to be rebroadcast
877         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
878         my @dxchan = get_all_ak1a();
879         my $dxchan;
880         
881         # send it if it isn't the except list and isn't isolated and still has a hop count
882         foreach $dxchan (@dxchan) {
883                 next if grep $dxchan == $_, @except;
884                 my $routeit = adjust_hops($dxchan, $s);      # adjust its hop count by node name
885                 $dxchan->send($routeit);
886         }
887 }
888
889 # broadcast to all users
890 # storing the spot or whatever until it is in a state to receive it
891 sub broadcast_users
892 {
893         my $s = shift;                          # the line to be rebroadcast
894         my $sort = shift;           # the type of transmission
895         my $fref = shift;           # a reference to an object to filter on
896         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
897         my @dxchan = get_all_users();
898         my $dxchan;
899         my @out;
900         
901         foreach $dxchan (@dxchan) {
902                 next if grep $dxchan == $_, @except;
903                 push @out, $dxchan;
904         }
905         broadcast_list($s, $sort, $fref, @out);
906 }
907
908 # broadcast to a list of users
909 sub broadcast_list
910 {
911         my $s = shift;
912         my $sort = shift;
913         my $fref = shift;
914         my $dxchan;
915         
916         foreach $dxchan (@_) {
917                 my $filter = 1;
918                 
919                 if ($sort eq 'dx') {
920                     next unless $dxchan->{dx};
921                         ($filter) = Filter::it($dxchan->{spotfilter}, @{$fref}) if ref $fref;
922                         next unless $filter;
923                 }
924                 next if $sort eq 'ann' && !$dxchan->{ann};
925                 next if $sort eq 'wwv' && !$dxchan->{wwv};
926                 next if $sort eq 'wx' && !$dxchan->{wx};
927
928                 $s =~ s/\a//og unless $dxchan->{beep};
929
930                 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
931                         $dxchan->send($s);      
932                 } else {
933                         $dxchan->delay($s);
934                 }
935         }
936 }
937
938 #
939 # gimme all the ak1a nodes
940 #
941 sub get_all_ak1a
942 {
943         my @list = DXChannel->get_all();
944         my $ref;
945         my @out;
946         foreach $ref (@list) {
947                 push @out, $ref if $ref->is_ak1a;
948         }
949         return @out;
950 }
951
952 # return a list of all users
953 sub get_all_users
954 {
955         my @list = DXChannel->get_all();
956         my $ref;
957         my @out;
958         foreach $ref (@list) {
959                 push @out, $ref if $ref->is_user;
960         }
961         return @out;
962 }
963
964 # return a list of all user callsigns
965 sub get_all_user_calls
966 {
967         my @list = DXChannel->get_all();
968         my $ref;
969         my @out;
970         foreach $ref (@list) {
971                 push @out, $ref->call if $ref->is_user;
972         }
973         return @out;
974 }
975
976 #
977 # obtain the hops from the list for this callsign and pc no 
978 #
979
980 sub get_hops
981 {
982         my ($pcno) = @_;
983         my $hops = $DXProt::hopcount{$pcno};
984         $hops = $DXProt::def_hopcount if !$hops;
985         return "H$hops";       
986 }
987
988
989 # adjust the hop count on a per node basis using the user loadable 
990 # hop table if available or else decrement an existing one
991 #
992
993 sub adjust_hops
994 {
995         my $self = shift;
996         my $s = shift;
997         my $call = $self->{call};
998         my $hops;
999         
1000         if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
1001                 my ($pcno) = $s =~ /^PC(\d\d)/o;
1002                 confess "$call called adjust_hops with '$s'" unless $pcno;
1003                 my $ref = $nodehops{$call} if %nodehops;
1004                 if ($ref) {
1005                         my $newhops = $ref->{$pcno};
1006                         return "" if defined $newhops && $newhops == 0;
1007                         $newhops = $ref->{default} unless $newhops;
1008                         return "" if defined $newhops && $newhops == 0;
1009                         $newhops = $hops if !$newhops;
1010                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
1011                 } else {
1012                         # simply decrement it
1013                         $hops--;
1014                         return "" if !$hops;
1015                         $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
1016                 }
1017         }
1018         return $s;
1019 }
1020
1021
1022 # load hop tables
1023 #
1024 sub load_hops
1025 {
1026         my $self = shift;
1027         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1028         do "$main::data/hop_table.pl";
1029         return $@ if $@;
1030         return 0;
1031 }
1032
1033 # remove leading and trailing spaces from an input string
1034 sub unpad
1035 {
1036         my $s = shift;
1037         $s =~ s/^\s+|\s+$//;
1038         return $s;
1039 }
1040
1041 # add a ping request to the ping queues
1042 sub addping
1043 {
1044         my ($from, $to) = @_;
1045         my $ref = $pings{$to};
1046         $ref = $pings{$to} = [] if !$ref;
1047         my $r = {};
1048         $r->{call} = $from;
1049         $r->{t} = $main::systime;
1050         route($to, pc51($to, $main::mycall, 1));
1051         push @$ref, $r;
1052 }
1053
1054 # add a rcmd request to the rcmd queues
1055 sub addrcmd
1056 {
1057         my ($from, $to, $cmd) = @_;
1058         my $r = {};
1059         $r->{call} = $from;
1060         $r->{t} = $main::systime;
1061         $r->{cmd} = $cmd;
1062         route($to, pc34($main::mycall, $to, $cmd));
1063         $rcmds{$to} = $r;
1064 }
1065 1;
1066 __END__