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(@_);
197 # this is how a pc connection starts (for an incoming connection)
198 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
199 # all the crap that comes between).
202 my ($self, $line, $sort) = @_;
203 my $call = $self->{call};
204 my $user = $self->{user};
206 # remember type of connection
207 $self->{consort} = $line;
208 $self->{outbound} = $sort eq 'O';
209 $self->{priv} = $user->priv || 1; # other clusters can always be 'normal' users
210 $self->{lang} = $user->lang || 'en';
211 $self->{isolate} = $user->{isolate};
212 $self->{consort} = $line; # save the connection type
215 # get the output filters
216 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
217 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
218 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
219 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
220 $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) ;
223 # get the INPUT filters (these only pertain to Clusters)
224 $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
225 $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
226 $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
227 $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
228 $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1);
230 # set unbuffered and no echo
231 $self->send_now('B',"0");
232 $self->send_now('E',"0");
234 # ping neighbour node stuff
235 my $ping = $user->pingint;
236 $ping = 5*60 unless defined $ping;
237 $self->{pingint} = $ping;
238 $self->{nopings} = $user->nopings || 2;
239 $self->{pingtime} = [ ];
240 $self->{pingave} = 0;
242 # send initialisation string
243 unless ($self->{outbound}) {
245 $self->{lastping} = $main::systime;
247 $self->{lastping} = $main::systime + ($self->pingint / 2);
249 $self->state('init');
250 $self->{pc50_t} = $main::systime;
252 # send info to all logged in thingies
253 $self->tell_login('loginn');
255 # add this node to the table, the values get filled in later
256 $main::routeroot->add($call);
258 Log('DXProt', "$call connected");
262 # This is the normal pcxx despatcher
266 my ($self, $line) = @_;
267 my @field = split /\^/, $line;
268 return unless @field;
270 pop @field if $field[-1] eq '~';
272 # print join(',', @field), "\n";
274 # ignore any lines that don't start with PC
275 return if !$field[0] =~ /^PC/;
278 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
280 return if $pcno < 10 || $pcno > 99;
282 # check for and dump bad protocol messages
283 my $n = check($pcno, @field);
285 dbg('chan', "PCPROT: bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")");
292 $pcr = Local::pcprot($self, $pcno, @field);
294 # dbg('local', "Local::pcprot error $@") if $@;
298 if ($pcno == 10) { # incoming talk
300 # will we allow it at all?
303 if (@bad = BadWords::check($field[3])) {
304 dbg('chan', "PCPROT: Bad words: @bad, dropped" );
309 # is it for me or one of mine?
310 my ($to, $via, $call, $dxchan);
311 if ($field[5] gt ' ') {
312 $call = $via = $field[2];
315 $call = $to = $field[2];
317 $dxchan = DXChannel->get($call);
318 if ($dxchan && $dxchan->is_user) {
319 $field[3] =~ s/\%5E/^/g;
320 $dxchan->talk($field[1], $to, $via, $field[3]);
322 $self->route($field[2], $line); # relay it on its way
327 if ($pcno == 11 || $pcno == 26) { # dx spot
329 # route 'foreign' pc26s
331 if ($field[7] ne $main::mycall) {
332 $self->route($field[7], $line);
337 # if this is a 'nodx' node then ignore it
338 if ($badnode->in($field[7])) {
339 dbg('chan', "PCPROT: Bad Node, dropped");
343 # if this is a 'bad spotter' user then ignore it
344 if ($badspotter->in($field[6])) {
345 dbg('chan', "PCPROT: Bad Spotter, dropped");
349 # convert the date to a unix date
350 my $d = cltounix($field[3], $field[4]);
351 # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
352 if (!$d || ($pcno == 11 && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
353 dbg('chan', "PCPROT: Spot ignored, invalid date or out of range ($field[3] $field[4])\n");
358 if ($baddx->in($field[2])) {
359 dbg('chan', "PCPROT: Bad DX spot, ignored");
364 $field[5] =~ s/^\s+//; # take any leading blanks off
365 $field[2] = unpad($field[2]); # take off leading and trailing blanks from spotted callsign
366 if ($field[2] =~ /BUST\w*$/) {
367 dbg('chan', "PCPROT: useless 'BUSTED' spot");
370 if (Spot::dup($field[1], $field[2], $d, $field[5])) {
371 dbg('chan', "PCPROT: Duplicate Spot ignored\n");
376 if (@bad = BadWords::check($field[5])) {
377 dbg('chan', "PCPROT: Bad words: @bad, dropped" );
382 my @spot = Spot::prepare($field[1], $field[2], $d, $field[5], $field[6], $field[7]);
383 # global spot filtering on INPUT
384 if ($self->{inspotsfilter}) {
385 my ($filter, $hops) = $self->{inspotsfilter}->it(@spot);
387 dbg('chan', "PCPROT: Rejected by filter");
396 # @spot at this point contains:-
397 # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
398 # then spotted itu, spotted cq, spotters itu, spotters cq
399 # you should be able to route on any of these
402 # fix up qra locators of known users
403 my $user = DXUser->get_current($spot[4]);
405 my $qra = $user->qra;
406 unless ($qra && DXBearing::is_qra($qra)) {
407 my $lat = $user->lat;
408 my $long = $user->long;
409 if (defined $lat && defined $long) {
410 $user->qra(DXBearing::lltoqra($lat, $long));
415 # send a remote command to a distant cluster if it is visible and there is no
416 # qra locator and we havn't done it for a month.
418 unless ($user->qra) {
420 my $to = $user->homenode;
421 my $last = $user->lastoper || 0;
422 if ($send_opernam && $main::systime > $last + $DXUser::lastoperinterval && $to && ($node = Route::Node::get($to)) ) {
423 my $cmd = "forward/opernam $spot[4]";
424 # send the rcmd but we aren't interested in the replies...
425 my $dxchan = $node->dxchan;
426 if ($dxchan && $dxchan->is_clx) {
427 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
429 route(undef, $to, pc34($main::mycall, $to, $cmd));
431 if ($to ne $field[7]) {
433 $node = Route::Node::get($to);
435 $dxchan = $node->dxchan;
436 if ($dxchan && $dxchan->is_clx) {
437 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
439 route(undef, $to, pc34($main::mycall, $to, $cmd));
443 $user->lastoper($main::systime);
452 $r = Local::spot($self, @spot);
454 # dbg('local', "Local::spot1 error $@") if $@;
457 # DON'T be silly and send on PC26s!
458 return if $pcno == 26;
460 # send out the filtered spots
461 send_dx_spot($self, $line, @spot) if @spot;
465 if ($pcno == 12) { # announces
466 # announce duplicate checking
467 $field[3] =~ s/^\s+//; # remove leading blanks
468 if (AnnTalk::dup($field[1], $field[2], $field[3])) {
469 dbg('chan', "PCPROT: Duplicate Announce ignored");
475 if (@bad = BadWords::check($field[3])) {
476 dbg('chan', "PCPROT: Bad words: @bad, dropped" );
481 if ($field[2] eq '*' || $field[2] eq $main::mycall) {
483 # global ann filtering on INPUT
484 if ($self->{inannfilter}) {
485 my ($ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
486 my @dxcc = Prefix::extract($field[1]);
488 $ann_dxcc = $dxcc[1]->dxcc;
489 $ann_itu = $dxcc[1]->itu;
490 $ann_cq = $dxcc[1]->cq();
492 @dxcc = Prefix::extract($field[5]);
494 $org_dxcc = $dxcc[1]->dxcc;
495 $org_itu = $dxcc[1]->itu;
496 $org_cq = $dxcc[1]->cq();
498 my ($filter, $hops) = $self->{inannfilter}->it(@field[1..6], $self->{call},
499 $ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq);
501 dbg('chan', "PCPROT: Rejected by filter");
507 $self->send_announce($line, @field[1..6]);
509 $self->route($field[2], $line);
525 if ($pcno == 16) { # add a user
529 my $newline = "PC16^";
531 if ($field[1] eq $main::mycall || $field[2] eq $main::mycall) {
532 dbg('chan', "PCPROT: trying to alter config on this node from outside!");
535 if ($field[2] eq $main::myalias && DXChannel->get($field[1])) {
536 dbg('chan', "PCPROT: trying to connect sysop from outside!");
539 if (($dxchan = DXChannel->get($field[1])) && $dxchan != $self) {
540 dbg('chan', "PCPROT: $field[1] connected locally");
544 my $node = Route::Node::get($field[1]);
546 dbg('chan', "PCPROT: Node $field[1] 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, $node->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($node->call) if !$user->homenode;
563 $user->node($node->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 $field[1] for: " . join(',', map{$_->call} @rout)) if @rout;
573 $self->route_pc16($node, @rout) if @rout;
577 if ($pcno == 17) { # remove a user
579 if ($field[1] eq $main::mycall || $field[2] eq $main::mycall) {
580 dbg('chan', "PCPROT: trying to alter config on this node from outside!");
583 if ($field[1] eq $main::myalias && DXChannel->get($field[1])) {
584 dbg('chan', "PCPROT: trying to disconnect sysop from outside!");
587 if ($dxchan = DXChannel->get($field[1])) {
588 dbg('chan', "PCPROT: $field[1] connected locally");
592 my $node = Route::Node::get($field[2]);
594 dbg('chan', "PCPROT: Route::Node $field[2] not in config");
597 my @rout = $node->del_user($field[1]);
598 dbg('route', "B/C PC17 on $field[2] for: $field[1]");
599 $self->route_pc17($node, @rout) if @rout;
603 if ($pcno == 18) { # link request
604 $self->state('init');
606 # first clear out any nodes on this dxchannel
607 my $node = Route::Node::get($self->{call});
610 my $r = Route::Node::get($_);
611 push @rout, $r->del_node if $r;
613 $self->route_pc21(@rout, $node);
614 $self->send_local_config();
616 return; # we don't pass these on
619 if ($pcno == 19) { # incoming cluster list
621 my $newline = "PC19^";
625 my $node = Route::Node::get($self->{call});
628 for ($i = 1; $i < $#field-1; $i += 4) {
629 my $here = $field[$i];
630 my $call = uc $field[$i+1];
631 my $conf = $field[$i+2];
632 my $ver = $field[$i+3];
633 next unless defined $here && defined $conf && is_callsign($call);
634 # check for sane parameters
635 $ver = 5000 if $ver eq '0000';
636 next if $ver < 5000; # only works with version 5 software
637 next if length $call < 3; # min 3 letter callsigns
639 # update it if required
640 if ($node->call eq $call && !$node->version) {
641 $node->version($ver);
642 $node->flags(Route::here($here)|Route::conf($conf));
644 } elsif ($node->call ne $call) {
645 next if $call eq $main::mycall || $call eq $self->{call};
647 my $r = $node->add($call, $ver, Route::here($here)|Route::conf($conf));
648 push @rout, $r if $r;
651 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
652 my $mref = DXMsg::get_busy($call);
653 $mref->stop_msg($call) if $mref;
655 # add this station to the user database, if required (don't remove SSID from nodes)
656 my $user = DXUser->get_current($call);
658 $user = DXUser->new($call);
660 $user->priv(1); # I have relented and defaulted nodes
661 $self->{priv} = 1; # to user RCMDs allowed
662 $user->homenode($call);
665 $user->lastin($main::systime) unless DXChannel->get($call);
669 dbg('route', "B/C PC19 for: " . join(',', map{$_->call} @rout)) if @rout;
671 $self->route_pc19(@rout) if @rout;
675 if ($pcno == 20) { # send local configuration
676 $self->send_local_config();
678 $self->state('normal');
682 if ($pcno == 21) { # delete a cluster from the list
683 my $call = uc $field[1];
685 my $node = Route::Node::get($call);
687 if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
688 if ($call eq $self->{call}) {
689 dbg('chan', "PCPROT: Trying to disconnect myself with PC21");
695 push @rout, $node->del_node($call);
697 dbg('chan', "PCPROT: Route::Node $call not in config");
700 dbg('chan', "PCPROT: I WILL _NOT_ be disconnected!");
703 dbg('route', "B/C PC21 for: " . join(',', (map{$_->call} @rout))) if @rout;
705 $self->route_pc21(@rout) if @rout;
710 $self->state('normal');
714 if ($pcno == 23 || $pcno == 27) { # WWV info
716 # route 'foreign' pc27s
718 if ($field[8] ne $main::mycall) {
719 $self->route($field[8], $line);
725 my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
726 my $sfi = unpad($field[3]);
727 my $k = unpad($field[4]);
728 my $i = unpad($field[5]);
729 my ($r) = $field[6] =~ /R=(\d+)/;
731 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
732 dbg('chan', "PCPROT: WWV Date ($field[1] $field[2]) out of range");
735 if (Geomag::dup($d,$sfi,$k,$i,$field[6])) {
736 dbg('chan', "PCPROT: Dup WWV Spot ignored\n");
739 $field[7] =~ s/-\d+$//o; # remove spotter's ssid
741 my $wwv = Geomag::update($d, $field[2], $sfi, $k, $i, @field[6..8], $r);
745 $rep = Local::wwv($self, $field[1], $field[2], $sfi, $k, $i, @field[6..8], $r);
747 # dbg('local', "Local::wwv2 error $@") if $@;
750 # DON'T be silly and send on PC27s!
751 return if $pcno == 27;
753 # broadcast to the eager world
754 send_wwv_spot($self, $line, $d, $field[2], $sfi, $k, $i, @field[6..8]);
758 if ($pcno == 24) { # set here status
759 my $call = uc $field[1];
760 my $ref = Route::Node::get($call);
761 $ref->here($field[2]) if $ref;
762 $ref = Route::User::get($call);
763 $ref->here($field[2]) if $ref;
767 if ($pcno == 25) { # merge request
768 if ($field[1] ne $main::mycall) {
769 $self->route($field[1], $line);
772 if ($field[2] eq $main::mycall) {
773 dbg('chan', "PCPROT: Trying to merge to myself, ignored");
777 Log('DXProt', "Merge request for $field[3] spots and $field[4] WWV from $field[1]");
781 my @in = reverse Spot::search(1, undef, undef, 0, $field[3]);
784 $self->send(pc26(@{$in}[0..4], $field[2]));
790 my @in = reverse Geomag::search(0, $field[4], time, 1);
793 $self->send(pc27(@{$in}[0..5], $field[2]));
799 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
800 if ($pcno == 49 || $field[1] eq $main::mycall) {
801 DXMsg::process($self, $line);
803 $self->route($field[1], $line) unless $self->is_clx;
808 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
809 if ($field[1] eq $main::mycall) {
810 my $ref = DXUser->get_current($field[2]);
811 my $cref = Route::Node::get($field[2]);
812 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
813 unless (!$cref || !$ref || $cref->call ne $ref->homenode) { # not allowed to relay RCMDS!
814 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
815 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
816 my $oldpriv = $self->{priv};
817 $self->{priv} = $ref->{priv}; # assume the user's privilege level
818 my @in = (DXCommandmode::run_cmd($self, $field[3]));
819 $self->{priv} = $oldpriv;
822 $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
823 Log('rcmd', 'out', $field[2], $_);
825 delete $self->{remotecmd};
827 $self->send(pc35($main::mycall, $field[2], "$main::mycall:sorry...!"));
830 $self->send(pc35($main::mycall, $field[2], "$main::mycall:your attempt is logged, Tut tut tut...!"));
833 my $ref = DXUser->get_current($field[1]);
834 if ($ref && $ref->is_clx) {
835 $self->route($field[1], pc84($field[2], $field[1], $field[2], $field[3]));
837 $self->route($field[1], $line);
843 if ($pcno == 35) { # remote command replies
844 if ($field[1] eq $main::mycall) {
845 my $s = $rcmds{$field[2]};
847 my $dxchan = DXChannel->get($s->{call});
848 $dxchan->send($field[3]) if $dxchan;
849 delete $rcmds{$field[2]} if !$dxchan;
851 # send unsolicited ones to the sysop
852 my $dxchan = DXChannel->get($main::myalias);
853 $dxchan->send($field[3]) if $dxchan;
856 my $ref = DXUser->get_current($field[1]);
857 if ($ref && $ref->is_clx) {
858 $self->route($field[1], pc85($field[2], $field[1], $field[2], $field[3]));
860 $self->route($field[1], $line);
866 # for pc 37 see 44 onwards
868 if ($pcno == 38) { # node connected list from neighbour
872 if ($pcno == 39) { # incoming disconnect
873 if ($field[1] eq $self->{call}) {
874 $self->disconnect(1);
876 dbg('chan', "PCPROT: came in on wrong channel");
881 if ($pcno == 41) { # user info
882 # add this station to the user database, if required
883 my $user = DXUser->get_current($field[1]);
884 $user = DXUser->new($field[1]) if !$user;
886 if ($field[2] == 1) {
887 $user->name($field[3]);
888 } elsif ($field[2] == 2) {
889 $user->qth($field[3]);
890 } elsif ($field[2] == 3) {
891 my ($lat, $long) = DXBearing::stoll($field[3]);
894 $user->qra(DXBearing::lltoqra($lat, $long)) unless $user->qra && DXBearing::is_qra($user->qra);
895 } elsif ($field[2] == 4) {
896 $user->homenode($field[3]);
898 $user->lastoper($main::systime); # to cut down on excessive for/opers being generated
905 if ($pcno == 37 || $pcno == 44 || $pcno == 45 || $pcno == 46 || $pcno == 47 || $pcno == 48) {
906 DXDb::process($self, $line);
910 if ($pcno == 50) { # keep alive/user list
911 my $node = Route::Node::get($field[1]);
913 return unless $node->call eq $self->{call};
914 $node->usercount($field[2]);
919 if ($pcno == 51) { # incoming ping requests/answers
922 if ($field[1] eq $main::mycall) {
923 my $flag = $field[3];
925 $self->send(pc51($field[2], $field[1], '0'));
927 # it's a reply, look in the ping list for this one
928 my $ref = $pings{$field[2]};
930 my $tochan = DXChannel->get($field[2]);
933 my $dxchan = DXChannel->get($r->{call});
935 my $t = tv_interval($r->{t}, [ gettimeofday ]);
936 if ($dxchan->is_user) {
937 my $s = sprintf "%.2f", $t;
938 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
939 $dxchan->send($dxchan->msg('pingi', $field[2], $s, $ave))
940 } elsif ($dxchan->is_node) {
942 $tochan->{nopings} = $tochan->user->nopings || 2; # pump up the timer
943 push @{$tochan->{pingtime}}, $t;
944 shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
946 for (@{$tochan->{pingtime}}) {
949 $tochan->{pingave} = $st / @{$tochan->{pingtime}};
956 # route down an appropriate thingy
957 $self->route($field[1], $line);
962 if ($pcno == 75) { # dunno but route it
963 if ($field[1] ne $main::mycall) {
964 $self->route($field[1], $line);
969 if ($pcno == 73) { # WCY broadcasts
972 my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
973 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
974 dbg('chan', "PCPROT: WCY Date ($field[1] $field[2]) out of range");
977 @field = map { unpad($_) } @field;
978 if (WCY::dup($d,@field[3..7])) {
979 dbg('chan', "PCPROT: Dup WCY Spot ignored\n");
983 my $wcy = WCY::update($d, @field[2..12]);
987 $rep = Local::wwv($self, @field[1..12]);
989 # dbg('local', "Local::wcy error $@") if $@;
992 # broadcast to the eager world
993 send_wcy_spot($self, $line, $d, @field[2..12]);
997 if ($pcno == 84) { # remote commands (incoming)
998 if ($field[1] eq $main::mycall) {
999 my $ref = DXUser->get_current($field[2]);
1000 my $cref = Route::Node::get($field[2]);
1001 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[4]);
1002 unless ($field[4] =~ /rcmd/i || !$cref || !$ref || $cref->call ne $ref->homenode) { # not allowed to relay RCMDS!
1003 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
1004 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
1005 my $oldpriv = $self->{priv};
1006 $self->{priv} = $ref->{priv}; # assume the user's privilege level
1007 my @in = (DXCommandmode::run_cmd($self, $field[4]));
1008 $self->{priv} = $oldpriv;
1011 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:$_"));
1012 Log('rcmd', 'out', $field[2], $_);
1014 delete $self->{remotecmd};
1016 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:sorry...!"));
1019 $self->send(pc85($main::mycall, $field[2], $field[3],"$main::mycall:your attempt is logged, Tut tut tut...!"));
1022 my $ref = DXUser->get_current($field[1]);
1023 if ($ref && $ref->is_clx) {
1024 $self->route($field[1], $line);
1026 $self->route($field[1], pc34($field[2], $field[1], $field[4]));
1032 if ($pcno == 85) { # remote command replies
1033 if ($field[1] eq $main::mycall) {
1034 my $dxchan = DXChannel->get($field[3]);
1036 $dxchan->send($field[4]);
1038 my $s = $rcmds{$field[2]};
1040 $dxchan = DXChannel->get($s->{call});
1041 $dxchan->send($field[4]) if $dxchan;
1042 delete $rcmds{$field[2]} if !$dxchan;
1044 # send unsolicited ones to the sysop
1045 my $dxchan = DXChannel->get($main::myalias);
1046 $dxchan->send($field[4]) if $dxchan;
1050 my $ref = DXUser->get_current($field[1]);
1051 if ($ref && $ref->is_clx) {
1052 $self->route($field[1], $line);
1054 $self->route($field[1], pc35($field[2], $field[1], $field[4]));
1061 # if get here then rebroadcast the thing with its Hop count decremented (if
1062 # there is one). If it has a hop count and it decrements to zero then don't
1065 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
1069 unless ($self->{isolate}) {
1070 broadcast_ak1a($line, $self); # send it to everyone but me
1075 # This is called from inside the main cluster processing loop and is used
1076 # for despatching commands that are doing some long processing job
1081 my @dxchan = DXChannel->get_all();
1084 foreach $dxchan (@dxchan) {
1085 next unless $dxchan->is_node();
1086 next if $dxchan == $me;
1088 # send a pc50 out on this channel
1089 $dxchan->{pc50_t} = $main::systime unless exists $dxchan->{pc50_t};
1090 if ($t >= $dxchan->{pc50_t} + $DXProt::pc50_interval) {
1091 $dxchan->send(pc50(scalar DXChannel::get_all_users));
1092 $dxchan->{pc50_t} = $t;
1095 # send a ping out on this channel
1096 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
1097 if ($dxchan->{nopings} <= 0) {
1098 $dxchan->disconnect;
1100 addping($main::mycall, $dxchan->call);
1101 $dxchan->{nopings} -= 1;
1102 $dxchan->{lastping} = $t;
1110 if ($main::systime - 3600 > $last_hour) {
1114 $last_hour = $main::systime;
1119 # finish up a pc context
1123 # some active measures
1130 my @dxchan = DXChannel->get_all();
1133 # send it if it isn't the except list and isn't isolated and still has a hop count
1134 # taking into account filtering and so on
1135 foreach $dxchan (@dxchan) {
1136 next if $dxchan == $me;
1138 my ($filter, $hops);
1140 if ($dxchan->{spotsfilter}) {
1141 ($filter, $hops) = $dxchan->{spotsfilter}->it(@_, $self->{call} );
1142 next unless $filter;
1145 if ($dxchan->is_node) {
1146 next if $dxchan == $self;
1149 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1151 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1152 next unless $routeit;
1155 $dxchan->send($routeit) if $routeit;
1157 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1159 } elsif ($dxchan->is_user && $dxchan->{dx}) {
1160 my $buf = Spot::formatb($dxchan->{user}->wantgrid, $_[0], $_[1], $_[2], $_[3], $_[4]);
1161 $buf .= "\a\a" if $dxchan->{beep};
1163 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1164 $dxchan->send($buf);
1166 $dxchan->delay($buf);
1176 my @dxchan = DXChannel->get_all();
1178 my ($wwv_dxcc, $wwv_itu, $wwv_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1179 my @dxcc = Prefix::extract($_[7]);
1181 $wwv_dxcc = $dxcc[1]->dxcc;
1182 $wwv_itu = $dxcc[1]->itu;
1183 $wwv_cq = $dxcc[1]->cq;
1185 @dxcc = Prefix::extract($_[8]);
1187 $org_dxcc = $dxcc[1]->dxcc;
1188 $org_itu = $dxcc[1]->itu;
1189 $org_cq = $dxcc[1]->cq;
1192 # send it if it isn't the except list and isn't isolated and still has a hop count
1193 # taking into account filtering and so on
1194 foreach $dxchan (@dxchan) {
1195 next if $dxchan == $self;
1196 next if $dxchan == $me;
1198 my ($filter, $hops);
1200 if ($dxchan->{wwvfilter}) {
1201 ($filter, $hops) = $dxchan->{wwvfilter}->it(@_, $self->{call}, $wwv_dxcc, $wwv_itu, $wwv_cq, $org_dxcc, $org_itu, $org_cq);
1202 next unless $filter;
1204 if ($dxchan->is_node) {
1207 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1209 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1210 next unless $routeit;
1213 $dxchan->send($routeit) if $routeit;
1215 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1218 } elsif ($dxchan->is_user && $dxchan->{wwv}) {
1219 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
1220 $buf .= "\a\a" if $dxchan->{beep};
1221 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1222 $dxchan->send($buf);
1224 $dxchan->delay($buf);
1234 my @dxchan = DXChannel->get_all();
1236 my ($wcy_dxcc, $wcy_itu, $wcy_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1237 my @dxcc = Prefix::extract($_[11]);
1239 $wcy_dxcc = $dxcc[1]->dxcc;
1240 $wcy_itu = $dxcc[1]->itu;
1241 $wcy_cq = $dxcc[1]->cq;
1243 @dxcc = Prefix::extract($_[12]);
1245 $org_dxcc = $dxcc[1]->dxcc;
1246 $org_itu = $dxcc[1]->itu;
1247 $org_cq = $dxcc[1]->cq;
1250 # send it if it isn't the except list and isn't isolated and still has a hop count
1251 # taking into account filtering and so on
1252 foreach $dxchan (@dxchan) {
1253 next if $dxchan == $me;
1255 my ($filter, $hops);
1257 if ($dxchan->{wcyfilter}) {
1258 ($filter, $hops) = $dxchan->{wcyfilter}->it(@_, $self->{call}, $wcy_dxcc, $wcy_itu, $wcy_cq, $org_dxcc, $org_itu, $org_cq);
1259 next unless $filter;
1261 if ($dxchan->is_clx || $dxchan->is_spider || $dxchan->is_dxnet) {
1264 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1266 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1267 next unless $routeit;
1270 $dxchan->send($routeit) if $routeit;
1272 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1274 } elsif ($dxchan->is_user && $dxchan->{wcy}) {
1275 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1276 $buf .= "\a\a" if $dxchan->{beep};
1277 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1278 $dxchan->send($buf);
1280 $dxchan->delay($buf);
1291 my @dxchan = DXChannel->get_all();
1293 my $text = unpad($_[2]);
1297 if ($_[3] eq '*') { # sysops
1299 } elsif ($_[3] gt ' ') { # speciality list handling
1300 my ($name) = split /\./, $_[3];
1301 $target = "$name"; # put the rest in later (if bothered)
1308 $target = "ALL" if !$target;
1310 Log('ann', $target, $_[0], $text);
1312 # obtain country codes etc
1313 my ($ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1314 my @dxcc = Prefix::extract($_[0]);
1316 $ann_dxcc = $dxcc[1]->dxcc;
1317 $ann_itu = $dxcc[1]->itu;
1318 $ann_cq = $dxcc[1]->cq;
1320 @dxcc = Prefix::extract($_[4]);
1322 $org_dxcc = $dxcc[1]->dxcc;
1323 $org_itu = $dxcc[1]->itu;
1324 $org_cq = $dxcc[1]->cq;
1327 # send it if it isn't the except list and isn't isolated and still has a hop count
1328 # taking into account filtering and so on
1329 foreach $dxchan (@dxchan) {
1330 next if $dxchan == $self;
1331 next if $dxchan == $me;
1333 my ($filter, $hops);
1335 if ($dxchan->{annfilter}) {
1336 ($filter, $hops) = $dxchan->{annfilter}->it(@_, $self->{call}, $ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq);
1337 next unless $filter;
1339 if ($dxchan->is_node && $_[1] ne $main::mycall) { # i.e not specifically routed to me
1342 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1344 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1345 next unless $routeit;
1348 $dxchan->send($routeit) if $routeit;
1350 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1353 } elsif ($dxchan->is_user) {
1354 unless ($dxchan->{ann}) {
1355 next if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
1357 next if $target eq 'SYSOP' && $dxchan->{priv} < 5;
1358 my $buf = "$to$target de $_[0]: $text";
1360 $buf .= "\a\a" if $dxchan->{beep};
1361 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1362 $dxchan->send($buf);
1364 $dxchan->delay($buf);
1370 sub send_local_config
1379 if ($self->{isolate}) {
1380 @localcalls = ( $main::mycall );
1382 # create a list of all the nodes that are not connected to this connection
1383 # and are not themselves isolated, this to make sure that isolated nodes
1384 # don't appear outside of this node
1385 my @dxchan = grep { $_->call ne $main::mycall && $_->call ne $self->{call} && !$_->{isolate} } DXChannel::get_all_nodes();
1386 @localcalls = map { $_->{call} } @dxchan if @dxchan;
1387 @remotecalls = map {my $r = Route::Node::get($_); $r ? $r->rnodes(@localcalls, $main::mycall, $self->{call}) : () } @localcalls if @localcalls;
1388 unshift @localcalls, $main::mycall;
1390 @nodes = map {my $r = Route::Node::get($_); $r ? $r : ()} (@localcalls, @remotecalls);
1392 send_route($self, \&pc19, scalar @nodes, @nodes);
1394 # get all the users connected on the above nodes and send them out
1395 foreach $n (@nodes) {
1396 send_route($self, \&pc16, 1, $n, map {my $r = Route::User::get($_); $r ? ($r) : ()} $n->users);
1401 # route a message down an appropriate interface for a callsign
1403 # is called route(to, pcline);
1407 my ($self, $call, $line) = @_;
1409 if (ref $self && $call eq $self->{call}) {
1410 dbg('chan', "PCPROT: Trying to route back to source, dropped");
1414 # always send it down the local interface if available
1415 my $dxchan = DXChannel->get($call);
1417 my $cl = Route::Node::get($call);
1418 $dxchan = $cl->dxchan if $cl;
1420 if (ref $self && $dxchan eq $self) {
1421 dbg('chan', "PCPROT: Trying to route back to source, dropped");
1427 my $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1429 $dxchan->send($routeit);
1432 dbg('chan', "PCPROT: No route available, dropped");
1436 # broadcast a message to all clusters taking into account isolation
1437 # [except those mentioned after buffer]
1440 my $s = shift; # the line to be rebroadcast
1441 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1442 my @dxchan = DXChannel::get_all_nodes();
1445 # send it if it isn't the except list and isn't isolated and still has a hop count
1446 foreach $dxchan (@dxchan) {
1447 next if grep $dxchan == $_, @except;
1448 next if $dxchan == $me;
1450 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
1451 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
1455 # broadcast a message to all clusters ignoring isolation
1456 # [except those mentioned after buffer]
1457 sub broadcast_all_ak1a
1459 my $s = shift; # the line to be rebroadcast
1460 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1461 my @dxchan = DXChannel::get_all_nodes();
1464 # send it if it isn't the except list and isn't isolated and still has a hop count
1465 foreach $dxchan (@dxchan) {
1466 next if grep $dxchan == $_, @except;
1467 next if $dxchan == $me;
1469 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
1470 $dxchan->send($routeit);
1474 # broadcast to all users
1475 # storing the spot or whatever until it is in a state to receive it
1478 my $s = shift; # the line to be rebroadcast
1479 my $sort = shift; # the type of transmission
1480 my $fref = shift; # a reference to an object to filter on
1481 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1482 my @dxchan = DXChannel::get_all_users();
1486 foreach $dxchan (@dxchan) {
1487 next if grep $dxchan == $_, @except;
1490 broadcast_list($s, $sort, $fref, @out);
1493 # broadcast to a list of users
1501 foreach $dxchan (@_) {
1503 next if $dxchan == $me;
1505 if ($sort eq 'dx') {
1506 next unless $dxchan->{dx};
1507 ($filter) = $dxchan->{spotsfilter}->it(@{$fref}) if ref $fref;
1508 next unless $filter;
1510 next if $sort eq 'ann' && !$dxchan->{ann};
1511 next if $sort eq 'wwv' && !$dxchan->{wwv};
1512 next if $sort eq 'wcy' && !$dxchan->{wcy};
1513 next if $sort eq 'wx' && !$dxchan->{wx};
1515 $s =~ s/\a//og unless $dxchan->{beep};
1517 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1527 # obtain the hops from the list for this callsign and pc no
1533 my $hops = $DXProt::hopcount{$pcno};
1534 $hops = $DXProt::def_hopcount if !$hops;
1539 # adjust the hop count on a per node basis using the user loadable
1540 # hop table if available or else decrement an existing one
1547 my $call = $self->{call};
1550 if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
1551 my ($pcno) = $s =~ /^PC(\d\d)/o;
1552 confess "$call called adjust_hops with '$s'" unless $pcno;
1553 my $ref = $nodehops{$call} if %nodehops;
1555 my $newhops = $ref->{$pcno};
1556 return "" if defined $newhops && $newhops == 0;
1557 $newhops = $ref->{default} unless $newhops;
1558 return "" if defined $newhops && $newhops == 0;
1559 $newhops = $hops if !$newhops;
1560 $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
1562 # simply decrement it
1564 return "" if !$hops;
1565 $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
1577 return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1578 do "$main::data/hop_table.pl";
1584 # add a ping request to the ping queues
1587 my ($from, $to) = @_;
1588 my $ref = $pings{$to} || [];
1591 $r->{t} = [ gettimeofday ];
1592 route(undef, $to, pc51($to, $main::mycall, 1));
1597 # add a rcmd request to the rcmd queues
1600 my ($self, $to, $cmd) = @_;
1603 $r->{call} = $self->{call};
1604 $r->{t} = $main::systime;
1608 my $ref = Route::Node::get($to);
1609 my $dxchan = $ref->dxchan;
1610 if ($dxchan && $dxchan->is_clx) {
1611 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1613 route(undef, $to, pc34($main::mycall, $to, $cmd));
1620 my $pc39flag = shift;
1621 my $call = $self->call;
1623 unless ($pc39flag && $pc39flag == 1) {
1624 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1628 # my $node = Route::Node::get($self->{call});
1629 # my @rout = $node->del_nodes if $node;
1630 my @rout = $main::routeroot->del_node($call);
1631 dbg('route', "B/C PC21 (from PC39) for: " . join(',', (map{ $_->call } @rout))) if @rout;
1633 # unbusy and stop and outgoing mail
1634 my $mref = DXMsg::get_busy($call);
1635 $mref->stop_msg($call) if $mref;
1637 # broadcast to all other nodes that all the nodes connected to via me are gone
1638 unless ($pc39flag && $pc39flag == 2) {
1639 $self->route_pc21(@rout) if @rout;
1642 # remove outstanding pings
1643 delete $pings{$call};
1645 # I was the last node visited
1646 $self->user->node($main::mycall);
1648 # send info to all logged in thingies
1649 $self->tell_login('logoutn');
1651 Log('DXProt', $call . " Disconnected");
1653 $self->SUPER::disconnect;
1658 # send a talk message to this thingy
1662 my ($self, $from, $to, $via, $line) = @_;
1664 $line =~ s/\^/\\5E/g; # remove any ^ characters
1665 $self->send(DXProt::pc10($from, $to, $via, $line));
1666 Log('talk', $self->call, $from, $via?$via:$main::mycall, $line);
1669 # send it if it isn't the except list and isn't isolated and still has a hop count
1670 # taking into account filtering and so on
1674 my $generate = shift;
1675 my $no = shift; # the no of things to filter on
1677 my ($filter, $hops);
1680 if ($self->{routefilter}) {
1681 for (; @_ && $no; $no--) {
1683 ($filter, $hops) = $self->{routefilter}->it($self->{call}, $self->{dxcc}, $self->{itu}, $self->{cq}, $r->call, $r->dxcc, $r->itu, $r->cq);
1684 push @rin, $r if $filter;
1688 foreach my $line (&$generate(@rin, @_)) {
1691 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1693 $routeit = adjust_hops($self, $line); # adjust its hop count by node name
1694 next unless $routeit;
1696 $self->send($routeit);
1704 my $generate = shift;
1705 my @dxchan = DXChannel::get_all_nodes();
1709 foreach $dxchan (@dxchan) {
1710 next if $dxchan == $self;
1711 next if $dxchan == $me;
1712 $dxchan->send_route($generate, @_);
1719 broadcast_route($self, \&pc16, 1, @_);
1725 broadcast_route($self, \&pc17, 1, @_);
1731 broadcast_route($self, \&pc19, scalar @_, @_);
1737 broadcast_route($self, \&pc21, scalar @_, @_);