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);
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
39 my $user = DXUser->get($main::mycall);
40 $me = DXProt->new($main::mycall, undef, $user);
42 # $me->{sort} = 'M'; # M for me
46 # obtain a new connection this is derived from dxchannel
51 my $self = DXChannel::alloc(@_);
52 $self->{sort} = 'A'; # in absence of how to find out what sort of an object I am
56 # this is how a pc connection starts (for an incoming connection)
57 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
58 # all the crap that comes between).
61 my ($self, $line, $sort) = @_;
62 my $call = $self->{call};
63 my $user = $self->{user};
65 # remember type of connection
66 $self->{consort} = $line;
67 $self->{outbound} = $sort eq 'O';
68 $self->{priv} = $user->priv;
69 $self->{lang} = $user->lang;
70 $self->{consort} = $line; # save the connection type
74 $self->send_now('B',"0");
76 # send initialisation string
77 if (!$self->{outbound}) {
78 $self->send(pc38()) if DXNode->get_all();
84 Log('DXProt', "$call connected");
88 # This is the normal pcxx despatcher
92 my ($self, $line) = @_;
93 my @field = split /[\^\~]/, $line;
95 # ignore any lines that don't start with PC
96 return if !$field[0] =~ /^PC/;
99 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
100 return if $pcno < 10 || $pcno > 51;
103 if ($pcno == 10) { # incoming talk
105 # is it for me or one of mine?
106 my $call = ($field[5] gt ' ') ? $field[5] : $field[2];
107 if ($call eq $main::mycall || grep $_ eq $call, get_all_user_calls()) {
110 my $text = unpad($field[3]);
111 Log('talk', $call, $field[1], $field[6], $text);
112 $call = $main::myalias if $call eq $main::mycall;
113 my $ref = DXChannel->get($call);
114 $ref->send("$call de $field[1]: $text") if $ref;
116 route($field[2], $line); # relay it on its way
121 if ($pcno == 11 || $pcno == 26) { # dx spot
123 # if this is a 'nodx' node then ignore it
124 last SWITCH if grep $field[7] =~ /^$_/, @DXProt::nodx_node;
126 # convert the date to a unix date
127 my $d = cltounix($field[3], $field[4]);
128 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
130 # strip off the leading & trailing spaces from the comment
131 my $text = unpad($field[5]);
134 my $spotter = $field[6];
135 $spotter =~ s/-\d+$//o; # strip off the ssid from the spotter
138 my $dupkey = "$field[1]$field[2]$d$text$field[6]";
139 return if $dup{$dupkey};
142 my $spot = Spot::add($field[1], $field[2], $d, $text, $spotter);
144 # send orf to the users
145 if ($spot && $pcno == 11) {
146 my $buf = Spot::formatb($field[1], $field[2], $d, $text, $spotter);
147 broadcast_users("$buf\a\a");
153 if ($pcno == 12) { # announces
155 if ($field[2] eq '*' || $field[2] eq $main::mycall) {
157 # strip leading and trailing stuff
158 my $text = unpad($field[3]);
163 if ($field[4] eq '*') { # sysops
165 @list = map { $_->priv >= 5 ? $_ : () } get_all_users();
166 } elsif ($field[4] gt ' ') { # speciality list handling
167 my ($name) = split /\./, $field[4];
168 $target = "$name"; # put the rest in later (if bothered)
171 if ($field[6] eq '1') {
175 $target = "All" if !$target;
178 broadcast_list("$to$target de $field[1]: $text", @list);
180 broadcast_users("$target de $field[1]: $text");
182 Log('ann', $target, $field[1], $text);
184 return if $field[2] eq $main::mycall; # it's routed to me
186 route($field[2], $line);
187 return; # only on a routed one
203 if ($pcno == 16) { # add a user
204 my $node = DXCluster->get_exact($field[1]);
205 last SWITCH if !$node; # ignore if havn't seen a PC19 for this one yet
209 for ($i = 2; $i < $#field; $i++) {
210 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (-) (\d)/o;
211 next if length $call < 3;
214 next if DXCluster->get_exact($call); # we already have this (loop?)
216 $confmode = $confmode eq '*';
217 DXNodeuser->new($self, $node, $call, $confmode, $here);
219 # add this station to the user database, if required
220 $call =~ s/-\d+$//o; # remove ssid for users
221 my $user = DXUser->get_current($call);
222 $user = DXUser->new($call) if !$user;
223 $user->homenode($node->call) if !$user->homenode;
224 $user->node($node->call);
225 $user->lastin($main::systime);
229 # queue up any messages (look for privates only)
230 DXMsg::queue_msg(1) if $self->state eq 'normal';
234 if ($pcno == 17) { # remove a user
236 my $ref = DXCluster->get_exact($field[1]);
241 if ($pcno == 18) { # link request
242 $self->send_local_config();
244 $self->state('init');
248 if ($pcno == 19) { # incoming cluster list
250 for ($i = 1; $i < $#field-1; $i += 4) {
251 my $here = $field[$i];
252 my $call = uc $field[$i+1];
253 my $confmode = $field[$i+2] eq '*';
254 my $ver = $field[$i+3];
256 # now check the call over
257 next if DXCluster->get_exact($call); # we already have this
259 # check for sane parameters
260 next if $ver < 5000; # only works with version 5 software
261 next if length $call < 3; # min 3 letter callsigns
262 DXNode->new($self, $call, $confmode, $here, $ver);
264 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
265 my $mref = DXMsg::get_busy($call);
266 $mref->stop_msg($self) if $mref;
268 # add this station to the user database, if required (don't remove SSID from nodes)
269 my $user = DXUser->get_current($call);
271 $user = DXUser->new($call);
273 $user->priv(1); # I have relented and defaulted nodes
274 $self->{priv} = 1; # to user RCMDs allowed
275 $user->homenode($call);
278 $user->lastin($main::systime);
282 # queue up any messages
283 DXMsg::queue_msg() if $self->state eq 'normal';
287 if ($pcno == 20) { # send local configuration
288 $self->send_local_config();
290 $self->state('normal');
297 if ($pcno == 21) { # delete a cluster from the list
298 my $call = uc $field[1];
299 if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
300 my $ref = DXCluster->get_exact($call);
307 $self->state('normal');
314 if ($pcno == 23 || $pcno == 27) { # WWV info
315 Geomag::update(@field[1..$#field]);
319 if ($pcno == 24) { # set here status
320 my $call = uc $field[1];
321 my $ref = DXCluster->get_exact($call);
322 $ref->here($field[2]) if $ref;
330 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
331 DXMsg::process($self, $line);
335 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
336 if ($field[1] eq $main::mycall) {
337 my $ref = DXUser->get_current($field[2]);
338 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
339 unless ($field[3] =~ /rcmd/i) { # not allowed to relay RCMDS!
340 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
341 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
342 my @in = (DXCommandmode::run_cmd($self, $field[3]));
345 $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
346 Log('rcmd', 'out', $field[2], $_);
348 delete $self->{remotecmd};
351 $self->send(pc35($main::mycall, $field[2], "$main::mycall:Tut tut tut...!"));
354 route($field[1], $line);
359 if ($pcno == 35) { # remote command replies
360 if ($field[1] eq $main::mycall) {
361 my $s = $rcmds{$field[2]};
363 my $dxchan = DXChannel->get($s->{call});
364 $dxchan->send($field[3]) if $dxchan;
365 delete $rcmds{$field[2]} if !$dxchan;
368 route($field[1], $line);
377 if ($pcno == 38) { # node connected list from neighbour
381 if ($pcno == 39) { # incoming disconnect
386 if ($pcno == 41) { # user info
387 # add this station to the user database, if required
388 my $user = DXUser->get_current($field[1]);
390 # then try without an SSID
391 $field[1] =~ s/-\d+$//o;
392 $user = DXUser->get_current($field[1]);
394 $user = DXUser->new($field[1]) if !$user;
396 if ($field[2] == 1) {
397 $user->name($field[3]);
398 } elsif ($field[2] == 2) {
399 $user->qth($field[3]);
400 } elsif ($field[2] == 3) {
401 my ($lat, $long) = DXBearing::stoll($field[3]);
404 } elsif ($field[2] == 4) {
405 $user->homenode($field[3]);
429 if ($pcno == 50) { # keep alive/user list
430 my $ref = DXCluster->get_exact($field[1]);
431 $ref->update_users($field[2]) if $ref;
435 if ($pcno == 51) { # incoming ping requests/answers
438 if ($field[1] eq $main::mycall) {
439 my $flag = $field[3];
441 $self->send(pc51($field[2], $field[1], '0'));
443 # it's a reply, look in the ping list for this one
444 my $ref = $pings{$field[2]};
447 my $dxchan = DXChannel->get($r->{call});
448 $dxchan->send($dxchan->msg('pingi', $field[2], atime($main::systime), $main::systime - $r->{t})) if $dxchan;
453 # route down an appropriate thingy
454 route($field[1], $line);
460 # if get here then rebroadcast the thing with its Hop count decremented (if
461 # there is one). If it has a hop count and it decrements to zero then don't
464 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
469 if (($hops) = $line =~ /H(\d+)\^\~?$/o) {
470 my $newhops = $hops - 1;
472 $line =~ s/\^H$hops(\^\~?)$/\^H$newhops$1/; # change the hop count
473 broadcast_ak1a($line, $self); # send it to everyone but me
479 # This is called from inside the main cluster processing loop and is used
480 # for despatching commands that are doing some long processing job
485 my @chan = DXChannel->get_all();
488 foreach $chan (@chan) {
489 next if !$chan->is_ak1a();
491 # send a pc50 out on this channel
492 if ($t >= $chan->pc50_t + $DXProt::pc50_interval) {
501 if ($main::systime - 3600 > $last_hour) {
502 $cutoff = $main::systime - $pc11_dup_age;
503 while (($key, $val) = each %dup) {
504 delete $dup{$key} if $val < $cutoff;
506 $last_hour = $main::systime;
511 # finish up a pc context
516 my $call = $self->call;
517 my $ref = DXCluster->get_exact($call);
519 # unbusy and stop and outgoing mail
520 my $mref = DXMsg::get_busy($call);
521 $mref->stop_msg($self) if $mref;
523 # broadcast to all other nodes that all the nodes connected to via me are gone
524 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
527 foreach $node (@gonenodes) {
528 next if $node->call eq $call;
529 broadcast_ak1a(pc21($node->call, 'Gone'), $self); # done like this 'cos DXNodes don't have a pc21 method
533 # remove outstanding pings
534 delete $pings{$call};
536 # now broadcast to all other ak1a nodes that I have gone
537 broadcast_ak1a(pc21($call, 'Gone.'), $self);
539 Log('DXProt', $call . " Disconnected");
544 # some active measures
547 sub send_local_config
553 my @nodes = DXNode::get_all();
555 # create a list of all the nodes that are not connected to this connection
556 @nodes = grep { $_->dxchan != $self } @nodes;
557 $self->send($me->pc19(@nodes));
559 # get all the users connected on the above nodes and send them out
560 foreach $n (@nodes) {
561 my @users = values %{$n->list};
562 $self->send(DXProt::pc16($n, @users));
567 # route a message down an appropriate interface for a callsign
569 # is called route(to, pcline);
573 my ($call, $line) = @_;
574 my $cl = DXCluster->get_exact($call);
577 my $dxchan = $cl->{dxchan};
578 if (($hops) = $line =~ /H(\d+)\^\~?$/o) {
579 my $newhops = $hops - 1;
581 $line =~ s/\^H$hops(\^\~?)$/\^H$newhops$1/; # change the hop count
582 $dxchan->send($line) if $dxchan;
585 $dxchan->send($line) if $dxchan; # for them wot don't have Hops
590 # broadcast a message to all clusters [except those mentioned after buffer]
593 my $s = shift; # the line to be rebroadcast
594 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
595 my @chan = get_all_ak1a();
598 foreach $chan (@chan) {
599 next if grep $chan == $_, @except;
600 $chan->send($s); # send it if it isn't the except list
604 # broadcast to all users
607 my $s = shift; # the line to be rebroadcast
608 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
609 my @chan = get_all_users();
612 foreach $chan (@chan) {
613 next if grep $chan == $_, @except;
614 $s =~ s/\a//og if !$chan->{beep};
615 $chan->send($s); # send it if it isn't the except list
619 # broadcast to a list of users
626 $chan->send($s); # send it
631 # gimme all the ak1a nodes
635 my @list = DXChannel->get_all();
638 foreach $ref (@list) {
639 push @out, $ref if $ref->is_ak1a;
644 # return a list of all users
647 my @list = DXChannel->get_all();
650 foreach $ref (@list) {
651 push @out, $ref if $ref->is_user;
656 # return a list of all user callsigns
657 sub get_all_user_calls
659 my @list = DXChannel->get_all();
662 foreach $ref (@list) {
663 push @out, $ref->call if $ref->is_user;
669 # obtain the hops from the list for this callsign and pc no
675 my $hops = $DXProt::hopcount{$pcno};
676 $hops = $DXProt::def_hopcount if !$hops;
680 # remove leading and trailing spaces from an input string
688 # add a ping request to the ping queues
691 my ($from, $to) = @_;
692 my $ref = $pings{$to};
693 $ref = $pings{$to} = [] if !$ref;
696 $r->{t} = $main::systime;
697 route($to, pc51($to, $main::mycall, 1));
701 # add a rcmd request to the rcmd queues
704 my ($from, $to, $cmd) = @_;
707 $r->{t} = $main::systime;
709 route($to, pc34($main::mycall, $to, $cmd));