3 # This module impliments the protocal mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
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);
35 $me = undef; # the channel id for this cluster
36 $pc11_max_age = 1*3600; # the maximum age for an incoming 'real-time' pc11
37 $pc11_dup_age = 24*3600; # the maximum time to keep the spot dup list for
38 $pc23_dup_age = 24*3600; # the maximum time to keep the wwv dup list for
39 %spotdup = (); # the pc11 and 26 dup hash
40 %wwvdup = (); # the pc23 and 27 dup hash
41 $last_hour = time; # last time I did an hourly periodic update
42 %pings = (); # outstanding ping requests outbound
43 %rcmds = (); # outstanding rcmd requests outbound
44 %nodehops = (); # node specific hop control
45 @baddx = (); # list of illegal spotted callsigns
47 $baddxfn = "$main::data/baddx.pl";
51 my $user = DXUser->get($main::mycall);
52 $DXProt::myprot_version += $main::version*100;
53 $me = DXProt->new($main::mycall, 0, $user);
55 $me->{state} = "indifferent";
56 do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
58 # $me->{sort} = 'M'; # M for me
60 # now prime the spot duplicates file with today's and yesterday's data
61 my @today = Julian::unixtoj(time);
62 my @spots = Spot::readfile(@today);
63 @today = Julian::sub(@today, 1);
64 push @spots, Spot::readfile(@today);
66 my $dupkey = "$_->[0]$_->[1]$_->[2]$_->[3]$_->[4]";
67 $spotdup{$dupkey} = $_->[2];
70 # now prime the wwv duplicates file with just this month's data
71 my @wwv = Geomag::readfile(time);
73 my $dupkey = "$_->[1].$_->[2]$_->[3]$_->[4]";
74 $wwvdup{$dupkey} = $_->[1];
78 do "$baddxfn" if -e "$baddxfn";
83 # obtain a new connection this is derived from dxchannel
88 my $self = DXChannel::alloc(@_);
89 $self->{'sort'} = 'A'; # in absence of how to find out what sort of an object I am
93 # this is how a pc connection starts (for an incoming connection)
94 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
95 # all the crap that comes between).
98 my ($self, $line, $sort) = @_;
99 my $call = $self->{call};
100 my $user = $self->{user};
102 # remember type of connection
103 $self->{consort} = $line;
104 $self->{outbound} = $sort eq 'O';
105 $self->{priv} = $user->priv;
106 $self->{lang} = $user->lang;
107 $self->{isolate} = $user->{isolate};
108 $self->{consort} = $line; # save the connection type
112 $self->{spotfilter} = Filter::read_in('spots', $call);
113 $self->{wwvfilter} = Filter::read_in('wwv', $call);
114 $self->{annfilter} = Filter::read_in('ann', $call);
117 $self->send_now('B',"0");
119 # send initialisation string
120 if (!$self->{outbound}) {
121 $self->send(pc38()) if DXNode->get_all();
124 $self->state('init');
127 Log('DXProt', "$call connected");
131 # This is the normal pcxx despatcher
135 my ($self, $line) = @_;
136 my @field = split /\^/, $line;
137 pop @field if $field[-1] eq '~';
139 # print join(',', @field), "\n";
141 # ignore any lines that don't start with PC
142 return if !$field[0] =~ /^PC/;
145 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
147 return if $pcno < 10 || $pcno > 51;
152 $pcr = Local::pcprot($self, $pcno, @field);
154 # dbg('local', "Local::pcprot error $@") if $@;
158 if ($pcno == 10) { # incoming talk
160 # is it for me or one of mine?
161 my $call = ($field[5] gt ' ') ? $field[5] : $field[2];
162 if ($call eq $main::mycall || grep $_ eq $call, get_all_user_calls()) {
165 my $text = unpad($field[3]);
166 Log('talk', $call, $field[1], $field[6], $text);
167 $call = $main::myalias if $call eq $main::mycall;
168 my $ref = DXChannel->get($call);
169 $ref->send("$call de $field[1]: $text") if $ref && $ref->{talk};
171 route($field[2], $line); # relay it on its way
176 if ($pcno == 11 || $pcno == 26) { # dx spot
178 # if this is a 'nodx' node then ignore it
179 last SWITCH if grep $field[7] =~ /^$_/, @DXProt::nodx_node;
181 # convert the date to a unix date
182 my $d = cltounix($field[3], $field[4]);
183 # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
184 if (!$d || ($pcno == 11 && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
185 dbg('chan', "Spot ignored, invalid date or out of range ($field[3] $field[4])\n");
189 # strip off the leading & trailing spaces from the comment
190 my $text = unpad($field[5]);
193 my $spotter = $field[6];
194 $spotter =~ s/-\d+$//o; # strip off the ssid from the spotter
197 my $freq = $field[1] - 0;
198 my $dupkey = "$freq$field[2]$d$text$spotter";
199 if ($spotdup{$dupkey}) {
200 dbg('chan', "Duplicate Spot ignored\n");
204 $spotdup{$dupkey} = $d;
207 if (grep $field[2] eq $_, @baddx) {
208 dbg('chan', "Bad DX spot, ignored");
212 my @spot = Spot::add($freq, $field[2], $d, $text, $spotter, $field[7]);
215 # @spot at this point contains:-
216 # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
217 # then spotted itu, spotted cq, spotters itu, spotters cq
218 # you should be able to route on any of these
224 $r = Local::spot($self, @spot);
226 # dbg('local', "Local::spot1 error $@") if $@;
229 # DON'T be silly and send on PC26s!
230 return if $pcno == 26;
232 # send out the filtered spots
233 send_dx_spot($self, $line, @spot) if @spot;
237 if ($pcno == 12) { # announces
239 if ($field[2] eq '*' || $field[2] eq $main::mycall) {
241 # strip leading and trailing stuff
242 my $text = unpad($field[3]);
247 if ($field[4] eq '*') { # sysops
249 @list = map { $_->priv >= 5 ? $_ : () } get_all_users();
250 } elsif ($field[4] gt ' ') { # speciality list handling
251 my ($name) = split /\./, $field[4];
252 $target = "$name"; # put the rest in later (if bothered)
255 if ($field[6] eq '1') {
259 $target = "All" if !$target;
262 broadcast_list("$to$target de $field[1]: $text", 'ann', undef, @list);
264 broadcast_users("$target de $field[1]: $text", 'ann', undef);
266 Log('ann', $target, $field[1], $text);
268 return if $field[2] eq $main::mycall; # it's routed to me
270 route($field[2], $line);
271 return; # only on a routed one
287 if ($pcno == 16) { # add a user
288 my $node = DXCluster->get_exact($field[1]);
289 last SWITCH if !$node; # ignore if havn't seen a PC19 for this one yet
290 last SWITCH unless $node->isa('DXNode');
294 for ($i = 2; $i < $#field; $i++) {
295 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (\S) (\d)/o;
296 next if !$call || length $call < 3 || length $call > 8;
299 next if DXCluster->get_exact($call); # we already have this (loop?)
301 $confmode = $confmode eq '*';
302 DXNodeuser->new($self, $node, $call, $confmode, $here);
304 # add this station to the user database, if required
305 $call =~ s/-\d+$//o; # remove ssid for users
306 my $user = DXUser->get_current($call);
307 $user = DXUser->new($call) if !$user;
308 $user->homenode($node->call) if !$user->homenode;
309 $user->node($node->call);
310 $user->lastin($main::systime);
314 # queue up any messages (look for privates only)
315 DXMsg::queue_msg(1) if $self->state eq 'normal';
319 if ($pcno == 17) { # remove a user
321 my $ref = DXCluster->get_exact($field[1]);
326 if ($pcno == 18) { # link request
327 $self->send_local_config();
329 $self->state('init');
330 return; # we don't pass these on
333 if ($pcno == 19) { # incoming cluster list
335 for ($i = 1; $i < $#field-1; $i += 4) {
336 my $here = $field[$i];
337 my $call = uc $field[$i+1];
338 my $confmode = $field[$i+2] eq '*';
339 my $ver = $field[$i+3];
341 # now check the call over
342 next if DXCluster->get_exact($call); # we already have this
344 # check for sane parameters
345 next if $ver < 5000; # only works with version 5 software
346 next if length $call < 3; # min 3 letter callsigns
347 DXNode->new($self, $call, $confmode, $here, $ver);
349 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
350 my $mref = DXMsg::get_busy($call);
351 $mref->stop_msg($self) if $mref;
353 # add this station to the user database, if required (don't remove SSID from nodes)
354 my $user = DXUser->get_current($call);
356 $user = DXUser->new($call);
358 $user->priv(1); # I have relented and defaulted nodes
359 $self->{priv} = 1; # to user RCMDs allowed
360 $user->homenode($call);
363 $user->lastin($main::systime);
367 # queue up any messages
368 DXMsg::queue_msg(0) if $self->state eq 'normal';
372 if ($pcno == 20) { # send local configuration
373 $self->send_local_config();
375 $self->state('normal');
383 if ($pcno == 21) { # delete a cluster from the list
384 my $call = uc $field[1];
385 if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
386 my $ref = DXCluster->get_exact($call);
393 $self->state('normal');
400 if ($pcno == 23 || $pcno == 27) { # WWV info
402 my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
403 my $sfi = unpad($field[3]);
404 my $k = unpad($field[4]);
405 my $i = unpad($field[5]);
406 my $dupkey = "$d.$sfi$k$i";
407 if ($wwvdup{$dupkey}) {
408 dbg('chan', "Dup WWV Spot ignored\n");
411 if ($d > $main::systime + 900 || $field[2] < 0 || $field[2] > 23) {
412 dbg('chan', "WWV Date ($field[1] $field[2]) out of range");
415 $wwvdup{$dupkey} = $d;
416 $field[6] =~ s/-\d+$//o; # remove spotter's ssid
418 my $wwv = Geomag::update($d, $field[2], $sfi, $k, $i, @field[6..$#field]);
422 $r = Local::wwv($self, $field[1], $field[2], $sfi, $k, $i, @field[6..$#field]);
424 # dbg('local', "Local::wwv2 error $@") if $@;
427 # DON'T be silly and send on PC27s!
428 return if $pcno == 27;
430 # broadcast to the eager users
431 broadcast_users("WWV de $field[7] <$field[2]>: SFI=$sfi, A=$k, K=$i, $field[6]", 'wwv', $wwv );
435 if ($pcno == 24) { # set here status
436 my $call = uc $field[1];
437 my $ref = DXCluster->get_exact($call);
438 $ref->here($field[2]) if $ref;
442 if ($pcno == 25) { # merge request
443 unless ($field[1] eq $main::mycall) {
444 dbg('chan', "merge request to $field[1] from $field[2] ignored");
448 Log('DXProt', "Merge request for $field[3] spots and $field[4] WWV from $field[1]");
452 my @in = reverse Spot::search(1, undef, undef, 0, $field[3]-1);
455 $self->send(pc26(@{$in}[0..4], $in->[7]));
461 my @in = reverse Geomag::search(0, $field[4], time, 1);
464 $self->send(pc27(@{$in}));
470 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
471 DXMsg::process($self, $line);
475 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
476 if ($field[1] eq $main::mycall) {
477 my $ref = DXUser->get_current($field[2]);
478 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
479 unless ($field[3] =~ /rcmd/i) { # not allowed to relay RCMDS!
480 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
481 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
482 my @in = (DXCommandmode::run_cmd($self, $field[3]));
485 $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
486 Log('rcmd', 'out', $field[2], $_);
488 delete $self->{remotecmd};
491 $self->send(pc35($main::mycall, $field[2], "$main::mycall:Tut tut tut...!"));
494 route($field[1], $line);
499 if ($pcno == 35) { # remote command replies
500 if ($field[1] eq $main::mycall) {
501 my $s = $rcmds{$field[2]};
503 my $dxchan = DXChannel->get($s->{call});
504 $dxchan->send($field[3]) if $dxchan;
505 delete $rcmds{$field[2]} if !$dxchan;
508 route($field[1], $line);
517 if ($pcno == 38) { # node connected list from neighbour
521 if ($pcno == 39) { # incoming disconnect
526 if ($pcno == 41) { # user info
527 # add this station to the user database, if required
528 my $user = DXUser->get_current($field[1]);
530 # then try without an SSID
531 $field[1] =~ s/-\d+$//o;
532 $user = DXUser->get_current($field[1]);
534 $user = DXUser->new($field[1]) if !$user;
536 if ($field[2] == 1) {
537 $user->name($field[3]);
538 } elsif ($field[2] == 2) {
539 $user->qth($field[3]);
540 } elsif ($field[2] == 3) {
541 my ($lat, $long) = DXBearing::stoll($field[3]);
544 } elsif ($field[2] == 4) {
545 $user->homenode($field[3]);
569 if ($pcno == 50) { # keep alive/user list
570 my $ref = DXCluster->get_exact($field[1]);
571 $ref->update_users($field[2]) if $ref;
575 if ($pcno == 51) { # incoming ping requests/answers
578 if ($field[1] eq $main::mycall) {
579 my $flag = $field[3];
581 $self->send(pc51($field[2], $field[1], '0'));
583 # it's a reply, look in the ping list for this one
584 my $ref = $pings{$field[2]};
587 my $dxchan = DXChannel->get($r->{call});
588 $dxchan->send($dxchan->msg('pingi', $field[2], atime($main::systime), $main::systime - $r->{t})) if $dxchan;
593 # route down an appropriate thingy
594 route($field[1], $line);
600 # if get here then rebroadcast the thing with its Hop count decremented (if
601 # there is one). If it has a hop count and it decrements to zero then don't
604 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
608 if (!$self->{isolate}) {
609 broadcast_ak1a($line, $self); # send it to everyone but me
614 # This is called from inside the main cluster processing loop and is used
615 # for despatching commands that are doing some long processing job
620 my @dxchan = DXChannel->get_all();
623 foreach $dxchan (@dxchan) {
624 next unless $dxchan->is_ak1a();
625 next if $dxchan == $me;
627 # send a pc50 out on this channel
628 if ($t >= $dxchan->pc50_t + $DXProt::pc50_interval) {
629 $dxchan->send(pc50());
637 if ($main::systime - 3600 > $last_hour) {
638 $cutoff = $main::systime - $pc11_dup_age;
639 while (($key, $val) = each %spotdup) {
640 delete $spotdup{$key} if $val < $cutoff;
642 $cutoff = $main::systime - $pc23_dup_age;
643 while (($key, $val) = each %wwvdup) {
644 delete $wwvdup{$key} if $val < $cutoff;
646 $last_hour = $main::systime;
651 # finish up a pc context
656 my $call = $self->call;
657 my $ref = DXCluster->get_exact($call);
659 # unbusy and stop and outgoing mail
660 my $mref = DXMsg::get_busy($call);
661 $mref->stop_msg($self) if $mref;
663 # broadcast to all other nodes that all the nodes connected to via me are gone
664 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
667 foreach $node (@gonenodes) {
668 next if $node->call eq $call;
669 broadcast_ak1a(pc21($node->call, 'Gone') , $self) unless $self->{isolate};
673 # remove outstanding pings
674 delete $pings{$call};
676 # now broadcast to all other ak1a nodes that I have gone
677 broadcast_ak1a(pc21($call, 'Gone.'), $self);
679 Log('DXProt', $call . " Disconnected");
684 # some active measures
690 my @dxchan = DXChannel->get_all();
693 # send it if it isn't the except list and isn't isolated and still has a hop count
694 # taking into account filtering and so on
695 foreach $dxchan (@dxchan) {
697 my ($filter, $hops) = Filter::it($dxchan->{spotfilter}, @_, $self->{call} ) if $dxchan->{spotfilter};
698 if ($dxchan->is_ak1a) {
699 next if $dxchan == $self;
702 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
704 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
705 next unless $routeit;
708 $dxchan->send($routeit) if $routeit;
710 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
713 } elsif ($dxchan->is_user) {
714 my $buf = Spot::formatb($_[0], $_[1], $_[2], $_[3], $_[4]);
715 $buf .= "\a\a" if $dxchan->beep;
716 $dxchan->send($buf) if !$hops || ($hops && $filter);
721 sub send_local_config
730 if ($self->{isolate}) {
731 @localnodes = (DXCluster->get_exact($main::mycall));
733 # create a list of all the nodes that are not connected to this connection
734 # and are not themselves isolated, this to make sure that isolated nodes
735 # don't appear outside of this node
736 @nodes = DXNode::get_all();
737 @nodes = grep { $_->{call} ne $main::mycall } @nodes;
738 @nodes = grep { $_->dxchan != $self } @nodes if @nodes;
739 @nodes = grep { !$_->dxchan->{isolate} } @nodes if @nodes;
740 @localnodes = grep { $_->dxchan->{call} eq $_->{call} } @nodes if @nodes;
741 unshift @localnodes, DXCluster->get_exact($main::mycall);
742 @remotenodes = grep { $_->dxchan->{call} ne $_->{call} } @nodes if @nodes;
745 my @s = $me->pc19(@localnodes, @remotenodes);
747 my $routeit = adjust_hops($self, $_);
748 $self->send($routeit) if $routeit;
751 # get all the users connected on the above nodes and send them out
752 foreach $n (@localnodes, @remotenodes) {
753 my @users = values %{$n->list};
754 my @s = pc16($n, @users);
756 my $routeit = adjust_hops($self, $_);
757 $self->send($routeit) if $routeit;
763 # route a message down an appropriate interface for a callsign
765 # is called route(to, pcline);
769 my ($call, $line) = @_;
770 my $cl = DXCluster->get_exact($call);
773 my $dxchan = $cl->{dxchan};
775 my $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
777 $dxchan->send($routeit) if $dxchan;
783 # broadcast a message to all clusters taking into account isolation
784 # [except those mentioned after buffer]
787 my $s = shift; # the line to be rebroadcast
788 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
789 my @dxchan = get_all_ak1a();
792 # send it if it isn't the except list and isn't isolated and still has a hop count
793 foreach $dxchan (@dxchan) {
794 next if grep $dxchan == $_, @except;
795 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
796 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
800 # broadcast a message to all clusters ignoring isolation
801 # [except those mentioned after buffer]
802 sub broadcast_all_ak1a
804 my $s = shift; # the line to be rebroadcast
805 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
806 my @dxchan = get_all_ak1a();
809 # send it if it isn't the except list and isn't isolated and still has a hop count
810 foreach $dxchan (@dxchan) {
811 next if grep $dxchan == $_, @except;
812 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
813 $dxchan->send($routeit);
817 # broadcast to all users
818 # storing the spot or whatever until it is in a state to receive it
821 my $s = shift; # the line to be rebroadcast
822 my $sort = shift; # the type of transmission
823 my $fref = shift; # a reference to an object to filter on
824 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
825 my @dxchan = get_all_users();
829 foreach $dxchan (@dxchan) {
830 next if grep $dxchan == $_, @except;
833 broadcast_list($s, $sort, $fref, @out);
836 # broadcast to a list of users
844 foreach $dxchan (@_) {
848 next unless $dxchan->{dx};
849 ($filter) = Filter::it($dxchan->{spotfilter}, @{$fref}) if ref $fref;
852 next if $sort eq 'ann' && !$dxchan->{ann};
853 next if $sort eq 'wwv' && !$dxchan->{wwv};
854 next if $sort eq 'wx' && !$dxchan->{wx};
856 $s =~ s/\a//og unless $dxchan->{beep};
858 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
867 # gimme all the ak1a nodes
871 my @list = DXChannel->get_all();
874 foreach $ref (@list) {
875 push @out, $ref if $ref->is_ak1a;
880 # return a list of all users
883 my @list = DXChannel->get_all();
886 foreach $ref (@list) {
887 push @out, $ref if $ref->is_user;
892 # return a list of all user callsigns
893 sub get_all_user_calls
895 my @list = DXChannel->get_all();
898 foreach $ref (@list) {
899 push @out, $ref->call if $ref->is_user;
905 # obtain the hops from the list for this callsign and pc no
911 my $hops = $DXProt::hopcount{$pcno};
912 $hops = $DXProt::def_hopcount if !$hops;
917 # adjust the hop count on a per node basis using the user loadable
918 # hop table if available or else decrement an existing one
925 my $call = $self->{call};
928 if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
929 my ($pcno) = $s =~ /^PC(\d\d)/o;
930 confess "$call called adjust_hops with '$s'" unless $pcno;
931 my $ref = $nodehops{$call} if %nodehops;
933 my $newhops = $ref->{$pcno};
934 return "" if defined $newhops && $newhops == 0;
935 $newhops = $ref->{default} unless $newhops;
936 return "" if defined $newhops && $newhops == 0;
937 $newhops = $hops if !$newhops;
938 $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
940 # simply decrement it
943 $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
955 return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
956 do "$main::data/hop_table.pl";
961 # remove leading and trailing spaces from an input string
969 # add a ping request to the ping queues
972 my ($from, $to) = @_;
973 my $ref = $pings{$to};
974 $ref = $pings{$to} = [] if !$ref;
977 $r->{t} = $main::systime;
978 route($to, pc51($to, $main::mycall, 1));
982 # add a rcmd request to the rcmd queues
985 my ($from, $to, $cmd) = @_;
988 $r->{t} = $main::systime;
990 route($to, pc34($main::mycall, $to, $cmd));