3 # This module impliments the protocal mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
31 use Time::HiRes qw(gettimeofday tv_interval);
36 use vars qw($me $pc11_max_age $pc23_max_age
37 $last_hour %pings %rcmds
38 %nodehops $baddx $badspotter $badnode $censorpc
39 $allowzero $decode_dk0wcy $send_opernam @checklist);
41 $me = undef; # the channel id for this cluster
42 $pc11_max_age = 1*3600; # the maximum age for an incoming 'real-time' pc11
43 $pc23_max_age = 1*3600; # the maximum age for an incoming 'real-time' pc23
45 $last_hour = time; # last time I did an hourly periodic update
46 %pings = (); # outstanding ping requests outbound
47 %rcmds = (); # outstanding rcmd requests outbound
48 %nodehops = (); # node specific hop control
49 $censorpc = 1; # Do a BadWords::check on text fields and reject things
50 # loads of 'bad things'
51 $baddx = new DXHash "baddx";
52 $badspotter = new DXHash "badspotter";
53 $badnode = new DXHash "badnode";
57 [ qw(c c m bp bc c) ], # pc10
58 [ qw(f m d t m c c h) ], # pc11
59 [ qw(c bc m bp bm p h) ], # pc12
63 undef , # pc16 has to be validated manually
66 undef , # pc19 has to be validated manually
67 undef , # pc20 no validation
69 undef , # pc22 no validation
70 [ qw(d n n n n m c c h) ], # pc23
72 [ qw(c c n n) ], # pc25
73 [ qw(f m d t m c c bc) ], # pc26
74 [ qw(d n n n n m c c bc) ], # pc27
75 [ qw(c c m c d t p m bp n p bp bc) ], # pc28
76 [ qw(c c n m) ], # pc29
84 [ qw(c c n m) ], # pc37
85 undef, # pc38 not interested
87 [ qw(c c m p n) ], # pc40
88 [ qw(c n m h) ], # pc41
90 undef, # pc43 don't handle it
91 [ qw(c c n m m c) ], # pc44
92 [ qw(c c n m) ], # pc45
120 [ qw(d n n n n n n m m m c c h) ], # pc73
131 [ qw(c c c m) ], # pc84
132 [ qw(c c c m) ], # pc85
135 # use the entry in the check list to check the field list presented
136 # return OK if line NOT in check list (for now)
141 return 0 if $n < 0 || $n > @checklist;
142 my $ref = $checklist[$n];
143 return 0 unless ref $ref;
146 shift; # not interested in the first field
147 for ($i = 0; $i < @$ref; $i++) {
148 my ($blank, $act) = $$ref[$i] =~ /^(b?)(\w)$/;
149 return 0 unless $act;
150 next if $blank && $_[$i] =~ /^[ \*]$/;
152 return $i+1 unless is_callsign($_[$i]);
153 } elsif ($act eq 'm') {
154 return $i+1 unless is_pctext($_[$i]);
155 } elsif ($act eq 'p') {
156 return $i+1 unless is_pcflag($_[$i]);
157 } elsif ($act eq 'f') {
158 return $i+1 unless is_freq($_[$i]);
159 } elsif ($act eq 'n') {
160 return $i+1 unless $_[$i] =~ /^[\d ]+$/;
161 } elsif ($act eq 'h') {
162 return $i+1 unless $_[$i] =~ /^H\d\d?$/;
163 } elsif ($act eq 'd') {
164 return $i+1 unless $_[$i] =~ /^\s*\d+-\w\w\w-[12][90]\d\d$/;
165 } elsif ($act eq 't') {
166 return $i+1 unless $_[$i] =~ /^[012]\d[012345]\dZ$/;
174 my $user = DXUser->get($main::mycall);
175 $DXProt::myprot_version += $main::version*100;
176 $me = DXProt->new($main::mycall, 0, $user);
178 $me->{state} = "indifferent";
179 do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
181 $me->{sort} = 'S'; # S for spider
185 # obtain a new connection this is derived from dxchannel
190 my $self = DXChannel::alloc(@_);
194 # this is how a pc connection starts (for an incoming connection)
195 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
196 # all the crap that comes between).
199 my ($self, $line, $sort) = @_;
200 my $call = $self->{call};
201 my $user = $self->{user};
203 # remember type of connection
204 $self->{consort} = $line;
205 $self->{outbound} = $sort eq 'O';
206 $self->{priv} = $user->priv || 1; # other clusters can always be 'normal' users
207 $self->{lang} = $user->lang || 'en';
208 $self->{isolate} = $user->{isolate};
209 $self->{consort} = $line; # save the connection type
212 # get the output filters
213 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
214 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
215 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
216 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
219 # get the INPUT filters (these only pertain to Clusters)
220 $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
221 $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
222 $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
223 $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
225 # set unbuffered and no echo
226 $self->send_now('B',"0");
227 $self->send_now('E',"0");
229 # ping neighbour node stuff
230 my $ping = $user->pingint;
231 $ping = 5*60 unless defined $ping;
232 $self->{pingint} = $ping;
233 $self->{nopings} = $user->nopings || 2;
234 $self->{pingtime} = [ ];
235 $self->{pingave} = 0;
237 # send initialisation string
238 unless ($self->{outbound}) {
240 $self->{lastping} = $main::systime;
242 $self->{lastping} = $main::systime + ($self->pingint / 2);
244 $self->state('init');
245 $self->{pc50_t} = $main::systime;
247 # send info to all logged in thingies
248 $self->tell_login('loginn');
250 Log('DXProt', "$call connected");
254 # This is the normal pcxx despatcher
258 my ($self, $line) = @_;
259 my @field = split /\^/, $line;
260 return unless @field;
262 pop @field if $field[-1] eq '~';
264 # print join(',', @field), "\n";
266 # ignore any lines that don't start with PC
267 return if !$field[0] =~ /^PC/;
270 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
272 return if $pcno < 10 || $pcno > 99;
274 # check for and dump bad protocol messages
275 my $n = check($pcno, @field);
277 dbg('chan', "PCPROT: bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")");
284 $pcr = Local::pcprot($self, $pcno, @field);
286 # dbg('local', "Local::pcprot error $@") if $@;
290 if ($pcno == 10) { # incoming talk
292 # will we allow it at all?
295 if (@bad = BadWords::check($field[3])) {
296 dbg('chan', "PCPROT: Bad words: @bad, dropped" );
301 # is it for me or one of mine?
302 my ($to, $via, $call, $dxchan);
303 if ($field[5] gt ' ') {
304 $call = $via = $field[2];
307 $call = $to = $field[2];
309 $dxchan = DXChannel->get($call);
310 if ($dxchan && $dxchan->is_user) {
311 $field[3] =~ s/\%5E/^/g;
312 $dxchan->talk($field[1], $to, $via, $field[3]);
314 $self->route($field[2], $line); # relay it on its way
319 if ($pcno == 11 || $pcno == 26) { # dx spot
321 # route 'foreign' pc26s
323 if ($field[7] ne $main::mycall) {
324 $self->route($field[7], $line);
329 # if this is a 'nodx' node then ignore it
330 if ($badnode->in($field[7])) {
331 dbg('chan', "PCPROT: Bad Node, dropped");
335 # if this is a 'bad spotter' user then ignore it
336 if ($badspotter->in($field[6])) {
337 dbg('chan', "PCPROT: Bad Spotter, dropped");
341 # convert the date to a unix date
342 my $d = cltounix($field[3], $field[4]);
343 # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
344 if (!$d || ($pcno == 11 && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
345 dbg('chan', "PCPROT: Spot ignored, invalid date or out of range ($field[3] $field[4])\n");
350 if ($baddx->in($field[2])) {
351 dbg('chan', "PCPROT: Bad DX spot, ignored");
356 $field[5] =~ s/^\s+//; # take any leading blanks off
357 $field[2] = unpad($field[2]); # take off leading and trailing blanks from spotted callsign
358 if ($field[2] =~ /BUST\w*$/) {
359 dbg('chan', "PCPROT: useless 'BUSTED' spot");
362 if (Spot::dup($field[1], $field[2], $d, $field[5])) {
363 dbg('chan', "PCPROT: Duplicate Spot ignored\n");
368 if (@bad = BadWords::check($field[5])) {
369 dbg('chan', "PCPROT: Bad words: @bad, dropped" );
374 my @spot = Spot::add($field[1], $field[2], $d, $field[5], $field[6], $field[7]);
377 # @spot at this point contains:-
378 # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
379 # then spotted itu, spotted cq, spotters itu, spotters cq
380 # you should be able to route on any of these
383 # fix up qra locators of known users
384 my $user = DXUser->get_current($spot[4]);
386 my $qra = $user->qra;
387 unless ($qra && DXBearing::is_qra($qra)) {
388 my $lat = $user->lat;
389 my $long = $user->long;
390 if (defined $lat && defined $long) {
391 $user->qra(DXBearing::lltoqra($lat, $long));
396 # send a remote command to a distant cluster if it is visible and there is no
397 # qra locator and we havn't done it for a month.
399 unless ($user->qra) {
401 my $to = $user->homenode;
402 my $last = $user->lastoper || 0;
403 if ($send_opernam && $main::systime > $last + $DXUser::lastoperinterval && $to && ($node = DXCluster->get_exact($to)) ) {
404 my $cmd = "forward/opernam $spot[4]";
405 # send the rcmd but we aren't interested in the replies...
406 if ($node && $node->dxchan && $node->dxchan->is_clx) {
407 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
409 route(undef, $to, pc34($main::mycall, $to, $cmd));
411 if ($to ne $field[7]) {
413 $node = DXCluster->get_exact($to);
414 if ($node && $node->dxchan && $node->dxchan->is_clx) {
415 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
417 route(undef, $to, pc34($main::mycall, $to, $cmd));
420 $user->lastoper($main::systime);
429 $r = Local::spot($self, @spot);
431 # dbg('local', "Local::spot1 error $@") if $@;
434 # DON'T be silly and send on PC26s!
435 return if $pcno == 26;
437 # send out the filtered spots
438 send_dx_spot($self, $line, @spot) if @spot;
442 if ($pcno == 12) { # announces
443 # announce duplicate checking
444 $field[3] =~ s/^\s+//; # remove leading blanks
445 if (AnnTalk::dup($field[1], $field[2], $field[3])) {
446 dbg('chan', "PCPROT: Duplicate Announce ignored");
452 if (@bad = BadWords::check($field[3])) {
453 dbg('chan', "PCPROT: Bad words: @bad, dropped" );
458 if ($field[2] eq '*' || $field[2] eq $main::mycall) {
460 # global ann filtering on INPUT
461 if ($self->{inannfilter}) {
462 my ($ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
463 my @dxcc = Prefix::extract($field[1]);
465 $ann_dxcc = $dxcc[1]->dxcc;
466 $ann_itu = $dxcc[1]->itu;
467 $ann_cq = $dxcc[1]->cq();
469 @dxcc = Prefix::extract($field[5]);
471 $org_dxcc = $dxcc[1]->dxcc;
472 $org_itu = $dxcc[1]->itu;
473 $org_cq = $dxcc[1]->cq();
475 my ($filter, $hops) = $self->{inannfilter}->it(@field[1..6], $self->{call},
476 $ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq);
478 dbg('chan', "PCPROT: Rejected by filter");
484 $self->send_announce($line, @field[1..6]);
486 $self->route($field[2], $line);
502 if ($pcno == 16) { # add a user
503 my $node = DXCluster->get_exact($field[1]);
505 if (!$node && ($dxchan = DXChannel->get($field[1]))) {
506 # add it to the node table if it isn't present and it's
508 $node = DXNode->new($dxchan, $field[1], 0, 1, 5400);
509 dbg('chan', "PCPROT: $field[1] no PC19 yet, autovivified as node");
510 # broadcast_ak1a(pc19($dxchan, $node), $dxchan, $self) unless $dxchan->{isolate};
513 if ($field[1] eq $main::mycall || $field[2] eq $main::mycall) {
514 dbg('chan', "PCPROT: trying to alter config on this node from outside!");
517 if ($field[2] eq $main::myalias && DXChannel->get($field[1])) {
518 dbg('chan', "PCPROT: trying to connect sysop from outside!");
522 dbg('chan', "PCPROT: Node $field[1] not in config");
525 unless ($node->isa('DXNode')) {
526 dbg('chan', "PCPROT: $field[1] is not a node");
529 if ($node->dxchan != $self) {
530 dbg('chan', "PCPROT: $field[1] came in on wrong channel");
533 if (($dxchan = DXChannel->get($field[1])) && $dxchan != $self) {
534 dbg('chan', "PCPROT: $field[1] connected locally");
539 for ($i = 2; $i < $#field; $i++) {
540 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (\S) (\d)/o;
541 next unless $call && $confmode && defined $here && is_callsign($call);
542 my $ref = DXCluster->get_exact($call);
544 if ($ref->isa('DXNode')) {
545 dbg('chan', "PCPROT: $call is a node");
548 my $rcall = $ref->mynode->call;
549 dbg('chan', "PCPROT: already have $call on $rcall");
553 $confmode = $confmode eq '*';
554 DXNodeuser->new($self, $node, $call, $confmode, $here);
556 # add this station to the user database, if required
557 $call =~ s/-\d+$//o; # remove ssid for users
558 my $user = DXUser->get_current($call);
559 $user = DXUser->new($call) if !$user;
560 $user->homenode($node->call) if !$user->homenode;
561 $user->node($node->call);
562 $user->lastin($main::systime) unless DXChannel->get($call);
566 # queue up any messages (look for privates only)
567 DXMsg::queue_msg(1) if $self->state eq 'normal';
568 # broadcast_route($line, $self, $field[1]);
573 if ($pcno == 17) { # remove a user
574 my $node = DXCluster->get_exact($field[2]);
576 if (!$node && ($dxchan = DXChannel->get($field[2]))) {
577 # add it to the node table if it isn't present and it's
579 $node = DXNode->new($dxchan, $field[2], 0, 1, 5400);
580 dbg('chan', "PCPROT: $field[2] no PC19 yet, autovivified as node");
581 # broadcast_ak1a(pc19($dxchan, $node), $dxchan, $self) unless $dxchan->{isolate};
583 if ($field[1] eq $main::mycall || $field[2] eq $main::mycall) {
584 dbg('chan', "PCPROT: trying to alter config on this node from outside!");
587 if ($field[1] eq $main::myalias && DXChannel->get($field[1])) {
588 dbg('chan', "PCPROT: trying to disconnect sysop from outside!");
592 dbg('chan', "PCPROT: Node $field[2] not in config");
595 unless ($node->isa('DXNode')) {
596 dbg('chan', "PCPROT: $field[2] is not a node");
599 if ($node->dxchan != $self) {
600 dbg('chan', "PCPROT: $field[2] came in on wrong channel");
603 if ($dxchan = DXChannel->get($field[1])) {
604 dbg('chan', "PCPROT: $field[1] connected locally");
607 my $ref = DXCluster->get_exact($field[1]);
609 if ($ref->mynode != $node) {
610 dbg('chan', "PCPROT: $field[1] came in from wrong node $field[2]");
615 dbg('chan', "PCPROT: $field[1] not known" );
618 # broadcast_route($line, $self, $field[2]);
623 if ($pcno == 18) { # link request
624 $self->state('init');
626 # first clear out any nodes on this dxchannel
627 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
628 foreach my $node (@gonenodes) {
629 next if $node->dxchan == $DXProt::me;
630 broadcast_ak1a(pc21($node->call, 'Gone, re-init') , $self) unless $self->{isolate};
633 $self->send_local_config();
635 return; # we don't pass these on
638 if ($pcno == 19) { # incoming cluster list
640 my $newline = "PC19^";
641 for ($i = 1; $i < $#field-1; $i += 4) {
642 my $here = $field[$i];
643 my $call = uc $field[$i+1];
644 my $confmode = $field[$i+2];
645 my $ver = $field[$i+3];
646 next unless defined $here && defined $confmode && is_callsign($call);
648 $ver = 5400 if !$ver && $allowzero;
650 # now check the call over
651 my $node = DXCluster->get_exact($call);
654 if (($dxchan = DXChannel->get($call)) && $dxchan != $self) {
655 dbg('chan', "PCPROT: $call connected locally");
657 if ($node->dxchan != $self) {
658 dbg('chan', "PCPROT: $call come in on wrong channel");
661 my $rcall = $node->mynode->call;
662 dbg('chan', "PCPROT: already have $call on $rcall");
666 # check for sane parameters
667 next if $ver < 5000; # only works with version 5 software
668 next if length $call < 3; # min 3 letter callsigns
670 # add it to the nodes table and outgoing line
671 $newline .= "$here^$call^$confmode^$ver^";
672 DXNode->new($self, $call, $confmode, $here, $ver);
674 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
675 my $mref = DXMsg::get_busy($call);
676 $mref->stop_msg($call) if $mref;
678 # add this station to the user database, if required (don't remove SSID from nodes)
679 my $user = DXUser->get_current($call);
681 $user = DXUser->new($call);
683 $user->priv(1); # I have relented and defaulted nodes
684 $self->{priv} = 1; # to user RCMDs allowed
685 $user->homenode($call);
688 $user->lastin($main::systime) unless DXChannel->get($call);
692 return if $newline eq "PC19^";
695 $newline .= get_hops(19) . "^";
700 if ($pcno == 20) { # send local configuration
701 $self->send_local_config();
703 $self->state('normal');
707 if ($pcno == 21) { # delete a cluster from the list
708 my $call = uc $field[1];
709 if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
710 my $node = DXCluster->get_exact($call);
712 unless ($node->isa('DXNode')) {
713 dbg('chan', "PCPROT: $call is not a node");
716 if ($call eq $self->{call}) {
717 dbg('chan', "PCPROT: Trying to disconnect myself with PC21");
720 if ($node->dxchan != $self) {
721 dbg('chan', "PCPROT: $call come in on wrong channel");
725 if ($dxchan = DXChannel->get($call)) {
726 dbg('chan', "PCPROT: $call connected locally");
731 dbg('chan', "PCPROT: $call not in table, dropped");
735 dbg('chan', "PCPROT: I WILL _NOT_ be disconnected!");
738 # broadcast_route($line, $self, $call);
744 $self->state('normal');
748 if ($pcno == 23 || $pcno == 27) { # WWV info
750 # route 'foreign' pc27s
752 if ($field[8] ne $main::mycall) {
753 $self->route($field[8], $line);
759 my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
760 my $sfi = unpad($field[3]);
761 my $k = unpad($field[4]);
762 my $i = unpad($field[5]);
763 my ($r) = $field[6] =~ /R=(\d+)/;
765 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
766 dbg('chan', "PCPROT: WWV Date ($field[1] $field[2]) out of range");
769 if (Geomag::dup($d,$sfi,$k,$i,$field[6])) {
770 dbg('chan', "PCPROT: Dup WWV Spot ignored\n");
773 $field[7] =~ s/-\d+$//o; # remove spotter's ssid
775 my $wwv = Geomag::update($d, $field[2], $sfi, $k, $i, @field[6..8], $r);
779 $rep = Local::wwv($self, $field[1], $field[2], $sfi, $k, $i, @field[6..8], $r);
781 # dbg('local', "Local::wwv2 error $@") if $@;
784 # DON'T be silly and send on PC27s!
785 return if $pcno == 27;
787 # broadcast to the eager world
788 send_wwv_spot($self, $line, $d, $field[2], $sfi, $k, $i, @field[6..8]);
792 if ($pcno == 24) { # set here status
793 my $call = uc $field[1];
794 my $ref = DXCluster->get_exact($call);
795 $ref->here($field[2]) if $ref;
799 if ($pcno == 25) { # merge request
800 if ($field[1] ne $main::mycall) {
801 $self->route($field[1], $line);
804 if ($field[2] eq $main::mycall) {
805 dbg('chan', "PCPROT: Trying to merge to myself, ignored");
809 Log('DXProt', "Merge request for $field[3] spots and $field[4] WWV from $field[1]");
813 my @in = reverse Spot::search(1, undef, undef, 0, $field[3]);
816 $self->send(pc26(@{$in}[0..4], $field[2]));
822 my @in = reverse Geomag::search(0, $field[4], time, 1);
825 $self->send(pc27(@{$in}[0..5], $field[2]));
831 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
832 if ($pcno == 49 || $field[1] eq $main::mycall) {
833 DXMsg::process($self, $line);
835 $self->route($field[1], $line) unless $self->is_clx;
840 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
841 if ($field[1] eq $main::mycall) {
842 my $ref = DXUser->get_current($field[2]);
843 my $cref = DXCluster->get($field[2]);
844 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
845 unless (!$cref || !$ref || $cref->mynode->call ne $ref->homenode) { # not allowed to relay RCMDS!
846 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
847 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
848 my $oldpriv = $self->{priv};
849 $self->{priv} = $ref->{priv}; # assume the user's privilege level
850 my @in = (DXCommandmode::run_cmd($self, $field[3]));
851 $self->{priv} = $oldpriv;
854 $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
855 Log('rcmd', 'out', $field[2], $_);
857 delete $self->{remotecmd};
859 $self->send(pc35($main::mycall, $field[2], "$main::mycall:sorry...!"));
862 $self->send(pc35($main::mycall, $field[2], "$main::mycall:your attempt is logged, Tut tut tut...!"));
865 my $ref = DXUser->get_current($field[1]);
866 if ($ref && $ref->is_clx) {
867 $self->route($field[1], pc84($field[2], $field[1], $field[2], $field[3]));
869 $self->route($field[1], $line);
875 if ($pcno == 35) { # remote command replies
876 if ($field[1] eq $main::mycall) {
877 my $s = $rcmds{$field[2]};
879 my $dxchan = DXChannel->get($s->{call});
880 $dxchan->send($field[3]) if $dxchan;
881 delete $rcmds{$field[2]} if !$dxchan;
883 # send unsolicited ones to the sysop
884 my $dxchan = DXChannel->get($main::myalias);
885 $dxchan->send($field[3]) if $dxchan;
888 my $ref = DXUser->get_current($field[1]);
889 if ($ref && $ref->is_clx) {
890 $self->route($field[1], pc85($field[2], $field[1], $field[2], $field[3]));
892 $self->route($field[1], $line);
898 # for pc 37 see 44 onwards
900 if ($pcno == 38) { # node connected list from neighbour
904 if ($pcno == 39) { # incoming disconnect
905 if ($field[1] eq $self->{call}) {
906 $self->disconnect(1);
908 dbg('chan', "PCPROT: came in on wrong channel");
913 if ($pcno == 41) { # user info
914 # add this station to the user database, if required
915 my $user = DXUser->get_current($field[1]);
916 $user = DXUser->new($field[1]) if !$user;
918 if ($field[2] == 1) {
919 $user->name($field[3]);
920 } elsif ($field[2] == 2) {
921 $user->qth($field[3]);
922 } elsif ($field[2] == 3) {
923 my ($lat, $long) = DXBearing::stoll($field[3]);
926 $user->qra(DXBearing::lltoqra($lat, $long)) unless $user->qra && DXBearing::is_qra($user->qra);
927 } elsif ($field[2] == 4) {
928 $user->homenode($field[3]);
930 $user->lastoper($main::systime); # to cut down on excessive for/opers being generated
937 if ($pcno == 37 || $pcno == 44 || $pcno == 45 || $pcno == 46 || $pcno == 47 || $pcno == 48) {
938 DXDb::process($self, $line);
942 if ($pcno == 50) { # keep alive/user list
943 my $node = DXCluster->get_exact($field[1]);
945 return unless $node->isa('DXNode');
946 return unless $node->dxchan == $self;
947 $node->update_users($field[2]);
952 if ($pcno == 51) { # incoming ping requests/answers
955 if ($field[1] eq $main::mycall) {
956 my $flag = $field[3];
958 $self->send(pc51($field[2], $field[1], '0'));
960 # it's a reply, look in the ping list for this one
961 my $ref = $pings{$field[2]};
963 my $tochan = DXChannel->get($field[2]);
966 my $dxchan = DXChannel->get($r->{call});
968 my $t = tv_interval($r->{t}, [ gettimeofday ]);
969 if ($dxchan->is_user) {
970 my $s = sprintf "%.2f", $t;
971 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
972 $dxchan->send($dxchan->msg('pingi', $field[2], $s, $ave))
973 } elsif ($dxchan->is_node) {
975 $tochan->{nopings} = $tochan->user->nopings || 2; # pump up the timer
976 push @{$tochan->{pingtime}}, $t;
977 shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
979 for (@{$tochan->{pingtime}}) {
982 $tochan->{pingave} = $st / @{$tochan->{pingtime}};
989 # route down an appropriate thingy
990 $self->route($field[1], $line);
995 if ($pcno == 75) { # dunno but route it
996 if ($field[1] ne $main::mycall) {
997 $self->route($field[1], $line);
1002 if ($pcno == 73) { # WCY broadcasts
1005 my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
1006 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
1007 dbg('chan', "PCPROT: WCY Date ($field[1] $field[2]) out of range");
1010 @field = map { unpad($_) } @field;
1011 if (WCY::dup($d,@field[3..7])) {
1012 dbg('chan', "PCPROT: Dup WCY Spot ignored\n");
1016 my $wcy = WCY::update($d, @field[2..12]);
1020 $rep = Local::wwv($self, @field[1..12]);
1022 # dbg('local', "Local::wcy error $@") if $@;
1025 # broadcast to the eager world
1026 send_wcy_spot($self, $line, $d, @field[2..12]);
1030 if ($pcno == 84) { # remote commands (incoming)
1031 if ($field[1] eq $main::mycall) {
1032 my $ref = DXUser->get_current($field[2]);
1033 my $cref = DXCluster->get($field[2]);
1034 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[4]);
1035 unless ($field[4] =~ /rcmd/i || !$cref || !$ref || $cref->mynode->call ne $ref->homenode) { # not allowed to relay RCMDS!
1036 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
1037 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
1038 my $oldpriv = $self->{priv};
1039 $self->{priv} = $ref->{priv}; # assume the user's privilege level
1040 my @in = (DXCommandmode::run_cmd($self, $field[4]));
1041 $self->{priv} = $oldpriv;
1044 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:$_"));
1045 Log('rcmd', 'out', $field[2], $_);
1047 delete $self->{remotecmd};
1049 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:sorry...!"));
1052 $self->send(pc85($main::mycall, $field[2], $field[3],"$main::mycall:your attempt is logged, Tut tut tut...!"));
1055 my $ref = DXUser->get_current($field[1]);
1056 if ($ref && $ref->is_clx) {
1057 $self->route($field[1], $line);
1059 $self->route($field[1], pc34($field[2], $field[1], $field[4]));
1065 if ($pcno == 85) { # remote command replies
1066 if ($field[1] eq $main::mycall) {
1067 my $dxchan = DXChannel->get($field[3]);
1069 $dxchan->send($field[4]);
1071 my $s = $rcmds{$field[2]};
1073 $dxchan = DXChannel->get($s->{call});
1074 $dxchan->send($field[4]) if $dxchan;
1075 delete $rcmds{$field[2]} if !$dxchan;
1077 # send unsolicited ones to the sysop
1078 my $dxchan = DXChannel->get($main::myalias);
1079 $dxchan->send($field[4]) if $dxchan;
1083 my $ref = DXUser->get_current($field[1]);
1084 if ($ref && $ref->is_clx) {
1085 $self->route($field[1], $line);
1087 $self->route($field[1], pc35($field[2], $field[1], $field[4]));
1094 # if get here then rebroadcast the thing with its Hop count decremented (if
1095 # there is one). If it has a hop count and it decrements to zero then don't
1098 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
1102 unless ($self->{isolate}) {
1103 broadcast_ak1a($line, $self); # send it to everyone but me
1108 # This is called from inside the main cluster processing loop and is used
1109 # for despatching commands that are doing some long processing job
1114 my @dxchan = DXChannel->get_all();
1117 foreach $dxchan (@dxchan) {
1118 next unless $dxchan->is_node();
1119 next if $dxchan == $me;
1121 # send a pc50 out on this channel
1122 if ($t >= $dxchan->{pc50_t} + $DXProt::pc50_interval) {
1123 $dxchan->send(pc50(scalar DXChannel::get_all_users));
1124 $dxchan->{pc50_t} = $t;
1127 # send a ping out on this channel
1128 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
1129 if ($dxchan->{nopings} <= 0) {
1130 $dxchan->disconnect;
1132 addping($main::mycall, $dxchan->call);
1133 $dxchan->{nopings} -= 1;
1134 $dxchan->{lastping} = $t;
1142 if ($main::systime - 3600 > $last_hour) {
1146 $last_hour = $main::systime;
1151 # finish up a pc context
1155 # some active measures
1161 my @dxchan = DXChannel::get_all_nodes();
1164 # send it if it isn't the except list and isn't isolated and still has a hop count
1165 # taking into account filtering and so on
1166 foreach $dxchan (@dxchan) {
1168 my ($filter, $hops);
1170 if ($dxchan->{routefilter}) {
1171 ($filter, $hops) = $dxchan->{routefilter}->it($self->{call}, @_);
1172 next unless $filter;
1174 next if $dxchan == $self;
1177 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1179 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1180 next unless $routeit;
1183 $dxchan->send($routeit) if $routeit;
1185 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1194 my @dxchan = DXChannel->get_all();
1197 # send it if it isn't the except list and isn't isolated and still has a hop count
1198 # taking into account filtering and so on
1199 foreach $dxchan (@dxchan) {
1201 my ($filter, $hops);
1203 if ($dxchan->{spotsfilter}) {
1204 ($filter, $hops) = $dxchan->{spotsfilter}->it(@_, $self->{call} );
1205 next unless $filter;
1208 if ($dxchan->is_node) {
1209 next if $dxchan == $self;
1212 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1214 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1215 next unless $routeit;
1218 $dxchan->send($routeit) if $routeit;
1220 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1222 } elsif ($dxchan->is_user && $dxchan->{dx}) {
1223 my $buf = Spot::formatb($dxchan->{user}->wantgrid, $_[0], $_[1], $_[2], $_[3], $_[4]);
1224 $buf .= "\a\a" if $dxchan->{beep};
1226 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1227 $dxchan->send($buf);
1229 $dxchan->delay($buf);
1239 my @dxchan = DXChannel->get_all();
1241 my ($wwv_dxcc, $wwv_itu, $wwv_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1242 my @dxcc = Prefix::extract($_[7]);
1244 $wwv_dxcc = $dxcc[1]->dxcc;
1245 $wwv_itu = $dxcc[1]->itu;
1246 $wwv_cq = $dxcc[1]->cq;
1248 @dxcc = Prefix::extract($_[8]);
1250 $org_dxcc = $dxcc[1]->dxcc;
1251 $org_itu = $dxcc[1]->itu;
1252 $org_cq = $dxcc[1]->cq;
1255 # send it if it isn't the except list and isn't isolated and still has a hop count
1256 # taking into account filtering and so on
1257 foreach $dxchan (@dxchan) {
1259 my ($filter, $hops);
1261 if ($dxchan->{wwvfilter}) {
1262 ($filter, $hops) = $dxchan->{wwvfilter}->it(@_, $self->{call}, $wwv_dxcc, $wwv_itu, $wwv_cq, $org_dxcc, $org_itu, $org_cq);
1263 next unless $filter;
1265 if ($dxchan->is_node) {
1266 next if $dxchan == $self;
1269 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1271 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1272 next unless $routeit;
1275 $dxchan->send($routeit) if $routeit;
1277 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1280 } elsif ($dxchan->is_user && $dxchan->{wwv}) {
1281 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
1282 $buf .= "\a\a" if $dxchan->{beep};
1283 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1284 $dxchan->send($buf);
1286 $dxchan->delay($buf);
1296 my @dxchan = DXChannel->get_all();
1298 my ($wcy_dxcc, $wcy_itu, $wcy_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1299 my @dxcc = Prefix::extract($_[11]);
1301 $wcy_dxcc = $dxcc[1]->dxcc;
1302 $wcy_itu = $dxcc[1]->itu;
1303 $wcy_cq = $dxcc[1]->cq;
1305 @dxcc = Prefix::extract($_[12]);
1307 $org_dxcc = $dxcc[1]->dxcc;
1308 $org_itu = $dxcc[1]->itu;
1309 $org_cq = $dxcc[1]->cq;
1312 # send it if it isn't the except list and isn't isolated and still has a hop count
1313 # taking into account filtering and so on
1314 foreach $dxchan (@dxchan) {
1316 my ($filter, $hops);
1318 if ($dxchan->{wcyfilter}) {
1319 ($filter, $hops) = $dxchan->{wcyfilter}->it(@_, $self->{call}, $wcy_dxcc, $wcy_itu, $wcy_cq, $org_dxcc, $org_itu, $org_cq);
1320 next unless $filter;
1322 if ($dxchan->is_clx || $dxchan->is_spider || $dxchan->is_dxnet) {
1323 next if $dxchan == $self;
1326 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1328 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1329 next unless $routeit;
1332 $dxchan->send($routeit) if $routeit;
1334 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1336 } elsif ($dxchan->is_user && $dxchan->{wcy}) {
1337 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1338 $buf .= "\a\a" if $dxchan->{beep};
1339 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1340 $dxchan->send($buf);
1342 $dxchan->delay($buf);
1353 my @dxchan = DXChannel->get_all();
1355 my $text = unpad($_[2]);
1359 if ($_[3] eq '*') { # sysops
1361 } elsif ($_[3] gt ' ') { # speciality list handling
1362 my ($name) = split /\./, $_[3];
1363 $target = "$name"; # put the rest in later (if bothered)
1370 $target = "ALL" if !$target;
1372 Log('ann', $target, $_[0], $text);
1374 # obtain country codes etc
1375 my ($ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1376 my @dxcc = Prefix::extract($_[0]);
1378 $ann_dxcc = $dxcc[1]->dxcc;
1379 $ann_itu = $dxcc[1]->itu;
1380 $ann_cq = $dxcc[1]->cq;
1382 @dxcc = Prefix::extract($_[4]);
1384 $org_dxcc = $dxcc[1]->dxcc;
1385 $org_itu = $dxcc[1]->itu;
1386 $org_cq = $dxcc[1]->cq;
1389 # send it if it isn't the except list and isn't isolated and still has a hop count
1390 # taking into account filtering and so on
1391 foreach $dxchan (@dxchan) {
1393 my ($filter, $hops);
1395 if ($dxchan->{annfilter}) {
1396 ($filter, $hops) = $dxchan->{annfilter}->it(@_, $self->{call}, $ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq);
1397 next unless $filter;
1399 if ($dxchan->is_node && $_[1] ne $main::mycall) { # i.e not specifically routed to me
1400 next if $dxchan == $self;
1403 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1405 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1406 next unless $routeit;
1409 $dxchan->send($routeit) if $routeit;
1411 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1414 } elsif ($dxchan->is_user) {
1415 unless ($dxchan->{ann}) {
1416 next if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
1418 next if $target eq 'SYSOP' && $dxchan->{priv} < 5;
1419 my $buf = "$to$target de $_[0]: $text";
1421 $buf .= "\a\a" if $dxchan->{beep};
1422 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1423 $dxchan->send($buf);
1425 $dxchan->delay($buf);
1431 sub send_local_config
1440 if ($self->{isolate}) {
1441 @localnodes = (DXCluster->get_exact($main::mycall));
1443 # create a list of all the nodes that are not connected to this connection
1444 # and are not themselves isolated, this to make sure that isolated nodes
1445 # don't appear outside of this node
1446 @nodes = DXNode::get_all();
1447 @nodes = grep { $_->{call} ne $main::mycall } @nodes;
1448 @nodes = grep { $_->dxchan != $self } @nodes if @nodes;
1449 @nodes = grep { !$_->dxchan->{isolate} } @nodes if @nodes;
1450 @localnodes = grep { $_->dxchan->{call} eq $_->{call} } @nodes if @nodes;
1451 unshift @localnodes, DXCluster->get_exact($main::mycall);
1452 @remotenodes = grep { $_->dxchan->{call} ne $_->{call} } @nodes if @nodes;
1455 my @s = $me->pc19(@localnodes, @remotenodes);
1457 my $routeit = adjust_hops($self, $_);
1458 $self->send($routeit) if $routeit;
1461 # get all the users connected on the above nodes and send them out
1462 foreach $n (@localnodes, @remotenodes) {
1463 my @users = values %{$n->list};
1464 my @s = pc16($n, @users);
1466 my $routeit = adjust_hops($self, $_);
1467 $self->send($routeit) if $routeit;
1473 # route a message down an appropriate interface for a callsign
1475 # is called route(to, pcline);
1479 my ($self, $call, $line) = @_;
1481 if (ref $self && $call eq $self->{call}) {
1482 dbg('chan', "PCPROT: Trying to route back to source, dropped");
1486 # always send it down the local interface if available
1487 my $dxchan = DXChannel->get($call);
1489 my $cl = DXCluster->get_exact($call);
1490 $dxchan = $cl->dxchan if $cl;
1492 if (ref $self && $dxchan eq $self) {
1493 dbg('chan', "PCPROT: Trying to route back to source, dropped");
1499 my $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1501 $dxchan->send($routeit);
1504 dbg('chan', "PCPROT: No route available, dropped");
1508 # broadcast a message to all clusters taking into account isolation
1509 # [except those mentioned after buffer]
1512 my $s = shift; # the line to be rebroadcast
1513 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1514 my @dxchan = DXChannel::get_all_nodes();
1517 # send it if it isn't the except list and isn't isolated and still has a hop count
1518 foreach $dxchan (@dxchan) {
1519 next if grep $dxchan == $_, @except;
1520 next if $dxchan == $me;
1522 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
1523 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
1527 # broadcast a message to all clusters ignoring isolation
1528 # [except those mentioned after buffer]
1529 sub broadcast_all_ak1a
1531 my $s = shift; # the line to be rebroadcast
1532 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1533 my @dxchan = DXChannel::get_all_nodes();
1536 # send it if it isn't the except list and isn't isolated and still has a hop count
1537 foreach $dxchan (@dxchan) {
1538 next if grep $dxchan == $_, @except;
1539 next if $dxchan == $me;
1541 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
1542 $dxchan->send($routeit);
1546 # broadcast to all users
1547 # storing the spot or whatever until it is in a state to receive it
1550 my $s = shift; # the line to be rebroadcast
1551 my $sort = shift; # the type of transmission
1552 my $fref = shift; # a reference to an object to filter on
1553 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1554 my @dxchan = DXChannel::get_all_users();
1558 foreach $dxchan (@dxchan) {
1559 next if grep $dxchan == $_, @except;
1562 broadcast_list($s, $sort, $fref, @out);
1565 # broadcast to a list of users
1573 foreach $dxchan (@_) {
1575 next if $dxchan == $me;
1577 if ($sort eq 'dx') {
1578 next unless $dxchan->{dx};
1579 ($filter) = $dxchan->{spotsfilter}->it(@{$fref}) if ref $fref;
1580 next unless $filter;
1582 next if $sort eq 'ann' && !$dxchan->{ann};
1583 next if $sort eq 'wwv' && !$dxchan->{wwv};
1584 next if $sort eq 'wcy' && !$dxchan->{wcy};
1585 next if $sort eq 'wx' && !$dxchan->{wx};
1587 $s =~ s/\a//og unless $dxchan->{beep};
1589 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1599 # obtain the hops from the list for this callsign and pc no
1605 my $hops = $DXProt::hopcount{$pcno};
1606 $hops = $DXProt::def_hopcount if !$hops;
1611 # adjust the hop count on a per node basis using the user loadable
1612 # hop table if available or else decrement an existing one
1619 my $call = $self->{call};
1622 if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
1623 my ($pcno) = $s =~ /^PC(\d\d)/o;
1624 confess "$call called adjust_hops with '$s'" unless $pcno;
1625 my $ref = $nodehops{$call} if %nodehops;
1627 my $newhops = $ref->{$pcno};
1628 return "" if defined $newhops && $newhops == 0;
1629 $newhops = $ref->{default} unless $newhops;
1630 return "" if defined $newhops && $newhops == 0;
1631 $newhops = $hops if !$newhops;
1632 $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
1634 # simply decrement it
1636 return "" if !$hops;
1637 $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
1649 return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1650 do "$main::data/hop_table.pl";
1656 # add a ping request to the ping queues
1659 my ($from, $to) = @_;
1660 my $ref = $pings{$to} || [];
1663 $r->{t} = [ gettimeofday ];
1664 route(undef, $to, pc51($to, $main::mycall, 1));
1669 # add a rcmd request to the rcmd queues
1672 my ($self, $to, $cmd) = @_;
1675 $r->{call} = $self->{call};
1676 $r->{t} = $main::systime;
1680 my $ref = DXCluster->get_exact($to);
1681 if ($ref && $ref->dxchan && $ref->dxchan->is_clx) {
1682 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1684 route(undef, $to, pc34($main::mycall, $to, $cmd));
1691 my $pc39flag = shift;
1692 my $call = $self->call;
1694 unless ($pc39flag && $pc39flag == 1) {
1695 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1698 # unbusy and stop and outgoing mail
1699 my $mref = DXMsg::get_busy($call);
1700 $mref->stop_msg($call) if $mref;
1702 # create a list of all the nodes that have gone and delete them from the table
1704 foreach my $node (grep { $_->dxchancall eq $call } DXNode::get_all) {
1705 next if $node->call eq $call;
1706 next if $node->call eq $main::mycall;
1707 push @nodes, $node->call;
1711 # broadcast to all other nodes that all the nodes connected to via me are gone
1712 unless ($pc39flag && $pc39flag == 2) {
1713 unless ($self->{isolate}) {
1716 broadcast_ak1a(pc21($_, 'Gone.'), $self);
1721 # remove this node from the tables
1722 my $node = DXCluster->get_exact($call);
1723 $node->del if $node;
1725 # remove outstanding pings
1726 delete $pings{$call};
1728 # I was the last node visited
1729 $self->user->node($main::mycall);
1731 # send info to all logged in thingies
1732 $self->tell_login('logoutn');
1734 Log('DXProt', $call . " Disconnected");
1736 $self->SUPER::disconnect;
1741 # send a talk message to this thingy
1745 my ($self, $from, $to, $via, $line) = @_;
1747 $line =~ s/\^/\\5E/g; # remove any ^ characters
1748 $self->send(DXProt::pc10($from, $to, $via, $line));
1749 Log('talk', $self->call, $from, $via?$via:$main::mycall, $line);