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);
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
37 my $user = DXUser->get($main::mycall);
38 $me = DXProt->new($main::mycall, undef, $user);
39 # $me->{sort} = 'M'; # M for me
43 # obtain a new connection this is derived from dxchannel
48 my $self = DXChannel::alloc(@_);
49 $self->{sort} = 'A'; # in absence of how to find out what sort of an object I am
53 # this is how a pc connection starts (for an incoming connection)
54 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
55 # all the crap that comes between).
58 my ($self, $line, $sort) = @_;
59 my $call = $self->{call};
60 my $user = $self->{user};
62 # remember type of connection
63 $self->{consort} = $line;
64 $self->{outbound} = $sort eq 'O';
65 $self->{priv} = $user->priv;
66 $self->{lang} = $user->lang;
67 $self->{consort} = $line; # save the connection type
71 $self->send_now('B',"0");
73 # send initialisation string
74 if (!$self->{outbound}) {
75 $self->send(pc38()) if DXNode->get_all();
80 Log('DXProt', "$call connected");
84 # This is the normal pcxx despatcher
88 my ($self, $line) = @_;
89 my @field = split /[\^\~]/, $line;
91 # ignore any lines that don't start with PC
92 return if !$field[0] =~ /^PC/;
95 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
96 return if $pcno < 10 || $pcno > 51;
99 if ($pcno == 10) { # incoming talk
101 # is it for me or one of mine?
102 my $call = ($field[5] gt ' ') ? $field[5] : $field[2];
103 if ($call eq $main::mycall || grep $_ eq $call, get_all_user_calls()) {
106 my $text = unpad($field[3]);
107 my $ref = DXChannel->get($call);
108 $ref->send("$call de $field[1]: $text") if $ref;
110 route($field[2], $line); # relay it on its way
115 if ($pcno == 11 || $pcno == 26) { # dx spot
117 # if this is a 'nodx' node then ignore it
118 last SWITCH if grep $field[7] =~ /^$_/, @DXProt::nodx_node;
120 # convert the date to a unix date
121 my $d = cltounix($field[3], $field[4]);
122 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
124 # strip off the leading & trailing spaces from the comment
125 my $text = unpad($field[5]);
128 my $spotter = $field[6];
129 $spotter =~ s/-\d+$//o; # strip off the ssid from the spotter
132 my $dupkey = "$field[1]$field[2]$d$text$field[6]";
133 return if $dup{$dupkey};
136 my $spot = Spot::add($field[1], $field[2], $d, $text, $spotter);
138 # send orf to the users
139 if ($spot && $pcno == 11) {
140 my $buf = Spot::formatb($field[1], $field[2], $d, $text, $spotter);
141 broadcast_users("$buf\a\a");
147 if ($pcno == 12) { # announces
149 if ($field[2] eq '*' || $field[2] eq $main::mycall) {
151 # strip leading and trailing stuff
152 my $text = unpad($field[3]);
156 if ($field[4] eq '*') { # sysops
157 $target = "To Sysops";
158 @list = map { $_->priv >= 5 ? $_ : () } get_all_users();
159 } elsif ($field[4] gt ' ') { # speciality list handling
160 my ($name) = split /\./, $field[4];
161 $target = "To $name"; # put the rest in later (if bothered)
164 $target = "WX" if $field[6] eq '1';
165 $target = "To All" if !$target;
168 broadcast_list("$target de $field[1]: $text", @list);
170 broadcast_users("$target de $field[1]: $text");
173 return if $field[2] eq $main::mycall; # it's routed to me
175 route($field[2], $line);
176 return; # only on a routed one
182 if ($pcno == 13) {last SWITCH;}
183 if ($pcno == 14) {last SWITCH;}
184 if ($pcno == 15) {last SWITCH;}
186 if ($pcno == 16) { # add a user
187 my $node = DXCluster->get($field[1]);
188 last SWITCH if !$node; # ignore if havn't seen a PC19 for this one yet
191 for ($i = 2; $i < $#field; $i++) {
192 my ($call, $confmode, $here) = $field[$i] =~ /^(\w+) (-) (\d)/o;
193 next if length $call < 3;
196 $call =~ s/-\d+$//o; # remove ssid
197 next if DXCluster->get($call); # we already have this (loop?)
199 $confmode = $confmode eq '*';
200 DXNodeuser->new($self, $node, $call, $confmode, $here);
202 # add this station to the user database, if required
203 my $user = DXUser->get_current($call);
204 $user = DXUser->new($call) if !$user;
205 $user->node($node->call) if !$user->node;
209 # queue up any messages (look for privates only)
210 DXMsg::queue_msg(1) if $self->state eq 'normal';
214 if ($pcno == 17) { # remove a user
215 my $ref = DXCluster->get($field[1]);
220 if ($pcno == 18) { # link request
221 $self->send_local_config();
223 $self->state('init');
227 if ($pcno == 19) { # incoming cluster list
229 for ($i = 1; $i < $#field-1; $i += 4) {
230 my $here = $field[$i];
231 my $call = uc $field[$i+1];
232 my $confmode = $field[$i+2] eq '*';
233 my $ver = $field[$i+3];
235 # now check the call over
236 next if DXCluster->get($call); # we already have this
238 # check for sane parameters
239 next if $ver < 5000; # only works with version 5 software
240 next if length $call < 3; # min 3 letter callsigns
241 DXNode->new($self, $call, $confmode, $here, $ver);
243 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
244 my $mref = DXMsg::get_busy($call);
245 $mref->stop_msg($self) if $mref;
247 # add this station to the user database, if required
248 my $user = DXUser->get_current($call);
249 $user = DXUser->new($call) if !$user;
250 $user->node($call) if !$user->node;
255 # queue up any messages
256 DXMsg::queue_msg() if $self->state eq 'normal';
260 if ($pcno == 20) { # send local configuration
261 $self->send_local_config();
263 $self->state('normal');
270 if ($pcno == 21) { # delete a cluster from the list
271 my $call = uc $field[1];
272 if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
273 my $ref = DXCluster->get($call);
279 if ($pcno == 22) {last SWITCH;}
281 if ($pcno == 23 || $pcno == 27) { # WWV info
282 Geomag::update(@field[1..$#field]);
286 if ($pcno == 24) { # set here status
287 my $call = uc $field[1];
289 my $ref = DXCluster->get($call);
290 $ref->here($field[2]) if $ref;
294 if ($pcno == 25) {last SWITCH;}
296 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42) { # mail/file handling
297 DXMsg::process($self, $line);
301 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
302 if ($field[1] eq $main::mycall) {
303 if ($self->{priv}) { # you have to have SOME privilege, the commands have further filtering
304 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
305 for (DXCommandmode::run_cmd($self, $field[3])) {
307 $self->send(pc35($main::mycall, $self->{call}, "$main::mycall:$_"));
309 delete $self->{remotecmd};
312 route($field[1], $line);
317 if ($pcno == 35) { # remote command replies
318 if ($field[1] eq $main::mycall) {
319 my $s = DXChannel::get($main::myalias);
320 my @ref = grep { $_->pc34to eq $field[2] } DXChannel::get_all(); # people that have rcmded someone
327 route($field[1], $line);
332 if ($pcno == 37) {last SWITCH;}
334 if ($pcno == 38) { # node connected list from neighbour
338 if ($pcno == 39) { # incoming disconnect
343 if ($pcno == 41) { # user info
344 # add this station to the user database, if required
345 $field[1] =~ s/-\d+$//o;
346 my $user = DXUser->get_current($field[1]);
347 $user = DXUser->new($field[1]) if !$user;
349 if ($field[2] == 1) {
350 $user->name($field[3]);
351 } elsif ($field[2] == 2) {
352 $user->qth($field[3]);
353 } elsif ($field[2] == 3) {
354 my ($latd, $latm, $latl, $longd, $longm, $longl) = split /\s+/, $field[3];
355 $longd += ($longm/60);
356 $longd = 0-$longd if (uc $longl) eq 'W';
359 $latd = 0-$latd if (uc $latl) eq 'S';
361 } elsif ($field[2] == 4) {
362 $user->node($field[3]);
367 if ($pcno == 43) {last SWITCH;}
368 if ($pcno == 44) {last SWITCH;}
369 if ($pcno == 45) {last SWITCH;}
370 if ($pcno == 46) {last SWITCH;}
371 if ($pcno == 47) {last SWITCH;}
372 if ($pcno == 48) {last SWITCH;}
373 if ($pcno == 49) {last SWITCH;}
375 if ($pcno == 50) { # keep alive/user list
376 my $ref = DXCluster->get($field[1]);
377 $ref->update_users($field[2]) if $ref;
381 if ($pcno == 51) { # incoming ping requests/answers
384 if ($field[1] eq $main::mycall) {
385 my $flag = $field[3];
387 $self->send($self->pc51($field[2], $field[1], $flag));
389 # route down an appropriate thingy
390 route($field[1], $line);
396 # if get here then rebroadcast the thing with its Hop count decremented (if
397 # there is one). If it has a hop count and it decrements to zero then don't
400 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
405 if (($hops) = $line =~ /H(\d+)\^\~?$/o) {
406 my $newhops = $hops - 1;
408 $line =~ s/\^H$hops(\^\~?)$/\^H$newhops$1/; # change the hop count
409 broadcast_ak1a($line, $self); # send it to everyone but me
415 # This is called from inside the main cluster processing loop and is used
416 # for despatching commands that are doing some long processing job
421 my @chan = DXChannel->get_all();
424 foreach $chan (@chan) {
425 next if !$chan->is_ak1a();
427 # send a pc50 out on this channel
428 if ($t >= $chan->pc50_t + $DXProt::pc50_interval) {
437 if ($main::systime - 3600 > $last_hour) {
438 $cutoff = $main::systime - $pc11_dup_age;
439 while (($key, $val) = each %dup) {
440 delete $dup{$key} if $val < $cutoff;
442 $last_hour = $main::systime;
447 # finish up a pc context
452 my $ref = DXCluster->get($self->call);
454 # unbusy and stop and outgoing mail
455 my $mref = DXMsg::get_busy($self->call);
456 $mref->stop_msg($self) if $mref;
458 # broadcast to all other nodes that all the nodes connected to via me are gone
459 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
462 foreach $node (@gonenodes) {
463 next if $node->call eq $self->call;
464 broadcast_ak1a(pc21($node->call, 'Gone'), $self); # done like this 'cos DXNodes don't have a pc21 method
468 # now broadcast to all other ak1a nodes that I have gone
469 broadcast_ak1a(pc21($self->call, 'Gone.'), $self);
470 Log('DXProt', $self->call . " Disconnected");
475 # some active measures
478 sub send_local_config
484 my @nodes = DXNode::get_all();
486 # create a list of all the nodes that are not connected to this connection
487 @nodes = grep { $_->dxchan != $self } @nodes;
488 $self->send($me->pc19(@nodes));
490 # get all the users connected on the above nodes and send them out
491 foreach $n (@nodes) {
492 my @users = values %{$n->list};
493 $self->send(DXProt::pc16($n, @users));
498 # route a message down an appropriate interface for a callsign
500 # is called route(to, pcline);
504 my ($call, $line) = @_;
505 my $cl = DXCluster->get($call);
508 my $dxchan = $cl->{dxchan};
509 if (($hops) = $line =~ /H(\d+)\^\~?$/o) {
510 my $newhops = $hops - 1;
512 $line =~ s/\^H$hops(\^\~?)$/\^H$newhops$1/; # change the hop count
513 $dxchan->send($line) if $dxchan;
516 $dxchan->send($line) if $dxchan; # for them wot don't have Hops
521 # broadcast a message to all clusters [except those mentioned after buffer]
524 my $s = shift; # the line to be rebroadcast
525 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
526 my @chan = get_all_ak1a();
529 foreach $chan (@chan) {
530 next if grep $chan == $_, @except;
531 $chan->send($s); # send it if it isn't the except list
535 # broadcast to all users
538 my $s = shift; # the line to be rebroadcast
539 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
540 my @chan = get_all_users();
543 foreach $chan (@chan) {
544 next if grep $chan == $_, @except;
545 $chan->send($s); # send it if it isn't the except list
549 # broadcast to a list of users
556 $chan->send($s); # send it
561 # gimme all the ak1a nodes
565 my @list = DXChannel->get_all();
568 foreach $ref (@list) {
569 push @out, $ref if $ref->is_ak1a;
574 # return a list of all users
577 my @list = DXChannel->get_all();
580 foreach $ref (@list) {
581 push @out, $ref if $ref->is_user;
586 # return a list of all user callsigns
587 sub get_all_user_calls
589 my @list = DXChannel->get_all();
592 foreach $ref (@list) {
593 push @out, $ref->call if $ref->is_user;
599 # obtain the hops from the list for this callsign and pc no
605 my $hops = $DXProt::hopcount{$pcno};
606 $hops = $DXProt::def_hopcount if !$hops;
610 # remove leading and trailing spaces from an input string