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};
315 $self->{pc92filter} = Filter::read_in('pc92', $call, 0) || Filter::read_in('pc92', 'node_default', 0) unless $self->{isolate} ;
318 # get the INPUT filters (these only pertain to Clusters)
319 $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
320 $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
321 $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
322 $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
323 $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1) unless $self->{isolate};
324 $self->{inpc92filter} = Filter::read_in('pc92', $call, 0) || Filter::read_in('pc92', 'node_default', 0) unless $self->{isolate} ;
327 # set unbuffered and no echo
328 $self->send_now('B',"0");
329 $self->send_now('E',"0");
330 $self->conn->echo(0) if $self->conn->can('echo');
332 # ping neighbour node stuff
333 my $ping = $user->pingint;
334 $ping = $pingint unless defined $ping;
335 $self->{pingint} = $ping;
336 $self->{nopings} = $user->nopings || $obscount;
337 $self->{pingtime} = [ ];
338 $self->{pingave} = 999;
339 $self->{metric} ||= 100;
340 $self->{lastping} = $main::systime;
342 # send initialisation string
343 unless ($self->{outbound}) {
347 $self->state('init');
348 $self->{pc50_t} = $main::systime;
350 # send info to all logged in thingies
351 $self->tell_login('loginn');
353 # run a script send the output to the debug file
354 my $script = new Script(lc $call) || new Script('node_default');
355 $script->run($self) if $script;
357 # set up a config broadcast "quite soon" to converge tables quicker
358 $main::me->update_pc92_next($pc92_short_update_period);
359 $self->update_pc92_next($pc92_short_update_period);
361 # set next keepalive time
362 $self->update_pc92_keepalive;
366 # send outgoing 'challenge'
372 $self->send(pc18(($self->{isolate} || !$self->user->wantpc9x) ? "" : " pc9x"));
376 # This is the normal pcxx despatcher
380 my ($self, $line) = @_;
382 if ($line =~ '^<\w+\s' && $main::do_xml) {
383 DXXml::normal($self, $line);
387 my @field = split /\^/, $line;
388 return unless @field;
390 pop @field if $field[-1] eq '~';
392 # print join(',', @field), "\n";
395 # process PC frames, this will fail unless the frame starts PCnn
396 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
397 unless (defined $pcno && $pcno >= 10 && $pcno <= 99) {
398 dbg("PCPROT: unknown protocol") if isdbg('chanerr');
402 # check for and dump bad protocol messages
403 my $n = check($pcno, @field);
405 dbg("PCPROT: bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")") if isdbg('chanerr');
409 # modify the hop count here
410 if ($self != $main::me) {
411 if (my ($hops, $trail) = $line =~ /\^H(\d+)(\^?\~?)?$/) {
415 $line =~ s/\^H(\d+)(\^?\~?)?$/sprintf('^H%d%s', $hops, $trail)/e;
416 $field[-1] = "H$hops";
420 if (defined &Local::pcprot) {
422 eval { $r = Local::pcprot($self, $pcno, $line, @field); };
423 return if $r; # i.e don't process it
426 # send it out for processing
427 my $origin = $self->{call};
429 my $sub = "handle_$pcno";
431 if ($self->can($sub)) {
432 $self->$sub($pcno, $line, $origin, @field);
434 $self->handle_default($pcno, $line, $origin, @field);
439 # This is called from inside the main cluster processing loop and is used
440 # for despatching commands that are doing some long processing job
445 my @dxchan = DXChannel::get_all();
450 if ($t - $last10 >= 10) {
458 # send out a pc50 on EVERY channel all at once
459 if ($t >= $last_pc50 + $pc50_interval) {
460 $pc50s = pc50($main::me, scalar DXChannel::get_all_users);
465 foreach $dxchan (@dxchan) {
466 next unless $dxchan->is_node;
467 next if $dxchan == $main::me;
470 $dxchan->send($pc50s) if !$dxchan->{do_pc9x} && $pc50s;
472 # send a ping out on this channel
473 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
474 if ($dxchan->{nopings} <= 0) {
475 dbg("ROUTE: $dxchan->{call} disconnected on ping obscount") if isdbg('obscount');
478 DXXml::Ping::add($main::me, $dxchan->call);
479 $dxchan->{nopings} -= 1;
480 $dxchan->{lastping} = $t;
481 $dxchan->{lastping} += $dxchan->{pingint} / 2 unless @{$dxchan->{pingtime}};
482 dbg("ROUTE: $dxchan->{call} ping obscount = $dxchan->{nopings}") if isdbg('obscount');
489 # send out config broadcasts
490 foreach $dxchan (@dxchan) {
491 next unless $dxchan->is_node;
493 # send out a PC92 config record if required for me and
494 # all my non pc9x dependent nodes. But for dependent nodes we only do
495 # this if we have not seen any from anyone else for at least half
496 # of one update period. This should stop quite a bit of excess C
497 # records. Someone will win, it does not really matter who, because
498 # we always believe "us".
499 if ($main::systime >= $dxchan->{next_pc92_update}) {
500 if ($dxchan == $main::me || !$dxchan->{do_pc9x}) {
501 dbg("ROUTE: pc92 broadcast candidate: $dxchan->{call}") if isdbg('obscount');
502 my $ref = Route::Node::get($dxchan->{call});
503 if ($dxchan == $main::me || ($ref && ($ref->measure_pc9x_t($main::systime-$main::systime_daystart)) >= $pc92_extnode_update_period/2)) {
504 $dxchan->broadcast_pc92_update($dxchan->{call});
506 $dxchan->update_pc92_next;
509 $dxchan->update_pc92_next; # this won't actually do anything, it's just to be tidy
514 # do the keepalive for me, if required
515 if ($main::systime >= $main::me->{next_pc92_keepalive}) {
516 time_out_pc92_routes();
517 $main::me->broadcast_pc92_keepalive($main::mycall);
520 if ($pc92_slug_changes && $main::systime >= $last_pc92_slug + $pc92_slug_changes) {
521 my ($add, $del) = gen_pc92_changes();
522 $main::me->route_pc92d($main::mycall, undef, $main::routeroot, @$del) if @$del;
523 $main::me->route_pc92a($main::mycall, undef, $main::routeroot, @$add) if @$add;
524 clear_pc92_changes();
528 if ($main::systime - 3600 > $last_hour) {
529 $last_hour = $main::systime;
534 # finish up a pc context
538 # some active measures
546 my @dxchan = DXChannel::get_all();
549 # send it if it isn't the except list and isn't isolated and still has a hop count
550 # taking into account filtering and so on
551 foreach $dxchan (@dxchan) {
552 next if $dxchan == $main::me;
553 next if $dxchan == $self && $self->is_node;
554 next if $line =~ /PC61/ && !$dxchan->is_spider && !$dxchan->is_user;
555 $dxchan->dx_spot($line, $self->{isolate}, @_, $self->{call});
566 if ($self->{spotsfilter}) {
567 ($filter, $hops) = $self->{spotsfilter}->it(@_);
568 return unless $filter;
570 send_prot_line($self, $filter, $hops, $isolate, $line);
575 my ($self, $filter, $hops, $isolate, $line) = @_;
581 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
583 $routeit = adjust_hops($self, $line); # adjust its hop count by node name
584 return unless $routeit;
587 $self->send($routeit);
589 $self->send($routeit) unless $self->{isolate} || $isolate;
598 my @dxchan = DXChannel::get_all();
600 my @dxcc = ((Prefix::cty_data($_[6]))[0..2], (Prefix::cty_data($_[7]))[0..2]);
602 # send it if it isn't the except list and isn't isolated and still has a hop count
603 # taking into account filtering and so on
604 foreach $dxchan (@dxchan) {
605 next if $dxchan == $main::me;
606 next if $dxchan == $self && $self->is_node;
610 $dxchan->wwv($line, $self->{isolate}, @_, $self->{call}, @dxcc);
621 if ($self->{wwvfilter}) {
622 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_]);
623 return unless $filter;
625 send_prot_line($self, $filter, $hops, $isolate, $line)
632 my @dxchan = DXChannel::get_all();
634 my @dxcc = ((Prefix::cty_data($_[10]))[0..2], (Prefix::cty_data($_[11]))[0..2]);
636 # send it if it isn't the except list and isn't isolated and still has a hop count
637 # taking into account filtering and so on
638 foreach $dxchan (@dxchan) {
639 next if $dxchan == $main::me;
640 next if $dxchan == $self;
642 $dxchan->wcy($line, $self->{isolate}, @_, $self->{call}, @dxcc);
653 if ($self->{wcyfilter}) {
654 ($filter, $hops) = $self->{wcyfilter}->it(@_);
655 return unless $filter;
657 send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
664 my $from_pc9x = shift;
666 my @dxchan = DXChannel::get_all();
670 my $text = unpad($_[2]);
673 if ($_[3] eq '*') { # sysops
675 } elsif ($_[3] gt ' ') { # speciality list handling
676 my ($name) = split /\./, $_[3];
677 $target = "$name"; # put the rest in later (if bothered)
684 $target = "ALL" if !$target;
687 # obtain country codes etc
688 my @a = Prefix::cty_data($from);
689 my @b = Prefix::cty_data($_[4]);
690 if ($self->{inannfilter}) {
691 my ($filter, $hops) =
692 $self->{inannfilter}->it(@_, $self->{call},
694 @b[0..2], $a[3], $b[3]);
696 dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
701 # the sysop ('*') thing is an attempt to minimise the damage caused by non-updated PC93 generators
702 if (AnnTalk::dup($from, $target, $_[2]) || ($_[3] eq '*' && AnnTalk::dup($from, 'ALL', $_[2]))) {
703 my $dxchan = DXChannel::get($from);
704 if ($self == $main::me && $dxchan && $dxchan->is_user) {
705 if ($dxchan->priv < 5) {
706 $dxchan->send($dxchan->msg('dup'));
710 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
715 Log('ann', $target, $from, $text);
717 # send it if it isn't the except list and isn't isolated and still has a hop count
718 # taking into account filtering and so on
719 foreach $dxchan (@dxchan) {
720 next if $dxchan == $main::me;
721 next if $dxchan == $self && $self->is_node;
722 next if $from_pc9x && $dxchan->{do_pc9x};
723 next if $target eq 'LOCAL' && $dxchan->is_node;
724 $dxchan->announce($line, $self->{isolate}, $to, $target, $text, @_, $self->{call},
729 my $msgid = int rand(1000);
734 $msgid = 1 if $msgid > 999;
742 my $from_pc9x = shift;
744 my @dxchan = DXChannel::get_all();
747 my $text = unpad($_[2]);
751 # munge the group and recast the line if required
752 if ($target =~ s/\.LST$//) {
756 # obtain country codes etc
757 my @a = Prefix::cty_data($from);
758 my @b = Prefix::cty_data($_[4]);
759 if ($self->{inannfilter}) {
760 my ($filter, $hops) =
761 $self->{inannfilter}->it(@_, $self->{call},
763 @b[0..2], $a[3], $b[3]);
765 dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
770 if (AnnTalk::dup($from, $target, $_[2], $main::systime + $chatdupeage)) {
771 my $dxchan = DXChannel::get($from);
772 if ($self == $main::me && $dxchan && $dxchan->is_user) {
773 if ($dxchan->priv < 5) {
774 $dxchan->send($dxchan->msg('dup'));
778 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
784 Log('chat', $target, $from, $text);
786 # send it if it isn't the except list and isn't isolated and still has a hop count
787 # taking into account filtering and so on
788 foreach $dxchan (@dxchan) {
789 my $is_ak1a = $dxchan->is_ak1a;
791 if ($dxchan->is_node) {
792 next if $dxchan == $main::me;
793 next if $dxchan == $self;
794 next if $from_pc9x && $dxchan->{do_pc9x};
795 next unless $dxchan->is_spider || $is_ak1a;
796 next if $target eq 'LOCAL';
797 if (!$ak1a_line && $is_ak1a) {
798 $ak1a_line = pc12($_[0], $text, $_[1], "$target.LST");
802 $dxchan->chat($is_ak1a ? $ak1a_line : $line, $self->{isolate}, $target, $_[1],
803 $text, @_, $self->{call}, @a[0..2], @b[0..2]);
817 if ($self->{annfilter}) {
818 ($filter, $hops) = $self->{annfilter}->it(@_);
819 return unless $filter;
821 send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
834 if ($self->{annfilter}) {
835 ($filter, $hops) = $self->{annfilter}->it(@_);
836 return unless $filter;
838 if (($self->is_spider || $self->is_ak1a) && $_[1] ne $main::mycall) {
839 send_prot_line($self, $filter, $hops, $isolate, $line);
844 sub send_local_config
848 dbg('DXProt::send_local_config') if isdbg('trace');
856 if ($self->{isolate}) {
857 dbg("send_local_config: isolated");
858 @localnodes = ( $main::routeroot );
859 $self->send_route($main::mycall, \&pc19, 1, $main::routeroot);
860 } elsif ($self->{do_pc9x}) {
861 dbg("send_local_config: doing pc9x");
862 my $node = Route::Node::get($self->{call});
863 # $self->send_last_pc92_config($main::routeroot);
864 # $self->send(pc92a($main::routeroot, $node)) unless $main::routeroot->last_PC92C =~ /$self->{call}/;
865 $self->send(pc92a($main::routeroot, $node));
866 $self->send(pc92k($main::routeroot));
868 # create a list of all the nodes that are not connected to this connection
869 # and are not themselves isolated, this to make sure that isolated nodes
870 # don't appear outside of this node
872 dbg("send_local_config: traditional");
874 # send locally connected nodes
875 my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
876 @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan if @dxchan;
877 $self->send_route($main::mycall, \&pc19, scalar(@localnodes)+1, $main::routeroot, @localnodes);
880 my @rawintcalls = map { $_->nodes } @localnodes if @localnodes;
882 foreach $node (@rawintcalls) {
883 push @intcalls, $node if grep $_ && $node != $_, @intcalls;
885 my $ref = Route::Node::get($self->{call});
886 my @rnodes = $ref->nodes;
887 foreach $node (@intcalls) {
888 push @remotenodes, Route::Node::get($node) if grep $_ && $node != $_, @rnodes, @remotenodes;
890 $self->send_route($main::mycall, \&pc19, scalar(@remotenodes), @remotenodes);
893 # get all the users connected on the above nodes and send them out
894 unless ($self->{do_pc9x}) {
895 foreach $node ($main::routeroot, @localnodes, @remotenodes) {
897 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
898 $self->send_route($main::mycall, \&pc16, 1, $node, @rout) if @rout && $self->user->wantsendpc16;
900 dbg("sent a null value") if isdbg('chanerr');
906 sub gen_my_pc92_config
910 if ($node->{call} eq $main::mycall) {
911 clear_pc92_changes(); # remove any slugged data, we are generating it as now
912 my @dxchan = grep { $_->call ne $main::mycall && !$_->{isolate} } DXChannel::get_all();
913 dbg("ROUTE: all dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('routelow');
914 my @localnodes = map { my $r = Route::get($_->{call}); $r ? $r : () } @dxchan;
915 dbg("ROUTE: localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('routelow');
916 return pc92c($node, @localnodes);
918 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
919 return pc92c($node, @rout);
923 sub send_last_pc92_config
927 if (my $l = $node->last_PC92C) {
930 $self->send_pc92_config($node);
939 dbg('DXProt::send_pc92_config') if isdbg('trace');
941 $node->last_PC92C(gen_my_pc92_config($node));
942 $self->send($node->last_PC92C);
945 sub broadcast_pc92_update
950 dbg("ROUTE: broadcast_pc92_update $call") if isdbg('obscount');
952 my $nref = Route::Node::get($call);
954 dbg("ERROR: broadcast_pc92_update - Route::Node $call disappeared");
957 my $l = $nref->last_PC92C(gen_my_pc92_config($nref));
958 $nref->lastid(last_pc9x_id());
959 $main::me->broadcast_route_pc9x($main::mycall, undef, $l, 0);
960 $self->update_pc92_next;
963 sub broadcast_pc92_keepalive
968 dbg("ROUTE: broadcast_pc92_keepalive $call") if isdbg('obscount');
970 my $nref = Route::Node::get($call);
972 dbg("ERROR: broadcast_pc92_keepalive - Route::Node $call disappeared");
975 my $l = pc92k($nref);
976 $nref->lastid(last_pc9x_id());
977 $main::me->broadcast_route_pc9x($main::mycall, undef, $l, 0);
978 $self->update_pc92_keepalive;
981 sub time_out_pc92_routes
983 my @nodes = grep {$_->call ne $main::mycall && ($_->do_pc9x || $_->via_pc92)} Route::Node::get_all();
985 foreach my $n (@nodes) {
988 if (my $dxchan = DXChannel::get($n->call)) {
989 dbg("ROUTE: disconnecting local pc92 $dxchan->{call} on obscount") if isdbg('obscount');
993 my @parents = map {Route::Node::get($_)} $n->parents;
996 dbg("ROUTE: deleting pc92 $_->{call} from $n->{call} on obscount") if isdbg('obscount');
997 push @rdel, $n->del($_);
1001 dbg("ROUTE: obscount on $n->{call} now $o") if isdbg('obscount');
1005 $main::me->route_pc21($main::mycall, undef, $_) if $_;
1010 # route a message down an appropriate interface for a callsign
1012 # is called route(to, pcline);
1017 my ($self, $call, $line) = @_;
1019 if (ref $self && $call eq $self->{call}) {
1020 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1024 # always send it down the local interface if available
1025 my $dxchan = DXChannel::get($call);
1027 dbg("route: $call -> $dxchan->{call} direct" ) if isdbg('route');
1029 my $cl = Route::get($call);
1030 $dxchan = $cl->dxchan if $cl;
1032 if (ref $self && $dxchan eq $self) {
1033 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1036 dbg("route: $call -> $dxchan->{call} using normal route" ) if isdbg('route');
1040 # try the backstop method
1042 my $rcall = RouteDB::get($call);
1044 if ($self && $rcall eq $self->{call}) {
1045 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1048 $dxchan = DXChannel::get($rcall);
1049 dbg("route: $call -> $rcall using RouteDB" ) if isdbg('route') && $dxchan;
1054 my $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1056 $dxchan->send($routeit) unless $dxchan == $main::me;
1059 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
1064 # obtain the hops from the list for this callsign and pc no
1070 my $hops = $DXProt::hopcount{$pcno};
1071 $hops = $DXProt::def_hopcount if !$hops;
1076 # adjust the hop count on a per node basis using the user loadable
1077 # hop table if available or else decrement an existing one
1084 my $call = $self->{call};
1087 if (($hops) = $s =~ /\^H([-\d]+)\^?~?$/o) {
1088 my ($pcno) = $s =~ /^PC(\d\d)/o;
1089 confess "$call called adjust_hops with '$s'" unless $pcno;
1090 my $ref = $nodehops{$call} if %nodehops;
1092 my $newhops = $ref->{$pcno};
1093 return "" if defined $newhops && $newhops == 0;
1094 $newhops = $ref->{default} unless $newhops;
1095 return "" if defined $newhops && $newhops == 0;
1096 $newhops = $hops unless $newhops;
1097 return "" unless $newhops > 0;
1098 $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops != $hops;
1100 return "" unless $hops > 0;
1112 return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1113 do "$main::data/hop_table.pl";
1120 my ($self, $tonode, $fromnode, $user, $cmd) = @_;
1121 if ($tonode eq $main::mycall) {
1122 my $ref = DXUser::get_current($fromnode);
1123 my $cref = Route::Node::get($fromnode);
1124 Log('rcmd', 'in', ($ref->{priv}||0), $fromnode, $cmd);
1125 if ($cmd !~ /^\s*rcmd/i && $cref && $ref && $ref->homenode && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS!
1126 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
1127 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
1128 my $oldpriv = $self->{priv};
1129 $self->{priv} = $ref->{priv}; # assume the user's privilege level
1130 my @in = (DXCommandmode::run_cmd($self, $cmd));
1131 $self->{priv} = $oldpriv;
1132 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @in);
1133 delete $self->{remotecmd};
1135 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1138 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "your attempt is logged, Tut tut tut...!");
1141 my $ref = DXUser::get_current($tonode);
1142 if ($ref && $ref->is_clx) {
1143 $self->route($tonode, pc84($fromnode, $tonode, $user, $cmd));
1145 $self->route($tonode, pc34($fromnode, $tonode, $cmd));
1150 sub process_rcmd_reply
1152 my ($self, $tonode, $fromnode, $user, $line) = @_;
1153 if ($tonode eq $main::mycall) {
1154 my $s = $rcmds{$fromnode};
1156 my $dxchan = DXChannel::get($s->{call});
1157 my $ref = $user eq $tonode ? $dxchan : (DXChannel::get($user) || $dxchan);
1158 $ref->send($line) if $ref;
1159 delete $rcmds{$fromnode} if !$dxchan;
1161 # send unsolicited ones to the sysop
1162 my $dxchan = DXChannel::get($main::myalias);
1163 $dxchan->send($line) if $dxchan;
1166 my $ref = DXUser::get_current($tonode);
1167 if ($ref && $ref->is_clx) {
1168 $self->route($tonode, pc85($fromnode, $tonode, $user, $line));
1170 $self->route($tonode, pc35($fromnode, $tonode, $line));
1179 my $fromnode = shift;
1184 Log('rcmd', 'out', $fromnode, $line);
1185 if ($self->is_clx) {
1186 $self->send(pc85($main::mycall, $fromnode, $user, "$main::mycall:$line"));
1188 $self->send(pc35($main::mycall, $fromnode, "$main::mycall:$line"));
1193 # add a rcmd request to the rcmd queues
1196 my ($self, $to, $cmd) = @_;
1199 $r->{call} = $self->{call};
1200 $r->{t} = $main::systime;
1204 my $ref = Route::Node::get($to);
1205 my $dxchan = $ref->dxchan;
1206 if ($dxchan && $dxchan->is_clx) {
1207 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1209 route(undef, $to, pc34($main::mycall, $to, $cmd));
1216 my $pc39flag = shift || 0;
1217 my $call = $self->call;
1219 return if $self->{disconnecting}++;
1221 unless ($pc39flag == 1) {
1222 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1225 # get rid of any PC16/17/19
1226 eph_del_regex("^PC1[679]*$call");
1228 # do routing stuff, remove me from routing table
1229 my $node = Route::Node::get($call);
1231 RouteDB::delete_interface($call);
1233 # unbusy and stop and outgoing mail
1234 my $mref = DXMsg::get_busy($call);
1235 $mref->stop_msg($call) if $mref;
1237 # remove outstanding pings
1238 delete $pings{$call};
1240 # I was the last node visited
1241 $self->user->node($main::mycall);
1243 # send info to all logged in thingies
1244 $self->tell_login('logoutn');
1246 Log('DXProt', $call . " Disconnected");
1248 $self->SUPER::disconnect;
1250 # here we determine what needs to go out of the routing table
1252 if ($node && $pc39flag != 2) {
1253 dbg('%Route::Node::List = ' . join(',', sort keys %Route::Node::list)) if isdbg('routedisc');
1255 @rout = $node->del($main::routeroot);
1257 dbg('@rout = ' . join(',', sort map {$_->call} @rout)) if isdbg('routedisc');
1259 # now we need to see what can't be routed anymore and came
1260 # in via this node (probably).
1262 while ($n != @rout) {
1264 for (Route::Node::get_all()) {
1265 unless ($_->dxchan) {
1266 push @rout, $_->delete;
1269 dbg('@rout = ' . join(',', sort map {$_->call} @rout)) if isdbg('routedisc');
1272 dbg('%Route::Node::List = ' . join(',', sort keys %Route::Node::list)) if isdbg('routedisc');
1274 # and all my ephemera as well
1277 eph_del_regex("^PC1[679].*$c");
1281 # broadcast to all other nodes that all the nodes connected to via me are gone
1282 unless ($pc39flag == 2) {
1283 $self->route_pc21($main::mycall, undef, @rout) if @rout;
1284 $self->route_pc92d($main::mycall, undef, $main::routeroot, $node) if $node;
1290 # send a talk message to this thingy
1294 my ($self, $from, $to, $via, $line, $origin) = @_;
1296 if ($self->{do_pc9x}) {
1297 $self->send(pc93($to, $from, $via, $line));
1299 $self->send(pc10($from, $to, $via, $line, $origin));
1301 Log('talk', $to, $from, '>' . ($via || $origin || $self->call), $line) unless $origin && $origin ne $main::mycall;
1304 # send it if it isn't the except list and isn't isolated and still has a hop count
1305 # taking into account filtering and so on
1311 my $generate = shift;
1312 my $no = shift; # the no of things to filter on
1314 my ($filter, $hops);
1317 for (; @_ && $no; $no--) {
1320 # don't send messages with $self's call in back to them
1321 if ($r->call eq $self->{call}) {
1322 dbg("PCPROT: trying to send $self->{call} back itself") if isdbg('chanerr');
1326 if (!$self->{isolate} && $self->{routefilter}) {
1329 ($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});
1333 dbg("PCPROT: $self->{call}/" . $r->call . " rejected by output filter") if isdbg('filter');
1336 dbg("was sent a null value") if isdbg('chanerr');
1339 push @rin, $r unless $self->{isolate} && $r->call ne $main::mycall;
1343 foreach my $line (&$generate(@rin, @_)) {
1346 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1348 $routeit = adjust_hops($self, $line); # adjust its hop count by node name
1349 next unless $routeit;
1352 $self->send($routeit);
1357 # broadcast everywhere
1362 my $generate = shift;
1364 my @dxchan = DXChannel::get_all_nodes();
1368 $line =~ /\^H(\d+)\^?\~?$/;
1369 return unless $1 > 0;
1371 unless ($self->{isolate}) {
1372 foreach $dxchan (@dxchan) {
1373 next if $dxchan == $self || $dxchan == $main::me;
1374 next if $origin eq $dxchan->{call}; # don't route some from this call back again.
1375 next unless $dxchan->isa('DXProt');
1377 $dxchan->send_route($origin, $generate, @_);
1382 # broadcast to non-pc9x nodes
1383 sub broadcast_route_nopc9x
1387 my $generate = shift;
1389 my @dxchan = DXChannel::get_all_nodes();
1393 $line =~ /\^H(\d+)\^?\~?$/;
1394 return unless $1 > 0;
1396 unless ($self->{isolate}) {
1397 foreach $dxchan (@dxchan) {
1398 next if $dxchan == $self || $dxchan == $main::me;
1399 next if $origin eq $dxchan->{call}; # don't route some from this call back again.
1400 next unless $dxchan->isa('DXProt');
1401 next if $dxchan->{do_pc9x};
1402 if ($generate == \&pc16 || $generate==\&pc17) {
1403 next unless $dxchan->user->wantsendpc16;
1405 $dxchan->send_route($origin, $generate, @_);
1410 # this is only used for next door nodes on init
1415 return unless $self->{do_pc9x};
1418 my $generate = shift;
1419 my $no = shift; # the no of things to filter on
1422 $line = &$generate(@_);
1426 # broadcast only to pc9x nodes
1427 sub broadcast_route_pc9x
1431 my $generate = shift;
1434 my @dxchan = DXChannel::get_all_nodes();
1437 if ($origin eq $main::mycall && $generate && !$line) {
1438 $line = &$generate(@_);
1441 $line =~ /\^H(\d+)\^\~?$/;
1442 unless ($1 > 0 && $self->{isolate}) {
1443 foreach $dxchan (@dxchan) {
1444 next if $dxchan == $self || $dxchan == $main::me;
1445 next if $origin eq $dxchan->{call}; # don't route some from this call back again.
1446 next unless $dxchan->isa('DXProt');
1447 next unless $dxchan->{do_pc9x};
1449 $dxchan->send($line);
1457 return unless $self->user->wantpc16;
1460 broadcast_route_nopc9x($self, $origin, \&pc16, $line, 1, @_);
1466 return unless $self->user->wantpc16;
1469 broadcast_route_nopc9x($self, $origin, \&pc17, $line, 1, @_);
1477 broadcast_route_nopc9x($self, $origin, \&pc19, $line, scalar @_, @_);
1485 broadcast_route_nopc9x($self, $origin, \&pc21, $line, scalar @_, @_);
1493 broadcast_route($self, $origin, \&pc24, $line, 1, @_);
1501 broadcast_route($self, $origin, \&pc41, $line, 1, @_);
1504 # this is probably obsolete now
1511 broadcast_route($self, $origin, \&pc50, $line, 1, @_);
1519 broadcast_route_pc9x($self, $origin, \&pc92c, $line, 1, @_);
1527 broadcast_route_pc9x($self, $origin, \&pc92a, $line, 1, @_);
1535 broadcast_route_pc9x($self, $origin, \&pc92d, $line, 1, @_);
1542 my ($filter, $hops) = (1, 1);
1544 if ($self->{inroutefilter}) {
1545 ($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);
1546 dbg("PCPROT: $self->{call}/" . $r->call . ' rejected by in_filter_route') if !$filter && isdbg('chanerr');
1554 my $t = shift || $eph_restime;
1558 $s =~ s/\^H\d\d?\^?\~?$//;
1559 $r = 1 if exists $eph{$s}; # pump up the dup if it keeps circulating
1560 $eph{$s} = $main::systime + $t;
1561 dbg("PCPROT: emphemeral duplicate") if $r && isdbg('chanerr');
1569 while (($key, $val) = each %eph) {
1570 if ($key =~ m{$regex}) {
1580 while (($key, $val) = each %eph) {
1581 if ($main::systime >= $val) {
1592 while (($key, $val) = each %eph) {
1593 push @out, $key, $val;
1600 goto &DXCommandmode::run_cmd;
1604 # import any msgs in the chat directory
1605 # the messages are sent to the chat group which forms the
1606 # the first part of the name (eg: solar.1243.txt would be
1607 # sent to chat group SOLAR)
1609 # Each message found is sent: one non-blank line to one chat
1610 # message. So 4 lines = 4 chat messages.
1612 # The special name LOCAL is for local users ANN
1613 # The special name ALL is for ANN/FULL
1614 # The special name SYSOP is for ANN/SYSOP
1618 # are there any to do in this directory?
1619 return unless -d $chatimportfn;
1620 unless (opendir(DIR, $chatimportfn)) {
1621 dbg("can\'t open $chatimportfn $!") if isdbg('msg');
1622 Log('msg', "can\'t open $chatimportfn $!");
1626 my @names = readdir(DIR);
1629 foreach $name (@names) {
1630 next if $name =~ /^\./;
1631 my $splitit = $name =~ /^split/;
1632 my $fn = "$chatimportfn/$name";
1634 unless (open(MSG, $fn)) {
1635 dbg("can\'t open import file $fn $!") if isdbg('msg');
1636 Log('msg', "can\'t open import file $fn $!");
1640 my @msg = map { s/\r?\n$//; $_ } <MSG>;
1644 my @cat = split /\./, $name;
1645 my $target = uc $cat[0];
1647 foreach my $text (@msg) {
1648 next unless $text && $text !~ /^\s*#/;
1649 if ($target eq 'ALL' || $target eq 'LOCAL' || $target eq 'SYSOP' || $target eq 'WX') {
1650 my $sysop = uc $target eq 'SYSOP' ? '*' : ' ';
1651 my $wx = uc $target eq 'WX' ? '1' : '0';
1653 $via = '*' if $target eq 'ALL' || $target eq 'SYSOP';
1654 Log('ann', $target, $main::mycall, $text);
1655 $main::me->normal(DXProt::pc93($target, $main::mycall, $via, $text));
1657 DXCommandmode::send_chats($main::me, $target, $text);
1663 # start a pc92 find operation
1668 my $key = "$dxchan->{call}|$target";
1669 if ($pc92_find{$key}) {
1674 # function (not method) to handle pc92 find returns
1675 sub handle_pc92_find_reply
1677 my ($dxchan, $node, $from, $target, $flag, $ms) = @_;
1679 $dxchan->print_pc92_find_reply($node, $target, $flag, $ms) if $dxchan->can('print_pc92_find_return');