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();
83 Log('DXProt', "$call connected");
87 # This is the normal pcxx despatcher
91 my ($self, $line) = @_;
92 my @field = split /[\^\~]/, $line;
94 # ignore any lines that don't start with PC
95 return if !$field[0] =~ /^PC/;
98 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
99 return if $pcno < 10 || $pcno > 51;
102 if ($pcno == 10) { # incoming talk
104 # is it for me or one of mine?
105 my $call = ($field[5] gt ' ') ? $field[5] : $field[2];
106 if ($call eq $main::mycall || grep $_ eq $call, get_all_user_calls()) {
109 my $text = unpad($field[3]);
110 Log('talk', $call, $field[1], $field[6], $text);
111 $call = $main::myalias if $call eq $main::mycall;
112 my $ref = DXChannel->get($call);
113 $ref->send("$call de $field[1]: $text") if $ref;
115 route($field[2], $line); # relay it on its way
120 if ($pcno == 11 || $pcno == 26) { # dx spot
122 # if this is a 'nodx' node then ignore it
123 last SWITCH if grep $field[7] =~ /^$_/, @DXProt::nodx_node;
125 # convert the date to a unix date
126 my $d = cltounix($field[3], $field[4]);
127 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
129 # strip off the leading & trailing spaces from the comment
130 my $text = unpad($field[5]);
133 my $spotter = $field[6];
134 $spotter =~ s/-\d+$//o; # strip off the ssid from the spotter
137 my $dupkey = "$field[1]$field[2]$d$text$field[6]";
138 return if $dup{$dupkey};
141 my $spot = Spot::add($field[1], $field[2], $d, $text, $spotter);
143 # send orf to the users
144 if ($spot && $pcno == 11) {
145 my $buf = Spot::formatb($field[1], $field[2], $d, $text, $spotter);
146 broadcast_users("$buf\a\a");
152 if ($pcno == 12) { # announces
154 if ($field[2] eq '*' || $field[2] eq $main::mycall) {
156 # strip leading and trailing stuff
157 my $text = unpad($field[3]);
162 if ($field[4] eq '*') { # sysops
164 @list = map { $_->priv >= 5 ? $_ : () } get_all_users();
165 } elsif ($field[4] gt ' ') { # speciality list handling
166 my ($name) = split /\./, $field[4];
167 $target = "$name"; # put the rest in later (if bothered)
170 if ($field[6] eq '1') {
174 $target = "All" if !$target;
177 broadcast_list("$to$target de $field[1]: $text", @list);
179 broadcast_users("$target de $field[1]: $text");
181 Log('ann', $target, $field[1], $text);
183 return if $field[2] eq $main::mycall; # it's routed to me
185 route($field[2], $line);
186 return; # only on a routed one
202 if ($pcno == 16) { # add a user
203 my $node = DXCluster->get_exact($field[1]);
204 last SWITCH if !$node; # ignore if havn't seen a PC19 for this one yet
207 for ($i = 2; $i < $#field; $i++) {
208 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (-) (\d)/o;
209 next if length $call < 3;
212 next if DXCluster->get_exact($call); # we already have this (loop?)
214 $confmode = $confmode eq '*';
215 DXNodeuser->new($self, $node, $call, $confmode, $here);
217 # add this station to the user database, if required
218 $call =~ s/-\d+$//o; # remove ssid for users
219 my $user = DXUser->get_current($call);
220 $user = DXUser->new($call) if !$user;
221 $user->node($node->call);
222 $user->homenode($node->call) if !$user->homenode;
226 # queue up any messages (look for privates only)
227 DXMsg::queue_msg(1) if $self->state eq 'normal';
231 if ($pcno == 17) { # remove a user
233 my $ref = DXCluster->get_exact($field[1]);
238 if ($pcno == 18) { # link request
239 $self->send_local_config();
241 $self->state('init');
245 if ($pcno == 19) { # incoming cluster list
247 for ($i = 1; $i < $#field-1; $i += 4) {
248 my $here = $field[$i];
249 my $call = uc $field[$i+1];
250 my $confmode = $field[$i+2] eq '*';
251 my $ver = $field[$i+3];
253 # now check the call over
254 next if DXCluster->get_exact($call); # we already have this
256 # check for sane parameters
257 next if $ver < 5000; # only works with version 5 software
258 next if length $call < 3; # min 3 letter callsigns
259 DXNode->new($self, $call, $confmode, $here, $ver);
261 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
262 my $mref = DXMsg::get_busy($call);
263 $mref->stop_msg($self) if $mref;
265 # add this station to the user database, if required (don't remove SSID from nodes)
266 my $user = DXUser->get_current($call);
268 $user = DXUser->new($call);
271 $user->homenode($call);
276 # queue up any messages
277 DXMsg::queue_msg() if $self->state eq 'normal';
281 if ($pcno == 20) { # send local configuration
282 $self->send_local_config();
284 $self->state('normal');
291 if ($pcno == 21) { # delete a cluster from the list
292 my $call = uc $field[1];
293 if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
294 my $ref = DXCluster->get_exact($call);
301 $self->state('normal');
308 if ($pcno == 23 || $pcno == 27) { # WWV info
309 Geomag::update(@field[1..$#field]);
313 if ($pcno == 24) { # set here status
314 my $call = uc $field[1];
315 my $ref = DXCluster->get_exact($call);
316 $ref->here($field[2]) if $ref;
324 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42) { # mail/file handling
325 DXMsg::process($self, $line);
329 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
330 if ($field[1] eq $main::mycall) {
331 my $ref = DXUser->get_current($field[2]);
332 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
333 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
334 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
335 my @in = (DXCommandmode::run_cmd($self, $field[3]));
338 $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
339 Log('rcmd', 'out', $field[2], $_);
341 delete $self->{remotecmd};
344 route($field[1], $line);
349 if ($pcno == 35) { # remote command replies
350 if ($field[1] eq $main::mycall) {
351 my $s = $rcmds{$field[2]};
353 my $dxchan = DXChannel->get($s->{call});
354 $dxchan->send($field[3]) if $dxchan;
355 delete $rcmds{$field[2]} if !$dxchan;
358 route($field[1], $line);
367 if ($pcno == 38) { # node connected list from neighbour
371 if ($pcno == 39) { # incoming disconnect
376 if ($pcno == 41) { # user info
377 # add this station to the user database, if required
378 my $user = DXUser->get_current($field[1]);
380 # then try without an SSID
381 $field[1] =~ s/-\d+$//o;
382 $user = DXUser->get_current($field[1]);
384 $user = DXUser->new($field[1]) if !$user;
386 if ($field[2] == 1) {
387 $user->name($field[3]);
388 } elsif ($field[2] == 2) {
389 $user->qth($field[3]);
390 } elsif ($field[2] == 3) {
391 my ($latd, $latm, $latl, $longd, $longm, $longl) = split /\s+/, $field[3];
392 $longd += ($longm/60);
393 $longd = 0-$longd if (uc $longl) eq 'W';
396 $latd = 0-$latd if (uc $latl) eq 'S';
398 } elsif ($field[2] == 4) {
399 $user->homenode($field[3]);
426 if ($pcno == 50) { # keep alive/user list
427 my $ref = DXCluster->get_exact($field[1]);
428 $ref->update_users($field[2]) if $ref;
432 if ($pcno == 51) { # incoming ping requests/answers
435 if ($field[1] eq $main::mycall) {
436 my $flag = $field[3];
438 $self->send(pc51($field[2], $field[1], '0'));
440 # it's a reply, look in the ping list for this one
441 my $ref = $pings{$field[2]};
444 my $dxchan = DXChannel->get($r->{call});
445 $dxchan->send($dxchan->msg('pingi', $field[2], atime($main::systime), $main::systime - $r->{t})) if $dxchan;
450 # route down an appropriate thingy
451 route($field[1], $line);
457 # if get here then rebroadcast the thing with its Hop count decremented (if
458 # there is one). If it has a hop count and it decrements to zero then don't
461 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
466 if (($hops) = $line =~ /H(\d+)\^\~?$/o) {
467 my $newhops = $hops - 1;
469 $line =~ s/\^H$hops(\^\~?)$/\^H$newhops$1/; # change the hop count
470 broadcast_ak1a($line, $self); # send it to everyone but me
476 # This is called from inside the main cluster processing loop and is used
477 # for despatching commands that are doing some long processing job
482 my @chan = DXChannel->get_all();
485 foreach $chan (@chan) {
486 next if !$chan->is_ak1a();
488 # send a pc50 out on this channel
489 if ($t >= $chan->pc50_t + $DXProt::pc50_interval) {
498 if ($main::systime - 3600 > $last_hour) {
499 $cutoff = $main::systime - $pc11_dup_age;
500 while (($key, $val) = each %dup) {
501 delete $dup{$key} if $val < $cutoff;
503 $last_hour = $main::systime;
508 # finish up a pc context
513 my $call = $self->call;
514 my $ref = DXCluster->get_exact($call);
516 # unbusy and stop and outgoing mail
517 my $mref = DXMsg::get_busy($call);
518 $mref->stop_msg($self) if $mref;
520 # broadcast to all other nodes that all the nodes connected to via me are gone
521 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
524 foreach $node (@gonenodes) {
525 next if $node->call eq $call;
526 broadcast_ak1a(pc21($node->call, 'Gone'), $self); # done like this 'cos DXNodes don't have a pc21 method
530 # remove outstanding pings
531 delete $pings{$call};
533 # now broadcast to all other ak1a nodes that I have gone
534 broadcast_ak1a(pc21($call, 'Gone.'), $self);
535 Log('DXProt', $call . " Disconnected");
540 # some active measures
543 sub send_local_config
549 my @nodes = DXNode::get_all();
551 # create a list of all the nodes that are not connected to this connection
552 @nodes = grep { $_->dxchan != $self } @nodes;
553 $self->send($me->pc19(@nodes));
555 # get all the users connected on the above nodes and send them out
556 foreach $n (@nodes) {
557 my @users = values %{$n->list};
558 $self->send(DXProt::pc16($n, @users));
563 # route a message down an appropriate interface for a callsign
565 # is called route(to, pcline);
569 my ($call, $line) = @_;
570 my $cl = DXCluster->get_exact($call);
573 my $dxchan = $cl->{dxchan};
574 if (($hops) = $line =~ /H(\d+)\^\~?$/o) {
575 my $newhops = $hops - 1;
577 $line =~ s/\^H$hops(\^\~?)$/\^H$newhops$1/; # change the hop count
578 $dxchan->send($line) if $dxchan;
581 $dxchan->send($line) if $dxchan; # for them wot don't have Hops
586 # broadcast a message to all clusters [except those mentioned after buffer]
589 my $s = shift; # the line to be rebroadcast
590 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
591 my @chan = get_all_ak1a();
594 foreach $chan (@chan) {
595 next if grep $chan == $_, @except;
596 $chan->send($s); # send it if it isn't the except list
600 # broadcast to all users
603 my $s = shift; # the line to be rebroadcast
604 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
605 my @chan = get_all_users();
608 foreach $chan (@chan) {
609 next if grep $chan == $_, @except;
610 $s =~ s/\a//og if !$chan->{beep};
611 $chan->send($s); # send it if it isn't the except list
615 # broadcast to a list of users
622 $chan->send($s); # send it
627 # gimme all the ak1a nodes
631 my @list = DXChannel->get_all();
634 foreach $ref (@list) {
635 push @out, $ref if $ref->is_ak1a;
640 # return a list of all users
643 my @list = DXChannel->get_all();
646 foreach $ref (@list) {
647 push @out, $ref if $ref->is_user;
652 # return a list of all user callsigns
653 sub get_all_user_calls
655 my @list = DXChannel->get_all();
658 foreach $ref (@list) {
659 push @out, $ref->call if $ref->is_user;
665 # obtain the hops from the list for this callsign and pc no
671 my $hops = $DXProt::hopcount{$pcno};
672 $hops = $DXProt::def_hopcount if !$hops;
676 # remove leading and trailing spaces from an input string
684 # add a ping request to the ping queues
687 my ($from, $to) = @_;
688 my $ref = $pings{$to};
689 $ref = $pings{$to} = [] if !$ref;
692 $r->{t} = $main::systime;
693 route($to, pc51($to, $main::mycall, 1));
697 # add a rcmd request to the rcmd queues
700 my ($from, $to, $cmd) = @_;
703 $r->{t} = $main::systime;
705 route($to, pc34($main::mycall, $to, $cmd));