3 # This module impliments the protocal mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
28 use vars qw($me $pc11_max_age $pc11_dup_age $pc23_dup_age %spotdup %wwvdup $last_hour %pings %rcmds %nodehops);
30 $me = undef; # the channel id for this cluster
31 $pc11_max_age = 1*3600; # the maximum age for an incoming 'real-time' pc11
32 $pc11_dup_age = 24*3600; # the maximum time to keep the spot dup list for
33 $pc23_dup_age = 24*3600; # the maximum time to keep the wwv dup list for
34 %spotdup = (); # the pc11 and 26 dup hash
35 %wwvdup = (); # the pc23 and 27 dup hash
36 $last_hour = time; # last time I did an hourly periodic update
37 %pings = (); # outstanding ping requests outbound
38 %rcmds = (); # outstanding rcmd requests outbound
39 %nodehops = (); # node specific hop control
44 my $user = DXUser->get($main::mycall);
45 $DXProt::myprot_version += $main::version*100;
46 $me = DXProt->new($main::mycall, 0, $user);
48 $me->{state} = "indifferent";
49 do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
51 # $me->{sort} = 'M'; # M for me
53 # now prime the spot duplicates file with today's and yesterday's data
54 my @today = Julian::unixtoj(time);
55 my @spots = Spot::readfile(@today);
56 @today = Julian::sub(@today, 1);
57 push @spots, Spot::readfile(@today);
59 my $dupkey = "$_->[0]$_->[1]$_->[2]$_->[3]$_->[4]";
60 $spotdup{$dupkey} = $_->[2];
63 # now prime the wwv duplicates file with just this month's data
64 my @wwv = Geomag::readfile(time);
66 my $dupkey = "$_->[1].$_->[2]$_->[3]$_->[4]";
67 $wwvdup{$dupkey} = $_->[1];
73 # obtain a new connection this is derived from dxchannel
78 my $self = DXChannel::alloc(@_);
79 $self->{'sort'} = 'A'; # in absence of how to find out what sort of an object I am
83 # this is how a pc connection starts (for an incoming connection)
84 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
85 # all the crap that comes between).
88 my ($self, $line, $sort) = @_;
89 my $call = $self->{call};
90 my $user = $self->{user};
92 # remember type of connection
93 $self->{consort} = $line;
94 $self->{outbound} = $sort eq 'O';
95 $self->{priv} = $user->priv;
96 $self->{lang} = $user->lang;
97 $self->{isolate} = $user->{isolate};
98 $self->{consort} = $line; # save the connection type
102 $self->send_now('B',"0");
104 # send initialisation string
105 if (!$self->{outbound}) {
106 $self->send(pc38()) if DXNode->get_all();
109 $self->state('init');
112 Log('DXProt', "$call connected");
116 # This is the normal pcxx despatcher
120 my ($self, $line) = @_;
121 my @field = split /[\^\~]/, $line;
123 # ignore any lines that don't start with PC
124 return if !$field[0] =~ /^PC/;
127 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
129 return if $pcno < 10 || $pcno > 51;
132 if ($pcno == 10) { # incoming talk
134 # is it for me or one of mine?
135 my $call = ($field[5] gt ' ') ? $field[5] : $field[2];
136 if ($call eq $main::mycall || grep $_ eq $call, get_all_user_calls()) {
139 my $text = unpad($field[3]);
140 Log('talk', $call, $field[1], $field[6], $text);
141 $call = $main::myalias if $call eq $main::mycall;
142 my $ref = DXChannel->get($call);
143 $ref->send("$call de $field[1]: $text") if $ref;
145 route($field[2], $line); # relay it on its way
150 if ($pcno == 11 || $pcno == 26) { # dx spot
152 # if this is a 'nodx' node then ignore it
153 last SWITCH if grep $field[7] =~ /^$_/, @DXProt::nodx_node;
155 # convert the date to a unix date
156 my $d = cltounix($field[3], $field[4]);
157 # bang out (and don't pass on) if date is invalid or the spot is too old
158 if (!$d || ($pcno == 11 && $d < $main::systime - $pc11_max_age)) {
159 dbg('chan', "Spot ignored, invalid date or too old\n");
163 # strip off the leading & trailing spaces from the comment
164 my $text = unpad($field[5]);
167 my $spotter = $field[6];
168 $spotter =~ s/-\d+$//o; # strip off the ssid from the spotter
171 my $freq = $field[1] - 0;
172 my $dupkey = "$freq$field[2]$d$text$spotter";
173 if ($spotdup{$dupkey}) {
174 dbg('chan', "Duplicate Spot ignored\n");
178 $spotdup{$dupkey} = $d;
180 my $spot = Spot::add($freq, $field[2], $d, $text, $spotter);
182 # send orf to the users
183 if ($spot && $pcno == 11) {
184 my $buf = Spot::formatb($field[1], $field[2], $d, $text, $spotter);
185 broadcast_users("$buf\a\a");
188 # DON'T be silly and send on PC26s!
189 return if $pcno == 26;
194 if ($pcno == 12) { # announces
196 if ($field[2] eq '*' || $field[2] eq $main::mycall) {
198 # strip leading and trailing stuff
199 my $text = unpad($field[3]);
204 if ($field[4] eq '*') { # sysops
206 @list = map { $_->priv >= 5 ? $_ : () } get_all_users();
207 } elsif ($field[4] gt ' ') { # speciality list handling
208 my ($name) = split /\./, $field[4];
209 $target = "$name"; # put the rest in later (if bothered)
212 if ($field[6] eq '1') {
216 $target = "All" if !$target;
219 broadcast_list("$to$target de $field[1]: $text", @list);
221 broadcast_users("$target de $field[1]: $text");
223 Log('ann', $target, $field[1], $text);
225 return if $field[2] eq $main::mycall; # it's routed to me
227 route($field[2], $line);
228 return; # only on a routed one
244 if ($pcno == 16) { # add a user
245 my $node = DXCluster->get_exact($field[1]);
246 last SWITCH if !$node; # ignore if havn't seen a PC19 for this one yet
247 last SWITCH unless $node->isa('DXNode');
251 for ($i = 2; $i < $#field; $i++) {
252 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (\S) (\d)/o;
253 next if length $call < 3 || length $call > 8;
256 next if DXCluster->get_exact($call); # we already have this (loop?)
258 $confmode = $confmode eq '*';
259 DXNodeuser->new($self, $node, $call, $confmode, $here);
261 # add this station to the user database, if required
262 $call =~ s/-\d+$//o; # remove ssid for users
263 my $user = DXUser->get_current($call);
264 $user = DXUser->new($call) if !$user;
265 $user->homenode($node->call) if !$user->homenode;
266 $user->node($node->call);
267 $user->lastin($main::systime);
271 # queue up any messages (look for privates only)
272 DXMsg::queue_msg(1) if $self->state eq 'normal';
276 if ($pcno == 17) { # remove a user
278 my $ref = DXCluster->get_exact($field[1]);
283 if ($pcno == 18) { # link request
284 $self->send_local_config();
286 $self->state('init');
287 return; # we don't pass these on
290 if ($pcno == 19) { # incoming cluster list
292 for ($i = 1; $i < $#field-1; $i += 4) {
293 my $here = $field[$i];
294 my $call = uc $field[$i+1];
295 my $confmode = $field[$i+2] eq '*';
296 my $ver = $field[$i+3];
298 # now check the call over
299 next if DXCluster->get_exact($call); # we already have this
301 # check for sane parameters
302 next if $ver < 5000; # only works with version 5 software
303 next if length $call < 3; # min 3 letter callsigns
304 DXNode->new($self, $call, $confmode, $here, $ver);
306 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
307 my $mref = DXMsg::get_busy($call);
308 $mref->stop_msg($self) if $mref;
310 # add this station to the user database, if required (don't remove SSID from nodes)
311 my $user = DXUser->get_current($call);
313 $user = DXUser->new($call);
315 $user->priv(1); # I have relented and defaulted nodes
316 $self->{priv} = 1; # to user RCMDs allowed
317 $user->homenode($call);
320 $user->lastin($main::systime);
324 # queue up any messages
325 DXMsg::queue_msg(0) if $self->state eq 'normal';
329 if ($pcno == 20) { # send local configuration
330 $self->send_local_config();
332 $self->state('normal');
339 if ($pcno == 21) { # delete a cluster from the list
340 my $call = uc $field[1];
341 if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
342 my $ref = DXCluster->get_exact($call);
349 $self->state('normal');
356 if ($pcno == 23 || $pcno == 27) { # WWV info
358 my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
359 my $sfi = unpad($field[3]);
360 my $k = unpad($field[4]);
361 my $i = unpad($field[5]);
362 my $dupkey = "$d.$sfi$k$i";
363 if ($wwvdup{$dupkey}) {
364 dbg('chan', "Dup WWV Spot ignored\n");
368 $wwvdup{$dupkey} = $d;
369 Geomag::update($field[1], $field[2], $sfi, $k, $i, @field[6..$#field]);
371 # DON'T be silly and send on PC27s!
372 return if $pcno == 27;
377 if ($pcno == 24) { # set here status
378 my $call = uc $field[1];
379 my $ref = DXCluster->get_exact($call);
380 $ref->here($field[2]) if $ref;
388 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
389 DXMsg::process($self, $line);
393 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
394 if ($field[1] eq $main::mycall) {
395 my $ref = DXUser->get_current($field[2]);
396 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
397 unless ($field[3] =~ /rcmd/i) { # not allowed to relay RCMDS!
398 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
399 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
400 my @in = (DXCommandmode::run_cmd($self, $field[3]));
403 $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
404 Log('rcmd', 'out', $field[2], $_);
406 delete $self->{remotecmd};
409 $self->send(pc35($main::mycall, $field[2], "$main::mycall:Tut tut tut...!"));
412 route($field[1], $line);
417 if ($pcno == 35) { # remote command replies
418 if ($field[1] eq $main::mycall) {
419 my $s = $rcmds{$field[2]};
421 my $dxchan = DXChannel->get($s->{call});
422 $dxchan->send($field[3]) if $dxchan;
423 delete $rcmds{$field[2]} if !$dxchan;
426 route($field[1], $line);
435 if ($pcno == 38) { # node connected list from neighbour
439 if ($pcno == 39) { # incoming disconnect
444 if ($pcno == 41) { # user info
445 # add this station to the user database, if required
446 my $user = DXUser->get_current($field[1]);
448 # then try without an SSID
449 $field[1] =~ s/-\d+$//o;
450 $user = DXUser->get_current($field[1]);
452 $user = DXUser->new($field[1]) if !$user;
454 if ($field[2] == 1) {
455 $user->name($field[3]);
456 } elsif ($field[2] == 2) {
457 $user->qth($field[3]);
458 } elsif ($field[2] == 3) {
459 my ($lat, $long) = DXBearing::stoll($field[3]);
462 } elsif ($field[2] == 4) {
463 $user->homenode($field[3]);
487 if ($pcno == 50) { # keep alive/user list
488 my $ref = DXCluster->get_exact($field[1]);
489 $ref->update_users($field[2]) if $ref;
493 if ($pcno == 51) { # incoming ping requests/answers
496 if ($field[1] eq $main::mycall) {
497 my $flag = $field[3];
499 $self->send(pc51($field[2], $field[1], '0'));
501 # it's a reply, look in the ping list for this one
502 my $ref = $pings{$field[2]};
505 my $dxchan = DXChannel->get($r->{call});
506 $dxchan->send($dxchan->msg('pingi', $field[2], atime($main::systime), $main::systime - $r->{t})) if $dxchan;
511 # route down an appropriate thingy
512 route($field[1], $line);
518 # if get here then rebroadcast the thing with its Hop count decremented (if
519 # there is one). If it has a hop count and it decrements to zero then don't
522 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
526 if (!$self->{isolate}) {
527 broadcast_ak1a($line, $self); # send it to everyone but me
532 # This is called from inside the main cluster processing loop and is used
533 # for despatching commands that are doing some long processing job
538 my @dxchan = DXChannel->get_all();
541 foreach $dxchan (@dxchan) {
542 next unless $dxchan->is_ak1a();
543 next if $dxchan == $me;
545 # send a pc50 out on this channel
546 if ($t >= $dxchan->pc50_t + $DXProt::pc50_interval) {
547 $dxchan->send(pc50());
555 if ($main::systime - 3600 > $last_hour) {
556 $cutoff = $main::systime - $pc11_dup_age;
557 while (($key, $val) = each %spotdup) {
558 delete $spotdup{$key} if $val < $cutoff;
560 $cutoff = $main::systime - $pc23_dup_age;
561 while (($key, $val) = each %wwvdup) {
562 delete $wwvdup{$key} if $val < $cutoff;
564 $last_hour = $main::systime;
569 # finish up a pc context
574 my $call = $self->call;
575 my $ref = DXCluster->get_exact($call);
577 # unbusy and stop and outgoing mail
578 my $mref = DXMsg::get_busy($call);
579 $mref->stop_msg($self) if $mref;
581 # broadcast to all other nodes that all the nodes connected to via me are gone
582 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
585 foreach $node (@gonenodes) {
586 next if $node->call eq $call;
587 broadcast_ak1a(pc21($node->call, 'Gone') , $self) unless $self->{isolate};
591 # remove outstanding pings
592 delete $pings{$call};
594 # now broadcast to all other ak1a nodes that I have gone
595 broadcast_ak1a(pc21($call, 'Gone.'), $self);
597 Log('DXProt', $call . " Disconnected");
602 # some active measures
605 sub send_local_config
612 if ($self->{isolate}) {
613 @nodes = (DXCluster->get_exact($main::mycall));
615 # create a list of all the nodes that are not connected to this connection
616 @nodes = DXNode::get_all();
617 @nodes = grep { $_->dxchan != $self } @nodes;
620 my @s = $me->pc19(@nodes);
622 my $routeit = adjust_hops($self, $_);
623 $self->send($routeit) if $routeit;
626 # get all the users connected on the above nodes and send them out
627 foreach $n (@nodes) {
628 my @users = values %{$n->list};
629 my @s = pc16($n, @users);
631 my $routeit = adjust_hops($self, $_);
632 $self->send($routeit) if $routeit;
638 # route a message down an appropriate interface for a callsign
640 # is called route(to, pcline);
644 my ($call, $line) = @_;
645 my $cl = DXCluster->get_exact($call);
648 my $dxchan = $cl->{dxchan};
650 my $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
652 $dxchan->send($routeit) if $dxchan;
658 # broadcast a message to all clusters [except those mentioned after buffer]
661 my $s = shift; # the line to be rebroadcast
662 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
663 my @dxchan = get_all_ak1a();
666 # send it if it isn't the except list and isn't isolated and still has a hop count
667 foreach $dxchan (@dxchan) {
668 next if grep $dxchan == $_, @except;
669 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
670 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
674 # broadcast to all users
677 my $s = shift; # the line to be rebroadcast
678 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
679 my @dxchan = get_all_users();
682 foreach $dxchan (@dxchan) {
683 next if grep $dxchan == $_, @except;
684 $s =~ s/\a//og if !$dxchan->{beep};
685 $dxchan->send($s); # send it if it isn't the except list or hasn't a passout flag
689 # broadcast to a list of users
695 foreach $dxchan (@_) {
696 $dxchan->send($s); # send it
701 # gimme all the ak1a nodes
705 my @list = DXChannel->get_all();
708 foreach $ref (@list) {
709 push @out, $ref if $ref->is_ak1a;
714 # return a list of all users
717 my @list = DXChannel->get_all();
720 foreach $ref (@list) {
721 push @out, $ref if $ref->is_user;
726 # return a list of all user callsigns
727 sub get_all_user_calls
729 my @list = DXChannel->get_all();
732 foreach $ref (@list) {
733 push @out, $ref->call if $ref->is_user;
739 # obtain the hops from the list for this callsign and pc no
745 my $hops = $DXProt::hopcount{$pcno};
746 $hops = $DXProt::def_hopcount if !$hops;
751 # adjust the hop count on a per node basis using the user loadable
752 # hop table if available or else decrement an existing one
759 my $call = $self->{call};
762 if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
763 my ($pcno) = $s =~ /^PC(\d\d)/o;
764 confess "$call called adjust_hops with '$s'" unless $pcno;
765 my $ref = $nodehops{$call} if %nodehops;
767 my $newhops = $ref->{$pcno};
768 return "" if defined $newhops && $newhops == 0;
769 $newhops = $ref->{default} unless $newhops;
770 return "" if defined $newhops && $newhops == 0;
771 $newhops = $hops if !$newhops;
772 $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
774 # simply decrement it
777 $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
789 return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
790 do "$main::data/hop_table.pl";
795 # remove leading and trailing spaces from an input string
803 # add a ping request to the ping queues
806 my ($from, $to) = @_;
807 my $ref = $pings{$to};
808 $ref = $pings{$to} = [] if !$ref;
811 $r->{t} = $main::systime;
812 route($to, pc51($to, $main::mycall, 1));
816 # add a rcmd request to the rcmd queues
819 my ($from, $to, $cmd) = @_;
822 $r->{t} = $main::systime;
824 route($to, pc34($main::mycall, $to, $cmd));