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