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