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);
35 use vars qw($me $pc11_max_age $pc23_max_age
36 $last_hour %pings %rcmds
37 %nodehops @baddx $baddxfn $censorpc
38 $allowzero $decode_dk0wcy $send_opernam @checklist);
40 $me = undef; # the channel id for this cluster
41 $pc11_max_age = 1*3600; # the maximum age for an incoming 'real-time' pc11
42 $pc23_max_age = 1*3600; # the maximum age for an incoming 'real-time' pc23
44 $last_hour = time; # last time I did an hourly periodic update
45 %pings = (); # outstanding ping requests outbound
46 %rcmds = (); # outstanding rcmd requests outbound
47 %nodehops = (); # node specific hop control
48 @baddx = (); # list of illegal spotted callsigns
49 $censorpc = 0; # Do a BadWords::check on text fields and reject things
51 $baddxfn = "$main::data/baddx.pl";
55 [ qw(c c m bp bc c) ], # pc10
56 [ qw(f m d t m c c h) ], # pc11
57 [ qw(c bc m bp bm p h) ], # pc12
61 undef , # pc16 has to be validated manually
64 undef , # pc19 has to be validated manually
65 undef , # pc20 no validation
67 undef , # pc22 no validation
68 [ qw(d n n n n m c c h) ], # pc23
70 [ qw(c c n n) ], # pc25
71 [ qw(f m d t m c c bc) ], # pc26
72 [ qw(d n n n n m c c bc) ], # pc27
73 [ qw(c c m c d t p m bp n p bp bc) ], # pc28
74 [ qw(c c n m) ], # pc29
82 [ qw(c c n m) ], # pc37
83 undef, # pc38 not interested
85 [ qw(c c m p n) ], # pc40
86 [ qw(c n m h) ], # pc41
88 undef, # pc43 don't handle it
89 [ qw(c c n m m c) ], # pc44
90 [ qw(c c n m) ], # pc45
118 [ qw(d n n n n n n m m m c c h) ], # pc73
129 [ qw(c c c m) ], # pc84
130 [ qw(c c c m) ], # pc85
133 # use the entry in the check list to check the field list presented
134 # return OK if line NOT in check list (for now)
139 return 0 if $n < 0 || $n > @checklist;
140 my $ref = $checklist[$n];
141 return 0 unless ref $ref;
144 shift; # not interested in the first field
145 for ($i = 0; $i < @$ref; $i++) {
146 my ($blank, $act) = $$ref[$i] =~ /^(b?)(\w)$/;
147 return 0 unless $act;
148 next if $blank && $_[$i] =~ /^[ \*]$/;
150 return $i+1 unless is_callsign($_[$i]);
151 } elsif ($act eq 'm') {
152 return $i+1 unless is_pctext($_[$i]);
153 } elsif ($act eq 'p') {
154 return $i+1 unless is_pcflag($_[$i]);
155 } elsif ($act eq 'f') {
156 return $i+1 unless is_freq($_[$i]);
157 } elsif ($act eq 'n') {
158 return $i+1 unless $_[$i] =~ /^[\d ]+$/;
159 } elsif ($act eq 'h') {
160 return $i+1 unless $_[$i] =~ /^H\d\d?$/;
161 } elsif ($act eq 'd') {
162 return $i+1 unless $_[$i] =~ /^\s*\d+-\w\w\w-[12][90]\d\d$/;
163 } elsif ($act eq 't') {
164 return $i+1 unless $_[$i] =~ /^[012]\d[012345]\dZ$/;
172 my $user = DXUser->get($main::mycall);
173 $DXProt::myprot_version += $main::version*100;
174 $me = DXProt->new($main::mycall, 0, $user);
176 $me->{state} = "indifferent";
177 do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
179 $me->{sort} = 'S'; # S for spider
181 # load the baddx file
182 do "$baddxfn" if -e "$baddxfn";
187 # obtain a new connection this is derived from dxchannel
192 my $self = DXChannel::alloc(@_);
196 # this is how a pc connection starts (for an incoming connection)
197 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
198 # all the crap that comes between).
201 my ($self, $line, $sort) = @_;
202 my $call = $self->{call};
203 my $user = $self->{user};
205 # remember type of connection
206 $self->{consort} = $line;
207 $self->{outbound} = $sort eq 'O';
208 $self->{priv} = $user->priv || 1; # other clusters can always be 'normal' users
209 $self->{lang} = $user->lang || 'en';
210 $self->{isolate} = $user->{isolate};
211 $self->{consort} = $line; # save the connection type
214 # get the output filters
215 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
216 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
217 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
218 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
221 # get the INPUT filters (these only pertain to Clusters)
222 $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
223 $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
224 $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
225 $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
227 # set unbuffered and no echo
228 $self->send_now('B',"0");
229 $self->send_now('E',"0");
231 # ping neighbour node stuff
232 my $ping = $user->pingint;
233 $ping = 5*60 unless defined $ping;
234 $self->{pingint} = $ping;
235 $self->{nopings} = $user->nopings || 2;
236 $self->{pingtime} = [ ];
237 $self->{pingave} = 0;
239 # send initialisation string
240 unless ($self->{outbound}) {
241 # $self->send(pc38()) if DXNode->get_all();
243 $self->{lastping} = $main::systime;
245 # remove from outstanding connects queue
246 @main::outstanding_connects = grep {$_->{call} ne $call} @main::outstanding_connects;
247 $self->{lastping} = $main::systime + $self->pingint / 2;
249 $self->state('init');
252 # send info to all logged in thingies
253 $self->tell_login('loginn');
255 Log('DXProt', "$call connected");
259 # This is the normal pcxx despatcher
263 my ($self, $line) = @_;
264 my @field = split /\^/, $line;
265 return unless @field;
267 pop @field if $field[-1] eq '~';
269 # print join(',', @field), "\n";
271 # ignore any lines that don't start with PC
272 return if !$field[0] =~ /^PC/;
275 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
277 return if $pcno < 10 || $pcno > 99;
279 # check for and dump bad protocol messages
280 my $n = check($pcno, @field);
282 dbg('chan', "PCPROT: bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")");
289 $pcr = Local::pcprot($self, $pcno, @field);
291 # dbg('local', "Local::pcprot error $@") if $@;
295 if ($pcno == 10) { # incoming talk
297 # will we allow it at all?
300 if (@bad = BadWords::check($field[3])) {
301 dbg('chan', "PCPROT: Bad words: @bad, dropped" );
306 # is it for me or one of mine?
307 my ($to, $via, $call, $dxchan);
308 if ($field[5] gt ' ') {
309 $call = $via = $field[2];
312 $call = $to = $field[2];
314 $dxchan = DXChannel->get($call);
315 if ($dxchan && $dxchan->is_user) {
316 $field[3] =~ s/\%5E/^/g;
317 $dxchan->talk($field[1], $to, $via, $field[3]);
319 $self->route($field[2], $line); # relay it on its way
324 if ($pcno == 11 || $pcno == 26) { # dx spot
326 # route 'foreign' pc26s
328 if ($field[7] ne $main::mycall) {
329 $self->route($field[7], $line);
334 # if this is a 'nodx' node then ignore it
335 if (grep $field[7] =~ /^$_/, @DXProt::nodx_node) {
336 dbg('chan', "PCPROT: Bad DXNode, dropped");
340 # convert the date to a unix date
341 my $d = cltounix($field[3], $field[4]);
342 # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
343 if (!$d || ($pcno == 11 && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
344 dbg('chan', "PCPROT: Spot ignored, invalid date or out of range ($field[3] $field[4])\n");
349 if (grep $field[2] eq $_, @baddx) {
350 dbg('chan', "PCPROT: Bad DX spot, ignored");
355 $field[5] =~ s/^\s+//; # take any leading blanks off
356 if (Spot::dup($field[1], $field[2], $d, $field[5])) {
357 dbg('chan', "PCPROT: Duplicate Spot ignored\n");
362 if (@bad = BadWords::check($field[5])) {
363 dbg('chan', "PCPROT: Bad words: @bad, dropped" );
368 my @spot = Spot::add($field[1], $field[2], $d, $field[5], $field[6], $field[7]);
371 # @spot at this point contains:-
372 # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
373 # then spotted itu, spotted cq, spotters itu, spotters cq
374 # you should be able to route on any of these
377 # fix up qra locators of known users
378 my $user = DXUser->get_current($spot[4]);
380 my $qra = $user->qra;
381 unless ($qra && DXBearing::is_qra($qra)) {
382 my $lat = $user->lat;
383 my $long = $user->long;
384 if (defined $lat && defined $long) {
385 $user->qra(DXBearing::lltoqra($lat, $long));
390 # send a remote command to a distant cluster if it is visible and there is no
391 # qra locator and we havn't done it for a month.
393 unless ($user->qra) {
395 my $to = $user->homenode;
396 my $last = $user->lastoper || 0;
397 if ($send_opernam && $main::systime > $last + $DXUser::lastoperinterval && $to && ($node = DXCluster->get_exact($to)) ) {
398 my $cmd = "forward/opernam $spot[4]";
399 # send the rcmd but we aren't interested in the replies...
400 if ($node && $node->dxchan && $node->dxchan->is_clx) {
401 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
403 route(undef, $to, pc34($main::mycall, $to, $cmd));
405 if ($to ne $field[7]) {
407 $node = DXCluster->get_exact($to);
408 if ($node && $node->dxchan && $node->dxchan->is_clx) {
409 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
411 route(undef, $to, pc34($main::mycall, $to, $cmd));
414 $user->lastoper($main::systime);
423 $r = Local::spot($self, @spot);
425 # dbg('local', "Local::spot1 error $@") if $@;
428 # DON'T be silly and send on PC26s!
429 return if $pcno == 26;
431 # send out the filtered spots
432 send_dx_spot($self, $line, @spot) if @spot;
436 if ($pcno == 12) { # announces
437 # announce duplicate checking
438 $field[3] =~ s/^\s+//; # remove leading blanks
439 if (AnnTalk::dup($field[1], $field[2], $field[3])) {
440 dbg('chan', "PCPROT: Duplicate Announce ignored");
446 if (@bad = BadWords::check($field[3])) {
447 dbg('chan', "PCPROT: Bad words: @bad, dropped" );
452 if ($field[2] eq '*' || $field[2] eq $main::mycall) {
454 # global ann filtering on INPUT
455 if ($self->{inannfilter}) {
456 my ($ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
457 my @dxcc = Prefix::extract($field[1]);
459 $ann_dxcc = $dxcc[1]->dxcc;
460 $ann_itu = $dxcc[1]->itu;
461 $ann_cq = $dxcc[1]->cq();
463 @dxcc = Prefix::extract($field[5]);
465 $org_dxcc = $dxcc[1]->dxcc;
466 $org_itu = $dxcc[1]->itu;
467 $org_cq = $dxcc[1]->cq();
469 my ($filter, $hops) = $self->{inannfilter}->it(@field[1..6], $self->{call},
470 $ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq);
472 dbg('chan', "PCPROT: Rejected by filter");
478 $self->send_announce($line, @field[1..6]);
480 $self->route($field[2], $line);
496 if ($pcno == 16) { # add a user
497 my $node = DXCluster->get_exact($field[1]);
499 if (!$node && ($dxchan = DXChannel->get($field[1]))) {
500 # add it to the node table if it isn't present and it's
502 $node = DXNode->new($dxchan, $field[1], 0, 1, 5400);
503 dbg('chan', "PCPROT: $field[1] no PC19 yet, autovivified as node");
504 # broadcast_ak1a(pc19($dxchan, $node), $dxchan, $self) unless $dxchan->{isolate};
507 if ($field[1] eq $main::mycall || $field[2] eq $main::mycall) {
508 dbg('chan', "PCPROT: trying to alter config on this node from outside!");
511 if ($field[2] eq $main::myalias && DXChannel->get($field[1])) {
512 dbg('chan', "PCPROT: trying to connect sysop from outside!");
516 dbg('chan', "PCPROT: Node $field[1] not in config");
519 unless ($node->isa('DXNode')) {
520 dbg('chan', "PCPROT: $field[1] is not a node");
523 if ($node->dxchan != $self) {
524 dbg('chan', "PCPROT: $field[1] came in on wrong channel");
527 if (($dxchan = DXChannel->get($field[1])) && $dxchan != $self) {
528 dbg('chan', "PCPROT: $field[1] connected locally");
533 for ($i = 2; $i < $#field; $i++) {
534 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (\S) (\d)/o;
535 next unless $call && $confmode && defined $here && is_callsign($call);
536 my $ref = DXCluster->get_exact($call);
538 if ($ref->isa('DXNode')) {
539 dbg('chan', "PCPROT: $call is a node");
542 my $rcall = $ref->mynode->call;
543 dbg('chan', "PCPROT: already have $call on $rcall");
547 $confmode = $confmode eq '*';
548 DXNodeuser->new($self, $node, $call, $confmode, $here);
550 # add this station to the user database, if required
551 $call =~ s/-\d+$//o; # remove ssid for users
552 my $user = DXUser->get_current($call);
553 $user = DXUser->new($call) if !$user;
554 $user->homenode($node->call) if !$user->homenode;
555 $user->node($node->call);
556 $user->lastin($main::systime) unless DXChannel->get($call);
560 # queue up any messages (look for privates only)
561 DXMsg::queue_msg(1) if $self->state eq 'normal';
565 if ($pcno == 17) { # remove a user
566 my $node = DXCluster->get_exact($field[2]);
568 if (!$node && ($dxchan = DXChannel->get($field[2]))) {
569 # add it to the node table if it isn't present and it's
571 $node = DXNode->new($dxchan, $field[2], 0, 1, 5400);
572 dbg('chan', "PCPROT: $field[2] no PC19 yet, autovivified as node");
573 # broadcast_ak1a(pc19($dxchan, $node), $dxchan, $self) unless $dxchan->{isolate};
575 if ($field[1] eq $main::mycall || $field[2] eq $main::mycall) {
576 dbg('chan', "PCPROT: trying to alter config on this node from outside!");
579 if ($field[1] eq $main::myalias && DXChannel->get($field[1])) {
580 dbg('chan', "PCPROT: trying to disconnect sysop from outside!");
584 dbg('chan', "PCPROT: Node $field[2] not in config");
587 unless ($node->isa('DXNode')) {
588 dbg('chan', "PCPROT: $field[2] is not a node");
591 if ($node->dxchan != $self) {
592 dbg('chan', "PCPROT: $field[2] came in on wrong channel");
595 if (($dxchan = DXChannel->get($field[2])) && $dxchan != $self) {
596 dbg('chan', "PCPROT: $field[2] connected locally");
599 my $ref = DXCluster->get_exact($field[1]);
603 dbg('chan', "PCPROT: $field[1] not known" );
609 if ($pcno == 18) { # link request
610 $self->state('init');
612 # first clear out any nodes on this dxchannel
613 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
614 foreach my $node (@gonenodes) {
615 next if $node->dxchan == $DXProt::me;
616 broadcast_ak1a(pc21($node->call, 'Gone, re-init') , $self) unless $self->{isolate};
619 $self->send_local_config();
621 return; # we don't pass these on
624 if ($pcno == 19) { # incoming cluster list
626 my $newline = "PC19^";
627 for ($i = 1; $i < $#field-1; $i += 4) {
628 my $here = $field[$i];
629 my $call = uc $field[$i+1];
630 my $confmode = $field[$i+2];
631 my $ver = $field[$i+3];
632 next unless defined $here && defined $confmode && is_callsign($call);
634 $ver = 5400 if !$ver && $allowzero;
636 # now check the call over
637 my $node = DXCluster->get_exact($call);
640 if (($dxchan = DXChannel->get($call)) && $dxchan != $self) {
641 dbg('chan', "PCPROT: $call connected locally");
643 if ($node->dxchan != $self) {
644 dbg('chan', "PCPROT: $call come in on wrong channel");
647 my $rcall = $node->mynode->call;
648 dbg('chan', "PCPROT: already have $call on $rcall");
652 # check for sane parameters
653 next if $ver < 5000; # only works with version 5 software
654 next if length $call < 3; # min 3 letter callsigns
656 # add it to the nodes table and outgoing line
657 $newline .= "$here^$call^$confmode^$ver^";
658 DXNode->new($self, $call, $confmode, $here, $ver);
660 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
661 my $mref = DXMsg::get_busy($call);
662 $mref->stop_msg($call) if $mref;
664 # add this station to the user database, if required (don't remove SSID from nodes)
665 my $user = DXUser->get_current($call);
667 $user = DXUser->new($call);
669 $user->priv(1); # I have relented and defaulted nodes
670 $self->{priv} = 1; # to user RCMDs allowed
671 $user->homenode($call);
674 $user->lastin($main::systime) unless DXChannel->get($call);
678 return if $newline eq "PC19^";
681 $newline .= get_hops(19) . "^";
686 if ($pcno == 20) { # send local configuration
687 $self->send_local_config();
689 $self->state('normal');
693 if ($pcno == 21) { # delete a cluster from the list
694 my $call = uc $field[1];
695 if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
696 my $node = DXCluster->get_exact($call);
698 unless ($node->isa('DXNode')) {
699 dbg('chan', "PCPROT: $call is not a node");
702 if ($call eq $self->{call}) {
703 dbg('chan', "PCPROT: Trying to disconnect myself with PC21");
706 if ($node->dxchan != $self) {
707 dbg('chan', "PCPROT: $call come in on wrong channel");
711 if ($dxchan = DXChannel->get($call)) {
712 dbg('chan', "PCPROT: $call connected locally");
717 dbg('chan', "PCPROT: $call not in table, dropped");
721 dbg('chan', "PCPROT: I WILL _NOT_ be disconnected!");
728 $self->state('normal');
732 if ($pcno == 23 || $pcno == 27) { # WWV info
734 # route 'foreign' pc27s
736 if ($field[8] ne $main::mycall) {
737 $self->route($field[8], $line);
743 my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
744 my $sfi = unpad($field[3]);
745 my $k = unpad($field[4]);
746 my $i = unpad($field[5]);
747 my ($r) = $field[6] =~ /R=(\d+)/;
749 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
750 dbg('chan', "PCPROT: WWV Date ($field[1] $field[2]) out of range");
753 if (Geomag::dup($d,$sfi,$k,$i,$field[6])) {
754 dbg('chan', "PCPROT: Dup WWV Spot ignored\n");
757 $field[7] =~ s/-\d+$//o; # remove spotter's ssid
759 my $wwv = Geomag::update($d, $field[2], $sfi, $k, $i, @field[6..8], $r);
763 $rep = Local::wwv($self, $field[1], $field[2], $sfi, $k, $i, @field[6..8], $r);
765 # dbg('local', "Local::wwv2 error $@") if $@;
768 # DON'T be silly and send on PC27s!
769 return if $pcno == 27;
771 # broadcast to the eager world
772 send_wwv_spot($self, $line, $d, $field[2], $sfi, $k, $i, @field[6..8]);
776 if ($pcno == 24) { # set here status
777 my $call = uc $field[1];
778 my $ref = DXCluster->get_exact($call);
779 $ref->here($field[2]) if $ref;
783 if ($pcno == 25) { # merge request
784 if ($field[1] ne $main::mycall) {
785 $self->route($field[1], $line);
788 if ($field[2] eq $main::mycall) {
789 dbg('chan', "PCPROT: Trying to merge to myself, ignored");
793 Log('DXProt', "Merge request for $field[3] spots and $field[4] WWV from $field[1]");
797 my @in = reverse Spot::search(1, undef, undef, 0, $field[3]);
800 $self->send(pc26(@{$in}[0..4], $field[2]));
806 my @in = reverse Geomag::search(0, $field[4], time, 1);
809 $self->send(pc27(@{$in}[0..5], $field[2]));
815 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
816 if ($pcno == 49 || $field[1] eq $main::mycall) {
817 DXMsg::process($self, $line);
819 $self->route($field[1], $line) unless $self->is_clx;
824 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
825 if ($field[1] eq $main::mycall) {
826 my $ref = DXUser->get_current($field[2]);
827 my $cref = DXCluster->get($field[2]);
828 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
829 unless (!$cref || !$ref || $cref->mynode->call ne $ref->homenode) { # not allowed to relay RCMDS!
830 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
831 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
832 my $oldpriv = $self->{priv};
833 $self->{priv} = $ref->{priv}; # assume the user's privilege level
834 my @in = (DXCommandmode::run_cmd($self, $field[3]));
835 $self->{priv} = $oldpriv;
838 $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
839 Log('rcmd', 'out', $field[2], $_);
841 delete $self->{remotecmd};
843 $self->send(pc35($main::mycall, $field[2], "$main::mycall:sorry...!"));
846 $self->send(pc35($main::mycall, $field[2], "$main::mycall:your attempt is logged, Tut tut tut...!"));
849 my $ref = DXUser->get_current($field[1]);
850 if ($ref && $ref->is_clx) {
851 route($field[1], pc84($field[2], $field[1], $field[2], $field[3]));
853 $self->route($field[1], $line);
859 if ($pcno == 35) { # remote command replies
860 if ($field[1] eq $main::mycall) {
861 my $s = $rcmds{$field[2]};
863 my $dxchan = DXChannel->get($s->{call});
864 $dxchan->send($field[3]) if $dxchan;
865 delete $rcmds{$field[2]} if !$dxchan;
867 # send unsolicited ones to the sysop
868 my $dxchan = DXChannel->get($main::myalias);
869 $dxchan->send($field[3]) if $dxchan;
872 my $ref = DXUser->get_current($field[1]);
873 if ($ref && $ref->is_clx) {
874 route($field[1], pc85($field[2], $field[1], $field[2], $field[3]));
876 $self->route($field[1], $line);
882 # for pc 37 see 44 onwards
884 if ($pcno == 38) { # node connected list from neighbour
888 if ($pcno == 39) { # incoming disconnect
889 if ($field[1] eq $self->{call}) {
890 $self->disconnect(1);
892 dbg('chan', "PCPROT: came in on wrong channel");
897 if ($pcno == 41) { # user info
898 # add this station to the user database, if required
899 my $user = DXUser->get_current($field[1]);
900 $user = DXUser->new($field[1]) if !$user;
902 if ($field[2] == 1) {
903 $user->name($field[3]);
904 } elsif ($field[2] == 2) {
905 $user->qth($field[3]);
906 } elsif ($field[2] == 3) {
907 my ($lat, $long) = DXBearing::stoll($field[3]);
910 $user->qra(DXBearing::lltoqra($lat, $long)) unless $user->qra && DXBearing::is_qra($user->qra);
911 } elsif ($field[2] == 4) {
912 $user->homenode($field[3]);
914 $user->lastoper($main::systime); # to cut down on excessive for/opers being generated
921 if ($pcno == 37 || $pcno == 44 || $pcno == 45 || $pcno == 46 || $pcno == 47 || $pcno == 48) {
922 DXDb::process($self, $line);
926 if ($pcno == 50) { # keep alive/user list
927 my $node = DXCluster->get_exact($field[1]);
929 return unless $node->isa('DXNode');
930 return unless $node->dxchan == $self;
931 $node->update_users($field[2]);
936 if ($pcno == 51) { # incoming ping requests/answers
939 if ($field[1] eq $main::mycall) {
940 my $flag = $field[3];
942 $self->send(pc51($field[2], $field[1], '0'));
944 # it's a reply, look in the ping list for this one
945 my $ref = $pings{$field[2]};
947 my $tochan = DXChannel->get($field[2]);
950 my $dxchan = DXChannel->get($r->{call});
952 my $t = tv_interval($r->{t}, [ gettimeofday ]);
953 if ($dxchan->is_user) {
954 my $s = sprintf "%.2f", $t;
955 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
956 $dxchan->send($dxchan->msg('pingi', $field[2], $s, $ave))
957 } elsif ($dxchan->is_node) {
959 $tochan->{nopings} = 2; # pump up the timer
960 push @{$tochan->{pingtime}}, $t;
961 shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
963 for (@{$tochan->{pingtime}}) {
966 $tochan->{pingave} = $st / @{$tochan->{pingtime}};
973 # route down an appropriate thingy
974 $self->route($field[1], $line);
979 if ($pcno == 75) { # dunno but route it
980 if ($field[1] ne $main::mycall) {
981 $self->route($field[1], $line);
986 if ($pcno == 73) { # WCY broadcasts
989 my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
990 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
991 dbg('chan', "PCPROT: WCY Date ($field[1] $field[2]) out of range");
994 @field = map { unpad($_) } @field;
995 if (WCY::dup($d,@field[3..7])) {
996 dbg('chan', "PCPROT: Dup WCY Spot ignored\n");
1000 my $wcy = WCY::update($d, @field[2..12]);
1004 $rep = Local::wwv($self, @field[1..12]);
1006 # dbg('local', "Local::wcy error $@") if $@;
1009 # broadcast to the eager world
1010 send_wcy_spot($self, $line, $d, @field[2..12]);
1014 if ($pcno == 84) { # remote commands (incoming)
1015 if ($field[1] eq $main::mycall) {
1016 my $ref = DXUser->get_current($field[2]);
1017 my $cref = DXCluster->get($field[2]);
1018 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[4]);
1019 unless ($field[4] =~ /rcmd/i || !$cref || !$ref || $cref->mynode->call ne $ref->homenode) { # not allowed to relay RCMDS!
1020 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
1021 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
1022 my $oldpriv = $self->{priv};
1023 $self->{priv} = $ref->{priv}; # assume the user's privilege level
1024 my @in = (DXCommandmode::run_cmd($self, $field[4]));
1025 $self->{priv} = $oldpriv;
1028 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:$_"));
1029 Log('rcmd', 'out', $field[2], $_);
1031 delete $self->{remotecmd};
1033 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:sorry...!"));
1036 $self->send(pc85($main::mycall, $field[2], $field[3],"$main::mycall:your attempt is logged, Tut tut tut...!"));
1039 my $ref = DXUser->get_current($field[1]);
1040 if ($ref && $ref->is_clx) {
1041 $self->route($field[1], $line);
1043 route($field[1], pc34($field[2], $field[1], $field[4]));
1049 if ($pcno == 85) { # remote command replies
1050 if ($field[1] eq $main::mycall) {
1051 my $dxchan = DXChannel->get($field[3]);
1053 $dxchan->send($field[4]);
1055 my $s = $rcmds{$field[2]};
1057 $dxchan = DXChannel->get($s->{call});
1058 $dxchan->send($field[4]) if $dxchan;
1059 delete $rcmds{$field[2]} if !$dxchan;
1061 # send unsolicited ones to the sysop
1062 my $dxchan = DXChannel->get($main::myalias);
1063 $dxchan->send($field[4]) if $dxchan;
1067 my $ref = DXUser->get_current($field[1]);
1068 if ($ref && $ref->is_clx) {
1069 $self->route($field[1], $line);
1071 route($field[1], pc35($field[2], $field[1], $field[4]));
1078 # if get here then rebroadcast the thing with its Hop count decremented (if
1079 # there is one). If it has a hop count and it decrements to zero then don't
1082 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
1086 unless ($self->{isolate}) {
1087 broadcast_ak1a($line, $self); # send it to everyone but me
1092 # This is called from inside the main cluster processing loop and is used
1093 # for despatching commands that are doing some long processing job
1098 my @dxchan = DXChannel->get_all();
1101 foreach $dxchan (@dxchan) {
1102 next unless $dxchan->is_node();
1103 next if $dxchan == $me;
1105 # send a pc50 out on this channel
1106 if ($t >= $dxchan->pc50_t + $DXProt::pc50_interval) {
1107 $dxchan->send(pc50(scalar DXChannel::get_all_users));
1108 $dxchan->pc50_t($t);
1111 # send a ping out on this channel
1112 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
1113 if ($dxchan->{nopings} <= 0) {
1114 $dxchan->disconnect;
1116 addping($main::mycall, $dxchan->call);
1117 $dxchan->{nopings} -= 1;
1118 $dxchan->{lastping} = $t;
1126 if ($main::systime - 3600 > $last_hour) {
1130 $last_hour = $main::systime;
1135 # finish up a pc context
1140 my $call = $self->call;
1142 my $ref = DXCluster->get_exact($call);
1144 # unbusy and stop and outgoing mail
1145 my $mref = DXMsg::get_busy($call);
1146 $mref->stop_msg($call) if $mref;
1148 # broadcast to all other nodes that all the nodes connected to via me are gone
1149 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
1152 foreach $node (@gonenodes) {
1153 next if $node->call eq $call;
1154 broadcast_ak1a(pc21($node->call, 'Gone') , $self) unless $self->{isolate};
1158 # remove outstanding pings
1159 delete $pings{$call};
1161 # now broadcast to all other ak1a nodes that I have gone
1162 broadcast_ak1a(pc21($call, 'Gone.'), $self) unless $self->{isolate};
1164 # I was the last node visited
1165 $self->user->node($main::mycall);
1167 # send info to all logged in thingies
1168 $self->tell_login('logoutn');
1170 Log('DXProt', $call . " Disconnected");
1171 $ref->del() if $ref;
1175 # some active measures
1181 my @dxchan = DXChannel->get_all();
1184 # send it if it isn't the except list and isn't isolated and still has a hop count
1185 # taking into account filtering and so on
1186 foreach $dxchan (@dxchan) {
1188 my ($filter, $hops);
1190 if ($dxchan->{spotsfilter}) {
1191 ($filter, $hops) = $dxchan->{spotsfilter}->it(@_, $self->{call} );
1192 next unless $filter;
1195 if ($dxchan->is_node) {
1196 next if $dxchan == $self;
1199 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1201 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1202 next unless $routeit;
1205 $dxchan->send($routeit) if $routeit;
1207 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1209 } elsif ($dxchan->is_user && $dxchan->{dx}) {
1210 my $buf = Spot::formatb($dxchan->{user}->wantgrid, $_[0], $_[1], $_[2], $_[3], $_[4]);
1211 $buf .= "\a\a" if $dxchan->{beep};
1213 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1214 $dxchan->send($buf);
1216 $dxchan->delay($buf);
1226 my @dxchan = DXChannel->get_all();
1229 # send it if it isn't the except list and isn't isolated and still has a hop count
1230 # taking into account filtering and so on
1231 foreach $dxchan (@dxchan) {
1233 my ($filter, $hops);
1235 if ($dxchan->{wwvfilter}) {
1236 ($filter, $hops) = $dxchan->{wwvfilter}->it(@_, $self->{call} );
1237 next unless $filter;
1239 if ($dxchan->is_node) {
1240 next if $dxchan == $self;
1243 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1245 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1246 next unless $routeit;
1249 $dxchan->send($routeit) if $routeit;
1251 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1254 } elsif ($dxchan->is_user && $dxchan->{wwv}) {
1255 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
1256 $buf .= "\a\a" if $dxchan->{beep};
1257 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1258 $dxchan->send($buf);
1260 $dxchan->delay($buf);
1270 my @dxchan = DXChannel->get_all();
1273 # send it if it isn't the except list and isn't isolated and still has a hop count
1274 # taking into account filtering and so on
1275 foreach $dxchan (@dxchan) {
1277 my ($filter, $hops);
1279 if ($dxchan->{wcyfilter}) {
1280 ($filter, $hops) = $dxchan->{wcyfilter}->it(@_, $self->{call} );
1281 next unless $filter;
1283 if ($dxchan->is_clx || $dxchan->is_spider || $dxchan->is_dxnet) {
1284 next if $dxchan == $self;
1287 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1289 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1290 next unless $routeit;
1293 $dxchan->send($routeit) if $routeit;
1295 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1297 } elsif ($dxchan->is_user && $dxchan->{wcy}) {
1298 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1299 $buf .= "\a\a" if $dxchan->{beep};
1300 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1301 $dxchan->send($buf);
1303 $dxchan->delay($buf);
1314 my @dxchan = DXChannel->get_all();
1316 my $text = unpad($_[2]);
1320 if ($_[3] eq '*') { # sysops
1322 } elsif ($_[3] gt ' ') { # speciality list handling
1323 my ($name) = split /\./, $_[3];
1324 $target = "$name"; # put the rest in later (if bothered)
1331 $target = "All" if !$target;
1333 Log('ann', $target, $_[0], $text);
1335 # send it if it isn't the except list and isn't isolated and still has a hop count
1336 # taking into account filtering and so on
1337 foreach $dxchan (@dxchan) {
1339 my ($filter, $hops);
1341 if ($dxchan->{annfilter}) {
1342 my ($ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1343 my @dxcc = Prefix::extract($_[0]);
1345 $ann_dxcc = $dxcc[1]->dxcc;
1346 $ann_itu = $dxcc[1]->itu;
1347 $ann_cq = $dxcc[1]->cq;
1349 @dxcc = Prefix::extract($_[4]);
1351 $org_dxcc = $dxcc[1]->dxcc;
1352 $org_itu = $dxcc[1]->itu;
1353 $org_cq = $dxcc[1]->cq;
1355 ($filter, $hops) = $dxchan->{annfilter}->it(@_, $self->{call}, $ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq);
1356 next unless $filter;
1358 if ($dxchan->is_node && $_[1] ne $main::mycall) { # i.e not specifically routed to me
1359 next if $dxchan == $self;
1362 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1364 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1365 next unless $routeit;
1368 $dxchan->send($routeit) if $routeit;
1370 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1373 } elsif ($dxchan->is_user) {
1374 unless ($dxchan->{ann}) {
1375 next if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
1377 next if $target eq 'SYSOP' && $dxchan->{priv} < 5;
1378 my $buf = "$to$target de $_[0]: $text";
1380 $buf .= "\a\a" if $dxchan->{beep};
1381 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1382 $dxchan->send($buf);
1384 $dxchan->delay($buf);
1390 sub send_local_config
1399 if ($self->{isolate}) {
1400 @localnodes = (DXCluster->get_exact($main::mycall));
1402 # create a list of all the nodes that are not connected to this connection
1403 # and are not themselves isolated, this to make sure that isolated nodes
1404 # don't appear outside of this node
1405 @nodes = DXNode::get_all();
1406 @nodes = grep { $_->{call} ne $main::mycall } @nodes;
1407 @nodes = grep { $_->dxchan != $self } @nodes if @nodes;
1408 @nodes = grep { !$_->dxchan->{isolate} } @nodes if @nodes;
1409 @localnodes = grep { $_->dxchan->{call} eq $_->{call} } @nodes if @nodes;
1410 unshift @localnodes, DXCluster->get_exact($main::mycall);
1411 @remotenodes = grep { $_->dxchan->{call} ne $_->{call} } @nodes if @nodes;
1414 my @s = $me->pc19(@localnodes, @remotenodes);
1416 my $routeit = adjust_hops($self, $_);
1417 $self->send($routeit) if $routeit;
1420 # get all the users connected on the above nodes and send them out
1421 foreach $n (@localnodes, @remotenodes) {
1422 my @users = values %{$n->list};
1423 my @s = pc16($n, @users);
1425 my $routeit = adjust_hops($self, $_);
1426 $self->send($routeit) if $routeit;
1432 # route a message down an appropriate interface for a callsign
1434 # is called route(to, pcline);
1438 my ($self, $call, $line) = @_;
1440 if (ref $self && $call eq $self->{call}) {
1441 dbg('chan', "PCPROT: Trying to route back to source, dropped");
1445 # always send it down the local interface if available
1446 my $dxchan = DXChannel->get($call);
1448 my $cl = DXCluster->get_exact($call);
1449 $dxchan = $cl->dxchan if $cl;
1452 my $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1454 $dxchan->send($routeit);
1459 # broadcast a message to all clusters taking into account isolation
1460 # [except those mentioned after buffer]
1463 my $s = shift; # the line to be rebroadcast
1464 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1465 my @dxchan = DXChannel::get_all_nodes();
1468 # send it if it isn't the except list and isn't isolated and still has a hop count
1469 foreach $dxchan (@dxchan) {
1470 next if grep $dxchan == $_, @except;
1471 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
1472 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
1476 # broadcast a message to all clusters ignoring isolation
1477 # [except those mentioned after buffer]
1478 sub broadcast_all_ak1a
1480 my $s = shift; # the line to be rebroadcast
1481 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1482 my @dxchan = DXChannel::get_all_nodes();
1485 # send it if it isn't the except list and isn't isolated and still has a hop count
1486 foreach $dxchan (@dxchan) {
1487 next if grep $dxchan == $_, @except;
1488 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
1489 $dxchan->send($routeit);
1493 # broadcast to all users
1494 # storing the spot or whatever until it is in a state to receive it
1497 my $s = shift; # the line to be rebroadcast
1498 my $sort = shift; # the type of transmission
1499 my $fref = shift; # a reference to an object to filter on
1500 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1501 my @dxchan = DXChannel::get_all_users();
1505 foreach $dxchan (@dxchan) {
1506 next if grep $dxchan == $_, @except;
1509 broadcast_list($s, $sort, $fref, @out);
1512 # broadcast to a list of users
1520 foreach $dxchan (@_) {
1523 if ($sort eq 'dx') {
1524 next unless $dxchan->{dx};
1525 ($filter) = $dxchan->{spotsfilter}->it(@{$fref}) if ref $fref;
1526 next unless $filter;
1528 next if $sort eq 'ann' && !$dxchan->{ann};
1529 next if $sort eq 'wwv' && !$dxchan->{wwv};
1530 next if $sort eq 'wcy' && !$dxchan->{wcy};
1531 next if $sort eq 'wx' && !$dxchan->{wx};
1533 $s =~ s/\a//og unless $dxchan->{beep};
1535 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1545 # obtain the hops from the list for this callsign and pc no
1551 my $hops = $DXProt::hopcount{$pcno};
1552 $hops = $DXProt::def_hopcount if !$hops;
1557 # adjust the hop count on a per node basis using the user loadable
1558 # hop table if available or else decrement an existing one
1565 my $call = $self->{call};
1568 if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
1569 my ($pcno) = $s =~ /^PC(\d\d)/o;
1570 confess "$call called adjust_hops with '$s'" unless $pcno;
1571 my $ref = $nodehops{$call} if %nodehops;
1573 my $newhops = $ref->{$pcno};
1574 return "" if defined $newhops && $newhops == 0;
1575 $newhops = $ref->{default} unless $newhops;
1576 return "" if defined $newhops && $newhops == 0;
1577 $newhops = $hops if !$newhops;
1578 $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
1580 # simply decrement it
1582 return "" if !$hops;
1583 $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
1595 return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1596 do "$main::data/hop_table.pl";
1602 # add a ping request to the ping queues
1605 my ($from, $to) = @_;
1606 my $ref = $pings{$to} || [];
1609 $r->{t} = [ gettimeofday ];
1610 route(undef, $to, pc51($to, $main::mycall, 1));
1615 # add a rcmd request to the rcmd queues
1618 my ($self, $to, $cmd) = @_;
1621 $r->{call} = $self->{call};
1622 $r->{t} = $main::systime;
1626 my $ref = DXCluster->get_exact($to);
1627 if ($ref && $ref->dxchan && $ref->dxchan->is_clx) {
1628 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1630 route(undef, $to, pc34($main::mycall, $to, $cmd));
1639 if ($self->{conn} && !$nopc39) {
1640 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1643 $self->SUPER::disconnect;
1648 # send a talk message to this thingy
1652 my ($self, $from, $to, $via, $line) = @_;
1654 $line =~ s/\^/\\5E/g; # remove any ^ characters
1655 $self->send(DXProt::pc10($from, $to, $via, $line));
1656 Log('talk', $self->call, $from, $via?$via:$main::mycall, $line);