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