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);
34 use vars qw($me $pc11_max_age $pc23_max_age
35 $last_hour %pings %rcmds
36 %nodehops @baddx $baddxfn
37 $allowzero $decode_dk0wcy $send_opernam @checklist);
39 $me = undef; # the channel id for this cluster
40 $pc11_max_age = 1*3600; # the maximum age for an incoming 'real-time' pc11
41 $pc23_max_age = 1*3600; # the maximum age for an incoming 'real-time' pc23
43 $last_hour = time; # last time I did an hourly periodic update
44 %pings = (); # outstanding ping requests outbound
45 %rcmds = (); # outstanding rcmd requests outbound
46 %nodehops = (); # node specific hop control
47 @baddx = (); # list of illegal spotted callsigns
50 $baddxfn = "$main::data/baddx.pl";
54 [ qw(c c m p bc c) ], # pc10
55 [ qw(f m d t m c c h) ], # pc11
56 [ qw(c bc m bp bm p h) ], # pc12
60 undef , # pc16 has to be validated manually
63 undef , # pc19 has to be validated manually
64 undef , # pc20 no validation
66 undef , # pc22 no validation
67 [ qw(d n n n n m c c h) ], # pc23
69 [ qw(c c n n) ], # pc25
70 [ qw(f m d t m c c bc) ], # pc26
71 [ qw(d n n n n m c c bc) ], # pc27
72 [ qw(c c c c d t p m bp n p bp bc) ], # pc28
73 [ qw(c c n m) ], # pc29
81 [ qw(c c n m) ], # pc37
82 undef, # pc38 not interested
84 [ qw(c c m p n) ], # pc40
85 [ qw(c n m h) ], # pc41
87 undef, # pc43 don't handle it
88 [ qw(c c n m m c) ], # pc44
89 [ qw(c c n m) ], # pc45
117 [ qw(d n n n n n n m m m c c h) ], # pc73
128 [ qw(c c c m) ], # pc84
129 [ qw(c c c m) ], # pc85
132 # use the entry in the check list to check the field list presented
133 # return OK if line NOT in check list (for now)
138 return 0 if $n < 0 || $n > @checklist;
139 my $ref = $checklist[$n];
140 return 0 unless ref $ref;
143 shift; # not interested in the first field
144 for ($i = 0; $i < @$ref; $i++) {
145 my ($blank, $act) = $$ref[$i] =~ /^(b?)(\w)$/;
146 return 0 unless $act;
147 next if $blank && $_[$i] =~ /^[ \*]$/;
149 return $i+1 unless is_callsign($_[$i]);
150 } elsif ($act eq 'm') {
151 return $i+1 unless is_pctext($_[$i]);
152 } elsif ($act eq 'p') {
153 return $i+1 unless is_pcflag($_[$i]);
154 } elsif ($act eq 'f') {
155 return $i+1 unless is_freq($_[$i]);
156 } elsif ($act eq 'n') {
157 return $i+1 unless $_[$i] =~ /^[\d ]+$/;
158 } elsif ($act eq 'h') {
159 return $i+1 unless $_[$i] =~ /^H\d\d?$/;
160 } elsif ($act eq 'd') {
161 return $i+1 unless $_[$i] =~ /^\s*\d+-\w\w\w-[12][90]\d\d$/;
162 } elsif ($act eq 't') {
163 return $i+1 unless $_[$i] =~ /^[012]\d[012345]\dZ$/;
171 my $user = DXUser->get($main::mycall);
172 $DXProt::myprot_version += $main::version*100;
173 $me = DXProt->new($main::mycall, 0, $user);
175 $me->{state} = "indifferent";
176 do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
178 $me->{sort} = 'S'; # S for spider
180 # now prime the spot and wwv duplicates file with data
181 # my @today = Julian::unixtoj(time);
182 # for (Spot::readfile(@today), Spot::readfile(Julian::sub(@today, 1))) {
183 # Spot::dup(@{$_}[0..3]);
185 # for (Geomag::readfile(time)) {
186 # Geomag::dup(@{$_}[1..5]);
189 # load the baddx file
190 do "$baddxfn" if -e "$baddxfn";
195 # obtain a new connection this is derived from dxchannel
200 my $self = DXChannel::alloc(@_);
204 # this is how a pc connection starts (for an incoming connection)
205 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
206 # all the crap that comes between).
209 my ($self, $line, $sort) = @_;
210 my $call = $self->{call};
211 my $user = $self->{user};
213 # remember type of connection
214 $self->{consort} = $line;
215 $self->{outbound} = $sort eq 'O';
216 $self->{priv} = $user->priv || 1; # other clusters can always be 'normal' users
217 $self->{lang} = $user->lang || 'en';
218 $self->{isolate} = $user->{isolate};
219 $self->{consort} = $line; # save the connection type
222 # get the INPUT filters (these only pertain to Clusters)
223 $self->{inspotfilter} = Filter::read_in('spots', $call, 1);
224 $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1);
225 $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1);
226 $self->{inannfilter} = Filter::read_in('ann', $call, 1);
228 # set unbuffered and no echo
229 $self->send_now('B',"0");
230 $self->send_now('E',"0");
232 # ping neighbour node stuff
233 my $ping = $user->pingint;
234 $ping = 5*60 unless defined $ping;
235 $self->{pingint} = $ping;
236 $self->{nopings} = $user->nopings || 2;
237 $self->{pingtime} = [ ];
238 $self->{pingave} = 0;
240 # send initialisation string
241 unless ($self->{outbound}) {
242 $self->send(pc38()) if DXNode->get_all();
244 $self->{lastping} = $main::systime;
246 # remove from outstanding connects queue
247 @main::outstanding_connects = grep {$_->{call} ne $call} @main::outstanding_connects;
248 $self->{lastping} = $main::systime + $self->pingint / 2;
250 $self->state('init');
253 # send info to all logged in thingies
254 $self->tell_login('loginn');
256 Log('DXProt', "$call connected");
260 # This is the normal pcxx despatcher
264 my ($self, $line) = @_;
265 my @field = split /\^/, $line;
266 return unless @field;
268 pop @field if $field[-1] eq '~';
270 # print join(',', @field), "\n";
272 # ignore any lines that don't start with PC
273 return if !$field[0] =~ /^PC/;
276 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
278 return if $pcno < 10 || $pcno > 99;
280 # check for and dump bad protocol messages
281 my $n = check($pcno, @field);
283 dbg('chan', "bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")");
290 $pcr = Local::pcprot($self, $pcno, @field);
292 # dbg('local', "Local::pcprot error $@") if $@;
296 if ($pcno == 10) { # incoming talk
298 # is it for me or one of mine?
299 my ($to, $via, $call, $dxchan);
300 if ($field[5] gt ' ') {
301 $call = $via = $field[2];
303 unless (is_callsign($to)) {
304 dbg('chan', "Corrupt talk, rejected");
308 $call = $to = $field[2];
310 if ($dxchan = DXChannel->get($call)) {
311 $dxchan->talk($field[1], $to, $via, $field[3]);
313 $self->route($field[2], $line); # relay it on its way
318 if ($pcno == 11 || $pcno == 26) { # dx spot
320 # route 'foreign' pc26s
322 if ($field[7] ne $main::mycall) {
323 $self->route($field[7], $line);
328 # if this is a 'nodx' node then ignore it
329 if (grep $field[7] =~ /^$_/, @DXProt::nodx_node) {
330 dbg('chan', "Bad DXNode, dropped");
334 # convert the date to a unix date
335 my $d = cltounix($field[3], $field[4]);
336 # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
337 if (!$d || ($pcno == 11 && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
338 dbg('chan', "Spot ignored, invalid date or out of range ($field[3] $field[4])\n");
343 if (grep $field[2] eq $_, @baddx) {
344 dbg('chan', "Bad DX spot, ignored");
349 $field[5] =~ s/^\s+//; # take any leading blanks off
350 if (Spot::dup($field[1], $field[2], $d, $field[5])) {
351 dbg('chan', "Duplicate Spot ignored\n");
355 my @spot = Spot::add($field[1], $field[2], $d, $field[5], $field[6], $field[7]);
358 # @spot at this point contains:-
359 # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
360 # then spotted itu, spotted cq, spotters itu, spotters cq
361 # you should be able to route on any of these
364 # fix up qra locators of known users
365 my $user = DXUser->get_current($spot[4]);
367 my $qra = $user->qra;
368 unless ($qra && DXBearing::is_qra($qra)) {
369 my $lat = $user->lat;
370 my $long = $user->long;
371 if (defined $lat && defined $long) {
372 $user->qra(DXBearing::lltoqra($lat, $long));
377 # send a remote command to a distant cluster if it is visible and there is no
378 # qra locator and we havn't done it for a month.
380 unless ($user->qra) {
382 my $to = $user->homenode;
383 my $last = $user->lastoper || 0;
384 if ($send_opernam && $main::systime > $last + $DXUser::lastoperinterval && $to && ($node = DXCluster->get_exact($to)) ) {
385 my $cmd = "forward/opernam $spot[4]";
386 # send the rcmd but we aren't interested in the replies...
387 if ($node && $node->dxchan && $node->dxchan->is_clx) {
388 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
390 route(undef, $to, pc34($main::mycall, $to, $cmd));
392 if ($to ne $field[7]) {
394 $node = DXCluster->get_exact($to);
395 if ($node && $node->dxchan && $node->dxchan->is_clx) {
396 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
398 route(undef, $to, pc34($main::mycall, $to, $cmd));
401 $user->lastoper($main::systime);
410 $r = Local::spot($self, @spot);
412 # dbg('local', "Local::spot1 error $@") if $@;
415 # DON'T be silly and send on PC26s!
416 return if $pcno == 26;
418 # send out the filtered spots
419 send_dx_spot($self, $line, @spot) if @spot;
423 if ($pcno == 12) { # announces
424 # announce duplicate checking
425 $field[3] =~ s/^\s+//; # remove leading blanks
426 if (AnnTalk::dup($field[1], $field[2], $field[3])) {
427 dbg('chan', "Duplicate Announce ignored");
431 if ($field[2] eq '*' || $field[2] eq $main::mycall) {
433 # global ann filtering on INPUT
434 if ($self->{inannfilter}) {
435 my ($filter, $hops) = Filter::it($self->{inannfilter}, @field[1..6], $self->{call} );
437 dbg('chan', "Rejected by filter");
443 $self->send_announce($line, @field[1..6]);
445 $self->route($field[2], $line);
461 if ($pcno == 16) { # add a user
462 my $node = DXCluster->get_exact($field[1]);
464 if (!$node && ($dxchan = DXChannel->get($field[1]))) {
465 # add it to the node table if it isn't present and it's
467 $node = DXNode->new($dxchan, $field[1], 0, 1, 5400);
468 broadcast_ak1a(pc19($dxchan, $node), $dxchan, $self) unless $dxchan->{isolate};
471 return unless $node; # ignore if havn't seen a PC19 for this one yet
472 return unless $node->isa('DXNode');
473 if ($node->dxchan != $self) {
474 dbg('chan', "LOOP: $field[1] came in on wrong channel");
477 if (($dxchan = DXChannel->get($field[1])) && $dxchan != $self) {
478 dbg('chan', "LOOP: $field[1] connected locally");
483 for ($i = 2; $i < $#field; $i++) {
484 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (\S) (\d)/o;
485 next if !$call || length $call < 3 || length $call > 8;
488 next if DXCluster->get_exact($call); # we already have this (loop?)
490 $confmode = $confmode eq '*';
491 DXNodeuser->new($self, $node, $call, $confmode, $here);
493 # add this station to the user database, if required
494 $call =~ s/-\d+$//o; # remove ssid for users
495 my $user = DXUser->get_current($call);
496 $user = DXUser->new($call) if !$user;
497 $user->homenode($node->call) if !$user->homenode;
498 $user->node($node->call);
499 $user->lastin($main::systime) unless DXChannel->get($call);
503 # queue up any messages (look for privates only)
504 DXMsg::queue_msg(1) if $self->state eq 'normal';
508 if ($pcno == 17) { # remove a user
509 my $node = DXCluster->get_exact($field[2]);
511 if (!$node && ($dxchan = DXChannel->get($field[2]))) {
512 # add it to the node table if it isn't present and it's
514 $node = DXNode->new($dxchan, $field[2], 0, 1, 5400);
515 broadcast_ak1a(pc19($dxchan, $node), $dxchan, $self) unless $dxchan->{isolate};
519 return unless $node->isa('DXNode');
520 if ($node->dxchan != $self) {
521 dbg('chan', "LOOP: $field[2] came in on wrong channel");
524 if (($dxchan = DXChannel->get($field[2])) && $dxchan != $self) {
525 dbg('chan', "LOOP: $field[2] connected locally");
528 my $ref = DXCluster->get_exact($field[1]);
533 if ($pcno == 18) { # link request
534 $self->state('init');
536 # first clear out any nodes on this dxchannel
537 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
538 foreach my $node (@gonenodes) {
539 next if $node->dxchan == $DXProt::me;
540 broadcast_ak1a(pc21($node->call, 'Gone, re-init') , $self) unless $self->{isolate};
543 $self->send_local_config();
545 return; # we don't pass these on
548 if ($pcno == 19) { # incoming cluster list
550 my $newline = "PC19^";
551 for ($i = 1; $i < $#field-1; $i += 4) {
552 my $here = $field[$i];
553 my $call = uc $field[$i+1];
554 my $confmode = $field[$i+2];
555 my $ver = $field[$i+3];
557 $ver = 5400 if !$ver && $allowzero;
559 # now check the call over
560 my $node = DXCluster->get_exact($call);
563 if (($dxchan = DXChannel->get($call)) && $dxchan != $self) {
564 dbg('chan', "LOOP: $call connected locally");
566 if ($node->dxchan != $self) {
567 dbg('chan', "LOOP: $call come in on wrong channel");
570 dbg('chan', "already have $call");
574 # check for sane parameters
575 next if $ver < 5000; # only works with version 5 software
576 next if length $call < 3; # min 3 letter callsigns
578 # add it to the nodes table and outgoing line
579 $newline .= "$here^$call^$confmode^$ver^";
580 DXNode->new($self, $call, $confmode, $here, $ver);
582 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
583 my $mref = DXMsg::get_busy($call);
584 $mref->stop_msg($call) if $mref;
586 # add this station to the user database, if required (don't remove SSID from nodes)
587 my $user = DXUser->get_current($call);
589 $user = DXUser->new($call);
591 $user->priv(1); # I have relented and defaulted nodes
592 $self->{priv} = 1; # to user RCMDs allowed
593 $user->homenode($call);
596 $user->lastin($main::systime) unless DXChannel->get($call);
600 return if $newline eq "PC19^";
603 $newline .= get_hops(19) . "^";
608 if ($pcno == 20) { # send local configuration
609 $self->send_local_config();
611 $self->state('normal');
615 if ($pcno == 21) { # delete a cluster from the list
616 my $call = uc $field[1];
617 if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
618 my $node = DXCluster->get_exact($call);
620 if ($node->dxchan != $self) {
621 dbg('chan', "LOOP: $call come in on wrong channel");
625 if (($dxchan = DXChannel->get($call)) && $dxchan != $self) {
626 dbg('chan', "LOOP: $call connected locally");
631 dbg('chan', "$call not in table, dropped");
639 $self->state('normal');
643 if ($pcno == 23 || $pcno == 27) { # WWV info
645 # route 'foreign' pc27s
647 if ($field[8] ne $main::mycall) {
648 $self->route($field[8], $line);
654 my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
655 my $sfi = unpad($field[3]);
656 my $k = unpad($field[4]);
657 my $i = unpad($field[5]);
658 my ($r) = $field[6] =~ /R=(\d+)/;
660 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
661 dbg('chan', "WWV Date ($field[1] $field[2]) out of range");
664 if (Geomag::dup($d,$sfi,$k,$i,$field[6])) {
665 dbg('chan', "Dup WWV Spot ignored\n");
668 $field[7] =~ s/-\d+$//o; # remove spotter's ssid
670 my $wwv = Geomag::update($d, $field[2], $sfi, $k, $i, @field[6..8], $r);
674 $rep = Local::wwv($self, $field[1], $field[2], $sfi, $k, $i, @field[6..8], $r);
676 # dbg('local', "Local::wwv2 error $@") if $@;
679 # DON'T be silly and send on PC27s!
680 return if $pcno == 27;
682 # broadcast to the eager world
683 send_wwv_spot($self, $line, $d, $field[2], $sfi, $k, $i, @field[6..8]);
687 if ($pcno == 24) { # set here status
688 my $call = uc $field[1];
689 my $ref = DXCluster->get_exact($call);
690 $ref->here($field[2]) if $ref;
694 if ($pcno == 25) { # merge request
695 if ($field[1] ne $main::mycall) {
696 $self->route($field[1], $line);
699 if ($field[2] eq $main::mycall) {
700 dbg('chan', "Trying to merge to myself, ignored");
704 Log('DXProt', "Merge request for $field[3] spots and $field[4] WWV from $field[1]");
708 my @in = reverse Spot::search(1, undef, undef, 0, $field[3]);
711 $self->send(pc26(@{$in}[0..4], $field[2]));
717 my @in = reverse Geomag::search(0, $field[4], time, 1);
720 $self->send(pc27(@{$in}[0..5], $field[2]));
726 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
727 if ($pcno == 49 || $field[1] eq $main::mycall) {
728 DXMsg::process($self, $line);
730 $self->route($field[1], $line) unless $self->is_clx;
735 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
736 if ($field[1] eq $main::mycall) {
737 my $ref = DXUser->get_current($field[2]);
738 my $cref = DXCluster->get($field[2]);
739 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
740 unless (!$cref || !$ref || $cref->mynode->call ne $ref->homenode) { # not allowed to relay RCMDS!
741 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
742 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
743 my $oldpriv = $self->{priv};
744 $self->{priv} = $ref->{priv}; # assume the user's privilege level
745 my @in = (DXCommandmode::run_cmd($self, $field[3]));
746 $self->{priv} = $oldpriv;
749 $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
750 Log('rcmd', 'out', $field[2], $_);
752 delete $self->{remotecmd};
754 $self->send(pc35($main::mycall, $field[2], "$main::mycall:sorry...!"));
757 $self->send(pc35($main::mycall, $field[2], "$main::mycall:your attempt is logged, Tut tut tut...!"));
760 my $ref = DXUser->get_current($field[1]);
761 if ($ref && $ref->is_clx) {
762 route($field[1], pc84($field[2], $field[1], $field[2], $field[3]));
764 $self->route($field[1], $line);
770 if ($pcno == 35) { # remote command replies
771 if ($field[1] eq $main::mycall) {
772 my $s = $rcmds{$field[2]};
774 my $dxchan = DXChannel->get($s->{call});
775 $dxchan->send($field[3]) if $dxchan;
776 delete $rcmds{$field[2]} if !$dxchan;
778 # send unsolicited ones to the sysop
779 my $dxchan = DXChannel->get($main::myalias);
780 $dxchan->send($field[3]) if $dxchan;
783 my $ref = DXUser->get_current($field[1]);
784 if ($ref && $ref->is_clx) {
785 route($field[1], pc85($field[2], $field[1], $field[2], $field[3]));
787 $self->route($field[1], $line);
793 # for pc 37 see 44 onwards
795 if ($pcno == 38) { # node connected list from neighbour
799 if ($pcno == 39) { # incoming disconnect
800 $self->disconnect(1);
804 if ($pcno == 41) { # user info
805 # add this station to the user database, if required
806 my $user = DXUser->get_current($field[1]);
808 # then try without an SSID
809 $field[1] =~ s/-\d+$//o;
810 $user = DXUser->get_current($field[1]);
812 $user = DXUser->new($field[1]) if !$user;
814 if ($field[2] == 1) {
815 $user->name($field[3]);
816 } elsif ($field[2] == 2) {
817 $user->qth($field[3]);
818 } elsif ($field[2] == 3) {
819 my ($lat, $long) = DXBearing::stoll($field[3]);
822 $user->qra(DXBearing::lltoqra($lat, $long)) unless $user->qra && DXBearing::is_qra($user->qra);
823 } elsif ($field[2] == 4) {
824 $user->homenode($field[3]);
826 $user->lastoper($main::systime); # to cut down on excessive for/opers being generated
833 if ($pcno == 37 || $pcno == 44 || $pcno == 45 || $pcno == 46 || $pcno == 47 || $pcno == 48) {
834 DXDb::process($self, $line);
838 if ($pcno == 50) { # keep alive/user list
839 my $node = DXCluster->get_exact($field[1]);
841 return unless $node->isa('DXNode');
842 return unless $node->dxchan == $self;
843 $node->update_users($field[2]);
848 if ($pcno == 51) { # incoming ping requests/answers
851 if ($field[1] eq $main::mycall) {
852 my $flag = $field[3];
854 $self->send(pc51($field[2], $field[1], '0'));
856 # it's a reply, look in the ping list for this one
857 my $ref = $pings{$field[2]};
859 my $tochan = DXChannel->get($field[2]);
862 my $dxchan = DXChannel->get($r->{call});
864 my $t = tv_interval($r->{t}, [ gettimeofday ]);
865 if ($dxchan->is_user) {
866 my $s = sprintf "%.2f", $t;
867 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
868 $dxchan->send($dxchan->msg('pingi', $field[2], $s, $ave))
869 } elsif ($dxchan->is_node) {
871 $tochan->{nopings} = 2; # pump up the timer
872 push @{$tochan->{pingtime}}, $t;
873 shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
875 for (@{$tochan->{pingtime}}) {
878 $tochan->{pingave} = $st / @{$tochan->{pingtime}};
885 # route down an appropriate thingy
886 $self->route($field[1], $line);
891 if ($pcno == 75) { # dunno but route it
892 if ($field[1] ne $main::mycall) {
893 $self->route($field[1], $line);
898 if ($pcno == 73) { # WCY broadcasts
901 my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
902 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
903 dbg('chan', "WCY Date ($field[1] $field[2]) out of range");
906 @field = map { unpad($_) } @field;
907 if (WCY::dup($d,@field[3..7])) {
908 dbg('chan', "Dup WCY Spot ignored\n");
912 my $wcy = WCY::update($d, @field[2..12]);
916 $rep = Local::wwv($self, @field[1..12]);
918 # dbg('local', "Local::wcy error $@") if $@;
921 # broadcast to the eager world
922 send_wcy_spot($self, $line, $d, @field[2..12]);
926 if ($pcno == 84) { # remote commands (incoming)
927 if ($field[1] eq $main::mycall) {
928 my $ref = DXUser->get_current($field[2]);
929 my $cref = DXCluster->get($field[2]);
930 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[4]);
931 unless ($field[4] =~ /rcmd/i || !$cref || !$ref || $cref->mynode->call ne $ref->homenode) { # not allowed to relay RCMDS!
932 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
933 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
934 my $oldpriv = $self->{priv};
935 $self->{priv} = $ref->{priv}; # assume the user's privilege level
936 my @in = (DXCommandmode::run_cmd($self, $field[4]));
937 $self->{priv} = $oldpriv;
940 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:$_"));
941 Log('rcmd', 'out', $field[2], $_);
943 delete $self->{remotecmd};
945 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:sorry...!"));
948 $self->send(pc85($main::mycall, $field[2], $field[3],"$main::mycall:your attempt is logged, Tut tut tut...!"));
951 my $ref = DXUser->get_current($field[1]);
952 if ($ref && $ref->is_clx) {
953 $self->route($field[1], $line);
955 route($field[1], pc34($field[2], $field[1], $field[4]));
961 if ($pcno == 85) { # remote command replies
962 if ($field[1] eq $main::mycall) {
963 my $dxchan = DXChannel->get($field[3]);
965 $dxchan->send($field[4]);
967 my $s = $rcmds{$field[2]};
969 $dxchan = DXChannel->get($s->{call});
970 $dxchan->send($field[4]) if $dxchan;
971 delete $rcmds{$field[2]} if !$dxchan;
973 # send unsolicited ones to the sysop
974 my $dxchan = DXChannel->get($main::myalias);
975 $dxchan->send($field[4]) if $dxchan;
979 my $ref = DXUser->get_current($field[1]);
980 if ($ref && $ref->is_clx) {
981 $self->route($field[1], $line);
983 route($field[1], pc35($field[2], $field[1], $field[4]));
990 # if get here then rebroadcast the thing with its Hop count decremented (if
991 # there is one). If it has a hop count and it decrements to zero then don't
994 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
998 unless ($self->{isolate}) {
999 broadcast_ak1a($line, $self); # send it to everyone but me
1004 # This is called from inside the main cluster processing loop and is used
1005 # for despatching commands that are doing some long processing job
1010 my @dxchan = DXChannel->get_all();
1013 foreach $dxchan (@dxchan) {
1014 next unless $dxchan->is_node();
1015 next if $dxchan == $me;
1017 # send a pc50 out on this channel
1018 if ($t >= $dxchan->pc50_t + $DXProt::pc50_interval) {
1019 $dxchan->send(pc50(scalar DXChannel::get_all_users));
1020 $dxchan->pc50_t($t);
1023 # send a ping out on this channel
1024 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
1025 if ($dxchan->{nopings} <= 0) {
1026 $dxchan->disconnect;
1028 addping($main::mycall, $dxchan->call);
1029 $dxchan->{nopings} -= 1;
1030 $dxchan->{lastping} = $t;
1038 if ($main::systime - 3600 > $last_hour) {
1042 $last_hour = $main::systime;
1047 # finish up a pc context
1052 my $call = $self->call;
1054 my $ref = DXCluster->get_exact($call);
1056 # unbusy and stop and outgoing mail
1057 my $mref = DXMsg::get_busy($call);
1058 $mref->stop_msg($call) if $mref;
1060 # broadcast to all other nodes that all the nodes connected to via me are gone
1061 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
1064 foreach $node (@gonenodes) {
1065 next if $node->call eq $call;
1066 broadcast_ak1a(pc21($node->call, 'Gone') , $self) unless $self->{isolate};
1070 # remove outstanding pings
1071 delete $pings{$call};
1073 # now broadcast to all other ak1a nodes that I have gone
1074 broadcast_ak1a(pc21($call, 'Gone.'), $self) unless $self->{isolate};
1076 # I was the last node visited
1077 $self->user->node($main::mycall);
1079 # send info to all logged in thingies
1080 $self->tell_login('logoutn');
1082 Log('DXProt', $call . " Disconnected");
1083 $ref->del() if $ref;
1087 # some active measures
1093 my @dxchan = DXChannel->get_all();
1096 # send it if it isn't the except list and isn't isolated and still has a hop count
1097 # taking into account filtering and so on
1098 foreach $dxchan (@dxchan) {
1100 my ($filter, $hops);
1102 if ($dxchan->{spotfilter}) {
1103 ($filter, $hops) = Filter::it($dxchan->{spotfilter}, @_, $self->{call} );
1104 next unless $filter;
1107 if ($dxchan->is_node) {
1108 next if $dxchan == $self;
1111 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1113 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1114 next unless $routeit;
1117 $dxchan->send($routeit) if $routeit;
1119 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1121 } elsif ($dxchan->is_user && $dxchan->{dx}) {
1122 my $buf = Spot::formatb($dxchan->{user}->wantgrid, $_[0], $_[1], $_[2], $_[3], $_[4]);
1123 $buf .= "\a\a" if $dxchan->{beep};
1124 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1125 $dxchan->send($buf);
1127 $dxchan->delay($buf);
1137 my @dxchan = DXChannel->get_all();
1140 # send it if it isn't the except list and isn't isolated and still has a hop count
1141 # taking into account filtering and so on
1142 foreach $dxchan (@dxchan) {
1144 my ($filter, $hops);
1146 if ($dxchan->{wwvfilter}) {
1147 ($filter, $hops) = Filter::it($dxchan->{wwvfilter}, @_, $self->{call} );
1148 next unless $filter;
1150 if ($dxchan->is_node) {
1151 next if $dxchan == $self;
1154 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1156 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1157 next unless $routeit;
1160 $dxchan->send($routeit) if $routeit;
1162 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1165 } elsif ($dxchan->is_user && $dxchan->{wwv}) {
1166 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
1167 $buf .= "\a\a" if $dxchan->{beep};
1168 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1169 $dxchan->send($buf);
1171 $dxchan->delay($buf);
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->{wcyfilter}) {
1191 ($filter, $hops) = Filter::it($dxchan->{wcyfilter}, @_, $self->{call} );
1192 next unless $filter;
1194 if ($dxchan->is_clx || $dxchan->is_spider) {
1195 next if $dxchan == $self;
1198 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1200 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1201 next unless $routeit;
1204 $dxchan->send($routeit) if $routeit;
1206 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1208 } elsif ($dxchan->is_user && $dxchan->{wcy}) {
1209 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1210 $buf .= "\a\a" if $dxchan->{beep};
1211 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1212 $dxchan->send($buf);
1214 $dxchan->delay($buf);
1225 my @dxchan = DXChannel->get_all();
1227 my $text = unpad($_[2]);
1231 if ($_[3] eq '*') { # sysops
1233 } elsif ($_[3] gt ' ') { # speciality list handling
1234 my ($name) = split /\./, $_[3];
1235 $target = "$name"; # put the rest in later (if bothered)
1242 $target = "All" if !$target;
1244 Log('ann', $target, $_[0], $text);
1246 # send it if it isn't the except list and isn't isolated and still has a hop count
1247 # taking into account filtering and so on
1248 foreach $dxchan (@dxchan) {
1250 my ($filter, $hops);
1252 if ($dxchan->{annfilter}) {
1253 ($filter, $hops) = Filter::it($dxchan->{annfilter}, @_, $self->{call} );
1254 next unless $filter;
1256 if ($dxchan->is_node && $_[1] ne $main::mycall) { # i.e not specifically routed to me
1257 next if $dxchan == $self;
1260 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1262 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1263 next unless $routeit;
1266 $dxchan->send($routeit) if $routeit;
1268 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1271 } elsif ($dxchan->is_user) {
1272 unless ($dxchan->{ann}) {
1273 next if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
1275 next if $target eq 'SYSOP' && $dxchan->{priv} < 5;
1276 my $buf = "$to$target de $_[0]: $text";
1277 $buf .= "\a\a" if $dxchan->{beep};
1278 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1279 $dxchan->send($buf);
1281 $dxchan->delay($buf);
1287 sub send_local_config
1296 if ($self->{isolate}) {
1297 @localnodes = (DXCluster->get_exact($main::mycall));
1299 # create a list of all the nodes that are not connected to this connection
1300 # and are not themselves isolated, this to make sure that isolated nodes
1301 # don't appear outside of this node
1302 @nodes = DXNode::get_all();
1303 @nodes = grep { $_->{call} ne $main::mycall } @nodes;
1304 @nodes = grep { $_->dxchan != $self } @nodes if @nodes;
1305 @nodes = grep { !$_->dxchan->{isolate} } @nodes if @nodes;
1306 @localnodes = grep { $_->dxchan->{call} eq $_->{call} } @nodes if @nodes;
1307 unshift @localnodes, DXCluster->get_exact($main::mycall);
1308 @remotenodes = grep { $_->dxchan->{call} ne $_->{call} } @nodes if @nodes;
1311 my @s = $me->pc19(@localnodes, @remotenodes);
1313 my $routeit = adjust_hops($self, $_);
1314 $self->send($routeit) if $routeit;
1317 # get all the users connected on the above nodes and send them out
1318 foreach $n (@localnodes, @remotenodes) {
1319 my @users = values %{$n->list};
1320 my @s = pc16($n, @users);
1322 my $routeit = adjust_hops($self, $_);
1323 $self->send($routeit) if $routeit;
1329 # route a message down an appropriate interface for a callsign
1331 # is called route(to, pcline);
1335 my ($self, $call, $line) = @_;
1336 my $cl = DXCluster->get_exact($call);
1337 if ($cl) { # don't route it back down itself
1338 if (ref $self && $call eq $self->{call}) {
1339 dbg('chan', "Trying to route back to source, dropped");
1343 my $dxchan = $cl->{dxchan};
1345 my $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1347 $dxchan->send($routeit) if $dxchan;
1353 # broadcast a message to all clusters taking into account isolation
1354 # [except those mentioned after buffer]
1357 my $s = shift; # the line to be rebroadcast
1358 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1359 my @dxchan = DXChannel::get_all_nodes();
1362 # send it if it isn't the except list and isn't isolated and still has a hop count
1363 foreach $dxchan (@dxchan) {
1364 next if grep $dxchan == $_, @except;
1365 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
1366 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
1370 # broadcast a message to all clusters ignoring isolation
1371 # [except those mentioned after buffer]
1372 sub broadcast_all_ak1a
1374 my $s = shift; # the line to be rebroadcast
1375 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1376 my @dxchan = DXChannel::get_all_nodes();
1379 # send it if it isn't the except list and isn't isolated and still has a hop count
1380 foreach $dxchan (@dxchan) {
1381 next if grep $dxchan == $_, @except;
1382 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
1383 $dxchan->send($routeit);
1387 # broadcast to all users
1388 # storing the spot or whatever until it is in a state to receive it
1391 my $s = shift; # the line to be rebroadcast
1392 my $sort = shift; # the type of transmission
1393 my $fref = shift; # a reference to an object to filter on
1394 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1395 my @dxchan = DXChannel::get_all_users();
1399 foreach $dxchan (@dxchan) {
1400 next if grep $dxchan == $_, @except;
1403 broadcast_list($s, $sort, $fref, @out);
1406 # broadcast to a list of users
1414 foreach $dxchan (@_) {
1417 if ($sort eq 'dx') {
1418 next unless $dxchan->{dx};
1419 ($filter) = Filter::it($dxchan->{spotfilter}, @{$fref}) if ref $fref;
1420 next unless $filter;
1422 next if $sort eq 'ann' && !$dxchan->{ann};
1423 next if $sort eq 'wwv' && !$dxchan->{wwv};
1424 next if $sort eq 'wcy' && !$dxchan->{wcy};
1425 next if $sort eq 'wx' && !$dxchan->{wx};
1427 $s =~ s/\a//og unless $dxchan->{beep};
1429 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1439 # obtain the hops from the list for this callsign and pc no
1445 my $hops = $DXProt::hopcount{$pcno};
1446 $hops = $DXProt::def_hopcount if !$hops;
1451 # adjust the hop count on a per node basis using the user loadable
1452 # hop table if available or else decrement an existing one
1459 my $call = $self->{call};
1462 if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
1463 my ($pcno) = $s =~ /^PC(\d\d)/o;
1464 confess "$call called adjust_hops with '$s'" unless $pcno;
1465 my $ref = $nodehops{$call} if %nodehops;
1467 my $newhops = $ref->{$pcno};
1468 return "" if defined $newhops && $newhops == 0;
1469 $newhops = $ref->{default} unless $newhops;
1470 return "" if defined $newhops && $newhops == 0;
1471 $newhops = $hops if !$newhops;
1472 $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
1474 # simply decrement it
1476 return "" if !$hops;
1477 $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
1489 return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1490 do "$main::data/hop_table.pl";
1496 # add a ping request to the ping queues
1499 my ($from, $to) = @_;
1500 my $ref = $pings{$to} || [];
1503 $r->{t} = [ gettimeofday ];
1504 route(undef, $to, pc51($to, $main::mycall, 1));
1509 # add a rcmd request to the rcmd queues
1512 my ($self, $to, $cmd) = @_;
1515 $r->{call} = $self->{call};
1516 $r->{t} = $main::systime;
1520 my $ref = DXCluster->get_exact($to);
1521 if ($ref && $ref->dxchan && $ref->dxchan->is_clx) {
1522 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1524 route(undef, $to, pc34($main::mycall, $to, $cmd));
1533 if ($self->{conn} && !$nopc39) {
1534 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1537 $self->SUPER::disconnect;
1542 # send a talk message to this thingy
1546 my ($self, $from, $to, $via, $line) = @_;
1548 $line =~ s/\^/\\5E/g; # remove any ^ characters
1549 $self->send(DXProt::pc10($from, $to, $via, $line));
1550 Log('talk', $self->call, $from, $via?$via:$main::mycall, $line);