3 # This module impliments the protocal mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
39 use vars qw($pc11_max_age $pc23_max_age $last_pc50 $eph_restime $eph_info_restime $eph_pc34_restime
40 $last_hour $last10 %eph %pings %rcmds $ann_to_talk
41 $pingint $obscount %pc19list $chatdupeage $chatimportfn
42 $pc19_version $myprot_version
43 %nodehops $baddx $badspotter $badnode $censorpc
44 $allowzero $decode_dk0wcy $send_opernam @checklist
45 $eph_pc15_restime $pc92_update_period $pc92_obs_timeout
46 %pc92_find $pc92_find_timeout $pc92_short_update_period
47 $next_pc92_obs_timeout $pc92_slug_changes $last_pc92_slug
48 $pc92_extnode_update_period $pc50_interval
49 $pc92_keepalive_period
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 %rcmds = (); # outstanding rcmd requests outbound
57 %nodehops = (); # node specific hop control
58 %pc19list = (); # list of outstanding PC19s that haven't had PC16s on them
60 $censorpc = 1; # Do a BadWords::check on text fields and reject things
61 # loads of 'bad things'
62 $baddx = new DXHash "baddx";
63 $badspotter = new DXHash "badspotter";
64 $badnode = new DXHash "badnode";
65 $last10 = $last_pc50 = time;
68 $eph_info_restime = 60*60;
69 $eph_pc15_restime = 6*60;
70 $eph_pc34_restime = 30;
73 $chatdupeage = 20 * 60;
74 $chatimportfn = "$main::root/chat_import";
75 $pc19_version = 5454; # the visible version no for outgoing PC19s generated from pc59
76 $pc92_update_period = 4*60*60; # the period between outgoing PC92 C updates
77 $pc92_short_update_period = 15*60; # shorten the update period after a connection or start up
78 $pc92_extnode_update_period = 1*60*60; # the update period for external nodes
79 $pc92_keepalive_period = 1*60*60; # frequency of PC92 K (keepalive) records
80 %pc92_find = (); # outstanding pc92 find operations
81 $pc92_find_timeout = 30; # maximum time to wait for a reply
86 [ qw(i c c m bp bc c) ], # pc10
87 [ qw(i f m d t m c c h) ], # pc11
88 [ qw(i c bm m bm bm p h) ], # pc12
92 undef , # pc16 has to be validated manually
93 [ qw(i c c h) ], # pc17
95 undef , # pc19 has to be validated manually
96 undef , # pc20 no validation
97 [ qw(i c m h) ], # pc21
98 undef , # pc22 no validation
99 [ qw(i d n n n n m c c h) ], # pc23
100 [ qw(i c p h) ], # pc24
101 [ qw(i c c n n) ], # pc25
102 [ qw(i f m d t m c c bc) ], # pc26
103 [ qw(i d n n n n m c c bc) ], # pc27
104 [ qw(i c c m c d t p m bp n p bp bc) ], # pc28
105 [ qw(i c c n m) ], # pc29
106 [ qw(i c c n) ], # pc30
107 [ qw(i c c n) ], # pc31
108 [ qw(i c c n) ], # pc32
109 [ qw(i c c n) ], # pc33
110 [ qw(i c c m) ], # pc34
111 [ qw(i c c m) ], # pc35
112 [ qw(i c c m) ], # pc36
113 [ qw(i c c n m) ], # pc37
114 undef, # pc38 not interested
115 [ qw(i c m) ], # pc39
116 [ qw(i c c m p n) ], # pc40
117 [ qw(i c n m h) ], # pc41
118 [ qw(i c c n) ], # pc42
119 undef, # pc43 don't handle it
120 [ qw(i c c n m m c) ], # pc44
121 [ qw(i c c n m) ], # pc45
122 [ qw(i c c n) ], # pc46
125 [ qw(i c m h) ], # pc49
126 [ qw(i c n h) ], # pc50
127 [ qw(i c c n) ], # pc51
149 [ qw(i d n n n n n n m m m c c h) ], # pc73
160 [ qw(i c c c m) ], # pc84
161 [ qw(i c c c m) ], # pc85
168 [ qw(i c f l)], # pc92
169 [ qw(i c f *m c *c m)], # pc93
172 # use the entry in the check list to check the field list presented
173 # return OK if line NOT in check list (for now)
178 return 0 if $n < 0 || $n > @checklist;
179 my $ref = $checklist[$n];
180 return 0 unless ref $ref;
183 for ($i = 1; $i < @$ref; $i++) {
184 my ($blank, $act) = $$ref[$i] =~ /^(b?)(\w)$/;
185 return 0 unless $act;
186 next if $blank eq 'b' && $_[$i] =~ /^[ \*]$/;
187 next if $blank eq '*' && $_[$i] =~ /^\*$/;
189 return $i unless is_callsign($_[$i]);
190 } elsif ($act eq 'i') {
192 } elsif ($act eq 'm') {
193 return $i unless is_pctext($_[$i]);
194 } elsif ($act eq 'p') {
195 return $i unless is_pcflag($_[$i]);
196 } elsif ($act eq 'f') {
197 return $i unless is_freq($_[$i]);
198 } elsif ($act eq 'n') {
199 return $i unless $_[$i] =~ /^[\d ]+$/;
200 } elsif ($act eq 'h') {
201 return $i unless $_[$i] =~ /^H\d\d?$/;
202 } elsif ($act eq 'd') {
203 return $i unless $_[$i] =~ /^\s*\d+-\w\w\w-[12][90]\d\d$/;
204 } elsif ($act eq 't') {
205 return $i unless $_[$i] =~ /^[012]\d[012345]\dZ$/;
206 } elsif ($act eq 'l') {
207 return $i unless $_[$i] =~ /^[A-Z]$/;
216 my $period = shift || ($self->{do_pc9x} ? $pc92_update_period : $pc92_extnode_update_period);
217 $self->{next_pc92_update} = $main::systime + $period - int rand($period / 4);
218 dbg("ROUTE: update_pc92_next: $self->{call} " . atime($self->{next_pc92_update})) if isdbg('obscount');
221 sub update_pc92_keepalive
224 my $period = shift || $pc92_keepalive_period;
225 $self->{next_pc92_keepalive} = $main::systime + $period - int rand($period / 4);
226 dbg("ROUTE: update_pc92_keepalive: $self->{call} " . atime($self->{next_pc92_keepalive})) if isdbg('obscount');
231 do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
234 my $user = DXUser::get($main::mycall);
235 die "User $main::mycall not setup or disappeared RTFM" unless $user;
237 $myprot_version += $main::version*100;
238 $main::me = DXProt->new($main::mycall, 0, $user);
239 $main::me->{here} = 1;
240 $main::me->{state} = "indifferent";
241 $main::me->{sort} = 'S'; # S for spider
242 $main::me->{priv} = 9;
243 $main::me->{metric} = 0;
244 $main::me->{pingave} = 0;
245 $main::me->{registered} = 1;
246 $main::me->{version} = $main::version;
247 $main::me->{build} = "$main::subversion.$main::build";
248 $main::me->{do_pc9x} = 1;
249 $main::me->update_pc92_next($pc92_short_update_period);
250 $main::me->update_pc92_keepalive;
254 # obtain a new connection this is derived from dxchannel
259 my $self = DXChannel::alloc(@_);
261 # add this node to the table, the values get filled in later
265 # if we have an entry already, then send a PC21 to all connect
266 # old style connections, because we are about to get the real deal
267 if (my $ref = Route::Node::get($call)) {
268 dbg("ROUTE: $call is already in the routing table, deleting") if isdbg('route');
269 my @rout = $ref->delete;
270 $self->route_pc21($main::mycall, undef, @rout) if @rout;
272 $main::routeroot->add($call, '5000', Route::here(1)) if $call ne $main::mycall;
277 # this is how a pc connection starts (for an incoming connection)
278 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
279 # all the crap that comes between).
282 my ($self, $line, $sort) = @_;
283 my $call = $self->{call};
284 my $user = $self->{user};
287 my $host = $self->{conn}->{peerhost};
288 $host ||= "AGW Port #$self->{conn}->{agwport}" if exists $self->{conn}->{agwport};
291 Log('DXProt', "$call connected from $host");
293 # remember type of connection
294 $self->{consort} = $line;
295 $self->{outbound} = $sort eq 'O';
296 my $priv = $user->priv;
297 $priv = $user->priv(1) unless $priv;
298 $self->{priv} = $priv; # other clusters can always be 'normal' users
299 $self->{lang} = $user->lang || 'en';
300 $self->{isolate} = $user->{isolate};
301 $self->{consort} = $line; # save the connection type
305 # sort out registration
306 $self->{registered} = 1;
308 # get the output filters
309 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
310 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
311 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
312 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
313 $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) unless $self->{isolate};
314 $self->{pc92filter} = Filter::read_in('pc92', $call, 0) || Filter::read_in('pc92', 'node_default', 0) unless $self->{isolate} ;
317 # get the INPUT filters (these only pertain to Clusters)
318 $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
319 $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
320 $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
321 $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
322 $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1) unless $self->{isolate};
323 $self->{inpc92filter} = Filter::read_in('pc92', $call, 0) || Filter::read_in('pc92', 'node_default', 0) unless $self->{isolate} ;
326 # set unbuffered and no echo
327 $self->send_now('B',"0");
328 $self->send_now('E',"0");
329 $self->conn->echo(0) if $self->conn->can('echo');
331 # ping neighbour node stuff
332 my $ping = $user->pingint;
333 $ping = $pingint unless defined $ping;
334 $self->{pingint} = $ping;
335 $self->{nopings} = $user->nopings || $obscount;
336 $self->{pingtime} = [ ];
337 $self->{pingave} = 999;
338 $self->{metric} ||= 100;
339 $self->{lastping} = $main::systime;
341 # send initialisation string
342 unless ($self->{outbound}) {
346 $self->state('init');
347 $self->{pc50_t} = $main::systime;
349 # send info to all logged in thingies
350 $self->tell_login('loginn');
352 # run a script send the output to the debug file
353 my $script = new Script(lc $call) || new Script('node_default');
354 $script->run($self) if $script;
356 # set up a config broadcast "quite soon" to converge tables quicker
357 $main::me->update_pc92_next($pc92_short_update_period);
358 $self->update_pc92_next($pc92_short_update_period);
360 # set next keepalive time
361 $self->update_pc92_keepalive;
365 # send outgoing 'challenge'
371 $self->send(pc18(($self->{isolate} || !$self->user->wantpc9x) ? "" : " pc9x"));
375 # This is the normal pcxx despatcher
379 my ($self, $line) = @_;
381 if ($line =~ '^<\w+\s' && $main::do_xml) {
382 DXXml::normal($self, $line);
386 my @field = split /\^/, $line;
387 return unless @field;
389 pop @field if $field[-1] eq '~';
391 # print join(',', @field), "\n";
394 # process PC frames, this will fail unless the frame starts PCnn
395 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
396 unless (defined $pcno && $pcno >= 10 && $pcno <= 99) {
397 dbg("PCPROT: unknown protocol") if isdbg('chanerr');
401 # check for and dump bad protocol messages
402 my $n = check($pcno, @field);
404 dbg("PCPROT: bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")") if isdbg('chanerr');
408 # modify the hop count here
409 if ($self != $main::me) {
410 if (my ($hops, $trail) = $line =~ /\^H(\d+)(\^?\~?)?$/) {
414 $line =~ s/\^H(\d+)(\^?\~?)?$/sprintf('^H%d%s', $hops, $trail)/e;
415 $field[-1] = "H$hops";
419 if (defined &Local::pcprot) {
421 eval { $r = Local::pcprot($self, $pcno, $line, @field); };
422 return if $r; # i.e don't process it
425 # send it out for processing
426 my $origin = $self->{call};
428 my $sub = "handle_$pcno";
430 if ($self->can($sub)) {
431 $self->$sub($pcno, $line, $origin, @field);
433 $self->handle_default($pcno, $line, $origin, @field);
438 # This is called from inside the main cluster processing loop and is used
439 # for despatching commands that are doing some long processing job
444 my @dxchan = DXChannel::get_all();
449 if ($t - $last10 >= 10) {
457 # send out a pc50 on EVERY channel all at once
458 if ($t >= $last_pc50 + $pc50_interval) {
459 $pc50s = pc50($main::me, scalar DXChannel::get_all_users);
464 foreach $dxchan (@dxchan) {
465 next unless $dxchan->is_node;
466 next if $dxchan == $main::me;
469 $dxchan->send($pc50s) if !$dxchan->{do_pc9x} && $pc50s;
471 # send a ping out on this channel
472 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
473 if ($dxchan->{nopings} <= 0) {
474 dbg("ROUTE: $dxchan->{call} disconnected on ping obscount") if isdbg('obscount');
477 DXXml::Ping::add($main::me, $dxchan->call);
478 $dxchan->{nopings} -= 1;
479 $dxchan->{lastping} = $t;
480 $dxchan->{lastping} += $dxchan->{pingint} / 2 unless @{$dxchan->{pingtime}};
481 dbg("ROUTE: $dxchan->{call} ping obscount = $dxchan->{nopings}") if isdbg('obscount');
488 # send out config broadcasts
489 foreach $dxchan (@dxchan) {
490 next unless $dxchan->is_node;
492 # send out a PC92 config record if required for me and
493 # all my non pc9x dependent nodes. But for dependent nodes we only do
494 # this if we have not seen any from anyone else for at least half
495 # of one update period. This should stop quite a bit of excess C
496 # records. Someone will win, it does not really matter who, because
497 # we always believe "us".
498 if ($main::systime >= $dxchan->{next_pc92_update}) {
499 if ($dxchan == $main::me || !$dxchan->{do_pc9x}) {
500 dbg("ROUTE: pc92 broadcast candidate: $dxchan->{call}") if isdbg('obscount');
501 my $ref = Route::Node::get($dxchan->{call});
502 if ($dxchan == $main::me || ($ref && ($ref->measure_pc9x_t($main::systime-$main::systime_daystart)) >= $pc92_extnode_update_period/2)) {
503 $dxchan->broadcast_pc92_update($dxchan->{call});
505 $dxchan->update_pc92_next;
508 $dxchan->update_pc92_next; # this won't actually do anything, it's just to be tidy
513 # do the keepalive for me, if required
514 if ($main::systime >= $main::me->{next_pc92_keepalive}) {
515 time_out_pc92_routes();
516 $main::me->broadcast_pc92_keepalive($main::mycall);
519 if ($pc92_slug_changes && $main::systime >= $last_pc92_slug + $pc92_slug_changes) {
520 my ($add, $del) = gen_pc92_changes();
521 $main::me->route_pc92d($main::mycall, undef, $main::routeroot, @$del) if @$del;
522 $main::me->route_pc92a($main::mycall, undef, $main::routeroot, @$add) if @$add;
523 clear_pc92_changes();
527 if ($main::systime - 3600 > $last_hour) {
528 $last_hour = $main::systime;
533 # finish up a pc context
537 # some active measures
545 my @dxchan = DXChannel::get_all();
548 # send it if it isn't the except list and isn't isolated and still has a hop count
549 # taking into account filtering and so on
550 foreach $dxchan (@dxchan) {
551 next if $dxchan == $main::me;
552 next if $dxchan == $self && $self->is_node;
553 next if $line =~ /PC61/ && !$dxchan->is_spider && !$dxchan->is_user;
554 $dxchan->dx_spot($line, $self->{isolate}, @_, $self->{call});
565 if ($self->{spotsfilter}) {
566 ($filter, $hops) = $self->{spotsfilter}->it(@_);
567 return unless $filter;
569 send_prot_line($self, $filter, $hops, $isolate, $line);
574 my ($self, $filter, $hops, $isolate, $line) = @_;
580 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
582 $routeit = adjust_hops($self, $line); # adjust its hop count by node name
583 return unless $routeit;
586 $self->send($routeit);
588 $self->send($routeit) unless $self->{isolate} || $isolate;
597 my @dxchan = DXChannel::get_all();
599 my @dxcc = ((Prefix::cty_data($_[6]))[0..2], (Prefix::cty_data($_[7]))[0..2]);
601 # send it if it isn't the except list and isn't isolated and still has a hop count
602 # taking into account filtering and so on
603 foreach $dxchan (@dxchan) {
604 next if $dxchan == $main::me;
605 next if $dxchan == $self && $self->is_node;
609 $dxchan->wwv($line, $self->{isolate}, @_, $self->{call}, @dxcc);
620 if ($self->{wwvfilter}) {
621 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_]);
622 return unless $filter;
624 send_prot_line($self, $filter, $hops, $isolate, $line)
631 my @dxchan = DXChannel::get_all();
633 my @dxcc = ((Prefix::cty_data($_[10]))[0..2], (Prefix::cty_data($_[11]))[0..2]);
635 # send it if it isn't the except list and isn't isolated and still has a hop count
636 # taking into account filtering and so on
637 foreach $dxchan (@dxchan) {
638 next if $dxchan == $main::me;
639 next if $dxchan == $self;
641 $dxchan->wcy($line, $self->{isolate}, @_, $self->{call}, @dxcc);
652 if ($self->{wcyfilter}) {
653 ($filter, $hops) = $self->{wcyfilter}->it(@_);
654 return unless $filter;
656 send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
663 my $from_pc9x = shift;
665 my @dxchan = DXChannel::get_all();
669 my $text = unpad($_[2]);
672 if ($_[3] eq '*') { # sysops
674 } elsif ($_[3] gt ' ') { # speciality list handling
675 my ($name) = split /\./, $_[3];
676 $target = "$name"; # put the rest in later (if bothered)
683 $target = "ALL" if !$target;
686 # obtain country codes etc
687 my @a = Prefix::cty_data($from);
688 my @b = Prefix::cty_data($_[4]);
689 if ($self->{inannfilter}) {
690 my ($filter, $hops) =
691 $self->{inannfilter}->it(@_, $self->{call},
693 @b[0..2], $a[3], $b[3]);
695 dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
700 # the sysop ('*') thing is an attempt to minimise the damage caused by non-updated PC93 generators
701 if (AnnTalk::dup($from, $target, $_[2]) || ($_[3] eq '*' && AnnTalk::dup($from, 'ALL', $_[2]))) {
702 my $dxchan = DXChannel::get($from);
703 if ($self == $main::me && $dxchan && $dxchan->is_user) {
704 if ($dxchan->priv < 5) {
705 $dxchan->send($dxchan->msg('dup'));
709 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
714 Log('ann', $target, $from, $text);
716 # send it if it isn't the except list and isn't isolated and still has a hop count
717 # taking into account filtering and so on
718 foreach $dxchan (@dxchan) {
719 next if $dxchan == $main::me;
720 next if $dxchan == $self && $self->is_node;
721 next if $from_pc9x && $dxchan->{do_pc9x};
722 next if $target eq 'LOCAL' && $dxchan->is_node;
723 $dxchan->announce($line, $self->{isolate}, $to, $target, $text, @_, $self->{call},
728 my $msgid = int rand(1000);
733 $msgid = 1 if $msgid > 999;
741 my $from_pc9x = shift;
743 my @dxchan = DXChannel::get_all();
746 my $text = unpad($_[2]);
750 # munge the group and recast the line if required
751 if ($target =~ s/\.LST$//) {
755 # obtain country codes etc
756 my @a = Prefix::cty_data($from);
757 my @b = Prefix::cty_data($_[4]);
758 if ($self->{inannfilter}) {
759 my ($filter, $hops) =
760 $self->{inannfilter}->it(@_, $self->{call},
762 @b[0..2], $a[3], $b[3]);
764 dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
769 if (AnnTalk::dup($from, $target, $_[2], $main::systime + $chatdupeage)) {
770 my $dxchan = DXChannel::get($from);
771 if ($self == $main::me && $dxchan && $dxchan->is_user) {
772 if ($dxchan->priv < 5) {
773 $dxchan->send($dxchan->msg('dup'));
777 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
783 Log('chat', $target, $from, $text);
785 # send it if it isn't the except list and isn't isolated and still has a hop count
786 # taking into account filtering and so on
787 foreach $dxchan (@dxchan) {
788 my $is_ak1a = $dxchan->is_ak1a;
790 if ($dxchan->is_node) {
791 next if $dxchan == $main::me;
792 next if $dxchan == $self;
793 next if $from_pc9x && $dxchan->{do_pc9x};
794 next unless $dxchan->is_spider || $is_ak1a;
795 next if $target eq 'LOCAL';
796 if (!$ak1a_line && $is_ak1a) {
797 $ak1a_line = pc12($_[0], $text, $_[1], "$target.LST");
801 $dxchan->chat($is_ak1a ? $ak1a_line : $line, $self->{isolate}, $target, $_[1],
802 $text, @_, $self->{call}, @a[0..2], @b[0..2]);
816 if ($self->{annfilter}) {
817 ($filter, $hops) = $self->{annfilter}->it(@_);
818 return unless $filter;
820 send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
833 if ($self->{annfilter}) {
834 ($filter, $hops) = $self->{annfilter}->it(@_);
835 return unless $filter;
837 if (($self->is_spider || $self->is_ak1a) && $_[1] ne $main::mycall) {
838 send_prot_line($self, $filter, $hops, $isolate, $line);
843 sub send_local_config
847 dbg('DXProt::send_local_config') if isdbg('trace');
855 if ($self->{isolate}) {
856 dbg("send_local_config: isolated");
857 @localnodes = ( $main::routeroot );
858 $self->send_route($main::mycall, \&pc19, 1, $main::routeroot);
859 } elsif ($self->{do_pc9x}) {
860 dbg("send_local_config: doing pc9x");
861 my $node = Route::Node::get($self->{call});
862 # $self->send_last_pc92_config($main::routeroot);
863 # $self->send(pc92a($main::routeroot, $node)) unless $main::routeroot->last_PC92C =~ /$self->{call}/;
864 $self->send(pc92a($main::routeroot, $node));
865 $self->send(pc92k($main::routeroot));
867 # create a list of all the nodes that are not connected to this connection
868 # and are not themselves isolated, this to make sure that isolated nodes
869 # don't appear outside of this node
871 dbg("send_local_config: traditional");
873 # send locally connected nodes
874 my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
875 @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan if @dxchan;
876 $self->send_route($main::mycall, \&pc19, scalar(@localnodes)+1, $main::routeroot, @localnodes);
879 my @rawintcalls = map { $_->nodes } @localnodes if @localnodes;
881 foreach $node (@rawintcalls) {
882 push @intcalls, $node if grep $_ && $node != $_, @intcalls;
884 my $ref = Route::Node::get($self->{call});
885 my @rnodes = $ref->nodes;
886 foreach $node (@intcalls) {
887 push @remotenodes, Route::Node::get($node) if grep $_ && $node != $_, @rnodes, @remotenodes;
889 $self->send_route($main::mycall, \&pc19, scalar(@remotenodes), @remotenodes);
892 # get all the users connected on the above nodes and send them out
893 unless ($self->{do_pc9x}) {
894 foreach $node ($main::routeroot, @localnodes, @remotenodes) {
896 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
897 $self->send_route($main::mycall, \&pc16, 1, $node, @rout) if @rout && $self->user->wantsendpc16;
899 dbg("sent a null value") if isdbg('chanerr');
905 sub gen_my_pc92_config
909 if ($node->{call} eq $main::mycall) {
910 clear_pc92_changes(); # remove any slugged data, we are generating it as now
911 my @dxchan = grep { $_->call ne $main::mycall && !$_->{isolate} } DXChannel::get_all();
912 dbg("ROUTE: all dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('routelow');
913 my @localnodes = map { my $r = Route::get($_->{call}); $r ? $r : () } @dxchan;
914 dbg("ROUTE: localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('routelow');
915 return pc92c($node, @localnodes);
917 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
918 return pc92c($node, @rout);
922 sub send_last_pc92_config
926 if (my $l = $node->last_PC92C) {
929 $self->send_pc92_config($node);
938 dbg('DXProt::send_pc92_config') if isdbg('trace');
940 $node->last_PC92C(gen_my_pc92_config($node));
941 $self->send($node->last_PC92C);
944 sub broadcast_pc92_update
949 dbg("ROUTE: broadcast_pc92_update $call") if isdbg('obscount');
951 my $nref = Route::Node::get($call);
953 dbg("ERROR: broadcast_pc92_update - Route::Node $call disappeared");
956 my $l = $nref->last_PC92C(gen_my_pc92_config($nref));
957 $nref->lastid(last_pc9x_id());
958 $main::me->broadcast_route_pc9x($main::mycall, undef, $l, 0);
959 $self->update_pc92_next;
962 sub broadcast_pc92_keepalive
967 dbg("ROUTE: broadcast_pc92_keepalive $call") if isdbg('obscount');
969 my $nref = Route::Node::get($call);
971 dbg("ERROR: broadcast_pc92_keepalive - Route::Node $call disappeared");
974 my $l = pc92k($nref);
975 $nref->lastid(last_pc9x_id());
976 $main::me->broadcast_route_pc9x($main::mycall, undef, $l, 0);
977 $self->update_pc92_keepalive;
980 sub time_out_pc92_routes
982 my @nodes = grep {$_->call ne $main::mycall && ($_->do_pc9x || $_->via_pc92)} Route::Node::get_all();
984 foreach my $n (@nodes) {
987 if (my $dxchan = DXChannel::get($n->call)) {
988 dbg("ROUTE: disconnecting local pc92 $dxchan->{call} on obscount") if isdbg('obscount');
992 my @parents = map {Route::Node::get($_)} $n->parents;
995 dbg("ROUTE: deleting pc92 $_->{call} from $n->{call} on obscount") if isdbg('obscount');
996 push @rdel, $n->del($_);
1000 dbg("ROUTE: obscount on $n->{call} now $o") if isdbg('obscount');
1004 $main::me->route_pc21($main::mycall, undef, $_) if $_;
1009 # route a message down an appropriate interface for a callsign
1011 # is called route(to, pcline);
1016 my ($self, $call, $line) = @_;
1018 if (ref $self && $call eq $self->{call}) {
1019 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1023 # always send it down the local interface if available
1024 my $dxchan = DXChannel::get($call);
1026 dbg("route: $call -> $dxchan->{call} direct" ) if isdbg('route');
1028 my $cl = Route::get($call);
1029 $dxchan = $cl->dxchan if $cl;
1031 if (ref $self && $dxchan eq $self) {
1032 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1035 dbg("route: $call -> $dxchan->{call} using normal route" ) if isdbg('route');
1040 my $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1042 $dxchan->send($routeit) unless $dxchan == $main::me;
1045 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
1050 # obtain the hops from the list for this callsign and pc no
1056 my $hops = $DXProt::hopcount{$pcno};
1057 $hops = $DXProt::def_hopcount if !$hops;
1062 # adjust the hop count on a per node basis using the user loadable
1063 # hop table if available or else decrement an existing one
1070 my $call = $self->{call};
1073 if (($hops) = $s =~ /\^H([-\d]+)\^?~?$/o) {
1074 my ($pcno) = $s =~ /^PC(\d\d)/o;
1075 confess "$call called adjust_hops with '$s'" unless $pcno;
1076 my $ref = $nodehops{$call} if %nodehops;
1078 my $newhops = $ref->{$pcno};
1079 return "" if defined $newhops && $newhops == 0;
1080 $newhops = $ref->{default} unless $newhops;
1081 return "" if defined $newhops && $newhops == 0;
1082 $newhops = $hops unless $newhops;
1083 return "" unless $newhops > 0;
1084 $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops != $hops;
1086 return "" unless $hops > 0;
1098 return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1099 do "$main::data/hop_table.pl";
1106 my ($self, $tonode, $fromnode, $user, $cmd) = @_;
1107 if ($tonode eq $main::mycall) {
1108 my $ref = DXUser::get_current($fromnode);
1109 my $cref = Route::Node::get($fromnode);
1110 Log('rcmd', 'in', ($ref->{priv}||0), $fromnode, $cmd);
1111 if ($cmd !~ /^\s*rcmd/i && $cref && $ref && $ref->homenode && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS!
1112 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
1113 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
1114 my $oldpriv = $self->{priv};
1115 $self->{priv} = $ref->{priv}; # assume the user's privilege level
1116 my @in = (DXCommandmode::run_cmd($self, $cmd));
1117 $self->{priv} = $oldpriv;
1118 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @in);
1119 delete $self->{remotecmd};
1121 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1124 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "your attempt is logged, Tut tut tut...!");
1127 my $ref = DXUser::get_current($tonode);
1128 if ($ref && $ref->is_clx) {
1129 $self->route($tonode, pc84($fromnode, $tonode, $user, $cmd));
1131 $self->route($tonode, pc34($fromnode, $tonode, $cmd));
1136 sub process_rcmd_reply
1138 my ($self, $tonode, $fromnode, $user, $line) = @_;
1139 if ($tonode eq $main::mycall) {
1140 my $s = $rcmds{$fromnode};
1142 my $dxchan = DXChannel::get($s->{call});
1143 my $ref = $user eq $tonode ? $dxchan : (DXChannel::get($user) || $dxchan);
1144 $ref->send($line) if $ref;
1145 delete $rcmds{$fromnode} if !$dxchan;
1147 # send unsolicited ones to the sysop
1148 my $dxchan = DXChannel::get($main::myalias);
1149 $dxchan->send($line) if $dxchan;
1152 my $ref = DXUser::get_current($tonode);
1153 if ($ref && $ref->is_clx) {
1154 $self->route($tonode, pc85($fromnode, $tonode, $user, $line));
1156 $self->route($tonode, pc35($fromnode, $tonode, $line));
1165 my $fromnode = shift;
1170 Log('rcmd', 'out', $fromnode, $line);
1171 if ($self->is_clx) {
1172 $self->send(pc85($main::mycall, $fromnode, $user, "$main::mycall:$line"));
1174 $self->send(pc35($main::mycall, $fromnode, "$main::mycall:$line"));
1179 # add a rcmd request to the rcmd queues
1182 my ($self, $to, $cmd) = @_;
1185 $r->{call} = $self->{call};
1186 $r->{t} = $main::systime;
1190 my $ref = Route::Node::get($to);
1191 my $dxchan = $ref->dxchan;
1192 if ($dxchan && $dxchan->is_clx) {
1193 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1195 route(undef, $to, pc34($main::mycall, $to, $cmd));
1202 my $pc39flag = shift || 0;
1203 my $call = $self->call;
1205 return if $self->{disconnecting}++;
1207 unless ($pc39flag == 1) {
1208 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1211 # get rid of any PC16/17/19
1212 eph_del_regex("^PC1[679]*$call");
1214 # do routing stuff, remove me from routing table
1215 my $node = Route::Node::get($call);
1217 Route::delete_interface($call);
1219 # unbusy and stop and outgoing mail
1220 my $mref = DXMsg::get_busy($call);
1221 $mref->stop_msg($call) if $mref;
1223 # remove outstanding pings
1224 delete $pings{$call};
1226 # I was the last node visited
1227 $self->user->node($main::mycall);
1229 # send info to all logged in thingies
1230 $self->tell_login('logoutn');
1232 Log('DXProt', $call . " Disconnected");
1234 $self->SUPER::disconnect;
1236 # here we determine what needs to go out of the routing table
1238 if ($node && $pc39flag != 2) {
1239 dbg('%Route::Node::List = ' . join(',', sort keys %Route::Node::list)) if isdbg('routedisc');
1241 @rout = $node->del($main::routeroot);
1243 dbg('@rout = ' . join(',', sort map {$_->call} @rout)) if isdbg('routedisc');
1245 # now we need to see what can't be routed anymore and came
1246 # in via this node (probably).
1248 while ($n != @rout) {
1250 for (Route::Node::get_all()) {
1251 unless ($_->dxchan) {
1252 push @rout, $_->delete;
1255 dbg('@rout = ' . join(',', sort map {$_->call} @rout)) if isdbg('routedisc');
1258 dbg('%Route::Node::List = ' . join(',', sort keys %Route::Node::list)) if isdbg('routedisc');
1260 # and all my ephemera as well
1263 eph_del_regex("^PC1[679].*$c");
1267 # broadcast to all other nodes that all the nodes connected to via me are gone
1268 unless ($pc39flag == 2) {
1269 $self->route_pc21($main::mycall, undef, @rout) if @rout;
1270 $self->route_pc92d($main::mycall, undef, $main::routeroot, $node) if $node;
1276 # send a talk message to this thingy
1280 my ($self, $from, $to, $via, $line, $origin) = @_;
1282 if ($self->{do_pc9x}) {
1283 $self->send(pc93($to, $from, $via, $line));
1285 $self->send(pc10($from, $to, $via, $line, $origin));
1287 Log('talk', $to, $from, '>' . ($via || $origin || $self->call), $line) unless $origin && $origin ne $main::mycall;
1290 # send it if it isn't the except list and isn't isolated and still has a hop count
1291 # taking into account filtering and so on
1297 my $generate = shift;
1298 my $no = shift; # the no of things to filter on
1300 my ($filter, $hops);
1303 for (; @_ && $no; $no--) {
1306 # don't send messages with $self's call in back to them
1307 if ($r->call eq $self->{call}) {
1308 dbg("PCPROT: trying to send $self->{call} back itself") if isdbg('chanerr');
1312 if (!$self->{isolate} && $self->{routefilter}) {
1315 ($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});
1319 dbg("PCPROT: $self->{call}/" . $r->call . " rejected by output filter") if isdbg('filter');
1322 dbg("was sent a null value") if isdbg('chanerr');
1325 push @rin, $r unless $self->{isolate} && $r->call ne $main::mycall;
1329 foreach my $line (&$generate(@rin, @_)) {
1332 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1334 $routeit = adjust_hops($self, $line); # adjust its hop count by node name
1335 next unless $routeit;
1338 $self->send($routeit);
1343 # broadcast everywhere
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');
1363 $dxchan->send_route($origin, $generate, @_);
1368 # broadcast to non-pc9x nodes
1369 sub broadcast_route_nopc9x
1373 my $generate = shift;
1375 my @dxchan = DXChannel::get_all_nodes();
1379 $line =~ /\^H(\d+)\^?\~?$/;
1380 return unless $1 > 0;
1382 unless ($self->{isolate}) {
1383 foreach $dxchan (@dxchan) {
1384 next if $dxchan == $self || $dxchan == $main::me;
1385 next if $origin eq $dxchan->{call}; # don't route some from this call back again.
1386 next unless $dxchan->isa('DXProt');
1387 next if $dxchan->{do_pc9x};
1388 if ($generate == \&pc16 || $generate==\&pc17) {
1389 next unless $dxchan->user->wantsendpc16;
1391 $dxchan->send_route($origin, $generate, @_);
1396 # this is only used for next door nodes on init
1401 return unless $self->{do_pc9x};
1404 my $generate = shift;
1405 my $no = shift; # the no of things to filter on
1408 $line = &$generate(@_);
1412 # broadcast only to pc9x nodes
1413 sub broadcast_route_pc9x
1417 my $generate = shift;
1420 my @dxchan = DXChannel::get_all_nodes();
1423 if ($origin eq $main::mycall && $generate && !$line) {
1424 $line = &$generate(@_);
1427 $line =~ /\^H(\d+)\^\~?$/;
1428 unless ($1 > 0 && $self->{isolate}) {
1429 foreach $dxchan (@dxchan) {
1430 next if $dxchan == $self || $dxchan == $main::me;
1431 next if $origin eq $dxchan->{call}; # don't route some from this call back again.
1432 next unless $dxchan->isa('DXProt');
1433 next unless $dxchan->{do_pc9x};
1435 $dxchan->send($line);
1443 return unless $self->user->wantpc16;
1446 broadcast_route_nopc9x($self, $origin, \&pc16, $line, 1, @_);
1452 return unless $self->user->wantpc16;
1455 broadcast_route_nopc9x($self, $origin, \&pc17, $line, 1, @_);
1463 broadcast_route_nopc9x($self, $origin, \&pc19, $line, scalar @_, @_);
1471 broadcast_route_nopc9x($self, $origin, \&pc21, $line, scalar @_, @_);
1479 broadcast_route($self, $origin, \&pc24, $line, 1, @_);
1487 broadcast_route($self, $origin, \&pc41, $line, 1, @_);
1490 # this is probably obsolete now
1497 broadcast_route($self, $origin, \&pc50, $line, 1, @_);
1505 broadcast_route_pc9x($self, $origin, \&pc92c, $line, 1, @_);
1513 broadcast_route_pc9x($self, $origin, \&pc92a, $line, 1, @_);
1521 broadcast_route_pc9x($self, $origin, \&pc92d, $line, 1, @_);
1528 my ($filter, $hops) = (1, 1);
1530 if ($self->{inroutefilter}) {
1531 ($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);
1532 dbg("PCPROT: $self->{call}/" . $r->call . ' rejected by in_filter_route') if !$filter && isdbg('chanerr');
1540 my $t = shift || $eph_restime;
1544 $s =~ s/\^H\d\d?\^?\~?$//;
1545 $r = 1 if exists $eph{$s}; # pump up the dup if it keeps circulating
1546 $eph{$s} = $main::systime + $t;
1547 dbg("PCPROT: emphemeral duplicate") if $r && isdbg('chanerr');
1555 while (($key, $val) = each %eph) {
1556 if ($key =~ m{$regex}) {
1566 while (($key, $val) = each %eph) {
1567 if ($main::systime >= $val) {
1578 while (($key, $val) = each %eph) {
1579 push @out, $key, $val;
1586 goto &DXCommandmode::run_cmd;
1590 # import any msgs in the chat directory
1591 # the messages are sent to the chat group which forms the
1592 # the first part of the name (eg: solar.1243.txt would be
1593 # sent to chat group SOLAR)
1595 # Each message found is sent: one non-blank line to one chat
1596 # message. So 4 lines = 4 chat messages.
1598 # The special name LOCAL is for local users ANN
1599 # The special name ALL is for ANN/FULL
1600 # The special name SYSOP is for ANN/SYSOP
1604 # are there any to do in this directory?
1605 return unless -d $chatimportfn;
1606 unless (opendir(DIR, $chatimportfn)) {
1607 dbg("can\'t open $chatimportfn $!") if isdbg('msg');
1608 Log('msg', "can\'t open $chatimportfn $!");
1612 my @names = readdir(DIR);
1615 foreach $name (@names) {
1616 next if $name =~ /^\./;
1617 my $splitit = $name =~ /^split/;
1618 my $fn = "$chatimportfn/$name";
1620 unless (open(MSG, $fn)) {
1621 dbg("can\'t open import file $fn $!") if isdbg('msg');
1622 Log('msg', "can\'t open import file $fn $!");
1626 my @msg = map { s/\r?\n$//; $_ } <MSG>;
1630 my @cat = split /\./, $name;
1631 my $target = uc $cat[0];
1633 foreach my $text (@msg) {
1634 next unless $text && $text !~ /^\s*#/;
1635 if ($target eq 'ALL' || $target eq 'LOCAL' || $target eq 'SYSOP' || $target eq 'WX') {
1636 my $sysop = uc $target eq 'SYSOP' ? '*' : ' ';
1637 my $wx = uc $target eq 'WX' ? '1' : '0';
1639 $via = '*' if $target eq 'ALL' || $target eq 'SYSOP';
1640 Log('ann', $target, $main::mycall, $text);
1641 $main::me->normal(DXProt::pc93($target, $main::mycall, $via, $text));
1643 DXCommandmode::send_chats($main::me, $target, $text);
1649 # start a pc92 find operation
1654 my $key = "$dxchan->{call}|$target";
1655 if ($pc92_find{$key}) {
1660 # function (not method) to handle pc92 find returns
1661 sub handle_pc92_find_reply
1663 my ($dxchan, $node, $from, $target, $flag, $ms) = @_;
1665 $dxchan->print_pc92_find_reply($node, $target, $flag, $ms) if $dxchan->can('print_pc92_find_return');