3 # This module impliments the protocal mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
30 use vars qw($me $pc11_max_age $pc11_dup_age $pc23_dup_age %spotdup %wwvdup $last_hour %pings %rcmds %nodehops);
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
46 my $user = DXUser->get($main::mycall);
47 $DXProt::myprot_version += $main::version*100;
48 $me = DXProt->new($main::mycall, 0, $user);
50 $me->{state} = "indifferent";
51 do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
53 # $me->{sort} = 'M'; # M for me
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);
61 my $dupkey = "$_->[0]$_->[1]$_->[2]$_->[3]$_->[4]";
62 $spotdup{$dupkey} = $_->[2];
65 # now prime the wwv duplicates file with just this month's data
66 my @wwv = Geomag::readfile(time);
68 my $dupkey = "$_->[1].$_->[2]$_->[3]$_->[4]";
69 $wwvdup{$dupkey} = $_->[1];
75 # obtain a new connection this is derived from dxchannel
80 my $self = DXChannel::alloc(@_);
81 $self->{'sort'} = 'A'; # in absence of how to find out what sort of an object I am
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).
90 my ($self, $line, $sort) = @_;
91 my $call = $self->{call};
92 my $user = $self->{user};
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
104 $self->send_now('B',"0");
106 # send initialisation string
107 if (!$self->{outbound}) {
108 $self->send(pc38()) if DXNode->get_all();
111 $self->state('init');
114 Log('DXProt', "$call connected");
118 # This is the normal pcxx despatcher
122 my ($self, $line) = @_;
123 my @field = split /\^/, $line;
124 pop @field if $field[-1] eq '~';
126 # print join(',', @field), "\n";
128 # ignore any lines that don't start with PC
129 return if !$field[0] =~ /^PC/;
132 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
134 return if $pcno < 10 || $pcno > 51;
139 $pcr = Local::pcprot($self, $pcno, @field);
141 dbg('local', "Local::pcprot error $@") if $@;
145 if ($pcno == 10) { # incoming talk
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()) {
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};
158 route($field[2], $line); # relay it on its way
163 if ($pcno == 11 || $pcno == 26) { # dx spot
165 # if this is a 'nodx' node then ignore it
166 last SWITCH if grep $field[7] =~ /^$_/, @DXProt::nodx_node;
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");
176 # strip off the leading & trailing spaces from the comment
177 my $text = unpad($field[5]);
180 my $spotter = $field[6];
181 $spotter =~ s/-\d+$//o; # strip off the ssid from the spotter
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");
191 $spotdup{$dupkey} = $d;
193 my $spot = Spot::add($freq, $field[2], $d, $text, $spotter, $field[7]);
198 $r = Local::spot1($self, $freq, $field[2], $d, $text, $spotter, $field[7]);
200 dbg('local', "Local::spot1 error $@") if $@;
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);
209 # DON'T be silly and send on PC26s!
210 return if $pcno == 26;
215 if ($pcno == 12) { # announces
217 if ($field[2] eq '*' || $field[2] eq $main::mycall) {
219 # strip leading and trailing stuff
220 my $text = unpad($field[3]);
225 if ($field[4] eq '*') { # sysops
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)
233 if ($field[6] eq '1') {
237 $target = "All" if !$target;
240 broadcast_list("$to$target de $field[1]: $text", 'ann', undef, @list);
242 broadcast_users("$target de $field[1]: $text", 'ann', undef);
244 Log('ann', $target, $field[1], $text);
246 return if $field[2] eq $main::mycall; # it's routed to me
248 route($field[2], $line);
249 return; # only on a routed one
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');
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;
277 next if DXCluster->get_exact($call); # we already have this (loop?)
279 $confmode = $confmode eq '*';
280 DXNodeuser->new($self, $node, $call, $confmode, $here);
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);
292 # queue up any messages (look for privates only)
293 DXMsg::queue_msg(1) if $self->state eq 'normal';
297 if ($pcno == 17) { # remove a user
299 my $ref = DXCluster->get_exact($field[1]);
304 if ($pcno == 18) { # link request
305 $self->send_local_config();
307 $self->state('init');
308 return; # we don't pass these on
311 if ($pcno == 19) { # incoming cluster list
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];
319 # now check the call over
320 next if DXCluster->get_exact($call); # we already have this
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);
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;
331 # add this station to the user database, if required (don't remove SSID from nodes)
332 my $user = DXUser->get_current($call);
334 $user = DXUser->new($call);
336 $user->priv(1); # I have relented and defaulted nodes
337 $self->{priv} = 1; # to user RCMDs allowed
338 $user->homenode($call);
341 $user->lastin($main::systime);
345 # queue up any messages
346 DXMsg::queue_msg(0) if $self->state eq 'normal';
350 if ($pcno == 20) { # send local configuration
351 $self->send_local_config();
353 $self->state('normal');
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);
370 $self->state('normal');
377 if ($pcno == 23 || $pcno == 27) { # WWV info
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");
388 if ($d > $main::systime + 900) {
389 dbg('chan', "WWV Date ($field[1] $field[2]) out of range");
393 $wwvdup{$dupkey} = $d;
394 my $wwv = Geomag::update($d, $field[2], $sfi, $k, $i, @field[6..$#field]);
398 $r = Local::wwv2($self, $field[1], $field[2], $sfi, $k, $i, @field[6..$#field]);
400 dbg('local', "Local::wwv2 error $@") if $@;
403 # DON'T be silly and send on PC27s!
404 return if $pcno == 27;
406 # broadcast to the eager users
407 broadcast_users("WWV de $field[7] <$field[2]>: SFI=$sfi, K=$k, A=$i, $field[6]", 'wwv', $wwv );
411 if ($pcno == 24) { # set here status
412 my $call = uc $field[1];
413 my $ref = DXCluster->get_exact($call);
414 $ref->here($field[2]) if $ref;
418 if ($pcno == 25) { # merge request
419 unless ($field[1] eq $main::mycall) {
420 dbg('chan', "merge request to $field[1] from $field[2] ignored");
424 Log('DXProt', "Merge request for $field[3] spots and $field[4] WWV from $field[1]");
428 my @in = reverse Spot::search(1, undef, undef, 0, $field[3]-1);
431 $self->send(pc26(@{$in}[0..4], $in->[7]));
437 my @in = reverse Geomag::search(0, $field[4], time, 1);
440 $self->send(pc27(@{$in}));
446 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
447 DXMsg::process($self, $line);
451 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
452 if ($field[1] eq $main::mycall) {
453 my $ref = DXUser->get_current($field[2]);
454 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
455 unless ($field[3] =~ /rcmd/i) { # not allowed to relay RCMDS!
456 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
457 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
458 my @in = (DXCommandmode::run_cmd($self, $field[3]));
461 $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
462 Log('rcmd', 'out', $field[2], $_);
464 delete $self->{remotecmd};
467 $self->send(pc35($main::mycall, $field[2], "$main::mycall:Tut tut tut...!"));
470 route($field[1], $line);
475 if ($pcno == 35) { # remote command replies
476 if ($field[1] eq $main::mycall) {
477 my $s = $rcmds{$field[2]};
479 my $dxchan = DXChannel->get($s->{call});
480 $dxchan->send($field[3]) if $dxchan;
481 delete $rcmds{$field[2]} if !$dxchan;
484 route($field[1], $line);
493 if ($pcno == 38) { # node connected list from neighbour
497 if ($pcno == 39) { # incoming disconnect
502 if ($pcno == 41) { # user info
503 # add this station to the user database, if required
504 my $user = DXUser->get_current($field[1]);
506 # then try without an SSID
507 $field[1] =~ s/-\d+$//o;
508 $user = DXUser->get_current($field[1]);
510 $user = DXUser->new($field[1]) if !$user;
512 if ($field[2] == 1) {
513 $user->name($field[3]);
514 } elsif ($field[2] == 2) {
515 $user->qth($field[3]);
516 } elsif ($field[2] == 3) {
517 my ($lat, $long) = DXBearing::stoll($field[3]);
520 } elsif ($field[2] == 4) {
521 $user->homenode($field[3]);
545 if ($pcno == 50) { # keep alive/user list
546 my $ref = DXCluster->get_exact($field[1]);
547 $ref->update_users($field[2]) if $ref;
551 if ($pcno == 51) { # incoming ping requests/answers
554 if ($field[1] eq $main::mycall) {
555 my $flag = $field[3];
557 $self->send(pc51($field[2], $field[1], '0'));
559 # it's a reply, look in the ping list for this one
560 my $ref = $pings{$field[2]};
563 my $dxchan = DXChannel->get($r->{call});
564 $dxchan->send($dxchan->msg('pingi', $field[2], atime($main::systime), $main::systime - $r->{t})) if $dxchan;
569 # route down an appropriate thingy
570 route($field[1], $line);
576 # if get here then rebroadcast the thing with its Hop count decremented (if
577 # there is one). If it has a hop count and it decrements to zero then don't
580 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
584 if (!$self->{isolate}) {
585 broadcast_ak1a($line, $self); # send it to everyone but me
590 # This is called from inside the main cluster processing loop and is used
591 # for despatching commands that are doing some long processing job
596 my @dxchan = DXChannel->get_all();
599 foreach $dxchan (@dxchan) {
600 next unless $dxchan->is_ak1a();
601 next if $dxchan == $me;
603 # send a pc50 out on this channel
604 if ($t >= $dxchan->pc50_t + $DXProt::pc50_interval) {
605 $dxchan->send(pc50());
613 if ($main::systime - 3600 > $last_hour) {
614 $cutoff = $main::systime - $pc11_dup_age;
615 while (($key, $val) = each %spotdup) {
616 delete $spotdup{$key} if $val < $cutoff;
618 $cutoff = $main::systime - $pc23_dup_age;
619 while (($key, $val) = each %wwvdup) {
620 delete $wwvdup{$key} if $val < $cutoff;
622 $last_hour = $main::systime;
627 # finish up a pc context
632 my $call = $self->call;
633 my $ref = DXCluster->get_exact($call);
635 # unbusy and stop and outgoing mail
636 my $mref = DXMsg::get_busy($call);
637 $mref->stop_msg($self) if $mref;
639 # broadcast to all other nodes that all the nodes connected to via me are gone
640 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
643 foreach $node (@gonenodes) {
644 next if $node->call eq $call;
645 broadcast_ak1a(pc21($node->call, 'Gone') , $self) unless $self->{isolate};
649 # remove outstanding pings
650 delete $pings{$call};
652 # now broadcast to all other ak1a nodes that I have gone
653 broadcast_ak1a(pc21($call, 'Gone.'), $self);
655 Log('DXProt', $call . " Disconnected");
660 # some active measures
663 sub send_local_config
670 if ($self->{isolate}) {
671 @nodes = (DXCluster->get_exact($main::mycall));
673 # create a list of all the nodes that are not connected to this connection
674 @nodes = DXNode::get_all();
675 @nodes = grep { $_->dxchan != $self } @nodes;
678 my @s = $me->pc19(@nodes);
680 my $routeit = adjust_hops($self, $_);
681 $self->send($routeit) if $routeit;
684 # get all the users connected on the above nodes and send them out
685 foreach $n (@nodes) {
686 my @users = values %{$n->list};
687 my @s = pc16($n, @users);
689 my $routeit = adjust_hops($self, $_);
690 $self->send($routeit) if $routeit;
696 # route a message down an appropriate interface for a callsign
698 # is called route(to, pcline);
702 my ($call, $line) = @_;
703 my $cl = DXCluster->get_exact($call);
706 my $dxchan = $cl->{dxchan};
708 my $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
710 $dxchan->send($routeit) if $dxchan;
716 # broadcast a message to all clusters [except those mentioned after buffer]
719 my $s = shift; # the line to be rebroadcast
720 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
721 my @dxchan = get_all_ak1a();
724 # send it if it isn't the except list and isn't isolated and still has a hop count
725 foreach $dxchan (@dxchan) {
726 next if grep $dxchan == $_, @except;
727 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
728 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
732 # broadcast to all users
733 # storing the spot or whatever until it is in a state to receive it
736 my $s = shift; # the line to be rebroadcast
737 my $sort = shift; # the type of transmission
738 my $fref = shift; # a reference to an object to filter on
739 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
740 my @dxchan = get_all_users();
744 foreach $dxchan (@dxchan) {
745 next if grep $dxchan == $_, @except;
748 broadcast_list($s, $sort, $fref, @out);
751 # broadcast to a list of users
759 foreach $dxchan (@_) {
761 next if $sort eq 'dx' && !$dxchan->{dx};
762 next if $sort eq 'ann' && !$dxchan->{ann};
763 next if $sort eq 'wwv' && !$dxchan->{wwv};
764 next if $sort eq 'wx' && !$dxchan->{wx};
766 $s =~ s/\a//og unless $dxchan->{beep};
767 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'convers') {
776 # gimme all the ak1a nodes
780 my @list = DXChannel->get_all();
783 foreach $ref (@list) {
784 push @out, $ref if $ref->is_ak1a;
789 # return a list of all users
792 my @list = DXChannel->get_all();
795 foreach $ref (@list) {
796 push @out, $ref if $ref->is_user;
801 # return a list of all user callsigns
802 sub get_all_user_calls
804 my @list = DXChannel->get_all();
807 foreach $ref (@list) {
808 push @out, $ref->call if $ref->is_user;
814 # obtain the hops from the list for this callsign and pc no
820 my $hops = $DXProt::hopcount{$pcno};
821 $hops = $DXProt::def_hopcount if !$hops;
826 # adjust the hop count on a per node basis using the user loadable
827 # hop table if available or else decrement an existing one
834 my $call = $self->{call};
837 if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
838 my ($pcno) = $s =~ /^PC(\d\d)/o;
839 confess "$call called adjust_hops with '$s'" unless $pcno;
840 my $ref = $nodehops{$call} if %nodehops;
842 my $newhops = $ref->{$pcno};
843 return "" if defined $newhops && $newhops == 0;
844 $newhops = $ref->{default} unless $newhops;
845 return "" if defined $newhops && $newhops == 0;
846 $newhops = $hops if !$newhops;
847 $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
849 # simply decrement it
852 $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
864 return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
865 do "$main::data/hop_table.pl";
870 # remove leading and trailing spaces from an input string
878 # add a ping request to the ping queues
881 my ($from, $to) = @_;
882 my $ref = $pings{$to};
883 $ref = $pings{$to} = [] if !$ref;
886 $r->{t} = $main::systime;
887 route($to, pc51($to, $main::mycall, 1));
891 # add a rcmd request to the rcmd queues
894 my ($from, $to, $cmd) = @_;
897 $r->{t} = $main::systime;
899 route($to, pc34($main::mycall, $to, $cmd));