3 # This module impliments the protocal mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
30 use Time::HiRes qw(gettimeofday tv_interval);
40 use vars qw($VERSION $BRANCH);
41 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
42 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
43 $main::build += $VERSION;
44 $main::branch += $BRANCH;
46 use vars qw($pc11_max_age $pc23_max_age $last_pc50 $eph_restime $eph_info_restime $eph_pc34_restime
47 $last_hour $last10 %eph %pings %rcmds $ann_to_talk $pc19_version
48 $pingint $obscount %pc19list $chatdupeage $investigation_int
49 %nodehops $baddx $badspotter $badnode $censorpc $rspfcheck
50 $allowzero $decode_dk0wcy $send_opernam @checklist);
52 $pc11_max_age = 1*3600; # the maximum age for an incoming 'real-time' pc11
53 $pc23_max_age = 1*3600; # the maximum age for an incoming 'real-time' pc23
55 $last_hour = time; # last time I did an hourly periodic update
56 %pings = (); # outstanding ping requests outbound
57 %rcmds = (); # outstanding rcmd requests outbound
58 %nodehops = (); # node specific hop control
59 %pc19list = (); # list of outstanding PC19s that haven't had PC16s on them
61 $censorpc = 1; # Do a BadWords::check on text fields and reject things
62 # loads of 'bad things'
63 $baddx = new DXHash "baddx";
64 $badspotter = new DXHash "badspotter";
65 $badnode = new DXHash "badnode";
66 $last10 = $last_pc50 = time;
70 $eph_info_restime = 60*60;
71 $eph_pc34_restime = 30;
74 $chatdupeage = 20 * 60 * 60;
75 $investigation_int = 7*86400; # time between checks to see if we can see this node
76 $pc19_version = 5466; # the visible version no for outgoing PC19s generated from pc59
81 [ qw(i c c m bp bc c) ], # pc10
82 [ qw(i f m d t m c c h) ], # pc11
83 [ qw(i c bm m bm bm p h) ], # pc12
87 undef , # pc16 has to be validated manually
88 [ qw(i c c h) ], # pc17
90 undef , # pc19 has to be validated manually
91 undef , # pc20 no validation
92 [ qw(i c m h) ], # pc21
93 undef , # pc22 no validation
94 [ qw(i d n n n n m c c h) ], # pc23
95 [ qw(i c p h) ], # pc24
96 [ qw(i c c n n) ], # pc25
97 [ qw(i f m d t m c c bc) ], # pc26
98 [ qw(i d n n n n m c c bc) ], # pc27
99 [ qw(i c c m c d t p m bp n p bp bc) ], # pc28
100 [ qw(i c c n m) ], # pc29
101 [ qw(i c c n) ], # pc30
102 [ qw(i c c n) ], # pc31
103 [ qw(i c c n) ], # pc32
104 [ qw(i c c n) ], # pc33
105 [ qw(i c c m) ], # pc34
106 [ qw(i c c m) ], # pc35
107 [ qw(i c c m) ], # pc36
108 [ qw(i c c n m) ], # pc37
109 undef, # pc38 not interested
110 [ qw(i c m) ], # pc39
111 [ qw(i c c m p n) ], # pc40
112 [ qw(i c n m h) ], # pc41
113 [ qw(i c c n) ], # pc42
114 undef, # pc43 don't handle it
115 [ qw(i c c n m m c) ], # pc44
116 [ qw(i c c n m) ], # pc45
117 [ qw(i c c n) ], # pc46
120 [ qw(i c m h) ], # pc49
121 [ qw(i c n h) ], # pc50
122 [ qw(i c c n) ], # pc51
144 [ qw(i d n n n n n n m m m c c h) ], # pc73
155 [ qw(i c c c m) ], # pc84
156 [ qw(i c c c m) ], # pc85
161 [ qw(i c n) ], # pc90
164 # use the entry in the check list to check the field list presented
165 # return OK if line NOT in check list (for now)
170 return 0 if $n < 0 || $n > @checklist;
171 my $ref = $checklist[$n];
172 return 0 unless ref $ref;
175 for ($i = 1; $i < @$ref; $i++) {
176 my ($blank, $act) = $$ref[$i] =~ /^(b?)(\w)$/;
177 return 0 unless $act;
178 next if $blank && $_[$i] =~ /^[ \*]$/;
180 return $i unless is_callsign($_[$i]);
181 } elsif ($act eq 'i') {
183 } elsif ($act eq 'm') {
184 return $i unless is_pctext($_[$i]);
185 } elsif ($act eq 'p') {
186 return $i unless is_pcflag($_[$i]);
187 } elsif ($act eq 'f') {
188 return $i unless is_freq($_[$i]);
189 } elsif ($act eq 'n') {
190 return $i unless $_[$i] =~ /^[\d ]+$/;
191 } elsif ($act eq 'h') {
192 return $i unless $_[$i] =~ /^H\d\d?$/;
193 } elsif ($act eq 'd') {
194 return $i unless $_[$i] =~ /^\s*\d+-\w\w\w-[12][90]\d\d$/;
195 } elsif ($act eq 't') {
196 return $i unless $_[$i] =~ /^[012]\d[012345]\dZ$/;
204 do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
207 my $user = DXUser->get($main::mycall);
208 $DXProt::myprot_version += $main::version*100;
209 $main::me = DXProt->SUPER::alloc($main::mycall, 0, $user);
210 $main::me->{here} = 1;
211 $main::me->{state} = "indifferent";
212 $main::me->{sort} = 'S'; # S for spider
213 $main::me->{priv} = 9;
214 $main::me->{metric} = 0;
215 $main::me->{pingave} = 0;
216 $main::me->{registered} = 1;
217 $main::me->{version} = $main::version;
218 $main::me->{build} = $main::build;
222 # obtain a new connection this is derived from dxchannel
227 my $self = DXChannel::alloc(@_);
229 # add this node to the table, the values get filled in later
233 my $uref = Route::Node::get($call) || Route::Node->new($call);
236 $uref->version($pc19_version);
237 $main::routeroot->link_node($uref, $self);
241 # this is how a pc connection starts (for an incoming connection)
242 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
243 # all the crap that comes between).
246 my ($self, $line, $sort) = @_;
247 my $call = $self->{call};
248 my $user = $self->{user};
251 my $host = $self->{conn}->{peerhost} || "unknown";
252 Log('DXProt', "$call connected from $host");
254 # remember type of connection
255 $self->{consort} = $line;
256 $self->{outbound} = $sort eq 'O';
257 my $priv = $user->priv;
258 $priv = $user->priv(1) unless $priv;
259 $self->{priv} = $priv; # other clusters can always be 'normal' users
260 $self->{lang} = $user->lang || 'en';
261 $self->{isolate} = $user->{isolate};
262 $self->{consort} = $line; # save the connection type
266 # sort out registration
267 $self->{registered} = 1;
269 # get the output filters
270 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
271 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
272 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
273 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
274 $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) unless $self->{isolate} ;
277 # get the INPUT filters (these only pertain to Clusters)
278 $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
279 $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
280 $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
281 $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
282 $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1) unless $self->{isolate};
284 # set unbuffered and no echo
285 $self->send_now('B',"0");
286 $self->send_now('E',"0");
287 $self->conn->echo(0) if $self->conn->can('echo');
289 # ping neighbour node stuff
290 my $ping = $user->pingint;
291 $ping = $pingint unless defined $ping;
292 $self->{pingint} = $ping;
293 $self->{nopings} = $user->nopings || $obscount;
294 $self->{pingtime} = [ ];
295 $self->{pingave} = 999;
296 $self->{metric} ||= 100;
297 $self->{lastping} = $main::systime;
299 # send initialisation string
300 unless ($self->{outbound}) {
304 $self->state('init');
305 $self->{pc50_t} = $main::systime;
307 # send info to all logged in thingies
308 $self->tell_login('loginn');
310 # run a script send the output to the debug file
311 my $script = new Script(lc $call) || new Script('node_default');
312 $script->run($self) if $script;
316 # send outgoing 'challenge'
327 $_[0] =~ s/^PC90\^[-A-Z0-9]+\^\d+\^//;
328 $_[0] =~ s/^PC91\^[-A-Z0-9]+\^\d+\^[-A-Z0-9]+\^//;
336 # $self->SUPER::send($line);
341 # This is the normal pcxx despatcher
345 my ($self, $line) = @_;
347 # remove any incoming PC90 frames
350 my @field = split /\^/, $line;
351 return unless @field;
353 pop @field if $field[-1] eq '~';
355 # print join(',', @field), "\n";
358 # process PC frames, this will fail unless the frame starts PCnn
359 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
360 unless (defined $pcno && $pcno >= 10 && $pcno <= 99) {
361 dbg("PCPROT: unknown protocol") if isdbg('chanerr');
365 # check for and dump bad protocol messages
366 my $n = check($pcno, @field);
368 dbg("PCPROT: bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")") if isdbg('chanerr');
372 my $origin = $self->{call};
374 my $sub = "handle_$pcno";
376 if ($self->can($sub)) {
377 $self->$sub($pcno, $line, $origin, @field);
379 $self->handle_default($pcno, $line, $origin, @field);
383 # incoming talk commands
392 return if $rspfcheck and !$self->rspfcheck(0, $_[6], $_[1]);
394 # will we allow it at all?
397 if (@bad = BadWords::check($_[3])) {
398 dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
403 # is it for me or one of mine?
404 my ($from, $to, $via, $call, $dxchan);
413 # if this is a 'nodx' node then ignore it
414 if ($badnode->in($_[6]) || ($via && $badnode->in($via))) {
415 dbg("PCPROT: Bad Node, dropped") if isdbg('chanerr');
419 # if this is a 'bad spotter' user then ignore it
421 $nossid =~ s/-\d+$//;
422 if ($badspotter->in($nossid)) {
423 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
427 # if we are converting announces to talk is it a dup?
429 if (AnnTalk::is_talk_candidate($from, $_[3]) && AnnTalk::dup($from, $to, $_[3])) {
430 dbg("DXPROT: Dupe talk from announce, dropped") if isdbg('chanerr');
435 # it is here and logged on
436 $dxchan = DXChannel->get($main::myalias) if $to eq $main::mycall;
437 $dxchan = DXChannel->get($to) unless $dxchan;
438 if ($dxchan && $dxchan->is_user) {
440 $dxchan->talk($from, $to, $via, $_[3]);
444 # is it elsewhere, visible on the cluster via the to address?
445 # note: this discards the via unless the to address is on
448 if ($ref = Route::get($to)) {
449 $vref = Route::Node::get($via) if $via;
450 $vref = undef unless $vref && grep $to eq $_, $vref->users;
451 $ref->bestdxchan->talk($from, $to, $vref ? $via : undef, $_[3], $_[6]);
455 # not visible here, send a message of condolence
457 $ref = Route::get($from);
458 $vref = $ref = Route::Node::get($_[6]) unless $ref;
460 $dxchan = $ref->bestdxchan;
461 $dxchan->talk($main::mycall, $from, $vref ? $vref->call : undef, $dxchan->msg('talknh', $to) );
473 # route 'foreign' pc26s
475 if ($_[7] ne $main::mycall) {
476 $self->route($_[7], $line);
482 # return if $rspfcheck and !$self->rspfcheck(1, $_[7], $_[6]);
484 # if this is a 'nodx' node then ignore it
485 if ($badnode->in($_[7])) {
486 dbg("PCPROT: Bad Node, dropped") if isdbg('chanerr');
490 # if this is a 'bad spotter' user then ignore it
492 $nossid =~ s/-\d+$//;
493 if ($badspotter->in($nossid)) {
494 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
498 # convert the date to a unix date
499 my $d = cltounix($_[3], $_[4]);
500 # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
501 if (!$d || ($pcno == 11 && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
502 dbg("PCPROT: Spot ignored, invalid date or out of range ($_[3] $_[4])\n") if isdbg('chanerr');
507 if ($baddx->in($_[2]) || BadWords::check($_[2]) || $_[2] =~ /COCK/) {
508 dbg("PCPROT: Bad DX spot, ignored") if isdbg('chanerr');
513 $_[5] =~ s/^\s+//; # take any leading blanks off
514 $_[2] = unpad($_[2]); # take off leading and trailing blanks from spotted callsign
515 if ($_[2] =~ /BUST\w*$/) {
516 dbg("PCPROT: useless 'BUSTED' spot") if isdbg('chanerr');
521 if (@bad = BadWords::check($_[5])) {
522 dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
528 my @spot = Spot::prepare($_[1], $_[2], $d, $_[5], $_[6], $_[7]);
529 # global spot filtering on INPUT
530 if ($self->{inspotsfilter}) {
531 my ($filter, $hops) = $self->{inspotsfilter}->it(@spot);
533 dbg("PCPROT: Rejected by input spot filter") if isdbg('chanerr');
538 # this goes after the input filtering, but before the add
539 # so that if it is input filtered, it isn't added to the dup
540 # list. This allows it to come in from a "legitimate" source
541 if (Spot::dup($_[1], $_[2], $d, $_[5], $_[6])) {
542 dbg("PCPROT: Duplicate Spot ignored\n") if isdbg('chanerr');
550 # @spot at this point contains:-
551 # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
552 # then spotted itu, spotted cq, spotters itu, spotters cq
553 # you should be able to route on any of these
556 # fix up qra locators of known users
557 my $user = DXUser->get_current($spot[4]);
559 my $qra = $user->qra;
560 unless ($qra && is_qra($qra)) {
561 my $lat = $user->lat;
562 my $long = $user->long;
563 if (defined $lat && defined $long) {
564 $user->qra(DXBearing::lltoqra($lat, $long));
569 # send a remote command to a distant cluster if it is visible and there is no
570 # qra locator and we havn't done it for a month.
572 unless ($user->qra) {
574 my $to = $user->homenode;
575 my $last = $user->lastoper || 0;
576 if ($send_opernam && $to && $to ne $main::mycall && $main::systime > $last + $DXUser::lastoperinterval && ($node = Route::Node::get($to)) ) {
577 my $cmd = "forward/opernam $spot[4]";
578 # send the rcmd but we aren't interested in the replies...
579 my $dxchan = $node->bestdxchan;
580 if ($dxchan && $dxchan->is_clx) {
581 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
583 route(undef, $to, pc34($main::mycall, $to, $cmd));
587 $node = Route::Node::get($to);
589 $dxchan = $node->bestdxchan;
590 if ($dxchan && $dxchan->is_clx) {
591 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
593 route(undef, $to, pc34($main::mycall, $to, $cmd));
597 $user->lastoper($main::systime);
606 $r = Local::spot($self, @spot);
608 # dbg("Local::spot1 error $@") if isdbg('local') if $@;
611 # DON'T be silly and send on PC26s!
612 return if $pcno == 26;
614 # send out the filtered spots
615 send_dx_spot($self, $line, @spot) if @spot;
626 # return if $rspfcheck and !$self->rspfcheck(1, $_[5], $_[1]);
628 # announce duplicate checking
629 $_[3] =~ s/^\s+//; # remove leading blanks
633 if (@bad = BadWords::check($_[3])) {
634 dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
639 # if this is a 'nodx' node then ignore it
640 if ($badnode->in($_[5])) {
641 dbg("PCPROT: Bad Node, dropped") if isdbg('chanerr');
645 # if this is a 'bad spotter' user then ignore it
647 $nossid =~ s/-\d+$//;
648 if ($badspotter->in($nossid)) {
649 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
655 if ((($dxchan = DXChannel->get($_[2])) && $dxchan->is_user) || $_[4] =~ /^[\#\w.]+$/){
656 $self->send_chat($line, @_[1..6]);
657 } elsif ($_[2] eq '*' || $_[2] eq $main::mycall) {
659 # ignore something that looks like a chat line coming in with sysop
660 # flag - this is a kludge...
661 if ($_[3] =~ /^\#\d+ / && $_[4] eq '*') {
662 dbg('PCPROT: Probable chat rewrite, dropped') if isdbg('chanerr');
666 # here's a bit of fun, convert incoming ann with a callsign in the first word
667 # or one saying 'to <call>' to a talk if we can route to the recipient
669 my $call = AnnTalk::is_talk_candidate($_[1], $_[3]);
671 my $ref = Route::get($call);
673 $dxchan = $ref->bestdxchan;
674 $dxchan->talk($_[1], $call, undef, $_[3], $_[5]) if $dxchan != $self;
681 $self->send_announce($line, @_[1..6]);
683 $self->route($_[2], $line);
699 my $newline = "PC16^";
701 # do I want users from this channel?
702 unless ($self->user->wantpc16) {
703 dbg("PCPROT: don't send users to $self->{call}") if isdbg('chanerr');
708 if ($ncall eq $main::mycall) {
709 dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
713 # do we believe this call?
714 unless ($ncall eq $self->{call} || $self->is_believed($ncall)) {
715 if (my $ivp = Investigate::get($ncall, $self->{call})) {
716 $ivp->store_pcxx($pcno,$line,$origin,@_);
718 dbg("PCPROT: We don't believe $ncall on $self->{call}");
722 my $node = Route::Node::get($ncall);
724 dbg("PCPROT: Node $ncall not in config") if isdbg('chanerr');
728 # dedupe only that which we potentially process
729 if (eph_dup($line)) {
730 dbg("PCPROT: dup PC16 detected") if isdbg('chanerr');
737 for ($i = 2; $i < $#_; $i++) {
738 my ($call, $conf, $here) = $_[$i] =~ /^(\S+) (\S) (\d)/o;
739 next unless $call && $conf && defined $here && is_callsign($call);
740 next if $call eq $main::mycall;
742 eph_del_regex("^PC17\\^$call\\^$ncall");
744 $conf = $conf eq '*';
746 # reject this if we think it is a node already
747 my $r = Route::Node::get($call);
748 my $u = DXUser->get_current($call) unless $r;
749 if ($r || ($u && $u->is_node)) {
750 dbg("PCPROT: $call is a node") if isdbg('chanerr');
754 $r = Route::User::get($call) || Route::User->new($call);
757 $node->lastseen($main::systime);
759 push @new, $node->add_user($r);
761 # add this station to the user database, if required
762 $call =~ s/-\d+$//o; # remove ssid for users
763 my $user = DXUser->get_current($call);
764 $user = DXUser->new($call) if !$user;
765 $user->homenode($node->call) if !$user->homenode;
766 $user->node($node->call);
767 $user->lastin($main::systime) unless DXChannel->get($call);
770 $self->route_pc16($origin, $line, $node, @new) if @new;
784 eph_del_regex("^PC16\\^$ncall.*$ucall");
786 # do I want users from this channel?
787 unless ($self->user->wantpc16) {
788 dbg("PCPROT: don't send users to $self->{call}") if isdbg('chanerr');
791 if ($ncall eq $main::mycall) {
792 dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
796 # do we believe this call?
797 unless ($ncall eq $self->{call} || $self->is_believed($ncall)) {
798 if (my $ivp = Investigate::get($ncall, $self->{call})) {
799 $ivp->store_pcxx($pcno,$line,$origin,@_);
801 dbg("PCPROT: We don't believe $ncall on $self->{call}");
805 my $uref = Route::User::get($ucall);
807 dbg("PCPROT: Route::User $ucall not in config") if isdbg('chanerr');
809 my $node = Route::Node::get($ncall);
811 dbg("PCPROT: Route::Node $ncall not in config") if isdbg('chanerr');
814 return unless $node && $uref;
818 if ($self->in_filter_route($node)) {
820 if (eph_dup($line)) {
821 dbg("PCPROT: dup PC17 detected") if isdbg('chanerr');
824 push @new, $node->del_user($uref);
827 $self->route_pc17($origin, $line, $node, $uref) if @new;
829 # get rid of orphaned users;
840 $self->state('init');
842 # record the type and version offered
843 if ($_[1] =~ /DXSpider Version: (\d+\.\d+) Build: (\d+\.\d+)/) {
844 $self->version(53 + $1);
845 $self->user->version(53 + $1);
846 $self->build(0 + $2);
847 $self->user->build(0 + $2);
848 unless ($self->is_spider) {
849 $self->user->sort('S');
854 $self->version(50.0);
855 $self->version($_[2] / 100) if $_[2] && $_[2] =~ /^\d+$/;
856 $self->user->version($self->version);
858 $self->newroute( $_[1] =~ /NewRoute/ );
860 # first clear out any nodes on this dxchannel
861 my $node = Route::Node::get($self->{call}) ;
863 foreach my $n ($node->nodes) {
864 next if $n eq $main::mycall;
865 next if $n eq $self->{call};
866 my $nref = Route::Node::get($n);
867 push @rout, $node->remove_route($nref, $self) if $nref;
869 $self->route_pc21($origin, $line, @rout) if @rout;
874 # send the new config
875 $self->send_local_config();
879 # incoming cluster list
888 my $newline = "PC19^";
890 if (eph_dup($line)) {
891 dbg("PCPROT: dup PC19 detected") if isdbg('chanerr');
898 # first get the INTERFACE node
899 my $parent = Route::Node::get($self->{call});
901 dbg("DXPROT: my parent $self->{call} has disappeared");
909 for ($i = 1; $i < $#_-1; $i += 4) {
911 my $call = uc $_[$i+1];
914 next unless defined $here && defined $conf && is_callsign($call);
916 # check for sane parameters
917 # $ver = 5000 if $ver eq '0000';
918 next if $ver < 5000; # only works with version 5 software
919 next if length $call < 3; # min 3 letter callsigns
920 next if $call eq $main::mycall;
922 # add this station to the user database, if required (don't remove SSID from nodes)
923 my $user = DXUser->get_current($call);
925 $user = DXUser->new($call);
927 $user->priv(1); # I have relented and defaulted nodes
929 $user->homenode($call);
932 $user->wantroutepc19(1) unless defined $user->wantroutepc19;
933 $user->lastin($main::systime) unless DXChannel->get($call);
936 # do we believe this call?
937 unless ($call eq $self->{call} || $self->is_believed($call)) {
938 my $pt = $user->lastping || 0;
939 if ($pt+$investigation_int < $main::systime && !Investigate::get($call, $self->{call})) {
940 my $ivp = Investigate->new($call, $self->{call});
943 $ivp->store_pcxx($pcno,$line,$origin,'PC19',$here,$call,$conf,$ver,$_[-1]);
945 dbg("PCPROT: We don't believe $call on $self->{call}");
949 eph_del_regex("^PC(?:21\\^$call|17\\^[^\\^]+\\^$call)");
951 my $r = Route::Node::get($call) || Route::Node->new($call);
955 $r->lastseen($main::systime);
957 if ($self->in_filter_route($r)) {
958 push @new, $parent->link_node($r, $self);
962 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
963 my $mref = DXMsg::get_busy($call);
964 $mref->stop_msg($call) if $mref;
967 # route out new nodes to legacy nodes
968 $self->route_pc19($origin, $line, @new) if @new;
969 $self->route_pc59('A', 0, $self->{call}, @rout) if @rout;
972 # send local configuration
979 $self->send_local_config();
981 $self->state('normal');
982 $self->{lastping} = 0;
985 # delete a cluster from the list
994 return if $call eq $main::mycall; # don't allow malicious buggers to disconnect me (or ignore loops)!
996 unless ($call eq $self->{call} || $self->is_believed($call)) {
997 if (my $ivp = Investigate::get($call, $self->{call})) {
998 $ivp->store_pcxx($pcno,$line,$origin,@_);
1000 dbg("PCPROT: We don't believe $call on $self->{call}");
1004 eph_del_regex("^PC1[679].*$call");
1006 # if I get a PC21 from the same callsign as self then treat it
1007 # as a PC39: I have gone away
1008 if ($call eq $self->call) {
1009 $self->disconnect(1);
1015 my $parent = Route::Node::get($self->{call});
1017 dbg("DXPROT: my parent $self->{call} has disappeared");
1023 my $node = Route::Node::get($call);
1025 $node->lastseen($main::systime);
1028 return unless $self->in_filter_route($node);
1030 push @new, $parent->remove_route($node, $self);
1033 $self->route_pc21($origin, $line, @new) if @new;
1034 $self->route_pc59('D', hexstamp(), $self->{call}, @rout) if @rout;
1036 # get rid of orphaned nodes;
1037 $_->delete for @new;
1047 $self->state('normal');
1048 $self->{lastping} = 0;
1059 # route foreign' pc27s
1061 if ($_[8] ne $main::mycall) {
1062 $self->route($_[8], $line);
1067 # only do a rspf check on PC23 (not 27)
1069 return if $rspfcheck and !$self->rspfcheck(1, $_[8], $_[7])
1073 my $d = cltounix($_[1], sprintf("%02d18Z", $_[2]));
1074 my $sfi = unpad($_[3]);
1075 my $k = unpad($_[4]);
1076 my $i = unpad($_[5]);
1077 my ($r) = $_[6] =~ /R=(\d+)/;
1079 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $_[2] < 0 || $_[2] > 23) {
1080 dbg("PCPROT: WWV Date ($_[1] $_[2]) out of range") if isdbg('chanerr');
1083 if (Geomag::dup($d,$sfi,$k,$i,$_[6])) {
1084 dbg("PCPROT: Dup WWV Spot ignored\n") if isdbg('chanerr');
1087 $_[7] =~ s/-\d+$//o; # remove spotter's ssid
1089 my $wwv = Geomag::update($d, $_[2], $sfi, $k, $i, @_[6..8], $r);
1093 $rep = Local::wwv($self, $_[1], $_[2], $sfi, $k, $i, @_[6..8], $r);
1095 # dbg("Local::wwv2 error $@") if isdbg('local') if $@;
1098 # DON'T be silly and send on PC27s!
1099 return if $pcno == 27;
1101 # broadcast to the eager world
1102 send_wwv_spot($self, $line, $d, $_[2], $sfi, $k, $i, @_[6..8]);
1112 my $call = uc $_[1];
1114 $nref = Route::Node::get($call);
1115 $uref = Route::User::get($call);
1116 return unless $nref || $uref; # if we don't know where they are, it's pointless sending it on
1118 if (eph_dup($line)) {
1119 dbg("PCPROT: Dup PC24 ignored\n") if isdbg('chanerr');
1123 $nref->here($_[2]) if $nref;
1124 $uref->here($_[2]) if $uref;
1125 my $ref = $nref || $uref;
1126 return unless $self->in_filter_route($ref);
1128 $self->route_pc24($origin, $line, $ref, $_[3]);
1138 if ($_[1] ne $main::mycall) {
1139 $self->route($_[1], $line);
1142 if ($_[2] eq $main::mycall) {
1143 dbg("PCPROT: Trying to merge to myself, ignored") if isdbg('chanerr');
1147 Log('DXProt', "Merge request for $_[3] spots and $_[4] WWV from $_[2]");
1151 my @in = reverse Spot::search(1, undef, undef, 0, $_[3]);
1154 $self->send(pc26(@{$in}[0..4], $_[2]));
1160 my @in = reverse Geomag::search(0, $_[4], time, 1);
1163 $self->send(pc27(@{$in}[0..5], $_[2]));
1168 sub handle_26 {goto &handle_11}
1169 sub handle_27 {goto &handle_23}
1171 # mail/file handling
1178 if ($_[1] eq $main::mycall) {
1180 my $sub = "DXMsg::handle_$pcno";
1183 $self->route($_[1], $line) unless $self->is_clx;
1187 sub handle_29 {goto &handle_28}
1188 sub handle_30 {goto &handle_28}
1189 sub handle_31 {goto &handle_28}
1190 sub handle_32 {goto &handle_28}
1191 sub handle_33 {goto &handle_28}
1199 if (eph_dup($line, $eph_pc34_restime)) {
1200 dbg("PCPROT: dupe PC34, ignored") if isdbg('chanerr');
1202 $self->process_rcmd($_[1], $_[2], $_[2], $_[3]);
1206 # remote command replies
1213 eph_del_regex("^PC35\\^$_[2]\\^$_[1]\\^");
1214 $self->process_rcmd_reply($_[1], $_[2], $_[1], $_[3]);
1217 sub handle_36 {goto &handle_34}
1226 if ($_[1] eq $main::mycall) {
1228 my $sub = "DXDb::handle_$pcno";
1231 $self->route($_[1], $line) unless $self->is_clx;
1235 # node connected list from neighbour
1244 # incoming disconnect
1251 if ($_[1] eq $self->{call}) {
1252 $self->disconnect(1);
1254 dbg("PCPROT: came in on wrong channel") if isdbg('chanerr');
1258 sub handle_40 {goto &handle_28}
1270 $l =~ s/[\x00-\x20\x7f-\xff]+//g; # remove all funny characters and spaces for dup checking
1271 if (eph_dup($l, $eph_info_restime)) {
1272 dbg("PCPROT: dup PC41, ignored") if isdbg('chanerr');
1276 # input filter if required
1277 # my $ref = Route::get($call) || Route->new($call);
1278 # return unless $self->in_filter_route($ref);
1280 if ($_[3] eq $_[2] || $_[3] =~ /^\s*$/) {
1281 dbg('PCPROT: invalid value') if isdbg('chanerr');
1285 # add this station to the user database, if required
1286 my $user = DXUser->get_current($call);
1287 $user = DXUser->new($call) unless $user;
1291 } elsif ($_[2] == 2) {
1293 } elsif ($_[2] == 3) {
1294 if (is_latlong($_[3])) {
1295 my ($lat, $long) = DXBearing::stoll($_[3]);
1298 $user->qra(DXBearing::lltoqra($lat, $long));
1300 dbg('PCPROT: not a valid lat/long') if isdbg('chanerr');
1303 } elsif ($_[2] == 4) {
1304 $user->homenode($_[3]);
1305 } elsif ($_[2] == 5) {
1306 if (is_qra(uc $_[3])) {
1307 my ($lat, $long) = DXBearing::qratoll(uc $_[3]);
1310 $user->qra(uc $_[3]);
1312 dbg('PCPROT: not a valid QRA locator') if isdbg('chanerr');
1316 $user->lastoper($main::systime); # to cut down on excessive for/opers being generated
1319 unless ($self->{isolate}) {
1320 DXChannel::broadcast_nodes($line, $self); # send it to everyone but me
1323 # perhaps this IS what we want after all
1324 # $self->route_pc41($ref, $call, $_[2], $_[3], $_[4]);
1327 sub handle_42 {goto &handle_28}
1331 sub handle_44 {goto &handle_37}
1332 sub handle_45 {goto &handle_37}
1333 sub handle_46 {goto &handle_37}
1334 sub handle_47 {goto &handle_37}
1335 sub handle_48 {goto &handle_37}
1337 # message and database
1345 if (eph_dup($line)) {
1346 dbg("PCPROT: Dup PC49 ignored\n") if isdbg('chanerr');
1350 if ($_[1] eq $main::mycall) {
1351 DXMsg::handle_49($self, @_);
1353 $self->route($_[1], $line) unless $self->is_clx;
1357 # keep alive/user list
1366 my $node = Route::Node::get($call);
1368 return unless $node->call eq $self->{call};
1369 $node->usercount($_[2]);
1371 # input filter if required
1372 return unless $self->in_filter_route($node);
1374 $self->route_pc50($origin, $line, $node, $_[2], $_[3]) unless eph_dup($line);
1378 # incoming ping requests/answers
1391 if ($to eq $main::mycall) {
1393 $self->send(pc51($from, $to, '0'));
1395 # it's a reply, look in the ping list for this one
1396 my $ref = $pings{$from};
1398 my $tochan = DXChannel->get($from);
1400 my $r = shift @$ref;
1401 my $dxchan = DXChannel->get($r->{call});
1402 next unless $dxchan;
1403 my $t = tv_interval($r->{t}, [ gettimeofday ]);
1404 if ($dxchan->is_user) {
1405 my $s = sprintf "%.2f", $t;
1406 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
1407 $dxchan->send($dxchan->msg('pingi', $from, $s, $ave))
1408 } elsif ($dxchan->is_node) {
1410 my $nopings = $tochan->user->nopings || $obscount;
1411 push @{$tochan->{pingtime}}, $t;
1412 shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
1414 # cope with a missed ping, this means you must set the pingint large enough
1415 if ($t > $tochan->{pingint} && $t < 2 * $tochan->{pingint} ) {
1416 $t -= $tochan->{pingint};
1419 # calc smoothed RTT a la TCP
1420 if (@{$tochan->{pingtime}} == 1) {
1421 $tochan->{pingave} = $t;
1423 $tochan->{pingave} = $tochan->{pingave} + (($t - $tochan->{pingave}) / 6);
1425 my $rref = Route::Node::get($tochan->{call});
1426 $rref->pingtime($tochan->{pingave}) if $rref;
1427 $tochan->{nopings} = $nopings; # pump up the timer
1428 if (my $ivp = Investigate::get($from, $self->{call})) {
1431 } elsif (my $rref = Route::Node::get($r->{call})) {
1432 if (defined $rref->pingtime) {
1433 $rref->pingtime($rref->pingtime + (($t - $rref->pingtime) / 6));
1435 $rref->pingtime($t);
1437 if (my $ivp = Investigate::get($from, $self->{call})) {
1446 if (eph_dup($line)) {
1447 dbg("PCPROT: dup PC51 detected") if isdbg('chanerr');
1450 # route down an appropriate thingy
1451 $self->route($to, $line);
1455 # New style routing handler
1463 my ($sort, $hextime, $ncall) = @_[1,2,3];
1464 if ($ncall eq $main::mycall) {
1465 dbg("PCPROT: ignoring PC59 for me") if isdbg('chan');
1469 # do this once for filtering with a throwaway routing entry if a new node
1470 my $fnode = Route::Node::get($ncall) || Route::new($ncall);
1471 return unless $self->in_filter_route($fnode);
1473 return if eph_dup($line);
1475 # mark myself as NewRoute if I get a PC59
1476 $self->{newroute} = 1 if $ncall eq $self->{call};
1478 # now do it properly for actions
1479 my $node = Route::Node::get($ncall) || Route::Node->new($ncall);
1482 # find each of the entries (or create new ones)
1484 for my $ent (@_[4..$#_]) {
1485 next if $ent =~ /^H\d+$/;
1487 my ($esort, $ehere, $ecall) = unpack "A A A*", $ent;
1490 next unless $esort && defined $ehere && $ecall;
1492 # create user, if required
1493 my $user = DXUser->get_current($ecall);
1495 $user = DXUser->new($ecall);
1497 $user->priv(1); # I have relented and defaulted nodes
1499 $user->homenode($ncall);
1500 $user->node($ncall);
1502 if ($esort eq 'U') {
1503 $ref = Route::User::get($ecall);
1505 # create user, if required
1506 my $user = DXUser->get_current($ecall);
1508 $user = DXUser->new($ecall);
1510 $user->homenode($ncall);
1511 $user->node($ncall);
1514 $ref = Route::User->new($ecall, 0);
1516 } elsif ($esort eq 'N') {
1517 $ref = Route::Node::get($ecall);
1519 # create user, if required
1520 my $user = DXUser->get_current($ecall);
1522 $user = DXUser->new($ecall);
1523 $user->priv(1); # I have relented and defaulted nodes
1526 $user->homenode($ncall);
1527 $user->node($ncall);
1530 $ref = Route::Node->new($ecall, 0);
1533 dbg("DXPROT: unknown entity type '$esort' on $ecall for node $ncall") if isdbg('chan');
1536 $ref->here($ehere); # might as well set this here
1537 $ref->lastseen($main::systime);
1541 # if it is a delete, disconnect all the entries mentioned
1542 # from this node (which is a parent in this context).
1546 for my $ref (@refs) {
1547 next if $ref->call eq $ncall;
1548 next if $ref->call eq $main::mycall;
1549 if ($ref->isa('Route::Node')) {
1550 push @delnode, $node->unlink_node($ref, $self);
1551 } elsif ($ref->isa('Route::User')) {
1552 push @deluser, $node->del_user($ref);
1557 # if it is an add, connect all the entries
1561 for my $ref (@refs) {
1562 next if $ref->call eq $ncall;
1563 next if $ref->call eq $main::mycall;
1564 if ($ref->isa('Route::Node')) {
1565 my $new = $node->link_node($ref, $self);
1566 push @addnode, $new if $new;
1567 } elsif ($ref->isa('Route::User')) {
1568 push @adduser, $node->add_user($ref);
1573 # if it is a configure, unlink all the nodes and users that
1574 # are not in @refs but are in the node, then add all the
1575 # nodes and users that are @refs but not in the node.
1582 for my $r (map {Route::Node::get($_)} $node->nodes) {
1584 next if $r->call eq $ncall;
1585 next if $r->call eq $main::mycall;
1586 push @dn, $r unless grep $_->call eq $r->call, @refs;
1588 for my $r (map {Route::User::get($_)} $node->users) {
1590 push @du, $r unless grep $_->call eq $r->call, @refs;
1594 next if $r->call eq $ncall;
1595 next if $r->call eq $main::mycall;
1596 if ($r->isa('Route::Node')) {
1597 push @an, $r unless grep $r->call eq $_, $node->nodes;
1598 } elsif ($r->isa('Route::User')) {
1599 push @au, $r unless grep $r->call eq $_, $node->users;
1602 push @addnode, $node if $self->{state} =~ /^init/;
1603 push @delnode, $node->unlink_node($_, $self) for @dn;
1604 push @deluser, $node->del_user($_) for @du;
1605 push @addnode, $node->link_node($_, $self) for @an;
1606 push @adduser, $node->add_user($_) for @au;
1609 $self->route_pc21($origin, $line, @delnode) if @delnode;
1610 $self->route_pc19($origin, $line, @addnode) if @addnode;
1611 $self->route_pc17($origin, $line, $node, @deluser) if @deluser;
1612 $self->route_pc16($origin, $line, $node, @adduser) if @adduser;
1614 $self->route_pc59($sort, $hextime, $ncall, @refs) if @refs;
1615 $_->delete for @delnode, @deluser;
1619 # dunno but route it
1627 if ($call ne $main::mycall) {
1628 $self->route($call, $line);
1642 my $d = cltounix($call, sprintf("%02d18Z", $_[2]));
1643 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $_[2] < 0 || $_[2] > 23) {
1644 dbg("PCPROT: WCY Date ($call $_[2]) out of range") if isdbg('chanerr');
1647 @_ = map { unpad($_) } @_;
1649 dbg("PCPROT: Dup WCY Spot ignored\n") if isdbg('chanerr');
1653 my $wcy = WCY::update($d, @_[2..12]);
1657 $rep = Local::wcy($self, @_[1..12]);
1659 # dbg("Local::wcy error $@") if isdbg('local') if $@;
1662 # broadcast to the eager world
1663 send_wcy_spot($self, $line, $d, @_[2..12]);
1666 # remote commands (incoming)
1673 $self->process_rcmd($_[1], $_[2], $_[3], $_[4]);
1676 # remote command replies
1683 $self->process_rcmd_reply($_[1], $_[2], $_[3], $_[4]);
1686 # if get here then rebroadcast the thing with its Hop count decremented (if
1687 # there is one). If it has a hop count and it decrements to zero then don't
1690 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
1701 if (eph_dup($line)) {
1702 dbg("PCPROT: Ephemeral dup, dropped") if isdbg('chanerr');
1704 unless ($self->{isolate}) {
1705 DXChannel::broadcast_nodes($line, $self) if $line =~ /\^H\d+\^?~?$/; # send it to everyone but me
1711 # This is called from inside the main cluster processing loop and is used
1712 # for despatching commands that are doing some long processing job
1714 # It is called once per second
1719 my @dxchan = DXChannel->get_all();
1723 # send out a pc50 on EVERY channel all at once
1724 if ($t >= $last_pc50 + $DXProt::pc50_interval) {
1725 $pc50s = pc50($main::me, scalar DXChannel::get_all_users);
1730 foreach $dxchan (@dxchan) {
1731 next unless $dxchan->is_node();
1732 next if $dxchan == $main::me;
1735 $dxchan->send($pc50s) if $pc50s;
1737 # send a ping out on this channel
1738 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
1739 if ($dxchan->{nopings} <= 0) {
1740 $dxchan->disconnect;
1742 addping($main::mycall, $dxchan->call);
1743 $dxchan->{nopings} -= 1;
1744 $dxchan->{lastping} = $t;
1749 Investigate::process();
1752 if ($t - $last10 >= 10) {
1753 # clean out ephemera
1760 if ($main::systime - 3600 > $last_hour) {
1761 $last_hour = $main::systime;
1766 # finish up a pc context
1770 # some active measures
1778 my @dxchan = DXChannel->get_all();
1781 # send it if it isn't the except list and isn't isolated and still has a hop count
1782 # taking into account filtering and so on
1783 foreach $dxchan (@dxchan) {
1784 next if $dxchan == $main::me;
1785 next if $dxchan == $self && $self->is_node;
1786 $dxchan->dx_spot($line, $self->{isolate}, @_, $self->{call});
1794 my $isolate = shift;
1795 my ($filter, $hops);
1797 if ($self->{spotsfilter}) {
1798 ($filter, $hops) = $self->{spotsfilter}->it(@_);
1799 return unless $filter;
1801 send_prot_line($self, $filter, $hops, $isolate, $line);
1806 my ($self, $filter, $hops, $isolate, $line) = @_;
1812 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1814 $routeit = adjust_hops($self, $line); # adjust its hop count by node name
1815 return unless $routeit;
1818 $self->send($routeit);
1820 $self->send($routeit) unless $self->{isolate} || $isolate;
1829 my @dxchan = DXChannel->get_all();
1831 my @dxcc = ((Prefix::cty_data($_[6]))[0..2], (Prefix::cty_data($_[7]))[0..2]);
1833 # send it if it isn't the except list and isn't isolated and still has a hop count
1834 # taking into account filtering and so on
1835 foreach $dxchan (@dxchan) {
1836 next if $dxchan == $main::me;
1837 next if $dxchan == $self && $self->is_node;
1839 my ($filter, $hops);
1841 $dxchan->wwv($line, $self->{isolate}, @_, $self->{call}, @dxcc);
1849 my $isolate = shift;
1850 my ($filter, $hops);
1852 if ($self->{wwvfilter}) {
1853 ($filter, $hops) = $self->{wwvfilter}->it(@_);
1854 return unless $filter;
1856 send_prot_line($self, $filter, $hops, $isolate, $line)
1863 my @dxchan = DXChannel->get_all();
1865 my @dxcc = ((Prefix::cty_data($_[10]))[0..2], (Prefix::cty_data($_[11]))[0..2]);
1867 # send it if it isn't the except list and isn't isolated and still has a hop count
1868 # taking into account filtering and so on
1869 foreach $dxchan (@dxchan) {
1870 next if $dxchan == $main::me;
1871 next if $dxchan == $self;
1873 $dxchan->wcy($line, $self->{isolate}, @_, $self->{call}, @dxcc);
1881 my $isolate = shift;
1882 my ($filter, $hops);
1884 if ($self->{wcyfilter}) {
1885 ($filter, $hops) = $self->{wcyfilter}->it(@_);
1886 return unless $filter;
1888 send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
1896 my @dxchan = DXChannel->get_all();
1900 my $text = unpad($_[2]);
1902 if ($_[3] eq '*') { # sysops
1904 } elsif ($_[3] gt ' ') { # speciality list handling
1905 my ($name) = split /\./, $_[3];
1906 $target = "$name"; # put the rest in later (if bothered)
1913 $target = "ALL" if !$target;
1916 # obtain country codes etc
1917 my @a = Prefix::cty_data($_[0]);
1918 my @b = Prefix::cty_data($_[4]);
1919 if ($self->{inannfilter}) {
1920 my ($filter, $hops) =
1921 $self->{inannfilter}->it(@_, $self->{call},
1923 @b[0..2], $a[3], $b[3]);
1925 dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
1930 if (AnnTalk::dup($_[0], $_[1], $_[2])) {
1931 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
1935 Log('ann', $target, $_[0], $text);
1937 # send it if it isn't the except list and isn't isolated and still has a hop count
1938 # taking into account filtering and so on
1939 foreach $dxchan (@dxchan) {
1940 next if $dxchan == $main::me;
1941 next if $dxchan == $self && $self->is_node;
1942 $dxchan->announce($line, $self->{isolate}, $to, $target, $text, @_, $self->{call},
1943 @a[0..2], @b[0..2]);
1952 $msgid = 1 if $msgid > 999;
1961 my @dxchan = DXChannel->get_all();
1964 my $text = unpad($_[2]);
1967 # munge the group and recast the line if required
1968 if ($target =~ s/\.LST$//) {
1972 # obtain country codes etc
1973 my @a = Prefix::cty_data($_[0]);
1974 my @b = Prefix::cty_data($_[4]);
1975 if ($self->{inannfilter}) {
1976 my ($filter, $hops) =
1977 $self->{inannfilter}->it(@_, $self->{call},
1979 @b[0..2], $a[3], $b[3]);
1981 dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
1986 if (AnnTalk::dup($_[0], $_[1], $_[2], $chatdupeage)) {
1987 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
1992 Log('chat', $target, $_[0], $text);
1994 # send it if it isn't the except list and isn't isolated and still has a hop count
1995 # taking into account filtering and so on
1996 foreach $dxchan (@dxchan) {
1997 my $is_ak1a = $dxchan->is_ak1a;
1999 if ($dxchan->is_node) {
2000 next if $dxchan == $main::me;
2001 next if $dxchan == $self;
2002 next unless $dxchan->is_spider || $is_ak1a;
2003 next if $target eq 'LOCAL';
2004 if (!$ak1a_line && $is_ak1a) {
2005 $ak1a_line = DXProt::pc12($_[0], $text, $_[1], "$target.LST");
2009 $dxchan->chat($is_ak1a ? $ak1a_line : $line, $self->{isolate}, $target, $_[1],
2010 $text, @_, $self->{call}, @a[0..2], @b[0..2]);
2018 my $isolate = shift;
2022 my ($filter, $hops);
2024 if ($self->{annfilter}) {
2025 ($filter, $hops) = $self->{annfilter}->it(@_);
2026 return unless $filter;
2028 send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
2037 sub send_local_config
2045 dbg('DXProt::send_local_config') if isdbg('trace');
2047 if ($self->{newroute}) {
2048 my @nodes = $self->{isolate} ? ($main::routeroot) : grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
2049 my @users = DXChannel::get_all_users();
2050 @localnodes = map { Route::Node::get($_->{call}) } @nodes;
2051 my @localusers = map { Route::User::get($_->{call}) } @users;
2052 $self->send_route($main::mycall, \&pc59, @nodes+@users+4, 'C', 0, $main::mycall, $main::routeroot, @localnodes, @localusers);
2055 if ($self->{isolate}) {
2056 @localnodes = ( $main::routeroot );
2057 $self->send_route($main::mycall, \&pc19, 1, $main::routeroot);
2059 # create a list of all the nodes that are not connected to this connection
2060 # and are not themselves isolated, this to make sure that isolated nodes
2061 # don't appear outside of this node
2063 # send locally connected nodes
2064 my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
2065 @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan if @dxchan;
2066 $self->send_route($main::mycall, \&pc19, scalar(@localnodes)+1, $main::routeroot, @localnodes);
2069 my @rawintcalls = map { $_->nodes } @localnodes if @localnodes;
2071 for $node (@rawintcalls) {
2072 push @intcalls, $node unless grep $node eq $_, @intcalls;
2074 my $ref = Route::Node::get($self->{call});
2075 my @rnodes = $ref->nodes;
2076 for $node (@intcalls) {
2077 push @remotenodes, Route::Node::get($node) unless grep $node eq $_, @rnodes, @remotenodes;
2079 $self->send_route($main::mycall, \&pc19, scalar(@remotenodes), @remotenodes);
2082 # get all the users connected on the above nodes and send them out
2083 foreach $node ($main::routeroot, @localnodes, @remotenodes) {
2085 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
2086 $self->send_route($main::mycall, \&pc16, 1, $node, @rout) if @rout && $self->user->wantsendpc16;
2088 dbg("sent a null value") if isdbg('chanerr');
2095 # route a message down an appropriate interface for a callsign
2097 # is called route(to, pcline);
2101 my ($self, $call, $line) = @_;
2103 if (ref $self && $call eq $self->{call}) {
2104 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
2108 # always send it down the local interface if available
2109 my $dxchan = DXChannel->get($call);
2111 my $cl = Route::get($call);
2112 $dxchan = $cl->bestdxchan if $cl;
2114 if (ref $self && $dxchan eq $self) {
2115 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
2121 my $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
2123 $dxchan->send($routeit) unless $dxchan == $main::me;
2126 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
2131 # obtain the hops from the list for this callsign and pc no
2137 my $hops = $DXProt::hopcount{$pcno};
2138 $hops = $DXProt::def_hopcount if !$hops;
2143 # adjust the hop count on a per node basis using the user loadable
2144 # hop table if available or else decrement an existing one
2151 my $call = $self->{call};
2154 if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
2155 my ($pcno) = $s =~ /^PC(\d\d)/o;
2156 confess "$call called adjust_hops with '$s'" unless $pcno;
2157 my $ref = $nodehops{$call} if %nodehops;
2159 my $newhops = $ref->{$pcno};
2160 return "" if defined $newhops && $newhops == 0;
2161 $newhops = $ref->{default} unless $newhops;
2162 return "" if defined $newhops && $newhops == 0;
2163 $newhops = $hops if !$newhops;
2164 $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
2166 # simply decrement it
2168 return "" if !$hops;
2169 $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
2181 return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
2182 do "$main::data/hop_table.pl";
2188 # add a ping request to the ping queues
2191 my ($from, $to, $via) = @_;
2192 my $ref = $pings{$to} || [];
2195 $r->{t} = [ gettimeofday ];
2196 if ($via && (my $dxchan = DXChannel->get($via))) {
2197 $dxchan->send(pc51($to, $main::mycall, 1));
2199 route(undef, $to, pc51($to, $main::mycall, 1));
2203 my $u = DXUser->get_current($to);
2205 $u->lastping($main::systime);
2212 my ($self, $tonode, $fromnode, $user, $cmd) = @_;
2213 if ($tonode eq $main::mycall) {
2214 my $ref = DXUser->get_current($fromnode);
2215 my $cref = Route::Node::get($fromnode);
2216 Log('rcmd', 'in', $ref->{priv}, $fromnode, $cmd);
2217 if ($cmd !~ /^\s*rcmd/i && $cref && $ref && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS!
2218 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
2219 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
2220 my $oldpriv = $self->{priv};
2221 $self->{priv} = $ref->{priv}; # assume the user's privilege level
2222 my @in = (DXCommandmode::run_cmd($self, $cmd));
2223 $self->{priv} = $oldpriv;
2224 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @in);
2225 delete $self->{remotecmd};
2227 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
2230 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "your attempt is logged, Tut tut tut...!");
2233 my $ref = DXUser->get_current($tonode);
2234 if ($ref && $ref->is_clx) {
2235 $self->route($tonode, pc84($fromnode, $tonode, $user, $cmd));
2237 $self->route($tonode, pc34($fromnode, $tonode, $cmd));
2242 sub process_rcmd_reply
2244 my ($self, $tonode, $fromnode, $user, $line) = @_;
2245 if ($tonode eq $main::mycall) {
2246 my $s = $rcmds{$fromnode};
2248 my $dxchan = DXChannel->get($s->{call});
2249 my $ref = $user eq $tonode ? $dxchan : (DXChannel->get($user) || $dxchan);
2250 $ref->send($line) if $ref;
2251 delete $rcmds{$fromnode} if !$dxchan;
2253 # send unsolicited ones to the sysop
2254 my $dxchan = DXChannel->get($main::myalias);
2255 $dxchan->send($line) if $dxchan;
2258 my $ref = DXUser->get_current($tonode);
2259 if ($ref && $ref->is_clx) {
2260 $self->route($tonode, pc85($fromnode, $tonode, $user, $line));
2262 $self->route($tonode, pc35($fromnode, $tonode, $line));
2271 my $fromnode = shift;
2276 Log('rcmd', 'out', $fromnode, $line);
2277 if ($self->is_clx) {
2278 $self->send(pc85($main::mycall, $fromnode, $user, "$main::mycall:$line"));
2280 $self->send(pc35($main::mycall, $fromnode, "$main::mycall:$line"));
2285 # add a rcmd request to the rcmd queues
2288 my ($self, $to, $cmd) = @_;
2291 $r->{call} = $self->{call};
2292 $r->{t} = $main::systime;
2296 my $ref = Route::Node::get($to);
2297 my $dxchan = $ref->bestdxchan;
2298 if ($dxchan && $dxchan->is_clx) {
2299 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
2301 route(undef, $to, pc34($main::mycall, $to, $cmd));
2308 my $pc39flag = shift;
2309 my $call = $self->call;
2311 return if $self->{disconnecting}++;
2313 unless ($pc39flag && $pc39flag == 1) {
2314 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
2317 # get rid of any PC16/17/19
2318 eph_del_regex("^PC1[679]*$call");
2320 # do routing stuff, remove me from routing table
2321 my $node = Route::Node::get($call);
2325 # remove the route from this node and return a list
2326 # of nodes that have become orphanned as a result.
2327 push @rout, $main::routeroot->remove_route($node, $self);
2329 # remove all my ephemera as well
2332 eph_del_regex("^PC1[679].*$c");
2336 # unbusy and stop and outgoing mail
2337 my $mref = DXMsg::get_busy($call);
2338 $mref->stop_msg($call) if $mref;
2340 # broadcast to all other nodes that all the nodes connected to via me are gone
2341 unless ($pc39flag && $pc39flag == 2) {
2342 $self->route_pc21($main::mycall, undef, @rout) if @rout;
2343 $self->route_pc59('D', 0, $main::mycall, $node);
2346 # delete all the unwanted nodes
2347 $_->delete for @rout;
2349 # remove outstanding pings
2350 delete $pings{$call};
2352 # I was the last node visited
2353 $self->user->node($main::mycall);
2355 # send info to all logged in thingies
2356 $self->tell_login('logoutn');
2358 Log('DXProt', $call . " Disconnected");
2360 $self->SUPER::disconnect;
2365 # send a talk message to this thingy
2369 my ($self, $from, $to, $via, $line, $origin) = @_;
2371 $line =~ s/\^/\\5E/g; # remove any ^ characters
2372 $self->send(DXProt::pc10($from, $to, $via, $line, $origin));
2373 Log('talk', $to, $from, $via?$via:$self->call, $line) unless $origin && $origin ne $main::mycall;
2376 # send it if it isn't the except list and isn't isolated and still has a hop count
2377 # taking into account filtering and so on
2383 my $generate = shift;
2384 my $no = shift; # the no of things to filter on
2386 my ($filter, $hops);
2389 for (; @_ && $no; $no--) {
2392 # deal with non routing parameters
2393 unless (ref $r && $r->isa('Route')) {
2398 if (!$self->{isolate} && $self->{routefilter}) {
2401 ($filter, $hops) = $self->{routefilter}->it($self->{call}, $self->{dxcc}, $self->{itu}, $self->{cq}, $r->call, $r->dxcc, $r->itu, $r->cq, $self->{state}, $r->{state});
2405 dbg("DXPROT: $self->{call}/" . $r->call . " rejected by output filter") if isdbg('chanerr');
2408 dbg("was sent a null value") if isdbg('chanerr');
2411 push @rin, $r unless $self->{isolate} && $r->call ne $main::mycall;
2415 foreach my $line (&$generate(@rin, @_)) {
2418 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
2420 $routeit = adjust_hops($self, $line); # adjust its hop count by node name
2421 next unless $routeit;
2424 $self->send($routeit);
2433 my $generate = shift;
2435 my @dxchan = DXChannel::get_all_nodes();
2438 unless ($self->{isolate}) {
2439 foreach $dxchan (@dxchan) {
2440 next if $dxchan == $self;
2441 next if $dxchan == $main::me;
2442 next unless $dxchan->isa('DXProt');
2443 next if ($generate == \&pc16 || $generate==\&pc17) && !$dxchan->user->wantsendpc16;
2444 if ($dxchan->{newroute}) {
2445 next if ($generate == \&pc19 || $generate==\&pc21);
2447 next if ($generate == \&pc19 || $generate==\&pc21) && !$dxchan->user->wantroutepc19;
2448 next if ($generate == \&pc59);
2451 $dxchan->send_route($origin, $generate, @_);
2459 return unless $self->user->wantpc16;
2462 broadcast_route($self, $origin, \&pc16, $line, 1, @_);
2468 return unless $self->user->wantpc16;
2471 broadcast_route($self, $origin, \&pc17, $line, 1, @_);
2479 broadcast_route($self, $origin, \&pc19, $line, scalar @_, @_);
2487 broadcast_route($self, $origin, \&pc21, $line, scalar @_, @_);
2495 broadcast_route($self, $origin, \&pc24, $line, 1, @_);
2503 broadcast_route($self, $origin, \&pc41, $line, 1, @_);
2511 broadcast_route($self, $origin, \&pc50, $line, 1, @_);
2520 broadcast_route($self, $origin, \&pc59, $line, scalar @_, @_);
2527 my ($filter, $hops) = (1, 1);
2529 if ($self->{inroutefilter}) {
2530 ($filter, $hops) = $self->{inroutefilter}->it($self->{call}, $self->{dxcc}, $self->{itu}, $self->{cq}, $r->call, $r->dxcc, $r->itu, $r->cq, $self->state, $r->state);
2531 dbg("PCPROT: $self->{call}/" . $r->call . ' rejected by in_filter_route') if !$filter && isdbg('chanerr');
2539 my $t = shift || $eph_restime;
2543 $s =~ s/\^H\d\d?\^?\~?$//;
2544 $r = 1 if exists $eph{$s}; # pump up the dup if it keeps circulating
2545 $eph{$s} = $main::systime + $t;
2553 while (($key, $val) = each %eph) {
2554 if ($key =~ m{$regex}) {
2564 while (($key, $val) = each %eph) {
2565 if ($main::systime >= $val) {
2576 while (($key, $val) = each %eph) {
2577 push @out, $key, $val;
2584 goto &DXCommandmode::run_cmd;