3 # This module impliments the protocal mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
37 use Time::HiRes qw(gettimeofday tv_interval);
38 use Mojo::IOLoop::Subprocess;
42 use vars qw($pc11_max_age $pc23_max_age $last_pc50 $eph_restime $eph_info_restime $eph_pc34_restime
43 $last_hour $last10 %eph %pings %rcmds $ann_to_talk
44 $pingint $obscount %pc19list $chatdupeage $chatimportfn
45 $pc19_version $myprot_version
46 %nodehops $baddx $badspotter $badnode $censorpc
47 $allowzero $decode_dk0wcy $send_opernam @checklist
48 $eph_pc15_restime $pc92_update_period $pc92_obs_timeout
49 %pc92_find $pc92_find_timeout $pc92_short_update_period
50 $next_pc92_obs_timeout $pc92_slug_changes $last_pc92_slug
51 $pc92_extnode_update_period $pc50_interval
52 $pc92_keepalive_period
55 $pc11_max_age = 1*3600; # the maximum age for an incoming 'real-time' pc11
56 $pc23_max_age = 1*3600; # the maximum age for an incoming 'real-time' pc23
58 $last_hour = time; # last time I did an hourly periodic update
59 %rcmds = (); # outstanding rcmd requests outbound
60 %nodehops = (); # node specific hop control
61 %pc19list = (); # list of outstanding PC19s that haven't had PC16s on them
63 $censorpc = 1; # Do a BadWords::check on text fields and reject things
64 # loads of 'bad things'
65 $baddx = new DXHash "baddx";
66 $badspotter = new DXHash "badspotter";
67 $badnode = new DXHash "badnode";
68 $last10 = $last_pc50 = time;
71 $eph_info_restime = 18*60*60;
72 $eph_pc15_restime = 6*60;
73 $eph_pc34_restime = 30;
76 $chatdupeage = 20 * 60;
77 $chatimportfn = "$main::root/chat_import";
78 $pc19_version = 5455; # the visible version no for outgoing PC19s generated from pc59
79 $pc92_update_period = 4*60*60; # the period between outgoing PC92 C updates
80 $pc92_short_update_period = 15*60; # shorten the update period after a connection or start up
81 $pc92_extnode_update_period = 1*60*60; # the update period for external nodes
82 $pc92_keepalive_period = 1*60*60; # frequency of PC92 K (keepalive) records
83 %pc92_find = (); # outstanding pc92 find operations
84 $pc92_find_timeout = 30; # maximum time to wait for a reply
89 [ qw(i c c m bp bc c) ], # pc10
90 [ qw(i f m d t m c c h) ], # pc11
91 [ qw(i c bm m bm bm p h) ], # pc12
95 undef , # pc16 has to be validated manually
96 [ qw(i c c h) ], # pc17
98 undef , # pc19 has to be validated manually
99 undef , # pc20 no validation
100 [ qw(i c m h) ], # pc21
101 undef , # pc22 no validation
102 [ qw(i d n n n n m c c h) ], # pc23
103 [ qw(i c p h) ], # pc24
104 [ qw(i c c n n) ], # pc25
105 [ qw(i f m d t m c c bc) ], # pc26
106 [ qw(i d n n n n m c c bc) ], # pc27
107 [ qw(i c c m c d t p m bp n p bp bc) ], # pc28
108 [ qw(i c c n m) ], # pc29
109 [ qw(i c c n) ], # pc30
110 [ qw(i c c n) ], # pc31
111 [ qw(i c c n) ], # pc32
112 [ qw(i c c n) ], # pc33
113 [ qw(i c c m) ], # pc34
114 [ qw(i c c m) ], # pc35
115 [ qw(i c c m) ], # pc36
116 [ qw(i c c n m) ], # pc37
117 undef, # pc38 not interested
118 [ qw(i c m) ], # pc39
119 [ qw(i c c m p n) ], # pc40
120 [ qw(i c n m h) ], # pc41
121 [ qw(i c c n) ], # pc42
122 undef, # pc43 don't handle it
123 [ qw(i c c n m m c) ], # pc44
124 [ qw(i c c n m) ], # pc45
125 [ qw(i c c n) ], # pc46
128 [ qw(i c m h) ], # pc49
129 [ qw(i c n h) ], # pc50
130 [ qw(i c c n) ], # pc51
140 [ qw(i f m d t m c c a h) ], # pc61
152 [ qw(i d n n n n n n m m m c c h) ], # pc73
163 [ qw(i c c c m) ], # pc84
164 [ qw(i c c c m) ], # pc85
171 [ qw(i c f l)], # pc92
172 [ qw(i c f *m c *c m)], # pc93
175 # use the entry in the check list to check the field list presented
176 # return OK if line NOT in check list (for now)
183 return 0 if $n < 0 || $n > @checklist;
184 my $ref = $checklist[$n];
185 return 0 unless ref $ref;
188 for ($i = 1; $i < @$ref; $i++) {
189 my ($blank, $act) = $$ref[$i] =~ /^(b?)(\w)$/;
190 return 0 unless $act;
191 next if $blank eq 'b' && $pc->[$i] =~ /^[ \*]$/;
192 next if $blank eq '*' && $pc->[$i] =~ /^\*$/;
194 return $i unless is_callsign($pc->[$i]);
195 } elsif ($act eq 'i') {
197 } elsif ($act eq 'm') {
198 return $i unless is_pctext($pc->[$i]);
199 } elsif ($act eq 'p') {
200 return $i unless is_pcflag($pc->[$i]);
201 } elsif ($act eq 'f') {
202 return $i unless is_freq($pc->[$i]);
203 } elsif ($act eq 'n') {
204 return $i unless $pc->[$i] =~ /^[\d ]+$/;
205 } elsif ($act eq 'h') {
206 return $i unless $pc->[$i] =~ /^H\d\d?$/;
207 } elsif ($act eq 'd') {
208 return $i unless $pc->[$i] =~ /^\s*\d+-\w\w\w-[12][90]\d\d$/;
209 } elsif ($act eq 't') {
210 return $i unless $pc->[$i] =~ /^[012]\d[012345]\dZ$/;
211 } elsif ($act eq 'l') {
212 return $i unless $pc->[$i] =~ /^[A-Z]$/;
213 } elsif ($act eq 'a') {
214 return $i unless is_ipaddr($pc->[$i]);
223 my $period = shift || ($self->{do_pc9x} ? $pc92_update_period : $pc92_extnode_update_period);
224 $self->{next_pc92_update} = $main::systime + $period - int rand($period / 4);
225 dbg("ROUTE: update_pc92_next: $self->{call} " . atime($self->{next_pc92_update})) if isdbg('obscount');
228 sub update_pc92_keepalive
231 my $period = shift || $pc92_keepalive_period;
232 $self->{next_pc92_keepalive} = $main::systime + $period - int rand($period / 4);
233 dbg("ROUTE: update_pc92_keepalive: $self->{call} " . atime($self->{next_pc92_keepalive})) if isdbg('obscount');
238 my $fn = localdata("hop_table.pl");
242 my $user = DXUser::get($main::mycall);
243 die "User $main::mycall not setup or disappeared RTFM" unless $user;
245 $myprot_version += $main::version*100;
246 $main::me = DXProt->new($main::mycall, 0, $user);
247 $main::me->{here} = 1;
248 $main::me->{state} = "indifferent";
249 $main::me->{sort} = 'S'; # S for spider
250 $main::me->{priv} = 9;
251 $main::me->{metric} = 0;
252 $main::me->{pingave} = 0;
253 $main::me->{registered} = 1;
254 $main::me->{version} = $main::version;
255 $main::me->{build} = $main::build;
256 $main::me->{do_pc9x} = 1;
257 $main::me->{hostname} = $main::clusteraddr;
258 $main::me->update_pc92_next($pc92_short_update_period);
259 $main::me->update_pc92_keepalive;
263 # obtain a new connection this is derived from dxchannel
268 my $self = DXChannel::alloc(@_);
270 # add this node to the table, the values get filled in later
274 # if we have an entry already, then send a PC21 to all connect
275 # old style connections, because we are about to get the real deal
276 if (my $ref = Route::Node::get($call)) {
277 dbg("ROUTE: $call is already in the routing table, deleting") if isdbg('route');
278 my @rout = $ref->delete;
279 $self->route_pc21($main::mycall, undef, @rout) if @rout;
281 $main::routeroot->add($call, '5000', Route::here(1), $self->{conn}->peerhost) if $call ne $main::mycall;
286 # this is how a pc connection starts (for an incoming connection)
287 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
288 # all the crap that comes between).
291 my ($self, $line, $sort) = @_;
292 my $call = $self->{call};
293 my $user = $self->{user};
296 my $host = $self->{conn}->peerhost;
297 $host ||= "AGW Port #$self->{conn}->{agwport}" if exists $self->{conn}->{agwport};
299 $self->{hostname} = $host if is_ipaddr($host);
301 Log('DXProt', "$call connected from $host");
303 # remember type of connection
304 $self->{consort} = $line;
305 $self->{outbound} = $sort eq 'O';
306 my $priv = $user->priv;
307 $priv = $user->priv(1) unless defined $priv;
308 $self->{priv} = $priv; # other clusters can always be 'normal' users
309 $self->{lang} = $user->lang || 'en';
310 $self->{isolate} = $user->{isolate};
311 $self->{consort} = $line; # save the connection type
315 # sort out registration
316 $self->{registered} = 1;
318 # get the output filters
319 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
320 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
321 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
322 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
323 $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) unless $self->{isolate};
324 $self->{pc92filter} = Filter::read_in('pc92', $call, 0) || Filter::read_in('pc92', 'node_default', 0) unless $self->{isolate} ;
327 # get the INPUT filters (these only pertain to Clusters)
328 $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
329 $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
330 $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
331 $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
332 $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1) unless $self->{isolate};
333 $self->{inpc92filter} = Filter::read_in('pc92', $call, 0) || Filter::read_in('pc92', 'node_default', 0) unless $self->{isolate} ;
336 # set unbuffered and no echo
337 $self->send_now('B',"0");
338 $self->send_now('E',"0");
339 $self->conn->echo(0) if $self->conn->can('echo');
341 # ping neighbour node stuff
342 my $ping = $user->pingint;
343 $ping = $pingint unless defined $ping;
344 $self->{pingint} = $ping;
345 $self->{nopings} = $user->nopings || $obscount;
346 $self->{pingtime} = [ ];
347 $self->{pingave} = 999;
348 $self->{metric} ||= 100;
349 $self->{lastping} = $main::systime;
351 # send initialisation string
352 unless ($self->{outbound}) {
356 $self->state('init');
357 $self->{pc50_t} = $main::systime;
359 # send info to all logged in thingies
360 $self->tell_login('loginn');
362 # run a script send the output to the debug file
363 my $script = new Script(lc $call) || new Script('node_default');
364 $script->run($self) if $script;
366 # set up a config broadcast "quite soon" to converge tables quicker
367 $main::me->update_pc92_next($pc92_short_update_period);
368 $self->update_pc92_next($pc92_short_update_period);
370 # set next keepalive time
371 $self->update_pc92_keepalive;
375 # send outgoing 'challenge'
381 $self->send(pc18(($self->{isolate} || !$self->user->wantpc9x) ? "" : " pc9x"));
385 # This is the normal pcxx despatcher
389 my ($self, $line) = @_;
391 if ($line =~ '^<\w+\s' && $main::do_xml) {
392 DXXml::normal($self, $line);
396 my @field = split /\^/, $line;
397 return unless @field;
399 pop @field if $field[-1] eq '~';
401 # print join(',', @field), "\n";
404 # process PC frames, this will fail unless the frame starts PCnn
405 my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
406 unless (defined $pcno && $pcno >= 10 && $pcno <= 99) {
407 dbg("PCPROT: unknown protocol") if isdbg('chanerr');
411 # check for and dump bad protocol messages
412 my $n = check($pcno, \@field);
414 dbg("PCPROT: bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")") if isdbg('chanerr');
418 # modify the hop count here
419 if ($self != $main::me) {
420 if (my ($hops, $trail) = $line =~ /\^H(\d+)(\^?\~?)?$/) {
424 $line =~ s/\^H(\d+)(\^?\~?)?$/sprintf('^H%d%s', $hops, $trail)/e;
425 $field[-1] = "H$hops";
429 if (defined &Local::pcprot) {
431 eval { $r = Local::pcprot($self, $pcno, $line, @field); };
432 return if $r; # i.e don't process it
435 # send it out for processing
436 my $origin = $self->{call};
438 my $sub = "handle_$pcno";
440 if ($self->can($sub)) {
441 $self->$sub($pcno, $line, $origin, \@field);
443 $self->handle_default($pcno, $line, $origin, \@field);
448 # This is called from inside the main cluster processing loop and is used
449 # for despatching commands that are doing some long processing job
454 my @dxchan = DXChannel::get_all();
459 if ($t - $last10 >= 10) {
467 # send out a pc50 on EVERY channel all at once
468 if ($t >= $last_pc50 + $pc50_interval) {
469 $pc50s = pc50($main::me, scalar DXChannel::get_all_users);
474 foreach $dxchan (@dxchan) {
475 next unless $dxchan->is_node;
476 next if $dxchan == $main::me;
479 $dxchan->send($pc50s) if !$dxchan->{do_pc9x} && $pc50s;
481 # send a ping out on this channel
482 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
483 if ($dxchan->{nopings} <= 0) {
484 dbg("ROUTE: $dxchan->{call} disconnected on ping obscount") if isdbg('obscount');
487 DXXml::Ping::add($main::me, $dxchan->call);
488 $dxchan->{nopings} -= 1;
489 $dxchan->{lastping} = $t;
490 $dxchan->{lastping} += $dxchan->{pingint} / 2 unless @{$dxchan->{pingtime}};
491 dbg("ROUTE: $dxchan->{call} ping obscount = $dxchan->{nopings}") if isdbg('obscount');
498 # send out config broadcasts
499 foreach $dxchan (@dxchan) {
500 next unless $dxchan->is_node;
502 # send out a PC92 config record if required for me and
503 # all my non pc9x dependent nodes. But for dependent nodes we only do
504 # this if we have not seen any from anyone else for at least half
505 # of one update period. This should stop quite a bit of excess C
506 # records. Someone will win, it does not really matter who, because
507 # we always believe "us".
508 if ($main::systime >= $dxchan->{next_pc92_update}) {
509 if ($dxchan == $main::me || !$dxchan->{do_pc9x}) {
510 dbg("ROUTE: pc92 broadcast candidate: $dxchan->{call}") if isdbg('obscount');
511 my $ref = Route::Node::get($dxchan->{call});
512 if ($dxchan == $main::me || ($ref && ($ref->measure_pc9x_t($main::systime-$main::systime_daystart)) >= $pc92_extnode_update_period/2)) {
513 $dxchan->broadcast_pc92_update($dxchan->{call});
515 $dxchan->update_pc92_next;
518 $dxchan->update_pc92_next; # this won't actually do anything, it's just to be tidy
523 # do the keepalive for me, if required
524 if ($main::systime >= $main::me->{next_pc92_keepalive}) {
525 time_out_pc92_routes();
526 $main::me->broadcast_pc92_keepalive($main::mycall);
529 if ($pc92_slug_changes && $main::systime >= $last_pc92_slug + $pc92_slug_changes) {
530 my ($add, $del) = gen_pc92_changes();
531 $main::me->route_pc92d($main::mycall, undef, $main::routeroot, @$del) if @$del;
532 $main::me->route_pc92a($main::mycall, undef, $main::routeroot, @$add) if @$add;
533 clear_pc92_changes();
537 if ($main::systime - 3600 > $last_hour) {
538 $last_hour = $main::systime;
543 # finish up a pc context
547 # some active measures
555 my @dxchan = DXChannel::get_all();
559 # send it if it isn't the except list and isn't isolated and still has a hop count
560 # taking into account filtering and so on
561 foreach $dxchan (@dxchan) {
562 next if $dxchan == $main::me;
563 next if $dxchan == $self && $self->is_node;
564 if ($line =~ /PC61/ && !($dxchan->is_spider || $dxchan->is_user)) {
566 my @f = split /\^/, $line;
567 $pc11 = join '^', 'PC11', @f[1..7,9];
569 $dxchan->dx_spot($pc11, $self->{isolate}, @_, $self->{call});
571 $dxchan->dx_spot($line, $self->{isolate}, @_, $self->{call});
583 if ($self->{spotsfilter}) {
584 ($filter, $hops) = $self->{spotsfilter}->it(@_);
585 return unless $filter;
587 send_prot_line($self, $filter, $hops, $isolate, $line);
592 my ($self, $filter, $hops, $isolate, $line) = @_;
598 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
600 $routeit = adjust_hops($self, $line); # adjust its hop count by node name
601 return unless $routeit;
604 $self->send($routeit);
606 $self->send($routeit) unless $self->{isolate} || $isolate;
615 my @dxchan = DXChannel::get_all();
617 my @dxcc = ((Prefix::cty_data($_[6]))[0..2], (Prefix::cty_data($_[7]))[0..2]);
619 # send it if it isn't the except list and isn't isolated and still has a hop count
620 # taking into account filtering and so on
621 foreach $dxchan (@dxchan) {
622 next if $dxchan == $main::me;
623 next if $dxchan == $self && $self->is_node;
627 $dxchan->wwv($line, $self->{isolate}, @_, $self->{call}, @dxcc);
638 if ($self->{wwvfilter}) {
639 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_]);
640 return unless $filter;
642 send_prot_line($self, $filter, $hops, $isolate, $line)
649 my @dxchan = DXChannel::get_all();
651 my @dxcc = ((Prefix::cty_data($_[10]))[0..2], (Prefix::cty_data($_[11]))[0..2]);
653 # send it if it isn't the except list and isn't isolated and still has a hop count
654 # taking into account filtering and so on
655 foreach $dxchan (@dxchan) {
656 next if $dxchan == $main::me;
657 next if $dxchan == $self;
659 $dxchan->wcy($line, $self->{isolate}, @_, $self->{call}, @dxcc);
670 if ($self->{wcyfilter}) {
671 ($filter, $hops) = $self->{wcyfilter}->it(@_);
672 return unless $filter;
674 send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
681 my $from_pc9x = shift;
683 my @dxchan = DXChannel::get_all();
687 my $text = unpad($_[2]);
690 if ($_[3] eq '*') { # sysops
692 } elsif ($_[3] gt ' ') { # speciality list handling
693 my ($name) = split /\./, $_[3];
694 $target = "$name"; # put the rest in later (if bothered)
701 $target = "ALL" if !$target;
704 # obtain country codes etc
705 my @a = Prefix::cty_data($from);
706 my @b = Prefix::cty_data($_[4]);
707 if ($self->{inannfilter}) {
708 my ($filter, $hops) =
709 $self->{inannfilter}->it(@_, $self->{call},
711 @b[0..2], $a[3], $b[3]);
713 dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
718 # the sysop ('*') thing is an attempt to minimise the damage caused by non-updated PC93 generators
719 if (AnnTalk::dup($from, $target, $_[2]) || ($_[3] eq '*' && AnnTalk::dup($from, 'ALL', $_[2]))) {
720 my $dxchan = DXChannel::get($from);
721 if ($self == $main::me && $dxchan && $dxchan->is_user) {
722 if ($dxchan->priv < 5) {
723 $dxchan->send($dxchan->msg('dup'));
727 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
732 Log('ann', $target, $from, $text);
734 # send it if it isn't the except list and isn't isolated and still has a hop count
735 # taking into account filtering and so on
736 foreach $dxchan (@dxchan) {
737 next if $dxchan == $main::me;
738 next if $dxchan == $self && $self->is_node;
739 next if $from_pc9x && $dxchan->{do_pc9x};
740 next if $target eq 'LOCAL' && $dxchan->is_node;
741 $dxchan->announce($line, $self->{isolate}, $to, $target, $text, @_, $self->{call},
746 my $msgid = int rand(1000);
751 $msgid = 1 if $msgid > 999;
759 my $from_pc9x = shift;
761 my @dxchan = DXChannel::get_all();
764 my $text = unpad($_[2]);
768 # munge the group and recast the line if required
769 if ($target =~ s/\.LST$//) {
773 # obtain country codes etc
774 my @a = Prefix::cty_data($from);
775 my @b = Prefix::cty_data($_[4]);
776 if ($self->{inannfilter}) {
777 my ($filter, $hops) =
778 $self->{inannfilter}->it(@_, $self->{call},
780 @b[0..2], $a[3], $b[3]);
782 dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
787 if (AnnTalk::dup($from, $target, $_[2], $main::systime + $chatdupeage)) {
788 my $dxchan = DXChannel::get($from);
789 if ($self == $main::me && $dxchan && $dxchan->is_user) {
790 if ($dxchan->priv < 5) {
791 $dxchan->send($dxchan->msg('dup'));
795 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
801 Log('chat', $target, $from, $text);
803 # send it if it isn't the except list and isn't isolated and still has a hop count
804 # taking into account filtering and so on
805 foreach $dxchan (@dxchan) {
806 if ($dxchan->is_node) {
807 next if $dxchan == $main::me;
808 next if $dxchan == $self;
809 next if $from_pc9x && $dxchan->do_pc9x;
810 next unless $dxchan->is_spider && $dxchan->do_pc9x;
811 next if $target eq 'LOCAL';
814 $dxchan->chat($line, $self->{isolate}, $target, $_[1],
815 $text, @_, $self->{call}, @a[0..2], @b[0..2]);
829 if ($self->{annfilter}) {
830 ($filter, $hops) = $self->{annfilter}->it(@_);
831 return unless $filter;
833 send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
846 if ($self->{annfilter}) {
847 ($filter, $hops) = $self->{annfilter}->it(@_);
848 return unless $filter;
850 if (($self->is_spider || $self->is_ak1a) && $_[1] ne $main::mycall) {
851 send_prot_line($self, $filter, $hops, $isolate, $line);
856 sub send_local_config
860 dbg('DXProt::send_local_config') if isdbg('trace');
868 if ($self->{isolate}) {
869 dbg("send_local_config: isolated");
870 @localnodes = ( $main::routeroot );
871 $self->send_route($main::mycall, \&pc19, 1, $main::routeroot);
872 } elsif ($self->{do_pc9x}) {
873 dbg("send_local_config: doing pc9x");
874 my $node = Route::Node::get($self->{call});
875 # $self->send_last_pc92_config($main::routeroot);
876 # $self->send(pc92a($main::routeroot, $node)) unless $main::routeroot->last_PC92C =~ /$self->{call}/;
877 $self->send(pc92a($main::routeroot, $node));
878 $self->send(pc92k($main::routeroot));
880 # create a list of all the nodes that are not connected to this connection
881 # and are not themselves isolated, this to make sure that isolated nodes
882 # don't appear outside of this node
884 dbg("send_local_config: traditional");
886 # send locally connected nodes
887 my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
888 @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan if @dxchan;
889 $self->send_route($main::mycall, \&pc19, scalar(@localnodes)+1, $main::routeroot, @localnodes);
892 my @rawintcalls = map { $_->nodes } @localnodes if @localnodes;
894 foreach $node (@rawintcalls) {
895 push @intcalls, $node if grep $_ && $node != $_, @intcalls;
897 my $ref = Route::Node::get($self->{call});
898 my @rnodes = $ref->nodes;
899 foreach $node (@intcalls) {
900 push @remotenodes, Route::Node::get($node) if grep $_ && $node != $_, @rnodes, @remotenodes;
902 $self->send_route($main::mycall, \&pc19, scalar(@remotenodes), @remotenodes);
905 # get all the users connected on the above nodes and send them out
906 unless ($self->{do_pc9x}) {
907 foreach $node ($main::routeroot, @localnodes, @remotenodes) {
909 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
910 $self->send_route($main::mycall, \&pc16, 1, $node, @rout) if @rout && $self->user->wantsendpc16;
912 dbg("sent a null value") if isdbg('chanerr');
918 sub gen_my_pc92_config
922 if ($node->{call} eq $main::mycall) {
923 clear_pc92_changes(); # remove any slugged data, we are generating it as now
924 my @dxchan = grep { $_->call ne $main::mycall && !$_->{isolate} } DXChannel::get_all();
925 dbg("ROUTE: all dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('routelow');
926 my @localnodes = map { my $r = Route::get($_->{call}); $r ? $r : () } @dxchan;
927 dbg("ROUTE: localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('routelow');
928 return pc92c($node, @localnodes);
930 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
931 return pc92c($node, @rout);
935 sub send_last_pc92_config
939 if (my $l = $node->last_PC92C) {
942 $self->send_pc92_config($node);
951 dbg('DXProt::send_pc92_config') if isdbg('trace');
953 $node->last_PC92C(gen_my_pc92_config($node));
954 $self->send($node->last_PC92C);
957 sub broadcast_pc92_update
962 dbg("ROUTE: broadcast_pc92_update $call") if isdbg('obscount');
964 my $nref = Route::Node::get($call);
966 cluck("ERROR: broadcast_pc92_update - Route::Node $call disappeared");
967 $self->update_pc92_next;
970 my $l = $nref->last_PC92C(gen_my_pc92_config($nref));
971 $nref->lastid(last_pc9x_id());
972 $main::me->broadcast_route_pc9x($main::mycall, undef, $l, 0);
973 $self->update_pc92_next;
976 sub broadcast_pc92_keepalive
981 dbg("ROUTE: broadcast_pc92_keepalive $call") if isdbg('obscount');
983 my $nref = Route::Node::get($call);
985 cluck("ERROR: broadcast_pc92_keepalive - Route::Node $call disappeared");
986 $self->update_pc92_keepalive;
989 my $l = pc92k($nref);
990 $nref->lastid(last_pc9x_id());
991 $main::me->broadcast_route_pc9x($main::mycall, undef, $l, 0);
992 $self->update_pc92_keepalive;
995 sub time_out_pc92_routes
997 my @nodes = grep {$_->call ne $main::mycall && ($_->do_pc9x || $_->via_pc92)} Route::Node::get_all();
999 foreach my $n (@nodes) {
1000 my $o = $n->dec_obs;
1002 if (my $dxchan = DXChannel::get($n->call)) {
1003 dbg("ROUTE: disconnecting local pc92 $dxchan->{call} on obscount") if isdbg('obscount');
1004 $dxchan->disconnect;
1007 my @parents = map {Route::Node::get($_)} $n->parents;
1010 dbg("ROUTE: deleting pc92 $_->{call} from $n->{call} on obscount") if isdbg('obscount');
1011 push @rdel, $n->del($_);
1015 dbg("ROUTE: obscount on $n->{call} now $o") if isdbg('obscount');
1019 $main::me->route_pc21($main::mycall, undef, $_) if $_;
1024 # route a message down an appropriate interface for a callsign
1026 # is called route(to, pcline);
1031 my ($self, $call, $line) = @_;
1033 if (ref $self && $call eq $self->{call}) {
1034 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1038 # always send it down the local interface if available
1039 my $dxchan = DXChannel::get($call);
1041 dbg("route: $call -> $dxchan->{call} direct" ) if isdbg('route');
1043 my $cl = Route::get($call);
1044 $dxchan = $cl->dxchan if $cl;
1046 if (ref $self && $dxchan eq $self) {
1047 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1050 dbg("route: $call -> $dxchan->{call} using normal route" ) if isdbg('route');
1055 my $routeit = adjust_hops($dxchan, $line); # adjust its hop count by node name
1057 $dxchan->send($routeit) unless $dxchan == $main::me;
1060 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
1065 # obtain the hops from the list for this callsign and pc no
1071 my $hops = $DXProt::hopcount{$pcno};
1072 $hops = $DXProt::def_hopcount if !$hops;
1077 # adjust the hop count on a per node basis using the user loadable
1078 # hop table if available or else decrement an existing one
1085 my $call = $self->{call};
1088 if (($hops) = $s =~ /\^H([-\d]+)\^?~?$/o) {
1089 my ($pcno) = $s =~ /^PC(\d\d)/o;
1090 confess "$call called adjust_hops with '$s'" unless $pcno;
1091 my $ref = $nodehops{$call} if %nodehops;
1093 my $newhops = $ref->{$pcno};
1094 return "" if defined $newhops && $newhops == 0;
1095 $newhops = $ref->{default} unless $newhops;
1096 return "" if defined $newhops && $newhops == 0;
1097 $newhops = $hops unless $newhops;
1098 return "" unless $newhops > 0;
1099 $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops != $hops;
1101 return "" unless $hops > 0;
1113 return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1114 do "$main::data/hop_table.pl";
1121 my ($self, $tonode, $fromnode, $user, $cmd) = @_;
1123 if ($tonode eq $main::mycall) {
1124 my $ref = DXUser::get_current($fromnode);
1125 unless ($ref && UNIVERSAL::isa($ref, 'DXUser')) {
1126 dbg("DXProt process_rcmd: user $fromnode isn't a reference (check user_asc and tell G1TLH)");
1127 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1130 Log('rcmd', 'in', ($ref->{priv}||0), $fromnode, $cmd, $user);
1131 my $cref = Route::Node::get($fromnode);
1132 unless ($cref && UNIVERSAL::isa($cref, 'Route')) {
1133 dbg("DXProt process_rcmd: Route $fromnode isn't a reference (tell G1TLH)");
1134 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1137 if ($cmd !~ /^\s*rcmd/i && $ref->homenode && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS!
1138 if ($ref->{priv}) { # you have to have SOME privilege, the commands have further filtering
1139 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
1140 my $oldpriv = $self->{priv};
1141 $self->{priv} = 1; # set a maximum privilege
1143 # park homenode and user for any spawned command that run_cmd may do.
1144 $self->{_rcmd_user} = $user;
1145 $self->{_rcmd_fromnode} = $fromnode;
1146 my @in = (DXCommandmode::run_cmd($self, $cmd));
1147 delete $self->{_rcmd_fromnode};
1148 delete $self->{_rcmd_user};
1149 $self->{priv} = $oldpriv;
1150 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @in);
1151 delete $self->{remotecmd};
1153 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1156 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "your attempt is logged, Tut tut tut...!");
1159 my $ref = DXUser::get_current($tonode);
1160 if ($ref && $ref->is_clx) {
1161 $self->route($tonode, pc84($fromnode, $tonode, $user, $cmd));
1163 $self->route($tonode, pc34($fromnode, $tonode, $cmd));
1173 my $fromnode = shift;
1178 Log('rcmd', 'out', $fromnode, $line, $user);
1179 if ($self->is_clx) {
1180 $self->send(pc85($main::mycall, $fromnode, $user, "$main::mycall:$line"));
1182 $self->send(pc35($main::mycall, $fromnode, "$main::mycall:$line"));
1187 # Punt off a long running command into a separate process - this will be caused by an rcmd from outside
1189 # This is called from commands to run some potentially long running
1190 # function. The process forks and then runs the function and returns
1191 # the result back to the cmd.
1193 # NOTE: this merely forks the current process and then runs the cmd in that (current) context.
1194 # IT DOES NOT START UP SOME NEW PROGRAM AND RELIES ON THE FACT THAT IT IS RUNNING DXSPIDER
1195 # THE CURRENT CONTEXT!!
1197 # call: $self->spawn_cmd($original_cmd_line, \<function>, [cb => sub{...}], [prefix => "cmd> "], [progress => 0|1], [args => [...]]);
1203 my $call = $self->{call};
1207 my $cb = delete $args{cb};
1208 my $prefix = delete $args{prefix};
1209 my $progress = delete $args{progress};
1210 my $args = delete $args{args} || [];
1211 my $t0 = [gettimeofday];
1213 # remembered from process_cmd when spawn_cmd was called thru DXCommandmode::run_cmd which was called by process_rcmd
1214 my $fromnode = $self->{_rcmd_fromnode};
1215 my $user = $self->{_rcmd_user};
1219 my $fc = Mojo::IOLoop::Subprocess->new;
1221 # just behave normally if something has set the "one-shot" _nospawn in the channel
1222 if ($self->{_nospawn}) {
1223 eval { @out = $cmdref->(@$args); };
1225 DXDebug::dbgprintring(25);
1226 push @out, DXDebug::shortmess($@);
1231 # $fc->serializer(\&encode_json);
1232 # $fc->deserializer(\&decode_json);
1236 if (isdbg('progress')) {
1237 my $s = qq{line: "$line"};
1238 $s .= ", args: " . join(', ', map { defined $_ ? qq{'$_'} : q{'undef'} } @$args) if $args && @$args;
1241 eval { @out = $cmdref->(@$args); };
1243 DXDebug::dbgprintring(25);
1244 push @out, DXDebug::shortmess($@);
1250 my ($fc, $err, @res) = @_;
1251 my $self = DXChannel::get($call);
1252 return unless $self;
1255 my $s = "DXCommand::spawn_cmd: call $call error $err";
1256 dbg($s) if isdbg('chan');
1257 if ($fromnode && $user) {
1258 $self->send_rcmd_reply($main::mycall, $fromnode, $user, $s);
1265 # transform output if required
1266 @res = $cb->($self, @res);
1269 if ($fromnode && $user) {
1270 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @res);
1275 diffms("rcmd from $user on $call", $line, $t0, scalar @res) if isdbg('progress');
1281 sub process_rcmd_reply
1283 my ($self, $tonode, $fromnode, $user, $line) = @_;
1284 if ($tonode eq $main::mycall) {
1285 my $s = $rcmds{$fromnode};
1287 my $dxchan = DXChannel::get($s->{call});
1288 my $ref = $user eq $tonode ? $dxchan : (DXChannel::get($user) || $dxchan);
1289 $ref->send($line) if $ref;
1290 delete $rcmds{$fromnode} if !$dxchan;
1292 # send unsolicited ones to the sysop
1293 my $dxchan = DXChannel::get($main::myalias);
1294 $dxchan->send($line) if $dxchan;
1297 my $ref = DXUser::get_current($tonode);
1298 if ($ref && $ref->is_clx) {
1299 $self->route($tonode, pc85($fromnode, $tonode, $user, $line));
1301 $self->route($tonode, pc35($fromnode, $tonode, $line));
1308 # add a rcmd request to the rcmd queues
1311 my ($self, $to, $cmd) = @_;
1314 $r->{call} = $self->{call};
1315 $r->{t} = $main::systime;
1319 my $ref = Route::Node::get($to);
1320 my $dxchan = $ref->dxchan;
1321 if ($dxchan && $dxchan->is_clx) {
1322 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1324 route(undef, $to, pc34($main::mycall, $to, $cmd));
1331 my $pc39flag = shift || 0;
1332 my $call = $self->call;
1334 return if $self->{disconnecting}++;
1336 unless ($pc39flag == 1) {
1337 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1340 # get rid of any PC16/17/19
1341 eph_del_regex("^PC1[679]*$call");
1343 # do routing stuff, remove me from routing table
1344 my $node = Route::Node::get($call);
1346 Route::delete_interface($call);
1348 # unbusy and stop and outgoing mail
1349 my $mref = DXMsg::get_busy($call);
1350 $mref->stop_msg($call) if $mref;
1352 # remove outstanding pings
1353 delete $pings{$call};
1355 # I was the last node visited
1356 $self->user->node($main::mycall);
1358 # send info to all logged in thingies
1359 $self->tell_login('logoutn');
1361 Log('DXProt', $call . " Disconnected");
1363 $self->SUPER::disconnect;
1365 # here we determine what needs to go out of the routing table
1367 if ($node && $pc39flag != 2) {
1368 dbg('%Route::Node::List = ' . join(',', sort keys %Route::Node::list)) if isdbg('routedisc');
1370 @rout = $node->del($main::routeroot);
1372 dbg('@rout = ' . join(',', sort map {$_->call} @rout)) if isdbg('routedisc');
1374 # now we need to see what can't be routed anymore and came
1375 # in via this node (probably).
1377 while ($n != @rout) {
1379 for (Route::Node::get_all()) {
1380 unless ($_->dxchan) {
1381 push @rout, $_->delete;
1384 dbg('@rout = ' . join(',', sort map {$_->call} @rout)) if isdbg('routedisc');
1387 dbg('%Route::Node::List = ' . join(',', sort keys %Route::Node::list)) if isdbg('routedisc');
1389 # and all my ephemera as well
1392 eph_del_regex("^PC1[679].*$c");
1396 # broadcast to all other nodes that all the nodes connected to via me are gone
1397 unless ($pc39flag == 2) {
1398 $self->route_pc21($main::mycall, undef, @rout) if @rout;
1399 $self->route_pc92d($main::mycall, undef, $main::routeroot, $node) if $node;
1405 # send a talk message to this thingy
1409 my ($self, $from, $to, $via, $line, $origin) = @_;
1411 if ($self->{do_pc9x}) {
1412 $self->send(pc93($to, $from, $via, $line));
1414 $self->send(pc10($from, $to, $via, $line, $origin));
1416 Log('talk', $to, $from, '>' . ($via || $origin || $self->call), $line) unless $origin && $origin ne $main::mycall;
1419 # send it if it isn't the except list and isn't isolated and still has a hop count
1420 # taking into account filtering and so on
1426 my $generate = shift;
1427 my $no = shift; # the no of things to filter on
1429 my ($filter, $hops);
1432 for (; @_ && $no; $no--) {
1435 # don't send messages with $self's call in back to them
1436 if ($r->call eq $self->{call}) {
1437 dbg("PCPROT: trying to send $self->{call} back itself") if isdbg('chanerr');
1441 if (!$self->{isolate} && $self->{routefilter}) {
1444 ($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});
1448 dbg("PCPROT: $self->{call}/" . $r->call . " rejected by output filter") if isdbg('filter');
1451 dbg("was sent a null value") if isdbg('chanerr');
1454 push @rin, $r unless $self->{isolate} && $r->call ne $main::mycall;
1458 foreach my $line (&$generate(@rin, @_)) {
1461 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1463 $routeit = adjust_hops($self, $line); # adjust its hop count by node name
1464 next unless $routeit;
1467 $self->send($routeit);
1472 # broadcast everywhere
1477 my $generate = shift;
1479 my @dxchan = DXChannel::get_all_nodes();
1483 $line =~ /\^H(\d+)\^?\~?$/;
1484 return unless $1 > 0;
1486 unless ($self->{isolate}) {
1487 foreach $dxchan (@dxchan) {
1488 next if $dxchan == $self || $dxchan == $main::me;
1489 next if $origin eq $dxchan->{call}; # don't route some from this call back again.
1490 next unless $dxchan->isa('DXProt');
1492 $dxchan->send_route($origin, $generate, @_);
1497 # broadcast to non-pc9x nodes
1498 sub broadcast_route_nopc9x
1502 my $generate = shift;
1504 my @dxchan = DXChannel::get_all_nodes();
1508 $line =~ /\^H(\d+)\^?\~?$/;
1509 return unless $1 > 0;
1511 unless ($self->{isolate}) {
1512 foreach $dxchan (@dxchan) {
1513 next if $dxchan == $self || $dxchan == $main::me;
1514 next if $origin eq $dxchan->{call}; # don't route some from this call back again.
1515 next unless $dxchan->isa('DXProt');
1516 next if $dxchan->{do_pc9x};
1517 if ($generate == \&pc16 || $generate==\&pc17) {
1518 next unless $dxchan->user->wantsendpc16;
1520 $dxchan->send_route($origin, $generate, @_);
1525 # this is only used for next door nodes on init
1530 return unless $self->{do_pc9x};
1533 my $generate = shift;
1534 my $no = shift; # the no of things to filter on
1537 $line = &$generate(@_);
1541 # broadcast only to pc9x nodes
1542 sub broadcast_route_pc9x
1546 my $generate = shift;
1549 my @dxchan = DXChannel::get_all_nodes();
1552 if ($origin eq $main::mycall && $generate && !$line) {
1553 $line = &$generate(@_);
1556 $line =~ /\^H(\d+)\^\~?$/;
1557 unless ($1 > 0 && $self->{isolate}) {
1558 foreach $dxchan (@dxchan) {
1559 next if $dxchan == $self || $dxchan == $main::me;
1560 next if $origin eq $dxchan->{call}; # don't route some from this call back again.
1561 next unless $dxchan->isa('DXProt');
1562 next unless $dxchan->{do_pc9x};
1564 $dxchan->send($line);
1572 return unless $self->user->wantpc16;
1575 broadcast_route_nopc9x($self, $origin, \&pc16, $line, 1, @_);
1581 return unless $self->user->wantpc16;
1584 broadcast_route_nopc9x($self, $origin, \&pc17, $line, 1, @_);
1592 broadcast_route_nopc9x($self, $origin, \&pc19, $line, scalar @_, @_);
1600 broadcast_route_nopc9x($self, $origin, \&pc21, $line, scalar @_, @_);
1608 broadcast_route($self, $origin, \&pc24, $line, 1, @_);
1616 broadcast_route($self, $origin, \&pc41, $line, 1, @_);
1619 # this is probably obsolete now
1626 broadcast_route($self, $origin, \&pc50, $line, 1, @_);
1634 broadcast_route_pc9x($self, $origin, \&pc92c, $line, 1, @_);
1642 broadcast_route_pc9x($self, $origin, \&pc92a, $line, 1, @_);
1650 broadcast_route_pc9x($self, $origin, \&pc92d, $line, 1, @_);
1657 my ($filter, $hops) = (1, 1);
1659 if ($self->{inroutefilter}) {
1660 ($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);
1661 dbg("PCPROT: $self->{call}/" . $r->call . ' rejected by in_filter_route') if !$filter && isdbg('chanerr');
1669 my $t = shift || $eph_restime;
1673 $s =~ s/\^H\d\d?\^?\~?$//;
1674 $r = 1 if exists $eph{$s}; # pump up the dup if it keeps circulating
1675 $eph{$s} = $main::systime + $t;
1676 dbg("PCPROT: emphemeral duplicate") if $r && isdbg('chanerr');
1684 while (($key, $val) = each %eph) {
1685 if ($key =~ m{$regex}) {
1695 while (($key, $val) = each %eph) {
1696 if ($main::systime >= $val) {
1707 while (($key, $val) = each %eph) {
1708 push @out, $key, $val;
1715 goto &DXCommandmode::run_cmd;
1719 # import any msgs in the chat directory
1720 # the messages are sent to the chat group which forms the
1721 # the first part of the name (eg: solar.1243.txt would be
1722 # sent to chat group SOLAR)
1724 # Each message found is sent: one non-blank line to one chat
1725 # message. So 4 lines = 4 chat messages.
1727 # The special name LOCAL is for local users ANN
1728 # The special name ALL is for ANN/FULL
1729 # The special name SYSOP is for ANN/SYSOP
1733 # are there any to do in this directory?
1734 return unless -d $chatimportfn;
1735 unless (opendir(DIR, $chatimportfn)) {
1736 dbg("can\'t open $chatimportfn $!") if isdbg('msg');
1737 Log('msg', "can\'t open $chatimportfn $!");
1741 my @names = readdir(DIR);
1744 foreach $name (@names) {
1745 next if $name =~ /^\./;
1746 my $splitit = $name =~ /^split/;
1747 my $fn = "$chatimportfn/$name";
1749 unless (open(MSG, $fn)) {
1750 dbg("can\'t open import file $fn $!") if isdbg('msg');
1751 Log('msg', "can\'t open import file $fn $!");
1755 my @msg = map { s/\r?\n$//; $_ } <MSG>;
1759 my @cat = split /\./, $name;
1760 my $target = uc $cat[0];
1762 foreach my $text (@msg) {
1763 next unless $text && $text !~ /^\s*#/;
1764 if ($target eq 'ALL' || $target eq 'LOCAL' || $target eq 'SYSOP' || $target eq 'WX') {
1765 my $sysop = uc $target eq 'SYSOP' ? '*' : ' ';
1766 my $wx = uc $target eq 'WX' ? '1' : '0';
1768 $via = '*' if $target eq 'ALL' || $target eq 'SYSOP';
1769 Log('ann', $target, $main::mycall, $text);
1770 $main::me->normal(DXProt::pc93($target, $main::mycall, $via, $text));
1772 DXCommandmode::send_chats($main::me, $target, $text);
1778 # start a pc92 find operation
1783 my $key = "$dxchan->{call}|$target";
1784 if ($pc92_find{$key}) {
1789 # function (not method) to handle pc92 find returns
1790 sub handle_pc92_find_reply
1792 my ($dxchan, $node, $from, $target, $flag, $ms) = @_;
1794 $dxchan->print_pc92_find_reply($node, $target, $flag, $ms) if $dxchan->can('print_pc92_find_return');