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