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 p c 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) ], # pc26
71 [ qw(d n n n n m c c) ], # 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 < @_; $i++) {
145 my ($blank, $act) = $$ref[$i] =~ /^(b?)(\w)$/;
146 next if $blank && $_[$i] =~ /^[ \*]$/;
148 return $i+1 unless is_callsign($_[$i]);
149 } elsif ($act eq 'm') {
150 return $i+1 unless is_pctext($_[$i]);
151 } elsif ($act eq 'p') {
152 return $i+1 unless is_pcflag($_[$i]);
153 } elsif ($act eq 'f') {
154 return $i+1 unless is_freq($_[$i]);
155 } elsif ($act eq 'n') {
156 return $i+1 unless $_[$i] =~ /^[\d ]+$/;
157 } elsif ($act eq 'h') {
158 return $i+1 unless $_[$i] =~ /^H\d\d?$/;
159 } elsif ($act eq 'd') {
160 return $i+1 unless $_[$i] =~ /^\s*\d+-\w\w\w-[12][90]\d\d$/;
161 } elsif ($act eq 't') {
162 return $i+1 unless $_[$i] =~ /^[012]\d[012345]\dZ$/;
170 my $user = DXUser->get($main::mycall);
171 $DXProt::myprot_version += $main::version*100;
172 $me = DXProt->new($main::mycall, 0, $user);
174 $me->{state} = "indifferent";
175 do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
177 # $me->{sort} = 'M'; # M for me
179 # now prime the spot and wwv duplicates file with data
180 my @today = Julian::unixtoj(time);
181 for (Spot::readfile(@today), Spot::readfile(Julian::sub(@today, 1))) {
182 Spot::dup(@{$_}[0..3]);
184 for (Geomag::readfile(time)) {
185 Geomag::dup(@{$_}[1..5]);
188 # load the baddx file
189 do "$baddxfn" if -e "$baddxfn";
194 # obtain a new connection this is derived from dxchannel
199 my $self = DXChannel::alloc(@_);
203 # this is how a pc connection starts (for an incoming connection)
204 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
205 # all the crap that comes between).
208 my ($self, $line, $sort) = @_;
209 my $call = $self->{call};
210 my $user = $self->{user};
212 # remember type of connection
213 $self->{consort} = $line;
214 $self->{outbound} = $sort eq 'O';
215 $self->{priv} = $user->priv || 1; # other clusters can always be 'normal' users
216 $self->{lang} = $user->lang || 'en';
217 $self->{isolate} = $user->{isolate};
218 $self->{consort} = $line; # save the connection type
221 # get the INPUT filters (these only pertain to Clusters)
222 $self->{inspotfilter} = Filter::read_in('spots', $call, 1);
223 $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1);
224 $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1);
225 $self->{inannfilter} = Filter::read_in('ann', $call, 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 pop @field if $field[-1] eq '~';
267 # print join(',', @field), "\n";
269 # ignore any lines that don't start with PC
270 return if !$field[0] =~ /^PC/;
273 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
275 return if $pcno < 10 || $pcno > 99;
277 # check for and dump bad protocol messages
278 my $n = check($pcno, @field);
280 dbg('chan', "bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")");
287 $pcr = Local::pcprot($self, $pcno, @field);
289 # dbg('local', "Local::pcprot error $@") if $@;
293 if ($pcno == 10) { # incoming talk
295 # is it for me or one of mine?
296 my ($to, $via, $call, $dxchan);
297 if ($field[5] gt ' ') {
298 $call = $via = $field[2];
300 unless (is_callsign($to)) {
301 dbg('chan', "Corrupt talk, rejected");
305 $call = $to = $field[2];
307 if ($dxchan = DXChannel->get($call)) {
308 $dxchan->talk($field[1], $to, $via, $field[3]);
310 $self->route($field[2], $line); # relay it on its way
315 if ($pcno == 11 || $pcno == 26) { # dx spot
317 # route 'foreign' pc26s
319 if ($field[7] ne $main::mycall) {
320 $self->route($field[7], $line);
325 # if this is a 'nodx' node then ignore it
326 if (grep $field[7] =~ /^$_/, @DXProt::nodx_node) {
327 dbg('chan', "Bad DXNode, dropped");
331 # convert the date to a unix date
332 my $d = cltounix($field[3], $field[4]);
333 # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
334 if (!$d || ($pcno == 11 && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
335 dbg('chan', "Spot ignored, invalid date or out of range ($field[3] $field[4])\n");
340 if (grep $field[2] eq $_, @baddx) {
341 dbg('chan', "Bad DX spot, ignored");
346 $field[5] =~ s/^\s+//; # take any leading blanks off
347 if (Spot::dup($field[1], $field[2], $d, $field[5])) {
348 dbg('chan', "Duplicate Spot ignored\n");
352 my @spot = Spot::add($field[1], $field[2], $d, $field[5], $field[6], $field[7]);
355 # @spot at this point contains:-
356 # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
357 # then spotted itu, spotted cq, spotters itu, spotters cq
358 # you should be able to route on any of these
361 # fix up qra locators of known users
362 my $user = DXUser->get_current($spot[4]);
364 my $qra = $user->qra;
365 unless ($qra && DXBearing::is_qra($qra)) {
366 my $lat = $user->lat;
367 my $long = $user->long;
368 if (defined $lat && defined $long) {
369 $user->qra(DXBearing::lltoqra($lat, $long));
374 # send a remote command to a distant cluster if it is visible and there is no
375 # qra locator and we havn't done it for a month.
377 unless ($user->qra) {
379 my $to = $user->homenode;
380 my $last = $user->lastoper || 0;
381 if ($send_opernam && $main::systime > $last + $DXUser::lastoperinterval && $to && ($node = DXCluster->get_exact($to)) ) {
382 my $cmd = "forward/opernam $spot[4]";
383 # send the rcmd but we aren't interested in the replies...
384 if ($node && $node->dxchan && $node->dxchan->is_clx) {
385 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
387 route(undef, $to, pc34($main::mycall, $to, $cmd));
389 if ($to ne $field[7]) {
391 $node = DXCluster->get_exact($to);
392 if ($node && $node->dxchan && $node->dxchan->is_clx) {
393 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
395 route(undef, $to, pc34($main::mycall, $to, $cmd));
398 $user->lastoper($main::systime);
407 $r = Local::spot($self, @spot);
409 # dbg('local', "Local::spot1 error $@") if $@;
412 # DON'T be silly and send on PC26s!
413 return if $pcno == 26;
415 # send out the filtered spots
416 send_dx_spot($self, $line, @spot) if @spot;
420 if ($pcno == 12) { # announces
421 # announce duplicate checking
422 $field[3] =~ s/^\s+//; # remove leading blanks
423 if (AnnTalk::dup($field[1], $field[2], $field[3])) {
424 dbg('chan', "Duplicate Announce ignored\n");
428 if ($field[2] eq '*' || $field[2] eq $main::mycall) {
430 # global ann filtering on INPUT
431 if ($self->{inannfilter}) {
432 my ($filter, $hops) = Filter::it($self->{inannfilter}, @field[1..6], $self->{call} );
434 dbg('chan', "Rejected by filter");
440 $self->send_announce($line, @field[1..6]);
442 $self->route($field[2], $line);
458 if ($pcno == 16) { # add a user
459 my $node = DXCluster->get_exact($field[1]);
461 if (!$node && ($dxchan = DXChannel->get($field[1]))) {
462 # add it to the node table if it isn't present and it's
464 $node = DXNode->new($dxchan, $field[1], 0, 1, 5400);
465 broadcast_ak1a(pc19($dxchan, $node), $dxchan, $self) unless $dxchan->{isolate};
468 return unless $node; # ignore if havn't seen a PC19 for this one yet
469 return unless $node->isa('DXNode');
470 if ($node->dxchan != $self) {
471 dbg('chan', "LOOP: $field[1] came in on wrong channel");
474 if (($dxchan = DXChannel->get($field[1])) && $dxchan != $self) {
475 dbg('chan', "LOOP: $field[1] connected locally");
480 for ($i = 2; $i < $#field; $i++) {
481 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (\S) (\d)/o;
482 next if !$call || length $call < 3 || length $call > 8;
485 next if DXCluster->get_exact($call); # we already have this (loop?)
487 $confmode = $confmode eq '*';
488 DXNodeuser->new($self, $node, $call, $confmode, $here);
490 # add this station to the user database, if required
491 $call =~ s/-\d+$//o; # remove ssid for users
492 my $user = DXUser->get_current($call);
493 $user = DXUser->new($call) if !$user;
494 $user->homenode($node->call) if !$user->homenode;
495 $user->node($node->call);
496 $user->lastin($main::systime) unless DXChannel->get($call);
500 # queue up any messages (look for privates only)
501 DXMsg::queue_msg(1) if $self->state eq 'normal';
505 if ($pcno == 17) { # remove a user
506 my $node = DXCluster->get_exact($field[2]);
508 if (!$node && ($dxchan = DXChannel->get($field[2]))) {
509 # add it to the node table if it isn't present and it's
511 $node = DXNode->new($dxchan, $field[2], 0, 1, 5400);
512 broadcast_ak1a(pc19($dxchan, $node), $dxchan, $self) unless $dxchan->{isolate};
516 return unless $node->isa('DXNode');
517 if ($node->dxchan != $self) {
518 dbg('chan', "LOOP: $field[2] came in on wrong channel");
521 if (($dxchan = DXChannel->get($field[2])) && $dxchan != $self) {
522 dbg('chan', "LOOP: $field[2] connected locally");
525 my $ref = DXCluster->get_exact($field[1]);
530 if ($pcno == 18) { # link request
531 $self->state('init');
533 # first clear out any nodes on this dxchannel
534 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
535 foreach my $node (@gonenodes) {
536 next if $node->dxchan == $DXProt::me;
537 broadcast_ak1a(pc21($node->call, 'Gone, re-init') , $self) unless $self->{isolate};
540 $self->send_local_config();
542 return; # we don't pass these on
545 if ($pcno == 19) { # incoming cluster list
547 my $newline = "PC19^";
548 for ($i = 1; $i < $#field-1; $i += 4) {
549 my $here = $field[$i];
550 my $call = uc $field[$i+1];
551 my $confmode = $field[$i+2];
552 my $ver = $field[$i+3];
554 $ver = 5400 if !$ver && $allowzero;
556 # now check the call over
557 my $node = DXCluster->get_exact($call);
560 if (($dxchan = DXChannel->get($call)) && $dxchan != $self) {
561 dbg('chan', "LOOP: $call connected locally");
563 if ($node->dxchan != $self) {
564 dbg('chan', "LOOP: $call come in on wrong channel");
567 dbg('chan', "already have $call");
571 # check for sane parameters
572 next if $ver < 5000; # only works with version 5 software
573 next if length $call < 3; # min 3 letter callsigns
575 # add it to the nodes table and outgoing line
576 $newline .= "$here^$call^$confmode^$ver^";
577 DXNode->new($self, $call, $confmode, $here, $ver);
579 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
580 my $mref = DXMsg::get_busy($call);
581 $mref->stop_msg($call) if $mref;
583 # add this station to the user database, if required (don't remove SSID from nodes)
584 my $user = DXUser->get_current($call);
586 $user = DXUser->new($call);
588 $user->priv(1); # I have relented and defaulted nodes
589 $self->{priv} = 1; # to user RCMDs allowed
590 $user->homenode($call);
593 $user->lastin($main::systime) unless DXChannel->get($call);
597 return if $newline eq "PC19^";
600 $newline .= get_hops(19) . "^";
605 if ($pcno == 20) { # send local configuration
606 $self->send_local_config();
608 $self->state('normal');
612 if ($pcno == 21) { # delete a cluster from the list
613 my $call = uc $field[1];
614 if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
615 my $node = DXCluster->get_exact($call);
617 if ($node->dxchan != $self) {
618 dbg('chan', "LOOP: $call come in on wrong channel");
622 if (($dxchan = DXChannel->get($call)) && $dxchan != $self) {
623 dbg('chan', "LOOP: $call connected locally");
628 dbg('chan', "$call not in table, dropped");
636 $self->state('normal');
640 if ($pcno == 23 || $pcno == 27) { # WWV info
642 # route 'foreign' pc27s
644 if ($field[8] ne $main::mycall) {
645 $self->route($field[8], $line);
651 my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
652 my $sfi = unpad($field[3]);
653 my $k = unpad($field[4]);
654 my $i = unpad($field[5]);
655 my ($r) = $field[6] =~ /R=(\d+)/;
657 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
658 dbg('chan', "WWV Date ($field[1] $field[2]) out of range");
661 if (Geomag::dup($d,$sfi,$k,$i,$field[6])) {
662 dbg('chan', "Dup WWV Spot ignored\n");
665 $field[7] =~ s/-\d+$//o; # remove spotter's ssid
667 my $wwv = Geomag::update($d, $field[2], $sfi, $k, $i, @field[6..8], $r);
671 $rep = Local::wwv($self, $field[1], $field[2], $sfi, $k, $i, @field[6..8], $r);
673 # dbg('local', "Local::wwv2 error $@") if $@;
676 # DON'T be silly and send on PC27s!
677 return if $pcno == 27;
679 # broadcast to the eager world
680 send_wwv_spot($self, $line, $d, $field[2], $sfi, $k, $i, @field[6..8]);
684 if ($pcno == 24) { # set here status
685 my $call = uc $field[1];
686 my $ref = DXCluster->get_exact($call);
687 $ref->here($field[2]) if $ref;
691 if ($pcno == 25) { # merge request
692 if ($field[1] ne $main::mycall) {
693 $self->route($field[1], $line);
696 if ($field[2] eq $main::mycall) {
697 dbg('chan', "Trying to merge to myself, ignored");
701 Log('DXProt', "Merge request for $field[3] spots and $field[4] WWV from $field[1]");
705 my @in = reverse Spot::search(1, undef, undef, 0, $field[3]);
708 $self->send(pc26(@{$in}[0..4], $field[2]));
714 my @in = reverse Geomag::search(0, $field[4], time, 1);
717 $self->send(pc27(@{$in}[0..5], $field[2]));
723 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
724 if ($pcno == 49 || $field[1] eq $main::mycall) {
725 DXMsg::process($self, $line);
727 $self->route($field[1], $line);
732 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
733 if ($field[1] eq $main::mycall) {
734 my $ref = DXUser->get_current($field[2]);
735 my $cref = DXCluster->get($field[2]);
736 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
737 unless (!$cref || !$ref || $cref->mynode->call ne $ref->homenode) { # not allowed to relay RCMDS!
738 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
739 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
740 my $oldpriv = $self->{priv};
741 $self->{priv} = $ref->{priv}; # assume the user's privilege level
742 my @in = (DXCommandmode::run_cmd($self, $field[3]));
743 $self->{priv} = $oldpriv;
746 $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
747 Log('rcmd', 'out', $field[2], $_);
749 delete $self->{remotecmd};
751 $self->send(pc35($main::mycall, $field[2], "$main::mycall:sorry...!"));
754 $self->send(pc35($main::mycall, $field[2], "$main::mycall:your attempt is logged, Tut tut tut...!"));
757 my $ref = DXUser->get_current($field[1]);
758 if ($ref && $ref->is_clx) {
759 route($field[1], pc84($field[2], $field[1], $field[2], $field[3]));
761 $self->route($field[1], $line);
767 if ($pcno == 35) { # remote command replies
768 if ($field[1] eq $main::mycall) {
769 my $s = $rcmds{$field[2]};
771 my $dxchan = DXChannel->get($s->{call});
772 $dxchan->send($field[3]) if $dxchan;
773 delete $rcmds{$field[2]} if !$dxchan;
775 # send unsolicited ones to the sysop
776 my $dxchan = DXChannel->get($main::myalias);
777 $dxchan->send($field[3]) if $dxchan;
780 my $ref = DXUser->get_current($field[1]);
781 if ($ref && $ref->is_clx) {
782 route($field[1], pc85($field[2], $field[1], $field[2], $field[3]));
784 $self->route($field[1], $line);
790 # for pc 37 see 44 onwards
792 if ($pcno == 38) { # node connected list from neighbour
796 if ($pcno == 39) { # incoming disconnect
797 $self->disconnect(1);
801 if ($pcno == 41) { # user info
802 # add this station to the user database, if required
803 my $user = DXUser->get_current($field[1]);
805 # then try without an SSID
806 $field[1] =~ s/-\d+$//o;
807 $user = DXUser->get_current($field[1]);
809 $user = DXUser->new($field[1]) if !$user;
811 if ($field[2] == 1) {
812 $user->name($field[3]);
813 } elsif ($field[2] == 2) {
814 $user->qth($field[3]);
815 } elsif ($field[2] == 3) {
816 my ($lat, $long) = DXBearing::stoll($field[3]);
819 $user->qra(DXBearing::lltoqra($lat, $long)) unless $user->qra && DXBearing::is_qra($user->qra);
820 } elsif ($field[2] == 4) {
821 $user->homenode($field[3]);
823 $user->lastoper($main::systime); # to cut down on excessive for/opers being generated
830 if ($pcno == 37 || $pcno == 44 || $pcno == 45 || $pcno == 46 || $pcno == 47 || $pcno == 48) {
831 DXDb::process($self, $line);
835 if ($pcno == 50) { # keep alive/user list
836 my $node = DXCluster->get_exact($field[1]);
838 return unless $node->isa('DXNode');
839 return unless $node->dxchan == $self;
840 $node->update_users($field[2]);
845 if ($pcno == 51) { # incoming ping requests/answers
848 if ($field[1] eq $main::mycall) {
849 my $flag = $field[3];
851 $self->send(pc51($field[2], $field[1], '0'));
853 # it's a reply, look in the ping list for this one
854 my $ref = $pings{$field[2]};
856 my $tochan = DXChannel->get($field[2]);
859 my $dxchan = DXChannel->get($r->{call});
861 my $t = tv_interval($r->{t}, [ gettimeofday ]);
862 if ($dxchan->is_user) {
863 my $s = sprintf "%.2f", $t;
864 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
865 $dxchan->send($dxchan->msg('pingi', $field[2], $s, $ave))
866 } elsif ($dxchan->is_node) {
868 $tochan->{nopings} = 2; # pump up the timer
869 push @{$tochan->{pingtime}}, $t;
870 shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
872 for (@{$tochan->{pingtime}}) {
875 $tochan->{pingave} = $st / @{$tochan->{pingtime}};
882 # route down an appropriate thingy
883 $self->route($field[1], $line);
888 if ($pcno == 73) { # WCY broadcasts
891 my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
892 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
893 dbg('chan', "WCY Date ($field[1] $field[2]) out of range");
896 @field = map { unpad($_) } @field;
897 if (WCY::dup($d,@field[3..7])) {
898 dbg('chan', "Dup WCY Spot ignored\n");
902 my $wcy = WCY::update($d, @field[2..12]);
906 $rep = Local::wwv($self, @field[1..12]);
908 # dbg('local', "Local::wcy error $@") if $@;
911 # broadcast to the eager world
912 send_wcy_spot($self, $line, $d, @field[2..12]);
916 if ($pcno == 84) { # remote commands (incoming)
917 if ($field[1] eq $main::mycall) {
918 my $ref = DXUser->get_current($field[2]);
919 my $cref = DXCluster->get($field[2]);
920 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[4]);
921 unless ($field[4] =~ /rcmd/i || !$cref || !$ref || $cref->mynode->call ne $ref->homenode) { # not allowed to relay RCMDS!
922 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
923 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
924 my $oldpriv = $self->{priv};
925 $self->{priv} = $ref->{priv}; # assume the user's privilege level
926 my @in = (DXCommandmode::run_cmd($self, $field[4]));
927 $self->{priv} = $oldpriv;
930 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:$_"));
931 Log('rcmd', 'out', $field[2], $_);
933 delete $self->{remotecmd};
935 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:sorry...!"));
938 $self->send(pc85($main::mycall, $field[2], $field[3],"$main::mycall:your attempt is logged, Tut tut tut...!"));
941 my $ref = DXUser->get_current($field[1]);
942 if ($ref && $ref->is_clx) {
943 $self->route($field[1], $line);
945 route($field[1], pc34($field[2], $field[1], $field[4]));
951 if ($pcno == 85) { # remote command replies
952 if ($field[1] eq $main::mycall) {
953 my $dxchan = DXChannel->get($field[3]);
955 $dxchan->send($field[4]);
957 my $s = $rcmds{$field[2]};
959 $dxchan = DXChannel->get($s->{call});
960 $dxchan->send($field[4]) if $dxchan;
961 delete $rcmds{$field[2]} if !$dxchan;
963 # send unsolicited ones to the sysop
964 my $dxchan = DXChannel->get($main::myalias);
965 $dxchan->send($field[4]) if $dxchan;
969 my $ref = DXUser->get_current($field[1]);
970 if ($ref && $ref->is_clx) {
971 $self->route($field[1], $line);
973 route($field[1], pc35($field[2], $field[1], $field[4]));
980 # if get here then rebroadcast the thing with its Hop count decremented (if
981 # there is one). If it has a hop count and it decrements to zero then don't
984 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
988 unless ($self->{isolate}) {
989 broadcast_ak1a($line, $self); # send it to everyone but me
994 # This is called from inside the main cluster processing loop and is used
995 # for despatching commands that are doing some long processing job
1000 my @dxchan = DXChannel->get_all();
1003 foreach $dxchan (@dxchan) {
1004 next unless $dxchan->is_node();
1005 next if $dxchan == $me;
1007 # send a pc50 out on this channel
1008 if ($t >= $dxchan->pc50_t + $DXProt::pc50_interval) {
1009 $dxchan->send(pc50(scalar DXChannel::get_all_users));
1010 $dxchan->pc50_t($t);
1013 # send a ping out on this channel
1014 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
1015 if ($dxchan->{nopings} <= 0) {
1016 $dxchan->disconnect;
1018 addping($main::mycall, $dxchan->call);
1019 $dxchan->{nopings} -= 1;
1020 $dxchan->{lastping} = $t;
1028 if ($main::systime - 3600 > $last_hour) {
1032 $last_hour = $main::systime;
1037 # finish up a pc context
1042 my $call = $self->call;
1044 my $ref = DXCluster->get_exact($call);
1046 # unbusy and stop and outgoing mail
1047 my $mref = DXMsg::get_busy($call);
1048 $mref->stop_msg($call) if $mref;
1050 # broadcast to all other nodes that all the nodes connected to via me are gone
1051 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
1054 foreach $node (@gonenodes) {
1055 next if $node->call eq $call;
1056 broadcast_ak1a(pc21($node->call, 'Gone') , $self) unless $self->{isolate};
1060 # remove outstanding pings
1061 delete $pings{$call};
1063 # now broadcast to all other ak1a nodes that I have gone
1064 broadcast_ak1a(pc21($call, 'Gone.'), $self) unless $self->{isolate};
1066 # I was the last node visited
1067 $self->user->node($main::mycall);
1069 # send info to all logged in thingies
1070 $self->tell_login('logoutn');
1072 Log('DXProt', $call . " Disconnected");
1073 $ref->del() if $ref;
1077 # some active measures
1083 my @dxchan = DXChannel->get_all();
1086 # send it if it isn't the except list and isn't isolated and still has a hop count
1087 # taking into account filtering and so on
1088 foreach $dxchan (@dxchan) {
1090 my ($filter, $hops);
1092 if ($dxchan->{spotfilter}) {
1093 ($filter, $hops) = Filter::it($dxchan->{spotfilter}, @_, $self->{call} );
1094 next unless $filter;
1097 if ($dxchan->is_node) {
1098 next if $dxchan == $self;
1101 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1103 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1104 next unless $routeit;
1107 $dxchan->send($routeit) if $routeit;
1109 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1111 } elsif ($dxchan->is_user && $dxchan->{dx}) {
1112 my $buf = Spot::formatb($dxchan->{user}->wantgrid, $_[0], $_[1], $_[2], $_[3], $_[4]);
1113 $buf .= "\a\a" if $dxchan->{beep};
1114 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1115 $dxchan->send($buf);
1117 $dxchan->delay($buf);
1127 my @dxchan = DXChannel->get_all();
1130 # send it if it isn't the except list and isn't isolated and still has a hop count
1131 # taking into account filtering and so on
1132 foreach $dxchan (@dxchan) {
1134 my ($filter, $hops);
1136 if ($dxchan->{wwvfilter}) {
1137 ($filter, $hops) = Filter::it($dxchan->{wwvfilter}, @_, $self->{call} );
1138 next unless $filter;
1140 if ($dxchan->is_node) {
1141 next if $dxchan == $self;
1144 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1146 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1147 next unless $routeit;
1150 $dxchan->send($routeit) if $routeit;
1152 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1155 } elsif ($dxchan->is_user && $dxchan->{wwv}) {
1156 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
1157 $buf .= "\a\a" if $dxchan->{beep};
1158 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1159 $dxchan->send($buf);
1161 $dxchan->delay($buf);
1171 my @dxchan = DXChannel->get_all();
1174 # send it if it isn't the except list and isn't isolated and still has a hop count
1175 # taking into account filtering and so on
1176 foreach $dxchan (@dxchan) {
1178 my ($filter, $hops);
1180 if ($dxchan->{wcyfilter}) {
1181 ($filter, $hops) = Filter::it($dxchan->{wcyfilter}, @_, $self->{call} );
1182 next unless $filter;
1184 if ($dxchan->is_clx || $dxchan->is_spider) {
1185 next if $dxchan == $self;
1188 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1190 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1191 next unless $routeit;
1194 $dxchan->send($routeit) if $routeit;
1196 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1198 } elsif ($dxchan->is_user && $dxchan->{wcy}) {
1199 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1200 $buf .= "\a\a" if $dxchan->{beep};
1201 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1202 $dxchan->send($buf);
1204 $dxchan->delay($buf);
1215 my @dxchan = DXChannel->get_all();
1217 my $text = unpad($_[2]);
1221 if ($_[3] eq '*') { # sysops
1223 } elsif ($_[3] gt ' ') { # speciality list handling
1224 my ($name) = split /\./, $_[3];
1225 $target = "$name"; # put the rest in later (if bothered)
1232 $target = "All" if !$target;
1234 Log('ann', $target, $_[0], $text);
1236 # send it if it isn't the except list and isn't isolated and still has a hop count
1237 # taking into account filtering and so on
1238 foreach $dxchan (@dxchan) {
1240 my ($filter, $hops);
1242 if ($dxchan->{annfilter}) {
1243 ($filter, $hops) = Filter::it($dxchan->{annfilter}, @_, $self->{call} );
1244 next unless $filter;
1246 if ($dxchan->is_node && $_[1] ne $main::mycall) { # i.e not specifically routed to me
1247 next if $dxchan == $self;
1250 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1252 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1253 next unless $routeit;
1256 $dxchan->send($routeit) if $routeit;
1258 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1261 } elsif ($dxchan->is_user && $dxchan->{ann}) {
1262 next if $target eq 'SYSOP' && $dxchan->{priv} < 5;
1263 my $buf = "$to$target de $_[0]: $text";
1264 $buf .= "\a\a" if $dxchan->{beep};
1265 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1266 $dxchan->send($buf);
1268 $dxchan->delay($buf);
1274 sub send_local_config
1283 if ($self->{isolate}) {
1284 @localnodes = (DXCluster->get_exact($main::mycall));
1286 # create a list of all the nodes that are not connected to this connection
1287 # and are not themselves isolated, this to make sure that isolated nodes
1288 # don't appear outside of this node
1289 @nodes = DXNode::get_all();
1290 @nodes = grep { $_->{call} ne $main::mycall } @nodes;
1291 @nodes = grep { $_->dxchan != $self } @nodes if @nodes;
1292 @nodes = grep { !$_->dxchan->{isolate} } @nodes if @nodes;
1293 @localnodes = grep { $_->dxchan->{call} eq $_->{call} } @nodes if @nodes;
1294 unshift @localnodes, DXCluster->get_exact($main::mycall);
1295 @remotenodes = grep { $_->dxchan->{call} ne $_->{call} } @nodes if @nodes;
1298 my @s = $me->pc19(@localnodes, @remotenodes);
1300 my $routeit = adjust_hops($self, $_);
1301 $self->send($routeit) if $routeit;
1304 # get all the users connected on the above nodes and send them out
1305 foreach $n (@localnodes, @remotenodes) {
1306 my @users = values %{$n->list};
1307 my @s = pc16($n, @users);
1309 my $routeit = adjust_hops($self, $_);
1310 $self->send($routeit) if $routeit;
1316 # route a message down an appropriate interface for a callsign
1318 # is called route(to, pcline);
1322 my ($self, $call, $line) = @_;
1323 my $cl = DXCluster->get_exact($call);
1324 if ($cl) { # don't route it back down itself
1325 if (ref $self && $call eq $self->{call}) {
1326 dbg('chan', "Trying to route back to source, dropped");
1330 my $dxchan = $cl->{dxchan};
1332 my $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1334 $dxchan->send($routeit) if $dxchan;
1340 # broadcast a message to all clusters taking into account isolation
1341 # [except those mentioned after buffer]
1344 my $s = shift; # the line to be rebroadcast
1345 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1346 my @dxchan = DXChannel::get_all_ak1a();
1349 # send it if it isn't the except list and isn't isolated and still has a hop count
1350 foreach $dxchan (@dxchan) {
1351 next if grep $dxchan == $_, @except;
1352 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
1353 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
1357 # broadcast a message to all clusters ignoring isolation
1358 # [except those mentioned after buffer]
1359 sub broadcast_all_ak1a
1361 my $s = shift; # the line to be rebroadcast
1362 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1363 my @dxchan = DXChannel::get_all_ak1a();
1366 # send it if it isn't the except list and isn't isolated and still has a hop count
1367 foreach $dxchan (@dxchan) {
1368 next if grep $dxchan == $_, @except;
1369 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
1370 $dxchan->send($routeit);
1374 # broadcast to all users
1375 # storing the spot or whatever until it is in a state to receive it
1378 my $s = shift; # the line to be rebroadcast
1379 my $sort = shift; # the type of transmission
1380 my $fref = shift; # a reference to an object to filter on
1381 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1382 my @dxchan = DXChannel::get_all_users();
1386 foreach $dxchan (@dxchan) {
1387 next if grep $dxchan == $_, @except;
1390 broadcast_list($s, $sort, $fref, @out);
1393 # broadcast to a list of users
1401 foreach $dxchan (@_) {
1404 if ($sort eq 'dx') {
1405 next unless $dxchan->{dx};
1406 ($filter) = Filter::it($dxchan->{spotfilter}, @{$fref}) if ref $fref;
1407 next unless $filter;
1409 next if $sort eq 'ann' && !$dxchan->{ann};
1410 next if $sort eq 'wwv' && !$dxchan->{wwv};
1411 next if $sort eq 'wcy' && !$dxchan->{wcy};
1412 next if $sort eq 'wx' && !$dxchan->{wx};
1414 $s =~ s/\a//og unless $dxchan->{beep};
1416 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1426 # obtain the hops from the list for this callsign and pc no
1432 my $hops = $DXProt::hopcount{$pcno};
1433 $hops = $DXProt::def_hopcount if !$hops;
1438 # adjust the hop count on a per node basis using the user loadable
1439 # hop table if available or else decrement an existing one
1446 my $call = $self->{call};
1449 if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
1450 my ($pcno) = $s =~ /^PC(\d\d)/o;
1451 confess "$call called adjust_hops with '$s'" unless $pcno;
1452 my $ref = $nodehops{$call} if %nodehops;
1454 my $newhops = $ref->{$pcno};
1455 return "" if defined $newhops && $newhops == 0;
1456 $newhops = $ref->{default} unless $newhops;
1457 return "" if defined $newhops && $newhops == 0;
1458 $newhops = $hops if !$newhops;
1459 $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
1461 # simply decrement it
1463 return "" if !$hops;
1464 $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
1476 return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1477 do "$main::data/hop_table.pl";
1483 # add a ping request to the ping queues
1486 my ($from, $to) = @_;
1487 my $ref = $pings{$to} || [];
1490 $r->{t} = [ gettimeofday ];
1491 route(undef, $to, pc51($to, $main::mycall, 1));
1496 # add a rcmd request to the rcmd queues
1499 my ($self, $to, $cmd) = @_;
1502 $r->{call} = $self->{call};
1503 $r->{t} = $main::systime;
1507 my $ref = DXCluster->get_exact($to);
1508 if ($ref && $ref->dxchan && $ref->dxchan->is_clx) {
1509 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1511 route(undef, $to, pc34($main::mycall, $to, $cmd));
1520 if ($self->{conn} && !$nopc39) {
1521 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1524 $self->SUPER::disconnect;
1529 # send a talk message to this thingy
1533 my ($self, $from, $to, $via, $line) = @_;
1535 $line =~ s/\^/\\5E/g; # remove any ^ characters
1536 $self->send(DXProt::pc10($from, $to, $via, $line));
1537 Log('talk', $self->call, $from, $via?$via:$main::mycall, $line);