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 = 18*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 = 5455; # 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
137 [ qw(i f m d t m c c a h) ], # pc61
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)
180 return 0 if $n < 0 || $n > @checklist;
181 my $ref = $checklist[$n];
182 return 0 unless ref $ref;
185 for ($i = 1; $i < @$ref; $i++) {
186 my ($blank, $act) = $$ref[$i] =~ /^(b?)(\w)$/;
187 return 0 unless $act;
188 next if $blank eq 'b' && $pc->[$i] =~ /^[ \*]$/;
189 next if $blank eq '*' && $pc->[$i] =~ /^\*$/;
191 return $i unless is_callsign($pc->[$i]);
192 } elsif ($act eq 'i') {
194 } elsif ($act eq 'm') {
195 return $i unless is_pctext($pc->[$i]);
196 } elsif ($act eq 'p') {
197 return $i unless is_pcflag($pc->[$i]);
198 } elsif ($act eq 'f') {
199 return $i unless is_freq($pc->[$i]);
200 } elsif ($act eq 'n') {
201 return $i unless $pc->[$i] =~ /^[\d ]+$/;
202 } elsif ($act eq 'h') {
203 return $i unless $pc->[$i] =~ /^H\d\d?$/;
204 } elsif ($act eq 'd') {
205 return $i unless $pc->[$i] =~ /^\s*\d+-\w\w\w-[12][90]\d\d$/;
206 } elsif ($act eq 't') {
207 return $i unless $pc->[$i] =~ /^[012]\d[012345]\dZ$/;
208 } elsif ($act eq 'l') {
209 return $i unless $pc->[$i] =~ /^[A-Z]$/;
210 } elsif ($act eq 'a') {
211 return $i unless is_ipaddr($pc->[$i]);
220 my $period = shift || ($self->{do_pc9x} ? $pc92_update_period : $pc92_extnode_update_period);
221 $self->{next_pc92_update} = $main::systime + $period - int rand($period / 4);
222 dbg("ROUTE: update_pc92_next: $self->{call} " . atime($self->{next_pc92_update})) if isdbg('obscount');
225 sub update_pc92_keepalive
228 my $period = shift || $pc92_keepalive_period;
229 $self->{next_pc92_keepalive} = $main::systime + $period - int rand($period / 4);
230 dbg("ROUTE: update_pc92_keepalive: $self->{call} " . atime($self->{next_pc92_keepalive})) if isdbg('obscount');
235 my $fn = localdata("hop_table.pl");
239 my $user = DXUser::get($main::mycall);
240 die "User $main::mycall not setup or disappeared RTFM" unless $user;
242 $myprot_version += $main::version*100;
243 $main::me = DXProt->new($main::mycall, 0, $user);
244 $main::me->{here} = 1;
245 $main::me->{state} = "indifferent";
246 $main::me->{sort} = 'S'; # S for spider
247 $main::me->{priv} = 9;
248 $main::me->{metric} = 0;
249 $main::me->{pingave} = 0;
250 $main::me->{registered} = 1;
251 $main::me->{version} = $main::version;
252 $main::me->{build} = $main::build;
253 $main::me->{do_pc9x} = 1;
254 $main::me->update_pc92_next($pc92_short_update_period);
255 $main::me->update_pc92_keepalive;
259 # obtain a new connection this is derived from dxchannel
264 my $self = DXChannel::alloc(@_);
266 # add this node to the table, the values get filled in later
270 # if we have an entry already, then send a PC21 to all connect
271 # old style connections, because we are about to get the real deal
272 if (my $ref = Route::Node::get($call)) {
273 dbg("ROUTE: $call is already in the routing table, deleting") if isdbg('route');
274 my @rout = $ref->delete;
275 $self->route_pc21($main::mycall, undef, @rout) if @rout;
277 $main::routeroot->add($call, '5000', Route::here(1), $self->{conn}->peerhost) if $call ne $main::mycall;
282 # this is how a pc connection starts (for an incoming connection)
283 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
284 # all the crap that comes between).
287 my ($self, $line, $sort) = @_;
288 my $call = $self->{call};
289 my $user = $self->{user};
292 my $host = $self->{conn}->peerhost;
293 $host ||= "AGW Port #$self->{conn}->{agwport}" if exists $self->{conn}->{agwport};
295 $self->{hostname} = $host if is_ipaddr($host);
297 Log('DXProt', "$call connected from $host");
299 # remember type of connection
300 $self->{consort} = $line;
301 $self->{outbound} = $sort eq 'O';
302 my $priv = $user->priv;
303 $priv = $user->priv(1) unless defined $priv;
304 $self->{priv} = $priv; # other clusters can always be 'normal' users
305 $self->{lang} = $user->lang || 'en';
306 $self->{isolate} = $user->{isolate};
307 $self->{consort} = $line; # save the connection type
311 # sort out registration
312 $self->{registered} = 1;
314 # get the output filters
315 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
316 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
317 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
318 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
319 $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) unless $self->{isolate};
320 $self->{pc92filter} = Filter::read_in('pc92', $call, 0) || Filter::read_in('pc92', 'node_default', 0) unless $self->{isolate} ;
323 # get the INPUT filters (these only pertain to Clusters)
324 $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
325 $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
326 $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
327 $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
328 $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1) unless $self->{isolate};
329 $self->{inpc92filter} = Filter::read_in('pc92', $call, 0) || Filter::read_in('pc92', 'node_default', 0) unless $self->{isolate} ;
332 # set unbuffered and no echo
333 $self->send_now('B',"0");
334 $self->send_now('E',"0");
335 $self->conn->echo(0) if $self->conn->can('echo');
337 # ping neighbour node stuff
338 my $ping = $user->pingint;
339 $ping = $pingint unless defined $ping;
340 $self->{pingint} = $ping;
341 $self->{nopings} = $user->nopings || $obscount;
342 $self->{pingtime} = [ ];
343 $self->{pingave} = 999;
344 $self->{metric} ||= 100;
345 $self->{lastping} = $main::systime;
347 # send initialisation string
348 unless ($self->{outbound}) {
352 $self->state('init');
353 $self->{pc50_t} = $main::systime;
355 # send info to all logged in thingies
356 $self->tell_login('loginn');
358 # run a script send the output to the debug file
359 my $script = new Script(lc $call) || new Script('node_default');
360 $script->run($self) if $script;
362 # set up a config broadcast "quite soon" to converge tables quicker
363 $main::me->update_pc92_next($pc92_short_update_period);
364 $self->update_pc92_next($pc92_short_update_period);
366 # set next keepalive time
367 $self->update_pc92_keepalive;
371 # send outgoing 'challenge'
377 $self->send(pc18(($self->{isolate} || !$self->user->wantpc9x) ? "" : " pc9x"));
381 # This is the normal pcxx despatcher
385 my ($self, $line) = @_;
387 if ($line =~ '^<\w+\s' && $main::do_xml) {
388 DXXml::normal($self, $line);
392 my @field = split /\^/, $line;
393 return unless @field;
395 pop @field if $field[-1] eq '~';
397 # print join(',', @field), "\n";
400 # process PC frames, this will fail unless the frame starts PCnn
401 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
402 unless (defined $pcno && $pcno >= 10 && $pcno <= 99) {
403 dbg("PCPROT: unknown protocol") if isdbg('chanerr');
407 # check for and dump bad protocol messages
408 my $n = check($pcno, \@field);
410 dbg("PCPROT: bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")") if isdbg('chanerr');
414 # modify the hop count here
415 if ($self != $main::me) {
416 if (my ($hops, $trail) = $line =~ /\^H(\d+)(\^?\~?)?$/) {
420 $line =~ s/\^H(\d+)(\^?\~?)?$/sprintf('^H%d%s', $hops, $trail)/e;
421 $field[-1] = "H$hops";
425 if (defined &Local::pcprot) {
427 eval { $r = Local::pcprot($self, $pcno, $line, @field); };
428 return if $r; # i.e don't process it
431 # send it out for processing
432 my $origin = $self->{call};
434 my $sub = "handle_$pcno";
436 if ($self->can($sub)) {
437 $self->$sub($pcno, $line, $origin, \@field);
439 $self->handle_default($pcno, $line, $origin, \@field);
444 # This is called from inside the main cluster processing loop and is used
445 # for despatching commands that are doing some long processing job
450 my @dxchan = DXChannel::get_all();
455 if ($t - $last10 >= 10) {
463 # send out a pc50 on EVERY channel all at once
464 if ($t >= $last_pc50 + $pc50_interval) {
465 $pc50s = pc50($main::me, scalar DXChannel::get_all_users);
470 foreach $dxchan (@dxchan) {
471 next unless $dxchan->is_node;
472 next if $dxchan == $main::me;
475 $dxchan->send($pc50s) if !$dxchan->{do_pc9x} && $pc50s;
477 # send a ping out on this channel
478 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
479 if ($dxchan->{nopings} <= 0) {
480 dbg("ROUTE: $dxchan->{call} disconnected on ping obscount") if isdbg('obscount');
483 DXXml::Ping::add($main::me, $dxchan->call);
484 $dxchan->{nopings} -= 1;
485 $dxchan->{lastping} = $t;
486 $dxchan->{lastping} += $dxchan->{pingint} / 2 unless @{$dxchan->{pingtime}};
487 dbg("ROUTE: $dxchan->{call} ping obscount = $dxchan->{nopings}") if isdbg('obscount');
494 # send out config broadcasts
495 foreach $dxchan (@dxchan) {
496 next unless $dxchan->is_node;
498 # send out a PC92 config record if required for me and
499 # all my non pc9x dependent nodes. But for dependent nodes we only do
500 # this if we have not seen any from anyone else for at least half
501 # of one update period. This should stop quite a bit of excess C
502 # records. Someone will win, it does not really matter who, because
503 # we always believe "us".
504 if ($main::systime >= $dxchan->{next_pc92_update}) {
505 if ($dxchan == $main::me || !$dxchan->{do_pc9x}) {
506 dbg("ROUTE: pc92 broadcast candidate: $dxchan->{call}") if isdbg('obscount');
507 my $ref = Route::Node::get($dxchan->{call});
508 if ($dxchan == $main::me || ($ref && ($ref->measure_pc9x_t($main::systime-$main::systime_daystart)) >= $pc92_extnode_update_period/2)) {
509 $dxchan->broadcast_pc92_update($dxchan->{call});
511 $dxchan->update_pc92_next;
514 $dxchan->update_pc92_next; # this won't actually do anything, it's just to be tidy
519 # do the keepalive for me, if required
520 if ($main::systime >= $main::me->{next_pc92_keepalive}) {
521 time_out_pc92_routes();
522 $main::me->broadcast_pc92_keepalive($main::mycall);
525 if ($pc92_slug_changes && $main::systime >= $last_pc92_slug + $pc92_slug_changes) {
526 my ($add, $del) = gen_pc92_changes();
527 $main::me->route_pc92d($main::mycall, undef, $main::routeroot, @$del) if @$del;
528 $main::me->route_pc92a($main::mycall, undef, $main::routeroot, @$add) if @$add;
529 clear_pc92_changes();
533 if ($main::systime - 3600 > $last_hour) {
534 $last_hour = $main::systime;
539 # finish up a pc context
543 # some active measures
551 my @dxchan = DXChannel::get_all();
555 # send it if it isn't the except list and isn't isolated and still has a hop count
556 # taking into account filtering and so on
557 foreach $dxchan (@dxchan) {
558 next if $dxchan == $main::me;
559 next if $dxchan == $self && $self->is_node;
560 if ($line =~ /PC61/ && !($dxchan->is_spider || $dxchan->is_user)) {
562 my @f = split /\^/, $line;
563 $pc11 = join '^', 'PC11', @f[1..7,9];
565 $dxchan->dx_spot($pc11, $self->{isolate}, @_, $self->{call});
567 $dxchan->dx_spot($line, $self->{isolate}, @_, $self->{call});
579 if ($self->{spotsfilter}) {
580 ($filter, $hops) = $self->{spotsfilter}->it(@_);
581 return unless $filter;
583 send_prot_line($self, $filter, $hops, $isolate, $line);
588 my ($self, $filter, $hops, $isolate, $line) = @_;
594 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
596 $routeit = adjust_hops($self, $line); # adjust its hop count by node name
597 return unless $routeit;
600 $self->send($routeit);
602 $self->send($routeit) unless $self->{isolate} || $isolate;
611 my @dxchan = DXChannel::get_all();
613 my @dxcc = ((Prefix::cty_data($_[6]))[0..2], (Prefix::cty_data($_[7]))[0..2]);
615 # send it if it isn't the except list and isn't isolated and still has a hop count
616 # taking into account filtering and so on
617 foreach $dxchan (@dxchan) {
618 next if $dxchan == $main::me;
619 next if $dxchan == $self && $self->is_node;
623 $dxchan->wwv($line, $self->{isolate}, @_, $self->{call}, @dxcc);
634 if ($self->{wwvfilter}) {
635 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_]);
636 return unless $filter;
638 send_prot_line($self, $filter, $hops, $isolate, $line)
645 my @dxchan = DXChannel::get_all();
647 my @dxcc = ((Prefix::cty_data($_[10]))[0..2], (Prefix::cty_data($_[11]))[0..2]);
649 # send it if it isn't the except list and isn't isolated and still has a hop count
650 # taking into account filtering and so on
651 foreach $dxchan (@dxchan) {
652 next if $dxchan == $main::me;
653 next if $dxchan == $self;
655 $dxchan->wcy($line, $self->{isolate}, @_, $self->{call}, @dxcc);
666 if ($self->{wcyfilter}) {
667 ($filter, $hops) = $self->{wcyfilter}->it(@_);
668 return unless $filter;
670 send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
677 my $from_pc9x = shift;
679 my @dxchan = DXChannel::get_all();
683 my $text = unpad($_[2]);
686 if ($_[3] eq '*') { # sysops
688 } elsif ($_[3] gt ' ') { # speciality list handling
689 my ($name) = split /\./, $_[3];
690 $target = "$name"; # put the rest in later (if bothered)
697 $target = "ALL" if !$target;
700 # obtain country codes etc
701 my @a = Prefix::cty_data($from);
702 my @b = Prefix::cty_data($_[4]);
703 if ($self->{inannfilter}) {
704 my ($filter, $hops) =
705 $self->{inannfilter}->it(@_, $self->{call},
707 @b[0..2], $a[3], $b[3]);
709 dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
714 # the sysop ('*') thing is an attempt to minimise the damage caused by non-updated PC93 generators
715 if (AnnTalk::dup($from, $target, $_[2]) || ($_[3] eq '*' && AnnTalk::dup($from, 'ALL', $_[2]))) {
716 my $dxchan = DXChannel::get($from);
717 if ($self == $main::me && $dxchan && $dxchan->is_user) {
718 if ($dxchan->priv < 5) {
719 $dxchan->send($dxchan->msg('dup'));
723 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
728 Log('ann', $target, $from, $text);
730 # send it if it isn't the except list and isn't isolated and still has a hop count
731 # taking into account filtering and so on
732 foreach $dxchan (@dxchan) {
733 next if $dxchan == $main::me;
734 next if $dxchan == $self && $self->is_node;
735 next if $from_pc9x && $dxchan->{do_pc9x};
736 next if $target eq 'LOCAL' && $dxchan->is_node;
737 $dxchan->announce($line, $self->{isolate}, $to, $target, $text, @_, $self->{call},
742 my $msgid = int rand(1000);
747 $msgid = 1 if $msgid > 999;
755 my $from_pc9x = shift;
757 my @dxchan = DXChannel::get_all();
760 my $text = unpad($_[2]);
764 # munge the group and recast the line if required
765 if ($target =~ s/\.LST$//) {
769 # obtain country codes etc
770 my @a = Prefix::cty_data($from);
771 my @b = Prefix::cty_data($_[4]);
772 if ($self->{inannfilter}) {
773 my ($filter, $hops) =
774 $self->{inannfilter}->it(@_, $self->{call},
776 @b[0..2], $a[3], $b[3]);
778 dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
783 if (AnnTalk::dup($from, $target, $_[2], $main::systime + $chatdupeage)) {
784 my $dxchan = DXChannel::get($from);
785 if ($self == $main::me && $dxchan && $dxchan->is_user) {
786 if ($dxchan->priv < 5) {
787 $dxchan->send($dxchan->msg('dup'));
791 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
797 Log('chat', $target, $from, $text);
799 # send it if it isn't the except list and isn't isolated and still has a hop count
800 # taking into account filtering and so on
801 foreach $dxchan (@dxchan) {
802 if ($dxchan->is_node) {
803 next if $dxchan == $main::me;
804 next if $dxchan == $self;
805 next if $from_pc9x && $dxchan->do_pc9x;
806 next unless $dxchan->is_spider && $dxchan->do_pc9x;
807 next if $target eq 'LOCAL';
810 $dxchan->chat($line, $self->{isolate}, $target, $_[1],
811 $text, @_, $self->{call}, @a[0..2], @b[0..2]);
825 if ($self->{annfilter}) {
826 ($filter, $hops) = $self->{annfilter}->it(@_);
827 return unless $filter;
829 send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
842 if ($self->{annfilter}) {
843 ($filter, $hops) = $self->{annfilter}->it(@_);
844 return unless $filter;
846 if (($self->is_spider || $self->is_ak1a) && $_[1] ne $main::mycall) {
847 send_prot_line($self, $filter, $hops, $isolate, $line);
852 sub send_local_config
856 dbg('DXProt::send_local_config') if isdbg('trace');
864 if ($self->{isolate}) {
865 dbg("send_local_config: isolated");
866 @localnodes = ( $main::routeroot );
867 $self->send_route($main::mycall, \&pc19, 1, $main::routeroot);
868 } elsif ($self->{do_pc9x}) {
869 dbg("send_local_config: doing pc9x");
870 my $node = Route::Node::get($self->{call});
871 # $self->send_last_pc92_config($main::routeroot);
872 # $self->send(pc92a($main::routeroot, $node)) unless $main::routeroot->last_PC92C =~ /$self->{call}/;
873 $self->send(pc92a($main::routeroot, $node));
874 $self->send(pc92k($main::routeroot));
876 # create a list of all the nodes that are not connected to this connection
877 # and are not themselves isolated, this to make sure that isolated nodes
878 # don't appear outside of this node
880 dbg("send_local_config: traditional");
882 # send locally connected nodes
883 my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
884 @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan if @dxchan;
885 $self->send_route($main::mycall, \&pc19, scalar(@localnodes)+1, $main::routeroot, @localnodes);
888 my @rawintcalls = map { $_->nodes } @localnodes if @localnodes;
890 foreach $node (@rawintcalls) {
891 push @intcalls, $node if grep $_ && $node != $_, @intcalls;
893 my $ref = Route::Node::get($self->{call});
894 my @rnodes = $ref->nodes;
895 foreach $node (@intcalls) {
896 push @remotenodes, Route::Node::get($node) if grep $_ && $node != $_, @rnodes, @remotenodes;
898 $self->send_route($main::mycall, \&pc19, scalar(@remotenodes), @remotenodes);
901 # get all the users connected on the above nodes and send them out
902 unless ($self->{do_pc9x}) {
903 foreach $node ($main::routeroot, @localnodes, @remotenodes) {
905 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
906 $self->send_route($main::mycall, \&pc16, 1, $node, @rout) if @rout && $self->user->wantsendpc16;
908 dbg("sent a null value") if isdbg('chanerr');
914 sub gen_my_pc92_config
918 if ($node->{call} eq $main::mycall) {
919 clear_pc92_changes(); # remove any slugged data, we are generating it as now
920 my @dxchan = grep { $_->call ne $main::mycall && !$_->{isolate} } DXChannel::get_all();
921 dbg("ROUTE: all dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('routelow');
922 my @localnodes = map { my $r = Route::get($_->{call}); $r ? $r : () } @dxchan;
923 dbg("ROUTE: localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('routelow');
924 return pc92c($node, @localnodes);
926 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
927 return pc92c($node, @rout);
931 sub send_last_pc92_config
935 if (my $l = $node->last_PC92C) {
938 $self->send_pc92_config($node);
947 dbg('DXProt::send_pc92_config') if isdbg('trace');
949 $node->last_PC92C(gen_my_pc92_config($node));
950 $self->send($node->last_PC92C);
953 sub broadcast_pc92_update
958 dbg("ROUTE: broadcast_pc92_update $call") if isdbg('obscount');
960 my $nref = Route::Node::get($call);
962 cluck("ERROR: broadcast_pc92_update - Route::Node $call disappeared");
963 $self->update_pc92_next;
966 my $l = $nref->last_PC92C(gen_my_pc92_config($nref));
967 $nref->lastid(last_pc9x_id());
968 $main::me->broadcast_route_pc9x($main::mycall, undef, $l, 0);
969 $self->update_pc92_next;
972 sub broadcast_pc92_keepalive
977 dbg("ROUTE: broadcast_pc92_keepalive $call") if isdbg('obscount');
979 my $nref = Route::Node::get($call);
981 cluck("ERROR: broadcast_pc92_keepalive - Route::Node $call disappeared");
982 $self->update_pc92_keepalive;
985 my $l = pc92k($nref);
986 $nref->lastid(last_pc9x_id());
987 $main::me->broadcast_route_pc9x($main::mycall, undef, $l, 0);
988 $self->update_pc92_keepalive;
991 sub time_out_pc92_routes
993 my @nodes = grep {$_->call ne $main::mycall && ($_->do_pc9x || $_->via_pc92)} Route::Node::get_all();
995 foreach my $n (@nodes) {
998 if (my $dxchan = DXChannel::get($n->call)) {
999 dbg("ROUTE: disconnecting local pc92 $dxchan->{call} on obscount") if isdbg('obscount');
1000 $dxchan->disconnect;
1003 my @parents = map {Route::Node::get($_)} $n->parents;
1006 dbg("ROUTE: deleting pc92 $_->{call} from $n->{call} on obscount") if isdbg('obscount');
1007 push @rdel, $n->del($_);
1011 dbg("ROUTE: obscount on $n->{call} now $o") if isdbg('obscount');
1015 $main::me->route_pc21($main::mycall, undef, $_) if $_;
1020 # route a message down an appropriate interface for a callsign
1022 # is called route(to, pcline);
1027 my ($self, $call, $line) = @_;
1029 if (ref $self && $call eq $self->{call}) {
1030 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1034 # always send it down the local interface if available
1035 my $dxchan = DXChannel::get($call);
1037 dbg("route: $call -> $dxchan->{call} direct" ) if isdbg('route');
1039 my $cl = Route::get($call);
1040 $dxchan = $cl->dxchan if $cl;
1042 if (ref $self && $dxchan eq $self) {
1043 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1046 dbg("route: $call -> $dxchan->{call} using normal route" ) if isdbg('route');
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 unless ($ref && UNIVERSAL::isa($ref, 'DXUser')) {
1121 dbg("DXProt process_rcmd: user $fromnode isn't a reference (check user_asc and tell G1TLH)");
1122 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1125 my $cref = Route::Node::get($fromnode);
1126 unless ($cref && UNIVERSAL::isa($cref, 'Route')) {
1127 dbg("DXProt process_rcmd: Route $fromnode isn't a reference (tell G1TLH)");
1128 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1131 Log('rcmd', 'in', ($ref->{priv}||0), $fromnode, $cmd);
1132 if ($cmd !~ /^\s*rcmd/i && $ref->homenode && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS!
1133 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
1134 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
1135 my $oldpriv = $self->{priv};
1136 $self->{priv} = $ref->{priv}; # assume the user's privilege level
1137 my @in = (DXCommandmode::run_cmd($self, $cmd));
1138 $self->{priv} = $oldpriv;
1139 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @in);
1140 delete $self->{remotecmd};
1142 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1145 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "your attempt is logged, Tut tut tut...!");
1148 my $ref = DXUser::get_current($tonode);
1149 if ($ref && $ref->is_clx) {
1150 $self->route($tonode, pc84($fromnode, $tonode, $user, $cmd));
1152 $self->route($tonode, pc34($fromnode, $tonode, $cmd));
1157 sub process_rcmd_reply
1159 my ($self, $tonode, $fromnode, $user, $line) = @_;
1160 if ($tonode eq $main::mycall) {
1161 my $s = $rcmds{$fromnode};
1163 my $dxchan = DXChannel::get($s->{call});
1164 my $ref = $user eq $tonode ? $dxchan : (DXChannel::get($user) || $dxchan);
1165 $ref->send($line) if $ref;
1166 delete $rcmds{$fromnode} if !$dxchan;
1168 # send unsolicited ones to the sysop
1169 my $dxchan = DXChannel::get($main::myalias);
1170 $dxchan->send($line) if $dxchan;
1173 my $ref = DXUser::get_current($tonode);
1174 if ($ref && $ref->is_clx) {
1175 $self->route($tonode, pc85($fromnode, $tonode, $user, $line));
1177 $self->route($tonode, pc35($fromnode, $tonode, $line));
1186 my $fromnode = shift;
1191 Log('rcmd', 'out', $fromnode, $line);
1192 if ($self->is_clx) {
1193 $self->send(pc85($main::mycall, $fromnode, $user, "$main::mycall:$line"));
1195 $self->send(pc35($main::mycall, $fromnode, "$main::mycall:$line"));
1200 # add a rcmd request to the rcmd queues
1203 my ($self, $to, $cmd) = @_;
1206 $r->{call} = $self->{call};
1207 $r->{t} = $main::systime;
1211 my $ref = Route::Node::get($to);
1212 my $dxchan = $ref->dxchan;
1213 if ($dxchan && $dxchan->is_clx) {
1214 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1216 route(undef, $to, pc34($main::mycall, $to, $cmd));
1223 my $pc39flag = shift || 0;
1224 my $call = $self->call;
1226 return if $self->{disconnecting}++;
1228 unless ($pc39flag == 1) {
1229 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1232 # get rid of any PC16/17/19
1233 eph_del_regex("^PC1[679]*$call");
1235 # do routing stuff, remove me from routing table
1236 my $node = Route::Node::get($call);
1238 Route::delete_interface($call);
1240 # unbusy and stop and outgoing mail
1241 my $mref = DXMsg::get_busy($call);
1242 $mref->stop_msg($call) if $mref;
1244 # remove outstanding pings
1245 delete $pings{$call};
1247 # I was the last node visited
1248 $self->user->node($main::mycall);
1250 # send info to all logged in thingies
1251 $self->tell_login('logoutn');
1253 Log('DXProt', $call . " Disconnected");
1255 $self->SUPER::disconnect;
1257 # here we determine what needs to go out of the routing table
1259 if ($node && $pc39flag != 2) {
1260 dbg('%Route::Node::List = ' . join(',', sort keys %Route::Node::list)) if isdbg('routedisc');
1262 @rout = $node->del($main::routeroot);
1264 dbg('@rout = ' . join(',', sort map {$_->call} @rout)) if isdbg('routedisc');
1266 # now we need to see what can't be routed anymore and came
1267 # in via this node (probably).
1269 while ($n != @rout) {
1271 for (Route::Node::get_all()) {
1272 unless ($_->dxchan) {
1273 push @rout, $_->delete;
1276 dbg('@rout = ' . join(',', sort map {$_->call} @rout)) if isdbg('routedisc');
1279 dbg('%Route::Node::List = ' . join(',', sort keys %Route::Node::list)) if isdbg('routedisc');
1281 # and all my ephemera as well
1284 eph_del_regex("^PC1[679].*$c");
1288 # broadcast to all other nodes that all the nodes connected to via me are gone
1289 unless ($pc39flag == 2) {
1290 $self->route_pc21($main::mycall, undef, @rout) if @rout;
1291 $self->route_pc92d($main::mycall, undef, $main::routeroot, $node) if $node;
1297 # send a talk message to this thingy
1301 my ($self, $from, $to, $via, $line, $origin) = @_;
1303 if ($self->{do_pc9x}) {
1304 $self->send(pc93($to, $from, $via, $line));
1306 $self->send(pc10($from, $to, $via, $line, $origin));
1308 Log('talk', $to, $from, '>' . ($via || $origin || $self->call), $line) unless $origin && $origin ne $main::mycall;
1311 # send it if it isn't the except list and isn't isolated and still has a hop count
1312 # taking into account filtering and so on
1318 my $generate = shift;
1319 my $no = shift; # the no of things to filter on
1321 my ($filter, $hops);
1324 for (; @_ && $no; $no--) {
1327 # don't send messages with $self's call in back to them
1328 if ($r->call eq $self->{call}) {
1329 dbg("PCPROT: trying to send $self->{call} back itself") if isdbg('chanerr');
1333 if (!$self->{isolate} && $self->{routefilter}) {
1336 ($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});
1340 dbg("PCPROT: $self->{call}/" . $r->call . " rejected by output filter") if isdbg('filter');
1343 dbg("was sent a null value") if isdbg('chanerr');
1346 push @rin, $r unless $self->{isolate} && $r->call ne $main::mycall;
1350 foreach my $line (&$generate(@rin, @_)) {
1353 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1355 $routeit = adjust_hops($self, $line); # adjust its hop count by node name
1356 next unless $routeit;
1359 $self->send($routeit);
1364 # broadcast everywhere
1369 my $generate = shift;
1371 my @dxchan = DXChannel::get_all_nodes();
1375 $line =~ /\^H(\d+)\^?\~?$/;
1376 return unless $1 > 0;
1378 unless ($self->{isolate}) {
1379 foreach $dxchan (@dxchan) {
1380 next if $dxchan == $self || $dxchan == $main::me;
1381 next if $origin eq $dxchan->{call}; # don't route some from this call back again.
1382 next unless $dxchan->isa('DXProt');
1384 $dxchan->send_route($origin, $generate, @_);
1389 # broadcast to non-pc9x nodes
1390 sub broadcast_route_nopc9x
1394 my $generate = shift;
1396 my @dxchan = DXChannel::get_all_nodes();
1400 $line =~ /\^H(\d+)\^?\~?$/;
1401 return unless $1 > 0;
1403 unless ($self->{isolate}) {
1404 foreach $dxchan (@dxchan) {
1405 next if $dxchan == $self || $dxchan == $main::me;
1406 next if $origin eq $dxchan->{call}; # don't route some from this call back again.
1407 next unless $dxchan->isa('DXProt');
1408 next if $dxchan->{do_pc9x};
1409 if ($generate == \&pc16 || $generate==\&pc17) {
1410 next unless $dxchan->user->wantsendpc16;
1412 $dxchan->send_route($origin, $generate, @_);
1417 # this is only used for next door nodes on init
1422 return unless $self->{do_pc9x};
1425 my $generate = shift;
1426 my $no = shift; # the no of things to filter on
1429 $line = &$generate(@_);
1433 # broadcast only to pc9x nodes
1434 sub broadcast_route_pc9x
1438 my $generate = shift;
1441 my @dxchan = DXChannel::get_all_nodes();
1444 if ($origin eq $main::mycall && $generate && !$line) {
1445 $line = &$generate(@_);
1448 $line =~ /\^H(\d+)\^\~?$/;
1449 unless ($1 > 0 && $self->{isolate}) {
1450 foreach $dxchan (@dxchan) {
1451 next if $dxchan == $self || $dxchan == $main::me;
1452 next if $origin eq $dxchan->{call}; # don't route some from this call back again.
1453 next unless $dxchan->isa('DXProt');
1454 next unless $dxchan->{do_pc9x};
1456 $dxchan->send($line);
1464 return unless $self->user->wantpc16;
1467 broadcast_route_nopc9x($self, $origin, \&pc16, $line, 1, @_);
1473 return unless $self->user->wantpc16;
1476 broadcast_route_nopc9x($self, $origin, \&pc17, $line, 1, @_);
1484 broadcast_route_nopc9x($self, $origin, \&pc19, $line, scalar @_, @_);
1492 broadcast_route_nopc9x($self, $origin, \&pc21, $line, scalar @_, @_);
1500 broadcast_route($self, $origin, \&pc24, $line, 1, @_);
1508 broadcast_route($self, $origin, \&pc41, $line, 1, @_);
1511 # this is probably obsolete now
1518 broadcast_route($self, $origin, \&pc50, $line, 1, @_);
1526 broadcast_route_pc9x($self, $origin, \&pc92c, $line, 1, @_);
1534 broadcast_route_pc9x($self, $origin, \&pc92a, $line, 1, @_);
1542 broadcast_route_pc9x($self, $origin, \&pc92d, $line, 1, @_);
1549 my ($filter, $hops) = (1, 1);
1551 if ($self->{inroutefilter}) {
1552 ($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);
1553 dbg("PCPROT: $self->{call}/" . $r->call . ' rejected by in_filter_route') if !$filter && isdbg('chanerr');
1561 my $t = shift || $eph_restime;
1565 $s =~ s/\^H\d\d?\^?\~?$//;
1566 $r = 1 if exists $eph{$s}; # pump up the dup if it keeps circulating
1567 $eph{$s} = $main::systime + $t;
1568 dbg("PCPROT: emphemeral duplicate") if $r && isdbg('chanerr');
1576 while (($key, $val) = each %eph) {
1577 if ($key =~ m{$regex}) {
1587 while (($key, $val) = each %eph) {
1588 if ($main::systime >= $val) {
1599 while (($key, $val) = each %eph) {
1600 push @out, $key, $val;
1607 goto &DXCommandmode::run_cmd;
1611 # import any msgs in the chat directory
1612 # the messages are sent to the chat group which forms the
1613 # the first part of the name (eg: solar.1243.txt would be
1614 # sent to chat group SOLAR)
1616 # Each message found is sent: one non-blank line to one chat
1617 # message. So 4 lines = 4 chat messages.
1619 # The special name LOCAL is for local users ANN
1620 # The special name ALL is for ANN/FULL
1621 # The special name SYSOP is for ANN/SYSOP
1625 # are there any to do in this directory?
1626 return unless -d $chatimportfn;
1627 unless (opendir(DIR, $chatimportfn)) {
1628 dbg("can\'t open $chatimportfn $!") if isdbg('msg');
1629 Log('msg', "can\'t open $chatimportfn $!");
1633 my @names = readdir(DIR);
1636 foreach $name (@names) {
1637 next if $name =~ /^\./;
1638 my $splitit = $name =~ /^split/;
1639 my $fn = "$chatimportfn/$name";
1641 unless (open(MSG, $fn)) {
1642 dbg("can\'t open import file $fn $!") if isdbg('msg');
1643 Log('msg', "can\'t open import file $fn $!");
1647 my @msg = map { s/\r?\n$//; $_ } <MSG>;
1651 my @cat = split /\./, $name;
1652 my $target = uc $cat[0];
1654 foreach my $text (@msg) {
1655 next unless $text && $text !~ /^\s*#/;
1656 if ($target eq 'ALL' || $target eq 'LOCAL' || $target eq 'SYSOP' || $target eq 'WX') {
1657 my $sysop = uc $target eq 'SYSOP' ? '*' : ' ';
1658 my $wx = uc $target eq 'WX' ? '1' : '0';
1660 $via = '*' if $target eq 'ALL' || $target eq 'SYSOP';
1661 Log('ann', $target, $main::mycall, $text);
1662 $main::me->normal(DXProt::pc93($target, $main::mycall, $via, $text));
1664 DXCommandmode::send_chats($main::me, $target, $text);
1670 # start a pc92 find operation
1675 my $key = "$dxchan->{call}|$target";
1676 if ($pc92_find{$key}) {
1681 # function (not method) to handle pc92 find returns
1682 sub handle_pc92_find_reply
1684 my ($dxchan, $node, $from, $target, $flag, $ms) = @_;
1686 $dxchan->print_pc92_find_reply($node, $target, $flag, $ms) if $dxchan->can('print_pc92_find_return');