3 # This module impliments the protocal mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
27 use vars qw($me $pc11_max_age $pc11_dup_age %dup $last_hour %pings %rcmds %nodehops);
29 $me = undef; # the channel id for this cluster
30 $pc11_max_age = 1*3600; # the maximum age for an incoming 'real-time' pc11
31 $pc11_dup_age = 24*3600; # the maximum time to keep the dup list for
32 %dup = (); # the pc11 and 26 dup hash
33 $last_hour = time; # last time I did an hourly periodic update
34 %pings = (); # outstanding ping requests outbound
35 %rcmds = (); # outstanding rcmd requests outbound
36 %nodehops = (); # node specific hop control
41 my $user = DXUser->get($main::mycall);
42 $DXProt::myprot_version += $main::version*100;
43 $me = DXProt->new($main::mycall, 0, $user);
45 $me->{state} = "indifferent";
46 do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
48 # $me->{sort} = 'M'; # M for me
52 # obtain a new connection this is derived from dxchannel
57 my $self = DXChannel::alloc(@_);
58 $self->{'sort'} = 'A'; # in absence of how to find out what sort of an object I am
62 # this is how a pc connection starts (for an incoming connection)
63 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
64 # all the crap that comes between).
67 my ($self, $line, $sort) = @_;
68 my $call = $self->{call};
69 my $user = $self->{user};
71 # remember type of connection
72 $self->{consort} = $line;
73 $self->{outbound} = $sort eq 'O';
74 $self->{priv} = $user->priv;
75 $self->{lang} = $user->lang;
76 $self->{isolate} = $user->{isolate};
77 $self->{consort} = $line; # save the connection type
81 $self->send_now('B',"0");
83 # send initialisation string
84 if (!$self->{outbound}) {
85 $self->send(pc38()) if DXNode->get_all();
91 Log('DXProt', "$call connected");
95 # This is the normal pcxx despatcher
99 my ($self, $line) = @_;
100 my @field = split /[\^\~]/, $line;
102 # ignore any lines that don't start with PC
103 return if !$field[0] =~ /^PC/;
106 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
108 return if $pcno < 10 || $pcno > 51;
111 if ($pcno == 10) { # incoming talk
113 # is it for me or one of mine?
114 my $call = ($field[5] gt ' ') ? $field[5] : $field[2];
115 if ($call eq $main::mycall || grep $_ eq $call, get_all_user_calls()) {
118 my $text = unpad($field[3]);
119 Log('talk', $call, $field[1], $field[6], $text);
120 $call = $main::myalias if $call eq $main::mycall;
121 my $ref = DXChannel->get($call);
122 $ref->send("$call de $field[1]: $text") if $ref;
124 route($field[2], $line); # relay it on its way
129 if ($pcno == 11 || $pcno == 26) { # dx spot
131 # if this is a 'nodx' node then ignore it
132 last SWITCH if grep $field[7] =~ /^$_/, @DXProt::nodx_node;
134 # convert the date to a unix date
135 my $d = cltounix($field[3], $field[4]);
136 return if !$d || ($pcno == 11 && $d < $main::systime - $pc11_max_age); # bang out (and don't pass on) if date is invalid or the spot is too old
138 # strip off the leading & trailing spaces from the comment
139 my $text = unpad($field[5]);
142 my $spotter = $field[6];
143 $spotter =~ s/-\d+$//o; # strip off the ssid from the spotter
146 my $dupkey = "$field[1]$field[2]$d$text$field[6]";
147 return if $dup{$dupkey};
150 my $spot = Spot::add($field[1], $field[2], $d, $text, $spotter);
152 # send orf to the users
153 if ($spot && $pcno == 11) {
154 my $buf = Spot::formatb($field[1], $field[2], $d, $text, $spotter);
155 broadcast_users("$buf\a\a");
161 if ($pcno == 12) { # announces
163 if ($field[2] eq '*' || $field[2] eq $main::mycall) {
165 # strip leading and trailing stuff
166 my $text = unpad($field[3]);
171 if ($field[4] eq '*') { # sysops
173 @list = map { $_->priv >= 5 ? $_ : () } get_all_users();
174 } elsif ($field[4] gt ' ') { # speciality list handling
175 my ($name) = split /\./, $field[4];
176 $target = "$name"; # put the rest in later (if bothered)
179 if ($field[6] eq '1') {
183 $target = "All" if !$target;
186 broadcast_list("$to$target de $field[1]: $text", @list);
188 broadcast_users("$target de $field[1]: $text");
190 Log('ann', $target, $field[1], $text);
192 return if $field[2] eq $main::mycall; # it's routed to me
194 route($field[2], $line);
195 return; # only on a routed one
211 if ($pcno == 16) { # add a user
212 my $node = DXCluster->get_exact($field[1]);
213 last SWITCH if !$node; # ignore if havn't seen a PC19 for this one yet
217 for ($i = 2; $i < $#field; $i++) {
218 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (-) (\d)/o;
219 next if length $call < 3;
222 next if DXCluster->get_exact($call); # we already have this (loop?)
224 $confmode = $confmode eq '*';
225 DXNodeuser->new($self, $node, $call, $confmode, $here);
227 # add this station to the user database, if required
228 $call =~ s/-\d+$//o; # remove ssid for users
229 my $user = DXUser->get_current($call);
230 $user = DXUser->new($call) if !$user;
231 $user->homenode($node->call) if !$user->homenode;
232 $user->node($node->call);
233 $user->lastin($main::systime);
237 # queue up any messages (look for privates only)
238 DXMsg::queue_msg(1) if $self->state eq 'normal';
242 if ($pcno == 17) { # remove a user
244 my $ref = DXCluster->get_exact($field[1]);
249 if ($pcno == 18) { # link request
250 $self->send_local_config();
252 $self->state('init');
256 if ($pcno == 19) { # incoming cluster list
258 for ($i = 1; $i < $#field-1; $i += 4) {
259 my $here = $field[$i];
260 my $call = uc $field[$i+1];
261 my $confmode = $field[$i+2] eq '*';
262 my $ver = $field[$i+3];
264 # now check the call over
265 next if DXCluster->get_exact($call); # we already have this
267 # check for sane parameters
268 next if $ver < 5000; # only works with version 5 software
269 next if length $call < 3; # min 3 letter callsigns
270 DXNode->new($self, $call, $confmode, $here, $ver);
272 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
273 my $mref = DXMsg::get_busy($call);
274 $mref->stop_msg($self) if $mref;
276 # add this station to the user database, if required (don't remove SSID from nodes)
277 my $user = DXUser->get_current($call);
279 $user = DXUser->new($call);
281 $user->priv(1); # I have relented and defaulted nodes
282 $self->{priv} = 1; # to user RCMDs allowed
283 $user->homenode($call);
286 $user->lastin($main::systime);
290 # queue up any messages
291 DXMsg::queue_msg(0) if $self->state eq 'normal';
295 if ($pcno == 20) { # send local configuration
296 $self->send_local_config();
298 $self->state('normal');
305 if ($pcno == 21) { # delete a cluster from the list
306 my $call = uc $field[1];
307 if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
308 my $ref = DXCluster->get_exact($call);
315 $self->state('normal');
322 if ($pcno == 23 || $pcno == 27) { # WWV info
323 Geomag::update(@field[1..$#field]);
327 if ($pcno == 24) { # set here status
328 my $call = uc $field[1];
329 my $ref = DXCluster->get_exact($call);
330 $ref->here($field[2]) if $ref;
338 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
339 DXMsg::process($self, $line);
343 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
344 if ($field[1] eq $main::mycall) {
345 my $ref = DXUser->get_current($field[2]);
346 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
347 unless ($field[3] =~ /rcmd/i) { # not allowed to relay RCMDS!
348 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
349 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
350 my @in = (DXCommandmode::run_cmd($self, $field[3]));
353 $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
354 Log('rcmd', 'out', $field[2], $_);
356 delete $self->{remotecmd};
359 $self->send(pc35($main::mycall, $field[2], "$main::mycall:Tut tut tut...!"));
362 route($field[1], $line);
367 if ($pcno == 35) { # remote command replies
368 if ($field[1] eq $main::mycall) {
369 my $s = $rcmds{$field[2]};
371 my $dxchan = DXChannel->get($s->{call});
372 $dxchan->send($field[3]) if $dxchan;
373 delete $rcmds{$field[2]} if !$dxchan;
376 route($field[1], $line);
385 if ($pcno == 38) { # node connected list from neighbour
389 if ($pcno == 39) { # incoming disconnect
394 if ($pcno == 41) { # user info
395 # add this station to the user database, if required
396 my $user = DXUser->get_current($field[1]);
398 # then try without an SSID
399 $field[1] =~ s/-\d+$//o;
400 $user = DXUser->get_current($field[1]);
402 $user = DXUser->new($field[1]) if !$user;
404 if ($field[2] == 1) {
405 $user->name($field[3]);
406 } elsif ($field[2] == 2) {
407 $user->qth($field[3]);
408 } elsif ($field[2] == 3) {
409 my ($lat, $long) = DXBearing::stoll($field[3]);
412 } elsif ($field[2] == 4) {
413 $user->homenode($field[3]);
437 if ($pcno == 50) { # keep alive/user list
438 my $ref = DXCluster->get_exact($field[1]);
439 $ref->update_users($field[2]) if $ref;
443 if ($pcno == 51) { # incoming ping requests/answers
446 if ($field[1] eq $main::mycall) {
447 my $flag = $field[3];
449 $self->send(pc51($field[2], $field[1], '0'));
451 # it's a reply, look in the ping list for this one
452 my $ref = $pings{$field[2]};
455 my $dxchan = DXChannel->get($r->{call});
456 $dxchan->send($dxchan->msg('pingi', $field[2], atime($main::systime), $main::systime - $r->{t})) if $dxchan;
461 # route down an appropriate thingy
462 route($field[1], $line);
468 # if get here then rebroadcast the thing with its Hop count decremented (if
469 # there is one). If it has a hop count and it decrements to zero then don't
472 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
476 if (!$self->{isolate}) {
477 broadcast_ak1a($line, $self); # send it to everyone but me
482 # This is called from inside the main cluster processing loop and is used
483 # for despatching commands that are doing some long processing job
488 my @dxchan = DXChannel->get_all();
491 foreach $dxchan (@dxchan) {
492 next unless $dxchan->is_ak1a();
493 next if $dxchan == $me;
495 # send a pc50 out on this channel
496 if ($t >= $dxchan->pc50_t + $DXProt::pc50_interval) {
497 $dxchan->send(pc50());
505 if ($main::systime - 3600 > $last_hour) {
506 $cutoff = $main::systime - $pc11_dup_age;
507 while (($key, $val) = each %dup) {
508 delete $dup{$key} if $val < $cutoff;
510 $last_hour = $main::systime;
515 # finish up a pc context
520 my $call = $self->call;
521 my $ref = DXCluster->get_exact($call);
523 # unbusy and stop and outgoing mail
524 my $mref = DXMsg::get_busy($call);
525 $mref->stop_msg($self) if $mref;
527 # broadcast to all other nodes that all the nodes connected to via me are gone
528 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
531 foreach $node (@gonenodes) {
532 next if $node->call eq $call;
533 broadcast_ak1a(pc21($node->call, 'Gone'), $self) unless $self->{isolate}; # done like this 'cos DXNodes don't have a pc21 method
537 # remove outstanding pings
538 delete $pings{$call};
540 # now broadcast to all other ak1a nodes that I have gone
541 broadcast_ak1a(pc21($call, 'Gone.'), $self);
543 Log('DXProt', $call . " Disconnected");
548 # some active measures
551 sub send_local_config
558 if ($self->{isolate}) {
559 @nodes = (DXCluster->get_exact($main::mycall));
561 # create a list of all the nodes that are not connected to this connection
562 @nodes = DXNode::get_all();
563 @nodes = grep { $_->dxchan != $self } @nodes;
566 my @s = $me->pc19(@nodes);
568 my $routeit = adjust_hops($self, $_);
569 $self->send($_) if $routeit;
572 # get all the users connected on the above nodes and send them out
573 foreach $n (@nodes) {
574 my @users = values %{$n->list};
575 my @s = pc16($n, @users);
577 my $routeit = adjust_hops($self, $_);
578 $self->send($_) if $routeit;
584 # route a message down an appropriate interface for a callsign
586 # is called route(to, pcline);
590 my ($call, $line) = @_;
591 my $cl = DXCluster->get_exact($call);
594 my $dxchan = $cl->{dxchan};
596 my $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
598 $dxchan->send($line) if $dxchan;
604 # broadcast a message to all clusters [except those mentioned after buffer]
607 my $s = shift; # the line to be rebroadcast
608 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
609 my @dxchan = get_all_ak1a();
612 # send it if it isn't the except list and isn't isolated and still has a hop count
613 foreach $dxchan (@dxchan) {
614 next if grep $dxchan == $_, @except;
615 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
616 $dxchan->send($s) unless $dxchan->{isolate} || !$routeit;
620 # broadcast to all users
623 my $s = shift; # the line to be rebroadcast
624 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
625 my @dxchan = get_all_users();
628 foreach $dxchan (@dxchan) {
629 next if grep $dxchan == $_, @except;
630 $s =~ s/\a//og if !$dxchan->{beep};
631 $dxchan->send($s); # send it if it isn't the except list or hasn't a passout flag
635 # broadcast to a list of users
641 foreach $dxchan (@_) {
642 $dxchan->send($s); # send it
647 # gimme all the ak1a nodes
651 my @list = DXChannel->get_all();
654 foreach $ref (@list) {
655 push @out, $ref if $ref->is_ak1a;
660 # return a list of all users
663 my @list = DXChannel->get_all();
666 foreach $ref (@list) {
667 push @out, $ref if $ref->is_user;
672 # return a list of all user callsigns
673 sub get_all_user_calls
675 my @list = DXChannel->get_all();
678 foreach $ref (@list) {
679 push @out, $ref->call if $ref->is_user;
685 # obtain the hops from the list for this callsign and pc no
691 my $hops = $DXProt::hopcount{$pcno};
692 $hops = $DXProt::def_hopcount if !$hops;
697 # adjust the hop count on a per node basis using the user loadable
698 # hop table if available or else decrement an existing one
704 my $call = $self->{call};
707 if (($hops) = $_[0] =~ /\^H(\d+)\^~?$/o) {
708 my ($pcno) = $_[0] =~ /^PC(\d\d)/o;
709 confess "$call called adjust_hops with '$_[0]'" unless $pcno;
710 my $ref = $nodehops{$call} if %nodehops;
712 my $newhops = $ref->{$pcno};
713 return 0 if defined $newhops && $newhops == 0;
714 $newhops = $ref->{default} unless $newhops;
715 return 0 if defined $newhops && $newhops == 0;
716 $newhops = $hops if !$newhops;
717 $_[0] =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
719 # simply decrement it
722 $_[0] =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
734 return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
735 do "$main::data/hop_table.pl";
740 # remove leading and trailing spaces from an input string
748 # add a ping request to the ping queues
751 my ($from, $to) = @_;
752 my $ref = $pings{$to};
753 $ref = $pings{$to} = [] if !$ref;
756 $r->{t} = $main::systime;
757 route($to, pc51($to, $main::mycall, 1));
761 # add a rcmd request to the rcmd queues
764 my ($from, $to, $cmd) = @_;
767 $r->{t} = $main::systime;
769 route($to, pc34($main::mycall, $to, $cmd));