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
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
79 [ qw(i c c m bp bc c) ], # pc10
80 [ qw(i f m d t m c c h) ], # pc11
81 [ qw(i c bm m bm bm p h) ], # pc12
85 undef , # pc16 has to be validated manually
86 [ qw(i c c h) ], # pc17
88 undef , # pc19 has to be validated manually
89 undef , # pc20 no validation
90 [ qw(i c m h) ], # pc21
91 undef , # pc22 no validation
92 [ qw(i d n n n n m c c h) ], # pc23
93 [ qw(i c p h) ], # pc24
94 [ qw(i c c n n) ], # pc25
95 [ qw(i f m d t m c c bc) ], # pc26
96 [ qw(i d n n n n m c c bc) ], # pc27
97 [ qw(i c c m c d t p m bp n p bp bc) ], # pc28
98 [ qw(i c c n m) ], # pc29
99 [ qw(i c c n) ], # pc30
100 [ qw(i c c n) ], # pc31
101 [ qw(i c c n) ], # pc32
102 [ qw(i c c n) ], # pc33
103 [ qw(i c c m) ], # pc34
104 [ qw(i c c m) ], # pc35
105 [ qw(i c c m) ], # pc36
106 [ qw(i c c n m) ], # pc37
107 undef, # pc38 not interested
108 [ qw(i c m) ], # pc39
109 [ qw(i c c m p n) ], # pc40
110 [ qw(i c n m h) ], # pc41
111 [ qw(i c c n) ], # pc42
112 undef, # pc43 don't handle it
113 [ qw(i c c n m m c) ], # pc44
114 [ qw(i c c n m) ], # pc45
115 [ qw(i c c n) ], # pc46
118 [ qw(i c m h) ], # pc49
119 [ qw(i c n h) ], # pc50
120 [ qw(i c c n) ], # pc51
142 [ qw(i d n n n n n n m m m c c h) ], # pc73
153 [ qw(i c c c m) ], # pc84
154 [ qw(i c c c m) ], # pc85
159 [ qw(i c n) ], # pc90
162 # use the entry in the check list to check the field list presented
163 # return OK if line NOT in check list (for now)
168 return 0 if $n < 0 || $n > @checklist;
169 my $ref = $checklist[$n];
170 return 0 unless ref $ref;
173 for ($i = 1; $i < @$ref; $i++) {
174 my ($blank, $act) = $$ref[$i] =~ /^(b?)(\w)$/;
175 return 0 unless $act;
176 next if $blank && $_[$i] =~ /^[ \*]$/;
178 return $i unless is_callsign($_[$i]);
179 } elsif ($act eq 'i') {
181 } elsif ($act eq 'm') {
182 return $i unless is_pctext($_[$i]);
183 } elsif ($act eq 'p') {
184 return $i unless is_pcflag($_[$i]);
185 } elsif ($act eq 'f') {
186 return $i unless is_freq($_[$i]);
187 } elsif ($act eq 'n') {
188 return $i unless $_[$i] =~ /^[\d ]+$/;
189 } elsif ($act eq 'h') {
190 return $i unless $_[$i] =~ /^H\d\d?$/;
191 } elsif ($act eq 'd') {
192 return $i unless $_[$i] =~ /^\s*\d+-\w\w\w-[12][90]\d\d$/;
193 } elsif ($act eq 't') {
194 return $i unless $_[$i] =~ /^[012]\d[012345]\dZ$/;
202 do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
205 my $user = DXUser->get($main::mycall);
206 $DXProt::myprot_version += $main::version*100;
207 $main::me = DXProt->SUPER::alloc($main::mycall, 0, $user);
208 $main::me->{here} = 1;
209 $main::me->{state} = "indifferent";
210 $main::me->{sort} = 'S'; # S for spider
211 $main::me->{priv} = 9;
212 $main::me->{metric} = 0;
213 $main::me->{pingave} = 0;
214 $main::me->{registered} = 1;
215 $main::me->{version} = $main::version;
216 $main::me->{build} = $main::build;
220 # obtain a new connection this is derived from dxchannel
225 my $self = DXChannel::alloc(@_);
227 # add this node to the table, the values get filled in later
231 my $uref = Route::Node::get($call) || Route::Node->new($call);
234 $uref->version(5000);
235 $main::routeroot->link_node($uref, $self);
239 # this is how a pc connection starts (for an incoming connection)
240 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
241 # all the crap that comes between).
244 my ($self, $line, $sort) = @_;
245 my $call = $self->{call};
246 my $user = $self->{user};
249 my $host = $self->{conn}->{peerhost} || "unknown";
250 Log('DXProt', "$call connected from $host");
252 # remember type of connection
253 $self->{consort} = $line;
254 $self->{outbound} = $sort eq 'O';
255 my $priv = $user->priv;
256 $priv = $user->priv(1) unless $priv;
257 $self->{priv} = $priv; # other clusters can always be 'normal' users
258 $self->{lang} = $user->lang || 'en';
259 $self->{isolate} = $user->{isolate};
260 $self->{consort} = $line; # save the connection type
264 # sort out registration
265 $self->{registered} = 1;
267 # get the output filters
268 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
269 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
270 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
271 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
272 $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) unless $self->{isolate} ;
275 # get the INPUT filters (these only pertain to Clusters)
276 $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
277 $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
278 $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
279 $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
280 $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1) unless $self->{isolate};
282 # set unbuffered and no echo
283 $self->send_now('B',"0");
284 $self->send_now('E',"0");
285 $self->conn->echo(0) if $self->conn->can('echo');
287 # ping neighbour node stuff
288 my $ping = $user->pingint;
289 $ping = $pingint unless defined $ping;
290 $self->{pingint} = $ping;
291 $self->{nopings} = $user->nopings || $obscount;
292 $self->{pingtime} = [ ];
293 $self->{pingave} = 999;
294 $self->{metric} ||= 100;
295 $self->{lastping} = $main::systime;
297 # send initialisation string
298 unless ($self->{outbound}) {
302 $self->state('init');
303 $self->{pc50_t} = $main::systime;
305 # send info to all logged in thingies
306 $self->tell_login('loginn');
308 # run a script send the output to the debug file
309 my $script = new Script(lc $call) || new Script('node_default');
310 $script->run($self) if $script;
314 # send outgoing 'challenge'
325 $_[0] =~ s/^PC90\^[-A-Z0-9]+\^\d+\^//;
326 $_[0] =~ s/^PC91\^[-A-Z0-9]+\^\d+\^[-A-Z0-9]+\^//;
334 # $self->SUPER::send($line);
339 # This is the normal pcxx despatcher
343 my ($self, $line) = @_;
345 # remove any incoming PC90 frames
348 my @field = split /\^/, $line;
349 return unless @field;
351 pop @field if $field[-1] eq '~';
353 # print join(',', @field), "\n";
356 # process PC frames, this will fail unless the frame starts PCnn
357 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
358 unless (defined $pcno && $pcno >= 10 && $pcno <= 99) {
359 dbg("PCPROT: unknown protocol") if isdbg('chanerr');
363 # check for and dump bad protocol messages
364 my $n = check($pcno, @field);
366 dbg("PCPROT: bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")") if isdbg('chanerr');
370 my $origin = $self->{call};
372 my $sub = "handle_$pcno";
374 if ($self->can($sub)) {
375 $self->$sub($pcno, $line, $origin, @field);
377 $self->handle_default($pcno, $line, $origin, @field);
381 # incoming talk commands
390 return if $rspfcheck and !$self->rspfcheck(0, $_[6], $_[1]);
392 # will we allow it at all?
395 if (@bad = BadWords::check($_[3])) {
396 dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
401 # is it for me or one of mine?
402 my ($from, $to, $via, $call, $dxchan);
411 # if this is a 'nodx' node then ignore it
412 if ($badnode->in($_[6]) || ($via && $badnode->in($via))) {
413 dbg("PCPROT: Bad Node, dropped") if isdbg('chanerr');
417 # if this is a 'bad spotter' user then ignore it
419 $nossid =~ s/-\d+$//;
420 if ($badspotter->in($nossid)) {
421 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
425 # if we are converting announces to talk is it a dup?
427 if (AnnTalk::is_talk_candidate($from, $_[3]) && AnnTalk::dup($from, $to, $_[3])) {
428 dbg("DXPROT: Dupe talk from announce, dropped") if isdbg('chanerr');
433 # it is here and logged on
434 $dxchan = DXChannel->get($main::myalias) if $to eq $main::mycall;
435 $dxchan = DXChannel->get($to) unless $dxchan;
436 if ($dxchan && $dxchan->is_user) {
438 $dxchan->talk($from, $to, $via, $_[3]);
442 # is it elsewhere, visible on the cluster via the to address?
443 # note: this discards the via unless the to address is on
446 if ($ref = Route::get($to)) {
447 $vref = Route::Node::get($via) if $via;
448 $vref = undef unless $vref && grep $to eq $_, $vref->users;
449 $ref->bestdxchan->talk($from, $to, $vref ? $via : undef, $_[3], $_[6]);
453 # not visible here, send a message of condolence
455 $ref = Route::get($from);
456 $vref = $ref = Route::Node::get($_[6]) unless $ref;
458 $dxchan = $ref->bestdxchan;
459 $dxchan->talk($main::mycall, $from, $vref ? $vref->call : undef, $dxchan->msg('talknh', $to) );
471 # route 'foreign' pc26s
473 if ($_[7] ne $main::mycall) {
474 $self->route($_[7], $line);
480 # return if $rspfcheck and !$self->rspfcheck(1, $_[7], $_[6]);
482 # if this is a 'nodx' node then ignore it
483 if ($badnode->in($_[7])) {
484 dbg("PCPROT: Bad Node, dropped") if isdbg('chanerr');
488 # if this is a 'bad spotter' user then ignore it
490 $nossid =~ s/-\d+$//;
491 if ($badspotter->in($nossid)) {
492 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
496 # convert the date to a unix date
497 my $d = cltounix($_[3], $_[4]);
498 # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
499 if (!$d || ($pcno == 11 && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
500 dbg("PCPROT: Spot ignored, invalid date or out of range ($_[3] $_[4])\n") if isdbg('chanerr');
505 if ($baddx->in($_[2]) || BadWords::check($_[2]) || $_[2] =~ /COCK/) {
506 dbg("PCPROT: Bad DX spot, ignored") if isdbg('chanerr');
511 $_[5] =~ s/^\s+//; # take any leading blanks off
512 $_[2] = unpad($_[2]); # take off leading and trailing blanks from spotted callsign
513 if ($_[2] =~ /BUST\w*$/) {
514 dbg("PCPROT: useless 'BUSTED' spot") if isdbg('chanerr');
519 if (@bad = BadWords::check($_[5])) {
520 dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
526 my @spot = Spot::prepare($_[1], $_[2], $d, $_[5], $_[6], $_[7]);
527 # global spot filtering on INPUT
528 if ($self->{inspotsfilter}) {
529 my ($filter, $hops) = $self->{inspotsfilter}->it(@spot);
531 dbg("PCPROT: Rejected by input spot filter") if isdbg('chanerr');
536 # this goes after the input filtering, but before the add
537 # so that if it is input filtered, it isn't added to the dup
538 # list. This allows it to come in from a "legitimate" source
539 if (Spot::dup($_[1], $_[2], $d, $_[5], $_[6])) {
540 dbg("PCPROT: Duplicate Spot ignored\n") if isdbg('chanerr');
548 # @spot at this point contains:-
549 # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
550 # then spotted itu, spotted cq, spotters itu, spotters cq
551 # you should be able to route on any of these
554 # fix up qra locators of known users
555 my $user = DXUser->get_current($spot[4]);
557 my $qra = $user->qra;
558 unless ($qra && is_qra($qra)) {
559 my $lat = $user->lat;
560 my $long = $user->long;
561 if (defined $lat && defined $long) {
562 $user->qra(DXBearing::lltoqra($lat, $long));
567 # send a remote command to a distant cluster if it is visible and there is no
568 # qra locator and we havn't done it for a month.
570 unless ($user->qra) {
572 my $to = $user->homenode;
573 my $last = $user->lastoper || 0;
574 if ($send_opernam && $to && $to ne $main::mycall && $main::systime > $last + $DXUser::lastoperinterval && ($node = Route::Node::get($to)) ) {
575 my $cmd = "forward/opernam $spot[4]";
576 # send the rcmd but we aren't interested in the replies...
577 my $dxchan = $node->bestdxchan;
578 if ($dxchan && $dxchan->is_clx) {
579 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
581 route(undef, $to, pc34($main::mycall, $to, $cmd));
585 $node = Route::Node::get($to);
587 $dxchan = $node->bestdxchan;
588 if ($dxchan && $dxchan->is_clx) {
589 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
591 route(undef, $to, pc34($main::mycall, $to, $cmd));
595 $user->lastoper($main::systime);
604 $r = Local::spot($self, @spot);
606 # dbg("Local::spot1 error $@") if isdbg('local') if $@;
609 # DON'T be silly and send on PC26s!
610 return if $pcno == 26;
612 # send out the filtered spots
613 send_dx_spot($self, $line, @spot) if @spot;
624 # return if $rspfcheck and !$self->rspfcheck(1, $_[5], $_[1]);
626 # announce duplicate checking
627 $_[3] =~ s/^\s+//; # remove leading blanks
631 if (@bad = BadWords::check($_[3])) {
632 dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
637 # if this is a 'nodx' node then ignore it
638 if ($badnode->in($_[5])) {
639 dbg("PCPROT: Bad Node, dropped") if isdbg('chanerr');
643 # if this is a 'bad spotter' user then ignore it
645 $nossid =~ s/-\d+$//;
646 if ($badspotter->in($nossid)) {
647 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
653 if ((($dxchan = DXChannel->get($_[2])) && $dxchan->is_user) || $_[4] =~ /^[\#\w.]+$/){
654 $self->send_chat($line, @_[1..6]);
655 } elsif ($_[2] eq '*' || $_[2] eq $main::mycall) {
657 # ignore something that looks like a chat line coming in with sysop
658 # flag - this is a kludge...
659 if ($_[3] =~ /^\#\d+ / && $_[4] eq '*') {
660 dbg('PCPROT: Probable chat rewrite, dropped') if isdbg('chanerr');
664 # here's a bit of fun, convert incoming ann with a callsign in the first word
665 # or one saying 'to <call>' to a talk if we can route to the recipient
667 my $call = AnnTalk::is_talk_candidate($_[1], $_[3]);
669 my $ref = Route::get($call);
671 $dxchan = $ref->bestdxchan;
672 $dxchan->talk($_[1], $call, undef, $_[3], $_[5]) if $dxchan != $self;
679 $self->send_announce($line, @_[1..6]);
681 $self->route($_[2], $line);
697 my $newline = "PC16^";
699 # do I want users from this channel?
700 unless ($self->user->wantpc16) {
701 dbg("PCPROT: don't send users to $self->{call}") if isdbg('chanerr');
706 if ($ncall eq $main::mycall) {
707 dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
711 # do we believe this call?
712 unless ($ncall eq $self->{call} || $self->is_believed($ncall)) {
713 dbg("PCPROT: We don't believe $ncall on $self->{call}");
717 my $node = Route::Node::get($ncall);
719 dbg("PCPROT: Node $ncall not in config") if isdbg('chanerr');
723 # dedupe only that which we potentially process
724 if (eph_dup($line)) {
725 dbg("PCPROT: dup PC16 detected") if isdbg('chanerr');
732 for ($i = 2; $i < $#_; $i++) {
733 my ($call, $conf, $here) = $_[$i] =~ /^(\S+) (\S) (\d)/o;
734 next unless $call && $conf && defined $here && is_callsign($call);
735 next if $call eq $main::mycall;
737 eph_del_regex("^PC17\\^$call\\^$ncall");
739 $conf = $conf eq '*';
741 # reject this if we think it is a node already
742 my $r = Route::Node::get($call);
743 my $u = DXUser->get_current($call) unless $r;
744 if ($r || ($u && $u->is_node)) {
745 dbg("PCPROT: $call is a node") if isdbg('chanerr');
749 $r = Route::User::get($call) || Route::User->new($call);
752 $node->lastseen($main::systime);
754 push @new, $node->add_user($r);
756 # add this station to the user database, if required
757 $call =~ s/-\d+$//o; # remove ssid for users
758 my $user = DXUser->get_current($call);
759 $user = DXUser->new($call) if !$user;
760 $user->homenode($node->call) if !$user->homenode;
761 $user->node($node->call);
762 $user->lastin($main::systime) unless DXChannel->get($call);
765 $self->route_pc16($origin, $line, $node, @new) if @new;
779 eph_del_regex("^PC16\\^$ncall.*$ucall");
781 # do I want users from this channel?
782 unless ($self->user->wantpc16) {
783 dbg("PCPROT: don't send users to $self->{call}") if isdbg('chanerr');
786 if ($ncall eq $main::mycall) {
787 dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
791 # do we believe this call?
792 unless ($ncall eq $self->{call} || $self->is_believed($ncall)) {
793 dbg("PCPROT: We don't believe $ncall on $self->{call}");
797 my $uref = Route::User::get($ucall);
799 dbg("PCPROT: Route::User $ucall not in config") if isdbg('chanerr');
801 my $node = Route::Node::get($ncall);
803 dbg("PCPROT: Route::Node $ncall not in config") if isdbg('chanerr');
806 return unless $node && $uref;
810 if ($self->in_filter_route($node)) {
812 if (eph_dup($line)) {
813 dbg("PCPROT: dup PC17 detected") if isdbg('chanerr');
816 push @new, $node->del_user($uref);
819 $self->route_pc17($origin, $line, $node, $uref) if @new;
821 # get rid of orphaned users;
832 $self->state('init');
834 # record the type and version offered
835 if ($_[1] =~ /DXSpider Version: (\d+\.\d+) Build: (\d+\.\d+)/) {
836 $self->version(53 + $1);
837 $self->user->version(53 + $1);
838 $self->build(0 + $2);
839 $self->user->build(0 + $2);
840 unless ($self->is_spider) {
841 $self->user->sort('S');
846 $self->version(50.0);
847 $self->version($_[2] / 100) if $_[2] && $_[2] =~ /^\d+$/;
848 $self->user->version($self->version);
850 $self->newroute( $_[1] =~ /NewRoute/ );
852 # first clear out any nodes on this dxchannel
853 my $node = Route::Node::get($self->{call}) ;
855 foreach my $n ($node->nodes) {
856 next if $n eq $main::mycall;
857 next if $n eq $self->{call};
858 my $nref = Route::Node::get($n);
859 push @rout, $node->remove_route($nref, $self) if $nref;
861 $self->route_pc21($origin, $line, @rout) if @rout;
866 # send the new config
867 $self->send_local_config();
871 # incoming cluster list
880 my $newline = "PC19^";
882 if (eph_dup($line)) {
883 dbg("PCPROT: dup PC19 detected") if isdbg('chanerr');
890 # first get the INTERFACE node
891 my $parent = Route::Node::get($self->{call});
893 dbg("DXPROT: my parent $self->{call} has disappeared");
901 for ($i = 1; $i < $#_-1; $i += 4) {
903 my $call = uc $_[$i+1];
906 next unless defined $here && defined $conf && is_callsign($call);
908 # check for sane parameters
909 # $ver = 5000 if $ver eq '0000';
910 next if $ver < 5000; # only works with version 5 software
911 next if length $call < 3; # min 3 letter callsigns
912 next if $call eq $main::mycall;
914 # add this station to the user database, if required (don't remove SSID from nodes)
915 my $user = DXUser->get_current($call);
917 $user = DXUser->new($call);
919 $user->priv(1); # I have relented and defaulted nodes
921 $user->homenode($call);
924 $user->wantroutepc19(1) unless defined $user->wantroutepc19;
925 $user->lastin($main::systime) unless DXChannel->get($call);
928 # do we believe this call?
929 unless ($call eq $self->{call} || $self->is_believed($call)) {
930 my $pt = $user->lastping || 0;
931 if ($pt+$investigation_int < $main::systime && !Investigate::get($call)) {
932 my $iref = Investigate->new($call);
933 $iref->version($ver);
936 dbg("PCPROT: We don't believe $call on $self->{call}");
940 eph_del_regex("^PC(?:21\\^$call|17\\^[^\\^]+\\^$call)");
942 my $r = Route::Node::get($call) || Route::Node->new($call);
946 $r->lastseen($main::systime);
948 if ($self->in_filter_route($r)) {
949 push @new, $parent->link_node($r, $self);
953 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
954 my $mref = DXMsg::get_busy($call);
955 $mref->stop_msg($call) if $mref;
958 # route out new nodes to legacy nodes
959 $self->route_pc19($origin, $line, @new) if @new;
960 $self->route_pc59('A', 0, $self->{call}, @rout) if @rout;
963 # send local configuration
970 $self->send_local_config();
972 $self->state('normal');
973 $self->{lastping} = 0;
976 # delete a cluster from the list
985 return if $call eq $main::mycall; # don't allow malicious buggers to disconnect me (or ignore loops)!
987 unless ($call eq $self->{call} || $self->is_believed($call)) {
988 dbg("PCPROT: We don't believe $call on $self->{call}");
992 eph_del_regex("^PC1[679].*$call");
994 # if I get a PC21 from the same callsign as self then treat it
995 # as a PC39: I have gone away
996 if ($call eq $self->call) {
997 $self->disconnect(1);
1003 my $parent = Route::Node::get($self->{call});
1005 dbg("DXPROT: my parent $self->{call} has disappeared");
1011 my $node = Route::Node::get($call);
1013 $node->lastseen($main::systime);
1016 return unless $self->in_filter_route($node);
1018 push @new, $node->link_node($parent, $self);
1021 $self->route_pc21($origin, $line, @new) if @new;
1022 $self->route_pc59('D', 0, $self->{call}, @rout) if @rout;
1024 # get rid of orphaned nodes;
1025 $_->delete for @new;
1035 $self->state('normal');
1036 $self->{lastping} = 0;
1047 # route foreign' pc27s
1049 if ($_[8] ne $main::mycall) {
1050 $self->route($_[8], $line);
1055 # only do a rspf check on PC23 (not 27)
1057 return if $rspfcheck and !$self->rspfcheck(1, $_[8], $_[7])
1061 my $d = cltounix($_[1], sprintf("%02d18Z", $_[2]));
1062 my $sfi = unpad($_[3]);
1063 my $k = unpad($_[4]);
1064 my $i = unpad($_[5]);
1065 my ($r) = $_[6] =~ /R=(\d+)/;
1067 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $_[2] < 0 || $_[2] > 23) {
1068 dbg("PCPROT: WWV Date ($_[1] $_[2]) out of range") if isdbg('chanerr');
1071 if (Geomag::dup($d,$sfi,$k,$i,$_[6])) {
1072 dbg("PCPROT: Dup WWV Spot ignored\n") if isdbg('chanerr');
1075 $_[7] =~ s/-\d+$//o; # remove spotter's ssid
1077 my $wwv = Geomag::update($d, $_[2], $sfi, $k, $i, @_[6..8], $r);
1081 $rep = Local::wwv($self, $_[1], $_[2], $sfi, $k, $i, @_[6..8], $r);
1083 # dbg("Local::wwv2 error $@") if isdbg('local') if $@;
1086 # DON'T be silly and send on PC27s!
1087 return if $pcno == 27;
1089 # broadcast to the eager world
1090 send_wwv_spot($self, $line, $d, $_[2], $sfi, $k, $i, @_[6..8]);
1100 my $call = uc $_[1];
1102 $nref = Route::Node::get($call);
1103 $uref = Route::User::get($call);
1104 return unless $nref || $uref; # if we don't know where they are, it's pointless sending it on
1106 if (eph_dup($line)) {
1107 dbg("PCPROT: Dup PC24 ignored\n") if isdbg('chanerr');
1111 $nref->here($_[2]) if $nref;
1112 $uref->here($_[2]) if $uref;
1113 my $ref = $nref || $uref;
1114 return unless $self->in_filter_route($ref);
1116 $self->route_pc24($origin, $line, $ref, $_[3]);
1126 if ($_[1] ne $main::mycall) {
1127 $self->route($_[1], $line);
1130 if ($_[2] eq $main::mycall) {
1131 dbg("PCPROT: Trying to merge to myself, ignored") if isdbg('chanerr');
1135 Log('DXProt', "Merge request for $_[3] spots and $_[4] WWV from $_[2]");
1139 my @in = reverse Spot::search(1, undef, undef, 0, $_[3]);
1142 $self->send(pc26(@{$in}[0..4], $_[2]));
1148 my @in = reverse Geomag::search(0, $_[4], time, 1);
1151 $self->send(pc27(@{$in}[0..5], $_[2]));
1156 sub handle_26 {goto &handle_11}
1157 sub handle_27 {goto &handle_23}
1159 # mail/file handling
1166 if ($_[1] eq $main::mycall) {
1168 my $sub = "DXMsg::handle_$pcno";
1171 $self->route($_[1], $line) unless $self->is_clx;
1175 sub handle_29 {goto &handle_28}
1176 sub handle_30 {goto &handle_28}
1177 sub handle_31 {goto &handle_28}
1178 sub handle_32 {goto &handle_28}
1179 sub handle_33 {goto &handle_28}
1187 if (eph_dup($line, $eph_pc34_restime)) {
1188 dbg("PCPROT: dupe PC34, ignored") if isdbg('chanerr');
1190 $self->process_rcmd($_[1], $_[2], $_[2], $_[3]);
1194 # remote command replies
1201 eph_del_regex("^PC35\\^$_[2]\\^$_[1]\\^");
1202 $self->process_rcmd_reply($_[1], $_[2], $_[1], $_[3]);
1205 sub handle_36 {goto &handle_34}
1214 if ($_[1] eq $main::mycall) {
1216 my $sub = "DXDb::handle_$pcno";
1219 $self->route($_[1], $line) unless $self->is_clx;
1223 # node connected list from neighbour
1232 # incoming disconnect
1239 if ($_[1] eq $self->{call}) {
1240 $self->disconnect(1);
1242 dbg("PCPROT: came in on wrong channel") if isdbg('chanerr');
1246 sub handle_40 {goto &handle_28}
1258 $l =~ s/[\x00-\x20\x7f-\xff]+//g; # remove all funny characters and spaces for dup checking
1259 if (eph_dup($l, $eph_info_restime)) {
1260 dbg("PCPROT: dup PC41, ignored") if isdbg('chanerr');
1264 # input filter if required
1265 # my $ref = Route::get($call) || Route->new($call);
1266 # return unless $self->in_filter_route($ref);
1268 if ($_[3] eq $_[2] || $_[3] =~ /^\s*$/) {
1269 dbg('PCPROT: invalid value') if isdbg('chanerr');
1273 # add this station to the user database, if required
1274 my $user = DXUser->get_current($call);
1275 $user = DXUser->new($call) unless $user;
1279 } elsif ($_[2] == 2) {
1281 } elsif ($_[2] == 3) {
1282 if (is_latlong($_[3])) {
1283 my ($lat, $long) = DXBearing::stoll($_[3]);
1286 $user->qra(DXBearing::lltoqra($lat, $long));
1288 dbg('PCPROT: not a valid lat/long') if isdbg('chanerr');
1291 } elsif ($_[2] == 4) {
1292 $user->homenode($_[3]);
1293 } elsif ($_[2] == 5) {
1294 if (is_qra(uc $_[3])) {
1295 my ($lat, $long) = DXBearing::qratoll(uc $_[3]);
1298 $user->qra(uc $_[3]);
1300 dbg('PCPROT: not a valid QRA locator') if isdbg('chanerr');
1304 $user->lastoper($main::systime); # to cut down on excessive for/opers being generated
1307 unless ($self->{isolate}) {
1308 DXChannel::broadcast_nodes($line, $self); # send it to everyone but me
1311 # perhaps this IS what we want after all
1312 # $self->route_pc41($ref, $call, $_[2], $_[3], $_[4]);
1315 sub handle_42 {goto &handle_28}
1319 sub handle_44 {goto &handle_37}
1320 sub handle_45 {goto &handle_37}
1321 sub handle_46 {goto &handle_37}
1322 sub handle_47 {goto &handle_37}
1323 sub handle_48 {goto &handle_37}
1325 # message and database
1333 if (eph_dup($line)) {
1334 dbg("PCPROT: Dup PC49 ignored\n") if isdbg('chanerr');
1338 if ($_[1] eq $main::mycall) {
1339 DXMsg::handle_49($self, @_);
1341 $self->route($_[1], $line) unless $self->is_clx;
1345 # keep alive/user list
1354 my $node = Route::Node::get($call);
1356 return unless $node->call eq $self->{call};
1357 $node->usercount($_[2]);
1359 # input filter if required
1360 return unless $self->in_filter_route($node);
1362 $self->route_pc50($origin, $line, $node, $_[2], $_[3]) unless eph_dup($line);
1366 # incoming ping requests/answers
1379 if ($to eq $main::mycall) {
1381 $self->send(pc51($from, $to, '0'));
1383 # it's a reply, look in the ping list for this one
1384 my $ref = $pings{$from};
1386 my $tochan = DXChannel->get($from);
1388 my $r = shift @$ref;
1389 my $dxchan = DXChannel->get($r->{call});
1390 next unless $dxchan;
1391 my $t = tv_interval($r->{t}, [ gettimeofday ]);
1392 if ($dxchan->is_user) {
1393 my $s = sprintf "%.2f", $t;
1394 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
1395 $dxchan->send($dxchan->msg('pingi', $from, $s, $ave))
1396 } elsif ($dxchan->is_node) {
1398 my $nopings = $tochan->user->nopings || $obscount;
1399 push @{$tochan->{pingtime}}, $t;
1400 shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
1402 # cope with a missed ping, this means you must set the pingint large enough
1403 if ($t > $tochan->{pingint} && $t < 2 * $tochan->{pingint} ) {
1404 $t -= $tochan->{pingint};
1407 # calc smoothed RTT a la TCP
1408 if (@{$tochan->{pingtime}} == 1) {
1409 $tochan->{pingave} = $t;
1411 $tochan->{pingave} = $tochan->{pingave} + (($t - $tochan->{pingave}) / 6);
1413 my $rref = Route::Node::get($tochan->{call});
1414 $rref->pingtime($tochan->{pingave}) if $rref;
1415 $tochan->{nopings} = $nopings; # pump up the timer
1416 } elsif (my $rref = Route::Node::get($r->{call})) {
1417 if (defined $rref->pingtime) {
1418 $rref->pingtime($rref->pingtime + (($t - $rref->pingtime) / 6));
1420 $rref->pingtime($t);
1428 if (eph_dup($line)) {
1429 dbg("PCPROT: dup PC51 detected") if isdbg('chanerr');
1432 # route down an appropriate thingy
1433 $self->route($to, $line);
1437 # New style routing handler
1445 my ($sort, $hextime, $ncall) = @_[1,2,3];
1446 if ($ncall eq $main::mycall) {
1447 dbg("PCPROT: ignoring PC59 for me") if isdbg('chan');
1451 # do this once for filtering with a throwaway routing entry if a new node
1452 my $fnode = Route::Node::get($ncall) || Route::new($ncall);
1453 return unless $self->in_filter_route($fnode);
1455 return if eph_dup($line);
1457 # mark myself as NewRoute if I get a PC59
1458 $self->{newroute} = 1 if $ncall eq $self->{call};
1460 # now do it properly for actions
1461 my $node = Route::Node::get($ncall) || Route::Node->new($ncall);
1464 # find each of the entries (or create new ones)
1466 for my $ent (@_[4..$#_]) {
1467 next if $ent =~ /^H\d+$/;
1469 my ($esort, $ehere, $ecall) = unpack "A A A*", $ent;
1472 next unless $esort && defined $ehere && $ecall;
1474 # create user, if required
1475 my $user = DXUser->get_current($ecall);
1477 $user = DXUser->new($ecall);
1479 $user->priv(1); # I have relented and defaulted nodes
1481 $user->homenode($ncall);
1482 $user->node($ncall);
1484 if ($esort eq 'U') {
1485 $ref = Route::User::get($ecall);
1487 # create user, if required
1488 my $user = DXUser->get_current($ecall);
1490 $user = DXUser->new($ecall);
1492 $user->homenode($ncall);
1493 $user->node($ncall);
1496 $ref = Route::User->new($ecall, 0);
1498 } elsif ($esort eq 'N') {
1499 $ref = Route::Node::get($ecall);
1501 # create user, if required
1502 my $user = DXUser->get_current($ecall);
1504 $user = DXUser->new($ecall);
1505 $user->priv(1); # I have relented and defaulted nodes
1508 $user->homenode($ncall);
1509 $user->node($ncall);
1512 $ref = Route::Node->new($ecall, 0);
1515 dbg("DXPROT: unknown entity type '$esort' on $ecall for node $ncall") if isdbg('chan');
1518 $ref->here($ehere); # might as well set this here
1519 $ref->lastseen($main::systime);
1523 # if it is a delete, disconnect all the entries mentioned
1524 # from this node (which is a parent in this context).
1528 for my $ref (@refs) {
1529 next if $ref->call eq $ncall;
1530 next if $ref->call eq $main::mycall;
1531 if ($ref->isa('Route::Node')) {
1532 push @delnode, $node->unlink_node($ref, $self);
1533 } elsif ($ref->isa('Route::User')) {
1534 push @deluser, $node->del_user($ref);
1539 # if it is an add, connect all the entries
1543 for my $ref (@refs) {
1544 next if $ref->call eq $ncall;
1545 next if $ref->call eq $main::mycall;
1546 if ($ref->isa('Route::Node')) {
1547 my $new = $node->link_node($ref, $self);
1548 push @addnode, $new if $new;
1549 } elsif ($ref->isa('Route::User')) {
1550 push @adduser, $node->del_user($ref);
1555 # if it is a configure, unlink all the nodes and users that
1556 # are not in @refs but are in the node, then add all the
1557 # nodes and users that are @refs but not in the node.
1564 for my $r (map {Route::Node::get($_)} $node->nodes) {
1566 next if $r->call eq $ncall;
1567 next if $r->call eq $main::mycall;
1568 push @dn, $r unless grep $_->call eq $r->call, @refs;
1570 for my $r (map {Route::User::get($_)} $node->users) {
1572 push @du, $r unless grep $_->call eq $r->call, @refs;
1576 next if $r->call eq $ncall;
1577 next if $r->call eq $main::mycall;
1578 if ($r->isa('Route::Node')) {
1579 push @an, $r unless grep $r->call eq $_, $node->nodes;
1580 } elsif ($r->isa('Route::User')) {
1581 push @au, $r unless grep $r->call eq $_, $node->users;
1584 push @delnode, $node->unlink_node($_, $self) for @dn;
1585 push @deluser, $node->del_user($_) for @du;
1586 push @addnode, $node->link_node($_, $self) for @an;
1587 push @adduser, $node->add_user($_) for @au;
1590 $self->route_pc21($origin, $line, @delnode) if @delnode;
1591 $self->route_pc19($origin, $line, @addnode) if @addnode;
1592 $self->route_pc17($origin, $line, @deluser) if @deluser;
1593 $self->route_pc16($origin, $line, @adduser) if @adduser;
1595 $self->route_pc59($sort, $hextime, $ncall, @refs) if @refs;
1596 $_->delete for @delnode, @deluser;
1600 # dunno but route it
1608 if ($call ne $main::mycall) {
1609 $self->route($call, $line);
1623 my $d = cltounix($call, sprintf("%02d18Z", $_[2]));
1624 if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $_[2] < 0 || $_[2] > 23) {
1625 dbg("PCPROT: WCY Date ($call $_[2]) out of range") if isdbg('chanerr');
1628 @_ = map { unpad($_) } @_;
1630 dbg("PCPROT: Dup WCY Spot ignored\n") if isdbg('chanerr');
1634 my $wcy = WCY::update($d, @_[2..12]);
1638 $rep = Local::wcy($self, @_[1..12]);
1640 # dbg("Local::wcy error $@") if isdbg('local') if $@;
1643 # broadcast to the eager world
1644 send_wcy_spot($self, $line, $d, @_[2..12]);
1647 # remote commands (incoming)
1654 $self->process_rcmd($_[1], $_[2], $_[3], $_[4]);
1657 # remote command replies
1664 $self->process_rcmd_reply($_[1], $_[2], $_[3], $_[4]);
1667 # if get here then rebroadcast the thing with its Hop count decremented (if
1668 # there is one). If it has a hop count and it decrements to zero then don't
1671 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
1682 if (eph_dup($line)) {
1683 dbg("PCPROT: Ephemeral dup, dropped") if isdbg('chanerr');
1685 unless ($self->{isolate}) {
1686 DXChannel::broadcast_nodes($line, $self) if $line =~ /\^H\d+\^?~?$/; # send it to everyone but me
1692 # This is called from inside the main cluster processing loop and is used
1693 # for despatching commands that are doing some long processing job
1698 my @dxchan = DXChannel->get_all();
1702 # send out a pc50 on EVERY channel all at once
1703 if ($t >= $last_pc50 + $DXProt::pc50_interval) {
1704 $pc50s = pc50($main::me, scalar DXChannel::get_all_users);
1709 foreach $dxchan (@dxchan) {
1710 next unless $dxchan->is_node();
1711 next if $dxchan == $main::me;
1714 $dxchan->send($pc50s) if $pc50s;
1716 # send a ping out on this channel
1717 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
1718 if ($dxchan->{nopings} <= 0) {
1719 $dxchan->disconnect;
1721 addping($main::mycall, $dxchan->call);
1722 $dxchan->{nopings} -= 1;
1723 $dxchan->{lastping} = $t;
1729 if ($t - $last10 >= 10) {
1730 # clean out ephemera
1737 if ($main::systime - 3600 > $last_hour) {
1738 $last_hour = $main::systime;
1743 # finish up a pc context
1747 # some active measures
1755 my @dxchan = DXChannel->get_all();
1758 # send it if it isn't the except list and isn't isolated and still has a hop count
1759 # taking into account filtering and so on
1760 foreach $dxchan (@dxchan) {
1761 next if $dxchan == $main::me;
1762 next if $dxchan == $self && $self->is_node;
1763 $dxchan->dx_spot($line, $self->{isolate}, @_, $self->{call});
1771 my $isolate = shift;
1772 my ($filter, $hops);
1774 if ($self->{spotsfilter}) {
1775 ($filter, $hops) = $self->{spotsfilter}->it(@_);
1776 return unless $filter;
1778 send_prot_line($self, $filter, $hops, $isolate, $line);
1783 my ($self, $filter, $hops, $isolate, $line) = @_;
1789 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1791 $routeit = adjust_hops($self, $line); # adjust its hop count by node name
1792 return unless $routeit;
1795 $self->send($routeit);
1797 $self->send($routeit) unless $self->{isolate} || $isolate;
1806 my @dxchan = DXChannel->get_all();
1808 my @dxcc = ((Prefix::cty_data($_[6]))[0..2], (Prefix::cty_data($_[7]))[0..2]);
1810 # send it if it isn't the except list and isn't isolated and still has a hop count
1811 # taking into account filtering and so on
1812 foreach $dxchan (@dxchan) {
1813 next if $dxchan == $main::me;
1814 next if $dxchan == $self && $self->is_node;
1816 my ($filter, $hops);
1818 $dxchan->wwv($line, $self->{isolate}, @_, $self->{call}, @dxcc);
1826 my $isolate = shift;
1827 my ($filter, $hops);
1829 if ($self->{wwvfilter}) {
1830 ($filter, $hops) = $self->{wwvfilter}->it(@_);
1831 return unless $filter;
1833 send_prot_line($self, $filter, $hops, $isolate, $line)
1840 my @dxchan = DXChannel->get_all();
1842 my @dxcc = ((Prefix::cty_data($_[10]))[0..2], (Prefix::cty_data($_[11]))[0..2]);
1844 # send it if it isn't the except list and isn't isolated and still has a hop count
1845 # taking into account filtering and so on
1846 foreach $dxchan (@dxchan) {
1847 next if $dxchan == $main::me;
1848 next if $dxchan == $self;
1850 $dxchan->wcy($line, $self->{isolate}, @_, $self->{call}, @dxcc);
1858 my $isolate = shift;
1859 my ($filter, $hops);
1861 if ($self->{wcyfilter}) {
1862 ($filter, $hops) = $self->{wcyfilter}->it(@_);
1863 return unless $filter;
1865 send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
1873 my @dxchan = DXChannel->get_all();
1877 my $text = unpad($_[2]);
1879 if ($_[3] eq '*') { # sysops
1881 } elsif ($_[3] gt ' ') { # speciality list handling
1882 my ($name) = split /\./, $_[3];
1883 $target = "$name"; # put the rest in later (if bothered)
1890 $target = "ALL" if !$target;
1893 # obtain country codes etc
1894 my @a = Prefix::cty_data($_[0]);
1895 my @b = Prefix::cty_data($_[4]);
1896 if ($self->{inannfilter}) {
1897 my ($filter, $hops) =
1898 $self->{inannfilter}->it(@_, $self->{call},
1900 @b[0..2], $a[3], $b[3]);
1902 dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
1907 if (AnnTalk::dup($_[0], $_[1], $_[2])) {
1908 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
1912 Log('ann', $target, $_[0], $text);
1914 # send it if it isn't the except list and isn't isolated and still has a hop count
1915 # taking into account filtering and so on
1916 foreach $dxchan (@dxchan) {
1917 next if $dxchan == $main::me;
1918 next if $dxchan == $self && $self->is_node;
1919 $dxchan->announce($line, $self->{isolate}, $to, $target, $text, @_, $self->{call},
1920 @a[0..2], @b[0..2]);
1929 $msgid = 1 if $msgid > 999;
1938 my @dxchan = DXChannel->get_all();
1941 my $text = unpad($_[2]);
1944 # munge the group and recast the line if required
1945 if ($target =~ s/\.LST$//) {
1949 # obtain country codes etc
1950 my @a = Prefix::cty_data($_[0]);
1951 my @b = Prefix::cty_data($_[4]);
1952 if ($self->{inannfilter}) {
1953 my ($filter, $hops) =
1954 $self->{inannfilter}->it(@_, $self->{call},
1956 @b[0..2], $a[3], $b[3]);
1958 dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
1963 if (AnnTalk::dup($_[0], $_[1], $_[2], $chatdupeage)) {
1964 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
1969 Log('chat', $target, $_[0], $text);
1971 # send it if it isn't the except list and isn't isolated and still has a hop count
1972 # taking into account filtering and so on
1973 foreach $dxchan (@dxchan) {
1974 my $is_ak1a = $dxchan->is_ak1a;
1976 if ($dxchan->is_node) {
1977 next if $dxchan == $main::me;
1978 next if $dxchan == $self;
1979 next unless $dxchan->is_spider || $is_ak1a;
1980 next if $target eq 'LOCAL';
1981 if (!$ak1a_line && $is_ak1a) {
1982 $ak1a_line = DXProt::pc12($_[0], $text, $_[1], "$target.LST");
1986 $dxchan->chat($is_ak1a ? $ak1a_line : $line, $self->{isolate}, $target, $_[1],
1987 $text, @_, $self->{call}, @a[0..2], @b[0..2]);
1995 my $isolate = shift;
1999 my ($filter, $hops);
2001 if ($self->{annfilter}) {
2002 ($filter, $hops) = $self->{annfilter}->it(@_);
2003 return unless $filter;
2005 send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
2014 sub send_local_config
2022 dbg('DXProt::send_local_config') if isdbg('trace');
2024 if ($self->{newroute}) {
2025 my @nodes = $self->{isolate} ? ($main::routeroot) : grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
2026 my @users = DXChannel::get_all_users();
2027 @localnodes = map { Route::Node::get($_->{call}) } @nodes;
2028 my @localusers = map { Route::User::get($_->{call}) } @users;
2029 $self->send_route($main::mycall, \&pc59, @nodes+@users+4, 'C', 0, $main::mycall, $main::routeroot, @localnodes, @localusers);
2032 if ($self->{isolate}) {
2033 @localnodes = ( $main::routeroot );
2034 $self->send_route($main::mycall, \&pc19, 1, $main::routeroot);
2036 # create a list of all the nodes that are not connected to this connection
2037 # and are not themselves isolated, this to make sure that isolated nodes
2038 # don't appear outside of this node
2040 # send locally connected nodes
2041 my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
2042 @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan if @dxchan;
2043 $self->send_route($main::mycall, \&pc19, scalar(@localnodes)+1, $main::routeroot, @localnodes);
2046 my @rawintcalls = map { $_->nodes } @localnodes if @localnodes;
2048 for $node (@rawintcalls) {
2049 push @intcalls, $node unless grep $node eq $_, @intcalls;
2051 my $ref = Route::Node::get($self->{call});
2052 my @rnodes = $ref->nodes;
2053 for $node (@intcalls) {
2054 push @remotenodes, Route::Node::get($node) unless grep $node eq $_, @rnodes, @remotenodes;
2056 $self->send_route($main::mycall, \&pc19, scalar(@remotenodes), @remotenodes);
2059 # get all the users connected on the above nodes and send them out
2060 foreach $node ($main::routeroot, @localnodes, @remotenodes) {
2062 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
2063 $self->send_route($main::mycall, \&pc16, 1, $node, @rout) if @rout && $self->user->wantsendpc16;
2065 dbg("sent a null value") if isdbg('chanerr');
2072 # route a message down an appropriate interface for a callsign
2074 # is called route(to, pcline);
2078 my ($self, $call, $line) = @_;
2080 if (ref $self && $call eq $self->{call}) {
2081 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
2085 # always send it down the local interface if available
2086 my $dxchan = DXChannel->get($call);
2088 my $cl = Route::get($call);
2089 $dxchan = $cl->bestdxchan if $cl;
2091 if (ref $self && $dxchan eq $self) {
2092 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
2098 my $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
2100 $dxchan->send($routeit) unless $dxchan == $main::me;
2103 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
2108 # obtain the hops from the list for this callsign and pc no
2114 my $hops = $DXProt::hopcount{$pcno};
2115 $hops = $DXProt::def_hopcount if !$hops;
2120 # adjust the hop count on a per node basis using the user loadable
2121 # hop table if available or else decrement an existing one
2128 my $call = $self->{call};
2131 if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
2132 my ($pcno) = $s =~ /^PC(\d\d)/o;
2133 confess "$call called adjust_hops with '$s'" unless $pcno;
2134 my $ref = $nodehops{$call} if %nodehops;
2136 my $newhops = $ref->{$pcno};
2137 return "" if defined $newhops && $newhops == 0;
2138 $newhops = $ref->{default} unless $newhops;
2139 return "" if defined $newhops && $newhops == 0;
2140 $newhops = $hops if !$newhops;
2141 $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
2143 # simply decrement it
2145 return "" if !$hops;
2146 $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
2158 return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
2159 do "$main::data/hop_table.pl";
2165 # add a ping request to the ping queues
2168 my ($from, $to) = @_;
2169 my $ref = $pings{$to} || [];
2172 $r->{t} = [ gettimeofday ];
2173 route(undef, $to, pc51($to, $main::mycall, 1));
2176 my $u = DXUser->get_current($to);
2177 $u->lastping($main::systime) if $u;
2182 my ($self, $tonode, $fromnode, $user, $cmd) = @_;
2183 if ($tonode eq $main::mycall) {
2184 my $ref = DXUser->get_current($fromnode);
2185 my $cref = Route::Node::get($fromnode);
2186 Log('rcmd', 'in', $ref->{priv}, $fromnode, $cmd);
2187 if ($cmd !~ /^\s*rcmd/i && $cref && $ref && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS!
2188 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
2189 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
2190 my $oldpriv = $self->{priv};
2191 $self->{priv} = $ref->{priv}; # assume the user's privilege level
2192 my @in = (DXCommandmode::run_cmd($self, $cmd));
2193 $self->{priv} = $oldpriv;
2194 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @in);
2195 delete $self->{remotecmd};
2197 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
2200 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "your attempt is logged, Tut tut tut...!");
2203 my $ref = DXUser->get_current($tonode);
2204 if ($ref && $ref->is_clx) {
2205 $self->route($tonode, pc84($fromnode, $tonode, $user, $cmd));
2207 $self->route($tonode, pc34($fromnode, $tonode, $cmd));
2212 sub process_rcmd_reply
2214 my ($self, $tonode, $fromnode, $user, $line) = @_;
2215 if ($tonode eq $main::mycall) {
2216 my $s = $rcmds{$fromnode};
2218 my $dxchan = DXChannel->get($s->{call});
2219 my $ref = $user eq $tonode ? $dxchan : (DXChannel->get($user) || $dxchan);
2220 $ref->send($line) if $ref;
2221 delete $rcmds{$fromnode} if !$dxchan;
2223 # send unsolicited ones to the sysop
2224 my $dxchan = DXChannel->get($main::myalias);
2225 $dxchan->send($line) if $dxchan;
2228 my $ref = DXUser->get_current($tonode);
2229 if ($ref && $ref->is_clx) {
2230 $self->route($tonode, pc85($fromnode, $tonode, $user, $line));
2232 $self->route($tonode, pc35($fromnode, $tonode, $line));
2241 my $fromnode = shift;
2246 Log('rcmd', 'out', $fromnode, $line);
2247 if ($self->is_clx) {
2248 $self->send(pc85($main::mycall, $fromnode, $user, "$main::mycall:$line"));
2250 $self->send(pc35($main::mycall, $fromnode, "$main::mycall:$line"));
2255 # add a rcmd request to the rcmd queues
2258 my ($self, $to, $cmd) = @_;
2261 $r->{call} = $self->{call};
2262 $r->{t} = $main::systime;
2266 my $ref = Route::Node::get($to);
2267 my $dxchan = $ref->bestdxchan;
2268 if ($dxchan && $dxchan->is_clx) {
2269 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
2271 route(undef, $to, pc34($main::mycall, $to, $cmd));
2278 my $pc39flag = shift;
2279 my $call = $self->call;
2281 return if $self->{disconnecting}++;
2283 unless ($pc39flag && $pc39flag == 1) {
2284 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
2287 # get rid of any PC16/17/19
2288 eph_del_regex("^PC1[679]*$call");
2290 # do routing stuff, remove me from routing table
2291 my $node = Route::Node::get($call);
2295 # remove the route from this node and return a list
2296 # of nodes that have become orphanned as a result.
2297 push @rout, $main::routeroot->remove_route($node, $self);
2299 # remove all my ephemera as well
2302 eph_del_regex("^PC1[679].*$c");
2306 # unbusy and stop and outgoing mail
2307 my $mref = DXMsg::get_busy($call);
2308 $mref->stop_msg($call) if $mref;
2310 # broadcast to all other nodes that all the nodes connected to via me are gone
2311 unless ($pc39flag && $pc39flag == 2) {
2312 $self->route_pc21($main::mycall, undef, @rout) if @rout;
2313 $self->route_pc59('D', 0, $main::mycall, $node);
2316 # delete all the unwanted nodes
2317 $_->delete for @rout;
2319 # remove outstanding pings
2320 delete $pings{$call};
2322 # I was the last node visited
2323 $self->user->node($main::mycall);
2325 # send info to all logged in thingies
2326 $self->tell_login('logoutn');
2328 Log('DXProt', $call . " Disconnected");
2330 $self->SUPER::disconnect;
2335 # send a talk message to this thingy
2339 my ($self, $from, $to, $via, $line, $origin) = @_;
2341 $line =~ s/\^/\\5E/g; # remove any ^ characters
2342 $self->send(DXProt::pc10($from, $to, $via, $line, $origin));
2343 Log('talk', $to, $from, $via?$via:$self->call, $line) unless $origin && $origin ne $main::mycall;
2346 # send it if it isn't the except list and isn't isolated and still has a hop count
2347 # taking into account filtering and so on
2353 my $generate = shift;
2354 my $no = shift; # the no of things to filter on
2356 my ($filter, $hops);
2359 for (; @_ && $no; $no--) {
2362 # deal with non routing parameters
2363 unless (ref $r && $r->isa('Route')) {
2368 if (!$self->{isolate} && $self->{routefilter}) {
2371 ($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});
2375 dbg("DXPROT: $self->{call}/" . $r->call . " rejected by output filter") if isdbg('chanerr');
2378 dbg("was sent a null value") if isdbg('chanerr');
2381 push @rin, $r unless $self->{isolate} && $r->call ne $main::mycall;
2385 foreach my $line (&$generate(@rin, @_)) {
2388 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
2390 $routeit = adjust_hops($self, $line); # adjust its hop count by node name
2391 next unless $routeit;
2394 $self->send($routeit);
2403 my $generate = shift;
2405 my @dxchan = DXChannel::get_all_nodes();
2408 unless ($self->{isolate}) {
2409 foreach $dxchan (@dxchan) {
2410 next if $dxchan == $self;
2411 next if $dxchan == $main::me;
2412 next unless $dxchan->isa('DXProt');
2413 next if ($generate == \&pc16 || $generate==\&pc17) && !$dxchan->user->wantsendpc16;
2414 if ($dxchan->{newroute}) {
2415 next if ($generate == \&pc19 || $generate==\&pc21);
2417 next if ($generate == \&pc19 || $generate==\&pc21) && !$dxchan->user->wantroutepc19;
2418 next if ($generate == \&pc59);
2421 $dxchan->send_route($origin, $generate, @_);
2429 return unless $self->user->wantpc16;
2432 broadcast_route($self, $origin, \&pc16, $line, 1, @_);
2438 return unless $self->user->wantpc16;
2441 broadcast_route($self, $origin, \&pc17, $line, 1, @_);
2449 broadcast_route($self, $origin, \&pc19, $line, scalar @_, @_);
2457 broadcast_route($self, $origin, \&pc21, $line, scalar @_, @_);
2465 broadcast_route($self, $origin, \&pc24, $line, 1, @_);
2473 broadcast_route($self, $origin, \&pc41, $line, 1, @_);
2481 broadcast_route($self, $origin, \&pc50, $line, 1, @_);
2490 broadcast_route($self, $origin, \&pc59, $line, scalar @_, @_);
2497 my ($filter, $hops) = (1, 1);
2499 if ($self->{inroutefilter}) {
2500 ($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);
2501 dbg("PCPROT: $self->{call}/" . $r->call . ' rejected by in_filter_route') if !$filter && isdbg('chanerr');
2509 my $t = shift || $eph_restime;
2513 $s =~ s/\^H\d\d?\^?\~?$//;
2514 $r = 1 if exists $eph{$s}; # pump up the dup if it keeps circulating
2515 $eph{$s} = $main::systime + $t;
2523 while (($key, $val) = each %eph) {
2524 if ($key =~ m{$regex}) {
2534 while (($key, $val) = each %eph) {
2535 if ($main::systime >= $val) {
2546 while (($key, $val) = each %eph) {
2547 push @out, $key, $val;
2554 goto &DXCommandmode::run_cmd;