3 # This module impliments the protocal mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
30 use Time::HiRes qw(gettimeofday tv_interval);
37 use vars qw($me $pc11_max_age $pc23_max_age
38 $last_hour %pings %rcmds
39 %nodehops $baddx $badspotter $badnode $censorpc
40 $allowzero $decode_dk0wcy $send_opernam @checklist);
42 $me = undef; # the channel id for this cluster
43 $pc11_max_age = 1*3600; # the maximum age for an incoming 'real-time' pc11
44 $pc23_max_age = 1*3600; # the maximum age for an incoming 'real-time' pc23
46 $last_hour = time; # last time I did an hourly periodic update
47 %pings = (); # outstanding ping requests outbound
48 %rcmds = (); # outstanding rcmd requests outbound
49 %nodehops = (); # node specific hop control
50 $censorpc = 1; # Do a BadWords::check on text fields and reject things
51 # loads of 'bad things'
52 $baddx = new DXHash "baddx";
53 $badspotter = new DXHash "badspotter";
54 $badnode = new DXHash "badnode";
58 [ qw(c c m bp bc c) ], # pc10
59 [ qw(f m d t m c c h) ], # pc11
60 [ qw(c bc m bp bm p h) ], # pc12
64 undef , # pc16 has to be validated manually
67 undef , # pc19 has to be validated manually
68 undef , # pc20 no validation
70 undef , # pc22 no validation
71 [ qw(d n n n n m c c h) ], # pc23
73 [ qw(c c n n) ], # pc25
74 [ qw(f m d t m c c bc) ], # pc26
75 [ qw(d n n n n m c c bc) ], # pc27
76 [ qw(c c m c d t p m bp n p bp bc) ], # pc28
77 [ qw(c c n m) ], # pc29
85 [ qw(c c n m) ], # pc37
86 undef, # pc38 not interested
88 [ qw(c c m p n) ], # pc40
89 [ qw(c n m h) ], # pc41
91 undef, # pc43 don't handle it
92 [ qw(c c n m m c) ], # pc44
93 [ qw(c c n m) ], # pc45
121 [ qw(d n n n n n n m m m c c h) ], # pc73
132 [ qw(c c c m) ], # pc84
133 [ qw(c c c m) ], # pc85
136 # use the entry in the check list to check the field list presented
137 # return OK if line NOT in check list (for now)
142 return 0 if $n < 0 || $n > @checklist;
143 my $ref = $checklist[$n];
144 return 0 unless ref $ref;
147 shift; # not interested in the first field
148 for ($i = 0; $i < @$ref; $i++) {
149 my ($blank, $act) = $$ref[$i] =~ /^(b?)(\w)$/;
150 return 0 unless $act;
151 next if $blank && $_[$i] =~ /^[ \*]$/;
153 return $i+1 unless is_callsign($_[$i]);
154 } elsif ($act eq 'm') {
155 return $i+1 unless is_pctext($_[$i]);
156 } elsif ($act eq 'p') {
157 return $i+1 unless is_pcflag($_[$i]);
158 } elsif ($act eq 'f') {
159 return $i+1 unless is_freq($_[$i]);
160 } elsif ($act eq 'n') {
161 return $i+1 unless $_[$i] =~ /^[\d ]+$/;
162 } elsif ($act eq 'h') {
163 return $i+1 unless $_[$i] =~ /^H\d\d?$/;
164 } elsif ($act eq 'd') {
165 return $i+1 unless $_[$i] =~ /^\s*\d+-\w\w\w-[12][90]\d\d$/;
166 } elsif ($act eq 't') {
167 return $i+1 unless $_[$i] =~ /^[012]\d[012345]\dZ$/;
175 my $user = DXUser->get($main::mycall);
176 $DXProt::myprot_version += $main::version*100;
177 $me = DXProt->new($main::mycall, 0, $user);
179 $me->{state} = "indifferent";
180 do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
182 $me->{sort} = 'S'; # S for spider
184 # $Route::Node::me->adddxchan($me);
188 # obtain a new connection this is derived from dxchannel
193 my $self = DXChannel::alloc(@_);
195 # add this node to the table, the values get filled in later
198 $main::routeroot->add($call) if $call ne $main::mycall;
203 # this is how a pc connection starts (for an incoming connection)
204 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
205 # all the crap that comes between).
208 my ($self, $line, $sort) = @_;
209 my $call = $self->{call};
210 my $user = $self->{user};
212 # remember type of connection
213 $self->{consort} = $line;
214 $self->{outbound} = $sort eq 'O';
215 $self->{priv} = $user->priv || 1; # other clusters can always be 'normal' users
216 $self->{lang} = $user->lang || 'en';
217 $self->{isolate} = $user->{isolate};
218 $self->{consort} = $line; # save the connection type
221 # get the output filters
222 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
223 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
224 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
225 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
226 $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) ;
229 # get the INPUT filters (these only pertain to Clusters)
230 $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
231 $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
232 $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
233 $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
234 $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1);
236 # set unbuffered and no echo
237 $self->send_now('B',"0");
238 $self->send_now('E',"0");
240 # ping neighbour node stuff
241 my $ping = $user->pingint;
242 $ping = 5*60 unless defined $ping;
243 $self->{pingint} = $ping;
244 $self->{nopings} = $user->nopings || 2;
245 $self->{pingtime} = [ ];
246 $self->{pingave} = 0;
248 # send initialisation string
249 unless ($self->{outbound}) {
251 $self->{lastping} = $main::systime;
253 $self->{lastping} = $main::systime + ($self->pingint / 2);
255 $self->state('init');
256 $self->{pc50_t} = $main::systime;
258 # send info to all logged in thingies
259 $self->tell_login('loginn');
261 Log('DXProt', "$call connected");
265 # This is the normal pcxx despatcher
269 my ($self, $line) = @_;
270 my @field = split /\^/, $line;
271 return unless @field;
273 pop @field if $field[-1] eq '~';
275 # print join(',', @field), "\n";
277 # ignore any lines that don't start with PC
278 return if !$field[0] =~ /^PC/;
281 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
283 return if $pcno < 10 || $pcno > 99;
285 # check for and dump bad protocol messages
286 my $n = check($pcno, @field);
288 dbg('chan', "PCPROT: bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")");
295 $pcr = Local::pcprot($self, $pcno, @field);
297 # dbg('local', "Local::pcprot error $@") if $@;
301 if ($pcno == 10) { # incoming talk
303 # will we allow it at all?
306 if (@bad = BadWords::check($field[3])) {
307 dbg('chan', "PCPROT: Bad words: @bad, dropped" );
312 # is it for me or one of mine?
313 my ($to, $via, $call, $dxchan);
314 if ($field[5] gt ' ') {
315 $call = $via = $field[2];
318 $call = $to = $field[2];
320 $dxchan = DXChannel->get($call);
321 if ($dxchan && $dxchan->is_user) {
322 $field[3] =~ s/\%5E/^/g;
323 $dxchan->talk($field[1], $to, $via, $field[3]);
325 $self->route($field[2], $line); # relay it on its way
330 if ($pcno == 11 || $pcno == 26) { # dx spot
332 # route 'foreign' pc26s
334 if ($field[7] ne $main::mycall) {
335 $self->route($field[7], $line);
340 # if this is a 'nodx' node then ignore it
341 if ($badnode->in($field[7])) {
342 dbg('chan', "PCPROT: Bad Node, dropped");
346 # if this is a 'bad spotter' user then ignore it
347 if ($badspotter->in($field[6])) {
348 dbg('chan', "PCPROT: Bad Spotter, dropped");
352 # convert the date to a unix date
353 my $d = cltounix($field[3], $field[4]);
354 # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
355 if (!$d || ($pcno == 11 && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
356 dbg('chan', "PCPROT: Spot ignored, invalid date or out of range ($field[3] $field[4])\n");
361 if ($baddx->in($field[2])) {
362 dbg('chan', "PCPROT: Bad DX spot, ignored");
367 $field[5] =~ s/^\s+//; # take any leading blanks off
368 $field[2] = unpad($field[2]); # take off leading and trailing blanks from spotted callsign
369 if ($field[2] =~ /BUST\w*$/) {
370 dbg('chan', "PCPROT: useless 'BUSTED' spot");
373 if (Spot::dup($field[1], $field[2], $d, $field[5])) {
374 dbg('chan', "PCPROT: Duplicate Spot ignored\n");
379 if (@bad = BadWords::check($field[5])) {
380 dbg('chan', "PCPROT: Bad words: @bad, dropped" );
385 my @spot = Spot::prepare($field[1], $field[2], $d, $field[5], $field[6], $field[7]);
386 # global spot filtering on INPUT
387 if ($self->{inspotsfilter}) {
388 my ($filter, $hops) = $self->{inspotsfilter}->it(@spot);
390 dbg('chan', "PCPROT: Rejected by filter");
399 # @spot at this point contains:-
400 # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
401 # then spotted itu, spotted cq, spotters itu, spotters cq
402 # you should be able to route on any of these
405 # fix up qra locators of known users
406 my $user = DXUser->get_current($spot[4]);
408 my $qra = $user->qra;
409 unless ($qra && DXBearing::is_qra($qra)) {
410 my $lat = $user->lat;
411 my $long = $user->long;
412 if (defined $lat && defined $long) {
413 $user->qra(DXBearing::lltoqra($lat, $long));
418 # send a remote command to a distant cluster if it is visible and there is no
419 # qra locator and we havn't done it for a month.
421 unless ($user->qra) {
423 my $to = $user->homenode;
424 my $last = $user->lastoper || 0;
425 if ($send_opernam && $main::systime > $last + $DXUser::lastoperinterval && $to && ($node = Route::Node::get($to)) ) {
426 my $cmd = "forward/opernam $spot[4]";
427 # send the rcmd but we aren't interested in the replies...
428 my $dxchan = $node->dxchan;
429 if ($dxchan && $dxchan->is_clx) {
430 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
432 route(undef, $to, pc34($main::mycall, $to, $cmd));
434 if ($to ne $field[7]) {
436 $node = Route::Node::get($to);
438 $dxchan = $node->dxchan;
439 if ($dxchan && $dxchan->is_clx) {
440 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
442 route(undef, $to, pc34($main::mycall, $to, $cmd));
446 $user->lastoper($main::systime);
455 $r = Local::spot($self, @spot);
457 # dbg('local', "Local::spot1 error $@") if $@;
460 # DON'T be silly and send on PC26s!
461 return if $pcno == 26;
463 # send out the filtered spots
464 send_dx_spot($self, $line, @spot) if @spot;
468 if ($pcno == 12) { # announces
469 # announce duplicate checking
470 $field[3] =~ s/^\s+//; # remove leading blanks
471 if (AnnTalk::dup($field[1], $field[2], $field[3])) {
472 dbg('chan', "PCPROT: Duplicate Announce ignored");
478 if (@bad = BadWords::check($field[3])) {
479 dbg('chan', "PCPROT: Bad words: @bad, dropped" );
484 if ($field[2] eq '*' || $field[2] eq $main::mycall) {
486 # global ann filtering on INPUT
487 if ($self->{inannfilter}) {
488 my ($ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
489 my @dxcc = Prefix::extract($field[1]);
491 $ann_dxcc = $dxcc[1]->dxcc;
492 $ann_itu = $dxcc[1]->itu;
493 $ann_cq = $dxcc[1]->cq();
495 @dxcc = Prefix::extract($field[5]);
497 $org_dxcc = $dxcc[1]->dxcc;
498 $org_itu = $dxcc[1]->itu;
499 $org_cq = $dxcc[1]->cq();
501 my ($filter, $hops) = $self->{inannfilter}->it(@field[1..6], $self->{call},
502 $ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq);
504 dbg('chan', "PCPROT: Rejected by filter");
510 $self->send_announce($line, @field[1..6]);
512 $self->route($field[2], $line);
528 if ($pcno == 16) { # add a user
532 my $ncall = $field[1];
533 my $newline = "PC16^";
535 if ($ncall eq $main::mycall) {
536 dbg('chan', "PCPROT: trying to alter config on this node from outside!");
539 $dxchan = DXChannel->get($ncall);
540 if ($dxchan && $dxchan ne $self) {
541 dbg('chan', "PCPROT: PC16 from $self->{call} trying to alter locally connected $ncall, ignored!");
544 my $parent = Route::Node::get($ncall);
546 dbg('chan', "PCPROT: Node $ncall not in config");
551 for ($i = 2; $i < $#field; $i++) {
552 my ($call, $conf, $here) = $field[$i] =~ /^(\S+) (\S) (\d)/o;
553 next unless $call && $conf && defined $here && is_callsign($call);
554 $conf = $conf eq '*';
556 push @rout, $parent->add_user($call, Route::here($here)|Route::conf($conf));
558 # add this station to the user database, if required
559 $call =~ s/-\d+$//o; # remove ssid for users
560 my $user = DXUser->get_current($call);
561 $user = DXUser->new($call) if !$user;
562 $user->homenode($parent->call) if !$user->homenode;
563 $user->node($parent->call);
564 $user->lastin($main::systime) unless DXChannel->get($call);
569 # queue up any messages (look for privates only)
570 DXMsg::queue_msg(1) if $self->state eq 'normal';
572 dbg('route', "B/C PC16 on $ncall for: " . join(',', map{$_->call} @rout)) if @rout;
573 $self->route_pc16($parent, @rout) if @rout;
577 if ($pcno == 17) { # remove a user
579 my $ncall = $field[2];
580 if ($ncall eq $main::mycall) {
581 dbg('chan', "PCPROT: trying to alter config on this node from outside!");
584 $dxchan = DXChannel->get($ncall);
585 if ($dxchan && $dxchan ne $self) {
586 dbg('chan', "PCPROT: PC17 from $self->{call} trying to alter locally connected $ncall, ignored!");
589 my $parent = Route::Node::get($ncall);
591 dbg('chan', "PCPROT: Route::Node $ncall not in config");
594 my @rout = $parent->del_user($field[1]);
595 dbg('route', "B/C PC17 on $ncall for: $field[1]");
596 $self->route_pc17($parent, @rout) if @rout;
600 if ($pcno == 18) { # link request
601 $self->state('init');
603 # first clear out any nodes on this dxchannel
604 my $parent = Route::Node::get($self->{call});
606 for ($parent->nodes) {
607 my $r = Route::Node::get($_);
608 push @rout, $r->del_node if $r;
610 $self->route_pc21(@rout, $parent);
611 $self->send_local_config();
613 return; # we don't pass these on
616 if ($pcno == 19) { # incoming cluster list
618 my $newline = "PC19^";
622 my $parent = Route::Node::get($self->{call});
625 for ($i = 1; $i < $#field-1; $i += 4) {
626 my $here = $field[$i];
627 my $call = uc $field[$i+1];
628 my $conf = $field[$i+2];
629 my $ver = $field[$i+3];
630 next unless defined $here && defined $conf && is_callsign($call);
631 # check for sane parameters
632 $ver = 5000 if $ver eq '0000';
633 next if $ver < 5000; # only works with version 5 software
634 next if length $call < 3; # min 3 letter callsigns
636 # update it if required
637 if ($parent->call eq $call && !$parent->version) {
638 $parent->version($ver);
639 $parent->flags(Route::here($here)|Route::conf($conf));
641 } elsif ($parent->call ne $call) {
642 next if $call eq $main::mycall || $call eq $self->{call};
644 my $r = $parent->add($call, $ver, Route::here($here)|Route::conf($conf));
645 push @rout, $r if $r;
648 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
649 my $mref = DXMsg::get_busy($call);
650 $mref->stop_msg($call) if $mref;
652 # add this station to the user database, if required (don't remove SSID from nodes)
653 my $user = DXUser->get_current($call);
655 $user = DXUser->new($call);
657 $user->priv(1); # I have relented and defaulted nodes
658 $self->{priv} = 1; # to user RCMDs allowed
659 $user->homenode($call);
662 $user->lastin($main::systime) unless DXChannel->get($call);
666 dbg('route', "B/C PC19 for: " . join(',', map{$_->call} @rout)) if @rout;
668 $self->route_pc19(@rout) if @rout;
672 if ($pcno == 20) { # send local configuration
673 $self->send_local_config();
675 $self->state('normal');
679 if ($pcno == 21) { # delete a cluster from the list
680 my $call = uc $field[1];
682 my $parent = Route::Node::get($self->{call});
684 if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
685 if ($call eq $self->{call}) {
686 dbg('chan', "PCPROT: Trying to disconnect myself with PC21");
692 push @rout, $parent->del_node($call);
694 dbg('chan', "PCPROT: Route::Node $call not in config");
697 dbg('chan', "PCPROT: I WILL _NOT_ be disconnected!");
700 dbg('route', "B/C PC21 for: " . join(',', (map{$_->call} @rout))) if @rout;
702 $self->route_pc21(@rout) if @rout;
707 $self->state('normal');
711 if ($pcno == 23 || $pcno == 27) { # WWV info
713 # route 'foreign' pc27s
715 if ($field[8] ne $main::mycall) {
716 $self->route($field[8], $line);
722 my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
723 my $sfi = unpad($field[3]);
724 my $k = unpad($field[4]);
725 my $i = unpad($field[5]);
726 my ($r) = $field[6] =~ /R=(\d+)/;
728 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
729 dbg('chan', "PCPROT: WWV Date ($field[1] $field[2]) out of range");
732 if (Geomag::dup($d,$sfi,$k,$i,$field[6])) {
733 dbg('chan', "PCPROT: Dup WWV Spot ignored\n");
736 $field[7] =~ s/-\d+$//o; # remove spotter's ssid
738 my $wwv = Geomag::update($d, $field[2], $sfi, $k, $i, @field[6..8], $r);
742 $rep = Local::wwv($self, $field[1], $field[2], $sfi, $k, $i, @field[6..8], $r);
744 # dbg('local', "Local::wwv2 error $@") if $@;
747 # DON'T be silly and send on PC27s!
748 return if $pcno == 27;
750 # broadcast to the eager world
751 send_wwv_spot($self, $line, $d, $field[2], $sfi, $k, $i, @field[6..8]);
755 if ($pcno == 24) { # set here status
756 my $call = uc $field[1];
757 my $ref = Route::Node::get($call);
758 $ref->here($field[2]) if $ref;
759 $ref = Route::User::get($call);
760 $ref->here($field[2]) if $ref;
764 if ($pcno == 25) { # merge request
765 if ($field[1] ne $main::mycall) {
766 $self->route($field[1], $line);
769 if ($field[2] eq $main::mycall) {
770 dbg('chan', "PCPROT: Trying to merge to myself, ignored");
774 Log('DXProt', "Merge request for $field[3] spots and $field[4] WWV from $field[1]");
778 my @in = reverse Spot::search(1, undef, undef, 0, $field[3]);
781 $self->send(pc26(@{$in}[0..4], $field[2]));
787 my @in = reverse Geomag::search(0, $field[4], time, 1);
790 $self->send(pc27(@{$in}[0..5], $field[2]));
796 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
797 if ($pcno == 49 || $field[1] eq $main::mycall) {
798 DXMsg::process($self, $line);
800 $self->route($field[1], $line) unless $self->is_clx;
805 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
806 if ($field[1] eq $main::mycall) {
807 my $ref = DXUser->get_current($field[2]);
808 my $cref = Route::Node::get($field[2]);
809 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
810 unless (!$cref || !$ref || $cref->call ne $ref->homenode) { # not allowed to relay RCMDS!
811 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
812 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
813 my $oldpriv = $self->{priv};
814 $self->{priv} = $ref->{priv}; # assume the user's privilege level
815 my @in = (DXCommandmode::run_cmd($self, $field[3]));
816 $self->{priv} = $oldpriv;
819 $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
820 Log('rcmd', 'out', $field[2], $_);
822 delete $self->{remotecmd};
824 $self->send(pc35($main::mycall, $field[2], "$main::mycall:sorry...!"));
827 $self->send(pc35($main::mycall, $field[2], "$main::mycall:your attempt is logged, Tut tut tut...!"));
830 my $ref = DXUser->get_current($field[1]);
831 if ($ref && $ref->is_clx) {
832 $self->route($field[1], pc84($field[2], $field[1], $field[2], $field[3]));
834 $self->route($field[1], $line);
840 if ($pcno == 35) { # remote command replies
841 if ($field[1] eq $main::mycall) {
842 my $s = $rcmds{$field[2]};
844 my $dxchan = DXChannel->get($s->{call});
845 $dxchan->send($field[3]) if $dxchan;
846 delete $rcmds{$field[2]} if !$dxchan;
848 # send unsolicited ones to the sysop
849 my $dxchan = DXChannel->get($main::myalias);
850 $dxchan->send($field[3]) if $dxchan;
853 my $ref = DXUser->get_current($field[1]);
854 if ($ref && $ref->is_clx) {
855 $self->route($field[1], pc85($field[2], $field[1], $field[2], $field[3]));
857 $self->route($field[1], $line);
863 # for pc 37 see 44 onwards
865 if ($pcno == 38) { # node connected list from neighbour
869 if ($pcno == 39) { # incoming disconnect
870 if ($field[1] eq $self->{call}) {
871 $self->disconnect(1);
873 dbg('chan', "PCPROT: came in on wrong channel");
878 if ($pcno == 41) { # user info
879 # add this station to the user database, if required
880 my $user = DXUser->get_current($field[1]);
881 $user = DXUser->new($field[1]) if !$user;
883 if ($field[2] == 1) {
884 $user->name($field[3]);
885 } elsif ($field[2] == 2) {
886 $user->qth($field[3]);
887 } elsif ($field[2] == 3) {
888 my ($lat, $long) = DXBearing::stoll($field[3]);
891 $user->qra(DXBearing::lltoqra($lat, $long)) unless $user->qra && DXBearing::is_qra($user->qra);
892 } elsif ($field[2] == 4) {
893 $user->homenode($field[3]);
895 $user->lastoper($main::systime); # to cut down on excessive for/opers being generated
902 if ($pcno == 37 || $pcno == 44 || $pcno == 45 || $pcno == 46 || $pcno == 47 || $pcno == 48) {
903 DXDb::process($self, $line);
907 if ($pcno == 50) { # keep alive/user list
908 my $node = Route::Node::get($field[1]);
910 return unless $node->call eq $self->{call};
911 $node->usercount($field[2]);
916 if ($pcno == 51) { # incoming ping requests/answers
919 if ($field[1] eq $main::mycall) {
920 my $flag = $field[3];
922 $self->send(pc51($field[2], $field[1], '0'));
924 # it's a reply, look in the ping list for this one
925 my $ref = $pings{$field[2]};
927 my $tochan = DXChannel->get($field[2]);
930 my $dxchan = DXChannel->get($r->{call});
932 my $t = tv_interval($r->{t}, [ gettimeofday ]);
933 if ($dxchan->is_user) {
934 my $s = sprintf "%.2f", $t;
935 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
936 $dxchan->send($dxchan->msg('pingi', $field[2], $s, $ave))
937 } elsif ($dxchan->is_node) {
939 $tochan->{nopings} = $tochan->user->nopings || 2; # pump up the timer
940 push @{$tochan->{pingtime}}, $t;
941 shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
943 for (@{$tochan->{pingtime}}) {
946 $tochan->{pingave} = $st / @{$tochan->{pingtime}};
953 # route down an appropriate thingy
954 $self->route($field[1], $line);
959 if ($pcno == 75) { # dunno but route it
960 if ($field[1] ne $main::mycall) {
961 $self->route($field[1], $line);
966 if ($pcno == 73) { # WCY broadcasts
969 my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
970 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
971 dbg('chan', "PCPROT: WCY Date ($field[1] $field[2]) out of range");
974 @field = map { unpad($_) } @field;
975 if (WCY::dup($d,@field[3..7])) {
976 dbg('chan', "PCPROT: Dup WCY Spot ignored\n");
980 my $wcy = WCY::update($d, @field[2..12]);
984 $rep = Local::wwv($self, @field[1..12]);
986 # dbg('local', "Local::wcy error $@") if $@;
989 # broadcast to the eager world
990 send_wcy_spot($self, $line, $d, @field[2..12]);
994 if ($pcno == 84) { # remote commands (incoming)
995 if ($field[1] eq $main::mycall) {
996 my $ref = DXUser->get_current($field[2]);
997 my $cref = Route::Node::get($field[2]);
998 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[4]);
999 unless ($field[4] =~ /rcmd/i || !$cref || !$ref || $cref->call ne $ref->homenode) { # not allowed to relay RCMDS!
1000 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
1001 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
1002 my $oldpriv = $self->{priv};
1003 $self->{priv} = $ref->{priv}; # assume the user's privilege level
1004 my @in = (DXCommandmode::run_cmd($self, $field[4]));
1005 $self->{priv} = $oldpriv;
1008 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:$_"));
1009 Log('rcmd', 'out', $field[2], $_);
1011 delete $self->{remotecmd};
1013 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:sorry...!"));
1016 $self->send(pc85($main::mycall, $field[2], $field[3],"$main::mycall:your attempt is logged, Tut tut tut...!"));
1019 my $ref = DXUser->get_current($field[1]);
1020 if ($ref && $ref->is_clx) {
1021 $self->route($field[1], $line);
1023 $self->route($field[1], pc34($field[2], $field[1], $field[4]));
1029 if ($pcno == 85) { # remote command replies
1030 if ($field[1] eq $main::mycall) {
1031 my $dxchan = DXChannel->get($field[3]);
1033 $dxchan->send($field[4]);
1035 my $s = $rcmds{$field[2]};
1037 $dxchan = DXChannel->get($s->{call});
1038 $dxchan->send($field[4]) if $dxchan;
1039 delete $rcmds{$field[2]} if !$dxchan;
1041 # send unsolicited ones to the sysop
1042 my $dxchan = DXChannel->get($main::myalias);
1043 $dxchan->send($field[4]) if $dxchan;
1047 my $ref = DXUser->get_current($field[1]);
1048 if ($ref && $ref->is_clx) {
1049 $self->route($field[1], $line);
1051 $self->route($field[1], pc35($field[2], $field[1], $field[4]));
1058 # if get here then rebroadcast the thing with its Hop count decremented (if
1059 # there is one). If it has a hop count and it decrements to zero then don't
1062 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
1066 unless ($self->{isolate}) {
1067 broadcast_ak1a($line, $self); # send it to everyone but me
1072 # This is called from inside the main cluster processing loop and is used
1073 # for despatching commands that are doing some long processing job
1078 my @dxchan = DXChannel->get_all();
1081 foreach $dxchan (@dxchan) {
1082 next unless $dxchan->is_node();
1083 next if $dxchan == $me;
1085 # send a pc50 out on this channel
1086 $dxchan->{pc50_t} = $main::systime unless exists $dxchan->{pc50_t};
1087 if ($t >= $dxchan->{pc50_t} + $DXProt::pc50_interval) {
1088 $dxchan->send(pc50(scalar DXChannel::get_all_users));
1089 $dxchan->{pc50_t} = $t;
1092 # send a ping out on this channel
1093 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
1094 if ($dxchan->{nopings} <= 0) {
1095 $dxchan->disconnect;
1097 addping($main::mycall, $dxchan->call);
1098 $dxchan->{nopings} -= 1;
1099 $dxchan->{lastping} = $t;
1107 if ($main::systime - 3600 > $last_hour) {
1111 $last_hour = $main::systime;
1116 # finish up a pc context
1120 # some active measures
1127 my @dxchan = DXChannel->get_all();
1130 # send it if it isn't the except list and isn't isolated and still has a hop count
1131 # taking into account filtering and so on
1132 foreach $dxchan (@dxchan) {
1133 next if $dxchan == $me;
1135 my ($filter, $hops);
1137 if ($dxchan->{spotsfilter}) {
1138 ($filter, $hops) = $dxchan->{spotsfilter}->it(@_, $self->{call} );
1139 next unless $filter;
1142 if ($dxchan->is_node) {
1143 next if $dxchan == $self;
1146 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1148 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1149 next unless $routeit;
1152 $dxchan->send($routeit) if $routeit;
1154 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1156 } elsif ($dxchan->is_user && $dxchan->{dx}) {
1157 my $buf = Spot::formatb($dxchan->{user}->wantgrid, $_[0], $_[1], $_[2], $_[3], $_[4]);
1158 $buf .= "\a\a" if $dxchan->{beep};
1160 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1161 $dxchan->send($buf);
1163 $dxchan->delay($buf);
1173 my @dxchan = DXChannel->get_all();
1175 my ($wwv_dxcc, $wwv_itu, $wwv_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1176 my @dxcc = Prefix::extract($_[7]);
1178 $wwv_dxcc = $dxcc[1]->dxcc;
1179 $wwv_itu = $dxcc[1]->itu;
1180 $wwv_cq = $dxcc[1]->cq;
1182 @dxcc = Prefix::extract($_[8]);
1184 $org_dxcc = $dxcc[1]->dxcc;
1185 $org_itu = $dxcc[1]->itu;
1186 $org_cq = $dxcc[1]->cq;
1189 # send it if it isn't the except list and isn't isolated and still has a hop count
1190 # taking into account filtering and so on
1191 foreach $dxchan (@dxchan) {
1192 next if $dxchan == $self;
1193 next if $dxchan == $me;
1195 my ($filter, $hops);
1197 if ($dxchan->{wwvfilter}) {
1198 ($filter, $hops) = $dxchan->{wwvfilter}->it(@_, $self->{call}, $wwv_dxcc, $wwv_itu, $wwv_cq, $org_dxcc, $org_itu, $org_cq);
1199 next unless $filter;
1201 if ($dxchan->is_node) {
1204 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1206 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1207 next unless $routeit;
1210 $dxchan->send($routeit) if $routeit;
1212 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1215 } elsif ($dxchan->is_user && $dxchan->{wwv}) {
1216 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
1217 $buf .= "\a\a" if $dxchan->{beep};
1218 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1219 $dxchan->send($buf);
1221 $dxchan->delay($buf);
1231 my @dxchan = DXChannel->get_all();
1233 my ($wcy_dxcc, $wcy_itu, $wcy_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1234 my @dxcc = Prefix::extract($_[11]);
1236 $wcy_dxcc = $dxcc[1]->dxcc;
1237 $wcy_itu = $dxcc[1]->itu;
1238 $wcy_cq = $dxcc[1]->cq;
1240 @dxcc = Prefix::extract($_[12]);
1242 $org_dxcc = $dxcc[1]->dxcc;
1243 $org_itu = $dxcc[1]->itu;
1244 $org_cq = $dxcc[1]->cq;
1247 # send it if it isn't the except list and isn't isolated and still has a hop count
1248 # taking into account filtering and so on
1249 foreach $dxchan (@dxchan) {
1250 next if $dxchan == $me;
1252 my ($filter, $hops);
1254 if ($dxchan->{wcyfilter}) {
1255 ($filter, $hops) = $dxchan->{wcyfilter}->it(@_, $self->{call}, $wcy_dxcc, $wcy_itu, $wcy_cq, $org_dxcc, $org_itu, $org_cq);
1256 next unless $filter;
1258 if ($dxchan->is_clx || $dxchan->is_spider || $dxchan->is_dxnet) {
1261 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1263 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1264 next unless $routeit;
1267 $dxchan->send($routeit) if $routeit;
1269 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1271 } elsif ($dxchan->is_user && $dxchan->{wcy}) {
1272 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1273 $buf .= "\a\a" if $dxchan->{beep};
1274 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1275 $dxchan->send($buf);
1277 $dxchan->delay($buf);
1288 my @dxchan = DXChannel->get_all();
1290 my $text = unpad($_[2]);
1294 if ($_[3] eq '*') { # sysops
1296 } elsif ($_[3] gt ' ') { # speciality list handling
1297 my ($name) = split /\./, $_[3];
1298 $target = "$name"; # put the rest in later (if bothered)
1305 $target = "ALL" if !$target;
1307 Log('ann', $target, $_[0], $text);
1309 # obtain country codes etc
1310 my ($ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1311 my @dxcc = Prefix::extract($_[0]);
1313 $ann_dxcc = $dxcc[1]->dxcc;
1314 $ann_itu = $dxcc[1]->itu;
1315 $ann_cq = $dxcc[1]->cq;
1317 @dxcc = Prefix::extract($_[4]);
1319 $org_dxcc = $dxcc[1]->dxcc;
1320 $org_itu = $dxcc[1]->itu;
1321 $org_cq = $dxcc[1]->cq;
1324 # send it if it isn't the except list and isn't isolated and still has a hop count
1325 # taking into account filtering and so on
1326 foreach $dxchan (@dxchan) {
1327 next if $dxchan == $self;
1328 next if $dxchan == $me;
1330 my ($filter, $hops);
1332 if ($dxchan->{annfilter}) {
1333 ($filter, $hops) = $dxchan->{annfilter}->it(@_, $self->{call}, $ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq);
1334 next unless $filter;
1336 if ($dxchan->is_node && $_[1] ne $main::mycall) { # i.e not specifically routed to me
1339 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1341 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1342 next unless $routeit;
1345 $dxchan->send($routeit) if $routeit;
1347 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1350 } elsif ($dxchan->is_user) {
1351 unless ($dxchan->{ann}) {
1352 next if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
1354 next if $target eq 'SYSOP' && $dxchan->{priv} < 5;
1355 my $buf = "$to$target de $_[0]: $text";
1357 $buf .= "\a\a" if $dxchan->{beep};
1358 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1359 $dxchan->send($buf);
1361 $dxchan->delay($buf);
1367 sub send_local_config
1375 dbg('trace', 'DXProt::send_local_config');
1378 if ($self->{isolate}) {
1379 @localnodes = ( $main::routeroot );
1381 # create a list of all the nodes that are not connected to this connection
1382 # and are not themselves isolated, this to make sure that isolated nodes
1383 # don't appear outside of this node
1384 my @dxchan = grep { $_->call ne $main::mycall && $_->call ne $self->{call} } DXChannel::get_all_nodes();
1385 @localnodes = map { Route::Node::get($_->{call}) or die "connot find node $_->{call}" } @dxchan if @dxchan;
1386 my @intcalls = map { $_->nodes } @localnodes if @localnodes;
1387 my $ref = Route::Node::get($self->{call});
1388 my @rnodes = $ref->nodes;
1389 for my $n (@intcalls) {
1390 push @remotenodes, Route::Node::get($n) unless grep $n eq $_, @rnodes;
1392 unshift @localnodes, $main::routeroot;
1395 send_route($self, \&pc19, scalar(@localnodes)+scalar(@remotenodes), @localnodes, @remotenodes);
1397 # get all the users connected on the above nodes and send them out
1398 foreach $n (@localnodes, @remotenodes) {
1399 send_route($self, \&pc16, 1, $n, map {my $r = Route::User::get($_); $r ? ($r) : ()} $n->users);
1404 # route a message down an appropriate interface for a callsign
1406 # is called route(to, pcline);
1410 my ($self, $call, $line) = @_;
1412 if (ref $self && $call eq $self->{call}) {
1413 dbg('chan', "PCPROT: Trying to route back to source, dropped");
1417 # always send it down the local interface if available
1418 my $dxchan = DXChannel->get($call);
1420 my $cl = Route::Node::get($call);
1421 $dxchan = $cl->dxchan if $cl;
1423 if (ref $self && $dxchan eq $self) {
1424 dbg('chan', "PCPROT: Trying to route back to source, dropped");
1430 my $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1432 $dxchan->send($routeit);
1435 dbg('chan', "PCPROT: No route available, dropped");
1439 # broadcast a message to all clusters taking into account isolation
1440 # [except those mentioned after buffer]
1443 my $s = shift; # the line to be rebroadcast
1444 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1445 my @dxchan = DXChannel::get_all_nodes();
1448 # send it if it isn't the except list and isn't isolated and still has a hop count
1449 foreach $dxchan (@dxchan) {
1450 next if grep $dxchan == $_, @except;
1451 next if $dxchan == $me;
1453 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
1454 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
1458 # broadcast a message to all clusters ignoring isolation
1459 # [except those mentioned after buffer]
1460 sub broadcast_all_ak1a
1462 my $s = shift; # the line to be rebroadcast
1463 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1464 my @dxchan = DXChannel::get_all_nodes();
1467 # send it if it isn't the except list and isn't isolated and still has a hop count
1468 foreach $dxchan (@dxchan) {
1469 next if grep $dxchan == $_, @except;
1470 next if $dxchan == $me;
1472 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
1473 $dxchan->send($routeit);
1477 # broadcast to all users
1478 # storing the spot or whatever until it is in a state to receive it
1481 my $s = shift; # the line to be rebroadcast
1482 my $sort = shift; # the type of transmission
1483 my $fref = shift; # a reference to an object to filter on
1484 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1485 my @dxchan = DXChannel::get_all_users();
1489 foreach $dxchan (@dxchan) {
1490 next if grep $dxchan == $_, @except;
1493 broadcast_list($s, $sort, $fref, @out);
1496 # broadcast to a list of users
1504 foreach $dxchan (@_) {
1506 next if $dxchan == $me;
1508 if ($sort eq 'dx') {
1509 next unless $dxchan->{dx};
1510 ($filter) = $dxchan->{spotsfilter}->it(@{$fref}) if ref $fref;
1511 next unless $filter;
1513 next if $sort eq 'ann' && !$dxchan->{ann};
1514 next if $sort eq 'wwv' && !$dxchan->{wwv};
1515 next if $sort eq 'wcy' && !$dxchan->{wcy};
1516 next if $sort eq 'wx' && !$dxchan->{wx};
1518 $s =~ s/\a//og unless $dxchan->{beep};
1520 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1530 # obtain the hops from the list for this callsign and pc no
1536 my $hops = $DXProt::hopcount{$pcno};
1537 $hops = $DXProt::def_hopcount if !$hops;
1542 # adjust the hop count on a per node basis using the user loadable
1543 # hop table if available or else decrement an existing one
1550 my $call = $self->{call};
1553 if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
1554 my ($pcno) = $s =~ /^PC(\d\d)/o;
1555 confess "$call called adjust_hops with '$s'" unless $pcno;
1556 my $ref = $nodehops{$call} if %nodehops;
1558 my $newhops = $ref->{$pcno};
1559 return "" if defined $newhops && $newhops == 0;
1560 $newhops = $ref->{default} unless $newhops;
1561 return "" if defined $newhops && $newhops == 0;
1562 $newhops = $hops if !$newhops;
1563 $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
1565 # simply decrement it
1567 return "" if !$hops;
1568 $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
1580 return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1581 do "$main::data/hop_table.pl";
1587 # add a ping request to the ping queues
1590 my ($from, $to) = @_;
1591 my $ref = $pings{$to} || [];
1594 $r->{t} = [ gettimeofday ];
1595 route(undef, $to, pc51($to, $main::mycall, 1));
1600 # add a rcmd request to the rcmd queues
1603 my ($self, $to, $cmd) = @_;
1606 $r->{call} = $self->{call};
1607 $r->{t} = $main::systime;
1611 my $ref = Route::Node::get($to);
1612 my $dxchan = $ref->dxchan;
1613 if ($dxchan && $dxchan->is_clx) {
1614 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1616 route(undef, $to, pc34($main::mycall, $to, $cmd));
1623 my $pc39flag = shift;
1624 my $call = $self->call;
1626 unless ($pc39flag && $pc39flag == 1) {
1627 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1631 # my $node = Route::Node::get($self->{call});
1632 # my @rout = $node->del_nodes if $node;
1633 my @rout = $main::routeroot->del_node($call);
1634 dbg('route', "B/C PC21 (from PC39) for: " . join(',', (map{ $_->call } @rout))) if @rout;
1636 # unbusy and stop and outgoing mail
1637 my $mref = DXMsg::get_busy($call);
1638 $mref->stop_msg($call) if $mref;
1640 # broadcast to all other nodes that all the nodes connected to via me are gone
1641 unless ($pc39flag && $pc39flag == 2) {
1642 $self->route_pc21(@rout) if @rout;
1645 # remove outstanding pings
1646 delete $pings{$call};
1648 # I was the last node visited
1649 $self->user->node($main::mycall);
1651 # send info to all logged in thingies
1652 $self->tell_login('logoutn');
1654 Log('DXProt', $call . " Disconnected");
1656 $self->SUPER::disconnect;
1661 # send a talk message to this thingy
1665 my ($self, $from, $to, $via, $line) = @_;
1667 $line =~ s/\^/\\5E/g; # remove any ^ characters
1668 $self->send(DXProt::pc10($from, $to, $via, $line));
1669 Log('talk', $self->call, $from, $via?$via:$main::mycall, $line);
1672 # send it if it isn't the except list and isn't isolated and still has a hop count
1673 # taking into account filtering and so on
1677 my $generate = shift;
1678 my $no = shift; # the no of things to filter on
1680 my ($filter, $hops);
1683 if ($self->{routefilter}) {
1684 for (; @_ && $no; $no--) {
1686 ($filter, $hops) = $self->{routefilter}->it($self->{call}, $self->{dxcc}, $self->{itu}, $self->{cq}, $r->call, $r->dxcc, $r->itu, $r->cq);
1687 push @rin, $r if $filter;
1691 foreach my $line (&$generate(@rin, @_)) {
1694 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1696 $routeit = adjust_hops($self, $line); # adjust its hop count by node name
1697 next unless $routeit;
1699 $self->send($routeit);
1707 my $generate = shift;
1708 my @dxchan = DXChannel::get_all_nodes();
1712 foreach $dxchan (@dxchan) {
1713 next if $dxchan == $self;
1714 next if $dxchan == $me;
1715 $dxchan->send_route($generate, @_);
1722 broadcast_route($self, \&pc16, 1, @_);
1728 broadcast_route($self, \&pc17, 1, @_);
1734 broadcast_route($self, \&pc19, scalar @_, @_);
1740 broadcast_route($self, \&pc21, scalar @_, @_);