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}) {
239 # $self->send(pc38()) if DXNode->get_all();
241 $self->{lastping} = $main::systime;
243 # remove from outstanding connects queue
244 @main::outstanding_connects = grep {$_->{call} ne $call} @main::outstanding_connects;
245 $self->{lastping} = $main::systime + $self->pingint / 2;
247 $self->state('init');
250 # send info to all logged in thingies
251 $self->tell_login('loginn');
253 Log('DXProt', "$call connected");
257 # This is the normal pcxx despatcher
261 my ($self, $line) = @_;
262 my @field = split /\^/, $line;
263 return unless @field;
265 pop @field if $field[-1] eq '~';
267 # print join(',', @field), "\n";
269 # ignore any lines that don't start with PC
270 return if !$field[0] =~ /^PC/;
273 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
275 return if $pcno < 10 || $pcno > 99;
277 # check for and dump bad protocol messages
278 my $n = check($pcno, @field);
280 dbg('chan', "PCPROT: bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")");
287 $pcr = Local::pcprot($self, $pcno, @field);
289 # dbg('local', "Local::pcprot error $@") if $@;
293 if ($pcno == 10) { # incoming talk
295 # will we allow it at all?
298 if (@bad = BadWords::check($field[3])) {
299 dbg('chan', "PCPROT: Bad words: @bad, dropped" );
304 # is it for me or one of mine?
305 my ($to, $via, $call, $dxchan);
306 if ($field[5] gt ' ') {
307 $call = $via = $field[2];
310 $call = $to = $field[2];
312 $dxchan = DXChannel->get($call);
313 if ($dxchan && $dxchan->is_user) {
314 $field[3] =~ s/\%5E/^/g;
315 $dxchan->talk($field[1], $to, $via, $field[3]);
317 $self->route($field[2], $line); # relay it on its way
322 if ($pcno == 11 || $pcno == 26) { # dx spot
324 # route 'foreign' pc26s
326 if ($field[7] ne $main::mycall) {
327 $self->route($field[7], $line);
332 # if this is a 'nodx' node then ignore it
333 if ($badnode->in($field[7])) {
334 dbg('chan', "PCPROT: Bad Node, dropped");
338 # if this is a 'bad spotter' user then ignore it
339 if ($badspotter->in($field[6])) {
340 dbg('chan', "PCPROT: Bad Spotter, dropped");
344 # convert the date to a unix date
345 my $d = cltounix($field[3], $field[4]);
346 # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
347 if (!$d || ($pcno == 11 && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
348 dbg('chan', "PCPROT: Spot ignored, invalid date or out of range ($field[3] $field[4])\n");
353 if ($baddx->in($field[2])) {
354 dbg('chan', "PCPROT: Bad DX spot, ignored");
359 $field[5] =~ s/^\s+//; # take any leading blanks off
360 $field[2] = unpad($field[2]); # take off leading and trailing blanks from spotted callsign
361 if (Spot::dup($field[1], $field[2], $d, $field[5])) {
362 dbg('chan', "PCPROT: Duplicate Spot ignored\n");
367 if (@bad = BadWords::check($field[5])) {
368 dbg('chan', "PCPROT: Bad words: @bad, dropped" );
373 my @spot = Spot::add($field[1], $field[2], $d, $field[5], $field[6], $field[7]);
376 # @spot at this point contains:-
377 # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
378 # then spotted itu, spotted cq, spotters itu, spotters cq
379 # you should be able to route on any of these
382 # fix up qra locators of known users
383 my $user = DXUser->get_current($spot[4]);
385 my $qra = $user->qra;
386 unless ($qra && DXBearing::is_qra($qra)) {
387 my $lat = $user->lat;
388 my $long = $user->long;
389 if (defined $lat && defined $long) {
390 $user->qra(DXBearing::lltoqra($lat, $long));
395 # send a remote command to a distant cluster if it is visible and there is no
396 # qra locator and we havn't done it for a month.
398 unless ($user->qra) {
400 my $to = $user->homenode;
401 my $last = $user->lastoper || 0;
402 if ($send_opernam && $main::systime > $last + $DXUser::lastoperinterval && $to && ($node = DXCluster->get_exact($to)) ) {
403 my $cmd = "forward/opernam $spot[4]";
404 # send the rcmd but we aren't interested in the replies...
405 if ($node && $node->dxchan && $node->dxchan->is_clx) {
406 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
408 route(undef, $to, pc34($main::mycall, $to, $cmd));
410 if ($to ne $field[7]) {
412 $node = DXCluster->get_exact($to);
413 if ($node && $node->dxchan && $node->dxchan->is_clx) {
414 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
416 route(undef, $to, pc34($main::mycall, $to, $cmd));
419 $user->lastoper($main::systime);
428 $r = Local::spot($self, @spot);
430 # dbg('local', "Local::spot1 error $@") if $@;
433 # DON'T be silly and send on PC26s!
434 return if $pcno == 26;
436 # send out the filtered spots
437 send_dx_spot($self, $line, @spot) if @spot;
441 if ($pcno == 12) { # announces
442 # announce duplicate checking
443 $field[3] =~ s/^\s+//; # remove leading blanks
444 if (AnnTalk::dup($field[1], $field[2], $field[3])) {
445 dbg('chan', "PCPROT: Duplicate Announce ignored");
451 if (@bad = BadWords::check($field[3])) {
452 dbg('chan', "PCPROT: Bad words: @bad, dropped" );
457 if ($field[2] eq '*' || $field[2] eq $main::mycall) {
459 # global ann filtering on INPUT
460 if ($self->{inannfilter}) {
461 my ($ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
462 my @dxcc = Prefix::extract($field[1]);
464 $ann_dxcc = $dxcc[1]->dxcc;
465 $ann_itu = $dxcc[1]->itu;
466 $ann_cq = $dxcc[1]->cq();
468 @dxcc = Prefix::extract($field[5]);
470 $org_dxcc = $dxcc[1]->dxcc;
471 $org_itu = $dxcc[1]->itu;
472 $org_cq = $dxcc[1]->cq();
474 my ($filter, $hops) = $self->{inannfilter}->it(@field[1..6], $self->{call},
475 $ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq);
477 dbg('chan', "PCPROT: Rejected by filter");
483 $self->send_announce($line, @field[1..6]);
485 $self->route($field[2], $line);
501 if ($pcno == 16) { # add a user
502 my $node = DXCluster->get_exact($field[1]);
504 if (!$node && ($dxchan = DXChannel->get($field[1]))) {
505 # add it to the node table if it isn't present and it's
507 $node = DXNode->new($dxchan, $field[1], 0, 1, 5400);
508 dbg('chan', "PCPROT: $field[1] no PC19 yet, autovivified as node");
509 # broadcast_ak1a(pc19($dxchan, $node), $dxchan, $self) unless $dxchan->{isolate};
512 if ($field[1] eq $main::mycall || $field[2] eq $main::mycall) {
513 dbg('chan', "PCPROT: trying to alter config on this node from outside!");
516 if ($field[2] eq $main::myalias && DXChannel->get($field[1])) {
517 dbg('chan', "PCPROT: trying to connect sysop from outside!");
521 dbg('chan', "PCPROT: Node $field[1] not in config");
524 unless ($node->isa('DXNode')) {
525 dbg('chan', "PCPROT: $field[1] is not a node");
528 if ($node->dxchan != $self) {
529 dbg('chan', "PCPROT: $field[1] came in on wrong channel");
532 if (($dxchan = DXChannel->get($field[1])) && $dxchan != $self) {
533 dbg('chan', "PCPROT: $field[1] connected locally");
538 for ($i = 2; $i < $#field; $i++) {
539 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (\S) (\d)/o;
540 next unless $call && $confmode && defined $here && is_callsign($call);
541 my $ref = DXCluster->get_exact($call);
543 if ($ref->isa('DXNode')) {
544 dbg('chan', "PCPROT: $call is a node");
547 my $rcall = $ref->mynode->call;
548 dbg('chan', "PCPROT: already have $call on $rcall");
552 $confmode = $confmode eq '*';
553 DXNodeuser->new($self, $node, $call, $confmode, $here);
555 # add this station to the user database, if required
556 $call =~ s/-\d+$//o; # remove ssid for users
557 my $user = DXUser->get_current($call);
558 $user = DXUser->new($call) if !$user;
559 $user->homenode($node->call) if !$user->homenode;
560 $user->node($node->call);
561 $user->lastin($main::systime) unless DXChannel->get($call);
565 # queue up any messages (look for privates only)
566 DXMsg::queue_msg(1) if $self->state eq 'normal';
570 if ($pcno == 17) { # remove a user
571 my $node = DXCluster->get_exact($field[2]);
573 if (!$node && ($dxchan = DXChannel->get($field[2]))) {
574 # add it to the node table if it isn't present and it's
576 $node = DXNode->new($dxchan, $field[2], 0, 1, 5400);
577 dbg('chan', "PCPROT: $field[2] no PC19 yet, autovivified as node");
578 # broadcast_ak1a(pc19($dxchan, $node), $dxchan, $self) unless $dxchan->{isolate};
580 if ($field[1] eq $main::mycall || $field[2] eq $main::mycall) {
581 dbg('chan', "PCPROT: trying to alter config on this node from outside!");
584 if ($field[1] eq $main::myalias && DXChannel->get($field[1])) {
585 dbg('chan', "PCPROT: trying to disconnect sysop from outside!");
589 dbg('chan', "PCPROT: Node $field[2] not in config");
592 unless ($node->isa('DXNode')) {
593 dbg('chan', "PCPROT: $field[2] is not a node");
596 if ($node->dxchan != $self) {
597 dbg('chan', "PCPROT: $field[2] came in on wrong channel");
600 if (($dxchan = DXChannel->get($field[2])) && $dxchan != $self) {
601 dbg('chan', "PCPROT: $field[2] connected locally");
604 my $ref = DXCluster->get_exact($field[1]);
608 dbg('chan', "PCPROT: $field[1] not known" );
614 if ($pcno == 18) { # link request
615 $self->state('init');
617 # first clear out any nodes on this dxchannel
618 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
619 foreach my $node (@gonenodes) {
620 next if $node->dxchan == $DXProt::me;
621 broadcast_ak1a(pc21($node->call, 'Gone, re-init') , $self) unless $self->{isolate};
624 $self->send_local_config();
626 return; # we don't pass these on
629 if ($pcno == 19) { # incoming cluster list
631 my $newline = "PC19^";
632 for ($i = 1; $i < $#field-1; $i += 4) {
633 my $here = $field[$i];
634 my $call = uc $field[$i+1];
635 my $confmode = $field[$i+2];
636 my $ver = $field[$i+3];
637 next unless defined $here && defined $confmode && is_callsign($call);
639 $ver = 5400 if !$ver && $allowzero;
641 # now check the call over
642 my $node = DXCluster->get_exact($call);
645 if (($dxchan = DXChannel->get($call)) && $dxchan != $self) {
646 dbg('chan', "PCPROT: $call connected locally");
648 if ($node->dxchan != $self) {
649 dbg('chan', "PCPROT: $call come in on wrong channel");
652 my $rcall = $node->mynode->call;
653 dbg('chan', "PCPROT: already have $call on $rcall");
657 # check for sane parameters
658 next if $ver < 5000; # only works with version 5 software
659 next if length $call < 3; # min 3 letter callsigns
661 # add it to the nodes table and outgoing line
662 $newline .= "$here^$call^$confmode^$ver^";
663 DXNode->new($self, $call, $confmode, $here, $ver);
665 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
666 my $mref = DXMsg::get_busy($call);
667 $mref->stop_msg($call) if $mref;
669 # add this station to the user database, if required (don't remove SSID from nodes)
670 my $user = DXUser->get_current($call);
672 $user = DXUser->new($call);
674 $user->priv(1); # I have relented and defaulted nodes
675 $self->{priv} = 1; # to user RCMDs allowed
676 $user->homenode($call);
679 $user->lastin($main::systime) unless DXChannel->get($call);
683 return if $newline eq "PC19^";
686 $newline .= get_hops(19) . "^";
691 if ($pcno == 20) { # send local configuration
692 $self->send_local_config();
694 $self->state('normal');
698 if ($pcno == 21) { # delete a cluster from the list
699 my $call = uc $field[1];
700 if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
701 my $node = DXCluster->get_exact($call);
703 unless ($node->isa('DXNode')) {
704 dbg('chan', "PCPROT: $call is not a node");
707 if ($call eq $self->{call}) {
708 dbg('chan', "PCPROT: Trying to disconnect myself with PC21");
711 if ($node->dxchan != $self) {
712 dbg('chan', "PCPROT: $call come in on wrong channel");
716 if ($dxchan = DXChannel->get($call)) {
717 dbg('chan', "PCPROT: $call connected locally");
722 dbg('chan', "PCPROT: $call not in table, dropped");
726 dbg('chan', "PCPROT: I WILL _NOT_ be disconnected!");
733 $self->state('normal');
737 if ($pcno == 23 || $pcno == 27) { # WWV info
739 # route 'foreign' pc27s
741 if ($field[8] ne $main::mycall) {
742 $self->route($field[8], $line);
748 my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
749 my $sfi = unpad($field[3]);
750 my $k = unpad($field[4]);
751 my $i = unpad($field[5]);
752 my ($r) = $field[6] =~ /R=(\d+)/;
754 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
755 dbg('chan', "PCPROT: WWV Date ($field[1] $field[2]) out of range");
758 if (Geomag::dup($d,$sfi,$k,$i,$field[6])) {
759 dbg('chan', "PCPROT: Dup WWV Spot ignored\n");
762 $field[7] =~ s/-\d+$//o; # remove spotter's ssid
764 my $wwv = Geomag::update($d, $field[2], $sfi, $k, $i, @field[6..8], $r);
768 $rep = Local::wwv($self, $field[1], $field[2], $sfi, $k, $i, @field[6..8], $r);
770 # dbg('local', "Local::wwv2 error $@") if $@;
773 # DON'T be silly and send on PC27s!
774 return if $pcno == 27;
776 # broadcast to the eager world
777 send_wwv_spot($self, $line, $d, $field[2], $sfi, $k, $i, @field[6..8]);
781 if ($pcno == 24) { # set here status
782 my $call = uc $field[1];
783 my $ref = DXCluster->get_exact($call);
784 $ref->here($field[2]) if $ref;
788 if ($pcno == 25) { # merge request
789 if ($field[1] ne $main::mycall) {
790 $self->route($field[1], $line);
793 if ($field[2] eq $main::mycall) {
794 dbg('chan', "PCPROT: Trying to merge to myself, ignored");
798 Log('DXProt', "Merge request for $field[3] spots and $field[4] WWV from $field[1]");
802 my @in = reverse Spot::search(1, undef, undef, 0, $field[3]);
805 $self->send(pc26(@{$in}[0..4], $field[2]));
811 my @in = reverse Geomag::search(0, $field[4], time, 1);
814 $self->send(pc27(@{$in}[0..5], $field[2]));
820 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
821 if ($pcno == 49 || $field[1] eq $main::mycall) {
822 DXMsg::process($self, $line);
824 $self->route($field[1], $line) unless $self->is_clx;
829 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
830 if ($field[1] eq $main::mycall) {
831 my $ref = DXUser->get_current($field[2]);
832 my $cref = DXCluster->get($field[2]);
833 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
834 unless (!$cref || !$ref || $cref->mynode->call ne $ref->homenode) { # not allowed to relay RCMDS!
835 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
836 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
837 my $oldpriv = $self->{priv};
838 $self->{priv} = $ref->{priv}; # assume the user's privilege level
839 my @in = (DXCommandmode::run_cmd($self, $field[3]));
840 $self->{priv} = $oldpriv;
843 $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
844 Log('rcmd', 'out', $field[2], $_);
846 delete $self->{remotecmd};
848 $self->send(pc35($main::mycall, $field[2], "$main::mycall:sorry...!"));
851 $self->send(pc35($main::mycall, $field[2], "$main::mycall:your attempt is logged, Tut tut tut...!"));
854 my $ref = DXUser->get_current($field[1]);
855 if ($ref && $ref->is_clx) {
856 $self->route($field[1], pc84($field[2], $field[1], $field[2], $field[3]));
858 $self->route($field[1], $line);
864 if ($pcno == 35) { # remote command replies
865 if ($field[1] eq $main::mycall) {
866 my $s = $rcmds{$field[2]};
868 my $dxchan = DXChannel->get($s->{call});
869 $dxchan->send($field[3]) if $dxchan;
870 delete $rcmds{$field[2]} if !$dxchan;
872 # send unsolicited ones to the sysop
873 my $dxchan = DXChannel->get($main::myalias);
874 $dxchan->send($field[3]) if $dxchan;
877 my $ref = DXUser->get_current($field[1]);
878 if ($ref && $ref->is_clx) {
879 $self->route($field[1], pc85($field[2], $field[1], $field[2], $field[3]));
881 $self->route($field[1], $line);
887 # for pc 37 see 44 onwards
889 if ($pcno == 38) { # node connected list from neighbour
893 if ($pcno == 39) { # incoming disconnect
894 if ($field[1] eq $self->{call}) {
895 $self->disconnect(1);
897 dbg('chan', "PCPROT: came in on wrong channel");
902 if ($pcno == 41) { # user info
903 # add this station to the user database, if required
904 my $user = DXUser->get_current($field[1]);
905 $user = DXUser->new($field[1]) if !$user;
907 if ($field[2] == 1) {
908 $user->name($field[3]);
909 } elsif ($field[2] == 2) {
910 $user->qth($field[3]);
911 } elsif ($field[2] == 3) {
912 my ($lat, $long) = DXBearing::stoll($field[3]);
915 $user->qra(DXBearing::lltoqra($lat, $long)) unless $user->qra && DXBearing::is_qra($user->qra);
916 } elsif ($field[2] == 4) {
917 $user->homenode($field[3]);
919 $user->lastoper($main::systime); # to cut down on excessive for/opers being generated
926 if ($pcno == 37 || $pcno == 44 || $pcno == 45 || $pcno == 46 || $pcno == 47 || $pcno == 48) {
927 DXDb::process($self, $line);
931 if ($pcno == 50) { # keep alive/user list
932 my $node = DXCluster->get_exact($field[1]);
934 return unless $node->isa('DXNode');
935 return unless $node->dxchan == $self;
936 $node->update_users($field[2]);
941 if ($pcno == 51) { # incoming ping requests/answers
944 if ($field[1] eq $main::mycall) {
945 my $flag = $field[3];
947 $self->send(pc51($field[2], $field[1], '0'));
949 # it's a reply, look in the ping list for this one
950 my $ref = $pings{$field[2]};
952 my $tochan = DXChannel->get($field[2]);
955 my $dxchan = DXChannel->get($r->{call});
957 my $t = tv_interval($r->{t}, [ gettimeofday ]);
958 if ($dxchan->is_user) {
959 my $s = sprintf "%.2f", $t;
960 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
961 $dxchan->send($dxchan->msg('pingi', $field[2], $s, $ave))
962 } elsif ($dxchan->is_node) {
964 $tochan->{nopings} = $tochan->user->nopings || 2; # pump up the timer
965 push @{$tochan->{pingtime}}, $t;
966 shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
968 for (@{$tochan->{pingtime}}) {
971 $tochan->{pingave} = $st / @{$tochan->{pingtime}};
978 # route down an appropriate thingy
979 $self->route($field[1], $line);
984 if ($pcno == 75) { # dunno but route it
985 if ($field[1] ne $main::mycall) {
986 $self->route($field[1], $line);
991 if ($pcno == 73) { # WCY broadcasts
994 my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
995 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
996 dbg('chan', "PCPROT: WCY Date ($field[1] $field[2]) out of range");
999 @field = map { unpad($_) } @field;
1000 if (WCY::dup($d,@field[3..7])) {
1001 dbg('chan', "PCPROT: Dup WCY Spot ignored\n");
1005 my $wcy = WCY::update($d, @field[2..12]);
1009 $rep = Local::wwv($self, @field[1..12]);
1011 # dbg('local', "Local::wcy error $@") if $@;
1014 # broadcast to the eager world
1015 send_wcy_spot($self, $line, $d, @field[2..12]);
1019 if ($pcno == 84) { # remote commands (incoming)
1020 if ($field[1] eq $main::mycall) {
1021 my $ref = DXUser->get_current($field[2]);
1022 my $cref = DXCluster->get($field[2]);
1023 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[4]);
1024 unless ($field[4] =~ /rcmd/i || !$cref || !$ref || $cref->mynode->call ne $ref->homenode) { # not allowed to relay RCMDS!
1025 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
1026 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
1027 my $oldpriv = $self->{priv};
1028 $self->{priv} = $ref->{priv}; # assume the user's privilege level
1029 my @in = (DXCommandmode::run_cmd($self, $field[4]));
1030 $self->{priv} = $oldpriv;
1033 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:$_"));
1034 Log('rcmd', 'out', $field[2], $_);
1036 delete $self->{remotecmd};
1038 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:sorry...!"));
1041 $self->send(pc85($main::mycall, $field[2], $field[3],"$main::mycall:your attempt is logged, Tut tut tut...!"));
1044 my $ref = DXUser->get_current($field[1]);
1045 if ($ref && $ref->is_clx) {
1046 $self->route($field[1], $line);
1048 $self->route($field[1], pc34($field[2], $field[1], $field[4]));
1054 if ($pcno == 85) { # remote command replies
1055 if ($field[1] eq $main::mycall) {
1056 my $dxchan = DXChannel->get($field[3]);
1058 $dxchan->send($field[4]);
1060 my $s = $rcmds{$field[2]};
1062 $dxchan = DXChannel->get($s->{call});
1063 $dxchan->send($field[4]) if $dxchan;
1064 delete $rcmds{$field[2]} if !$dxchan;
1066 # send unsolicited ones to the sysop
1067 my $dxchan = DXChannel->get($main::myalias);
1068 $dxchan->send($field[4]) if $dxchan;
1072 my $ref = DXUser->get_current($field[1]);
1073 if ($ref && $ref->is_clx) {
1074 $self->route($field[1], $line);
1076 $self->route($field[1], pc35($field[2], $field[1], $field[4]));
1083 # if get here then rebroadcast the thing with its Hop count decremented (if
1084 # there is one). If it has a hop count and it decrements to zero then don't
1087 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
1091 unless ($self->{isolate}) {
1092 broadcast_ak1a($line, $self); # send it to everyone but me
1097 # This is called from inside the main cluster processing loop and is used
1098 # for despatching commands that are doing some long processing job
1103 my @dxchan = DXChannel->get_all();
1106 foreach $dxchan (@dxchan) {
1107 next unless $dxchan->is_node();
1108 next if $dxchan == $me;
1110 # send a pc50 out on this channel
1111 if ($t >= $dxchan->pc50_t + $DXProt::pc50_interval) {
1112 $dxchan->send(pc50(scalar DXChannel::get_all_users));
1113 $dxchan->pc50_t($t);
1116 # send a ping out on this channel
1117 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
1118 if ($dxchan->{nopings} <= 0) {
1119 $dxchan->disconnect;
1121 addping($main::mycall, $dxchan->call);
1122 $dxchan->{nopings} -= 1;
1123 $dxchan->{lastping} = $t;
1131 if ($main::systime - 3600 > $last_hour) {
1135 $last_hour = $main::systime;
1140 # finish up a pc context
1145 my $call = $self->call;
1147 my $ref = DXCluster->get_exact($call);
1149 # unbusy and stop and outgoing mail
1150 my $mref = DXMsg::get_busy($call);
1151 $mref->stop_msg($call) if $mref;
1153 # broadcast to all other nodes that all the nodes connected to via me are gone
1154 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
1157 foreach $node (@gonenodes) {
1158 next if $node->call eq $call;
1159 broadcast_ak1a(pc21($node->call, 'Gone') , $self) unless $self->{isolate};
1163 # remove outstanding pings
1164 delete $pings{$call};
1166 # now broadcast to all other ak1a nodes that I have gone
1167 broadcast_ak1a(pc21($call, 'Gone.'), $self) unless $self->{isolate};
1169 # I was the last node visited
1170 $self->user->node($main::mycall);
1172 # send info to all logged in thingies
1173 $self->tell_login('logoutn');
1175 Log('DXProt', $call . " Disconnected");
1176 $ref->del() if $ref;
1180 # some active measures
1186 my @dxchan = DXChannel->get_all();
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) {
1193 my ($filter, $hops);
1195 if ($dxchan->{spotsfilter}) {
1196 ($filter, $hops) = $dxchan->{spotsfilter}->it(@_, $self->{call} );
1197 next unless $filter;
1200 if ($dxchan->is_node) {
1201 next if $dxchan == $self;
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};
1214 } elsif ($dxchan->is_user && $dxchan->{dx}) {
1215 my $buf = Spot::formatb($dxchan->{user}->wantgrid, $_[0], $_[1], $_[2], $_[3], $_[4]);
1216 $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 ($wwv_dxcc, $wwv_itu, $wwv_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1234 my @dxcc = Prefix::extract($_[7]);
1236 $wwv_dxcc = $dxcc[1]->dxcc;
1237 $wwv_itu = $dxcc[1]->itu;
1238 $wwv_cq = $dxcc[1]->cq;
1240 @dxcc = Prefix::extract($_[8]);
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) {
1251 my ($filter, $hops);
1253 if ($dxchan->{wwvfilter}) {
1254 ($filter, $hops) = $dxchan->{wwvfilter}->it(@_, $self->{call}, $wwv_dxcc, $wwv_itu, $wwv_cq, $org_dxcc, $org_itu, $org_cq);
1255 next unless $filter;
1257 if ($dxchan->is_node) {
1258 next if $dxchan == $self;
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};
1272 } elsif ($dxchan->is_user && $dxchan->{wwv}) {
1273 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
1274 $buf .= "\a\a" if $dxchan->{beep};
1275 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1276 $dxchan->send($buf);
1278 $dxchan->delay($buf);
1288 my @dxchan = DXChannel->get_all();
1290 my ($wcy_dxcc, $wcy_itu, $wcy_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1291 my @dxcc = Prefix::extract($_[11]);
1293 $wcy_dxcc = $dxcc[1]->dxcc;
1294 $wcy_itu = $dxcc[1]->itu;
1295 $wcy_cq = $dxcc[1]->cq;
1297 @dxcc = Prefix::extract($_[12]);
1299 $org_dxcc = $dxcc[1]->dxcc;
1300 $org_itu = $dxcc[1]->itu;
1301 $org_cq = $dxcc[1]->cq;
1304 # send it if it isn't the except list and isn't isolated and still has a hop count
1305 # taking into account filtering and so on
1306 foreach $dxchan (@dxchan) {
1308 my ($filter, $hops);
1310 if ($dxchan->{wcyfilter}) {
1311 ($filter, $hops) = $dxchan->{wcyfilter}->it(@_, $self->{call}, $wcy_dxcc, $wcy_itu, $wcy_cq, $org_dxcc, $org_itu, $org_cq);
1312 next unless $filter;
1314 if ($dxchan->is_clx || $dxchan->is_spider || $dxchan->is_dxnet) {
1315 next if $dxchan == $self;
1318 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1320 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1321 next unless $routeit;
1324 $dxchan->send($routeit) if $routeit;
1326 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1328 } elsif ($dxchan->is_user && $dxchan->{wcy}) {
1329 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1330 $buf .= "\a\a" if $dxchan->{beep};
1331 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1332 $dxchan->send($buf);
1334 $dxchan->delay($buf);
1345 my @dxchan = DXChannel->get_all();
1347 my $text = unpad($_[2]);
1351 if ($_[3] eq '*') { # sysops
1353 } elsif ($_[3] gt ' ') { # speciality list handling
1354 my ($name) = split /\./, $_[3];
1355 $target = "$name"; # put the rest in later (if bothered)
1362 $target = "ALL" if !$target;
1364 Log('ann', $target, $_[0], $text);
1366 # obtain country codes etc
1367 my ($ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1368 my @dxcc = Prefix::extract($_[0]);
1370 $ann_dxcc = $dxcc[1]->dxcc;
1371 $ann_itu = $dxcc[1]->itu;
1372 $ann_cq = $dxcc[1]->cq;
1374 @dxcc = Prefix::extract($_[4]);
1376 $org_dxcc = $dxcc[1]->dxcc;
1377 $org_itu = $dxcc[1]->itu;
1378 $org_cq = $dxcc[1]->cq;
1381 # send it if it isn't the except list and isn't isolated and still has a hop count
1382 # taking into account filtering and so on
1383 foreach $dxchan (@dxchan) {
1385 my ($filter, $hops);
1387 if ($dxchan->{annfilter}) {
1388 ($filter, $hops) = $dxchan->{annfilter}->it(@_, $self->{call}, $ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq);
1389 next unless $filter;
1391 if ($dxchan->is_node && $_[1] ne $main::mycall) { # i.e not specifically routed to me
1392 next if $dxchan == $self;
1395 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1397 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1398 next unless $routeit;
1401 $dxchan->send($routeit) if $routeit;
1403 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1406 } elsif ($dxchan->is_user) {
1407 unless ($dxchan->{ann}) {
1408 next if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
1410 next if $target eq 'SYSOP' && $dxchan->{priv} < 5;
1411 my $buf = "$to$target de $_[0]: $text";
1413 $buf .= "\a\a" if $dxchan->{beep};
1414 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1415 $dxchan->send($buf);
1417 $dxchan->delay($buf);
1423 sub send_local_config
1432 if ($self->{isolate}) {
1433 @localnodes = (DXCluster->get_exact($main::mycall));
1435 # create a list of all the nodes that are not connected to this connection
1436 # and are not themselves isolated, this to make sure that isolated nodes
1437 # don't appear outside of this node
1438 @nodes = DXNode::get_all();
1439 @nodes = grep { $_->{call} ne $main::mycall } @nodes;
1440 @nodes = grep { $_->dxchan != $self } @nodes if @nodes;
1441 @nodes = grep { !$_->dxchan->{isolate} } @nodes if @nodes;
1442 @localnodes = grep { $_->dxchan->{call} eq $_->{call} } @nodes if @nodes;
1443 unshift @localnodes, DXCluster->get_exact($main::mycall);
1444 @remotenodes = grep { $_->dxchan->{call} ne $_->{call} } @nodes if @nodes;
1447 my @s = $me->pc19(@localnodes, @remotenodes);
1449 my $routeit = adjust_hops($self, $_);
1450 $self->send($routeit) if $routeit;
1453 # get all the users connected on the above nodes and send them out
1454 foreach $n (@localnodes, @remotenodes) {
1455 my @users = values %{$n->list};
1456 my @s = pc16($n, @users);
1458 my $routeit = adjust_hops($self, $_);
1459 $self->send($routeit) if $routeit;
1465 # route a message down an appropriate interface for a callsign
1467 # is called route(to, pcline);
1471 my ($self, $call, $line) = @_;
1473 if (ref $self && $call eq $self->{call}) {
1474 dbg('chan', "PCPROT: Trying to route back to source, dropped");
1478 # always send it down the local interface if available
1479 my $dxchan = DXChannel->get($call);
1481 my $cl = DXCluster->get_exact($call);
1482 $dxchan = $cl->dxchan if $cl;
1484 if (ref $self && $dxchan eq $self) {
1485 dbg('chan', "PCPROT: Trying to route back to source, dropped");
1491 my $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1493 $dxchan->send($routeit);
1496 dbg('chan', "PCPROT: No route available, dropped");
1500 # broadcast a message to all clusters taking into account isolation
1501 # [except those mentioned after buffer]
1504 my $s = shift; # the line to be rebroadcast
1505 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1506 my @dxchan = DXChannel::get_all_nodes();
1509 # send it if it isn't the except list and isn't isolated and still has a hop count
1510 foreach $dxchan (@dxchan) {
1511 next if grep $dxchan == $_, @except;
1512 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
1513 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
1517 # broadcast a message to all clusters ignoring isolation
1518 # [except those mentioned after buffer]
1519 sub broadcast_all_ak1a
1521 my $s = shift; # the line to be rebroadcast
1522 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1523 my @dxchan = DXChannel::get_all_nodes();
1526 # send it if it isn't the except list and isn't isolated and still has a hop count
1527 foreach $dxchan (@dxchan) {
1528 next if grep $dxchan == $_, @except;
1529 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
1530 $dxchan->send($routeit);
1534 # broadcast to all users
1535 # storing the spot or whatever until it is in a state to receive it
1538 my $s = shift; # the line to be rebroadcast
1539 my $sort = shift; # the type of transmission
1540 my $fref = shift; # a reference to an object to filter on
1541 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1542 my @dxchan = DXChannel::get_all_users();
1546 foreach $dxchan (@dxchan) {
1547 next if grep $dxchan == $_, @except;
1550 broadcast_list($s, $sort, $fref, @out);
1553 # broadcast to a list of users
1561 foreach $dxchan (@_) {
1564 if ($sort eq 'dx') {
1565 next unless $dxchan->{dx};
1566 ($filter) = $dxchan->{spotsfilter}->it(@{$fref}) if ref $fref;
1567 next unless $filter;
1569 next if $sort eq 'ann' && !$dxchan->{ann};
1570 next if $sort eq 'wwv' && !$dxchan->{wwv};
1571 next if $sort eq 'wcy' && !$dxchan->{wcy};
1572 next if $sort eq 'wx' && !$dxchan->{wx};
1574 $s =~ s/\a//og unless $dxchan->{beep};
1576 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1586 # obtain the hops from the list for this callsign and pc no
1592 my $hops = $DXProt::hopcount{$pcno};
1593 $hops = $DXProt::def_hopcount if !$hops;
1598 # adjust the hop count on a per node basis using the user loadable
1599 # hop table if available or else decrement an existing one
1606 my $call = $self->{call};
1609 if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
1610 my ($pcno) = $s =~ /^PC(\d\d)/o;
1611 confess "$call called adjust_hops with '$s'" unless $pcno;
1612 my $ref = $nodehops{$call} if %nodehops;
1614 my $newhops = $ref->{$pcno};
1615 return "" if defined $newhops && $newhops == 0;
1616 $newhops = $ref->{default} unless $newhops;
1617 return "" if defined $newhops && $newhops == 0;
1618 $newhops = $hops if !$newhops;
1619 $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
1621 # simply decrement it
1623 return "" if !$hops;
1624 $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
1636 return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1637 do "$main::data/hop_table.pl";
1643 # add a ping request to the ping queues
1646 my ($from, $to) = @_;
1647 my $ref = $pings{$to} || [];
1650 $r->{t} = [ gettimeofday ];
1651 route(undef, $to, pc51($to, $main::mycall, 1));
1656 # add a rcmd request to the rcmd queues
1659 my ($self, $to, $cmd) = @_;
1662 $r->{call} = $self->{call};
1663 $r->{t} = $main::systime;
1667 my $ref = DXCluster->get_exact($to);
1668 if ($ref && $ref->dxchan && $ref->dxchan->is_clx) {
1669 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1671 route(undef, $to, pc34($main::mycall, $to, $cmd));
1680 if ($self->{conn} && !$nopc39) {
1681 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1684 $self->SUPER::disconnect;
1689 # send a talk message to this thingy
1693 my ($self, $from, $to, $via, $line) = @_;
1695 $line =~ s/\^/\\5E/g; # remove any ^ characters
1696 $self->send(DXProt::pc10($from, $to, $via, $line));
1697 Log('talk', $self->call, $from, $via?$via:$main::mycall, $line);