3 # This module impliments the protocal mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
40 use vars qw($pc11_max_age $pc23_max_age $last_pc50 $eph_restime $eph_info_restime $eph_pc34_restime
41 $last_hour $last10 %eph %pings %rcmds $ann_to_talk
42 $pingint $obscount %pc19list $chatdupeage $chatimportfn
43 $investigation_int $pc19_version $myprot_version
44 %nodehops $baddx $badspotter $badnode $censorpc
45 $allowzero $decode_dk0wcy $send_opernam @checklist
46 $eph_pc15_restime $pc92_update_period $pc92_obs_timeout
47 %pc92_find $pc92_find_timeout $pc92_short_update_period
48 $next_pc92_obs_timeout $pc92_slug_changes $last_pc92_slug
51 $pc11_max_age = 1*3600; # the maximum age for an incoming 'real-time' pc11
52 $pc23_max_age = 1*3600; # the maximum age for an incoming 'real-time' pc23
54 $last_hour = time; # last time I did an hourly periodic update
55 %rcmds = (); # outstanding rcmd requests outbound
56 %nodehops = (); # node specific hop control
57 %pc19list = (); # list of outstanding PC19s that haven't had PC16s on them
59 $censorpc = 1; # Do a BadWords::check on text fields and reject things
60 # loads of 'bad things'
61 $baddx = new DXHash "baddx";
62 $badspotter = new DXHash "badspotter";
63 $badnode = new DXHash "badnode";
64 $last10 = $last_pc50 = time;
67 $eph_info_restime = 60*60;
68 $eph_pc15_restime = 6*60;
69 $eph_pc34_restime = 30;
72 $chatdupeage = 20 * 60;
73 $chatimportfn = "$main::root/chat_import";
74 $investigation_int = 12*60*60; # time between checks to see if we can see this node
75 $pc19_version = 5454; # the visible version no for outgoing PC19s generated from pc59
76 $pc92_update_period = 60*60; # the period between outgoing PC92 C updates
77 $pc92_short_update_period = 15*60; # shorten the update period after a connection
78 %pc92_find = (); # outstanding pc92 find operations
79 $pc92_find_timeout = 30; # maximum time to wait for a reply
80 #$pc92_obs_timeout = $pc92_update_period; # the time between obscount countdowns
81 $pc92_obs_timeout = 60*60; # the time between obscount for incoming countdowns
82 $next_pc92_obs_timeout = $main::systime + 60*60; # the time between obscount countdowns
87 [ qw(i c c m bp bc c) ], # pc10
88 [ qw(i f m d t m c c h) ], # pc11
89 [ qw(i c bm m bm bm p h) ], # pc12
93 undef , # pc16 has to be validated manually
94 [ qw(i c c h) ], # pc17
96 undef , # pc19 has to be validated manually
97 undef , # pc20 no validation
98 [ qw(i c m h) ], # pc21
99 undef , # pc22 no validation
100 [ qw(i d n n n n m c c h) ], # pc23
101 [ qw(i c p h) ], # pc24
102 [ qw(i c c n n) ], # pc25
103 [ qw(i f m d t m c c bc) ], # pc26
104 [ qw(i d n n n n m c c bc) ], # pc27
105 [ qw(i c c m c d t p m bp n p bp bc) ], # pc28
106 [ qw(i c c n m) ], # pc29
107 [ qw(i c c n) ], # pc30
108 [ qw(i c c n) ], # pc31
109 [ qw(i c c n) ], # pc32
110 [ qw(i c c n) ], # pc33
111 [ qw(i c c m) ], # pc34
112 [ qw(i c c m) ], # pc35
113 [ qw(i c c m) ], # pc36
114 [ qw(i c c n m) ], # pc37
115 undef, # pc38 not interested
116 [ qw(i c m) ], # pc39
117 [ qw(i c c m p n) ], # pc40
118 [ qw(i c n m h) ], # pc41
119 [ qw(i c c n) ], # pc42
120 undef, # pc43 don't handle it
121 [ qw(i c c n m m c) ], # pc44
122 [ qw(i c c n m) ], # pc45
123 [ qw(i c c n) ], # pc46
126 [ qw(i c m h) ], # pc49
127 [ qw(i c n h) ], # pc50
128 [ qw(i c c n) ], # pc51
150 [ qw(i d n n n n n n m m m c c h) ], # pc73
161 [ qw(i c c c m) ], # pc84
162 [ qw(i c c c m) ], # pc85
169 [ qw(i c f l)], # pc92
170 [ qw(i c f *m c *c m)], # pc93
173 # use the entry in the check list to check the field list presented
174 # return OK if line NOT in check list (for now)
179 return 0 if $n < 0 || $n > @checklist;
180 my $ref = $checklist[$n];
181 return 0 unless ref $ref;
184 for ($i = 1; $i < @$ref; $i++) {
185 my ($blank, $act) = $$ref[$i] =~ /^(b?)(\w)$/;
186 return 0 unless $act;
187 next if $blank eq 'b' && $_[$i] =~ /^[ \*]$/;
188 next if $blank eq '*' && $_[$i] =~ /^\*$/;
190 return $i unless is_callsign($_[$i]);
191 } elsif ($act eq 'i') {
193 } elsif ($act eq 'm') {
194 return $i unless is_pctext($_[$i]);
195 } elsif ($act eq 'p') {
196 return $i unless is_pcflag($_[$i]);
197 } elsif ($act eq 'f') {
198 return $i unless is_freq($_[$i]);
199 } elsif ($act eq 'n') {
200 return $i unless $_[$i] =~ /^[\d ]+$/;
201 } elsif ($act eq 'h') {
202 return $i unless $_[$i] =~ /^H\d\d?$/;
203 } elsif ($act eq 'd') {
204 return $i unless $_[$i] =~ /^\s*\d+-\w\w\w-[12][90]\d\d$/;
205 } elsif ($act eq 't') {
206 return $i unless $_[$i] =~ /^[012]\d[012345]\dZ$/;
207 } elsif ($act eq 'l') {
208 return $i unless $_[$i] =~ /^[A-Z]$/;
217 my $period = shift || $pc92_update_period;
218 $self->{next_pc92_update} = $main::systime + $period - int rand($period / 4);
219 dbg("ROUTE: update_pc92_next: $self->{call} " . atime($self->{next_pc92_update})) if isdbg('obscount');
224 do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
227 my $user = DXUser->get($main::mycall);
228 die "User $main::mycall not setup or disappeared RTFM" unless $user;
230 $myprot_version += $main::version*100;
231 $main::me = DXProt->new($main::mycall, 0, $user);
232 $main::me->{here} = 1;
233 $main::me->{state} = "indifferent";
234 $main::me->{sort} = 'S'; # S for spider
235 $main::me->{priv} = 9;
236 $main::me->{metric} = 0;
237 $main::me->{pingave} = 0;
238 $main::me->{registered} = 1;
239 $main::me->{version} = $main::version;
240 $main::me->{build} = "$main::subversion.$main::build";
241 $main::me->{do_pc9x} = 1;
242 $main::me->update_pc92_next($pc92_update_period);
246 # obtain a new connection this is derived from dxchannel
251 my $self = DXChannel::alloc(@_);
253 # add this node to the table, the values get filled in later
257 # if we have an entry already, then send a PC21 to all connect
258 # old style connections, because we are about to get the real deal
259 if (my $ref = Route::Node::get($call)) {
260 dbg("ROUTE: $call is already in the routing table, deleting") if isdbg('route');
261 my @rout = $ref->delete;
262 $self->route_pc21($main::mycall, undef, @rout) if @rout;
264 $main::routeroot->add($call, '5000', Route::here(1)) if $call ne $main::mycall;
269 # this is how a pc connection starts (for an incoming connection)
270 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
271 # all the crap that comes between).
274 my ($self, $line, $sort) = @_;
275 my $call = $self->{call};
276 my $user = $self->{user};
279 my $host = $self->{conn}->{peerhost};
280 $host ||= "AGW Port #$self->{conn}->{agwport}" if exists $self->{conn}->{agwport};
283 Log('DXProt', "$call connected from $host");
285 # remember type of connection
286 $self->{consort} = $line;
287 $self->{outbound} = $sort eq 'O';
288 my $priv = $user->priv;
289 $priv = $user->priv(1) unless $priv;
290 $self->{priv} = $priv; # other clusters can always be 'normal' users
291 $self->{lang} = $user->lang || 'en';
292 $self->{isolate} = $user->{isolate};
293 $self->{consort} = $line; # save the connection type
297 # sort out registration
298 $self->{registered} = 1;
300 # get the output filters
301 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
302 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
303 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
304 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
305 $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) unless $self->{isolate} ;
308 # get the INPUT filters (these only pertain to Clusters)
309 $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
310 $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
311 $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
312 $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
313 $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1) unless $self->{isolate};
314 # if there is no route input filter then specify a default one.
315 # obviously this can be changed later by the sysop.
316 if (!$self->{inroutefilter}) {
317 my $dxcc = $self->dxcc;
318 $Route::filterdef->cmd($self, 'route', 'accept', "input by_dxcc $dxcc" );
321 # set unbuffered and no echo
322 $self->send_now('B',"0");
323 $self->send_now('E',"0");
324 $self->conn->echo(0) if $self->conn->can('echo');
326 # ping neighbour node stuff
327 my $ping = $user->pingint;
328 $ping = $pingint unless defined $ping;
329 $self->{pingint} = $ping;
330 $self->{nopings} = $user->nopings || $obscount;
331 $self->{pingtime} = [ ];
332 $self->{pingave} = 999;
333 $self->{metric} ||= 100;
334 $self->{lastping} = $main::systime;
336 # send initialisation string
337 unless ($self->{outbound}) {
341 $self->state('init');
342 $self->{pc50_t} = $main::systime;
344 # send info to all logged in thingies
345 $self->tell_login('loginn');
347 # run a script send the output to the debug file
348 my $script = new Script(lc $call) || new Script('node_default');
349 $script->run($self) if $script;
351 # set next_pc92_update time for this node sooner
352 $self->update_pc92_next($self->{outbound} ? $pc92_short_update_period : $pc92_update_period);
356 # send outgoing 'challenge'
362 $self->send(pc18(($self->{isolate} || !$self->user->wantpc9x) ? "" : " pc9x"));
366 # This is the normal pcxx despatcher
370 my ($self, $line) = @_;
372 if ($line =~ '^<\w+\s' && $main::do_xml) {
373 DXXml::normal($self, $line);
377 my @field = split /\^/, $line;
378 return unless @field;
380 pop @field if $field[-1] eq '~';
382 # print join(',', @field), "\n";
385 # process PC frames, this will fail unless the frame starts PCnn
386 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
387 unless (defined $pcno && $pcno >= 10 && $pcno <= 99) {
388 dbg("PCPROT: unknown protocol") if isdbg('chanerr');
392 # check for and dump bad protocol messages
393 my $n = check($pcno, @field);
395 dbg("PCPROT: bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")") if isdbg('chanerr');
399 # modify the hop count here
400 if ($self != $main::me) {
401 if (my ($hops, $trail) = $line =~ /\^H(\d+)(\^?\~?)?$/) {
405 $line =~ s/\^H(\d+)(\^?\~?)?$/sprintf('^H%d%s', $hops, $trail)/e;
406 $field[-1] = "H$hops";
410 # send it out for processing
411 my $origin = $self->{call};
413 my $sub = "handle_$pcno";
415 if ($self->can($sub)) {
416 $self->$sub($pcno, $line, $origin, @field);
418 $self->handle_default($pcno, $line, $origin, @field);
423 # This is called from inside the main cluster processing loop and is used
424 # for despatching commands that are doing some long processing job
429 my @dxchan = DXChannel::get_all();
433 # send out a pc50 on EVERY channel all at once
434 if ($t >= $last_pc50 + $DXProt::pc50_interval) {
435 $pc50s = pc50($main::me, scalar DXChannel::get_all_users);
440 foreach $dxchan (@dxchan) {
441 next unless $dxchan->is_node;
442 next if $dxchan->handle_xml;
443 next if $dxchan == $main::me;
446 $dxchan->send($pc50s) if $pc50s;
448 # send a ping out on this channel
449 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
450 if ($dxchan->{nopings} <= 0) {
453 DXXml::Ping::add($main::me, $dxchan->call);
454 $dxchan->{nopings} -= 1;
455 $dxchan->{lastping} = $t;
456 $dxchan->{lastping} += $dxchan->{pingint} / 2 unless @{$dxchan->{pingtime}};
462 Investigate::process();
466 if ($t - $last10 >= 10) {
472 if ($main::systime >= $next_pc92_obs_timeout) {
473 time_out_pc92_routes();
474 $next_pc92_obs_timeout = $main::systime + $pc92_obs_timeout;
479 # send out config broadcasts
480 foreach $dxchan (@dxchan) {
481 next unless $dxchan->is_node;
483 # send out a PC92 config record if required for me and
484 # all my non pc9x dependent nodes. But for dependent nodes we only do
485 # this if we have not seen any from anyone else for at least half
486 # of one update period. This should stop quite a bit of excess C
487 # records. Someone will win, it does not really matter who, because
488 # we always believe "us".
489 if ($main::systime >= $dxchan->{next_pc92_update}) {
490 dbg("ROUTE: pc92 broadcast candidate: $dxchan->{call}") if isdbg('obscount');
491 if ($dxchan == $main::me || !$dxchan->{do_pc9x}) {
492 my $ref = Route::Node::get($dxchan->{call});
493 if ($dxchan == $main::me || ($ref && ($ref->measure_pc9x_t($main::systime-$main::systime_daystart)) >= $pc92_update_period/2)) {
494 $dxchan->broadcast_pc92_update($dxchan->{call});
496 $dxchan->update_pc92_next($pc92_update_period - rand($pc92_update_period/4));
502 if ($pc92_slug_changes && $main::systime >= $last_pc92_slug + $pc92_slug_changes) {
503 my ($add, $del) = gen_pc92_changes();
504 $main::me->route_pc92d($main::mycall, undef, $main::routeroot, @$del) if @$del;
505 $main::me->route_pc92a($main::mycall, undef, $main::routeroot, @$add) if @$add;
506 clear_pc92_changes();
510 if ($main::systime - 3600 > $last_hour) {
511 $last_hour = $main::systime;
516 # finish up a pc context
520 # some active measures
528 my @dxchan = DXChannel::get_all();
531 # send it if it isn't the except list and isn't isolated and still has a hop count
532 # taking into account filtering and so on
533 foreach $dxchan (@dxchan) {
534 next if $dxchan == $main::me;
535 next if $dxchan == $self && $self->is_node;
536 $dxchan->dx_spot($line, $self->{isolate}, @_, $self->{call});
547 if ($self->{spotsfilter}) {
548 ($filter, $hops) = $self->{spotsfilter}->it(@_);
549 return unless $filter;
551 send_prot_line($self, $filter, $hops, $isolate, $line);
556 my ($self, $filter, $hops, $isolate, $line) = @_;
562 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
564 $routeit = adjust_hops($self, $line); # adjust its hop count by node name
565 return unless $routeit;
568 $self->send($routeit);
570 $self->send($routeit) unless $self->{isolate} || $isolate;
579 my @dxchan = DXChannel::get_all();
581 my @dxcc = ((Prefix::cty_data($_[6]))[0..2], (Prefix::cty_data($_[7]))[0..2]);
583 # send it if it isn't the except list and isn't isolated and still has a hop count
584 # taking into account filtering and so on
585 foreach $dxchan (@dxchan) {
586 next if $dxchan == $main::me;
587 next if $dxchan == $self && $self->is_node;
591 $dxchan->wwv($line, $self->{isolate}, @_, $self->{call}, @dxcc);
602 if ($self->{wwvfilter}) {
603 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_]);
604 return unless $filter;
606 send_prot_line($self, $filter, $hops, $isolate, $line)
613 my @dxchan = DXChannel::get_all();
615 my @dxcc = ((Prefix::cty_data($_[10]))[0..2], (Prefix::cty_data($_[11]))[0..2]);
617 # send it if it isn't the except list and isn't isolated and still has a hop count
618 # taking into account filtering and so on
619 foreach $dxchan (@dxchan) {
620 next if $dxchan == $main::me;
621 next if $dxchan == $self;
623 $dxchan->wcy($line, $self->{isolate}, @_, $self->{call}, @dxcc);
634 if ($self->{wcyfilter}) {
635 ($filter, $hops) = $self->{wcyfilter}->it(@_);
636 return unless $filter;
638 send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
645 my $from_pc9x = shift;
647 my @dxchan = DXChannel::get_all();
651 my $text = unpad($_[2]);
654 if ($_[3] eq '*') { # sysops
656 } elsif ($_[3] gt ' ') { # speciality list handling
657 my ($name) = split /\./, $_[3];
658 $target = "$name"; # put the rest in later (if bothered)
665 $target = "ALL" if !$target;
668 # obtain country codes etc
669 my @a = Prefix::cty_data($from);
670 my @b = Prefix::cty_data($_[4]);
671 if ($self->{inannfilter}) {
672 my ($filter, $hops) =
673 $self->{inannfilter}->it(@_, $self->{call},
675 @b[0..2], $a[3], $b[3]);
677 dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
682 # the sysop ('*') thing is an attempt to minimise the damage caused by non-updated PC93 generators
683 if (AnnTalk::dup($from, $target, $_[2]) || ($_[3] eq '*' && AnnTalk::dup($from, 'ALL', $_[2]))) {
684 my $dxchan = DXChannel::get($from);
685 if ($self == $main::me && $dxchan && $dxchan->is_user) {
686 if ($dxchan->priv < 5) {
687 $dxchan->send($dxchan->msg('dup'));
691 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
696 Log('ann', $target, $from, $text);
698 # send it if it isn't the except list and isn't isolated and still has a hop count
699 # taking into account filtering and so on
700 foreach $dxchan (@dxchan) {
701 next if $dxchan == $main::me;
702 next if $dxchan == $self && $self->is_node;
703 next if $from_pc9x && $dxchan->{do_pc9x};
704 next if $target eq 'LOCAL' && $dxchan->is_node;
705 $dxchan->announce($line, $self->{isolate}, $to, $target, $text, @_, $self->{call},
710 my $msgid = int rand(1000);
715 $msgid = 1 if $msgid > 999;
723 my $from_pc9x = shift;
725 my @dxchan = DXChannel::get_all();
728 my $text = unpad($_[2]);
732 # munge the group and recast the line if required
733 if ($target =~ s/\.LST$//) {
737 # obtain country codes etc
738 my @a = Prefix::cty_data($from);
739 my @b = Prefix::cty_data($_[4]);
740 if ($self->{inannfilter}) {
741 my ($filter, $hops) =
742 $self->{inannfilter}->it(@_, $self->{call},
744 @b[0..2], $a[3], $b[3]);
746 dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
751 if (AnnTalk::dup($from, $target, $_[2], $main::systime + $chatdupeage)) {
752 my $dxchan = DXChannel::get($from);
753 if ($self == $main::me && $dxchan && $dxchan->is_user) {
754 if ($dxchan->priv < 5) {
755 $dxchan->send($dxchan->msg('dup'));
759 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
765 Log('chat', $target, $from, $text);
767 # send it if it isn't the except list and isn't isolated and still has a hop count
768 # taking into account filtering and so on
769 foreach $dxchan (@dxchan) {
770 my $is_ak1a = $dxchan->is_ak1a;
772 if ($dxchan->is_node) {
773 next if $dxchan == $main::me;
774 next if $dxchan == $self;
775 next if $from_pc9x && $dxchan->{do_pc9x};
776 next unless $dxchan->is_spider || $is_ak1a;
777 next if $target eq 'LOCAL';
778 if (!$ak1a_line && $is_ak1a) {
779 $ak1a_line = pc12($_[0], $text, $_[1], "$target.LST");
783 $dxchan->chat($is_ak1a ? $ak1a_line : $line, $self->{isolate}, $target, $_[1],
784 $text, @_, $self->{call}, @a[0..2], @b[0..2]);
798 if ($self->{annfilter}) {
799 ($filter, $hops) = $self->{annfilter}->it(@_);
800 return unless $filter;
802 send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
815 if ($self->{annfilter}) {
816 ($filter, $hops) = $self->{annfilter}->it(@_);
817 return unless $filter;
819 if (($self->is_spider || $self->is_ak1a) && $_[1] ne $main::mycall) {
820 send_prot_line($self, $filter, $hops, $isolate, $line);
825 sub send_local_config
829 dbg('DXProt::send_local_config') if isdbg('trace');
837 if ($self->{isolate}) {
838 dbg("send_local_config: isolated");
839 @localnodes = ( $main::routeroot );
840 $self->send_route($main::mycall, \&pc19, 1, $main::routeroot);
841 } elsif ($self->{do_pc9x}) {
842 dbg("send_local_config: doing pc9x");
843 my $node = Route::Node::get($self->{call});
844 $self->send_last_pc92_config($main::routeroot);
845 $self->send(pc92a($main::routeroot, $node)) unless $main::routeroot->last_PC92C =~ /$self->{call}/;
847 # create a list of all the nodes that are not connected to this connection
848 # and are not themselves isolated, this to make sure that isolated nodes
849 # don't appear outside of this node
851 dbg("send_local_config: traditional");
853 # send locally connected nodes
854 my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
855 @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan if @dxchan;
856 $self->send_route($main::mycall, \&pc19, scalar(@localnodes)+1, $main::routeroot, @localnodes);
859 my @rawintcalls = map { $_->nodes } @localnodes if @localnodes;
861 foreach $node (@rawintcalls) {
862 push @intcalls, $node if grep $_ && $node != $_, @intcalls;
864 my $ref = Route::Node::get($self->{call});
865 my @rnodes = $ref->nodes;
866 foreach $node (@intcalls) {
867 push @remotenodes, Route::Node::get($node) if grep $_ && $node != $_, @rnodes, @remotenodes;
869 $self->send_route($main::mycall, \&pc19, scalar(@remotenodes), @remotenodes);
872 # get all the users connected on the above nodes and send them out
873 unless ($self->{do_pc9x}) {
874 foreach $node ($main::routeroot, @localnodes, @remotenodes) {
876 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
877 $self->send_route($main::mycall, \&pc16, 1, $node, @rout) if @rout && $self->user->wantsendpc16;
879 dbg("sent a null value") if isdbg('chanerr');
885 sub gen_my_pc92_config
889 if ($node->{call} eq $main::mycall) {
890 clear_pc92_changes(); # remove any slugged data, we are generating it as now
891 my @dxchan = grep { $_->call ne $main::mycall && !$_->{isolate} } DXChannel::get_all();
892 dbg("ROUTE: all dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('routelow');
893 my @localnodes = map { my $r = Route::get($_->{call}); $r ? $r : () } @dxchan;
894 dbg("ROUTE: localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('routelow');
895 return pc92c($node, @localnodes);
897 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
898 return pc92c($node, @rout);
902 sub send_last_pc92_config
906 if (my $l = $node->last_PC92C) {
909 $self->send_pc92_config($node);
918 dbg('DXProt::send_pc92_config') if isdbg('trace');
920 $node->last_PC92C(gen_my_pc92_config($node));
921 $self->send($node->last_PC92C);
924 sub broadcast_pc92_update
929 dbg("ROUTE: broadcast_pc92_update $call") if isdbg('obscount');
931 my $nref = Route::Node::get($call);
933 dbg("ERROR: broadcast_pc92_update - Route::Node $call disappeared");
936 my $l = $nref->last_PC92C(gen_my_pc92_config($nref));
937 $nref->lastid(last_pc9x_id());
938 $main::me->broadcast_route_pc9x($main::mycall, undef, $l, 0);
939 $self->update_pc92_next($pc92_update_period);
942 sub time_out_pc92_routes
944 my @nodes = grep {$_->call ne $main::mycall && ($_->do_pc9x || $_->via_pc92)} Route::Node::get_all();
946 foreach my $n (@nodes) {
949 if (my $dxchan = DXChannel::get($n->call)) {
950 dbg("ROUTE: disconnecting local pc92 $dxchan->{call} on obscount") if isdbg('obscount');
954 my @parents = map {Route::Node::get($_)} $n->parents;
957 dbg("ROUTE: deleting pc92 $_->{call} from $n->{call} on obscount") if isdbg('obscount');
958 push @rdel, $n->del($_);
962 dbg("ROUTE: obscount on $n->{call} now $o") if isdbg('obscount');
966 $main::me->route_pc21($main::mycall, undef, $_) if $_;
971 # route a message down an appropriate interface for a callsign
973 # is called route(to, pcline);
978 my ($self, $call, $line) = @_;
980 if (ref $self && $call eq $self->{call}) {
981 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
985 # always send it down the local interface if available
986 my $dxchan = DXChannel::get($call);
988 dbg("route: $call -> $dxchan->{call} direct" ) if isdbg('route');
990 my $cl = Route::get($call);
991 $dxchan = $cl->dxchan if $cl;
993 if (ref $self && $dxchan eq $self) {
994 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
997 dbg("route: $call -> $dxchan->{call} using normal route" ) if isdbg('route');
1001 # try the backstop method
1003 my $rcall = RouteDB::get($call);
1005 if ($self && $rcall eq $self->{call}) {
1006 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1009 $dxchan = DXChannel::get($rcall);
1010 dbg("route: $call -> $rcall using RouteDB" ) if isdbg('route') && $dxchan;
1015 my $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1017 $dxchan->send($routeit) unless $dxchan == $main::me;
1020 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
1025 # obtain the hops from the list for this callsign and pc no
1031 my $hops = $DXProt::hopcount{$pcno};
1032 $hops = $DXProt::def_hopcount if !$hops;
1037 # adjust the hop count on a per node basis using the user loadable
1038 # hop table if available or else decrement an existing one
1045 my $call = $self->{call};
1048 if (($hops) = $s =~ /\^H([-\d]+)\^?~?$/o) {
1049 my ($pcno) = $s =~ /^PC(\d\d)/o;
1050 confess "$call called adjust_hops with '$s'" unless $pcno;
1051 my $ref = $nodehops{$call} if %nodehops;
1053 my $newhops = $ref->{$pcno};
1054 return "" if defined $newhops && $newhops == 0;
1055 $newhops = $ref->{default} unless $newhops;
1056 return "" if defined $newhops && $newhops == 0;
1057 $newhops = $hops unless $newhops;
1058 return "" unless $newhops > 0;
1059 $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops != $hops;
1061 return "" unless $hops > 0;
1073 return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1074 do "$main::data/hop_table.pl";
1081 my ($self, $tonode, $fromnode, $user, $cmd) = @_;
1082 if ($tonode eq $main::mycall) {
1083 my $ref = DXUser->get_current($fromnode);
1084 my $cref = Route::Node::get($fromnode);
1085 Log('rcmd', 'in', $ref->{priv}, $fromnode, $cmd);
1086 if ($cmd !~ /^\s*rcmd/i && $cref && $ref && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS!
1087 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
1088 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
1089 my $oldpriv = $self->{priv};
1090 $self->{priv} = $ref->{priv}; # assume the user's privilege level
1091 my @in = (DXCommandmode::run_cmd($self, $cmd));
1092 $self->{priv} = $oldpriv;
1093 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @in);
1094 delete $self->{remotecmd};
1096 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1099 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "your attempt is logged, Tut tut tut...!");
1102 my $ref = DXUser->get_current($tonode);
1103 if ($ref && $ref->is_clx) {
1104 $self->route($tonode, pc84($fromnode, $tonode, $user, $cmd));
1106 $self->route($tonode, pc34($fromnode, $tonode, $cmd));
1111 sub process_rcmd_reply
1113 my ($self, $tonode, $fromnode, $user, $line) = @_;
1114 if ($tonode eq $main::mycall) {
1115 my $s = $rcmds{$fromnode};
1117 my $dxchan = DXChannel::get($s->{call});
1118 my $ref = $user eq $tonode ? $dxchan : (DXChannel::get($user) || $dxchan);
1119 $ref->send($line) if $ref;
1120 delete $rcmds{$fromnode} if !$dxchan;
1122 # send unsolicited ones to the sysop
1123 my $dxchan = DXChannel::get($main::myalias);
1124 $dxchan->send($line) if $dxchan;
1127 my $ref = DXUser->get_current($tonode);
1128 if ($ref && $ref->is_clx) {
1129 $self->route($tonode, pc85($fromnode, $tonode, $user, $line));
1131 $self->route($tonode, pc35($fromnode, $tonode, $line));
1140 my $fromnode = shift;
1145 Log('rcmd', 'out', $fromnode, $line);
1146 if ($self->is_clx) {
1147 $self->send(pc85($main::mycall, $fromnode, $user, "$main::mycall:$line"));
1149 $self->send(pc35($main::mycall, $fromnode, "$main::mycall:$line"));
1154 # add a rcmd request to the rcmd queues
1157 my ($self, $to, $cmd) = @_;
1160 $r->{call} = $self->{call};
1161 $r->{t} = $main::systime;
1165 my $ref = Route::Node::get($to);
1166 my $dxchan = $ref->dxchan;
1167 if ($dxchan && $dxchan->is_clx) {
1168 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1170 route(undef, $to, pc34($main::mycall, $to, $cmd));
1177 my $pc39flag = shift;
1178 my $call = $self->call;
1180 return if $self->{disconnecting}++;
1182 unless ($pc39flag && $pc39flag == 1) {
1183 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1186 # get rid of any PC16/17/19
1187 eph_del_regex("^PC1[679]*$call");
1189 # do routing stuff, remove me from routing table
1190 my $node = Route::Node::get($call);
1192 RouteDB::delete_interface($call);
1194 # unbusy and stop and outgoing mail
1195 my $mref = DXMsg::get_busy($call);
1196 $mref->stop_msg($call) if $mref;
1198 # remove outstanding pings
1199 delete $pings{$call};
1201 # I was the last node visited
1202 $self->user->node($main::mycall);
1204 # send info to all logged in thingies
1205 $self->tell_login('logoutn');
1207 Log('DXProt', $call . " Disconnected");
1209 $self->SUPER::disconnect;
1211 # here we determine what needs to go out of the routing table
1213 if ($node && $pc39flag != 2) {
1214 dbg('%Route::Node::List = ' . join(',', sort keys %Route::Node::list)) if isdbg('routedisc');
1216 @rout = $node->del($main::routeroot);
1218 dbg('@rout = ' . join(',', sort map {$_->call} @rout)) if isdbg('routedisc');
1220 # now we need to see what can't be routed anymore and came
1221 # in via this node (probably).
1223 while ($n != @rout) {
1225 for (Route::Node::get_all()) {
1226 unless ($_->dxchan) {
1227 push @rout, $_->delete;
1230 dbg('@rout = ' . join(',', sort map {$_->call} @rout)) if isdbg('routedisc');
1233 dbg('%Route::Node::List = ' . join(',', sort keys %Route::Node::list)) if isdbg('routedisc');
1235 # and all my ephemera as well
1238 eph_del_regex("^PC1[679].*$c");
1242 # broadcast to all other nodes that all the nodes connected to via me are gone
1243 unless ($pc39flag && $pc39flag == 2) {
1244 $self->route_pc21($main::mycall, undef, @rout) if @rout;
1245 $self->route_pc92d($main::mycall, undef, $main::routeroot, $node) if $node;
1251 # send a talk message to this thingy
1255 my ($self, $from, $to, $via, $line, $origin) = @_;
1257 if ($self->{do_pc9x}) {
1258 $self->send(pc93($to, $from, $via, $line));
1260 $self->send(pc10($from, $to, $via, $line, $origin));
1262 Log('talk', $to, $from, '>' . ($via || $origin || $self->call), $line) unless $origin && $origin ne $main::mycall;
1265 # send it if it isn't the except list and isn't isolated and still has a hop count
1266 # taking into account filtering and so on
1272 my $generate = shift;
1273 my $no = shift; # the no of things to filter on
1275 my ($filter, $hops);
1278 for (; @_ && $no; $no--) {
1281 # don't send messages with $self's call in back to them
1282 if ($r->call eq $self->{call}) {
1283 dbg("PCPROT: trying to send $self->{call} back itself") if isdbg('chanerr');
1287 if (!$self->{isolate} && $self->{routefilter}) {
1290 ($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});
1294 dbg("PCPROT: $self->{call}/" . $r->call . " rejected by output filter") if isdbg('filter');
1297 dbg("was sent a null value") if isdbg('chanerr');
1300 push @rin, $r unless $self->{isolate} && $r->call ne $main::mycall;
1304 foreach my $line (&$generate(@rin, @_)) {
1307 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1309 $routeit = adjust_hops($self, $line); # adjust its hop count by node name
1310 next unless $routeit;
1313 $self->send($routeit);
1318 # broadcast everywhere
1323 my $generate = shift;
1325 my @dxchan = DXChannel::get_all_nodes();
1329 $line =~ /\^H(\d+)\^?\~?$/;
1330 return unless $1 > 0;
1332 unless ($self->{isolate}) {
1333 foreach $dxchan (@dxchan) {
1334 next if $dxchan == $self || $dxchan == $main::me;
1335 next if $origin eq $dxchan->{call}; # don't route some from this call back again.
1336 next unless $dxchan->isa('DXProt');
1338 $dxchan->send_route($origin, $generate, @_);
1343 # broadcast to non-pc9x nodes
1344 sub broadcast_route_nopc9x
1348 my $generate = shift;
1350 my @dxchan = DXChannel::get_all_nodes();
1354 $line =~ /\^H(\d+)\^?\~?$/;
1355 return unless $1 > 0;
1357 unless ($self->{isolate}) {
1358 foreach $dxchan (@dxchan) {
1359 next if $dxchan == $self || $dxchan == $main::me;
1360 next if $origin eq $dxchan->{call}; # don't route some from this call back again.
1361 next unless $dxchan->isa('DXProt');
1362 next if $dxchan->{do_pc9x};
1363 if ($generate == \&pc16 || $generate==\&pc17) {
1364 next unless $dxchan->user->wantsendpc16;
1366 $dxchan->send_route($origin, $generate, @_);
1371 # this is only used for next door nodes on init
1376 return unless $self->{do_pc9x};
1379 my $generate = shift;
1380 my $no = shift; # the no of things to filter on
1383 $line = &$generate(@_);
1387 # broadcast only to pc9x nodes
1388 sub broadcast_route_pc9x
1392 my $generate = shift;
1395 my @dxchan = DXChannel::get_all_nodes();
1398 if ($origin eq $main::mycall && $generate && !$line) {
1399 $line = &$generate(@_);
1402 $line =~ /\^H(\d+)\^\~?$/;
1403 unless ($1 > 0 && $self->{isolate}) {
1404 foreach $dxchan (@dxchan) {
1405 next if $dxchan == $self || $dxchan == $main::me;
1406 next if $origin eq $dxchan->{call}; # don't route some from this call back again.
1407 next unless $dxchan->isa('DXProt');
1408 next unless $dxchan->{do_pc9x};
1410 $dxchan->send($line);
1418 return unless $self->user->wantpc16;
1421 broadcast_route_nopc9x($self, $origin, \&pc16, $line, 1, @_);
1427 return unless $self->user->wantpc16;
1430 broadcast_route_nopc9x($self, $origin, \&pc17, $line, 1, @_);
1438 broadcast_route_nopc9x($self, $origin, \&pc19, $line, scalar @_, @_);
1446 broadcast_route_nopc9x($self, $origin, \&pc21, $line, scalar @_, @_);
1454 broadcast_route($self, $origin, \&pc24, $line, 1, @_);
1462 broadcast_route($self, $origin, \&pc41, $line, 1, @_);
1470 broadcast_route($self, $origin, \&pc50, $line, 1, @_);
1478 broadcast_route_pc9x($self, $origin, \&pc92c, $line, 1, @_);
1486 broadcast_route_pc9x($self, $origin, \&pc92a, $line, 1, @_);
1494 broadcast_route_pc9x($self, $origin, \&pc92d, $line, 1, @_);
1501 my ($filter, $hops) = (1, 1);
1503 if ($self->{inroutefilter}) {
1504 ($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);
1505 dbg("PCPROT: $self->{call}/" . $r->call . ' rejected by in_filter_route') if !$filter && isdbg('chanerr');
1513 my $t = shift || $eph_restime;
1517 $s =~ s/\^H\d\d?\^?\~?$//;
1518 $r = 1 if exists $eph{$s}; # pump up the dup if it keeps circulating
1519 $eph{$s} = $main::systime + $t;
1520 dbg("PCPROT: emphemeral duplicate") if $r && isdbg('chanerr');
1528 while (($key, $val) = each %eph) {
1529 if ($key =~ m{$regex}) {
1539 while (($key, $val) = each %eph) {
1540 if ($main::systime >= $val) {
1551 while (($key, $val) = each %eph) {
1552 push @out, $key, $val;
1559 goto &DXCommandmode::run_cmd;
1563 # import any msgs in the chat directory
1564 # the messages are sent to the chat group which forms the
1565 # the first part of the name (eg: solar.1243.txt would be
1566 # sent to chat group SOLAR)
1568 # Each message found is sent: one non-blank line to one chat
1569 # message. So 4 lines = 4 chat messages.
1571 # The special name LOCAL is for local users ANN
1572 # The special name ALL is for ANN/FULL
1573 # The special name SYSOP is for ANN/SYSOP
1577 # are there any to do in this directory?
1578 return unless -d $chatimportfn;
1579 unless (opendir(DIR, $chatimportfn)) {
1580 dbg("can\'t open $chatimportfn $!") if isdbg('msg');
1581 Log('msg', "can\'t open $chatimportfn $!");
1585 my @names = readdir(DIR);
1588 foreach $name (@names) {
1589 next if $name =~ /^\./;
1590 my $splitit = $name =~ /^split/;
1591 my $fn = "$chatimportfn/$name";
1593 unless (open(MSG, $fn)) {
1594 dbg("can\'t open import file $fn $!") if isdbg('msg');
1595 Log('msg', "can\'t open import file $fn $!");
1599 my @msg = map { s/\r?\n$//; $_ } <MSG>;
1603 my @cat = split /\./, $name;
1604 my $target = uc $cat[0];
1606 foreach my $text (@msg) {
1607 next unless $text && $text !~ /^\s*#/;
1608 if ($target eq 'ALL' || $target eq 'LOCAL' || $target eq 'SYSOP' || $target eq 'WX') {
1609 my $sysop = uc $target eq 'SYSOP' ? '*' : ' ';
1610 my $wx = uc $target eq 'WX' ? '1' : '0';
1612 $via = '*' if $target eq 'ALL' || $target eq 'SYSOP';
1613 Log('ann', $target, $main::mycall, $text);
1614 $main::me->normal(DXProt::pc93($target, $main::mycall, $via, $text));
1616 DXCommandmode::send_chats($main::me, $target, $text);
1622 # start a pc92 find operation
1627 my $key = "$dxchan->{call}|$target";
1628 if ($pc92_find{$key}) {
1633 # function (not method) to handle pc92 find returns
1634 sub handle_pc92_find_reply
1636 my ($dxchan, $node, $from, $target, $flag, $ms) = @_;
1638 $dxchan->print_pc92_find_reply($node, $target, $flag, $ms) if $dxchan->can('print_pc92_find_return');