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