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 # bang out (and don't pass on) if date is invalid or the spot is too old
137 if (!$d || ($pcno == 11 && $d < $main::systime - $pc11_max_age)) {
138 dbg('chan', "Spot ignored, invalid date or too old");
142 # strip off the leading & trailing spaces from the comment
143 my $text = unpad($field[5]);
146 my $spotter = $field[6];
147 $spotter =~ s/-\d+$//o; # strip off the ssid from the spotter
150 my $dupkey = "$field[1]$field[2]$d$text$field[6]";
152 dbg('chan', "Duplicate Spot ignored");
158 my $spot = Spot::add($field[1], $field[2], $d, $text, $spotter);
160 # send orf to the users
161 if ($spot && $pcno == 11) {
162 my $buf = Spot::formatb($field[1], $field[2], $d, $text, $spotter);
163 broadcast_users("$buf\a\a");
169 if ($pcno == 12) { # announces
171 if ($field[2] eq '*' || $field[2] eq $main::mycall) {
173 # strip leading and trailing stuff
174 my $text = unpad($field[3]);
179 if ($field[4] eq '*') { # sysops
181 @list = map { $_->priv >= 5 ? $_ : () } get_all_users();
182 } elsif ($field[4] gt ' ') { # speciality list handling
183 my ($name) = split /\./, $field[4];
184 $target = "$name"; # put the rest in later (if bothered)
187 if ($field[6] eq '1') {
191 $target = "All" if !$target;
194 broadcast_list("$to$target de $field[1]: $text", @list);
196 broadcast_users("$target de $field[1]: $text");
198 Log('ann', $target, $field[1], $text);
200 return if $field[2] eq $main::mycall; # it's routed to me
202 route($field[2], $line);
203 return; # only on a routed one
219 if ($pcno == 16) { # add a user
220 my $node = DXCluster->get_exact($field[1]);
221 last SWITCH if !$node; # ignore if havn't seen a PC19 for this one yet
225 for ($i = 2; $i < $#field; $i++) {
226 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (-) (\d)/o;
227 next if length $call < 3;
230 next if DXCluster->get_exact($call); # we already have this (loop?)
232 $confmode = $confmode eq '*';
233 DXNodeuser->new($self, $node, $call, $confmode, $here);
235 # add this station to the user database, if required
236 $call =~ s/-\d+$//o; # remove ssid for users
237 my $user = DXUser->get_current($call);
238 $user = DXUser->new($call) if !$user;
239 $user->homenode($node->call) if !$user->homenode;
240 $user->node($node->call);
241 $user->lastin($main::systime);
245 # queue up any messages (look for privates only)
246 DXMsg::queue_msg(1) if $self->state eq 'normal';
250 if ($pcno == 17) { # remove a user
252 my $ref = DXCluster->get_exact($field[1]);
257 if ($pcno == 18) { # link request
258 $self->send_local_config();
260 $self->state('init');
261 return; # we don't pass these on
264 if ($pcno == 19) { # incoming cluster list
266 for ($i = 1; $i < $#field-1; $i += 4) {
267 my $here = $field[$i];
268 my $call = uc $field[$i+1];
269 my $confmode = $field[$i+2] eq '*';
270 my $ver = $field[$i+3];
272 # now check the call over
273 next if DXCluster->get_exact($call); # we already have this
275 # check for sane parameters
276 next if $ver < 5000; # only works with version 5 software
277 next if length $call < 3; # min 3 letter callsigns
278 DXNode->new($self, $call, $confmode, $here, $ver);
280 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
281 my $mref = DXMsg::get_busy($call);
282 $mref->stop_msg($self) if $mref;
284 # add this station to the user database, if required (don't remove SSID from nodes)
285 my $user = DXUser->get_current($call);
287 $user = DXUser->new($call);
289 $user->priv(1); # I have relented and defaulted nodes
290 $self->{priv} = 1; # to user RCMDs allowed
291 $user->homenode($call);
294 $user->lastin($main::systime);
298 # queue up any messages
299 DXMsg::queue_msg(0) if $self->state eq 'normal';
303 if ($pcno == 20) { # send local configuration
304 $self->send_local_config();
306 $self->state('normal');
313 if ($pcno == 21) { # delete a cluster from the list
314 my $call = uc $field[1];
315 if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
316 my $ref = DXCluster->get_exact($call);
323 $self->state('normal');
330 if ($pcno == 23 || $pcno == 27) { # WWV info
331 Geomag::update(@field[1..$#field]);
335 if ($pcno == 24) { # set here status
336 my $call = uc $field[1];
337 my $ref = DXCluster->get_exact($call);
338 $ref->here($field[2]) if $ref;
346 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
347 DXMsg::process($self, $line);
351 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
352 if ($field[1] eq $main::mycall) {
353 my $ref = DXUser->get_current($field[2]);
354 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
355 unless ($field[3] =~ /rcmd/i) { # not allowed to relay RCMDS!
356 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
357 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
358 my @in = (DXCommandmode::run_cmd($self, $field[3]));
361 $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
362 Log('rcmd', 'out', $field[2], $_);
364 delete $self->{remotecmd};
367 $self->send(pc35($main::mycall, $field[2], "$main::mycall:Tut tut tut...!"));
370 route($field[1], $line);
375 if ($pcno == 35) { # remote command replies
376 if ($field[1] eq $main::mycall) {
377 my $s = $rcmds{$field[2]};
379 my $dxchan = DXChannel->get($s->{call});
380 $dxchan->send($field[3]) if $dxchan;
381 delete $rcmds{$field[2]} if !$dxchan;
384 route($field[1], $line);
393 if ($pcno == 38) { # node connected list from neighbour
397 if ($pcno == 39) { # incoming disconnect
402 if ($pcno == 41) { # user info
403 # add this station to the user database, if required
404 my $user = DXUser->get_current($field[1]);
406 # then try without an SSID
407 $field[1] =~ s/-\d+$//o;
408 $user = DXUser->get_current($field[1]);
410 $user = DXUser->new($field[1]) if !$user;
412 if ($field[2] == 1) {
413 $user->name($field[3]);
414 } elsif ($field[2] == 2) {
415 $user->qth($field[3]);
416 } elsif ($field[2] == 3) {
417 my ($lat, $long) = DXBearing::stoll($field[3]);
420 } elsif ($field[2] == 4) {
421 $user->homenode($field[3]);
445 if ($pcno == 50) { # keep alive/user list
446 my $ref = DXCluster->get_exact($field[1]);
447 $ref->update_users($field[2]) if $ref;
451 if ($pcno == 51) { # incoming ping requests/answers
454 if ($field[1] eq $main::mycall) {
455 my $flag = $field[3];
457 $self->send(pc51($field[2], $field[1], '0'));
459 # it's a reply, look in the ping list for this one
460 my $ref = $pings{$field[2]};
463 my $dxchan = DXChannel->get($r->{call});
464 $dxchan->send($dxchan->msg('pingi', $field[2], atime($main::systime), $main::systime - $r->{t})) if $dxchan;
469 # route down an appropriate thingy
470 route($field[1], $line);
476 # if get here then rebroadcast the thing with its Hop count decremented (if
477 # there is one). If it has a hop count and it decrements to zero then don't
480 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
484 if (!$self->{isolate}) {
485 broadcast_ak1a($line, $self); # send it to everyone but me
490 # This is called from inside the main cluster processing loop and is used
491 # for despatching commands that are doing some long processing job
496 my @dxchan = DXChannel->get_all();
499 foreach $dxchan (@dxchan) {
500 next unless $dxchan->is_ak1a();
501 next if $dxchan == $me;
503 # send a pc50 out on this channel
504 if ($t >= $dxchan->pc50_t + $DXProt::pc50_interval) {
505 $dxchan->send(pc50());
513 if ($main::systime - 3600 > $last_hour) {
514 $cutoff = $main::systime - $pc11_dup_age;
515 while (($key, $val) = each %dup) {
516 delete $dup{$key} if $val < $cutoff;
518 $last_hour = $main::systime;
523 # finish up a pc context
528 my $call = $self->call;
529 my $ref = DXCluster->get_exact($call);
531 # unbusy and stop and outgoing mail
532 my $mref = DXMsg::get_busy($call);
533 $mref->stop_msg($self) if $mref;
535 # broadcast to all other nodes that all the nodes connected to via me are gone
536 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
539 foreach $node (@gonenodes) {
540 next if $node->call eq $call;
541 broadcast_ak1a(pc21($node->call, 'Gone') , $self) unless $self->{isolate};
545 # remove outstanding pings
546 delete $pings{$call};
548 # now broadcast to all other ak1a nodes that I have gone
549 broadcast_ak1a(pc21($call, 'Gone.'), $self);
551 Log('DXProt', $call . " Disconnected");
556 # some active measures
559 sub send_local_config
566 if ($self->{isolate}) {
567 @nodes = (DXCluster->get_exact($main::mycall));
569 # create a list of all the nodes that are not connected to this connection
570 @nodes = DXNode::get_all();
571 @nodes = grep { $_->dxchan != $self } @nodes;
574 my @s = $me->pc19(@nodes);
576 my $routeit = adjust_hops($self, $_);
577 $self->send($routeit) if $routeit;
580 # get all the users connected on the above nodes and send them out
581 foreach $n (@nodes) {
582 my @users = values %{$n->list};
583 my @s = pc16($n, @users);
585 my $routeit = adjust_hops($self, $_);
586 $self->send($routeit) if $routeit;
592 # route a message down an appropriate interface for a callsign
594 # is called route(to, pcline);
598 my ($call, $line) = @_;
599 my $cl = DXCluster->get_exact($call);
602 my $dxchan = $cl->{dxchan};
604 my $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
606 $dxchan->send($routeit) if $dxchan;
612 # broadcast a message to all clusters [except those mentioned after buffer]
615 my $s = shift; # the line to be rebroadcast
616 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
617 my @dxchan = get_all_ak1a();
620 # send it if it isn't the except list and isn't isolated and still has a hop count
621 foreach $dxchan (@dxchan) {
622 next if grep $dxchan == $_, @except;
623 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
624 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
628 # broadcast to all users
631 my $s = shift; # the line to be rebroadcast
632 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
633 my @dxchan = get_all_users();
636 foreach $dxchan (@dxchan) {
637 next if grep $dxchan == $_, @except;
638 $s =~ s/\a//og if !$dxchan->{beep};
639 $dxchan->send($s); # send it if it isn't the except list or hasn't a passout flag
643 # broadcast to a list of users
649 foreach $dxchan (@_) {
650 $dxchan->send($s); # send it
655 # gimme all the ak1a nodes
659 my @list = DXChannel->get_all();
662 foreach $ref (@list) {
663 push @out, $ref if $ref->is_ak1a;
668 # return a list of all users
671 my @list = DXChannel->get_all();
674 foreach $ref (@list) {
675 push @out, $ref if $ref->is_user;
680 # return a list of all user callsigns
681 sub get_all_user_calls
683 my @list = DXChannel->get_all();
686 foreach $ref (@list) {
687 push @out, $ref->call if $ref->is_user;
693 # obtain the hops from the list for this callsign and pc no
699 my $hops = $DXProt::hopcount{$pcno};
700 $hops = $DXProt::def_hopcount if !$hops;
705 # adjust the hop count on a per node basis using the user loadable
706 # hop table if available or else decrement an existing one
713 my $call = $self->{call};
716 if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
717 my ($pcno) = $s =~ /^PC(\d\d)/o;
718 confess "$call called adjust_hops with '$s'" unless $pcno;
719 my $ref = $nodehops{$call} if %nodehops;
721 my $newhops = $ref->{$pcno};
722 return "" if defined $newhops && $newhops == 0;
723 $newhops = $ref->{default} unless $newhops;
724 return "" if defined $newhops && $newhops == 0;
725 $newhops = $hops if !$newhops;
726 $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
728 # simply decrement it
731 $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
743 return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
744 do "$main::data/hop_table.pl";
749 # remove leading and trailing spaces from an input string
757 # add a ping request to the ping queues
760 my ($from, $to) = @_;
761 my $ref = $pings{$to};
762 $ref = $pings{$to} = [] if !$ref;
765 $r->{t} = $main::systime;
766 route($to, pc51($to, $main::mycall, 1));
770 # add a rcmd request to the rcmd queues
773 my ($from, $to, $cmd) = @_;
776 $r->{t} = $main::systime;
778 route($to, pc34($main::mycall, $to, $cmd));