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 $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
49 $pc92_extnode_update_period $pc50_interval
50 $pc92_keepalive_period
53 $pc11_max_age = 1*3600; # the maximum age for an incoming 'real-time' pc11
54 $pc23_max_age = 1*3600; # the maximum age for an incoming 'real-time' pc23
56 $last_hour = time; # last time I did an hourly periodic update
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;
69 $eph_info_restime = 60*60;
70 $eph_pc15_restime = 6*60;
71 $eph_pc34_restime = 30;
74 $chatdupeage = 20 * 60;
75 $chatimportfn = "$main::root/chat_import";
76 $pc19_version = 5454; # the visible version no for outgoing PC19s generated from pc59
77 $pc92_update_period = 4*60*60; # the period between outgoing PC92 C updates
78 $pc92_short_update_period = 15*60; # shorten the update period after a connection or start up
79 $pc92_extnode_update_period = 1*60*60; # the update period for external nodes
80 $pc92_keepalive_period = 1*60*60; # frequency of PC92 K (keepalive) records
81 %pc92_find = (); # outstanding pc92 find operations
82 $pc92_find_timeout = 30; # maximum time to wait for a reply
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 || ($self->{do_pc9x} ? $pc92_update_period : $pc92_extnode_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');
222 sub update_pc92_keepalive
225 my $period = shift || $pc92_keepalive_period;
226 $self->{next_pc92_keepalive} = $main::systime + $period - int rand($period / 4);
227 dbg("ROUTE: update_pc92_keepalive: $self->{call} " . atime($self->{next_pc92_keepalive})) if isdbg('obscount');
232 do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
235 my $user = DXUser->get($main::mycall);
236 die "User $main::mycall not setup or disappeared RTFM" unless $user;
238 $myprot_version += $main::version*100;
239 $main::me = DXProt->new($main::mycall, 0, $user);
240 $main::me->{here} = 1;
241 $main::me->{state} = "indifferent";
242 $main::me->{sort} = 'S'; # S for spider
243 $main::me->{priv} = 9;
244 $main::me->{metric} = 0;
245 $main::me->{pingave} = 0;
246 $main::me->{registered} = 1;
247 $main::me->{version} = $main::version;
248 $main::me->{build} = "$main::subversion.$main::build";
249 $main::me->{do_pc9x} = 1;
250 $main::me->update_pc92_next($pc92_short_update_period);
251 $main::me->update_pc92_keepalive;
255 # obtain a new connection this is derived from dxchannel
260 my $self = DXChannel::alloc(@_);
262 # add this node to the table, the values get filled in later
266 # if we have an entry already, then send a PC21 to all connect
267 # old style connections, because we are about to get the real deal
268 if (my $ref = Route::Node::get($call)) {
269 dbg("ROUTE: $call is already in the routing table, deleting") if isdbg('route');
270 my @rout = $ref->delete;
271 $self->route_pc21($main::mycall, undef, @rout) if @rout;
273 $main::routeroot->add($call, '5000', Route::here(1)) if $call ne $main::mycall;
278 # this is how a pc connection starts (for an incoming connection)
279 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
280 # all the crap that comes between).
283 my ($self, $line, $sort) = @_;
284 my $call = $self->{call};
285 my $user = $self->{user};
288 my $host = $self->{conn}->{peerhost};
289 $host ||= "AGW Port #$self->{conn}->{agwport}" if exists $self->{conn}->{agwport};
292 Log('DXProt', "$call connected from $host");
294 # remember type of connection
295 $self->{consort} = $line;
296 $self->{outbound} = $sort eq 'O';
297 my $priv = $user->priv;
298 $priv = $user->priv(1) unless $priv;
299 $self->{priv} = $priv; # other clusters can always be 'normal' users
300 $self->{lang} = $user->lang || 'en';
301 $self->{isolate} = $user->{isolate};
302 $self->{consort} = $line; # save the connection type
306 # sort out registration
307 $self->{registered} = 1;
309 # get the output filters
310 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
311 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
312 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
313 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
314 $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', '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 # if there is no route input filter then specify a default one.
324 # obviously this can be changed later by the sysop.
325 if (!$self->{inroutefilter}) {
326 my $dxcc = $self->dxcc;
327 $Route::filterdef->cmd($self, 'route', 'accept', "input by_dxcc $dxcc" );
330 # set unbuffered and no echo
331 $self->send_now('B',"0");
332 $self->send_now('E',"0");
333 $self->conn->echo(0) if $self->conn->can('echo');
335 # ping neighbour node stuff
336 my $ping = $user->pingint;
337 $ping = $pingint unless defined $ping;
338 $self->{pingint} = $ping;
339 $self->{nopings} = $user->nopings || $obscount;
340 $self->{pingtime} = [ ];
341 $self->{pingave} = 999;
342 $self->{metric} ||= 100;
343 $self->{lastping} = $main::systime;
345 # send initialisation string
346 unless ($self->{outbound}) {
350 $self->state('init');
351 $self->{pc50_t} = $main::systime;
353 # send info to all logged in thingies
354 $self->tell_login('loginn');
356 # run a script send the output to the debug file
357 my $script = new Script(lc $call) || new Script('node_default');
358 $script->run($self) if $script;
360 # set up a config broadcast "quite soon" to converge tables quicker
361 $main::me->update_pc92_next($pc92_short_update_period);
362 $self->update_pc92_next($pc92_short_update_period);
364 # set next keepalive time
365 $self->update_pc92_keepalive;
369 # send outgoing 'challenge'
375 $self->send(pc18(($self->{isolate} || !$self->user->wantpc9x) ? "" : " pc9x"));
379 # This is the normal pcxx despatcher
383 my ($self, $line) = @_;
385 if ($line =~ '^<\w+\s' && $main::do_xml) {
386 DXXml::normal($self, $line);
390 my @field = split /\^/, $line;
391 return unless @field;
393 pop @field if $field[-1] eq '~';
395 # print join(',', @field), "\n";
398 # process PC frames, this will fail unless the frame starts PCnn
399 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
400 unless (defined $pcno && $pcno >= 10 && $pcno <= 99) {
401 dbg("PCPROT: unknown protocol") if isdbg('chanerr');
405 # check for and dump bad protocol messages
406 my $n = check($pcno, @field);
408 dbg("PCPROT: bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")") if isdbg('chanerr');
412 # modify the hop count here
413 if ($self != $main::me) {
414 if (my ($hops, $trail) = $line =~ /\^H(\d+)(\^?\~?)?$/) {
418 $line =~ s/\^H(\d+)(\^?\~?)?$/sprintf('^H%d%s', $hops, $trail)/e;
419 $field[-1] = "H$hops";
423 # send it out for processing
424 my $origin = $self->{call};
426 my $sub = "handle_$pcno";
428 if ($self->can($sub)) {
429 $self->$sub($pcno, $line, $origin, @field);
431 $self->handle_default($pcno, $line, $origin, @field);
436 # This is called from inside the main cluster processing loop and is used
437 # for despatching commands that are doing some long processing job
442 my @dxchan = DXChannel::get_all();
447 if ($t - $last10 >= 10) {
455 # send out a pc50 on EVERY channel all at once
456 if ($t >= $last_pc50 + $pc50_interval) {
457 $pc50s = pc50($main::me, scalar DXChannel::get_all_users);
462 foreach $dxchan (@dxchan) {
463 next unless $dxchan->is_node;
464 next if $dxchan == $main::me;
467 $dxchan->send($pc50s) if !$dxchan->{do_pc9x} && $pc50s;
469 # send a ping out on this channel
470 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
471 if ($dxchan->{nopings} <= 0) {
472 dbg("ROUTE: $dxchan->{call} disconnected on ping obscount") if isdbg('obscount');
475 DXXml::Ping::add($main::me, $dxchan->call);
476 $dxchan->{nopings} -= 1;
477 $dxchan->{lastping} = $t;
478 $dxchan->{lastping} += $dxchan->{pingint} / 2 unless @{$dxchan->{pingtime}};
479 dbg("ROUTE: $dxchan->{call} ping obscount = $dxchan->{nopings}") if isdbg('obscount');
486 # send out config broadcasts
487 foreach $dxchan (@dxchan) {
488 next unless $dxchan->is_node;
490 # send out a PC92 config record if required for me and
491 # all my non pc9x dependent nodes. But for dependent nodes we only do
492 # this if we have not seen any from anyone else for at least half
493 # of one update period. This should stop quite a bit of excess C
494 # records. Someone will win, it does not really matter who, because
495 # we always believe "us".
496 if ($main::systime >= $dxchan->{next_pc92_update}) {
497 if ($dxchan == $main::me || !$dxchan->{do_pc9x}) {
498 dbg("ROUTE: pc92 broadcast candidate: $dxchan->{call}") if isdbg('obscount');
499 my $ref = Route::Node::get($dxchan->{call});
500 if ($dxchan == $main::me || ($ref && ($ref->measure_pc9x_t($main::systime-$main::systime_daystart)) >= $pc92_extnode_update_period/2)) {
501 $dxchan->broadcast_pc92_update($dxchan->{call});
503 $dxchan->update_pc92_next;
506 $dxchan->update_pc92_next; # this won't actually do anything, it's just to be tidy
511 # do the keepalive for me, if required
512 if ($main::systime >= $main::me->{next_pc92_keepalive}) {
513 time_out_pc92_routes();
514 $main::me->broadcast_pc92_keepalive($main::mycall);
517 if ($pc92_slug_changes && $main::systime >= $last_pc92_slug + $pc92_slug_changes) {
518 my ($add, $del) = gen_pc92_changes();
519 $main::me->route_pc92d($main::mycall, undef, $main::routeroot, @$del) if @$del;
520 $main::me->route_pc92a($main::mycall, undef, $main::routeroot, @$add) if @$add;
521 clear_pc92_changes();
525 if ($main::systime - 3600 > $last_hour) {
526 $last_hour = $main::systime;
531 # finish up a pc context
535 # some active measures
543 my @dxchan = DXChannel::get_all();
546 # send it if it isn't the except list and isn't isolated and still has a hop count
547 # taking into account filtering and so on
548 foreach $dxchan (@dxchan) {
549 next if $dxchan == $main::me;
550 next if $dxchan == $self && $self->is_node;
551 next if $line =~ /PC61/ && !$dxchan->is_spider && !$dxchan->is_user;
552 $dxchan->dx_spot($line, $self->{isolate}, @_, $self->{call});
563 if ($self->{spotsfilter}) {
564 ($filter, $hops) = $self->{spotsfilter}->it(@_);
565 return unless $filter;
567 send_prot_line($self, $filter, $hops, $isolate, $line);
572 my ($self, $filter, $hops, $isolate, $line) = @_;
578 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
580 $routeit = adjust_hops($self, $line); # adjust its hop count by node name
581 return unless $routeit;
584 $self->send($routeit);
586 $self->send($routeit) unless $self->{isolate} || $isolate;
595 my @dxchan = DXChannel::get_all();
597 my @dxcc = ((Prefix::cty_data($_[6]))[0..2], (Prefix::cty_data($_[7]))[0..2]);
599 # send it if it isn't the except list and isn't isolated and still has a hop count
600 # taking into account filtering and so on
601 foreach $dxchan (@dxchan) {
602 next if $dxchan == $main::me;
603 next if $dxchan == $self && $self->is_node;
607 $dxchan->wwv($line, $self->{isolate}, @_, $self->{call}, @dxcc);
618 if ($self->{wwvfilter}) {
619 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_]);
620 return unless $filter;
622 send_prot_line($self, $filter, $hops, $isolate, $line)
629 my @dxchan = DXChannel::get_all();
631 my @dxcc = ((Prefix::cty_data($_[10]))[0..2], (Prefix::cty_data($_[11]))[0..2]);
633 # send it if it isn't the except list and isn't isolated and still has a hop count
634 # taking into account filtering and so on
635 foreach $dxchan (@dxchan) {
636 next if $dxchan == $main::me;
637 next if $dxchan == $self;
639 $dxchan->wcy($line, $self->{isolate}, @_, $self->{call}, @dxcc);
650 if ($self->{wcyfilter}) {
651 ($filter, $hops) = $self->{wcyfilter}->it(@_);
652 return unless $filter;
654 send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
661 my $from_pc9x = shift;
663 my @dxchan = DXChannel::get_all();
667 my $text = unpad($_[2]);
670 if ($_[3] eq '*') { # sysops
672 } elsif ($_[3] gt ' ') { # speciality list handling
673 my ($name) = split /\./, $_[3];
674 $target = "$name"; # put the rest in later (if bothered)
681 $target = "ALL" if !$target;
684 # obtain country codes etc
685 my @a = Prefix::cty_data($from);
686 my @b = Prefix::cty_data($_[4]);
687 if ($self->{inannfilter}) {
688 my ($filter, $hops) =
689 $self->{inannfilter}->it(@_, $self->{call},
691 @b[0..2], $a[3], $b[3]);
693 dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
698 # the sysop ('*') thing is an attempt to minimise the damage caused by non-updated PC93 generators
699 if (AnnTalk::dup($from, $target, $_[2]) || ($_[3] eq '*' && AnnTalk::dup($from, 'ALL', $_[2]))) {
700 my $dxchan = DXChannel::get($from);
701 if ($self == $main::me && $dxchan && $dxchan->is_user) {
702 if ($dxchan->priv < 5) {
703 $dxchan->send($dxchan->msg('dup'));
707 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
712 Log('ann', $target, $from, $text);
714 # send it if it isn't the except list and isn't isolated and still has a hop count
715 # taking into account filtering and so on
716 foreach $dxchan (@dxchan) {
717 next if $dxchan == $main::me;
718 next if $dxchan == $self && $self->is_node;
719 next if $from_pc9x && $dxchan->{do_pc9x};
720 next if $target eq 'LOCAL' && $dxchan->is_node;
721 $dxchan->announce($line, $self->{isolate}, $to, $target, $text, @_, $self->{call},
726 my $msgid = int rand(1000);
731 $msgid = 1 if $msgid > 999;
739 my $from_pc9x = shift;
741 my @dxchan = DXChannel::get_all();
744 my $text = unpad($_[2]);
748 # munge the group and recast the line if required
749 if ($target =~ s/\.LST$//) {
753 # obtain country codes etc
754 my @a = Prefix::cty_data($from);
755 my @b = Prefix::cty_data($_[4]);
756 if ($self->{inannfilter}) {
757 my ($filter, $hops) =
758 $self->{inannfilter}->it(@_, $self->{call},
760 @b[0..2], $a[3], $b[3]);
762 dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
767 if (AnnTalk::dup($from, $target, $_[2], $main::systime + $chatdupeage)) {
768 my $dxchan = DXChannel::get($from);
769 if ($self == $main::me && $dxchan && $dxchan->is_user) {
770 if ($dxchan->priv < 5) {
771 $dxchan->send($dxchan->msg('dup'));
775 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
781 Log('chat', $target, $from, $text);
783 # send it if it isn't the except list and isn't isolated and still has a hop count
784 # taking into account filtering and so on
785 foreach $dxchan (@dxchan) {
786 my $is_ak1a = $dxchan->is_ak1a;
788 if ($dxchan->is_node) {
789 next if $dxchan == $main::me;
790 next if $dxchan == $self;
791 next if $from_pc9x && $dxchan->{do_pc9x};
792 next unless $dxchan->is_spider || $is_ak1a;
793 next if $target eq 'LOCAL';
794 if (!$ak1a_line && $is_ak1a) {
795 $ak1a_line = pc12($_[0], $text, $_[1], "$target.LST");
799 $dxchan->chat($is_ak1a ? $ak1a_line : $line, $self->{isolate}, $target, $_[1],
800 $text, @_, $self->{call}, @a[0..2], @b[0..2]);
814 if ($self->{annfilter}) {
815 ($filter, $hops) = $self->{annfilter}->it(@_);
816 return unless $filter;
818 send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
831 if ($self->{annfilter}) {
832 ($filter, $hops) = $self->{annfilter}->it(@_);
833 return unless $filter;
835 if (($self->is_spider || $self->is_ak1a) && $_[1] ne $main::mycall) {
836 send_prot_line($self, $filter, $hops, $isolate, $line);
841 sub send_local_config
845 dbg('DXProt::send_local_config') if isdbg('trace');
853 if ($self->{isolate}) {
854 dbg("send_local_config: isolated");
855 @localnodes = ( $main::routeroot );
856 $self->send_route($main::mycall, \&pc19, 1, $main::routeroot);
857 } elsif ($self->{do_pc9x}) {
858 dbg("send_local_config: doing pc9x");
859 my $node = Route::Node::get($self->{call});
860 # $self->send_last_pc92_config($main::routeroot);
861 # $self->send(pc92a($main::routeroot, $node)) unless $main::routeroot->last_PC92C =~ /$self->{call}/;
862 $self->send(pc92a($main::routeroot, $node));
863 $self->send(pc92k($main::routeroot));
865 # create a list of all the nodes that are not connected to this connection
866 # and are not themselves isolated, this to make sure that isolated nodes
867 # don't appear outside of this node
869 dbg("send_local_config: traditional");
871 # send locally connected nodes
872 my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
873 @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan if @dxchan;
874 $self->send_route($main::mycall, \&pc19, scalar(@localnodes)+1, $main::routeroot, @localnodes);
877 my @rawintcalls = map { $_->nodes } @localnodes if @localnodes;
879 foreach $node (@rawintcalls) {
880 push @intcalls, $node if grep $_ && $node != $_, @intcalls;
882 my $ref = Route::Node::get($self->{call});
883 my @rnodes = $ref->nodes;
884 foreach $node (@intcalls) {
885 push @remotenodes, Route::Node::get($node) if grep $_ && $node != $_, @rnodes, @remotenodes;
887 $self->send_route($main::mycall, \&pc19, scalar(@remotenodes), @remotenodes);
890 # get all the users connected on the above nodes and send them out
891 unless ($self->{do_pc9x}) {
892 foreach $node ($main::routeroot, @localnodes, @remotenodes) {
894 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
895 $self->send_route($main::mycall, \&pc16, 1, $node, @rout) if @rout && $self->user->wantsendpc16;
897 dbg("sent a null value") if isdbg('chanerr');
903 sub gen_my_pc92_config
907 if ($node->{call} eq $main::mycall) {
908 clear_pc92_changes(); # remove any slugged data, we are generating it as now
909 my @dxchan = grep { $_->call ne $main::mycall && !$_->{isolate} } DXChannel::get_all();
910 dbg("ROUTE: all dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('routelow');
911 my @localnodes = map { my $r = Route::get($_->{call}); $r ? $r : () } @dxchan;
912 dbg("ROUTE: localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('routelow');
913 return pc92c($node, @localnodes);
915 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
916 return pc92c($node, @rout);
920 sub send_last_pc92_config
924 if (my $l = $node->last_PC92C) {
927 $self->send_pc92_config($node);
936 dbg('DXProt::send_pc92_config') if isdbg('trace');
938 $node->last_PC92C(gen_my_pc92_config($node));
939 $self->send($node->last_PC92C);
942 sub broadcast_pc92_update
947 dbg("ROUTE: broadcast_pc92_update $call") if isdbg('obscount');
949 my $nref = Route::Node::get($call);
951 dbg("ERROR: broadcast_pc92_update - Route::Node $call disappeared");
954 my $l = $nref->last_PC92C(gen_my_pc92_config($nref));
955 $nref->lastid(last_pc9x_id());
956 $main::me->broadcast_route_pc9x($main::mycall, undef, $l, 0);
957 $self->update_pc92_next;
960 sub broadcast_pc92_keepalive
965 dbg("ROUTE: broadcast_pc92_keepalive $call") if isdbg('obscount');
967 my $nref = Route::Node::get($call);
969 dbg("ERROR: broadcast_pc92_keepalive - Route::Node $call disappeared");
972 my $l = pc92k($nref);
973 $nref->lastid(last_pc9x_id());
974 $main::me->broadcast_route_pc9x($main::mycall, undef, $l, 0);
975 $self->update_pc92_keepalive;
978 sub time_out_pc92_routes
980 my @nodes = grep {$_->call ne $main::mycall && ($_->do_pc9x || $_->via_pc92)} Route::Node::get_all();
982 foreach my $n (@nodes) {
985 if (my $dxchan = DXChannel::get($n->call)) {
986 dbg("ROUTE: disconnecting local pc92 $dxchan->{call} on obscount") if isdbg('obscount');
990 my @parents = map {Route::Node::get($_)} $n->parents;
993 dbg("ROUTE: deleting pc92 $_->{call} from $n->{call} on obscount") if isdbg('obscount');
994 push @rdel, $n->del($_);
998 dbg("ROUTE: obscount on $n->{call} now $o") if isdbg('obscount');
1002 $main::me->route_pc21($main::mycall, undef, $_) if $_;
1007 # route a message down an appropriate interface for a callsign
1009 # is called route(to, pcline);
1014 my ($self, $call, $line) = @_;
1016 if (ref $self && $call eq $self->{call}) {
1017 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1021 # always send it down the local interface if available
1022 my $dxchan = DXChannel::get($call);
1024 dbg("route: $call -> $dxchan->{call} direct" ) if isdbg('route');
1026 my $cl = Route::get($call);
1027 $dxchan = $cl->dxchan if $cl;
1029 if (ref $self && $dxchan eq $self) {
1030 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1033 dbg("route: $call -> $dxchan->{call} using normal route" ) if isdbg('route');
1037 # try the backstop method
1039 my $rcall = RouteDB::get($call);
1041 if ($self && $rcall eq $self->{call}) {
1042 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1045 $dxchan = DXChannel::get($rcall);
1046 dbg("route: $call -> $rcall using RouteDB" ) if isdbg('route') && $dxchan;
1051 my $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1053 $dxchan->send($routeit) unless $dxchan == $main::me;
1056 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
1061 # obtain the hops from the list for this callsign and pc no
1067 my $hops = $DXProt::hopcount{$pcno};
1068 $hops = $DXProt::def_hopcount if !$hops;
1073 # adjust the hop count on a per node basis using the user loadable
1074 # hop table if available or else decrement an existing one
1081 my $call = $self->{call};
1084 if (($hops) = $s =~ /\^H([-\d]+)\^?~?$/o) {
1085 my ($pcno) = $s =~ /^PC(\d\d)/o;
1086 confess "$call called adjust_hops with '$s'" unless $pcno;
1087 my $ref = $nodehops{$call} if %nodehops;
1089 my $newhops = $ref->{$pcno};
1090 return "" if defined $newhops && $newhops == 0;
1091 $newhops = $ref->{default} unless $newhops;
1092 return "" if defined $newhops && $newhops == 0;
1093 $newhops = $hops unless $newhops;
1094 return "" unless $newhops > 0;
1095 $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops != $hops;
1097 return "" unless $hops > 0;
1109 return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1110 do "$main::data/hop_table.pl";
1117 my ($self, $tonode, $fromnode, $user, $cmd) = @_;
1118 if ($tonode eq $main::mycall) {
1119 my $ref = DXUser->get_current($fromnode);
1120 my $cref = Route::Node::get($fromnode);
1121 Log('rcmd', 'in', $ref->{priv}, $fromnode, $cmd);
1122 if ($cmd !~ /^\s*rcmd/i && $cref && $ref && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS!
1123 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
1124 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
1125 my $oldpriv = $self->{priv};
1126 $self->{priv} = $ref->{priv}; # assume the user's privilege level
1127 my @in = (DXCommandmode::run_cmd($self, $cmd));
1128 $self->{priv} = $oldpriv;
1129 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @in);
1130 delete $self->{remotecmd};
1132 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1135 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "your attempt is logged, Tut tut tut...!");
1138 my $ref = DXUser->get_current($tonode);
1139 if ($ref && $ref->is_clx) {
1140 $self->route($tonode, pc84($fromnode, $tonode, $user, $cmd));
1142 $self->route($tonode, pc34($fromnode, $tonode, $cmd));
1147 sub process_rcmd_reply
1149 my ($self, $tonode, $fromnode, $user, $line) = @_;
1150 if ($tonode eq $main::mycall) {
1151 my $s = $rcmds{$fromnode};
1153 my $dxchan = DXChannel::get($s->{call});
1154 my $ref = $user eq $tonode ? $dxchan : (DXChannel::get($user) || $dxchan);
1155 $ref->send($line) if $ref;
1156 delete $rcmds{$fromnode} if !$dxchan;
1158 # send unsolicited ones to the sysop
1159 my $dxchan = DXChannel::get($main::myalias);
1160 $dxchan->send($line) if $dxchan;
1163 my $ref = DXUser->get_current($tonode);
1164 if ($ref && $ref->is_clx) {
1165 $self->route($tonode, pc85($fromnode, $tonode, $user, $line));
1167 $self->route($tonode, pc35($fromnode, $tonode, $line));
1176 my $fromnode = shift;
1181 Log('rcmd', 'out', $fromnode, $line);
1182 if ($self->is_clx) {
1183 $self->send(pc85($main::mycall, $fromnode, $user, "$main::mycall:$line"));
1185 $self->send(pc35($main::mycall, $fromnode, "$main::mycall:$line"));
1190 # add a rcmd request to the rcmd queues
1193 my ($self, $to, $cmd) = @_;
1196 $r->{call} = $self->{call};
1197 $r->{t} = $main::systime;
1201 my $ref = Route::Node::get($to);
1202 my $dxchan = $ref->dxchan;
1203 if ($dxchan && $dxchan->is_clx) {
1204 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1206 route(undef, $to, pc34($main::mycall, $to, $cmd));
1213 my $pc39flag = shift || 0;
1214 my $call = $self->call;
1216 return if $self->{disconnecting}++;
1218 unless ($pc39flag == 1) {
1219 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1222 # get rid of any PC16/17/19
1223 eph_del_regex("^PC1[679]*$call");
1225 # do routing stuff, remove me from routing table
1226 my $node = Route::Node::get($call);
1228 RouteDB::delete_interface($call);
1230 # unbusy and stop and outgoing mail
1231 my $mref = DXMsg::get_busy($call);
1232 $mref->stop_msg($call) if $mref;
1234 # remove outstanding pings
1235 delete $pings{$call};
1237 # I was the last node visited
1238 $self->user->node($main::mycall);
1240 # send info to all logged in thingies
1241 $self->tell_login('logoutn');
1243 Log('DXProt', $call . " Disconnected");
1245 $self->SUPER::disconnect;
1247 # here we determine what needs to go out of the routing table
1249 if ($node && $pc39flag != 2) {
1250 dbg('%Route::Node::List = ' . join(',', sort keys %Route::Node::list)) if isdbg('routedisc');
1252 @rout = $node->del($main::routeroot);
1254 dbg('@rout = ' . join(',', sort map {$_->call} @rout)) if isdbg('routedisc');
1256 # now we need to see what can't be routed anymore and came
1257 # in via this node (probably).
1259 while ($n != @rout) {
1261 for (Route::Node::get_all()) {
1262 unless ($_->dxchan) {
1263 push @rout, $_->delete;
1266 dbg('@rout = ' . join(',', sort map {$_->call} @rout)) if isdbg('routedisc');
1269 dbg('%Route::Node::List = ' . join(',', sort keys %Route::Node::list)) if isdbg('routedisc');
1271 # and all my ephemera as well
1274 eph_del_regex("^PC1[679].*$c");
1278 # broadcast to all other nodes that all the nodes connected to via me are gone
1279 unless ($pc39flag == 2) {
1280 $self->route_pc21($main::mycall, undef, @rout) if @rout;
1281 $self->route_pc92d($main::mycall, undef, $main::routeroot, $node) if $node;
1287 # send a talk message to this thingy
1291 my ($self, $from, $to, $via, $line, $origin) = @_;
1293 if ($self->{do_pc9x}) {
1294 $self->send(pc93($to, $from, $via, $line));
1296 $self->send(pc10($from, $to, $via, $line, $origin));
1298 Log('talk', $to, $from, '>' . ($via || $origin || $self->call), $line) unless $origin && $origin ne $main::mycall;
1301 # send it if it isn't the except list and isn't isolated and still has a hop count
1302 # taking into account filtering and so on
1308 my $generate = shift;
1309 my $no = shift; # the no of things to filter on
1311 my ($filter, $hops);
1314 for (; @_ && $no; $no--) {
1317 # don't send messages with $self's call in back to them
1318 if ($r->call eq $self->{call}) {
1319 dbg("PCPROT: trying to send $self->{call} back itself") if isdbg('chanerr');
1323 if (!$self->{isolate} && $self->{routefilter}) {
1326 ($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});
1330 dbg("PCPROT: $self->{call}/" . $r->call . " rejected by output filter") if isdbg('filter');
1333 dbg("was sent a null value") if isdbg('chanerr');
1336 push @rin, $r unless $self->{isolate} && $r->call ne $main::mycall;
1340 foreach my $line (&$generate(@rin, @_)) {
1343 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1345 $routeit = adjust_hops($self, $line); # adjust its hop count by node name
1346 next unless $routeit;
1349 $self->send($routeit);
1354 # broadcast everywhere
1359 my $generate = shift;
1361 my @dxchan = DXChannel::get_all_nodes();
1365 $line =~ /\^H(\d+)\^?\~?$/;
1366 return unless $1 > 0;
1368 unless ($self->{isolate}) {
1369 foreach $dxchan (@dxchan) {
1370 next if $dxchan == $self || $dxchan == $main::me;
1371 next if $origin eq $dxchan->{call}; # don't route some from this call back again.
1372 next unless $dxchan->isa('DXProt');
1374 $dxchan->send_route($origin, $generate, @_);
1379 # broadcast to non-pc9x nodes
1380 sub broadcast_route_nopc9x
1384 my $generate = shift;
1386 my @dxchan = DXChannel::get_all_nodes();
1390 $line =~ /\^H(\d+)\^?\~?$/;
1391 return unless $1 > 0;
1393 unless ($self->{isolate}) {
1394 foreach $dxchan (@dxchan) {
1395 next if $dxchan == $self || $dxchan == $main::me;
1396 next if $origin eq $dxchan->{call}; # don't route some from this call back again.
1397 next unless $dxchan->isa('DXProt');
1398 next if $dxchan->{do_pc9x};
1399 if ($generate == \&pc16 || $generate==\&pc17) {
1400 next unless $dxchan->user->wantsendpc16;
1402 $dxchan->send_route($origin, $generate, @_);
1407 # this is only used for next door nodes on init
1412 return unless $self->{do_pc9x};
1415 my $generate = shift;
1416 my $no = shift; # the no of things to filter on
1419 $line = &$generate(@_);
1423 # broadcast only to pc9x nodes
1424 sub broadcast_route_pc9x
1428 my $generate = shift;
1431 my @dxchan = DXChannel::get_all_nodes();
1434 if ($origin eq $main::mycall && $generate && !$line) {
1435 $line = &$generate(@_);
1438 $line =~ /\^H(\d+)\^\~?$/;
1439 unless ($1 > 0 && $self->{isolate}) {
1440 foreach $dxchan (@dxchan) {
1441 next if $dxchan == $self || $dxchan == $main::me;
1442 next if $origin eq $dxchan->{call}; # don't route some from this call back again.
1443 next unless $dxchan->isa('DXProt');
1444 next unless $dxchan->{do_pc9x};
1446 $dxchan->send($line);
1454 return unless $self->user->wantpc16;
1457 broadcast_route_nopc9x($self, $origin, \&pc16, $line, 1, @_);
1463 return unless $self->user->wantpc16;
1466 broadcast_route_nopc9x($self, $origin, \&pc17, $line, 1, @_);
1474 broadcast_route_nopc9x($self, $origin, \&pc19, $line, scalar @_, @_);
1482 broadcast_route_nopc9x($self, $origin, \&pc21, $line, scalar @_, @_);
1490 broadcast_route($self, $origin, \&pc24, $line, 1, @_);
1498 broadcast_route($self, $origin, \&pc41, $line, 1, @_);
1501 # this is probably obsolete now
1508 broadcast_route($self, $origin, \&pc50, $line, 1, @_);
1516 broadcast_route_pc9x($self, $origin, \&pc92c, $line, 1, @_);
1524 broadcast_route_pc9x($self, $origin, \&pc92a, $line, 1, @_);
1532 broadcast_route_pc9x($self, $origin, \&pc92d, $line, 1, @_);
1539 my ($filter, $hops) = (1, 1);
1541 if ($self->{inroutefilter}) {
1542 ($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);
1543 dbg("PCPROT: $self->{call}/" . $r->call . ' rejected by in_filter_route') if !$filter && isdbg('chanerr');
1551 my $t = shift || $eph_restime;
1555 $s =~ s/\^H\d\d?\^?\~?$//;
1556 $r = 1 if exists $eph{$s}; # pump up the dup if it keeps circulating
1557 $eph{$s} = $main::systime + $t;
1558 dbg("PCPROT: emphemeral duplicate") if $r && isdbg('chanerr');
1566 while (($key, $val) = each %eph) {
1567 if ($key =~ m{$regex}) {
1577 while (($key, $val) = each %eph) {
1578 if ($main::systime >= $val) {
1589 while (($key, $val) = each %eph) {
1590 push @out, $key, $val;
1597 goto &DXCommandmode::run_cmd;
1601 # import any msgs in the chat directory
1602 # the messages are sent to the chat group which forms the
1603 # the first part of the name (eg: solar.1243.txt would be
1604 # sent to chat group SOLAR)
1606 # Each message found is sent: one non-blank line to one chat
1607 # message. So 4 lines = 4 chat messages.
1609 # The special name LOCAL is for local users ANN
1610 # The special name ALL is for ANN/FULL
1611 # The special name SYSOP is for ANN/SYSOP
1615 # are there any to do in this directory?
1616 return unless -d $chatimportfn;
1617 unless (opendir(DIR, $chatimportfn)) {
1618 dbg("can\'t open $chatimportfn $!") if isdbg('msg');
1619 Log('msg', "can\'t open $chatimportfn $!");
1623 my @names = readdir(DIR);
1626 foreach $name (@names) {
1627 next if $name =~ /^\./;
1628 my $splitit = $name =~ /^split/;
1629 my $fn = "$chatimportfn/$name";
1631 unless (open(MSG, $fn)) {
1632 dbg("can\'t open import file $fn $!") if isdbg('msg');
1633 Log('msg', "can\'t open import file $fn $!");
1637 my @msg = map { s/\r?\n$//; $_ } <MSG>;
1641 my @cat = split /\./, $name;
1642 my $target = uc $cat[0];
1644 foreach my $text (@msg) {
1645 next unless $text && $text !~ /^\s*#/;
1646 if ($target eq 'ALL' || $target eq 'LOCAL' || $target eq 'SYSOP' || $target eq 'WX') {
1647 my $sysop = uc $target eq 'SYSOP' ? '*' : ' ';
1648 my $wx = uc $target eq 'WX' ? '1' : '0';
1650 $via = '*' if $target eq 'ALL' || $target eq 'SYSOP';
1651 Log('ann', $target, $main::mycall, $text);
1652 $main::me->normal(DXProt::pc93($target, $main::mycall, $via, $text));
1654 DXCommandmode::send_chats($main::me, $target, $text);
1660 # start a pc92 find operation
1665 my $key = "$dxchan->{call}|$target";
1666 if ($pc92_find{$key}) {
1671 # function (not method) to handle pc92 find returns
1672 sub handle_pc92_find_reply
1674 my ($dxchan, $node, $from, $target, $flag, $ms) = @_;
1676 $dxchan->print_pc92_find_reply($node, $target, $flag, $ms) if $dxchan->can('print_pc92_find_return');