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) = shift;
59 my $call = $self->call;
61 # remember type of connection
62 $self->{consort} = $line;
65 $self->send_now('B',"0");
67 # send initialisation string
68 $self->send(pc38()) if DXNode->get_all();
72 Log('DXProt', "$call connected");
76 # This is the normal pcxx despatcher
80 my ($self, $line) = @_;
81 my @field = split /[\^\~]/, $line;
83 # ignore any lines that don't start with PC
84 return if !$field[0] =~ /^PC/;
87 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
88 return if $pcno < 10 || $pcno > 51;
91 if ($pcno == 10) { # incoming talk
93 # is it for me or one of mine?
94 my $call = ($field[5] gt ' ') ? $field[5] : $field[2];
95 if ($call eq $main::mycall || grep $_ eq $call, get_all_user_calls()) {
98 my $text = unpad($field[3]);
99 my $ref = DXChannel->get($call);
100 $ref->send("$call de $field[1]: $text") if $ref;
102 route($field[2], $line); # relay it on its way
107 if ($pcno == 11 || $pcno == 26) { # dx spot
109 # if this is a 'nodx' node then ignore it
110 last SWITCH if grep $field[7] =~ /^$_/, @DXProt::nodx_node;
112 # convert the date to a unix date
113 my $d = cltounix($field[3], $field[4]);
114 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
116 # strip off the leading & trailing spaces from the comment
117 my $text = unpad($field[5]);
120 my $spotter = $field[6];
121 $spotter =~ s/-\d+$//o; # strip off the ssid from the spotter
124 my $dupkey = "$field[1]$field[2]$d$text$field[6]";
125 return if $dup{$dupkey};
128 my $spot = Spot::add($field[1], $field[2], $d, $text, $spotter);
130 # send orf to the users
131 if ($spot && $pcno == 11) {
132 my $buf = Spot::formatb($field[1], $field[2], $d, $text, $spotter);
133 broadcast_users("$buf\a\a");
139 if ($pcno == 12) { # announces
141 if ($field[2] eq '*' || $field[2] eq $main::mycall) {
143 # strip leading and trailing stuff
144 my $text = unpad($field[3]);
148 if ($field[4] eq '*') { # sysops
149 $target = "To Sysops";
150 @list = map { $_->priv >= 5 ? $_ : () } get_all_users();
151 } elsif ($field[4] gt ' ') { # speciality list handling
152 my ($name) = split /\./, $field[4];
153 $target = "To $name"; # put the rest in later (if bothered)
156 $target = "WX" if $field[6] eq '1';
157 $target = "To All" if !$target;
160 broadcast_list("$target de $field[1]: $text", @list);
162 broadcast_users("$target de $field[1]: $text");
165 return if $field[2] eq $main::mycall; # it's routed to me
167 route($field[2], $line);
168 return; # only on a routed one
174 if ($pcno == 13) {last SWITCH;}
175 if ($pcno == 14) {last SWITCH;}
176 if ($pcno == 15) {last SWITCH;}
178 if ($pcno == 16) { # add a user
179 my $node = DXCluster->get($field[1]);
180 last SWITCH if !$node; # ignore if havn't seen a PC19 for this one yet
183 for ($i = 2; $i < $#field; $i++) {
184 my ($call, $confmode, $here) = $field[$i] =~ /^(\w+) (-) (\d)/o;
185 next if length $call < 3;
188 $call =~ s/-\d+$//o; # remove ssid
189 next if DXCluster->get($call); # we already have this (loop?)
191 $confmode = $confmode eq '*';
192 DXNodeuser->new($self, $node, $call, $confmode, $here);
194 # add this station to the user database, if required
195 my $user = DXUser->get_current($call);
196 $user = DXUser->new($call) if !$user;
197 $user->node($node->call) if !$user->node;
201 # queue up any messages (look for privates only)
202 DXMsg::queue_msg(1) if $self->state eq 'normal';
206 if ($pcno == 17) { # remove a user
207 my $ref = DXCluster->get($field[1]);
212 if ($pcno == 18) { # link request
213 $self->send_local_config();
215 $self->state('init');
219 if ($pcno == 19) { # incoming cluster list
221 for ($i = 1; $i < $#field-1; $i += 4) {
222 my $here = $field[$i];
223 my $call = uc $field[$i+1];
224 my $confmode = $field[$i+2] eq '*';
225 my $ver = $field[$i+3];
227 # now check the call over
228 next if DXCluster->get($call); # we already have this
230 # check for sane parameters
231 next if $ver < 5000; # only works with version 5 software
232 next if length $call < 3; # min 3 letter callsigns
233 DXNode->new($self, $call, $confmode, $here, $ver);
235 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
236 my $mref = DXMsg::get_busy($call);
237 $mref->stop_msg($self) if $mref;
240 # queue up any messages
241 DXMsg::queue_msg() if $self->state eq 'normal';
245 if ($pcno == 20) { # send local configuration
246 $self->send_local_config();
248 $self->state('normal');
255 if ($pcno == 21) { # delete a cluster from the list
256 my $call = uc $field[1];
257 if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
258 my $ref = DXCluster->get($call);
264 if ($pcno == 22) {last SWITCH;}
266 if ($pcno == 23 || $pcno == 27) { # WWV info
267 Geomag::update(@field[1..$#field]);
271 if ($pcno == 24) { # set here status
272 my $call = uc $field[1];
274 my $ref = DXCluster->get($call);
275 $ref->here($field[2]) if $ref;
279 if ($pcno == 25) {last SWITCH;}
281 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42) { # mail/file handling
282 DXMsg::process($self, $line);
286 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
290 if ($pcno == 35) { # remote command replies
294 if ($pcno == 37) {last SWITCH;}
296 if ($pcno == 38) { # node connected list from neighbour
300 if ($pcno == 39) { # incoming disconnect
305 if ($pcno == 41) { # user info
306 # add this station to the user database, if required
307 $field[1] =~ s/-\d+$//o;
308 my $user = DXUser->get_current($field[1]);
309 $user = DXUser->new($field[1]) if !$user;
311 if ($field[2] == 1) {
312 $user->name($field[3]);
313 } elsif ($field[2] == 2) {
314 $user->qth($field[3]);
315 } elsif ($field[2] == 3) {
316 my ($latd, $latm, $latl, $longd, $longm, $longl) = split /\s+/, $field[3];
317 $longd += ($longm/60);
318 $longd = 0-$longd if (uc $longl) eq 'W';
321 $latd = 0-$latd if (uc $latl) eq 'S';
323 } elsif ($field[2] == 4) {
324 $user->node($field[3]);
329 if ($pcno == 43) {last SWITCH;}
330 if ($pcno == 44) {last SWITCH;}
331 if ($pcno == 45) {last SWITCH;}
332 if ($pcno == 46) {last SWITCH;}
333 if ($pcno == 47) {last SWITCH;}
334 if ($pcno == 48) {last SWITCH;}
335 if ($pcno == 49) {last SWITCH;}
337 if ($pcno == 50) { # keep alive/user list
338 my $ref = DXCluster->get($field[1]);
339 $ref->update_users($field[2]) if $ref;
343 if ($pcno == 51) { # incoming ping requests/answers
346 if ($field[1] eq $main::mycall) {
347 my $flag = $field[3];
349 $self->send($self->pc51($field[2], $field[1], $flag));
351 # route down an appropriate thingy
352 route($field[1], $line);
358 # if get here then rebroadcast the thing with its Hop count decremented (if
359 # there is one). If it has a hop count and it decrements to zero then don't
362 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
367 if (($hops) = $line =~ /H(\d+)\^\~?$/o) {
368 my $newhops = $hops - 1;
370 $line =~ s/\^H$hops(\^\~?)$/\^H$newhops$1/; # change the hop count
371 broadcast_ak1a($line, $self); # send it to everyone but me
377 # This is called from inside the main cluster processing loop and is used
378 # for despatching commands that are doing some long processing job
383 my @chan = DXChannel->get_all();
386 foreach $chan (@chan) {
387 next if !$chan->is_ak1a();
389 # send a pc50 out on this channel
390 if ($t >= $chan->pc50_t + $DXProt::pc50_interval) {
399 if ($main::systime - 3600 > $last_hour) {
400 $cutoff = $main::systime - $pc11_dup_age;
401 while (($key, $val) = each %dup) {
402 delete $dup{$key} if $val < $cutoff;
404 $last_hour = $main::systime;
409 # finish up a pc context
414 my $ref = DXCluster->get($self->call);
416 # unbusy and stop and outgoing mail
417 my $mref = DXMsg::get_busy($self->call);
418 $mref->stop_msg($self) if $mref;
420 # broadcast to all other nodes that all the nodes connected to via me are gone
421 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
424 foreach $node (@gonenodes) {
425 next if $node->call eq $self->call;
426 broadcast_ak1a(pc21($node->call, 'Gone'), $self); # done like this 'cos DXNodes don't have a pc21 method
430 # now broadcast to all other ak1a nodes that I have gone
431 broadcast_ak1a(pc21($self->call, 'Gone.'), $self);
432 Log('DXProt', $self->call . " Disconnected");
437 # some active measures
440 sub send_local_config
446 my @nodes = DXNode::get_all();
448 # create a list of all the nodes that are not connected to this connection
449 @nodes = map { $_->dxchan != $self ? $_ : () } @nodes;
450 $self->send($me->pc19(@nodes));
452 # get all the users connected on the above nodes and send them out
453 foreach $n (@nodes) {
454 my @users = values %{$n->list};
455 $self->send(DXProt::pc16($n, @users));
460 # route a message down an appropriate interface for a callsign
462 # is called route(to, pcline);
466 my ($call, $line) = @_;
467 my $cl = DXCluster->get($call);
470 my $dxchan = $cl->{dxchan};
471 if (($hops) = $line =~ /H(\d+)\^\~?$/o) {
472 my $newhops = $hops - 1;
474 $line =~ s/\^H$hops(\^\~?)$/\^H$newhops$1/; # change the hop count
475 $dxchan->send($line) if $dxchan;
478 $dxchan->send($line) if $dxchan; # for them wot don't have Hops
483 # broadcast a message to all clusters [except those mentioned after buffer]
486 my $s = shift; # the line to be rebroadcast
487 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
488 my @chan = get_all_ak1a();
491 foreach $chan (@chan) {
492 next if grep $chan == $_, @except;
493 $chan->send($s); # send it if it isn't the except list
497 # broadcast to all users
500 my $s = shift; # the line to be rebroadcast
501 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
502 my @chan = get_all_users();
505 foreach $chan (@chan) {
506 next if grep $chan == $_, @except;
507 $chan->send($s); # send it if it isn't the except list
511 # broadcast to a list of users
518 $chan->send($s); # send it
523 # gimme all the ak1a nodes
527 my @list = DXChannel->get_all();
530 foreach $ref (@list) {
531 push @out, $ref if $ref->is_ak1a;
536 # return a list of all users
539 my @list = DXChannel->get_all();
542 foreach $ref (@list) {
543 push @out, $ref if $ref->is_user;
548 # return a list of all user callsigns
549 sub get_all_user_calls
551 my @list = DXChannel->get_all();
554 foreach $ref (@list) {
555 push @out, $ref->call if $ref->is_user;
561 # obtain the hops from the list for this callsign and pc no
567 my $hops = $DXProt::hopcount{$pcno};
568 $hops = $DXProt::def_hopcount if !$hops;
572 # remove leading and trailing spaces from an input string