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