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 Log('talk', $call, $field[1], $field[6], $text);
108 $call = $main::myalias if $call eq $main::mycall;
109 my $ref = DXChannel->get($call);
110 $ref->send("$call de $field[1]: $text") if $ref;
112 route($field[2], $line); # relay it on its way
117 if ($pcno == 11 || $pcno == 26) { # dx spot
119 # if this is a 'nodx' node then ignore it
120 last SWITCH if grep $field[7] =~ /^$_/, @DXProt::nodx_node;
122 # convert the date to a unix date
123 my $d = cltounix($field[3], $field[4]);
124 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
126 # strip off the leading & trailing spaces from the comment
127 my $text = unpad($field[5]);
130 my $spotter = $field[6];
131 $spotter =~ s/-\d+$//o; # strip off the ssid from the spotter
134 my $dupkey = "$field[1]$field[2]$d$text$field[6]";
135 return if $dup{$dupkey};
138 my $spot = Spot::add($field[1], $field[2], $d, $text, $spotter);
140 # send orf to the users
141 if ($spot && $pcno == 11) {
142 my $buf = Spot::formatb($field[1], $field[2], $d, $text, $spotter);
143 broadcast_users("$buf\a\a");
149 if ($pcno == 12) { # announces
151 if ($field[2] eq '*' || $field[2] eq $main::mycall) {
153 # strip leading and trailing stuff
154 my $text = unpad($field[3]);
159 if ($field[4] eq '*') { # sysops
161 @list = map { $_->priv >= 5 ? $_ : () } get_all_users();
162 } elsif ($field[4] gt ' ') { # speciality list handling
163 my ($name) = split /\./, $field[4];
164 $target = "$name"; # put the rest in later (if bothered)
167 if ($field[6] eq '1') {
171 $target = "All" if !$target;
174 broadcast_list("$to$target de $field[1]: $text", @list);
176 broadcast_users("$target de $field[1]: $text");
178 Log('ann', $target, $field[1], $text);
180 return if $field[2] eq $main::mycall; # it's routed to me
182 route($field[2], $line);
183 return; # only on a routed one
199 if ($pcno == 16) { # add a user
200 my $node = DXCluster->get_exact($field[1]);
201 last SWITCH if !$node; # ignore if havn't seen a PC19 for this one yet
204 for ($i = 2; $i < $#field; $i++) {
205 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (-) (\d)/o;
206 next if length $call < 3;
209 next if DXCluster->get_exact($call); # we already have this (loop?)
211 $confmode = $confmode eq '*';
212 DXNodeuser->new($self, $node, $call, $confmode, $here);
214 # add this station to the user database, if required
215 $call =~ s/-\d+$//o; # remove ssid for users
216 my $user = DXUser->get_current($call);
217 $user = DXUser->new($call) if !$user;
218 $user->node($node->call);
219 $user->homenode($node->call) if !$user->homenode;
223 # queue up any messages (look for privates only)
224 DXMsg::queue_msg(1) if $self->state eq 'normal';
228 if ($pcno == 17) { # remove a user
230 my $ref = DXCluster->get_exact($field[1]);
235 if ($pcno == 18) { # link request
236 $self->send_local_config();
238 $self->state('init');
242 if ($pcno == 19) { # incoming cluster list
244 for ($i = 1; $i < $#field-1; $i += 4) {
245 my $here = $field[$i];
246 my $call = uc $field[$i+1];
247 my $confmode = $field[$i+2] eq '*';
248 my $ver = $field[$i+3];
250 # now check the call over
251 next if DXCluster->get_exact($call); # we already have this
253 # check for sane parameters
254 next if $ver < 5000; # only works with version 5 software
255 next if length $call < 3; # min 3 letter callsigns
256 DXNode->new($self, $call, $confmode, $here, $ver);
258 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
259 my $mref = DXMsg::get_busy($call);
260 $mref->stop_msg($self) if $mref;
262 # add this station to the user database, if required (don't remove SSID from nodes)
263 my $user = DXUser->get_current($call);
265 $user = DXUser->new($call);
268 $user->homenode($call);
273 # queue up any messages
274 DXMsg::queue_msg() if $self->state eq 'normal';
278 if ($pcno == 20) { # send local configuration
279 $self->send_local_config();
281 $self->state('normal');
288 if ($pcno == 21) { # delete a cluster from the list
289 my $call = uc $field[1];
290 if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
291 my $ref = DXCluster->get_exact($call);
301 if ($pcno == 23 || $pcno == 27) { # WWV info
302 Geomag::update(@field[1..$#field]);
306 if ($pcno == 24) { # set here status
307 my $call = uc $field[1];
308 my $ref = DXCluster->get_exact($call);
309 $ref->here($field[2]) if $ref;
317 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42) { # mail/file handling
318 DXMsg::process($self, $line);
322 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
323 if ($field[1] eq $main::mycall) {
324 my $ref = DXUser->get_current($field[2]);
325 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
326 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
327 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
328 my @in = (DXCommandmode::run_cmd($self, $field[3]));
331 $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
332 Log('rcmd', 'out', $field[2], $_);
334 delete $self->{remotecmd};
337 route($field[1], $line);
342 if ($pcno == 35) { # remote command replies
343 if ($field[1] eq $main::mycall) {
344 my $s = DXChannel::get($main::myalias);
345 my @ref = grep { $_->pc34to eq $field[2] } DXChannel::get_all(); # people that have rcmded someone
352 route($field[1], $line);
361 if ($pcno == 38) { # node connected list from neighbour
365 if ($pcno == 39) { # incoming disconnect
370 if ($pcno == 41) { # user info
371 # add this station to the user database, if required
372 my $user = DXUser->get_current($field[1]);
374 # then try without an SSID
375 $field[1] =~ s/-\d+$//o;
376 $user = DXUser->get_current($field[1]);
378 $user = DXUser->new($field[1]) if !$user;
380 if ($field[2] == 1) {
381 $user->name($field[3]);
382 } elsif ($field[2] == 2) {
383 $user->qth($field[3]);
384 } elsif ($field[2] == 3) {
385 my ($latd, $latm, $latl, $longd, $longm, $longl) = split /\s+/, $field[3];
386 $longd += ($longm/60);
387 $longd = 0-$longd if (uc $longl) eq 'W';
390 $latd = 0-$latd if (uc $latl) eq 'S';
392 } elsif ($field[2] == 4) {
393 $user->homenode($field[3]);
420 if ($pcno == 50) { # keep alive/user list
421 my $ref = DXCluster->get_exact($field[1]);
422 $ref->update_users($field[2]) if $ref;
426 if ($pcno == 51) { # incoming ping requests/answers
429 if ($field[1] eq $main::mycall) {
430 my $flag = $field[3];
432 $self->send($self->pc51($field[2], $field[1], $flag));
434 # route down an appropriate thingy
435 route($field[1], $line);
441 # if get here then rebroadcast the thing with its Hop count decremented (if
442 # there is one). If it has a hop count and it decrements to zero then don't
445 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
450 if (($hops) = $line =~ /H(\d+)\^\~?$/o) {
451 my $newhops = $hops - 1;
453 $line =~ s/\^H$hops(\^\~?)$/\^H$newhops$1/; # change the hop count
454 broadcast_ak1a($line, $self); # send it to everyone but me
460 # This is called from inside the main cluster processing loop and is used
461 # for despatching commands that are doing some long processing job
466 my @chan = DXChannel->get_all();
469 foreach $chan (@chan) {
470 next if !$chan->is_ak1a();
472 # send a pc50 out on this channel
473 if ($t >= $chan->pc50_t + $DXProt::pc50_interval) {
482 if ($main::systime - 3600 > $last_hour) {
483 $cutoff = $main::systime - $pc11_dup_age;
484 while (($key, $val) = each %dup) {
485 delete $dup{$key} if $val < $cutoff;
487 $last_hour = $main::systime;
492 # finish up a pc context
497 my $ref = DXCluster->get_exact($self->call);
499 # unbusy and stop and outgoing mail
500 my $mref = DXMsg::get_busy($self->call);
501 $mref->stop_msg($self) if $mref;
503 # broadcast to all other nodes that all the nodes connected to via me are gone
504 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
507 foreach $node (@gonenodes) {
508 next if $node->call eq $self->call;
509 broadcast_ak1a(pc21($node->call, 'Gone'), $self); # done like this 'cos DXNodes don't have a pc21 method
513 # now broadcast to all other ak1a nodes that I have gone
514 broadcast_ak1a(pc21($self->call, 'Gone.'), $self);
515 Log('DXProt', $self->call . " Disconnected");
520 # some active measures
523 sub send_local_config
529 my @nodes = DXNode::get_all();
531 # create a list of all the nodes that are not connected to this connection
532 @nodes = grep { $_->dxchan != $self } @nodes;
533 $self->send($me->pc19(@nodes));
535 # get all the users connected on the above nodes and send them out
536 foreach $n (@nodes) {
537 my @users = values %{$n->list};
538 $self->send(DXProt::pc16($n, @users));
543 # route a message down an appropriate interface for a callsign
545 # is called route(to, pcline);
549 my ($call, $line) = @_;
550 my $cl = DXCluster->get_exact($call);
553 my $dxchan = $cl->{dxchan};
554 if (($hops) = $line =~ /H(\d+)\^\~?$/o) {
555 my $newhops = $hops - 1;
557 $line =~ s/\^H$hops(\^\~?)$/\^H$newhops$1/; # change the hop count
558 $dxchan->send($line) if $dxchan;
561 $dxchan->send($line) if $dxchan; # for them wot don't have Hops
566 # broadcast a message to all clusters [except those mentioned after buffer]
569 my $s = shift; # the line to be rebroadcast
570 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
571 my @chan = get_all_ak1a();
574 foreach $chan (@chan) {
575 next if grep $chan == $_, @except;
576 $chan->send($s); # send it if it isn't the except list
580 # broadcast to all users
583 my $s = shift; # the line to be rebroadcast
584 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
585 my @chan = get_all_users();
588 foreach $chan (@chan) {
589 next if grep $chan == $_, @except;
590 $chan->send($s); # send it if it isn't the except list
594 # broadcast to a list of users
601 $chan->send($s); # send it
606 # gimme all the ak1a nodes
610 my @list = DXChannel->get_all();
613 foreach $ref (@list) {
614 push @out, $ref if $ref->is_ak1a;
619 # return a list of all users
622 my @list = DXChannel->get_all();
625 foreach $ref (@list) {
626 push @out, $ref if $ref->is_user;
631 # return a list of all user callsigns
632 sub get_all_user_calls
634 my @list = DXChannel->get_all();
637 foreach $ref (@list) {
638 push @out, $ref->call if $ref->is_user;
644 # obtain the hops from the list for this callsign and pc no
650 my $hops = $DXProt::hopcount{$pcno};
651 $hops = $DXProt::def_hopcount if !$hops;
655 # remove leading and trailing spaces from an input string