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 c m d t 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 t n n n m c c h), # pc23
70 qw(f c m d t c c), # pc26
71 qw(d t n n n m c c), # pc27
72 qw(c c c c d t p m bp n p bp c), # pc28
86 undef, # pc43 don't handle it
87 qw(c c n m m c), # pc44
116 qw(d t n n n n n n m m m c c), # pc73
131 # use the entry in the check list to check the field list presented
132 # return OK if line NOT in check list (for now)
137 return 0 if $n < 10 || $n > @checklist;
138 my $ref = $checklist[$n];
139 return 0 unless ref $ref;
142 shift; # not interested in the first field
143 for ($i = 0; $i < @_; $i++) {
144 my ($blank, $act) = $$ref[$i] =~ /^(b?)(\w)$/;
145 next if $blank && $_[$i] eq ' ';
147 return $i+1 unless is_callsign($_[$i]);
148 } elsif ($act eq 'm') {
149 return $i+1 unless is_pctext($_[$i]);
150 } elsif ($act eq 'p') {
151 return $i+1 unless is_pcflag($_[$i]);
152 } elsif ($act eq 'f') {
153 return $i+1 unless is_freq($_[$i]);
154 } elsif ($act eq 'n') {
155 return $i+1 if $_[$i] !~ /^[^\d ]$/;
156 } elsif ($act eq 'h') {
157 return $i+1 unless $_[$i] =~ /^H\d\d?$/;
158 } elsif ($act eq 'd') {
159 return $i+1 unless $_[$i] =~ /^\s*\d+-\w\w\w-[12][90]\d\d$/;
160 } elsif ($act eq 't') {
161 return $i+1 unless $_[$i] =~ /^[012]\d[012345]\dZ$/;
169 my $user = DXUser->get($main::mycall);
170 $DXProt::myprot_version += $main::version*100;
171 $me = DXProt->new($main::mycall, 0, $user);
173 $me->{state} = "indifferent";
174 do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
176 # $me->{sort} = 'M'; # M for me
178 # now prime the spot and wwv duplicates file with data
179 my @today = Julian::unixtoj(time);
180 for (Spot::readfile(@today), Spot::readfile(Julian::sub(@today, 1))) {
181 Spot::dup(@{$_}[0..3]);
183 for (Geomag::readfile(time)) {
184 Geomag::dup(@{$_}[1..5]);
187 # load the baddx file
188 do "$baddxfn" if -e "$baddxfn";
193 # obtain a new connection this is derived from dxchannel
198 my $self = DXChannel::alloc(@_);
202 # this is how a pc connection starts (for an incoming connection)
203 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
204 # all the crap that comes between).
207 my ($self, $line, $sort) = @_;
208 my $call = $self->{call};
209 my $user = $self->{user};
211 # remember type of connection
212 $self->{consort} = $line;
213 $self->{outbound} = $sort eq 'O';
214 $self->{priv} = $user->priv || 1; # other clusters can always be 'normal' users
215 $self->{lang} = $user->lang || 'en';
216 $self->{isolate} = $user->{isolate};
217 $self->{consort} = $line; # save the connection type
220 # get the INPUT filters (these only pertain to Clusters)
221 $self->{inspotfilter} = Filter::read_in('spots', $call, 1);
222 $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1);
223 $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1);
224 $self->{inannfilter} = Filter::read_in('ann', $call, 1);
226 # set unbuffered and no echo
227 $self->send_now('B',"0");
228 $self->send_now('E',"0");
230 # ping neighbour node stuff
231 my $ping = $user->pingint;
232 $ping = 5*60 unless defined $ping;
233 $self->{pingint} = $ping;
234 $self->{nopings} = $user->nopings || 2;
235 $self->{pingtime} = [ ];
236 $self->{pingave} = 0;
238 # send initialisation string
239 unless ($self->{outbound}) {
240 $self->send(pc38()) if DXNode->get_all();
242 $self->{lastping} = $main::systime;
244 # remove from outstanding connects queue
245 @main::outstanding_connects = grep {$_->{call} ne $call} @main::outstanding_connects;
246 $self->{lastping} = $main::systime + $self->pingint / 2;
248 $self->state('init');
251 # send info to all logged in thingies
252 $self->tell_login('loginn');
254 Log('DXProt', "$call connected");
258 # This is the normal pcxx despatcher
262 my ($self, $line) = @_;
263 my @field = split /\^/, $line;
264 pop @field if $field[-1] eq '~';
266 # print join(',', @field), "\n";
268 # ignore any lines that don't start with PC
269 return if !$field[0] =~ /^PC/;
272 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
274 return if $pcno < 10 || $pcno > 99;
276 # check for and dump bad protocol messages
277 my $n = check($pcno, @field);
279 dbg('chan', "bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")");
286 $pcr = Local::pcprot($self, $pcno, @field);
288 # dbg('local', "Local::pcprot error $@") if $@;
292 if ($pcno == 10) { # incoming talk
294 # is it for me or one of mine?
295 my ($to, $via, $call, $dxchan);
296 if ($field[5] gt ' ') {
297 $call = $via = $field[2];
299 unless (is_callsign($to)) {
300 dbg('chan', "Corrupt talk, rejected");
304 $call = $to = $field[2];
306 if ($dxchan = DXChannel->get($call)) {
307 $dxchan->talk($field[1], $to, $via, $field[3]);
309 $self->route($field[2], $line); # relay it on its way
314 if ($pcno == 11 || $pcno == 26) { # dx spot
316 # route 'foreign' pc26s
318 if ($field[7] ne $main::mycall) {
319 $self->route($field[7], $line);
324 # if this is a 'nodx' node then ignore it
325 if (grep $field[7] =~ /^$_/, @DXProt::nodx_node) {
326 dbg('chan', "Bad DXNode, dropped");
330 # convert the date to a unix date
331 my $d = cltounix($field[3], $field[4]);
332 # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
333 if (!$d || ($pcno == 11 && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
334 dbg('chan', "Spot ignored, invalid date or out of range ($field[3] $field[4])\n");
339 if (grep $field[2] eq $_, @baddx) {
340 dbg('chan', "Bad DX spot, ignored");
345 $field[5] =~ s/^\s+//; # take any leading blanks off
346 if (Spot::dup($field[1], $field[2], $d, $field[5])) {
347 dbg('chan', "Duplicate Spot ignored\n");
351 my @spot = Spot::add($field[1], $field[2], $d, $field[5], $field[6], $field[7]);
354 # @spot at this point contains:-
355 # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
356 # then spotted itu, spotted cq, spotters itu, spotters cq
357 # you should be able to route on any of these
360 # fix up qra locators of known users
361 my $user = DXUser->get_current($spot[4]);
363 my $qra = $user->qra;
364 unless ($qra && DXBearing::is_qra($qra)) {
365 my $lat = $user->lat;
366 my $long = $user->long;
367 if (defined $lat && defined $long) {
368 $user->qra(DXBearing::lltoqra($lat, $long));
373 # send a remote command to a distant cluster if it is visible and there is no
374 # qra locator and we havn't done it for a month.
376 unless ($user->qra) {
378 my $to = $user->homenode;
379 my $last = $user->lastoper || 0;
380 if ($send_opernam && $main::systime > $last + $DXUser::lastoperinterval && $to && ($node = DXCluster->get_exact($to)) ) {
381 my $cmd = "forward/opernam $spot[4]";
382 # send the rcmd but we aren't interested in the replies...
383 if ($node && $node->dxchan && $node->dxchan->is_clx) {
384 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
386 route(undef, $to, pc34($main::mycall, $to, $cmd));
388 if ($to ne $field[7]) {
390 $node = DXCluster->get_exact($to);
391 if ($node && $node->dxchan && $node->dxchan->is_clx) {
392 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
394 route(undef, $to, pc34($main::mycall, $to, $cmd));
397 $user->lastoper($main::systime);
406 $r = Local::spot($self, @spot);
408 # dbg('local', "Local::spot1 error $@") if $@;
411 # DON'T be silly and send on PC26s!
412 return if $pcno == 26;
414 # send out the filtered spots
415 send_dx_spot($self, $line, @spot) if @spot;
419 if ($pcno == 12) { # announces
420 # announce duplicate checking
421 $field[3] =~ s/^\s+//; # remove leading blanks
422 if (AnnTalk::dup($field[1], $field[2], $field[3])) {
423 dbg('chan', "Duplicate Announce ignored\n");
427 if ($field[2] eq '*' || $field[2] eq $main::mycall) {
429 # global ann filtering on INPUT
430 if ($self->{inannfilter}) {
431 my ($filter, $hops) = Filter::it($self->{inannfilter}, @field[1..6], $self->{call} );
433 dbg('chan', "Rejected by filter");
439 $self->send_announce($line, @field[1..6]);
441 $self->route($field[2], $line);
457 if ($pcno == 16) { # add a user
458 my $node = DXCluster->get_exact($field[1]);
460 if (!$node && ($dxchan = DXChannel->get($field[1]))) {
461 # add it to the node table if it isn't present and it's
463 $node = DXNode->new($dxchan, $field[1], 0, 1, 5400);
464 broadcast_ak1a(pc19($dxchan, $node), $dxchan, $self) unless $dxchan->{isolate};
467 return unless $node; # ignore if havn't seen a PC19 for this one yet
468 return unless $node->isa('DXNode');
469 if ($node->dxchan != $self) {
470 dbg('chan', "LOOP: $field[1] came in on wrong channel");
473 if (($dxchan = DXChannel->get($field[1])) && $dxchan != $self) {
474 dbg('chan', "LOOP: $field[1] connected locally");
479 for ($i = 2; $i < $#field; $i++) {
480 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (\S) (\d)/o;
481 next if !$call || length $call < 3 || length $call > 8;
484 next if DXCluster->get_exact($call); # we already have this (loop?)
486 $confmode = $confmode eq '*';
487 DXNodeuser->new($self, $node, $call, $confmode, $here);
489 # add this station to the user database, if required
490 $call =~ s/-\d+$//o; # remove ssid for users
491 my $user = DXUser->get_current($call);
492 $user = DXUser->new($call) if !$user;
493 $user->homenode($node->call) if !$user->homenode;
494 $user->node($node->call);
495 $user->lastin($main::systime) unless DXChannel->get($call);
499 # queue up any messages (look for privates only)
500 DXMsg::queue_msg(1) if $self->state eq 'normal';
504 if ($pcno == 17) { # remove a user
505 my $node = DXCluster->get_exact($field[2]);
507 if (!$node && ($dxchan = DXChannel->get($field[2]))) {
508 # add it to the node table if it isn't present and it's
510 $node = DXNode->new($dxchan, $field[2], 0, 1, 5400);
511 broadcast_ak1a(pc19($dxchan, $node), $dxchan, $self) unless $dxchan->{isolate};
515 return unless $node->isa('DXNode');
516 if ($node->dxchan != $self) {
517 dbg('chan', "LOOP: $field[2] came in on wrong channel");
520 if (($dxchan = DXChannel->get($field[2])) && $dxchan != $self) {
521 dbg('chan', "LOOP: $field[2] connected locally");
524 my $ref = DXCluster->get_exact($field[1]);
529 if ($pcno == 18) { # link request
530 $self->state('init');
532 # first clear out any nodes on this dxchannel
533 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
534 foreach my $node (@gonenodes) {
535 next if $node->dxchan == $DXProt::me;
536 broadcast_ak1a(pc21($node->call, 'Gone, re-init') , $self) unless $self->{isolate};
539 $self->send_local_config();
541 return; # we don't pass these on
544 if ($pcno == 19) { # incoming cluster list
546 my $newline = "PC19^";
547 for ($i = 1; $i < $#field-1; $i += 4) {
548 my $here = $field[$i];
549 my $call = uc $field[$i+1];
550 my $confmode = $field[$i+2];
551 my $ver = $field[$i+3];
553 $ver = 5400 if !$ver && $allowzero;
555 # now check the call over
556 my $node = DXCluster->get_exact($call);
559 if (($dxchan = DXChannel->get($call)) && $dxchan != $self) {
560 dbg('chan', "LOOP: $call connected locally");
562 if ($node->dxchan != $self) {
563 dbg('chan', "LOOP: $call come in on wrong channel");
566 dbg('chan', "already have $call");
570 # check for sane parameters
571 next if $ver < 5000; # only works with version 5 software
572 next if length $call < 3; # min 3 letter callsigns
574 # add it to the nodes table and outgoing line
575 $newline .= "$here^$call^$confmode^$ver^";
576 DXNode->new($self, $call, $confmode, $here, $ver);
578 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
579 my $mref = DXMsg::get_busy($call);
580 $mref->stop_msg($call) if $mref;
582 # add this station to the user database, if required (don't remove SSID from nodes)
583 my $user = DXUser->get_current($call);
585 $user = DXUser->new($call);
587 $user->priv(1); # I have relented and defaulted nodes
588 $self->{priv} = 1; # to user RCMDs allowed
589 $user->homenode($call);
592 $user->lastin($main::systime) unless DXChannel->get($call);
596 return if $newline eq "PC19^";
599 $newline .= get_hops(19) . "^";
604 if ($pcno == 20) { # send local configuration
605 $self->send_local_config();
607 $self->state('normal');
611 if ($pcno == 21) { # delete a cluster from the list
612 my $call = uc $field[1];
613 if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
614 my $node = DXCluster->get_exact($call);
616 if ($node->dxchan != $self) {
617 dbg('chan', "LOOP: $call come in on wrong channel");
621 if (($dxchan = DXChannel->get($call)) && $dxchan != $self) {
622 dbg('chan', "LOOP: $call connected locally");
627 dbg('chan', "$call not in table, dropped");
635 $self->state('normal');
639 if ($pcno == 23 || $pcno == 27) { # WWV info
641 # route 'foreign' pc27s
643 if ($field[8] ne $main::mycall) {
644 $self->route($field[8], $line);
650 my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
651 my $sfi = unpad($field[3]);
652 my $k = unpad($field[4]);
653 my $i = unpad($field[5]);
654 my ($r) = $field[6] =~ /R=(\d+)/;
656 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
657 dbg('chan', "WWV Date ($field[1] $field[2]) out of range");
660 if (Geomag::dup($d,$sfi,$k,$i,$field[6])) {
661 dbg('chan', "Dup WWV Spot ignored\n");
664 $field[7] =~ s/-\d+$//o; # remove spotter's ssid
666 my $wwv = Geomag::update($d, $field[2], $sfi, $k, $i, @field[6..8], $r);
670 $rep = Local::wwv($self, $field[1], $field[2], $sfi, $k, $i, @field[6..8], $r);
672 # dbg('local', "Local::wwv2 error $@") if $@;
675 # DON'T be silly and send on PC27s!
676 return if $pcno == 27;
678 # broadcast to the eager world
679 send_wwv_spot($self, $line, $d, $field[2], $sfi, $k, $i, @field[6..8]);
683 if ($pcno == 24) { # set here status
684 my $call = uc $field[1];
685 my $ref = DXCluster->get_exact($call);
686 $ref->here($field[2]) if $ref;
690 if ($pcno == 25) { # merge request
691 if ($field[1] ne $main::mycall) {
692 $self->route($field[1], $line);
695 if ($field[2] eq $main::mycall) {
696 dbg('chan', "Trying to merge to myself, ignored");
700 Log('DXProt', "Merge request for $field[3] spots and $field[4] WWV from $field[1]");
704 my @in = reverse Spot::search(1, undef, undef, 0, $field[3]);
707 $self->send(pc26(@{$in}[0..4], $field[2]));
713 my @in = reverse Geomag::search(0, $field[4], time, 1);
716 $self->send(pc27(@{$in}[0..5], $field[2]));
722 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
723 if ($pcno == 49 || $field[1] eq $main::mycall) {
724 DXMsg::process($self, $line);
726 $self->route($field[1], $line);
731 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
732 if ($field[1] eq $main::mycall) {
733 my $ref = DXUser->get_current($field[2]);
734 my $cref = DXCluster->get($field[2]);
735 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
736 unless (!$cref || !$ref || $cref->mynode->call ne $ref->homenode) { # not allowed to relay RCMDS!
737 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
738 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
739 my $oldpriv = $self->{priv};
740 $self->{priv} = $ref->{priv}; # assume the user's privilege level
741 my @in = (DXCommandmode::run_cmd($self, $field[3]));
742 $self->{priv} = $oldpriv;
745 $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
746 Log('rcmd', 'out', $field[2], $_);
748 delete $self->{remotecmd};
750 $self->send(pc35($main::mycall, $field[2], "$main::mycall:sorry...!"));
753 $self->send(pc35($main::mycall, $field[2], "$main::mycall:your attempt is logged, Tut tut tut...!"));
756 my $ref = DXUser->get_current($field[1]);
757 if ($ref && $ref->is_clx) {
758 route($field[1], pc84($field[2], $field[1], $field[2], $field[3]));
760 $self->route($field[1], $line);
766 if ($pcno == 35) { # remote command replies
767 if ($field[1] eq $main::mycall) {
768 my $s = $rcmds{$field[2]};
770 my $dxchan = DXChannel->get($s->{call});
771 $dxchan->send($field[3]) if $dxchan;
772 delete $rcmds{$field[2]} if !$dxchan;
774 # send unsolicited ones to the sysop
775 my $dxchan = DXChannel->get($main::myalias);
776 $dxchan->send($field[3]) if $dxchan;
779 my $ref = DXUser->get_current($field[1]);
780 if ($ref && $ref->is_clx) {
781 route($field[1], pc85($field[2], $field[1], $field[2], $field[3]));
783 $self->route($field[1], $line);
789 # for pc 37 see 44 onwards
791 if ($pcno == 38) { # node connected list from neighbour
795 if ($pcno == 39) { # incoming disconnect
796 $self->disconnect(1);
800 if ($pcno == 41) { # user info
801 # add this station to the user database, if required
802 my $user = DXUser->get_current($field[1]);
804 # then try without an SSID
805 $field[1] =~ s/-\d+$//o;
806 $user = DXUser->get_current($field[1]);
808 $user = DXUser->new($field[1]) if !$user;
810 if ($field[2] == 1) {
811 $user->name($field[3]);
812 } elsif ($field[2] == 2) {
813 $user->qth($field[3]);
814 } elsif ($field[2] == 3) {
815 my ($lat, $long) = DXBearing::stoll($field[3]);
818 $user->qra(DXBearing::lltoqra($lat, $long)) unless $user->qra && DXBearing::is_qra($user->qra);
819 } elsif ($field[2] == 4) {
820 $user->homenode($field[3]);
822 $user->lastoper($main::systime); # to cut down on excessive for/opers being generated
829 if ($pcno == 37 || $pcno == 44 || $pcno == 45 || $pcno == 46 || $pcno == 47 || $pcno == 48) {
830 DXDb::process($self, $line);
834 if ($pcno == 50) { # keep alive/user list
835 my $node = DXCluster->get_exact($field[1]);
837 return unless $node->isa('DXNode');
838 return unless $node->dxchan == $self;
839 $node->update_users($field[2]);
844 if ($pcno == 51) { # incoming ping requests/answers
847 if ($field[1] eq $main::mycall) {
848 my $flag = $field[3];
850 $self->send(pc51($field[2], $field[1], '0'));
852 # it's a reply, look in the ping list for this one
853 my $ref = $pings{$field[2]};
855 my $tochan = DXChannel->get($field[2]);
858 my $dxchan = DXChannel->get($r->{call});
860 my $t = tv_interval($r->{t}, [ gettimeofday ]);
861 if ($dxchan->is_user) {
862 my $s = sprintf "%.2f", $t;
863 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
864 $dxchan->send($dxchan->msg('pingi', $field[2], $s, $ave))
865 } elsif ($dxchan->is_node) {
867 $tochan->{nopings} = 2; # pump up the timer
868 push @{$tochan->{pingtime}}, $t;
869 shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
871 for (@{$tochan->{pingtime}}) {
874 $tochan->{pingave} = $st / @{$tochan->{pingtime}};
881 # route down an appropriate thingy
882 $self->route($field[1], $line);
887 if ($pcno == 73) { # WCY broadcasts
890 my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
891 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
892 dbg('chan', "WCY Date ($field[1] $field[2]) out of range");
895 @field = map { unpad($_) } @field;
896 if (WCY::dup($d,@field[3..7])) {
897 dbg('chan', "Dup WCY Spot ignored\n");
901 my $wcy = WCY::update($d, @field[2..12]);
905 $rep = Local::wwv($self, @field[1..12]);
907 # dbg('local', "Local::wcy error $@") if $@;
910 # broadcast to the eager world
911 send_wcy_spot($self, $line, $d, @field[2..12]);
915 if ($pcno == 84) { # remote commands (incoming)
916 if ($field[1] eq $main::mycall) {
917 my $ref = DXUser->get_current($field[2]);
918 my $cref = DXCluster->get($field[2]);
919 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[4]);
920 unless ($field[4] =~ /rcmd/i || !$cref || !$ref || $cref->mynode->call ne $ref->homenode) { # not allowed to relay RCMDS!
921 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
922 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
923 my $oldpriv = $self->{priv};
924 $self->{priv} = $ref->{priv}; # assume the user's privilege level
925 my @in = (DXCommandmode::run_cmd($self, $field[4]));
926 $self->{priv} = $oldpriv;
929 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:$_"));
930 Log('rcmd', 'out', $field[2], $_);
932 delete $self->{remotecmd};
934 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:sorry...!"));
937 $self->send(pc85($main::mycall, $field[2], $field[3],"$main::mycall:your attempt is logged, Tut tut tut...!"));
940 my $ref = DXUser->get_current($field[1]);
941 if ($ref && $ref->is_clx) {
942 $self->route($field[1], $line);
944 route($field[1], pc34($field[2], $field[1], $field[4]));
950 if ($pcno == 85) { # remote command replies
951 if ($field[1] eq $main::mycall) {
952 my $dxchan = DXChannel->get($field[3]);
954 $dxchan->send($field[4]);
956 my $s = $rcmds{$field[2]};
958 $dxchan = DXChannel->get($s->{call});
959 $dxchan->send($field[4]) if $dxchan;
960 delete $rcmds{$field[2]} if !$dxchan;
962 # send unsolicited ones to the sysop
963 my $dxchan = DXChannel->get($main::myalias);
964 $dxchan->send($field[4]) if $dxchan;
968 my $ref = DXUser->get_current($field[1]);
969 if ($ref && $ref->is_clx) {
970 $self->route($field[1], $line);
972 route($field[1], pc35($field[2], $field[1], $field[4]));
979 # if get here then rebroadcast the thing with its Hop count decremented (if
980 # there is one). If it has a hop count and it decrements to zero then don't
983 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
987 unless ($self->{isolate}) {
988 broadcast_ak1a($line, $self); # send it to everyone but me
993 # This is called from inside the main cluster processing loop and is used
994 # for despatching commands that are doing some long processing job
999 my @dxchan = DXChannel->get_all();
1002 foreach $dxchan (@dxchan) {
1003 next unless $dxchan->is_node();
1004 next if $dxchan == $me;
1006 # send a pc50 out on this channel
1007 if ($t >= $dxchan->pc50_t + $DXProt::pc50_interval) {
1008 $dxchan->send(pc50(scalar DXChannel::get_all_users));
1009 $dxchan->pc50_t($t);
1012 # send a ping out on this channel
1013 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
1014 if ($dxchan->{nopings} <= 0) {
1015 $dxchan->disconnect;
1017 addping($main::mycall, $dxchan->call);
1018 $dxchan->{nopings} -= 1;
1019 $dxchan->{lastping} = $t;
1027 if ($main::systime - 3600 > $last_hour) {
1031 $last_hour = $main::systime;
1036 # finish up a pc context
1041 my $call = $self->call;
1043 my $ref = DXCluster->get_exact($call);
1045 # unbusy and stop and outgoing mail
1046 my $mref = DXMsg::get_busy($call);
1047 $mref->stop_msg($call) if $mref;
1049 # broadcast to all other nodes that all the nodes connected to via me are gone
1050 my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
1053 foreach $node (@gonenodes) {
1054 next if $node->call eq $call;
1055 broadcast_ak1a(pc21($node->call, 'Gone') , $self) unless $self->{isolate};
1059 # remove outstanding pings
1060 delete $pings{$call};
1062 # now broadcast to all other ak1a nodes that I have gone
1063 broadcast_ak1a(pc21($call, 'Gone.'), $self) unless $self->{isolate};
1065 # I was the last node visited
1066 $self->user->node($main::mycall);
1068 # send info to all logged in thingies
1069 $self->tell_login('logoutn');
1071 Log('DXProt', $call . " Disconnected");
1072 $ref->del() if $ref;
1076 # some active measures
1082 my @dxchan = DXChannel->get_all();
1085 # send it if it isn't the except list and isn't isolated and still has a hop count
1086 # taking into account filtering and so on
1087 foreach $dxchan (@dxchan) {
1089 my ($filter, $hops);
1091 if ($dxchan->{spotfilter}) {
1092 ($filter, $hops) = Filter::it($dxchan->{spotfilter}, @_, $self->{call} );
1093 next unless $filter;
1096 if ($dxchan->is_node) {
1097 next if $dxchan == $self;
1100 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1102 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1103 next unless $routeit;
1106 $dxchan->send($routeit) if $routeit;
1108 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1110 } elsif ($dxchan->is_user && $dxchan->{dx}) {
1111 my $buf = Spot::formatb($dxchan->{user}->wantgrid, $_[0], $_[1], $_[2], $_[3], $_[4]);
1112 $buf .= "\a\a" if $dxchan->{beep};
1113 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1114 $dxchan->send($buf);
1116 $dxchan->delay($buf);
1126 my @dxchan = DXChannel->get_all();
1129 # send it if it isn't the except list and isn't isolated and still has a hop count
1130 # taking into account filtering and so on
1131 foreach $dxchan (@dxchan) {
1133 my ($filter, $hops);
1135 if ($dxchan->{wwvfilter}) {
1136 ($filter, $hops) = Filter::it($dxchan->{wwvfilter}, @_, $self->{call} );
1137 next unless $filter;
1139 if ($dxchan->is_node) {
1140 next if $dxchan == $self;
1143 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1145 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1146 next unless $routeit;
1149 $dxchan->send($routeit) if $routeit;
1151 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1154 } elsif ($dxchan->is_user && $dxchan->{wwv}) {
1155 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
1156 $buf .= "\a\a" if $dxchan->{beep};
1157 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1158 $dxchan->send($buf);
1160 $dxchan->delay($buf);
1170 my @dxchan = DXChannel->get_all();
1173 # send it if it isn't the except list and isn't isolated and still has a hop count
1174 # taking into account filtering and so on
1175 foreach $dxchan (@dxchan) {
1177 my ($filter, $hops);
1179 if ($dxchan->{wcyfilter}) {
1180 ($filter, $hops) = Filter::it($dxchan->{wcyfilter}, @_, $self->{call} );
1181 next unless $filter;
1183 if ($dxchan->is_clx || $dxchan->is_spider) {
1184 next if $dxchan == $self;
1187 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1189 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1190 next unless $routeit;
1193 $dxchan->send($routeit) if $routeit;
1195 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1197 } elsif ($dxchan->is_user && $dxchan->{wcy}) {
1198 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1199 $buf .= "\a\a" if $dxchan->{beep};
1200 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1201 $dxchan->send($buf);
1203 $dxchan->delay($buf);
1214 my @dxchan = DXChannel->get_all();
1216 my $text = unpad($_[2]);
1220 if ($_[3] eq '*') { # sysops
1222 } elsif ($_[3] gt ' ') { # speciality list handling
1223 my ($name) = split /\./, $_[3];
1224 $target = "$name"; # put the rest in later (if bothered)
1231 $target = "All" if !$target;
1233 Log('ann', $target, $_[0], $text);
1235 # send it if it isn't the except list and isn't isolated and still has a hop count
1236 # taking into account filtering and so on
1237 foreach $dxchan (@dxchan) {
1239 my ($filter, $hops);
1241 if ($dxchan->{annfilter}) {
1242 ($filter, $hops) = Filter::it($dxchan->{annfilter}, @_, $self->{call} );
1243 next unless $filter;
1245 if ($dxchan->is_node && $_[1] ne $main::mycall) { # i.e not specifically routed to me
1246 next if $dxchan == $self;
1249 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1251 $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1252 next unless $routeit;
1255 $dxchan->send($routeit) if $routeit;
1257 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1260 } elsif ($dxchan->is_user && $dxchan->{ann}) {
1261 next if $target eq 'SYSOP' && $dxchan->{priv} < 5;
1262 my $buf = "$to$target de $_[0]: $text";
1263 $buf .= "\a\a" if $dxchan->{beep};
1264 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1265 $dxchan->send($buf);
1267 $dxchan->delay($buf);
1273 sub send_local_config
1282 if ($self->{isolate}) {
1283 @localnodes = (DXCluster->get_exact($main::mycall));
1285 # create a list of all the nodes that are not connected to this connection
1286 # and are not themselves isolated, this to make sure that isolated nodes
1287 # don't appear outside of this node
1288 @nodes = DXNode::get_all();
1289 @nodes = grep { $_->{call} ne $main::mycall } @nodes;
1290 @nodes = grep { $_->dxchan != $self } @nodes if @nodes;
1291 @nodes = grep { !$_->dxchan->{isolate} } @nodes if @nodes;
1292 @localnodes = grep { $_->dxchan->{call} eq $_->{call} } @nodes if @nodes;
1293 unshift @localnodes, DXCluster->get_exact($main::mycall);
1294 @remotenodes = grep { $_->dxchan->{call} ne $_->{call} } @nodes if @nodes;
1297 my @s = $me->pc19(@localnodes, @remotenodes);
1299 my $routeit = adjust_hops($self, $_);
1300 $self->send($routeit) if $routeit;
1303 # get all the users connected on the above nodes and send them out
1304 foreach $n (@localnodes, @remotenodes) {
1305 my @users = values %{$n->list};
1306 my @s = pc16($n, @users);
1308 my $routeit = adjust_hops($self, $_);
1309 $self->send($routeit) if $routeit;
1315 # route a message down an appropriate interface for a callsign
1317 # is called route(to, pcline);
1321 my ($self, $call, $line) = @_;
1322 my $cl = DXCluster->get_exact($call);
1323 if ($cl) { # don't route it back down itself
1324 if (ref $self && $call eq $self->{call}) {
1325 dbg('chan', "Trying to route back to source, dropped");
1329 my $dxchan = $cl->{dxchan};
1331 my $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1333 $dxchan->send($routeit) if $dxchan;
1339 # broadcast a message to all clusters taking into account isolation
1340 # [except those mentioned after buffer]
1343 my $s = shift; # the line to be rebroadcast
1344 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1345 my @dxchan = DXChannel::get_all_ak1a();
1348 # send it if it isn't the except list and isn't isolated and still has a hop count
1349 foreach $dxchan (@dxchan) {
1350 next if grep $dxchan == $_, @except;
1351 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
1352 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
1356 # broadcast a message to all clusters ignoring isolation
1357 # [except those mentioned after buffer]
1358 sub broadcast_all_ak1a
1360 my $s = shift; # the line to be rebroadcast
1361 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1362 my @dxchan = DXChannel::get_all_ak1a();
1365 # send it if it isn't the except list and isn't isolated and still has a hop count
1366 foreach $dxchan (@dxchan) {
1367 next if grep $dxchan == $_, @except;
1368 my $routeit = adjust_hops($dxchan, $s); # adjust its hop count by node name
1369 $dxchan->send($routeit);
1373 # broadcast to all users
1374 # storing the spot or whatever until it is in a state to receive it
1377 my $s = shift; # the line to be rebroadcast
1378 my $sort = shift; # the type of transmission
1379 my $fref = shift; # a reference to an object to filter on
1380 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
1381 my @dxchan = DXChannel::get_all_users();
1385 foreach $dxchan (@dxchan) {
1386 next if grep $dxchan == $_, @except;
1389 broadcast_list($s, $sort, $fref, @out);
1392 # broadcast to a list of users
1400 foreach $dxchan (@_) {
1403 if ($sort eq 'dx') {
1404 next unless $dxchan->{dx};
1405 ($filter) = Filter::it($dxchan->{spotfilter}, @{$fref}) if ref $fref;
1406 next unless $filter;
1408 next if $sort eq 'ann' && !$dxchan->{ann};
1409 next if $sort eq 'wwv' && !$dxchan->{wwv};
1410 next if $sort eq 'wcy' && !$dxchan->{wcy};
1411 next if $sort eq 'wx' && !$dxchan->{wx};
1413 $s =~ s/\a//og unless $dxchan->{beep};
1415 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1425 # obtain the hops from the list for this callsign and pc no
1431 my $hops = $DXProt::hopcount{$pcno};
1432 $hops = $DXProt::def_hopcount if !$hops;
1437 # adjust the hop count on a per node basis using the user loadable
1438 # hop table if available or else decrement an existing one
1445 my $call = $self->{call};
1448 if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
1449 my ($pcno) = $s =~ /^PC(\d\d)/o;
1450 confess "$call called adjust_hops with '$s'" unless $pcno;
1451 my $ref = $nodehops{$call} if %nodehops;
1453 my $newhops = $ref->{$pcno};
1454 return "" if defined $newhops && $newhops == 0;
1455 $newhops = $ref->{default} unless $newhops;
1456 return "" if defined $newhops && $newhops == 0;
1457 $newhops = $hops if !$newhops;
1458 $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
1460 # simply decrement it
1462 return "" if !$hops;
1463 $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
1475 return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1476 do "$main::data/hop_table.pl";
1482 # add a ping request to the ping queues
1485 my ($from, $to) = @_;
1486 my $ref = $pings{$to} || [];
1489 $r->{t} = [ gettimeofday ];
1490 route(undef, $to, pc51($to, $main::mycall, 1));
1495 # add a rcmd request to the rcmd queues
1498 my ($self, $to, $cmd) = @_;
1501 $r->{call} = $self->{call};
1502 $r->{t} = $main::systime;
1506 my $ref = DXCluster->get_exact($to);
1507 if ($ref && $ref->dxchan && $ref->dxchan->is_clx) {
1508 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1510 route(undef, $to, pc34($main::mycall, $to, $cmd));
1519 if ($self->{conn} && !$nopc39) {
1520 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1523 $self->SUPER::disconnect;
1528 # send a talk message to this thingy
1532 my ($self, $from, $to, $via, $line) = @_;
1534 $line =~ s/\^/\\5E/g; # remove any ^ characters
1535 $self->send(DXProt::pc10($from, $to, $via, $line));
1536 Log('talk', $self->call, $from, $via?$via:$main::mycall, $line);