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 $DXProt::myprot_version += $main::version*100;
41 $me = DXProt->new($main::mycall, undef, $user);
43 # $me->{sort} = 'M'; # M for me
47 # obtain a new connection this is derived from dxchannel
52 my $self = DXChannel::alloc(@_);
53 $self->{sort} = 'A'; # in absence of how to find out what sort of an object I am
57 # this is how a pc connection starts (for an incoming connection)
58 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
59 # all the crap that comes between).
62 my ($self, $line, $sort) = @_;
63 my $call = $self->{call};
64 my $user = $self->{user};
66 # remember type of connection
67 $self->{consort} = $line;
68 $self->{outbound} = $sort eq 'O';
69 $self->{priv} = $user->priv;
70 $self->{lang} = $user->lang;
71 $self->{isolate} = $user->{isolate};
72 $self->{consort} = $line; # save the connection type
76 $self->send_now('B',"0");
78 # send initialisation string
79 if (!$self->{outbound}) {
80 $self->send(pc38()) if DXNode->get_all();
86 Log('DXProt', "$call connected");
90 # This is the normal pcxx despatcher
94 my ($self, $line) = @_;
95 my @field = split /[\^\~]/, $line;
97 # ignore any lines that don't start with PC
98 return if !$field[0] =~ /^PC/;
101 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
102 return if $pcno < 10 || $pcno > 51;
105 if ($pcno == 10) { # incoming talk
107 # is it for me or one of mine?
108 my $call = ($field[5] gt ' ') ? $field[5] : $field[2];
109 if ($call eq $main::mycall || grep $_ eq $call, get_all_user_calls()) {
112 my $text = unpad($field[3]);
113 Log('talk', $call, $field[1], $field[6], $text);
114 $call = $main::myalias if $call eq $main::mycall;
115 my $ref = DXChannel->get($call);
116 $ref->send("$call de $field[1]: $text") if $ref;
118 route($field[2], $line); # relay it on its way
123 if ($pcno == 11 || $pcno == 26) { # dx spot
125 # if this is a 'nodx' node then ignore it
126 last SWITCH if grep $field[7] =~ /^$_/, @DXProt::nodx_node;
128 # convert the date to a unix date
129 my $d = cltounix($field[3], $field[4]);
130 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
132 # strip off the leading & trailing spaces from the comment
133 my $text = unpad($field[5]);
136 my $spotter = $field[6];
137 $spotter =~ s/-\d+$//o; # strip off the ssid from the spotter
140 my $dupkey = "$field[1]$field[2]$d$text$field[6]";
141 return if $dup{$dupkey};
144 my $spot = Spot::add($field[1], $field[2], $d, $text, $spotter);
146 # send orf to the users
147 if ($spot && $pcno == 11) {
148 my $buf = Spot::formatb($field[1], $field[2], $d, $text, $spotter);
149 broadcast_users("$buf\a\a");
155 if ($pcno == 12) { # announces
157 if ($field[2] eq '*' || $field[2] eq $main::mycall) {
159 # strip leading and trailing stuff
160 my $text = unpad($field[3]);
165 if ($field[4] eq '*') { # sysops
167 @list = map { $_->priv >= 5 ? $_ : () } get_all_users();
168 } elsif ($field[4] gt ' ') { # speciality list handling
169 my ($name) = split /\./, $field[4];
170 $target = "$name"; # put the rest in later (if bothered)
173 if ($field[6] eq '1') {
177 $target = "All" if !$target;
180 broadcast_list("$to$target de $field[1]: $text", @list);
182 broadcast_users("$target de $field[1]: $text");
184 Log('ann', $target, $field[1], $text);
186 return if $field[2] eq $main::mycall; # it's routed to me
188 route($field[2], $line);
189 return; # only on a routed one
205 if ($pcno == 16) { # add a user
206 my $node = DXCluster->get_exact($field[1]);
207 last SWITCH if !$node; # ignore if havn't seen a PC19 for this one yet
211 for ($i = 2; $i < $#field; $i++) {
212 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (-) (\d)/o;
213 next if length $call < 3;
216 next if DXCluster->get_exact($call); # we already have this (loop?)
218 $confmode = $confmode eq '*';
219 DXNodeuser->new($self, $node, $call, $confmode, $here);
221 # add this station to the user database, if required
222 $call =~ s/-\d+$//o; # remove ssid for users
223 my $user = DXUser->get_current($call);
224 $user = DXUser->new($call) if !$user;
225 $user->homenode($node->call) if !$user->homenode;
226 $user->node($node->call);
227 $user->lastin($main::systime);
231 # queue up any messages (look for privates only)
232 DXMsg::queue_msg(1) if $self->state eq 'normal';
236 if ($pcno == 17) { # remove a user
238 my $ref = DXCluster->get_exact($field[1]);
243 if ($pcno == 18) { # link request
244 $self->send_local_config();
246 $self->state('init');
250 if ($pcno == 19) { # incoming cluster list
252 for ($i = 1; $i < $#field-1; $i += 4) {
253 my $here = $field[$i];
254 my $call = uc $field[$i+1];
255 my $confmode = $field[$i+2] eq '*';
256 my $ver = $field[$i+3];
258 # now check the call over
259 next if DXCluster->get_exact($call); # we already have this
261 # check for sane parameters
262 next if $ver < 5000; # only works with version 5 software
263 next if length $call < 3; # min 3 letter callsigns
264 DXNode->new($self, $call, $confmode, $here, $ver);
266 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
267 my $mref = DXMsg::get_busy($call);
268 $mref->stop_msg($self) if $mref;
270 # add this station to the user database, if required (don't remove SSID from nodes)
271 my $user = DXUser->get_current($call);
273 $user = DXUser->new($call);
275 $user->priv(1); # I have relented and defaulted nodes
276 $self->{priv} = 1; # to user RCMDs allowed
277 $user->homenode($call);
280 $user->lastin($main::systime);
284 # queue up any messages
285 DXMsg::queue_msg() if $self->state eq 'normal';
289 if ($pcno == 20) { # send local configuration
290 $self->send_local_config();
292 $self->state('normal');
299 if ($pcno == 21) { # delete a cluster from the list
300 my $call = uc $field[1];
301 if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
302 my $ref = DXCluster->get_exact($call);
309 $self->state('normal');
316 if ($pcno == 23 || $pcno == 27) { # WWV info
317 Geomag::update(@field[1..$#field]);
321 if ($pcno == 24) { # set here status
322 my $call = uc $field[1];
323 my $ref = DXCluster->get_exact($call);
324 $ref->here($field[2]) if $ref;
332 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
333 DXMsg::process($self, $line);
337 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
338 if ($field[1] eq $main::mycall) {
339 my $ref = DXUser->get_current($field[2]);
340 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
341 unless ($field[3] =~ /rcmd/i) { # not allowed to relay RCMDS!
342 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
343 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
344 my @in = (DXCommandmode::run_cmd($self, $field[3]));
347 $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
348 Log('rcmd', 'out', $field[2], $_);
350 delete $self->{remotecmd};
353 $self->send(pc35($main::mycall, $field[2], "$main::mycall:Tut tut tut...!"));
356 route($field[1], $line);
361 if ($pcno == 35) { # remote command replies
362 if ($field[1] eq $main::mycall) {
363 my $s = $rcmds{$field[2]};
365 my $dxchan = DXChannel->get($s->{call});
366 $dxchan->send($field[3]) if $dxchan;
367 delete $rcmds{$field[2]} if !$dxchan;
370 route($field[1], $line);
379 if ($pcno == 38) { # node connected list from neighbour
383 if ($pcno == 39) { # incoming disconnect
388 if ($pcno == 41) { # user info
389 # add this station to the user database, if required
390 my $user = DXUser->get_current($field[1]);
392 # then try without an SSID
393 $field[1] =~ s/-\d+$//o;
394 $user = DXUser->get_current($field[1]);
396 $user = DXUser->new($field[1]) if !$user;
398 if ($field[2] == 1) {
399 $user->name($field[3]);
400 } elsif ($field[2] == 2) {
401 $user->qth($field[3]);
402 } elsif ($field[2] == 3) {
403 my ($lat, $long) = DXBearing::stoll($field[3]);
406 } elsif ($field[2] == 4) {
407 $user->homenode($field[3]);
431 if ($pcno == 50) { # keep alive/user list
432 my $ref = DXCluster->get_exact($field[1]);
433 $ref->update_users($field[2]) if $ref;
437 if ($pcno == 51) { # incoming ping requests/answers
440 if ($field[1] eq $main::mycall) {
441 my $flag = $field[3];
443 $self->send(pc51($field[2], $field[1], '0'));
445 # it's a reply, look in the ping list for this one
446 my $ref = $pings{$field[2]};
449 my $dxchan = DXChannel->get($r->{call});
450 $dxchan->send($dxchan->msg('pingi', $field[2], atime($main::systime), $main::systime - $r->{t})) if $dxchan;
455 # route down an appropriate thingy
456 route($field[1], $line);
462 # if get here then rebroadcast the thing with its Hop count decremented (if
463 # there is one). If it has a hop count and it decrements to zero then don't
466 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
471 if (!$self->{isolate} && (($hops) = $line =~ /H(\d+)\^\~?$/o)) {
472 my $newhops = $hops - 1;
474 $line =~ s/\^H$hops(\^\~?)$/\^H$newhops$1/; # change the hop count
475 broadcast_ak1a($line, $self); # send it to everyone but me
481 # This is called from inside the main cluster processing loop and is used
482 # for despatching commands that are doing some long processing job
487 my @chan = DXChannel->get_all();
490 foreach $chan (@chan) {
491 next if !$chan->is_ak1a();
493 # send a pc50 out on this channel
494 if ($t >= $chan->pc50_t + $DXProt::pc50_interval) {
503 if ($main::systime - 3600 > $last_hour) {
504 $cutoff = $main::systime - $pc11_dup_age;
505 while (($key, $val) = each %dup) {
506 delete $dup{$key} if $val < $cutoff;
508 $last_hour = $main::systime;
513 # finish up a pc context
518 my $call = $self->call;
519 my $ref = DXCluster->get_exact($call);
521 # unbusy and stop and outgoing mail
522 my $mref = DXMsg::get_busy($call);
523 $mref->stop_msg($self) if $mref;
525 # broadcast to all other nodes that all the nodes connected to via me are gone
526 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
529 foreach $node (@gonenodes) {
530 next if $node->call eq $call;
531 broadcast_ak1a(pc21($node->call, 'Gone'), $self) unless $self->{isolate}; # done like this 'cos DXNodes don't have a pc21 method
535 # remove outstanding pings
536 delete $pings{$call};
538 # now broadcast to all other ak1a nodes that I have gone
539 broadcast_ak1a(pc21($call, 'Gone.'), $self);
541 Log('DXProt', $call . " Disconnected");
546 # some active measures
549 sub send_local_config
556 if ($self->{isolate}) {
557 @nodes = (DXCluster->get_exact($main::mycall));
559 # create a list of all the nodes that are not connected to this connection
560 @nodes = DXNode::get_all();
561 @nodes = grep { $_->dxchan != $self } @nodes;
563 $self->send($me->pc19(@nodes));
565 # get all the users connected on the above nodes and send them out
566 foreach $n (@nodes) {
567 my @users = values %{$n->list};
568 $self->send(DXProt::pc16($n, @users));
573 # route a message down an appropriate interface for a callsign
575 # is called route(to, pcline);
579 my ($call, $line) = @_;
580 my $cl = DXCluster->get_exact($call);
583 my $dxchan = $cl->{dxchan};
584 if (($hops) = $line =~ /H(\d+)\^\~?$/o) {
585 my $newhops = $hops - 1;
587 $line =~ s/\^H$hops(\^\~?)$/\^H$newhops$1/; # change the hop count
588 $dxchan->send($line) if $dxchan;
591 $dxchan->send($line) if $dxchan; # for them wot don't have Hops
596 # broadcast a message to all clusters [except those mentioned after buffer]
599 my $s = shift; # the line to be rebroadcast
600 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
601 my @chan = get_all_ak1a();
604 foreach $chan (@chan) {
605 next if grep $chan == $_, @except;
606 $chan->send($s) unless $chan->{isolate}; # send it if it isn't the except list
610 # broadcast to all users
613 my $s = shift; # the line to be rebroadcast
614 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
615 my @chan = get_all_users();
618 foreach $chan (@chan) {
619 next if grep $chan == $_, @except;
620 $s =~ s/\a//og if !$chan->{beep};
621 $chan->send($s); # send it if it isn't the except list or hasn't a passout flag
625 # broadcast to a list of users
632 $chan->send($s); # send it
637 # gimme all the ak1a nodes
641 my @list = DXChannel->get_all();
644 foreach $ref (@list) {
645 push @out, $ref if $ref->is_ak1a;
650 # return a list of all users
653 my @list = DXChannel->get_all();
656 foreach $ref (@list) {
657 push @out, $ref if $ref->is_user;
662 # return a list of all user callsigns
663 sub get_all_user_calls
665 my @list = DXChannel->get_all();
668 foreach $ref (@list) {
669 push @out, $ref->call if $ref->is_user;
675 # obtain the hops from the list for this callsign and pc no
681 my $hops = $DXProt::hopcount{$pcno};
682 $hops = $DXProt::def_hopcount if !$hops;
686 # remove leading and trailing spaces from an input string
694 # add a ping request to the ping queues
697 my ($from, $to) = @_;
698 my $ref = $pings{$to};
699 $ref = $pings{$to} = [] if !$ref;
702 $r->{t} = $main::systime;
703 route($to, pc51($to, $main::mycall, 1));
707 # add a rcmd request to the rcmd queues
710 my ($from, $to, $cmd) = @_;
713 $r->{t} = $main::systime;
715 route($to, pc34($main::mycall, $to, $cmd));