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