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 c 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 # now prime the spot and wwv duplicates file with data
182 # my @today = Julian::unixtoj(time);
183 # for (Spot::readfile(@today), Spot::readfile(Julian::sub(@today, 1))) {
184 # Spot::dup(@{$_}[0..3]);
186 # for (Geomag::readfile(time)) {
187 # Geomag::dup(@{$_}[1..5]);
190 # load the baddx file
191 do "$baddxfn" if -e "$baddxfn";
196 # obtain a new connection this is derived from dxchannel
201 my $self = DXChannel::alloc(@_);
205 # this is how a pc connection starts (for an incoming connection)
206 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
207 # all the crap that comes between).
210 my ($self, $line, $sort) = @_;
211 my $call = $self->{call};
212 my $user = $self->{user};
214 # remember type of connection
215 $self->{consort} = $line;
216 $self->{outbound} = $sort eq 'O';
217 $self->{priv} = $user->priv || 1; # other clusters can always be 'normal' users
218 $self->{lang} = $user->lang || 'en';
219 $self->{isolate} = $user->{isolate};
220 $self->{consort} = $line; # save the connection type
223 # get the INPUT filters (these only pertain to Clusters)
224 $self->{inspotfilter} = Filter::read_in('spots', $call, 1);
225 $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1);
226 $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1);
227 $self->{inannfilter} = Filter::read_in('ann', $call, 1);
229 # set unbuffered and no echo
230 $self->send_now('B',"0");
231 $self->send_now('E',"0");
233 # ping neighbour node stuff
234 my $ping = $user->pingint;
235 $ping = 5*60 unless defined $ping;
236 $self->{pingint} = $ping;
237 $self->{nopings} = $user->nopings || 2;
238 $self->{pingtime} = [ ];
239 $self->{pingave} = 0;
241 # send initialisation string
242 unless ($self->{outbound}) {
243 # $self->send(pc38()) if DXNode->get_all();
245 $self->{lastping} = $main::systime;
247 # remove from outstanding connects queue
248 @main::outstanding_connects = grep {$_->{call} ne $call} @main::outstanding_connects;
249 $self->{lastping} = $main::systime + $self->pingint / 2;
251 $self->state('init');
254 # send info to all logged in thingies
255 $self->tell_login('loginn');
257 Log('DXProt', "$call connected");
261 # This is the normal pcxx despatcher
265 my ($self, $line) = @_;
266 my @field = split /\^/, $line;
267 return unless @field;
269 pop @field if $field[-1] eq '~';
271 # print join(',', @field), "\n";
273 # ignore any lines that don't start with PC
274 return if !$field[0] =~ /^PC/;
277 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
279 return if $pcno < 10 || $pcno > 99;
281 # check for and dump bad protocol messages
282 my $n = check($pcno, @field);
284 dbg('chan', "bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")");
291 $pcr = Local::pcprot($self, $pcno, @field);
293 # dbg('local', "Local::pcprot error $@") if $@;
297 if ($pcno == 10) { # incoming talk
299 # will we allow it at all?
302 if (@bad = BadWords::check($field[3])) {
303 dbg('chan', "Bad words: @bad, dropped" );
308 # is it for me or one of mine?
309 my ($to, $via, $call, $dxchan);
310 if ($field[5] gt ' ') {
311 $call = $via = $field[2];
314 $call = $to = $field[2];
316 $dxchan = DXChannel->get($call);
317 if ($dxchan && $dxchan->is_user) {
318 $field[3] =~ s/\%5E/^/g;
319 $dxchan->talk($field[1], $to, $via, $field[3]);
321 $self->route($field[2], $line); # relay it on its way
326 if ($pcno == 11 || $pcno == 26) { # dx spot
328 # route 'foreign' pc26s
330 if ($field[7] ne $main::mycall) {
331 $self->route($field[7], $line);
336 # if this is a 'nodx' node then ignore it
337 if (grep $field[7] =~ /^$_/, @DXProt::nodx_node) {
338 dbg('chan', "Bad DXNode, dropped");
342 # convert the date to a unix date
343 my $d = cltounix($field[3], $field[4]);
344 # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
345 if (!$d || ($pcno == 11 && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
346 dbg('chan', "Spot ignored, invalid date or out of range ($field[3] $field[4])\n");
351 if (grep $field[2] eq $_, @baddx) {
352 dbg('chan', "Bad DX spot, ignored");
357 $field[5] =~ s/^\s+//; # take any leading blanks off
358 if (Spot::dup($field[1], $field[2], $d, $field[5])) {
359 dbg('chan', "Duplicate Spot ignored\n");
364 if (@bad = BadWords::check($field[5])) {
365 dbg('chan', "Bad words: @bad, dropped" );
370 my @spot = Spot::add($field[1], $field[2], $d, $field[5], $field[6], $field[7]);
373 # @spot at this point contains:-
374 # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
375 # then spotted itu, spotted cq, spotters itu, spotters cq
376 # you should be able to route on any of these
379 # fix up qra locators of known users
380 my $user = DXUser->get_current($spot[4]);
382 my $qra = $user->qra;
383 unless ($qra && DXBearing::is_qra($qra)) {
384 my $lat = $user->lat;
385 my $long = $user->long;
386 if (defined $lat && defined $long) {
387 $user->qra(DXBearing::lltoqra($lat, $long));
392 # send a remote command to a distant cluster if it is visible and there is no
393 # qra locator and we havn't done it for a month.
395 unless ($user->qra) {
397 my $to = $user->homenode;
398 my $last = $user->lastoper || 0;
399 if ($send_opernam && $main::systime > $last + $DXUser::lastoperinterval && $to && ($node = DXCluster->get_exact($to)) ) {
400 my $cmd = "forward/opernam $spot[4]";
401 # send the rcmd but we aren't interested in the replies...
402 if ($node && $node->dxchan && $node->dxchan->is_clx) {
403 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
405 route(undef, $to, pc34($main::mycall, $to, $cmd));
407 if ($to ne $field[7]) {
409 $node = DXCluster->get_exact($to);
410 if ($node && $node->dxchan && $node->dxchan->is_clx) {
411 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
413 route(undef, $to, pc34($main::mycall, $to, $cmd));
416 $user->lastoper($main::systime);
425 $r = Local::spot($self, @spot);
427 # dbg('local', "Local::spot1 error $@") if $@;
430 # DON'T be silly and send on PC26s!
431 return if $pcno == 26;
433 # send out the filtered spots
434 send_dx_spot($self, $line, @spot) if @spot;
438 if ($pcno == 12) { # announces
439 # announce duplicate checking
440 $field[3] =~ s/^\s+//; # remove leading blanks
441 if (AnnTalk::dup($field[1], $field[2], $field[3])) {
442 dbg('chan', "Duplicate Announce ignored");
448 if (@bad = BadWords::check($field[3])) {
449 dbg('chan', "Bad words: @bad, dropped" );
454 if ($field[2] eq '*' || $field[2] eq $main::mycall) {
456 # global ann filtering on INPUT
457 if ($self->{inannfilter}) {
458 my ($filter, $hops) = Filter::it($self->{inannfilter}, @field[1..6], $self->{call} );
460 dbg('chan', "Rejected by filter");
466 $self->send_announce($line, @field[1..6]);
468 $self->route($field[2], $line);
484 if ($pcno == 16) { # add a user
485 my $node = DXCluster->get_exact($field[1]);
487 if (!$node && ($dxchan = DXChannel->get($field[1]))) {
488 # add it to the node table if it isn't present and it's
490 $node = DXNode->new($dxchan, $field[1], 0, 1, 5400);
491 broadcast_ak1a(pc19($dxchan, $node), $dxchan, $self) unless $dxchan->{isolate};
494 return unless $node; # ignore if havn't seen a PC19 for this one yet
495 return unless $node->isa('DXNode');
496 if ($node->dxchan != $self) {
497 dbg('chan', "LOOP: $field[1] came in on wrong channel");
500 if (($dxchan = DXChannel->get($field[1])) && $dxchan != $self) {
501 dbg('chan', "LOOP: $field[1] connected locally");
506 for ($i = 2; $i < $#field; $i++) {
507 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (\S) (\d)/o;
508 next if !$call || length $call < 3 || length $call > 8;
511 next if DXCluster->get_exact($call); # we already have this (loop?)
513 $confmode = $confmode eq '*';
514 DXNodeuser->new($self, $node, $call, $confmode, $here);
516 # add this station to the user database, if required
517 $call =~ s/-\d+$//o; # remove ssid for users
518 my $user = DXUser->get_current($call);
519 $user = DXUser->new($call) if !$user;
520 $user->homenode($node->call) if !$user->homenode;
521 $user->node($node->call);
522 $user->lastin($main::systime) unless DXChannel->get($call);
526 # queue up any messages (look for privates only)
527 DXMsg::queue_msg(1) if $self->state eq 'normal';
531 if ($pcno == 17) { # remove a user
532 my $node = DXCluster->get_exact($field[2]);
534 if (!$node && ($dxchan = DXChannel->get($field[2]))) {
535 # add it to the node table if it isn't present and it's
537 $node = DXNode->new($dxchan, $field[2], 0, 1, 5400);
538 broadcast_ak1a(pc19($dxchan, $node), $dxchan, $self) unless $dxchan->{isolate};
542 return unless $node->isa('DXNode');
543 if ($node->dxchan != $self) {
544 dbg('chan', "LOOP: $field[2] came in on wrong channel");
547 if (($dxchan = DXChannel->get($field[2])) && $dxchan != $self) {
548 dbg('chan', "LOOP: $field[2] connected locally");
551 my $ref = DXCluster->get_exact($field[1]);
556 if ($pcno == 18) { # link request
557 $self->state('init');
559 # first clear out any nodes on this dxchannel
560 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
561 foreach my $node (@gonenodes) {
562 next if $node->dxchan == $DXProt::me;
563 broadcast_ak1a(pc21($node->call, 'Gone, re-init') , $self) unless $self->{isolate};
566 $self->send_local_config();
568 return; # we don't pass these on
571 if ($pcno == 19) { # incoming cluster list
573 my $newline = "PC19^";
574 for ($i = 1; $i < $#field-1; $i += 4) {
575 my $here = $field[$i];
576 my $call = uc $field[$i+1];
577 my $confmode = $field[$i+2];
578 my $ver = $field[$i+3];
580 $ver = 5400 if !$ver && $allowzero;
582 # now check the call over
583 my $node = DXCluster->get_exact($call);
586 if (($dxchan = DXChannel->get($call)) && $dxchan != $self) {
587 dbg('chan', "LOOP: $call connected locally");
589 if ($node->dxchan != $self) {
590 dbg('chan', "LOOP: $call come in on wrong channel");
593 dbg('chan', "already have $call");
597 # check for sane parameters
598 next if $ver < 5000; # only works with version 5 software
599 next if length $call < 3; # min 3 letter callsigns
601 # add it to the nodes table and outgoing line
602 $newline .= "$here^$call^$confmode^$ver^";
603 DXNode->new($self, $call, $confmode, $here, $ver);
605 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
606 my $mref = DXMsg::get_busy($call);
607 $mref->stop_msg($call) if $mref;
609 # add this station to the user database, if required (don't remove SSID from nodes)
610 my $user = DXUser->get_current($call);
612 $user = DXUser->new($call);
614 $user->priv(1); # I have relented and defaulted nodes
615 $self->{priv} = 1; # to user RCMDs allowed
616 $user->homenode($call);
619 $user->lastin($main::systime) unless DXChannel->get($call);
623 return if $newline eq "PC19^";
626 $newline .= get_hops(19) . "^";
631 if ($pcno == 20) { # send local configuration
632 $self->send_local_config();
634 $self->state('normal');
638 if ($pcno == 21) { # delete a cluster from the list
639 my $call = uc $field[1];
640 if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
641 my $node = DXCluster->get_exact($call);
643 if ($call eq $self->{call}) {
644 dbg('chan', "LOOP: Trying to disconnect myself with PC21");
647 if ($node->dxchan != $self) {
648 dbg('chan', "LOOP: $call come in on wrong channel");
652 if ($dxchan = DXChannel->get($call)) {
653 dbg('chan', "LOOP: $call connected locally");
658 dbg('chan', "$call not in table, dropped");
666 $self->state('normal');
670 if ($pcno == 23 || $pcno == 27) { # WWV info
672 # route 'foreign' pc27s
674 if ($field[8] ne $main::mycall) {
675 $self->route($field[8], $line);
681 my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
682 my $sfi = unpad($field[3]);
683 my $k = unpad($field[4]);
684 my $i = unpad($field[5]);
685 my ($r) = $field[6] =~ /R=(\d+)/;
687 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
688 dbg('chan', "WWV Date ($field[1] $field[2]) out of range");
691 if (Geomag::dup($d,$sfi,$k,$i,$field[6])) {
692 dbg('chan', "Dup WWV Spot ignored\n");
695 $field[7] =~ s/-\d+$//o; # remove spotter's ssid
697 my $wwv = Geomag::update($d, $field[2], $sfi, $k, $i, @field[6..8], $r);
701 $rep = Local::wwv($self, $field[1], $field[2], $sfi, $k, $i, @field[6..8], $r);
703 # dbg('local', "Local::wwv2 error $@") if $@;
706 # DON'T be silly and send on PC27s!
707 return if $pcno == 27;
709 # broadcast to the eager world
710 send_wwv_spot($self, $line, $d, $field[2], $sfi, $k, $i, @field[6..8]);
714 if ($pcno == 24) { # set here status
715 my $call = uc $field[1];
716 my $ref = DXCluster->get_exact($call);
717 $ref->here($field[2]) if $ref;
721 if ($pcno == 25) { # merge request
722 if ($field[1] ne $main::mycall) {
723 $self->route($field[1], $line);
726 if ($field[2] eq $main::mycall) {
727 dbg('chan', "Trying to merge to myself, ignored");
731 Log('DXProt', "Merge request for $field[3] spots and $field[4] WWV from $field[1]");
735 my @in = reverse Spot::search(1, undef, undef, 0, $field[3]);
738 $self->send(pc26(@{$in}[0..4], $field[2]));
744 my @in = reverse Geomag::search(0, $field[4], time, 1);
747 $self->send(pc27(@{$in}[0..5], $field[2]));
753 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
754 if ($pcno == 49 || $field[1] eq $main::mycall) {
755 DXMsg::process($self, $line);
757 $self->route($field[1], $line) unless $self->is_clx;
762 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
763 if ($field[1] eq $main::mycall) {
764 my $ref = DXUser->get_current($field[2]);
765 my $cref = DXCluster->get($field[2]);
766 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
767 unless (!$cref || !$ref || $cref->mynode->call ne $ref->homenode) { # not allowed to relay RCMDS!
768 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
769 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
770 my $oldpriv = $self->{priv};
771 $self->{priv} = $ref->{priv}; # assume the user's privilege level
772 my @in = (DXCommandmode::run_cmd($self, $field[3]));
773 $self->{priv} = $oldpriv;
776 $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
777 Log('rcmd', 'out', $field[2], $_);
779 delete $self->{remotecmd};
781 $self->send(pc35($main::mycall, $field[2], "$main::mycall:sorry...!"));
784 $self->send(pc35($main::mycall, $field[2], "$main::mycall:your attempt is logged, Tut tut tut...!"));
787 my $ref = DXUser->get_current($field[1]);
788 if ($ref && $ref->is_clx) {
789 route($field[1], pc84($field[2], $field[1], $field[2], $field[3]));
791 $self->route($field[1], $line);
797 if ($pcno == 35) { # remote command replies
798 if ($field[1] eq $main::mycall) {
799 my $s = $rcmds{$field[2]};
801 my $dxchan = DXChannel->get($s->{call});
802 $dxchan->send($field[3]) if $dxchan;
803 delete $rcmds{$field[2]} if !$dxchan;
805 # send unsolicited ones to the sysop
806 my $dxchan = DXChannel->get($main::myalias);
807 $dxchan->send($field[3]) if $dxchan;
810 my $ref = DXUser->get_current($field[1]);
811 if ($ref && $ref->is_clx) {
812 route($field[1], pc85($field[2], $field[1], $field[2], $field[3]));
814 $self->route($field[1], $line);
820 # for pc 37 see 44 onwards
822 if ($pcno == 38) { # node connected list from neighbour
826 if ($pcno == 39) { # incoming disconnect
827 $self->disconnect(1);
831 if ($pcno == 41) { # user info
832 # add this station to the user database, if required
833 my $user = DXUser->get_current($field[1]);
835 # then try without an SSID
836 $field[1] =~ s/-\d+$//o;
837 $user = DXUser->get_current($field[1]);
839 $user = DXUser->new($field[1]) if !$user;
841 if ($field[2] == 1) {
842 $user->name($field[3]);
843 } elsif ($field[2] == 2) {
844 $user->qth($field[3]);
845 } elsif ($field[2] == 3) {
846 my ($lat, $long) = DXBearing::stoll($field[3]);
849 $user->qra(DXBearing::lltoqra($lat, $long)) unless $user->qra && DXBearing::is_qra($user->qra);
850 } elsif ($field[2] == 4) {
851 $user->homenode($field[3]);
853 $user->lastoper($main::systime); # to cut down on excessive for/opers being generated
860 if ($pcno == 37 || $pcno == 44 || $pcno == 45 || $pcno == 46 || $pcno == 47 || $pcno == 48) {
861 DXDb::process($self, $line);
865 if ($pcno == 50) { # keep alive/user list
866 my $node = DXCluster->get_exact($field[1]);
868 return unless $node->isa('DXNode');
869 return unless $node->dxchan == $self;
870 $node->update_users($field[2]);
875 if ($pcno == 51) { # incoming ping requests/answers
878 if ($field[1] eq $main::mycall) {
879 my $flag = $field[3];
881 $self->send(pc51($field[2], $field[1], '0'));
883 # it's a reply, look in the ping list for this one
884 my $ref = $pings{$field[2]};
886 my $tochan = DXChannel->get($field[2]);
889 my $dxchan = DXChannel->get($r->{call});
891 my $t = tv_interval($r->{t}, [ gettimeofday ]);
892 if ($dxchan->is_user) {
893 my $s = sprintf "%.2f", $t;
894 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
895 $dxchan->send($dxchan->msg('pingi', $field[2], $s, $ave))
896 } elsif ($dxchan->is_node) {
898 $tochan->{nopings} = 2; # pump up the timer
899 push @{$tochan->{pingtime}}, $t;
900 shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
902 for (@{$tochan->{pingtime}}) {
905 $tochan->{pingave} = $st / @{$tochan->{pingtime}};
912 # route down an appropriate thingy
913 $self->route($field[1], $line);
918 if ($pcno == 75) { # dunno but route it
919 if ($field[1] ne $main::mycall) {
920 $self->route($field[1], $line);
925 if ($pcno == 73) { # WCY broadcasts
928 my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
929 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
930 dbg('chan', "WCY Date ($field[1] $field[2]) out of range");
933 @field = map { unpad($_) } @field;
934 if (WCY::dup($d,@field[3..7])) {
935 dbg('chan', "Dup WCY Spot ignored\n");
939 my $wcy = WCY::update($d, @field[2..12]);
943 $rep = Local::wwv($self, @field[1..12]);
945 # dbg('local', "Local::wcy error $@") if $@;
948 # broadcast to the eager world
949 send_wcy_spot($self, $line, $d, @field[2..12]);
953 if ($pcno == 84) { # remote commands (incoming)
954 if ($field[1] eq $main::mycall) {
955 my $ref = DXUser->get_current($field[2]);
956 my $cref = DXCluster->get($field[2]);
957 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[4]);
958 unless ($field[4] =~ /rcmd/i || !$cref || !$ref || $cref->mynode->call ne $ref->homenode) { # not allowed to relay RCMDS!
959 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
960 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
961 my $oldpriv = $self->{priv};
962 $self->{priv} = $ref->{priv}; # assume the user's privilege level
963 my @in = (DXCommandmode::run_cmd($self, $field[4]));
964 $self->{priv} = $oldpriv;
967 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:$_"));
968 Log('rcmd', 'out', $field[2], $_);
970 delete $self->{remotecmd};
972 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:sorry...!"));
975 $self->send(pc85($main::mycall, $field[2], $field[3],"$main::mycall:your attempt is logged, Tut tut tut...!"));
978 my $ref = DXUser->get_current($field[1]);
979 if ($ref && $ref->is_clx) {
980 $self->route($field[1], $line);
982 route($field[1], pc34($field[2], $field[1], $field[4]));
988 if ($pcno == 85) { # remote command replies
989 if ($field[1] eq $main::mycall) {
990 my $dxchan = DXChannel->get($field[3]);
992 $dxchan->send($field[4]);
994 my $s = $rcmds{$field[2]};
996 $dxchan = DXChannel->get($s->{call});
997 $dxchan->send($field[4]) if $dxchan;
998 delete $rcmds{$field[2]} if !$dxchan;
1000 # send unsolicited ones to the sysop
1001 my $dxchan = DXChannel->get($main::myalias);
1002 $dxchan->send($field[4]) if $dxchan;
1006 my $ref = DXUser->get_current($field[1]);
1007 if ($ref && $ref->is_clx) {
1008 $self->route($field[1], $line);
1010 route($field[1], pc35($field[2], $field[1], $field[4]));
1017 # if get here then rebroadcast the thing with its Hop count decremented (if
1018 # there is one). If it has a hop count and it decrements to zero then don't
1021 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
1025 unless ($self->{isolate}) {
1026 broadcast_ak1a($line, $self); # send it to everyone but me
1031 # This is called from inside the main cluster processing loop and is used
1032 # for despatching commands that are doing some long processing job
1037 my @dxchan = DXChannel->get_all();
1040 foreach $dxchan (@dxchan) {
1041 next unless $dxchan->is_node();
1042 next if $dxchan == $me;
1044 # send a pc50 out on this channel
1045 if ($t >= $dxchan->pc50_t + $DXProt::pc50_interval) {
1046 $dxchan->send(pc50(scalar DXChannel::get_all_users));
1047 $dxchan->pc50_t($t);
1050 # send a ping out on this channel
1051 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
1052 if ($dxchan->{nopings} <= 0) {
1053 $dxchan->disconnect;
1055 addping($main::mycall, $dxchan->call);
1056 $dxchan->{nopings} -= 1;
1057 $dxchan->{lastping} = $t;
1065 if ($main::systime - 3600 > $last_hour) {
1069 $last_hour = $main::systime;
1074 # finish up a pc context
1079 my $call = $self->call;
1081 my $ref = DXCluster->get_exact($call);
1083 # unbusy and stop and outgoing mail
1084 my $mref = DXMsg::get_busy($call);
1085 $mref->stop_msg($call) if $mref;
1087 # broadcast to all other nodes that all the nodes connected to via me are gone
1088 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
1091 foreach $node (@gonenodes) {
1092 next if $node->call eq $call;
1093 broadcast_ak1a(pc21($node->call, 'Gone') , $self) unless $self->{isolate};
1097 # remove outstanding pings
1098 delete $pings{$call};
1100 # now broadcast to all other ak1a nodes that I have gone
1101 broadcast_ak1a(pc21($call, 'Gone.'), $self) unless $self->{isolate};
1103 # I was the last node visited
1104 $self->user->node($main::mycall);
1106 # send info to all logged in thingies
1107 $self->tell_login('logoutn');
1109 Log('DXProt', $call . " Disconnected");
1110 $ref->del() if $ref;
1114 # some active measures
1120 my @dxchan = DXChannel->get_all();
1123 # send it if it isn't the except list and isn't isolated and still has a hop count
1124 # taking into account filtering and so on
1125 foreach $dxchan (@dxchan) {
1127 my ($filter, $hops);
1129 if ($dxchan->{spotfilter}) {
1130 ($filter, $hops) = Filter::it($dxchan->{spotfilter}, @_, $self->{call} );
1131 next unless $filter;
1134 if ($dxchan->is_node) {
1135 next if $dxchan == $self;
1138 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1140 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1141 next unless $routeit;
1144 $dxchan->send($routeit) if $routeit;
1146 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1148 } elsif ($dxchan->is_user && $dxchan->{dx}) {
1149 my $buf = Spot::formatb($dxchan->{user}->wantgrid, $_[0], $_[1], $_[2], $_[3], $_[4]);
1150 $buf .= "\a\a" if $dxchan->{beep};
1152 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1153 $dxchan->send($buf);
1155 $dxchan->delay($buf);
1165 my @dxchan = DXChannel->get_all();
1168 # send it if it isn't the except list and isn't isolated and still has a hop count
1169 # taking into account filtering and so on
1170 foreach $dxchan (@dxchan) {
1172 my ($filter, $hops);
1174 if ($dxchan->{wwvfilter}) {
1175 ($filter, $hops) = Filter::it($dxchan->{wwvfilter}, @_, $self->{call} );
1176 next unless $filter;
1178 if ($dxchan->is_node) {
1179 next if $dxchan == $self;
1182 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1184 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1185 next unless $routeit;
1188 $dxchan->send($routeit) if $routeit;
1190 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1193 } elsif ($dxchan->is_user && $dxchan->{wwv}) {
1194 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
1195 $buf .= "\a\a" if $dxchan->{beep};
1196 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1197 $dxchan->send($buf);
1199 $dxchan->delay($buf);
1209 my @dxchan = DXChannel->get_all();
1212 # send it if it isn't the except list and isn't isolated and still has a hop count
1213 # taking into account filtering and so on
1214 foreach $dxchan (@dxchan) {
1216 my ($filter, $hops);
1218 if ($dxchan->{wcyfilter}) {
1219 ($filter, $hops) = Filter::it($dxchan->{wcyfilter}, @_, $self->{call} );
1220 next unless $filter;
1222 if ($dxchan->is_clx || $dxchan->is_spider) {
1223 next if $dxchan == $self;
1226 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1228 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1229 next unless $routeit;
1232 $dxchan->send($routeit) if $routeit;
1234 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1236 } elsif ($dxchan->is_user && $dxchan->{wcy}) {
1237 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1238 $buf .= "\a\a" if $dxchan->{beep};
1239 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1240 $dxchan->send($buf);
1242 $dxchan->delay($buf);
1253 my @dxchan = DXChannel->get_all();
1255 my $text = unpad($_[2]);
1259 if ($_[3] eq '*') { # sysops
1261 } elsif ($_[3] gt ' ') { # speciality list handling
1262 my ($name) = split /\./, $_[3];
1263 $target = "$name"; # put the rest in later (if bothered)
1270 $target = "All" if !$target;
1272 Log('ann', $target, $_[0], $text);
1274 # send it if it isn't the except list and isn't isolated and still has a hop count
1275 # taking into account filtering and so on
1276 foreach $dxchan (@dxchan) {
1278 my ($filter, $hops);
1280 if ($dxchan->{annfilter}) {
1281 ($filter, $hops) = Filter::it($dxchan->{annfilter}, @_, $self->{call} );
1282 next unless $filter;
1284 if ($dxchan->is_node && $_[1] ne $main::mycall) { # i.e not specifically routed to me
1285 next if $dxchan == $self;
1288 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1290 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1291 next unless $routeit;
1294 $dxchan->send($routeit) if $routeit;
1296 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1299 } elsif ($dxchan->is_user) {
1300 unless ($dxchan->{ann}) {
1301 next if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
1303 next if $target eq 'SYSOP' && $dxchan->{priv} < 5;
1304 my $buf = "$to$target de $_[0]: $text";
1306 $buf .= "\a\a" if $dxchan->{beep};
1307 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1308 $dxchan->send($buf);
1310 $dxchan->delay($buf);
1316 sub send_local_config
1325 if ($self->{isolate}) {
1326 @localnodes = (DXCluster->get_exact($main::mycall));
1328 # create a list of all the nodes that are not connected to this connection
1329 # and are not themselves isolated, this to make sure that isolated nodes
1330 # don't appear outside of this node
1331 @nodes = DXNode::get_all();
1332 @nodes = grep { $_->{call} ne $main::mycall } @nodes;
1333 @nodes = grep { $_->dxchan != $self } @nodes if @nodes;
1334 @nodes = grep { !$_->dxchan->{isolate} } @nodes if @nodes;
1335 @localnodes = grep { $_->dxchan->{call} eq $_->{call} } @nodes if @nodes;
1336 unshift @localnodes, DXCluster->get_exact($main::mycall);
1337 @remotenodes = grep { $_->dxchan->{call} ne $_->{call} } @nodes if @nodes;
1340 my @s = $me->pc19(@localnodes, @remotenodes);
1342 my $routeit = adjust_hops($self, $_);
1343 $self->send($routeit) if $routeit;
1346 # get all the users connected on the above nodes and send them out
1347 foreach $n (@localnodes, @remotenodes) {
1348 my @users = values %{$n->list};
1349 my @s = pc16($n, @users);
1351 my $routeit = adjust_hops($self, $_);
1352 $self->send($routeit) if $routeit;
1358 # route a message down an appropriate interface for a callsign
1360 # is called route(to, pcline);
1364 my ($self, $call, $line) = @_;
1365 my $cl = DXCluster->get_exact($call);
1366 if ($cl) { # don't route it back down itself
1367 if (ref $self && $call eq $self->{call}) {
1368 dbg('chan', "Trying to route back to source, dropped");
1372 my $dxchan = $cl->{dxchan};
1374 my $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1376 $dxchan->send($routeit) if $dxchan;
1382 # broadcast a message to all clusters taking into account isolation
1383 # [except those mentioned after buffer]
1386 my $s = shift; # the line to be rebroadcast
1387 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1388 my @dxchan = DXChannel::get_all_nodes();
1391 # send it if it isn't the except list and isn't isolated and still has a hop count
1392 foreach $dxchan (@dxchan) {
1393 next if grep $dxchan == $_, @except;
1394 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
1395 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
1399 # broadcast a message to all clusters ignoring isolation
1400 # [except those mentioned after buffer]
1401 sub broadcast_all_ak1a
1403 my $s = shift; # the line to be rebroadcast
1404 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1405 my @dxchan = DXChannel::get_all_nodes();
1408 # send it if it isn't the except list and isn't isolated and still has a hop count
1409 foreach $dxchan (@dxchan) {
1410 next if grep $dxchan == $_, @except;
1411 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
1412 $dxchan->send($routeit);
1416 # broadcast to all users
1417 # storing the spot or whatever until it is in a state to receive it
1420 my $s = shift; # the line to be rebroadcast
1421 my $sort = shift; # the type of transmission
1422 my $fref = shift; # a reference to an object to filter on
1423 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1424 my @dxchan = DXChannel::get_all_users();
1428 foreach $dxchan (@dxchan) {
1429 next if grep $dxchan == $_, @except;
1432 broadcast_list($s, $sort, $fref, @out);
1435 # broadcast to a list of users
1443 foreach $dxchan (@_) {
1446 if ($sort eq 'dx') {
1447 next unless $dxchan->{dx};
1448 ($filter) = Filter::it($dxchan->{spotfilter}, @{$fref}) if ref $fref;
1449 next unless $filter;
1451 next if $sort eq 'ann' && !$dxchan->{ann};
1452 next if $sort eq 'wwv' && !$dxchan->{wwv};
1453 next if $sort eq 'wcy' && !$dxchan->{wcy};
1454 next if $sort eq 'wx' && !$dxchan->{wx};
1456 $s =~ s/\a//og unless $dxchan->{beep};
1458 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1468 # obtain the hops from the list for this callsign and pc no
1474 my $hops = $DXProt::hopcount{$pcno};
1475 $hops = $DXProt::def_hopcount if !$hops;
1480 # adjust the hop count on a per node basis using the user loadable
1481 # hop table if available or else decrement an existing one
1488 my $call = $self->{call};
1491 if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
1492 my ($pcno) = $s =~ /^PC(\d\d)/o;
1493 confess "$call called adjust_hops with '$s'" unless $pcno;
1494 my $ref = $nodehops{$call} if %nodehops;
1496 my $newhops = $ref->{$pcno};
1497 return "" if defined $newhops && $newhops == 0;
1498 $newhops = $ref->{default} unless $newhops;
1499 return "" if defined $newhops && $newhops == 0;
1500 $newhops = $hops if !$newhops;
1501 $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
1503 # simply decrement it
1505 return "" if !$hops;
1506 $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
1518 return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1519 do "$main::data/hop_table.pl";
1525 # add a ping request to the ping queues
1528 my ($from, $to) = @_;
1529 my $ref = $pings{$to} || [];
1532 $r->{t} = [ gettimeofday ];
1533 route(undef, $to, pc51($to, $main::mycall, 1));
1538 # add a rcmd request to the rcmd queues
1541 my ($self, $to, $cmd) = @_;
1544 $r->{call} = $self->{call};
1545 $r->{t} = $main::systime;
1549 my $ref = DXCluster->get_exact($to);
1550 if ($ref && $ref->dxchan && $ref->dxchan->is_clx) {
1551 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1553 route(undef, $to, pc34($main::mycall, $to, $cmd));
1562 if ($self->{conn} && !$nopc39) {
1563 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1566 $self->SUPER::disconnect;
1571 # send a talk message to this thingy
1575 my ($self, $from, $to, $via, $line) = @_;
1577 $line =~ s/\^/\\5E/g; # remove any ^ characters
1578 $self->send(DXProt::pc10($from, $to, $via, $line));
1579 Log('talk', $self->call, $from, $via?$via:$main::mycall, $line);