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